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

Function doReconnect

src/bridge/replBridge.ts:626–845  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

624 }
625
626 async function doReconnect(): Promise<boolean> {
627 environmentRecreations++
628 // Invalidate any in-flight v2 handshake — the environment is being
629 // recreated, so a stale transport arriving post-reconnect would be
630 // pointed at a dead session.
631 v2Generation++
632 logForDebugging(
633 `[bridge:repl] Reconnecting after env lost (attempt ${environmentRecreations}/${MAX_ENVIRONMENT_RECREATIONS})`,
634 )
635
636 if (environmentRecreations > MAX_ENVIRONMENT_RECREATIONS) {
637 logForDebugging(
638 `[bridge:repl] Environment reconnect limit reached (${MAX_ENVIRONMENT_RECREATIONS}), giving up`,
639 )
640 return false
641 }
642
643 // Close the stale transport. Capture seq BEFORE close — if Strategy 1
644 // (tryReconnectInPlace) succeeds we keep the SAME session, and the
645 // next transport must resume where this one left off, not replay from
646 // the last transport-swap checkpoint.
647 if (transport) {
648 const seq = transport.getLastSequenceNum()
649 if (seq > lastTransportSequenceNum) {
650 lastTransportSequenceNum = seq
651 }
652 transport.close()
653 transport = null
654 }
655 // Transport is gone — wake the poll loop out of its at-capacity
656 // heartbeat sleep so it can fast-poll for re-dispatched work.
657 wakePollLoop()
658 // Reset flush gate so writeMessages() hits the !transport guard
659 // instead of silently queuing into a dead buffer.
660 flushGate.drop()
661
662 // Release the current work item (force=false — we may want the session
663 // back). Best-effort: the env is probably gone, so this likely 404s.
664 if (currentWorkId) {
665 const workIdBeingCleared = currentWorkId
666 await api
667 .stopWork(environmentId, workIdBeingCleared, false)
668 .catch(() => {})
669 // When doReconnect runs concurrently with the poll loop (ws_closed
670 // handler case — void-called, unlike the awaited onEnvironmentLost
671 // path), onWorkReceived can fire during the stopWork await and set
672 // a fresh currentWorkId. If it did, the poll loop has already
673 // recovered on its own — defer to it rather than proceeding to
674 // archiveSession, which would destroy the session its new
675 // transport is connected to.
676 if (currentWorkId !== workIdBeingCleared) {
677 logForDebugging(
678 '[bridge:repl] Poll loop recovered during stopWork await — deferring to it',
679 )
680 environmentRecreations = 0
681 return true
682 }
683 currentWorkId = null

Callers 1

Calls 12

tryReconnectInPlaceFunction · 0.85
updateSessionBridgeIdFunction · 0.85
toCompatSessionIdFunction · 0.85
writeBridgePointerFunction · 0.85
getLastSequenceNumMethod · 0.80
dropMethod · 0.80
logEventFunction · 0.70
archiveSessionFunction · 0.70
logForDebuggingFunction · 0.50
errorMessageFunction · 0.50
closeMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected