diff --git a/tools/gh-monitor/poll.py b/tools/gh-monitor/poll.py index 638a29d..8a0d910 100644 --- a/tools/gh-monitor/poll.py +++ b/tools/gh-monitor/poll.py @@ -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}' +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 # --------------------------------------------------------------------------- @@ -259,9 +312,8 @@ def poll_repo(repo_cfg: dict, state: dict) -> dict: all_new_events.sort(key=lambda x: x[1]["created_at"]) for pr, event in all_new_events: - text = format_notification(repo_slug, pr, event) - notify(text) - log.info("[%s] Notified: PR #%d %s by %s", repo_slug, pr["number"], event["action"], event["actor"]) + log.info("[%s] New event: PR #%d %s by %s", repo_slug, pr["number"], event["action"], event["actor"]) + dispatch_agent(repo_slug, pr, event) if event.get("id"): seen_ids.add(event["id"])