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,363 @@
1
+ #!/usr/bin/env python3
2
+ import argparse
3
+ from contextlib import closing
4
+ import json
5
+ import sqlite3
6
+ import tempfile
7
+ import time
8
+ from pathlib import Path
9
+ from uuid import uuid4
10
+
11
+ from robert_agent.common import emit
12
+ from robert_agent import run_once
13
+ from robert_agent import validate_config
14
+ from robert_agent.resource_files import resource
15
+
16
+
17
+ DEFAULT_WORKFLOW = resource("workflow.yml")
18
+
19
+
20
+ def _acceptance_id():
21
+ return f"live-acceptance-{uuid4().hex[:12]}"
22
+
23
+
24
+ def _json_load(value):
25
+ if not value:
26
+ return {}
27
+ try:
28
+ parsed = json.loads(value)
29
+ except (TypeError, ValueError):
30
+ return {}
31
+ return parsed if isinstance(parsed, dict) else {}
32
+
33
+
34
+ def _tail(path, limit=4000):
35
+ if not path:
36
+ return ""
37
+ path = Path(path)
38
+ if not path.exists():
39
+ return ""
40
+ text = path.read_text(encoding="utf-8", errors="replace")
41
+ return text[-limit:]
42
+
43
+
44
+ def _write_json(path, payload):
45
+ path.parent.mkdir(parents=True, exist_ok=True)
46
+ path.write_text(json.dumps(payload, ensure_ascii=False, indent=2, sort_keys=True), encoding="utf-8")
47
+
48
+
49
+ def _workspace_dir(workspace_dir):
50
+ if workspace_dir:
51
+ path = Path(workspace_dir).expanduser().resolve()
52
+ path.mkdir(parents=True, exist_ok=True)
53
+ return path
54
+ return Path(tempfile.mkdtemp(prefix="dd-live-acceptance-")).resolve()
55
+
56
+
57
+ def _isolated_config(source_config, workspace_dir):
58
+ config = validate_config.validate_config(source_config, skip_external=True)
59
+ if not config["ok"]:
60
+ return config
61
+ workers = [
62
+ {
63
+ "name": worker["name"],
64
+ "agent": worker["agent"],
65
+ "command": worker["command"],
66
+ "default_model": worker["default_model"],
67
+ "default_effort": worker["default_effort"],
68
+ }
69
+ for worker in config["workers"]
70
+ ]
71
+ isolated = {
72
+ "data_dir": str(workspace_dir / "data"),
73
+ "database": "dd.sqlite3",
74
+ "workers": workers,
75
+ "route_worker_models": config.get("route_worker_models", {}),
76
+ "max_concurrency": 1,
77
+ "stale_after_minutes": config.get("stale_after_minutes", 20),
78
+ "hard_timeout_minutes": config.get("hard_timeout_minutes", 90),
79
+ "worker_startup_grace_seconds": config.get("worker_startup_grace_seconds", 300),
80
+ "lease_ttl_minutes": config.get("lease_ttl_minutes", 9),
81
+ "repos": config["repos"],
82
+ }
83
+ config_path = workspace_dir / "config.json"
84
+ _write_json(config_path, isolated)
85
+ return {
86
+ "ok": True,
87
+ "status": "prepared",
88
+ "source_config": config,
89
+ "config_path": str(config_path),
90
+ "db_path": str(workspace_dir / "data" / "dd.sqlite3"),
91
+ }
92
+
93
+
94
+ def _default_fixture(config, workspace_dir, issue_number=77):
95
+ repo = config["repos"][0]
96
+ actor = repo["trusted_actors"][0]
97
+ github_account = repo["github_account"]
98
+ full_name = repo["full_name"]
99
+ event_id = _acceptance_id()
100
+ fixture_path = workspace_dir / "fixture.json"
101
+ _write_json(
102
+ fixture_path,
103
+ {
104
+ "events": [
105
+ {
106
+ "id": event_id,
107
+ "number": issue_number,
108
+ "source_type": "issue",
109
+ "event_type": "comment",
110
+ "actor_login": actor,
111
+ "author_association": "OWNER",
112
+ "body": f"@{github_account} please analyze this controlled live acceptance event",
113
+ "intent": "analysis",
114
+ "url": f"https://github.com/{full_name}/issues/{issue_number}",
115
+ }
116
+ ]
117
+ },
118
+ )
119
+ return fixture_path
120
+
121
+
122
+ def _latest_artifacts(conn, task_id):
123
+ rows = conn.execute(
124
+ """
125
+ SELECT artifact_type, path
126
+ FROM artifacts
127
+ WHERE task_id = ?
128
+ AND artifact_type IN ('prompt', 'worker_stdout', 'worker_stderr')
129
+ ORDER BY created_at, artifact_id
130
+ """,
131
+ (task_id,),
132
+ ).fetchall()
133
+ artifacts = {}
134
+ for artifact_type, path in rows:
135
+ artifacts[artifact_type] = path
136
+ return artifacts
137
+
138
+
139
+ def _current_evidence(db_path):
140
+ db_path = Path(db_path)
141
+ if not db_path.exists():
142
+ return {"db_exists": False}
143
+ with closing(sqlite3.connect(db_path)) as conn:
144
+ conn.execute("PRAGMA foreign_keys = ON")
145
+ row = conn.execute(
146
+ """
147
+ SELECT
148
+ t.task_id,
149
+ t.lifecycle,
150
+ rd.route_id,
151
+ a.attempt_id,
152
+ a.status,
153
+ wr.result_id,
154
+ wr.output_type,
155
+ wr.metadata_json
156
+ FROM tasks t
157
+ LEFT JOIN route_decisions rd ON rd.task_id = t.task_id
158
+ LEFT JOIN attempts a ON a.task_id = t.task_id
159
+ LEFT JOIN worker_results wr ON wr.attempt_id = a.attempt_id
160
+ ORDER BY t.created_at DESC, a.attempt_no DESC, wr.created_at DESC
161
+ LIMIT 1
162
+ """
163
+ ).fetchone()
164
+ if not row:
165
+ return {"db_exists": True}
166
+ (
167
+ task_id,
168
+ task_lifecycle,
169
+ route_id,
170
+ attempt_id,
171
+ attempt_status,
172
+ result_id,
173
+ output_type,
174
+ result_metadata_json,
175
+ ) = row
176
+ artifacts = _latest_artifacts(conn, task_id)
177
+ action_rows = conn.execute(
178
+ """
179
+ SELECT audit_status, publish_status, COUNT(*)
180
+ FROM github_actions
181
+ WHERE task_id = ?
182
+ GROUP BY audit_status, publish_status
183
+ """,
184
+ (task_id,),
185
+ ).fetchall()
186
+ accepted_count = sum(count for audit, _publish, count in action_rows if audit == "accepted")
187
+ not_published_count = sum(count for _audit, publish, count in action_rows if publish == "not_published")
188
+ evidence = {
189
+ "db_exists": True,
190
+ "task_id": task_id,
191
+ "task_lifecycle": task_lifecycle,
192
+ "route_id": route_id,
193
+ "attempt_id": attempt_id,
194
+ "attempt_status": attempt_status,
195
+ "result_id": result_id,
196
+ "output_type": output_type,
197
+ "worker_result_metadata": _json_load(result_metadata_json),
198
+ "action_counts": {
199
+ "accepted": accepted_count,
200
+ "not_published": not_published_count,
201
+ },
202
+ "artifacts": artifacts,
203
+ }
204
+ stdout_path = artifacts.get("worker_stdout")
205
+ stderr_path = artifacts.get("worker_stderr")
206
+ if stdout_path:
207
+ evidence["worker_stdout_tail"] = _tail(stdout_path)
208
+ if stderr_path:
209
+ evidence["worker_stderr_tail"] = _tail(stderr_path)
210
+ return evidence
211
+
212
+
213
+ def _wait_for_worker_result(db_path, timeout_seconds, poll_interval_seconds):
214
+ deadline = time.monotonic() + timeout_seconds
215
+ evidence = _current_evidence(db_path)
216
+ while time.monotonic() <= deadline:
217
+ evidence = _current_evidence(db_path)
218
+ if evidence.get("result_id") and evidence.get("attempt_status") == "completed":
219
+ return evidence
220
+ time.sleep(poll_interval_seconds)
221
+ return evidence
222
+
223
+
224
+ def live_worker_acceptance(
225
+ config_path,
226
+ workspace_dir=None,
227
+ fixture_path=None,
228
+ timeout_seconds=600,
229
+ poll_interval_seconds=2,
230
+ workflow_path=DEFAULT_WORKFLOW,
231
+ ):
232
+ workspace = _workspace_dir(workspace_dir)
233
+ prepared = _isolated_config(config_path, workspace)
234
+ if not prepared["ok"]:
235
+ return prepared
236
+
237
+ isolated_config_path = Path(prepared["config_path"])
238
+ db_path = Path(prepared["db_path"])
239
+ if fixture_path:
240
+ dispatch_fixture = Path(fixture_path).expanduser().resolve()
241
+ else:
242
+ dispatch_fixture = _default_fixture(prepared["source_config"], workspace)
243
+
244
+ first_run = run_once.run_once(
245
+ isolated_config_path,
246
+ workflow_path=workflow_path,
247
+ fixture_path=dispatch_fixture,
248
+ dry_run=False,
249
+ skip_external=True,
250
+ )
251
+ if not first_run["ok"]:
252
+ evidence = _current_evidence(db_path)
253
+ return {
254
+ "ok": False,
255
+ "status": first_run.get("status", "failed_dispatch"),
256
+ "workspace_dir": str(workspace),
257
+ "config_path": str(isolated_config_path),
258
+ "fixture_path": str(dispatch_fixture),
259
+ "db_path": str(db_path),
260
+ "first_run": first_run,
261
+ "evidence": evidence,
262
+ "safe_error": first_run.get("safe_error", "live worker dispatch failed"),
263
+ }
264
+
265
+ worker_evidence = _wait_for_worker_result(db_path, timeout_seconds, poll_interval_seconds)
266
+ if not worker_evidence.get("result_id"):
267
+ return {
268
+ "ok": False,
269
+ "status": "worker_result_timeout",
270
+ "workspace_dir": str(workspace),
271
+ "config_path": str(isolated_config_path),
272
+ "fixture_path": str(dispatch_fixture),
273
+ "db_path": str(db_path),
274
+ "first_run": first_run,
275
+ "evidence": worker_evidence,
276
+ "safe_error": "worker did not record a result before timeout",
277
+ }
278
+
279
+ second_run = run_once.run_once(
280
+ isolated_config_path,
281
+ workflow_path=workflow_path,
282
+ dry_run=True,
283
+ skip_external=True,
284
+ )
285
+ final_evidence = _current_evidence(db_path)
286
+ if not second_run["ok"]:
287
+ return {
288
+ "ok": False,
289
+ "status": second_run.get("status", "failed_audit_or_publish"),
290
+ "workspace_dir": str(workspace),
291
+ "config_path": str(isolated_config_path),
292
+ "fixture_path": str(dispatch_fixture),
293
+ "db_path": str(db_path),
294
+ "first_run": first_run,
295
+ "second_run": second_run,
296
+ "evidence": final_evidence,
297
+ "safe_error": second_run.get("safe_error", "dry-run audit or publication failed"),
298
+ }
299
+ if final_evidence["action_counts"]["accepted"] == 0:
300
+ return {
301
+ "ok": False,
302
+ "status": "audit_not_accepted",
303
+ "workspace_dir": str(workspace),
304
+ "config_path": str(isolated_config_path),
305
+ "fixture_path": str(dispatch_fixture),
306
+ "db_path": str(db_path),
307
+ "first_run": first_run,
308
+ "second_run": second_run,
309
+ "evidence": final_evidence,
310
+ "safe_error": "worker result did not produce an accepted GitHub action",
311
+ }
312
+
313
+ return {
314
+ "ok": True,
315
+ "status": "completed",
316
+ "workspace_dir": str(workspace),
317
+ "config_path": str(isolated_config_path),
318
+ "fixture_path": str(dispatch_fixture),
319
+ "db_path": str(db_path),
320
+ "route_id": final_evidence.get("route_id"),
321
+ "task_id": final_evidence.get("task_id"),
322
+ "attempt_id": final_evidence.get("attempt_id"),
323
+ "worker_result": {
324
+ "result_id": final_evidence.get("result_id"),
325
+ "output_type": final_evidence.get("output_type"),
326
+ "metadata": final_evidence.get("worker_result_metadata", {}),
327
+ },
328
+ "action_counts": final_evidence["action_counts"],
329
+ "dry_run_publication": second_run.get("publish_result", {}),
330
+ "first_run": first_run,
331
+ "second_run": second_run,
332
+ "artifacts": final_evidence.get("artifacts", {}),
333
+ "worker_stdout_tail": final_evidence.get("worker_stdout_tail", ""),
334
+ "worker_stderr_tail": final_evidence.get("worker_stderr_tail", ""),
335
+ }
336
+
337
+
338
+ def main(argv=None):
339
+ parser = argparse.ArgumentParser()
340
+ parser.add_argument("--config", required=True)
341
+ parser.add_argument("--workspace-dir")
342
+ parser.add_argument("--fixture")
343
+ parser.add_argument("--workflow", default=str(DEFAULT_WORKFLOW))
344
+ parser.add_argument("--timeout-seconds", type=float, default=600)
345
+ parser.add_argument("--poll-interval-seconds", type=float, default=2)
346
+ args = parser.parse_args(argv)
347
+ result = live_worker_acceptance(
348
+ args.config,
349
+ workspace_dir=args.workspace_dir,
350
+ fixture_path=args.fixture,
351
+ timeout_seconds=args.timeout_seconds,
352
+ poll_interval_seconds=args.poll_interval_seconds,
353
+ workflow_path=Path(args.workflow),
354
+ )
355
+ if result.get("ok"):
356
+ return emit(result, 0)
357
+ if result.get("status") == "failed_config":
358
+ return emit(result, 3)
359
+ return emit(result, 2)
360
+
361
+
362
+ if __name__ == "__main__":
363
+ raise SystemExit(main())
@@ -0,0 +1,344 @@
1
+ #!/usr/bin/env python3
2
+ import argparse
3
+ from contextlib import closing
4
+ import json
5
+ import os
6
+ import signal
7
+ import sqlite3
8
+ import subprocess
9
+ import tempfile
10
+ import time
11
+ from pathlib import Path
12
+ from uuid import uuid4
13
+
14
+ from robert_agent.common import emit
15
+ from robert_agent import run_once
16
+ from robert_agent import validate_config
17
+ from robert_agent.resource_files import resource
18
+
19
+
20
+ DEFAULT_WORKFLOW = resource("workflow.yml")
21
+
22
+
23
+ def _acceptance_id():
24
+ return f"worktree-acceptance-{uuid4().hex[:12]}"
25
+
26
+
27
+ def _write_json(path, payload):
28
+ path.parent.mkdir(parents=True, exist_ok=True)
29
+ path.write_text(json.dumps(payload, ensure_ascii=False, indent=2, sort_keys=True), encoding="utf-8")
30
+
31
+
32
+ def _workspace_dir(workspace_dir):
33
+ if workspace_dir:
34
+ path = Path(workspace_dir).expanduser().resolve()
35
+ path.mkdir(parents=True, exist_ok=True)
36
+ return path
37
+ return Path(tempfile.mkdtemp(prefix="dd-worktree-acceptance-")).resolve()
38
+
39
+
40
+ def _run(args, cwd=None):
41
+ completed = subprocess.run(
42
+ args,
43
+ cwd=cwd,
44
+ text=True,
45
+ capture_output=True,
46
+ check=True,
47
+ )
48
+ return {
49
+ "args": [str(arg) for arg in args],
50
+ "cwd": str(cwd) if cwd else "",
51
+ "stdout": completed.stdout.strip(),
52
+ "stderr": completed.stderr.strip(),
53
+ }
54
+
55
+
56
+ def _controlled_checkout(workspace_dir):
57
+ source = workspace_dir / "upstream-source"
58
+ bare = workspace_dir / "upstream.git"
59
+ checkout = workspace_dir / "checkout"
60
+ commands = []
61
+ source.mkdir(parents=True, exist_ok=True)
62
+ commands.append(_run(["git", "init", "-b", "main"], cwd=source))
63
+ (source / "README.md").write_text("# acceptance\n", encoding="utf-8")
64
+ commands.append(_run(["git", "add", "README.md"], cwd=source))
65
+ commands.append(
66
+ _run(
67
+ [
68
+ "git",
69
+ "-c",
70
+ "user.name=DD Acceptance",
71
+ "-c",
72
+ "user.email=dd-acceptance@example.invalid",
73
+ "commit",
74
+ "-m",
75
+ "initial acceptance fixture",
76
+ ],
77
+ cwd=source,
78
+ )
79
+ )
80
+ commands.append(_run(["git", "clone", "--bare", str(source), str(bare)]))
81
+ commands.append(_run(["git", "clone", str(bare), str(checkout)]))
82
+ commands.append(_run(["git", "remote", "rename", "origin", "upstream"], cwd=checkout))
83
+ worktree_root = checkout / ".worktrees"
84
+ worktree_root.mkdir(parents=True, exist_ok=True)
85
+ return {
86
+ "repo_root": checkout,
87
+ "worktree_root": worktree_root,
88
+ "upstream": bare,
89
+ "commands": commands,
90
+ }
91
+
92
+
93
+ def _mock_worker(workspace_dir):
94
+ worker = workspace_dir / "mock-worker.py"
95
+ worker.write_text(
96
+ """#!/usr/bin/env python3
97
+ import time
98
+
99
+ time.sleep(30)
100
+ """,
101
+ encoding="utf-8",
102
+ )
103
+ os.chmod(worker, 0o755)
104
+ return worker
105
+
106
+
107
+ def _config(source_config_path, workspace_dir, checkout, worker):
108
+ source_config = validate_config.validate_config(source_config_path, skip_external=True)
109
+ if not source_config["ok"]:
110
+ return source_config
111
+ repo = source_config["repos"][0]
112
+ controlled = {
113
+ "data_dir": str(workspace_dir / "data"),
114
+ "database": "dd.sqlite3",
115
+ "worker_command": str(worker),
116
+ "max_concurrency": 1,
117
+ "worker_startup_grace_seconds": 30,
118
+ "stale_after_minutes": source_config.get("stale_after_minutes", 20),
119
+ "hard_timeout_minutes": source_config.get("hard_timeout_minutes", 90),
120
+ "lease_ttl_minutes": source_config.get("lease_ttl_minutes", 9),
121
+ "repos": [
122
+ {
123
+ "full_name": repo["full_name"],
124
+ "github_account": repo["github_account"],
125
+ "trusted_actors": repo["trusted_actors"],
126
+ "default_base_branch": "main",
127
+ "repo_root": str(checkout["repo_root"]),
128
+ "worktree_root": str(checkout["worktree_root"]),
129
+ }
130
+ ],
131
+ }
132
+ config_path = workspace_dir / "config.json"
133
+ _write_json(config_path, controlled)
134
+ return {
135
+ "ok": True,
136
+ "status": "prepared",
137
+ "config_path": config_path,
138
+ "db_path": workspace_dir / "data" / "dd.sqlite3",
139
+ "repo": controlled["repos"][0],
140
+ }
141
+
142
+
143
+ def _fixture(config, workspace_dir, issue_number=77):
144
+ repo = config["repo"]
145
+ fixture_path = workspace_dir / "fixture.json"
146
+ _write_json(
147
+ fixture_path,
148
+ {
149
+ "events": [
150
+ {
151
+ "id": _acceptance_id(),
152
+ "number": issue_number,
153
+ "source_type": "issue",
154
+ "event_type": "comment",
155
+ "actor_login": repo["trusted_actors"][0],
156
+ "author_association": "OWNER",
157
+ "title": "Fix worktree acceptance",
158
+ "body": f"@{repo['github_account']} please fix this controlled worktree acceptance bug",
159
+ "intent": "bug_fix",
160
+ "url": f"https://github.com/{repo['full_name']}/issues/{issue_number}",
161
+ }
162
+ ]
163
+ },
164
+ )
165
+ return fixture_path
166
+
167
+
168
+ def _json_load(value):
169
+ if not value:
170
+ return {}
171
+ try:
172
+ parsed = json.loads(value)
173
+ except (TypeError, ValueError):
174
+ return {}
175
+ return parsed if isinstance(parsed, dict) else {}
176
+
177
+
178
+ def _attempt_evidence(db_path):
179
+ with closing(sqlite3.connect(db_path)) as conn:
180
+ row = conn.execute(
181
+ """
182
+ SELECT
183
+ t.route_id,
184
+ t.lifecycle,
185
+ a.attempt_id,
186
+ a.status,
187
+ a.worktree_path,
188
+ a.branch_name,
189
+ a.metadata_json
190
+ FROM attempts a
191
+ JOIN tasks t ON t.task_id = a.task_id
192
+ ORDER BY a.started_at DESC, a.attempt_id DESC
193
+ LIMIT 1
194
+ """
195
+ ).fetchone()
196
+ if not row:
197
+ return {}
198
+ route_id, task_lifecycle, attempt_id, attempt_status, worktree_path, branch_name, metadata_json = row
199
+ metadata = _json_load(metadata_json)
200
+ return {
201
+ "route_id": route_id,
202
+ "task_lifecycle": task_lifecycle,
203
+ "attempt_id": attempt_id,
204
+ "attempt_status": attempt_status,
205
+ "worktree_path": worktree_path,
206
+ "branch_name": branch_name,
207
+ "dispatch": metadata.get("dispatch") or {},
208
+ }
209
+
210
+
211
+ def _git_branch(worktree_path):
212
+ return subprocess.run(
213
+ ["git", "branch", "--show-current"],
214
+ cwd=worktree_path,
215
+ text=True,
216
+ capture_output=True,
217
+ check=True,
218
+ ).stdout.strip()
219
+
220
+
221
+ def _worktree_list_contains_branch(repo_root, branch_name):
222
+ completed = subprocess.run(
223
+ ["git", "worktree", "list", "--porcelain"],
224
+ cwd=repo_root,
225
+ text=True,
226
+ capture_output=True,
227
+ check=True,
228
+ )
229
+ return f"branch refs/heads/{branch_name}" in completed.stdout
230
+
231
+
232
+ def _terminate_worker(dispatch_result):
233
+ pid = dispatch_result.get("pid")
234
+ if not pid:
235
+ return {"status": "not_signalled", "reason": "missing_pid"}
236
+ try:
237
+ pid = int(pid)
238
+ except (TypeError, ValueError):
239
+ return {"status": "not_signalled", "reason": "invalid_pid", "pid": pid}
240
+ try:
241
+ os.kill(pid, signal.SIGTERM)
242
+ except ProcessLookupError:
243
+ return {"status": "not_found", "pid": pid}
244
+ proc = run_once.dispatch._BACKGROUND_PROCESSES.get(pid)
245
+ if proc:
246
+ try:
247
+ proc.wait(timeout=2)
248
+ except subprocess.TimeoutExpired:
249
+ return {"status": "signalled", "pid": pid}
250
+ return {"status": "terminated", "pid": pid}
251
+ for _ in range(20):
252
+ try:
253
+ os.kill(pid, 0)
254
+ except ProcessLookupError:
255
+ return {"status": "terminated", "pid": pid}
256
+ time.sleep(0.05)
257
+ return {"status": "signalled", "pid": pid}
258
+
259
+
260
+ def live_worktree_acceptance(config_path, workspace_dir=None, workflow_path=DEFAULT_WORKFLOW):
261
+ workspace = _workspace_dir(workspace_dir)
262
+ checkout = _controlled_checkout(workspace)
263
+ worker = _mock_worker(workspace)
264
+ config = _config(config_path, workspace, checkout, worker)
265
+ if not config["ok"]:
266
+ return config
267
+ fixture = _fixture(config, workspace)
268
+ result = run_once.run_once(
269
+ config["config_path"],
270
+ workflow_path=workflow_path,
271
+ fixture_path=fixture,
272
+ dry_run=False,
273
+ skip_external=True,
274
+ )
275
+ evidence = _attempt_evidence(config["db_path"])
276
+ cleanup = _terminate_worker(evidence.get("dispatch") or {})
277
+ if not result["ok"]:
278
+ return {
279
+ "ok": False,
280
+ "status": result.get("status", "failed_worktree_acceptance"),
281
+ "safe_error": result.get("safe_error", "worktree acceptance run failed"),
282
+ "workspace_dir": str(workspace),
283
+ "config_path": str(config["config_path"]),
284
+ "fixture_path": str(fixture),
285
+ "db_path": str(config["db_path"]),
286
+ "run_once": result,
287
+ "evidence": evidence,
288
+ "cleanup": cleanup,
289
+ }
290
+
291
+ worktree_path = Path(evidence.get("worktree_path") or "")
292
+ branch_name = evidence.get("branch_name") or ""
293
+ git_branch = _git_branch(worktree_path) if worktree_path.exists() else ""
294
+ in_worktree_list = _worktree_list_contains_branch(checkout["repo_root"], branch_name)
295
+ ok = (
296
+ evidence.get("route_id") == "new-pr"
297
+ and evidence.get("attempt_status") == "running"
298
+ and worktree_path.is_dir()
299
+ and git_branch == branch_name
300
+ and in_worktree_list
301
+ )
302
+ return {
303
+ "ok": ok,
304
+ "status": "completed" if ok else "failed_worktree_verification",
305
+ "workspace_dir": str(workspace),
306
+ "config_path": str(config["config_path"]),
307
+ "fixture_path": str(fixture),
308
+ "db_path": str(config["db_path"]),
309
+ "repo_root": str(checkout["repo_root"]),
310
+ "worktree_root": str(checkout["worktree_root"]),
311
+ "route_id": evidence.get("route_id"),
312
+ "task_lifecycle": evidence.get("task_lifecycle"),
313
+ "attempt_id": evidence.get("attempt_id"),
314
+ "attempt_status": evidence.get("attempt_status"),
315
+ "worktree_path": str(worktree_path),
316
+ "branch_name": branch_name,
317
+ "git_branch": git_branch,
318
+ "git_worktree_list_contains_branch": in_worktree_list,
319
+ "run_once": result,
320
+ "checkout_commands": checkout["commands"],
321
+ "cleanup": cleanup,
322
+ }
323
+
324
+
325
+ def main(argv=None):
326
+ parser = argparse.ArgumentParser()
327
+ parser.add_argument("--config", required=True)
328
+ parser.add_argument("--workspace-dir")
329
+ parser.add_argument("--workflow", default=str(DEFAULT_WORKFLOW))
330
+ args = parser.parse_args(argv)
331
+ result = live_worktree_acceptance(
332
+ args.config,
333
+ workspace_dir=args.workspace_dir,
334
+ workflow_path=Path(args.workflow),
335
+ )
336
+ if result.get("ok"):
337
+ return emit(result, 0)
338
+ if result.get("status") == "failed_config":
339
+ return emit(result, 3)
340
+ return emit(result, 2)
341
+
342
+
343
+ if __name__ == "__main__":
344
+ raise SystemExit(main())