From 5d94272d25858d3b4d5209ab0ff1a5e9554d00ed Mon Sep 17 00:00:00 2001 From: bymyself Date: Mon, 3 Aug 2026 14:35:07 -0700 Subject: [PATCH 1/5] fix(first-run-tour): fail a run the queue never accepted A submission the backend refuses never gets a prompt_id, so no workflow status ever appears and none of the run-state branches can fire. Account preconditions - sign-in, subscription, credits - are deliberately kept out of the error stores by ComfyApp.queuePrompt, so the refused term cannot see them either. The Result card was left promising a result forever. A paid subscriber out of credits is the reachable case: canRunWorkflows is isActiveSubscription && (!isFreeTier || ...), which is true for them, so the paywall branch never postpones the tour. Give acceptance a deadline rather than the run. Any status at all clears it, so a job that is merely slow is never cut short - the case the offline-grace tests already pin. The existing generatingRun helper never acknowledged its run, which is what an unacknowledged submission looks like. It now reports a status, matching what the queue actually does, and the unacknowledged case is covered by its own test. Closes #14619 Co-Authored-By: Claude Opus 5 (1M context) --- .../tour/useFirstRunTourController.test.ts | 34 +++++++++++++++++++ .../tour/useFirstRunTourController.ts | 27 +++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.test.ts b/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.test.ts index 0cfcf962273..6379d4827a9 100644 --- a/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.test.ts +++ b/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.test.ts @@ -16,6 +16,7 @@ const TOUR_WORKFLOW = { path: 'tour.json' } const OTHER_WORKFLOW = { path: 'other.json' } const INTRO_PREVIEW_MS = 500 const OFFLINE_GRACE_MS = 20_000 +const ACCEPT_DEADLINE_MS = 15_000 const mocks = vi.hoisted(() => ({ canRunWorkflows: { value: true }, @@ -352,14 +353,47 @@ describe('useFirstRunTourController', () => { }) describe('a run behind a dropped socket', () => { + /** + * A run the queue accepted: the click reports `generating`, then the + * backend answers with a status. The acknowledgement matters — an + * unacknowledged submission is a refusal, and is covered separately below. + */ async function generatingRun() { await tourOnRunStep() mountRunButton('queue-button', () => {}).click() expect(mocks.runState.value).toBe('generating') + await finishRun(TOUR_WORKFLOW, 'running') const { api } = await import('@/scripts/api') return api } + it('stops promising a result the queue never accepted', async () => { + await tourOnRunStep() + mountRunButton('queue-button', () => {}).click() + expect(mocks.runState.value).toBe('generating') + + // No status ever arrives. A refused submission gets no prompt_id, and + // account preconditions - sign-in, subscription, credits - are kept out + // of the error stores on purpose, so nothing else can report this. + await vi.advanceTimersByTimeAsync(ACCEPT_DEADLINE_MS) + + expect( + mocks.runState.value, + 'a paid user out of credits is refused silently; the card must not promise a result forever' + ).toBe('failed') + }) + + it('leaves an accepted run past the acceptance deadline alone', async () => { + await generatingRun() + + await vi.advanceTimersByTimeAsync(ACCEPT_DEADLINE_MS * 4) + + expect( + mocks.runState.value, + 'the deadline is on acceptance, not on the run: a job that answered must never be cut short' + ).toBe('generating') + }) + it('stops promising a result once the socket stays gone', async () => { const api = await generatingRun() diff --git a/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.ts b/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.ts index 36cd1fda129..e73244a544a 100644 --- a/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.ts +++ b/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.ts @@ -32,6 +32,9 @@ const INTRO_PREVIEW_MS = 500 const OFFLINE_GRACE_MS = 20_000 +/** How long a submitted run has to be accepted before the card stops promising. */ +const ACCEPT_DEADLINE_MS = 15_000 + function useFirstRunTourControllerInternal() { const engine = useOnboardingTourStore() const billing = useBillingContext() @@ -71,6 +74,7 @@ function useFirstRunTourControllerInternal() { executionErrorStore.hasNodeError || executionErrorStore.hasPromptError ], ([status, refused], previous) => { + if (status !== undefined) stopAcceptDeadline() if (status === 'running') runState.value = 'generating' else if (status === 'completed') runState.value = 'succeeded' else if (status === 'failed') runState.value = 'failed' @@ -83,6 +87,27 @@ function useFirstRunTourControllerInternal() { } ) + /** + * A submission the backend refuses never gets a prompt_id, so no status ever + * appears and none of the branches above can fire. Account preconditions — + * sign-in, subscription, credits — are deliberately kept out of the error + * stores by `ComfyApp.queuePrompt`, so the refusal is invisible there too. + * Give acceptance a deadline. Only acceptance: any status at all clears it, + * so a run that is merely slow is never cut short. + */ + let acceptTimer: ReturnType | undefined + function stopAcceptDeadline() { + clearTimeout(acceptTimer) + acceptTimer = undefined + } + function startAcceptDeadline() { + stopAcceptDeadline() + acceptTimer = setTimeout(() => { + stopAcceptDeadline() + if (runState.value === 'generating') runState.value = 'failed' + }, ACCEPT_DEADLINE_MS) + } + let offlineTimer: ReturnType | undefined function stopOfflineGrace() { clearTimeout(offlineTimer) @@ -116,6 +141,7 @@ function useFirstRunTourControllerInternal() { } runState.value = 'generating' + startAcceptDeadline() engine.next() }, { capture: true } @@ -132,6 +158,7 @@ function useFirstRunTourControllerInternal() { ending?.tour === 'firstRun' && ending.outcome === 'completed' nudgeArmed.value = true stopOfflineGrace() + stopAcceptDeadline() releaseFirstRunTargets() tourWorkflow.value = null runState.value = 'idle' From b69793bb897565ff17d17c0619b8bc7bd9f4013f Mon Sep 17 00:00:00 2001 From: t Date: Tue, 11 Aug 2026 18:56:24 -0700 Subject: [PATCH 2/5] fix(first-run-tour): deadline acceptance, not execution start @benjcooley's blocker: the deadline cleared on workflow status, whose only writer is `handleExecutionStart`. Acceptance and execution start are not the same event - cloud accepts a job and reports "Waiting for a machine", leaving it in `initializingJobIds` with no status at all until a worker is allocated, which routinely takes longer than any deadline short enough to be useful. So a healthy queued run was failed at t=15s and told the user to close the tour and run again - inviting a duplicate paid submission - before silently reverting to "generating". Keys off the job the queue stores on acceptance instead. A refused submission never gets one, which is the condition this was always trying to detect. That also settles the grace-period interaction: acceptance arrives on the queuePrompt response rather than the socket, so a run accepted at all disarms this deadline immediately and leaves the connection question to the longer `OFFLINE_GRACE_MS`. A drop before the first status now gets its full 20s. Both regressions are covered, and both fail against the reviewed implementation: reverting to the status-keyed clear fails exactly these two and leaves the other 52 passing. --- .../tour/useFirstRunTourController.test.ts | 50 ++++++++++++++++++- .../tour/useFirstRunTourController.ts | 30 +++++++++-- 2 files changed, 76 insertions(+), 4 deletions(-) diff --git a/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.test.ts b/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.test.ts index 6379d4827a9..03550bdcf06 100644 --- a/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.test.ts +++ b/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.test.ts @@ -24,6 +24,7 @@ const mocks = vi.hoisted(() => ({ workflowStatus: { value: new Map() }, executionErrors: { hasNodeError: false, hasPromptError: false }, activeWorkflow: { value: null as unknown }, + queuedJobs: { value: {} as Record }, linearMode: { value: false }, vueNodesEnabled: true, steps: [] as CoachStep[], @@ -51,10 +52,14 @@ vi.mock('@/composables/billing/useBillingContext', () => ({ vi.mock('@/stores/executionStore', async () => { const { shallowRef } = await import('vue') mocks.workflowStatus = shallowRef(new Map()) + mocks.queuedJobs = shallowRef({} as Record) return { useExecutionStore: () => ({ getWorkflowStatus: (workflow: unknown) => - mocks.workflowStatus.value.get(workflow) + mocks.workflowStatus.value.get(workflow), + get queuedJobs() { + return mocks.queuedJobs.value + } }) } }) @@ -193,6 +198,12 @@ const EVERY_ENDING: { named: string; ending: TourEnding }[] = [ ...UNFINISHED_ENDINGS ] +/** The queue storing a job, which is what acceptance actually looks like. */ +function acceptRun(workflow: unknown) { + mocks.queuedJobs.value = { 'job-1': { workflow } } + return nextTick() +} + function finishRun(workflow: unknown, status: string) { mocks.workflowStatus.value = new Map(mocks.workflowStatus.value).set( workflow, @@ -383,6 +394,43 @@ describe('useFirstRunTourController', () => { ).toBe('failed') }) + it('leaves a run accepted but still waiting for a machine alone', async () => { + // Cloud accepts the job and reports "Waiting for a machine" — it is in + // `initializingJobIds` with NO workflow status until a worker picks it + // up, which routinely outlasts the deadline. Keying on status instead of + // acceptance would fail this healthy run and tell the user to run again, + // prompting a duplicate paid submission. + await tourOnRunStep() + mountRunButton('queue-button', () => {}).click() + await acceptRun(TOUR_WORKFLOW) + + await vi.advanceTimersByTimeAsync(ACCEPT_DEADLINE_MS * 4) + + expect( + mocks.runState.value, + 'an accepted job with no status yet is queued, not refused' + ).toBe('generating') + }) + + it('lets the offline grace outlive the acceptance deadline', async () => { + // The grace is 20s and the acceptance deadline 15s. A drop before the + // first status must still get the full grace: acceptance arrives on the + // queuePrompt response, not the socket, so it disarms this deadline even + // while the connection is down. + const { api } = await import('@/scripts/api') + await tourOnRunStep() + mountRunButton('queue-button', () => {}).click() + await acceptRun(TOUR_WORKFLOW) + + api.dispatchCustomEvent('reconnecting') + await vi.advanceTimersByTimeAsync(OFFLINE_GRACE_MS - 1) + + expect( + mocks.runState.value, + 'the 15s acceptance deadline must not cut the 20s grace short' + ).toBe('generating') + }) + it('leaves an accepted run past the acceptance deadline alone', async () => { await generatingRun() diff --git a/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.ts b/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.ts index e73244a544a..85c29dbbc1e 100644 --- a/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.ts +++ b/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.ts @@ -74,7 +74,6 @@ function useFirstRunTourControllerInternal() { executionErrorStore.hasNodeError || executionErrorStore.hasPromptError ], ([status, refused], previous) => { - if (status !== undefined) stopAcceptDeadline() if (status === 'running') runState.value = 'generating' else if (status === 'completed') runState.value = 'succeeded' else if (status === 'failed') runState.value = 'failed' @@ -87,14 +86,38 @@ function useFirstRunTourControllerInternal() { } ) + /** + * The queue stores a job the moment it accepts a submission, so a job + * carrying this tour's workflow is proof of acceptance. A refused submission + * never gets one. + * + * Deliberately *not* the workflow status: that is only written by + * `handleExecutionStart`, and a cloud job sits accepted in + * `initializingJobIds` — "Waiting for a machine" — with no status at all + * while a worker is allocated. Allocation routinely outlasts any deadline + * short enough to be useful, so keying on status would fail healthy runs. + */ + const tourRunAccepted = computed(() => + Object.values(executionStore.queuedJobs).some( + (job) => job.workflow === tourWorkflow.value + ) + ) + /** * A submission the backend refuses never gets a prompt_id, so no status ever * appears and none of the branches above can fire. Account preconditions — * sign-in, subscription, credits — are deliberately kept out of the error * stores by `ComfyApp.queuePrompt`, so the refusal is invisible there too. - * Give acceptance a deadline. Only acceptance: any status at all clears it, - * so a run that is merely slow is never cut short. + * + * Give *acceptance* a deadline, not the run. Acceptance arrives on the + * queuePrompt response rather than the socket, so this cannot pre-empt the + * longer offline grace: a run accepted at all disarms this immediately and + * leaves the connection question to `OFFLINE_GRACE_MS`. */ + watch(tourRunAccepted, (accepted) => { + if (accepted) stopAcceptDeadline() + }) + let acceptTimer: ReturnType | undefined function stopAcceptDeadline() { clearTimeout(acceptTimer) @@ -102,6 +125,7 @@ function useFirstRunTourControllerInternal() { } function startAcceptDeadline() { stopAcceptDeadline() + if (tourRunAccepted.value) return acceptTimer = setTimeout(() => { stopAcceptDeadline() if (runState.value === 'generating') runState.value = 'failed' From 9dfd9537b77293b0dddf271fcf3e543b01056d52 Mon Sep 17 00:00:00 2001 From: Connor Byrne Date: Wed, 12 Aug 2026 15:29:12 -0700 Subject: [PATCH 3/5] fix(first-run-tour): disarm the acceptance deadline on any reported status `resetExecutionState` drops a job from `queuedJobs` without clearing its workflow status, so `tourRunAccepted` can read false while the run reports `running`. Watchers batch, so an accept-then-reset inside one tick is never observed and the deadline stays armed against a live run. Acceptance and status now both disarm it. A refused submission produces neither, so #14619 stays caught. Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/15091#discussion_r3763181018 --- .../firstRunTour/tour/useFirstRunTourController.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.ts b/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.ts index 85c29dbbc1e..981c4a08ccf 100644 --- a/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.ts +++ b/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.ts @@ -74,6 +74,7 @@ function useFirstRunTourControllerInternal() { executionErrorStore.hasNodeError || executionErrorStore.hasPromptError ], ([status, refused], previous) => { + if (status !== undefined) stopAcceptDeadline() if (status === 'running') runState.value = 'generating' else if (status === 'completed') runState.value = 'succeeded' else if (status === 'failed') runState.value = 'failed' @@ -113,6 +114,10 @@ function useFirstRunTourControllerInternal() { * queuePrompt response rather than the socket, so this cannot pre-empt the * longer offline grace: a run accepted at all disarms this immediately and * leaves the connection question to `OFFLINE_GRACE_MS`. + * + * Acceptance is not the only disarm. `resetExecutionState` drops a job from + * `queuedJobs` without clearing its status, so a run can report a status + * while this reads false. A refusal produces neither signal. */ watch(tourRunAccepted, (accepted) => { if (accepted) stopAcceptDeadline() From 2e0ba756a14bb8624c4b080a5ba472837cc5bda4 Mon Sep 17 00:00:00 2001 From: Connor Byrne Date: Wed, 12 Aug 2026 15:30:45 -0700 Subject: [PATCH 4/5] test(first-run-tour): stop leaking queued jobs between tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `acceptRun` wrote to a module-level holder that `beforeEach` never reset, so every test after the first ran with a job already accepted and the deadline suppressed rather than disarmed. Four tests asserting a reported run survives past the deadline passed only on that leak. The mock factories also ran on the first dynamic import — mid-test for whichever test ran first — and replaced each holder, discarding setup that test had already written. That made any isolated run read a null workflow and key-miss every status lookup, so `-t`, `.only` and sharding all failed. Verified: full file 54/54, and each affected test green on its own. Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/15091#discussion_r3763181015 --- .../tour/useFirstRunTourController.test.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.test.ts b/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.test.ts index d3be2cb84b1..b6d64e653d0 100644 --- a/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.test.ts +++ b/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.test.ts @@ -47,10 +47,13 @@ vi.mock('@/composables/billing/useBillingContext', () => ({ }) })) +// Each factory runs on the first dynamic import, which lands mid-test for +// whichever test runs first. Seed the new ref from the holder so that test's +// setup survives instead of being discarded. vi.mock('@/stores/executionStore', async () => { const { shallowRef } = await import('vue') - mocks.workflowStatus = shallowRef(new Map()) - mocks.queuedJobs = shallowRef({} as Record) + mocks.workflowStatus = shallowRef(new Map(mocks.workflowStatus.value)) + mocks.queuedJobs = shallowRef(mocks.queuedJobs.value) return { useExecutionStore: () => ({ getWorkflowStatus: (workflow: unknown) => @@ -64,16 +67,13 @@ vi.mock('@/stores/executionStore', async () => { vi.mock('@/stores/executionErrorStore', async () => { const { reactive } = await import('vue') - mocks.executionErrors = reactive({ - hasNodeError: false, - hasPromptError: false - }) + mocks.executionErrors = reactive({ ...mocks.executionErrors }) return { useExecutionErrorStore: () => mocks.executionErrors } }) vi.mock('@/platform/workflow/management/stores/workflowStore', async () => { const { shallowRef } = await import('vue') - mocks.activeWorkflow = shallowRef(null as unknown) + mocks.activeWorkflow = shallowRef(mocks.activeWorkflow.value) return { useWorkflowStore: () => ({ get activeWorkflow() { @@ -85,7 +85,7 @@ vi.mock('@/platform/workflow/management/stores/workflowStore', async () => { vi.mock('@/renderer/core/canvas/canvasStore', async () => { const { shallowRef } = await import('vue') - mocks.linearMode = shallowRef(false) + mocks.linearMode = shallowRef(mocks.linearMode.value) return { useCanvasStore: () => ({ get linearMode() { @@ -242,6 +242,7 @@ describe('useFirstRunTourController', () => { beforeEach(() => { mocks.canRunWorkflows = ref(true) mocks.workflowStatus.value = new Map() + mocks.queuedJobs.value = {} mocks.executionErrors.hasNodeError = false mocks.executionErrors.hasPromptError = false mocks.activeWorkflow.value = null From 70704d9dc6729b7d2ec230999a645aca3451da6b Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Sat, 22 Aug 2026 18:01:05 -0700 Subject: [PATCH 5/5] fix(first-run-tour): stop promising a result once acceptance is lost (#15172) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Stacked on #15091 — base is `fix/14619-tour-unaccepted-run`, merge after it. #15091 gave the first-run tour a 15s acceptance deadline, which covers a submission the backend refuses outright. It left one hole: a run that *was* accepted and then loses its job without an outcome. Two live paths do that, and **neither is the mid-run credits path this description originally led with** — thanks @benjcooley for catching that: | path | what it leaves behind | covered before this PR? | | --- | --- | --- | | accepted job dropped with **no status ever written** — the cloud "waiting for a machine" job cancelled or reconciled away | nothing | ❌ no branch can fire | | `handleServiceLevelError` ("Job has stagnated") — drops the job, records a prompt error, never touches `workflowStatus` | a stale `running` | ❌ the stale `running` shadowed the error branch | | ~~mid-run credits (`handleAccountPreconditionError`)~~ | ~~stale `running`~~ | ✅ **already covered** — #15161 clears the status, so it ends via the existing `undefined`-after-`running` branch | In the uncovered cases `runState` sits on `generating` forever and the card keeps promising "Hang tight, your result lands right here the moment it's ready." The controller had two disarms and no re-arm. This makes losing acceptance a signal in its own right: extend the existing `tourRunAccepted` watcher so true -> false fails the run when it is still `generating`. ```ts watch(tourRunAccepted, (accepted) => { if (accepted) stopAcceptDeadline() else if (runState.value === 'generating') runState.value = 'failed' }) ``` No re-armed deadline — this is immediate, not a second timer. The `'generating'` gate is what keeps healthy runs safe: every run leaves the queue when it finishes. `handleExecutionSuccess` and `handleExecutionError` write the terminal status and call `resetExecutionState` in the same synchronous handler, and the status watcher is registered before this one, so a finished run has already left `generating` by the time this fires. Test 3 below covers exactly that tick. Tests, in the existing `'a run behind a dropped socket'` describe: - accepted job removed with no status -> `failed` - accepted job, status `running`, removed (the stagnation case) -> `failed` - accepted job, status `completed` written in the same tick as the removal -> stays `succeeded` - accepted job, status `failed`, then removed -> stays `failed` The first two fail on the base branch (`expected 'generating' to be 'failed'`). Of the last two terminal-status guards, **only the `completed` one discriminates** — ungated, the `failed` case writes `'failed'` over `'failed'` and still passes. It is kept as documentation of intent, not as a guard; the earlier claim that both pinned the gate was wrong. Two tests added in review: - `stays failed when an unrelated workflow churns the status map` — pins the transition gate below. Fails without it (`expected 'generating' to be 'failed'`). - `gives up on a stagnated job that leaves an error and a stale status` — the other half of the stagnation path. Documents it; does not discriminate the gate. **Review fix:** the status watcher's `status === 'running'` branch was unconditional, and its source re-evaluates whenever the `workflowStatus` map is replaced (`mutateStatus` swaps the whole map, so *any* workflow's change re-runs it) or an error flag flips. With a stale `running` sitting there, each re-evaluation put the card back on "your result lands right here" after this watcher had already failed the run. It is now gated on an actual transition into `running`, which also removes the registration-order dependency the old comment overclaimed away. Complementary to #15161 (**merged 2026-08-13**), which fixes the same user-visible symptom from the store side by clearing the stale status. Independent of it: this is the controller refusing to promise regardless of whether the store cleans up. Both are wanted; neither duplicates the other. Requested by @benjcooley in review on #15091 (acceptance-loss watch for the mid-run credits variant) and by CodeRabbit (acceptance-loss coverage, terminal statuses stay terminal). ## Tests - 58 unit tests green in `useFirstRunTourController.test.ts` (54 existing + 4 new), full-file and each new test in isolation. - `pnpm lint`, `pnpm typecheck`, `pnpm format:check` pass. --------- Co-authored-by: Connor Byrne --- .../tour/useFirstRunTourController.test.ts | 110 ++++++++++++++++++ .../tour/useFirstRunTourController.ts | 29 ++++- 2 files changed, 138 insertions(+), 1 deletion(-) diff --git a/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.test.ts b/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.test.ts index b6d64e653d0..2569d66ef63 100644 --- a/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.test.ts +++ b/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.test.ts @@ -202,6 +202,12 @@ function acceptRun(workflow: unknown) { return nextTick() } +/** The queue letting a job go, which `resetExecutionState` does silently. */ +function removeRun() { + mocks.queuedJobs.value = {} + return nextTick() +} + function finishRun(workflow: unknown, status: string) { mocks.workflowStatus.value = new Map(mocks.workflowStatus.value).set( workflow, @@ -506,6 +512,110 @@ describe('useFirstRunTourController', () => { 'the grace timer must not clobber an outcome that arrived before it fired' ).toBe('succeeded') }) + + it('stops promising a result once the queue lets go of its job', async () => { + await tourOnRunStep() + mountRunButton('queue-button', () => {}).click() + await acceptRun(TOUR_WORKFLOW) + + await removeRun() + + expect( + mocks.runState.value, + 'an accepted job that leaves without an outcome leaves the card waiting on a result nobody will send' + ).toBe('failed') + }) + + it('stops promising a result when a running job is dropped mid-run', async () => { + // `handleServiceLevelError` ("Job has stagnated") is the live path: it + // drops the job and records a prompt error but never touches + // `workflowStatus`, so the `running` from `handleExecutionStart` + // outlives the run and no status change reports the end. + // + // Deliberately not the mid-run credits path — #15161 made + // `handleAccountPreconditionError` clear the status, so that one ends + // via the `undefined`-after-`running` branch without this watcher. + await tourOnRunStep() + mountRunButton('queue-button', () => {}).click() + await acceptRun(TOUR_WORKFLOW) + await finishRun(TOUR_WORKFLOW, 'running') + + await removeRun() + + expect( + mocks.runState.value, + 'a run cut short for credits keeps its running status, so losing the job is the only signal left' + ).toBe('failed') + }) + + it('keeps a completed run that drops out of the queue as it finishes', async () => { + await tourOnRunStep() + mountRunButton('queue-button', () => {}).click() + await acceptRun(TOUR_WORKFLOW) + + // `handleExecutionSuccess` reports the outcome and drops the job in one + // tick, so both land before either watcher runs. + void finishRun(TOUR_WORKFLOW, 'completed') + await removeRun() + + expect( + mocks.runState.value, + 'every healthy run leaves the queue when it finishes; failing those would fail every run' + ).toBe('succeeded') + }) + + it('keeps a failed run that leaves the queue after reporting', async () => { + await tourOnRunStep() + mountRunButton('queue-button', () => {}).click() + await acceptRun(TOUR_WORKFLOW) + await finishRun(TOUR_WORKFLOW, 'failed') + + await removeRun() + + expect( + mocks.runState.value, + 'a reported outcome is the last word; losing the job afterwards says nothing new' + ).toBe('failed') + }) + + // Pins the transition gate on the status watcher. The stagnation path + // leaves `running` in `workflowStatus` forever, and that source + // re-evaluates whenever the map is replaced for *any* workflow. Without + // the gate the stale `running` is re-read and the card goes back to + // promising a result it has already given up on. + it('stays failed when an unrelated workflow churns the status map', async () => { + await tourOnRunStep() + mountRunButton('queue-button', () => {}).click() + await acceptRun(TOUR_WORKFLOW) + await finishRun(TOUR_WORKFLOW, 'running') + + await removeRun() + expect(mocks.runState.value).toBe('failed') + + await finishRun(OTHER_WORKFLOW, 'running') + + expect( + mocks.runState.value, + 'another workflow starting is not this run coming back from the dead' + ).toBe('failed') + }) + + // The other half of the stagnation path: the prompt error it records must + // still be able to end the run while the stale `running` sits there. + it('gives up on a stagnated job that leaves an error and a stale status', async () => { + await tourOnRunStep() + mountRunButton('queue-button', () => {}).click() + await acceptRun(TOUR_WORKFLOW) + await finishRun(TOUR_WORKFLOW, 'running') + + mocks.executionErrors.hasPromptError = true + await removeRun() + + expect( + mocks.runState.value, + 'a stagnated run reports an error and abandons the job; the status it leaves behind is not news' + ).toBe('failed') + }) }) describe('run outcome', () => { diff --git a/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.ts b/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.ts index 981c4a08ccf..44fbddacb43 100644 --- a/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.ts +++ b/src/renderer/extensions/firstRunTour/tour/useFirstRunTourController.ts @@ -75,7 +75,16 @@ function useFirstRunTourControllerInternal() { ], ([status, refused], previous) => { if (status !== undefined) stopAcceptDeadline() - if (status === 'running') runState.value = 'generating' + // Only an actual transition into `running` starts the wait. This source + // re-evaluates whenever the `workflowStatus` map is replaced — which + // `mutateStatus` does for *any* workflow — or whenever an error flag + // flips. Paths that drop a job without clearing its status leave + // `running` behind forever (`handleServiceLevelError` is the live one), + // so an unconditional branch here would re-read that stale value and put + // the card back on "your result lands right here" after the watcher + // below has already failed the run. + if (status === 'running' && previous?.[0] !== 'running') + runState.value = 'generating' else if (status === 'completed') runState.value = 'succeeded' else if (status === 'failed') runState.value = 'failed' // A refused run never queues; a stopped one drops its status rather than @@ -118,9 +127,27 @@ function useFirstRunTourControllerInternal() { * Acceptance is not the only disarm. `resetExecutionState` drops a job from * `queuedJobs` without clearing its status, so a run can report a status * while this reads false. A refusal produces neither signal. + * + * Losing acceptance is itself a signal, not a re-armed deadline. Two paths + * drop the job without ever writing an outcome: + * + * - an accepted job that disappears with **no status written at all** — the + * cloud "waiting for a machine" job that is cancelled or reconciled away + * - `handleServiceLevelError` ("Job has stagnated"), which drops the job and + * records a prompt error but never touches `workflowStatus`, so the + * `running` written by `handleExecutionStart` outlives the run + * + * Not the mid-run credits path: #15161 made + * `handleAccountPreconditionError` clear the status, so that one already + * ends via the `undefined`-after-`running` branch above. + * + * A finished run leaves the queue too, but reports a terminal status in the + * same flush, and the terminal branches above overwrite unconditionally — so + * the outcome wins whichever watcher runs first. */ watch(tourRunAccepted, (accepted) => { if (accepted) stopAcceptDeadline() + else if (runState.value === 'generating') runState.value = 'failed' }) let acceptTimer: ReturnType | undefined