Skip to content

fix(responses): preserve literal spans in citation escaping - #8811

Merged
lidge-jun merged 1 commit into
lidge-jun:devfrom
luvs01:fix/citation-escaper-span-allocation
Nov 17, 2026
Merged

lidge-jun merged 1 commit into
lidge-jun:devfrom
luvs01:fix/citation-escaper-span-allocation

Conversation

@luvs01

@luvs01 luvs01 commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Streaming Responses deliveries pass provider text through the memory-citation escaper. Ordinary '<' characters previously created a new lookahead slice and output fragment for each character, so an otherwise admitted delta could allocate many temporary string objects before pending-tail accounting ran.

  • Preserve unchanged text as spans in the original input and move only the scan cursor across ordinary characters.
  • Flush the unchanged span only when escaping a real tag mention or retaining an unresolved suffix; inspect a bounded body only for complete opening-tag candidates.
  • Preserve split opening/closing tags, real and ids-only citation blocks, malformed and oversized candidates, and the existing 16,384 UTF-16-unit hold limit.
  • Keep Responses delta/done output, held-byte accounting, signed-progress handling, and Chat/Messages passthrough behavior unchanged.
  • Add bounded operation-count regressions, mixed-span and hold-boundary controls, and production bridge delta/done checks.

Verification

Recorded local validation:

  • Independent prepatch source-to-sink inspection and bounded offline measurements confirmed the allocation amplification on the current runtime. The original report's 8 MiB/process-exhaustion scenario was not reproduced.
  • For 2,048 / 8,192 / 32,768 ordinary '<' characters, the original makes 4,096 / 16,384 / 65,536 slice calls; the candidate makes 19 / 19 / 19. The original fails the new bounded-slice assertions, while the candidate preserves the same output and pending suffix.
  • A separate 256 KiB observation reduced additional JSC string objects from 526,816 to 21, and the observed RSS increase from 28,258,304 to 5,173,248 bytes. These are observations, not fixed CI thresholds or peak-memory guarantees.
  • bun run test tests/responses/citation-markers.test.ts tests/web-search/web-search-memory-citation.test.ts: 53 passed / 366 assertions, covering the helper, production Responses bridge and mocked web-search sidecar.
  • Fresh independent review of the immutable final tree found no blocking issue; it checked the source, direct consumers, budget boundaries, span/partial-tag behavior and test cleanup. The focused test counts above are the author's execution evidence, not a duplicate reviewer run.

Upstream validation for f50d4ad43febb3db23406cd5e105c1942b3bf6a1 passed: Cross-platform CI, Catalog async contracts, and PR gates. The PR CI ran its changed-test subset; skipped native-platform jobs are not claimed as executed. A second read-only review of this exact commit found no blocking correctness or test-isolation issue; it did not rerun tests or performance measurements. Installed/live application validation and explicit maintainer security review before merge remain separate.

Checklist

  • Scope stays focused and avoids unrelated cleanup.
  • Docs or release notes were updated when needed.
  • Security-sensitive changes were reviewed for secrets, auth, and unsafe defaults.

The security checkbox records implementation review; explicit maintainer security review remains required before merge.

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of streamed memory citations so incomplete or invalid citation blocks are held until they can be safely processed.
    • Preserved consistent citation escaping across different stream chunk sizes, including around long runs of angle brackets.
    • Kept final responses consistent with streamed updates, whether citation escaping is enabled or disabled.

@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The streaming memory-citation escaper now accumulates unchanged text spans and holds candidate blocks until their closing tag and body validate them. Tests cover slicing, chunk-size parity, hold-limit boundaries, and Responses bridge output parity.

Changes: Memory citation streaming

Layer / File(s) Summary
Span buffering and validation
src/responses/citation-markers.ts:218-291, structure/transports/byte-accounting.md:148-154, tests/responses/citation-markers.test.ts:1,288-337,399-409
The escaper accumulates unchanged text and releases a candidate block only after validation. Documentation describes bounded lookahead and the 16,384-unit hold limit. Tests cover slice counts, chunking, hold-limit boundaries, and bridge delta, done-event, and buffered-JSON parity.

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

Change: Refactor

Possibly related PRs

  • lidge-jun/opencodex#8044: Introduces the Responses memory-citation escaper and its streaming hold behavior, which this change updates.
Merge Risk: Minimal · up to f50d4

This change reduces slicing overhead when escaping memory citations in streamed responses and keeps the output unchanged. The risk of merging is low, and the focused tests are reported as passing.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "fix(responses): preserve literal spans in citation escaping" matches the main change. The raw summary and objectives both show that the memory-citation escaper in src/responses/citation-mar…
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 "fix(responses): preserve literal spans in citation escaping" matches the main change. The raw summary and objectives both show that the memory-citation escaper in src/responses/citation-markers.ts now keeps unchanged ordinary text as input spans instead of creating a separate slice at each '<'. The title names the Responses area, the fix type, and the core concept. It is concise, specific, and has no file lists or vague terms. Its wording follows the PR objectives directly.

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

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 595de1e and f50d4ad.

Files selected for processing: 3

  • src/responses/citation-markers.ts
  • structure/transports/byte-accounting.md
  • tests/responses/citation-markers.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.

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

Copy link
Copy Markdown
Contributor

✅ Deterministic PR hygiene checks passed.

@lidge-jun

Copy link
Copy Markdown
Owner

Maintainer integration into dev under MAINTAINERS.md (not an approving review), 2026-11-17 triage round.

@lidge-jun
lidge-jun merged commit dd9d4c0 into lidge-jun:dev Nov 17, 2026
43 of 45 checks passed
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.

2 participants