MCPcopy Index your code
hub / github.com/codeaashu/claude-code / flushHistory

Function flushHistory

src/bridge/remoteBridgeCore.ts:624–656  ·  view source on GitHub ↗
(msgs: Message[])

Source from the content-addressed store, hash-verified

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()

Callers 1

wireTransportCallbacksFunction · 0.85

Calls 4

logForDebuggingFunction · 0.85
toSDKMessagesFunction · 0.85
reportStateMethod · 0.80
writeBatchMethod · 0.80

Tested by

no test coverage detected