diff --git a/README.md b/README.md index 0195c4d4ad..0e79171f55 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,8 @@ Optional diagnostics: - `T3CODE_LIVESTORE_BOOTSTRAP_PARITY_CHECK=1` - In `livestore-read-pilot` mode, compares LiveStore vs legacy `state.bootstrap` snapshots and logs drift diagnostics. +- `T3CODE_LIVESTORE_CATCHUP_PARITY_CHECK=1` + - In `livestore-read-pilot` mode, compares LiveStore vs legacy `state.catchUp` responses and logs drift diagnostics. ## Provider architecture diff --git a/apps/server/src/index.ts b/apps/server/src/index.ts index 8abae986d9..7be789c49c 100644 --- a/apps/server/src/index.ts +++ b/apps/server/src/index.ts @@ -135,6 +135,8 @@ async function main() { const syncEngineMode = resolveSyncEngineMode(process.env.T3CODE_SYNC_ENGINE_MODE); const enableLiveStoreBootstrapParityCheck = parseBooleanEnv(process.env.T3CODE_LIVESTORE_BOOTSTRAP_PARITY_CHECK) ?? false; + const enableLiveStoreCatchUpParityCheck = + parseBooleanEnv(process.env.T3CODE_LIVESTORE_CATCHUP_PARITY_CHECK) ?? false; const persistenceService = new PersistenceService({ dbPath: stateDbPath, legacyProjectsJsonPath: path.join(legacyStateDir, "projects.json"), @@ -154,6 +156,7 @@ async function main() { delegate: legacyStateSyncEngine, mirror: liveStoreMirror, enableBootstrapParityCheck: enableLiveStoreBootstrapParityCheck, + enableCatchUpParityCheck: enableLiveStoreCatchUpParityCheck, }) : legacyStateSyncEngine; const devUrl = process.env.VITE_DEV_SERVER_URL; @@ -188,6 +191,7 @@ async function main() { legacyStateDir, syncEngineMode, liveStoreBootstrapParityCheck: enableLiveStoreBootstrapParityCheck, + liveStoreCatchUpParityCheck: enableLiveStoreCatchUpParityCheck, authEnabled: Boolean(authToken), }); diff --git a/apps/server/src/stateSyncEngineLiveStoreReadPilot.test.ts b/apps/server/src/stateSyncEngineLiveStoreReadPilot.test.ts index 54daef002f..4a4a50ae41 100644 --- a/apps/server/src/stateSyncEngineLiveStoreReadPilot.test.ts +++ b/apps/server/src/stateSyncEngineLiveStoreReadPilot.test.ts @@ -306,6 +306,48 @@ describe("LiveStoreReadPilotStateSyncEngine", () => { } }); + it("runs catch-up parity check against delegate when enabled", () => { + const delegate = new MockDelegateStateSyncEngine(); + delegate.catchUpMock.mockReturnValue({ + events: [], + lastStateSeq: 11, + }); + const mirror = makeMirrorStub({ + snapshot: { + projects: [], + threads: [], + lastStateSeq: 5, + }, + catchUp: { + events: [], + lastStateSeq: 7, + }, + listMessages: { + messages: [], + total: 0, + nextOffset: null, + }, + }); + + const engine = new LiveStoreReadPilotStateSyncEngine({ + delegate, + mirror, + enableCatchUpParityCheck: true, + }); + + try { + expect(engine.catchUp({ afterSeq: 3 })).toEqual({ + events: [], + lastStateSeq: 7, + }); + // One call from parity-check comparison path while mirror remains source. + expect(delegate.catchUpMock).toHaveBeenCalledTimes(1); + expect(delegate.catchUpMock).toHaveBeenCalledWith({ afterSeq: 3 }); + } 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 94a8164379..3878945e06 100644 --- a/apps/server/src/stateSyncEngineLiveStoreReadPilot.ts +++ b/apps/server/src/stateSyncEngineLiveStoreReadPilot.ts @@ -38,6 +38,7 @@ export interface LiveStoreReadPilotStateSyncEngineOptions { delegate: StateSyncEngine; mirror: LiveStoreStateMirror; enableBootstrapParityCheck?: boolean; + enableCatchUpParityCheck?: boolean; } export class LiveStoreReadPilotStateSyncEngine @@ -49,17 +50,20 @@ export class LiveStoreReadPilotStateSyncEngine private readonly logger = createLogger("livestore-read-pilot"); private readonly unsubscribeDelegate: () => void; private readonly enableBootstrapParityCheck: boolean; + private readonly enableCatchUpParityCheck: boolean; private closed = false; private bootstrapSource: LiveStoreReadSource = "delegate"; private catchUpSource: LiveStoreReadSource = "delegate"; private listMessagesSource: LiveStoreReadSource = "delegate"; private bootstrapParityState: "unknown" | "in-parity" | "drift" = "unknown"; + private catchUpParityState: "unknown" | "in-parity" | "drift" = "unknown"; constructor(options: LiveStoreReadPilotStateSyncEngineOptions) { super(); this.delegate = options.delegate; this.mirror = options.mirror; this.enableBootstrapParityCheck = options.enableBootstrapParityCheck ?? false; + this.enableCatchUpParityCheck = options.enableCatchUpParityCheck ?? false; this.unsubscribeDelegate = this.delegate.onStateEvent((event) => { this.emit("stateEvent", event); void this.mirror.mirrorStateEvent(event).catch((error) => { @@ -157,6 +161,9 @@ export class LiveStoreReadPilotStateSyncEngine const afterSeq = raw.afterSeq ?? 0; try { const result = this.mirror.debugCatchUp(afterSeq); + if (this.enableCatchUpParityCheck) { + this.checkCatchUpParity(raw, result); + } this.logReadSourceChange("state.catchUp", this.catchUpSource, "livestore", { afterSeq, }); @@ -176,6 +183,37 @@ export class LiveStoreReadPilotStateSyncEngine } } + private checkCatchUpParity(raw: StateCatchUpInput, mirrorResult: StateCatchUpResult): void { + let delegateResult: StateCatchUpResult; + try { + delegateResult = this.delegate.catchUp(raw); + } catch (error) { + this.logger.warn("catch-up parity check failed to read delegate catch-up", { error }); + return; + } + + const diffs = diffCatchUpResults(delegateResult, mirrorResult); + if (diffs.length === 0) { + if (this.catchUpParityState !== "in-parity") { + this.catchUpParityState = "in-parity"; + this.logger.info("livestore read pilot catch-up parity check passed", { + afterSeq: raw.afterSeq ?? 0, + lastStateSeq: mirrorResult.lastStateSeq, + }); + } + return; + } + + this.catchUpParityState = "drift"; + this.logger.warn("livestore read pilot catch-up parity drift detected", { + afterSeq: raw.afterSeq ?? 0, + diffCount: diffs.length, + sampleDiffs: diffs.slice(0, 5), + delegateLastStateSeq: delegateResult.lastStateSeq, + mirrorLastStateSeq: mirrorResult.lastStateSeq, + }); + } + private logReadSourceChange( route: "state.bootstrap" | "state.catchUp" | "state.listMessages", previous: LiveStoreReadSource, @@ -258,3 +296,46 @@ export class LiveStoreReadPilotStateSyncEngine void this.mirror.dispose(); } } + +function diffCatchUpResults(expected: StateCatchUpResult, actual: StateCatchUpResult): string[] { + const diffs: string[] = []; + + if (expected.lastStateSeq !== actual.lastStateSeq) { + diffs.push( + `lastStateSeq mismatch: expected=${expected.lastStateSeq} actual=${actual.lastStateSeq}`, + ); + } + + if (expected.events.length !== actual.events.length) { + diffs.push(`events.length mismatch: expected=${expected.events.length} actual=${actual.events.length}`); + } + + const minLength = Math.min(expected.events.length, actual.events.length); + for (let index = 0; index < minLength; index += 1) { + const expectedEvent = expected.events[index]; + const actualEvent = actual.events[index]; + if (!expectedEvent || !actualEvent) { + continue; + } + if (expectedEvent.seq !== actualEvent.seq) { + diffs.push(`events[${index}].seq mismatch: expected=${expectedEvent.seq} actual=${actualEvent.seq}`); + } + if (expectedEvent.eventType !== actualEvent.eventType) { + diffs.push( + `events[${index}].eventType mismatch: expected=${expectedEvent.eventType} actual=${actualEvent.eventType}`, + ); + } + if (expectedEvent.entityId !== actualEvent.entityId) { + diffs.push( + `events[${index}].entityId mismatch: expected=${expectedEvent.entityId} actual=${actualEvent.entityId}`, + ); + } + const expectedPayload = JSON.stringify(expectedEvent.payload); + const actualPayload = JSON.stringify(actualEvent.payload); + if (expectedPayload !== actualPayload) { + diffs.push(`events[${index}].payload mismatch`); + } + } + + return diffs; +}