-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathportfolio_memory.py
More file actions
733 lines (601 loc) · 25.3 KB
/
portfolio_memory.py
File metadata and controls
733 lines (601 loc) · 25.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
import os
"""
Portfolio Memory - Access cross-project patterns, lessons, and metadata
Reads from ~/.claude/portfolio/project_index.json to provide:
- Project statistics and metadata
- Cross-project patterns
- Lessons learned from past work
- Project context for intelligence queries
- Project health tracking and trends
"""
import json
import logging
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List, Optional
logger = logging.getLogger(__name__)
class PortfolioMemory:
"""Access portfolio-wide patterns, lessons, and project metadata"""
def __init__(self, portfolio_path: Path = Path.home() / ".claude" / "portfolio"):
self.portfolio_path = portfolio_path
self.index_file = portfolio_path / "project_index.json"
self.portfolio_data = self._load_portfolio()
# Lazy load health tracker
self._health_tracker = None
def _load_portfolio(self) -> Dict[str, Any]:
"""Load portfolio data from project_index.json"""
if not self.index_file.exists():
return {
"meta": {
"last_updated": datetime.now().isoformat(),
"total_projects": 0,
"total_specs": 0,
},
"projects": {},
}
with open(self.index_file, "r") as f:
return json.load(f)
def _get_health_tracker(self):
"""Lazy load HealthTracker to avoid import errors"""
if self._health_tracker is None:
try:
# Try absolute import first
from cortex.agents.data_agent.analyzers.health_tracker import (
HealthTracker,
)
self._health_tracker = HealthTracker()
except ImportError:
try:
# Try relative import
from agents.data_agent.analyzers.health_tracker import HealthTracker
self._health_tracker = HealthTracker()
except ImportError:
# HealthTracker not available
pass
return self._health_tracker
def get_stats(self, include_health: bool = True) -> Dict[str, Any]:
"""
Get portfolio statistics
Args:
include_health: Include health summary (default: True)
Returns:
Portfolio statistics with optional health data
"""
projects = self.portfolio_data.get("projects", {})
# Calculate stats
total_projects = len(projects)
tier1_count = sum(1 for p in projects.values() if p.get("priority") == "tier1")
tier2_count = sum(1 for p in projects.values() if p.get("priority") == "tier2")
tier3_count = sum(1 for p in projects.values() if p.get("priority") == "tier3")
# Active projects (commits in last 7 days)
active_projects = [
name for name, data in projects.items() if data.get("activity_commits_7d", 0) > 0
]
# Tech stack distribution
all_tech = []
for proj_data in projects.values():
all_tech.extend(proj_data.get("tech_stack", []))
from collections import Counter
tech_counts = Counter(all_tech)
top_tech = dict(tech_counts.most_common(10))
# Pattern distribution
all_patterns = []
for proj_data in projects.values():
all_patterns.extend(proj_data.get("common_patterns", []))
pattern_counts = Counter(all_patterns)
top_patterns = dict(pattern_counts.most_common(10))
stats = {
"total_projects": total_projects,
"by_priority": {
"tier1": tier1_count,
"tier2": tier2_count,
"tier3": tier3_count,
},
"active_projects": len(active_projects),
"active_project_names": active_projects[:10], # Top 10
"top_technologies": top_tech,
"top_patterns": top_patterns,
"last_updated": self.portfolio_data.get("meta", {}).get("last_updated", "Unknown"),
}
# Add health summary if requested
if include_health:
health_summary = self.get_portfolio_health_summary(days=7)
if "error" not in health_summary:
stats["health"] = {
"healthy_count": len(health_summary["aggregate"]["healthy_projects"]),
"at_risk_count": len(health_summary["aggregate"]["at_risk_projects"]),
"critical_count": len(health_summary["aggregate"]["critical_projects"]),
"projects": health_summary["projects"],
}
return stats
def get_cross_project_patterns(
self, pattern_type: Optional[str] = None
) -> List[Dict[str, Any]]:
"""
Get cross-project patterns
Args:
pattern_type: Optional filter (e.g., 'async_fastapi_routes', 'sqlalchemy_2.0_queries')
Returns:
List of pattern dictionaries with usage examples
"""
projects = self.portfolio_data.get("projects", {})
# Collect all patterns with their project usage
pattern_usage = {}
for project_name, project_data in projects.items():
patterns = project_data.get("common_patterns", [])
for pattern in patterns:
if pattern_type and pattern != pattern_type:
continue
if pattern not in pattern_usage:
pattern_usage[pattern] = {
"pattern": pattern,
"used_in": [],
"count": 0,
}
pattern_usage[pattern]["used_in"].append(
{
"project": project_name,
"priority": project_data.get("priority", "tier3"),
"path": project_data.get("path", ""),
}
)
pattern_usage[pattern]["count"] += 1
# Sort by usage count (most common first)
patterns_list = sorted(pattern_usage.values(), key=lambda x: x["count"], reverse=True)
return patterns_list
def get_lessons_learned(
self, project: Optional[str] = None, pattern: Optional[str] = None
) -> List[Dict[str, Any]]:
"""
Get lessons learned from past work
Args:
project: Optional project filter
pattern: Optional pattern filter (e.g., 'async', 'sqlalchemy')
Returns:
List of lesson dictionaries
"""
projects = self.portfolio_data.get("projects", {})
lessons = []
for project_name, project_data in projects.items():
# Filter by project if specified
if project and project_name != project:
continue
# Extract issues as lessons
issues = project_data.get("common_issues", [])
for issue in issues:
# Filter by pattern if specified
if pattern and pattern.lower() not in issue.lower():
continue
lessons.append(
{
"lesson": issue,
"project": project_name,
"priority": project_data.get("priority", "tier3"),
"source": "common_issues",
}
)
# Extract from related_projects context (migration lessons)
related = project_data.get("related_projects", [])
if related and "migration" in str(project_data.get("tech_stack", [])).lower():
lessons.append(
{
"lesson": f"Project shares context with: {', '.join(related)}",
"project": project_name,
"priority": project_data.get("priority", "tier3"),
"source": "related_projects",
}
)
return lessons
def store_deep_analysis(
self,
project: str,
tech_stack: Dict[str, Any],
architecture: Dict[str, Any],
code_quality: Dict[str, Any],
) -> bool:
"""
Store deep analysis results in portfolio memory.
Args:
project: Project name
tech_stack: Tech stack analysis results
architecture: Architecture analysis results
code_quality: Code quality analysis results
Returns:
True if stored successfully
"""
try:
projects = self.portfolio_data.get("projects", {})
if project not in projects:
projects[project] = {}
# Store analysis results
projects[project]["deep_analysis"] = {
"tech_stack": tech_stack,
"architecture": architecture,
"code_quality": code_quality,
"analyzed_at": datetime.now().isoformat(),
}
# Update tech stack in project data
if tech_stack.get("languages"):
projects[project]["tech_stack"] = tech_stack.get("languages", [])
# Save updated portfolio
self.portfolio_data["projects"] = projects
self.portfolio_data["meta"]["last_updated"] = datetime.now().isoformat()
# Save to file
self.index_file.parent.mkdir(parents=True, exist_ok=True)
with open(self.index_file, "w") as f:
json.dump(self.portfolio_data, f, indent=2)
return True
except Exception as e:
logger.error(f"Failed to store deep analysis for {project}: {e}")
return False
def store_warnings(self, project: str, warnings: List[Dict[str, Any]]) -> bool:
"""
Store warnings in portfolio memory.
Args:
project: Project name
warnings: List of warning dictionaries
Returns:
True if stored successfully
"""
try:
projects = self.portfolio_data.get("projects", {})
if project not in projects:
projects[project] = {}
# Store warnings
projects[project]["warnings"] = warnings
projects[project]["warnings_updated_at"] = datetime.now().isoformat()
# Save updated portfolio
self.portfolio_data["projects"] = projects
self.portfolio_data["meta"]["last_updated"] = datetime.now().isoformat()
# Save to file
self.index_file.parent.mkdir(parents=True, exist_ok=True)
with open(self.index_file, "w") as f:
json.dump(self.portfolio_data, f, indent=2)
return True
except Exception as e:
logger.error(f"Failed to store warnings for {project}: {e}")
return False
def get_warnings(
self, project: Optional[str] = None, severity: Optional[str] = None
) -> List[Dict[str, Any]]:
"""
Get warnings for project(s).
Args:
project: Optional project filter
severity: Optional severity filter (critical, high, medium, low)
Returns:
List of warnings
"""
projects = self.portfolio_data.get("projects", {})
all_warnings = []
for project_name, project_data in projects.items():
if project and project_name != project:
continue
warnings = project_data.get("warnings", [])
for warning in warnings:
if severity and warning.get("severity") != severity:
continue
all_warnings.append(warning)
# Sort by severity and creation time
severity_order = {"critical": 0, "high": 1, "medium": 2, "low": 3}
all_warnings.sort(
key=lambda w: (
severity_order.get(w.get("severity", "low"), 3),
w.get("created_at", ""),
)
)
return all_warnings
def get_project_context(self, project: str, include_health: bool = True) -> Dict[str, Any]:
"""
Get detailed context for a specific project
Args:
project: Project name (e.g., 'vortex-backend', 'cortex')
include_health: Include health score data (default: True)
Returns:
Project context dictionary
"""
projects = self.portfolio_data.get("projects", {})
if project not in projects:
# Try case-insensitive match
project_lower = project.lower()
for proj_name in projects.keys():
if proj_name.lower() == project_lower:
project = proj_name
break
else:
return {
"error": f"Project '{project}' not found in portfolio",
"available_projects": list(projects.keys())[:10],
}
project_data = projects[project]
# Enrich with cross-project context
patterns = project_data.get("common_patterns", [])
pattern_context = []
for pattern in patterns:
cross_project_patterns = self.get_cross_project_patterns(pattern)
if cross_project_patterns:
pattern_context.append(
{
"pattern": pattern,
"also_used_in": [
p["project"]
for p in cross_project_patterns[0]["used_in"]
if p["project"] != project
][
:5
], # Top 5 other projects
}
)
context = {
"project": project,
"path": project_data.get("path", ""),
"priority": project_data.get("priority", "tier3"),
"activity_7d": project_data.get("activity_commits_7d", 0),
"tech_stack": project_data.get("tech_stack", []),
"patterns": pattern_context,
"common_issues": project_data.get("common_issues", []),
"related_projects": project_data.get("related_projects", []),
}
# Add health data if requested
if include_health:
# Use Dev directory as git repo root
health_data = self._get_health_for_project(project, days=7)
if health_data:
context["health"] = health_data
return context
def search_projects(self, query: str) -> List[str]:
"""
Search for projects by name or technology
Args:
query: Search query (project name or tech)
Returns:
List of matching project names
"""
projects = self.portfolio_data.get("projects", {})
query_lower = query.lower()
matches = []
for project_name, project_data in projects.items():
# Match on project name
if query_lower in project_name.lower():
matches.append(project_name)
continue
# Match on tech stack
tech_stack = project_data.get("tech_stack", [])
if any(query_lower in tech.lower() for tech in tech_stack):
matches.append(project_name)
continue
# Match on patterns
patterns = project_data.get("common_patterns", [])
if any(query_lower in pattern.lower() for pattern in patterns):
matches.append(project_name)
return matches
def _get_health_for_project(self, project_name: str, days: int = 7) -> Optional[Dict[str, Any]]:
"""
Internal method to get health data for a project
Since all projects are in the Dev git repo, we analyze the entire repo.
This is a helper for get_project_context.
Args:
project_name: Project name
days: Days to analyze
Returns:
Dict with score, assessment, trend, commits_7d, uncommitted_files or None
"""
tracker = self._get_health_tracker()
if not tracker:
return None
# Use Dev directory as the git root
dev_path = Path(os.environ.get("CORTEX_ROOT_DIR", str(Path.cwd())))
if not (dev_path / ".git").exists():
return None
try:
result = tracker.get_cached_health("Dev", dev_path, days=days)
# Extract health metrics from nested structure
health_section = result.get("health", {})
commits_section = result.get("commits", {})
uncommitted_section = result.get("uncommitted", {})
return {
"score": health_section.get("total_score", 0),
"assessment": health_section.get("assessment", "unknown"),
"trend": commits_section.get("trend", "unknown"),
"commits_7d": commits_section.get("count", 0),
"uncommitted_files": uncommitted_section.get("total", 0),
}
except Exception:
return None
def get_project_health(
self, project_name: str, days: int = 7, force_refresh: bool = False
) -> Dict[str, Any]:
"""
Get health score and analysis for a specific project
Args:
project_name: Project name (e.g., 'vortex-backend', 'cortex')
days: Days to analyze (default: 7)
force_refresh: Force cache refresh
Returns:
Dict with health score, assessment, recommendations
Example:
>>> pm = PortfolioMemory()
>>> health = pm.get_project_health("cortex")
>>> print(health["score"])
"""
tracker = self._get_health_tracker()
if not tracker:
return {"error": "HealthTracker not available", "project": project_name}
projects = self.portfolio_data.get("projects", {})
# Find project (case-insensitive)
actual_name = project_name
for proj_name in projects.keys():
if proj_name.lower() == project_name.lower():
actual_name = proj_name
break
# Use Dev directory as git root (all projects are in one repo)
dev_path = Path(os.environ.get("CORTEX_ROOT_DIR", str(Path.cwd())))
if not (dev_path / ".git").exists():
return {"error": "Git repository not found", "project": actual_name}
try:
result = tracker.get_cached_health(
"Dev", dev_path, days=days, force_refresh=force_refresh
)
# Extract and flatten health data
health_section = result.get("health", {})
commits_section = result.get("commits", {})
uncommitted_section = result.get("uncommitted", {})
return {
"project": actual_name,
"score": health_section.get("total_score", 0),
"assessment": health_section.get("assessment", "unknown"),
"breakdown": health_section.get("breakdown", {}),
"trend": commits_section.get("trend", "unknown"),
"commits_7d": commits_section.get("count", 0),
"uncommitted_files": uncommitted_section.get("total", 0),
"analysis_period_days": days,
"from_cache": result.get("from_cache", False),
}
except Exception as e:
return {"error": str(e), "project": actual_name}
def get_portfolio_health_summary(self, days: int = 7) -> Dict[str, Any]:
"""
Get health summary for all projects in portfolio
Args:
days: Days to analyze (default: 7)
Returns:
Dict with health scores for all projects
Example:
>>> pm = PortfolioMemory()
>>> summary = pm.get_portfolio_health_summary()
>>> for project, health in summary["projects"].items():
... print(f"{project}: {health['score']}")
"""
tracker = self._get_health_tracker()
if not tracker:
return {"error": "HealthTracker not available"}
projects = self.portfolio_data.get("projects", {})
# Get health for Dev repo (contains all projects)
dev_path = Path(os.environ.get("CORTEX_ROOT_DIR", str(Path.cwd())))
if not (dev_path / ".git").exists():
return {"error": "Git repository not found"}
try:
result = tracker.get_cached_health("Dev", dev_path, days=days)
health_section = result.get("health", {})
commits_section = result.get("commits", {})
uncommitted_section = result.get("uncommitted", {})
score = health_section.get("total_score", 0)
assessment = health_section.get("assessment", "unknown")
trend = commits_section.get("trend", "unknown")
commits = commits_section.get("count", 0)
uncommitted = uncommitted_section.get("total", 0)
except Exception as e:
return {"error": str(e)}
summary = {
"timestamp": datetime.now().isoformat(),
"total_projects": len(projects),
"analysis_period_days": days,
"projects": {},
"aggregate": {
"healthy_projects": [],
"at_risk_projects": [],
"critical_projects": [],
},
"overall": {
"score": score,
"assessment": assessment,
"trend": trend,
"commits": commits,
"uncommitted": uncommitted,
},
}
# Apply same health metrics to all projects (since they're in same repo)
for project_name in projects.keys():
summary["projects"][project_name] = {
"score": score,
"assessment": assessment,
"trend": trend,
"commits": commits,
"uncommitted": uncommitted,
}
# Categorize by health
if score >= 70:
summary["aggregate"]["healthy_projects"].append(project_name)
elif score >= 50:
summary["aggregate"]["at_risk_projects"].append(project_name)
else:
summary["aggregate"]["critical_projects"].append(project_name)
return summary
def get_project_health_trends(self, project_name: str) -> Dict[str, Any]:
"""
Get comprehensive health trends for a project
Args:
project_name: Project name
Returns:
Dict with trends, insights, recommendations
Example:
>>> pm = PortfolioMemory()
>>> trends = pm.get_project_health_trends("cortex")
>>> print(trends["insights"])
"""
tracker = self._get_health_tracker()
if not tracker:
return {"error": "HealthTracker not available", "project": project_name}
projects = self.portfolio_data.get("projects", {})
# Find project (case-insensitive)
project_data = None
actual_name = project_name
for proj_name, data in projects.items():
if proj_name.lower() == project_name.lower():
project_data = data
actual_name = proj_name
break
if not project_data:
return {"error": f"Project '{project_name}' not found in portfolio"}
project_path = Path(project_data.get("path", ""))
if not project_path.exists():
return {"error": f"Project path not found: {project_path}"}
try:
trends = tracker.get_health_trends(actual_name, project_path)
return trends
except Exception as e:
return {"error": str(e), "project": actual_name}
# CLI helper for testing
if __name__ == "__main__":
import sys
pm = PortfolioMemory()
if len(sys.argv) < 2:
print("Usage: python portfolio_memory.py <command> [args]")
print("Commands: stats, patterns, lessons, project <name>, search <query>")
sys.exit(1)
command = sys.argv[1]
if command == "stats":
import json
print(json.dumps(pm.get_stats(), indent=2))
elif command == "patterns":
pattern_type = sys.argv[2] if len(sys.argv) > 2 else None
patterns = pm.get_cross_project_patterns(pattern_type)
for p in patterns:
print(f"\n{p['pattern']} (used in {p['count']} projects)")
for usage in p["used_in"][:3]:
print(f" - {usage['project']} ({usage['priority']})")
elif command == "lessons":
project = sys.argv[2] if len(sys.argv) > 2 else None
lessons = pm.get_lessons_learned(project=project)
for lesson in lessons:
print(f"\n[{lesson['project']}] {lesson['lesson']}")
elif command == "project":
if len(sys.argv) < 3:
print("Usage: python portfolio_memory.py project <name>")
sys.exit(1)
project_name = sys.argv[2]
context = pm.get_project_context(project_name)
import json
print(json.dumps(context, indent=2))
elif command == "search":
if len(sys.argv) < 3:
print("Usage: python portfolio_memory.py search <query>")
sys.exit(1)
query = sys.argv[2]
matches = pm.search_projects(query)
print(f"Found {len(matches)} projects matching '{query}':")
for match in matches:
print(f" - {match}")
else:
print(f"Unknown command: {command}")
sys.exit(1)