Commit Graph
5 Commits
Author SHA1 Message Date
DongHyeonkaandClaude Opus 5.5 aac7a32862 perf(realtime): split SSE lines on bytes and decode each line once (R3-5)
The parser now finds CR/LF with Uint8Array.indexOf before decoding, so a
line's UTF-8 byte count comes from byte offsets instead of re-encoding the
text. Each complete line is decoded once by a streaming fatal TextDecoder;
a line that continues into the next read is stream-decoded, and the bytes
of a character split across reads count once it completes, as before.

Behavior kept (the push() input was already Uint8Array; no seam change in
fetch-sse-connection.ts, public API unchanged):
- CR / LF / CRLF, CR and LF split across reads, multi-line data
- one leading BOM stripped (now done by the parser, decoder uses
  ignoreBOM: true, so a BOM at a later line start stays text)
- line / event / incomplete-buffer byte limits, same failure point
- drain() and the per-call item cap; kept bytes are copied
- a pending CR or the item cap waits while only part of a character is
  left, as the text-based parser did
- error codes (MALFORMED_EVENT / EVENT_TOO_LARGE / QUEUE_OVERFLOW / CLOSED)

Differences only on invalid UTF-8: the old parser decoded a whole chunk
first, so malformed bytes won over another error later in that chunk and
malformed bytes past the item cap failed in push(). Now the first
problem in stream order wins and those bytes fail in drain(). Both
still fail closed.

Fixed: Node's streaming TextDecoder (ignoreBOM: false) drops a U+FEFF
right after the stream's first character when that character is split
across reads, e.g. [EF] [BB BF EF BB BF] decoded to "" instead of
"". The old parser inherited that; the new one keeps the text.

Tests: existing parser and connection tests unchanged and passing. Added
a split-BOM case and a differential test against the round-2 parser
(tests/unit/realtime/sse-parser-reference.ts) over 2x1,500 seeded random
ASCII / multibyte streams with random read splits, random small limits,
truncated streams and skipped drains. 300,000 seeds also matched in a
manual run.

Benchmark (manual, not committed; Node v24.14.0, one push of one chunk,
best of 150 runs, host load average ~11-14 so absolute ms are noisy):

| input (one chunk)              | original e59994a | round 2 | new     | new vs original | new vs round 2 |
|--------------------------------|------------------|---------|---------|-----------------|----------------|
| ASCII, 249 KB, 209 events      | 103.72 ms        | 0.48 ms | 0.30 ms | 348x            | 1.6x           |
| Korean/emoji, 249 KB, 209 ev.  | 23.30 ms         | 0.85 ms | 0.44 ms | 53x             | 1.9x           |
| ASCII, 234 KB, 4 x 60 KB lines | 176.45 ms        | 0.61 ms | 0.34 ms | 513x            | 1.8x           |
| Korean/emoji, 234 KB, 4 x 60KB | 25.28 ms         | 1.10 ms | 0.65 ms | 39x             | 1.7x           |

Across five runs, multibyte new vs original stayed between 19x and 70x
(target: 10x or more).

Realtime bundle: 39903 / 40000 gzip bytes (baseline 39685, +218).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-24 15:31:29 +09:00
DongHyeonkaandClaude Opus 5.5 b35304dd48 fix(realtime): retryable OFFLINE for a throwing poll, one BOM, single encodes (Q-6.4-11)
- bounded-poll: an executor that throws becomes OFFLINE retryable:true,
  but OFFLINE is not a poll-retryable kind, so the run ended through
  pollFailure(kind) with retryable:false. OFFLINE now keeps its
  retryable flag on that exit, like the lifecycle OFFLINE exits.
- sse-parser: TextDecoder(ignoreBOM: false) already strips the one
  leading BOM the stream grammar allows; the parser stripped a second one.
  The manual strip is removed.
- event-codec: the raw event was UTF-8 encoded twice; now once.
- fetch-sse-connection: one module TextEncoder instead of a new one per
  event and per cursor check.
- websocket-protocol: the R-05 admission JSDoc sat on utf8ByteLength; it
  now documents exceedsUtf8ByteLimit, and utf8ByteLength reuses one
  encoder.

RED: a throwing executor returned retryable:false; a stream starting with
two BOMs before "data: x" produced an event. Both pass now; the
encode/JSDoc items are covered by the existing codec, SSE and WS suites.
Gates: architecture, types app/test, lint, adapter-inventory, test:unit,
test:component pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-24 13:54:42 +09:00
DongHyeonkaandClaude Opus 5.5 cc906f08b4 fix(realtime): hand SSE items past the per-chunk cap to drain() (Q-6.4-5)
A single reader chunk with more than maxItemsPerChunk (256) items used to
fail the stream with non-retryable QUEUE_OVERFLOW, killing normal replay
bursts even though maxChunkBytes already bounds memory. The parser now
stops at the cap, keeps the remaining decoded text, and returns it through
a new additive drain() call; fetch-sse-connection drains after every push
before reading again. Pushing or finishing with undrained text still fails
closed (QUEUE_OVERFLOW), so buffered text stays bounded by one chunk. A
lone CR followed by another line now re-checks the cap before continuing.
COMMENT and empty item lists reuse frozen constants.

The old parser test that expected QUEUE_OVERFLOW for 3 comments with a
cap of 2 encoded the bug and is replaced.

RED: new parser drain test and a 300-event single-chunk connection test
failed with QUEUE_OVERFLOW before the change. Gates: check:architecture,
check:types:app/test, lint, check:adapter-inventory, test:unit,
test:component all pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-24 13:54:29 +09:00
DongHyeonkaandClaude Opus 5.5 df9b15c039 fix(realtime): parse SSE text a line at a time (R-4.4)
consumeText() walked every code point, appended it to the line string and
called TextEncoder.encode() on it to count UTF-8 bytes, so a 250KB chunk
allocated ~250k arrays on the main thread.

It now finds the next CR/LF with one regex scan, appends the text before it
as one slice and encodes that slice once for its byte count. Byte counts only
grow within a line, so checking the line/incomplete-buffer limits per segment
fails on exactly the same inputs as the per-character check. BOM, pending CR
across reads, CRLF terminator bytes and QUEUE_OVERFLOW ordering are unchanged.

Correctness: existing parser tests pass unchanged. New tests (all green on
the old parser first, as characterization): whole vs byte-by-byte input with
CRLF, CR-only, multi-line data, BOM and a mid-stream U+FEFF, 2/3/4-byte
UTF-8 split across reads; trailing CR blank line dispatched at finish; UTF-8
bytes (not UTF-16 units) counted against line and event limits, also when a
multibyte character is split across reads; a max-size line over 1,000-byte
reads accepted and one byte more rejected; a 248,780-byte chunk of 200
events.

Benchmark (throwaway script, not committed; Node 24.14; 200 events in one
~250KB chunk; machine under load avg 39-54 on 22 cores, so numbers are noisy):
- old/new interleaved in one process, forced GC before each run, median:
  ASCII 248,780 B: 294-345 ms -> 4.3-8.2 ms (36-80x)
  multibyte 248,180 B (Korean/emoji/accented): 95-130 ms -> 19-26 ms (~5x)
- each parser standalone, sequential runs, average:
  ASCII: 664-764 ms -> 0.9-3.7 ms
  multibyte: 155-196 ms -> 1.5-6.3 ms
Remaining cost of the new parser is mostly native TextDecoder/TextEncoder
work (profiled); a JS charCode loop for the byte count was not faster.

Gates: check:architecture, check:types:app, check:types:test, lint,
test:component pass. test:unit passes except removal-fixture.test.ts, which
needs generated artifacts/release evidence absent from this fresh worktree
(unrelated).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-24 12:19:48 +09:00
donghyeon-ka 6c52cdb916 feat: 기능 추가 과정중 2026-07-30 15:58:20 +09:00