Repository navigation
feat(gateway): SSE keepalive comment frames on /v1/chat/completions streams - #68
Merged
Merged
Conversation
…treams Full-agent runs emit no assistant deltas while tools execute, so the SSE stream goes silent for minutes. Consumers (e.g. the BenchAGI agent-chat runtime, 2026-06-12 incident) cannot distinguish 'working' from 'dead' and are forced into turn-length read timeouts — long tasks get killed by an arbitrary deadline. - Streaming handler writes ': keepalive\n\n' SSE comment frames every gateway.http.endpoints.chatCompletions.sseKeepaliveIntervalMs (default 15000; 0 disables). Comment frames are ignored by spec-compliant SSE and OpenAI clients. - Timer is unref'd, stops in maybeFinalize, and res 'close' is the catch-all cleanup so no end path can leak the interval. - Config key added to GatewayHttpChatCompletionsConfig + zod schema (additive, optional). - Tests pin the config resolution (default / explicit / 0-disables / clamp semantics). With keepalives in place, stream consumers can drop to a short liveness read timeout (~60-90s) instead of capping turn duration. Co-Authored-By: Claude Fable 5 <[email protected]>
Author
|
Anvil hardening update for
Verdict: |
LightDriverCS
marked this pull request as ready for review
June 13, 2026 02:15
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
/v1/chat/completionsSSE streams emit nothing while the agent executes tools — no token deltas, no frames at all. Stream consumers cannot distinguish "agent working a long tool phase" from "gateway dead," which forces them into turn-length read timeouts. On 2026-06-12 the BenchAGI agent-chat runtime killed a healthy long-running turn at exactly its 180s read timeout and reported the misleading errorcould not reach gateway; the partially streamed reply was discarded.Timeouts are the wrong primitive for agent turns: tasks are legitimately long. Liveness is the right one.
Implementation
: keepalive\n\nSSE comment frames on a timer (gateway.http.endpoints.chatCompletions.sseKeepaliveIntervalMs, default 15000,0disables). Comment frames are part of the SSE spec and ignored by spec-compliant clients including OpenAI SDKs; consumers that only parsedata:lines (like the BenchAGI runtime) skip them by construction.unref()'d, stopped inmaybeFinalize, andres.once('close', …)as the catch-all cleanup so no end path (finalize, error frame, client disconnect, tool-choice failure) can leak the interval.GatewayHttpChatCompletionsConfig+ zod schema.Verification
openai-http.keepalive.test.ts: default 15s, explicit value,0disables, clamp semantics — 12/12 across the 3 gateway projects.openai-http.usage,openai-http.image-budget): 24/24 pass.tsc --noEmitclean on touched files.Risks & rollback
sseKeepaliveIntervalMs: 0restores exact previous behavior per-install. Revert is a single commit.Downstream plan
Once this ships in a gateway release, the BenchAGI agent-chat runtime drops its
GATEWAY_TIMEOUT_SECONDSfrom the interim 600s to a short liveness window (~90s), removing the turn-duration cap entirely.🤖 Generated with Claude Code