Skip to content

fix(web): enforce semantic idle and propagate stream closure - #1108

Open
Laev wants to merge 1 commit into
chenyme:mainfrom
Laev:codex/web-stream-lifecycle-20261120
Open

Laev wants to merge 1 commit into
chenyme:mainfrom
Laev:codex/web-stream-lifecycle-20261120

Conversation

@Laev

@Laev Laev commented Nov 20, 2026

Copy link
Copy Markdown

Problem

Grok Web requests can remain active without useful generation when the WebSocket keeps sending control frames. Closing a downstream Web response can also leave its producer blocked on the upstream body until a separate timeout fires.

Both failures reproduce against current main using local fixtures, without a Grok account, proxy, CDN, or external network:

  • With a one-second Web idle timeout, a fixture sends only pong events for 1.4 seconds and then completes. Current main treats the response as successful because every control frame resets the raw-byte idle timer.
  • A Web response emits its initial SSE frame, then blocks reading an upstream pipe. Closing the returned response body does not close that upstream pipe.

Changes

  • Enforce the existing configurable Web idle window while waiting in WebSocket reads. Non-empty text/reasoning deltas and new tool/search/citation events refresh the window; control frames, empty deltas and duplicate tool events do not.
  • Bound tool-activity tracking and handle search results that omit a tool-call ID.
  • Pause idle accounting while the producer is blocked writing downstream. Keep the existing total request deadline and media exemption.
  • Close the upstream body when the downstream Web response body closes. Preserve normal EOF and classify explicit closure as client cancellation before egress feedback.
  • Return the existing upstream-idle sentinel with useful-progress metadata so existing failure and cooldown handling remains applicable.

The Web path no longer needs a second raw-byte idle timer around its pipe. A cancellation callback closes the WebSocket even when a read is blocked.

Scope

This is limited to Grok Web text-response lifecycle handling. It does not change configured timeout values, retry limits, account selection policy, credentials, proxy configuration, or downstream heartbeat behavior. It does not claim to repair an upstream search that stops producing useful events.

Build already has semantic idle protection and downstream-backpressure handling in #923 and #1026. #1021 handles audit classification after a client interruption; this PR completes closure propagation in the Web producer. The separate degraded-search-content guard in #1105 addresses a different failure mode.

Validation

  • New unchanged regression tests fail against main (8535932b0783db2033657f36e2e1a84196c4e151):
    • TestWebNonStreamingControlFramesDoNotResetIdle
    • TestWebStreamCloseReleasesBlockedUpstream
  • The same tests pass after the fix.
  • go test -p 4 ./... passes.
  • go test -race ./internal/infra/provider/web ./internal/infra/provider/streamidle ./internal/pkg/neterror passes.
  • go test -race ./internal/application/gateway ./internal/transport/http/inference passes.
  • Targeted idle/close/cancellation regression tests pass three consecutive runs with -race.
  • go vet ./internal/infra/provider/web and git diff --check pass.
  • Independent read-only review found no blocking or significant issue in the scoped changes.

Tests cover useful text/reasoning/search activity, duplicate/no-ID search results, control-only stalls, downstream backpressure, explicit cancellation, upstream close idempotency, and absence of egress-health feedback after a client close. Validation used Go 1.27.4; the module's Go requirement is unchanged.

@github-actions

Copy link
Copy Markdown
Contributor

This pull request has had no activity for 7 days. It will be closed in 7 days if there is no further activity.

@github-actions github-actions Bot added the stale label Nov 28, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant