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.
- robert_agent/__init__.py +1 -0
- robert_agent/acceptance.py +394 -0
- robert_agent/acceptance_suite.py +287 -0
- robert_agent/audit_result.py +501 -0
- robert_agent/authorize.py +117 -0
- robert_agent/board.py +538 -0
- robert_agent/chat_status.py +338 -0
- robert_agent/cli/__init__.py +1 -0
- robert_agent/cli/exit_codes.py +5 -0
- robert_agent/cli/main.py +535 -0
- robert_agent/cli/output.py +20 -0
- robert_agent/common.py +6 -0
- robert_agent/controlled_e2e_acceptance.py +394 -0
- robert_agent/daemon.py +607 -0
- robert_agent/daemon_state.py +319 -0
- robert_agent/diagnostics.py +119 -0
- robert_agent/discover.py +775 -0
- robert_agent/dispatch.py +217 -0
- robert_agent/doctor.py +94 -0
- robert_agent/github_api.py +12 -0
- robert_agent/init_config.py +126 -0
- robert_agent/integrations/__init__.py +1 -0
- robert_agent/integrations/openclaw.py +285 -0
- robert_agent/live_discovery_acceptance.py +70 -0
- robert_agent/live_worker_acceptance.py +363 -0
- robert_agent/live_worktree_acceptance.py +344 -0
- robert_agent/loop_engine.py +285 -0
- robert_agent/memory_curator.py +105 -0
- robert_agent/migrate.py +157 -0
- robert_agent/notify.py +74 -0
- robert_agent/paths.py +16 -0
- robert_agent/production_write_canary.py +254 -0
- robert_agent/project_memory.py +503 -0
- robert_agent/publish.py +743 -0
- robert_agent/publish_dedupe_acceptance.py +270 -0
- robert_agent/redaction.py +41 -0
- robert_agent/render_prompt.py +544 -0
- robert_agent/resource_files.py +8 -0
- robert_agent/resources/__init__.py +1 -0
- robert_agent/resources/acceptance-matrix.md +120 -0
- robert_agent/resources/classifier.md +19 -0
- robert_agent/resources/command-matrix.md +38 -0
- robert_agent/resources/config.example.yml +20 -0
- robert_agent/resources/db/migrations/001_legacy_dd_to_robert.sql +14 -0
- robert_agent/resources/db/schema.sql +479 -0
- robert_agent/resources/failure-modes.md +36 -0
- robert_agent/resources/fixtures/actor-permission.json +1 -0
- robert_agent/resources/fixtures/discovery.json +1 -0
- robert_agent/resources/fixtures/redaction.json +1 -0
- robert_agent/resources/fixtures/review-followup.json +1 -0
- robert_agent/resources/fixtures/worker-results.json +1 -0
- robert_agent/resources/fixtures/workstreams.json +1 -0
- robert_agent/resources/github-events.md +77 -0
- robert_agent/resources/kanban-control.md +165 -0
- robert_agent/resources/redaction.md +29 -0
- robert_agent/resources/result-contract.md +56 -0
- robert_agent/resources/routes.yml +82 -0
- robert_agent/resources/schemas/config.schema.json +123 -0
- robert_agent/resources/schemas/event.schema.json +18 -0
- robert_agent/resources/schemas/result.schema.json +99 -0
- robert_agent/resources/schemas/route.schema.json +23 -0
- robert_agent/resources/schemas/snapshot.schema.json +17 -0
- robert_agent/resources/schemas/task.schema.json +15 -0
- robert_agent/resources/trust-model.md +16 -0
- robert_agent/resources/worker-prompt.md +121 -0
- robert_agent/resources/worker-protocol.md +47 -0
- robert_agent/resources/workflow.yml +50 -0
- robert_agent/resources/workstream-model.md +17 -0
- robert_agent/route.py +232 -0
- robert_agent/route_config.py +64 -0
- robert_agent/run_once.py +5375 -0
- robert_agent/runtime_knowledge.py +392 -0
- robert_agent/service.py +161 -0
- robert_agent/skills.py +28 -0
- robert_agent/status.py +1051 -0
- robert_agent/storage.py +376 -0
- robert_agent/summarize.py +100 -0
- robert_agent/supervise.py +72 -0
- robert_agent/usage.py +192 -0
- robert_agent/validate_config.py +616 -0
- robert_agent/wakeup.py +261 -0
- robert_agent/web.py +2893 -0
- robert_agent/web_assets/board.css +255 -0
- robert_agent/web_assets/board.html +165 -0
- robert_agent/web_assets/board.js +660 -0
- robert_agent/web_assets/github-shell.css +122 -0
- robert_agent/web_assets/index.html +103 -0
- robert_agent/web_assets/operations.css +168 -0
- robert_agent/web_assets/workbench.css +131 -0
- robert_agent/web_assets/workbench.js +581 -0
- robert_agent/work_items.py +1016 -0
- robert_agent/workbench.py +701 -0
- robert_agent/worker/__init__.py +1 -0
- robert_agent/worker/heartbeat.py +190 -0
- robert_agent/worker/result.py +321 -0
- robert_agent/worker/snapshot.py +98 -0
- robert_agent/worker_adapters/__init__.py +138 -0
- robert_agent/worker_adapters/cbc.py +68 -0
- robert_agent/worker_adapters/codex.py +56 -0
- robert_agent/worker_adapters/command.py +33 -0
- robert_agent/worker_adapters/tcodex.py +56 -0
- robert_agent/worker_heartbeat.py +6 -0
- robert_agent/worker_result.py +6 -0
- robert_agent/worker_snapshot.py +6 -0
- robert_agent/workstream.py +55 -0
- robert_agent/worktree.py +297 -0
- robert_github_agent-0.1.0b1.dist-info/METADATA +217 -0
- robert_github_agent-0.1.0b1.dist-info/RECORD +111 -0
- robert_github_agent-0.1.0b1.dist-info/WHEEL +4 -0
- robert_github_agent-0.1.0b1.dist-info/entry_points.txt +2 -0
- robert_github_agent-0.1.0b1.dist-info/licenses/LICENSE +201 -0
robert_agent/publish.py
ADDED
|
@@ -0,0 +1,743 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
import argparse
|
|
3
|
+
from contextlib import closing
|
|
4
|
+
import json
|
|
5
|
+
import re
|
|
6
|
+
import sqlite3
|
|
7
|
+
import subprocess
|
|
8
|
+
from datetime import datetime, timezone
|
|
9
|
+
from urllib.parse import urlparse
|
|
10
|
+
from uuid import uuid4
|
|
11
|
+
|
|
12
|
+
from robert_agent.common import emit
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
COMMENT_MARKER_RE = re.compile(
|
|
16
|
+
r"<!--\s*(?:robert-comment|dd-comment)\b.*?-->",
|
|
17
|
+
re.I | re.S,
|
|
18
|
+
)
|
|
19
|
+
WORKSTREAM_MARKER_RE = re.compile(
|
|
20
|
+
r"<!--\s*(?:robert-workstream|dd-workstream)\b.*?-->",
|
|
21
|
+
re.I | re.S,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _now():
|
|
26
|
+
return datetime.now(timezone.utc).isoformat()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _decode_json(value):
|
|
30
|
+
if not value:
|
|
31
|
+
return {}
|
|
32
|
+
try:
|
|
33
|
+
return json.loads(value)
|
|
34
|
+
except (TypeError, ValueError):
|
|
35
|
+
return {}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _encode_json(value):
|
|
39
|
+
return json.dumps(value, ensure_ascii=False, sort_keys=True)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _id(prefix):
|
|
43
|
+
return f"{prefix}-{uuid4().hex[:12]}"
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _notification_metadata(action, result):
|
|
47
|
+
return {
|
|
48
|
+
"action_id": action["action_id"],
|
|
49
|
+
"action_type": action["action_type"],
|
|
50
|
+
"safe_error": result.get("safe_error", ""),
|
|
51
|
+
"command": result.get("command", []),
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _existing_action_notification(conn, action, notification_type):
|
|
56
|
+
for row in conn.execute(
|
|
57
|
+
"""
|
|
58
|
+
SELECT notification_id, metadata_json
|
|
59
|
+
FROM notifications
|
|
60
|
+
WHERE task_id = ?
|
|
61
|
+
AND notification_type = ?
|
|
62
|
+
AND channel = 'local'
|
|
63
|
+
ORDER BY created_at DESC, notification_id DESC
|
|
64
|
+
""",
|
|
65
|
+
(action["task_id"], notification_type),
|
|
66
|
+
):
|
|
67
|
+
metadata = _decode_json(row["metadata_json"])
|
|
68
|
+
if metadata.get("action_id") == action["action_id"]:
|
|
69
|
+
return row["notification_id"]
|
|
70
|
+
return None
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _insert_notification(conn, action, notification_type, result, now):
|
|
74
|
+
metadata = _notification_metadata(action, result)
|
|
75
|
+
notification_id = _existing_action_notification(conn, action, notification_type)
|
|
76
|
+
if notification_id:
|
|
77
|
+
conn.execute(
|
|
78
|
+
"""
|
|
79
|
+
UPDATE notifications
|
|
80
|
+
SET status = 'recorded',
|
|
81
|
+
created_at = ?,
|
|
82
|
+
metadata_json = ?
|
|
83
|
+
WHERE notification_id = ?
|
|
84
|
+
""",
|
|
85
|
+
(now, _encode_json(metadata), notification_id),
|
|
86
|
+
)
|
|
87
|
+
return
|
|
88
|
+
conn.execute(
|
|
89
|
+
"""
|
|
90
|
+
INSERT INTO notifications(
|
|
91
|
+
notification_id, task_id, notification_type, channel, status, created_at, metadata_json
|
|
92
|
+
)
|
|
93
|
+
VALUES (?, ?, ?, 'local', 'recorded', ?, ?)
|
|
94
|
+
""",
|
|
95
|
+
(
|
|
96
|
+
_id("notification"),
|
|
97
|
+
action["task_id"],
|
|
98
|
+
notification_type,
|
|
99
|
+
now,
|
|
100
|
+
_encode_json(metadata),
|
|
101
|
+
),
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def _resolve_action_notification(conn, action, notification_type, result, now):
|
|
106
|
+
notification_id = _existing_action_notification(conn, action, notification_type)
|
|
107
|
+
if not notification_id:
|
|
108
|
+
return
|
|
109
|
+
row = conn.execute(
|
|
110
|
+
"SELECT metadata_json FROM notifications WHERE notification_id = ?",
|
|
111
|
+
(notification_id,),
|
|
112
|
+
).fetchone()
|
|
113
|
+
metadata = _decode_json(row[0] if row else "{}")
|
|
114
|
+
metadata["resolved_at"] = now
|
|
115
|
+
metadata["resolved_publish_status"] = result.get("status", "")
|
|
116
|
+
metadata["resolved_external_id"] = result.get("external_id", "")
|
|
117
|
+
metadata["resolved_target_url"] = result.get("target_url", "")
|
|
118
|
+
conn.execute(
|
|
119
|
+
"""
|
|
120
|
+
UPDATE notifications
|
|
121
|
+
SET status = 'resolved',
|
|
122
|
+
created_at = ?,
|
|
123
|
+
metadata_json = ?
|
|
124
|
+
WHERE notification_id = ?
|
|
125
|
+
""",
|
|
126
|
+
(now, _encode_json(metadata), notification_id),
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def _issue_comment_endpoint(target_url):
|
|
131
|
+
parsed = urlparse(target_url or "")
|
|
132
|
+
parts = [part for part in parsed.path.split("/") if part]
|
|
133
|
+
if parsed.netloc != "github.com" or len(parts) < 4:
|
|
134
|
+
return None
|
|
135
|
+
owner, repo, source_type, number = parts[:4]
|
|
136
|
+
if source_type not in {"issues", "pull"} or not number.isdigit():
|
|
137
|
+
return None
|
|
138
|
+
return f"repos/{owner}/{repo}/issues/{number}/comments"
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def _hidden_marker(body, marker_re):
|
|
142
|
+
match = marker_re.search(body or "")
|
|
143
|
+
return match.group(0) if match else ""
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def _iter_json_objects(value):
|
|
147
|
+
if isinstance(value, dict):
|
|
148
|
+
yield value
|
|
149
|
+
return
|
|
150
|
+
if isinstance(value, list):
|
|
151
|
+
for item in value:
|
|
152
|
+
yield from _iter_json_objects(item)
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def _command_failure(completed, fallback):
|
|
156
|
+
if getattr(completed, "returncode", 0) == 0:
|
|
157
|
+
return None
|
|
158
|
+
return (getattr(completed, "stderr", "") or fallback).strip()
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def _find_existing_comment(endpoint, marker, run_command):
|
|
162
|
+
command = ["gh", "api", f"{endpoint}?per_page=100", "--paginate", "--slurp"]
|
|
163
|
+
completed = run_command(command, capture_output=True, text=True)
|
|
164
|
+
safe_error = _command_failure(completed, "gh api comment lookup failed")
|
|
165
|
+
if safe_error:
|
|
166
|
+
return {
|
|
167
|
+
"ok": False,
|
|
168
|
+
"status": "publish_failed",
|
|
169
|
+
"safe_error": f"comment duplicate preflight failed: {safe_error}",
|
|
170
|
+
"command": command,
|
|
171
|
+
}
|
|
172
|
+
response = _decode_json(getattr(completed, "stdout", ""))
|
|
173
|
+
for comment in _iter_json_objects(response):
|
|
174
|
+
body = comment.get("body") if isinstance(comment.get("body"), str) else ""
|
|
175
|
+
if marker and marker in body:
|
|
176
|
+
return {
|
|
177
|
+
"ok": True,
|
|
178
|
+
"status": "published",
|
|
179
|
+
"deduplicated": True,
|
|
180
|
+
"external_id": str(comment.get("id") or ""),
|
|
181
|
+
"target_url": comment.get("html_url") or comment.get("url") or "",
|
|
182
|
+
"response": {"existing_comment": comment},
|
|
183
|
+
"command": command,
|
|
184
|
+
}
|
|
185
|
+
return {"ok": True, "status": "not_found", "command": command}
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def _publish_comment(action, run_command):
|
|
189
|
+
metadata = _decode_json(action["metadata_json"])
|
|
190
|
+
body = metadata.get("body")
|
|
191
|
+
if not isinstance(body, str) or not body:
|
|
192
|
+
return {
|
|
193
|
+
"ok": False,
|
|
194
|
+
"status": "publish_failed",
|
|
195
|
+
"safe_error": "comment action requires body",
|
|
196
|
+
}
|
|
197
|
+
marker = _hidden_marker(body, COMMENT_MARKER_RE)
|
|
198
|
+
if not marker:
|
|
199
|
+
return {
|
|
200
|
+
"ok": False,
|
|
201
|
+
"status": "publish_failed",
|
|
202
|
+
"safe_error": "comment action requires robert-comment idempotency marker",
|
|
203
|
+
}
|
|
204
|
+
endpoint = _issue_comment_endpoint(action["target_url"] or metadata.get("target_url") or metadata.get("url"))
|
|
205
|
+
if not endpoint:
|
|
206
|
+
return {
|
|
207
|
+
"ok": False,
|
|
208
|
+
"status": "publish_failed",
|
|
209
|
+
"safe_error": "comment action requires GitHub issue or PR target_url",
|
|
210
|
+
}
|
|
211
|
+
existing = _find_existing_comment(endpoint, marker, run_command)
|
|
212
|
+
if not existing["ok"] or existing["status"] == "published":
|
|
213
|
+
return existing
|
|
214
|
+
command = ["gh", "api", endpoint, "-f", f"body={body}"]
|
|
215
|
+
completed = run_command(command, capture_output=True, text=True)
|
|
216
|
+
safe_error = _command_failure(completed, "gh api failed")
|
|
217
|
+
if safe_error:
|
|
218
|
+
return {
|
|
219
|
+
"ok": False,
|
|
220
|
+
"status": "publish_failed",
|
|
221
|
+
"safe_error": safe_error,
|
|
222
|
+
"command": command,
|
|
223
|
+
}
|
|
224
|
+
response = _decode_json(getattr(completed, "stdout", ""))
|
|
225
|
+
if not isinstance(response, dict):
|
|
226
|
+
response = {}
|
|
227
|
+
return {
|
|
228
|
+
"ok": True,
|
|
229
|
+
"status": "published",
|
|
230
|
+
"external_id": str(response.get("id") or ""),
|
|
231
|
+
"target_url": response.get("html_url") or response.get("url") or action["target_url"],
|
|
232
|
+
"response": response,
|
|
233
|
+
"command": command,
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def _pr_number_from_url(url):
|
|
238
|
+
match = re.search(r"/pull/(\d+)(?:$|[/?#])", url or "")
|
|
239
|
+
return match.group(1) if match else ""
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def _metadata_value(metadata, *keys):
|
|
243
|
+
for key in keys:
|
|
244
|
+
value = metadata.get(key)
|
|
245
|
+
if isinstance(value, str) and value:
|
|
246
|
+
return value
|
|
247
|
+
return ""
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def _head_branch(head):
|
|
251
|
+
if ":" in head:
|
|
252
|
+
return head.split(":", 1)[1]
|
|
253
|
+
return head
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def _owner_login(value):
|
|
257
|
+
if isinstance(value, str):
|
|
258
|
+
return value
|
|
259
|
+
if isinstance(value, dict):
|
|
260
|
+
for key in ("login", "name"):
|
|
261
|
+
owner = value.get(key)
|
|
262
|
+
if isinstance(owner, str) and owner:
|
|
263
|
+
return owner
|
|
264
|
+
owner = value.get("owner")
|
|
265
|
+
if owner is not value:
|
|
266
|
+
return _owner_login(owner)
|
|
267
|
+
return ""
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
def _owners_match(expected, actual):
|
|
271
|
+
return bool(expected and actual and expected.lower() == actual.lower())
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
def _pr_head_owner(pr):
|
|
275
|
+
return _owner_login(pr.get("headRepositoryOwner")) or _owner_login(pr.get("headRepository"))
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
def _find_existing_open_pr(repo, head, marker, run_command, base="", head_owner=""):
|
|
279
|
+
branch = _head_branch(head)
|
|
280
|
+
command = [
|
|
281
|
+
"gh",
|
|
282
|
+
"pr",
|
|
283
|
+
"list",
|
|
284
|
+
"--repo",
|
|
285
|
+
repo,
|
|
286
|
+
"--head",
|
|
287
|
+
branch,
|
|
288
|
+
"--state",
|
|
289
|
+
"open",
|
|
290
|
+
"--json",
|
|
291
|
+
"number,url,baseRefName,headRefName,headRepositoryOwner",
|
|
292
|
+
]
|
|
293
|
+
completed = run_command(command, capture_output=True, text=True)
|
|
294
|
+
safe_error = _command_failure(completed, "gh pr list failed")
|
|
295
|
+
if safe_error:
|
|
296
|
+
return {
|
|
297
|
+
"ok": False,
|
|
298
|
+
"status": "publish_failed",
|
|
299
|
+
"safe_error": f"open_pr duplicate preflight failed: {safe_error}",
|
|
300
|
+
"command": command,
|
|
301
|
+
}
|
|
302
|
+
response = _decode_json(getattr(completed, "stdout", ""))
|
|
303
|
+
for pr in _iter_json_objects(response):
|
|
304
|
+
url = pr.get("url") or ""
|
|
305
|
+
number = str(pr.get("number") or _pr_number_from_url(url) or "")
|
|
306
|
+
base_ref = pr.get("baseRefName") or ""
|
|
307
|
+
if base and base_ref and base_ref != base:
|
|
308
|
+
continue
|
|
309
|
+
head_ref = pr.get("headRefName") or ""
|
|
310
|
+
if branch and head_ref and head_ref != branch:
|
|
311
|
+
continue
|
|
312
|
+
target = number or url
|
|
313
|
+
if not target:
|
|
314
|
+
continue
|
|
315
|
+
view_command = [
|
|
316
|
+
"gh",
|
|
317
|
+
"pr",
|
|
318
|
+
"view",
|
|
319
|
+
target,
|
|
320
|
+
"--repo",
|
|
321
|
+
repo,
|
|
322
|
+
"--json",
|
|
323
|
+
"number,url,body,baseRefName,headRefName,headRepositoryOwner",
|
|
324
|
+
]
|
|
325
|
+
view = run_command(view_command, capture_output=True, text=True)
|
|
326
|
+
safe_error = _command_failure(view, "gh pr view failed")
|
|
327
|
+
if safe_error:
|
|
328
|
+
return {
|
|
329
|
+
"ok": False,
|
|
330
|
+
"status": "publish_failed",
|
|
331
|
+
"safe_error": f"open_pr duplicate preflight failed: {safe_error}",
|
|
332
|
+
"command": view_command,
|
|
333
|
+
}
|
|
334
|
+
existing = _decode_json(getattr(view, "stdout", ""))
|
|
335
|
+
existing_base = existing.get("baseRefName") or base_ref
|
|
336
|
+
if base and existing_base and existing_base != base:
|
|
337
|
+
continue
|
|
338
|
+
existing_head = existing.get("headRefName") or head_ref
|
|
339
|
+
if branch and existing_head and existing_head != branch:
|
|
340
|
+
continue
|
|
341
|
+
body = existing.get("body") if isinstance(existing.get("body"), str) else ""
|
|
342
|
+
existing_owner = _pr_head_owner(existing) or _pr_head_owner(pr)
|
|
343
|
+
dedupe_reason = ""
|
|
344
|
+
if marker and marker in body:
|
|
345
|
+
dedupe_reason = "matching_marker"
|
|
346
|
+
elif _owners_match(head_owner, existing_owner):
|
|
347
|
+
dedupe_reason = "matching_head"
|
|
348
|
+
else:
|
|
349
|
+
continue
|
|
350
|
+
return {
|
|
351
|
+
"ok": True,
|
|
352
|
+
"status": "published",
|
|
353
|
+
"deduplicated": True,
|
|
354
|
+
"external_id": str(existing.get("number") or number),
|
|
355
|
+
"target_url": existing.get("url") or url,
|
|
356
|
+
"response": {
|
|
357
|
+
"existing_pr": existing or pr,
|
|
358
|
+
"dedupe_reason": dedupe_reason,
|
|
359
|
+
},
|
|
360
|
+
"command": view_command,
|
|
361
|
+
}
|
|
362
|
+
return {"ok": True, "status": "not_found", "command": command}
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
def _github_remote_owner(remote_url):
|
|
366
|
+
remote_url = (remote_url or "").strip()
|
|
367
|
+
if not remote_url:
|
|
368
|
+
return ""
|
|
369
|
+
if remote_url.startswith("git@") and ":" in remote_url:
|
|
370
|
+
path = remote_url.split(":", 1)[1]
|
|
371
|
+
else:
|
|
372
|
+
path = urlparse(remote_url).path.lstrip("/")
|
|
373
|
+
if path.endswith(".git"):
|
|
374
|
+
path = path[:-4]
|
|
375
|
+
parts = [part for part in path.split("/") if part]
|
|
376
|
+
return parts[0] if len(parts) >= 2 else ""
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
def _head_owner_from_push_metadata(metadata, head, run_command):
|
|
380
|
+
branch = _metadata_value(metadata, "branch", "head", "head_branch")
|
|
381
|
+
if branch and branch != head:
|
|
382
|
+
return ""
|
|
383
|
+
worktree_path = _metadata_value(metadata, "worktree_path")
|
|
384
|
+
if not worktree_path:
|
|
385
|
+
return ""
|
|
386
|
+
remote = _metadata_value(metadata, "remote") or "origin"
|
|
387
|
+
completed = run_command(
|
|
388
|
+
["git", "remote", "get-url", remote],
|
|
389
|
+
cwd=worktree_path,
|
|
390
|
+
capture_output=True,
|
|
391
|
+
text=True,
|
|
392
|
+
)
|
|
393
|
+
if _command_failure(completed, "git remote get-url failed"):
|
|
394
|
+
return ""
|
|
395
|
+
return _github_remote_owner(getattr(completed, "stdout", ""))
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
def _sibling_push_metadata(conn, action, head):
|
|
399
|
+
if not conn or not action["result_id"]:
|
|
400
|
+
return {}
|
|
401
|
+
rows = conn.execute(
|
|
402
|
+
"""
|
|
403
|
+
SELECT metadata_json
|
|
404
|
+
FROM github_actions
|
|
405
|
+
WHERE result_id = ?
|
|
406
|
+
AND action_type = 'push_existing_pr'
|
|
407
|
+
ORDER BY created_at DESC, action_id DESC
|
|
408
|
+
""",
|
|
409
|
+
(action["result_id"],),
|
|
410
|
+
).fetchall()
|
|
411
|
+
for row in rows:
|
|
412
|
+
metadata = _decode_json(row["metadata_json"])
|
|
413
|
+
branch = _metadata_value(metadata, "branch", "head", "head_branch")
|
|
414
|
+
if not branch or branch == head:
|
|
415
|
+
return metadata
|
|
416
|
+
return {}
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
def _head_owner_for_action(metadata, head, action, conn, run_command):
|
|
420
|
+
if ":" in head:
|
|
421
|
+
return head.split(":", 1)[0]
|
|
422
|
+
owner = _metadata_value(metadata, "head_owner", "fork_owner", "remote_owner", "head_repo_owner")
|
|
423
|
+
if not owner:
|
|
424
|
+
push_metadata = _sibling_push_metadata(conn, action, head)
|
|
425
|
+
owner = _head_owner_from_push_metadata(push_metadata, head, run_command) if push_metadata else ""
|
|
426
|
+
return owner
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
def _head_for_pr_create(repo, head, owner):
|
|
430
|
+
if ":" in head:
|
|
431
|
+
return head
|
|
432
|
+
repo_owner = repo.split("/", 1)[0]
|
|
433
|
+
if owner and owner.lower() == repo_owner.lower():
|
|
434
|
+
return head
|
|
435
|
+
return f"{owner}:{head}" if owner else head
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
def _publish_open_pr(action, run_command, conn=None):
|
|
439
|
+
metadata = _decode_json(action["metadata_json"])
|
|
440
|
+
repo = _metadata_value(metadata, "repo", "repository")
|
|
441
|
+
head = _metadata_value(metadata, "head", "head_branch", "branch")
|
|
442
|
+
base = _metadata_value(metadata, "base", "base_branch", "target_base_branch")
|
|
443
|
+
title = _metadata_value(metadata, "title")
|
|
444
|
+
body = _metadata_value(metadata, "body", "pr_body")
|
|
445
|
+
missing = [
|
|
446
|
+
name
|
|
447
|
+
for name, value in {
|
|
448
|
+
"repo": repo,
|
|
449
|
+
"head": head,
|
|
450
|
+
"base": base,
|
|
451
|
+
"title": title,
|
|
452
|
+
"body": body,
|
|
453
|
+
}.items()
|
|
454
|
+
if not value
|
|
455
|
+
]
|
|
456
|
+
if missing:
|
|
457
|
+
return {
|
|
458
|
+
"ok": False,
|
|
459
|
+
"status": "publish_failed",
|
|
460
|
+
"safe_error": f"open_pr action missing fields: {missing}",
|
|
461
|
+
}
|
|
462
|
+
marker = _hidden_marker(body, WORKSTREAM_MARKER_RE)
|
|
463
|
+
if not marker:
|
|
464
|
+
return {
|
|
465
|
+
"ok": False,
|
|
466
|
+
"status": "publish_failed",
|
|
467
|
+
"safe_error": "open_pr action body requires robert-workstream metadata",
|
|
468
|
+
}
|
|
469
|
+
head_owner = _head_owner_for_action(metadata, head, action, conn, run_command)
|
|
470
|
+
existing = _find_existing_open_pr(repo, head, marker, run_command, base=base, head_owner=head_owner)
|
|
471
|
+
if not existing["ok"] or existing["status"] == "published":
|
|
472
|
+
return existing
|
|
473
|
+
create_head = _head_for_pr_create(repo, head, head_owner)
|
|
474
|
+
command = [
|
|
475
|
+
"gh",
|
|
476
|
+
"pr",
|
|
477
|
+
"create",
|
|
478
|
+
"--repo",
|
|
479
|
+
repo,
|
|
480
|
+
"--head",
|
|
481
|
+
create_head,
|
|
482
|
+
"--base",
|
|
483
|
+
base,
|
|
484
|
+
"--title",
|
|
485
|
+
title,
|
|
486
|
+
"--body",
|
|
487
|
+
body,
|
|
488
|
+
]
|
|
489
|
+
if metadata.get("draft"):
|
|
490
|
+
command.append("--draft")
|
|
491
|
+
completed = run_command(command, capture_output=True, text=True)
|
|
492
|
+
safe_error = _command_failure(completed, "gh pr create failed")
|
|
493
|
+
if safe_error:
|
|
494
|
+
existing_number = _pr_number_from_url(safe_error)
|
|
495
|
+
if existing_number and "already exists" in safe_error:
|
|
496
|
+
return {
|
|
497
|
+
"ok": True,
|
|
498
|
+
"status": "published",
|
|
499
|
+
"deduplicated": True,
|
|
500
|
+
"external_id": existing_number,
|
|
501
|
+
"target_url": f"https://github.com/{repo}/pull/{existing_number}",
|
|
502
|
+
"response": {"dedupe_reason": "create_reported_existing_pr"},
|
|
503
|
+
"command": command,
|
|
504
|
+
}
|
|
505
|
+
return {
|
|
506
|
+
"ok": False,
|
|
507
|
+
"status": "publish_failed",
|
|
508
|
+
"safe_error": safe_error,
|
|
509
|
+
"command": command,
|
|
510
|
+
}
|
|
511
|
+
lines = (getattr(completed, "stdout", "") or "").strip().splitlines()
|
|
512
|
+
url = lines[-1].strip() if lines else ""
|
|
513
|
+
return {
|
|
514
|
+
"ok": True,
|
|
515
|
+
"status": "published",
|
|
516
|
+
"external_id": _pr_number_from_url(url),
|
|
517
|
+
"target_url": url or action["target_url"],
|
|
518
|
+
"response": {"url": url},
|
|
519
|
+
"command": command,
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
|
|
523
|
+
def _publish_push_existing_pr(action, run_command):
|
|
524
|
+
metadata = _decode_json(action["metadata_json"])
|
|
525
|
+
worktree_path = _metadata_value(metadata, "worktree_path")
|
|
526
|
+
remote = _metadata_value(metadata, "remote") or "origin"
|
|
527
|
+
branch = _metadata_value(metadata, "branch", "head", "head_branch")
|
|
528
|
+
missing = [
|
|
529
|
+
name
|
|
530
|
+
for name, value in {
|
|
531
|
+
"worktree_path": worktree_path,
|
|
532
|
+
"branch": branch,
|
|
533
|
+
}.items()
|
|
534
|
+
if not value
|
|
535
|
+
]
|
|
536
|
+
if missing:
|
|
537
|
+
return {
|
|
538
|
+
"ok": False,
|
|
539
|
+
"status": "publish_failed",
|
|
540
|
+
"safe_error": f"push_existing_pr action missing fields: {missing}",
|
|
541
|
+
}
|
|
542
|
+
command = ["git", "push", remote, f"HEAD:{branch}"]
|
|
543
|
+
completed = run_command(command, cwd=worktree_path, capture_output=True, text=True)
|
|
544
|
+
safe_error = _command_failure(completed, "git push failed")
|
|
545
|
+
if safe_error:
|
|
546
|
+
return {
|
|
547
|
+
"ok": False,
|
|
548
|
+
"status": "publish_failed",
|
|
549
|
+
"safe_error": safe_error,
|
|
550
|
+
"command": command,
|
|
551
|
+
}
|
|
552
|
+
return {
|
|
553
|
+
"ok": True,
|
|
554
|
+
"status": "published",
|
|
555
|
+
"external_id": branch,
|
|
556
|
+
"target_url": action["target_url"] or metadata.get("target_url") or metadata.get("url"),
|
|
557
|
+
"response": {"stdout": getattr(completed, "stdout", "")},
|
|
558
|
+
"command": command,
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
def _publish_action(action, run_command, conn=None):
|
|
563
|
+
if action["action_type"] == "comment":
|
|
564
|
+
return _publish_comment(action, run_command)
|
|
565
|
+
if action["action_type"] == "open_pr":
|
|
566
|
+
return _publish_open_pr(action, run_command, conn=conn)
|
|
567
|
+
if action["action_type"] == "push_existing_pr":
|
|
568
|
+
return _publish_push_existing_pr(action, run_command)
|
|
569
|
+
return {
|
|
570
|
+
"ok": False,
|
|
571
|
+
"status": "unsupported_action",
|
|
572
|
+
"safe_error": f"publisher does not support action_type {action['action_type']}",
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
|
|
576
|
+
def _mark_published(conn, action, result, now):
|
|
577
|
+
metadata = _decode_json(action["metadata_json"])
|
|
578
|
+
metadata["publish"] = {
|
|
579
|
+
"status": "published",
|
|
580
|
+
"published_at": now,
|
|
581
|
+
"response": result.get("response", {}),
|
|
582
|
+
"deduplicated": bool(result.get("deduplicated")),
|
|
583
|
+
}
|
|
584
|
+
conn.execute(
|
|
585
|
+
"""
|
|
586
|
+
UPDATE github_actions
|
|
587
|
+
SET publish_status = 'published',
|
|
588
|
+
external_id = COALESCE(NULLIF(?, ''), external_id),
|
|
589
|
+
target_url = COALESCE(NULLIF(?, ''), target_url),
|
|
590
|
+
metadata_json = ?
|
|
591
|
+
WHERE action_id = ?
|
|
592
|
+
""",
|
|
593
|
+
(
|
|
594
|
+
result.get("external_id", ""),
|
|
595
|
+
result.get("target_url"),
|
|
596
|
+
_encode_json(metadata),
|
|
597
|
+
action["action_id"],
|
|
598
|
+
),
|
|
599
|
+
)
|
|
600
|
+
_resolve_action_notification(conn, action, "github_publish_failed", result, now)
|
|
601
|
+
|
|
602
|
+
|
|
603
|
+
def _mark_skipped(conn, action, result, now):
|
|
604
|
+
metadata = _decode_json(action["metadata_json"])
|
|
605
|
+
metadata["publish"] = {
|
|
606
|
+
"status": "skipped",
|
|
607
|
+
"skipped_at": now,
|
|
608
|
+
"safe_error": result.get("safe_error", ""),
|
|
609
|
+
}
|
|
610
|
+
conn.execute(
|
|
611
|
+
"""
|
|
612
|
+
UPDATE github_actions
|
|
613
|
+
SET publish_status = 'skipped',
|
|
614
|
+
metadata_json = ?
|
|
615
|
+
WHERE action_id = ?
|
|
616
|
+
""",
|
|
617
|
+
(_encode_json(metadata), action["action_id"]),
|
|
618
|
+
)
|
|
619
|
+
_insert_notification(conn, action, "github_publish_skipped", result, now)
|
|
620
|
+
|
|
621
|
+
|
|
622
|
+
def _record_failure(conn, action, result, now):
|
|
623
|
+
metadata = _decode_json(action["metadata_json"])
|
|
624
|
+
metadata["publish"] = {
|
|
625
|
+
"status": "publish_failed",
|
|
626
|
+
"failed_at": now,
|
|
627
|
+
"safe_error": result.get("safe_error", ""),
|
|
628
|
+
"command": result.get("command", []),
|
|
629
|
+
}
|
|
630
|
+
conn.execute(
|
|
631
|
+
"""
|
|
632
|
+
UPDATE github_actions
|
|
633
|
+
SET metadata_json = ?
|
|
634
|
+
WHERE action_id = ?
|
|
635
|
+
""",
|
|
636
|
+
(_encode_json(metadata), action["action_id"]),
|
|
637
|
+
)
|
|
638
|
+
_insert_notification(conn, action, "github_publish_failed", result, now)
|
|
639
|
+
|
|
640
|
+
|
|
641
|
+
def _ready_actions(conn, limit, repo_id=None):
|
|
642
|
+
repo_filter = ""
|
|
643
|
+
params = []
|
|
644
|
+
if repo_id:
|
|
645
|
+
repo_filter = """
|
|
646
|
+
AND EXISTS (
|
|
647
|
+
SELECT 1
|
|
648
|
+
FROM tasks t
|
|
649
|
+
JOIN workstreams w ON w.workstream_id = t.workstream_id
|
|
650
|
+
WHERE t.task_id = github_actions.task_id
|
|
651
|
+
AND w.repo_id = ?
|
|
652
|
+
)
|
|
653
|
+
"""
|
|
654
|
+
params.append(repo_id)
|
|
655
|
+
return [
|
|
656
|
+
dict(row)
|
|
657
|
+
for row in conn.execute(
|
|
658
|
+
f"""
|
|
659
|
+
SELECT action_id, result_id, task_id, action_type, target_url, external_id,
|
|
660
|
+
audit_status, publish_status, created_at, metadata_json
|
|
661
|
+
FROM github_actions
|
|
662
|
+
WHERE audit_status = 'accepted'
|
|
663
|
+
AND publish_status = 'not_published'
|
|
664
|
+
{repo_filter}
|
|
665
|
+
ORDER BY created_at, action_id
|
|
666
|
+
LIMIT ?
|
|
667
|
+
""",
|
|
668
|
+
params + [limit],
|
|
669
|
+
)
|
|
670
|
+
]
|
|
671
|
+
|
|
672
|
+
|
|
673
|
+
def publish_ready_actions(db_path, dry_run=False, limit=20, run_command=subprocess.run, repo_id=None):
|
|
674
|
+
with closing(sqlite3.connect(db_path)) as conn, conn:
|
|
675
|
+
conn.row_factory = sqlite3.Row
|
|
676
|
+
actions = _ready_actions(conn, limit, repo_id=repo_id)
|
|
677
|
+
if dry_run:
|
|
678
|
+
return {
|
|
679
|
+
"ok": True,
|
|
680
|
+
"status": "dry_run",
|
|
681
|
+
"pending_count": len(actions),
|
|
682
|
+
"published_count": 0,
|
|
683
|
+
"deduplicated_count": 0,
|
|
684
|
+
"skipped_count": 0,
|
|
685
|
+
"failed_count": 0,
|
|
686
|
+
}
|
|
687
|
+
published_count = 0
|
|
688
|
+
deduplicated_count = 0
|
|
689
|
+
skipped_count = 0
|
|
690
|
+
failed_count = 0
|
|
691
|
+
failures = []
|
|
692
|
+
failed_result_ids = set()
|
|
693
|
+
for action in actions:
|
|
694
|
+
result_id = action["result_id"]
|
|
695
|
+
if result_id and result_id in failed_result_ids:
|
|
696
|
+
continue
|
|
697
|
+
now = _now()
|
|
698
|
+
result = _publish_action(action, run_command, conn=conn)
|
|
699
|
+
if result["status"] == "published":
|
|
700
|
+
_mark_published(conn, action, result, now)
|
|
701
|
+
published_count += 1
|
|
702
|
+
if result.get("deduplicated"):
|
|
703
|
+
deduplicated_count += 1
|
|
704
|
+
continue
|
|
705
|
+
if result["status"] == "unsupported_action":
|
|
706
|
+
_mark_skipped(conn, action, result, now)
|
|
707
|
+
skipped_count += 1
|
|
708
|
+
continue
|
|
709
|
+
_record_failure(conn, action, result, now)
|
|
710
|
+
if result_id:
|
|
711
|
+
failed_result_ids.add(result_id)
|
|
712
|
+
failed_count += 1
|
|
713
|
+
failures.append(
|
|
714
|
+
{
|
|
715
|
+
"action_id": action["action_id"],
|
|
716
|
+
"safe_error": result.get("safe_error", ""),
|
|
717
|
+
"command": result.get("command", []),
|
|
718
|
+
}
|
|
719
|
+
)
|
|
720
|
+
return {
|
|
721
|
+
"ok": failed_count == 0,
|
|
722
|
+
"status": "published" if failed_count == 0 else "publish_failed",
|
|
723
|
+
"pending_count": len(actions),
|
|
724
|
+
"published_count": published_count,
|
|
725
|
+
"deduplicated_count": deduplicated_count,
|
|
726
|
+
"skipped_count": skipped_count,
|
|
727
|
+
"failed_count": failed_count,
|
|
728
|
+
"failures": failures,
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
|
|
732
|
+
def main(argv=None):
|
|
733
|
+
parser = argparse.ArgumentParser()
|
|
734
|
+
parser.add_argument("--db", required=True)
|
|
735
|
+
parser.add_argument("--dry-run", action="store_true")
|
|
736
|
+
parser.add_argument("--limit", type=int, default=20)
|
|
737
|
+
args = parser.parse_args(argv)
|
|
738
|
+
result = publish_ready_actions(args.db, dry_run=args.dry_run, limit=args.limit)
|
|
739
|
+
return emit(result, 0 if result.get("ok") else 3)
|
|
740
|
+
|
|
741
|
+
|
|
742
|
+
if __name__ == "__main__":
|
|
743
|
+
raise SystemExit(main())
|