Skip to content

perf(zed): skip repeated scans of fragmented frames - #8695

Merged
lidge-jun merged 1 commit into
lidge-jun:devfrom
0disoft:task/zed-fragmented-frame-scan
Nov 14, 2026
Merged

lidge-jun merged 1 commit into
lidge-jun:devfrom
0disoft:task/zed-fragmented-frame-scan

Conversation

@0disoft

@0disoft 0disoft commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Zed's NDJSON/SSE framing bridge searches the entire retained incomplete line for a newline on every transport read. A 65,623-byte synthetic stream delivered in 256-byte fragments accumulates 8,486,999 characters of newline-search span while producing the correct content. For n fixed-size fragments of b characters, that repeated-prefix search is quadratic, Θ(bn²).

Check the newly decoded text for a newline before searching the retained buffer. The suffix left after draining has no newline, so a fragment without one cannot complete a frame. The same real-parser probe now traverses 131,220 characters of newline-search span, a 98.45% reduction; newline scanning is linear in input size. The count instruments both indexOf and includes through the first match or the end of the searched string. It measures parser search work, not end-to-end provider latency or RSS.

  • Preserve UTF-8 decoding, split CRLF, coalesced-frame order, EOF failure, terminal suppression, credential scrubbing and the existing incomplete-frame character ceiling. The ceiling still runs for every nonterminal read, including reads without a newline.
  • Extend the existing Zed framing tests with a deterministic search-work bound and byte-split Unicode/CRLF/coalesced-terminal cases. Update the byte-accounting contract; no new files or layout mappings.

Duplicate search covered open and closed issues/PRs for Zed newline, quadratic work, fragments and zedEventStream, plus open Zed PR titles. No equivalent fix was found. Merged #6362 introduced the provider and #7599 repaired OpenAI tool-call relay; neither removes this repeated framing scan. #8277 addresses a similar loop in the separate Command Code adapter. This patch is independently implemented and leaves those changes intact.

Verification

Exact head: d9efa61312e82f75059daae1874040785abf2c46. Base: dev at ee0098a0325f5522d2c7a88e924314c2b8ea9088. Native Windows, Bun 1.4.4. Refetched upstream before submission: 1 ahead / 0 behind. The committed tree is unchanged from validation.

  • Before the runtime fix, bun test tests/providers/zed-hosted-provider.test.ts --test-name-pattern "Zed stream framing" --timeout 30000: 7 passed / 1 failed, 15 assertions. The new work-bound assertion failed with 8,486,999 > 262,492 characters; reconstructed content and terminal output were correct.
  • After the fix, the same command: 8 passed / 0 failed, 15 assertions.
  • bun test --isolate tests/providers/zed-hosted-provider.test.ts tests/providers/zed-retained-budget.test.ts tests/providers/zed-openai-contract.test.ts tests/providers/zed-lossless-arguments.test.ts tests/providers/zed-failure-relay.test.ts tests/providers/zed-argument-consistency.test.ts tests/responses/compaction-event-ownership.test.ts --timeout 30000: 293 passed / 0 failed, 1,110 assertions across seven files. Covers delegated provider behavior, lossless tool arguments, retained-byte/ciphertext ownership, cancellation, failure relay and the changed framing path.
  • bun run typecheck: passed, no type errors in 2,532 files.
  • bun run structure:check and bun run privacy:scan: passed.
  • git diff --check, staged whitespace validation before commit and git diff upstream/dev...HEAD --check: passed. Working tree clean.
  • A separate real-parser probe confirms 65,623 wire bytes / 131,220 searched characters, intact text and a final message_stop event.

Full-suite scope exception: the full local suite and broad import-connected test:changed were not run. Zed imports four delegated adapters and shared OAuth/bridge modules, making that broad run disproportionate to this private newline-search guard on this Windows laptop. All six Zed test files and the related compaction-ownership file were run explicitly. Wider integration/platform coverage remains for repository CI; no hosted CI success is claimed. No workflow dispatch or rerun, live provider request, credential change, dependency change or GUI change was made.

Applied test-design-guard to the work bound and the byte-split framing cases.

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. Scope review preserves credential handling, scrubbing and existing limits.

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.

Summary by CodeRabbit

  • Improvements
    • Zed streamed responses are processed more efficiently when incoming data does not contain a line break. Existing handling of split text, combined lines, and terminal frames is preserved.
  • Documentation
    • Updated byte-accounting documentation to describe Zed stream framing behavior.

@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The Zed event stream searches for buffered newlines only when newly decoded text contains a newline. Tests cover newline-search work and framing behavior across multiple chunk sizes.

Changes: Zed event-stream processing

Layer / File(s) Summary
Conditional newline search and validation
src/adapters/zed.ts, tests/providers/zed-hosted-provider.test.ts, structure/transports/byte-accounting.md
zedEventStream now searches for a newline only when the newly decoded chunk contains one. Tests cover search work, split UTF-8, split CRLF, coalesced lines, and terminal-frame suppression. The documentation describes the conditional search and per-read character-ceiling check.

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

Change: Refactor

Merge Risk: Minimal · up to d9efa

This change reduces redundant scanning of fragmented Zed streams without altering framing or output. No actionable merge-blocking risk was identified.

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 'perf(zed): skip repeated scans of fragmented frames' matches the main change. The zedEventStream now checks whether newly decoded text contains a newline before re-scanning the retained inc…
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 'perf(zed): skip repeated scans of fragmented frames' matches the main change. The zedEventStream now checks whether newly decoded text contains a newline before re-scanning the retained incomplete line, which avoids repeated newline searches over fragmented frames. The title is specific, concise, and follows a conventional commit style. The objectives confirm the same intent: reducing newline-search work for fragmented streams while preserving behavior.

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 ee0098a and d9efa61.

Files selected for processing: 3

  • src/adapters/zed.ts
  • structure/transports/byte-accounting.md
  • tests/providers/zed-hosted-provider.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

Copy link
Copy Markdown
Contributor

✅ Deterministic PR hygiene checks passed.

@github-actions github-actions Bot added enhancement New feature or request review-ready labels Nov 13, 2026
@github-actions
github-actions Bot marked this pull request as ready for review November 13, 2026 14:02
@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

✅ READY

  • all PR quality gates passed; the review readiness checklist is complete.

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.

✅ 4/4 boxes ticked.

This pull request is already Ready for Review.
The review-ready label marks this PR as ready; review automation runs independently.
Maintainers: @lidge-jun @Ingwannu

Hygiene

✅ Deterministic PR hygiene checks passed.

@lidge-jun

Copy link
Copy Markdown
Owner

Maintainer integration into dev (MAINTAINERS.md dev path), recorded by lidge-jun.

Thanks @0disoft.

@lidge-jun
lidge-jun merged commit 3972613 into lidge-jun:dev Nov 14, 2026
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request review-ready

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants