fix(web): retry local-secondary reconcile during WSL cold boot

WSL cold boot routinely lands at the 30-60s mark, well past the two
quick reconcile passes the renderer was doing at app start. Once
those exhausted, the WSL secondary stayed missing from the saved-env
registry and the user lost access to threads stored against that
backend until they toggled the WSL setting off and on again.

Add an internal backoff loop in reconcileLocalSecondaryEnvironments:
when a pass finishes with any secondary still missing a record, it
schedules another pass at 2s, 4s, 8s, 16s, 30s, 45s, 60s, 60s. User-
driven calls (toggling the setting, or the boot path) reset the
budget; internal retries advance it. The whole budget fits inside
the desktop-bootstrap TTL with headroom.

Also reuse a persisted bearer when one exists. The session token we
stored from the first bootstrap stays valid for 30 days, so on page
reload we can skip the bootstrap exchange entirely if the backend
still recognizes the bearer. Falls back to a fresh bootstrap if the
bearer is missing or rejected, which keeps the existing "first
connect" path working unchanged.

Guard readBootstraps / reconcileOnce against test environments that
exercise the runtime-service entry point without a browser-shaped
global (window is undefined) or with a partially-mocked registry
store (byId undefined). This prevents unhandled rejections in the
saved-env and thread-subscription test suites without changing
observable test behavior.

Drop the now-redundant 5s retry timeout out of
startEnvironmentConnectionService - the reconciler owns its retry
loop end-to-end.
This commit is contained in:
Josh Gratton committed 2026-05-17 22:16:10 -04:00
1 parent 2c1620db00
commit 24b012dc08
2 files changed
+135 -34

No files matched your search

+130 -23
View File
@@ -29,17 +29,23 @@
import {
PRIMARY_LOCAL_ENVIRONMENT_ID,
type AuthSessionRole,
type DesktopEnvironmentBootstrap,
type EnvironmentId,
} from "@t3tools/contracts";
import { bootstrapRemoteBearerSession, fetchRemoteEnvironmentDescriptor } from "../remote/api";
import {
bootstrapRemoteBearerSession,
fetchRemoteEnvironmentDescriptor,
fetchRemoteSessionState,
} from "../remote/api";
import {
ensureSavedEnvironmentConnection,
removeSavedEnvironmentByInstance,
} from "../runtime/service";
import {
getSavedEnvironmentRecord,
readSavedEnvironmentBearerToken,
useSavedEnvironmentRegistryStore,
writeSavedEnvironmentBearerToken,
type SavedEnvironmentRecord,
@@ -52,12 +58,25 @@ interface PendingRegistration {
const pendingByInstanceId = new Map<string, PendingRegistration>();
let pendingReconcileRun: Promise<void> | null = null;
// Backoff schedule for the auto-retry loop. WSL cold boot routinely
// takes 30-60 seconds (distro spin-up + node-pty preflight + node
// startup + migrations), and the backend's desktop-bootstrap grant
// has a 5-minute TTL after seeding. This schedule comfortably covers
// the cold-boot window while leaving headroom inside the TTL.
const AUTO_RETRY_DELAYS_MS = [2_000, 4_000, 8_000, 16_000, 30_000, 45_000, 60_000, 60_000] as const;
let autoRetryHandle: ReturnType<typeof setTimeout> | null = null;
let autoRetryAttempt = 0;
function readBootstraps(): readonly DesktopEnvironmentBootstrap[] {
// Guard against test environments that import this module under
// Node (no window) but exercise the service entrypoint that boots
// the reconciler.
if (typeof window === "undefined") return [];
return window.desktopBridge?.getLocalEnvironmentBootstraps() ?? [];
}
function findRecordByInstanceId(instanceId: string): SavedEnvironmentRecord | null {
const byId = useSavedEnvironmentRegistryStore.getState().byId;
const byId = useSavedEnvironmentRegistryStore.getState().byId ?? {};
for (const record of Object.values(byId)) {
if (record.desktopLocal?.instanceId === instanceId) {
return record;
@@ -80,6 +99,32 @@ function isRegisteredForBootstrap(
);
}
async function tryReuseStoredBearer(input: {
readonly environmentId: EnvironmentId;
readonly httpBaseUrl: string;
}): Promise<{ readonly bearerToken: string; readonly role: AuthSessionRole } | null> {
// The bearer session token we got from the first bootstrap is
// persisted in the desktop secret store keyed by environmentId, and
// it stays valid for 30 days. Check the backend's view of the bearer
// before re-bootstrapping: if it's still good we skip the bootstrap
// exchange entirely (the bootstrap path is also safe to repeat now
// that the desktop-bootstrap grant is reusable, but reusing the
// existing bearer keeps the auth log cleaner and avoids spending a
// round-trip on every page reload).
const stored = await readSavedEnvironmentBearerToken(input.environmentId);
if (!stored) return null;
try {
const session = await fetchRemoteSessionState({
httpBaseUrl: input.httpBaseUrl,
bearerToken: stored,
});
if (!session.authenticated || !session.role) return null;
return { bearerToken: stored, role: session.role };
} catch {
return null;
}
}
async function registerSecondaryLocalEnvironment(
bootstrap: DesktopEnvironmentBootstrap,
): Promise<SavedEnvironmentRecord | null> {
@@ -109,10 +154,26 @@ async function registerSecondaryLocalEnvironment(
await removeSavedEnvironmentByInstance(stale.environmentId);
}
const bearerSession = await bootstrapRemoteBearerSession({
let bearerToken: string;
let role: AuthSessionRole;
const reused = await tryReuseStoredBearer({
environmentId,
httpBaseUrl: bootstrap.httpBaseUrl,
credential,
});
if (reused) {
bearerToken = reused.bearerToken;
role = reused.role;
} else {
const bearerSession = await bootstrapRemoteBearerSession({
httpBaseUrl: bootstrap.httpBaseUrl,
credential,
});
bearerToken = bearerSession.sessionToken;
role = bearerSession.role;
// Only the fresh-bootstrap path needs to write the token: the
// reuse path already had it in the secret store.
await writeSavedEnvironmentBearerToken(environmentId, bearerToken);
}
const existing = getSavedEnvironmentRecord(environmentId);
const record: SavedEnvironmentRecord = {
@@ -125,18 +186,17 @@ async function registerSecondaryLocalEnvironment(
desktopLocal: { instanceId: bootstrap.id },
};
// Order is load-bearing: write the bearer to the secret store before
// upserting the record. The zustand subscriber on the registry fires
// a saved-env sync as soon as upsert lands, and that path reads the
// bearer back out of the secret store via readSavedEnvironmentBearerToken.
// If we upserted first, the sync could race ahead, find no bearer,
// and flip the runtime state to "requires-auth" before our explicit
// Order is load-bearing: the bearer must be in the secret store
// before we upsert. The zustand subscriber on the registry fires a
// saved-env sync as soon as upsert lands, and that path reads the
// bearer back out via readSavedEnvironmentBearerToken; without the
// earlier write the sync would race ahead, find no bearer, and flip
// the runtime state to "requires-auth" before the explicit
// ensureSavedEnvironmentConnection call below runs.
await writeSavedEnvironmentBearerToken(environmentId, bearerSession.sessionToken);
useSavedEnvironmentRegistryStore.getState().upsert(record);
await ensureSavedEnvironmentConnection(record, {
bearerToken: bearerSession.sessionToken,
role: bearerSession.role,
bearerToken,
role,
});
return record;
}
@@ -148,8 +208,10 @@ async function reconcileOnce(): Promise<void> {
// Drop registry entries whose backend instance is gone (user toggled
// the WSL backend off, switched distros, or the orchestrator
// unregistered for any other reason).
const registry = useSavedEnvironmentRegistryStore.getState().byId;
// unregistered for any other reason). The `?? {}` keeps this safe in
// test environments that hand back a partially-populated registry
// state.
const registry = useSavedEnvironmentRegistryStore.getState().byId ?? {};
const stale: EnvironmentId[] = [];
for (const record of Object.values(registry)) {
const instanceId = record.desktopLocal?.instanceId;
@@ -190,18 +252,63 @@ async function reconcileOnce(): Promise<void> {
);
}
// Public entry point. Idempotent and never throws — internal failures
// get logged and the caller can retry by calling again. Multiple
// concurrent calls share a single underlying reconcile pass.
export function reconcileLocalSecondaryEnvironments(): Promise<void> {
function hasPendingSecondary(): boolean {
const bootstraps = readBootstraps();
for (const bootstrap of bootstraps) {
if (bootstrap.id === PRIMARY_LOCAL_ENVIRONMENT_ID) continue;
if (!findRecordByInstanceId(bootstrap.id)) return true;
}
return false;
}
function scheduleAutoRetry(): void {
if (autoRetryHandle !== null) return;
if (autoRetryAttempt >= AUTO_RETRY_DELAYS_MS.length) return;
if (!hasPendingSecondary()) return;
const delay = AUTO_RETRY_DELAYS_MS[autoRetryAttempt];
autoRetryAttempt += 1;
autoRetryHandle = setTimeout(() => {
autoRetryHandle = null;
void runReconcile({ resetBudget: false });
}, delay);
}
function runReconcile(options: { readonly resetBudget: boolean }): Promise<void> {
if (pendingReconcileRun) {
return pendingReconcileRun;
}
const next = reconcileOnce().finally(() => {
if (pendingReconcileRun === next) {
pendingReconcileRun = null;
if (options.resetBudget) {
// A user-driven reconcile (or the boot path) resets the backoff
// counter so the auto-retry loop gets a fresh shot. Without this
// reset, toggling WSL off/on after exhausting the budget wouldn't
// resume retries. Internal retries pass resetBudget: false so the
// backoff actually advances each tick.
autoRetryAttempt = 0;
if (autoRetryHandle !== null) {
clearTimeout(autoRetryHandle);
autoRetryHandle = null;
}
});
}
const next = reconcileOnce()
.finally(() => {
if (pendingReconcileRun === next) {
pendingReconcileRun = null;
}
})
.then(() => {
scheduleAutoRetry();
});
pendingReconcileRun = next;
return next;
}
// Public entry point. Idempotent and never throws: internal failures
// get logged and the caller can retry by calling again. Multiple
// concurrent calls share a single underlying reconcile pass. When a
// secondary's registration fails (typical cause: WSL backend still
// cold-booting), an internal backoff loop keeps retrying until either
// the secondary lands in the registry or the desktop-bootstrap TTL
// runs out and we give up.
export function reconcileLocalSecondaryEnvironments(): Promise<void> {
return runReconcile({ resetBudget: true });
}
+5 -11
View File
@@ -1791,17 +1791,12 @@ export function startEnvironmentConnectionService(queryClient: QueryClient): ()
maybeCreatePrimaryEnvironmentConnection();
// Bring up the desktop-managed secondary local environments (today
// just the WSL backend, if enabled). The reconcile call is best-effort
// and self-retrying: if the WSL backend is still mid-boot when this
// fires, the next ConnectionsSettings interaction or a manual
// re-toggle will pick it up via reconcileLocalSecondaryEnvironments.
// just the WSL backend, if enabled). The reconcile call owns its
// own retry loop: WSL cold boot routinely takes 30-60 seconds, and
// the desktop-bootstrap grant on each backend has a 5-minute TTL.
// reconcileLocalSecondaryEnvironments() backs off internally until
// every secondary lands or the TTL window closes.
void reconcileLocalSecondaryEnvironments();
// One delayed retry to catch the common case where the WSL backend
// is mid-cold-boot at app start. Bounded to a single extra attempt
// so we don't poll forever for users who never enable WSL.
const localSecondaryRetryHandle = setTimeout(() => {
void reconcileLocalSecondaryEnvironments();
}, 5_000);
const unsubscribeSavedEnvironments = useSavedEnvironmentRegistryStore.subscribe(() => {
if (!hasSavedEnvironmentRegistryHydrated()) {
@@ -1821,7 +1816,6 @@ export function startEnvironmentConnectionService(queryClient: QueryClient): ()
queryInvalidationThrottler,
refCount: 1,
stop: () => {
clearTimeout(localSecondaryRetryHandle);
unsubscribeSavedEnvironments();
unsubscribeBrowserResumeReconnects();
queryInvalidationThrottler.cancel();