Skip to content

fix(responses): bound streaming citation spans - #418

Open
luvs01 wants to merge 20 commits into
Devfrom
codex/fix-streaming-dos-from-citation-markers
Open

fix(responses): bound streaming citation spans#418
luvs01 wants to merge 20 commits into
Devfrom
codex/fix-streaming-dos-from-citation-markers

Conversation

@luvs01

@luvs01 luvs01 commented Sep 3, 2026

Copy link
Copy Markdown
Owner

Motivation

  • Prevent a CPU/event-loop denial-of-service caused by the streaming citation filter repeatedly concatenating and rescanning a growing held string for every small delta.
  • Keep the original semantics of stripping complete citation spans, withholding unterminated tails, and releasing held text at stream close so model output is never lost.

Description

  • Replace the single-string held buffer with a small-chunk buffer and heldLength, and add a linear-time streaming parser in createCitationMarkerFilter() to avoid repeated full-string scans.
  • Introduce a conservative bound MAX_STREAMING_MARKER_SPAN_LENGTH = 4096 so implausibly large unterminated spans are released as ordinary text rather than retained indefinitely.
  • Keep completed citation spans removed without joining held chunks, and make flush() join and emit any retained pieces once, preserving user-visible bytes.
  • Add focused regression tests in tests/citation-markers.test.ts that verify large unterminated spans are released incrementally and that a later valid citation span is still removed.

Testing

  • Ran bun test tests/citation-markers.test.ts, which passed (all streaming and whole-string filter cases succeeded).
  • Ran bun run typecheck, which completed successfully against the changed files.
  • Ran bun run privacy:scan and git diff --check as repository hygiene checks; they passed for the touched files.
  • Ran the full test suite (bun run test) for context but it exposed unrelated pre-existing integration failures; the focused citation-marker tests passed within that run.

Codex Task

Summary by CodeRabbit

  • Bug Fixes
    • Improved streaming citation handling for malformed or incomplete citation spans.
    • Long, unterminated citation content is now released incrementally instead of being retained indefinitely.
    • Valid citations appearing after malformed content are now processed correctly.
    • Remaining text is preserved and displayed as expected.

lidge-jun and others added 20 commits August 25, 2026 10:36
release: promote dev into main for v2.32.1
# Conflicts:
#	package.json
[WRONG BRANCH] merge dev into main for the v2.33.0 release
Promotes the dev integration line onto main. The resulting tree is byte-identical
to origin/dev, including package.json at 2.34.0.

The package.json conflict is resolved to dev's side, NOT to main's stale 2.33.0.
Earlier promotions (lidge-jun#2553, lidge-jun#2507) kept the target's version so the release bump
would land on its own "release: vX.Y.Z" commit. That is no longer legal: this very
delta adds tests/release-version-line.test.ts, which fails when the in-tree version
sits behind the highest release tag. With v2.34.0-preview.20260827 now published,
2.33.0 orders behind it, so a promotion carrying the stale line turns CI red on
every shard that runs the suite.

The consequence for the release step is that scripts/release.ts skips the bump
(release.ts:568, currentVersion === version), so v2.34.0 gets tagged on this merge
commit rather than on a separate release commit. The workflow creates the tag itself
after publishing and validates expected-sha against the checked-out commit, so the
tag still names exactly the audited tree.
[WRONG BRANCH] promote dev onto main for v2.34.0
[WRONG BRANCH] promote dev onto main for v2.35.0
[WRONG BRANCH] promote dev onto main for v2.36.0
[WRONG BRANCH] promote dev to main for the v2.37.0 release
[WRONG BRANCH] promote dev onto main for v2.38.0
[WRONG BRANCH] promote dev onto main for v2.39.0
…rkflow call (lidge-jun#3262)

Both v2.40.0 release dispatches (33615174183 preview, 33615177849 main) died
at startup_failure: a workflow_call cannot grant its callee more than the
calling job holds, and dev-version-bump.yml's job declares contents+pull-
requests write. lidge-jun#3129 wired the call but never dispatched a release, so this
is its first live run. The caller job now declares exactly the callee's two
permissions; no other job in release.yml gains anything.

Co-authored-by: jun <jun@lidge.dev>
(cherry picked from commit 7ce0ba5)
…400-relfix

release: carry the release.yml permissions fix onto main for v2.40.0
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 3, 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-09-03T06:16:42.387502Z 8f7d576 PR opened
ℹ️ 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.

@github-actions github-actions Bot added the bug Something isn't working label Sep 3, 2026
@coderabbitai

coderabbitai Bot commented Sep 3, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Team

Run ID: 20c4a9a2-bec6-42f9-b87e-f24b5058f6a1

📥 Commits

Reviewing files that changed from the base of the PR and between 38f8a81 and 8f7d576.

📒 Files selected for processing (3)
  • .github/workflows/release.yml
  • src/responses/citation-markers.ts
  • tests/citation-markers.test.ts

Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

The citation marker filter now releases oversized unterminated spans incrementally and removes later valid spans correctly. The release workflow job now declares the write permissions required by its reusable workflow.

Changes

Streaming citation handling

Layer / File(s) Summary
Bounded citation filtering
src/responses/citation-markers.ts:71-154, tests/citation-markers.test.ts:90-105
The filter limits retained malformed spans to 4,096 characters, tracks chunks and held length, handles newer starts and flush paths, and tests incremental release and later valid-span removal.

Release workflow permissions

Layer / File(s) Summary
Reusable workflow permissions
.github/workflows/release.yml:70-77
The bump-dev-version job declares contents: write and pull-requests: write permissions for the called reusable workflow.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Merge Risk: ⚪ Minimal · up to 8f7d5

The citation filtering and release-workflow permission changes have no remaining concrete merge-blocking risk.

Suggested reviewers: lidge-jun, ingwannu

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 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: bounding streaming citation spans to prevent unbounded buffering and rescanning. It matches the citation-marker implementation and regression…
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files. (1 skipped: 1 …
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title clearly and concisely describes the main change: bounding streaming citation spans to prevent unbounded buffering and rescanning. It matches the citation-marker implementation and regression tests, while the workflow permission update is secondary.

Full details: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files. (1 skipped: 1 unsupported.)

✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/fix-streaming-dos-from-citation-markers

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 changed the title fix(responses): bound streaming citation spans [WRONG BRANCH] fix(responses): bound streaming citation spans Sep 3, 2026
@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown

✅ READY

  • all PR quality gates passed.

Hygiene

Deterministic PR hygiene checks passed.

@github-actions
github-actions Bot marked this pull request as draft September 3, 2026 06:16

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8f7d5763a3

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +114 to +115
const end = delta.indexOf(CITATION_MARKER_END, index);
const nextStart = delta.indexOf(CITATION_MARKER_START, index);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Scan each delimiter only once per delta

When one text delta contains many START characters and no END (for example, a model or upstream emits START.repeat(n)), every iteration calls indexOf(END, index) over the entire remaining suffix before advancing to the next START, making push() O(n²). This creates another event-loop denial-of-service path in the code intended to eliminate one; scan forward with parser state so each character is examined only a bounded number of times.

Useful? React with 👍 / 👎.

Comment on lines +142 to +145
// An implausibly large unterminated span is malformed ordinary text. Releasing
// it bounds both retained memory and work per delta; subsequent STARTs can still
// begin valid citation spans.
out.push(takeHeld());

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Keep oversized spans consistent in delta and done events

When a marker remains open beyond 4,096 characters but an END arrives later, this branch releases the opener and content as ordinary streaming deltas, and the later END is emitted too. However, closeCurrentMessage() in src/bridge.ts still passes the accumulated raw text through the unbounded stripCitationMarkers(), so response.output_text.done and response.content_part.done omit the entire span. The concatenated deltas therefore disagree with the terminal text; preserve the oversized-span decision when producing the final text, or make both paths use the same filtering state.

Useful? React with 👍 / 👎.

@luvs01 luvs01 changed the title [WRONG BRANCH] fix(responses): bound streaming citation spans fix(responses): bound streaming citation spans Sep 3, 2026
@luvs01
luvs01 changed the base branch from main to dev September 3, 2026 06:21
@github-actions
github-actions Bot marked this pull request as ready for review September 3, 2026 06:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

aardvark bug Something isn't working codex

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants