Add enforce-liveStore startup guard for sync engine mode

Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
Cursor AgentandJulius Marminge committed 2026-02-20 12:28:32 +00:00
1 parent 8b8ce91e37
commit e7d37a3643
5 files changed
+59 -2

No files matched your search

+4
View File
@@ -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`
+6
View File
@@ -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`
+7 -1
View File
@@ -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,
+28 -1
View File
@@ -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();
});
});
+14
View File
@@ -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.`,
);
}
}