diff --git a/README.md b/README.md index 4fb89eeafb..a12ac23c8a 100644 --- a/README.md +++ b/README.md @@ -138,12 +138,7 @@ Optional diagnostics: - 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: - -- `VITE_T3CODE_STATE_SOURCE_MODE=legacy-api|livestore-read-pilot` - - Selects the web state-source adapter mode. - - If unset, the web app derives mode from `server.getConfig().syncEngineMode` (`livestore-read-pilot`/`livestore` map to read-pilot source; everything else maps to legacy-api). - - Current behavior is protocol-compatible in both modes (delegates to `api.state.*`) while preserving a client-side seam for future LiveStore-native source wiring. +Web clients now always consume the server-authoritative `api.state.*` stream, and the server mode fully controls LiveStore vs fallback behavior. ## Provider architecture diff --git a/SYNC_ENGINE_MIGRATION.md b/SYNC_ENGINE_MIGRATION.md index 986c2e0152..89f11b189b 100644 --- a/SYNC_ENGINE_MIGRATION.md +++ b/SYNC_ENGINE_MIGRATION.md @@ -37,10 +37,7 @@ Optional safety gate: - In shadow mode, compares `state.listMessages` mirror output against delegate output. - `T3CODE_LIVESTORE_DISABLE_READ_FALLBACK=1` - In read-pilot mode, disables delegate read fallback for strict mirror-read canary validation. -- `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` (`livestore-read-pilot`/`livestore` -> read-pilot source). - - Current implementation is protocol-compatible in both modes. +- Web clients consume the server-authoritative `api.state.*` stream directly; server mode now controls fallback behavior. ## Suggested rollout sequence diff --git a/apps/web/src/routes/__root.tsx b/apps/web/src/routes/__root.tsx index 86e930232a..2e62415ac8 100644 --- a/apps/web/src/routes/__root.tsx +++ b/apps/web/src/routes/__root.tsx @@ -5,7 +5,7 @@ import { useParams, } from "@tanstack/react-router"; import { useEffect, useMemo, useRef } from "react"; -import { QueryClient, useQuery, useQueryClient } from "@tanstack/react-query"; +import { QueryClient, useQueryClient } from "@tanstack/react-query"; import { APP_DISPLAY_NAME } from "../branding"; import { Button } from "../components/ui/button"; @@ -13,12 +13,7 @@ import { AnchoredToastProvider, ToastProvider } from "../components/ui/toast"; import { isElectron } from "../env"; import { useNativeApi } from "../hooks/useNativeApi"; import { invalidateGitQueries } from "../lib/gitReactQuery"; -import { serverConfigQueryOptions } from "../lib/serverReactQuery"; -import { - createStateSource, - mapSyncEngineModeToStateSourceMode, - resolveStateSourceMode, -} from "../stateSource"; +import { createStateSource } from "../stateSource"; import { useStore } from "../store"; import { onServerWelcome } from "../wsNativeApi"; @@ -135,20 +130,9 @@ function StateSyncRouter() { strict: false, select: (params) => params.threadId, }); - const serverConfigQuery = useQuery(serverConfigQueryOptions(api)); - const stateSourceMode = useMemo(() => { - const rawOverride = import.meta.env.VITE_T3CODE_STATE_SOURCE_MODE as string | undefined; - if (rawOverride && rawOverride.trim().length > 0) { - return resolveStateSourceMode(rawOverride); - } - return mapSyncEngineModeToStateSourceMode(serverConfigQuery.data?.syncEngineMode); - }, [serverConfigQuery.data?.syncEngineMode]); const lastStateSeqRef = useRef(0); const stateQueueRef = useRef(Promise.resolve()); - const stateSource = useMemo( - () => (api ? createStateSource(api, { mode: stateSourceMode }) : null), - [api, stateSourceMode], - ); + const stateSource = useMemo(() => (api ? createStateSource(api) : null), [api]); useEffect(() => { if (!api || !stateSource) return; diff --git a/apps/web/src/stateSource.test.ts b/apps/web/src/stateSource.test.ts index be3e2d62a9..055d78972f 100644 --- a/apps/web/src/stateSource.test.ts +++ b/apps/web/src/stateSource.test.ts @@ -1,10 +1,6 @@ import { describe, expect, it, vi } from "vitest"; import type { NativeApi, StateEvent } from "@t3tools/contracts"; -import { - createStateSource, - mapSyncEngineModeToStateSourceMode, - resolveStateSourceMode, -} from "./stateSource"; +import { createStateSource } from "./stateSource"; function makeApi(): NativeApi { const onStateEvent = vi.fn<(callback: (event: StateEvent) => void) => () => void>((callback) => { @@ -169,7 +165,6 @@ describe("createStateSource", () => { it("delegates bootstrap/catchUp/onEvent to api.state", async () => { const api = makeApi(); const source = createStateSource(api); - expect(source.mode).toBe("legacy-api"); await expect(source.bootstrap()).resolves.toEqual({ projects: [], @@ -200,36 +195,4 @@ describe("createStateSource", () => { expect(api.state.catchUp).toHaveBeenCalledWith({ afterSeq: 1 }); expect(api.state.onEvent).toHaveBeenCalledTimes(1); }); - - it("supports explicit read-pilot mode selection", async () => { - const api = makeApi(); - const source = createStateSource(api, { mode: "livestore-read-pilot" }); - expect(source.mode).toBe("livestore-read-pilot"); - await source.bootstrap(); - expect(api.state.bootstrap).toHaveBeenCalledTimes(1); - }); -}); - -describe("resolveStateSourceMode", () => { - it("defaults to legacy-api for missing/unknown values", () => { - expect(resolveStateSourceMode(undefined)).toBe("legacy-api"); - expect(resolveStateSourceMode("unknown")).toBe("legacy-api"); - }); - - it("accepts livestore-read-pilot value", () => { - expect(resolveStateSourceMode("livestore-read-pilot")).toBe("livestore-read-pilot"); - }); -}); - -describe("mapSyncEngineModeToStateSourceMode", () => { - it("maps read-pilot server mode to read-pilot state source", () => { - expect(mapSyncEngineModeToStateSourceMode("livestore-read-pilot")).toBe("livestore-read-pilot"); - expect(mapSyncEngineModeToStateSourceMode("livestore")).toBe("livestore-read-pilot"); - }); - - it("maps legacy/shadow/undefined server modes to legacy-api source", () => { - expect(mapSyncEngineModeToStateSourceMode("legacy")).toBe("legacy-api"); - expect(mapSyncEngineModeToStateSourceMode("shadow")).toBe("legacy-api"); - expect(mapSyncEngineModeToStateSourceMode(undefined)).toBe("legacy-api"); - }); }); diff --git a/apps/web/src/stateSource.ts b/apps/web/src/stateSource.ts index 04f71ef54f..ed541d74a6 100644 --- a/apps/web/src/stateSource.ts +++ b/apps/web/src/stateSource.ts @@ -6,39 +6,14 @@ import type { StateEvent, } from "@t3tools/contracts"; -export type StateSourceMode = "legacy-api" | "livestore-read-pilot"; - export interface StateSource { - mode: StateSourceMode; bootstrap(): Promise; catchUp(input: StateCatchUpInput): Promise; onEvent(listener: (event: StateEvent) => void): () => void; } -export function resolveStateSourceMode(rawMode: string | undefined): StateSourceMode { - if (rawMode === "livestore-read-pilot") { - return "livestore-read-pilot"; - } - return "legacy-api"; -} - -export function mapSyncEngineModeToStateSourceMode( - syncEngineMode: "legacy" | "shadow" | "livestore-read-pilot" | "livestore" | undefined, -): StateSourceMode { - return syncEngineMode === "livestore-read-pilot" || syncEngineMode === "livestore" - ? "livestore-read-pilot" - : "legacy-api"; -} - -export function createStateSource( - api: NativeApi, - options: { - mode?: StateSourceMode; - } = {}, -): StateSource { - const mode = options.mode ?? "legacy-api"; +export function createStateSource(api: NativeApi): StateSource { return { - mode, bootstrap: () => api.state.bootstrap(), catchUp: (input) => api.state.catchUp(input), onEvent: (listener) => api.state.onEvent(listener),