From bba3afe33bdd108e9dddc548604a0ae1c4b1b632 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 20 Feb 2026 12:23:58 +0000 Subject: [PATCH] Add read-pilot route metrics and fallback counters Co-authored-by: Julius Marminge --- README.md | 1 + SYNC_ENGINE_MIGRATION.md | 1 + .../stateSyncEngineLiveStoreReadPilot.test.ts | 48 ++++++++++++++++ .../src/stateSyncEngineLiveStoreReadPilot.ts | 56 ++++++++++++++++++- 4 files changed, 105 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e416e93569..26d5ba6ef9 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,7 @@ Optional diagnostics: - 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). + - Read-source/fallback counters are emitted on shutdown via `livestore read pilot metrics` logs. Web client state-source selection: diff --git a/SYNC_ENGINE_MIGRATION.md b/SYNC_ENGINE_MIGRATION.md index 0a736eb80b..f70a9acc57 100644 --- a/SYNC_ENGINE_MIGRATION.md +++ b/SYNC_ENGINE_MIGRATION.md @@ -48,6 +48,7 @@ Server mode is controlled by `T3CODE_SYNC_ENGINE_MODE`: 4. **Confidence window** - Keep parity flags enabled until drift warnings are consistently absent. - Track fallback frequency (should trend toward zero in healthy conditions). + - Review `livestore read pilot metrics` shutdown logs for route-level source and fallback counts. ## Phase-5 cleanup readiness checklist diff --git a/apps/server/src/stateSyncEngineLiveStoreReadPilot.test.ts b/apps/server/src/stateSyncEngineLiveStoreReadPilot.test.ts index 94ae6e8f12..f66d7238b0 100644 --- a/apps/server/src/stateSyncEngineLiveStoreReadPilot.test.ts +++ b/apps/server/src/stateSyncEngineLiveStoreReadPilot.test.ts @@ -202,6 +202,18 @@ describe("LiveStoreReadPilotStateSyncEngine", () => { expect(delegate.loadSnapshotMock).not.toHaveBeenCalled(); expect(delegate.catchUpMock).not.toHaveBeenCalled(); expect(delegate.listMessagesMock).not.toHaveBeenCalled(); + expect(engine.debugReadMetrics()).toEqual({ + routeReadCounts: { + "state.bootstrap": { livestore: 1, delegate: 0 }, + "state.catchUp": { livestore: 1, delegate: 0 }, + "state.listMessages": { livestore: 1, delegate: 0 }, + }, + fallbackCounts: { + "state.bootstrap": { "mirror-error": 0, "empty-mirror": 0 }, + "state.catchUp": { "mirror-error": 0, "empty-mirror": 0 }, + "state.listMessages": { "mirror-error": 0, "empty-mirror": 0 }, + }, + }); } finally { engine.close(); } @@ -258,6 +270,18 @@ describe("LiveStoreReadPilotStateSyncEngine", () => { expect(delegate.loadSnapshotMock).toHaveBeenCalledTimes(1); expect(delegate.catchUpMock).toHaveBeenCalledTimes(1); expect(delegate.listMessagesMock).toHaveBeenCalledTimes(1); + expect(engine.debugReadMetrics()).toEqual({ + routeReadCounts: { + "state.bootstrap": { livestore: 0, delegate: 1 }, + "state.catchUp": { livestore: 0, delegate: 1 }, + "state.listMessages": { livestore: 0, delegate: 1 }, + }, + fallbackCounts: { + "state.bootstrap": { "mirror-error": 1, "empty-mirror": 0 }, + "state.catchUp": { "mirror-error": 1, "empty-mirror": 0 }, + "state.listMessages": { "mirror-error": 1, "empty-mirror": 0 }, + }, + }); } finally { engine.close(); } @@ -476,6 +500,18 @@ describe("LiveStoreReadPilotStateSyncEngine", () => { lastStateSeq: 0, }); expect(delegate.loadSnapshotMock).not.toHaveBeenCalled(); + expect(engine.debugReadMetrics()).toEqual({ + routeReadCounts: { + "state.bootstrap": { livestore: 1, delegate: 0 }, + "state.catchUp": { livestore: 0, delegate: 0 }, + "state.listMessages": { livestore: 0, delegate: 0 }, + }, + fallbackCounts: { + "state.bootstrap": { "mirror-error": 0, "empty-mirror": 0 }, + "state.catchUp": { "mirror-error": 0, "empty-mirror": 0 }, + "state.listMessages": { "mirror-error": 0, "empty-mirror": 0 }, + }, + }); } finally { engine.close(); } @@ -546,6 +582,18 @@ describe("LiveStoreReadPilotStateSyncEngine", () => { }); expect(delegate.catchUpMock).toHaveBeenCalledTimes(1); expect(delegate.listMessagesMock).toHaveBeenCalledTimes(1); + expect(engine.debugReadMetrics()).toEqual({ + routeReadCounts: { + "state.bootstrap": { livestore: 0, delegate: 0 }, + "state.catchUp": { livestore: 1, delegate: 1 }, + "state.listMessages": { livestore: 1, delegate: 1 }, + }, + fallbackCounts: { + "state.bootstrap": { "mirror-error": 0, "empty-mirror": 0 }, + "state.catchUp": { "mirror-error": 1, "empty-mirror": 0 }, + "state.listMessages": { "mirror-error": 1, "empty-mirror": 0 }, + }, + }); } finally { engine.close(); } diff --git a/apps/server/src/stateSyncEngineLiveStoreReadPilot.ts b/apps/server/src/stateSyncEngineLiveStoreReadPilot.ts index 3e825c35cd..cfdc095ea2 100644 --- a/apps/server/src/stateSyncEngineLiveStoreReadPilot.ts +++ b/apps/server/src/stateSyncEngineLiveStoreReadPilot.ts @@ -33,6 +33,13 @@ interface LiveStoreReadPilotEvents { } type LiveStoreReadSource = "delegate" | "livestore"; +type LiveStoreReadRoute = "state.bootstrap" | "state.catchUp" | "state.listMessages"; +type LiveStoreFallbackReason = "mirror-error" | "empty-mirror"; + +export interface LiveStoreReadPilotMetrics { + routeReadCounts: Record; + fallbackCounts: Record; +} export interface LiveStoreReadPilotStateSyncEngineOptions { delegate: StateSyncEngine; @@ -62,6 +69,18 @@ export class LiveStoreReadPilotStateSyncEngine private bootstrapParityState: "unknown" | "in-parity" | "drift" = "unknown"; private catchUpParityState: "unknown" | "in-parity" | "drift" = "unknown"; private listMessagesParityState: "unknown" | "in-parity" | "drift" = "unknown"; + private readonly metrics: LiveStoreReadPilotMetrics = { + routeReadCounts: { + "state.bootstrap": { livestore: 0, delegate: 0 }, + "state.catchUp": { livestore: 0, delegate: 0 }, + "state.listMessages": { livestore: 0, delegate: 0 }, + }, + fallbackCounts: { + "state.bootstrap": { "mirror-error": 0, "empty-mirror": 0 }, + "state.catchUp": { "mirror-error": 0, "empty-mirror": 0 }, + "state.listMessages": { "mirror-error": 0, "empty-mirror": 0 }, + }, + }; constructor(options: LiveStoreReadPilotStateSyncEngineOptions) { super(); @@ -100,12 +119,15 @@ export class LiveStoreReadPilotStateSyncEngine this.logReadSourceChange("state.bootstrap", this.bootstrapSource, "delegate", { lastStateSeq: delegateSnapshot.lastStateSeq, }); + this.recordReadRoute("state.bootstrap", "delegate"); + this.recordFallback("state.bootstrap", "empty-mirror"); this.bootstrapSource = "delegate"; return delegateSnapshot; } this.logReadSourceChange("state.bootstrap", this.bootstrapSource, "livestore", { lastStateSeq: snapshot.lastStateSeq, }); + this.recordReadRoute("state.bootstrap", "livestore"); this.bootstrapSource = "livestore"; return snapshot; } catch (error) { @@ -116,11 +138,13 @@ export class LiveStoreReadPilotStateSyncEngine throw error; } this.logger.warn("failed to read bootstrap from livestore mirror; using delegate", { error }); + this.recordFallback("state.bootstrap", "mirror-error"); } const snapshot = this.delegate.loadSnapshot(); this.logReadSourceChange("state.bootstrap", this.bootstrapSource, "delegate", { lastStateSeq: snapshot.lastStateSeq, }); + this.recordReadRoute("state.bootstrap", "delegate"); this.bootstrapSource = "delegate"; return snapshot; } @@ -163,6 +187,7 @@ export class LiveStoreReadPilotStateSyncEngine this.logReadSourceChange("state.listMessages", this.listMessagesSource, "livestore", { threadId: raw.threadId, }); + this.recordReadRoute("state.listMessages", "livestore"); this.listMessagesSource = "livestore"; return result; } catch (error) { @@ -176,6 +201,7 @@ export class LiveStoreReadPilotStateSyncEngine ); throw error; } + this.recordFallback("state.listMessages", "mirror-error"); this.logger.warn("failed to list messages from livestore mirror; using delegate", { error, threadId: raw.threadId, @@ -184,6 +210,7 @@ export class LiveStoreReadPilotStateSyncEngine this.logReadSourceChange("state.listMessages", this.listMessagesSource, "delegate", { threadId: raw.threadId, }); + this.recordReadRoute("state.listMessages", "delegate"); this.listMessagesSource = "delegate"; return result; } @@ -234,6 +261,7 @@ export class LiveStoreReadPilotStateSyncEngine this.logReadSourceChange("state.catchUp", this.catchUpSource, "livestore", { afterSeq, }); + this.recordReadRoute("state.catchUp", "livestore"); this.catchUpSource = "livestore"; return result; } catch (error) { @@ -244,6 +272,7 @@ export class LiveStoreReadPilotStateSyncEngine }); throw error; } + this.recordFallback("state.catchUp", "mirror-error"); this.logger.warn("failed to catch up from livestore mirror; using delegate", { error, afterSeq, @@ -252,11 +281,27 @@ export class LiveStoreReadPilotStateSyncEngine this.logReadSourceChange("state.catchUp", this.catchUpSource, "delegate", { afterSeq, }); + this.recordReadRoute("state.catchUp", "delegate"); this.catchUpSource = "delegate"; return result; } } + debugReadMetrics(): LiveStoreReadPilotMetrics { + return { + routeReadCounts: { + "state.bootstrap": { ...this.metrics.routeReadCounts["state.bootstrap"] }, + "state.catchUp": { ...this.metrics.routeReadCounts["state.catchUp"] }, + "state.listMessages": { ...this.metrics.routeReadCounts["state.listMessages"] }, + }, + fallbackCounts: { + "state.bootstrap": { ...this.metrics.fallbackCounts["state.bootstrap"] }, + "state.catchUp": { ...this.metrics.fallbackCounts["state.catchUp"] }, + "state.listMessages": { ...this.metrics.fallbackCounts["state.listMessages"] }, + }, + }; + } + private checkCatchUpParity(raw: StateCatchUpInput, mirrorResult: StateCatchUpResult): void { let delegateResult: StateCatchUpResult; try { @@ -289,7 +334,7 @@ export class LiveStoreReadPilotStateSyncEngine } private logReadSourceChange( - route: "state.bootstrap" | "state.catchUp" | "state.listMessages", + route: LiveStoreReadRoute, previous: LiveStoreReadSource, next: LiveStoreReadSource, metadata: Record, @@ -304,6 +349,14 @@ export class LiveStoreReadPilotStateSyncEngine }); } + private recordReadRoute(route: LiveStoreReadRoute, source: LiveStoreReadSource): void { + this.metrics.routeReadCounts[route][source] += 1; + } + + private recordFallback(route: LiveStoreReadRoute, reason: LiveStoreFallbackReason): void { + this.metrics.fallbackCounts[route][reason] += 1; + } + getAppSettings(): AppSettings { return this.delegate.getAppSettings(); } @@ -365,6 +418,7 @@ export class LiveStoreReadPilotStateSyncEngine return; } this.closed = true; + this.logger.info("livestore read pilot metrics", { metrics: this.metrics }); this.unsubscribeDelegate(); this.removeAllListeners(); void this.mirror.dispose();