Skip to content

fix(gateway): harden Responses terminal lifecycle - #1632

Merged
BYK merged 21 commits into
mainfrom
fix/stream-eof-before-post-response
Aug 15, 2026
Merged

fix(gateway): harden Responses terminal lifecycle#1632
BYK merged 21 commits into
mainfrom
fix/stream-eof-before-post-response

Conversation

@BYK

@BYK BYK commented Aug 13, 2026

Copy link
Copy Markdown
Owner

Summary

Follow-up to #1625 that makes Responses completion transactional: session-owned mutation, temporal persistence, recall anchors, and transfer accounting occur only after a validated terminal response reaches downstream EOF and post-response work succeeds. The branch is integrated with current main while preserving tenant isolation, safe compact routing, and setup/uninstall changes.

Changes

  • defer temporal storage, tracking, curation, identity publication, recall anchors, and transfer counters until downstream settlement
  • commit Responses recall state and successful-turn persistence in one SQLite savepoint, rolling back on cancellation or callback failure
  • validate the full standard Responses output-item union with phase-specific statuses and semantic parity across added, done, and terminal snapshots
  • preserve validated hosted-tool and enriched raw output while rejecting unknown types, explicit-null mutation, and illegal terminal changes
  • serialize identity admission and fence reset, eviction, cancellation, stale provisional ownership, tenant mismatch, and project re-attribution races
  • distinguish safe sessionless Responses compact passthrough from identified compact requests, which remain pinned to authenticated session state
  • reject unsafe adopted upstream URLs, including mapped IPv6 loopback forms
  • add mutation-sensitive lifecycle, cancellation, persistence, ownership, project, adoption, compact-routing, and migration regressions

Verification

Reviewed commit: 4e8b4759b4679ae688c55dd54ae696becf8ce6d6
Approved PR parent retained in history: b6df2dcd48b8e4326566047f733246336a2d6e42
Integrated main parent: 8a5bc6119ce6b10144756fa81ebcb3739a3c0644

  • pnpm test: 433 files, 9,330 tests passed; 15 files and 227 tests skipped
  • pnpm run typecheck
  • pnpm run format:check
  • pnpm run lint (exit 0, baseline warnings only)
  • packages/gateway/test/sync.property.test.ts: 50/50 passed across ten serial runs
  • independent adversarial correctness review of exact hash: MERGE
  • independent security review of exact hash: MERGE

@BYK BYK self-assigned this Aug 13, 2026
Comment thread packages/gateway/src/session.ts
@BYK
BYK force-pushed the fix/stream-eof-before-post-response branch from 5c943d6 to b6df2dc Compare August 15, 2026 19:05
@BYK BYK changed the title fix(gateway): close streams before post-response work fix(gateway): harden Responses terminal lifecycle Aug 15, 2026
Comment thread packages/gateway/src/stream/openai-responses.ts
Comment on lines 129 to +133
// on parse-failure to reject it. appendCustomHeader also sanitizes, but we
// normalize at the source so every consumer sees a clean value.
// oxlint-disable-next-line no-control-regex -- intentional control-character sanitization
const trimmed = raw.replace(/[\x00-\x1f\x7f]/g, "").trim();
if (!trimmed) continue;
if (!trimmed) invalid();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The captureUserEnvCredential function now silently discards a valid auth token if an associated upstream URL is invalid, instead of ignoring the invalid URL.
Severity: MEDIUM

Suggested Fix

Instead of immediately returning null when an invalid upstream URL is found, the function should either throw an error to explicitly inform the user about the invalid URL (similar to captureUserUpstream), or revert to the previous behavior of continuing the loop to search for other valid upstream URLs.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: packages/gateway/src/cli/agents.ts#L129-L133

Potential issue: In the `captureUserEnvCredential` function, if a valid authentication
token is found but a configured upstream URL is determined to be invalid by
`cleanBaseUrl`, the entire function now returns `null`. Previously, it would skip the
invalid URL and could still return the credential with a null `upstreamUrl`. This change
causes a valid credential to be silently discarded if the user has also configured an
invalid upstream URL, leading to credential discovery failure without a clear error
message explaining why.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in #1640 by surfacing an explicit invalid-upstream error while preserving token-only provider-default credentials.

@github-actions

Copy link
Copy Markdown
Contributor

Codecov Results 📊

✅ Patch coverage is 90.83%. Project has 8177 uncovered lines.
✅ Project coverage is 80.33%. Comparing base (base) to head (head).

Files with missing lines (13)
File Patch % Lines
packages/gateway/src/pipeline.ts 90.46% ⚠️ 127 Missing and 128 partials
packages/gateway/src/stream/openai-responses.ts 85.22% ⚠️ 17 Missing and 15 partials
packages/gateway/src/cache-warmer.ts 96.30% ⚠️ 3 Missing and 8 partials
packages/core/src/db.ts 97.10% ⚠️ 4 Missing and 4 partials
packages/core/src/warmup-histogram.ts 96.88% ⚠️ 3 Missing and 5 partials
packages/core/src/curator.ts 68.42% ⚠️ 6 Missing
packages/gateway/src/cli/run.ts 73.91% ⚠️ 6 Missing
packages/core/src/ltm.ts 63.64% ⚠️ 4 Missing
packages/core/src/recall.ts 86.67% ⚠️ 2 Missing and 2 partials
packages/core/src/distillation.ts 60.00% ⚠️ 2 Missing and 1 partials
packages/core/src/pattern-echo.ts 87.50% ⚠️ 2 Missing
packages/gateway/src/cli/agents.ts 95.45% ⚠️ 1 Missing and 1 partials
packages/gateway/src/translate/openai-responses.ts 100.00% ⚠️ 2 partials
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    79.42%    80.33%    +0.91%
==========================================
  Files          217       218        +1
  Lines        40073     41573     +1500
  Branches     31579     32929     +1350
==========================================
+ Hits         31828     33396     +1568
- Misses        8245      8177       -68
- Partials      3546      3643       +97

Generated by Codecov Action

@BYK
BYK merged commit 3b916ed into main Aug 15, 2026
24 checks passed
@BYK
BYK deleted the fix/stream-eof-before-post-response branch August 15, 2026 23:52
@github-actions

Copy link
Copy Markdown
Contributor
PR Preview Action v1.8.1
Preview removed because the pull request was closed.
2026-08-15 23:53 UTC

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