Add optional catch-up parity diagnostics for read pilot

Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
Cursor AgentandJulius Marminge committed 2026-02-20 10:33:55 +00:00
1 parent cbfe1e3a57
commit 7a9b733368
4 files changed
+129

No files matched your search

+2
View File
@@ -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
+4
View File
@@ -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),
});
@@ -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({
@@ -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;
}