Add optional shadow bootstrap parity diagnostics
Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
1 parent
6aa5a598b4
commit
08d4dcbbb8
5 files changed
+87
-2
No files matched your search
@@ -121,6 +121,8 @@ Optional diagnostics:
|
||||
- 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.
|
||||
- `T3CODE_LIVESTORE_SHADOW_BOOTSTRAP_PARITY_CHECK=1`
|
||||
- In `shadow` mode, compares mirror vs delegate `state.bootstrap` snapshots and logs drift diagnostics.
|
||||
|
||||
Web client state-source selection:
|
||||
|
||||
|
||||
@@ -18,6 +18,8 @@ Server mode is controlled by `T3CODE_SYNC_ENGINE_MODE`:
|
||||
- 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.
|
||||
- `T3CODE_LIVESTORE_SHADOW_BOOTSTRAP_PARITY_CHECK=1`
|
||||
- In shadow mode, compares `state.bootstrap` mirror output against delegate output.
|
||||
- `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`.
|
||||
|
||||
@@ -122,6 +122,8 @@ async function main() {
|
||||
parseBooleanEnv(process.env.T3CODE_LIVESTORE_CATCHUP_PARITY_CHECK) ?? false;
|
||||
const enableLiveStoreListMessagesParityCheck =
|
||||
parseBooleanEnv(process.env.T3CODE_LIVESTORE_LIST_MESSAGES_PARITY_CHECK) ?? false;
|
||||
const enableLiveStoreShadowBootstrapParityCheck =
|
||||
parseBooleanEnv(process.env.T3CODE_LIVESTORE_SHADOW_BOOTSTRAP_PARITY_CHECK) ?? false;
|
||||
const persistenceService = new PersistenceService({
|
||||
dbPath: stateDbPath,
|
||||
legacyProjectsJsonPath: path.join(legacyStateDir, "projects.json"),
|
||||
@@ -135,6 +137,7 @@ async function main() {
|
||||
? new ShadowStateSyncEngine({
|
||||
delegate: legacyStateSyncEngine,
|
||||
mirror: liveStoreMirror,
|
||||
enableBootstrapParityCheck: enableLiveStoreShadowBootstrapParityCheck,
|
||||
})
|
||||
: syncEngineMode === "livestore-read-pilot"
|
||||
? new LiveStoreReadPilotStateSyncEngine({
|
||||
@@ -180,6 +183,7 @@ async function main() {
|
||||
liveStoreBootstrapParityCheck: enableLiveStoreBootstrapParityCheck,
|
||||
liveStoreCatchUpParityCheck: enableLiveStoreCatchUpParityCheck,
|
||||
liveStoreListMessagesParityCheck: enableLiveStoreListMessagesParityCheck,
|
||||
liveStoreShadowBootstrapParityCheck: enableLiveStoreShadowBootstrapParityCheck,
|
||||
authEnabled: Boolean(authToken),
|
||||
});
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import type { StateEvent } from "@t3tools/contracts";
|
||||
import { diffStateSnapshots } from "./livestore/parity";
|
||||
import { LiveStoreStateMirror } from "./livestore/liveStoreEngine";
|
||||
@@ -177,4 +177,38 @@ describe("ShadowStateSyncEngine", () => {
|
||||
service.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("can run optional bootstrap parity diagnostics without changing read results", () => {
|
||||
const stateDir = makeTempDir("t3code-shadow-parity-diagnostics-state-");
|
||||
const projectDir = makeTempDir("t3code-shadow-parity-diagnostics-project-");
|
||||
const service = new PersistenceService({
|
||||
dbPath: path.join(stateDir, "state.sqlite"),
|
||||
});
|
||||
const legacy = new LegacyStateSyncEngine({ persistenceService: service });
|
||||
const debugReadSnapshot = vi.fn(() => ({
|
||||
projects: [],
|
||||
threads: [],
|
||||
lastStateSeq: 0,
|
||||
}));
|
||||
const mirror: StateEventMirror = {
|
||||
mirrorStateEvent: async () => undefined,
|
||||
debugReadSnapshot,
|
||||
dispose: async () => undefined,
|
||||
};
|
||||
const shadow = new ShadowStateSyncEngine({
|
||||
delegate: legacy,
|
||||
mirror,
|
||||
enableBootstrapParityCheck: true,
|
||||
});
|
||||
|
||||
try {
|
||||
legacy.addProject({ cwd: projectDir });
|
||||
const snapshot = shadow.loadSnapshot();
|
||||
expect(snapshot.projects).toHaveLength(1);
|
||||
expect(debugReadSnapshot).toHaveBeenCalledTimes(1);
|
||||
} finally {
|
||||
shadow.close();
|
||||
service.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -24,6 +24,7 @@ import type {
|
||||
ThreadsUpdateTitleInput,
|
||||
} from "@t3tools/contracts";
|
||||
import { createLogger } from "./logger";
|
||||
import { diffStateSnapshots } from "./livestore/parity";
|
||||
import type { ApplyCheckpointRevertInput, StateSyncEngine } from "./stateSyncEngine";
|
||||
|
||||
interface StateSyncEngineShadowEvents {
|
||||
@@ -32,12 +33,14 @@ interface StateSyncEngineShadowEvents {
|
||||
|
||||
export interface StateEventMirror {
|
||||
mirrorStateEvent(event: StateEvent): Promise<void>;
|
||||
debugReadSnapshot?(): StateBootstrapResult;
|
||||
dispose(): Promise<void> | void;
|
||||
}
|
||||
|
||||
export interface ShadowStateSyncEngineOptions {
|
||||
delegate: StateSyncEngine;
|
||||
mirror: StateEventMirror;
|
||||
enableBootstrapParityCheck?: boolean;
|
||||
}
|
||||
|
||||
export class ShadowStateSyncEngine
|
||||
@@ -46,14 +49,17 @@ export class ShadowStateSyncEngine
|
||||
{
|
||||
private readonly delegate: StateSyncEngine;
|
||||
private readonly mirror: StateEventMirror;
|
||||
private readonly enableBootstrapParityCheck: boolean;
|
||||
private readonly logger = createLogger("shadow-sync-engine");
|
||||
private readonly unsubscribeDelegate: () => void;
|
||||
private closed = false;
|
||||
private bootstrapParityState: "unknown" | "in-parity" | "drift" = "unknown";
|
||||
|
||||
constructor(options: ShadowStateSyncEngineOptions) {
|
||||
super();
|
||||
this.delegate = options.delegate;
|
||||
this.mirror = options.mirror;
|
||||
this.enableBootstrapParityCheck = options.enableBootstrapParityCheck ?? false;
|
||||
this.unsubscribeDelegate = this.delegate.onStateEvent((event) => {
|
||||
this.emit("stateEvent", event);
|
||||
void this.mirror.mirrorStateEvent(event).catch((error) => {
|
||||
@@ -70,7 +76,44 @@ export class ShadowStateSyncEngine
|
||||
}
|
||||
|
||||
loadSnapshot(): StateBootstrapResult {
|
||||
return this.delegate.loadSnapshot();
|
||||
const snapshot = this.delegate.loadSnapshot();
|
||||
if (this.enableBootstrapParityCheck) {
|
||||
this.checkBootstrapParity(snapshot);
|
||||
}
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
private checkBootstrapParity(delegateSnapshot: StateBootstrapResult): void {
|
||||
if (typeof this.mirror.debugReadSnapshot !== "function") {
|
||||
return;
|
||||
}
|
||||
|
||||
let mirrorSnapshot: StateBootstrapResult;
|
||||
try {
|
||||
mirrorSnapshot = this.mirror.debugReadSnapshot();
|
||||
} catch (error) {
|
||||
this.logger.warn("shadow bootstrap parity check failed to read mirror snapshot", { error });
|
||||
return;
|
||||
}
|
||||
|
||||
const diffs = diffStateSnapshots(delegateSnapshot, mirrorSnapshot);
|
||||
if (diffs.length === 0) {
|
||||
if (this.bootstrapParityState !== "in-parity") {
|
||||
this.bootstrapParityState = "in-parity";
|
||||
this.logger.info("shadow bootstrap parity check passed", {
|
||||
lastStateSeq: delegateSnapshot.lastStateSeq,
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
this.bootstrapParityState = "drift";
|
||||
this.logger.warn("shadow bootstrap parity drift detected", {
|
||||
diffCount: diffs.length,
|
||||
samplePaths: diffs.slice(0, 5).map((diff) => diff.path),
|
||||
delegateLastStateSeq: delegateSnapshot.lastStateSeq,
|
||||
mirrorLastStateSeq: mirrorSnapshot.lastStateSeq,
|
||||
});
|
||||
}
|
||||
|
||||
listMessages(raw: StateListMessagesInput): StateListMessagesResult {
|
||||
|
||||
Reference in new issue
Block a user