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 <noreply@anthropic.com>

* fix: keep draft handles unproxied so useMany's no-op check matches

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: have a preview frame ask its page to flush the marks it writes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Guilhem
2026-11-25 23:15:28 +01:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 3efe1e97a7
commit 53ebb94ddb
2 changed files with 57 additions and 15 deletions
@@ -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()
}
})
}
}
}
+23 -13
View File
@@ -930,13 +930,25 @@ function releaseEntry(mk: string): void {
*/
function makeDetachedHandle<V>(defaultValue?: V): UserDraftHandle<V> {
let val = $state<V | undefined>(snapshotDraftValue(defaultValue))
return {
get draft(): V | undefined {
return val
},
set draft(value: V | undefined) {
val = value
}
return new Handle<V>(
() => 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<V> implements UserDraftHandle<V> {
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<V>(
// throwing — the consumer should already be torn down by then.
const mk = mapKey(workspace, itemKind, path)
const stateOf = (): DraftState<unknown> | undefined => entries.get(mk)?.state
return {
get draft(): V | undefined {
return stateOf()?.val as V | undefined
},
set draft(value: V | undefined) {
return new Handle<V>(
() => stateOf()?.val as V | undefined,
(value) => {
const state = stateOf()
if (state) state.val = value
}
}
)
}
/** Test-only: clear all in-memory entries. */