fix(ui): preserve file lifetime fences across passive retargets

A queued file status update can run before a queued lifetime close and replace the target object. Preserve a separate runtime lifetime identity through passive status changes, renew it on every explicit file gesture, and fence lifetime cleanup by that identity. Recovered worktree cleanup retains the newest status target instead of restoring its stale snapshot.

Exercise both callback insertion orders, recovery after a status retarget, and explicit reopening of the same target. The reverse-order regression fails without the fix; all nine passive reconciliation checks and all eighteen foreground/selection checks pass.
This commit is contained in:
Pascal André 2026-11-15 12:07:54 +01:00
parent 17c8cb8954
commit 4c62b5eb1a
No known key found for this signature in database
4 changed files with 36 additions and 12 deletions

View file

@ -9,7 +9,7 @@ Panel toggles, floating drawers, project/right-panel tabs, the session sidebar a
- `stores/session-surface.ts` owns the central foreground projection and activation intents within one tab occurrence (`instanceId`). Its fixed union selects conversation, web, file or Mission. Browser targets bind explicitly to a native session; file targets retain their session/worktree lifetime; schedule and playbook readers can open without a conversation. Duplicate project tabs can consult different readers or the shared conversation. Feature stores retain browser guests/history, file bytes/drafts/editor state and Mission data/selection/tree/disclosures.
- New central readers plug into `session-surface`: add a typed union member and use its explicit gesture or passive path. `activateSessionSurface` requires an admitted intent. `sessionSurfaceGesture` admits synchronous gestures; `navigateMissionProjectView`, file/browser open/close and the `select*Session*` actions use it. Async callers capture one occurrence intent at gesture admission before awaiting, pass that same intent through every deferred adoption, and release it in `finally`. Adoption checks the intent alongside view and ownership fences. Never recapture authority after an await or add reciprocal cross-feature visibility effects. Browser Go, Back/Forward and Refresh invalidate at gesture admission; committed location/history changes of the currently displayed web session also fence pending activation, including same-URL history entries. Hidden previews retain location/history updates independently. Removed occurrences release their foreground/resources; unavailable occurrences retain restoration data while pending activation loses authority.
- The version-one `tab-view-state` envelope supplies restored browser modes and Mission reader targets to the foreground projection. Live updates preserve untouched branch and target identities; capture/restore applies validation and budgets. File targets, browsed worktrees and Files filters stay runtime-only. Restorable Mission selection/tree/disclosure state belongs to the occurrence envelope; completion acknowledgements remain per project within the window.
- Passive setters (`updateMissionProjectView`, session `setActive*`/hydration setters, `retargetMissionReader` and file lifetime/status reconciliation) use `reconcileSessionSurface`. It retains the latest callback per occurrence and reconciliation key while a gesture is pending, then runs it after settlement under its original target/selection fences. Deferred callbacks recompute conditional changes against current inventory and binding; callbacks sharing a key supply the complete reconciliation for that concern. Each deleted session identity retains its own parent/active selection reconciliation; file transitions, worktree absence and status use distinct keys. Reader reconciliation changes only that feature's matching target, preserving the adopted browser/file/conversation. Feature selection and disclosure bookkeeping stays data-only; an explicit gesture supplies its admitted intent to a setter. Browser resource restoration allocates independently of foreground authority. Deferred schedule clicks retain their original occurrence intent through passage discovery and hydration. Guard checks live in `session-surface-reconciliation.test.ts` and `session-surface.test.ts`; full-shell races live in `session-browser-history.test.ts`.
- Passive setters (`updateMissionProjectView`, session `setActive*`/hydration setters, `retargetMissionReader` and file lifetime/status reconciliation) use `reconcileSessionSurface`. It retains the latest callback per occurrence and reconciliation key while a gesture is pending, then runs it after settlement under its original target/selection fences. Deferred callbacks recompute conditional changes against current inventory and binding; callbacks sharing a key supply the complete reconciliation for that concern. Each deleted session identity retains its own parent/active selection reconciliation; file transitions, worktree absence and status use distinct keys. File status retargeting preserves the reader's lifetime identity, and an explicit file gesture renews it, so lifetime cleanup survives passive retargeting in either settlement order. Reader reconciliation changes only that feature's matching target, preserving the adopted browser/file/conversation. Feature selection and disclosure bookkeeping stays data-only; an explicit gesture supplies its admitted intent to a setter. Browser resource restoration allocates independently of foreground authority. Deferred schedule clicks retain their original occurrence intent through passage discovery and hydration. Guard checks live in `session-surface-reconciliation.test.ts` and `session-surface.test.ts`; full-shell races live in `session-browser-history.test.ts`.
- The selected right-panel tab and Files mode use the occurrence's restorable view envelope. The layout keys supply defaults for new occurrences; each mounted occurrence records its own selection and restores it when the panel remounts or the window restores. Panel dimensions, palette, tab order and customization retain their shared preference scope.
- Each right-panel tab owns its conditional body and lazy boundary; switching away during a pending module load leaves the new selection responsive. Only the selected tab's body mounts.
- Conversation selection and transcript scroll/follow state use the occurrence/session identity. Both occurrences receive the same native message and execution events while keeping an older reading position or latest-follow choice independently. File source/rendered choice and editor position use the target/model lifetimes in [FILES_PANEL.md](FILES_PANEL.md); the restore envelope's fields are specified in [architecture.md](architecture.md#native-profiles-windows-and-client-state).

View file

@ -31,6 +31,6 @@ export function closeFilePreview(instanceId: string, intent?: SessionSurfaceInte
}
export function reconcileFilePreviewLifetime(instanceId: string, shouldClose?: () => boolean) {
const target = getFilePreview(instanceId)
if (target) reconcileFileSurface(instanceId, target, () => !shouldClose || shouldClose() ? undefined : target,
if (target) reconcileFileSurface(instanceId, target, () => !shouldClose || shouldClose() ? undefined : getFilePreview(instanceId) ?? undefined,
shouldClose ? "file-worktree" : "file-lifetime")
}

View file

@ -95,8 +95,8 @@ test("deferred Mission reconciliation rechecks recovery and the latest passage",
publishInstanceLifecycleAuthority({ type: "removed", instanceId, folder: "/repo", occurrence: 0 })
})
test("file lifetime recovery is rechecked and status reconciliation cannot discard a pending close", () => {
const instanceId = "passive-file-recovery", ctx = { instanceId, sessionId: "same" }
for (const statusFirst of [false, true]) test(`file lifetime recovery is rechecked and status reconciliation cannot discard a pending close (status first: ${statusFirst})`, () => {
const instanceId = `passive-file-recovery-${statusFirst}`, ctx = { instanceId, sessionId: "same" }
const target = { sessionId: "same", path: "a", slug: "root", directory: "/repo" }
publishInstanceLifecycleAuthority({ type: "opened", instanceId, folder: "/repo", occurrence: 0 })
activateSessionSurface(ctx, { kind: "file", target })
@ -107,10 +107,30 @@ test("file lifetime recovery is rechecked and status reconciliation cannot disca
recovery.release()
assert.equal(filePreviews.getFilePreview(instanceId), target)
const close = captureSessionSurfaceIntent(ctx)
if (statusFirst) filePreviews.reconcileFilePreview(instanceId, target, () => ({ ...target, scope: "staged" }))
filePreviews.reconcileFilePreviewLifetime(instanceId)
filePreviews.reconcileFilePreviewLifetime(instanceId, () => missing)
filePreviews.reconcileFilePreview(instanceId, target, () => ({ ...target, scope: "staged" }))
if (!statusFirst) filePreviews.reconcileFilePreview(instanceId, target, () => ({ ...target, scope: "staged" }))
close.release()
assert.equal(filePreviews.getFilePreview(instanceId), null)
publishInstanceLifecycleAuthority({ type: "removed", instanceId, folder: "/repo", occurrence: 0 })
})
test("file recovery preserves a newer passive status target and a repeated file gesture renews its lifetime", () => {
const instanceId = "file-lifetime-identity", ctx = { instanceId, sessionId: "same" }
const target = { sessionId: "same", path: "a", slug: "root", directory: "/repo" }
const updated = { ...target, scope: "staged" as const }
publishInstanceLifecycleAuthority({ type: "opened", instanceId, folder: "/repo", occurrence: 0 })
activateSessionSurface(ctx, { kind: "file", target })
const recovery = captureSessionSurfaceIntent(ctx)
filePreviews.reconcileFilePreview(instanceId, target, () => updated)
filePreviews.reconcileFilePreviewLifetime(instanceId, () => false)
recovery.release()
assert.equal(filePreviews.getFilePreview(instanceId), updated)
const reopen = captureSessionSurfaceIntent(ctx)
filePreviews.reconcileFilePreviewLifetime(instanceId)
commitSurface(ctx, { kind: "file", target: updated }, reopen)
reopen.release()
assert.equal(filePreviews.getFilePreview(instanceId), updated)
publishInstanceLifecycleAuthority({ type: "removed", instanceId, folder: "/repo", occurrence: 0 })
})

View file

@ -12,7 +12,7 @@ export type CentralSurface =
| { kind: "mission"; target: MissionReaderTarget }
const conversation: CentralSurface = { kind: "conversation" }
const [files, setFiles] = createSignal<ReadonlyMap<string, FilePreviewTarget>>(new Map())
const [files, setFiles] = createSignal<ReadonlyMap<string, { target: FilePreviewTarget; lifetime: object }>>(new Map())
const intents = new Map<string, object>()
const reconciliations = new Map<string, Map<string, () => void>>()
@ -44,13 +44,16 @@ function settle(instanceId: string): void {
}
/** File lifetime/status reconciliation changes only the still-matching runtime target. */
export function reconcileFileSurface(instanceId: string, previous: FilePreviewTarget, update?: FilePreviewTarget | (() => FilePreviewTarget | undefined), key = "file-reader"): void {
export function reconcileFileSurface(instanceId: string, previous: FilePreviewTarget, update?: FilePreviewTarget | (() => FilePreviewTarget | undefined), key: "file-reader" | "file-worktree" | "file-lifetime" = "file-reader"): void {
const lifetime = files().get(instanceId)?.lifetime
if (!lifetime) return
reconcileSessionSurface(instanceId, key, () => {
if (files().get(instanceId) !== previous) return
const current = files().get(instanceId)
if (!current || (key === "file-reader" ? current.target !== previous : current.lifetime !== lifetime)) return
const target = typeof update === "function" ? update() : update
setFiles(current => {
const next = new Map(current)
if (target) next.set(instanceId, target)
if (target) next.set(instanceId, { target, lifetime })
else next.delete(instanceId)
return next
})
@ -62,7 +65,7 @@ export function reconcileFileSurface(instanceId: string, previous: FilePreviewTa
export function getSessionSurface({ instanceId, sessionId }: SurfaceContext): CentralSurface {
const view = getTabViewState(instanceId)
if (view.missions?.reader) return { kind: "mission", target: view.missions.reader }
const file = files().get(instanceId)
const file = files().get(instanceId)?.target
if (file && (sessionId === null || file.sessionId === sessionId)) return { kind: "file", target: file }
if (sessionId && view.previews?.[sessionId]?.mode === "preview") return { kind: "web", sessionId }
return conversation
@ -88,9 +91,10 @@ function writeSessionSurface(ctx: SurfaceContext, surface: CentralSurface): void
...(reader === view.missions?.reader ? {} : { missions: { ...view.missions, reader } }),
})
const target = surface.kind === "file" ? surface.target : undefined
if (files().get(ctx.instanceId) !== target) setFiles(previous => {
setFiles(previous => {
if (!target && !previous.has(ctx.instanceId)) return previous
const next = new Map(previous)
if (target) next.set(ctx.instanceId, target)
if (target) next.set(ctx.instanceId, { target, lifetime: {} })
else next.delete(ctx.instanceId)
return next
})