-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjournal.py
More file actions
411 lines (355 loc) · 14.7 KB
/
journal.py
File metadata and controls
411 lines (355 loc) · 14.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
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
"""
Journaling System for Coding Agent
Tracks all activities, iterations, and provides audit trail.
"""
import json
import os
from datetime import datetime
from pathlib import Path
from typing import Optional, Dict, Any, List
import hashlib
from config import JOURNAL_DIR, LOGS_DIR
class AgentJournal:
"""
Maintains a comprehensive journal of all agent activities.
Tracks iterations, fixes, optimizations, and AI interactions.
"""
def __init__(self, session_id: Optional[str] = None):
self.session_id = session_id or self._generate_session_id()
self.session_start = datetime.now()
self.journal_file = JOURNAL_DIR / f"agent_{self.session_id}.json"
self.log_file = LOGS_DIR / f"agent_{self.session_id}.log"
self.entries: List[Dict[str, Any]] = []
self._init_journal()
def _generate_session_id(self) -> str:
"""Generate unique session ID based on timestamp."""
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
hash_suffix = hashlib.md5(str(datetime.now().timestamp()).encode()).hexdigest()[:6]
return f"{timestamp}_{hash_suffix}"
def _init_journal(self):
"""Initialize journal file with session metadata."""
metadata = {
"session_id": self.session_id,
"started_at": self.session_start.isoformat(),
"version": "1.0",
"entries": []
}
self._save_journal(metadata)
self.log("INFO", f"Session started: {self.session_id}")
def _save_journal(self, data: Dict[str, Any]):
"""Save journal data to file."""
with open(self.journal_file, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=2, ensure_ascii=False, default=str)
def _load_journal(self) -> Dict[str, Any]:
"""Load journal data from file."""
if self.journal_file.exists():
with open(self.journal_file, 'r', encoding='utf-8') as f:
return json.load(f)
return {"session_id": self.session_id, "entries": []}
def log(self, level: str, message: str):
"""Write log entry to log file."""
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
log_entry = f"[{timestamp}] [{level}] {message}\n"
with open(self.log_file, 'a', encoding='utf-8') as f:
f.write(log_entry)
def add_entry(self, entry_type: str, data: Dict[str, Any]) -> str:
"""
Add a new entry to the journal.
Args:
entry_type: Type of entry
data: Entry data
Returns:
Entry ID
"""
entry_id = hashlib.md5(
f"{datetime.now().timestamp()}{entry_type}".encode()
).hexdigest()[:12]
entry = {
"id": entry_id,
"type": entry_type,
"timestamp": datetime.now().isoformat(),
"data": data
}
journal = self._load_journal()
journal["entries"].append(entry)
self._save_journal(journal)
self.log("INFO", f"Journal: {entry_type} - {entry_id}")
return entry_id
# Iteration tracking
def record_iteration_start(self, iteration_number: int,
target: str, mode: str) -> str:
"""Record the start of an iteration."""
return self.add_entry("iteration_start", {
"iteration": iteration_number,
"target": target,
"mode": mode
})
def record_iteration_end(self, iteration_number: int,
success: bool, duration: float,
summary: Dict[str, Any]) -> str:
"""Record the end of an iteration."""
return self.add_entry("iteration_end", {
"iteration": iteration_number,
"success": success,
"duration_seconds": duration,
"summary": summary
})
# Analysis tracking
def record_analysis(self, target: str, issues_found: List[Dict]) -> str:
"""Record code analysis results."""
return self.add_entry("analysis", {
"target": target,
"issues_count": len(issues_found),
"issues": issues_found
})
# Fix tracking
def record_fix_attempt(self, file_path: str, issue: str,
ai_provider: str, success: bool,
original_code: str = "",
fixed_code: str = "") -> str:
"""Record a fix attempt."""
return self.add_entry("fix_attempt", {
"file": file_path,
"issue": issue,
"provider": ai_provider,
"success": success,
"code_diff_lines": len(fixed_code.split('\n')) - len(original_code.split('\n'))
})
# Test tracking
def record_test_run(self, command: str, result: Dict[str, Any]) -> str:
"""Record test execution."""
return self.add_entry("test_run", {
"command": command,
"success": result.get("success", False),
"passed": result.get("passed", 0),
"failed": result.get("failed", 0),
"duration": result.get("duration_seconds", 0)
})
# Build tracking
def record_build(self, command: str, success: bool,
errors: List[str], warnings: List[str]) -> str:
"""Record build execution."""
return self.add_entry("build", {
"command": command,
"success": success,
"errors_count": len(errors),
"warnings_count": len(warnings),
"errors": errors[:10], # Limit to first 10
"warnings": warnings[:10]
})
# Optimization tracking
def record_optimization(self, file_path: str,
improvements: List[str],
metrics_before: Dict[str, Any] = None,
metrics_after: Dict[str, Any] = None) -> str:
"""Record code optimization."""
return self.add_entry("optimization", {
"file": file_path,
"improvements": improvements,
"metrics_before": metrics_before or {},
"metrics_after": metrics_after or {}
})
# AI interaction tracking
def record_ai_interaction(self, provider: str, model: str,
prompt_type: str, tokens_used: int,
latency_ms: float, success: bool) -> str:
"""Record AI provider interaction."""
return self.add_entry("ai_interaction", {
"provider": provider,
"model": model,
"prompt_type": prompt_type,
"tokens_used": tokens_used,
"latency_ms": latency_ms,
"success": success
})
# Multi-agent tracking
def record_agent_task(self, agent_id: str, task_id: str,
role: str, provider: str,
status: str, result: Dict[str, Any] = None) -> str:
"""Record multi-agent task execution."""
return self.add_entry("agent_task", {
"agent_id": agent_id,
"task_id": task_id,
"role": role,
"provider": provider,
"status": status,
"result_summary": str(result)[:500] if result else None
})
def record_consensus(self, issue: str, agents_count: int,
agreed: bool, confidence: float) -> str:
"""Record consensus voting result."""
return self.add_entry("consensus", {
"issue": issue,
"agents_participated": agents_count,
"consensus_reached": agreed,
"confidence": confidence
})
# Git tracking
def record_git_action(self, action: str, details: Dict[str, Any]) -> str:
"""Record git operations."""
return self.add_entry("git_action", {
"action": action,
**details
})
# Error tracking
def record_error(self, context: str, error: str,
details: Optional[Dict] = None) -> str:
"""Record an error."""
self.log("ERROR", f"{context}: {error}")
return self.add_entry("error", {
"context": context,
"error": error,
"details": details or {}
})
# Session management
def get_session_summary(self) -> Dict[str, Any]:
"""Get summary of current session."""
journal = self._load_journal()
entries = journal.get("entries", [])
iterations = [e for e in entries if e["type"] == "iteration_end"]
fixes = [e for e in entries if e["type"] == "fix_attempt"]
tests = [e for e in entries if e["type"] == "test_run"]
ai_calls = [e for e in entries if e["type"] == "ai_interaction"]
errors = [e for e in entries if e["type"] == "error"]
successful_fixes = [f for f in fixes if f["data"].get("success")]
successful_tests = [t for t in tests if t["data"].get("success")]
total_tokens = sum(
e["data"].get("tokens_used", 0) for e in ai_calls
)
total_latency = sum(
e["data"].get("latency_ms", 0) for e in ai_calls
)
# Calculate final status
final_success = (
iterations[-1]["data"]["success"]
if iterations else False
)
return {
"session_id": self.session_id,
"duration": str(datetime.now() - self.session_start),
"total_iterations": len(iterations),
"total_fixes_attempted": len(fixes),
"successful_fixes": len(successful_fixes),
"total_test_runs": len(tests),
"successful_test_runs": len(successful_tests),
"total_ai_calls": len(ai_calls),
"total_tokens_used": total_tokens,
"total_ai_latency_ms": total_latency,
"errors_count": len(errors),
"final_success": final_success,
"journal_file": str(self.journal_file),
"log_file": str(self.log_file)
}
def close_session(self) -> Dict[str, Any]:
"""Close the session and finalize journal."""
summary = self.get_session_summary()
self.add_entry("session_end", summary)
self.log("INFO", f"Session ended: {self.session_id}")
return summary
def export_report(self, format: str = "md") -> str:
"""
Export session as a readable report.
Args:
format: Output format (md, txt, json)
Returns:
Path to exported file
"""
summary = self.get_session_summary()
journal = self._load_journal()
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"report_{self.session_id}_{timestamp}.{format}"
filepath = JOURNAL_DIR / filename
if format == "md":
content = self._generate_markdown_report(summary, journal)
elif format == "txt":
content = self._generate_text_report(summary, journal)
else:
content = json.dumps({
"summary": summary,
"journal": journal
}, indent=2, default=str)
with open(filepath, 'w') as f:
f.write(content)
return str(filepath)
def _generate_markdown_report(self, summary: Dict, journal: Dict) -> str:
"""Generate markdown report."""
lines = [
f"# Coding Agent Session Report",
f"",
f"**Session ID:** {summary['session_id']}",
f"**Duration:** {summary['duration']}",
f"**Final Status:** {'Success' if summary['final_success'] else 'Failed'}",
f"",
f"## Summary",
f"",
f"| Metric | Value |",
f"|--------|-------|",
f"| Iterations | {summary['total_iterations']} |",
f"| Fixes Attempted | {summary['total_fixes_attempted']} |",
f"| Successful Fixes | {summary['successful_fixes']} |",
f"| Test Runs | {summary['total_test_runs']} |",
f"| Successful Tests | {summary['successful_test_runs']} |",
f"| AI Calls | {summary['total_ai_calls']} |",
f"| Tokens Used | {summary['total_tokens_used']} |",
f"| Errors | {summary['errors_count']} |",
f"",
f"## Timeline",
f""
]
for entry in journal.get("entries", []):
entry_type = entry["type"]
timestamp = entry["timestamp"]
data = entry["data"]
if entry_type == "iteration_start":
lines.append(f"### Iteration {data['iteration']}")
lines.append(f"*Started at {timestamp}*")
lines.append(f"")
elif entry_type == "fix_attempt":
status = "Fixed" if data["success"] else "Failed"
lines.append(f"- [{status}] {data['file']}: {data['issue'][:50]}...")
lines.append(f" - Provider: {data['provider']}")
elif entry_type == "test_run":
status = "Passed" if data["success"] else "Failed"
lines.append(f"- Tests: {status} ({data['passed']} passed, {data['failed']} failed)")
elif entry_type == "error":
lines.append(f"- **Error:** {data['context']}: {data['error'][:100]}")
return "\n".join(lines)
def _generate_text_report(self, summary: Dict, journal: Dict) -> str:
"""Generate plain text report."""
lines = [
"=" * 60,
"CODING AGENT SESSION REPORT",
"=" * 60,
"",
f"Session ID: {summary['session_id']}",
f"Duration: {summary['duration']}",
f"Status: {'SUCCESS' if summary['final_success'] else 'FAILED'}",
"",
"-" * 60,
"SUMMARY",
"-" * 60,
f"Iterations: {summary['total_iterations']}",
f"Fixes Attempted: {summary['total_fixes_attempted']}",
f"Successful Fixes: {summary['successful_fixes']}",
f"Test Runs: {summary['total_test_runs']}",
f"AI Calls: {summary['total_ai_calls']}",
f"Tokens Used: {summary['total_tokens_used']}",
f"Errors: {summary['errors_count']}",
"",
]
return "\n".join(lines)
# Global journal instance (created per session)
_current_journal: Optional[AgentJournal] = None
def get_journal() -> AgentJournal:
"""Get or create the current journal."""
global _current_journal
if _current_journal is None:
_current_journal = AgentJournal()
return _current_journal
def new_session() -> AgentJournal:
"""Start a new journal session."""
global _current_journal
if _current_journal:
_current_journal.close_session()
_current_journal = AgentJournal()
return _current_journal