-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreflection.py
More file actions
359 lines (297 loc) · 12.7 KB
/
reflection.py
File metadata and controls
359 lines (297 loc) · 12.7 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
"""
Reflection Synthesizer - Weekly progress summary from actual work artifacts.
Reads git commits, batch results, and test outcomes to provide concise progress summary.
No monitoring overhead - just synthesizes what already happened.
"""
import json
import os
import re
import subprocess
from datetime import datetime, timedelta
from pathlib import Path
from typing import Any, Dict, List, Optional
class ReflectionSynthesizer:
"""Synthesizes recent work into actionable progress summary."""
def __init__(self, root_dir: Path):
self.root_dir = Path(root_dir)
self.cortex_dir = root_dir / "cortex"
def synthesize_week(self, days: int = 7) -> Dict[str, Any]:
"""
Synthesize last N days of work into progress summary.
Returns:
Dictionary with:
- shipped: List of completed features/fixes from commits
- blocked_by: Current blockers from failed tests/batch jobs
- suggested_focus: Recommended next action
- metrics: Quantitative progress (commits, tests, batch jobs)
"""
# Gather data from multiple sources
commits = self._get_recent_commits(days)
batch_results = self._get_batch_results(days)
test_status = self._get_test_status()
goals_progress = self._get_goals_progress()
# Synthesize into insights
shipped = self._extract_shipped_work(commits)
blocked_by = self._extract_blockers(batch_results, test_status)
suggested_focus = self._suggest_focus(blocked_by, goals_progress)
return {
"period_days": days,
"shipped": shipped,
"blocked_by": blocked_by,
"suggested_focus": suggested_focus,
"metrics": {
"commits": len(commits),
"batch_jobs_completed": sum(1 for r in batch_results if r.get("success")),
"batch_jobs_failed": sum(1 for r in batch_results if not r.get("success")),
"active_projects": len(self._get_active_projects(commits)),
},
"goals_progress": goals_progress,
}
def _get_recent_commits(self, days: int) -> List[Dict[str, str]]:
"""Get commits from last N days across all branches."""
since = (datetime.now() - timedelta(days=days)).strftime("%Y-%m-%d") # noqa: DTZ005
try:
result = subprocess.run(
[
"git",
"log",
f"--since={since}",
"--all",
"--pretty=format:%H|%s|%an|%ad",
"--date=short",
],
cwd=self.root_dir,
capture_output=True,
text=True,
timeout=10,
)
if result.returncode != 0:
return []
commits = []
for line in result.stdout.strip().split("\n"):
if not line:
continue
parts = line.split("|", 3)
if len(parts) == 4:
commits.append(
{
"hash": parts[0][:8],
"message": parts[1],
"author": parts[2],
"date": parts[3],
}
)
return commits
except Exception:
return []
def _get_batch_results(self, days: int) -> List[Dict[str, Any]]:
"""Get batch job results from last N days."""
batch_dir = Path.home() / ".cortex" / "batch"
if not batch_dir.exists():
return []
cutoff = datetime.now() - timedelta(days=days) # noqa: DTZ005
results = []
# Check for result files
for result_file in batch_dir.glob("**/*_result.json"):
try:
if result_file.stat().st_mtime < cutoff.timestamp():
continue
with open(result_file) as f:
data = json.load(f)
results.append(
{
"job_id": result_file.stem.replace("_result", ""),
"success": data.get("success", False),
"error": data.get("error"),
"duration": data.get("duration_seconds"),
}
)
except Exception:
continue
return results
def _get_test_status(self) -> Dict[str, Any]:
"""Get current test status from recent test runs."""
# Check for pytest cache or recent test output
test_status = {"failing": [], "passing": None}
# Look for pytest cache
pytest_cache = self.root_dir / ".pytest_cache" / "v" / "cache" / "lastfailed"
if pytest_cache.exists():
try:
with open(pytest_cache) as f:
failures = json.load(f)
test_status["failing"] = list(failures.keys())
except Exception:
pass
return test_status
def _get_goals_progress(self) -> Dict[str, Any]:
"""Get progress on strategic goals from GOALS.md."""
goals_file = self.root_dir / "GOALS.md"
if not goals_file.exists():
return {"total": 0, "in_progress": 0, "completed": 0}
try:
content = goals_file.read_text(encoding="utf-8")
# Count goals by status
in_progress = len(re.findall(r"\[IN PROGRESS\]", content, re.IGNORECASE))
completed = len(re.findall(r"Status:.*?Complete", content, re.IGNORECASE))
# Extract goal titles
goals = []
for match in re.finditer(r"### Goal \d+: ([^\n\[]+)", content):
title = match.group(1).strip()
goals.append(title)
return {
"total": len(goals),
"in_progress": in_progress,
"completed": completed,
"goals": goals[:3], # First 3 goals
}
except Exception:
return {"total": 0, "in_progress": 0, "completed": 0}
def _extract_shipped_work(self, commits: List[Dict[str, str]]) -> List[str]:
"""Extract completed work from commit messages."""
shipped = []
# Pattern matching for meaningful commits
feature_patterns = [
r"feat\(([^)]+)\):?\s*(.+)",
r"fix\(([^)]+)\):?\s*(.+)",
r"docs\(([^)]+)\):?\s*(.+)",
]
for commit in commits:
msg = commit["message"]
for pattern in feature_patterns:
match = re.match(pattern, msg, re.IGNORECASE)
if match:
scope = match.group(1)
description = match.group(2)
shipped.append(f"{scope}: {description}")
break
# Group by project
grouped = {}
for item in shipped:
parts = item.split(":", 1)
if len(parts) == 2:
project, desc = parts
if project not in grouped:
grouped[project] = []
grouped[project].append(desc.strip())
# Format as bullet points
formatted = []
for project, items in grouped.items():
if len(items) == 1:
formatted.append(f"{project}: {items[0]}")
else:
formatted.append(f"{project}: {len(items)} changes")
return formatted[:5] # Top 5 items
def _extract_blockers(
self, batch_results: List[Dict[str, Any]], test_status: Dict[str, Any]
) -> List[str]:
"""Extract current blockers from failed jobs and tests."""
blockers = []
# Failed batch jobs
failed_jobs = [r for r in batch_results if not r.get("success")]
if failed_jobs:
for job in failed_jobs[:3]: # Top 3 failures
error = job.get("error", "Unknown error")
blockers.append(f"Batch job {job['job_id']}: {error}")
# Failed tests
failing_tests = test_status.get("failing", [])
if failing_tests:
if len(failing_tests) <= 3:
for test in failing_tests:
blockers.append(f"Test failing: {test}")
else:
blockers.append(f"{len(failing_tests)} tests failing")
return blockers
def _suggest_focus(self, blockers: List[str], goals_progress: Dict[str, Any]) -> str:
"""Suggest next focus based on blockers and goal progress."""
# Priority 1: Unblock failing work
if blockers:
return f"Unblock: {blockers[0]}"
# Priority 2: Advance in-progress goals
if goals_progress.get("in_progress", 0) > 0:
goals = goals_progress.get("goals", [])
if goals:
return f"Advance: {goals[0]}"
# Priority 3: Start next goal
if goals_progress.get("total", 0) > goals_progress.get("in_progress", 0):
return "Start next planned goal"
return "No clear blocker - continue current work"
def _get_active_projects(self, commits: List[Dict[str, str]]) -> set:
"""Extract unique projects from commits."""
projects = set()
for commit in commits:
# Try to extract project from commit message
match = re.match(r"(?:feat|fix|docs)\(([^)]+)\)", commit["message"])
if match:
projects.add(match.group(1))
return projects
def format_reflection(self, reflection: Dict[str, Any]) -> str:
"""Format reflection data for terminal display."""
lines = []
lines.append("╔══════════════════════════════════════════════════════╗")
lines.append("║ CORTEX - WEEKLY REFLECTION ║")
lines.append("╚══════════════════════════════════════════════════════╝")
lines.append("")
# Time period
period = reflection["period_days"]
lines.append(f"📅 Last {period} days")
lines.append("")
# What shipped
lines.append("🚀 SHIPPED")
lines.append("─" * 54)
shipped = reflection["shipped"]
if shipped:
for item in shipped:
lines.append(f" • {item}")
else:
lines.append(" (No notable commits)")
lines.append("")
# Blockers
lines.append("🚧 BLOCKED BY")
lines.append("─" * 54)
blockers = reflection["blocked_by"]
if blockers:
for blocker in blockers:
lines.append(f" • {blocker}")
else:
lines.append(" (No blockers)")
lines.append("")
# Suggested focus
lines.append("🎯 SUGGESTED FOCUS")
lines.append("─" * 54)
lines.append(f" {reflection['suggested_focus']}")
lines.append("")
# Metrics
metrics = reflection["metrics"]
lines.append("📊 METRICS")
lines.append("─" * 54)
lines.append(f" Commits: {metrics['commits']}")
lines.append(
f" Batch jobs: {metrics['batch_jobs_completed']} completed, {metrics['batch_jobs_failed']} failed"
)
lines.append(f" Active projects: {metrics['active_projects']}")
lines.append("")
# Goals progress
goals = reflection.get("goals_progress", {})
if goals.get("total", 0) > 0:
lines.append("🎯 GOALS PROGRESS")
lines.append("─" * 54)
lines.append(f" Total: {goals['total']}")
lines.append(f" In Progress: {goals['in_progress']}")
lines.append(f" Completed: {goals['completed']}")
if goals.get("goals"):
lines.append("")
lines.append(" Active Goals:")
for goal in goals["goals"]:
lines.append(f" • {goal}")
lines.append("")
return "\n".join(lines)
def generate_weekly_reflection(root_dir: Optional[Path] = None, days: int = 7) -> Dict[str, Any]:
"""Generate weekly reflection summary."""
if root_dir is None:
root_dir = Path(os.environ.get("CORTEX_ROOT_DIR", str(Path.cwd())))
synthesizer = ReflectionSynthesizer(root_dir)
return synthesizer.synthesize_week(days)
def format_reflection(reflection: Dict[str, Any]) -> str:
"""Format reflection for display."""
synthesizer = ReflectionSynthesizer(Path(os.environ.get("CORTEX_ROOT_DIR", str(Path.cwd()))))
return synthesizer.format_reflection(reflection)