jihuayu measured a 45 MiB chat going through 0.1.41's fetch: each step (withSystem, fitted, normalizeToolChoice, attend, the model, systemKey) parsed the whole body again, 6 parses a request, and normalizeToolChoice wrote it out whole even for an unchanged "auto". Bun's RSS reached 618-751 MiB for 4 serial requests where passing the body through took 270. prepared parses a request once. Each step makes its own change to that one object, in the same order (systemOn, fitTools, toolChoiceOn; unsignedOn for Messages). attend, the model and the system key read the same object, and a refused system prompt moves on it (systemAsUserOn). The body is written out once, only when a step changed it. The parsed chat is let go before the request is sent. A body a step throws on goes through the steps one by one, as before. The string-level helpers stay as wrappers. What was run, on this tree: - mote, the reporter's benchmark (no network, fetch stubbed, 4 x 45.3 MiB, peak RSS sampled every 5ms): 0.1.41 7 parses/request, 1087.5 MiB parsed, peak RSS 1096 / 979 MiB. This change: 2 parses/request (the request and the stub's SSE reply), 181.3 MiB parsed, peak 682 / 680 MiB. The bodies sent have the same SHA-256 in both. - HOME=$(mktemp -d) bun test packages/workbuddy on mote: 189 pass, 0 fail. bun scripts/check.mjs workbuddy: ok. - oneparse.test.mjs (new): 17 chat shapes, invalid JSON and bytes come out the same as the steps one by one made them, with the same tool names; an unchanged chat goes byte for byte; the loader parses a 2 MiB chat once. Broken by hand: every request sent through the step-by-step fallback, the loader test FAILs (5 parses, want 1). An unchanged "auto" counted as a change, the byte-for-byte test FAILs. - Real account (WorkBuddy AI, free-only on, sandbox HOME, this Mac): deepseek-v4.1-flash, a 373-byte chat and a 205,595-byte chat with a developer message, max_completion_tokens and a dotted tool name, through 0.1.41 and this change: 200 "OK" on all four; no sign-in refreshed. npm has 0.1.41, so 0.1.42 is free. Reported-by: jihuayu
113 lines
5.4 KiB
JavaScript
113 lines
5.4 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"
|
|
|
|
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 = 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
|
|
}
|
|
})
|