-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathspawner.py
More file actions
264 lines (228 loc) · 8.5 KB
/
Copy pathspawner.py
File metadata and controls
264 lines (228 loc) · 8.5 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
from __future__ import annotations
import os
import shlex
import shutil
import subprocess
import time
from pathlib import Path
from claude_teams import messaging, teams
from claude_teams.models import COLOR_PALETTE, InboxMessage, TeammateMember
from claude_teams.teams import _VALID_NAME_RE
from claude_teams import opencode_client
_OPENCODE_PROMPT_WRAPPER = """\
You are team member '{name}' on team '{team_name}'.
You have MCP tools from the claude-teams server for team coordination:
- poll_inbox(team_name="{team_name}", agent_name="{name}") - Check for new messages
- send_message(team_name="{team_name}", type="message", sender="{name}", recipient="team-lead", content="...", summary="...") - Message teammates
- task_list(team_name="{team_name}") - View team tasks
- task_update(team_name="{team_name}", task_id="...", status="...") - Update task status
- task_get(team_name="{team_name}", task_id="...") - Get task details
Start by reading your inbox for instructions.
---
{prompt}"""
def discover_harness_binary(name: str) -> str | None:
return shutil.which(name)
def use_tmux_windows() -> bool:
"""Return True when teammate processes should be spawned in tmux windows."""
return os.environ.get("USE_TMUX_WINDOWS") is not None
def build_tmux_spawn_args(command: str, name: str) -> list[str]:
"""Build the tmux command used to spawn a teammate process."""
if use_tmux_windows():
return [
"tmux",
"new-window",
"-dP",
"-F",
"#{window_id}",
"-n",
f"@claude-team | {name}",
command,
]
return ["tmux", "split-window", "-dP", "-F", "#{pane_id}", command]
def discover_opencode_models(opencode_binary: str) -> list[str]:
"""Run ``opencode models --refresh`` and return available model names."""
try:
result = subprocess.run(
[opencode_binary, "models", "--refresh"],
capture_output=True,
text=True,
timeout=30,
)
if result.returncode != 0:
return []
lines = result.stdout.strip().splitlines()
# First line is status message, rest are model names
return [line.strip() for line in lines[1:] if line.strip()]
except (subprocess.TimeoutExpired, FileNotFoundError, OSError):
return []
def assign_color(team_name: str, base_dir: Path | None = None) -> str:
config = teams.read_config(team_name, base_dir)
count = sum(1 for m in config.members if isinstance(m, TeammateMember))
return COLOR_PALETTE[count % len(COLOR_PALETTE)]
def build_spawn_command(
member: TeammateMember,
claude_binary: str,
lead_session_id: str,
) -> str:
team_name = member.agent_id.split("@", 1)[1]
cmd = (
f"cd {shlex.quote(member.cwd)} && "
f"CLAUDECODE=1 CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS=1 "
f"{shlex.quote(claude_binary)} "
f"--agent-id {shlex.quote(member.agent_id)} "
f"--agent-name {shlex.quote(member.name)} "
f"--team-name {shlex.quote(team_name)} "
f"--agent-color {shlex.quote(member.color)} "
f"--parent-session-id {shlex.quote(lead_session_id)} "
f"--agent-type {shlex.quote(member.agent_type)} "
f"--model {shlex.quote(member.model)}"
)
if member.plan_mode_required:
cmd += " --plan-mode-required"
return cmd
def build_opencode_attach_command(
opencode_binary: str,
server_url: str,
session_id: str,
cwd: str,
) -> str:
return (
f"{shlex.quote(opencode_binary)} attach "
f"{shlex.quote(server_url)} "
f"-s {shlex.quote(session_id)} "
f"--dir {shlex.quote(cwd)}"
)
def spawn_teammate(
team_name: str,
name: str,
prompt: str,
claude_binary: str,
lead_session_id: str,
*,
model: str = "sonnet",
subagent_type: str = "general-purpose",
cwd: str | None = None,
plan_mode_required: bool = False,
base_dir: Path | None = None,
backend_type: str = "claude",
opencode_binary: str | None = None,
opencode_server_url: str | None = None,
opencode_agent: str | None = None,
) -> TeammateMember:
if not _VALID_NAME_RE.match(name):
raise ValueError(
f"Invalid agent name: {name!r}. Use only letters, numbers, hyphens, underscores."
)
if len(name) > 64:
raise ValueError(f"Agent name too long ({len(name)} chars, max 64)")
if name == "team-lead":
raise ValueError("Agent name 'team-lead' is reserved")
if backend_type == "opencode" and not opencode_binary:
raise ValueError(
"Cannot spawn opencode teammate: 'opencode' binary not found on PATH. "
"Install OpenCode or ensure it is in your PATH."
)
if backend_type == "opencode" and not opencode_server_url:
raise ValueError(
"Cannot spawn opencode teammate: OPENCODE_SERVER_URL is not set. "
"Start 'opencode serve' and set the environment variable."
)
if backend_type == "claude" and not claude_binary:
raise ValueError(
"Cannot spawn claude teammate: 'claude' binary not found on PATH. "
"Install Claude Code or ensure it is in your PATH."
)
resolved_cwd = cwd or str(Path.cwd())
opencode_session_id: str | None = None
if backend_type == "opencode":
opencode_client.verify_mcp_configured(opencode_server_url)
opencode_session_id = opencode_client.create_session(
opencode_server_url,
title=f"{name}@{team_name}",
permissions=[{"permission": "*", "pattern": "*", "action": "allow"}],
)
color = assign_color(team_name, base_dir)
now_ms = int(time.time() * 1000)
member = TeammateMember(
agent_id=f"{name}@{team_name}",
name=name,
agent_type=subagent_type,
model=model,
prompt=prompt,
color=color,
plan_mode_required=plan_mode_required,
joined_at=now_ms,
tmux_pane_id="",
cwd=resolved_cwd,
backend_type=backend_type,
opencode_session_id=opencode_session_id,
is_active=False,
)
member_added = False
try:
teams.add_member(team_name, member, base_dir)
member_added = True
messaging.ensure_inbox(team_name, name, base_dir)
initial_msg = InboxMessage(
from_="team-lead",
text=prompt,
timestamp=messaging.now_iso(),
read=False,
)
messaging.append_message(team_name, name, initial_msg, base_dir)
if backend_type == "opencode":
wrapped = _OPENCODE_PROMPT_WRAPPER.format(
name=name,
team_name=team_name,
prompt=prompt,
)
opencode_client.send_prompt_async(
opencode_server_url,
opencode_session_id,
wrapped,
agent=opencode_agent or "build",
)
cmd = build_opencode_attach_command(
opencode_binary,
opencode_server_url,
opencode_session_id,
resolved_cwd,
)
else:
cmd = build_spawn_command(member, claude_binary, lead_session_id)
result = subprocess.run(
build_tmux_spawn_args(cmd, name),
capture_output=True,
text=True,
check=True,
)
pane_id = result.stdout.strip()
config = teams.read_config(team_name, base_dir)
for m in config.members:
if isinstance(m, TeammateMember) and m.name == name:
m.tmux_pane_id = pane_id
break
teams.write_config(team_name, config, base_dir)
except Exception:
if member_added:
try:
teams.remove_member(team_name, name, base_dir)
except Exception:
pass
if backend_type == "opencode" and opencode_server_url and opencode_session_id:
try:
opencode_client.abort_session(opencode_server_url, opencode_session_id)
except Exception:
pass
try:
opencode_client.delete_session(opencode_server_url, opencode_session_id)
except Exception:
pass
raise
member.tmux_pane_id = pane_id
return member
def kill_tmux_pane(pane_id: str) -> None:
if pane_id.startswith("@"):
subprocess.run(["tmux", "kill-window", "-t", pane_id], check=False)
return
subprocess.run(["tmux", "kill-pane", "-t", pane_id], check=False)