You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit da7e26c
Browse filesBrowse the repository at this point in the historyBrowse files
fix(mcp,ai,agent,coding-agent,tui): read large streamed lines and events in linear time (#12451)
Closes#12224.
## Problem
Stream readers kept an incomplete line or SSE event in one growing
string and searched all of it again on every chunk. Searching only from
a saved offset is not enough: searching a string built by `+=` makes V8
flatten (copy) the whole string first, so every chunk still costs the
full pending length.
Example: a 16 MiB MCP tool result arriving as one SSE `data:` line in 64
KiB chunks is about 256 chunks, each rescanning up to 16 MiB.
`consumeSseStream` took 9.8 s, blocking the event loop.
## Fix
Search only the new text, append the leftover to the pending string
without reading it, and read the pending string once when its line or
event ends. This is the approach #11791 used for the stdio line reader.
- **mcp**: `consumeSseStream`. The size limit now uses a running byte
count instead of measuring the buffer on every chunk. 16 MiB: 9.8 s ->
89 ms.
- **ai**: Anthropic, OpenAI Codex, Mistral and pi-messages each had
their own SSE parser with this bug. Anthropic's spec-conformant parser
moves to `src/utils/sse.ts` as `SseDecoder` (now linear) and all four
use it. Each keeps its own abort and error handling.
- **agent**: `streamProxy` line splitting.
- **coding-agent**: RPC JSONL reader, session file loading, llama.cpp
model events (now `SseDecoder`), and the `subagent` and
`rpc-extension-ui` examples.
- **env, durable**: text line readers searched the whole pending line
after every 64 KiB read. 16 MiB line: 1.35 s -> 72 ms.
- **chord**: sync batch lines and `importStore` JSONL.
- **tui**: `StdinBuffer` searched the whole bracketed paste for its end
marker on every chunk. It now searches the new text plus the last 5
characters, in case the marker is split across chunks.
- **p2**: terminal frames copied the whole incomplete frame on every
chunk (backlog replays reach 8 MiB). They now keep the chunks and join
them once per frame. Discord attachment downloads copied the growing 1
MiB upload chunk on every network read (about 800 MiB copied for a 25
MiB file); they now collect the parts and join them once per upload
chunk.
## Behavior changes in the shared SSE parser
- Codex also accepts CRLF line endings (it only split on LF before);
Codex and pi-messages also accept lone CR.
- llama.cpp model events also accept lone CR and CRLF split across
chunks.
- Anthropic no longer ends an event at a CRLF split across chunks.
- pi-messages joins multiple `data:` lines instead of taking the first.
- Mistral strips only the one space after `data:`. The payload is still
trimmed, so JSON is unaffected.
- Anthropic's `raw` diagnostic lines no longer carry comment lines from
earlier blocks that had no event or data.
## Not changed
- `readBodyText` in MCP: append-only, never rescanned. 16 MiB takes 100
ms.
- `p2-sandbox.mjs` exec frames: each frame is at most one chunk of child
output.
- Bounded buffers (1 MiB or less) that are re-measured or rescanned per
chunk: `apps/p2/src/runtime/logs.ts`, the `env/connection.ts` startup
marker scan, the `p2-sandbox.mjs` request header, terminal-reply parsing
in `StdinBuffer`.
- Editor and input paste buffers: they receive the whole paste in one
call from `StdinBuffer`.
## Tests
- mcp: a 1 MiB event in 4 KiB chunks measures at most 2x its size (the
old code measured 135M characters).
- ai: `test/sse.test.ts`, same events for every chunk size and line
ending, CRLF split across chunks.
- tui: paste end marker split at every position.
- p2: terminal frames at every chunk size; a 1 MiB frame calls
`Buffer.concat` once (the old code called it 256 times); a frame whose
handler threw is not delivered again.
- mcp: a line that never ends is rejected at the limit.
- ai: pi-messages skips `data: null`; an empty `event:` does not carry
into the next event.
- coding-agent: a throwing RPC line handler does not get its line joined
with the next.
- Existing tests pass for the Anthropic, Mistral, Codex and pi-messages
providers, proxy, RPC JSONL, session manager, llama, durable env-node,
env differential, and chord sync/store. `npm run check` and `apps/p2`
check pass.
Copy file name to clipboardExpand all lines: packages/agent/CHANGELOG.md
+4Lines changed: 4 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,10 @@
2
2
3
3
## [Unreleased]
4
4
5
+
### Fixed
6
+
7
+
- Fixed `streamProxy()` rescanning an incomplete line on every chunk, which made large proxy events take time quadratic in their size ([#12224](https://github.com/earendil-works/pi/issues/12224))
Copy file name to clipboardExpand all lines: packages/ai/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,6 +39,7 @@
39
39
- Fixed `openai-completions` dropping OpenRouter's `native_finish_reason`, the upstream provider's own finish reason. `rawStopReason` now records it next to `finish_reason`, e.g. `stop.MALFORMED_RESPONSE`, and error messages include it, e.g. `Provider finish_reason: error (native_finish_reason: MALFORMED_FUNCTION_CALL)`. Stop reasons are unchanged ([#11931](https://github.com/earendil-works/pi/issues/11931))
40
40
- Fixed GitHub Copilot listing classifier models the account cannot use, such as `microsoft-decision-1` on accounts whose plan does not include it, where every call failed with HTTP 400. After OAuth login or the next token refresh, only the decision models in the account's Copilot model catalog are listed ([#12395](https://github.com/earendil-works/pi/issues/12395))
41
41
- Fixed HTTP 400 with thinking `off` on Claude Sonnet 5.5 through GitHub Copilot, and on Claude Opus 5.5 and Sonnet 5.5 through Cloudflare AI Gateway and Vercel AI Gateway, which reject `thinking: {type: "disabled"}`. The catalog now takes whether a Claude model can turn thinking off from the thinking toggle on Anthropic's own models.dev entry, for every host that serves it, and selecting `off` on those models uses the lowest supported level ([#12429](https://github.com/earendil-works/pi/issues/12429))
42
+
- Fixed Anthropic, OpenAI Codex, Mistral, and pi-messages SSE streams rescanning an incomplete event on every chunk, which made large events take time quadratic in their size. All four now share one SSE parser, so OpenAI Codex also accepts CRLF line endings, OpenAI Codex and pi-messages accept lone CR line endings, pi-messages joins events with several `data:` lines, and Anthropic no longer ends an event at a CRLF split across chunks ([#12224](https://github.com/earendil-works/pi/issues/12224))
0 commit comments