Skip to content

fix: avoid double-counting logprobs in streamed chat completions - #2444

Merged
HAYDEN-OAI merged 2 commits into
openai:mainfrom
dfedoryshchev:fix/logprobs-double-count
Aug 25, 2026
Merged

HAYDEN-OAI merged 2 commits into
openai:mainfrom
dfedoryshchev:fix/logprobs-double-count

Conversation

@dfedoryshchev

@dfedoryshchev dfedoryshchev commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor
  • I understand that this repository is auto-generated and my pull request may not be merged

Changes being requested

#accumulateChatCompletion seeds a new choice with the chunk's logprobs by reference, so on that same iteration choice.logprobs === logprobs and choice.logprobs.content.push(...content) pushes the array into itself. One streamed token arrives as two entries in finalChatCompletion().choices[0].logprobs.content and in the logprobs.content.done payload.

The initialise branch then does choice.logprobs = { ...logprobs }, one level deep, so the snapshot keeps the caller's array and the next chunk's push appends into the chunk the caller passed in.

Current tests miss both: the fixture's first logprobs-bearing chunk is {"content":[],"refusal":null}, so push(...[]) is a no-op, and nothing streams two chunks that each carry logprobs.

Fix: seed logprobs: null, and copy the arrays when initialising.

Additional context & links

  • Three added tests cover the content path, the refusal path and cross-chunk accumulation; all three fail on main.
  • npx vitest run --config vitest.config.mts tests/lib/ChatCompletionStream.test.ts -> 38 passed. oxlint and oxfmt --check pass.

@dfedoryshchev
dfedoryshchev requested a review from a team as a code owner August 21, 2026 11:28

@HAYDEN-OAI HAYDEN-OAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed the streaming accumulator and event paths. This fixes first-chunk logprob duplication and avoids mutating source chunk arrays while preserving the streaming API contract. LGTM.

@HAYDEN-OAI
HAYDEN-OAI enabled auto-merge August 25, 2026 17:11
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Aug 25, 2026 •

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 ✅ Completed 2026-08-25T17:23:59.291042Z e5d024a New commits
🔒 Security Review ✅ Completed 2026-08-25T17:23:46.513188Z e5d024a New commits
ℹ️ 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.

@HAYDEN-OAI
HAYDEN-OAI disabled auto-merge August 25, 2026 17:29
@HAYDEN-OAI
HAYDEN-OAI merged commit ddf9922 into openai:main Aug 25, 2026
19 checks passed
@openai-sdks openai-sdks Bot mentioned this pull request Aug 25, 2026
github-merge-queue Bot pushed a commit that referenced this pull request Aug 25, 2026
## Summary

GitHub omits `workflow_run.pull_requests` for external-fork runs, and
querying the upstream repository's commit-association endpoint returns
no pull requests for those fork commits. That leaves the required
`Castiron / budget-only change` and `Castiron / custom-code budget`
contexts permanently expected even when the candidate workflow succeeds.

- Centralize trusted Python pull-request association in
`custom_code_report.py` and reuse it from trusted report generation,
comment publication, and budget evaluation.
- Resolve missing associations from the authenticated source run's
`head_repository`, including legitimately renamed forks, then
independently re-fetch every candidate PR from `openai/openai-node`.
- Apply equivalent validation in both privileged JavaScript publishers:
required commit statuses and failure-report comments.
- Refresh the candidate workflow's pinned reporter SHA-256 after the
trusted reporter change.

## Security model

Fork-side associations and PR numbers are discovery hints, never
authorization. The trusted paths:

1. Re-fetch the workflow run from the upstream Actions API and verify
its repository, immutable candidate SHA, workflow path, completion, and
run attempt as applicable.
2. Strictly validate the source repository's owner/name syntax and
consistency with authenticated `head_repository` metadata; reject
traversal-like components and spoofed identities.
3. Re-fetch each hinted PR from the upstream repository and require an
open PR with the exact candidate SHA, the exact source head repository,
the intended upstream repository and `main` base ref, and exactly one
valid current association.
4. Require the current `main` base SHA wherever budget evaluation or
status publication needs freshness; preserve existing stale-run behavior
and merge-group validation.

The existing trusted `workflow_run`/main-checkout boundary, bare Git
object store, candidate-artifact isolation, least-privilege job
permissions, merge-queue protections, and exact required status names
remain unchanged. No candidate workflow definition, mutable ref,
contributor artifact, or fork-supplied PR number is trusted.

## Affected contributor PRs

- #2463: live fork workflow run `32877584723` has `pull_requests: []`;
the updated resolver correctly finds its upstream PR through
`cmun2/openai-node`.
- #2444 and #2431: independently reproduced the same fork-only
association behavior; both PRs merged while this fix was being prepared.

## Verification

- `env PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s
scripts/castiron -p 'test_custom_code*.py'` — 60 tests pass, with one
pre-existing skip.
- `go run github.com/rhysd/actionlint/cmd/actionlint@latest
.github/workflows/castiron-custom-code.yml
.github/workflows/castiron-custom-code-comment.yml` — both workflows
pass actionlint v1.7.12.
- `ruff format --check scripts/castiron/custom_code_report.py
scripts/castiron/custom_code_budget.py
scripts/castiron/test_custom_code_report.py
scripts/castiron/test_custom_code_budget.py`.
- Ruff lint passes when ignoring only the same pre-existing baseline
rule findings; `git diff --check` passes.
- Executable publisher and Python regression coverage includes external
forks with empty run associations, fork-side lookup, same-repository and
renamed-fork PRs, malformed/spoofed repositories, unrelated source
heads, ambiguous/duplicate/invalid associations, stale heads/bases/run
attempts, exact required contexts, and merge groups.
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.

2 participants