diff --git a/packages/qoder/README.md b/packages/qoder/README.md index 75da839..25698af 100644 --- a/packages/qoder/README.md +++ b/packages/qoder/README.md @@ -98,10 +98,20 @@ itself. `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 session the agent names (since 0.2.31, #330: DSH's + `x-deepseek-harness-session-id` and the other agents' own session + headers, which magpie hands the plugin's `chat.headers` hook, and + OpenCode's session), else 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` and `chat_record_id`. +- Every call that answers one user prompt goes out as one task, as Qoder's + own client sends it (since 0.2.31, #330): one `request_set_id`, which is + also `business.id`, and one `business.begin_at` for every call of the + tool loop, `business.stage` "start" until a call is answered and + "processing" after. A new user prompt in the session is a new task. The + plugin keeps at most 512 tasks, each for 2 hours after its last call. + Before 0.2.31 each call was a task of its own. - 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. diff --git a/packages/qoder/index.mjs b/packages/qoder/index.mjs index e94978d..e81e69d 100644 --- a/packages/qoder/index.mjs +++ b/packages/qoder/index.mjs @@ -841,12 +841,16 @@ function cacheMarked(msgs) { // 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 +// prompt is cached only when its turns say they are one. It is the session +// the agent named (named: DSH's x-deepseek-harness-session-id, OpenCode's +// session, as the chat.headers hook is told it, #330), else 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 : "" +// so no key of the client's goes to Qoder as it is. DSH changes its system +// prompt mid-task (yetone/magpie#3298), so its own session is the one that +// holds for the whole task. +function sessionOf(chat, named = "") { + let seed = named ? "session\0" + named : 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)) @@ -857,9 +861,62 @@ function sessionOf(chat) { return `${h.slice(0, 8)}-${h.slice(8, 12)}-${h.slice(12, 16)}-${h.slice(16, 20)}-${h.slice(20, 32)}` } +// SESSION carries the session magpie (or OpenCode) names a request's +// conversation by, from the chat.headers hook to the loader's fetch. It +// goes no further: nothing of the request's headers is sent to Qoder. +const SESSION = "x-magpie-qoder-session" + +// namedSession is the session a chat.headers hook is told, "" when it +// names none. magpie, with no session header from the agent, makes one of +// the conversation's first user message ("magpie-" and 24 hex digits): +// that names no session, and sessionOf's own hash is taken instead. +function namedSession(input) { + const s = String(input?.sessionID ?? "").trim() + if (!s || /^magpie-[0-9a-f]{24}$/.test(s)) return "" + return s.slice(0, 128) +} + +// A task is what Qoder's client sends under one request_set_id and one +// business record (#330): every model call that answers one user prompt, +// the tool loop's turns included. qoder-agent-sdk (Qoder.app's +// qoder-worker-runtime, AgentLifecycle) makes the record once per prompt +// (business.id the prompt's requestSetId, begin_at when the prompt +// started, stage "start"), keeps it for each call of the loop, and moves +// stage to "processing" once a call is answered; only request_id and +// chat_record_id are new on every call. The plugin made a new record for +// every call, so a 265-call DSH task was 265 tasks to Qoder. A task here is +// a session's last user turn: the tool loop repeats it, and a new prompt +// is a new one. TASKS holds at most TASK_MAX of them, each for TASK_IDLE +// after its last call, so a long-running magpie doesn't grow with them. +const TASKS = new Map() +const TASK_MAX = 512 +const TASK_IDLE = 2 * 60 * 60 * 1000 + +// taskOf is the task chat's call belongs to, in session sid: made on its +// first call, the same one on every call after. +function taskOf(sid, chat, now = Date.now()) { + const msgs = chat.messages ?? [] + const at = msgs.findLastIndex((x) => x?.role === "user") + const key = createHash("sha256").update(JSON.stringify([sid, at, at < 0 ? null : msgs[at].content ?? null])).digest("hex") + for (const [k, t] of TASKS) { + if (now - t.used <= TASK_IDLE) break + TASKS.delete(k) + } + let t = TASKS.get(key) + if (t && now - t.used > TASK_IDLE) t = undefined + TASKS.delete(key) + t ??= { id: randomUUID(), begin: now, answered: false } + t.used = now + TASKS.set(key, t) + while (TASKS.size > TASK_MAX) TASKS.delete(TASKS.keys().next().value) + return t +} + // qoderBody is the plaintext agent_chat_generation takes for a chat -// completion request, for model m. -function qoderBody(chat, m) { +// completion request, for model m, in the session the agent named (named, +// "" when none) and the task it is a call of (taskOf's, a new one when +// none is given). +function qoderBody(chat, m, named = "", task) { const system = (chat.messages ?? []).filter((x) => x.role === "system" || x.role === "developer").map((x) => textOf(x.content)).filter(Boolean).join("\n\n") const tools = chat.tool_choice !== "none" ? (chat.tools ?? []).filter((t) => t?.type === "function" && t.function?.name) : [] let sysText = system ? QODER_SYS + "\n\n" + system : QODER_SYS @@ -871,13 +928,15 @@ function qoderBody(chat, m) { const window = windowFor(chat, m, params.max_tokens) if (window > 0) params.context_length = window const request = randomUUID() + const session = sessionOf(chat, named) + task ??= taskOf(session, chat) const body = { request_id: request, - request_set_id: request, + request_set_id: task.id, chat_record_id: request, - session_id: sessionOf(chat), + session_id: session, parameters: params, - business: { product: "app", version: COSY_VERSION, type: "agent", id: hexID(), name: "magpie session", begin_at: Date.now(), stage: "start" }, + business: { product: "app", version: COSY_VERSION, type: "agent", id: task.id, name: "magpie session", begin_at: task.begin, stage: task.answered ? "processing" : "start" }, agent_id: "agent_common", task_id: "common", session_type: "app", @@ -2069,7 +2128,7 @@ const makePlugin = (site) => async ({ client }) => { }) // ask is the answer to a chat completion on the live account cred. - const ask = async (chat, cred, init) => { + const ask = async (chat, cred, init, named = "") => { let m try { m = (await models(cred)).find((x) => x.key === chat.model) ?? (await models(cred, true)).find((x) => x.key === chat.model) @@ -2080,7 +2139,8 @@ const makePlugin = (site) => async ({ client }) => { if (!m) return errorResponse({ status: 400, message: `unknown or disabled model "${chat.model}"` }) const api = siteOf(site, cred).api const CHAT_URL = api + CHAT_PATH - const body = qoderBody(chat, m) + const task = taskOf(sessionOf(chat, named), chat) + const body = qoderBody(chat, m, named, task) const wire = encodeBody(JSON.stringify(body)) // Qoder's queue is waited out (queueWait) and the same request, its // request_set_id the one the queue knows, sent again @@ -2102,6 +2162,8 @@ const makePlugin = (site) => async ({ client }) => { return errorResponse({ status: 502, message: e.message }) } const f = !res.ok ? failure(res.status, (await res.text()).slice(0, 1 << 20)) : open.first.value?.error + // an answered call moves its task on, as Qoder's client moves it + if (!f) task.answered = true if (!f?.queue || waited >= QUEUE.recoveries) return f && !res.ok ? errorResponse(f) : answer(open, chat) try { if (!(await queueWait(api, cred, ask, f.queue, init.signal, deadline))) return errorResponse(f) @@ -2112,6 +2174,16 @@ const makePlugin = (site) => async ({ client }) => { } return { + // the session the request is part of, for session_id and its task + // (#330): magpie names it here (DSH's x-deepseek-harness-session-id + // among the agents' own), as OpenCode does, and the loader's fetch + // reads it back. Every plugin's hook sees every provider's requests, + // so it is put on this site's alone. + async "chat.headers"(input, output) { + if (input?.model?.providerID !== ID && input?.provider?.info?.id !== ID) return + const s = namedSession(input) + if (s) output.headers[SESSION] = s + }, auth: { provider: ID, // magpie renews the chat token RENEW_LEAD before its end, once for @@ -2164,7 +2236,9 @@ const makePlugin = (site) => async ({ client }) => { } catch (e) { return signedInError(e) } - return signed(await ask(chat, cred, init), renewed(cred)) + const h = new Headers(input instanceof Request ? input.headers : undefined) + new Headers(init.headers ?? {}).forEach((v, k) => h.set(k, v)) + return signed(await ask(chat, cred, init, h.get(SESSION)?.trim().slice(0, 128) ?? ""), renewed(cred)) }, } }, @@ -2274,4 +2348,4 @@ export const QoderAuthPlugin = makePlugin(SITES.qoder) export const QoderCNAuthPlugin = makePlugin(SITES["qoder-cn"]) // for tests -export const _internal = { campaignRuntime, RUNTIMES, campaignPage, campaignTarget, vpcInstance, vpcSite, parseUsage, gfmt, when, failure, modelInfo, qoderBody, qoderMessages, cacheMarked, sessionOf, SITES, decodeBody, events, QUEUE, queueState } +export const _internal = { campaignRuntime, RUNTIMES, campaignPage, campaignTarget, vpcInstance, vpcSite, parseUsage, gfmt, when, failure, modelInfo, qoderBody, qoderMessages, cacheMarked, sessionOf, namedSession, taskOf, TASKS, TASK_MAX, TASK_IDLE, SESSION, SITES, decodeBody, events, QUEUE, queueState } diff --git a/packages/qoder/package.json b/packages/qoder/package.json index ef68d95..8f10661 100644 --- a/packages/qoder/package.json +++ b/packages/qoder/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-qoder-auth", - "version": "0.2.30", + "version": "0.2.31", "description": "Qoder subscriptions in OpenCode and magpie: Qoder (qoder.com) and Qoder CN (qoder.cn)", "type": "module", "main": "./index.mjs", diff --git a/packages/qoder/task.test.mjs b/packages/qoder/task.test.mjs new file mode 100644 index 0000000..276b1c5 --- /dev/null +++ b/packages/qoder/task.test.mjs @@ -0,0 +1,160 @@ +// #330 (real-jacket): one DSH task on Qoder's dfmodel made 265 calls in 55 +// minutes, and Qoder then answered "Billing daily count exceeded". Each call +// went out as a task of its own: a new request_set_id and business.id, and +// begin_at the call's own time with stage "start". Qoder's own client +// (qoder-agent-sdk in Qoder.app, qoder-worker-runtime's AgentLifecycle) +// makes that record once for a user prompt and keeps it for every call of +// its tool loop, moving stage from "start" to "processing" once a call is +// answered; only request_id and chat_record_id are new on each call. DSH's +// own session (x-deepseek-harness-session-id, which magpie hands a plugin's +// chat.headers hook as its session) names session_id, so a system prompt +// DSH changes mid-task no longer splits it (yetone/magpie#3298). +import { afterEach, beforeEach, expect, test } from "bun:test" +import { QoderAuthPlugin, QoderCNAuthPlugin, _internal } from "./index.mjs" + +const { qoderBody, sessionOf, taskOf, TASKS, TASK_MAX, TASK_IDLE, SESSION } = _internal +const m = { key: "dfmodel", config: { key: "dfmodel" }, context: 128_000 } +const real = globalThis.fetch +beforeEach(() => TASKS.clear()) +afterEach(() => { + globalThis.fetch = real + TASKS.clear() +}) + +const API_CHAT = "https://api3.qoder.sh/v1/chat/completions" +const LISTING = { chat: [{ key: "dfmodel", display_name: "DeepSeek Flash", enable: true }] } +const json = (v, status = 200) => new Response(JSON.stringify(v), { status, headers: { "Content-Type": "application/json" } }) +const line = (body) => `data:${JSON.stringify({ headers: {}, body: typeof body === "string" ? body : JSON.stringify(body), statusCodeValue: 200 })}\n\n` +const answered = () => + new Response(line({ choices: [{ delta: { content: "ok" } }] }) + line({ choices: [{ delta: {}, finish_reason: "stop" }] }) + line("[DONE]"), { + headers: { "Content-Type": "text/event-stream" }, + }) + +// the plugin as magpie's host runs it: the chat.headers hook is told the +// session, and what it adds goes to the loader's fetch with the request +async function plugin(make = QoderAuthPlugin, id = "qoder") { + const auth = { type: "oauth", access: "jt", refresh: "rt", expires: Date.now() + 3_600_000, accountId: "one@x", uid: "u1", machineId: "m1" } + const sent = [] + globalThis.fetch = async (url, init = {}) => { + const u = new URL(String(url)) + if (u.pathname.endsWith("/model/list")) return json(LISTING) + sent.push({ body: JSON.parse(_internal.decodeBody(init.body)), headers: init.headers }) + return answered() + } + const hooks = await make({ client: { auth: { set: async () => {} } } }) + const l = await hooks.auth.loader(async () => auth) + const call = async (messages, session) => { + const out = { headers: {} } + await hooks["chat.headers"]({ sessionID: session ?? "", model: { providerID: id, id: "dfmodel" } }, out) + const res = await l.fetch(API_CHAT, { method: "POST", headers: new Headers(out.headers), body: JSON.stringify({ model: "dfmodel", messages }) }) + expect(res.status).toBe(200) + await res.text() + return sent.at(-1).body + } + return { call, sent, hooks } +} + +const sys = (s) => ({ role: "system", content: s }) +const user = (s) => ({ role: "user", content: s }) +const called = (id) => ({ role: "assistant", content: "", tool_calls: [{ id, type: "function", function: { name: "bash", arguments: '{"cmd":"ls"}' } }] }) +const result = (id) => ({ role: "tool", tool_call_id: id, content: "a.txt" }) + +test("every call of one task's tool loop goes out under one task, as Qoder's client sends it", async () => { + const { call } = await plugin() + const s = "dsh-session-1" + const one = [sys("You are DSH."), user("fix the bug")] + const a = await call(one, s) + const b = await call([...one, called("c1"), result("c1")], s) + const c = await call([...one, called("c1"), result("c1"), called("c2"), result("c2")], s) + for (const x of [b, c]) { + expect(x.request_set_id).toBe(a.request_set_id) + expect(x.business.id).toBe(a.business.id) + expect(x.business.begin_at).toBe(a.business.begin_at) + expect(x.session_id).toBe(a.session_id) + } + // the task's record is the prompt's: business.id is its request_set_id + expect(a.business.id).toBe(a.request_set_id) + expect(a.business.id).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/) + // the first call starts it; once a call is answered it is processing + expect([a, b, c].map((x) => x.business.stage)).toEqual(["start", "processing", "processing"]) + // each call is its own request + expect(new Set([a, b, c].map((x) => x.request_id)).size).toBe(3) + for (const x of [a, b, c]) expect(x.chat_record_id).toBe(x.request_id) +}) + +test("a new prompt in the session is a new task, and the session stays one", async () => { + const { call } = await plugin() + const s = "dsh-session-2" + const one = [sys("You are DSH."), user("fix the bug")] + const a = await call(one, s) + const b = await call([...one, { role: "assistant", content: "done" }, user("now add a test")], s) + expect(b.request_set_id).not.toBe(a.request_set_id) + expect(b.business.id).not.toBe(a.business.id) + expect(b.business.stage).toBe("start") + expect(b.session_id).toBe(a.session_id) + // the same words in another session are another task + const c = await call(one, "dsh-session-3") + expect(c.request_set_id).not.toBe(a.request_set_id) + expect(c.session_id).not.toBe(a.session_id) +}) + +test("the session the agent named is the session_id, through a system prompt that changes mid-task", async () => { + const { call, sent } = await plugin() + const s = "dsh-session-4" + const a = await call([sys("You are DSH. tools: a"), user("fix the bug")], s) + const b = await call([sys("You are DSH. tools: a, b"), user("fix the bug"), called("c1"), result("c1")], s) + expect(b.session_id).toBe(a.session_id) + expect(b.request_set_id).toBe(a.request_set_id) + expect(a.session_id).toBe(sessionOf({ messages: [] }, s)) + // without one, the conversation's opening names it, as before + const c = await call([sys("You are DSH. tools: a"), user("fix the bug")]) + expect(c.session_id).toBe(sessionOf({ messages: [sys("You are DSH. tools: a"), user("fix the bug")] })) + expect(c.session_id).not.toBe(a.session_id) + // the session goes to Qoder hashed, and the header never goes at all + for (const x of sent) { + expect(JSON.stringify(x.body)).not.toContain("dsh-session") + expect(Object.keys(x.headers).map((k) => k.toLowerCase())).not.toContain(SESSION) + } +}) + +test("magpie's own hash of the first message names no session, and another provider's request gets no header", async () => { + const { hooks } = await plugin() + const out = { headers: {} } + await hooks["chat.headers"]({ sessionID: "magpie-0123456789abcdef01234567", model: { providerID: "qoder" } }, out) + expect(out.headers).toEqual({}) + await hooks["chat.headers"]({ sessionID: "dsh-x", model: { providerID: "cursor" } }, out) + expect(out.headers).toEqual({}) + await hooks["chat.headers"]({ sessionID: "dsh-x", model: { providerID: "qoder" } }, out) + expect(out.headers).toEqual({ [SESSION]: "dsh-x" }) + // Qoder CN's hook names its own provider's requests + const cn = await plugin(QoderCNAuthPlugin, "qoder-cn") + const o2 = { headers: {} } + await cn.hooks["chat.headers"]({ sessionID: "dsh-x", model: { providerID: "qoder" } }, o2) + expect(o2.headers).toEqual({}) + await cn.hooks["chat.headers"]({ sessionID: "dsh-x", model: { providerID: "qoder-cn" } }, o2) + expect(o2.headers).toEqual({ [SESSION]: "dsh-x" }) +}) + +test("a failed call leaves its task at start", () => { + const msgs = [user("hi")] + const t = taskOf(sessionOf({ messages: msgs }), { messages: msgs }) + expect(qoderBody({ messages: msgs }, m, "", t).business.stage).toBe("start") + expect(qoderBody({ messages: msgs }, m).business.stage).toBe("start") + t.answered = true + expect(qoderBody({ messages: msgs }, m).business.stage).toBe("processing") +}) + +test("the tasks kept are bounded in number and in time", () => { + const now = 1_000_000_000 + const first = taskOf("s", { messages: [user("task 0")] }, now) + for (let i = 1; i <= TASK_MAX + 50; i++) taskOf("s", { messages: [user("task " + i)] }, now + i) + expect(TASKS.size).toBe(TASK_MAX) + // the oldest went first: its prompt is a new task now + expect(taskOf("s", { messages: [user("task 0")] }, now + TASK_MAX + 60).id).not.toBe(first.id) + // one used within TASK_IDLE stays, past it is a new task and the idle ones go + const late = now + TASK_MAX + 100 + const kept = taskOf("s", { messages: [user("long task")] }, late) + expect(taskOf("s", { messages: [user("long task")] }, late + TASK_IDLE - 1).id).toBe(kept.id) + expect(taskOf("s", { messages: [user("long task")] }, late + 2 * TASK_IDLE).id).not.toBe(kept.id) + expect(TASKS.size).toBe(1) +})