From e7d37a3643aa974d812ff843065b2f6d0435e04c Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 20 Feb 2026 12:28:32 +0000 Subject: [PATCH] Add enforce-liveStore startup guard for sync engine mode Co-authored-by: Julius Marminge --- README.md | 4 ++++ SYNC_ENGINE_MIGRATION.md | 6 ++++++ apps/server/src/index.ts | 8 ++++++- apps/server/src/syncEngineMode.test.ts | 29 +++++++++++++++++++++++++- apps/server/src/syncEngineMode.ts | 14 +++++++++++++ 5 files changed, 59 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 26d5ba6ef9..61c599b996 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,10 @@ The server supports feature-flagged sync-engine modes while migrating from the l - `T3CODE_SYNC_ENGINE_MODE=shadow` - Keeps legacy state as canonical, but mirrors committed `state.event` traffic into a LiveStore shadow store for parity validation. +- `T3CODE_LIVESTORE_ENFORCE_MODE=1` + - Disallows `legacy` and `shadow` sync modes at startup. + - Intended for post-cutover environments to prevent accidental rollback to pre-LiveStore modes. + Optional diagnostics: - `T3CODE_LIVESTORE_BOOTSTRAP_PARITY_CHECK=1` diff --git a/SYNC_ENGINE_MIGRATION.md b/SYNC_ENGINE_MIGRATION.md index f70a9acc57..5baca18700 100644 --- a/SYNC_ENGINE_MIGRATION.md +++ b/SYNC_ENGINE_MIGRATION.md @@ -11,6 +11,12 @@ Server mode is controlled by `T3CODE_SYNC_ENGINE_MODE`: - `legacy`: legacy persistence service is canonical for reads/writes. - `shadow`: legacy remains canonical, and `state.event` writes are mirrored into LiveStore. +Optional safety gate: + +- `T3CODE_LIVESTORE_ENFORCE_MODE=1` + - prevents startup in `legacy` or `shadow` mode. + - use in post-cutover environments to block accidental rollback. + ## Diagnostics flags - `T3CODE_LIVESTORE_BOOTSTRAP_PARITY_CHECK=1` diff --git a/apps/server/src/index.ts b/apps/server/src/index.ts index 02fe515a4a..ca013ce2b3 100644 --- a/apps/server/src/index.ts +++ b/apps/server/src/index.ts @@ -11,7 +11,7 @@ import { PersistenceService } from "./persistenceService"; import { LegacyStateSyncEngine } from "./stateSyncEngineLegacy"; import { LiveStoreReadPilotStateSyncEngine } from "./stateSyncEngineLiveStoreReadPilot"; import { ShadowStateSyncEngine } from "./stateSyncEngineShadow"; -import { resolveSyncEngineMode } from "./syncEngineMode"; +import { assertSyncEngineModeAllowed, resolveSyncEngineMode } from "./syncEngineMode"; import { createServer } from "./wsServer"; fixPath(); @@ -116,6 +116,11 @@ 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 enforceLiveStoreOnly = + parseBooleanEnv(process.env.T3CODE_LIVESTORE_ENFORCE_MODE) ?? false; + assertSyncEngineModeAllowed(syncEngineMode, { + enforceLiveStoreOnly, + }); const enableLiveStoreBootstrapParityCheck = parseBooleanEnv(process.env.T3CODE_LIVESTORE_BOOTSTRAP_PARITY_CHECK) ?? false; const enableLiveStoreCatchUpParityCheck = @@ -191,6 +196,7 @@ async function main() { stateDbPath, legacyStateDir, syncEngineMode, + enforceLiveStoreOnly, liveStoreBootstrapParityCheck: enableLiveStoreBootstrapParityCheck, liveStoreCatchUpParityCheck: enableLiveStoreCatchUpParityCheck, liveStoreListMessagesParityCheck: enableLiveStoreListMessagesParityCheck, diff --git a/apps/server/src/syncEngineMode.test.ts b/apps/server/src/syncEngineMode.test.ts index ff7834ee87..a5d61dd7aa 100644 --- a/apps/server/src/syncEngineMode.test.ts +++ b/apps/server/src/syncEngineMode.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import { resolveSyncEngineMode } from "./syncEngineMode"; +import { assertSyncEngineModeAllowed, resolveSyncEngineMode } from "./syncEngineMode"; describe("resolveSyncEngineMode", () => { it("defaults to livestore-read-pilot when env var is missing", () => { @@ -21,3 +21,30 @@ describe("resolveSyncEngineMode", () => { ); }); }); + +describe("assertSyncEngineModeAllowed", () => { + it("allows any mode when enforcement is disabled", () => { + expect(() => assertSyncEngineModeAllowed("legacy")).not.toThrow(); + expect(() => assertSyncEngineModeAllowed("shadow")).not.toThrow(); + expect(() => assertSyncEngineModeAllowed("livestore-read-pilot")).not.toThrow(); + expect(() => assertSyncEngineModeAllowed("livestore")).not.toThrow(); + }); + + it("blocks legacy and shadow when enforceLiveStoreOnly is enabled", () => { + expect(() => + assertSyncEngineModeAllowed("legacy", { enforceLiveStoreOnly: true }), + ).toThrow(/disabled/i); + expect(() => + assertSyncEngineModeAllowed("shadow", { enforceLiveStoreOnly: true }), + ).toThrow(/disabled/i); + }); + + it("allows livestore modes when enforceLiveStoreOnly is enabled", () => { + expect(() => + assertSyncEngineModeAllowed("livestore-read-pilot", { enforceLiveStoreOnly: true }), + ).not.toThrow(); + expect(() => + assertSyncEngineModeAllowed("livestore", { enforceLiveStoreOnly: true }), + ).not.toThrow(); + }); +}); diff --git a/apps/server/src/syncEngineMode.ts b/apps/server/src/syncEngineMode.ts index d684cbe9a6..bda8ba7f04 100644 --- a/apps/server/src/syncEngineMode.ts +++ b/apps/server/src/syncEngineMode.ts @@ -19,3 +19,17 @@ export function resolveSyncEngineMode(raw: string | undefined): SyncEngineMode { `Invalid T3CODE_SYNC_ENGINE_MODE: ${raw}. Expected "legacy", "shadow", "livestore-read-pilot", or "livestore".`, ); } + +export function assertSyncEngineModeAllowed( + syncEngineMode: SyncEngineMode, + options: { enforceLiveStoreOnly?: boolean } = {}, +): void { + if (!options.enforceLiveStoreOnly) { + return; + } + if (syncEngineMode === "legacy" || syncEngineMode === "shadow") { + throw new Error( + `Sync engine mode "${syncEngineMode}" is disabled because T3CODE_LIVESTORE_ENFORCE_MODE is enabled.`, + ); + } +}