Files
plugins/packages/workbuddy/sent.test.mjs
T
yetone 95fa76126d workbuddy 0.1.44: a huge chat's ids are hashed in pieces, and it is read once on every path (#461)
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).
2026-10-28 23:26:45 +08:00

28 lines
1.4 KiB
JavaScript

// plugins#461: the chat is read once and its ids made without joining a
// long message into new strings. What goes to WorkBuddy is what 0.1.43
// sent, byte for byte: testdata/sent-0.1.43.json is testdata/sent.mjs run
// against 0.1.43's index.mjs (the npm tarball's), each body by its sha256
// and each derived id by which requests share it. Every fetch is a fake.
import { expect, test } from "bun:test"
import { readFileSync } from "node:fs"
import * as mod from "./index.mjs"
import { sentBy } from "./testdata/sent.mjs"
const was = JSON.parse(readFileSync(new URL("./testdata/sent-0.1.43.json", import.meta.url), "utf8"))
test("every chat goes as 0.1.43 sent it, retries and the free-only switch included", async () => {
// in this order: the second run meets what the first taught the plugin
// (a model that wants the reply after tools, a refused system prompt)
expect(await sentBy(mod, false)).toEqual(was.off)
expect(await sentBy(mod, true)).toEqual(was.freeOnly)
})
test("an id made of pieces is the id of the pieces joined, a surrogate pair split between them too", () => {
const { idFor } = mod._internal
const smile = "🙂"
for (const pieces of [["first:", "a", "[image_url:0123abcd]"], ["x" + smile[0], smile[1] + "y"], ["lone" + smile[0]], [smile[0], smile[0], smile[1]], ["", "u3:", "é中"]]) {
expect(idFor("turn", pieces)).toBe(idFor("turn", pieces.join("")))
}
expect(idFor("turn", ["a"])).not.toBe(idFor("conversation", ["a"]))
})