Files
yetone dc59000b1b qoder 0.2.26: mark the prompt for Qoder's cache as its client does (#290), and find a machine-wide Windows install's runtime-info
#290 (Codex side session on Ultimate read 0% from cache). Qoder's own
client (qoder-agent-sdk in Qoder.app 0.4.3, qodercli 1.1.75) sends, on
every agent_chat_generation request:
- cache_control {type: ephemeral} on the last non-thinking, non-tool
  block of the last message when it is a user turn (none when the last
  turn is tool results);
- each user turn's blocks as `contents` beside `content`;
- request_id / chat_record_id / request_set_id per request and a stable
  session_id per conversation.
The plugin sent none of that, and gave a tool call without an id a new
random id on each request, so the prefix changed every turn. Now:
- cacheMarked() marks as above, without touching the caller's turns;
- session_id is a UUID-shaped sha256 of prompt_cache_key (Codex's
  thread, which magpie forwards) or else of the system text and first
  user turn; the key itself never goes out;
- an id-less call's id is sha256(turn, place, name, arguments).

Check-in (accomplish on Discord, 10-21: Qoder intl can't check in, CN
can). Only the international site needs Qoder's runtime-info helper;
on Windows only %LOCALAPPDATA%\Programs\Qoder was searched. A
machine-wide install under %ProgramFiles%\Qoder is now found too. This
is one possible cause, not a confirmed one: the reporter's error text
and OS are unknown.

Run at this tree (rebased on 15d536a):
- HOME=$(mktemp -d) bun test in packages/qoder: 173 pass, 0 fail.
- Red on the old index.mjs: 3 of 5 cache.test.mjs tests (marker,
  session, stable call ids) and the new Program Files test.
- cn.test.mjs's exact-shape assertion updated to the marked last turn.

Not tried: a real Qoder account (no hit-rate measurement on Ultimate
or Fast), a real Windows machine-wide install, accomplish's account.
2026-10-22 02:07:03 +08:00

78 lines
4.0 KiB
JavaScript

// #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)
})