Skip to content

perf(bridge-json): scan stream deltas instead of reparsing the buffer - #140

Merged
Nomadcxx merged 1 commit into
mainfrom
fix/bridge-json-incremental-scan
Oct 8, 2026
Merged

Nomadcxx merged 1 commit into
mainfrom
fix/bridge-json-incremental-scan

Conversation

@Nomadcxx

@Nomadcxx Nomadcxx commented Oct 8, 2026

Copy link
Copy Markdown
Owner

Refs #139.

The bridge detector reparsed the whole candidate buffer on every partial delta, and called trimStart() on it every push. A streamed write envelope cost O(n²) on OpenCode's main thread.

Change

  • Track bracket depth and string state per delta. Only run the full parse once the candidate is balanced outside a string, which any parseable envelope has to be.
  • Check a fenced candidate's info line once, not on every push.

Proof

  • New unit test: a 9 KB write streamed in 7-char chunks now hits JSON.parse once (was 175). Plus a fenced-envelope streaming test.
  • Differential fuzz against the previous detector: 14,400 random chunkings (delta and snapshot modes, 6 tool sets, fences, arrays, prose, malformed input). Zero mismatches. A planted bug in the gate produced 3,064 mismatches, so the fuzz catches real divergence.
  • Benchmark, 4-char deltas:
Envelope Before After
20 KB 26 ms 0.9 ms
50 KB 146 ms 1.8 ms
100 KB 588 ms 2.7 ms
  • Live run: OpenCode 1.18.35 + local build, cursor-acp/auto. The model wrote a 40-function file through the bridge (call_bridge_* in the log), content intact including }]\" inside strings.
  • bridge-json unit tests 55/55, opencode-loop integration 25 pass / 1 skip. Full-suite failures and tsc errors match main exactly (42 and 43, none in this file).

The bridge detector reparsed the whole candidate buffer on every partial
delta and called trimStart() on it each push, so a streamed write envelope
cost O(n^2) on the OpenCode main thread (175 JSON.parse calls for a 9 KB
write; 588 ms for a 100 KB one).

Track bracket depth and string state per delta and only parse once the
candidate is balanced outside a string, which any parseable envelope must
be. Check a fenced candidate's info line once instead of every push.
Decisions are unchanged: a differential fuzz against the previous detector
over 14,400 chunkings matched exactly. 100 KB envelope: 588 ms -> 2.7 ms.

Refs #139
@Nomadcxx
Nomadcxx merged commit f008bd8 into main Oct 8, 2026
2 checks passed
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.

1 participant