Skip to content

feat(streaming): harden cancellation, headers and output framing - #3

Merged
dinhdev666 merged 4 commits into
mainfrom
feat/gateway-improvements
Dec 2, 2026
Merged

dinhdev666 merged 4 commits into
mainfrom
feat/gateway-improvements

Conversation

@dinhdev666

Copy link
Copy Markdown
Owner

Summary

A batch of defects and improvements found while auditing the gateway against its
own contract and documentation (the full proposal list is in the commit
messages; the temporary proposal file was removed before the first commit).

Fixes

  • Upstream supervision: a replacement server is adopted only after the previous
    child is terminated, a replaced child's late exit no longer clears the
    successor's tracking, and supervision is re-armed after stop(); a request
    timeout now answers 504 timeout_error.
  • Client disconnects are detected from the response connection
    (src/http/disconnect.ts). The previous request.raw listener fired when the
    body had been read, so a disconnect never cancelled the turn or a pending
    attachment download.
  • The SSE head keeps the headers Fastify set on the reply (CORS) and the new
    x-request-id.
  • EventHub.ensureConnected() releases every concurrent waiter instead of only
    the last one.
  • isPrivateHost() classifies IPv4/IPv6 by address family: public names such as
    fdic.gov are no longer blocked and site-local, multicast, NAT64 and 6to4
    literals now are.
  • The chat adapter reads the reasoning field from Config instead of module
    state shared by every server in the process.
  • /v1/responses: closes response.reasoning_summary_part, rejects an
    input_file that carries only file_id, and no longer leaks the
    active_streams gauge when the response head cannot be written.

Features

  • x-request-id: a validated caller-supplied correlation id, echoed on every
    JSON and SSE response and used as the request id in the logs.
  • JSON output mode: response_format (chat) and text.format (Responses) are
    rendered into the prompt as a JSON-only instruction.

Documentation

  • Corrected drift: the created example unit, the temperature/top_p claim,
    OPENCODE_REASONING_FIELD being described as outside Config, the startup
    validation list, the streaming 429 caveat and the pnpm build description.
  • Documented the new header, JSON output mode, event_stream_connected metric
    and the closed reasoning part.

Verification

  • pnpm typecheck — clean
  • pnpm build — clean
  • pnpm test — 214 passed (15 files)
  • pnpm lint:commits — clean for every commit on the branch

Adopt a freshly spawned server only after terminating the one it
replaces, ignore the late exit of a replaced child and re-arm the
stopping flag on every spawn. An admin restart or a markStale/resolve
cycle used to orphan a live `opencode serve` (and its port), the exit of
the stale child cleared the tracking of its successor, and a server
restarted after stop() was no longer supervised at all.

Also report the client's own request timeout as 504 timeout_error with
code upstream_timeout instead of 503 upstream_unreachable, matching the
documented status table; a caller abort still surfaces as 499.
ensureConnected() kept a single resolver slot, so concurrent completions
that raced the initial connection waited for the whole timeout even
though the stream was already up, and stop() left them pending. Track a
set of waiters and release all of them.

Report connectivity through a dedicated event_stream_connected gauge
instead of labelling a session-cache series "event_stream".
Client-disconnect detection moves into src/http/disconnect.ts. On
Node >= 16 the IncomingMessage "close" event fires as soon as the
request body has been read, so the per-surface listeners either missed
the disconnect or aborted a healthy request. The response/socket close
is the reliable signal: a disconnect now really interrupts the turn,
cancels a pending attachment download and releases the sticky slot.

Also, in the same pass:

- the SSE head keeps the headers Fastify set on the reply (CORS and the
  new x-request-id), which reply.hijack() would otherwise discard;
- a sanitised caller-supplied x-request-id is echoed on every response
  and used as the request id in the log lines;
- the chat adapter reads the reasoning field from Config, not from
  module state shared by every server in the process;
- /v1/responses closes the reasoning summary part, rejects an
  input_file carrying only a file_id, and no longer leaks the stream
  gauge when the response head cannot be written;
- response_format and text.format are rendered into the prompt as a
  JSON-only instruction;
- isPrivateHost classifies IPv4/IPv6 by address family: public names
  such as fdic.gov are no longer blocked, and site-local, multicast,
  NAT64 and 6to4 literals are;
- the unused bootstrapLogger, SSE_HEADERS and modality set are gone.
Correct the drift the batch exposed: the `created` example was in
milliseconds while the text promises seconds, temperature/top_p were
documented as forwarded upstream although nothing reads them, and
OPENCODE_REASONING_FIELD was listed as an adapter-level setting outside
Config. Startup validation no longer claims a missing key is fatal.

Document the streaming 429 caveat (retry_after_ms in the error frame),
the response-side disconnect detection, the new x-request-id header,
JSON output mode, the reasoning_summary_part.done frame, the
event_stream_connected metric and the input_file file_id rejection.
@dinhdev666
dinhdev666 merged commit 7a3c23f into main Dec 2, 2026
3 checks passed
@dinhdev666
dinhdev666 deleted the feat/gateway-improvements branch December 2, 2026 15:17
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.

1 participant