diff --git a/README.md b/README.md index 68dfad7b51..658b90c384 100644 --- a/README.md +++ b/README.md @@ -123,6 +123,8 @@ Optional diagnostics: - In `livestore-read-pilot` mode, compares LiveStore vs legacy `state.listMessages` responses and logs drift diagnostics. - `T3CODE_LIVESTORE_SHADOW_BOOTSTRAP_PARITY_CHECK=1` - In `shadow` mode, compares mirror vs delegate `state.bootstrap` snapshots and logs drift diagnostics. +- `T3CODE_LIVESTORE_DISABLE_READ_FALLBACK=1` + - In `livestore-read-pilot` mode, disables delegate read fallback and fails requests when mirror reads fail (strict canary mode). Web client state-source selection: diff --git a/SYNC_ENGINE_MIGRATION.md b/SYNC_ENGINE_MIGRATION.md index 008c4f8e3d..3b60a13f4e 100644 --- a/SYNC_ENGINE_MIGRATION.md +++ b/SYNC_ENGINE_MIGRATION.md @@ -20,6 +20,8 @@ Server mode is controlled by `T3CODE_SYNC_ENGINE_MODE`: - In read-pilot mode, compares `state.listMessages` mirror output against delegate output. - `T3CODE_LIVESTORE_SHADOW_BOOTSTRAP_PARITY_CHECK=1` - In shadow mode, compares `state.bootstrap` mirror output against delegate output. +- `T3CODE_LIVESTORE_DISABLE_READ_FALLBACK=1` + - In read-pilot mode, disables delegate read fallback for strict mirror-read canary validation. - `VITE_T3CODE_STATE_SOURCE_MODE=legacy-api|livestore-read-pilot` - Client-side state-source mode seam. - If unset, web derives mode from `server.getConfig().syncEngineMode`. diff --git a/apps/server/src/index.ts b/apps/server/src/index.ts index ce1e8225a7..3fd3e9eff2 100644 --- a/apps/server/src/index.ts +++ b/apps/server/src/index.ts @@ -124,6 +124,8 @@ async function main() { parseBooleanEnv(process.env.T3CODE_LIVESTORE_LIST_MESSAGES_PARITY_CHECK) ?? false; const enableLiveStoreShadowBootstrapParityCheck = parseBooleanEnv(process.env.T3CODE_LIVESTORE_SHADOW_BOOTSTRAP_PARITY_CHECK) ?? false; + const disableLiveStoreDelegateReadFallback = + parseBooleanEnv(process.env.T3CODE_LIVESTORE_DISABLE_READ_FALLBACK) ?? false; const persistenceService = new PersistenceService({ dbPath: stateDbPath, legacyProjectsJsonPath: path.join(legacyStateDir, "projects.json"), @@ -146,6 +148,7 @@ async function main() { enableBootstrapParityCheck: enableLiveStoreBootstrapParityCheck, enableCatchUpParityCheck: enableLiveStoreCatchUpParityCheck, enableListMessagesParityCheck: enableLiveStoreListMessagesParityCheck, + disableDelegateReadFallback: disableLiveStoreDelegateReadFallback, }) : legacyStateSyncEngine; const devUrl = process.env.VITE_DEV_SERVER_URL; @@ -184,6 +187,7 @@ async function main() { liveStoreCatchUpParityCheck: enableLiveStoreCatchUpParityCheck, liveStoreListMessagesParityCheck: enableLiveStoreListMessagesParityCheck, liveStoreShadowBootstrapParityCheck: enableLiveStoreShadowBootstrapParityCheck, + liveStoreDisableReadFallback: disableLiveStoreDelegateReadFallback, authEnabled: Boolean(authToken), }); diff --git a/apps/server/src/stateSyncEngineLiveStoreReadPilot.test.ts b/apps/server/src/stateSyncEngineLiveStoreReadPilot.test.ts index 3c6d46f1fa..94ae6e8f12 100644 --- a/apps/server/src/stateSyncEngineLiveStoreReadPilot.test.ts +++ b/apps/server/src/stateSyncEngineLiveStoreReadPilot.test.ts @@ -396,6 +396,91 @@ describe("LiveStoreReadPilotStateSyncEngine", () => { } }); + it("can disable delegate read fallback for strict mirror reads", () => { + const delegate = new MockDelegateStateSyncEngine(); + const mirror = makeMirrorStub({ + snapshot: { + projects: [], + threads: [], + lastStateSeq: 0, + }, + catchUp: { + events: [], + lastStateSeq: 0, + }, + listMessages: { + messages: [], + total: 0, + nextOffset: null, + }, + }); + vi.mocked(mirror.debugReadSnapshot).mockImplementationOnce(() => { + throw new Error("strict bootstrap failure"); + }); + vi.mocked(mirror.debugCatchUp).mockImplementationOnce(() => { + throw new Error("strict catch-up failure"); + }); + vi.mocked(mirror.debugListMessages).mockImplementationOnce(() => { + throw new Error("strict list failure"); + }); + + const engine = new LiveStoreReadPilotStateSyncEngine({ + delegate, + mirror, + disableDelegateReadFallback: true, + }); + + try { + expect(() => engine.loadSnapshot()).toThrow(/strict bootstrap failure/); + expect(() => engine.catchUp({ afterSeq: 0 })).toThrow(/strict catch-up failure/); + expect(() => engine.listMessages({ threadId: "thread-1", offset: 0, limit: 10 })).toThrow( + /strict list failure/, + ); + expect(delegate.loadSnapshotMock).not.toHaveBeenCalled(); + expect(delegate.catchUpMock).not.toHaveBeenCalled(); + expect(delegate.listMessagesMock).not.toHaveBeenCalled(); + } finally { + engine.close(); + } + }); + + it("uses mirror bootstrap even when seq is zero if fallback disabled", () => { + const delegate = new MockDelegateStateSyncEngine(); + const mirror = makeMirrorStub({ + snapshot: { + projects: [], + threads: [], + lastStateSeq: 0, + }, + catchUp: { + events: [], + lastStateSeq: 0, + }, + listMessages: { + messages: [], + total: 0, + nextOffset: null, + }, + }); + + const engine = new LiveStoreReadPilotStateSyncEngine({ + delegate, + mirror, + disableDelegateReadFallback: true, + }); + + try { + expect(engine.loadSnapshot()).toEqual({ + projects: [], + threads: [], + lastStateSeq: 0, + }); + expect(delegate.loadSnapshotMock).not.toHaveBeenCalled(); + } finally { + engine.close(); + } + }); + it("returns to mirror reads after transient fallback errors", () => { const delegate = new MockDelegateStateSyncEngine(); const mirror = makeMirrorStub({ diff --git a/apps/server/src/stateSyncEngineLiveStoreReadPilot.ts b/apps/server/src/stateSyncEngineLiveStoreReadPilot.ts index eca285d11d..3e825c35cd 100644 --- a/apps/server/src/stateSyncEngineLiveStoreReadPilot.ts +++ b/apps/server/src/stateSyncEngineLiveStoreReadPilot.ts @@ -40,6 +40,7 @@ export interface LiveStoreReadPilotStateSyncEngineOptions { enableBootstrapParityCheck?: boolean; enableCatchUpParityCheck?: boolean; enableListMessagesParityCheck?: boolean; + disableDelegateReadFallback?: boolean; } export class LiveStoreReadPilotStateSyncEngine @@ -53,6 +54,7 @@ export class LiveStoreReadPilotStateSyncEngine private readonly enableBootstrapParityCheck: boolean; private readonly enableCatchUpParityCheck: boolean; private readonly enableListMessagesParityCheck: boolean; + private readonly disableDelegateReadFallback: boolean; private closed = false; private bootstrapSource: LiveStoreReadSource = "delegate"; private catchUpSource: LiveStoreReadSource = "delegate"; @@ -68,6 +70,7 @@ export class LiveStoreReadPilotStateSyncEngine this.enableBootstrapParityCheck = options.enableBootstrapParityCheck ?? false; this.enableCatchUpParityCheck = options.enableCatchUpParityCheck ?? false; this.enableListMessagesParityCheck = options.enableListMessagesParityCheck ?? false; + this.disableDelegateReadFallback = options.disableDelegateReadFallback ?? false; this.unsubscribeDelegate = this.delegate.onStateEvent((event) => { this.emit("stateEvent", event); void this.mirror.mirrorStateEvent(event).catch((error) => { @@ -89,17 +92,29 @@ export class LiveStoreReadPilotStateSyncEngine loadSnapshot(): StateBootstrapResult { try { const snapshot = this.mirror.debugReadSnapshot(); - if (snapshot.lastStateSeq > 0) { - if (this.enableBootstrapParityCheck) { - this.checkBootstrapParity(snapshot); - } - this.logReadSourceChange("state.bootstrap", this.bootstrapSource, "livestore", { - lastStateSeq: snapshot.lastStateSeq, - }); - this.bootstrapSource = "livestore"; - return snapshot; + if (this.enableBootstrapParityCheck) { + this.checkBootstrapParity(snapshot); } + if (snapshot.lastStateSeq === 0 && !this.disableDelegateReadFallback) { + const delegateSnapshot = this.delegate.loadSnapshot(); + this.logReadSourceChange("state.bootstrap", this.bootstrapSource, "delegate", { + lastStateSeq: delegateSnapshot.lastStateSeq, + }); + this.bootstrapSource = "delegate"; + return delegateSnapshot; + } + this.logReadSourceChange("state.bootstrap", this.bootstrapSource, "livestore", { + lastStateSeq: snapshot.lastStateSeq, + }); + this.bootstrapSource = "livestore"; + return snapshot; } catch (error) { + if (this.disableDelegateReadFallback) { + this.logger.error("failed to read bootstrap from livestore mirror with fallback disabled", { + error, + }); + throw error; + } this.logger.warn("failed to read bootstrap from livestore mirror; using delegate", { error }); } const snapshot = this.delegate.loadSnapshot(); @@ -151,6 +166,16 @@ export class LiveStoreReadPilotStateSyncEngine this.listMessagesSource = "livestore"; return result; } catch (error) { + if (this.disableDelegateReadFallback) { + this.logger.error( + "failed to list messages from livestore mirror with fallback disabled", + { + error, + threadId: raw.threadId, + }, + ); + throw error; + } this.logger.warn("failed to list messages from livestore mirror; using delegate", { error, threadId: raw.threadId, @@ -212,6 +237,13 @@ export class LiveStoreReadPilotStateSyncEngine this.catchUpSource = "livestore"; return result; } catch (error) { + if (this.disableDelegateReadFallback) { + this.logger.error("failed to catch up from livestore mirror with fallback disabled", { + error, + afterSeq, + }); + throw error; + } this.logger.warn("failed to catch up from livestore mirror; using delegate", { error, afterSeq,