Repository navigation
fix(automations): stop headless runs failing at the 5-minute tui-idle default #22731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
leilei3167
wants to merge
8
commits into
stablyai:main
from
leilei3167:fix/issue-22725-headless-tui-idle-timeout
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bfadf32
fix(automations): stop headless runs failing at the 5-minute tui-idle…
leilei3167 2f6359c
fix(automations): observe headless completion before worktree lookup
leilei3167 30f03c1
fix(automations): settle headless completion before worktree await
leilei3167 970e45c
fix(automations): treat exited terminals as completion, not stale idle
leilei3167 d5ecb50
test(automations): align terminal wait fakes with RuntimeTerminalWait
cursoragent 31a88e0
fix(automations): abort headless completion when service stops
cursoragent e387f24
fix(automations): wire completionSignal through headless dispatch runner
cursoragent e53ed09
test(automations): await dispatched status after wrapped headless dis…
cursoragent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import type { | ||
| HeadlessAutomationDispatcher, | ||
| HeadlessAutomationDispatchLaunch | ||
| } from './headless-dispatch' | ||
|
|
||
| /** Tracks headless completion observers so AutomationService.stop() can abort them. */ | ||
| export class HeadlessCompletionAbortRegistry { | ||
| private readonly controllers = new Map<string, AbortController>() | ||
|
|
||
| beginRun(runId: string): AbortController { | ||
| const controller = new AbortController() | ||
| this.controllers.set(runId, controller) | ||
| return controller | ||
| } | ||
|
|
||
| wrapDispatcher(inner: HeadlessAutomationDispatcher): HeadlessAutomationDispatcher { | ||
| return async (request) => { | ||
| const controller = this.controllers.get(request.run.id) | ||
| if (!controller || controller.signal !== request.completionSignal) { | ||
| throw new Error('Headless completion abort must begin before dispatch.') | ||
| } | ||
| try { | ||
| const launch = await inner(request) | ||
| this.trackCompletion(request.run.id, controller, launch) | ||
| return { ...launch, completionAbortSignal: controller.signal } | ||
| } catch (error) { | ||
| this.controllers.delete(request.run.id) | ||
| throw error | ||
| } | ||
| } | ||
| } | ||
|
|
||
| abortAll(): void { | ||
| for (const controller of this.controllers.values()) { | ||
| controller.abort() | ||
| } | ||
| this.controllers.clear() | ||
| } | ||
|
|
||
| private trackCompletion( | ||
| runId: string, | ||
| controller: AbortController, | ||
| launch: HeadlessAutomationDispatchLaunch | ||
| ): void { | ||
| if (!launch.completion) { | ||
| this.controllers.delete(runId) | ||
| return | ||
| } | ||
| void launch.completion | ||
| .finally(() => { | ||
| if (this.controllers.get(runId) === controller) { | ||
| this.controllers.delete(runId) | ||
| } | ||
| }) | ||
| .catch(() => {}) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
src/main/automations/headless-dispatch-terminal-completion.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import { createHeadlessAutomationCompletion } from './headless-dispatch-terminal-completion' | ||
| import { | ||
| createRuntimeAutomationRunTerminalObserver, | ||
| type AutomationRunTerminalHost | ||
| } from './runtime-terminal-run-observer' | ||
|
|
||
| const HANDLE = 'terminal-1' | ||
| const RUNTIME_TUI_IDLE_TIMEOUT_MS = 5 * 60 * 1000 | ||
|
|
||
| type FakePane = { | ||
| lastAgentStatus: 'idle' | 'working' | 'permission' | null | ||
| } | ||
|
|
||
| type FakeWaitResult = { | ||
| handle: string | ||
| condition: 'tui-idle' | ||
| satisfied: boolean | ||
| status: 'running' | 'exited' | 'unknown' | ||
| exitCode: number | null | ||
| } | ||
|
|
||
| type FakeWaiter = { | ||
| resolve: (value: FakeWaitResult) => void | ||
| reject: (error: Error) => void | ||
| timer: ReturnType<typeof setTimeout> | ||
| } | ||
|
|
||
| function createFakeRuntime(initial: Partial<FakePane>) { | ||
| const pane: FakePane = { | ||
| lastAgentStatus: null, | ||
| ...initial | ||
| } | ||
| const waiters = new Set<FakeWaiter>() | ||
|
|
||
| const satisfiedNow = (): boolean => pane.lastAgentStatus === 'idle' | ||
|
|
||
| const runtime: AutomationRunTerminalHost & { | ||
| setPane: (next: Partial<FakePane>) => void | ||
| } = { | ||
| getTerminalHandleForPaneKey: () => HANDLE, | ||
| readTerminal: async () => ({ tail: ['agent finished after a long command'] }), | ||
| waitForTerminal: (_handle, options) => { | ||
| if (options?.signal?.aborted) { | ||
| return Promise.reject(new Error('request_aborted')) | ||
| } | ||
| if (satisfiedNow()) { | ||
| return Promise.resolve({ | ||
| handle: HANDLE, | ||
| condition: 'tui-idle', | ||
| satisfied: true, | ||
| status: 'running', | ||
| exitCode: null | ||
| }) | ||
| } | ||
| return new Promise((resolve, reject) => { | ||
| const waiter: FakeWaiter = { | ||
| resolve, | ||
| reject, | ||
| timer: setTimeout(() => { | ||
| waiters.delete(waiter) | ||
| reject(new Error('timeout')) | ||
| }, options?.timeoutMs ?? RUNTIME_TUI_IDLE_TIMEOUT_MS) | ||
| } | ||
| waiters.add(waiter) | ||
| }) | ||
| }, | ||
| setPane: (next) => { | ||
| Object.assign(pane, next) | ||
| if (!satisfiedNow()) { | ||
| return | ||
| } | ||
| for (const waiter of waiters) { | ||
| waiters.delete(waiter) | ||
| clearTimeout(waiter.timer) | ||
| waiter.resolve({ | ||
| handle: HANDLE, | ||
| condition: 'tui-idle', | ||
| satisfied: true, | ||
| status: 'running', | ||
| exitCode: null | ||
| }) | ||
| } | ||
| } | ||
| } | ||
| return runtime | ||
| } | ||
|
|
||
| describe('createHeadlessAutomationCompletion (#22725)', () => { | ||
| beforeEach(() => { | ||
| vi.useFakeTimers() | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers() | ||
| }) | ||
|
|
||
| it('routes headless completion through observeCompletion', async () => { | ||
| const observeCompletion = vi.fn(async () => ({ | ||
| status: 'completed' as const, | ||
| outputSnapshot: null, | ||
| error: null | ||
| })) | ||
| await expect( | ||
| createHeadlessAutomationCompletion({ | ||
| observeCompletion, | ||
| terminalHandle: HANDLE, | ||
| signal: new AbortController().signal | ||
| }) | ||
| ).resolves.toMatchObject({ status: 'completed', error: null }) | ||
| expect(observeCompletion).toHaveBeenCalledWith( | ||
| HANDLE, | ||
| expect.objectContaining({ signal: expect.any(AbortSignal) }) | ||
| ) | ||
| }) | ||
|
|
||
| it('still completes after the agent works past the 5-minute tui-idle default', async () => { | ||
| const runtime = createFakeRuntime({ lastAgentStatus: 'working' }) | ||
| const observer = createRuntimeAutomationRunTerminalObserver(runtime) | ||
| const completion = createHeadlessAutomationCompletion({ | ||
| observeCompletion: (handle, options) => observer.observeCompletion(handle, options), | ||
| terminalHandle: HANDLE, | ||
| signal: new AbortController().signal | ||
| }) | ||
|
|
||
| // Agent keeps working past the runtime default that used to reject headless. | ||
| await vi.advanceTimersByTimeAsync(RUNTIME_TUI_IDLE_TIMEOUT_MS + 18_000) | ||
| runtime.setPane({ lastAgentStatus: 'idle' }) | ||
| await vi.advanceTimersByTimeAsync(10) | ||
|
|
||
| await expect(completion).resolves.toMatchObject({ | ||
| status: 'completed', | ||
| error: null | ||
| }) | ||
| }) | ||
| }) |
21 changes: 21 additions & 0 deletions
21
src/main/automations/headless-dispatch-terminal-completion.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import type { | ||
| AutomationRunCompletionObservation, | ||
| AutomationRunTerminalObserver | ||
| } from './run-completion-watcher' | ||
|
|
||
| /** | ||
| * Headless dispatch used to call `waitForTerminal({ condition: 'tui-idle' })` | ||
| * with no `timeoutMs`, so it inherited the runtime's 5-minute default and marked | ||
| * longer agent runs as `dispatch_failed` / `timeout` while the agent was still | ||
| * working (#22725). Route through the same observer retained runs use so each | ||
| * tui-idle timeout re-arms until the shared 6h deadline. | ||
| */ | ||
| export function createHeadlessAutomationCompletion(args: { | ||
| observeCompletion: AutomationRunTerminalObserver['observeCompletion'] | ||
| terminalHandle: string | ||
| signal: AbortSignal | ||
| }): Promise<AutomationRunCompletionObservation> { | ||
| return args.observeCompletion(args.terminalHandle, { | ||
| signal: args.signal | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.