()
| 67 | } |
| 68 | |
| 69 | function spawnClaude() { |
| 70 | const isWin = process.platform === 'win32'; |
| 71 | const shell = isWin ? 'powershell.exe' : (process.env.SHELL || '/bin/bash'); |
| 72 | const claudeCmd = state.CLAUDE_EXTRA_ARGS.length > 0 |
| 73 | ? `claude ${state.CLAUDE_EXTRA_ARGS.join(' ')}` |
| 74 | : 'claude'; |
| 75 | const args = isWin |
| 76 | ? ['-NoLogo', '-NoProfile', '-Command', claudeCmd] |
| 77 | : ['-c', claudeCmd]; |
| 78 | |
| 79 | const cols = isTTY ? process.stdout.columns : 120; |
| 80 | const rows = isTTY ? process.stdout.rows : 40; |
| 81 | |
| 82 | const proc = state.claudeProc = pty.spawn(shell, args, { |
| 83 | name: 'xterm-256color', |
| 84 | cols, |
| 85 | rows, |
| 86 | cwd: state.CWD, |
| 87 | env: { ...process.env, FORCE_COLOR: '1', BRIDGE_PORT: String(state.PORT) }, |
| 88 | }); |
| 89 | |
| 90 | log(`Claude spawned (pid ${state.claudeProc.pid}) — ${cols}x${rows} cmd="${claudeCmd}"`); |
| 91 | setTurnState('idle', { sessionId: state.currentSessionId, reason: 'claude_spawned' }); |
| 92 | broadcast({ |
| 93 | type: 'status', |
| 94 | status: 'running', |
| 95 | pid: proc.pid, |
| 96 | cwd: state.CWD, |
| 97 | sessionId: state.currentSessionId, |
| 98 | lastSeq: latestEventSeq(), |
| 99 | }); |
| 100 | |
| 101 | proc.onData((data) => { |
| 102 | if (isTTY) process.stdout.write(data); |
| 103 | broadcast({ type: 'pty_output', data }); |
| 104 | }); |
| 105 | |
| 106 | attachTtyForwarders(); |
| 107 | |
| 108 | proc.onExit(({ exitCode, signal }) => { |
| 109 | if (state.claudeProc !== proc) { |
| 110 | log(`Ignoring stale Claude exit (pid ${proc.pid}, code=${exitCode}, signal=${signal})`); |
| 111 | return; |
| 112 | } |
| 113 | log(`Claude exited (code=${exitCode}, signal=${signal})`); |
| 114 | setTurnState('idle', { sessionId: state.currentSessionId, reason: 'pty_exit' }); |
| 115 | broadcast({ type: 'pty_exit', exitCode, signal }); |
| 116 | state.claudeProc = null; |
| 117 | |
| 118 | if (isTTY) { |
| 119 | process.stdin.setRawMode(false); |
| 120 | process.stdin.pause(); |
| 121 | } |
| 122 | stopTailing(); |
| 123 | log('Bridge shutting down.'); |
| 124 | setTimeout(() => process.exit(exitCode || 0), 300); |
| 125 | }); |
| 126 | } |
no test coverage detected