MCPcopy Create free account
hub / github.com/Noumena-Network/code / flushHistory

Function flushHistory

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

Source from the content-addressed store, hash-verified

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

Callers 1

wireTransportCallbacksFunction · 0.85

Calls 4

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

Tested by

no test coverage detected