(deps: SessionSpawnerDeps)
| 308 | } |
| 309 | |
| 310 | export function createSessionSpawner(deps: SessionSpawnerDeps): SessionSpawner { |
| 311 | return { |
| 312 | spawn(opts: SessionSpawnOpts, dir: string): SessionHandle { |
| 313 | // Debug file resolution: |
| 314 | // 1. If deps.debugFile is provided, use it with session ID suffix for uniqueness |
| 315 | // 2. If verbose or ant build, auto-generate a temp file path |
| 316 | // 3. Otherwise, no debug file |
| 317 | const safeId = safeFilenameId(opts.sessionId) |
| 318 | let debugFile: string | undefined |
| 319 | if (deps.debugFile) { |
| 320 | const ext = deps.debugFile.lastIndexOf('.') |
| 321 | if (ext > 0) { |
| 322 | debugFile = `${deps.debugFile.slice(0, ext)}-${safeId}${deps.debugFile.slice(ext)}` |
| 323 | } else { |
| 324 | debugFile = `${deps.debugFile}-${safeId}` |
| 325 | } |
| 326 | } else if (deps.verbose || isInternalBuild()) { |
| 327 | debugFile = join(tmpdir(), 'claude', `bridge-session-${safeId}.log`) |
| 328 | } |
| 329 | |
| 330 | // Transcript file: write raw NDJSON lines for post-hoc analysis. |
| 331 | // Placed alongside the debug file when one is configured. |
| 332 | let transcriptStream: WriteStream | null = null |
| 333 | let transcriptPath: string | undefined |
| 334 | if (deps.debugFile) { |
| 335 | transcriptPath = join( |
| 336 | dirname(deps.debugFile), |
| 337 | `bridge-transcript-${safeId}.jsonl`, |
| 338 | ) |
| 339 | transcriptStream = createWriteStream(transcriptPath, { flags: 'a' }) |
| 340 | transcriptStream.on('error', err => { |
| 341 | deps.onDebug( |
| 342 | `[bridge:session] Transcript write error: ${err.message}`, |
| 343 | ) |
| 344 | transcriptStream = null |
| 345 | }) |
| 346 | deps.onDebug(`[bridge:session] Transcript log: ${transcriptPath}`) |
| 347 | } |
| 348 | |
| 349 | const args = [ |
| 350 | ...deps.scriptArgs, |
| 351 | '--print', |
| 352 | '--sdk-url', |
| 353 | opts.sdkUrl, |
| 354 | '--session-id', |
| 355 | opts.sessionId, |
| 356 | '--input-format', |
| 357 | 'stream-json', |
| 358 | '--output-format', |
| 359 | 'stream-json', |
| 360 | '--replay-user-messages', |
| 361 | ...(deps.verbose ? ['--verbose'] : []), |
| 362 | ...(debugFile ? ['--debug-file', debugFile] : []), |
| 363 | ...(deps.permissionMode |
| 364 | ? ['--permission-mode', deps.permissionMode] |
| 365 | : []), |
| 366 | ] |
| 367 |
no outgoing calls
no test coverage detected