@orchestrator-claude/cli 3.25.1 → 3.26.0
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.
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/templates/base/claude/agents/debug-sidecar.md +133 -0
- package/dist/templates/base/claude/agents/doc-sidecar.md +60 -0
- package/dist/templates/base/claude/agents/implementer.md +162 -31
- package/dist/templates/base/claude/agents/test-sidecar.md +106 -0
- package/dist/templates/base/claude/hooks/mailbox-listener.ts +246 -0
- package/dist/templates/base/claude/hooks/sprint-registry.ts +85 -0
- package/dist/templates/base/claude/settings.json +19 -0
- package/dist/templates/base/claude/skills/sprint-launch/SKILL.md +176 -0
- package/dist/templates/base/claude/skills/sprint-teammate/sprint-teammate.md +79 -0
- package/dist/templates/base/scripts/lib/SprintLauncher.ts +325 -0
- package/dist/templates/base/scripts/lib/TmuxManager.ts +296 -0
- package/dist/templates/base/scripts/lib/WorktreeIsolator.ts +165 -0
- package/dist/templates/base/scripts/lib/WorktreeManager.ts +106 -0
- package/dist/templates/base/scripts/lib/mailbox/types.ts +175 -0
- package/dist/templates/base/scripts/lib/sidecar/SidecarWatcher.ts +249 -0
- package/dist/templates/base/scripts/lib/sidecar/run.ts +90 -0
- package/dist/templates/base/scripts/sprint-launch.ts +285 -0
- package/package.json +1 -1
- package/templates/base/claude/agents/debug-sidecar.md +133 -0
- package/templates/base/claude/agents/doc-sidecar.md +60 -0
- package/templates/base/claude/agents/implementer.md +162 -31
- package/templates/base/claude/agents/test-sidecar.md +106 -0
- package/templates/base/claude/hooks/mailbox-listener.ts +246 -0
- package/templates/base/claude/hooks/sprint-registry.ts +85 -0
- package/templates/base/claude/settings.json +19 -0
- package/templates/base/claude/skills/sprint-launch/SKILL.md +176 -0
- package/templates/base/claude/skills/sprint-teammate/sprint-teammate.md +79 -0
- package/templates/base/scripts/lib/SprintLauncher.ts +325 -0
- package/templates/base/scripts/lib/TmuxManager.ts +296 -0
- package/templates/base/scripts/lib/WorktreeIsolator.ts +165 -0
- package/templates/base/scripts/lib/WorktreeManager.ts +106 -0
- package/templates/base/scripts/lib/mailbox/types.ts +175 -0
- package/templates/base/scripts/lib/sidecar/SidecarWatcher.ts +249 -0
- package/templates/base/scripts/lib/sidecar/run.ts +90 -0
- package/templates/base/scripts/sprint-launch.ts +285 -0
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
import * as childProcess from 'child_process';
|
|
2
|
+
import * as fs from 'fs';
|
|
3
|
+
|
|
4
|
+
// ---------------------------------------------------------------------------
|
|
5
|
+
// Types
|
|
6
|
+
// ---------------------------------------------------------------------------
|
|
7
|
+
|
|
8
|
+
export interface PaneLaunchConfig {
|
|
9
|
+
/** Agent role name passed to `--agent` flag */
|
|
10
|
+
role: string;
|
|
11
|
+
/** Worktree or project root path used as the pane working directory */
|
|
12
|
+
workdir: string;
|
|
13
|
+
/**
|
|
14
|
+
* Optional initial prompt for this pane. Not used by TmuxManager directly —
|
|
15
|
+
* consumed by SprintLauncher which calls `sendPromptLiteral()` after launch.
|
|
16
|
+
*/
|
|
17
|
+
initialPrompt?: string;
|
|
18
|
+
/**
|
|
19
|
+
* Optional custom command to run instead of `claude --agent {role}`.
|
|
20
|
+
* Used by reactive sidecars (RFC-025 v3.3) to launch Node.js watcher
|
|
21
|
+
* processes instead of interactive Claude sessions.
|
|
22
|
+
*/
|
|
23
|
+
command?: string;
|
|
24
|
+
/**
|
|
25
|
+
* Sprint session name (e.g. "sprint-5c757626"). When set, exported as
|
|
26
|
+
* SPRINT_ID env var so the sprint-teammate skill can resolve mailbox paths.
|
|
27
|
+
*/
|
|
28
|
+
sprintId?: string;
|
|
29
|
+
/**
|
|
30
|
+
* Pane role with index (e.g. "implementer-0", "doc-sidecar-1"). When set,
|
|
31
|
+
* exported as PANE_ROLE env var for the sprint-teammate skill.
|
|
32
|
+
*/
|
|
33
|
+
paneRole?: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface TmuxManager {
|
|
37
|
+
/** Returns true when a tmux session with the given name exists. */
|
|
38
|
+
hasSession(name: string): boolean;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Creates a new detached tmux session and starts the first pane.
|
|
42
|
+
* Window geometry: 220 columns x 50 rows.
|
|
43
|
+
*/
|
|
44
|
+
createSession(sessionName: string, firstPane: PaneLaunchConfig): void;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Adds a pane to window 0 of an existing session via a horizontal split.
|
|
48
|
+
*/
|
|
49
|
+
addPane(sessionName: string, pane: PaneLaunchConfig): void;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Applies the `even-horizontal` layout to window 0 of the session.
|
|
53
|
+
* Call after all panes have been added.
|
|
54
|
+
*/
|
|
55
|
+
applyLayout(sessionName: string): void;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Kills the named tmux session.
|
|
59
|
+
* Errors (e.g. session not found) are swallowed.
|
|
60
|
+
*/
|
|
61
|
+
killSession(name: string): void;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Convenience method: creates session + adds remaining panes + applies layout.
|
|
65
|
+
*
|
|
66
|
+
* @param sessionName Name for the tmux session.
|
|
67
|
+
* @param panes At least one PaneLaunchConfig. First becomes the session's
|
|
68
|
+
* initial pane; subsequent configs are added via split-window.
|
|
69
|
+
* @returns The session name passed in.
|
|
70
|
+
* @throws When `panes` is empty.
|
|
71
|
+
*/
|
|
72
|
+
launchLayout(sessionName: string, panes: PaneLaunchConfig[]): string;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Sends a prompt to a pane via literal keystrokes (`send-keys -l`) + Enter.
|
|
76
|
+
* Does NOT trigger bracket paste mode — prompt auto-submits in Claude Code.
|
|
77
|
+
* Use for initial sprint prompts and mid-sprint task dispatches.
|
|
78
|
+
*/
|
|
79
|
+
sendPromptLiteral(sessionName: string, paneIndex: number, prompt: string): void;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* @deprecated Use `sendPromptLiteral` instead.
|
|
83
|
+
* paste-buffer triggers bracket paste mode (TD-133).
|
|
84
|
+
*/
|
|
85
|
+
sendPrompt(sessionName: string, paneIndex: number, prompt: string): void;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Attaches to the session.
|
|
89
|
+
* - If inside an existing tmux session (TMUX env var set): splits a window horizontally.
|
|
90
|
+
* - Otherwise: prints the attach command to console.log.
|
|
91
|
+
* Never throws.
|
|
92
|
+
*/
|
|
93
|
+
autoAttach(sessionName: string): void;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// ---------------------------------------------------------------------------
|
|
97
|
+
// Dependencies
|
|
98
|
+
// ---------------------------------------------------------------------------
|
|
99
|
+
|
|
100
|
+
export interface TmuxManagerDeps {
|
|
101
|
+
execSync: typeof childProcess.execSync;
|
|
102
|
+
/** Used by sendPrompt (deprecated) to write temp prompt files. Defaults to fs.writeFileSync. */
|
|
103
|
+
writeFileSync?: typeof fs.writeFileSync;
|
|
104
|
+
/** Used by sendPrompt (deprecated) to clean up temp prompt files. Defaults to fs.unlinkSync. */
|
|
105
|
+
unlinkSync?: typeof fs.unlinkSync;
|
|
106
|
+
/** Environment variables used for TMUX detection. Defaults to process.env. */
|
|
107
|
+
env?: Record<string, string | undefined>;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// ---------------------------------------------------------------------------
|
|
111
|
+
// Factory
|
|
112
|
+
// ---------------------------------------------------------------------------
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Creates a TmuxManager instance.
|
|
116
|
+
*
|
|
117
|
+
* @param deps Optional dependency overrides. Defaults to the real `execSync`
|
|
118
|
+
* from Node's `child_process` module so callers do not need to
|
|
119
|
+
* pass anything in production.
|
|
120
|
+
*/
|
|
121
|
+
export function createTmuxManager(deps?: Partial<TmuxManagerDeps>): TmuxManager {
|
|
122
|
+
const exec = deps?.execSync ?? childProcess.execSync;
|
|
123
|
+
const writeFile = deps?.writeFileSync ?? fs.writeFileSync;
|
|
124
|
+
const unlink = deps?.unlinkSync ?? fs.unlinkSync;
|
|
125
|
+
const env: Record<string, string | undefined> = deps?.env ?? process.env;
|
|
126
|
+
|
|
127
|
+
/** Detects the tmux base-index setting (default: 0). */
|
|
128
|
+
function getBaseIndex(): number {
|
|
129
|
+
try {
|
|
130
|
+
const output = exec('tmux show-option -gv base-index', { stdio: 'pipe' });
|
|
131
|
+
const parsed = parseInt(String(output).trim(), 10);
|
|
132
|
+
return isNaN(parsed) ? 0 : parsed;
|
|
133
|
+
} catch {
|
|
134
|
+
return 0;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Detects the tmux pane-base-index setting (default: 0). */
|
|
139
|
+
function getPaneBaseIndex(): number {
|
|
140
|
+
try {
|
|
141
|
+
const output = exec('tmux show-window-option -gv pane-base-index', { stdio: 'pipe' });
|
|
142
|
+
const parsed = parseInt(String(output).trim(), 10);
|
|
143
|
+
return isNaN(parsed) ? 0 : parsed;
|
|
144
|
+
} catch {
|
|
145
|
+
return 0;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Builds the shell command for a given pane config.
|
|
151
|
+
* If `config.command` is set (reactive sidecars), uses it directly.
|
|
152
|
+
* Otherwise builds the claude CLI invocation for interactive mode.
|
|
153
|
+
*/
|
|
154
|
+
function paneCmd(config: PaneLaunchConfig): string {
|
|
155
|
+
if (config.command) {
|
|
156
|
+
return config.command;
|
|
157
|
+
}
|
|
158
|
+
// Build env prefix: strip API key + inject sprint context vars.
|
|
159
|
+
const envParts = ['env -u ANTHROPIC_API_KEY'];
|
|
160
|
+
if (config.sprintId) {
|
|
161
|
+
envParts.push(`SPRINT_ID=${config.sprintId}`);
|
|
162
|
+
}
|
|
163
|
+
if (config.paneRole) {
|
|
164
|
+
envParts.push(`PANE_ROLE=${config.paneRole}`);
|
|
165
|
+
}
|
|
166
|
+
return `${envParts.join(' ')} claude --dangerously-skip-permissions --agent ${config.role}`;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Wraps a command so the pane shell stays alive after the process exits. */
|
|
170
|
+
function keepAlive(cmd: string): string {
|
|
171
|
+
return `bash -c "${cmd}; exec bash"`;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function hasSession(name: string): boolean {
|
|
175
|
+
try {
|
|
176
|
+
exec(`tmux has-session -t "${name}"`, { stdio: 'pipe' });
|
|
177
|
+
return true;
|
|
178
|
+
} catch {
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function createSession(sessionName: string, firstPane: PaneLaunchConfig): void {
|
|
184
|
+
const cmd = keepAlive(paneCmd(firstPane));
|
|
185
|
+
exec(
|
|
186
|
+
`tmux new-session -d -s "${sessionName}" -x 220 -y 50 -c "${firstPane.workdir}" -- ${cmd}`,
|
|
187
|
+
{ stdio: 'pipe' },
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function addPane(sessionName: string, pane: PaneLaunchConfig): void {
|
|
192
|
+
const cmd = keepAlive(paneCmd(pane));
|
|
193
|
+
const winIdx = getBaseIndex();
|
|
194
|
+
exec(
|
|
195
|
+
`tmux split-window -h -t "${sessionName}:${winIdx}" -c "${pane.workdir}" -- ${cmd}`,
|
|
196
|
+
{ stdio: 'pipe' },
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function applyLayout(sessionName: string): void {
|
|
201
|
+
exec(`tmux select-layout -t "${sessionName}" even-horizontal`, { stdio: 'pipe' });
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function killSession(name: string): void {
|
|
205
|
+
try {
|
|
206
|
+
exec(`tmux kill-session -t "${name}"`, { stdio: 'pipe' });
|
|
207
|
+
} catch {
|
|
208
|
+
// Session may not exist -- not an error for callers.
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function launchLayout(sessionName: string, panes: PaneLaunchConfig[]): string {
|
|
213
|
+
if (panes.length === 0) {
|
|
214
|
+
throw new Error('launchLayout requires at least one PaneLaunchConfig');
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
createSession(sessionName, panes[0]);
|
|
218
|
+
|
|
219
|
+
for (let i = 1; i < panes.length; i++) {
|
|
220
|
+
addPane(sessionName, panes[i]);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (panes.length > 1) {
|
|
224
|
+
applyLayout(sessionName);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return sessionName;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Sends a prompt to a pane using `send-keys -l` (literal keystrokes) + Enter.
|
|
232
|
+
* Unlike paste-buffer, literal keys do NOT trigger bracket paste mode,
|
|
233
|
+
* so the final Enter actually submits in Claude Code's TUI (TD-133).
|
|
234
|
+
* Slower than paste for long prompts (~1-2s for typical sprint prompts).
|
|
235
|
+
*/
|
|
236
|
+
function sendPromptLiteral(sessionName: string, paneIndex: number, prompt: string): void {
|
|
237
|
+
const winIdx = getBaseIndex();
|
|
238
|
+
const paneBase = getPaneBaseIndex();
|
|
239
|
+
const tmuxPaneIdx = paneIndex + paneBase;
|
|
240
|
+
const target = `"${sessionName}:${winIdx}.${tmuxPaneIdx}"`;
|
|
241
|
+
// send-keys -l sends characters literally (no key lookup), avoiding bracket paste.
|
|
242
|
+
// Escape single quotes for the shell wrapper.
|
|
243
|
+
const escaped = prompt.replace(/'/g, "'\\''");
|
|
244
|
+
exec(`tmux send-keys -l -t ${target} '${escaped}'`, { stdio: 'pipe' });
|
|
245
|
+
exec(`tmux send-keys -t ${target} Enter`, { stdio: 'pipe' });
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* @deprecated Use `sendPromptLiteral` instead.
|
|
250
|
+
* Bracket paste mode prevents auto-submission in Claude Code (TD-133).
|
|
251
|
+
* Kept only as fallback reference.
|
|
252
|
+
*/
|
|
253
|
+
function sendPrompt(sessionName: string, paneIndex: number, prompt: string): void {
|
|
254
|
+
const tmpPath = `/tmp/sprint-prompt-${sessionName}-${paneIndex}.txt`;
|
|
255
|
+
const winIdx = getBaseIndex();
|
|
256
|
+
const paneBase = getPaneBaseIndex();
|
|
257
|
+
const tmuxPaneIdx = paneIndex + paneBase;
|
|
258
|
+
writeFile(tmpPath, prompt);
|
|
259
|
+
try {
|
|
260
|
+
exec(`tmux load-buffer ${tmpPath}`, { stdio: 'pipe' });
|
|
261
|
+
exec(`tmux paste-buffer -t "${sessionName}:${winIdx}.${tmuxPaneIdx}"`, { stdio: 'pipe' });
|
|
262
|
+
exec(`tmux send-keys -t "${sessionName}:${winIdx}.${tmuxPaneIdx}" Enter`, { stdio: 'pipe' });
|
|
263
|
+
} finally {
|
|
264
|
+
unlink(tmpPath);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function autoAttach(sessionName: string): void {
|
|
269
|
+
const attachCmd = `tmux attach -t "${sessionName}"`;
|
|
270
|
+
try {
|
|
271
|
+
if (env['TMUX'] !== undefined) {
|
|
272
|
+
const baseIdx = getBaseIndex();
|
|
273
|
+
exec(`tmux link-window -s "${sessionName}:${baseIdx}"`, { stdio: 'pipe' });
|
|
274
|
+
} else {
|
|
275
|
+
// eslint-disable-next-line no-console
|
|
276
|
+
console.log(`Attach with: ${attachCmd}`);
|
|
277
|
+
}
|
|
278
|
+
} catch {
|
|
279
|
+
// Swallow errors — attach is best-effort.
|
|
280
|
+
// eslint-disable-next-line no-console
|
|
281
|
+
console.log(`Attach with: ${attachCmd}`);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
return {
|
|
286
|
+
hasSession,
|
|
287
|
+
createSession,
|
|
288
|
+
addPane,
|
|
289
|
+
applyLayout,
|
|
290
|
+
killSession,
|
|
291
|
+
launchLayout,
|
|
292
|
+
sendPromptLiteral,
|
|
293
|
+
sendPrompt,
|
|
294
|
+
autoAttach,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WorktreeIsolator.ts — Isolates worktree Claude environments (RFC-025)
|
|
3
|
+
*
|
|
4
|
+
* Responsibilities:
|
|
5
|
+
* 1. generateClaudeMd(role, worktreePath): Reads .claude/agents/<role>.md and
|
|
6
|
+
* writes a role-specific CLAUDE.md to the worktree with a header wrapper.
|
|
7
|
+
* 2. generateSettingsJson(role, worktreePath): Strips orchestrator hooks, overrides
|
|
8
|
+
* agent field, preserves all other settings, writes to worktree .claude/settings.json.
|
|
9
|
+
* 3. isolateWorktree(role, worktreePath): Convenience method calling both above.
|
|
10
|
+
*
|
|
11
|
+
* DI via factory function pattern (same pattern as gate-guardian.ts in ADR-017 Phase 4).
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import * as fs from 'node:fs';
|
|
15
|
+
import * as path from 'node:path';
|
|
16
|
+
|
|
17
|
+
// ─────────────────────────────────────────────────────────────
|
|
18
|
+
// Types
|
|
19
|
+
// ─────────────────────────────────────────────────────────────
|
|
20
|
+
|
|
21
|
+
export interface WorktreeIsolatorDeps {
|
|
22
|
+
readFileSync: typeof fs.readFileSync;
|
|
23
|
+
writeFileSync: typeof fs.writeFileSync;
|
|
24
|
+
mkdirSync: typeof fs.mkdirSync;
|
|
25
|
+
existsSync: typeof fs.existsSync;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface SprintContext {
|
|
29
|
+
sessionName: string;
|
|
30
|
+
workflowId: string;
|
|
31
|
+
mailboxRole: string;
|
|
32
|
+
task?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface WorktreeIsolator {
|
|
36
|
+
generateClaudeMd(role: string, worktreePath: string, sprintContext?: SprintContext): void;
|
|
37
|
+
generateSettingsJson(role: string, worktreePath: string, sprintContext?: SprintContext): void;
|
|
38
|
+
isolateWorktree(role: string, worktreePath: string, sprintContext?: SprintContext): void;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Shape of .claude/settings.json (only fields we care about)
|
|
42
|
+
interface ClaudeSettings {
|
|
43
|
+
env?: Record<string, string>;
|
|
44
|
+
permissions?: unknown;
|
|
45
|
+
hooks?: unknown;
|
|
46
|
+
enableAllProjectMcpServers?: boolean;
|
|
47
|
+
enabledPlugins?: unknown;
|
|
48
|
+
alwaysThinkingEnabled?: boolean;
|
|
49
|
+
agent?: string;
|
|
50
|
+
[key: string]: unknown;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// ─────────────────────────────────────────────────────────────
|
|
54
|
+
// Factory
|
|
55
|
+
// ─────────────────────────────────────────────────────────────
|
|
56
|
+
|
|
57
|
+
const defaultDeps: WorktreeIsolatorDeps = {
|
|
58
|
+
readFileSync: fs.readFileSync,
|
|
59
|
+
writeFileSync: fs.writeFileSync,
|
|
60
|
+
mkdirSync: fs.mkdirSync,
|
|
61
|
+
existsSync: fs.existsSync,
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export function createWorktreeIsolator(
|
|
65
|
+
projectRoot: string,
|
|
66
|
+
deps: Partial<WorktreeIsolatorDeps> = {},
|
|
67
|
+
): WorktreeIsolator {
|
|
68
|
+
const { readFileSync, writeFileSync, mkdirSync, existsSync } = {
|
|
69
|
+
...defaultDeps,
|
|
70
|
+
...deps,
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
function generateClaudeMd(role: string, worktreePath: string, sprintContext?: SprintContext): void {
|
|
74
|
+
const capitalised = role.split('-').map(w => w.charAt(0).toUpperCase() + w.slice(1)).join('-');
|
|
75
|
+
const lines: string[] = [`# ${capitalised} Agent`];
|
|
76
|
+
|
|
77
|
+
if (sprintContext) {
|
|
78
|
+
const taskHint = sprintContext.task
|
|
79
|
+
? `Task hint: ${sprintContext.task} (full task via mailbox)`
|
|
80
|
+
: 'Check mailbox for task assignments';
|
|
81
|
+
lines.push(
|
|
82
|
+
`Sprint: ${sprintContext.sessionName} | Mailbox: ${sprintContext.mailboxRole}`,
|
|
83
|
+
taskHint,
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const output = lines.join('\n') + '\n';
|
|
88
|
+
writeFileSync(path.join(worktreePath, 'CLAUDE.md'), output, 'utf-8');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function generateSettingsJson(
|
|
92
|
+
role: string,
|
|
93
|
+
worktreePath: string,
|
|
94
|
+
sprintContext?: SprintContext,
|
|
95
|
+
): void {
|
|
96
|
+
const settingsPath = path.join(projectRoot, '.claude', 'settings.json');
|
|
97
|
+
const raw = readFileSync(settingsPath, 'utf-8') as string;
|
|
98
|
+
const original: ClaudeSettings = JSON.parse(raw);
|
|
99
|
+
|
|
100
|
+
// Build hooks: inject sprint-specific hooks when sprint context is available
|
|
101
|
+
const hooks: Record<string, unknown> = sprintContext
|
|
102
|
+
? {
|
|
103
|
+
SessionStart: [
|
|
104
|
+
{
|
|
105
|
+
matcher: '',
|
|
106
|
+
hooks: [
|
|
107
|
+
{ type: 'command', command: 'npx tsx .claude/hooks/sprint-registry.ts' },
|
|
108
|
+
{
|
|
109
|
+
type: 'command',
|
|
110
|
+
command: 'npx tsx .claude/hooks/mailbox-listener.ts',
|
|
111
|
+
timeout: 3000,
|
|
112
|
+
},
|
|
113
|
+
],
|
|
114
|
+
},
|
|
115
|
+
],
|
|
116
|
+
PostToolUse: [
|
|
117
|
+
{
|
|
118
|
+
matcher: '',
|
|
119
|
+
hooks: [
|
|
120
|
+
{
|
|
121
|
+
type: 'command',
|
|
122
|
+
command: 'npx tsx .claude/hooks/mailbox-listener.ts',
|
|
123
|
+
timeout: 3000,
|
|
124
|
+
},
|
|
125
|
+
],
|
|
126
|
+
},
|
|
127
|
+
],
|
|
128
|
+
}
|
|
129
|
+
: {};
|
|
130
|
+
|
|
131
|
+
// Merge sprint env vars into existing env when sprint context is present
|
|
132
|
+
const env = sprintContext
|
|
133
|
+
? {
|
|
134
|
+
...(original.env ?? {}),
|
|
135
|
+
SPRINT_ID: sprintContext.sessionName,
|
|
136
|
+
PANE_ROLE: sprintContext.mailboxRole,
|
|
137
|
+
}
|
|
138
|
+
: original.env;
|
|
139
|
+
|
|
140
|
+
const isolated: ClaudeSettings = {
|
|
141
|
+
...original,
|
|
142
|
+
env,
|
|
143
|
+
hooks,
|
|
144
|
+
agent: role,
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const claudeDir = path.join(worktreePath, '.claude');
|
|
148
|
+
if (!existsSync(claudeDir)) {
|
|
149
|
+
mkdirSync(claudeDir, { recursive: true });
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
writeFileSync(
|
|
153
|
+
path.join(claudeDir, 'settings.json'),
|
|
154
|
+
JSON.stringify(isolated, null, 2),
|
|
155
|
+
'utf-8',
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function isolateWorktree(role: string, worktreePath: string, sprintContext?: SprintContext): void {
|
|
160
|
+
generateClaudeMd(role, worktreePath, sprintContext);
|
|
161
|
+
generateSettingsJson(role, worktreePath, sprintContext);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return { generateClaudeMd, generateSettingsJson, isolateWorktree };
|
|
165
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import * as childProcess from 'child_process';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
|
|
4
|
+
export interface WorktreeManagerDeps {
|
|
5
|
+
execSync: typeof childProcess.execSync;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
interface WorktreeEntry {
|
|
9
|
+
worktreePath: string;
|
|
10
|
+
branchName: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface WorktreeManager {
|
|
14
|
+
prune(): void;
|
|
15
|
+
getCurrentBranch(): string;
|
|
16
|
+
addWorktree(index: number, sessionName: string): string;
|
|
17
|
+
removeWorktree(worktreePath: string): void;
|
|
18
|
+
removeAll(sessionName: string): void;
|
|
19
|
+
getCreatedWorktrees(): string[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function createWorktreeManager(
|
|
23
|
+
projectRoot: string,
|
|
24
|
+
deps?: Partial<WorktreeManagerDeps>,
|
|
25
|
+
): WorktreeManager {
|
|
26
|
+
const exec = deps?.execSync ?? childProcess.execSync;
|
|
27
|
+
const opts = { encoding: 'utf-8' as const };
|
|
28
|
+
const tracked: WorktreeEntry[] = [];
|
|
29
|
+
|
|
30
|
+
function prune(): void {
|
|
31
|
+
exec(`git -C "${projectRoot}" worktree prune`, opts);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function getCurrentBranch(): string {
|
|
35
|
+
const result = exec(`git -C "${projectRoot}" rev-parse --abbrev-ref HEAD`, opts);
|
|
36
|
+
return (result as string).trim();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function addWorktree(index: number, sessionName: string): string {
|
|
40
|
+
const worktreePath = path.resolve(projectRoot, '..', `wt-${sessionName}-${index}`);
|
|
41
|
+
const branchName = `${sessionName}-impl-${index}`;
|
|
42
|
+
|
|
43
|
+
exec(`git -C "${projectRoot}" worktree add -b "${branchName}" "${worktreePath}" HEAD`, opts);
|
|
44
|
+
tracked.push({ worktreePath, branchName });
|
|
45
|
+
|
|
46
|
+
return worktreePath;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function removeWorktree(worktreePath: string): void {
|
|
50
|
+
try {
|
|
51
|
+
exec(`git -C "${projectRoot}" worktree remove --force "${worktreePath}"`, opts);
|
|
52
|
+
} catch {
|
|
53
|
+
// Worktree may not exist — not an error
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const entryIndex = tracked.findIndex((e) => e.worktreePath === worktreePath);
|
|
57
|
+
if (entryIndex !== -1) {
|
|
58
|
+
const { branchName } = tracked[entryIndex];
|
|
59
|
+
tracked.splice(entryIndex, 1);
|
|
60
|
+
try {
|
|
61
|
+
exec(`git -C "${projectRoot}" branch -d "${branchName}"`, opts);
|
|
62
|
+
} catch {
|
|
63
|
+
// Branch delete failure is non-fatal (e.g. unmerged changes) — warn but do not crash
|
|
64
|
+
console.warn(
|
|
65
|
+
`[WorktreeManager] Could not delete branch "${branchName}" — it may not have been fully merged.`,
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function removeAll(_sessionName: string): void {
|
|
72
|
+
// Snapshot so iteration is stable even if tracked mutates
|
|
73
|
+
const entries = [...tracked];
|
|
74
|
+
|
|
75
|
+
for (const { worktreePath } of entries) {
|
|
76
|
+
try {
|
|
77
|
+
exec(`git -C "${projectRoot}" worktree remove --force "${worktreePath}"`, opts);
|
|
78
|
+
} catch {
|
|
79
|
+
// Continue removing remaining worktrees on partial failure
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
for (const { branchName } of entries) {
|
|
84
|
+
try {
|
|
85
|
+
exec(`git -C "${projectRoot}" branch -D "${branchName}"`, opts);
|
|
86
|
+
} catch {
|
|
87
|
+
// Branch may already be deleted — not an error
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
tracked.splice(0);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function getCreatedWorktrees(): string[] {
|
|
95
|
+
return tracked.map((e) => e.worktreePath);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
prune,
|
|
100
|
+
getCurrentBranch,
|
|
101
|
+
addWorktree,
|
|
102
|
+
removeWorktree,
|
|
103
|
+
removeAll,
|
|
104
|
+
getCreatedWorktrees,
|
|
105
|
+
};
|
|
106
|
+
}
|