Skip to content

feat(gateway): SSE keepalive comment frames on /v1/chat/completions streams - #68

Merged
LightDriverCS merged 3 commits into
mainfrom
feat/chat-completions-sse-keepalive
Jun 13, 2026
Merged

LightDriverCS merged 3 commits into
mainfrom
feat/chat-completions-sse-keepalive

Conversation

@LightDriverCS

Copy link
Copy Markdown

Problem

/v1/chat/completions SSE 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 error could 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

  • The streaming handler writes : keepalive\n\n SSE comment frames on a timer (gateway.http.endpoints.chatCompletions.sseKeepaliveIntervalMs, default 15000, 0 disables). Comment frames are part of the SSE spec and ignored by spec-compliant clients including OpenAI SDKs; consumers that only parse data: lines (like the BenchAGI runtime) skip them by construction.
  • Timer hygiene: unref()'d, stopped in maybeFinalize, and res.once('close', …) as the catch-all cleanup so no end path (finalize, error frame, client disconnect, tool-choice failure) can leak the interval.
  • Config: additive optional key in GatewayHttpChatCompletionsConfig + zod schema.

Verification

  • New openai-http.keepalive.test.ts: default 15s, explicit value, 0 disables, clamp semantics — 12/12 across the 3 gateway projects.
  • Adjacent suites (openai-http.usage, openai-http.image-budget): 24/24 pass.
  • tsc --noEmit clean on touched files.

Risks & rollback

  • Default-on at 15s: one ~12-byte write per 15s of stream silence. Any consumer that chokes on SSE comments is already non-spec-compliant; sseKeepaliveIntervalMs: 0 restores 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_SECONDS from the interim 600s to a short liveness window (~90s), removing the turn-duration cap entirely.

🤖 Generated with Claude Code

Cory Shelton and others added 2 commits June 12, 2026 19:39
…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]>
@LightDriverCS

Copy link
Copy Markdown
Author

Anvil hardening update for 220de701c941b201abf2965b6e3ef2355fa3d076:

  • Pushed fix(gateway): harden chat completions keepalives: explicit SSE keepalive cleanup on disconnect/error paths, timer-safe interval handling via MAX_TIMER_TIMEOUT_MS, schema/help/docs alignment, and a streaming keepalive regression that observes comment frames on a silent /v1/chat/completions stream.
  • Local proof: pnpm test src/gateway/openai-http.keepalive.test.ts src/gateway/openai-http.test.ts src/config/schema.help.quality.test.ts -- --reporter=verbose && pnpm config:schema:check; pnpm check; pnpm build; pnpm check:docs.
  • Draft-side GitHub checks that ran are green: Analyze (actions), CodeQL, actionlint, no-tabs, dispatch. Full CI remains skipped because the PR is draft; local gates are the draft gate of record.

Verdict: FIXED_READY; ready for the orchestrator ready-for-review flip when selected.

@LightDriverCS
LightDriverCS merged commit 032d326 into main Jun 13, 2026
153 checks passed
@LightDriverCS
LightDriverCS deleted the feat/chat-completions-sse-keepalive branch June 13, 2026 07:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants