-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscriber.py
More file actions
1181 lines (993 loc) · 44.2 KB
/
subscriber.py
File metadata and controls
1181 lines (993 loc) · 44.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""Conductor subscriber — listens to Redis and spawns agents.
Subscribes to Redis channels, and when an issue with a conductor label
is opened (or reopened, or triggered via /run comment), spawns `claude -p`
in the target repo's directory.
Usage:
python3 subscriber.py # listen and spawn
python3 subscriber.py --dry-run # log only, don't spawn
python3 subscriber.py --channel conductor:issues # issues only
Redis channels:
conductor:issues — issue opened/reopened/labeled/closed events
conductor:comments — issue comment events (created/edited)
conductor:events — all events (firehose)
Comment triggers:
/run — spawn an agent for this issue (re-trigger missed or stalled work)
/run with additional context after the command is passed to the agent
"""
import argparse
import json
import logging
import os
import shutil
import subprocess
import sys
import threading
import time
from datetime import datetime, timezone
from pathlib import Path
import re
import redis
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)s %(message)s",
datefmt="%H:%M:%S",
)
log = logging.getLogger("subscriber")
# Track active agents to prevent duplicates
active_agents: dict[str, dict] = {} # key: "repo#number" → agent info
agent_lock = threading.Lock()
# Where agent logs go
LOG_DIR = Path("/home/tim/Projects/loqu8/conductor/logs")
# Where agent worktrees are created (isolated git checkouts)
WORKTREE_BASE = Path("/tmp/conductor-worktrees")
# Backend definitions — what can actually run
BACKENDS = {
"claude": {"command": "claude", "description": "Claude Code (default)"},
"claude-opus": {"command": "claude", "description": "Claude Code (Opus)"},
"cursor": {"command": None, "description": "Cursor (manual)"},
"marketing": {"command": "claude", "description": "Claude Code (marketing)"},
}
# Label/task-type → fallback chain mapping
# Each chain is tried in order; if a backend fails, the next is attempted.
# "task:*" keys are internal routing hints (never posted as GitHub labels).
TASK_ROUTING = {
"agent:opus": {"chain": ["claude-opus", "claude"], "description": "Complex tasks"},
"agent:cursor": {"chain": ["cursor", "claude"], "description": "Cursor-preferred"},
"agent:marketing": {"chain": ["marketing", "claude"], "description": "Marketing copy"},
"conductor": {"chain": ["claude"], "description": "Default"},
"task:feature": {"chain": ["claude"], "description": "Well-specified features"},
"task:test": {"chain": ["claude"], "description": "Test writing"},
"task:review": {"chain": ["claude"], "description": "Code review"},
}
MAX_RETRIES = 2
AGENT_TIMEOUT_SECONDS = 30 * 60 # 30 minutes — kill agents that exceed this
def agent_key(repo: str, number: int) -> str:
return f"{repo}#{number}"
def _try_claim(repo: str, number: int) -> bool:
"""Atomically check for duplicates and claim the slot.
Returns True if this caller claimed the slot (proceed with spawn).
Returns False if another agent already holds it (skip).
"""
key = agent_key(repo, number)
with agent_lock:
if key in active_agents:
info = active_agents[key]
proc = info.get("process")
# Still running or still spawning → duplicate
if info.get("status") == "spawning" or (proc and proc.poll() is None):
log.info(" Agent already running for %s (pid %s)", key, info.get("pid", "spawning"))
return False
# Process finished, clean up stale entry
del active_agents[key]
# Claim the slot before releasing the lock
active_agents[key] = {
"status": "spawning",
"repo": repo,
"issue_number": number,
"started": datetime.now(timezone.utc).isoformat(),
}
return True
def _release_claim(repo: str, number: int):
"""Release a claimed slot if spawn fails."""
key = agent_key(repo, number)
with agent_lock:
info = active_agents.get(key)
if info and info.get("status") == "spawning":
del active_agents[key]
def _slugify(title: str, max_len: int = 40) -> str:
"""Convert an issue title to a branch-safe slug."""
slug = title.lower()
slug = re.sub(r"[^a-z0-9]+", "-", slug)
slug = slug.strip("-")
if len(slug) > max_len:
slug = slug[:max_len].rstrip("-")
return slug or "task"
def _setup_agent_workspace(
repo_dir: str, number: int, title: str, base_branch: str, branch_strategy: str
) -> tuple[str | None, str | None, str | None]:
"""Create an isolated workspace for the agent using git worktrees.
For feature-branch strategy: creates a git worktree with a feature branch.
For direct-commit: works in the main checkout (no isolation).
Returns (work_dir, branch_name, worktree_path) or (None, None, None) on failure.
worktree_path is set when a worktree was created (for cleanup later).
"""
if branch_strategy != "feature-branch":
return repo_dir, None, None
slug = _slugify(title)
branch = f"feature/issue-{number}-{slug}"
safe_key = f"{repo_dir.replace('/', '-').lstrip('-')}-{number}"
worktree_path = WORKTREE_BASE / safe_key
try:
# Clean up any stale worktree at this path (e.g., from crashed agent)
if worktree_path.exists():
subprocess.run(
["git", "worktree", "remove", "--force", str(worktree_path)],
cwd=repo_dir, capture_output=True, timeout=15,
)
# If git worktree remove failed, force-remove the directory
if worktree_path.exists():
shutil.rmtree(worktree_path, ignore_errors=True)
# Fetch latest from remote
subprocess.run(
["git", "fetch", "origin"],
cwd=repo_dir, capture_output=True, timeout=30,
)
# Determine base ref
base_ref = f"origin/{base_branch}"
result = subprocess.run(
["git", "rev-parse", "--verify", base_ref],
cwd=repo_dir, capture_output=True, text=True, timeout=10,
)
if result.returncode != 0:
base_ref = base_branch
# Ensure worktree base directory exists
WORKTREE_BASE.mkdir(parents=True, exist_ok=True)
# Check if branch already exists (e.g., /run re-trigger)
result = subprocess.run(
["git", "rev-parse", "--verify", branch],
cwd=repo_dir, capture_output=True, text=True, timeout=10,
)
if result.returncode == 0:
# Branch exists — create worktree on existing branch
result = subprocess.run(
["git", "worktree", "add", str(worktree_path), branch],
cwd=repo_dir, capture_output=True, text=True, timeout=15,
)
log.info(" Worktree (existing branch): %s → %s", branch, worktree_path)
else:
# Create new branch in worktree
result = subprocess.run(
["git", "worktree", "add", "-b", branch, str(worktree_path), base_ref],
cwd=repo_dir, capture_output=True, text=True, timeout=15,
)
log.info(" Worktree (new branch): %s from %s → %s", branch, base_ref, worktree_path)
if result.returncode != 0:
log.error(" Failed to create worktree: %s", result.stderr.strip())
return None, None, None
return str(worktree_path), branch, str(worktree_path)
except Exception as e:
log.error(" Failed to create agent workspace: %s", e)
return None, None, None
def _cleanup_worktree(repo_dir: str, worktree_path: str | None):
"""Remove a git worktree. Best-effort."""
if not worktree_path:
return
try:
# Prune any stale worktree references first
subprocess.run(
["git", "worktree", "prune"],
cwd=repo_dir, capture_output=True, timeout=15,
)
# Remove the worktree
result = subprocess.run(
["git", "worktree", "remove", "--force", str(worktree_path)],
cwd=repo_dir, capture_output=True, text=True, timeout=15,
)
if result.returncode == 0:
log.info(" Cleaned up worktree: %s", worktree_path)
else:
# Fallback: force-remove directory
if os.path.isdir(worktree_path):
shutil.rmtree(worktree_path, ignore_errors=True)
log.info(" Force-removed worktree dir: %s", worktree_path)
except Exception as e:
log.warning(" Failed to clean up worktree %s: %s", worktree_path, e)
WORKTREE_STALE_HOURS = 2 # worktrees older than this are considered stale
def _cleanup_stale_worktrees():
"""Remove worktrees older than WORKTREE_STALE_HOURS at startup.
Selective cleanup: only removes worktrees that are likely from crashed agents,
not ones that might belong to currently running agents on another subscriber instance.
"""
if not WORKTREE_BASE.exists():
return
cutoff = time.time() - (WORKTREE_STALE_HOURS * 3600)
removed = 0
for entry in WORKTREE_BASE.iterdir():
if not entry.is_dir():
continue
try:
mtime = entry.stat().st_mtime
if mtime < cutoff:
shutil.rmtree(entry, ignore_errors=True)
removed += 1
log.info(" Removed stale worktree: %s (age: %.1fh)",
entry.name, (time.time() - mtime) / 3600)
except OSError:
pass
if removed:
log.info("Cleaned up %d stale worktree(s)", removed)
def _resolve_route(labels: list[str]) -> tuple[str, dict]:
"""Resolve labels to a route key and TASK_ROUTING entry.
Returns (route_key, route_entry). Scans labels for the first match
in TASK_ROUTING; falls back to 'conductor'.
"""
for label in labels:
if label in TASK_ROUTING:
return label, TASK_ROUTING[label]
return "conductor", TASK_ROUTING["conductor"]
def spawn_agent(event: dict, dry_run: bool = False, chain_index: int = 0):
"""Spawn a claude agent for a conductor issue.
Args:
event: Issue event dict with repo, issue_number, labels, etc.
dry_run: Log only, don't actually spawn.
chain_index: Position in the fallback chain to start from (0 = first backend).
"""
repo = event["repo"]
number = event["issue_number"]
title = event["issue_title"]
repo_dir = event["repo_dir"]
labels = event.get("labels", [])
issue_url = event.get("issue_url", "")
if not repo_dir:
log.error(" No directory mapping for %s — cannot spawn", repo)
return
if not os.path.isdir(repo_dir):
log.error(" Directory %s does not exist — cannot spawn", repo_dir)
return
if not _try_claim(repo, number):
return
# Resolve route from labels → fallback chain
route_key, route_entry = _resolve_route(labels)
chain = route_entry["chain"]
# Skip backends that can't spawn (command=None), advancing through chain
backend_key = None
backend = None
while chain_index < len(chain):
candidate = chain[chain_index]
if BACKENDS[candidate]["command"] is not None:
backend_key = candidate
backend = BACKENDS[candidate]
break
log.info(" Skipping %s (no command) — trying next in chain", BACKENDS[candidate]["description"])
chain_index += 1
if backend is None:
log.info(" No spawnable backend in chain for %s — logging only", route_entry["description"])
return
# Workspace setup — isolated worktree for feature branches
repo_config = event.get("repo_config", {})
branch_strategy = repo_config.get("branch_strategy", "feature-branch")
base_branch = repo_config.get("base_branch", "develop")
# Allow `direct-commit` label to override branch strategy for simple tasks
if "direct-commit" in labels:
branch_strategy = "direct-commit"
log.info(" direct-commit label — working on current branch (no worktree)")
work_dir, branch_name, worktree_path = _setup_agent_workspace(
repo_dir, number, title, base_branch, branch_strategy,
)
if work_dir is None:
log.warning(" Failed to create workspace — agent will work in main checkout")
work_dir = repo_dir
branch_instructions = ""
if branch_name:
branch_instructions = (
f"\n\n## Git Workflow\n"
f"You are on feature branch `{branch_name}` (based on `{base_branch}`).\n"
f"Do NOT push directly to `{base_branch}`, `master`, or `main`.\n"
f"When your work is complete:\n"
f"1. Commit and push your branch: `git push -u origin {branch_name}`\n"
f"2. Open a PR: `gh pr create --base {base_branch} --head {branch_name} "
f"--title \"<title>\" --body \"Closes #{number}\\n\\n<summary>\"`\n"
f"3. Call complete() with a link to the PR"
)
# Build the lean prompt — protocol details come from conductor MCP instructions
extra_context = event.get("extra_context", "")
prompt = (
f"You are a conductor agent assigned to {repo}#{number}: {title}\n"
f"URL: {issue_url}\n\n"
f"You have the conductor MCP connected. Call my_assignment() to get full context,\n"
f"then follow the protocol in the conductor instructions.\n"
f"Start by calling acknowledge().\n\n"
f"If conductor MCP tools fail (stale session), fall back to gh CLI:\n"
f" Comment: gh issue comment {number} --repo {repo} --body \"<message>\"\n"
f" Close: gh issue close {number} --repo {repo}\n"
f" Re-trigger: comment `/run` on the issue to re-spawn an agent"
f"{branch_instructions}"
)
if extra_context:
prompt += f"\n\nAdditional context:\n{extra_context}"
# Build command — use claudea for full permissions via Anthropic API
cmd = [
"/home/tim/bin/claudea",
"-p", prompt,
"--output-format", "json",
]
key = agent_key(repo, number)
log.info(" SPAWNING: %s in %s (chain: %s, index: %d)",
backend["description"], work_dir, route_key, chain_index)
if worktree_path:
log.info(" Worktree: %s", worktree_path)
log.info(" Issue: %s — %s", key, title)
if dry_run:
log.info(" [DRY RUN] Would run: claude -p '...' in %s", work_dir)
return
# Ensure log directory exists
LOG_DIR.mkdir(parents=True, exist_ok=True)
timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
safe_key = key.replace("/", "-").replace("#", "-")
log_file = LOG_DIR / f"{timestamp}-{safe_key}.log"
try:
with open(log_file, "w") as f:
proc = subprocess.Popen(
cmd,
cwd=work_dir,
stdout=f,
stderr=subprocess.STDOUT,
env={
**os.environ,
"CLAUDE_CODE_ENTRYPOINT": "conductor",
"CONDUCTOR_AGENT_REPO": repo,
"CONDUCTOR_AGENT_ISSUE": str(number),
},
)
# Upgrade the "spawning" placeholder to full agent info
with agent_lock:
active_agents[key] = {
"process": proc,
"pid": proc.pid,
"repo": repo,
"issue_number": number,
"title": title,
"started": datetime.now(timezone.utc).isoformat(),
"log_file": str(log_file),
"route_key": route_key,
"chain_index": chain_index,
"backend_key": backend_key,
"event": event,
"repo_dir": repo_dir,
"worktree_path": worktree_path,
}
log.info(" Started agent pid %d — log: %s", proc.pid, log_file)
# Track agent spawn
_track_event("agent_started", repo=repo, issue_number=number,
title=title, pid=proc.pid, backend=backend_key,
route=route_key)
# Comment on the issue that an agent has been dispatched
branch_note = f" on `{branch_name}`" if branch_name else ""
worktree_note = " (worktree)" if worktree_path else ""
try:
subprocess.run(
["gh", "issue", "comment", str(number),
"--repo", repo,
"--body", f"📡 Conductor dispatched agent (pid {proc.pid}, {backend['description']}) in `{repo_dir}`{branch_note}{worktree_note}"],
capture_output=True, text=True, timeout=15,
)
except Exception:
pass # Best effort
# Publish agent started event
try:
r = redis.Redis()
r.publish("conductor:agent_started", json.dumps({
"key": key,
"repo": repo,
"issue_number": number,
"title": title,
"pid": proc.pid,
"repo_dir": repo_dir,
"log_file": str(log_file),
"timestamp": datetime.now(timezone.utc).isoformat(),
}))
except Exception:
pass
# Start a thread to monitor completion
t = threading.Thread(
target=_monitor_agent, args=(key, proc, log_file, repo, number),
daemon=True,
)
t.start()
except Exception as e:
_release_claim(repo, number)
log.error(" Failed to spawn agent: %s", e)
def _record_attempt(repo: str, number: int, backend_key: str, success: bool, exit_code: int, elapsed: str):
"""Record attempt in Redis: per-issue history + per-backend stats."""
try:
r = redis.Redis()
# Per-issue retry history (7-day TTL)
history_key = f"conductor:retries:{repo}#{number}"
entry = json.dumps({
"backend": backend_key,
"success": success,
"exit_code": exit_code,
"elapsed": elapsed,
"timestamp": datetime.now(timezone.utc).isoformat(),
})
r.rpush(history_key, entry)
r.expire(history_key, 7 * 86400)
# Per-backend aggregate stats
stat_field = f"{backend_key}:{'successes' if success else 'failures'}"
r.hincrby("conductor:backend_stats", stat_field, 1)
except Exception as e:
log.warning("Failed to record attempt stats: %s", e)
def _post_retry_comment(repo: str, number: int, tried_desc: str, exit_code: int, next_desc: str, attempt: int):
"""Post a retry comment on the GitHub issue."""
body = f"🔄 Retry {attempt}/{MAX_RETRIES} — {tried_desc} failed (exit {exit_code}), trying {next_desc}"
try:
subprocess.run(
["gh", "issue", "comment", str(number), "--repo", repo, "--body", body],
capture_output=True, text=True, timeout=15,
)
except Exception:
pass
def _post_exhausted_comment(repo: str, number: int):
"""Post a comment that all backends in the chain have been exhausted."""
body = "❌ All backends exhausted — no more retries available. Comment `/run` to try again manually."
try:
subprocess.run(
["gh", "issue", "comment", str(number), "--repo", repo, "--body", body],
capture_output=True, text=True, timeout=15,
)
except Exception:
pass
def _extract_cost_info(log_file: Path) -> dict:
"""Try to extract cost/token info from the agent's JSON output log.
Claude Code with --output-format json writes a final JSON summary.
We look for it in the last few lines of the log file.
"""
try:
with open(log_file) as f:
# Read last 50 lines to find the JSON summary
lines = f.readlines()
for line in reversed(lines[-50:]):
line = line.strip()
if not line.startswith("{"):
continue
try:
data = json.loads(line)
result = {}
# Claude Code JSON output includes cost_usd and token usage
if "cost_usd" in data:
result["total_cost"] = data["cost_usd"]
if "total_tokens" in data:
result["total_tokens"] = data["total_tokens"]
if "input_tokens" in data:
result["input_tokens"] = data["input_tokens"]
if "output_tokens" in data:
result["output_tokens"] = data["output_tokens"]
# Also check nested stats
stats = data.get("stats", {})
if stats:
if "cost_usd" in stats:
result["total_cost"] = stats["cost_usd"]
if "total_tokens" in stats:
result["total_tokens"] = stats["total_tokens"]
if result:
return result
except (json.JSONDecodeError, KeyError):
continue
except Exception:
pass
return {}
def _monitor_agent(key: str, proc, log_file: Path, repo: str, number: int):
"""Monitor an agent process and retry on failure using the fallback chain."""
try:
proc.wait(timeout=AGENT_TIMEOUT_SECONDS)
except subprocess.TimeoutExpired:
log.warning("Agent %s exceeded timeout (%ds) — killing", key, AGENT_TIMEOUT_SECONDS)
proc.kill()
proc.wait(timeout=10) # reap the zombie
elapsed = "unknown"
with agent_lock:
info = active_agents.pop(key, {})
if info.get("started"):
start = datetime.fromisoformat(info["started"])
elapsed = str(datetime.now(timezone.utc) - start).split(".")[0]
exit_code = proc.returncode
route_key = info.get("route_key", "conductor")
chain_index = info.get("chain_index", 0)
backend_key = info.get("backend_key", "claude")
event = info.get("event", {})
agent_repo_dir = info.get("repo_dir", "")
worktree_path = info.get("worktree_path")
status = "completed" if exit_code == 0 else f"failed (exit {exit_code})"
log.info("Agent %s %s after %s — log: %s", key, status, elapsed, log_file)
# Try to extract cost/token info from agent JSON output
cost_info = _extract_cost_info(log_file)
if cost_info:
log.info(" Cost: $%.4f, tokens: %s", cost_info.get("total_cost", 0), cost_info.get("total_tokens", "?"))
# Record attempt stats + tracking
_record_attempt(repo, number, backend_key, exit_code == 0, exit_code, elapsed)
_track_event("agent_completed", repo=repo, issue_number=number,
exit_code=exit_code, elapsed=elapsed, backend=backend_key,
status="success" if exit_code == 0 else "failure",
**cost_info)
# Clean up worktree before retry or exit (frees the branch for reuse)
_cleanup_worktree(agent_repo_dir, worktree_path)
# Retry logic on failure
if exit_code != 0 and event:
chain = TASK_ROUTING.get(route_key, {}).get("chain", [])
next_index = chain_index + 1
if next_index < len(chain) and next_index <= MAX_RETRIES:
next_backend_key = chain[next_index]
tried_desc = BACKENDS.get(backend_key, {}).get("description", backend_key)
next_desc = BACKENDS.get(next_backend_key, {}).get("description", next_backend_key)
log.info(" Retrying %s: %s → %s (chain index %d)", key, tried_desc, next_desc, next_index)
_post_retry_comment(repo, number, tried_desc, exit_code, next_desc, next_index)
spawn_agent(event, chain_index=next_index)
return
else:
log.info(" All backends exhausted for %s", key)
_post_exhausted_comment(repo, number)
# Notify dispatching agent on completion (best-effort)
if exit_code == 0 and event:
prompt = _build_completed_prompt(repo, number, event, elapsed)
notify_callback(repo, number, event, reason="completed", prompt=prompt)
# Publish completion event to Redis
try:
r = redis.Redis()
r.publish("conductor:agent_complete", json.dumps({
"key": key,
"repo": repo,
"issue_number": number,
"status": "success" if exit_code == 0 else "failure",
"exit_code": exit_code,
"elapsed": elapsed,
"log_file": str(log_file),
"backend": backend_key,
"timestamp": datetime.now(timezone.utc).isoformat(),
}))
except Exception:
pass # Best effort
def notify_callback(repo: str, number: int, event: dict, reason: str, prompt: str, dry_run: bool = False):
"""Resume the dispatching agent's session with a notification.
Looks up the callback session ID stored by dispatch(), then spawns
`claudea --resume <session-id>` with the provided prompt.
Args:
repo: Target repo
number: Issue number
event: Original event dict (for repo_dir, issue_url, etc.)
reason: Short label for logging (e.g., "blocked", "completed")
prompt: Full prompt to send to the resumed session
dry_run: Log only, don't spawn
"""
try:
r = redis.Redis()
callback_key = f"conductor:callback:{repo}#{number}"
callback = r.hgetall(callback_key)
if not callback:
log.info(" No callback session for %s#%s — no one to notify", repo, number)
return
session_id = callback.get(b"session_id", b"").decode()
if not session_id:
log.info(" Callback has no session_id — skipping")
return
log.info(" Callback session found for %s: %s", reason, session_id[:8])
if dry_run:
log.info(" [DRY RUN] Would resume session %s with %s notification", session_id[:8], reason)
return
cmd = [
"/home/tim/bin/claudea",
"--resume", session_id,
"-p", prompt,
"--output-format", "json",
]
LOG_DIR.mkdir(parents=True, exist_ok=True)
timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
safe_key = f"{repo.replace('/', '-')}-{number}"
log_file = LOG_DIR / f"{timestamp}-callback-{reason}-{safe_key}.log"
with open(log_file, "w") as f:
proc = subprocess.Popen(
cmd,
cwd=event.get("repo_dir") or "/home/tim",
stdout=f,
stderr=subprocess.STDOUT,
)
log.info(" Callback agent spawned (pid %d, session %s, %s) — log: %s",
proc.pid, session_id[:8], reason, log_file)
except Exception as e:
log.error(" Failed to notify callback (%s): %s", reason, e)
def _build_blocked_prompt(repo: str, number: int, event: dict) -> str:
"""Build prompt for blocked callback notification."""
try:
result = subprocess.run(
["gh", "issue", "view", str(number), "--repo", repo,
"--json", "body,comments", "--jq",
'.comments[-1].body // "No reason given"'],
capture_output=True, text=True, timeout=15,
)
blocked_reason = result.stdout.strip() if result.returncode == 0 else "Could not fetch reason"
except Exception:
blocked_reason = "Could not fetch reason"
return (
f"⚠️ BLOCKED AGENT NOTIFICATION\n\n"
f"An agent you dispatched to {repo}#{number} is blocked and needs help.\n\n"
f"Issue: {event.get('issue_url', f'https://github.com/{repo}/issues/{number}')}\n"
f"Title: {event.get('issue_title', 'unknown')}\n\n"
f"Blocked reason (from latest comment):\n{blocked_reason}\n\n"
f"Please review the issue thread with "
f"`gh issue view {number} --repo {repo} --comments` "
f"and either:\n"
f"1. Comment on the issue with guidance for the agent, then "
f"comment `/run` to re-trigger\n"
f"2. Resolve the blocker yourself and comment `/run`\n"
f"3. Close the issue if the task should be abandoned"
)
def _build_completed_prompt(repo: str, number: int, event: dict, elapsed: str) -> str:
"""Build prompt for completion callback notification."""
try:
result = subprocess.run(
["gh", "issue", "view", str(number), "--repo", repo,
"--json", "comments", "--jq",
'.comments[-1].body // "No summary available"'],
capture_output=True, text=True, timeout=15,
)
summary = result.stdout.strip() if result.returncode == 0 else "No summary available"
except Exception:
summary = "No summary available"
return (
f"✅ DISPATCHED TASK COMPLETED\n\n"
f"The agent you dispatched to {repo}#{number} has finished ({elapsed}).\n\n"
f"Issue: {event.get('issue_url', f'https://github.com/{repo}/issues/{number}')}\n"
f"Title: {event.get('issue_title', 'unknown')}\n\n"
f"Agent's last comment:\n{summary}\n\n"
f"Review with `gh issue view {number} --repo {repo} --comments` if needed."
)
def handle_issue_event(event: dict, dry_run: bool = False):
"""Process an issue event from Redis."""
action = event.get("action")
repo = event.get("repo")
number = event.get("issue_number")
title = event.get("issue_title")
labels = event.get("labels", [])
repo_dir = event.get("repo_dir")
has_conductor = event.get("has_conductor_label", False)
log.info("Issue %s: %s#%s — %s", action, repo, number, title)
log.info(" Labels: %s", ", ".join(labels) if labels else "none")
log.info(" Directory: %s", repo_dir or "UNKNOWN")
if not has_conductor:
log.info(" No conductor label — skipping")
return
# Detect blocked label being added
if action == "labeled" and "blocked" in labels:
log.info(" 🚨 BLOCKED label detected — notifying callback")
prompt = _build_blocked_prompt(repo, number, event)
notify_callback(repo, number, event, reason="blocked", prompt=prompt, dry_run=dry_run)
return
if action not in ("opened", "reopened"):
# Only spawn on 'opened' or 'reopened', not 'labeled' — avoids double-spawn
# (GitHub sends both 'opened' and 'labeled' when issue is created with labels)
log.info(" Action '%s' — skipping (only 'opened'/'reopened' triggers spawn)", action)
return
spawn_agent(event, dry_run=dry_run)
def handle_comment_event(event: dict, dry_run: bool = False):
"""Process an issue comment event from Redis.
Supports /run command to trigger agent spawn, with optional extra context.
Any text after /run on the same or subsequent lines is passed as additional
context to the spawned agent.
"""
action = event.get("action")
repo = event.get("repo")
number = event.get("issue_number")
title = event.get("issue_title")
labels = event.get("labels", [])
has_conductor = event.get("has_conductor_label", False)
comment_body = event.get("comment_body", "")
comment_author = event.get("comment_author", "")
log.info("Comment %s: %s#%s by %s", action, repo, number, comment_author)
if action != "created":
log.info(" Comment action '%s' — skipping (only 'created' triggers)", action)
return
if not has_conductor:
log.info(" No conductor label on issue — skipping comment")
return
# Check for /run command
stripped = comment_body.strip()
if not stripped.startswith("/run"):
log.info(" Comment does not start with /run — skipping")
return
# Extract extra context: everything after "/run" (same line + subsequent lines)
extra = stripped[4:].strip()
log.info(" /run command detected from %s", comment_author)
if extra:
log.info(" Extra context: %s", extra[:100])
# Build a synthetic issue event for spawn_agent
spawn_event = {
"repo": repo,
"issue_number": number,
"issue_title": title,
"repo_dir": event.get("repo_dir"),
"repo_config": event.get("repo_config", {}),
"labels": labels,
"issue_url": event.get("issue_url", ""),
"extra_context": extra,
}
spawn_agent(spawn_event, dry_run=dry_run)
# ---------------------------------------------------------------------------
# PR Reviewer Agent
# ---------------------------------------------------------------------------
def handle_pr_event(event: dict, dry_run: bool = False):
"""Process a pull_request event from Redis."""
action = event.get("action")
repo = event.get("repo")
pr_number = event.get("pr_number")
pr_title = event.get("pr_title", "")
pr_base = event.get("pr_base", "")
pr_draft = event.get("pr_draft", False)
repo_config = event.get("repo_config", {})
repo_dir = event.get("repo_dir")
log.info("PR %s: %s#%s — %s", action, repo, pr_number, pr_title)
# Only review on opened (not synchronize, edited, etc.)
if action != "opened":
log.info(" PR action '%s' — skipping (only 'opened' triggers review)", action)
return
# Skip drafts
if pr_draft:
log.info(" Draft PR — skipping review")
return
# Only review PRs targeting the repo's base_branch
base_branch = repo_config.get("base_branch", "develop")
if pr_base != base_branch:
log.info(" PR targets %s, not %s — skipping review", pr_base, base_branch)
return
# Only review if repo is monitored
if not repo_dir:
log.info(" Repo not monitored — skipping review")
return
spawn_reviewer(event, dry_run=dry_run)
def spawn_reviewer(event: dict, dry_run: bool = False):
"""Spawn a reviewer agent for a pull request."""
repo = event["repo"]
pr_number = event["pr_number"]
pr_title = event.get("pr_title", "")
pr_url = event.get("pr_url", "")
pr_head = event.get("pr_head", "")
pr_base = event.get("pr_base", "")
repo_dir = event["repo_dir"]
repo_config = event.get("repo_config", {})
auto_merge = repo_config.get("auto_merge", False)
key = f"review:{repo}#{pr_number}"
# Dedup check
with agent_lock:
if key in active_agents:
proc = active_agents[key].get("process")
if proc and proc.poll() is None:
log.info(" Reviewer already running for %s", key)
return
del active_agents[key]
if auto_merge:
merge_instruction = (
"\n\n## Auto-merge\n"
f"This repo has auto_merge enabled. If your review passes AND all CI checks are green:\n"
f" gh pr merge {pr_number} --repo {repo} --squash --auto\n"
f"If review fails, post a requesting-changes review and do NOT merge."
)
else:
merge_instruction = (
"\n\n## Merge policy\n"
f"This repo does NOT have auto_merge. After reviewing, leave your verdict as a PR review.\n"
f"Do NOT merge. A human will merge after reviewing your assessment."
)
prompt = (
f"You are a code reviewer for {repo} PR #{pr_number}: {pr_title}\n"
f"URL: {pr_url}\n"
f"Branch: {pr_head} → {pr_base}\n\n"
f"## Review checklist\n"
f"1. Read the PR diff: `gh pr diff {pr_number} --repo {repo}`\n"
f"2. Read the PR description: `gh pr view {pr_number} --repo {repo}`\n"
f"3. Check for: correctness, edge cases, style consistency, test coverage\n"
f"4. If there are tests, run them (check the repo's CLAUDE.md for test commands)\n"
f"5. Check CI status: `gh pr checks {pr_number} --repo {repo} --watch`\n"
f" - If CI is still running, wait up to 5 minutes\n"
f"6. Submit your review:\n"
f" - APPROVE: `gh pr review {pr_number} --repo {repo} --approve --body \"<summary>\"`\n"
f" - REQUEST CHANGES: `gh pr review {pr_number} --repo {repo} --request-changes --body \"<issues found>\"`\n"
f"{merge_instruction}\n\n"
f"## Guidelines\n"
f"- Be concise. Flag real issues, not style nits.\n"
f"- If the PR looks good and tests pass, approve it.\n"
f"- If you find issues, be specific about what to fix.\n"
)
cmd = [
"/home/tim/bin/claudea",
"-p", prompt,
"--output-format", "json",
]
log.info(" SPAWNING REVIEWER: %s in %s (auto_merge=%s)", key, repo_dir, auto_merge)
if dry_run:
log.info(" [DRY RUN] Would spawn reviewer for %s", key)
return
LOG_DIR.mkdir(parents=True, exist_ok=True)
timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
safe_key = key.replace("/", "-").replace("#", "-").replace(":", "-")
log_file = LOG_DIR / f"{timestamp}-{safe_key}.log"
try:
with open(log_file, "w") as f:
proc = subprocess.Popen(
cmd,
cwd=repo_dir, # reviewer runs in main checkout (read-only review)
stdout=f,
stderr=subprocess.STDOUT,
env={
**os.environ,
"CLAUDE_CODE_ENTRYPOINT": "conductor-reviewer",
},
)
with agent_lock:
active_agents[key] = {
"process": proc,
"pid": proc.pid,
"repo": repo,
"pr_number": pr_number,
"title": pr_title,
"started": datetime.now(timezone.utc).isoformat(),
"log_file": str(log_file),
"type": "reviewer",
}
log.info(" Started reviewer pid %d — log: %s", proc.pid, log_file)
# Comment on the PR
try:
subprocess.run(
["gh", "pr", "comment", str(pr_number),
"--repo", repo,
"--body", f"🔍 Conductor reviewer dispatched (pid {proc.pid})"],
capture_output=True, text=True, timeout=15,
)
except Exception:
pass
# Publish reviewer started event + track
_track_event("reviewer_started", repo=repo, pr_number=pr_number,