Skip to content

feat: add the legacy completions surface and harden the gateway - #5

Merged
dinhdev666 merged 1 commit into
mainfrom
feat/gateway-hardening
Dec 3, 2026
Merged

dinhdev666 merged 1 commit into
mainfrom
feat/gateway-hardening

Conversation

@dinhdev666

Copy link
Copy Markdown
Owner

Summary

Implements the improvement proposal set: one new public surface and a set of correctness, security, performance and documentation fixes found while auditing the tree. IMPROVEMENT_PROPOSALS.md was the working document and is removed by this pull request (its content is now the changelog below).

Added

  • POST /v1/completions — legacy OpenAI completions, buffered (text_completion) and streamed (text_completion chunks + [DONE]), with x-conversation-id / x-opencode-agent support.
  • GET /admin/config — effective configuration with API keys and the upstream password redacted.
  • parallel_tool_calls: false is now honoured by the tool bridge (one bridged call per turn).

Fixed

  • Upstream event stream: reader is cancelled on every exit, an idle watchdog detects half-open sockets, and markStale() recovers a spawned server instead of retrying a dead URL.
  • Sticky sessions: the stale-session replay releases its slot, caches the replacement under the conversation key, and no longer throws out of the generator; prepare() failures also release the acquired slot.
  • Completions fail fast (event_stream_unavailable) when the event stream is down; /v1/responses no longer commits the SSE head before the first event, so pre-stream failures return a real HTTP status.
  • /v1/responses: resolved model/#variant are preserved, sequence_number stays contiguous on failures, failed turns report status: "failed" / response.failed, and usage keeps prompt + completion === total.
  • Anthropic: streamed thinking blocks carry a signature_delta identical to the buffered signature, block layout matches the buffered payload, streamed tool args are normalised, is_error-safe usage accounting includes thinking tokens in output_tokens.
  • Tool bridge: adjacent JSON envelopes are parsed; a none-only variant set no longer reports reasoning as enabled.
  • Config: strict integer parsing (a typo falls back with a warning), fail-closed permission/alias validation, and an explicitly empty key list no longer silently installs the published default key.
  • Security: validated media addresses are pinned for the connection (DNS rebinding), local attachment sizes are re-checked after reading, pre-aborted signals end downloads, and the metrics route label is bounded (no caller-controlled cardinality).
  • /ready reads cached state only; upstream latency is observed on failures too; catalogue calls use a short timeout; shutdown has a bounded drain.

Documentation

docs/API.md, docs/ARCHITECTURE.md, docs/CONFIGURATION.md, docs/VERIFICATION.md, README.md and .env.example were updated for every changed contract, endpoint, field and behaviour.

Verification

  • pnpm typecheck — clean
  • pnpm build — clean
  • pnpm test — 305 passed (20 files)
  • pnpm lint:commits — passes (one scope-empty warning for a repository-wide change)
  • Regression tests were added for each fix and verified to fail against the previous code.

Add POST /v1/completions (buffered and streamed) so prompt-style OpenAI
clients work, and fix the defects found while auditing the tree for the
improvement proposals:

- cancel the upstream SSE reader, add an idle watchdog and recover a
  spawned server whose endpoint went stale
- release every sticky-session slot (stale replay and prepare failures)
  and cache the replacement under the conversation key
- fail a completion fast when the event stream is unavailable, and let
  /v1/responses answer a pre-stream failure with a real HTTP status
- bound the metrics route label, keep /ready on cached state, parse
  integers strictly and fail closed on bad permission/alias JSON
- pin validated media addresses, re-check local attachment sizes and
  honour pre-aborted signals
- parse adjacent tool-call envelopes, keep reasoning out of a "none"
  variant, and report usage with reasoning inside the output budget
- sign streamed Anthropic thinking blocks identically to the buffered
  path, and emit response.failed for a failed Responses turn
- resolve media concurrently and observe upstream latency on failures
- add GET /admin/config and document every changed behaviour

305 unit tests (20 files) pass; typecheck and build are clean.
@dinhdev666 dinhdev666 closed this Dec 2, 2026
@dinhdev666 dinhdev666 reopened this Dec 2, 2026
@dinhdev666
dinhdev666 force-pushed the feat/gateway-hardening branch from 2cd275e to 0491946 Compare December 2, 2026 17:19
@dinhdev666
dinhdev666 merged commit 4867f09 into main Dec 3, 2026
3 checks passed
@dinhdev666
dinhdev666 deleted the feat/gateway-hardening branch December 3, 2026 01:05
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