/** File path conventions for spawn result coordination. Both the spawn-claude CLI and the hook handler use these paths to coordinate parent-child session relationships and result forwarding. Directory layout under `~/.claude/spawn-results/`: - `.by-pid/{claude_pid}`: maps Claude process PID to session identity - `spawns/{spawnId}.json`: spawn state for each child session - `spawns/{spawnId}.reported`: renamed after result injection @module */ import { join, } from 'node:path'; /** Environment lookup object accepted by path helpers. */ type Environment = Readonly; /** Resolves root directory for all spawn coordination files. @param env - environment values to inspect @returns spawn coordination root directory @example ```ts spawnResultsDir({ HOME: '/home/me' }); ``` */ function spawnResultsDir(env: Environment = process.env,): string { return join( env.HOME ?? '/tmp', '.claude', 'spawn-results', ); } /** Resolves directory for PID-to-session-identity mapping files. @param env - environment values to inspect @returns PID mapping directory path @example ```ts byPidDir({ HOME: '/home/me' }); ``` */ function byPidDir(env: Environment = process.env,): string { return join( spawnResultsDir(env,), '.by-pid', ); } /** Resolves directory for spawn state files. @param env - environment values to inspect @returns spawn state directory path @example ```ts spawnsDir({ HOME: '/home/me' }); ``` */ function spawnsDir(env: Environment = process.env,): string { return join( spawnResultsDir(env,), 'spawns', ); } /** Root directory for all spawn coordination files. */ const SPAWN_RESULTS_DIR: string = spawnResultsDir(); /** Directory for PID-to-session-identity mapping files. Written by SessionStart hook, read by CLI tool. */ const BY_PID_DIR: string = byPidDir(); /** Directory for spawn state files. Created by SessionStart hook, updated by Stop and SessionEnd hooks, read and renamed by the inject check. */ const SPAWNS_DIR: string = spawnsDir(); /** JSON structure for PID-to-session mapping files in `.by-pid/`. */ type PidMapping = { /** Claude Code session identifier from hook event data. */ readonly sessionId: string; /** Absolute path to the session transcript JSONL file. */ readonly transcriptPath: string; }; /** JSON structure for spawn state files in `spawns/`. */ type SpawnState = { /** UUID generated by the CLI tool at spawn time. */ readonly spawnId: string; /** Child session's Claude Code session identifier. */ readonly sessionId: string; /** Absolute path to the child's transcript JSONL file. */ readonly transcriptPath: string; /** Parent session's Claude Code session identifier. */ readonly parentSessionId: string; /** Current lifecycle state of the spawned session. */ readonly status: 'running' | 'stopped'; /** Text of the child's most recent assistant response. */ readonly lastMessage: string; }; export { byPidDir, BY_PID_DIR, spawnResultsDir, SPAWN_RESULTS_DIR, spawnsDir, SPAWNS_DIR, }; export type { Environment, PidMapping, SpawnState, };