From 53ebb94ddb167ae4bebd06fcdbed77df1391bc68 Mon Sep 17 00:00:00 2001 From: Guilhem Date: Wed, 25 Nov 2026 23:15:28 +0100 Subject: [PATCH] fix: skip the session backup in preview frames and keep draft handles stable (#12598) * fix: skip the AI session backup in a session preview tab's frame Co-Authored-By: Claude Opus 5.5 * fix: keep draft handles unproxied so useMany's no-op check matches Co-Authored-By: Claude Opus 5.5 * fix: have a preview frame ask its page to flush the marks it writes Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- .../sessions/sessionMirror.svelte.ts | 36 +++++++++++++++++-- frontend/src/lib/userDraft.svelte.ts | 36 ++++++++++++------- 2 files changed, 57 insertions(+), 15 deletions(-) diff --git a/frontend/src/lib/components/sessions/sessionMirror.svelte.ts b/frontend/src/lib/components/sessions/sessionMirror.svelte.ts index 657f120e98..d5ae49f121 100644 --- a/frontend/src/lib/components/sessions/sessionMirror.svelte.ts +++ b/frontend/src/lib/components/sessions/sessionMirror.svelte.ts @@ -67,6 +67,26 @@ import { type PushBody } from './sessionMirrorPlan' +/** A session preview tab loads the app in a frame of the app itself. Its storage is the + * page's, so the backup is left to the page: the frame only writes marks and asks the page to + * flush them, since its own restore would hold the user's lock against the page's flushes, and + * Firefox rejects a lock request from a frame's document while it is being removed. A frame + * under another origin (the app embedded in another site) keeps its own backup: no top-level + * page of the app reads its storage. */ +const inAppFrame = BROWSER && window.self !== window.top && sameOriginTop() + +/** Sent to the top page, not signalled through `storage` events: those reach every tab of + * the user, and each would flush a mark the page already uploads. */ +const FRAME_FLUSH_MESSAGE = 'wm-session-mirror-flush' + +function sameOriginTop(): boolean { + try { + return window.top?.location.origin === window.location.origin + } catch { + return false + } +} + /** A flush waits for the marks to go quiet this long. */ const QUIET_MS = 15_000 /** ...but never longer than this after the first unflushed mark. */ @@ -352,12 +372,17 @@ function clearTimers(): void { } function scheduleFlush(): void { + if (inAppFrame) { + window.top?.postMessage({ type: FRAME_FLUSH_MESSAGE }, window.location.origin) + return + } clearTimeout(quietTimer) quietTimer = setTimeout(runFlush, QUIET_MS) maxTimer ??= setTimeout(runFlush, MAX_DELAY_MS) } function runFlush(): void { + if (inAppFrame) return clearTimers() void enqueue(flush) } @@ -1557,7 +1582,7 @@ async function restoreWorkspace( * touched; only ones this browser lacks are brought back. */ export function restoreSessionBackups(currentWorkspace: string): void { - if (!BROWSER) return + if (!BROWSER || inAppFrame) return // A restore writes an absent session's pieces page by page and prunes what an earlier // one staged: two tabs doing that at once would write over each other, so it runs only // under the tab lock. Where Web Locks do not exist (a plain http origin), the browser @@ -1632,11 +1657,18 @@ if (BROWSER) { unwritableMarks.clear() if (email) setTimeout(runFlush, STARTUP_DELAY_MS) }) - // A tab going to the background may not come back: carry what it has now. if (typeof document !== 'undefined') { + // A tab going to the background may not come back: carry what it has now. document.addEventListener('visibilitychange', () => { if (document.visibilityState === 'hidden') runFlush() }) + if (!inAppFrame) { + window.addEventListener('message', (e) => { + if (e.origin === window.location.origin && e.data?.type === FRAME_FLUSH_MESSAGE) { + scheduleFlush() + } + }) + } } } diff --git a/frontend/src/lib/userDraft.svelte.ts b/frontend/src/lib/userDraft.svelte.ts index e28caeb895..0c5366120d 100644 --- a/frontend/src/lib/userDraft.svelte.ts +++ b/frontend/src/lib/userDraft.svelte.ts @@ -930,13 +930,25 @@ function releaseEntry(mk: string): void { */ function makeDetachedHandle(defaultValue?: V): UserDraftHandle { let val = $state(snapshotDraftValue(defaultValue)) - return { - get draft(): V | undefined { - return val - }, - set draft(value: V | undefined) { - val = value - } + return new Handle( + () => val, + (value) => (val = value) + ) +} + +// A class instance, not an object literal: `$state` proxies plain objects, so a literal +// stored in `useMany`'s `handles` would reach callers as a new proxy on every reconcile, +// and the reconcile's `===` no-op check would compare a proxy with its raw handle. +class Handle implements UserDraftHandle { + constructor( + private read: () => V | undefined, + private write: (value: V | undefined) => void + ) {} + get draft(): V | undefined { + return this.read() + } + set draft(value: V | undefined) { + this.write(value) } } @@ -950,15 +962,13 @@ function makeHandle( // throwing — the consumer should already be torn down by then. const mk = mapKey(workspace, itemKind, path) const stateOf = (): DraftState | undefined => entries.get(mk)?.state - return { - get draft(): V | undefined { - return stateOf()?.val as V | undefined - }, - set draft(value: V | undefined) { + return new Handle( + () => stateOf()?.val as V | undefined, + (value) => { const state = stateOf() if (state) state.val = value } - } + ) } /** Test-only: clear all in-memory entries. */