Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(desktop-app): report an open that mounted and then failed as a failure (R1-M1)

Diego Zacariasf4988b1e3ac2
diff --git a/desktop-app/src/__tests__/appService.sandboxUiLifecycle.test.ts b/desktop-app/src/__tests__/appService.sandboxUiLifecycle.test.ts
index dc7908347..e42d167bf 100644
--- a/desktop-app/src/__tests__/appService.sandboxUiLifecycle.test.ts
+++ b/desktop-app/src/__tests__/appService.sandboxUiLifecycle.test.ts
@@ -1067,6 +1067,33 @@ describe('AppService sandbox-ui view release', () => {
expectNoOrphanView(parentWindow)
})
+ it('releases a view whose open failed after mounting it as open_failed, before rejecting', async () => {
+ const { service, parentWindow, launch } = await setupReleaseRecorder('/r/asked')
+ const order: Array<[string, unknown]> = []
+ const args = launch('first-app', {
+ onReleased: event => {
+ order.push(['released', event])
+ },
+ })
+ parentWindow.contentView.addChildView.mockImplementationOnce(() => {
+ throw new Error('attach failed')
+ })
+
+ await service.openSandboxUi(args).catch((error: Error) => {
+ order.push(['rejected', error.message])
+ })
+
+ expect(order).toEqual([
+ [
+ 'released',
+ { launchId: args.launchId, reason: 'open_failed', location: { routePath: '/r/asked' } },
+ ],
+ ['rejected', 'attach failed'],
+ ])
+ expect(getActiveSandboxUi()).toBeNull()
+ expectNoOrphanView(parentWindow)
+ })
+
it('a close naming another launch leaves the live view alone', async () => {
const { service, parentWindow, released, launch } = await setupReleaseRecorder()
const args = launch('first-app')
diff --git a/desktop-app/src/__tests__/embedOwnership.property.test.ts b/desktop-app/src/__tests__/embedOwnership.property.test.ts
index b352d4dc7..a15d36d8a 100644
--- a/desktop-app/src/__tests__/embedOwnership.property.test.ts
+++ b/desktop-app/src/__tests__/embedOwnership.property.test.ts
@@ -18,6 +18,7 @@ import type {
EmbedOwnershipMachine,
EmbedRefreshErrorEvent,
} from '../../ui/src/lib/embedOwnershipMachine.types'
+import { embedFailureMessage, embedViewState } from '../../ui/src/lib/embedOwnershipView'
import {
activeWorkspaceTab,
closeWorkspaceTab,
@@ -198,6 +199,8 @@ type Reality =
/** The next view dies (crash, session change) inside its mount, before its open settles. */
| 'crashDuringMount'
| 'sessionDuringMount'
+ /** The next view mounts, then attaching it to the window throws: its open fails. */
+ | 'attachFailsDuringMount'
| 'crash'
| 'title'
| 'navigate'
@@ -278,6 +281,8 @@ class World {
crashNextLoad = false
/** The session changes the moment the next view starts loading. */
sessionChangeNextLoad = false
+ /** Attaching the next view to the window throws, after main made it the active view. */
+ attachFailsNextMount = false
tabs: WorkspaceTabsState
machine: EmbedOwnershipMachine
generation = 0
@@ -364,6 +369,17 @@ class World {
onLoad: (webContents, url) => world.onLoad(webContents, url),
})
world = new World(options, main)
+ const attach = main.parent.contentView.addChildView.getMockImplementation() as (
+ view: unknown
+ ) => void
+ main.parent.contentView.addChildView.mockImplementation((view: unknown) => {
+ if (world.attachFailsNextMount) {
+ world.attachFailsNextMount = false
+ world.log.push('reality attach fails inside the mount')
+ throw new Error('attach failed')
+ }
+ attach(view)
+ })
if (!options.lateMeasurer) world.registerMeasurer()
world.machine.projected(world.activeAppTabId())
return world
@@ -642,6 +658,10 @@ class World {
this.sessionChangeNextLoad = true
return true
}
+ if (kind === 'attachFailsDuringMount') {
+ this.attachFailsNextMount = true
+ return true
+ }
const active = getActiveSandboxUi()
const view = active ? this.viewOf.get(active.launchId) : undefined
if (!active || !view || view.webContents.destroyed) return false
@@ -1150,6 +1170,7 @@ const realityArb = fc.array(
fc.constantFrom<Reality>(
'crashDuringMount',
'sessionDuringMount',
+ 'attachFailsDuringMount',
'crash',
'title',
'navigate',
@@ -1573,6 +1594,74 @@ describe('embed ownership named cases (real main process)', () => {
assertClean(world)
})
+ it('K26: an open that mounts and then fails shows its error and fails its launch', async () => {
+ const { sink, world } = await manualWorld()
+ world.attachFailsNextMount = true
+
+ const launchId = world.selectTab(TAB_ALPHA)!
+ await sink.runAll()
+
+ const snapshot = world.machine.getSnapshot()
+ const view = embedViewState(snapshot)
+ expect(view.kind).toBe('failed')
+ expect(view.kind === 'failed' && embedFailureMessage(view.failure)).toBe('attach failed')
+ expect(world.outcomes.get(launchId)).toBe('failed')
+ // main releases the view it mounted before rejecting the open, and the
+ // route it was asked for still reaches the tab.
+ expect(world.releases).toEqual([
+ { launchId, reason: 'open_failed', location: { routePath: `/r/${launchId}` } },
+ ])
+ expect(world.tabs.tabs.find(tab => tab.id === TAB_ALPHA)?.app?.savedRoutePath).toBe(
+ `/r/${launchId}`
+ )
+ expect(snapshot.owner).toBeNull()
+ expectNoView(world)
+ world.checkDrained()
+ assertClean(world)
+ })
+
+ it('K26: a relaunch between the release and the rejection waits for the failed open, then opens', async () => {
+ const { sink, world } = await manualWorld()
+ world.attachFailsNextMount = true
+
+ const failed = world.selectTab(TAB_ALPHA)!
+ // Main's release of the failed view reaches the renderer; the open's
+ // rejection is still on the wire behind it.
+ await sink.runAll(isLabel('ipc→renderer reply sandboxUi:open', short(failed)))
+ expect(world.releases.map(release => release.reason)).toEqual(['open_failed'])
+ expect(world.machine.getSnapshot().views.get(failed)?.phase).toBe('releasedOpening')
+ const relaunched = world.relaunch()!
+ // Main already let the failed view go: nothing is sent to close it, and the
+ // relaunch measures only once the failed open has settled.
+ expect(closesSent(world)).toEqual([])
+ expect(world.machine.getSnapshot().views.get(relaunched)?.phase).toBe('waiting')
+ await sink.runAll()
+
+ expect(world.outcomes.get(failed)).toBe('abandoned')
+ expect(world.outcomes.get(relaunched)).toBe('mounted')
+ expect(getActiveSandboxUi()?.launchId).toBe(relaunched)
+ expect(closesSent(world)).toEqual([])
+ world.checkDrained()
+ assertClean(world)
+ })
+
+ it('K26: coming back to the tab before the failed open rejects launches it again', async () => {
+ const { sink, world } = await manualWorld()
+ world.attachFailsNextMount = true
+
+ const failed = world.selectTab(TAB_ALPHA)!
+ await sink.runAll(isLabel('ipc→renderer reply sandboxUi:open', short(failed)))
+ world.selectTab(TAB_BETA)
+ const back = world.selectTab(TAB_ALPHA)!
+ await sink.runAll()
+
+ expect(world.outcomes.get(failed)).toBe('abandoned')
+ expect(world.outcomes.get(back)).toBe('mounted')
+ expect(getActiveSandboxUi()?.launchId).toBe(back)
+ world.checkDrained()
+ assertClean(world)
+ })
+
it('K6: back to apps closes the view but keeps its tab; a pick from the grid opens its own tab', async () => {
const { sink, world } = await manualWorld()
const first = await liveOn(world, sink, TAB_ALPHA)
diff --git a/desktop-app/src/appService.ts b/desktop-app/src/appService.ts
index d54abd748..b93c16d60 100644
--- a/desktop-app/src/appService.ts
+++ b/desktop-app/src/appService.ts
@@ -5947,8 +5947,10 @@ export class AppService {
await this.mintAndMountSandboxUiView(args, recipeNs, recipeName, sessionEpoch)
} catch (error) {
// Only this open's own view, never one another launch owns; its release
- // goes out before the rejection.
- const reason = this.sandboxUiSessionEpoch === sessionEpoch ? 'closed' : 'session'
+ // goes out before the rejection. `open_failed` tells the renderer that
+ // rejection is on its way, so it reports the failure instead of an
+ // abandoned launch; a session that ended meanwhile is the real cause.
+ const reason = this.sandboxUiSessionEpoch === sessionEpoch ? 'open_failed' : 'session'
await this.teardownSandboxUiView(reason, args.launchId)
throw error
}
diff --git a/desktop-app/src/types.ts b/desktop-app/src/types.ts
index bce0411b0..f35f22391 100644
--- a/desktop-app/src/types.ts
+++ b/desktop-app/src/types.ts
@@ -34,11 +34,14 @@ export type { SandboxUiDeepLinkEnvelope } from '@clerum/desktop-app-links'
/**
* Why a sandbox-ui view died. `crashed` never carries a location: handing the
- * route back would relaunch straight into whatever crashed it.
+ * route back would relaunch straight into whatever crashed it. `open_failed`
+ * is a view torn down by the open that mounted it, which then rejects: the
+ * failure is that rejection, so the renderer must wait for it.
*/
export type SandboxUiReleaseReason =
| 'replaced'
| 'closed'
+ | 'open_failed'
| 'crashed'
| 'parent_closed'
| 'session'
diff --git a/desktop-app/ui/src/__tests__/App.deepLinks.test.tsx b/desktop-app/ui/src/__tests__/App.deepLinks.test.tsx
index 3199825f7..8e2d42ffd 100644
--- a/desktop-app/ui/src/__tests__/App.deepLinks.test.tsx
+++ b/desktop-app/ui/src/__tests__/App.deepLinks.test.tsx
@@ -1544,6 +1544,54 @@ describe('App deep-link orchestration', () => {
expect(ensureTeamContext).toHaveBeenCalledTimes(1)
})
+ it('defers a cross-team link whose open mounts and then fails, with its error', async () => {
+ vi.useFakeTimers({ shouldAdvanceTime: true })
+ let liveTeamId = 'team-a'
+ const ensureTeamContext = vi.fn(async ({ teamId }: { teamId?: string }) => {
+ if (!teamId || teamId === liveTeamId) return false
+ liveTeamId = teamId
+ return true
+ })
+ currentController = makeController({
+ initialExperienceLoading: false,
+ handleEnsureTeamContext: ensureTeamContext,
+ getCurrentTeamId: () => liveTeamId,
+ })
+ listApps.mockResolvedValue({
+ apps: [
+ {
+ appRef: 'ns/app',
+ title: 'Linked App',
+ defaultPath: '/',
+ ready: true,
+ phase: 'active',
+ },
+ ],
+ })
+ render(<App />)
+ await waitFor(() => expect(emitDeepLink).not.toBeNull())
+
+ act(() => {
+ emitDeepLink?.({ id: 1, appRef: 'ns/app', teamId: 'team-b' })
+ })
+ main.failNextOpenAfterMount(new Error('attach failed'))
+ await confirmPendingAppLink()
+ await waitForAppOpen()
+
+ await waitFor(() => {
+ expect(currentController.pushToast).toHaveBeenCalledWith('attach failed', 'error')
+ })
+ await act(async () => {
+ await main.settle()
+ })
+ expect(main.reported('sandboxUi:released')).toEqual([
+ expect.objectContaining({ reason: 'open_failed' }),
+ ])
+ expect(acknowledgeDeepLink).not.toHaveBeenCalled()
+ expect(liveTeamId).toBe('team-b')
+ expect(ensureTeamContext).toHaveBeenCalledTimes(1)
+ })
+
it('retries a failed cross-team native mount without treating its own restore as a user switch', async () => {
vi.useFakeTimers({ shouldAdvanceTime: true })
let liveTeamId = 'team-a'
diff --git a/desktop-app/ui/src/__tests__/sandboxUiMainFake.contract.test.ts b/desktop-app/ui/src/__tests__/sandboxUiMainFake.contract.test.ts
index 4e840897b..28dea626d 100644
--- a/desktop-app/ui/src/__tests__/sandboxUiMainFake.contract.test.ts
+++ b/desktop-app/ui/src/__tests__/sandboxUiMainFake.contract.test.ts
@@ -134,7 +134,7 @@ describe('the App suites’ sandbox-ui bridge delivers what main produces, in or
await open(A)
await main.settle()
- expect(seen).toEqual([`released ${A} closed {}`, `open ${A} failed: attach failed`])
+ expect(seen).toEqual([`released ${A} open_failed {}`, `open ${A} failed: attach failed`])
expect(main.live()).toBeNull()
})
diff --git a/desktop-app/ui/src/lib/__tests__/embedOwnership.table.test.ts b/desktop-app/ui/src/lib/__tests__/embedOwnership.table.test.ts
index a23bfa850..7e81706a1 100644
--- a/desktop-app/ui/src/lib/__tests__/embedOwnership.table.test.ts
+++ b/desktop-app/ui/src/lib/__tests__/embedOwnership.table.test.ts
@@ -96,6 +96,8 @@ const mountedSeq = [...openingSeq, ev.mounted(A)]
const closeRequestedSeq = [...mountedSeq, ev.backToApps()]
/** B is the owner of T1, waiting for A (closeRequested). */
const waitingSeq = [...mountedSeq, ev.relaunch(LB)]
+/** A is the owner of T1; main released its view and has yet to reject its open. */
+const releasedOpeningSeq = [...openingSeq, ev.released(A, 'open_failed', { routePath: '/r' })]
const phaseOf = (s: EmbedOwnershipState, id: LaunchId): ViewPhase | undefined =>
s.views.get(id)?.phase
@@ -117,6 +119,9 @@ describe('fixtures reach the phase they claim', () => {
const w = stateOf(waitingSeq)
expect(phaseOf(w, B)).toBe('waiting')
expect(w.owner).toBe(B)
+ const ro = stateOf(releasedOpeningSeq)
+ expect(phaseOf(ro, A)).toBe('releasedOpening')
+ expect(ro.owner).toBe(A)
})
})
@@ -452,6 +457,120 @@ describe('decision table rows', () => {
expectNoop(out.state, ev.openFailed(A))
})
+ it('R1c: the opening owner released because its open failed keeps the launch for its rejection', () => {
+ const out = reduceEmbedOwnership(
+ stateOf(openingSeq),
+ ev.released(A, 'open_failed', { routePath: '/r' })
+ )
+ expect(out.state.owner).toBe(A)
+ expect(phaseOf(out.state, A)).toBe('releasedOpening')
+ expect(out.state.failure).toBeNull()
+ expect(out.state.lastRoute.get(T1)).toEqual({ routePath: '/r' })
+ expect(out.effects).toEqual([
+ { type: 'persistRoute', tabId: T1, route: { action: 'set', routePath: '/r' } },
+ ])
+ })
+
+ it('R1c: the rejection that follows fails the launch with its message', () => {
+ const out = reduceEmbedOwnership(stateOf(releasedOpeningSeq), ev.openFailed(A, 'attach failed'))
+ expect(out.state.owner).toBeNull()
+ expect(out.state.views.size).toBe(0)
+ expect(out.state.failure).toEqual({
+ tabId: T1,
+ launchId: A,
+ kind: 'open',
+ message: 'attach failed',
+ })
+ expect(out.effects).toEqual([{ type: 'launchSettled', launchId: A, outcome: 'failed' }])
+ })
+
+ it('R1c: a rejection that arrives before the release fails the launch; the release is late', () => {
+ const s = stateOf([...openingSeq, ev.openFailed(A, 'attach failed')])
+ expect(s.failure).toMatchObject({ launchId: A, message: 'attach failed' })
+ expectNoop(s, ev.released(A, 'open_failed', { routePath: '/r' }))
+ })
+
+ it('R1c: a second release of the released view changes nothing', () => {
+ const s = stateOf(releasedOpeningSeq)
+ expectNoop(s, ev.released(A, 'open_failed', { routePath: '/r' }))
+ expectNoop(s, ev.released(A, 'crashed', null))
+ })
+
+ it('R1c: a relaunch before the rejection closes nothing and waits for it', () => {
+ const s = stateOf([...releasedOpeningSeq, ev.relaunch(LB)])
+ expect(s.owner).toBe(B)
+ expect(phaseOf(s, A)).toBe('releasedOpening')
+ expect(phaseOf(s, B)).toBe('waiting')
+ const out = reduceEmbedOwnership(s, ev.openFailed(A))
+ expect(out.state.failure).toBeNull()
+ expect(phaseOf(out.state, B)).toBe('measuring')
+ expect(out.effects).toEqual([
+ { type: 'measure', launchId: B },
+ { type: 'launchSettled', launchId: A, outcome: 'abandoned' },
+ ])
+ expect(
+ run([...releasedOpeningSeq, ev.relaunch(LB), ev.openFailed(A)]).effects
+ ).not.toContainEqual(expect.objectContaining({ type: 'close' }))
+ })
+
+ it('R1c: leaving the tab before the rejection closes nothing and abandons the launch', () => {
+ const s = stateOf([...releasedOpeningSeq, ev.projected(T2)])
+ expect(s.owner).toBeNull()
+ expect(phaseOf(s, A)).toBe('releasedOpening')
+ const out = reduceEmbedOwnership(s, ev.openFailed(A))
+ expect(out.state.views.size).toBe(0)
+ expect(out.state.failure).toBeNull()
+ expect(out.effects).toEqual([{ type: 'launchSettled', launchId: A, outcome: 'abandoned' }])
+ expect(run([...releasedOpeningSeq, ev.projected(T2)]).effects).not.toContainEqual(
+ expect.objectContaining({ type: 'close' })
+ )
+ })
+
+ it('R1c: a handoff before the rejection waits for it', () => {
+ const s = stateOf([...releasedOpeningSeq, ev.handoff(7)])
+ expect([...s.handoffs[0]!.waitFor]).toEqual([A])
+ const out = reduceEmbedOwnership(s, ev.openFailed(A))
+ expect(out.state.handoffs).toEqual([])
+ expect(out.effects).toContainEqual({ type: 'handoffReady', requestId: 7 })
+ })
+
+ it('R1c: a re-click before the rejection changes nothing', () => {
+ const s = stateOf(releasedOpeningSeq)
+ const out = reduceEmbedOwnership(s, ev.intent(launch(C, T1)))
+ expect(out.state).toBe(s)
+ expect(out.effects).toEqual([{ type: 'launchSettled', launchId: C, outcome: 'abandoned' }])
+ })
+
+ it('R1c: coming back to the tab before the rejection launches again once it lands', () => {
+ const s = stateOf([...releasedOpeningSeq, ev.projected(T2), ev.projected(T1), ev.intent(LB)])
+ expect(s.owner).toBe(B)
+ expect(phaseOf(s, B)).toBe('waiting')
+ const out = reduceEmbedOwnership(s, ev.openFailed(A))
+ expect(out.state.owner).toBe(B)
+ expect(phaseOf(out.state, B)).toBe('measuring')
+ expect(out.effects).toEqual([
+ { type: 'measure', launchId: B },
+ { type: 'launchSettled', launchId: A, outcome: 'abandoned' },
+ ])
+ })
+
+ it('R1c: a mount confirmation for the released view drops it as abandoned', () => {
+ const out = reduceEmbedOwnership(stateOf(releasedOpeningSeq), ev.mounted(A))
+ expect(out.state.owner).toBeNull()
+ expect(out.state.views.size).toBe(0)
+ expect(out.effects).toEqual([{ type: 'launchSettled', launchId: A, outcome: 'abandoned' }])
+ })
+
+ it('R1c: a closing view released because its open failed is just gone', () => {
+ const s = stateOf([...openingSeq, ev.backToApps()])
+ const out = reduceEmbedOwnership(s, ev.released(A, 'open_failed', { routePath: '/r' }))
+ expect(out.state.views.size).toBe(0)
+ expect(out.state.failure).toBeNull()
+ expect(out.effects).toEqual([
+ { type: 'persistRoute', tabId: T1, route: { action: 'set', routePath: '/r' } },
+ ])
+ })
+
it('R1b: a crash of a closing view is not a crash failure', () => {
const out = reduceEmbedOwnership(stateOf(closeRequestedSeq), ev.released(A, 'crashed', null))
expect(out.state.failure).toBeNull()
@@ -508,6 +627,7 @@ describe('totality: every unlisted (event, condition) pair is a no-op by referen
{ name: 'mounted owner', seq: mountedSeq, target: A },
{ name: 'closeRequested', seq: closeRequestedSeq, target: A },
{ name: 'closeRequested behind waiting owner', seq: waitingSeq, target: A },
+ { name: 'releasedOpening owner', seq: releasedOpeningSeq, target: A },
]
const targetedEvents: Array<{ key: string; make: (id: LaunchId) => EmbedEvent }> = [
@@ -518,6 +638,10 @@ describe('totality: every unlisted (event, condition) pair is a no-op by referen
{ key: 'closeFailed', make: id => ev.closeFailed(id) },
{ key: 'released(replaced,loc)', make: id => ev.released(id, 'replaced', { routePath: '/q' }) },
{ key: 'released(crashed,null)', make: id => ev.released(id, 'crashed', null) },
+ {
+ key: 'released(open_failed,loc)',
+ make: id => ev.released(id, 'open_failed', { routePath: '/q' }),
+ },
{ key: 'titleChanged', make: id => ev.title(id) },
]
@@ -530,6 +654,7 @@ describe('totality: every unlisted (event, condition) pair is a no-op by referen
'openSettled(failed)',
'released(replaced,loc)',
'released(crashed,null)',
+ 'released(open_failed,loc)',
'titleChanged',
],
'measuring owner': [
@@ -538,6 +663,7 @@ describe('totality: every unlisted (event, condition) pair is a no-op by referen
'openSettled(failed)',
'released(replaced,loc)',
'released(crashed,null)',
+ 'released(open_failed,loc)',
'titleChanged',
],
'opening owner': [
@@ -545,12 +671,14 @@ describe('totality: every unlisted (event, condition) pair is a no-op by referen
'openSettled(failed)',
'released(replaced,loc)',
'released(crashed,null)',
+ 'released(open_failed,loc)',
'titleChanged',
],
'mounted owner': [
'openSettled(failed)',
'released(replaced,loc)',
'released(crashed,null)',
+ 'released(open_failed,loc)',
'titleChanged',
],
closeRequested: [
@@ -558,13 +686,16 @@ describe('totality: every unlisted (event, condition) pair is a no-op by referen
'closeFailed',
'released(replaced,loc)',
'released(crashed,null)',
+ 'released(open_failed,loc)',
],
'closeRequested behind waiting owner': [
'openSettled(failed)',
'closeFailed',
'released(replaced,loc)',
'released(crashed,null)',
+ 'released(open_failed,loc)',
],
+ 'releasedOpening owner': ['openSettled(mounted)', 'openSettled(failed)', 'titleChanged'],
}
for (const fx of fixtures) {
@@ -600,6 +731,8 @@ describe('totality: every unlisted (event, condition) pair is a no-op by referen
[openingSeq, ev.openFailed(A)],
[closeRequestedSeq, ev.released(A, 'closed', { routePath: '/x' })],
[mountedSeq, ev.released(A, 'crashed', null)],
+ [openingSeq, ev.released(A, 'open_failed', { routePath: '/r' })],
+ [releasedOpeningSeq, ev.openFailed(A)],
[closeRequestedSeq, ev.closeFailed(A)],
[measuringSeq, ev.measured(A)],
[measuringSeq, ev.measured(A, null)],
@@ -803,7 +936,12 @@ describe('effect ordering', () => {
// ---- R-settle across all rows ----------------------------------------------------
-const PENDING: ReadonlySet<ViewPhase> = new Set(['waiting', 'measuring', 'opening'])
+const PENDING: ReadonlySet<ViewPhase> = new Set([
+ 'waiting',
+ 'measuring',
+ 'opening',
+ 'releasedOpening',
+])
const pendingIds = (s: EmbedOwnershipState) =>
new Set([...s.views.values()].filter(v => PENDING.has(v.phase)).map(v => v.launchId))
@@ -875,7 +1013,8 @@ function eventSequence() {
'crashed' as const,
'parent_closed' as const,
'session' as const,
- 'renderer_replaced' as const
+ 'renderer_replaced' as const,
+ 'open_failed' as const
)
const ev1: fc.Arbitrary<EmbedEvent> = fc.oneof(
fc.option(tab, { nil: null }).map(ev.projected),
@@ -923,6 +1062,18 @@ describe('R-settle walks every row', () => {
R1a: [...mountedSeq, ev.released(A, 'crashed', null)],
'R1a (opening)': [...openingSeq, ev.released(A, 'crashed', null)],
R1b: [...openingSeq, ev.released(A, 'session', null)],
+ R1c: [...releasedOpeningSeq, ev.openFailed(A)],
+ 'R1c (relaunch)': [...releasedOpeningSeq, ev.relaunch(LB), ev.openFailed(A), ev.measured(B)],
+ 'R1c (left)': [...releasedOpeningSeq, ev.projected(T2), ev.openFailed(A)],
+ 'R1c (back)': [
+ ...releasedOpeningSeq,
+ ev.projected(T2),
+ ev.projected(T1),
+ ev.intent(LB),
+ ev.openFailed(A),
+ ev.measured(B),
+ ],
+ 'R1c (mounted)': [...releasedOpeningSeq, ev.mounted(A)],
R2: [...closeRequestedSeq, ev.released(A), ev.released(A)],
X1: [...closeRequestedSeq, ev.closeFailed(A)],
T1: [...mountedSeq, ev.title(A)],
diff --git a/desktop-app/ui/src/lib/embedOwnership.ts b/desktop-app/ui/src/lib/embedOwnership.ts
index 064efcd52..fbd2c6bab 100644
--- a/desktop-app/ui/src/lib/embedOwnership.ts
+++ b/desktop-app/ui/src/lib/embedOwnership.ts
@@ -26,7 +26,7 @@ import type {
*
* Every transition is followed by the level rules, in this order:
* sweep — a record that is not the owner is dropped if main never saw it, or
- * CAS-closed once if it did;
+ * CAS-closed once if main still holds its view;
* advance — an owner waits until no other record of its tab is left, so it
* opens at the route its predecessor leaves behind;
* handoff — a handoff completes when none of the views it waits for remain;
@@ -52,7 +52,10 @@ export function createEmbedOwnershipState(): EmbedOwnershipState {
}
const isPending = (v: ViewRecord): boolean =>
- v.phase === 'waiting' || v.phase === 'measuring' || v.phase === 'opening'
+ v.phase === 'waiting' ||
+ v.phase === 'measuring' ||
+ v.phase === 'opening' ||
+ v.phase === 'releasedOpening'
function pendingIds(state: EmbedOwnershipState): LaunchId[] {
const ids: LaunchId[] = []
@@ -91,10 +94,16 @@ function openRoutePath(state: EmbedOwnershipState, record: ViewRecord): string |
return record.route.routePath
}
-/** Nothing of the launch's tab is still alive in a non-closing phase. */
+/**
+ * Nothing of the launch's tab is still alive in a non-closing phase. A released
+ * view that lost ownership only waits for its rejection, so it is as good as
+ * closing; while it is still the owner, its failure is about to show.
+ */
function tabIsFree(state: EmbedOwnershipState, launch: Launch): boolean {
for (const v of state.views.values()) {
- if (v.tabId === launch.tabId && v.phase !== 'closeRequested') return false
+ if (v.tabId !== launch.tabId || v.phase === 'closeRequested') continue
+ if (v.phase === 'releasedOpening' && v.launchId !== state.owner) continue
+ return false
}
return true
}
@@ -181,6 +190,15 @@ function applyRow(state: EmbedOwnershipState, event: EmbedEvent): RowResult {
const record = state.views.get(event.launchId)
if (!record) return unchanged
if (event.outcome === 'mounted') {
+ // Main never confirms a view it already released; should it, the launch
+ // has no view left to own.
+ if (record.phase === 'releasedOpening') {
+ const dropped = withoutView(state, record.launchId)
+ return {
+ state: record.launchId === state.owner ? { ...dropped, owner: null } : dropped,
+ effects: [],
+ }
+ }
// A launch already being closed keeps waiting for its `released`.
if (record.phase !== 'opening') return unchanged
return {
@@ -211,10 +229,16 @@ function applyRow(state: EmbedOwnershipState, event: EmbedEvent): RowResult {
case 'released': {
const record = state.views.get(event.launchId)
- if (!record) return unchanged
- let next = withoutView(state, record.launchId)
+ // Main releases a view once; the record only waits for its open's rejection.
+ if (!record || record.phase === 'releasedOpening') return unchanged
+ // The owner keeps the launch until the rejection that follows, so the
+ // failure it carries is reported instead of an abandoned launch.
+ const heldForRejection = event.reason === 'open_failed' && record.phase === 'opening'
+ let next = heldForRejection
+ ? withView(state, { ...record, phase: 'releasedOpening' })
+ : withoutView(state, record.launchId)
const wasOwner = record.launchId === state.owner
- if (wasOwner) next = { ...next, owner: null }
+ if (wasOwner && !heldForRejection) next = { ...next, owner: null }
const effects: EmbedEffect[] = []
if (event.location !== null) {
const lastRoute = new Map(next.lastRoute)
@@ -225,7 +249,8 @@ function applyRow(state: EmbedOwnershipState, event: EmbedEvent): RowResult {
}
// A crash of the view the user is waiting on or looking at must leave the tab
// something to show and a way back. Any other release (session end, a view
- // being closed) is not the app's fault and leaves the tab simply unowned.
+ // being closed) is not the app's fault and leaves the tab simply unowned,
+ // except an open that failed, whose rejection reports it.
const crashed =
event.reason === 'crashed' &&
wasOwner &&
@@ -266,6 +291,7 @@ function sweep(state: EmbedOwnershipState, effects: EmbedEffect[]): EmbedOwnersh
next = withView(next, { ...v, phase: 'closeRequested' })
effects.push({ type: 'close', launchId: v.launchId })
}
+ // `releasedOpening` stays until its rejection: main already let the view go.
}
return next
}
diff --git a/desktop-app/ui/src/lib/embedOwnership.types.ts b/desktop-app/ui/src/lib/embedOwnership.types.ts
index 1c633571e..47c0b674b 100644
--- a/desktop-app/ui/src/lib/embedOwnership.types.ts
+++ b/desktop-app/ui/src/lib/embedOwnership.types.ts
@@ -31,6 +31,12 @@ export type ViewPhase =
| 'mounted'
/** CAS close emitted; ends with `released`, a failed `openSettled` or `closeFailed`. */
| 'closeRequested'
+ /**
+ * Main released the view of an open that failed after mounting it; the launch
+ * still waits for that open's rejection, which carries the failure. Nothing
+ * of it is left in main, so it is never closed.
+ */
+ | 'releasedOpening'
export type ViewRecord = Launch & { phase: ViewPhase }