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,701 @@
1
+ """Scenario-specific SQLite read models for the DD GitHub workbench."""
2
+
3
+ import base64
4
+ import binascii
5
+ from collections import Counter, defaultdict
6
+ from contextlib import closing
7
+ import json
8
+ import shlex
9
+ import sqlite3
10
+
11
+
12
+ ACTIVE_TASK_LIFECYCLES = {"detected", "authorized", "classified", "queued", "running"}
13
+ BUCKETS = {"needs_attention", "working", "waiting", "history"}
14
+ BUCKET_ORDER = {"needs_attention": 0, "working": 1, "waiting": 2, "history": 3}
15
+ PRIORITY_ORDER = {"P0": 0, "P1": 1, "P2": 2, "P3": 3}
16
+ SORTS = {"priority", "updated-desc", "updated-asc"}
17
+ REQUIRED_TABLES = {
18
+ "repos",
19
+ "github_sources",
20
+ "workstreams",
21
+ "tasks",
22
+ "attempts",
23
+ "worker_phases",
24
+ "worker_results",
25
+ "github_actions",
26
+ "artifacts",
27
+ "notifications",
28
+ }
29
+
30
+
31
+ class WorkItemQueryError(ValueError):
32
+ """Raised for invalid workbench filters and cursors."""
33
+
34
+
35
+ def _decode_json(value, default=None):
36
+ if value in (None, ""):
37
+ return default
38
+ try:
39
+ return json.loads(value)
40
+ except (TypeError, ValueError):
41
+ return default
42
+
43
+
44
+ def _connect(db_path):
45
+ conn = sqlite3.connect(db_path)
46
+ conn.row_factory = sqlite3.Row
47
+ tables = {
48
+ row[0]
49
+ for row in conn.execute("SELECT name FROM sqlite_master WHERE type = 'table'")
50
+ }
51
+ missing = REQUIRED_TABLES - tables
52
+ if missing:
53
+ conn.close()
54
+ raise sqlite3.OperationalError(
55
+ "workbench schema is missing: " + ", ".join(sorted(missing))
56
+ )
57
+ return conn
58
+
59
+
60
+ def _encode_cursor(updated_at, workstream_id):
61
+ raw = json.dumps([updated_at, workstream_id], separators=(",", ":")).encode()
62
+ return base64.urlsafe_b64encode(raw).decode().rstrip("=")
63
+
64
+
65
+ def _decode_cursor(value):
66
+ try:
67
+ padded = value + "=" * (-len(value) % 4)
68
+ decoded = base64.urlsafe_b64decode(padded.encode()).decode()
69
+ updated_at, workstream_id = json.loads(decoded)
70
+ except (
71
+ ValueError,
72
+ TypeError,
73
+ UnicodeDecodeError,
74
+ binascii.Error,
75
+ json.JSONDecodeError,
76
+ ) as exc:
77
+ raise WorkItemQueryError("invalid cursor") from exc
78
+ if not isinstance(updated_at, str) or not isinstance(workstream_id, str):
79
+ raise WorkItemQueryError("invalid cursor")
80
+ return updated_at, workstream_id
81
+
82
+
83
+ def _group_rows(conn, sql, key):
84
+ grouped = defaultdict(list)
85
+ for row in conn.execute(sql):
86
+ item = dict(row)
87
+ grouped[item[key]].append(item)
88
+ return grouped
89
+
90
+
91
+ def _load_state(conn):
92
+ workstreams = [
93
+ dict(row)
94
+ for row in conn.execute(
95
+ """
96
+ SELECT
97
+ w.workstream_id,
98
+ w.repo_id,
99
+ w.primary_source_id,
100
+ w.origin_workstream_id,
101
+ w.lifecycle AS workstream_lifecycle,
102
+ w.active_task_id,
103
+ w.created_at,
104
+ w.updated_at,
105
+ r.full_name AS repo_full_name,
106
+ gs.source_key,
107
+ gs.source_type,
108
+ gs.number AS source_number,
109
+ gs.html_url AS github_url,
110
+ gs.title AS source_title,
111
+ gs.state AS source_state,
112
+ gs.author_login,
113
+ gs.source_updated_at
114
+ FROM workstreams w
115
+ JOIN repos r ON r.repo_id = w.repo_id
116
+ LEFT JOIN github_sources gs ON gs.source_id = w.primary_source_id
117
+ """
118
+ )
119
+ ]
120
+ tasks = _group_rows(
121
+ conn,
122
+ """
123
+ SELECT
124
+ task_id, workstream_id, lifecycle, parent_task_id, priority,
125
+ route_id, expected_output, created_at, updated_at
126
+ FROM tasks
127
+ ORDER BY updated_at DESC, task_id DESC
128
+ """,
129
+ "workstream_id",
130
+ )
131
+ attempts = _group_rows(
132
+ conn,
133
+ """
134
+ SELECT
135
+ attempt_id, task_id, attempt_no, status, worktree_path, branch_name,
136
+ started_at, heartbeat_at, finished_at, failure_json
137
+ FROM attempts
138
+ ORDER BY attempt_no DESC, started_at DESC, attempt_id DESC
139
+ """,
140
+ "task_id",
141
+ )
142
+ phases = _group_rows(
143
+ conn,
144
+ """
145
+ SELECT phase_id, attempt_id, phase, status, summary, next_step, created_at
146
+ FROM worker_phases
147
+ ORDER BY created_at, phase_id
148
+ """,
149
+ "attempt_id",
150
+ )
151
+ results = _group_rows(
152
+ conn,
153
+ """
154
+ SELECT
155
+ result_id, task_id, attempt_id, output_type, verification_json,
156
+ handoff, created_at, metadata_json
157
+ FROM worker_results
158
+ ORDER BY created_at DESC, result_id DESC
159
+ """,
160
+ "task_id",
161
+ )
162
+ actions = _group_rows(
163
+ conn,
164
+ """
165
+ SELECT
166
+ action_id, result_id, task_id, action_type, target_url, external_id,
167
+ audit_status, publish_status, created_at, metadata_json
168
+ FROM github_actions
169
+ ORDER BY created_at DESC, action_id DESC
170
+ """,
171
+ "task_id",
172
+ )
173
+ artifacts = _group_rows(
174
+ conn,
175
+ """
176
+ SELECT artifact_id, task_id, attempt_id, artifact_type, path, bytes, created_at
177
+ FROM artifacts
178
+ ORDER BY created_at DESC, artifact_id DESC
179
+ """,
180
+ "task_id",
181
+ )
182
+ notifications = _group_rows(
183
+ conn,
184
+ """
185
+ SELECT notification_id, task_id, notification_type, status, created_at
186
+ FROM notifications
187
+ ORDER BY created_at DESC, notification_id DESC
188
+ """,
189
+ "task_id",
190
+ )
191
+ return {
192
+ "workstreams": workstreams,
193
+ "tasks": tasks,
194
+ "attempts": attempts,
195
+ "phases": phases,
196
+ "results": results,
197
+ "actions": actions,
198
+ "artifacts": artifacts,
199
+ "notifications": notifications,
200
+ }
201
+
202
+
203
+ def _current_task(workstream, tasks):
204
+ active_task_id = workstream.get("active_task_id")
205
+ if active_task_id:
206
+ for task in tasks:
207
+ if task["task_id"] == active_task_id:
208
+ return task
209
+ return tasks[0] if tasks else None
210
+
211
+
212
+ def _publish_error(action):
213
+ metadata = _decode_json(action.get("metadata_json"), {}) or {}
214
+ publish = metadata.get("publish") if isinstance(metadata, dict) else None
215
+ if not isinstance(publish, dict):
216
+ return None
217
+ return publish.get("safe_error") or publish.get("error")
218
+
219
+
220
+ def _derive_operator_state(workstream, task, attempt, results, actions):
221
+ for action in actions:
222
+ if action["publish_status"] == "published":
223
+ continue
224
+ metadata = _decode_json(action.get("metadata_json"), {}) or {}
225
+ publish = metadata.get("publish") if isinstance(metadata, dict) else None
226
+ if isinstance(publish, dict) and publish.get("status") == "publish_failed":
227
+ return (
228
+ "needs_attention",
229
+ "publish_failed",
230
+ "Publishing the accepted GitHub action failed.",
231
+ )
232
+ if action["audit_status"] == "accepted" and action["publish_status"] == "skipped":
233
+ return (
234
+ "needs_attention",
235
+ "publish_skipped",
236
+ "An accepted GitHub action was skipped during publication.",
237
+ )
238
+ if task and task["lifecycle"] in {"failed", "canceled"}:
239
+ return (
240
+ "needs_attention",
241
+ "task_failed" if task["lifecycle"] == "failed" else "task_canceled",
242
+ "The current task did not complete successfully.",
243
+ )
244
+ if attempt and attempt["status"] == "stale":
245
+ return (
246
+ "needs_attention",
247
+ "worker_stale",
248
+ "The current worker attempt stopped updating its heartbeat.",
249
+ )
250
+ if any(action["audit_status"] in {"failed", "policy_violation"} for action in actions):
251
+ return (
252
+ "needs_attention",
253
+ "github_action_rejected",
254
+ "A planned GitHub action failed policy or audit checks.",
255
+ )
256
+ if workstream["workstream_lifecycle"] == "failed":
257
+ return (
258
+ "needs_attention",
259
+ "workstream_failed",
260
+ "The GitHub workstream is in a failed state.",
261
+ )
262
+ if (
263
+ workstream["workstream_lifecycle"] == "waiting_for_user"
264
+ or (task and task["lifecycle"] == "waiting_for_user")
265
+ ):
266
+ return (
267
+ "waiting",
268
+ "waiting_for_user",
269
+ "The agent is waiting for a trusted GitHub user response.",
270
+ )
271
+ if any(
272
+ action["audit_status"] == "accepted" and action["publish_status"] == "not_published"
273
+ for action in actions
274
+ ):
275
+ return (
276
+ "working",
277
+ "waiting_publish",
278
+ "An accepted GitHub action is waiting for publication.",
279
+ )
280
+ if results:
281
+ metadata = _decode_json(results[0].get("metadata_json"), {}) or {}
282
+ if not isinstance(metadata, dict) or not metadata.get("audit"):
283
+ return (
284
+ "working",
285
+ "result_pending_audit",
286
+ "The worker result is waiting for the next audit cycle.",
287
+ )
288
+ if task and task["lifecycle"] in ACTIVE_TASK_LIFECYCLES:
289
+ return (
290
+ "working",
291
+ "agent_working",
292
+ "The agent is actively processing this GitHub work item.",
293
+ )
294
+ if workstream["workstream_lifecycle"] == "active":
295
+ return (
296
+ "working",
297
+ "active_workstream",
298
+ "The GitHub workstream remains active.",
299
+ )
300
+ return (
301
+ "history",
302
+ "inactive",
303
+ "The GitHub workstream no longer requires active attention.",
304
+ )
305
+
306
+
307
+ def _source_payload(workstream):
308
+ return {
309
+ "source_id": workstream.get("primary_source_id"),
310
+ "repo_id": workstream.get("repo_id"),
311
+ "repo_full_name": workstream.get("repo_full_name"),
312
+ "type": workstream.get("source_type"),
313
+ "number": workstream.get("source_number"),
314
+ "title": workstream.get("source_title") or "Untitled GitHub work item",
315
+ "url": workstream.get("github_url"),
316
+ "state": workstream.get("source_state"),
317
+ "author_login": workstream.get("author_login"),
318
+ "updated_at": workstream.get("source_updated_at"),
319
+ "source_key": workstream.get("source_key"),
320
+ }
321
+
322
+
323
+ def _phase_for_attempt(state, attempt):
324
+ if not attempt:
325
+ return None
326
+ phases = state["phases"].get(attempt["attempt_id"], [])
327
+ return phases[-1]["phase"] if phases else None
328
+
329
+
330
+ def _item_from_workstream(state, workstream):
331
+ tasks = state["tasks"].get(workstream["workstream_id"], [])
332
+ task = _current_task(workstream, tasks)
333
+ task_id = task["task_id"] if task else None
334
+ task_attempts = state["attempts"].get(task_id, []) if task_id else []
335
+ attempt = task_attempts[0] if task_attempts else None
336
+ results = state["results"].get(task_id, []) if task_id else []
337
+ actions = state["actions"].get(task_id, []) if task_id else []
338
+ notifications = state["notifications"].get(task_id, []) if task_id else []
339
+ bucket, reason_code, reason_summary = _derive_operator_state(
340
+ workstream,
341
+ task,
342
+ attempt,
343
+ results,
344
+ actions,
345
+ )
346
+ failed_publish_actions = sum(
347
+ 1
348
+ for action in actions
349
+ if action["publish_status"] != "published" and _publish_error(action)
350
+ )
351
+ item = {
352
+ "id": workstream["workstream_id"],
353
+ "bucket": bucket,
354
+ "reason_code": reason_code,
355
+ "reason_summary": reason_summary,
356
+ "priority": task["priority"] if task else "P2",
357
+ "updated_at": (task or workstream).get("updated_at"),
358
+ "source": _source_payload(workstream),
359
+ "agent": {
360
+ "task_id": task_id,
361
+ "route_id": task.get("route_id") if task else None,
362
+ "attempt_id": attempt.get("attempt_id") if attempt else None,
363
+ "attempt_no": attempt.get("attempt_no") if attempt else None,
364
+ "attempt_status": attempt.get("status") if attempt else None,
365
+ "phase": _phase_for_attempt(state, attempt),
366
+ "heartbeat_at": attempt.get("heartbeat_at") if attempt else None,
367
+ },
368
+ "signals": {
369
+ "result_pending_audit": reason_code == "result_pending_audit",
370
+ "pending_publish_actions": sum(
371
+ 1
372
+ for action in actions
373
+ if action["audit_status"] == "accepted"
374
+ and action["publish_status"] == "not_published"
375
+ ),
376
+ "failed_publish_actions": failed_publish_actions,
377
+ "worker_resume_count": sum(
378
+ 1
379
+ for notification in notifications
380
+ if notification["notification_type"] == "worker_resume_prepared"
381
+ ),
382
+ },
383
+ "task_ids": [candidate["task_id"] for candidate in tasks],
384
+ }
385
+ return item
386
+
387
+
388
+ def _parse_query(query):
389
+ qualifiers = defaultdict(list)
390
+ terms = []
391
+ try:
392
+ tokens = shlex.split(query or "")
393
+ except ValueError as exc:
394
+ raise WorkItemQueryError("invalid query") from exc
395
+ for token in tokens:
396
+ if ":" in token:
397
+ name, value = token.split(":", 1)
398
+ if name in {"repo", "is", "author", "task"} and value:
399
+ qualifiers[name].append(value.lower())
400
+ continue
401
+ terms.append(token.lower())
402
+ return qualifiers, terms
403
+
404
+
405
+ def _text_contains_all(values, terms):
406
+ text = " ".join(str(value or "") for value in values).lower()
407
+ return all(term in text for term in terms)
408
+
409
+
410
+ def _matches_query(item, qualifiers, terms):
411
+ source = item["source"]
412
+ if qualifiers["repo"] and not any(
413
+ value in {
414
+ str(source.get("repo_id") or "").lower(),
415
+ str(source.get("repo_full_name") or "").lower(),
416
+ }
417
+ for value in qualifiers["repo"]
418
+ ):
419
+ return False
420
+ if qualifiers["author"] and str(source.get("author_login") or "").lower() not in qualifiers["author"]:
421
+ return False
422
+ if qualifiers["task"] and not all(
423
+ any(value in task_id.lower() for task_id in item["task_ids"])
424
+ for value in qualifiers["task"]
425
+ ):
426
+ return False
427
+ for value in qualifiers["is"]:
428
+ if value == "active" and item["bucket"] == "history":
429
+ return False
430
+ if value in {"attention", "needs_attention"} and item["bucket"] != "needs_attention":
431
+ return False
432
+ if value in {"working", "waiting", "history"} and item["bucket"] != value:
433
+ return False
434
+ if value in {"open", "closed"} and str(source.get("state") or "").lower() != value:
435
+ return False
436
+ return _text_contains_all(
437
+ [
438
+ source.get("title"),
439
+ source.get("number"),
440
+ source.get("source_key"),
441
+ *item["task_ids"],
442
+ ],
443
+ terms,
444
+ )
445
+
446
+
447
+ def _sort_items(items, sort):
448
+ if sort == "updated-asc":
449
+ return sorted(items, key=lambda item: (item["updated_at"] or "", item["id"]))
450
+ if sort == "updated-desc":
451
+ return sorted(
452
+ items,
453
+ key=lambda item: (item["updated_at"] or "", item["id"]),
454
+ reverse=True,
455
+ )
456
+ newest_first = sorted(
457
+ items,
458
+ key=lambda item: (item["updated_at"] or "", item["id"]),
459
+ reverse=True,
460
+ )
461
+ return sorted(
462
+ newest_first,
463
+ key=lambda item: (
464
+ BUCKET_ORDER[item["bucket"]],
465
+ PRIORITY_ORDER.get(item["priority"], 99),
466
+ ),
467
+ )
468
+
469
+
470
+ def list_work_items(
471
+ db_path,
472
+ *,
473
+ bucket=None,
474
+ repo=None,
475
+ actor=None,
476
+ query="",
477
+ sort="priority",
478
+ limit=30,
479
+ cursor=None,
480
+ ):
481
+ """Return a filtered and paginated PR/Issue-first work list."""
482
+
483
+ if bucket and bucket not in BUCKETS:
484
+ raise WorkItemQueryError("invalid bucket")
485
+ if sort not in SORTS:
486
+ raise WorkItemQueryError("invalid sort")
487
+ if not isinstance(limit, int) or not 1 <= limit <= 100:
488
+ raise WorkItemQueryError("limit must be between 1 and 100")
489
+ qualifiers, terms = _parse_query(query)
490
+ with closing(_connect(db_path)) as conn:
491
+ state = _load_state(conn)
492
+ items = [
493
+ _item_from_workstream(state, workstream)
494
+ for workstream in state["workstreams"]
495
+ ]
496
+ repositories = sorted(
497
+ {
498
+ (item["source"]["repo_id"], item["source"]["repo_full_name"])
499
+ for item in items
500
+ if item["source"]["repo_id"] and item["source"]["repo_full_name"]
501
+ },
502
+ key=lambda value: (value[1].lower(), value[0]),
503
+ )
504
+ actors = sorted(
505
+ {
506
+ item["source"]["author_login"]
507
+ for item in items
508
+ if item["source"]["author_login"]
509
+ },
510
+ key=str.lower,
511
+ )
512
+ items = [
513
+ item
514
+ for item in items
515
+ if (not repo or repo in {item["source"]["repo_id"], item["source"]["repo_full_name"]})
516
+ and (not actor or actor == item["source"]["author_login"])
517
+ and _matches_query(item, qualifiers, terms)
518
+ ]
519
+ counts = Counter(item["bucket"] for item in items)
520
+ if bucket:
521
+ items = [item for item in items if item["bucket"] == bucket]
522
+ elif not qualifiers["is"]:
523
+ items = [item for item in items if item["bucket"] != "history"]
524
+ items = _sort_items(items, sort)
525
+ if cursor:
526
+ cursor_pair = _decode_cursor(cursor)
527
+ try:
528
+ cursor_index = next(
529
+ index
530
+ for index, item in enumerate(items)
531
+ if (item["updated_at"] or "", item["id"]) == cursor_pair
532
+ )
533
+ except StopIteration as exc:
534
+ raise WorkItemQueryError("invalid cursor") from exc
535
+ items = items[cursor_index + 1 :]
536
+ page = items[:limit]
537
+ next_cursor = None
538
+ if len(items) > limit and page:
539
+ next_cursor = _encode_cursor(page[-1]["updated_at"] or "", page[-1]["id"])
540
+ for item in page:
541
+ item.pop("task_ids", None)
542
+ return {
543
+ "items": page,
544
+ "counts": {name: counts.get(name, 0) for name in BUCKET_ORDER},
545
+ "repositories": [
546
+ {"repo_id": repo_id, "full_name": full_name}
547
+ for repo_id, full_name in repositories
548
+ ],
549
+ "actors": actors,
550
+ "next_cursor": next_cursor,
551
+ }
552
+
553
+
554
+ def _safe_failure(value):
555
+ failure = _decode_json(value, {}) or {}
556
+ if not isinstance(failure, dict):
557
+ return None
558
+ return failure.get("safe_error") or failure.get("message") or failure.get("reason")
559
+
560
+
561
+ def _safe_result(row):
562
+ metadata = _decode_json(row.get("metadata_json"), {}) or {}
563
+ audit = metadata.get("audit") if isinstance(metadata, dict) else None
564
+ usage = metadata.get("usage") if isinstance(metadata, dict) else None
565
+ payload = {
566
+ "result_id": row["result_id"],
567
+ "task_id": row["task_id"],
568
+ "attempt_id": row["attempt_id"],
569
+ "output_type": row["output_type"],
570
+ "verification": _decode_json(row.get("verification_json"), []) or [],
571
+ "handoff": row.get("handoff") or "",
572
+ "created_at": row["created_at"],
573
+ "audit_status": audit.get("status") if isinstance(audit, dict) else None,
574
+ }
575
+ if isinstance(usage, dict):
576
+ payload["usage"] = {
577
+ key: usage.get(key)
578
+ for key in ("usage_available", "source", "usage", "total_cost_usd")
579
+ if key in usage
580
+ }
581
+ return payload
582
+
583
+
584
+ def _safe_action(row):
585
+ metadata = _decode_json(row.get("metadata_json"), {}) or {}
586
+ publish = metadata.get("publish") if isinstance(metadata, dict) else None
587
+ return {
588
+ "action_id": row["action_id"],
589
+ "result_id": row["result_id"],
590
+ "task_id": row["task_id"],
591
+ "action_type": row["action_type"],
592
+ "target_url": row["target_url"],
593
+ "external_id": row["external_id"],
594
+ "audit_status": row["audit_status"],
595
+ "publish_status": row["publish_status"],
596
+ "safe_error": publish.get("safe_error") if isinstance(publish, dict) else None,
597
+ "created_at": row["created_at"],
598
+ }
599
+
600
+
601
+ def get_work_item(db_path, workstream_id):
602
+ """Return the structured detail for one workstream."""
603
+
604
+ with closing(_connect(db_path)) as conn:
605
+ state = _load_state(conn)
606
+ workstream = next(
607
+ (
608
+ candidate
609
+ for candidate in state["workstreams"]
610
+ if candidate["workstream_id"] == workstream_id
611
+ ),
612
+ None,
613
+ )
614
+ if not workstream:
615
+ return None
616
+ item = _item_from_workstream(state, workstream)
617
+ related_sources = [
618
+ {
619
+ "relationship": row["relationship"],
620
+ "source_id": row["source_id"],
621
+ "source_key": row["source_key"],
622
+ "type": row["source_type"],
623
+ "number": row["number"],
624
+ "title": row["title"],
625
+ "url": row["html_url"],
626
+ "state": row["state"],
627
+ }
628
+ for row in conn.execute(
629
+ """
630
+ SELECT
631
+ ws.relationship, gs.source_id, gs.source_key, gs.source_type,
632
+ gs.number, gs.title, gs.html_url, gs.state
633
+ FROM workstream_sources ws
634
+ JOIN github_sources gs ON gs.source_id = ws.source_id
635
+ WHERE ws.workstream_id = ?
636
+ ORDER BY ws.created_at, gs.source_id
637
+ """,
638
+ (workstream_id,),
639
+ )
640
+ ]
641
+ tasks = state["tasks"].get(workstream_id, [])
642
+ task_ids = {task["task_id"] for task in tasks}
643
+ attempts = [row for task_id in task_ids for row in state["attempts"].get(task_id, [])]
644
+ attempt_ids = {attempt["attempt_id"] for attempt in attempts}
645
+ phases = [row for attempt_id in attempt_ids for row in state["phases"].get(attempt_id, [])]
646
+ results = [row for task_id in task_ids for row in state["results"].get(task_id, [])]
647
+ actions = [row for task_id in task_ids for row in state["actions"].get(task_id, [])]
648
+ artifacts = [row for task_id in task_ids for row in state["artifacts"].get(task_id, [])]
649
+ return {
650
+ "id": workstream_id,
651
+ "source": item["source"],
652
+ "related_sources": related_sources,
653
+ "operator_state": {
654
+ "bucket": item["bucket"],
655
+ "reason_code": item["reason_code"],
656
+ "reason_summary": item["reason_summary"],
657
+ },
658
+ "tasks": [
659
+ {
660
+ "task_id": row["task_id"],
661
+ "parent_task_id": row["parent_task_id"],
662
+ "lifecycle": row["lifecycle"],
663
+ "priority": row["priority"],
664
+ "route_id": row["route_id"],
665
+ "expected_output": row["expected_output"],
666
+ "created_at": row["created_at"],
667
+ "updated_at": row["updated_at"],
668
+ }
669
+ for row in sorted(tasks, key=lambda row: (row["created_at"], row["task_id"]))
670
+ ],
671
+ "attempts": [
672
+ {
673
+ "attempt_id": row["attempt_id"],
674
+ "task_id": row["task_id"],
675
+ "attempt_no": row["attempt_no"],
676
+ "status": row["status"],
677
+ "worktree_path": row["worktree_path"],
678
+ "branch_name": row["branch_name"],
679
+ "started_at": row["started_at"],
680
+ "heartbeat_at": row["heartbeat_at"],
681
+ "finished_at": row["finished_at"],
682
+ "failure_summary": _safe_failure(row["failure_json"]),
683
+ }
684
+ for row in attempts
685
+ ],
686
+ "phases": phases,
687
+ "results": [_safe_result(row) for row in results],
688
+ "actions": [_safe_action(row) for row in actions],
689
+ "artifacts": [
690
+ {
691
+ "artifact_id": row["artifact_id"],
692
+ "task_id": row["task_id"],
693
+ "attempt_id": row["attempt_id"],
694
+ "artifact_type": row["artifact_type"],
695
+ "bytes": row["bytes"],
696
+ "created_at": row["created_at"],
697
+ "preview_available": True,
698
+ }
699
+ for row in artifacts
700
+ ],
701
+ }
@@ -0,0 +1 @@
1
+ """Robert worker support package."""