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

Function tryReconnectInPlace

src/bridge/replBridge.ts:390–428  ·  view source on GitHub ↗

* Reconnect-in-place: if the just-registered environmentId matches what * was requested, call reconnectSession to force-stop stale workers and * re-queue the session. Used at init (perpetual mode — env is alive but * idle after clean teardown) and in doReconnect() Strategy 1 (env lost *

(
    requestedEnvId: string,
    sessionId: string,
  )

Source from the content-addressed store, hash-verified

388 * fresh session creation on false.
389 */
390 async function tryReconnectInPlace(
391 requestedEnvId: string,
392 sessionId: string,
393 ): Promise<boolean> {
394 if (environmentId !== requestedEnvId) {
395 logForDebugging(
396 `[bridge:repl] Env mismatch (requested ${requestedEnvId}, got ${environmentId}) — cannot reconnect in place`,
397 )
398 return false
399 }
400 // The pointer stores what createBridgeSession returned (session_*,
401 // compat/convert.go:41). /bridge/reconnect is an environments-layer
402 // endpoint — once the server's ccr_v2_compat_enabled gate is on it
403 // looks sessions up by their infra tag (cse_*) and returns "Session
404 // not found" for the session_* costume. We don't know the gate state
405 // pre-poll, so try both; the re-tag is a no-op if the ID is already
406 // cse_* (doReconnect Strategy 1 path — currentSessionId never mutates
407 // to cse_* but future-proof the check).
408 const infraId = toInfraSessionId(sessionId)
409 const candidates =
410 infraId === sessionId ? [sessionId] : [sessionId, infraId]
411 for (const id of candidates) {
412 try {
413 await api.reconnectSession(environmentId, id)
414 logForDebugging(
415 `[bridge:repl] Reconnected session ${id} in place on env ${environmentId}`,
416 )
417 return true
418 } catch (err) {
419 logForDebugging(
420 `[bridge:repl] reconnectSession(${id}) failed: ${errorMessage(err)}`,
421 )
422 }
423 }
424 logForDebugging(
425 '[bridge:repl] reconnectSession exhausted — falling through to fresh session',
426 )
427 return false
428 }
429
430 // Perpetual init: env is alive but has no queued work after clean
431 // teardown. reconnectSession re-queues it. doReconnect() has the same

Callers 2

initBridgeCoreFunction · 0.85
doReconnectFunction · 0.85

Calls 3

toInfraSessionIdFunction · 0.85
logForDebuggingFunction · 0.50
errorMessageFunction · 0.50

Tested by

no test coverage detected