@devries-cameron20 / t3code Mirror · read-only / commits / b5b769dddf
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 25, 03:45 UTC

Prevent stale preview snapshots from resurrecting sessions

- Track preview store revisions per thread - Ignore stale SWR results while revalidating - Avoid restoring closed sessions from outdated data

Julius Marminge committed Jun 12, 2026 at 18:33 UTC b5b769dddfe935650b48473dd5b55f2c099abdd8
3 files changed +38 -8
apps/web/src/components/preview/previewSessionState.ts
+9 -3
@@ -8,6 +8,7 @@ import * as Option from "effect/Option";
8 import { AsyncResult, Atom } from "effect/unstable/reactivity";
9
10 import { ensureEnvironmentApi } from "~/environmentApi";
11 +import { readPreviewStateRevision } from "~/previewStateStore";
12 import { appAtomRegistry } from "~/rpc/atomRegistry";
13
14 const PREVIEW_SESSION_STALE_TIME_MS = 5_000;
@@ -21,14 +22,16 @@ class PreviewSessionQueryError extends Data.TaggedError("PreviewSessionQueryErro
22 const previewSessionListAtom = Atom.family((threadKey: string) =>
23 Atom.make(
24 Effect.tryPromise({
24 - try: () => {
25 + try: async () => {
26 const threadRef = parseScopedThreadKey(threadKey);
27 if (!threadRef) {
28 throw new Error(`Invalid scoped thread key: ${threadKey}`);
29 }
29 - return ensureEnvironmentApi(threadRef.environmentId).preview.list({
30 + const revision = readPreviewStateRevision(threadRef);
31 + const result = await ensureEnvironmentApi(threadRef.environmentId).preview.list({
32 threadId: threadRef.threadId,
33 });
34 + return { result, revision };
35 },
36 catch: (cause) =>
37 new PreviewSessionQueryError({
@@ -47,7 +50,10 @@ const previewSessionListAtom = Atom.family((threadKey: string) =>
50 );
51
52 export interface PreviewSessionQueryState {
50 - readonly data: PreviewListResult | null;
53 + readonly data: {
54 + readonly result: PreviewListResult;
55 + readonly revision: number;
56 + } | null;
57 readonly error: string | null;
58 readonly isPending: boolean;
59 }
apps/web/src/components/preview/usePreviewSession.ts
+15 -5
@@ -6,7 +6,7 @@ import { useEffect } from "react";
6
7 import { ensureEnvironmentApi, readEnvironmentApi } from "~/environmentApi";
8 import { readEnvironmentConnection, subscribeEnvironmentConnections } from "~/environments/runtime";
9 -import { usePreviewStateStore } from "~/previewStateStore";
9 +import { readPreviewStateRevision, usePreviewStateStore } from "~/previewStateStore";
10
11 import { refreshPreviewSessionState, usePreviewSessionState } from "./previewSessionState";
12
@@ -24,14 +24,24 @@ export function usePreviewSession(threadRef: ScopedThreadRef): void {
24 const applyServerEvent = usePreviewStateStore((state) => state.applyServerEvent);
25
26 useEffect(() => {
27 - if (!query.data) return;
27 + // SWR retains stale data while revalidating. Do not project that stale
28 + // snapshot back into the live store because it can resurrect a session
29 + // that was just closed.
30 + if (
31 + query.isPending ||
32 + !query.data ||
33 + query.data.revision !== readPreviewStateRevision(threadRef)
34 + ) {
35 + return;
36 + }
37 const threadIdValue = threadRef.threadId;
38 let cancelled = false;
30 - if (query.data.sessions.length > 0) {
31 - for (const snapshot of query.data.sessions) applyServerSnapshot(threadRef, snapshot);
39 + if (query.data.result.sessions.length > 0) {
40 + for (const snapshot of query.data.result.sessions) {
41 + applyServerSnapshot(threadRef, snapshot);
42 + }
43 return;
44 }
34 - if (query.isPending) return;
45
46 // Server has no sessions — try to recover what the renderer remembers
47 // from before the disconnect.
apps/web/src/previewStateStore.ts
+14
@@ -49,6 +49,16 @@ const EMPTY_THREAD_PREVIEW_STATE: ThreadPreviewState = Object.freeze({
49 recentlySeenUrls: [] as string[],
50 });
51
52 +const revisionByThreadKey = new Map<string, number>();
53 +
54 +const bumpPreviewStateRevision = (threadKey: string): void => {
55 + revisionByThreadKey.set(threadKey, (revisionByThreadKey.get(threadKey) ?? 0) + 1);
56 +};
57 +
58 +export function readPreviewStateRevision(ref: ScopedThreadRef): number {
59 + return revisionByThreadKey.get(scopedThreadKey(ref)) ?? 0;
60 +}
61 +
62 export interface PreviewStateStoreState {
63 byThreadKey: Record<string, ThreadPreviewState>;
64 applyServerEvent: (ref: ScopedThreadRef, event: PreviewEvent) => void;
@@ -120,6 +130,7 @@ export const usePreviewStateStore = create<PreviewStateStoreState>()((set) => ({
130 applyServerEvent: (ref, event) =>
131 set((state) => {
132 const threadKey = scopedThreadKey(ref);
133 + bumpPreviewStateRevision(threadKey);
134 let nextByThread = state.byThreadKey;
135 switch (event.type) {
136 case "opened":
@@ -177,6 +188,7 @@ export const usePreviewStateStore = create<PreviewStateStoreState>()((set) => ({
188 applyServerSnapshot: (ref, snapshot) =>
189 set((state) => {
190 const threadKey = scopedThreadKey(ref);
191 + bumpPreviewStateRevision(threadKey);
192 const nextByThread = updateThread(state, threadKey, (current) => {
193 if (!snapshot && current.snapshot === null) return current;
194 if (!snapshot) {
@@ -226,6 +238,7 @@ export const usePreviewStateStore = create<PreviewStateStoreState>()((set) => ({
238 removeSession: (ref, tabId) =>
239 set((state) => {
240 const threadKey = scopedThreadKey(ref);
241 + bumpPreviewStateRevision(threadKey);
242 return {
243 byThreadKey: updateThread(state, threadKey, (current) => removeSession(current, tabId)),
244 };
@@ -258,6 +271,7 @@ export const usePreviewStateStore = create<PreviewStateStoreState>()((set) => ({
271 removeThread: (ref) =>
272 set((state) => {
273 const threadKey = scopedThreadKey(ref);
274 + bumpPreviewStateRevision(threadKey);
275 if (!(threadKey in state.byThreadKey)) return state;
276 return { byThreadKey: removeThreadKey(state.byThreadKey, threadKey) };
277 }),