-
Notifications
You must be signed in to change notification settings - Fork 0
fix: add model stream idle timeout #117
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
Open
byapparov
wants to merge
7
commits into
main
Choose a base branch
from
backport/issue-80
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b1dfcd9
feat(session): add model stream idle timeout
fe1cb16
fix(session): address idle timeout review
99a99db
test(session): clarify iterator cleanup assertion
0538c5f
fix(session): suspend idle timeout for local tools
byapparov 07e59ce
fix(sdk): expose stream idle timeout errors
byapparov 1911854
fix(session): bound local tool timeout suspension
byapparov ef850e7
fix(session): cover provider tool execution
byapparov 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { MessageV2 } from "./message-v2" | ||
|
|
||
| export namespace StreamIdle { | ||
| function error(ms: number, message: string) { | ||
| return new MessageV2.StreamIdleTimeoutError({ | ||
| message, | ||
| timeout: ms, | ||
| }) | ||
| } | ||
|
|
||
| export function signal(input?: AbortSignal) { | ||
| const controller = new AbortController() | ||
| return { | ||
| controller, | ||
| signal: input ? AbortSignal.any([input, controller.signal]) : controller.signal, | ||
| } | ||
| } | ||
|
|
||
| export async function* timeout<T>( | ||
| stream: AsyncIterable<T>, | ||
| ms: number, | ||
| abort: () => void, | ||
| updateSuspended: (value: T) => boolean = () => false, | ||
| suspendedTimeout = ms, | ||
| ) { | ||
| if (ms === 0) { | ||
| yield* stream | ||
| return | ||
| } | ||
|
|
||
| const iterator = stream[Symbol.asyncIterator]() | ||
| let suspended = false | ||
| try { | ||
| while (true) { | ||
| const timer = Promise.withResolvers<never>() | ||
| const appliedTimeout = suspended ? suspendedTimeout : ms | ||
| const id = setTimeout(() => { | ||
| timer.reject( | ||
| error( | ||
| appliedTimeout, | ||
| suspended | ||
| ? `Tool execution produced no result for ${appliedTimeout}ms` | ||
| : `Model stream produced no events for ${appliedTimeout}ms`, | ||
| ), | ||
| ) | ||
| abort() | ||
| }, appliedTimeout) | ||
| const next = await Promise.race([iterator.next(), timer.promise]).finally(() => clearTimeout(id)) | ||
| if (next.done) return | ||
| suspended = updateSuspended(next.value) | ||
| yield next.value | ||
| } | ||
| } finally { | ||
| // Do not await cleanup: an async generator queues return() behind an | ||
| // in-flight next(), which may never settle for the stalled stream we are | ||
| // escaping. The abort above gives cooperative providers a chance to close. | ||
| try { | ||
| iterator.return?.().catch(() => {}) | ||
| } 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
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,32 @@ | ||
| import { describe, expect, test } from "bun:test" | ||
| import { Flag } from "../../src/flag/flag" | ||
|
|
||
| describe("AICTRL_MODEL_STREAM_IDLE_TIMEOUT_MS", () => { | ||
| test("supports default, override, disable, and invalid fallback", () => { | ||
| const original = process.env.AICTRL_MODEL_STREAM_IDLE_TIMEOUT_MS | ||
|
|
||
| try { | ||
| delete process.env.AICTRL_MODEL_STREAM_IDLE_TIMEOUT_MS | ||
| expect(Flag.AICTRL_MODEL_STREAM_IDLE_TIMEOUT_MS).toBe(300_000) | ||
| process.env.AICTRL_MODEL_STREAM_IDLE_TIMEOUT_MS = "1234" | ||
| expect(Flag.AICTRL_MODEL_STREAM_IDLE_TIMEOUT_MS).toBe(1234) | ||
| process.env.AICTRL_MODEL_STREAM_IDLE_TIMEOUT_MS = "0" | ||
| expect(Flag.AICTRL_MODEL_STREAM_IDLE_TIMEOUT_MS).toBe(0) | ||
| process.env.AICTRL_MODEL_STREAM_IDLE_TIMEOUT_MS = "invalid" | ||
| expect(Flag.AICTRL_MODEL_STREAM_IDLE_TIMEOUT_MS).toBe(300_000) | ||
| process.env.AICTRL_MODEL_STREAM_IDLE_TIMEOUT_MS = "1e3" | ||
| expect(Flag.AICTRL_MODEL_STREAM_IDLE_TIMEOUT_MS).toBe(300_000) | ||
| process.env.AICTRL_MODEL_STREAM_IDLE_TIMEOUT_MS = "0x10" | ||
| expect(Flag.AICTRL_MODEL_STREAM_IDLE_TIMEOUT_MS).toBe(300_000) | ||
| process.env.AICTRL_MODEL_STREAM_IDLE_TIMEOUT_MS = "2147483647" | ||
| expect(Flag.AICTRL_MODEL_STREAM_IDLE_TIMEOUT_MS).toBe(2_147_483_647) | ||
| process.env.AICTRL_MODEL_STREAM_IDLE_TIMEOUT_MS = "2147483648" | ||
| expect(Flag.AICTRL_MODEL_STREAM_IDLE_TIMEOUT_MS).toBe(300_000) | ||
| process.env.AICTRL_MODEL_STREAM_IDLE_TIMEOUT_MS = "9007199254740992" | ||
| expect(Flag.AICTRL_MODEL_STREAM_IDLE_TIMEOUT_MS).toBe(300_000) | ||
| } finally { | ||
| if (original === undefined) delete process.env.AICTRL_MODEL_STREAM_IDLE_TIMEOUT_MS | ||
| else process.env.AICTRL_MODEL_STREAM_IDLE_TIMEOUT_MS = original | ||
| } | ||
| }) | ||
| }) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 New persisted error variant vs older readers.
🤖 Fix with your agent
Why this matters
StreamIdleTimeoutError is added to the persisted AssistantMessage.error zod union and the SDK wire types (EventSessionError). Older CLI/SDK builds whose error union lacks this variant will fail to parse (or drop) a persisted assistant message saved by this version after a rollback or in mixed-version setups. Worth confirming the deserialize path degrades gracefully (e.g. falls back to NamedError.Unknown) for unknown error names.