Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
plugin: a streamed reply's chunk frame is joined, not stringified, and its bytes viewed, not copied
yetoneef75a9a2b54c
diff --git a/internal/plugin/host.js b/internal/plugin/host.jsindex ca025abba..6badab79a 100644--- a/internal/plugin/host.js+++ b/internal/plugin/host.js@@ -112,8 +112,14 @@ function queueLine(line) {// send queues one message, ignoring whether it was written.const send = (msg) => queueLine(JSON.stringify(msg) + "\n").promise-// sendCancelable is send, with a way to give the wait up.-const sendCancelable = (msg) => queueLine(JSON.stringify(msg) + "\n")+// sendCancelable is send, with a way to give the wait up. A string is a+// line made already (chunkLine).+const sendCancelable = (msg) => queueLine(typeof msg === "string" ? msg : JSON.stringify(msg) + "\n")++// chunkLine is JSON.stringify({id, event: "chunk", data}) + "\n", made by+// joining: base64 has nothing JSON escapes, and stringify scanning every+// byte of it for something to escape cost more than the base64 itself.+const chunkLine = (id, data) => '{"id":' + JSON.stringify(id) + ',"event":"chunk","data":"' + data + '"}\n'// A host cancellation suppresses the answer; a local abort still owes Go// its error. The record, rather than the signal, owns pending wire output.@@ -2431,7 +2437,8 @@ async function fetchAs(id, record, key, params) {// the loop leaves rather than parking on a stream that never drainsfor await (const chunk of res.body) {checkFetch(record)- const bytes = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)+ // a chunk the stream handed over is this reader's: viewed, not copied+ const bytes = Buffer.isBuffer(chunk) ? chunk : ArrayBuffer.isView(chunk) ? Buffer.from(chunk.buffer, chunk.byteOffset, chunk.byteLength) : Buffer.from(chunk)for (let off = 0; off < bytes.length; ) {// one frame at a time, no more than the reader has credited: a// reader that stopped leaves the upstream paused here@@ -2440,7 +2447,7 @@ async function fetchAs(id, record, key, params) {if (n <= 0) breakconst data = bytes.subarray(off, off + n).toString("base64")gate.spend(n + FRAME_OVERHEAD)- await sendForFetch(record, { id, event: "chunk", data })+ await sendForFetch(record, chunkLine(id, data))checkFetch(record)off += n}diff --git a/internal/plugin/host_flow_test.go b/internal/plugin/host_flow_test.goindex 93ba2e6ba..4c0a52632 100644--- a/internal/plugin/host_flow_test.go+++ b/internal/plugin/host_flow_test.go@@ -121,6 +121,34 @@ async function main() {assert.equal(await run('gate.take(64)'), 64)run('gate.stop(); gate.add(1)')assert.equal(await run('gate.take(64)'), -1, 'stopped gate refuses remaining credit')+ } else if (name === 'chunk-frames') {+ const parts = [new Uint8Array([0, 1, 2, 250, 251, 252]), Buffer.from('data: {"a":"\\u00e9 \\"q\\""}\n\n'), new Uint8Array(70 << 10).fill(65)]+ context.reply.body = (async function* () { for (const p of parts) yield p })()+ let copies = 0+ let stringified = 0+ const realJSON = run('JSON')+ context.Buffer = new Proxy(Buffer, { get(target, key) {+ if (key !== 'from') return Reflect.get(target, key)+ return (...args) => {+ if (args.length === 1 && ArrayBuffer.isView(args[0])) copies+++ return target.from(...args)+ }+ } })+ context.JSON = { parse: realJSON.parse, stringify: (v, ...rest) => {+ if (v && v.event === 'chunk') stringified+++ return realJSON.stringify(v, ...rest)+ } }+ fetchOne()+ await until(idle, 'streamed fetch settles')+ context.Buffer = Buffer+ delete context.JSON+ const chunks = lines.filter(msg => msg.event === 'chunk')+ assert.ok(chunks.every(msg => msg.id === 1), 'every frame is the fetch\'s')+ assert.equal(chunks.length, 4, 'a chunk over 64 KB goes in two frames')+ assert.deepEqual(Buffer.concat(chunks.map(msg => Buffer.from(msg.data, 'base64'))), Buffer.concat(parts))+ assert.deepEqual(lines.map(msg => msg.event || 'result'), ['head', 'chunk', 'chunk', 'chunk', 'chunk', 'result'])+ assert.equal(stringified, 0, 'chunk frames are joined, not stringified')+ assert.equal(copies, 0, 'chunks are viewed, not copied')} else if (name === 'split-chunk') {blockOn = 'chunk'context.reply.body = (async function* () { yield Buffer.alloc(3 * (64 << 10)) })()@@ -651,6 +679,20 @@ func TestFlowBigReplyProd(t *testing.T) {// vendor's head — leave the host able to answer a later request. It is the Go// side of the lifecycle: the host's slots come back, and the JS side's own// finally (covered by construction) lets each request's controller and gate go.+// TestHostFramesChunksCheaply: the body's chunks go out as the same frames,+// but without copying each chunk or stringifying each frame: stringify+// scanned every base64 byte for something to escape, which cost more than+// the base64.+func TestHostFramesChunksCheaply(t *testing.T) {+ node, err := exec.LookPath("node")+ if err != nil {+ t.Skip("no node on PATH")+ }+ if out, err := runHostJSCase(t, node, "chunk-frames"); err != nil {+ t.Fatalf("chunk frames: %v\n%s", err, out)+ }+}+func TestFlowAbortDuringSetupStaysHealthy(t *testing.T) {node, err := exec.LookPath("node")if err != nil {