Skip to content

fix(openai-chat): keep captured usage on an invalid negotiated completion - #7849

Merged
lidge-jun merged 2 commits into
lidge-jun:devfrom
xubenqiang:fix/negotiated-completion-usage
Oct 28, 2026
Merged

lidge-jun merged 2 commits into
lidge-jun:devfrom
xubenqiang:fix/negotiated-completion-usage

Conversation

@xubenqiang

@xubenqiang xubenqiang commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #7841

Every terminal path of the OpenAI-compatible Chat streaming generator attaches pendingUsage to its error event — invalidChoicesEvent(pendingUsage), invalidToolCallsEvent(..., pendingUsage), upstreamErrorEvent(..., pendingUsage) — except the invalid negotiated tool completion path (block-stop-v1, #7561), which built its event without it. When upstream reported usage before a repeated completion marker or a late argument fragment, those tokens were missing from the request's error event.

Change

src/adapters/openai-chat.ts

  • The completion === "invalid" branch now passes usage: pendingUsage (when captured) like its sibling terminal errors.
  • No other behaviour changes: the event type, message and status are the same.

Test

tests/adapters/openai/openai-chat-parallel-stream.test.ts

  • New an invalid completion sequence still reports the usage already captured: streams a call, its completion marker, a usage-only chunk, then a late argument fragment that trips observeCompletedToolBlock, and asserts the final error event is still the negotiated-completion error and carries { inputTokens: 11, outputTokens: 7 }.

Verified: bun test tests/adapters/openai/openai-chat-parallel-stream.test.ts -> 52 pass / 0 fail; with only openai-chat.ts reverted, 51 pass / 1 fail (the new test). bun x tsc --noEmit exits 0.

Summary by CodeRabbit

  • Bug Fixes
    • Error reports for invalid tool-completion sequences now include captured input and output token usage when available, making usage information accessible even when a stream ends with an error. This improves visibility into token consumption in these cases without changing the behavior of valid tool-completion sequences.

Review readiness checklist

This PR stays in draft until every box below is ticked. Tick all four boxes once the requirements are met:

  • Required local validation passed; commands, results, and any full-suite exception are documented.

  • I pushed my PR to a recent dev commit (at most 10 behind; a maintainer may still ask for the exact tip before merge).

  • I resolved all correct Codex and CodeRabbit findings.

  • My PR is ready for review.

…tion

Every other terminal path of the streaming generator attaches `pendingUsage`
to its error event (`invalidChoicesEvent`, `invalidToolCallsEvent`,
`upstreamErrorEvent`), but the invalid negotiated tool completion path threw it
away. When upstream reported usage before a repeated completion marker or a
late argument fragment, those tokens were missing from the request's error
event even though every other error carries them.

Pass `pendingUsage` through like the sibling terminal errors.
Copilot AI balanced review requested due to automatic review settings October 27, 2026 19:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@coderabbitai

coderabbitai Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Caution

Review failed

The pull request is closed.

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 0c32c81 and 9d91a2e.

Files selected for processing: 2
  • src/adapters/openai-chat.ts
  • tests/adapters/openai/openai-chat-parallel-stream.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Walkthrough

When a negotiated tool-completion sequence is invalid, the stream error now includes captured usage when available. A regression test checks that the error retains the input and output token counts.

Changes: Usage in stream errors

Layer / File(s) Summary
Include pending usage in invalid completion errors
src/adapters/openai-chat.ts, tests/adapters/openai/openai-chat-parallel-stream.test.ts
The invalid negotiated tool-completion error now includes pending usage. The regression test checks that the error contains inputTokens: 11 and outputTokens: 7.

Priority: Low

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix · Severity of issue fixed: Low

Suggested labels: bug

Suggested reviewers: lidge-jun

Merge Risk: Minimal · previous review 9d91a

The PR is mergeable with no actionable risks identified.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within survi… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: preserving captured usage for invalid negotiated completions in the OpenAI Chat adapter.
Linked Issues check Passed Issue #7841 requires the invalid negotiated tool-completion path to preserve upstream usage and requires a regression test. In src/adapters/openai-chat.ts, the completion === "invalid" path now su…
Out of Scope Changes check Passed The reported pull request changes are limited to the required adapter fix in src/adapters/openai-chat.ts and its regression test in tests/adapters/openai/openai-chat-parallel-stream.test.ts. The t…
Full details: Linked Issues check

Explanation

Issue #7841 requires the invalid negotiated tool-completion path to preserve upstream usage and requires a regression test. In src/adapters/openai-chat.ts, the completion === "invalid" path now supplies usage: pendingUsage to the existing negotiated-completion error event. The event type, message, and status remain unchanged. In tests/adapters/openai/openai-chat-parallel-stream.test.ts, the new test sends completion, usage, and a late argument fragment, then asserts { inputTokens: 11, outputTokens: 7 } on the error event. The supplied test result reports 52 passes and 0 failures.

Full details: Out of Scope Changes check

Explanation

The reported pull request changes are limited to the required adapter fix in src/adapters/openai-chat.ts and its regression test in tests/adapters/openai/openai-chat-parallel-stream.test.ts. The test directly exercises the repeated-completion or late-fragment failure mode from issue #7841. No unrelated behavior, public declaration, or feature change is identified.

Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 1 functions across 2 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added the bug Something isn't working label Oct 27, 2026
@github-actions

Copy link
Copy Markdown
Contributor

✅ Deterministic PR hygiene checks passed.

@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

⏳ DRAFT

  • review readiness checklist open (0/4 boxes ticked).

What to do

  • Tick all four boxes in the PR description once you're done (currently 0/4).

Review readiness checklist

  • ⬜ Required local validation passed; commands, results, and any full-suite exception are documented.
  • ⬜ I pushed my PR to a recent dev commit (at most 10 behind; a maintainer may still ask for the exact tip before merge).
  • ⬜ I resolved all correct Codex and CodeRabbit findings.
  • ⬜ My PR is ready for review.

0/4 boxes ticked.

Automatic draft conversion failed. Please convert this pull request to a draft manually until every box above is ticked.

Hygiene

✅ Deterministic PR hygiene checks passed.

@github-actions
github-actions Bot marked this pull request as draft October 28, 2026 02:02
@lidge-jun

Copy link
Copy Markdown
Owner

Maintainer integration into dev (MAINTAINERS.md, "Review and merge policy"). This is maintainer integration, not an approving review.

  • Exact head 4945dd4914ffe14272650a19d15c3cc05115ebc9; no conflict with current dev.
  • Maintainer change on top of the author's fix: 4945dd491 folds the new usage spread into the existing single-line return. src/adapters/openai-chat.ts sits at its file-size cap, and the first head grew it by six lines, which failed test changed (file-size ratchet). Behavior is unchanged.
  • Exact-head PR CI: Cross-platform CI 40620184440 (gates, test changed, ci), Catalog async contracts 40620184382, enforce-target: all success.
  • Local (Bun 1.4.4): openai-chat-parallel-stream + file-size ratchet 61 pass / 0 fail; the new regression fails without the source change (per the description, 51 pass / 1 fail).

Fixes #7841. Thank you @xubenqiang.

@lidge-jun
lidge-jun marked this pull request as ready for review October 28, 2026 02:37
@lidge-jun
lidge-jun merged commit 1997c69 into lidge-jun:dev Oct 28, 2026
27 checks passed
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review 🔄 Running since 2026-10-28T02:37:21.555936Z 4945dd4 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

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

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants