Files
plugins/packages/workbuddy/aftertools.test.mjs
T
yetone f4d48ac3ee workbuddy 0.1.43: a chat names the model's default effort as WorkBuddy's client does, and goes gzipped
troilus1011 on X: WorkBuddy's DeepSeek in OpenCode through magpie thought
at low and high, never at the default. OpenCode's default variant names
no reasoning_effort, and WorkBuddy doesn't think without one: measured on
www.workbuddy.ai with 0.1.42, deepseek-v4.1-flash gave 0 reasoning
tokens with no effort (2 of 2), 0 at none, 16/37 at low, 34/56 at high.
WorkBuddy's own client (CLI 2.165.0 in WorkBuddy 5.9.3,
configureThinkingSettings) sends the session's effort, else the
reasoningEffort setting, else the model's reasoning.defaultEffort from
/v3/config, which is "high" for this model on both builds. A chat that
names no effort now gets the model's own defaultEffort when its listed
levels hold it, read from the same cached /v3/config list (one read per
minute; a failed read leaves the chat as it came and is not retried on
every chat). An effort the client names, none included, is untouched and
the list isn't read for it. Models with no reasoning config get nothing.

runmiNeverstop on X: 2.89 GB up to copilot.tencent.com through magpie.
WorkBuddy's client gzips a model request's body (GzipRequestProcessor,
on with productFeatures.RequestBodyGzip, true in both builds'
product.json and not turned off by the remote config); the plugin sent
it as plain JSON. Agent chat JSON (the whole history each turn, tool
schemas) compresses several-fold, so the upload was several times what
the official client sends for the same turns. A chat-completions body
now goes gzipped with Content-Encoding: gzip on both builds;
unanswered() weighs the gzipped size against the front door's 2 MiB.
Claude's Messages route goes as it came (not tried gzipped live).

Run (on mote, HOME=$(mktemp -d) bun test in packages/workbuddy): 197
pass, 0 fail. bun scripts/check.mjs: exit 0. Fakes that parse the sent
body gunzip it (testdata/unz.mjs); reset.test.mjs's 2 MiB bodies are
random base64 so they stay over 2 MiB on the wire.
Live (www.workbuddy.ai, a real account in a sandbox HOME): with 0.1.43
the default sent reasoning_effort high gzipped and thought (34 and 51
reasoning tokens), none stayed 0, low and high as before, all 200. A
gzipped chat answered 200 the same as a plain one.
Broken by hand: origin/main's index.mjs with the new
default-effort.test.mjs: 5 of 8 red (default effort, failed-read reuse,
both gzip tests, defaultEffortOf); the 3 that pass guard what must not
change (named efforts, models without a default, Messages).
Not tried: a live chat on the China build (copilot.tencent.com answered
401 to every token available here, its /v3/config read fine); Messages
gzipped; the reporters' own OpenCode sessions end to end.
2026-10-28 22:23:20 +08:00

97 lines
4.9 KiB
JavaScript

// WorkBuddy AI's gemini-3.8-flash answers tool results followed by the
// user's message with 400 code 11153, "the conversation must end with a user
// message" (#113). The body is the one www.workbuddy.ai sent on 2026-10-14.
import { expect, test } from "bun:test"
import { WorkBuddyAIAuthPlugin, _internal } from "./index.mjs"
import { unz } from "./testdata/unz.mjs"
const { replyAfterTools } = _internal
const REFUSAL = JSON.stringify({
code: 11153,
msg: "the conversation must end with a user message",
requestId: "555fb7db0fb1ef01abc76992cbfcad0e",
extError: { code: "400001", message: "the conversation must end with a user message", param: "", type: "invalid_request_error", StatusCode: 400, Request: null, Response: null },
displayMsg: { en: "This conversation must end with a user message. Please add your input and retry." },
})
const call = { role: "assistant", content: null, tool_calls: [{ id: "call_1", type: "function", function: { name: "Bash", arguments: '{"command":"ls"}' } }] }
const result = { role: "tool", tool_call_id: "call_1", content: "a.txt\nb.txt" }
const ask = { role: "user", content: "List the files." }
const reminder = { role: "user", content: [{ type: "text", text: "<system-reminder>note</system-reminder>" }] }
// served answers like gemini-3.8-flash: tool results then a user message are
// refused, anything else is served
async function withService(fn) {
const realFetch = globalThis.fetch
const sent = []
globalThis.fetch = async (_url, init) => {
const b = JSON.parse(unz(init.body))
sent.push(b)
const m = b.messages
let i = m.length - 1
while (i >= 0 && m[i].role === "user") i--
if (m.at(-1).role !== "user" || (i < m.length - 1 && m[i]?.role === "tool")) {
return new Response(REFUSAL, { status: 400, headers: { "content-type": "application/json" } })
}
return new Response("data: [DONE]\n\n", { headers: { "content-type": "text/event-stream" } })
}
try {
const hooks = await WorkBuddyAIAuthPlugin({ client: {} })
const loader = await hooks.auth.loader(async () => ({ type: "oauth", access: "access", uid: "uid", expires: Date.now() + 3600_000 }))
const post = (model, messages) =>
loader.fetch("https://www.workbuddy.ai/v2/chat/completions", { method: "POST", body: JSON.stringify({ model, stream: true, messages }) })
await fn(post, sent)
} finally {
globalThis.fetch = realFetch
}
}
test("tool results then the user's message go again with an empty reply between them", async () => {
await withService(async (post, sent) => {
const res = await post("gemini-after-tools-a", [ask, call, result, reminder])
expect(res.status).toBe(200)
expect(sent.length).toBe(2)
const roles = sent[1].messages.map((m) => m.role)
expect(roles).toEqual(["system", "user", "assistant", "tool", "assistant", "user"])
expect(sent[1].messages[4]).toEqual({ role: "assistant", content: "" })
// what the user said is still the user's, last
expect(sent[1].messages[5]).toEqual(reminder)
})
})
test("a model that refused it gets the empty reply up front on its next chat", async () => {
await withService(async (post, sent) => {
await post("gemini-after-tools-b", [ask, call, result, reminder])
sent.length = 0
const res = await post("gemini-after-tools-b", [ask, call, result, ask, reminder])
expect(res.status).toBe(200)
expect(sent.length).toBe(1)
expect(sent[0].messages.map((m) => m.role)).toEqual(["system", "user", "assistant", "tool", "assistant", "user", "user"])
// another model is asked as it was sent
const other = await post("deepseek-after-tools-b", [ask, call, result, { role: "assistant", content: "Two." }, ask])
expect(other.status).toBe(200)
expect(sent.length).toBe(2)
expect(sent[1].messages.filter((m) => m.role === "assistant" && m.content === "").length).toBe(0)
})
})
test("a chat ending with the assistant's message is a prefill, refused as it was", async () => {
await withService(async (post, sent) => {
const res = await post("gemini-after-tools-c", [ask, call, result, { role: "assistant", content: "There are" }])
expect(res.status).toBe(400)
expect(sent.length).toBe(1)
expect((await res.json()).msg).toBe("the conversation must end with a user message")
})
})
test("replyAfterTools changes only tool results followed by user messages", () => {
const body = (messages) => JSON.stringify({ model: "m", messages })
expect(JSON.parse(replyAfterTools(body([ask, call, result, ask, reminder]))).messages.map((m) => m.role)).toEqual(["user", "assistant", "tool", "assistant", "user", "user"])
expect(replyAfterTools(body([ask]))).toBeNull()
expect(replyAfterTools(body([ask, call, result]))).toBeNull()
expect(replyAfterTools(body([ask, call, result, { role: "assistant", content: "ok" }, ask]))).toBeNull()
expect(replyAfterTools(body([ask, { role: "assistant", content: "hi" }]))).toBeNull()
expect(replyAfterTools("not json")).toBeNull()
})