Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182#!/usr/bin/env node
/** CLI tool that spawns steerable child Claude Code sessions in terminal windows. Launches a child Claude session via `terminal-exec` with a pre-created spawn state file. Only `CLAUDE_SPAWN_ID` is passed as an env var; the child's SessionStart and Stop hooks use it to fill in session info and report completion. @module */
import { spawn, } from 'node:child_process';import { randomUUID, } from 'node:crypto';import { mkdir, writeFile,} from 'node:fs/promises';import { join, } from 'node:path';
// TODO: deprecate Optiqueimport { object, } from '@optique/core/constructs';// TODO: deprecate Optiqueimport { message, } from '@optique/core/message';// TODO: deprecate Optiqueimport { optional, } from '@optique/core/modifiers';// TODO: deprecate Optiqueimport { argument, option,} from '@optique/core/primitives';// TODO: deprecate Optiqueimport { string, } from '@optique/core/valueparser';// TODO: deprecate Optiqueimport { runSync, } from '@optique/run';
import { SPAWNS_DIR, type SpawnState,} from '../handler/claude-spawn/paths.ts';import { findCallingSession, SESSION_NOT_FOUND,} from '../handler/claude-spawn/session-finder.ts';import { splitWhitespace, } from '@monochromatic-dev/agent-harness-shared-text-scan/ts';
/** TODO: deprecate Optique Optique parser for the spawn-claude CLI. @example ``` spawn-claude "implement feature X" spawn-claude --cwd /some/path "fix the bug" spawn-claude --extra-arguments "--model sonnet" "refactor module Y" ``` */const parser = object({ cwd: optional(option( '--cwd', string({ metavar: 'DIR', },), { description: message`Working directory for the child session. Defaults to current directory.`, }, ),), extraArguments: optional(option( '--extra-arguments', string({ metavar: 'ARGS', },), { description: message`Additional CLI arguments passed directly to the claude command (e.g. "--model sonnet --allowedTools Edit,Bash").`, }, ),), prompt: argument( string({ metavar: 'PROMPT', },), { description: message`Initial prompt for the spawned Claude session.`, }, ),},);
/** TODO: deprecate Optique Parsed CLI arguments from the spawn-claude command invocation. */const args = runSync( parser, { programName: 'spawn-claude', help: 'option', brief: message`Spawn a steerable child Claude Code session in a visible terminal instance.`, },);
/** Resolved session identity of the calling Claude instance. */const identity = await findCallingSession();
if (identity === SESSION_NOT_FOUND) { console.error( 'Error: Could not find calling Claude session. Ensure the claude-spawn plugin hooks are active and SessionStart has fired.', ); process.exitCode = 1;}else { /** Unique identifier for this spawn, used to coordinate state between parent and child. */ const spawnId = randomUUID(); /** Working directory for the child session, defaulting to the current directory. */ const cwd = args.cwd ?? process .cwd();
/** Extra args split on whitespace, filtering empty strings. */ const extraArgs = splitWhitespace(args.extraArguments ?? '',);
await mkdir( SPAWNS_DIR, { recursive: true, }, );
/** Pre-create the spawn state file before launching the child. `sessionId` starts empty; the first SessionStart that sees it empty claims ownership. Subsequent sessions with stale `CLAUDE_SPAWN_ID` env vars see a non-empty `sessionId` and skip registration. */ const initialState: SpawnState = { spawnId, sessionId: '', transcriptPath: '', parentSessionId: identity.sessionId, status: 'running', lastMessage: '', };
await writeFile( join( SPAWNS_DIR, `${spawnId}.json`, ), JSON.stringify(initialState,), );
/** Detached child process running the spawned Claude session in a terminal instance. */ const proc = spawn( 'terminal-exec', [ '--', 'claude', ...extraArgs, args.prompt, ], { cwd, env: { ...process.env, CLAUDE_SPAWN_ID: spawnId, }, detached: true, stdio: 'ignore', }, );
proc.unref();
console.log(JSON.stringify({ spawnId, },),);}