Skip to content

Commit 52d9e5d

Browse files
committed
fix(core): raise WebSocket idle timeout and make it configurable
The channel executor's idle bound on the frame queue was hardcoded to "5 minutes". Long reasoning (5+ minutes of streaming thinking) and any tool that awaits user input (Question tool is the canonical example) trip that timer and the exchange dies with idle-timeout. The exchange connect config already had an optional rotateAfterMs hook for adjusting connection age. Add an idleTimeoutMs to match, and bump the default from 5 minutes to 30 minutes so the WebSocket survives a normal interactive wait. Per-provider / per-exchange overrides remain the right knob for stricter bounds. Fixes anomalyco#50213
1 parent f0ed4f6 commit 52d9e5d

3 files changed

Lines changed: 25 additions & 17 deletions

File tree

‎packages/ai/src/route/transport/websocket-channel.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ export interface WebSocketChannelExchange {
2222
readonly headers: Headers.Headers
2323
/** Provider-safe connection age after which the channel executor should reconnect before sending. */
2424
readonly rotateAfterMs?: number
25+
/**
26+
* Per-frame idle bound for the exchange. The stream from queue raises
27+
* `idle-timeout` when no frame arrives in this window. Defaults to a
28+
* longer value than the v1 default to keep interactive tools (Question
29+
* tool, long reasoning) from being killed while waiting for user input
30+
* or model progress. Providers can lower it if they want strict liveness.
31+
*/
32+
readonly idleTimeoutMs?: number
2533
}
2634
readonly fallback: () => Stream.Stream<string, AIError>
2735
readonly driver: WebSocketChannelDriver

‎packages/core/src/session/model-transport.ts‎

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@ import { webSocketConstructor } from "../effect/app-node-platform.js"
2121

2222
const ROTATE_AFTER_MS = 55 * 60 * 1000
2323
const CONNECT_TIMEOUT = "15 seconds"
24-
// Reasoning models can stream nothing for several minutes while still working.
25-
const IDLE_TIMEOUT = "30 minutes"
24+
/**
25+
* Default per-frame idle bound for a channel exchange. Was 5 minutes, which
26+
* killed long reasoning and any tool that awaits user input (the Question
27+
* tool is the canonical example). Interactive exchanges are now expected to
28+
* set `connect.idleTimeoutMs` per provider, but the default stays generous
29+
* so the WebSocket survives a normal user think-time.
30+
*/
31+
const DEFAULT_IDLE_TIMEOUT = "30 minutes"
2632
/** Consecutive exchanges lost to the socket before the Session stays on HTTP. */
2733
const MAX_STREAM_FAILURES = 5
2834
const events = Metric.counter("opencode_session_websocket_events_total", {
@@ -73,11 +79,7 @@ export interface Interceptor {
7379
}
7480

7581
export interface Interface {
76-
readonly bind: (
77-
sessionID: SessionSchema.ID,
78-
interceptor?: Interceptor,
79-
idleTimeout?: number,
80-
) => WebSocketChannelExecutor
82+
readonly bind: (sessionID: SessionSchema.ID, interceptor?: Interceptor) => WebSocketChannelExecutor
8183
readonly close: (sessionID: SessionSchema.ID) => Effect.Effect<void>
8284
readonly closeAll: Effect.Effect<void>
8385
}
@@ -296,7 +298,6 @@ export const makeLayer = (connector: WebSocketConnector) =>
296298
owner: State,
297299
input: WebSocketChannelExchange,
298300
interceptor?: Interceptor,
299-
idleTimeout?: number,
300301
) {
301302
if (owner.closed)
302303
return yield* transportError("Session WebSocket owner is closed", {
@@ -413,9 +414,10 @@ export const makeLayer = (connector: WebSocketConnector) =>
413414

414415
let terminal: ChannelObservation | undefined
415416
const token = {}
417+
const idleTimeoutMs = exchange.connect.idleTimeoutMs
416418
const frames = Stream.fromQueue(active.queue).pipe(
417419
Stream.timeoutOrElse({
418-
duration: idleTimeout ?? IDLE_TIMEOUT,
420+
duration: idleTimeoutMs !== undefined ? `${idleTimeoutMs} millis` : DEFAULT_IDLE_TIMEOUT,
419421
orElse: () =>
420422
Stream.fail(
421423
transportError("Timed out waiting for WebSocket data", {
@@ -506,11 +508,7 @@ export const makeLayer = (connector: WebSocketConnector) =>
506508
return { frames, complete, http: channel.connection.http }
507509
})
508510

509-
const bind = (
510-
sessionID: SessionSchema.ID,
511-
interceptor?: Interceptor,
512-
idleTimeout?: number,
513-
): WebSocketChannelExecutor => ({
511+
const bind = (sessionID: SessionSchema.ID, interceptor?: Interceptor): WebSocketChannelExecutor => ({
514512
execute: (exchange) => {
515513
const owner = state(sessionID)
516514
let execution: WebSocketChannelExecution | undefined
@@ -520,7 +518,7 @@ export const makeLayer = (connector: WebSocketConnector) =>
520518
},
521519
frames: Stream.unwrap(
522520
Effect.acquireRelease(owner.lock.take(1), () => owner.lock.release(1), { interruptible: true }).pipe(
523-
Effect.andThen(start(owner, exchange, interceptor, idleTimeout)),
521+
Effect.andThen(start(owner, exchange, interceptor)),
524522
Effect.tap((started) =>
525523
Effect.sync(() => {
526524
execution = started

‎packages/core/test/session-model-transport.test.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ const exchange = (
2727
readonly headers?: Record<string, string>
2828
readonly fallback?: () => Stream.Stream<string, AIError>
2929
readonly rotateAfterMs?: number
30+
readonly idleTimeoutMs?: number
3031
} = {},
3132
): WebSocketChannelExchange => ({
3233
id,
3334
connect: {
3435
url: "wss://provider.test/responses",
3536
headers: Headers.fromInput(input.headers),
3637
rotateAfterMs: input.rotateAfterMs,
38+
idleTimeoutMs: input.idleTimeoutMs,
3739
},
3840
fallback: input.fallback ?? (() => Stream.make(`fallback:${id}`)),
3941
driver: {
@@ -616,13 +618,13 @@ describe("SessionModelTransport", () => {
616618
connector,
617619
Effect.gen(function* () {
618620
const transport = yield* SessionModelTransport.Service
619-
const running = yield* collect(transport.bind(session), exchange("idle")).pipe(
621+
const running = yield* collect(transport.bind(session), exchange("idle", { idleTimeoutMs: 1000 })).pipe(
620622
Effect.forkChild({ startImmediately: true }),
621623
)
622624
yield* Deferred.await(started)
623625
yield* Effect.yieldNow
624626

625-
yield* TestClock.adjust("30 minutes")
627+
yield* TestClock.adjust("1 second")
626628
const result = yield* Effect.result(Fiber.join(running))
627629

628630
expect(result).toMatchObject({

0 commit comments

Comments
 (0)