(msgs: Message[])
| 622 | } |
| 623 | |
| 624 | async function flushHistory(msgs: Message[]): Promise<void> { |
| 625 | // v2 always creates a fresh server session (unconditional createCodeSession |
| 626 | // above) — no session reuse, no double-post risk. Unlike v1, we do NOT |
| 627 | // filter by previouslyFlushedUUIDs: that set persists across REPL enable/ |
| 628 | // disable cycles (useRef), so it would wrongly suppress history on re-enable. |
| 629 | const eligible = msgs.filter(isEligibleBridgeMessage) |
| 630 | const capped = |
| 631 | initialHistoryCap > 0 && eligible.length > initialHistoryCap |
| 632 | ? eligible.slice(-initialHistoryCap) |
| 633 | : eligible |
| 634 | if (capped.length < eligible.length) { |
| 635 | logForDebugging( |
| 636 | `[remote-bridge] Capped initial flush: ${eligible.length} -> ${capped.length} (cap=${initialHistoryCap})`, |
| 637 | ) |
| 638 | } |
| 639 | const events = toSDKMessages(capped).map(m => ({ |
| 640 | ...m, |
| 641 | session_id: sessionId, |
| 642 | })) |
| 643 | if (events.length === 0) return |
| 644 | // Mid-turn init: if Remote Control is enabled while a query is running, |
| 645 | // the last eligible message is a user prompt or tool_result (both 'user' |
| 646 | // type). Without this the init PUT's 'idle' sticks until the next user- |
| 647 | // type message forwards via writeMessages — which for a pure-text turn |
| 648 | // is never (only assistant chunks stream post-init). Check eligible (pre- |
| 649 | // cap), not capped: the cap may truncate to a user message even when the |
| 650 | // actual trailing message is assistant. |
| 651 | if (eligible.at(-1)?.type === 'user') { |
| 652 | transport.reportState('running') |
| 653 | } |
| 654 | logForDebugging(`[remote-bridge] Flushing ${events.length} history events`) |
| 655 | await transport.writeBatch(events) |
| 656 | } |
| 657 | |
| 658 | // ── 9. Teardown ─────────────────────────────────────────────────────────── |
| 659 | // On SIGINT/SIGTERM//exit, gracefulShutdown races runCleanupFunctions() |
no test coverage detected