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/daemon.py ADDED
@@ -0,0 +1,607 @@
1
+ #!/usr/bin/env python3
2
+ import argparse
3
+ from contextlib import closing
4
+ from dataclasses import dataclass
5
+ from datetime import datetime, timedelta
6
+ import json
7
+ from pathlib import Path
8
+ import signal
9
+ import subprocess
10
+ import sys
11
+ import time
12
+ from uuid import uuid4
13
+
14
+ from robert_agent.common import emit
15
+ from robert_agent import daemon_state, storage
16
+ from robert_agent import loop_engine
17
+ from robert_agent import validate_config
18
+
19
+
20
+ SCRIPT_ROOT = Path(__file__).resolve().parent
21
+ AGENTS_ROOT = SCRIPT_ROOT.parents[1]
22
+ _STOP_REQUESTED = False
23
+
24
+
25
+ @dataclass
26
+ class DaemonContext:
27
+ config_path: str
28
+ workflow_path: str | None
29
+ db_path: Path
30
+ repo_id: str
31
+ max_concurrency: int
32
+ daemon_config: dict
33
+ daemon_run_id: str
34
+ lease_id: str
35
+ repo_ids: list[str] | None = None
36
+ dry_run: bool = False
37
+ next_live_poll_at: str | None = None
38
+ startup_live_poll_pending: bool = False
39
+ rate_limit_decision_cache: dict | None = None
40
+ rate_limit_cache_expires_at: str | None = None
41
+
42
+
43
+ def _script_path(name):
44
+ return str(SCRIPT_ROOT / name)
45
+
46
+
47
+ def _workflow_args(workflow_path):
48
+ return ["--workflow", str(workflow_path)] if workflow_path else []
49
+
50
+
51
+ def build_run_once_command(config_path, workflow_path=None, *, dry_run=False):
52
+ command = [
53
+ sys.executable,
54
+ "-B",
55
+ _script_path("run_once.py"),
56
+ "--config",
57
+ str(config_path),
58
+ *_workflow_args(workflow_path),
59
+ ]
60
+ if dry_run:
61
+ command.append("--dry-run")
62
+ return command
63
+
64
+
65
+ def build_loop_command(
66
+ config_path,
67
+ workflow_path=None,
68
+ *,
69
+ dry_run=False,
70
+ max_seconds=180,
71
+ skip_publish=False,
72
+ ):
73
+ command = [
74
+ sys.executable,
75
+ "-B",
76
+ _script_path("loop_engine.py"),
77
+ "--config",
78
+ str(config_path),
79
+ "--skip-external",
80
+ "--max-seconds",
81
+ str(max_seconds),
82
+ *_workflow_args(workflow_path),
83
+ ]
84
+ if dry_run:
85
+ command.append("--dry-run")
86
+ if skip_publish:
87
+ command.append("--skip-publish")
88
+ return command
89
+
90
+
91
+ def _short_text(value, max_chars=2000):
92
+ text = str(value or "")
93
+ if len(text) <= max_chars:
94
+ return text
95
+ return f"{text[:max_chars]}...[truncated {len(text) - max_chars} chars]"
96
+
97
+
98
+ def run_child_json(command, *, timeout_seconds, runner=subprocess.run):
99
+ try:
100
+ completed = runner(
101
+ command,
102
+ cwd=str(AGENTS_ROOT),
103
+ text=True,
104
+ capture_output=True,
105
+ timeout=timeout_seconds,
106
+ check=False,
107
+ )
108
+ except subprocess.TimeoutExpired as exc:
109
+ return {
110
+ "ok": False,
111
+ "status": "child_timeout",
112
+ "safe_error": f"child timed out after {timeout_seconds}s",
113
+ "child": {
114
+ "command": command,
115
+ "timeout_seconds": timeout_seconds,
116
+ "stdout": _short_text(getattr(exc, "stdout", "")),
117
+ "stderr": _short_text(getattr(exc, "stderr", "")),
118
+ },
119
+ }
120
+ stdout = completed.stdout.strip()
121
+ child = {
122
+ "command": command,
123
+ "returncode": completed.returncode,
124
+ "stdout": _short_text(completed.stdout),
125
+ "stderr": _short_text(completed.stderr),
126
+ }
127
+ try:
128
+ payload = json.loads(stdout)
129
+ except json.JSONDecodeError:
130
+ return {
131
+ "ok": False,
132
+ "status": "child_failed",
133
+ "safe_error": _short_text(completed.stderr or completed.stdout or "child returned non-JSON output"),
134
+ "child": child,
135
+ }
136
+ payload = payload if isinstance(payload, dict) else {"payload": payload}
137
+ if completed.returncode != 0:
138
+ return {
139
+ **payload,
140
+ "ok": False,
141
+ "status": payload.get("status") or "child_failed",
142
+ "safe_error": payload.get("safe_error") or _short_text(completed.stderr or "child failed"),
143
+ "child": child,
144
+ }
145
+ return {**payload, "child": child}
146
+
147
+
148
+ def parse_rate_limit(payload):
149
+ resources = payload.get("resources") if isinstance(payload, dict) else {}
150
+ resources = resources if isinstance(resources, dict) else {}
151
+ core = resources.get("core") if isinstance(resources.get("core"), dict) else {}
152
+ search = resources.get("search") if isinstance(resources.get("search"), dict) else {}
153
+ return {
154
+ "core": {
155
+ "remaining": int(core.get("remaining", 0)),
156
+ "reset": core.get("reset"),
157
+ },
158
+ "search": {
159
+ "remaining": int(search.get("remaining", 0)),
160
+ "reset": search.get("reset"),
161
+ },
162
+ }
163
+
164
+
165
+ def should_skip_live_poll_for_rate_limit(rate_limit, daemon_config):
166
+ core = rate_limit.get("core") or {}
167
+ search = rate_limit.get("search") or {}
168
+ if search.get("remaining", 0) < daemon_config["min_search_remaining"]:
169
+ return {
170
+ "skip": True,
171
+ "reason": "search_rate_limit_floor",
172
+ "rate_limit": rate_limit,
173
+ }
174
+ if core.get("remaining", 0) < daemon_config["min_core_remaining"]:
175
+ return {
176
+ "skip": True,
177
+ "reason": "core_rate_limit_floor",
178
+ "rate_limit": rate_limit,
179
+ }
180
+ return {"skip": False, "reason": "rate_limit_ok", "rate_limit": rate_limit}
181
+
182
+
183
+ def fetch_rate_limit(runner=subprocess.run):
184
+ result = run_child_json(
185
+ ["gh", "api", "rate_limit"],
186
+ timeout_seconds=30,
187
+ runner=runner,
188
+ )
189
+ if not result.get("ok", True):
190
+ return result
191
+ return {
192
+ "ok": True,
193
+ "status": "rate_limit_loaded",
194
+ "rate_limit": parse_rate_limit(result),
195
+ }
196
+
197
+
198
+ def _request_stop(_signum, _frame):
199
+ global _STOP_REQUESTED
200
+ _STOP_REQUESTED = True
201
+
202
+
203
+ def _daemon_run_id():
204
+ return f"daemon-run-{uuid4().hex[:12]}"
205
+
206
+
207
+ def _daemon_resource_key(config_result):
208
+ return daemon_state.repo_id_for_config(config_result)
209
+
210
+
211
+ def _parse_time(value):
212
+ if isinstance(value, datetime):
213
+ return value
214
+ if value.endswith("Z"):
215
+ value = value[:-1] + "+00:00"
216
+ return datetime.fromisoformat(value)
217
+
218
+
219
+ def _iso_after(now, seconds):
220
+ return (_parse_time(now) + timedelta(seconds=seconds)).isoformat()
221
+
222
+
223
+ def _lease_ttl_seconds(daemon_config):
224
+ heartbeat_margin = max(daemon_config["local_poll_seconds"] * 3, 30)
225
+ return (
226
+ daemon_config["live_run_timeout_seconds"]
227
+ + daemon_config["local_drain_timeout_seconds"]
228
+ + heartbeat_margin
229
+ )
230
+
231
+
232
+ def _live_poll_interval_seconds(context, *, capacity_full):
233
+ if capacity_full:
234
+ return context.daemon_config["github_poll_when_full_seconds"]
235
+ return context.daemon_config["github_poll_seconds"]
236
+
237
+
238
+ def _schedule_next_live_poll(context, *, now, capacity_full):
239
+ interval_seconds = _live_poll_interval_seconds(context, capacity_full=capacity_full)
240
+ context.next_live_poll_at = _iso_after(now, interval_seconds)
241
+ return interval_seconds
242
+
243
+
244
+ def _idle_sleep_seconds(context, *, now=None):
245
+ now = now or daemon_state.utc_now()
246
+ local_poll_seconds = context.daemon_config["local_poll_seconds"]
247
+ if context.next_live_poll_at is None:
248
+ return local_poll_seconds
249
+ remaining_seconds = max(
250
+ 0,
251
+ int((_parse_time(context.next_live_poll_at) - _parse_time(now)).total_seconds()),
252
+ )
253
+ return min(local_poll_seconds, remaining_seconds)
254
+
255
+
256
+ def _sleep_seconds_after_decision(context, result, *, now=None):
257
+ if result.get("decision") == "idle":
258
+ return _idle_sleep_seconds(context, now=now)
259
+ if result.get("decision") == "local_drain":
260
+ if not result.get("ok", False):
261
+ return context.daemon_config["local_poll_seconds"]
262
+ if (result.get("result") or {}).get("stop_reason") == "no_progress":
263
+ return context.daemon_config["local_poll_seconds"]
264
+ return 0
265
+
266
+
267
+ def _lookup_repo_id(conn, full_name, fallback):
268
+ row = conn.execute(
269
+ "SELECT repo_id FROM repos WHERE full_name = ?",
270
+ (full_name,),
271
+ ).fetchone()
272
+ return row[0] if row else fallback
273
+
274
+
275
+ def _load_context(config_path, workflow_path=None, *, dry_run=False, now=None):
276
+ config_result = validate_config.validate_config(config_path, skip_external=True)
277
+ if not config_result.get("ok"):
278
+ return None, config_result
279
+ db_path = Path(config_result["db_path"])
280
+ storage.init_database(db_path)
281
+ if not config_result["daemon"]["enabled"]:
282
+ return None, {
283
+ "ok": True,
284
+ "status": "disabled",
285
+ "decision": "disabled",
286
+ "db_path": str(db_path),
287
+ }
288
+ daemon_run_id = _daemon_run_id()
289
+ resource_key = _daemon_resource_key(config_result)
290
+ daemon_config = config_result["daemon"]
291
+ ttl_seconds = _lease_ttl_seconds(daemon_config)
292
+ with closing(daemon_state.connect(db_path)) as conn, conn:
293
+ daemon_state.cleanup_old_events(
294
+ conn,
295
+ daemon_config["event_retention_days"],
296
+ now=now,
297
+ )
298
+ lease = daemon_state.acquire_daemon_lease(
299
+ conn,
300
+ resource_key=resource_key,
301
+ owner_id=daemon_run_id,
302
+ ttl_seconds=ttl_seconds,
303
+ now=now,
304
+ )
305
+ if not lease["ok"]:
306
+ return None, {"ok": True, "status": lease["status"], "lease": lease}
307
+ repo_ids = [
308
+ _lookup_repo_id(
309
+ conn,
310
+ repo["full_name"],
311
+ daemon_state.repo_id_for_full_name(repo["full_name"]),
312
+ )
313
+ for repo in config_result["repos"]
314
+ ]
315
+ daemon_state.start_daemon_run(
316
+ conn,
317
+ config_path=str(config_path),
318
+ owner_id=daemon_run_id,
319
+ now=now,
320
+ )
321
+ daemon_state.record_event(
322
+ conn,
323
+ daemon_run_id,
324
+ "daemon_started",
325
+ "ok",
326
+ {"resource_key": resource_key, "dry_run": dry_run},
327
+ now=now,
328
+ )
329
+ return DaemonContext(
330
+ config_path=str(config_path),
331
+ workflow_path=str(workflow_path) if workflow_path else None,
332
+ db_path=db_path,
333
+ repo_id=repo_ids[0],
334
+ max_concurrency=config_result["max_concurrency"],
335
+ daemon_config=daemon_config,
336
+ daemon_run_id=daemon_run_id,
337
+ lease_id=lease["lease_id"],
338
+ repo_ids=repo_ids,
339
+ dry_run=dry_run,
340
+ startup_live_poll_pending=daemon_config.get("run_on_start", False),
341
+ ), None
342
+
343
+
344
+ def _run_local_drain(context, *, skip_publish=False, child_runner=run_child_json, now=None):
345
+ command = build_loop_command(
346
+ context.config_path,
347
+ context.workflow_path,
348
+ dry_run=context.dry_run,
349
+ max_seconds=context.daemon_config["local_drain_timeout_seconds"],
350
+ skip_publish=skip_publish,
351
+ )
352
+ with closing(daemon_state.connect(context.db_path)) as conn, conn:
353
+ daemon_state.record_event(
354
+ conn,
355
+ context.daemon_run_id,
356
+ "local_drain_started",
357
+ "running",
358
+ {"command": command},
359
+ now=now,
360
+ )
361
+ result = child_runner(
362
+ command,
363
+ timeout_seconds=context.daemon_config["local_drain_timeout_seconds"],
364
+ )
365
+ with closing(daemon_state.connect(context.db_path)) as conn, conn:
366
+ daemon_state.record_event(
367
+ conn,
368
+ context.daemon_run_id,
369
+ "local_drain_completed" if result.get("ok") else result.get("status", "child_failed"),
370
+ "ok" if result.get("ok") else "failed",
371
+ result,
372
+ now=now,
373
+ )
374
+ return result
375
+
376
+
377
+ def _run_live_poll(context, child_runner=run_child_json, now=None):
378
+ command = build_run_once_command(
379
+ context.config_path,
380
+ context.workflow_path,
381
+ dry_run=context.dry_run,
382
+ )
383
+ with closing(daemon_state.connect(context.db_path)) as conn, conn:
384
+ daemon_state.record_event(
385
+ conn,
386
+ context.daemon_run_id,
387
+ "live_poll_started",
388
+ "running",
389
+ {"command": command},
390
+ now=now,
391
+ )
392
+ result = child_runner(
393
+ command,
394
+ timeout_seconds=context.daemon_config["live_run_timeout_seconds"],
395
+ )
396
+ with closing(daemon_state.connect(context.db_path)) as conn, conn:
397
+ daemon_state.record_event(
398
+ conn,
399
+ context.daemon_run_id,
400
+ "live_poll_completed" if result.get("ok") else result.get("status", "child_failed"),
401
+ "ok" if result.get("ok") else "failed",
402
+ result,
403
+ now=now,
404
+ )
405
+ return result
406
+
407
+
408
+ def _rate_limit_decision(context, rate_limit_runner, *, now=None):
409
+ now = now or daemon_state.utc_now()
410
+ cached_decision = context.rate_limit_decision_cache
411
+ cached_until = context.rate_limit_cache_expires_at
412
+ if (
413
+ cached_decision is not None
414
+ and cached_until is not None
415
+ and _parse_time(now) < _parse_time(cached_until)
416
+ ):
417
+ return {**cached_decision, "cached": True}
418
+ rate_limit_result = fetch_rate_limit(runner=rate_limit_runner)
419
+ if not rate_limit_result.get("ok", False):
420
+ decision = {
421
+ "skip": True,
422
+ "reason": "rate_limit_check_failed",
423
+ "rate_limit": rate_limit_result,
424
+ }
425
+ else:
426
+ decision = should_skip_live_poll_for_rate_limit(
427
+ rate_limit_result["rate_limit"],
428
+ context.daemon_config,
429
+ )
430
+ context.rate_limit_decision_cache = decision
431
+ context.rate_limit_cache_expires_at = _iso_after(
432
+ now,
433
+ context.daemon_config["rate_limit_cache_seconds"],
434
+ )
435
+ return {**decision, "cached": False}
436
+
437
+
438
+ def run_once_decision(
439
+ context,
440
+ *,
441
+ now=None,
442
+ child_runner=run_child_json,
443
+ rate_limit_runner=subprocess.run,
444
+ ):
445
+ now = now or daemon_state.utc_now()
446
+ repo_ids = context.repo_ids or [context.repo_id]
447
+ with closing(daemon_state.connect(context.db_path)) as conn, conn:
448
+ local_work = loop_engine.has_runnable_local_work(conn, repo_ids=repo_ids)
449
+ capacity_full = daemon_state.capacity_full_for_repos(
450
+ conn,
451
+ repo_ids,
452
+ context.max_concurrency,
453
+ )
454
+ live_poll_due = False
455
+ if context.startup_live_poll_pending:
456
+ context.startup_live_poll_pending = False
457
+ live_poll_due = True
458
+ elif context.next_live_poll_at is None:
459
+ _schedule_next_live_poll(context, now=now, capacity_full=capacity_full)
460
+ elif _parse_time(now) >= _parse_time(context.next_live_poll_at):
461
+ live_poll_due = True
462
+ if live_poll_due:
463
+ _schedule_next_live_poll(context, now=now, capacity_full=capacity_full)
464
+ rate_decision = _rate_limit_decision(context, rate_limit_runner, now=now)
465
+ if rate_decision["skip"]:
466
+ with closing(daemon_state.connect(context.db_path)) as conn, conn:
467
+ daemon_state.record_event(
468
+ conn,
469
+ context.daemon_run_id,
470
+ "live_poll_skipped_rate_limit",
471
+ "skipped",
472
+ rate_decision,
473
+ now=now,
474
+ )
475
+ return {
476
+ "ok": True,
477
+ "decision": "live_poll_skipped_rate_limit",
478
+ "rate_limit": rate_decision,
479
+ }
480
+ live_result = _run_live_poll(context, child_runner=child_runner, now=now)
481
+ drain_result = _run_local_drain(context, child_runner=child_runner, now=now)
482
+ return {
483
+ "ok": bool(live_result.get("ok") and drain_result.get("ok")),
484
+ "decision": "live_poll",
485
+ "live_result": live_result,
486
+ "drain_result": drain_result,
487
+ }
488
+ if local_work:
489
+ rate_decision = _rate_limit_decision(context, rate_limit_runner, now=now)
490
+ result = _run_local_drain(
491
+ context,
492
+ skip_publish=rate_decision["skip"],
493
+ child_runner=child_runner,
494
+ now=now,
495
+ )
496
+ return {
497
+ "ok": result.get("ok", False),
498
+ "decision": "local_drain",
499
+ "result": result,
500
+ "rate_limit": rate_decision,
501
+ }
502
+ return {"ok": True, "decision": "idle"}
503
+
504
+
505
+ def _finish_context(context, status, *, summary=None, error=None, now=None):
506
+ with closing(daemon_state.connect(context.db_path)) as conn, conn:
507
+ daemon_state.release_daemon_lease(conn, context.lease_id, now=now)
508
+ daemon_state.finish_daemon_run(
509
+ conn,
510
+ context.daemon_run_id,
511
+ status,
512
+ summary=summary,
513
+ error=error,
514
+ now=now,
515
+ )
516
+
517
+
518
+ def _heartbeat(context, now=None):
519
+ ttl_seconds = _lease_ttl_seconds(context.daemon_config)
520
+ with closing(daemon_state.connect(context.db_path)) as conn, conn:
521
+ daemon_state.heartbeat_daemon_lease(conn, context.lease_id, ttl_seconds, now=now)
522
+ daemon_state.record_event(
523
+ conn,
524
+ context.daemon_run_id,
525
+ "daemon_heartbeat",
526
+ "ok",
527
+ {},
528
+ now=now,
529
+ )
530
+
531
+
532
+ def _resident_loop(context):
533
+ global _STOP_REQUESTED
534
+ _STOP_REQUESTED = False
535
+ signal.signal(signal.SIGINT, _request_stop)
536
+ signal.signal(signal.SIGTERM, _request_stop)
537
+ while not _STOP_REQUESTED:
538
+ result = run_once_decision(context)
539
+ sleep_seconds = _sleep_seconds_after_decision(context, result)
540
+ if sleep_seconds > 0:
541
+ time.sleep(sleep_seconds)
542
+ _heartbeat(context)
543
+ with closing(daemon_state.connect(context.db_path)) as conn, conn:
544
+ daemon_state.record_event(
545
+ conn,
546
+ context.daemon_run_id,
547
+ "daemon_stopping",
548
+ "ok",
549
+ {},
550
+ )
551
+ _finish_context(context, "stopped", summary={"reason": "signal"})
552
+ return {"ok": True, "status": "stopped", "daemon_run_id": context.daemon_run_id}
553
+
554
+
555
+ def run_daemon(
556
+ config_path,
557
+ workflow_path=None,
558
+ *,
559
+ dry_run=False,
560
+ once=False,
561
+ child_runner=run_child_json,
562
+ rate_limit_runner=subprocess.run,
563
+ now=None,
564
+ ):
565
+ context, error = _load_context(
566
+ config_path,
567
+ workflow_path=workflow_path,
568
+ dry_run=dry_run,
569
+ now=now,
570
+ )
571
+ if error:
572
+ return error
573
+ try:
574
+ if once:
575
+ result = run_once_decision(
576
+ context,
577
+ now=now,
578
+ child_runner=child_runner,
579
+ rate_limit_runner=rate_limit_runner,
580
+ )
581
+ _finish_context(context, "stopped", summary=result, now=now)
582
+ return {**result, "status": "stopped", "daemon_run_id": context.daemon_run_id}
583
+ return _resident_loop(context)
584
+ except Exception as exc:
585
+ error_payload = {"status": "failed", "safe_error": str(exc), "error_type": type(exc).__name__}
586
+ _finish_context(context, "failed", error=error_payload, now=now)
587
+ return {"ok": False, **error_payload}
588
+
589
+
590
+ def main(argv=None):
591
+ parser = argparse.ArgumentParser()
592
+ parser.add_argument("--config", required=True)
593
+ parser.add_argument("--workflow")
594
+ parser.add_argument("--dry-run", action="store_true")
595
+ parser.add_argument("--once", action="store_true")
596
+ args = parser.parse_args(argv)
597
+ result = run_daemon(
598
+ args.config,
599
+ workflow_path=args.workflow,
600
+ dry_run=args.dry_run,
601
+ once=args.once,
602
+ )
603
+ return emit(result, 0 if result.get("ok") else 3)
604
+
605
+
606
+ if __name__ == "__main__":
607
+ raise SystemExit(main())