MCPcopy
hub / github.com/claude-code-best/claude-code / tryReconnectInPlace

Function tryReconnectInPlace

src/bridge/replBridge.ts:396–434  ·  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

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