-
Notifications
You must be signed in to change notification settings - Fork 0
feat: use interrupt() as graceful timeout alternative #353
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
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,104 @@ | ||
| /** | ||
| * Two-tier timeout strategy for graceful query interruption. | ||
| * | ||
| * Supports two strategies: | ||
| * - "abort_only": Single hard abort after timeout_ms (legacy behavior) | ||
| * - "interrupt_first": Soft interrupt at timeout_ms, hard abort after grace period | ||
| */ | ||
|
|
||
| import { createInterruptedError } from "./transcript-builder.js"; | ||
|
|
||
| import type { Query } from "./sdk-client.js"; | ||
| import type { | ||
| TimeoutStrategy, | ||
| TranscriptErrorEvent, | ||
| } from "../../types/index.js"; | ||
|
|
||
| /** Mutable holder for a Query reference, allowing timeout callbacks to access the query lazily. */ | ||
| export interface QueryHolder { | ||
| query: Query | undefined; | ||
| } | ||
|
|
||
| /** Result of createTimeout with cleanup and state tracking. */ | ||
| export interface TwoTierTimeout { | ||
| /** Call to clear all pending timers */ | ||
| cleanup: () => void; | ||
| /** Whether an interrupt was fired (for error classification) */ | ||
| interrupted: { value: boolean }; | ||
| } | ||
|
|
||
| export interface TimeoutConfig { | ||
| timeout_ms: number; | ||
| timeout_strategy: TimeoutStrategy; | ||
| interrupt_grace_ms: number; | ||
| } | ||
|
|
||
| /** | ||
| * Create a timeout mechanism for scenario execution. | ||
| * | ||
| * - `abort_only`: Single `setTimeout(() => abort(), timeout_ms)` | ||
| * - `interrupt_first`: Soft timeout calls `query.interrupt()`, then schedules | ||
| * hard abort after `interrupt_grace_ms`. If query isn't created yet, falls | ||
| * through to hard abort immediately. | ||
| */ | ||
| export function createTimeout( | ||
| controller: AbortController, | ||
| queryHolder: QueryHolder, | ||
| config: TimeoutConfig, | ||
| ): TwoTierTimeout { | ||
| const interrupted = { value: false }; | ||
| let softTimer: ReturnType<typeof setTimeout> | undefined; | ||
| let hardTimer: ReturnType<typeof setTimeout> | undefined; | ||
|
|
||
| const hardAbort = (): void => { | ||
| controller.abort(); | ||
| }; | ||
|
|
||
| if (config.timeout_strategy === "abort_only") { | ||
| softTimer = setTimeout(hardAbort, config.timeout_ms); | ||
| } else { | ||
| // interrupt_first strategy | ||
| softTimer = setTimeout(() => { | ||
| const query = queryHolder.query; | ||
| if (query) { | ||
| interrupted.value = true; | ||
| query.interrupt().catch(() => { | ||
| // Swallow errors — hard abort is the fallback | ||
| }); | ||
| // Schedule hard abort after grace period | ||
| hardTimer = setTimeout(hardAbort, config.interrupt_grace_ms); | ||
| } else { | ||
| // Query not yet created — fall through to hard abort | ||
| hardAbort(); | ||
| } | ||
| }, config.timeout_ms); | ||
| } | ||
|
|
||
| const cleanup = (): void => { | ||
| if (softTimer !== undefined) { | ||
| clearTimeout(softTimer); | ||
| softTimer = undefined; | ||
| } | ||
| if (hardTimer !== undefined) { | ||
| clearTimeout(hardTimer); | ||
| hardTimer = undefined; | ||
| } | ||
| }; | ||
|
|
||
| return { cleanup, interrupted }; | ||
| } | ||
|
|
||
| /** | ||
| * If an interrupt was fired but no hard abort (timeout) error was recorded, | ||
| * push an "interrupted" error event onto the errors array. | ||
| */ | ||
| export function addInterruptErrorIfNeeded( | ||
| interrupted: { value: boolean }, | ||
| errors: TranscriptErrorEvent[], | ||
| ): void { | ||
| if (interrupted.value && !errors.some((e) => e.error_type === "timeout")) { | ||
| errors.push( | ||
| createInterruptedError("Execution interrupted by soft timeout"), | ||
| ); | ||
| } | ||
| } |
Oops, something went wrong.
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.