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