Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop-app): clear the refresh-failure banner once a resumed refresh succeeds (R1-L1)
Diego Zacariasa0881ad891db
diff --git a/desktop-app/src/__tests__/appService.sandboxUiLifecycle.test.ts b/desktop-app/src/__tests__/appService.sandboxUiLifecycle.test.tsindex 91688b33f..ac2d7a993 100644--- a/desktop-app/src/__tests__/appService.sandboxUiLifecycle.test.ts+++ b/desktop-app/src/__tests__/appService.sandboxUiLifecycle.test.ts@@ -449,10 +449,15 @@ describe('AppService sandbox-ui session against a failing rpc-proxy', () => {// The retry deadline is wall-clock, so here the wall clock moves with the timers.vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'Date'] })const onRefreshError = vi.fn()+ const onRefreshRecovered = vi.fn()const service = makeService()initPluginSdkRuntime({ service, getMainWindow: () => null, userDataDir })const parentWindow = new FakeParentWindow()- await service.openSandboxUi({ ...openArgs('first-app', parentWindow), onRefreshError })+ await service.openSandboxUi({+ ...openArgs('first-app', parentWindow),+ onRefreshError,+ onRefreshRecovered,+ })const embed = getActiveSandboxUi()!mintResponders.set('first-app', () => mintFails(503, 'upstream unavailable'))@@ -464,6 +469,7 @@ describe('AppService sandbox-ui session against a failing rpc-proxy', () => {await vi.advanceTimersByTimeAsync(120_000)expect(onRefreshError).toHaveBeenCalledOnce()expect(onRefreshError.mock.calls[0]![0]).toContain('503')+ expect(onRefreshRecovered).not.toHaveBeenCalled()const mintsWhenExhausted = mintCallsFor('first-app')await vi.advanceTimersByTimeAsync(SANDBOX_UI_REFRESH_INTERVAL_MS)expect(mintCallsFor('first-app')).toBe(mintsWhenExhausted)@@ -478,6 +484,61 @@ describe('AppService sandbox-ui session against a failing rpc-proxy', () => {expect(electronMocks.sessionObject.cookies.set).toHaveBeenCalledWith(expect.objectContaining({ value: 'tok-first-app' }))+ expect(onRefreshRecovered).toHaveBeenCalledOnce()+ })++ it('a recovery whose cookie write lands after logout is not reported', async () => {+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'Date'] })+ const onRefreshError = vi.fn()+ const onRefreshRecovered = vi.fn()+ const service = makeService()+ initPluginSdkRuntime({ service, getMainWindow: () => null, userDataDir })+ const parentWindow = new FakeParentWindow()+ await service.openSandboxUi({+ ...openArgs('first-app', parentWindow),+ onRefreshError,+ onRefreshRecovered,+ })+ const embed = getActiveSandboxUi()!+ mintResponders.set('first-app', () => mintFails(503, 'upstream unavailable'))+ await vi.advanceTimersByTimeAsync(SANDBOX_UI_REFRESH_INTERVAL_MS + 1)+ await vi.advanceTimersByTimeAsync(120_000)+ expect(onRefreshError).toHaveBeenCalledOnce()++ mintResponders.delete('first-app')+ let finishCookieWrite: () => void = () => undefined+ electronMocks.sessionObject.cookies.set.mockClear()+ electronMocks.sessionObject.cookies.set.mockImplementationOnce(+ () =>+ new Promise<void>(resolve => {+ finishCookieWrite = resolve+ })+ )+ const refresh = service.requestSandboxUiRefresh(embed.webContentsId)+ await vi.waitFor(() => expect(electronMocks.sessionObject.cookies.set).toHaveBeenCalledOnce(), {+ timeout: 1_000,+ })+ // Lifecycle work ahead of the session's close keeps the loop running while+ // the cookie write lands under the next session.+ let releaseQueue: () => void = () => undefined+ const queueBusy = (+ service as unknown as { enqueueSandboxUiLifecycle(op: () => Promise<void>): Promise<void> }+ ).enqueueSandboxUiLifecycle(+ () =>+ new Promise<void>(resolve => {+ releaseQueue = resolve+ })+ )+ await service.logout()+ finishCookieWrite()+ await refresh+ expect(getActiveSandboxUi()?.webContentsId).toBe(embed.webContentsId)++ releaseQueue()+ await queueBusy+ await vi.waitFor(() => expect(getActiveSandboxUi()).toBeNull(), { timeout: 1_000 })+ expect(onRefreshRecovered).not.toHaveBeenCalled()+ expect(onRefreshError).toHaveBeenCalledOnce()})})diff --git a/desktop-app/src/__tests__/ipc.sandboxUiLaunchId.test.ts b/desktop-app/src/__tests__/ipc.sandboxUiLaunchId.test.tsindex d9d1b2bab..d058e626b 100644--- a/desktop-app/src/__tests__/ipc.sandboxUiLaunchId.test.ts+++ b/desktop-app/src/__tests__/ipc.sandboxUiLaunchId.test.ts@@ -74,14 +74,17 @@ describe('sandboxUi:open launch id', () => {// The replaced view reports after the relaunch of the same app.first!.onRefreshError?.('refresh failed')+ first!.onRefreshRecovered?.()first!.onTitleChanged?.('Old title')second!.onTitleChanged?.('New title')+ second!.onRefreshRecovered?.()expect(electron.send.mock.calls).toEqual([['sandboxUi:refreshError',{ appRef: 'sandbox-recipes/alpha', launchId: launch1, message: 'refresh failed' },],+ ['sandboxUi:refreshRecovered', { appRef: 'sandbox-recipes/alpha', launchId: launch1 }],['sandboxUi:titleChanged',{ appRef: 'sandbox-recipes/alpha', launchId: launch1, title: 'Old title' },@@ -90,6 +93,7 @@ describe('sandboxUi:open launch id', () => {'sandboxUi:titleChanged',{ appRef: 'sandbox-recipes/alpha', launchId: launch2, title: 'New title' },],+ ['sandboxUi:refreshRecovered', { appRef: 'sandbox-recipes/alpha', launchId: launch2 }],])})diff --git a/desktop-app/src/__tests__/preload.sandboxUiRefreshRecovered.test.ts b/desktop-app/src/__tests__/preload.sandboxUiRefreshRecovered.test.tsnew file mode 100644index 000000000..1df753e0c--- /dev/null+++ b/desktop-app/src/__tests__/preload.sandboxUiRefreshRecovered.test.ts@@ -0,0 +1,52 @@+import { afterEach, describe, expect, it, vi } from 'vitest'++const electron = vi.hoisted(() => ({+ exposeInMainWorld: vi.fn(),+ invoke: vi.fn(),+ off: vi.fn(),+ on: vi.fn(),+}))++vi.mock('electron', () => ({+ contextBridge: { exposeInMainWorld: electron.exposeInMainWorld },+ ipcRenderer: {+ invoke: electron.invoke,+ off: electron.off,+ on: electron.on,+ },+ webUtils: { getPathForFile: vi.fn() },+}))++afterEach(() => {+ electron.exposeInMainWorld.mockReset()+ electron.invoke.mockReset()+ electron.off.mockReset()+ electron.on.mockReset()+ vi.resetModules()+})++describe('preload sandboxUi refresh recovery contract', () => {+ it('forwards sandboxUi:refreshRecovered to the subscriber and unsubscribes on dispose', async () => {+ await import('../preload.js')++ const exposed = electron.exposeInMainWorld.mock.calls.find(+ ([name]) => name === 'clerum'+ )?.[1] as {+ sandboxUi: typeof window.clerum.sandboxUi+ }+ expect(exposed).toBeTruthy()++ const callback = vi.fn()+ const dispose = exposed.sandboxUi.onRefreshRecovered(callback)+ const listener = electron.on.mock.calls.find(+ ([channel]) => channel === 'sandboxUi:refreshRecovered'+ )?.[1]+ const event = { appRef: 'app-1', launchId: 'launch-1' }++ listener({}, event)++ expect(callback).toHaveBeenCalledWith(event)+ dispose()+ expect(electron.off).toHaveBeenCalledWith('sandboxUi:refreshRecovered', listener)+ })+})diff --git a/desktop-app/src/__tests__/sandboxUiSessionRefresh.test.ts b/desktop-app/src/__tests__/sandboxUiSessionRefresh.test.tsindex 7ce0c78a3..84ee7f156 100644--- a/desktop-app/src/__tests__/sandboxUiSessionRefresh.test.ts+++ b/desktop-app/src/__tests__/sandboxUiSessionRefresh.test.ts@@ -1,4 +1,5 @@-import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'+import { afterEach, beforeEach, describe, expect, it, onTestFinished, vi } from 'vitest'+import fc from 'fast-check'import {SANDBOX_UI_COOKIE_TTL_MS,SANDBOX_UI_RATE_LIMIT_MS,@@ -90,6 +91,7 @@ function makeArgs(refresh?: SandboxUiRefreshFninstallCookie?: SandboxUiInstallCookieFnonError?: (error: Error) => void+ onRecovered?: () => voidreload?: () => voidparentWindow?: FakeBrowserWindowwebContentsId?: number@@ -112,6 +114,7 @@ function makeArgs(installCookie:overrides.installCookie ?? vi.fn<SandboxUiInstallCookieFn>(async () => undefined),onError: overrides.onError ?? vi.fn(),+ onRecovered: overrides.onRecovered ?? vi.fn(),reload: overrides.reload ?? vi.fn(),now: wallNow,}@@ -748,3 +751,511 @@ describe('concurrent triggers share one mint', () => {expect(refresh).toHaveBeenCalledTimes(1)})})++describe('recovery signal after retries ran out', () => {+ /** From mount to the point where the loop gives up on a mint that keeps failing. */+ async function runOutOfRetries(): Promise<void> {+ await advance(SANDBOX_UI_REFRESH_INTERVAL_MS + 1)+ await advance(100_000)+ }++ it('is never signalled for mints that succeed, nor for a failure retried within the grace period', async () => {+ const refresh = vi.fn<SandboxUiRefreshFn>(mintOk)+ const onError = vi.fn()+ const onRecovered = vi.fn()+ startSandboxUiRefresh(makeArgs({ refresh, onError, onRecovered }))++ await advance(SANDBOX_UI_REFRESH_INTERVAL_MS + 1)+ expect(refresh).toHaveBeenCalledTimes(1)++ refresh.mockImplementationOnce(mintDown)+ await advance(SANDBOX_UI_REFRESH_INTERVAL_MS + 1)+ await advance(1_000)+ expect(refresh).toHaveBeenCalledTimes(3)+ expect(onError).not.toHaveBeenCalled()+ expect(onRecovered).not.toHaveBeenCalled()+ })++ it('is signalled once, after the cookie is installed, when a return resumes the loop and the mint succeeds', async () => {+ const window = new FakeBrowserWindow()+ const log: string[] = []+ const refresh = vi.fn<SandboxUiRefreshFn>(mintDown)+ const installCookie = vi.fn<SandboxUiInstallCookieFn>(async () => {+ log.push('installCookie')+ })+ const onError = vi.fn(() => log.push('onError'))+ const onRecovered = vi.fn(() => log.push('onRecovered'))+ startSandboxUiRefresh(+ makeArgs({ parentWindow: window, refresh, installCookie, onError, onRecovered })+ )+ await runOutOfRetries()+ expect(log).toEqual(['onError'])++ refresh.mockImplementation(mintOk)+ window.emit('focus')+ await flush()+ expect(log).toEqual(['onError', 'installCookie', 'onRecovered'])++ // The next timer mint has nothing left to recover from.+ await advance(SANDBOX_UI_REFRESH_INTERVAL_MS + 1)+ expect(log).toEqual(['onError', 'installCookie', 'onRecovered', 'installCookie'])+ })++ it.each([+ ['a parent window focus', (window: FakeBrowserWindow) => window.emit('focus')],+ ['a host resume', () => notifySandboxUiHostResumed()],+ [+ 'an embed refresh request',+ (_window: FakeBrowserWindow, webContentsId: number) =>+ void handleEmbedRefreshRequest(webContentsId),+ ],+ ])('is signalled when %s resumes the loop and the mint succeeds', async (_name, resume) => {+ const window = new FakeBrowserWindow()+ const refresh = vi.fn<SandboxUiRefreshFn>(mintDown)+ const onRecovered = vi.fn()+ const args = makeArgs({ parentWindow: window, refresh, onRecovered })+ startSandboxUiRefresh(args)+ await runOutOfRetries()+ const callsWhenExhausted = refresh.mock.calls.length++ refresh.mockImplementation(mintOk)+ resume(window, args.webContentsId)+ await flush()+ expect(refresh).toHaveBeenCalledTimes(callsWhenExhausted + 1)+ expect(onRecovered).toHaveBeenCalledTimes(1)+ })++ it('is signalled once for a streak of failures reported twice', async () => {+ const window = new FakeBrowserWindow()+ const refresh = vi.fn<SandboxUiRefreshFn>(mintDown)+ const onError = vi.fn()+ const onRecovered = vi.fn()+ startSandboxUiRefresh(makeArgs({ parentWindow: window, refresh, onError, onRecovered }))+ await runOutOfRetries()++ // The resumed episode fails all the way through as well.+ window.emit('focus')+ await advance(120_000)+ expect(onError).toHaveBeenCalledTimes(2)++ refresh.mockImplementation(mintOk)+ window.emit('focus')+ await flush()+ expect(onRecovered).toHaveBeenCalledTimes(1)+ })++ it('is signalled again for a failure reported after an earlier recovery', async () => {+ const window = new FakeBrowserWindow()+ const refresh = vi.fn<SandboxUiRefreshFn>(mintDown)+ const onError = vi.fn()+ const onRecovered = vi.fn()+ startSandboxUiRefresh(makeArgs({ parentWindow: window, refresh, onError, onRecovered }))+ await runOutOfRetries()+ refresh.mockImplementation(mintOk)+ window.emit('focus')+ await flush()+ expect(onRecovered).toHaveBeenCalledTimes(1)++ refresh.mockImplementation(mintDown)+ await runOutOfRetries()+ expect(onError).toHaveBeenCalledTimes(2)++ refresh.mockImplementation(mintOk)+ window.emit('focus')+ await flush()+ expect(onRecovered).toHaveBeenCalledTimes(2)+ })++ it.each([+ ['the mint', 'refresh'],+ ['the cookie install', 'installCookie'],+ ] as const)(+ 'is not signalled when the loop is cancelled during %s that would recover',+ async (_name, held) => {+ const window = new FakeBrowserWindow()+ let land: () => void = () => undefined+ const hold = <T>(value: T): Promise<T> =>+ new Promise(resolve => {+ land = () => resolve(value)+ })+ const refresh = vi.fn<SandboxUiRefreshFn>(mintDown)+ const installCookie = vi.fn<SandboxUiInstallCookieFn>(async () => undefined)+ const onRecovered = vi.fn()+ startSandboxUiRefresh(makeArgs({ parentWindow: window, refresh, installCookie, onRecovered }))+ await runOutOfRetries()++ if (held === 'refresh') {+ refresh.mockImplementation(() => hold({ setCookie: 'clerum_sandbox_ui_session=X' }))+ } else {+ refresh.mockImplementation(mintOk)+ installCookie.mockImplementation(() => hold(undefined))+ }+ window.emit('focus')+ await flush()+ cancelSandboxUiRefresh()+ land()+ await flush()+ expect(onRecovered).not.toHaveBeenCalled()+ }+ )++ it('a top-level 401 after the retries ran out reloads and then signals the recovery once', async () => {+ const log: string[] = []+ const refresh = vi.fn<SandboxUiRefreshFn>(mintDown)+ const reload = vi.fn(() => log.push('reload'))+ const onRecovered = vi.fn(() => log.push('onRecovered'))+ const args = makeArgs({ refresh, reload, onRecovered })+ startSandboxUiRefresh(args)+ await runOutOfRetries()++ refresh.mockImplementation(mintOk)+ handleSandboxUiUnauthorizedNavigation(args.webContentsId)+ await flush()+ expect(log).toEqual(['reload', 'onRecovered'])++ await advance(SANDBOX_UI_REFRESH_INTERVAL_MS + 1)+ expect(onRecovered).toHaveBeenCalledTimes(1)+ })++ it('a recovery listener that throws leaves the loop armed and rejects nothing', async () => {+ const unhandled: unknown[] = []+ const onUnhandled = (reason: unknown): void => {+ unhandled.push(reason)+ }+ process.on('unhandledRejection', onUnhandled)+ const consoleError = vi.spyOn(console, 'error').mockImplementation(() => undefined)+ try {+ const window = new FakeBrowserWindow()+ const refresh = vi.fn<SandboxUiRefreshFn>(mintDown)+ const onRecovered = vi.fn(() => {+ throw new Error('renderer gone')+ })+ startSandboxUiRefresh(makeArgs({ parentWindow: window, refresh, onRecovered }))+ await runOutOfRetries()+ refresh.mockImplementation(mintOk)+ window.emit('focus')+ await flush()+ expect(onRecovered).toHaveBeenCalledTimes(1)+ const callsAfterRecovery = refresh.mock.calls.length++ await advance(SANDBOX_UI_REFRESH_INTERVAL_MS + 1)+ expect(refresh).toHaveBeenCalledTimes(callsAfterRecovery + 1)+ await new Promise(resolve => setImmediate(resolve))+ expect(unhandled).toEqual([])+ } finally {+ process.off('unhandledRejection', onUnhandled)+ consoleError.mockRestore()+ }+ })+})++/**+ * Model-based check of the error/recovery signals against the real loop. Mints+ * and cookie installs are released by a scheduler, so an event can land while+ * either one is in flight. At rest, the renderer's banner (whether the last+ * signal was an error) must match the last mint that either gave up or+ * succeeded; failures that still had retries left say nothing about it.+ */+describe('recovery signal model', () => {+ type MintResult = 'gaveUp' | 'succeeded'++ type Scheduler = {+ schedule<T>(task: Promise<T>, label: string): Promise<T>+ count(): number+ waitOne(): Promise<void>+ }++ type Harness = {+ window: FakeBrowserWindow+ mintMode: 'ok' | 'fail'+ mints: number+ signals: Array<'error' | 'recovered'>+ results: MintResult[]+ cancelledAtSignal: number | null+ cancelled: boolean+ }++ type Model = Record<string, never>++ abstract class Step implements fc.AsyncCommand<Model, Harness> {+ check(): boolean {+ return true+ }+ abstract run(model: Model, harness: Harness): Promise<void>+ abstract toString(): string+ }++ class MintMode extends Step {+ constructor(readonly mode: 'ok' | 'fail') {+ super()+ }+ async run(_m: Model, h: Harness): Promise<void> {+ h.mintMode = this.mode+ }+ toString(): string {+ return `MintMode(${this.mode})`+ }+ }+ class Advance extends Step {+ constructor(readonly ms: number) {+ super()+ }+ async run(): Promise<void> {+ await advance(this.ms)+ }+ toString(): string {+ return `Advance(${this.ms})`+ }+ }+ /** Time passes with the network answering promptly: every mint lands within a second. */+ class Elapse extends Step {+ constructor(+ readonly ms: number,+ private readonly scheduler: () => Scheduler+ ) {+ super()+ }+ async run(): Promise<void> {+ for (let elapsed = 0; elapsed < this.ms; elapsed += 1_000) {+ await advance(1_000)+ await settle(this.scheduler())+ }+ }+ toString(): string {+ return `Elapse(${this.ms})`+ }+ }+ class Focus extends Step {+ async run(_m: Model, h: Harness): Promise<void> {+ h.window.emit('focus')+ await flush()+ }+ toString(): string {+ return 'Focus'+ }+ }+ class HostResumed extends Step {+ async run(): Promise<void> {+ notifySandboxUiHostResumed()+ await flush()+ }+ toString(): string {+ return 'HostResumed'+ }+ }+ class EmbedRequest extends Step {+ async run(): Promise<void> {+ void handleEmbedRefreshRequest(MODEL_SENDER).catch(() => undefined)+ await flush()+ }+ toString(): string {+ return 'EmbedRequest'+ }+ }+ class Unauthorized extends Step {+ async run(): Promise<void> {+ handleSandboxUiUnauthorizedNavigation(MODEL_SENDER)+ await flush()+ }+ toString(): string {+ return 'Unauthorized'+ }+ }+ class Cancel extends Step {+ async run(_m: Model, h: Harness): Promise<void> {+ if (!h.cancelled) h.cancelledAtSignal = h.signals.length+ h.cancelled = true+ cancelSandboxUiRefresh()+ }+ toString(): string {+ return 'Cancel'+ }+ }+ class Release extends Step {+ constructor(private readonly scheduler: () => Scheduler) {+ super()+ }+ async run(): Promise<void> {+ if (this.scheduler().count() === 0) return+ await this.scheduler().waitOne()+ await flush()+ }+ toString(): string {+ return 'Release'+ }+ }++ const MODEL_SENDER = 77++ /** `observeRecovery: false` wires a no-op `onRecovered`, as if nobody listened. */+ function startHarness(scheduler: Scheduler, observeRecovery: boolean): Harness {+ _resetSandboxUiRefreshForTests()+ const harness: Harness = {+ window: new FakeBrowserWindow(),+ // Starts failing: recoveries only exist after the loop gave up once.+ mintMode: 'fail',+ mints: 0,+ signals: [],+ results: [],+ cancelledAtSignal: null,+ cancelled: false,+ }+ let installs = 0+ startSandboxUiRefresh(+ makeArgs({+ parentWindow: harness.window,+ webContentsId: MODEL_SENDER,+ refresh: () => {+ const label = `mint#${harness.mints}`+ harness.mints += 1+ const fails = harness.mintMode === 'fail'+ return scheduler.schedule(Promise.resolve(), label).then(() => {+ if (fails) throw new Error('control-api 503')+ return { setCookie: 'clerum_sandbox_ui_session=X' }+ })+ },+ installCookie: () => {+ const label = `install#${installs}`+ installs += 1+ return scheduler.schedule(Promise.resolve(), label).then(() => {+ // A cancelled loop stops before acting on the install.+ if (!harness.cancelled) harness.results.push('succeeded')+ })+ },+ onError: () => {+ harness.signals.push('error')+ harness.results.push('gaveUp')+ },+ onRecovered: observeRecovery ? () => void harness.signals.push('recovered') : () => {},+ })+ )+ return harness+ }++ async function settle(scheduler: Scheduler): Promise<void> {+ while (scheduler.count() > 0) {+ await scheduler.waitOne()+ await flush()+ }+ }++ /** Releases scheduled tasks in the order a previous run released them. */+ function replayScheduler(order: readonly string[]): Scheduler {+ const pending = new Map<string, () => void>()+ let next = 0+ return {+ schedule: <T>(task: Promise<T>, label: string) =>+ new Promise<T>((resolve, reject) => {+ pending.set(label, () => void task.then(resolve, reject))+ }),+ count: () => pending.size,+ async waitOne() {+ const label = order[next]+ next += 1+ const release = label === undefined ? undefined : pending.get(label)+ if (!release) throw new Error(`replay diverged at release #${next - 1} (${label})`)+ pending.delete(label!)+ release()+ },+ }+ }++ function recordingScheduler(s: fc.Scheduler, order: string[]): Scheduler {+ return {+ schedule: <T>(task: Promise<T>, label: string) =>+ s.schedule(task, label).then(+ value => {+ order.push(label)+ return value+ },+ error => {+ order.push(label)+ throw error+ }+ ),+ count: () => s.count(),+ waitOne: () => s.waitOne(),+ }+ }++ it('keeps the banner in step with the last mint that gave up or succeeded', async () => {+ const consoleError = vi.spyOn(console, 'error').mockImplementation(() => undefined)+ onTestFinished(() => consoleError.mockRestore())+ let current: Scheduler | null = null+ const currentScheduler = (): Scheduler => current!+ const mode = fc.constantFrom('ok', 'fail').map(m => new MintMode(m as 'ok' | 'fail'))+ // Weighted so that runs regularly give up (a failing streak needs several+ // minutes of mints) and then come back, which is where the signals live.+ const step = fc.oneof(+ { weight: 3, arbitrary: mode },+ {+ weight: 2,+ arbitrary: fc+ .constantFrom(1_000, 5_000, 30_000, 100_000, SANDBOX_UI_REFRESH_INTERVAL_MS + 1)+ .map(ms => new Advance(ms)),+ },+ {+ weight: 4,+ arbitrary: fc+ .constantFrom(30_000, 100_000, SANDBOX_UI_REFRESH_INTERVAL_MS + 1_000)+ .map(ms => new Elapse(ms, currentScheduler)),+ },+ { weight: 2, arbitrary: fc.constant(new Focus()) },+ { weight: 1, arbitrary: fc.constant(new HostResumed()) },+ { weight: 1, arbitrary: fc.constant(new EmbedRequest()) },+ { weight: 1, arbitrary: fc.constant(new Unauthorized()) },+ { weight: 1, arbitrary: fc.constant(new Cancel()) },+ { weight: 3, arbitrary: fc.constant(new Release(currentScheduler)) }+ )++ await fc.assert(+ fc.asyncProperty(+ fc.scheduler(),+ fc.commands([step], { maxCommands: 50, size: '+1' }),+ async (s, cmds) => {+ const order: string[] = []+ const executed: Step[] = []+ current = recordingScheduler(s, order)+ const scheduler = current+ const harness = startHarness(scheduler, true)+ await fc.asyncModelRun(+ () => ({ model: {}, real: harness }),+ [...cmds].map(cmd => {+ const step = cmd as Step+ return {+ check: () => true,+ run: async (m: Model, h: Harness) => {+ executed.push(step)+ await step.run(m, h)+ },+ toString: () => step.toString(),+ }+ })+ )+ await settle(scheduler)++ const banner = harness.signals.at(-1) === 'error'+ const lastDecisive = harness.results.at(-1)+ // V1 / S1: the banner shows exactly when the last decisive mint gave up.+ expect(banner).toBe(lastDecisive === 'gaveUp')+ // C1 (per loop): never more recoveries than reported failures.+ const errors = harness.signals.filter(signal => signal === 'error').length+ const recoveries = harness.signals.filter(signal => signal === 'recovered').length+ expect(recoveries).toBeLessThanOrEqual(errors)+ // S2: nothing is signalled once the loop was cancelled.+ if (harness.cancelledAtSignal !== null) {+ expect(harness.signals.length).toBe(harness.cancelledAtSignal)+ }++ // C1: the signal itself never changes how many mints the loop makes.+ current = replayScheduler(order)+ const replay = startHarness(current, false)+ for (const step of executed) await step.run({}, replay)+ await settle(current)+ expect(replay.mints).toBe(harness.mints)+ }+ ),+ { numRuns: 300 }+ )+ })+})diff --git a/desktop-app/src/appService.ts b/desktop-app/src/appService.tsindex 8392436cd..ce2d96580 100644--- a/desktop-app/src/appService.ts+++ b/desktop-app/src/appService.ts@@ -5920,6 +5920,8 @@ export class AppService {/** Exactly once per view this open mounts, even if the open then rejects. */onReleased?: (event: import('./sandboxUiDriver.js').SandboxUiReleasedEvent) => voidonRefreshError?: (message: string) => void+ /** A refresh succeeded after `onRefreshError` reported that it had given up. */+ onRefreshRecovered?: () => voidonOauthError?: (message: string) => voidonTitleChanged?: (title: string) => void}): Promise<void> {@@ -6062,6 +6064,10 @@ export class AppService {if (this.sandboxUiSessionEpoch !== sessionEpoch) returnargs.onRefreshError?.(err instanceof Error ? err.message : String(err))},+ onRecovered: () => {+ if (this.sandboxUiSessionEpoch !== sessionEpoch) return+ args.onRefreshRecovered?.()+ },})}diff --git a/desktop-app/src/ipc.ts b/desktop-app/src/ipc.tsindex 497bde0b0..116060843 100644--- a/desktop-app/src/ipc.ts+++ b/desktop-app/src/ipc.ts@@ -2128,6 +2128,10 @@ export function registerIpcHandlers(service: AppService): void {if (parentWindow.isDestroyed()) returnparentWindow.webContents.send('sandboxUi:refreshError', { appRef, launchId, message })},+ onRefreshRecovered: () => {+ if (parentWindow.isDestroyed()) return+ parentWindow.webContents.send('sandboxUi:refreshRecovered', { appRef, launchId })+ },onTitleChanged: title => {if (parentWindow.isDestroyed()) returnparentWindow.webContents.send('sandboxUi:titleChanged', { appRef, launchId, title })diff --git a/desktop-app/src/preload.ts b/desktop-app/src/preload.tsindex 7b72a4b24..bdd7b5ccb 100644--- a/desktop-app/src/preload.ts+++ b/desktop-app/src/preload.ts@@ -755,6 +755,12 @@ const clerum = Object.freeze({ipcRenderer.on('sandboxUi:refreshError', listener)return () => ipcRenderer.off('sandboxUi:refreshError', listener)},+ onRefreshRecovered: (callback: (args: { appRef: string; launchId: string }) => void) => {+ const listener = (_event: unknown, args: { appRef: string; launchId: string }) =>+ callback(args)+ ipcRenderer.on('sandboxUi:refreshRecovered', listener)+ return () => ipcRenderer.off('sandboxUi:refreshRecovered', listener)+ },onTitleChanged: (callback: (args: { appRef: string; launchId: string; title: string }) => void) => {diff --git a/desktop-app/src/renderer.d.ts b/desktop-app/src/renderer.d.tsindex e5c801baf..7bc718fbb 100644--- a/desktop-app/src/renderer.d.ts+++ b/desktop-app/src/renderer.d.ts@@ -768,8 +768,8 @@ declare global {defaultPath?: stringroutePath?: stringbounds: { x: number; y: number; width: number; height: number; dpr?: number }- // Echoed by main on `onReleased` / `onRefreshError` / `onTitleChanged`- // for the view this open mounts.+ // Echoed by main on `onReleased` / `onRefreshError` / `onRefreshRecovered` /+ // `onTitleChanged` for the view this open mounts.launchId: string}) => Promise<void>// Closes that launch's view only if it is still the live one.@@ -820,6 +820,10 @@ declare global {onRefreshError: (callback: (args: { appRef: string; launchId: string; message: string }) => void) => () => void+ // A refresh that `onRefreshError` reported as failed succeeded later.+ onRefreshRecovered: (+ callback: (args: { appRef: string; launchId: string }) => void+ ) => () => voidonTitleChanged: (callback: (args: { appRef: string; launchId: string; title: string }) => void) => () => voiddiff --git a/desktop-app/src/sandboxUiSessionRefresh.ts b/desktop-app/src/sandboxUiSessionRefresh.tsindex bceb6d4e0..59ceb0f1f 100644--- a/desktop-app/src/sandboxUiSessionRefresh.ts+++ b/desktop-app/src/sandboxUiSessionRefresh.ts@@ -70,6 +70,8 @@ export type StartSandboxUiRefreshArgs = {/** Hard-reloads the embed after a mint that recovers a top-level 401. */reload: () => voidonError: (err: Error) => void+ /** A mint succeeded after `onError` reported that retries had run out. */+ onRecovered: () => void/*** Wall-clock time the initial mint was requested. The cookie's lifetime is* counted from the server's issue time, so the request start is the@@ -108,6 +110,8 @@ type ActiveRefresh = Omit<StartSandboxUiRefreshArgs, 'now' | 'mintedAt'> & {unauthorizedRecoveryMintSeq: number | null/** Last time a return event or a 401 reopened retries after they ran out. */lastResumeAt: number | null+ /** `onError` fired and no mint has succeeded since. */+ errorReported: boolean}let activeRefresh: ActiveRefresh | null = null@@ -222,6 +226,17 @@ async function attemptMint(ref: ActiveRefresh, trigger: RefreshTrigger): Promiseref.reload()}armTimer(ref)+ // Cleared only here, after reload() and armTimer(): if either throws, the+ // next successful mint still owes the renderer its recovery signal. A+ // throwing listener must not leave the loop without a timer either.+ if (ref.errorReported) {+ ref.errorReported = false+ try {+ ref.onRecovered()+ } catch (err) {+ console.error('[SandboxUI] refresh recovery listener failed:', err)+ }+ }}function scheduleRetry(ref: ActiveRefresh, err: Error): void {@@ -234,6 +249,7 @@ function scheduleRetry(ref: ActiveRefresh, err: Error): void {ref.consecutiveFailures += 1if (now + delay > ref.retryDeadline) {ref.nextAttemptAt = null+ ref.errorReported = trueref.onError(err)return}@@ -261,6 +277,7 @@ export function startSandboxUiRefresh(args: StartSandboxUiRefreshArgs): void {reloadOnNextMint: false,unauthorizedRecoveryMintSeq: null,lastResumeAt: null,+ errorReported: false,}senderToRefreshContext.set(args.webContentsId, ref)diff --git a/desktop-app/ui/src/App.tsx b/desktop-app/ui/src/App.tsxindex 2da74f983..f874248b6 100644--- a/desktop-app/ui/src/App.tsx+++ b/desktop-app/ui/src/App.tsx@@ -412,6 +412,10 @@ export function App() {},onOwnerRefreshError: event =>setEmbedRefreshError({ launchId: event.launchId, message: event.message }),+ // The machine already drops recoveries of a launch that lost the embed;+ // matching the launch here keeps one from ever clearing another's error.+ onOwnerRefreshRecovered: event =>+ setEmbedRefreshError(prev => (prev?.launchId === event.launchId ? null : prev)),onTransition: ({ previous, next }) => {// The tab stays but its embed is gone (back to apps, team handoff, a// failed open, a crash, a session end): drop the chrome that belonged@@ -469,10 +473,14 @@ export function App() {const offReleased = sandboxUi.onReleased?.(event => embed.onReleased(event))const offTitle = sandboxUi.onTitleChanged?.(event => embed.onTitleChanged(event))const offRefreshError = sandboxUi.onRefreshError?.(event => embed.onRefreshError(event))+ const offRefreshRecovered = sandboxUi.onRefreshRecovered?.(event =>+ embed.onRefreshRecovered(event)+ )return () => {offReleased?.()offTitle?.()offRefreshError?.()+ offRefreshRecovered?.()lifetime.mounts -= 1queueMicrotask(() => {if (lifetime.mounts > 0 || lifetime.disposed) returndiff --git a/desktop-app/ui/src/__tests__/App.sandboxUiOwnership.test.tsx b/desktop-app/ui/src/__tests__/App.sandboxUiOwnership.test.tsxindex 0290e4239..f60f0d08a 100644--- a/desktop-app/ui/src/__tests__/App.sandboxUiOwnership.test.tsx+++ b/desktop-app/ui/src/__tests__/App.sandboxUiOwnership.test.tsx@@ -710,6 +710,61 @@ describe('App wires gestures and main events to the embed ownership machine', ()await waitFor(() => expect(screen.queryByText(/Session refresh failed/)).toBeNull())})+ it('clears the session failure and its Retry once a resumed refresh of the same view succeeds', async () => {+ await renderApp()+ await openFromSidebar(ALPHA, 1)+ const first = main.openCall(0).launchId+ main.navigate('/tickets/7')++ await act(() => main.refreshFailed(new Error('mint failed')))+ expect(await screen.findByText('Session refresh failed: mint failed.')).toBeTruthy()+ const liveBefore = main.live()++ await act(() => main.hostReturned())+ await act(() => main.settle())++ expect(main.reported('sandboxUi:refreshRecovered')).toEqual([+ { appRef: ALPHA!.appRef, launchId: first },+ ])+ await waitFor(() => expect(screen.queryByText(/Session refresh failed/)).toBeNull())+ expect(screen.queryByRole('button', { name: 'Retry' })).toBeNull()+ expect(sandboxUi.open).toHaveBeenCalledTimes(1)+ expect(sandboxUi.close).not.toHaveBeenCalled()+ expect(main.live()).toEqual(liveBefore)+ expect(main.live()).toEqual({ launchId: first, appRef: ALPHA!.appRef, routePath: '/tickets/7' })+ })++ // The machine drops a recovery of a launch that no longer owns the embed, so a+ // late one cannot take down the failure of the view that replaced it.+ it("keeps a relaunched view's session failure when a recovery of the view it replaced arrives late", async () => {+ await renderApp()+ await openFromSidebar(ALPHA, 1)+ const first = main.openCall(0).launchId+ await act(() => main.refreshFailed(new Error('mint failed')))+ await act(() => main.hostReturned())+ await act(() => main.settle())+ const staleRecovery = main.reported('sandboxUi:refreshRecovered').at(-1) as {+ appRef: string+ launchId: string+ }+ expect(staleRecovery).toEqual({ appRef: ALPHA!.appRef, launchId: first })++ await act(() => main.refreshFailed(new Error('mint failed')))+ fireEvent.click(await screen.findByRole('button', { name: 'Retry' }))+ await waitFor(() => expect(sandboxUi.open).toHaveBeenCalledTimes(2))+ await act(() => main.settle())+ const second = main.openCall(1).launchId+ expect(main.live()?.launchId).toBe(second)+ await act(() => main.refreshFailed(new Error('second view failed')))+ expect(await screen.findByText('Session refresh failed: second view failed.')).toBeTruthy()++ act(() => latestListener<typeof staleRecovery>(sandboxUi.onRefreshRecovered)(staleRecovery))+ await flushMacrotask()++ expect(screen.getByText('Session refresh failed: second view failed.')).toBeTruthy()+ expect(screen.getByRole('button', { name: 'Retry' })).toBeTruthy()+ })+it('ignores the session failure of a view it already replaced', async () => {await renderApp()await openFromSidebar(ALPHA, 1)diff --git a/desktop-app/ui/src/__tests__/__fixtures__/sandboxUiMainFake.ts b/desktop-app/ui/src/__tests__/__fixtures__/sandboxUiMainFake.tsindex 2d4b3ae6c..7939a1124 100644--- a/desktop-app/ui/src/__tests__/__fixtures__/sandboxUiMainFake.ts+++ b/desktop-app/ui/src/__tests__/__fixtures__/sandboxUiMainFake.ts@@ -16,6 +16,7 @@ import {import { getActiveSandboxUi, getActiveSandboxUiLocation } from '../../../../src/sandboxUiDriver'import {SANDBOX_UI_COOKIE_TTL_MS,+ SANDBOX_UI_RATE_LIMIT_MS,SANDBOX_UI_REFRESH_INTERVAL_MS,SANDBOX_UI_RETRY_GRACE_MS,} from '../../../../src/sandboxUiSessionRefresh'@@ -72,6 +73,7 @@ export function createSandboxUiMainFake() {const refreshErrorListeners = new Set<(event: { appRef: string; launchId: string; message: string }) => void>()+ const refreshRecoveredListeners = new Set<(event: { appRef: string; launchId: string }) => void>()const userDataDir = mkdtempSync(path.join(tmpdir(), 'clerum-sandbox-ui-main-'))const openMints: MintDecision[] = []let failNextMount: Error | null = null@@ -124,6 +126,11 @@ export function createSandboxUiMainFake() {for (const listener of [...refreshErrorListeners]) {listener(payload as { appRef: string; launchId: string; message: string })}+ return+ case 'sandboxUi:refreshRecovered':+ for (const listener of [...refreshRecoveredListeners]) {+ listener(payload as { appRef: string; launchId: string })+ }}})}@@ -248,6 +255,12 @@ export function createSandboxUiMainFake() {return () => refreshErrorListeners.delete(listener)}),+ onRefreshRecovered: vi.fn(+ (listener: (event: { appRef: string; launchId: string }) => void) => {+ refreshRecoveredListeners.add(listener)+ return () => refreshRecoveredListeners.delete(listener)+ }+ ),},/** The open request of the `index`-th open call. */openCall(index: number): SandboxUiOpenCall {@@ -379,6 +392,24 @@ export function createSandboxUiMainFake() {}return toRendererLane},+ /**+ * The user comes back to the window after `awayMs` and rpc-proxy accepts+ * the session mint that brings: main must be due to refresh by then, either+ * because it gave up earlier or because the session is about to expire.+ * Resolves once what main reported about it has reached the renderer.+ */+ async hostReturned(awayMs: number = SANDBOX_UI_RATE_LIMIT_MS): Promise<void> {+ if (!liveView()) throw new Error('no live view to return to')+ const mintsBefore = sandboxUiMintFetch.mock.calls.length+ // Return events reopen exhausted retries at most once per rate-limit window.+ clockOffsetMs += awayMs+ main.parent.emit('focus')+ for (let i = 0; i < 100; i++) await Promise.resolve()+ if (sandboxUiMintFetch.mock.calls.length === mintsBefore) {+ throw new Error('returning to the window did not make main mint a session')+ }+ return toRendererLane+ },/** What main has sent the renderer so far on `channel`, oldest first. */reported,/**diff --git a/desktop-app/ui/src/__tests__/sandboxUiMainFake.contract.test.ts b/desktop-app/ui/src/__tests__/sandboxUiMainFake.contract.test.tsindex 28dea626d..cf5ce0ac8 100644--- a/desktop-app/ui/src/__tests__/sandboxUiMainFake.contract.test.ts+++ b/desktop-app/ui/src/__tests__/sandboxUiMainFake.contract.test.ts@@ -1,4 +1,5 @@import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'+import { SANDBOX_UI_REFRESH_INTERVAL_MS } from '../../../src/sandboxUiSessionRefresh'import { type SandboxUiMainFake, createSandboxUiMainFake } from './__fixtures__/sandboxUiMainFake'vi.mock(@@ -61,6 +62,12 @@ describe('the App suites’ sandbox-ui bridge delivers what main produces, in ormain.bridge.onReleased(event => {seen.push(`released ${event.launchId} ${event.reason} ${JSON.stringify(event.location)}`)})+ main.bridge.onRefreshError(event => {+ seen.push(`refreshError ${event.launchId} ${event.message}`)+ })+ main.bridge.onRefreshRecovered(event => {+ seen.push(`refreshRecovered ${event.launchId} ${event.appRef}`)+ })})afterEach(async () => {@@ -190,6 +197,34 @@ describe('the App suites’ sandbox-ui bridge delivers what main produces, in orexpect(seen).toEqual([`released ${A} closed {"routePath":"/inbox"}`, `close ${A} ok`])})+ it('reports a refresh that succeeds after main gave up as recovered, for the view that failed', async () => {+ await open(A)+ await main.settle()+ seen = []++ await main.refreshFailed(new Error('upstream unavailable'))+ await main.hostReturned()+ await main.settle()++ expect(seen).toEqual([+ expect.stringMatching(new RegExp(`^refreshError ${A} .*upstream unavailable`)),+ `refreshRecovered ${A} sandbox-recipes/alpha`,+ ])+ expect(main.live()?.launchId).toBe(A)+ })++ it('reports no recovery for a refresh that never failed', async () => {+ await open(A)+ await main.settle()+ seen = []++ await main.hostReturned(SANDBOX_UI_REFRESH_INTERVAL_MS)+ await main.settle()++ expect(seen).toEqual([])+ expect(main.live()?.launchId).toBe(A)+ })+it('releases the live view when the session changes', async () => {await open(A)await main.settle()diff --git a/desktop-app/ui/src/lib/__tests__/embedOwnershipMachine.test.ts b/desktop-app/ui/src/lib/__tests__/embedOwnershipMachine.test.tsindex b2433b5a3..1d1c1295b 100644--- a/desktop-app/ui/src/lib/__tests__/embedOwnershipMachine.test.ts+++ b/desktop-app/ui/src/lib/__tests__/embedOwnershipMachine.test.ts@@ -408,6 +408,51 @@ describe('embed ownership machine — notifications', () => {expect(onOwnerRefreshError).toHaveBeenCalledWith({ launchId: 'l-1', message: 'live' })})+ it("reports only the owner's refresh recoveries", async () => {+ const onOwnerRefreshRecovered = vi.fn()+ const h = harness({ onOwnerRefreshRecovered })+ await mountedOwner(h, launch('l-1'))++ h.machine.onRefreshRecovered({ launchId: 'l-old' })+ h.machine.onRefreshRecovered({ launchId: 'l-1' })++ expect(onOwnerRefreshRecovered).toHaveBeenCalledTimes(1)+ expect(onOwnerRefreshRecovered).toHaveBeenCalledWith({ launchId: 'l-1' })+ })++ // Main keeps refreshing a view until its close is dequeued, so a recovery of+ // the replaced launch can follow the close the relaunch sent.+ it('drops a recovery of a relaunched view, sent before or after its close reached main', async () => {+ const onOwnerRefreshRecovered = vi.fn()+ const h = harness({ onOwnerRefreshRecovered })+ await mountedOwner(h, launch('l-1'))++ h.machine.relaunch(launch('l-2'))+ expect(h.bridge.close).toHaveBeenCalledWith({ launchId: 'l-1' })+ h.machine.onRefreshRecovered({ launchId: 'l-1' })+ h.closes.get('l-1')!.resolve()+ await flush()+ h.machine.onRefreshRecovered({ launchId: 'l-1' })++ expect(h.machine.getSnapshot().owner).toBe('l-2')+ expect(onOwnerRefreshRecovered).not.toHaveBeenCalled()+ })++ it('drops a recovery once its launch left the embed, and every recovery after dispose', async () => {+ const onOwnerRefreshRecovered = vi.fn()+ const h = harness({ onOwnerRefreshRecovered })+ await mountedOwner(h, launch('l-1'))+ h.machine.backToApps()+ h.machine.onRefreshRecovered({ launchId: 'l-1' })++ const disposed = harness({ onOwnerRefreshRecovered })+ await mountedOwner(disposed, launch('l-2'))+ disposed.machine.dispose()+ disposed.machine.onRefreshRecovered({ launchId: 'l-2' })++ expect(onOwnerRefreshRecovered).not.toHaveBeenCalled()+ })+it('announces a handoff once the views it waits for are gone', async () => {const onHandoffReady = vi.fn()const h = harness({ onHandoffReady })diff --git a/desktop-app/ui/src/lib/embedOwnershipMachine.ts b/desktop-app/ui/src/lib/embedOwnershipMachine.tsindex 4cd3d665d..d1d1208b6 100644--- a/desktop-app/ui/src/lib/embedOwnershipMachine.ts+++ b/desktop-app/ui/src/lib/embedOwnershipMachine.ts@@ -243,6 +243,10 @@ export function createEmbedOwnershipMachine(if (disposed || event.launchId !== state.owner) returndeps.onOwnerRefreshError?.(event)},+ onRefreshRecovered: event => {+ if (disposed || event.launchId !== state.owner) return+ deps.onOwnerRefreshRecovered?.(event)+ },registerMeasurer(next) {if (disposed) return () => {}measurer = nextdiff --git a/desktop-app/ui/src/lib/embedOwnershipMachine.types.ts b/desktop-app/ui/src/lib/embedOwnershipMachine.types.tsindex 1b1ce5129..355f44907 100644--- a/desktop-app/ui/src/lib/embedOwnershipMachine.types.ts+++ b/desktop-app/ui/src/lib/embedOwnershipMachine.types.ts@@ -38,6 +38,8 @@ export type EmbedTabsUpdater = (state: WorkspaceTabsState) => WorkspaceTabsStateexport type EmbedRefreshErrorEvent = { launchId: LaunchId; message: string }+export type EmbedRefreshRecoveredEvent = { launchId: LaunchId }+export type EmbedTitleChangedEvent = { launchId: LaunchId; title: string }export type EmbedOwnershipMachineDeps = {@@ -47,6 +49,8 @@ export type EmbedOwnershipMachineDeps = {onHandoffReady?: (requestId: number) => void/** Only refresh failures of the launch that owns the embed when they arrive. */onOwnerRefreshError?: (event: EmbedRefreshErrorEvent) => void+ /** Only recoveries of the launch that owns the embed when they arrive. */+ onOwnerRefreshRecovered?: (event: EmbedRefreshRecoveredEvent) => void/*** Every reduced event with the state around it and its effects, before the* effects run. It must not dispatch: it observes, it does not decide.@@ -73,6 +77,7 @@ export type EmbedOwnershipMachine = {onReleased(event: SandboxUiReleasedEvent): voidonTitleChanged(event: EmbedTitleChangedEvent): voidonRefreshError(event: EmbedRefreshErrorEvent): void+ onRefreshRecovered(event: EmbedRefreshRecoveredEvent): void/** The page registers while its slot exists; measures wait until one is registered. */registerMeasurer(measurer: EmbedSlotMeasurer): () => void/**