From 8a6676cbfb6e7f66c8e10130174d22b090208aee Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 20 Feb 2026 11:03:10 +0000 Subject: [PATCH] Add optional listMessages parity diagnostics in read pilot Co-authored-by: Julius Marminge --- README.md | 2 + SYNC_ENGINE_MIGRATION.md | 2 + apps/server/src/index.ts | 4 + .../stateSyncEngineLiveStoreReadPilot.test.ts | 48 +++++++++++ .../src/stateSyncEngineLiveStoreReadPilot.ts | 79 +++++++++++++++++++ 5 files changed, 135 insertions(+) diff --git a/README.md b/README.md index c99d5954cd..35b035cd81 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,8 @@ Optional diagnostics: - 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. +- `T3CODE_LIVESTORE_LIST_MESSAGES_PARITY_CHECK=1` + - In `livestore-read-pilot` mode, compares LiveStore vs legacy `state.listMessages` responses and logs drift diagnostics. Web client state-source selection: diff --git a/SYNC_ENGINE_MIGRATION.md b/SYNC_ENGINE_MIGRATION.md index efd364c377..a58f05a7a2 100644 --- a/SYNC_ENGINE_MIGRATION.md +++ b/SYNC_ENGINE_MIGRATION.md @@ -16,6 +16,8 @@ Server mode is controlled by `T3CODE_SYNC_ENGINE_MODE`: - In read-pilot mode, compares `state.bootstrap` mirror output against delegate output. - `T3CODE_LIVESTORE_CATCHUP_PARITY_CHECK=1` - In read-pilot mode, compares `state.catchUp` mirror output against delegate output. +- `T3CODE_LIVESTORE_LIST_MESSAGES_PARITY_CHECK=1` + - In read-pilot mode, compares `state.listMessages` mirror output against delegate output. - `VITE_T3CODE_STATE_SOURCE_MODE=legacy-api|livestore-read-pilot` - Client-side state-source mode seam. Current implementation is protocol-compatible in both modes. diff --git a/apps/server/src/index.ts b/apps/server/src/index.ts index 3045750ae7..7d6571a773 100644 --- a/apps/server/src/index.ts +++ b/apps/server/src/index.ts @@ -120,6 +120,8 @@ async function main() { parseBooleanEnv(process.env.T3CODE_LIVESTORE_BOOTSTRAP_PARITY_CHECK) ?? false; const enableLiveStoreCatchUpParityCheck = parseBooleanEnv(process.env.T3CODE_LIVESTORE_CATCHUP_PARITY_CHECK) ?? false; + const enableLiveStoreListMessagesParityCheck = + parseBooleanEnv(process.env.T3CODE_LIVESTORE_LIST_MESSAGES_PARITY_CHECK) ?? false; const persistenceService = new PersistenceService({ dbPath: stateDbPath, legacyProjectsJsonPath: path.join(legacyStateDir, "projects.json"), @@ -140,6 +142,7 @@ async function main() { mirror: liveStoreMirror, enableBootstrapParityCheck: enableLiveStoreBootstrapParityCheck, enableCatchUpParityCheck: enableLiveStoreCatchUpParityCheck, + enableListMessagesParityCheck: enableLiveStoreListMessagesParityCheck, }) : legacyStateSyncEngine; const devUrl = process.env.VITE_DEV_SERVER_URL; @@ -175,6 +178,7 @@ async function main() { syncEngineMode, liveStoreBootstrapParityCheck: enableLiveStoreBootstrapParityCheck, liveStoreCatchUpParityCheck: enableLiveStoreCatchUpParityCheck, + liveStoreListMessagesParityCheck: enableLiveStoreListMessagesParityCheck, authEnabled: Boolean(authToken), }); diff --git a/apps/server/src/stateSyncEngineLiveStoreReadPilot.test.ts b/apps/server/src/stateSyncEngineLiveStoreReadPilot.test.ts index 4a4a50ae41..3c6d46f1fa 100644 --- a/apps/server/src/stateSyncEngineLiveStoreReadPilot.test.ts +++ b/apps/server/src/stateSyncEngineLiveStoreReadPilot.test.ts @@ -348,6 +348,54 @@ describe("LiveStoreReadPilotStateSyncEngine", () => { } }); + it("runs list-messages parity check against delegate when enabled", () => { + const delegate = new MockDelegateStateSyncEngine(); + delegate.listMessagesMock.mockReturnValue({ + messages: [], + total: 2, + nextOffset: 2, + }); + const mirror = makeMirrorStub({ + snapshot: { + projects: [], + threads: [], + lastStateSeq: 5, + }, + catchUp: { + events: [], + lastStateSeq: 5, + }, + listMessages: { + messages: [], + total: 1, + nextOffset: null, + }, + }); + + const engine = new LiveStoreReadPilotStateSyncEngine({ + delegate, + mirror, + enableListMessagesParityCheck: true, + }); + + try { + expect(engine.listMessages({ threadId: "thread-1", offset: 0, limit: 10 })).toEqual({ + messages: [], + total: 1, + nextOffset: null, + }); + // One call from parity-check comparison path while mirror remains source. + expect(delegate.listMessagesMock).toHaveBeenCalledTimes(1); + expect(delegate.listMessagesMock).toHaveBeenCalledWith({ + threadId: "thread-1", + offset: 0, + limit: 10, + }); + } 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 3878945e06..eca285d11d 100644 --- a/apps/server/src/stateSyncEngineLiveStoreReadPilot.ts +++ b/apps/server/src/stateSyncEngineLiveStoreReadPilot.ts @@ -39,6 +39,7 @@ export interface LiveStoreReadPilotStateSyncEngineOptions { mirror: LiveStoreStateMirror; enableBootstrapParityCheck?: boolean; enableCatchUpParityCheck?: boolean; + enableListMessagesParityCheck?: boolean; } export class LiveStoreReadPilotStateSyncEngine @@ -51,12 +52,14 @@ export class LiveStoreReadPilotStateSyncEngine private readonly unsubscribeDelegate: () => void; private readonly enableBootstrapParityCheck: boolean; private readonly enableCatchUpParityCheck: boolean; + private readonly enableListMessagesParityCheck: 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"; + private listMessagesParityState: "unknown" | "in-parity" | "drift" = "unknown"; constructor(options: LiveStoreReadPilotStateSyncEngineOptions) { super(); @@ -64,6 +67,7 @@ export class LiveStoreReadPilotStateSyncEngine this.mirror = options.mirror; this.enableBootstrapParityCheck = options.enableBootstrapParityCheck ?? false; this.enableCatchUpParityCheck = options.enableCatchUpParityCheck ?? false; + this.enableListMessagesParityCheck = options.enableListMessagesParityCheck ?? false; this.unsubscribeDelegate = this.delegate.onStateEvent((event) => { this.emit("stateEvent", event); void this.mirror.mirrorStateEvent(event).catch((error) => { @@ -138,6 +142,9 @@ export class LiveStoreReadPilotStateSyncEngine listMessages(raw: StateListMessagesInput): StateListMessagesResult { try { const result = this.mirror.debugListMessages(raw); + if (this.enableListMessagesParityCheck) { + this.checkListMessagesParity(raw, result); + } this.logReadSourceChange("state.listMessages", this.listMessagesSource, "livestore", { threadId: raw.threadId, }); @@ -157,6 +164,41 @@ export class LiveStoreReadPilotStateSyncEngine } } + private checkListMessagesParity( + raw: StateListMessagesInput, + mirrorResult: StateListMessagesResult, + ): void { + let delegateResult: StateListMessagesResult; + try { + delegateResult = this.delegate.listMessages(raw); + } catch (error) { + this.logger.warn("list-messages parity check failed to read delegate listMessages", { error }); + return; + } + + const diffs = diffListMessagesResults(delegateResult, mirrorResult); + if (diffs.length === 0) { + if (this.listMessagesParityState !== "in-parity") { + this.listMessagesParityState = "in-parity"; + this.logger.info("livestore read pilot list-messages parity check passed", { + threadId: raw.threadId, + offset: raw.offset ?? 0, + limit: raw.limit ?? 200, + }); + } + return; + } + + this.listMessagesParityState = "drift"; + this.logger.warn("livestore read pilot list-messages parity drift detected", { + threadId: raw.threadId, + offset: raw.offset ?? 0, + limit: raw.limit ?? 200, + diffCount: diffs.length, + sampleDiffs: diffs.slice(0, 5), + }); + } + catchUp(raw: StateCatchUpInput): StateCatchUpResult { const afterSeq = raw.afterSeq ?? 0; try { @@ -339,3 +381,40 @@ function diffCatchUpResults(expected: StateCatchUpResult, actual: StateCatchUpRe return diffs; } + +function diffListMessagesResults( + expected: StateListMessagesResult, + actual: StateListMessagesResult, +): string[] { + const diffs: string[] = []; + + if (expected.total !== actual.total) { + diffs.push(`total mismatch: expected=${expected.total} actual=${actual.total}`); + } + if (expected.nextOffset !== actual.nextOffset) { + diffs.push( + `nextOffset mismatch: expected=${String(expected.nextOffset)} actual=${String(actual.nextOffset)}`, + ); + } + if (expected.messages.length !== actual.messages.length) { + diffs.push( + `messages.length mismatch: expected=${expected.messages.length} actual=${actual.messages.length}`, + ); + } + + const minLength = Math.min(expected.messages.length, actual.messages.length); + for (let index = 0; index < minLength; index += 1) { + const expectedMessage = expected.messages[index]; + const actualMessage = actual.messages[index]; + if (!expectedMessage || !actualMessage) { + continue; + } + const expectedSerialized = JSON.stringify(expectedMessage); + const actualSerialized = JSON.stringify(actualMessage); + if (expectedSerialized !== actualSerialized) { + diffs.push(`messages[${index}] mismatch`); + } + } + + return diffs; +}