feat(gh-monitor): dispatch isolated agentTurn on PR events via openclaw cron add

This commit is contained in:
2026-03-15 21:37:37 -04:00
parent 5bbdcde00e
commit c25fb2e10e

View File

@@ -153,6 +153,59 @@ def format_notification(repo_slug: str, pr: dict, event: dict) -> str:
return f'[gh-monitor] PR #{number} "{title}"{actor} {action}:\n"{body_preview}"\n{url}' return f'[gh-monitor] PR #{number} "{title}"{actor} {action}:\n"{body_preview}"\n{url}'
def dispatch_agent(repo_slug: str, pr: dict, event: dict) -> None:
"""
Fire a one-shot isolated agentTurn via `openclaw cron add` to handle the event.
The agent reads the PR, replies to the comment, and pushes fixes if needed.
"""
number = pr["number"]
title = pr["title"]
actor = event["actor"]
action = event["action"]
body = (event["body"] or "")[:500]
url = event["url"]
message = (
f"gh-monitor detected new PR activity — act on it now.\n\n"
f"Repo: {repo_slug}\n"
f"PR #{number}: {title}\n"
f"Event: {actor} {action}\n"
f"Comment: {body}\n"
f"URL: {url}\n\n"
f"Instructions:\n"
f"1. Read the full PR and the comment at the URL above.\n"
f"2. Reply to the comment on GitHub with a thoughtful response.\n"
f"3. If the comment requests a code change, implement it on the PR branch, "
f"commit, and push.\n"
f"4. Keep replies concise and technical."
)
job_name = f"gh-monitor-pr{number}-{event.get('id', 'evt')}"
try:
result = subprocess.run(
[
"openclaw", "cron", "add",
"--name", job_name,
"--at", "1m",
"--session", "isolated",
"--message", message,
"--delete-after-run",
"--no-deliver",
"--timeout-seconds", "300",
],
capture_output=True,
text=True,
timeout=15,
)
if result.returncode == 0:
log.info("Dispatched agent for PR #%d %s", number, action)
else:
log.warning("Failed to dispatch agent for PR #%d: %s", number, result.stderr.strip())
except Exception as exc:
log.warning("Failed to dispatch agent for PR #%d: %s", number, exc)
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# STEP 8 — error tracking # STEP 8 — error tracking
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
@@ -259,9 +312,8 @@ def poll_repo(repo_cfg: dict, state: dict) -> dict:
all_new_events.sort(key=lambda x: x[1]["created_at"]) all_new_events.sort(key=lambda x: x[1]["created_at"])
for pr, event in all_new_events: for pr, event in all_new_events:
text = format_notification(repo_slug, pr, event) log.info("[%s] New event: PR #%d %s by %s", repo_slug, pr["number"], event["action"], event["actor"])
notify(text) dispatch_agent(repo_slug, pr, event)
log.info("[%s] Notified: PR #%d %s by %s", repo_slug, pr["number"], event["action"], event["actor"])
if event.get("id"): if event.get("id"):
seen_ids.add(event["id"]) seen_ids.add(event["id"])