Add optional shadow catchup and listMessages parity checks
Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
1 parent
75cdd78edb
commit
24dbfa58c9
5 files changed
+252
-2
No files matched your search
@@ -123,6 +123,10 @@ 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_SHADOW_CATCHUP_PARITY_CHECK=1`
|
||||
- In `shadow` mode, compares mirror vs delegate `state.catchUp` responses and logs drift diagnostics.
|
||||
- `T3CODE_LIVESTORE_SHADOW_LIST_MESSAGES_PARITY_CHECK=1`
|
||||
- In `shadow` mode, compares mirror vs delegate `state.listMessages` responses 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).
|
||||
|
||||
|
||||
@@ -20,6 +20,10 @@ 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_SHADOW_CATCHUP_PARITY_CHECK=1`
|
||||
- In shadow mode, compares `state.catchUp` mirror output against delegate output.
|
||||
- `T3CODE_LIVESTORE_SHADOW_LIST_MESSAGES_PARITY_CHECK=1`
|
||||
- In shadow mode, compares `state.listMessages` 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`
|
||||
|
||||
@@ -124,6 +124,10 @@ 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 enableLiveStoreShadowCatchUpParityCheck =
|
||||
parseBooleanEnv(process.env.T3CODE_LIVESTORE_SHADOW_CATCHUP_PARITY_CHECK) ?? false;
|
||||
const enableLiveStoreShadowListMessagesParityCheck =
|
||||
parseBooleanEnv(process.env.T3CODE_LIVESTORE_SHADOW_LIST_MESSAGES_PARITY_CHECK) ?? false;
|
||||
const disableLiveStoreDelegateReadFallback =
|
||||
parseBooleanEnv(process.env.T3CODE_LIVESTORE_DISABLE_READ_FALLBACK) ?? false;
|
||||
const persistenceService = new PersistenceService({
|
||||
@@ -140,6 +144,8 @@ async function main() {
|
||||
delegate: legacyStateSyncEngine,
|
||||
mirror: liveStoreMirror,
|
||||
enableBootstrapParityCheck: enableLiveStoreShadowBootstrapParityCheck,
|
||||
enableCatchUpParityCheck: enableLiveStoreShadowCatchUpParityCheck,
|
||||
enableListMessagesParityCheck: enableLiveStoreShadowListMessagesParityCheck,
|
||||
})
|
||||
: syncEngineMode === "livestore-read-pilot"
|
||||
? new LiveStoreReadPilotStateSyncEngine({
|
||||
@@ -187,6 +193,8 @@ async function main() {
|
||||
liveStoreCatchUpParityCheck: enableLiveStoreCatchUpParityCheck,
|
||||
liveStoreListMessagesParityCheck: enableLiveStoreListMessagesParityCheck,
|
||||
liveStoreShadowBootstrapParityCheck: enableLiveStoreShadowBootstrapParityCheck,
|
||||
liveStoreShadowCatchUpParityCheck: enableLiveStoreShadowCatchUpParityCheck,
|
||||
liveStoreShadowListMessagesParityCheck: enableLiveStoreShadowListMessagesParityCheck,
|
||||
liveStoreDisableReadFallback: disableLiveStoreDelegateReadFallback,
|
||||
authEnabled: Boolean(authToken),
|
||||
});
|
||||
|
||||
@@ -211,4 +211,63 @@ describe("ShadowStateSyncEngine", () => {
|
||||
service.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("can run optional catch-up and list-messages parity diagnostics", () => {
|
||||
const stateDir = makeTempDir("t3code-shadow-parity-diagnostics-reads-state-");
|
||||
const projectDir = makeTempDir("t3code-shadow-parity-diagnostics-reads-project-");
|
||||
const service = new PersistenceService({
|
||||
dbPath: path.join(stateDir, "state.sqlite"),
|
||||
});
|
||||
const legacy = new LegacyStateSyncEngine({ persistenceService: service });
|
||||
const debugCatchUp = vi.fn((afterSeq: number) => ({
|
||||
events: [],
|
||||
lastStateSeq: afterSeq,
|
||||
}));
|
||||
const debugListMessages = vi.fn(() => ({
|
||||
messages: [],
|
||||
total: 0,
|
||||
nextOffset: null,
|
||||
}));
|
||||
const mirror: StateEventMirror = {
|
||||
mirrorStateEvent: async () => undefined,
|
||||
debugCatchUp,
|
||||
debugListMessages,
|
||||
dispose: async () => undefined,
|
||||
};
|
||||
const shadow = new ShadowStateSyncEngine({
|
||||
delegate: legacy,
|
||||
mirror,
|
||||
enableCatchUpParityCheck: true,
|
||||
enableListMessagesParityCheck: true,
|
||||
});
|
||||
|
||||
try {
|
||||
const project = shadow.addProject({ cwd: projectDir }).project;
|
||||
const thread = shadow.createThread({
|
||||
projectId: project.id,
|
||||
title: "Shadow parity read diagnostics thread",
|
||||
}).thread;
|
||||
|
||||
const catchUp = shadow.catchUp({ afterSeq: 0 });
|
||||
expect(catchUp.events.length).toBeGreaterThan(0);
|
||||
const listed = shadow.listMessages({ threadId: thread.id, offset: 0, limit: 10 });
|
||||
expect(listed).toEqual({
|
||||
messages: [],
|
||||
total: 0,
|
||||
nextOffset: null,
|
||||
});
|
||||
|
||||
expect(debugCatchUp).toHaveBeenCalledTimes(1);
|
||||
expect(debugCatchUp).toHaveBeenCalledWith(0);
|
||||
expect(debugListMessages).toHaveBeenCalledTimes(1);
|
||||
expect(debugListMessages).toHaveBeenCalledWith({
|
||||
threadId: thread.id,
|
||||
offset: 0,
|
||||
limit: 10,
|
||||
});
|
||||
} finally {
|
||||
shadow.close();
|
||||
service.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -34,6 +34,8 @@ interface StateSyncEngineShadowEvents {
|
||||
export interface StateEventMirror {
|
||||
mirrorStateEvent(event: StateEvent): Promise<void>;
|
||||
debugReadSnapshot?(): StateBootstrapResult;
|
||||
debugCatchUp?(afterSeq: number): StateCatchUpResult;
|
||||
debugListMessages?(raw: StateListMessagesInput): StateListMessagesResult;
|
||||
dispose(): Promise<void> | void;
|
||||
}
|
||||
|
||||
@@ -41,6 +43,8 @@ export interface ShadowStateSyncEngineOptions {
|
||||
delegate: StateSyncEngine;
|
||||
mirror: StateEventMirror;
|
||||
enableBootstrapParityCheck?: boolean;
|
||||
enableCatchUpParityCheck?: boolean;
|
||||
enableListMessagesParityCheck?: boolean;
|
||||
}
|
||||
|
||||
export class ShadowStateSyncEngine
|
||||
@@ -50,16 +54,22 @@ export class ShadowStateSyncEngine
|
||||
private readonly delegate: StateSyncEngine;
|
||||
private readonly mirror: StateEventMirror;
|
||||
private readonly enableBootstrapParityCheck: boolean;
|
||||
private readonly enableCatchUpParityCheck: boolean;
|
||||
private readonly enableListMessagesParityCheck: boolean;
|
||||
private readonly logger = createLogger("shadow-sync-engine");
|
||||
private readonly unsubscribeDelegate: () => void;
|
||||
private closed = false;
|
||||
private bootstrapParityState: "unknown" | "in-parity" | "drift" = "unknown";
|
||||
private catchUpParityState: "unknown" | "in-parity" | "drift" = "unknown";
|
||||
private listMessagesParityState: "unknown" | "in-parity" | "drift" = "unknown";
|
||||
|
||||
constructor(options: ShadowStateSyncEngineOptions) {
|
||||
super();
|
||||
this.delegate = options.delegate;
|
||||
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) => {
|
||||
@@ -117,11 +127,96 @@ export class ShadowStateSyncEngine
|
||||
}
|
||||
|
||||
listMessages(raw: StateListMessagesInput): StateListMessagesResult {
|
||||
return this.delegate.listMessages(raw);
|
||||
const result = this.delegate.listMessages(raw);
|
||||
if (this.enableListMessagesParityCheck) {
|
||||
this.checkListMessagesParity(raw, result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
catchUp(raw: StateCatchUpInput): StateCatchUpResult {
|
||||
return this.delegate.catchUp(raw);
|
||||
const result = this.delegate.catchUp(raw);
|
||||
if (this.enableCatchUpParityCheck) {
|
||||
this.checkCatchUpParity(raw, result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private checkCatchUpParity(raw: StateCatchUpInput, delegateResult: StateCatchUpResult): void {
|
||||
if (typeof this.mirror.debugCatchUp !== "function") {
|
||||
return;
|
||||
}
|
||||
|
||||
const afterSeq = raw.afterSeq ?? 0;
|
||||
let mirrorResult: StateCatchUpResult;
|
||||
try {
|
||||
mirrorResult = this.mirror.debugCatchUp(afterSeq);
|
||||
} catch (error) {
|
||||
this.logger.warn("shadow catch-up parity check failed to read mirror 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("shadow catch-up parity check passed", {
|
||||
afterSeq,
|
||||
lastStateSeq: delegateResult.lastStateSeq,
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
this.catchUpParityState = "drift";
|
||||
this.logger.warn("shadow catch-up parity drift detected", {
|
||||
afterSeq,
|
||||
diffCount: diffs.length,
|
||||
sampleDiffs: diffs.slice(0, 5),
|
||||
delegateLastStateSeq: delegateResult.lastStateSeq,
|
||||
mirrorLastStateSeq: mirrorResult.lastStateSeq,
|
||||
});
|
||||
}
|
||||
|
||||
private checkListMessagesParity(
|
||||
raw: StateListMessagesInput,
|
||||
delegateResult: StateListMessagesResult,
|
||||
): void {
|
||||
if (typeof this.mirror.debugListMessages !== "function") {
|
||||
return;
|
||||
}
|
||||
|
||||
let mirrorResult: StateListMessagesResult;
|
||||
try {
|
||||
mirrorResult = this.mirror.debugListMessages(raw);
|
||||
} catch (error) {
|
||||
this.logger.warn("shadow list-messages parity check failed to read mirror listMessages", {
|
||||
error,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const diffs = diffListMessagesResults(delegateResult, mirrorResult);
|
||||
if (diffs.length === 0) {
|
||||
if (this.listMessagesParityState !== "in-parity") {
|
||||
this.listMessagesParityState = "in-parity";
|
||||
this.logger.info("shadow list-messages parity check passed", {
|
||||
threadId: raw.threadId,
|
||||
offset: raw.offset ?? 0,
|
||||
limit: raw.limit ?? 200,
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
this.listMessagesParityState = "drift";
|
||||
this.logger.warn("shadow list-messages parity drift detected", {
|
||||
threadId: raw.threadId,
|
||||
offset: raw.offset ?? 0,
|
||||
limit: raw.limit ?? 200,
|
||||
diffCount: diffs.length,
|
||||
sampleDiffs: diffs.slice(0, 5),
|
||||
});
|
||||
}
|
||||
|
||||
getAppSettings(): AppSettings {
|
||||
@@ -190,3 +285,83 @@ export class ShadowStateSyncEngine
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
Reference in new issue
Block a user