Add bootstrap parity diagnostics for LiveStore read pilot

Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
Cursor AgentandJulius Marminge committed 2026-02-20 10:02:40 +00:00
1 parent 637eae09a4
commit 38ca95a787
3 files changed
+84

No files matched your search

+4
View File
@@ -133,6 +133,8 @@ async function main() {
const legacyStateDir = resolveStateDir(process.env.T3CODE_STATE_DIR);
const stateDbPath = resolveStateDbPath(process.env.T3CODE_STATE_DB_PATH);
const syncEngineMode = resolveSyncEngineMode(process.env.T3CODE_SYNC_ENGINE_MODE);
const enableLiveStoreBootstrapParityCheck =
parseBooleanEnv(process.env.T3CODE_LIVESTORE_BOOTSTRAP_PARITY_CHECK) ?? false;
const persistenceService = new PersistenceService({
dbPath: stateDbPath,
legacyProjectsJsonPath: path.join(legacyStateDir, "projects.json"),
@@ -151,6 +153,7 @@ async function main() {
? new LiveStoreReadPilotStateSyncEngine({
delegate: legacyStateSyncEngine,
mirror: liveStoreMirror,
enableBootstrapParityCheck: enableLiveStoreBootstrapParityCheck,
})
: legacyStateSyncEngine;
const devUrl = process.env.VITE_DEV_SERVER_URL;
@@ -184,6 +187,7 @@ async function main() {
stateDbPath,
legacyStateDir,
syncEngineMode,
liveStoreBootstrapParityCheck: enableLiveStoreBootstrapParityCheck,
authEnabled: Boolean(authToken),
});
@@ -262,4 +262,47 @@ describe("LiveStoreReadPilotStateSyncEngine", () => {
engine.close();
}
});
it("runs bootstrap parity check against delegate when enabled", () => {
const delegate = new MockDelegateStateSyncEngine();
delegate.loadSnapshotMock.mockReturnValue({
projects: [],
threads: [],
lastStateSeq: 9,
});
const mirror = makeMirrorStub({
snapshot: {
projects: [],
threads: [],
lastStateSeq: 5,
},
catchUp: {
events: [],
lastStateSeq: 5,
},
listMessages: {
messages: [],
total: 0,
nextOffset: null,
},
});
const engine = new LiveStoreReadPilotStateSyncEngine({
delegate,
mirror,
enableBootstrapParityCheck: true,
});
try {
expect(engine.loadSnapshot()).toEqual({
projects: [],
threads: [],
lastStateSeq: 5,
});
// One call from the parity check path while mirror remains source.
expect(delegate.loadSnapshotMock).toHaveBeenCalledTimes(1);
} finally {
engine.close();
}
});
});
@@ -25,6 +25,7 @@ import type {
} from "@t3tools/contracts";
import { createLogger } from "./logger";
import type { LiveStoreStateMirror } from "./livestore/liveStoreEngine";
import { diffStateSnapshots } from "./livestore/parity";
import type { ApplyCheckpointRevertInput, StateSyncEngine } from "./stateSyncEngine";
interface LiveStoreReadPilotEvents {
@@ -34,6 +35,7 @@ interface LiveStoreReadPilotEvents {
export interface LiveStoreReadPilotStateSyncEngineOptions {
delegate: StateSyncEngine;
mirror: LiveStoreStateMirror;
enableBootstrapParityCheck?: boolean;
}
export class LiveStoreReadPilotStateSyncEngine
@@ -44,13 +46,16 @@ export class LiveStoreReadPilotStateSyncEngine
private readonly mirror: LiveStoreStateMirror;
private readonly logger = createLogger("livestore-read-pilot");
private readonly unsubscribeDelegate: () => void;
private readonly enableBootstrapParityCheck: boolean;
private closed = false;
private bootstrapSource: "delegate" | "livestore" = "delegate";
private bootstrapParityState: "unknown" | "in-parity" | "drift" = "unknown";
constructor(options: LiveStoreReadPilotStateSyncEngineOptions) {
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) => {
@@ -73,6 +78,9 @@ export class LiveStoreReadPilotStateSyncEngine
try {
const snapshot = this.mirror.debugReadSnapshot();
if (snapshot.lastStateSeq > 0) {
if (this.enableBootstrapParityCheck) {
this.checkBootstrapParity(snapshot);
}
if (this.bootstrapSource !== "livestore") {
this.bootstrapSource = "livestore";
this.logger.info("serving state.bootstrap from livestore mirror", {
@@ -91,6 +99,35 @@ export class LiveStoreReadPilotStateSyncEngine
return this.delegate.loadSnapshot();
}
private checkBootstrapParity(mirrorSnapshot: StateBootstrapResult): void {
let delegateSnapshot: StateBootstrapResult;
try {
delegateSnapshot = this.delegate.loadSnapshot();
} catch (error) {
this.logger.warn("bootstrap parity check failed to read delegate snapshot", { error });
return;
}
const diffs = diffStateSnapshots(delegateSnapshot, mirrorSnapshot);
if (diffs.length === 0) {
if (this.bootstrapParityState !== "in-parity") {
this.bootstrapParityState = "in-parity";
this.logger.info("livestore read pilot bootstrap parity check passed", {
lastStateSeq: mirrorSnapshot.lastStateSeq,
});
}
return;
}
this.bootstrapParityState = "drift";
this.logger.warn("livestore read pilot 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 {
try {
return this.mirror.debugListMessages(raw);