perf(server): stop serializing terminal chunks just to measure them

BO
2d ago
+125-13

Fork review PR (review only; the upstream submission will use this same diff).

Problem: the terminal output window ran JSON.stringify on every RPC chunk only to count its bytes for the ack window, after which the protocol serialized the same frame again for the wire. On the highest-throughput stream in the app that is a full extra serialization plus allocation per chunk.

Fix: windowed chunks carry terminal events, so charge them with the output queue's existing queuedSize model (128-byte envelope + payload byteLength per event) instead of measuring the serialized frame. Strings are charged by byteLength; unrecognized value shapes keep the exact JSON measurement as a fallback. The window is backpressure, not accounting, and batches upstream already cap at the same 64 KiB by the same charge model, so the two budgets now agree.

Counting check: JSON.stringify calls per terminal chunk go from 1 (plus the protocol's own encode) to 0 for terminal events. Verified with the existing OutputProtocol test file (13 tests, all passing, including the window-limit and stall-recovery cases) and a server typecheck. Not yet measured on a live terminal flood; upstream evidence will be gathered before submission if triage asks. Related but distinct: pingdotgg/t3code#23664 (output release timing).

Muse Wonder Full 1.0, in Muse Code (via T3 Code)


Summary by cubic

Stops the terminal output window from serializing each RPC chunk just to measure its size, removing a full extra JSON.stringify plus allocation per chunk on the highest-throughput stream.

Windowed chunks carry terminal events, so the window now charges them with the output queue's existing queuedSize model (128-byte envelope + payload byteLength per event). The charge covers every variable-size field an event can carry (output data, error message, activity label, thread and terminal ids, snapshot fields), so a large payload in an uncharged field can't bypass the 64 KiB budget; unrecognized value shapes keep the exact JSON measurement as a fallback. Both the window and upstream batches cap at 64 KiB using the same charge model, so their budgets agree. Adds a regression test where a single 64 KiB error message, or a single 64 KiB thread id, fills the window on its own.