0.1.42 brought jihuayu's 45 MiB repro from 7 parses a request to 2, and
0.1.43 keeps that: one full parse and one full stringify a request. What
was left of the peak was attendChat: the conversation and turn ids were
made from template strings holding the whole user message (`first:${sig}`,
`u1:${sig}`, `${conv}|${turn}`, then `${SALT}|kind|${key}`), each a fresh
45 MB string, ~160 MiB of peak a request. The keys are now lists of
pieces fed to one sha256 each, whole (Bun hashes a one-byte string in
place; 1 MiB slices cost a copy and were worse), with a high surrogate
split between pieces held for the next one, so every id is the hash of
the same string as before.
Also read once on paths the repro doesn't take:
- "Free models only" parsed the body again for its model; it reads the
chat prepared already parsed.
- A model known to want a reply after tools (11153) had the body parsed
and stringified again on every request; the reply goes into the one
parsed chat before the one stringify.
- The prepared result no longer holds the parsed tree past the send.
Every byte WorkBuddy sees is unchanged: sent.test.mjs runs 12 chats
(OpenCode tools, default effort, developer turn, after-tools refusal and
known, system refusal and known, both, Messages, bytes, not JSON),
switch off and on, and holds the gunzipped body's sha256, url, encoding
and which requests share an id to testdata/sent-0.1.43.json, made by
running the same harness on npm's 0.1.43 index.mjs (sha 9213db6d...).
Run on mote (Bun 1.4.2), the issue's benchmark.mjs as written but for
the import, 45.3 MiB body, 4 requests, concurrency 1, /usr/bin/time -l,
3 runs each:
direct (no plugin) 336 MiB
0.1.41 981-1117 MiB, 28 parses (1087.5 MiB), 24 stringifies
0.1.43 717-760 MiB, 8 parses (181.3 MiB), 20 stringifies
0.1.44 583 MiB all 3 runs, same counts, ~370-420 ms wall
(0.1.43 ~400-530 ms)
One request: direct 134, 0.1.41 569, 0.1.43 390, 0.1.44 275 MiB (with
id derivation stubbed out entirely: 229).
- HOME=$(mktemp -d) bun test in packages/workbuddy at this head:
201 pass, 0 fail. bun scripts/check.mjs workbuddy: ok.
- Broken by hand, each red: free-only parsing again -> the free-only
parse-count test; reply after tools put in by re-parsing -> the
after-tools parse-count test; no surrogate hold -> the idFor pieces
test; the reply put in before the system move -> sent.test.mjs.
gzip was measured too: Buffer.from before gzipSync made no difference
to the peak, so it is unchanged.
Not tried: a live WorkBuddy request; the reporter's Linux and Bun
1.4.4 (mote has 1.4.2); concurrency 4; a huge body that isn't ASCII
(Bun keeps it two-byte, so hashing it may copy); a huge Messages body
(its own path, not changed here).
175 lines
8.8 KiB
JavaScript
175 lines
8.8 KiB
JavaScript
// plugins#461 (jihuayu): a 45 MiB chat was parsed six times on its way to
|
|
// WorkBuddy, once by each step, and written out whole again even when no
|
|
// step changed it. prepared reads it once; what goes out is what the steps
|
|
// one by one made of it.
|
|
import { expect, test } from "bun:test"
|
|
import { WorkBuddyAuthPlugin, _internal } from "./index.mjs"
|
|
import { unz } from "./testdata/unz.mjs"
|
|
|
|
const { prepared, withSystem, fitted, normalizeToolChoice, withoutUnsigned } = _internal
|
|
|
|
const chats = [
|
|
{ model: "deepseek-v4.1-flash", messages: [{ role: "system", content: "Be brief." }, { role: "user", content: "hi" }] },
|
|
{ model: "m", messages: [{ role: "system", content: "s" }, { role: "user", content: "hi" }], tool_choice: "auto", tools: [{ type: "function", function: { name: "read", parameters: { type: "object" } } }] },
|
|
{ model: "m", messages: [{ role: "developer", content: "d" }, { role: "user", content: "x" }], max_completion_tokens: 9 },
|
|
{ model: "m", messages: [{ role: "user", content: "no system" }] },
|
|
{ model: "m", messages: [{ role: "system", content: "You are Claude Code, Anthropic's official CLI for Claude" }, { role: "user", content: "q" }] },
|
|
{ model: "m", messages: [{ role: "system", content: "s" }, { role: "user", content: "q" }, { role: "assistant", content: "a", reasoning_content: "thought" }, { role: "user", content: "q2" }] },
|
|
{ model: "m", messages: [{ role: "system", content: "s" }, { role: "user", content: "run" }], tools: [{ type: "function", function: { name: "fs.read", parameters: { type: "object", properties: { v: { type: ["string", "null"] } } } } }], tool_choice: { type: "function", function: { name: "fs.read" } } },
|
|
{ model: "m", messages: [{ role: "system", content: "s" }, { role: "user", content: "x" }], tools: [{ type: "function", function: { name: "a" } }], tool_choice: "none" },
|
|
{ model: "m", messages: [{ role: "system", content: "s" }, { role: "user", content: "x" }], tool_choice: { type: "required" } },
|
|
{ model: "m", messages: [{ role: "system", content: "s" }, { role: "user", content: "x" }], tool_choice: null },
|
|
{ model: "m", messages: [{ role: "system", content: "s" }, { role: "user", content: "x" }], tool_choice: { type: "other" } },
|
|
{ model: "m", messages: [] },
|
|
{ model: "m" },
|
|
[1, 2],
|
|
"a string",
|
|
42,
|
|
null,
|
|
]
|
|
|
|
const stepByStep = (body) => {
|
|
const fit = fitted(withSystem(body))
|
|
return { body: normalizeToolChoice(fit.body), names: fit.names }
|
|
}
|
|
const sent = (p) => (p.changed ? JSON.stringify(p.chat) : p.body)
|
|
const read = (s) => {
|
|
try {
|
|
return JSON.parse(s)
|
|
} catch {
|
|
return s
|
|
}
|
|
}
|
|
|
|
test("one parse makes what the steps one by one made", () => {
|
|
for (const c of chats) {
|
|
const body = JSON.stringify(c)
|
|
const old = stepByStep(body)
|
|
const p = prepared(body, false)
|
|
expect(read(sent(p))).toEqual(read(old.body))
|
|
expect([...p.names]).toEqual([...old.names])
|
|
}
|
|
for (const body of ["not json", "", "{", new TextEncoder().encode(JSON.stringify(chats[0]))]) {
|
|
const old = stepByStep(body)
|
|
expect(read(sent(prepared(body, false)))).toEqual(read(old.body))
|
|
}
|
|
})
|
|
|
|
test("a chat no step changes goes byte for byte, not written out again", () => {
|
|
// spaced as no JSON.stringify writes it: any rewrite would show
|
|
const body = `{"model": "m", "messages": [{"role": "system", "content": "s"}, {"role": "user", "content": "x"}], "tool_choice": "auto"}`
|
|
const p = prepared(body, false)
|
|
expect(p.changed).toBe(false)
|
|
expect(sent(p)).toBe(body)
|
|
})
|
|
|
|
test("a Messages body is read once too, its unsigned thinking dropped as before", () => {
|
|
const body = JSON.stringify({
|
|
model: "claude",
|
|
system: [{ type: "text", text: "s" }],
|
|
messages: [
|
|
{ role: "user", content: "q" },
|
|
{ role: "assistant", content: [{ type: "thinking", thinking: "t" }, { type: "text", text: "a" }] },
|
|
{ role: "user", content: "q2" },
|
|
],
|
|
})
|
|
expect(read(sent(prepared(body, true)))).toEqual(read(withoutUnsigned(body)))
|
|
const plain = JSON.stringify({ model: "claude", messages: [{ role: "user", content: "q" }] })
|
|
expect(sent(prepared(plain, true))).toBe(plain)
|
|
})
|
|
|
|
test("the loader parses a long chat once", async () => {
|
|
const realFetch = globalThis.fetch
|
|
const realParse = JSON.parse
|
|
let parses = 0
|
|
let out
|
|
JSON.parse = (s, ...a) => {
|
|
if (typeof s === "string" && s.length > 1 << 20) parses++
|
|
return realParse(s, ...a)
|
|
}
|
|
globalThis.fetch = async (_url, init) => {
|
|
out = unz(init.body)
|
|
return new Response('data: {"choices":[{"delta":{"content":"OK"}}]}\n\ndata: [DONE]\n\n', { headers: { "content-type": "text/event-stream" } })
|
|
}
|
|
try {
|
|
const hooks = await WorkBuddyAuthPlugin({ client: { auth: { set: async () => {} } }, magpie: { option: () => false } })
|
|
const auth = { type: "oauth", access: "test-only", refresh: "test-only", uid: "u", expires: Date.now() + 3600000 }
|
|
const loader = await hooks.auth.loader(async () => auth)
|
|
const body = JSON.stringify({
|
|
model: "deepseek-v4.1-flash",
|
|
stream: true,
|
|
tool_choice: "auto",
|
|
messages: [{ role: "system", content: "s" }, { role: "user", content: "x".repeat(2 << 20) }],
|
|
tools: [{ type: "function", function: { name: "test.tool.name", parameters: { type: "object" } } }],
|
|
})
|
|
const res = await loader.fetch("https://synthetic.invalid/v2/chat/completions", { method: "POST", body, headers: { "content-type": "application/json" } })
|
|
await res.text()
|
|
expect(parses).toBe(1)
|
|
expect(read(out)).toEqual(read(stepByStep(body).body))
|
|
} finally {
|
|
globalThis.fetch = realFetch
|
|
JSON.parse = realParse
|
|
}
|
|
})
|
|
|
|
// counted is what the loader parsed and wrote out of chats over 1 MiB,
|
|
// through a plugin with the given magpie options, for each chat sent; the
|
|
// fake answers each chat with answer(text) or OK
|
|
async function counted(bodies, { option = () => false, answer = () => null, auth } = {}) {
|
|
const realFetch = globalThis.fetch
|
|
const realParse = JSON.parse
|
|
const realStringify = JSON.stringify
|
|
const n = { parses: 0, writes: 0 }
|
|
JSON.parse = (s, ...a) => {
|
|
if (typeof s === "string" && s.length > 1 << 20) n.parses++
|
|
return realParse(s, ...a)
|
|
}
|
|
JSON.stringify = (...a) => {
|
|
const s = realStringify(...a)
|
|
if (typeof s === "string" && s.length > 1 << 20) n.writes++
|
|
return s
|
|
}
|
|
globalThis.fetch = async (url, init) => {
|
|
if (String(url).endsWith("/v3/config")) return Response.json({ code: 0, data: { agents: [{ name: "cli", models: ["hy3", "gemini-461"] }], models: [{ id: "hy3", credits: "x0.00" }, { id: "gemini-461", credits: "x0.00" }] } })
|
|
return answer(unz(init.body)) ?? new Response("data: [DONE]\n\n", { headers: { "content-type": "text/event-stream" } })
|
|
}
|
|
const out = []
|
|
try {
|
|
const hooks = await WorkBuddyAuthPlugin({ client: { auth: { set: async () => {} } }, magpie: { option } })
|
|
const loader = await hooks.auth.loader(async () => auth ?? { type: "oauth", access: "test-only", refresh: "test-only", uid: "u", expires: Date.now() + 3600000 })
|
|
for (const body of bodies) {
|
|
n.parses = n.writes = 0
|
|
const res = await loader.fetch("https://copilot.tencent.com/v2/chat/completions", { method: "POST", body, headers: { "content-type": "application/json" } })
|
|
await res.text()
|
|
out.push({ status: res.status, ...n })
|
|
}
|
|
} finally {
|
|
globalThis.fetch = realFetch
|
|
JSON.parse = realParse
|
|
JSON.stringify = realStringify
|
|
}
|
|
return out
|
|
}
|
|
|
|
const long = (model, extra = {}) =>
|
|
JSON.stringify({ model, stream: true, messages: [{ role: "system", content: "s" }, { role: "user", content: "x".repeat(2 << 20) }], ...extra })
|
|
|
|
test("with Free models only on, the model is read off the one parse", async () => {
|
|
const [r] = await counted([long("hy3")], { option: (_p, k) => k === "free_only", auth: { type: "api", key: "ck_test0000000000abcdefgh" } })
|
|
expect(r).toEqual({ status: 200, parses: 1, writes: 0 })
|
|
})
|
|
|
|
test("a model that wants the reply after tools has it put in on the one parse", async () => {
|
|
const call = { role: "assistant", content: "", tool_calls: [{ id: "c", type: "function", function: { name: "bash", arguments: "{}" } }] }
|
|
const chat = (n) => JSON.stringify({ model: "gemini-461", messages: [{ role: "user", content: "go" }, call, { role: "tool", tool_call_id: "c", content: "y".repeat(2 << 20) }, { role: "user", content: `now ${n}` }] })
|
|
// refused unless the reply sits after the tool's result; read without a
|
|
// parse of its own, which would be counted
|
|
const answer = (text) =>
|
|
text.indexOf('{"role":"assistant","content":""}') > text.indexOf('"role":"tool"') ? null : new Response('{"error":{"code":11153}}', { status: 400 })
|
|
// the first teaches the plugin (refused, sent again with the reply); the
|
|
// second has it up front: read once, written once
|
|
const [first, second] = await counted([chat(1), chat(2)], { answer })
|
|
expect(first.status).toBe(200)
|
|
expect(second).toEqual({ status: 200, parses: 1, writes: 1 })
|
|
})
|