real-jacket (#330): one DSH task on qoder/dfmodel made 265 calls in 55 minutes, then Qoder answered 429 "Billing daily count exceeded". qoderBody made every call a task of its own: request_set_id = chat_record_id = request_id, a new business.id, begin_at = the call's own time and stage "start" on every call. What Qoder's client does, read (read-only) from the installed Qoder.app's qoder-agent-sdk worker (dist/_worker/qoder-worker-runtime.obf.mjs): - AgentLifecycle.create runs once per user prompt (driveQuery), with requestSetId = the prompt's requestSetId, else a preallocated one, else randomUUID(); business = {product, version, type, id: the prompt's requestSetId || randomUUID(), name, begin_at: Date.now(), stage: "init"}. - changeBusinessState() moves init -> "start" right after create, so the first call goes with "start", and start -> "processing" after each answered call (it runs after the error check of a model reply). - The chat body per call: request_id = chat_record_id = a new id, request_set_id = the lifecycle's requestSetId, business = the lifecycle's businessInfo. A retry renews request_id/chat_record_id only. The plugin now keeps a task per session and last user turn (taskOf): one UUID for request_set_id and business.id, one begin_at, stage "start" until a call of it is answered, "processing" after; request_id/chat_record_id stay new per call. A new user prompt is a new task. Tasks are an LRU map held to 512 entries, each dropped 2h after its last call. session_id: the plugin now has a chat.headers hook (as cursor and trae do) that passes the session magpie hands it (provider.ConversationHeader, which is DSH's x-deepseek-harness-session-id when DSH sends it, or another agent's own session header; OpenCode's session in OpenCode) to its fetch in x-magpie-qoder-session, which never goes to Qoder. session_id is its hash; magpie's "magpie-<hash>" fallback names no session and the old hash (prompt_cache_key, user, system + first user turn) stays the fallback, so a DSH system prompt changing mid-task (yetone/magpie#3298) no longer splits the session. The hook acts on its own site's provider only. Run: in packages/qoder, TMPDIR=$H HOME=$H/home bun test: 201 pass, 0 fail (task.test.mjs 6 new). Repo root, same sandbox: 2311 pass, 0 fail. bun scripts/check.mjs qoder ok. Broken by hand, task.test.mjs red each time: per-call request_set_id/business.id/begin_at (2 red), session_id ignoring the named session (2), stage always "start" (2), no LRU/TTL eviction (1), the hook unscoped to its provider (1); on origin/main's index.mjs all 6 are red. Not tried: a real Qoder account (none used) or a capture of the official client's wire bytes. Whether Qoder's "Billing daily count" counts by business.id / request_set_id is not known: the change makes the calls look as the official client's do, it does not prove the count drops. Not changed: Qoder's client also reports the end of its prompt to /api/v2/service/ask/finish and sends a shorter model_config; the plugin still doesn't.
161 lines
8.6 KiB
JavaScript
161 lines
8.6 KiB
JavaScript
// #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)
|
|
})
|