(msgs: Message[])
| 649 | } |
| 650 | |
| 651 | async function flushHistory(msgs: Message[]): Promise<void> { |
| 652 | // v2 always creates a fresh server session (unconditional createCodeSession |
| 653 | // above) — no session reuse, no double-post risk. Unlike v1, we do NOT |
| 654 | // filter by previouslyFlushedUUIDs: that set persists across REPL enable/ |
| 655 | // disable cycles (useRef), so it would wrongly suppress history on re-enable. |
| 656 | const eligible = msgs.filter(isEligibleBridgeMessage) |
| 657 | const capped = |
| 658 | initialHistoryCap > 0 && eligible.length > initialHistoryCap |
| 659 | ? eligible.slice(-initialHistoryCap) |
| 660 | : eligible |
| 661 | if (capped.length < eligible.length) { |
| 662 | logForDebugging( |
| 663 | `[remote-bridge] Capped initial flush: ${eligible.length} -> ${capped.length} (cap=${initialHistoryCap})`, |
| 664 | ) |
| 665 | } |
| 666 | const events: TransportMessage[] = toSDKMessages(capped).map(m => ({ |
| 667 | ...m, |
| 668 | session_id: sessionId, |
| 669 | })) as TransportMessage[] |
| 670 | if (events.length === 0) return |
| 671 | // Mid-turn init: if Remote Control is enabled while a query is running, |
| 672 | // the last eligible message may be a real user prompt or tool_result. |
| 673 | // Hidden slash-command scaffolding and pure reminder wrappers should not |
| 674 | // resurrect a completed turn into "running". Check eligible (pre-cap), |
| 675 | // not capped: the cap may truncate to a user message even when the actual |
| 676 | // trailing message is assistant. |
| 677 | const lastEligible = eligible.at(-1) |
| 678 | if (lastEligible && shouldReportRunningForMessage(lastEligible)) { |
| 679 | transport.reportState('running') |
| 680 | } |
| 681 | logForDebugging(`[remote-bridge] Flushing ${events.length} history events`) |
| 682 | await transport.writeBatch(events as StdoutMessage[]) |
| 683 | } |
| 684 | |
| 685 | // ── 9. Teardown ─────────────────────────────────────────────────────────── |
| 686 | // On SIGINT/SIGTERM//exit, gracefulShutdown races runCleanupFunctions() |
no test coverage detected