robert-github-agent 0.1.0b1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. robert_agent/__init__.py +1 -0
  2. robert_agent/acceptance.py +394 -0
  3. robert_agent/acceptance_suite.py +287 -0
  4. robert_agent/audit_result.py +501 -0
  5. robert_agent/authorize.py +117 -0
  6. robert_agent/board.py +538 -0
  7. robert_agent/chat_status.py +338 -0
  8. robert_agent/cli/__init__.py +1 -0
  9. robert_agent/cli/exit_codes.py +5 -0
  10. robert_agent/cli/main.py +535 -0
  11. robert_agent/cli/output.py +20 -0
  12. robert_agent/common.py +6 -0
  13. robert_agent/controlled_e2e_acceptance.py +394 -0
  14. robert_agent/daemon.py +607 -0
  15. robert_agent/daemon_state.py +319 -0
  16. robert_agent/diagnostics.py +119 -0
  17. robert_agent/discover.py +775 -0
  18. robert_agent/dispatch.py +217 -0
  19. robert_agent/doctor.py +94 -0
  20. robert_agent/github_api.py +12 -0
  21. robert_agent/init_config.py +126 -0
  22. robert_agent/integrations/__init__.py +1 -0
  23. robert_agent/integrations/openclaw.py +285 -0
  24. robert_agent/live_discovery_acceptance.py +70 -0
  25. robert_agent/live_worker_acceptance.py +363 -0
  26. robert_agent/live_worktree_acceptance.py +344 -0
  27. robert_agent/loop_engine.py +285 -0
  28. robert_agent/memory_curator.py +105 -0
  29. robert_agent/migrate.py +157 -0
  30. robert_agent/notify.py +74 -0
  31. robert_agent/paths.py +16 -0
  32. robert_agent/production_write_canary.py +254 -0
  33. robert_agent/project_memory.py +503 -0
  34. robert_agent/publish.py +743 -0
  35. robert_agent/publish_dedupe_acceptance.py +270 -0
  36. robert_agent/redaction.py +41 -0
  37. robert_agent/render_prompt.py +544 -0
  38. robert_agent/resource_files.py +8 -0
  39. robert_agent/resources/__init__.py +1 -0
  40. robert_agent/resources/acceptance-matrix.md +120 -0
  41. robert_agent/resources/classifier.md +19 -0
  42. robert_agent/resources/command-matrix.md +38 -0
  43. robert_agent/resources/config.example.yml +20 -0
  44. robert_agent/resources/db/migrations/001_legacy_dd_to_robert.sql +14 -0
  45. robert_agent/resources/db/schema.sql +479 -0
  46. robert_agent/resources/failure-modes.md +36 -0
  47. robert_agent/resources/fixtures/actor-permission.json +1 -0
  48. robert_agent/resources/fixtures/discovery.json +1 -0
  49. robert_agent/resources/fixtures/redaction.json +1 -0
  50. robert_agent/resources/fixtures/review-followup.json +1 -0
  51. robert_agent/resources/fixtures/worker-results.json +1 -0
  52. robert_agent/resources/fixtures/workstreams.json +1 -0
  53. robert_agent/resources/github-events.md +77 -0
  54. robert_agent/resources/kanban-control.md +165 -0
  55. robert_agent/resources/redaction.md +29 -0
  56. robert_agent/resources/result-contract.md +56 -0
  57. robert_agent/resources/routes.yml +82 -0
  58. robert_agent/resources/schemas/config.schema.json +123 -0
  59. robert_agent/resources/schemas/event.schema.json +18 -0
  60. robert_agent/resources/schemas/result.schema.json +99 -0
  61. robert_agent/resources/schemas/route.schema.json +23 -0
  62. robert_agent/resources/schemas/snapshot.schema.json +17 -0
  63. robert_agent/resources/schemas/task.schema.json +15 -0
  64. robert_agent/resources/trust-model.md +16 -0
  65. robert_agent/resources/worker-prompt.md +121 -0
  66. robert_agent/resources/worker-protocol.md +47 -0
  67. robert_agent/resources/workflow.yml +50 -0
  68. robert_agent/resources/workstream-model.md +17 -0
  69. robert_agent/route.py +232 -0
  70. robert_agent/route_config.py +64 -0
  71. robert_agent/run_once.py +5375 -0
  72. robert_agent/runtime_knowledge.py +392 -0
  73. robert_agent/service.py +161 -0
  74. robert_agent/skills.py +28 -0
  75. robert_agent/status.py +1051 -0
  76. robert_agent/storage.py +376 -0
  77. robert_agent/summarize.py +100 -0
  78. robert_agent/supervise.py +72 -0
  79. robert_agent/usage.py +192 -0
  80. robert_agent/validate_config.py +616 -0
  81. robert_agent/wakeup.py +261 -0
  82. robert_agent/web.py +2893 -0
  83. robert_agent/web_assets/board.css +255 -0
  84. robert_agent/web_assets/board.html +165 -0
  85. robert_agent/web_assets/board.js +660 -0
  86. robert_agent/web_assets/github-shell.css +122 -0
  87. robert_agent/web_assets/index.html +103 -0
  88. robert_agent/web_assets/operations.css +168 -0
  89. robert_agent/web_assets/workbench.css +131 -0
  90. robert_agent/web_assets/workbench.js +581 -0
  91. robert_agent/work_items.py +1016 -0
  92. robert_agent/workbench.py +701 -0
  93. robert_agent/worker/__init__.py +1 -0
  94. robert_agent/worker/heartbeat.py +190 -0
  95. robert_agent/worker/result.py +321 -0
  96. robert_agent/worker/snapshot.py +98 -0
  97. robert_agent/worker_adapters/__init__.py +138 -0
  98. robert_agent/worker_adapters/cbc.py +68 -0
  99. robert_agent/worker_adapters/codex.py +56 -0
  100. robert_agent/worker_adapters/command.py +33 -0
  101. robert_agent/worker_adapters/tcodex.py +56 -0
  102. robert_agent/worker_heartbeat.py +6 -0
  103. robert_agent/worker_result.py +6 -0
  104. robert_agent/worker_snapshot.py +6 -0
  105. robert_agent/workstream.py +55 -0
  106. robert_agent/worktree.py +297 -0
  107. robert_github_agent-0.1.0b1.dist-info/METADATA +217 -0
  108. robert_github_agent-0.1.0b1.dist-info/RECORD +111 -0
  109. robert_github_agent-0.1.0b1.dist-info/WHEEL +4 -0
  110. robert_github_agent-0.1.0b1.dist-info/entry_points.txt +2 -0
  111. robert_github_agent-0.1.0b1.dist-info/licenses/LICENSE +201 -0
robert_agent/status.py ADDED
@@ -0,0 +1,1051 @@
1
+ #!/usr/bin/env python3
2
+ import argparse
3
+ from collections import Counter
4
+ from datetime import datetime, timezone
5
+ import json
6
+ import os
7
+ from pathlib import Path
8
+ import sqlite3
9
+ import subprocess
10
+ from contextlib import closing
11
+
12
+ from robert_agent.common import emit
13
+ from robert_agent import web
14
+
15
+
16
+ DEFAULT_DB = "~/.local/share/robert/dd.sqlite3"
17
+
18
+ REPO_STEP_ORDER = [
19
+ "acquire_lease",
20
+ "supervise",
21
+ "audit_results",
22
+ "publish_actions",
23
+ "reconcile",
24
+ "discover",
25
+ "authorize",
26
+ "route",
27
+ ]
28
+
29
+
30
+ def _decode_json(value, default=None):
31
+ if not value:
32
+ return default
33
+ try:
34
+ return json.loads(value)
35
+ except json.JSONDecodeError:
36
+ return default
37
+
38
+
39
+ def _row_dict(row):
40
+ return dict(row) if row else None
41
+
42
+
43
+ def _table_exists(conn, name):
44
+ return conn.execute(
45
+ "SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?",
46
+ (name,),
47
+ ).fetchone() is not None
48
+
49
+
50
+ def _checked_at_utc():
51
+ return datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
52
+
53
+
54
+ def _short_text(value, max_chars=240):
55
+ if value is None:
56
+ return None
57
+ text = str(value)
58
+ if len(text) <= max_chars:
59
+ return text
60
+ return f"{text[:max_chars]}...[truncated {len(text) - max_chars} chars]"
61
+
62
+
63
+ def _compact_json(value, max_items=8, max_chars=240):
64
+ if isinstance(value, dict):
65
+ items = list(value.items())
66
+ compact = {
67
+ key: _compact_json(item, max_items=max_items, max_chars=max_chars)
68
+ for key, item in items[:max_items]
69
+ }
70
+ if len(items) > max_items:
71
+ compact["_truncated_keys"] = len(items) - max_items
72
+ return compact
73
+ if isinstance(value, list):
74
+ compact = [
75
+ _compact_json(item, max_items=max_items, max_chars=max_chars)
76
+ for item in value[:max_items]
77
+ ]
78
+ if len(value) > max_items:
79
+ compact.append({"_truncated_items": len(value) - max_items})
80
+ return compact
81
+ if isinstance(value, str):
82
+ return _short_text(value, max_chars=max_chars)
83
+ return value
84
+
85
+
86
+ def _compact_command(command):
87
+ if isinstance(command, list):
88
+ head = []
89
+ for item in command[:16]:
90
+ head.append(_short_text(item, max_chars=120))
91
+ if item == "--":
92
+ break
93
+ return {
94
+ "argv0": command[0] if command else None,
95
+ "argc": len(command),
96
+ "head": head,
97
+ "truncated_after_separator": "--" in head,
98
+ }
99
+ if isinstance(command, str):
100
+ return {"text": _short_text(command)}
101
+ return None
102
+
103
+
104
+ def _compact_dispatch(dispatch):
105
+ if not isinstance(dispatch, dict):
106
+ return {}
107
+ compact = {
108
+ key: dispatch.get(key)
109
+ for key in ["ok", "status", "task_id", "attempt_id", "pid", "returncode", "safe_error"]
110
+ if key in dispatch
111
+ }
112
+ if "command" in dispatch:
113
+ compact["command"] = _compact_command(dispatch.get("command"))
114
+ for key in ["stdout_path", "stderr_path"]:
115
+ if key in dispatch:
116
+ compact[key] = dispatch.get(key)
117
+ return compact
118
+
119
+
120
+ def _proc_process_status(pid_value):
121
+ stat_path = Path(f"/proc/{pid_value}/stat")
122
+ try:
123
+ stat_text = stat_path.read_text(encoding="utf-8")
124
+ except FileNotFoundError:
125
+ return {"status": "not_found", "pid": pid_value}
126
+ except OSError as exc:
127
+ return {"status": "unknown", "pid": pid_value, "safe_error": str(exc)}
128
+ parts = stat_text.rsplit(") ", 1)
129
+ if len(parts) != 2:
130
+ return {"status": "unknown", "pid": pid_value, "safe_error": "invalid /proc stat"}
131
+ fields = parts[1].split()
132
+ state = fields[0] if fields else ""
133
+ return {
134
+ "status": "zombie" if state in {"Z", "X", "x"} else "running",
135
+ "pid": pid_value,
136
+ "stat": state,
137
+ "source": "proc",
138
+ }
139
+
140
+
141
+ def _process_status(pid):
142
+ if pid in (None, "", 0):
143
+ return {"status": "unknown", "pid": pid}
144
+ try:
145
+ pid_value = int(pid)
146
+ except (TypeError, ValueError):
147
+ return {"status": "invalid_pid", "pid": pid}
148
+ proc_status = _proc_process_status(pid_value)
149
+ if proc_status["status"] in {"running", "zombie"}:
150
+ return proc_status
151
+ alive_by_signal = False
152
+ try:
153
+ os.kill(pid_value, 0)
154
+ alive_by_signal = True
155
+ except ProcessLookupError:
156
+ return {"status": "not_found", "pid": pid_value}
157
+ except PermissionError:
158
+ return {"status": "running", "pid": pid_value, "visibility": "permission_denied"}
159
+ try:
160
+ result = subprocess.run(
161
+ ["ps", "-p", str(pid_value), "-o", "pid=", "-o", "etime=", "-o", "time=", "-o", "stat="],
162
+ check=False,
163
+ capture_output=True,
164
+ text=True,
165
+ timeout=2,
166
+ )
167
+ except (OSError, subprocess.SubprocessError) as exc:
168
+ if alive_by_signal:
169
+ return {"status": "running", "pid": pid_value, "source": "signal", "safe_error": str(exc)}
170
+ return {"status": "unknown", "pid": pid_value, "safe_error": str(exc)}
171
+ output = result.stdout.strip()
172
+ if result.returncode != 0 or not output:
173
+ if alive_by_signal:
174
+ return {"status": "running", "pid": pid_value, "source": "signal"}
175
+ return {"status": "not_found", "pid": pid_value}
176
+ parts = output.split(None, 3)
177
+ stat = parts[3] if len(parts) > 3 else ""
178
+ return {
179
+ "status": "zombie" if "Z" in stat else "running",
180
+ "pid": pid_value,
181
+ "etime": parts[1] if len(parts) > 1 else None,
182
+ "cpu_time": parts[2] if len(parts) > 2 else None,
183
+ "stat": stat,
184
+ }
185
+
186
+
187
+ def _connect(db_path):
188
+ path = Path(db_path).expanduser()
189
+ if not path.is_file():
190
+ return None, {
191
+ "ok": False,
192
+ "status": "missing_database",
193
+ "safe_error": f"database not found: {path}",
194
+ }
195
+ conn = sqlite3.connect(path)
196
+ conn.row_factory = sqlite3.Row
197
+ conn.execute("PRAGMA foreign_keys = ON")
198
+ conn.execute("PRAGMA busy_timeout = 5000")
199
+ return conn, None
200
+
201
+
202
+ def _compact_task(task):
203
+ return {
204
+ "task_id": task.get("task_id"),
205
+ "operator_state": task.get("operator_state"),
206
+ "lifecycle": task.get("lifecycle"),
207
+ "workstream_id": task.get("workstream_id"),
208
+ "route_id": task.get("route_id") or task.get("expected_output"),
209
+ "latest_attempt_id": task.get("latest_attempt_id"),
210
+ "latest_attempt_status": task.get("latest_attempt_status"),
211
+ "next_action": task.get("next_action"),
212
+ "updated_at": task.get("updated_at"),
213
+ "source_key": task.get("source_key"),
214
+ }
215
+
216
+
217
+ def build_status(db_path, history_limit=5):
218
+ data = web.build_dashboard_data(Path(db_path).expanduser(), history_limit=history_limit)
219
+ latest_run = (data.get("recent_runs") or [{}])[0]
220
+ return {
221
+ "summary": data.get("summary") or {},
222
+ "operator_state_counts": data.get("operator_state_counts") or {},
223
+ "daemon": data.get("daemon") or {},
224
+ "wakeup_summary": data.get("wakeup_summary") or {},
225
+ "acceptance_metrics": data.get("acceptance_metrics") or {},
226
+ "latest_loop": data.get("latest_loop") or {},
227
+ "operator_alerts": (data.get("operator_alerts") or [])[:history_limit],
228
+ "operator_next_steps": (data.get("operator_next_steps") or [])[:history_limit],
229
+ "recent_tasks": [
230
+ _compact_task(task)
231
+ for task in (data.get("recent_tasks") or [])[:history_limit]
232
+ ],
233
+ "latest_run": {
234
+ "run_id": latest_run.get("run_id"),
235
+ "status": latest_run.get("status"),
236
+ "started_at": latest_run.get("started_at"),
237
+ "finished_at": latest_run.get("finished_at"),
238
+ },
239
+ }
240
+
241
+
242
+ def _actions_for_task(conn, task_id):
243
+ return [
244
+ _row_dict(row)
245
+ for row in conn.execute(
246
+ """
247
+ SELECT action_id, action_type, target_url, external_id,
248
+ audit_status, publish_status, created_at
249
+ FROM github_actions
250
+ WHERE task_id = ?
251
+ ORDER BY created_at DESC, action_id DESC
252
+ """,
253
+ (task_id,),
254
+ )
255
+ ]
256
+
257
+
258
+ def _artifacts_for(conn, task_id=None, attempt_id=None):
259
+ clauses = []
260
+ params = []
261
+ if task_id:
262
+ clauses.append("task_id = ?")
263
+ params.append(task_id)
264
+ if attempt_id:
265
+ clauses.append("attempt_id = ?")
266
+ params.append(attempt_id)
267
+ where = " AND ".join(clauses) or "1 = 1"
268
+ return [
269
+ _row_dict(row)
270
+ for row in conn.execute(
271
+ f"""
272
+ SELECT artifact_type, path, bytes, created_at
273
+ FROM artifacts
274
+ WHERE {where}
275
+ ORDER BY created_at DESC, artifact_id DESC
276
+ """,
277
+ params,
278
+ )
279
+ ]
280
+
281
+
282
+ def _latest_attempt_for_task(conn, task_id):
283
+ attempt = _row_dict(
284
+ conn.execute(
285
+ """
286
+ SELECT attempt_id, attempt_no, status, worktree_path, branch_name,
287
+ started_at, heartbeat_at, finished_at, failure_json,
288
+ metadata_json
289
+ FROM attempts
290
+ WHERE task_id = ?
291
+ ORDER BY attempt_no DESC, started_at DESC, attempt_id DESC
292
+ LIMIT 1
293
+ """,
294
+ (task_id,),
295
+ ).fetchone()
296
+ )
297
+ if not attempt:
298
+ return None
299
+ metadata = _decode_json(attempt.pop("metadata_json"), {}) or {}
300
+ dispatch = _compact_dispatch(metadata.get("dispatch"))
301
+ attempt["failure"] = _decode_json(attempt.pop("failure_json"), {})
302
+ attempt["dispatch"] = dispatch
303
+ attempt["process"] = _process_status(dispatch.get("pid"))
304
+ return attempt
305
+
306
+
307
+ def _recent_phases_for_attempt(conn, attempt_id, limit=5):
308
+ return [
309
+ _row_dict(row)
310
+ for row in conn.execute(
311
+ """
312
+ SELECT phase, status, summary, next_step, created_at
313
+ FROM worker_phases
314
+ WHERE attempt_id = ?
315
+ ORDER BY created_at DESC, phase_id DESC
316
+ LIMIT ?
317
+ """,
318
+ (attempt_id, limit),
319
+ )
320
+ ]
321
+
322
+
323
+ def _results_for_task(conn, task_id, limit=5):
324
+ results = []
325
+ for row in conn.execute(
326
+ """
327
+ SELECT result_id, attempt_id, output_type, verification_json,
328
+ handoff, created_at, metadata_json
329
+ FROM worker_results
330
+ WHERE task_id = ?
331
+ ORDER BY created_at DESC, result_id DESC
332
+ LIMIT ?
333
+ """,
334
+ (task_id, limit),
335
+ ):
336
+ result = _row_dict(row)
337
+ verification = _decode_json(result.pop("verification_json"), []) or []
338
+ metadata = _decode_json(result.pop("metadata_json"), {}) or {}
339
+ result["verification_count"] = len(verification)
340
+ result["handoff"] = _short_text(result.get("handoff"), max_chars=500)
341
+ result["used_skills"] = (metadata.get("used_skills") or [])[:8]
342
+ results.append(result)
343
+ return results
344
+
345
+
346
+ def _action_counts_for_task(conn, task_id):
347
+ rows = [
348
+ _row_dict(row)
349
+ for row in conn.execute(
350
+ """
351
+ SELECT audit_status, publish_status, COUNT(*) AS count
352
+ FROM github_actions
353
+ WHERE task_id = ?
354
+ GROUP BY audit_status, publish_status
355
+ """,
356
+ (task_id,),
357
+ )
358
+ ]
359
+ audit_counts = Counter()
360
+ publish_counts = Counter()
361
+ total = 0
362
+ for row in rows:
363
+ count = row["count"]
364
+ total += count
365
+ audit_counts[row["audit_status"]] += count
366
+ publish_counts[row["publish_status"]] += count
367
+ return {
368
+ "total": total,
369
+ "audit_status": dict(sorted(audit_counts.items())),
370
+ "publish_status": dict(sorted(publish_counts.items())),
371
+ }
372
+
373
+
374
+ def _latest_notification_for_task(conn, task_id):
375
+ notification = _row_dict(
376
+ conn.execute(
377
+ """
378
+ SELECT notification_id, notification_type, channel, status,
379
+ created_at, metadata_json
380
+ FROM notifications
381
+ WHERE task_id = ?
382
+ ORDER BY created_at DESC, notification_id DESC
383
+ LIMIT 1
384
+ """,
385
+ (task_id,),
386
+ ).fetchone()
387
+ )
388
+ if not notification:
389
+ return None
390
+ notification["metadata"] = _compact_json(_decode_json(notification.pop("metadata_json"), {}) or {})
391
+ return notification
392
+
393
+
394
+ def build_task(conn, task_id, result_limit=5):
395
+ task = _row_dict(
396
+ conn.execute(
397
+ """
398
+ SELECT t.task_id, t.workstream_id, t.lifecycle, t.parent_task_id,
399
+ t.priority, COALESCE(t.route_id, rd.route_id) AS route_id,
400
+ COALESCE(t.expected_output, rd.expected_output) AS expected_output,
401
+ t.created_at, t.updated_at, w.lifecycle AS workstream_lifecycle,
402
+ rd.confidence AS route_confidence,
403
+ rd.allowed_github_actions_json,
404
+ rd.recommended_skills_json
405
+ FROM tasks t
406
+ JOIN workstreams w ON w.workstream_id = t.workstream_id
407
+ LEFT JOIN route_decisions rd ON rd.task_id = t.task_id
408
+ WHERE t.task_id = ?
409
+ ORDER BY rd.created_at DESC
410
+ LIMIT 1
411
+ """,
412
+ (task_id,),
413
+ ).fetchone()
414
+ )
415
+ if not task:
416
+ return None
417
+ task["allowed_github_actions"] = _decode_json(task.pop("allowed_github_actions_json"), [])
418
+ task["recommended_skills"] = _decode_json(task.pop("recommended_skills_json"), [])
419
+ task["latest_attempt"] = _latest_attempt_for_task(conn, task_id)
420
+ if task["latest_attempt"]:
421
+ task["recent_phases"] = _recent_phases_for_attempt(
422
+ conn,
423
+ task["latest_attempt"]["attempt_id"],
424
+ limit=5,
425
+ )
426
+ task["latest_phase"] = task["recent_phases"][0] if task["recent_phases"] else None
427
+ else:
428
+ task["recent_phases"] = []
429
+ task["latest_phase"] = None
430
+ task["results"] = _results_for_task(conn, task_id, limit=result_limit)
431
+ task["latest_result"] = task["results"][0] if task["results"] else None
432
+ task["action_counts"] = _action_counts_for_task(conn, task_id)
433
+ task["latest_notification"] = _latest_notification_for_task(conn, task_id)
434
+ task["actions"] = _actions_for_task(conn, task_id)
435
+ task["artifacts"] = _artifacts_for(conn, task_id=task_id)
436
+ task["events"] = [
437
+ _row_dict(row)
438
+ for row in conn.execute(
439
+ """
440
+ SELECT te.relationship, ge.event_fingerprint, ge.event_type,
441
+ ge.actor_login, ge.authorization_status, ge.event_at,
442
+ gs.source_key, gs.source_type, gs.number, gs.html_url
443
+ FROM task_events te
444
+ JOIN github_events ge ON ge.event_id = te.event_id
445
+ JOIN github_sources gs ON gs.source_id = ge.source_id
446
+ WHERE te.task_id = ?
447
+ ORDER BY te.created_at DESC
448
+ """,
449
+ (task_id,),
450
+ )
451
+ ]
452
+ return task
453
+
454
+
455
+ def _latest_run_id(conn):
456
+ row = conn.execute(
457
+ """
458
+ SELECT run_id
459
+ FROM agent_runs
460
+ ORDER BY started_at DESC, run_id DESC
461
+ LIMIT 1
462
+ """
463
+ ).fetchone()
464
+ return row["run_id"] if row else None
465
+
466
+
467
+ def _compact_run(row, step_counts=None):
468
+ run = _row_dict(row)
469
+ if not run:
470
+ return None
471
+ run["dry_run"] = bool(run["dry_run"])
472
+ run["summary"] = _compact_json(_decode_json(run.pop("summary_json"), {}) or {})
473
+ run["error"] = _compact_json(_decode_json(run.pop("error_json"), {}) or {})
474
+ run["step_status_counts"] = dict(sorted((step_counts or {}).items()))
475
+ return run
476
+
477
+
478
+ def build_runs(conn, limit=5):
479
+ runs = [
480
+ _row_dict(row)
481
+ for row in conn.execute(
482
+ """
483
+ SELECT run_id, status, started_at, finished_at, config_path, dry_run,
484
+ summary_json, error_json
485
+ FROM agent_runs
486
+ ORDER BY started_at DESC, run_id DESC
487
+ LIMIT ?
488
+ """,
489
+ (limit,),
490
+ )
491
+ ]
492
+ if not runs:
493
+ return []
494
+ placeholders = ",".join("?" for _run in runs)
495
+ counts = {run["run_id"]: Counter() for run in runs}
496
+ for row in conn.execute(
497
+ f"""
498
+ SELECT run_id, status, COUNT(*) AS count
499
+ FROM run_steps
500
+ WHERE run_id IN ({placeholders})
501
+ GROUP BY run_id, status
502
+ """,
503
+ [run["run_id"] for run in runs],
504
+ ):
505
+ counts[row["run_id"]][row["status"]] += row["count"]
506
+ return [_compact_run(run, counts[run["run_id"]]) for run in runs]
507
+
508
+
509
+ def build_run(conn, run_id):
510
+ if run_id == "latest":
511
+ run_id = _latest_run_id(conn)
512
+ if not run_id:
513
+ return None
514
+ run = _row_dict(
515
+ conn.execute(
516
+ """
517
+ SELECT run_id, status, started_at, finished_at, config_path, dry_run,
518
+ summary_json, error_json
519
+ FROM agent_runs
520
+ WHERE run_id = ?
521
+ """,
522
+ (run_id,),
523
+ ).fetchone()
524
+ )
525
+ if not run:
526
+ return None
527
+ run["dry_run"] = bool(run["dry_run"])
528
+ run["summary"] = _compact_json(_decode_json(run.pop("summary_json"), {}) or {})
529
+ run["error"] = _compact_json(_decode_json(run.pop("error_json"), {}) or {})
530
+ run["steps"] = [
531
+ {
532
+ **_row_dict(row),
533
+ "output": _compact_json(_decode_json(row["output_json"], {}) or {}),
534
+ "error": _compact_json(_decode_json(row["error_json"], {}) or {}),
535
+ }
536
+ for row in conn.execute(
537
+ """
538
+ SELECT step_key, status, started_at, finished_at, output_json, error_json
539
+ FROM run_steps
540
+ WHERE run_id = ?
541
+ ORDER BY
542
+ CASE step_key
543
+ WHEN 'validate_config' THEN 0
544
+ WHEN 'discover_notifications' THEN 1
545
+ WHEN 'acquire_lease' THEN 2
546
+ WHEN 'supervise' THEN 3
547
+ WHEN 'audit_results' THEN 4
548
+ WHEN 'publish_actions' THEN 5
549
+ WHEN 'reconcile' THEN 6
550
+ WHEN 'discover' THEN 7
551
+ WHEN 'authorize' THEN 8
552
+ WHEN 'route' THEN 9
553
+ WHEN 'repo_pipelines' THEN 10
554
+ WHEN 'dispatch' THEN 11
555
+ WHEN 'summarize' THEN 12
556
+ ELSE 99
557
+ END,
558
+ step_key
559
+ """,
560
+ (run_id,),
561
+ )
562
+ ]
563
+ for step in run["steps"]:
564
+ step.pop("output_json", None)
565
+ step.pop("error_json", None)
566
+ if _table_exists(conn, "run_repo_steps"):
567
+ repo_steps = [
568
+ dict(row)
569
+ for row in conn.execute(
570
+ """
571
+ SELECT rrs.run_id, rrs.repo_id, repos.full_name AS repo_full_name,
572
+ rrs.step_key, rrs.status, rrs.started_at, rrs.finished_at,
573
+ rrs.output_json, rrs.error_json
574
+ FROM run_repo_steps rrs
575
+ JOIN repos ON repos.repo_id = rrs.repo_id
576
+ WHERE rrs.run_id = ?
577
+ ORDER BY repos.full_name, rrs.step_key
578
+ """,
579
+ (run_id,),
580
+ )
581
+ ]
582
+ repo_order = {step: index for index, step in enumerate(REPO_STEP_ORDER)}
583
+ for step in repo_steps:
584
+ step["output"] = _compact_json(_decode_json(step.pop("output_json"), {}) or {})
585
+ step["error"] = _compact_json(_decode_json(step.pop("error_json"), {}) or {})
586
+ run["repo_steps"] = sorted(
587
+ repo_steps,
588
+ key=lambda step: (
589
+ step.get("repo_full_name") or "",
590
+ repo_order.get(step["step_key"], len(repo_order)),
591
+ step["step_key"],
592
+ ),
593
+ )
594
+ else:
595
+ run["repo_steps"] = []
596
+ return run
597
+
598
+
599
+ def build_attempt(conn, attempt_id):
600
+ attempt = _row_dict(
601
+ conn.execute(
602
+ """
603
+ SELECT a.attempt_id, a.task_id, a.attempt_no, a.status,
604
+ a.worktree_path, a.branch_name, a.started_at, a.heartbeat_at,
605
+ a.finished_at, a.failure_json, a.metadata_json,
606
+ t.workstream_id, t.lifecycle AS task_lifecycle
607
+ FROM attempts a
608
+ JOIN tasks t ON t.task_id = a.task_id
609
+ WHERE a.attempt_id = ?
610
+ """,
611
+ (attempt_id,),
612
+ ).fetchone()
613
+ )
614
+ if not attempt:
615
+ return None
616
+ metadata = _decode_json(attempt.pop("metadata_json"), {}) or {}
617
+ dispatch = _compact_dispatch(metadata.get("dispatch"))
618
+ attempt["failure"] = _decode_json(attempt.pop("failure_json"), {})
619
+ attempt["dispatch"] = dispatch
620
+ attempt["process"] = _process_status(dispatch.get("pid"))
621
+ attempt["phases"] = [
622
+ _row_dict(row)
623
+ for row in conn.execute(
624
+ """
625
+ SELECT phase, status, summary, next_step, created_at
626
+ FROM worker_phases
627
+ WHERE attempt_id = ?
628
+ ORDER BY created_at DESC, phase_id DESC
629
+ LIMIT 10
630
+ """,
631
+ (attempt_id,),
632
+ )
633
+ ]
634
+ attempt["result"] = _row_dict(
635
+ conn.execute(
636
+ """
637
+ SELECT result_id, output_type, created_at, handoff
638
+ FROM worker_results
639
+ WHERE attempt_id = ?
640
+ """,
641
+ (attempt_id,),
642
+ ).fetchone()
643
+ )
644
+ attempt["artifacts"] = _artifacts_for(conn, attempt_id=attempt_id)
645
+ return attempt
646
+
647
+
648
+ def _payload_summary(payload):
649
+ if not isinstance(payload, dict):
650
+ return {"value": _compact_json(payload)}
651
+ summary = {"keys": sorted(payload.keys())[:20]}
652
+ for key in ["id", "node_id", "url", "html_url", "intent", "state", "action"]:
653
+ if key in payload:
654
+ summary[key] = _compact_json(payload.get(key))
655
+ for key in ["title", "body"]:
656
+ if key in payload and payload[key] is not None:
657
+ text = str(payload[key])
658
+ summary[f"{key}_chars"] = len(text)
659
+ summary[f"{key}_preview"] = _short_text(text, max_chars=180)
660
+ return summary
661
+
662
+
663
+ def _event_candidates(identifier):
664
+ candidates = [identifier]
665
+ if ":" not in identifier:
666
+ for prefix in ["comment", "review_comment", "review", "assigned", "notification"]:
667
+ candidates.append(f"{prefix}:{identifier}")
668
+ return list(dict.fromkeys(candidates))
669
+
670
+
671
+ def build_event(conn, identifier):
672
+ event = None
673
+ for candidate in _event_candidates(identifier):
674
+ event = _row_dict(
675
+ conn.execute(
676
+ """
677
+ SELECT ge.event_id, ge.event_fingerprint, ge.event_type,
678
+ ge.actor_login, ge.author_association,
679
+ ge.authorization_status, ge.event_at, ge.payload_json,
680
+ gs.source_key, gs.source_type, gs.number, gs.html_url,
681
+ gs.title, gs.state
682
+ FROM github_events ge
683
+ JOIN github_sources gs ON gs.source_id = ge.source_id
684
+ WHERE ge.event_fingerprint = ?
685
+ """,
686
+ (candidate,),
687
+ ).fetchone()
688
+ )
689
+ if event:
690
+ break
691
+ if not event:
692
+ return None
693
+ event_id = event["event_id"]
694
+ payload = _decode_json(event.pop("payload_json"), {}) or {}
695
+ event["payload_summary"] = _payload_summary(payload)
696
+ event["tasks"] = [
697
+ _row_dict(row)
698
+ for row in conn.execute(
699
+ """
700
+ SELECT te.relationship, t.task_id, t.lifecycle, t.priority,
701
+ t.workstream_id, COALESCE(t.route_id, rd.route_id) AS route_id,
702
+ COALESCE(t.expected_output, rd.expected_output) AS expected_output,
703
+ t.parent_task_id,
704
+ a.attempt_id AS latest_attempt_id,
705
+ a.status AS latest_attempt_status,
706
+ a.heartbeat_at AS latest_attempt_heartbeat_at,
707
+ a.worktree_path,
708
+ a.branch_name
709
+ FROM task_events te
710
+ JOIN tasks t ON t.task_id = te.task_id
711
+ LEFT JOIN route_decisions rd ON rd.route_decision_id = (
712
+ SELECT route_decision_id
713
+ FROM route_decisions
714
+ WHERE task_id = t.task_id
715
+ ORDER BY created_at DESC, route_decision_id DESC
716
+ LIMIT 1
717
+ )
718
+ LEFT JOIN attempts a ON a.attempt_id = (
719
+ SELECT attempt_id
720
+ FROM attempts
721
+ WHERE task_id = t.task_id
722
+ ORDER BY attempt_no DESC, started_at DESC, attempt_id DESC
723
+ LIMIT 1
724
+ )
725
+ WHERE te.event_id = ?
726
+ ORDER BY te.created_at DESC, t.task_id DESC
727
+ """,
728
+ (event_id,),
729
+ )
730
+ ]
731
+ return event
732
+
733
+
734
+ def build_events(conn, query, limit=10):
735
+ like = f"%{query}%"
736
+ events = []
737
+ for row in conn.execute(
738
+ """
739
+ SELECT ge.event_id, ge.event_fingerprint, ge.event_type,
740
+ ge.actor_login, ge.author_association,
741
+ ge.authorization_status, ge.event_at, ge.payload_json,
742
+ gs.source_key, gs.source_type, gs.number, gs.html_url,
743
+ COUNT(te.task_id) AS linked_task_count
744
+ FROM github_events ge
745
+ JOIN github_sources gs ON gs.source_id = ge.source_id
746
+ LEFT JOIN task_events te ON te.event_id = ge.event_id
747
+ WHERE ge.event_fingerprint = ?
748
+ OR ge.event_fingerprint LIKE ?
749
+ OR ge.payload_json LIKE ?
750
+ OR gs.source_key LIKE ?
751
+ OR gs.html_url LIKE ?
752
+ GROUP BY ge.event_id
753
+ ORDER BY ge.event_at DESC, ge.event_id DESC
754
+ LIMIT ?
755
+ """,
756
+ (query, like, like, like, like, limit),
757
+ ):
758
+ event = _row_dict(row)
759
+ payload = _decode_json(event.pop("payload_json"), {}) or {}
760
+ event["payload_summary"] = _payload_summary(payload)
761
+ events.append(event)
762
+ return events
763
+
764
+
765
+ def build_artifact(db_path, task_id, artifact_type, max_bytes=65536):
766
+ return web.load_artifact_preview(
767
+ Path(db_path).expanduser(),
768
+ task_id,
769
+ artifact_type,
770
+ max_bytes=max_bytes,
771
+ )
772
+
773
+
774
+ def _source_matches(conn, identifier):
775
+ like = f"%{identifier}%"
776
+ return [
777
+ _row_dict(row)
778
+ for row in conn.execute(
779
+ """
780
+ SELECT source_id, source_key, source_type, number, html_url,
781
+ title, state, author_login, source_updated_at, metadata_json
782
+ FROM github_sources
783
+ WHERE source_key = ?
784
+ OR html_url = ?
785
+ OR CAST(number AS TEXT) = ?
786
+ OR source_key LIKE ?
787
+ OR html_url LIKE ?
788
+ ORDER BY source_updated_at DESC, number DESC, source_key
789
+ LIMIT 10
790
+ """,
791
+ (identifier, identifier, identifier, like, like),
792
+ )
793
+ ]
794
+
795
+
796
+ def build_source(conn, identifier, limit=50):
797
+ sources = _source_matches(conn, identifier)
798
+ if not sources:
799
+ return None
800
+ for source in sources:
801
+ source["metadata"] = _compact_json(_decode_json(source.pop("metadata_json"), {}) or {})
802
+ source_ids = [source["source_id"] for source in sources]
803
+ placeholders = ",".join("?" for _source_id in source_ids)
804
+ workstreams = [
805
+ _row_dict(row)
806
+ for row in conn.execute(
807
+ f"""
808
+ SELECT DISTINCT w.workstream_id, w.lifecycle, w.active_task_id,
809
+ w.origin_workstream_id, w.created_at, w.updated_at,
810
+ ws.relationship, gs.source_key
811
+ FROM workstreams w
812
+ LEFT JOIN workstream_sources ws ON ws.workstream_id = w.workstream_id
813
+ LEFT JOIN github_sources gs ON gs.source_id = ws.source_id
814
+ WHERE ws.source_id IN ({placeholders})
815
+ OR w.primary_source_id IN ({placeholders})
816
+ ORDER BY w.updated_at DESC, w.workstream_id DESC
817
+ """,
818
+ source_ids + source_ids,
819
+ )
820
+ ]
821
+ workstream_ids = [stream["workstream_id"] for stream in workstreams]
822
+ tasks = []
823
+ if workstream_ids:
824
+ stream_placeholders = ",".join("?" for _stream_id in workstream_ids)
825
+ tasks = [
826
+ _row_dict(row)
827
+ for row in conn.execute(
828
+ f"""
829
+ SELECT t.created_at, t.updated_at, t.task_id, t.workstream_id,
830
+ t.parent_task_id, t.route_id, t.expected_output,
831
+ t.lifecycle, a.attempt_id AS latest_attempt_id,
832
+ a.status AS latest_attempt_status, a.worktree_path,
833
+ a.branch_name, a.heartbeat_at AS latest_attempt_heartbeat_at
834
+ FROM tasks t
835
+ LEFT JOIN attempts a ON a.attempt_id = (
836
+ SELECT attempt_id
837
+ FROM attempts
838
+ WHERE task_id = t.task_id
839
+ ORDER BY attempt_no DESC, started_at DESC, attempt_id DESC
840
+ LIMIT 1
841
+ )
842
+ WHERE t.workstream_id IN ({stream_placeholders})
843
+ ORDER BY t.created_at, t.task_id
844
+ LIMIT ?
845
+ """,
846
+ workstream_ids + [limit],
847
+ )
848
+ ]
849
+ events = [
850
+ _row_dict(row)
851
+ for row in conn.execute(
852
+ f"""
853
+ SELECT event_fingerprint, event_type, actor_login,
854
+ authorization_status, event_at, source_id
855
+ FROM github_events
856
+ WHERE source_id IN ({placeholders})
857
+ ORDER BY event_at DESC, event_id DESC
858
+ LIMIT ?
859
+ """,
860
+ source_ids + [min(limit, 50)],
861
+ )
862
+ ]
863
+ return {
864
+ "sources": sources,
865
+ "workstreams": workstreams,
866
+ "tasks": tasks,
867
+ "events": events,
868
+ }
869
+
870
+
871
+ def build_workstream(conn, workstream_id):
872
+ stream = _row_dict(
873
+ conn.execute(
874
+ """
875
+ SELECT w.workstream_id, w.lifecycle, w.active_task_id, w.origin_workstream_id,
876
+ w.created_at, w.updated_at, r.full_name AS repo
877
+ FROM workstreams w
878
+ JOIN repos r ON r.repo_id = w.repo_id
879
+ WHERE w.workstream_id = ?
880
+ """,
881
+ (workstream_id,),
882
+ ).fetchone()
883
+ )
884
+ if not stream:
885
+ return None
886
+ stream["tasks"] = [
887
+ _row_dict(row)
888
+ for row in conn.execute(
889
+ """
890
+ SELECT t.task_id, t.lifecycle, t.priority, t.route_id, t.expected_output,
891
+ t.parent_task_id, t.created_at, t.updated_at,
892
+ a.attempt_id AS latest_attempt_id,
893
+ a.status AS latest_attempt_status,
894
+ a.heartbeat_at AS latest_attempt_heartbeat_at
895
+ FROM tasks t
896
+ LEFT JOIN attempts a ON a.attempt_id = (
897
+ SELECT attempt_id
898
+ FROM attempts
899
+ WHERE task_id = t.task_id
900
+ ORDER BY attempt_no DESC, started_at DESC, attempt_id DESC
901
+ LIMIT 1
902
+ )
903
+ WHERE t.workstream_id = ?
904
+ ORDER BY t.updated_at DESC, t.task_id DESC
905
+ LIMIT 20
906
+ """,
907
+ (workstream_id,),
908
+ )
909
+ ]
910
+ stream["sources"] = [
911
+ _row_dict(row)
912
+ for row in conn.execute(
913
+ """
914
+ SELECT ws.relationship, gs.source_key, gs.source_type, gs.number,
915
+ gs.html_url, gs.state, gs.title
916
+ FROM workstream_sources ws
917
+ JOIN github_sources gs ON gs.source_id = ws.source_id
918
+ WHERE ws.workstream_id = ?
919
+ ORDER BY ws.created_at DESC
920
+ """,
921
+ (workstream_id,),
922
+ )
923
+ ]
924
+ return stream
925
+
926
+
927
+ def _ready(command, payload):
928
+ return {
929
+ "ok": True,
930
+ "status": "ready",
931
+ "command": command,
932
+ "format": "json",
933
+ "checked_at_utc": _checked_at_utc(),
934
+ "data": payload,
935
+ }
936
+
937
+
938
+ def _not_found(kind, identifier):
939
+ return {
940
+ "ok": False,
941
+ "status": "not_found",
942
+ "safe_error": f"{kind} not found: {identifier}",
943
+ }
944
+
945
+
946
+ def main(argv=None):
947
+ parser = argparse.ArgumentParser()
948
+ parser.add_argument("--db", default=DEFAULT_DB)
949
+ parser.add_argument("--history-limit", type=int, default=5)
950
+ subparsers = parser.add_subparsers(dest="command", required=True)
951
+
952
+ subparsers.add_parser("status")
953
+
954
+ runs_parser = subparsers.add_parser("runs")
955
+ runs_parser.add_argument("--limit", type=int)
956
+
957
+ task_parser = subparsers.add_parser("task")
958
+ task_parser.add_argument("task_id")
959
+ task_parser.add_argument("--limit", type=int, default=5)
960
+
961
+ run_parser = subparsers.add_parser("run")
962
+ run_parser.add_argument("run_id")
963
+
964
+ attempt_parser = subparsers.add_parser("attempt")
965
+ attempt_parser.add_argument("attempt_id")
966
+
967
+ workstream_parser = subparsers.add_parser("workstream")
968
+ workstream_parser.add_argument("workstream_id")
969
+
970
+ event_parser = subparsers.add_parser("event")
971
+ event_parser.add_argument("event_fingerprint")
972
+
973
+ events_parser = subparsers.add_parser("events")
974
+ events_parser.add_argument("query")
975
+ events_parser.add_argument("--limit", type=int, default=10)
976
+
977
+ source_parser = subparsers.add_parser("source")
978
+ source_parser.add_argument("identifier")
979
+ source_parser.add_argument("--limit", type=int, default=50)
980
+
981
+ artifact_parser = subparsers.add_parser("artifact")
982
+ artifact_parser.add_argument("task_id")
983
+ artifact_parser.add_argument("artifact_type")
984
+ artifact_parser.add_argument("--max-bytes", type=int, default=65536)
985
+
986
+ args = parser.parse_args(argv)
987
+ conn, error = _connect(args.db)
988
+ if error:
989
+ return emit(error, exit_code=3)
990
+ with closing(conn):
991
+ try:
992
+ if args.command == "status":
993
+ return emit(_ready("status", build_status(args.db, args.history_limit)))
994
+ if args.command == "runs":
995
+ return emit(_ready("runs", build_runs(conn, args.limit or args.history_limit)))
996
+ if args.command == "task":
997
+ payload = build_task(conn, args.task_id, result_limit=args.limit)
998
+ if not payload:
999
+ return emit(_not_found("task", args.task_id), exit_code=3)
1000
+ return emit(_ready("task", payload))
1001
+ if args.command == "run":
1002
+ payload = build_run(conn, args.run_id)
1003
+ if not payload:
1004
+ return emit(_not_found("run", args.run_id), exit_code=3)
1005
+ return emit(_ready("run", payload))
1006
+ if args.command == "attempt":
1007
+ payload = build_attempt(conn, args.attempt_id)
1008
+ if not payload:
1009
+ return emit(_not_found("attempt", args.attempt_id), exit_code=3)
1010
+ return emit(_ready("attempt", payload))
1011
+ if args.command == "workstream":
1012
+ payload = build_workstream(conn, args.workstream_id)
1013
+ if not payload:
1014
+ return emit(_not_found("workstream", args.workstream_id), exit_code=3)
1015
+ return emit(_ready("workstream", payload))
1016
+ if args.command == "event":
1017
+ payload = build_event(conn, args.event_fingerprint)
1018
+ if not payload:
1019
+ return emit(_not_found("event", args.event_fingerprint), exit_code=3)
1020
+ return emit(_ready("event", payload))
1021
+ if args.command == "events":
1022
+ return emit(_ready("events", build_events(conn, args.query, limit=args.limit)))
1023
+ if args.command == "source":
1024
+ payload = build_source(conn, args.identifier, limit=args.limit)
1025
+ if not payload:
1026
+ return emit(_not_found("source", args.identifier), exit_code=3)
1027
+ return emit(_ready("source", payload))
1028
+ if args.command == "artifact":
1029
+ payload = build_artifact(
1030
+ args.db,
1031
+ args.task_id,
1032
+ args.artifact_type,
1033
+ max_bytes=args.max_bytes,
1034
+ )
1035
+ if not payload:
1036
+ return emit(
1037
+ _not_found("artifact", f"{args.task_id}:{args.artifact_type}"),
1038
+ exit_code=3,
1039
+ )
1040
+ return emit(_ready("artifact", payload))
1041
+ except sqlite3.Error as exc:
1042
+ return emit(
1043
+ {"ok": False, "status": "database_error", "safe_error": str(exc)},
1044
+ exit_code=3,
1045
+ )
1046
+
1047
+ return emit({"ok": False, "status": "unsupported", "safe_error": args.command}, exit_code=3)
1048
+
1049
+
1050
+ if __name__ == "__main__":
1051
+ raise SystemExit(main())