Skip to content

Commit 4eb29a6

Browse files
authored
fix(opencode): default chunk timeout to five minutes (anomalyco#46890)
1 parent 68abdce commit 4eb29a6

6 files changed

Lines changed: 95 additions & 11 deletions

File tree

‎packages/core/src/v1/config/provider.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,14 @@ export const Info = Schema.Struct({
114114
description:
115115
"Timeout in milliseconds to wait for response headers. Provider integrations may set defaults. Set to false to disable timeout.",
116116
}),
117-
chunkTimeout: Schema.optional(PositiveInt).annotate({
117+
chunkTimeout: Schema.optional(
118+
Schema.Union([PositiveInt, Schema.Literal(false)]).annotate({
119+
description:
120+
"Timeout in milliseconds between streamed SSE chunks for this provider (default: 300000). If no chunk arrives within this window, the request is aborted. Set to false to disable timeout.",
121+
}),
122+
).annotate({
118123
description:
119-
"Timeout in milliseconds between streamed SSE chunks for this provider. If no chunk arrives within this window, the request is aborted.",
124+
"Timeout in milliseconds between streamed SSE chunks for this provider (default: 300000). If no chunk arrives within this window, the request is aborted. Set to false to disable timeout.",
120125
}),
121126
}),
122127
[Schema.Record(Schema.String, Schema.Any)],

‎packages/opencode/src/provider/provider.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1792,7 +1792,7 @@ const layer = Layer.effect(
17921792
if (existing) return existing
17931793

17941794
const customFetch = options["fetch"]
1795-
const chunkTimeout = options["chunkTimeout"]
1795+
const chunkTimeout = options["chunkTimeout"] ?? 300_000
17961796
const headerTimeout = options["headerTimeout"]
17971797
delete options["chunkTimeout"]
17981798
delete options["headerTimeout"]

‎packages/opencode/test/provider/header-timeout.test.ts‎

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { Env } from "@/env"
1313
import { Plugin } from "@/plugin"
1414
import { Provider } from "@/provider/provider"
1515
import { ProviderError } from "@/provider/error"
16+
import { MessageV2 } from "@/session/message-v2"
17+
import { SessionRetry } from "@/session/retry"
1618

1719
afterEach(async () => {
1820
await disposeAllInstances()
@@ -46,7 +48,42 @@ it.live("headerTimeout does not abort delayed SSE body after headers arrive", ()
4648
}),
4749
)
4850

49-
it.live("chunkTimeout raises a response stream error when SSE body stalls", () =>
51+
it.live("default chunkTimeout is applied at fetch without changing provider options", () =>
52+
Effect.gen(function* () {
53+
const server = yield* Effect.acquireRelease(
54+
Effect.promise(() => delayedBodyServer(250)),
55+
(server) => Effect.sync(() => server.server.close()),
56+
)
57+
58+
yield* provideTmpdirInstance(
59+
() =>
60+
Effect.gen(function* () {
61+
const provider = yield* Provider.Service
62+
const configured = yield* provider.getProvider(ProviderV2.ID.make("test"))
63+
const signals: (AbortSignal | null | undefined)[] = []
64+
configured.options.fetch = (input: RequestInfo | URL, init?: RequestInit) => {
65+
signals.push(init?.signal)
66+
return fetch(input, init)
67+
}
68+
const model = yield* provider.getModel(ProviderV2.ID.make("test"), ModelV2.ID.make("test-model"))
69+
const language = yield* provider.getLanguage(model)
70+
yield* Effect.acquireRelease(
71+
Effect.promise(() =>
72+
language.doStream({ prompt: [{ role: "user", content: [{ type: "text", text: "hello" }] }] }),
73+
),
74+
(result) => Effect.promise(() => result.stream.cancel()),
75+
)
76+
77+
expect(signals).toHaveLength(1)
78+
expect(signals[0]).toBeInstanceOf(AbortSignal)
79+
expect(configured.options.chunkTimeout).toBeUndefined()
80+
}),
81+
{ config: providerConfig(server.url) },
82+
)
83+
}),
84+
)
85+
86+
it.live("configured chunkTimeout raises a retryable response stream error when SSE body stalls", () =>
5087
Effect.gen(function* () {
5188
const server = yield* Effect.acquireRelease(
5289
Effect.promise(() => delayedBodyServer(250)),
@@ -74,12 +111,41 @@ it.live("chunkTimeout raises a response stream error when SSE body stalls", () =
74111
}
75112
})
76113
expect(error).toBeInstanceOf(ProviderError.ResponseStreamError)
114+
expect(
115+
SessionRetry.retryable(MessageV2.fromError(error, { providerID: model.providerID }), model.providerID),
116+
).toEqual({ message: "SSE read timed out" })
77117
}),
78118
{ config: providerConfig(server.url, { chunkTimeout: 50 }) },
79119
)
80120
}),
81121
)
82122

123+
it.live("chunkTimeout can be disabled with false", () =>
124+
Effect.gen(function* () {
125+
const server = yield* Effect.acquireRelease(
126+
Effect.promise(() => delayedBodyServer(250)),
127+
(server) => Effect.sync(() => server.server.close()),
128+
)
129+
130+
yield* provideTmpdirInstance(
131+
() =>
132+
Effect.gen(function* () {
133+
const provider = yield* Provider.Service
134+
const configured = yield* provider.getProvider(ProviderV2.ID.make("test"))
135+
expect(configured.options.chunkTimeout).toBe(false)
136+
const model = yield* provider.getModel(ProviderV2.ID.make("test"), ModelV2.ID.make("test-model"))
137+
const result = streamText({
138+
model: yield* provider.getLanguage(model),
139+
messages: [{ role: "user", content: "hello" }],
140+
})
141+
142+
expect(yield* Effect.promise(() => result.text)).toBe("late")
143+
}),
144+
{ config: providerConfig(server.url, { chunkTimeout: false }) },
145+
)
146+
}),
147+
)
148+
83149
it.live("headerTimeout aborts when response headers do not arrive", () =>
84150
Effect.gen(function* () {
85151
const server = yield* Effect.acquireRelease(
@@ -136,7 +202,7 @@ it.live("headerTimeout is opt-in for non-OpenAI providers", () =>
136202
}),
137203
)
138204

139-
it.live("OpenAI Codex headerTimeout default can be disabled by config", () =>
205+
it.live("OpenAI Codex header and chunk timeout defaults can be disabled by config", () =>
140206
Effect.gen(function* () {
141207
yield* withAuthContent(
142208
Effect.gen(function* () {
@@ -146,8 +212,9 @@ it.live("OpenAI Codex headerTimeout default can be disabled by config", () =>
146212
const provider = yield* Provider.Service
147213
const openai = yield* provider.getProvider(ProviderV2.ID.openai)
148214
expect(openai.options.headerTimeout).toBe(false)
215+
expect(openai.options.chunkTimeout).toBe(false)
149216
}),
150-
{ config: { provider: { openai: { options: { headerTimeout: false } } } } },
217+
{ config: { provider: { openai: { options: { headerTimeout: false, chunkTimeout: false } } } } },
151218
)
152219
}),
153220
)

‎packages/sdk/js/src/v2/gen/types.gen.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,8 +1754,11 @@ export type ProviderConfig = {
17541754
* Timeout in milliseconds to wait for response headers. Provider integrations may set defaults. Set to false to disable timeout.
17551755
*/
17561756
headerTimeout?: number | false
1757-
chunkTimeout?: number
1758-
[key: string]: unknown | string | boolean | number | false | number | false | number | undefined
1757+
/**
1758+
* Timeout in milliseconds between streamed SSE chunks for this provider (default: 300000). If no chunk arrives within this window, the request is aborted. Set to false to disable timeout.
1759+
*/
1760+
chunkTimeout?: number | false
1761+
[key: string]: unknown | string | boolean | number | false | number | false | number | false | undefined
17591762
}
17601763
models?: {
17611764
[key: string]: {

‎packages/sdk/openapi.json‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20783,8 +20783,17 @@
2078320783
"description": "Timeout in milliseconds to wait for response headers. Provider integrations may set defaults. Set to false to disable timeout."
2078420784
},
2078520785
"chunkTimeout": {
20786-
"type": "integer",
20787-
"exclusiveMinimum": 0
20786+
"anyOf": [
20787+
{
20788+
"type": "integer",
20789+
"exclusiveMinimum": 0
20790+
},
20791+
{
20792+
"type": "boolean",
20793+
"enum": [false]
20794+
}
20795+
],
20796+
"description": "Timeout in milliseconds between streamed SSE chunks for this provider (default: 300000). If no chunk arrives within this window, the request is aborted. Set to false to disable timeout."
2078820797
}
2078920798
},
2079020799
"additionalProperties": {}

‎packages/web/src/content/docs/config.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ Provider options can include `timeout`, `chunkTimeout`, and `setCacheKey`:
392392
```
393393

394394
- `timeout` - Request timeout in milliseconds (default: 300000). Set to `false` to disable.
395-
- `chunkTimeout` - Timeout in milliseconds between streamed response chunks. If no chunk arrives in time, the request is aborted.
395+
- `chunkTimeout` - Timeout in milliseconds between streamed response chunks (default: 300000, or 5 minutes). If no chunk arrives in time, the request is aborted. Set to `false` to disable.
396396
- `setCacheKey` - Ensure a cache key is always set for designated provider.
397397

398398
You can also configure [local models](/docs/models#local). [Learn more](/docs/models).

0 commit comments

Comments
 (0)