(
line: string,
)
| 331 | } |
| 332 | |
| 333 | private async processLine( |
| 334 | line: string, |
| 335 | ): Promise<StdinMessage | SDKMessage | undefined> { |
| 336 | // Skip empty lines (e.g. from double newlines in piped stdin) |
| 337 | if (!line) { |
| 338 | return undefined |
| 339 | } |
| 340 | try { |
| 341 | const message = normalizeControlMessageKeys(jsonParse(line)) as |
| 342 | | StdinMessage |
| 343 | | SDKMessage |
| 344 | if (message.type === 'keep_alive') { |
| 345 | // Silently ignore keep-alive messages |
| 346 | return undefined |
| 347 | } |
| 348 | if (message.type === 'update_environment_variables') { |
| 349 | // Apply environment variable updates directly to process.env. |
| 350 | // Used by bridge session runner for auth token refresh |
| 351 | // (CLAUDE_CODE_SESSION_ACCESS_TOKEN) which must be readable |
| 352 | // by the REPL process itself, not just child Bash commands. |
| 353 | const keys = Object.keys(message.variables) |
| 354 | for (const [key, value] of Object.entries(message.variables)) { |
| 355 | process.env[key] = value |
| 356 | } |
| 357 | logForDebugging( |
| 358 | `[structuredIO] applied update_environment_variables: ${keys.join(', ')}`, |
| 359 | ) |
| 360 | return undefined |
| 361 | } |
| 362 | if (message.type === 'control_response') { |
| 363 | // Close lifecycle for every control_response, including duplicates |
| 364 | // and orphans — orphans don't yield to print.ts's main loop, so this |
| 365 | // is the only path that sees them. uuid is server-injected into the |
| 366 | // payload. |
| 367 | const uuid = |
| 368 | 'uuid' in message && typeof message.uuid === 'string' |
| 369 | ? message.uuid |
| 370 | : undefined |
| 371 | if (uuid) { |
| 372 | notifyCommandLifecycle(uuid, 'completed') |
| 373 | } |
| 374 | const request = this.pendingRequests.get(message.response.request_id) |
| 375 | if (!request) { |
| 376 | // Check if this tool_use was already resolved through the normal |
| 377 | // permission flow. Duplicate control_response deliveries (e.g. from |
| 378 | // WebSocket reconnects) arrive after the original was handled, and |
| 379 | // re-processing them would push duplicate assistant messages into |
| 380 | // the conversation, causing API 400 errors. |
| 381 | const responsePayload = |
| 382 | message.response.subtype === 'success' |
| 383 | ? message.response.response |
| 384 | : undefined |
| 385 | const toolUseID = responsePayload?.toolUseID |
| 386 | if ( |
| 387 | typeof toolUseID === 'string' && |
| 388 | this.resolvedToolUseIds.has(toolUseID) |
| 389 | ) { |
| 390 | logForDebugging( |
no test coverage detected