diff --git a/packages/qoder/README.md b/packages/qoder/README.md index 3d7728f..bbb28e2 100644 --- a/packages/qoder/README.md +++ b/packages/qoder/README.md @@ -91,6 +91,17 @@ itself. answers no call before it. A result with no call left to answer goes as text in the user's turn. - Your tools become Qoder's native function tools. + - A call the client sent without an id gets one made from its turn, its + place and its arguments, so it goes out the same on every request. +- It marks the prompt for Qoder's cache as Qoder's own client does (since + 0.2.26, #290): the last block of the last user turn carries + `cache_control: ephemeral` (nothing is marked when the last turn is a + tool's result), user turns carry their blocks as `contents` too, and each + request names its conversation in `session_id`. That id is a hash of + Codex's thread (`prompt_cache_key`) when magpie sends one, else of the + conversation's start, so every turn of one conversation names the same + session; the key itself never goes out. Each request has its own + `request_id`. - The model's own configuration from Qoder's list goes with it. - It picks the reasoning effort from the model's own levels: - The nearest level to the one asked, the higher one on a tie. @@ -257,8 +268,8 @@ the account's credentials. - Install Qoder desktop, or sign in to Qoder CLI once, to make its native runtime available. The plugin finds the desktop's `resources/umid` - (`Qoder.app` on macOS, `%LOCALAPPDATA%\Programs\Qoder` on Windows, - `/opt/Qoder` on Linux) or the runtime the CLI caches in `~/.qoder/.bin` + (`Qoder.app` on macOS, `%LOCALAPPDATA%\Programs\Qoder` or, since + 0.2.26, `%ProgramFiles%\Qoder` on Windows, `/opt/Qoder` on Linux) or the runtime the CLI caches in `~/.qoder/.bin` (`runtime-info---*` on macOS and Linux, `umid-win32-x64-*\runtime-info.exe` on Windows; `QODER_CONFIG_DIR`, `QODER_CLI_HOME` and `QODER_CONFIG_DIR_NAME` move it as they move the diff --git a/packages/qoder/cache.test.mjs b/packages/qoder/cache.test.mjs new file mode 100644 index 0000000..a524d70 --- /dev/null +++ b/packages/qoder/cache.test.mjs @@ -0,0 +1,77 @@ +// #290: Codex's side session on Qoder's Ultimate read 0% of its prompt from +// cache while Fast read most of it. Qoder's own client (qoder-agent-sdk in +// Qoder.app, qodercli 1.1.75) marks the last turn's last block +// cache_control ephemeral, sends the user turns' blocks as contents too, and +// names its session (session_id) on every turn; the plugin did none of +// that, and gave a call without an id a new random id on every request. + +import { expect, test } from "bun:test" +import { _internal } from "./index.mjs" + +const { qoderBody, sessionOf } = _internal +const m = { key: "ultimate", config: { key: "ultimate" }, context: 1_000_000 } + +const turns = [ + { role: "system", content: "You are Codex." }, + { role: "user", content: "review this diff" }, + { role: "assistant", content: "", tool_calls: [{ type: "function", function: { name: "shell", arguments: '{"cmd":"git diff"}' } }] }, + { role: "tool", content: "diff --git a/x b/x" }, + { role: "assistant", content: "Looks fine. Anything else?" }, + { role: "user", content: [{ type: "text", text: "check the tests" }] }, +] + +const marked = (body) => + body.messages.flatMap((x, i) => [...(Array.isArray(x.content) ? x.content : []), ...(x.contents ?? [])].filter((b) => b.cache_control).map((b) => [i, b.text, b.cache_control])) + +test("the last user turn's last block is marked for the cache, as Qoder's client marks it", () => { + const body = qoderBody({ messages: turns }, m) + const last = body.messages.at(-1) + expect(last.role).toBe("user") + expect(last.content.at(-1)).toEqual({ type: "text", text: "check the tests", cache_control: { type: "ephemeral" } }) + expect(last.contents).toEqual(last.content) + // only that block: the system prompt and earlier turns go as before + expect(new Set(marked(body).map(([i]) => i))).toEqual(new Set([body.messages.length - 1])) + // every user turn carries its blocks as contents too + for (const x of body.messages.filter((x) => x.role === "user")) expect(x.contents).toEqual(x.content) +}) + +test("a turn ending in a tool's result is not marked, as Qoder's client leaves it", () => { + const body = qoderBody({ messages: turns.slice(0, 4) }, m) + expect(body.messages.at(-1).role).toBe("tool") + expect(marked(body)).toEqual([]) +}) + +test("the caller's turns are left as they were", () => { + const msgs = structuredClone(turns) + qoderBody({ messages: msgs }, m) + expect(msgs).toEqual(turns) +}) + +test("every turn of one conversation names one session, another conversation another", () => { + const a = qoderBody({ messages: turns.slice(0, 2) }, m).session_id + expect(qoderBody({ messages: turns }, m).session_id).toBe(a) + expect(a).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/) + expect(qoderBody({ messages: [turns[0], { role: "user", content: "another task" }] }, m).session_id).not.toBe(a) + // Codex's thread names it when sent, and the key itself never goes out + const k = qoderBody({ prompt_cache_key: "019a-thread", messages: turns }, m) + expect(k.session_id).toBe(sessionOf({ prompt_cache_key: "019a-thread", messages: [] })) + expect(k.session_id).not.toBe(a) + expect(JSON.stringify(k)).not.toContain("019a-thread") + // each request is its own + const [x, y] = [qoderBody({ messages: turns }, m), qoderBody({ messages: turns }, m)] + expect(x.request_id).not.toBe(y.request_id) + expect(x.chat_record_id).toBe(x.request_id) +}) + +test("a call sent without an id goes out with the same id on every turn", () => { + const ids = (body) => body.messages.flatMap((x) => [...(x.tool_calls ?? []).map((c) => c.id), ...(x.tool_call_id ? [x.tool_call_id] : [])]) + const one = ids(qoderBody({ messages: turns.slice(0, 4) }, m)) + const two = ids(qoderBody({ messages: turns }, m)) + expect(one).toHaveLength(2) + expect(one[0]).toMatch(/^call_[0-9a-f]{32}$/) + expect(two).toEqual(one) + // two like calls of one turn still differ + const twin = { type: "function", function: { name: "shell", arguments: "{}" } } + const both = ids(qoderBody({ messages: [{ role: "user", content: "x" }, { role: "assistant", content: "", tool_calls: [twin, twin] }] }, m)) + expect(new Set(both).size).toBe(2) +}) diff --git a/packages/qoder/checkin.test.mjs b/packages/qoder/checkin.test.mjs index 9631d9b..f350bf0 100644 --- a/packages/qoder/checkin.test.mjs +++ b/packages/qoder/checkin.test.mjs @@ -129,6 +129,17 @@ test("Windows finds the Qoder desktop's installed runtime", async () => { expect(await _internal.campaignRuntime({ home: sandbox, platform: "win32", arch: "x64", env: { LOCALAPPDATA: sandbox } })).toBe(file) }) +// accomplish (Discord, 10-21): international check-in failed while CN's +// needs no device identity at all. A machine-wide Qoder install on Windows +// keeps the runtime under Program Files, which wasn't searched. +test("Windows finds a machine-wide Qoder install's runtime", async () => { + const file = join(sandbox, "Program Files", "Qoder", "resources", "umid", "runtime-info.exe") + mkdirSync(dirname(file), { recursive: true }) + writeFileSync(file, "fixture") + const env = { LOCALAPPDATA: join(sandbox, "Local"), ProgramFiles: join(sandbox, "Program Files") } + expect(await inSandbox(() => _internal.campaignRuntime({ home: join(sandbox, "nobody"), platform: "win32", arch: "x64", env }))).toBe(file) +}) + // Keep desktop discovery inside the fixture, even on a Mac with Qoder installed. async function inSandbox(run) { const inspect = spyOn(fsPromises, "stat").mockImplementation((path, ...args) => { diff --git a/packages/qoder/cn.test.mjs b/packages/qoder/cn.test.mjs index a11ede6..0123e75 100644 --- a/packages/qoder/cn.test.mjs +++ b/packages/qoder/cn.test.mjs @@ -173,7 +173,8 @@ const chatServer = (machines, refresh) => (at, { req, body, url }) => { const q = JSON.parse(_internal.decodeBody(body)) expect(q.model_config.key).toBe("performance") expect(q.parameters.reasoning_effort).toBe("high") - expect(q.messages.at(-1)).toEqual({ role: "user", content: [{ type: "text", text: "你好" }] }) + const said = [{ type: "text", text: "你好", cache_control: { type: "ephemeral" } }] + expect(q.messages.at(-1)).toEqual({ role: "user", content: said, contents: said }) machines.push(req.headers.get("cosy-machineid")) return sse( { choices: [{ delta: { reasoning_content: "想想" } }] }, diff --git a/packages/qoder/index.mjs b/packages/qoder/index.mjs index 75284c6..6cf597c 100644 --- a/packages/qoder/index.mjs +++ b/packages/qoder/index.mjs @@ -705,6 +705,16 @@ const textOf = (c) => (typeof c === "string" ? c : blocksOf(c).filter((b) => b.t // went out with in order, which is the pairing a dropped id means. Only a // result with no call of its turn left to answer becomes text in the user's // own turn, so the words the tool returned stay with the conversation. +// callID is the id a call the client sent without one goes out with: the +// same on every turn of the conversation (#290), made from where the call +// is and what it says, so the history Qoder sees doesn't change under a +// prompt cache from one request to the next. +function callID(at, k, c) { + const args = c.function?.arguments + const said = typeof args === "string" ? args : JSON.stringify(args ?? {}) + return "call_" + createHash("sha256").update(`${at}\0${k}\0${c.function?.name ?? ""}\0${said}`).digest("hex").slice(0, 32) +} + function qoderMessages(msgs) { const out = [] const names = {} @@ -757,8 +767,8 @@ function qoderMessages(msgs) { if (m.role === "assistant" && m.tool_calls?.length) { // a call the client sent without an id gets one, so its result can // answer it instead of being turned into text - const calls = m.tool_calls.map((c) => { - const id = String(c.id ?? "").trim() || "call_" + hexID() + const calls = m.tool_calls.map((c, k) => { + const id = String(c.id ?? "").trim() || callID(i, k, c) names[id] = c.function?.name const args = c.function?.arguments return { id, type: "function", function: { name: c.function?.name, arguments: typeof args === "string" ? args : JSON.stringify(args ?? {}) } } @@ -804,6 +814,49 @@ function windowFor(chat, m, reply = 0) { return m.windows.find((w) => w >= need) ?? m.windows[m.windows.length - 1] } +// UNCACHED are the blocks Qoder's client never marks for the prompt cache. +const UNCACHED = new Set(["thinking", "redacted_thinking", "tool_use", "tool_result"]) + +// cacheMarked is the turns as Qoder's own client sends them for the prompt +// cache (#290): every user turn's blocks also as contents, and the last +// block of the last turn that isn't thinking or a tool's marked +// cache_control ephemeral. That is qoder-agent-sdk's (Qoder.app and +// qodercli 1.1.75 alike): the last message's last such block, and nothing +// when the last turn is a tool's. A model behind Qoder that caches only +// what is marked (Anthropic's) read none of a conversation without it, +// while one that caches on its own (Fast) did. The caller's turns are left +// as they were. +function cacheMarked(msgs) { + const out = msgs.map((m) => (m.role === "user" && Array.isArray(m.content) ? { ...m, contents: m.content } : m)) + const last = out.at(-1) + if (last?.role !== "user" || !Array.isArray(last.content)) return out + const at = last.content.findLastIndex((b) => b?.type && !UNCACHED.has(b.type)) + if (at < 0) return out + const blocks = last.content.slice() + blocks[at] = { ...blocks[at], cache_control: { type: "ephemeral" } } + out[out.length - 1] = { ...last, content: blocks, contents: blocks } + return out +} + +// sessionOf is the session_id a request goes out under: one for every turn +// of a conversation and another for each conversation, as Qoder's client +// sends its own session's (#290). Qoder can keep a conversation where its +// prompt is cached only when its turns say they are one. It is the +// client's prompt_cache_key (Codex's thread), else its user, else the +// conversation's opening (its system prompt and first user turn), hashed, +// so no key of the client's goes to Qoder as it is. +function sessionOf(chat) { + let seed = String(chat.prompt_cache_key ?? "").trim() ? "key\0" + chat.prompt_cache_key : String(chat.user ?? "").trim() ? "user\0" + chat.user : "" + if (!seed) { + const msgs = chat.messages ?? [] + const sys = msgs.filter((x) => x.role === "system" || x.role === "developer").map((x) => textOf(x.content)) + const first = msgs.find((x) => x.role === "user") + seed = "chat\0" + JSON.stringify([sys, first?.content ?? ""]) + } + const h = createHash("sha256").update(seed).digest("hex") + return `${h.slice(0, 8)}-${h.slice(8, 12)}-${h.slice(12, 16)}-${h.slice(16, 20)}-${h.slice(20, 32)}` +} + // qoderBody is the plaintext agent_chat_generation takes for a chat // completion request, for model m. function qoderBody(chat, m) { @@ -817,7 +870,12 @@ function qoderBody(chat, m) { if (effort) params.reasoning_effort = effort const window = windowFor(chat, m, params.max_tokens) if (window > 0) params.context_length = window + const request = randomUUID() const body = { + request_id: request, + request_set_id: request, + chat_record_id: request, + session_id: sessionOf(chat), parameters: params, business: { product: "app", version: COSY_VERSION, type: "agent", id: hexID(), name: "magpie session", begin_at: Date.now(), stage: "start" }, agent_id: "agent_common", @@ -825,7 +883,7 @@ function qoderBody(chat, m) { session_type: "app", model_config: m.config, system: [sys], - messages: [{ role: "system", content: [sys] }, ...qoderMessages(chat.messages ?? [])], + messages: [{ role: "system", content: [sys] }, ...cacheMarked(qoderMessages(chat.messages ?? []))], } if (tools.length) body.tools = tools.map((t) => ({ @@ -1312,7 +1370,8 @@ const CAMPAIGN_MACHINE_TTL = 60 * 60 * 1000 // Qoder's desktop client renews its // // Where each one keeps it (Qoder desktop 0.4.3's packages, qodercli 1.1.66): // - desktop: /resources/umid/runtime-info(.exe): Qoder.app on -// macOS, %LOCALAPPDATA%\Programs\Qoder (the user installer) on Windows, +// macOS, %LOCALAPPDATA%\Programs\Qoder (the user installer) or +// %ProgramFiles%\Qoder (a machine-wide install) on Windows, // /opt/Qoder (the .deb and the .rpm) on Linux; // - CLI: /.bin, where is QODER_CONFIG_DIR, else // QODER_CLI_HOME (or the home folder) joined with QODER_CONFIG_DIR_NAME @@ -1327,6 +1386,8 @@ async function campaignRuntime({ home = homedir(), platform = process.platform, if (platform === "win32") { const local = env.LOCALAPPDATA || join(home, "AppData", "Local") paths.push(join(local, "Programs", "Qoder", "resources", "umid", name)) + // the machine-wide installer, as VS Code's system setup places it + for (const dir of [env.ProgramFiles || env.PROGRAMFILES, env.ProgramW6432]) if (dir) paths.push(join(dir, "Qoder", "resources", "umid", name)) } else if (platform === "darwin") { for (const dir of ["/Applications", join(home, "Applications")]) paths.push(join(dir, "Qoder.app", "Contents", "Resources", "umid", name)) } else if (platform === "linux") { @@ -1993,4 +2054,4 @@ export const QoderAuthPlugin = makePlugin(SITES.qoder) export const QoderCNAuthPlugin = makePlugin(SITES["qoder-cn"]) // for tests -export const _internal = { campaignRuntime, campaignPage, campaignTarget, vpcInstance, vpcSite, parseUsage, gfmt, when, failure, modelInfo, qoderBody, qoderMessages, SITES, decodeBody, events } +export const _internal = { campaignRuntime, campaignPage, campaignTarget, vpcInstance, vpcSite, parseUsage, gfmt, when, failure, modelInfo, qoderBody, qoderMessages, cacheMarked, sessionOf, SITES, decodeBody, events } diff --git a/packages/qoder/package.json b/packages/qoder/package.json index 995d3e3..8c89638 100644 --- a/packages/qoder/package.json +++ b/packages/qoder/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-qoder-auth", - "version": "0.2.25", + "version": "0.2.26", "description": "Qoder subscriptions in OpenCode and magpie: Qoder (qoder.com) and Qoder CN (qoder.cn)", "type": "module", "main": "./index.mjs",