@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.
Files changed (37) hide show
  1. package/dist/index.d.ts +1 -1
  2. package/dist/index.js +1 -1
  3. package/dist/templates/base/claude/agents/debug-sidecar.md +133 -0
  4. package/dist/templates/base/claude/agents/doc-sidecar.md +60 -0
  5. package/dist/templates/base/claude/agents/implementer.md +162 -31
  6. package/dist/templates/base/claude/agents/test-sidecar.md +106 -0
  7. package/dist/templates/base/claude/hooks/mailbox-listener.ts +246 -0
  8. package/dist/templates/base/claude/hooks/sprint-registry.ts +85 -0
  9. package/dist/templates/base/claude/settings.json +19 -0
  10. package/dist/templates/base/claude/skills/sprint-launch/SKILL.md +176 -0
  11. package/dist/templates/base/claude/skills/sprint-teammate/sprint-teammate.md +79 -0
  12. package/dist/templates/base/scripts/lib/SprintLauncher.ts +325 -0
  13. package/dist/templates/base/scripts/lib/TmuxManager.ts +296 -0
  14. package/dist/templates/base/scripts/lib/WorktreeIsolator.ts +165 -0
  15. package/dist/templates/base/scripts/lib/WorktreeManager.ts +106 -0
  16. package/dist/templates/base/scripts/lib/mailbox/types.ts +175 -0
  17. package/dist/templates/base/scripts/lib/sidecar/SidecarWatcher.ts +249 -0
  18. package/dist/templates/base/scripts/lib/sidecar/run.ts +90 -0
  19. package/dist/templates/base/scripts/sprint-launch.ts +285 -0
  20. package/package.json +1 -1
  21. package/templates/base/claude/agents/debug-sidecar.md +133 -0
  22. package/templates/base/claude/agents/doc-sidecar.md +60 -0
  23. package/templates/base/claude/agents/implementer.md +162 -31
  24. package/templates/base/claude/agents/test-sidecar.md +106 -0
  25. package/templates/base/claude/hooks/mailbox-listener.ts +246 -0
  26. package/templates/base/claude/hooks/sprint-registry.ts +85 -0
  27. package/templates/base/claude/settings.json +19 -0
  28. package/templates/base/claude/skills/sprint-launch/SKILL.md +176 -0
  29. package/templates/base/claude/skills/sprint-teammate/sprint-teammate.md +79 -0
  30. package/templates/base/scripts/lib/SprintLauncher.ts +325 -0
  31. package/templates/base/scripts/lib/TmuxManager.ts +296 -0
  32. package/templates/base/scripts/lib/WorktreeIsolator.ts +165 -0
  33. package/templates/base/scripts/lib/WorktreeManager.ts +106 -0
  34. package/templates/base/scripts/lib/mailbox/types.ts +175 -0
  35. package/templates/base/scripts/lib/sidecar/SidecarWatcher.ts +249 -0
  36. package/templates/base/scripts/lib/sidecar/run.ts +90 -0
  37. package/templates/base/scripts/sprint-launch.ts +285 -0
@@ -0,0 +1,325 @@
1
+ /**
2
+ * SprintLauncher.ts — Orchestrates the full sprint session launch (RFC-025)
3
+ *
4
+ * Responsibilities:
5
+ * 1. Validate prerequisites (tmux on PATH, claude on PATH, no duplicate session)
6
+ * 2. Prune stale worktrees
7
+ * 3. Create worktrees via WorktreeManager (one per pane)
8
+ * 4. Isolate each worktree via WorktreeIsolator
9
+ * 5. Launch tmux session via TmuxManager
10
+ * 6. Return LaunchResult
11
+ *
12
+ * DI via factory function — same pattern as gate-guardian.ts (ADR-017 Phase 4).
13
+ * execSync is injected for testability; in production use child_process.execSync.
14
+ * Input values (preset, workflowId) are validated before use in shell commands.
15
+ */
16
+
17
+ import type { WorktreeManager } from './WorktreeManager.js';
18
+ import type { WorktreeIsolator } from './WorktreeIsolator.js';
19
+ import type { TmuxManager, PaneLaunchConfig } from './TmuxManager.js';
20
+ import { SprintPreset } from '../../src/domain/value-objects/SprintLayout.js';
21
+ import type { PaneConfig } from '../../src/domain/value-objects/SprintLayout.js';
22
+ import type { generateLayout as GenerateLayoutFn } from '../../src/domain/value-objects/generateLayout.js';
23
+
24
+ // ---------------------------------------------------------------------------
25
+ // Constants
26
+ // ---------------------------------------------------------------------------
27
+
28
+ /**
29
+ * Milliseconds to wait after launchLayout before sending activation prompts.
30
+ * Claude Code needs ~5-8 seconds to boot and display the ❯ prompt.
31
+ */
32
+ export const BOOT_DELAY_MS = 7000;
33
+
34
+ // ---------------------------------------------------------------------------
35
+ // Public types
36
+ // ---------------------------------------------------------------------------
37
+
38
+ import type { MailboxAddress, MessageEnvelope } from './mailbox/types.js';
39
+
40
+ /** Minimal MailboxManager interface required by SprintLauncher. */
41
+ export interface MailboxManager {
42
+ /** Create mailbox inbox directories for each role in the sprint. */
43
+ init(addresses: readonly MailboxAddress[]): void;
44
+ /**
45
+ * Send a message envelope to the address encoded in envelope.to.
46
+ * Matches MailboxManager.sendMessage(envelope) from MailboxManager.ts.
47
+ */
48
+ sendMessage(envelope: MessageEnvelope): void;
49
+ }
50
+
51
+ export interface SprintTask {
52
+ taskId: string;
53
+ title: string;
54
+ description: string;
55
+ acceptanceCriteria: string[];
56
+ }
57
+
58
+ export interface LaunchResult {
59
+ sessionName: string;
60
+ workflowId: string;
61
+ preset: string;
62
+ worktreePaths: readonly string[];
63
+ }
64
+
65
+ export interface SprintLauncherDeps {
66
+ worktreeManager: WorktreeManager;
67
+ worktreeIsolator: WorktreeIsolator;
68
+ tmuxManager: TmuxManager;
69
+ generateLayout: typeof GenerateLayoutFn;
70
+ execSync: (cmd: string, opts?: object) => string | Buffer;
71
+ projectRoot: string;
72
+ /** Optional mailbox manager — initialises inbox directories before launch. */
73
+ mailboxManager?: MailboxManager;
74
+ /**
75
+ * Optional delay function — defaults to real setTimeout-based delay.
76
+ * Inject a no-op `() => Promise.resolve()` in tests to avoid 7s waits.
77
+ */
78
+ delay?: (ms: number) => Promise<void>;
79
+ }
80
+
81
+ export interface SprintLauncher {
82
+ launch(presetName: string, workflowId: string, tasks?: SprintTask[]): Promise<LaunchResult>;
83
+ cleanup(sessionName: string): void;
84
+ }
85
+
86
+ // ---------------------------------------------------------------------------
87
+ // Factory
88
+ // ---------------------------------------------------------------------------
89
+
90
+ /**
91
+ * Returns true when the agent role is a reactive sidecar (RFC-025 v3.3).
92
+ * Sidecars launch as Node.js watcher processes instead of interactive claude sessions.
93
+ * Implementers, reviewers, and other roles remain interactive.
94
+ */
95
+ function isSidecarRole(role: string): boolean {
96
+ return role.includes('sidecar');
97
+ }
98
+
99
+ /**
100
+ * Builds the shell command for a reactive sidecar pane.
101
+ * Launches the SidecarWatcher script which watches the mailbox inbox
102
+ * and spawns Agent SDK query() calls on demand.
103
+ *
104
+ * The inbox path matches MailboxManager's layout: `/tmp/{sessionName}/mailbox/{role}-{index}/inbox`
105
+ *
106
+ * NOTE: The script runs from `projectRoot` (not the worktree) so that node_modules
107
+ * are available for chokidar and SDK imports. The worktree is passed as --worktree
108
+ * and used as cwd for the SDK query() call.
109
+ */
110
+ function buildSidecarCommand(agentSlug: string, worktreePath: string, sessionName: string, mailboxRole: string, paneIndex: number, projectRoot: string): string {
111
+ const inboxPath = `/tmp/${sessionName}/mailbox/${mailboxRole}-${paneIndex}/inbox`;
112
+ return `cd "${projectRoot}" && npx tsx scripts/lib/sidecar/run.ts --inbox "${inboxPath}" --worktree "${worktreePath}" --agent ${agentSlug}`;
113
+ }
114
+
115
+ export function createSprintLauncher(deps: SprintLauncherDeps): SprintLauncher {
116
+ const { worktreeManager, worktreeIsolator, tmuxManager, generateLayout, execSync, mailboxManager } = deps;
117
+ const delay = deps.delay ?? ((ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms)));
118
+
119
+ function validatePrerequisites(sessionName: string): void {
120
+ // Validate tmux is on PATH (static command — no user input)
121
+ try {
122
+ execSync('which tmux', { stdio: 'pipe' });
123
+ } catch {
124
+ throw new Error(
125
+ 'tmux is not on PATH. Install tmux to use sprint-launch.',
126
+ );
127
+ }
128
+
129
+ // Validate claude is on PATH (static command — no user input)
130
+ try {
131
+ execSync('which claude', { stdio: 'pipe' });
132
+ } catch {
133
+ throw new Error(
134
+ 'claude is not on PATH. Install Claude Code CLI to use sprint-launch.',
135
+ );
136
+ }
137
+
138
+ // Guard against duplicate session via TmuxManager (no shell exec here)
139
+ if (tmuxManager.hasSession(sessionName)) {
140
+ throw new Error(
141
+ `tmux session '${sessionName}' already exists. Attach with: tmux attach -t ${sessionName}`,
142
+ );
143
+ }
144
+ }
145
+
146
+ async function launch(presetName: string, workflowId: string, tasks?: SprintTask[]): Promise<LaunchResult> {
147
+ // 1. Validate preset (validates input before any use in commands)
148
+ const presetResult = SprintPreset.create(presetName);
149
+ if (presetResult.isErr()) {
150
+ throw new Error(`Invalid preset: ${presetResult.error}`);
151
+ }
152
+ const preset = presetResult.value;
153
+
154
+ // 2. Derive session name
155
+ const sessionName = `sprint-${workflowId.slice(0, 8)}`;
156
+
157
+ // 3. Validate prerequisites
158
+ validatePrerequisites(sessionName);
159
+
160
+ // 4. Generate layout
161
+ const layoutResult = generateLayout(preset, workflowId);
162
+ if (layoutResult.isErr()) {
163
+ throw new Error(`Failed to generate layout: ${layoutResult.error}`);
164
+ }
165
+ const layout = layoutResult.value;
166
+
167
+ // 5. Initialise mailbox inbox directories (before worktree prune)
168
+ if (mailboxManager) {
169
+ const addresses: MailboxAddress[] = layout.panes.map((p: PaneConfig, i: number) => ({
170
+ role: p.context.mailboxRole as MailboxAddress['role'],
171
+ index: i,
172
+ }));
173
+ mailboxManager.init(addresses);
174
+ }
175
+
176
+ // 6. Prune stale worktrees
177
+ worktreeManager.prune();
178
+
179
+ // 7. Create worktrees (one per pane, indexed)
180
+ const worktreePaths: string[] = [];
181
+ for (let i = 0; i < layout.panes.length; i++) {
182
+ const worktreePath = worktreeManager.addWorktree(i, sessionName);
183
+ worktreePaths.push(worktreePath);
184
+ }
185
+
186
+ // 8. Build round-robin task assignment map (implementer panes only).
187
+ // PaneConfig is frozen — use a separate Map to track assignments.
188
+ const paneTaskMap = new Map<number, SprintTask>();
189
+ if (tasks && tasks.length > 0) {
190
+ const implementerIndices = layout.panes
191
+ .map((pane: PaneConfig, i: number) => ({ pane, i }))
192
+ .filter(({ pane }) => pane.agent.includes('implementer'))
193
+ .map(({ i }) => i);
194
+
195
+ for (let t = 0; t < tasks.length; t++) {
196
+ const paneIndex = implementerIndices[t % implementerIndices.length];
197
+ if (paneIndex !== undefined) {
198
+ paneTaskMap.set(paneIndex, tasks[t]);
199
+ }
200
+ }
201
+ }
202
+
203
+ // 9. Isolate each worktree — inject sprint context into CLAUDE.md
204
+ // so agents know their workflow, mailbox, and task at startup.
205
+ // Zero tmux input needed (TD-133 resolved via CLAUDE.md injection).
206
+ for (let i = 0; i < layout.panes.length; i++) {
207
+ const pane = layout.panes[i];
208
+ const assignedTask = paneTaskMap.get(i);
209
+ worktreeIsolator.isolateWorktree(pane.agent, worktreePaths[i], {
210
+ sessionName,
211
+ workflowId,
212
+ mailboxRole: `${pane.context.mailboxRole}-${i}`,
213
+ task: assignedTask?.title ?? pane.context.task,
214
+ });
215
+ }
216
+
217
+ // 10. Send task assignment messages to agents via mailbox (before launch)
218
+ if (mailboxManager) {
219
+ for (let i = 0; i < layout.panes.length; i++) {
220
+ const pane = layout.panes[i];
221
+ const assignedTask = paneTaskMap.get(i);
222
+
223
+ // Use assigned SprintTask if available, otherwise fall back to pane.context.task
224
+ if (assignedTask) {
225
+ const address: MailboxAddress = {
226
+ role: pane.context.mailboxRole as MailboxAddress['role'],
227
+ index: i,
228
+ };
229
+ const envelope: MessageEnvelope = {
230
+ id: crypto.randomUUID(),
231
+ from: { role: 'orchestrator', index: 0 },
232
+ to: address,
233
+ timestamp: new Date().toISOString(),
234
+ body: {
235
+ type: 'task_assignment',
236
+ taskId: assignedTask.taskId,
237
+ title: assignedTask.title,
238
+ description: assignedTask.description,
239
+ acceptanceCriteria: assignedTask.acceptanceCriteria,
240
+ },
241
+ };
242
+ mailboxManager.sendMessage(envelope);
243
+ } else if (pane.context.task) {
244
+ const address: MailboxAddress = {
245
+ role: pane.context.mailboxRole as MailboxAddress['role'],
246
+ index: i,
247
+ };
248
+ const envelope: MessageEnvelope = {
249
+ id: crypto.randomUUID(),
250
+ from: { role: 'orchestrator', index: 0 },
251
+ to: address,
252
+ timestamp: new Date().toISOString(),
253
+ body: {
254
+ type: 'task_assignment',
255
+ taskId: `task-${i}`,
256
+ title: pane.context.task,
257
+ description: '',
258
+ acceptanceCriteria: [],
259
+ },
260
+ };
261
+ mailboxManager.sendMessage(envelope);
262
+ }
263
+ }
264
+ }
265
+
266
+ // 11. Build PaneLaunchConfig[] for TmuxManager (no initialPrompt needed)
267
+ // Sidecar panes get a custom `command` that launches the reactive watcher
268
+ // instead of an interactive `claude --agent` session (RFC-025 v3.3).
269
+ const paneLaunchConfigs: PaneLaunchConfig[] = layout.panes.map((pane: PaneConfig, i: number) => {
270
+ const config: PaneLaunchConfig = {
271
+ role: pane.agent,
272
+ workdir: worktreePaths[i],
273
+ sprintId: sessionName,
274
+ paneRole: `${pane.context.mailboxRole}-${i}`,
275
+ };
276
+ if (isSidecarRole(pane.agent)) {
277
+ config.command = buildSidecarCommand(
278
+ pane.agent,
279
+ worktreePaths[i],
280
+ sessionName,
281
+ pane.context.mailboxRole as string,
282
+ i,
283
+ deps.projectRoot,
284
+ );
285
+ }
286
+ return config;
287
+ });
288
+
289
+ // 12. Launch tmux session (interactive mode — TUI visible, context from CLAUDE.md)
290
+ tmuxManager.launchLayout(sessionName, paneLaunchConfigs);
291
+
292
+ // 12a. Wait for Claude to boot then activate implementer panes.
293
+ // Sidecars stay idle — they respond reactively in a future version.
294
+ const implementerIndicesForActivation = paneLaunchConfigs
295
+ .map((pane, i) => ({ pane, i }))
296
+ .filter(({ pane }) => pane.role.includes('implementer'))
297
+ .map(({ i }) => i);
298
+
299
+ if (implementerIndicesForActivation.length > 0) {
300
+ // eslint-disable-next-line no-console
301
+ console.log('[sprint-launch] Activating implementer panes...');
302
+ await delay(BOOT_DELAY_MS);
303
+ for (const paneIndex of implementerIndicesForActivation) {
304
+ tmuxManager.sendPromptLiteral(sessionName, paneIndex, 'siga');
305
+ }
306
+ }
307
+
308
+ // 13. Auto-attach for seamless UX
309
+ tmuxManager.autoAttach(sessionName);
310
+
311
+ return {
312
+ sessionName,
313
+ workflowId,
314
+ preset: preset.value,
315
+ worktreePaths: Object.freeze(worktreePaths),
316
+ };
317
+ }
318
+
319
+ function cleanup(sessionName: string): void {
320
+ worktreeManager.removeAll(sessionName);
321
+ tmuxManager.killSession(sessionName);
322
+ }
323
+
324
+ return { launch, cleanup };
325
+ }
@@ -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
+ }