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
@@ -0,0 +1,376 @@
1
+ """SQLite storage helpers for the rewritten DD GitHub agent."""
2
+
3
+ from contextlib import closing
4
+ import hashlib
5
+ import json
6
+ from pathlib import Path
7
+ import sqlite3
8
+
9
+ from robert_agent.resource_files import resource
10
+
11
+
12
+ SCHEMA_RESOURCE = resource("db", "schema.sql")
13
+ WORK_ITEM_MIGRATION_VERSION = 2
14
+ WORK_ITEM_MIGRATION_NAME = "robert-work-item-control-v1"
15
+ WORK_ITEM_MIGRATION_CHECKSUM = hashlib.sha256(
16
+ WORK_ITEM_MIGRATION_NAME.encode("utf-8")
17
+ ).hexdigest()
18
+
19
+
20
+ def _ensure_column(conn, table, column_name, ddl):
21
+ columns = {row[1] for row in conn.execute(f"PRAGMA table_info({table})")}
22
+ if column_name not in columns:
23
+ conn.execute(f"ALTER TABLE {table} ADD COLUMN {ddl}")
24
+
25
+
26
+ def _origin_workstream_id(payload):
27
+ metadata = payload.get("metadata") or {}
28
+ dd_workstream = metadata.get("dd_workstream") or {}
29
+ return (
30
+ payload.get("origin_workstream_id")
31
+ or dd_workstream.get("origin_workstream_id")
32
+ or dd_workstream.get("workstream_id")
33
+ )
34
+
35
+
36
+ def _has_active_task(conn, workstream_id):
37
+ row = conn.execute(
38
+ """
39
+ SELECT 1
40
+ FROM tasks
41
+ WHERE workstream_id = ?
42
+ AND lifecycle IN ('detected', 'authorized', 'classified', 'queued', 'running')
43
+ LIMIT 1
44
+ """,
45
+ (workstream_id,),
46
+ ).fetchone()
47
+ return bool(row)
48
+
49
+
50
+ def _repair_origin_workstream_after_task_move(conn, origin_workstream_id, task_id, now):
51
+ row = conn.execute(
52
+ "SELECT active_task_id FROM workstreams WHERE workstream_id = ?",
53
+ (origin_workstream_id,),
54
+ ).fetchone()
55
+ if not row or row[0] != task_id:
56
+ return
57
+ lifecycle = "active" if _has_active_task(conn, origin_workstream_id) else "completed"
58
+ conn.execute(
59
+ """
60
+ UPDATE workstreams
61
+ SET lifecycle = ?, active_task_id = NULL, updated_at = ?
62
+ WHERE workstream_id = ?
63
+ """,
64
+ (lifecycle, now, origin_workstream_id),
65
+ )
66
+
67
+
68
+ def _migrate_pr_mainline_workstreams(conn):
69
+ _ensure_column(
70
+ conn,
71
+ "workstreams",
72
+ "origin_workstream_id",
73
+ "origin_workstream_id TEXT REFERENCES workstreams(workstream_id) ON DELETE SET NULL",
74
+ )
75
+ rows = conn.execute(
76
+ """
77
+ SELECT DISTINCT t.task_id, t.lifecycle, gs.repo_id, gs.source_id, gs.source_key, ge.payload_json
78
+ FROM tasks t
79
+ JOIN task_events te ON te.task_id = t.task_id
80
+ JOIN github_events ge ON ge.event_id = te.event_id
81
+ JOIN github_sources gs ON gs.source_id = ge.source_id
82
+ WHERE gs.source_type = 'pull_request'
83
+ AND t.workstream_id != gs.source_key
84
+ """
85
+ ).fetchall()
86
+ now = conn.execute("SELECT datetime('now')").fetchone()[0]
87
+ for task_id, task_lifecycle, repo_id, source_id, source_key, payload_json in rows:
88
+ try:
89
+ payload = json.loads(payload_json or "{}")
90
+ except ValueError:
91
+ payload = {}
92
+ origin_workstream_id = _origin_workstream_id(payload)
93
+ if not origin_workstream_id:
94
+ continue
95
+ active_task_id = task_id if task_lifecycle in {
96
+ "detected",
97
+ "authorized",
98
+ "classified",
99
+ "queued",
100
+ "running",
101
+ } else None
102
+ workstream_lifecycle = "active" if active_task_id else "completed"
103
+ conn.execute(
104
+ """
105
+ INSERT INTO workstreams(
106
+ workstream_id, repo_id, primary_source_id, origin_workstream_id,
107
+ lifecycle, active_task_id, created_at, updated_at, metadata_json
108
+ )
109
+ VALUES (?, ?, ?, ?, ?, ?, datetime('now'), datetime('now'), '{}')
110
+ ON CONFLICT(workstream_id) DO UPDATE SET
111
+ origin_workstream_id = excluded.origin_workstream_id,
112
+ primary_source_id = excluded.primary_source_id,
113
+ lifecycle = excluded.lifecycle,
114
+ active_task_id = excluded.active_task_id,
115
+ updated_at = excluded.updated_at
116
+ """,
117
+ (
118
+ source_key,
119
+ repo_id,
120
+ source_id,
121
+ origin_workstream_id,
122
+ workstream_lifecycle,
123
+ active_task_id,
124
+ ),
125
+ )
126
+ conn.execute(
127
+ "UPDATE tasks SET workstream_id = ? WHERE task_id = ?",
128
+ (source_key, task_id),
129
+ )
130
+ conn.execute(
131
+ """
132
+ INSERT INTO workstream_sources(workstream_id, source_id, relationship, created_at)
133
+ VALUES (?, ?, 'derived_pr', datetime('now'))
134
+ ON CONFLICT(workstream_id, source_id) DO NOTHING
135
+ """,
136
+ (source_key, source_id),
137
+ )
138
+ _repair_origin_workstream_after_task_move(conn, origin_workstream_id, task_id, now)
139
+
140
+
141
+ def _migrate_github_action_publish_status(conn):
142
+ _ensure_column(
143
+ conn,
144
+ "github_actions",
145
+ "publish_status",
146
+ "publish_status TEXT NOT NULL DEFAULT 'not_published' CHECK (publish_status IN ('not_published', 'published', 'skipped'))",
147
+ )
148
+
149
+
150
+ def _migrate_route_recommended_skills(conn):
151
+ columns = {row[1] for row in conn.execute("PRAGMA table_info(route_decisions)")}
152
+ _ensure_column(
153
+ conn,
154
+ "route_decisions",
155
+ "recommended_skills_json",
156
+ "recommended_skills_json TEXT NOT NULL DEFAULT '[]'",
157
+ )
158
+ source_column = "allowed_skills_json" if "allowed_skills_json" in columns else "required_skills_json"
159
+ conn.execute(
160
+ f"""
161
+ UPDATE route_decisions
162
+ SET recommended_skills_json = {source_column}
163
+ WHERE recommended_skills_json = '[]'
164
+ AND {source_column} != '[]'
165
+ """
166
+ )
167
+
168
+
169
+ def _migrate_project_memory_fts(conn):
170
+ try:
171
+ conn.execute(
172
+ """
173
+ CREATE VIRTUAL TABLE IF NOT EXISTS project_memory_fts USING fts5(
174
+ memory_id UNINDEXED,
175
+ repo_id UNINDEXED,
176
+ title,
177
+ short_summary,
178
+ long_summary,
179
+ paths,
180
+ symbols,
181
+ keywords
182
+ )
183
+ """
184
+ )
185
+ except sqlite3.OperationalError:
186
+ return
187
+
188
+
189
+ def _backfill_work_item_id(workstream_id):
190
+ digest = hashlib.sha256(workstream_id.encode("utf-8")).hexdigest()[:20]
191
+ return f"wi-{digest}"
192
+
193
+
194
+ def _backfill_event_id(workstream_id):
195
+ digest = hashlib.sha256(f"backfill:{workstream_id}".encode("utf-8")).hexdigest()[:20]
196
+ return f"wie-{digest}"
197
+
198
+
199
+ def _backfill_work_items(conn):
200
+ rows = conn.execute(
201
+ """
202
+ SELECT
203
+ w.workstream_id,
204
+ w.repo_id,
205
+ w.lifecycle,
206
+ w.created_at,
207
+ w.updated_at,
208
+ w.primary_source_id,
209
+ gs.title,
210
+ gs.author_login,
211
+ COALESCE(
212
+ (
213
+ SELECT t.priority
214
+ FROM tasks t
215
+ WHERE t.workstream_id = w.workstream_id
216
+ ORDER BY t.created_at DESC, t.task_id DESC
217
+ LIMIT 1
218
+ ),
219
+ 'P2'
220
+ ) AS priority
221
+ FROM workstreams w
222
+ LEFT JOIN github_sources gs ON gs.source_id = w.primary_source_id
223
+ WHERE w.origin_workstream_id IS NULL
224
+ ORDER BY w.created_at, w.workstream_id
225
+ """
226
+ ).fetchall()
227
+ for (
228
+ workstream_id,
229
+ repo_id,
230
+ lifecycle,
231
+ created_at,
232
+ updated_at,
233
+ primary_source_id,
234
+ source_title,
235
+ author_login,
236
+ priority,
237
+ ) in rows:
238
+ work_item_id = _backfill_work_item_id(workstream_id)
239
+ created_by = author_login or "system"
240
+ completed_at = updated_at if lifecycle == "completed" else None
241
+ canceled_at = updated_at if lifecycle == "canceled" else None
242
+ conn.execute(
243
+ """
244
+ INSERT INTO work_items(
245
+ work_item_id, repo_id, title, description, priority,
246
+ origin_type, origin_source_id, routing_mode, requested_worker,
247
+ workstream_id, creation_idempotency_key, created_by,
248
+ activated_at, completed_at, canceled_at, version,
249
+ created_at, updated_at, metadata_json
250
+ )
251
+ VALUES (?, ?, ?, '', ?, 'github', ?, 'auto', NULL, ?, ?, ?, ?, ?, ?, 1, ?, ?, '{}')
252
+ ON CONFLICT DO NOTHING
253
+ """,
254
+ (
255
+ work_item_id,
256
+ repo_id,
257
+ source_title or f"Workstream {workstream_id}",
258
+ priority if priority in {"P0", "P1", "P2", "P3"} else "P2",
259
+ primary_source_id,
260
+ workstream_id,
261
+ f"backfill:{workstream_id}",
262
+ created_by,
263
+ created_at,
264
+ completed_at,
265
+ canceled_at,
266
+ created_at,
267
+ updated_at,
268
+ ),
269
+ )
270
+ item = conn.execute(
271
+ "SELECT work_item_id FROM work_items WHERE workstream_id = ?",
272
+ (workstream_id,),
273
+ ).fetchone()
274
+ if not item:
275
+ continue
276
+ conn.execute(
277
+ """
278
+ INSERT INTO work_item_events(
279
+ event_id, work_item_id, event_type, actor_kind, actor_identity,
280
+ body, resolves_event_id, idempotency_key, created_at, metadata_json
281
+ )
282
+ VALUES (?, ?, 'backfilled', 'system', 'migration', '', NULL, ?, ?, ?)
283
+ ON CONFLICT(work_item_id, idempotency_key) DO NOTHING
284
+ """,
285
+ (
286
+ _backfill_event_id(workstream_id),
287
+ item[0],
288
+ f"backfill:{workstream_id}",
289
+ updated_at,
290
+ json.dumps(
291
+ {"source": "workstream", "workstream_id": workstream_id},
292
+ sort_keys=True,
293
+ ),
294
+ ),
295
+ )
296
+
297
+
298
+ def _migrate_work_item_control(conn):
299
+ marker = conn.execute(
300
+ "SELECT checksum FROM schema_migrations WHERE name = ?",
301
+ (WORK_ITEM_MIGRATION_NAME,),
302
+ ).fetchone()
303
+ if marker:
304
+ if marker[0] != WORK_ITEM_MIGRATION_CHECKSUM:
305
+ raise RuntimeError("work item migration checksum mismatch")
306
+ return
307
+
308
+ _ensure_column(
309
+ conn,
310
+ "tasks",
311
+ "routing_mode",
312
+ "routing_mode TEXT NOT NULL DEFAULT 'auto' CHECK (routing_mode IN ('auto', 'manual'))",
313
+ )
314
+ _ensure_column(conn, "tasks", "requested_worker", "requested_worker TEXT")
315
+ _ensure_column(
316
+ conn,
317
+ "wakeups",
318
+ "work_item_id",
319
+ "work_item_id TEXT REFERENCES work_items(work_item_id) ON DELETE CASCADE",
320
+ )
321
+ conn.execute(
322
+ """
323
+ CREATE INDEX IF NOT EXISTS idx_wakeups_pending_work_item
324
+ ON wakeups(work_item_id, status, not_before_at)
325
+ WHERE work_item_id IS NOT NULL
326
+ """
327
+ )
328
+ _backfill_work_items(conn)
329
+ conn.execute(
330
+ """
331
+ INSERT INTO schema_migrations(version, name, checksum, applied_at)
332
+ VALUES (?, ?, ?, datetime('now'))
333
+ """,
334
+ (
335
+ WORK_ITEM_MIGRATION_VERSION,
336
+ WORK_ITEM_MIGRATION_NAME,
337
+ WORK_ITEM_MIGRATION_CHECKSUM,
338
+ ),
339
+ )
340
+
341
+
342
+ def schema_sql() -> str:
343
+ return SCHEMA_RESOURCE.read_text(encoding="utf-8")
344
+
345
+
346
+ def init_database(db_path, schema_path=None):
347
+ db_path = Path(db_path).expanduser()
348
+ db_path.parent.mkdir(parents=True, exist_ok=True)
349
+ if schema_path is None:
350
+ schema = schema_sql()
351
+ schema_path = SCHEMA_RESOURCE
352
+ else:
353
+ schema_path = Path(schema_path)
354
+ schema = schema_path.read_text(encoding="utf-8")
355
+
356
+ with closing(sqlite3.connect(db_path)) as conn, conn:
357
+ conn.executescript(schema)
358
+ _migrate_pr_mainline_workstreams(conn)
359
+ _migrate_github_action_publish_status(conn)
360
+ _migrate_route_recommended_skills(conn)
361
+ _migrate_work_item_control(conn)
362
+ _migrate_project_memory_fts(conn)
363
+ tables = sorted(
364
+ row[0]
365
+ for row in conn.execute(
366
+ "SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name"
367
+ )
368
+ )
369
+
370
+ return {
371
+ "ok": True,
372
+ "status": "initialized",
373
+ "db_path": str(db_path),
374
+ "schema_path": str(schema_path),
375
+ "tables": tables,
376
+ }
@@ -0,0 +1,100 @@
1
+ #!/usr/bin/env python3
2
+ import argparse
3
+ from contextlib import closing
4
+ import sqlite3
5
+
6
+ from robert_agent.common import emit
7
+
8
+
9
+ SUMMARY_TABLES = [
10
+ "repos",
11
+ "github_sources",
12
+ "github_events",
13
+ "workstreams",
14
+ "tasks",
15
+ "attempts",
16
+ "worker_results",
17
+ "github_actions",
18
+ "notifications",
19
+ "agent_runs",
20
+ ]
21
+
22
+
23
+ def summarize_database(db_path):
24
+ with closing(sqlite3.connect(db_path)) as conn, conn:
25
+ summary = {
26
+ table: conn.execute(f"SELECT COUNT(*) FROM {table}").fetchone()[0]
27
+ for table in SUMMARY_TABLES
28
+ }
29
+ summary["rejected_worker_results"] = conn.execute(
30
+ """
31
+ SELECT COUNT(*)
32
+ FROM worker_results
33
+ WHERE json_extract(metadata_json, '$.audit.status') IN ('failed', 'policy_violation')
34
+ """
35
+ ).fetchone()[0]
36
+ summary["failed_github_actions"] = conn.execute(
37
+ """
38
+ SELECT COUNT(*)
39
+ FROM github_actions
40
+ WHERE audit_status IN ('failed', 'policy_violation')
41
+ """
42
+ ).fetchone()[0]
43
+ summary["pending_publish_actions"] = conn.execute(
44
+ """
45
+ SELECT COUNT(*)
46
+ FROM github_actions
47
+ WHERE audit_status = 'accepted'
48
+ AND publish_status = 'not_published'
49
+ """
50
+ ).fetchone()[0]
51
+ summary["publish_failed_actions"] = conn.execute(
52
+ """
53
+ SELECT COUNT(*)
54
+ FROM github_actions
55
+ WHERE audit_status = 'accepted'
56
+ AND publish_status = 'not_published'
57
+ AND json_extract(metadata_json, '$.publish.status') = 'publish_failed'
58
+ """
59
+ ).fetchone()[0]
60
+ summary["skipped_publish_actions"] = conn.execute(
61
+ """
62
+ SELECT COUNT(*)
63
+ FROM github_actions
64
+ WHERE audit_status = 'accepted'
65
+ AND publish_status = 'skipped'
66
+ """
67
+ ).fetchone()[0]
68
+ summary["pending_actor_permission_events"] = conn.execute(
69
+ """
70
+ SELECT COUNT(*)
71
+ FROM github_events
72
+ WHERE authorization_status = 'pending_actor_permission'
73
+ """
74
+ ).fetchone()[0]
75
+ summary["pending_authorization_events"] = conn.execute(
76
+ """
77
+ SELECT COUNT(*)
78
+ FROM github_events
79
+ WHERE authorization_status = 'pending_authorization'
80
+ """
81
+ ).fetchone()[0]
82
+ summary["skipped_closed_sources"] = conn.execute(
83
+ """
84
+ SELECT COUNT(*)
85
+ FROM audit_events
86
+ WHERE event_type = 'current_state_closed'
87
+ """
88
+ ).fetchone()[0]
89
+ return summary
90
+
91
+
92
+ def main(argv=None):
93
+ parser = argparse.ArgumentParser()
94
+ parser.add_argument("--db", required=True)
95
+ args = parser.parse_args(argv)
96
+ return emit({"ok": True, "status": "summarized", "summary": summarize_database(args.db)})
97
+
98
+
99
+ if __name__ == "__main__":
100
+ raise SystemExit(main())
@@ -0,0 +1,72 @@
1
+ #!/usr/bin/env python3
2
+ import argparse
3
+ from datetime import datetime, timezone
4
+
5
+ from robert_agent.common import emit
6
+
7
+
8
+ def _parse_time(value):
9
+ if isinstance(value, datetime):
10
+ return value
11
+ if value.endswith("Z"):
12
+ value = value[:-1] + "+00:00"
13
+ return datetime.fromisoformat(value)
14
+
15
+
16
+ def classify_attempt(
17
+ heartbeat_at,
18
+ started_at,
19
+ now=None,
20
+ stale_after_minutes=20,
21
+ hard_timeout_minutes=90,
22
+ ):
23
+ now = now or datetime.now(timezone.utc)
24
+ heartbeat_at = _parse_time(heartbeat_at)
25
+ started_at = _parse_time(started_at)
26
+ heartbeat_age = (now - heartbeat_at).total_seconds() / 60
27
+ runtime_age = (now - started_at).total_seconds() / 60
28
+
29
+ if runtime_age >= hard_timeout_minutes or heartbeat_age >= hard_timeout_minutes:
30
+ return {
31
+ "ok": False,
32
+ "status": "failed_timeout",
33
+ "terminate": True,
34
+ "heartbeat_age_minutes": heartbeat_age,
35
+ "runtime_minutes": runtime_age,
36
+ }
37
+ if heartbeat_age >= stale_after_minutes:
38
+ return {
39
+ "ok": True,
40
+ "status": "stale",
41
+ "terminate": False,
42
+ "heartbeat_age_minutes": heartbeat_age,
43
+ "runtime_minutes": runtime_age,
44
+ }
45
+ return {
46
+ "ok": True,
47
+ "status": "running",
48
+ "terminate": False,
49
+ "heartbeat_age_minutes": heartbeat_age,
50
+ "runtime_minutes": runtime_age,
51
+ }
52
+
53
+
54
+ def main(argv=None):
55
+ parser = argparse.ArgumentParser()
56
+ parser.add_argument("--heartbeat-at", required=True)
57
+ parser.add_argument("--started-at", required=True)
58
+ parser.add_argument("--stale-after-minutes", type=int, default=20)
59
+ parser.add_argument("--hard-timeout-minutes", type=int, default=90)
60
+ args = parser.parse_args(argv)
61
+ return emit(
62
+ classify_attempt(
63
+ heartbeat_at=args.heartbeat_at,
64
+ started_at=args.started_at,
65
+ stale_after_minutes=args.stale_after_minutes,
66
+ hard_timeout_minutes=args.hard_timeout_minutes,
67
+ )
68
+ )
69
+
70
+
71
+ if __name__ == "__main__":
72
+ raise SystemExit(main())