Zed's editor sends CompletionBody with thread_id (the agent thread's id, a UUID made once per thread) and prompt_id (a new UUID per message the user sends, kept through the tool calls answering it): zed crates/cloud_llm_client/src/cloud_llm_client.rs CompletionBody, crates/language_models_cloud (the Google path sends both), crates/agent/src/thread.rs advance_prompt_id. The plugin, and the retired built-in before it, sent neither, so to Zed's cloud every request was a thread of its own. Yezike-yyy's Gemini 4 Argon conversation read cache_read 0 on 11 requests of 180k-230k input tokens, where Zed's docs say Argon bills implicit cache hits. - a chat.headers hook names a Zed request's session in x-magpie-zed-session; the loader's fetch now keeps a Request's headers, and complete() reads the header and sends it no further. - thread_id is a UUID hashed (per-process salt) from the account and the session, else the first user message; prompt_id from the thread and the count of user messages with text (tool results don't count), on all four APIs (Messages, Responses, chat completions, Gemini). - usage mapping was checked and is right: magpie reads Gemini's cachedContentTokenCount through the plugin's passthrough. Run (mote, bun 1.4.2): packages/zed `bun test` 78 pass 0 fail; `bun run check` at the repo root, exit 0. Broken by hand: the new thread-id.test.mjs against the old index.mjs, 7 of 7 red; the loader dropping a Request's headers, its test red; tool-result turns counted as prompts, the Gemini test red. Not tried: no Zed account, so no real request; whether Zed's cloud uses thread_id or prompt_id for Gemini's implicit cache is not known (its server is closed source). The reporter is asked to retest.
159 lines
8.4 KiB
JavaScript
159 lines
8.4 KiB
JavaScript
// Zed's editor names each completion's thread and prompt in CompletionBody:
|
|
// thread_id once per agent thread, prompt_id again for each message the
|
|
// user sends (zed crates/cloud_llm_client CompletionBody; crates/agent
|
|
// thread.rs). The plugin sent neither, so every request was a thread of its
|
|
// own to Zed's cloud, and a Gemini 4 Argon conversation read no cache on 11
|
|
// requests of 180k to 230k tokens (plugins#731).
|
|
import { afterEach, expect, test } from "bun:test"
|
|
import { ZedAuthPlugin, _internal } from "./index.mjs"
|
|
|
|
const real = globalThis.fetch
|
|
afterEach(() => {
|
|
globalThis.fetch = real
|
|
_internal.tokens.clear()
|
|
})
|
|
|
|
const s = { userId: "4242", access: "plain-access", systemId: "", org: "org-me", plan: "zed_pro", who: "octo" }
|
|
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/
|
|
|
|
// sent is the CompletionBody the plugin hands Zed for a request to path.
|
|
async function sent(path, body, headers, who = s) {
|
|
let got
|
|
globalThis.fetch = async (url, init) => {
|
|
const p = new URL(String(url)).pathname
|
|
if (p === "/client/llm_tokens") return Response.json({ token: "llm-token" })
|
|
if (p === "/completions") {
|
|
got = JSON.parse(init.body)
|
|
return new Response(JSON.stringify({ status: "stream_ended" }) + "\n", { headers: { "x-zed-server-supports-status-messages": "true" } })
|
|
}
|
|
return new Response("", { status: 404 })
|
|
}
|
|
const res = await _internal.complete(who, "https://cloud.zed.dev" + path, { method: "POST", headers, body: JSON.stringify(body) })
|
|
await res.text().catch(() => {})
|
|
return got
|
|
}
|
|
|
|
const ARGON = "/v1beta/models/gemini-4-argon:streamGenerateContent"
|
|
const call = { functionCall: { name: "read", args: { path: "a.go" } } }
|
|
const answer = { functionResponse: { name: "read", response: { content: "package a" } } }
|
|
const turns = {
|
|
first: [{ role: "user", parts: [{ text: "read a.go" }] }],
|
|
toolRound: [{ role: "user", parts: [{ text: "read a.go" }] }, { role: "model", parts: [call] }, { role: "user", parts: [answer] }],
|
|
next: [{ role: "user", parts: [{ text: "read a.go" }] }, { role: "model", parts: [call] }, { role: "user", parts: [answer] }, { role: "model", parts: [{ text: "done" }] }, { role: "user", parts: [{ text: "now b.go" }] }],
|
|
}
|
|
const gemini = (contents) => ({ contents, systemInstruction: { parts: [{ text: "You are a coding agent." }] } })
|
|
|
|
test("a Gemini conversation keeps one thread_id, and a new prompt_id only for a message the user sends", async () => {
|
|
const h = { [_internal.SESSION]: "ses_abc" }
|
|
const a = await sent(ARGON, gemini(turns.first), h)
|
|
const b = await sent(ARGON, gemini(turns.toolRound), h)
|
|
const c = await sent(ARGON, gemini(turns.next), h)
|
|
expect(a.thread_id).toMatch(UUID)
|
|
expect(a.prompt_id).toMatch(UUID)
|
|
expect(b.thread_id).toBe(a.thread_id)
|
|
expect(c.thread_id).toBe(a.thread_id)
|
|
// the tool call's answer is the same prompt, as in Zed's editor
|
|
expect(b.prompt_id).toBe(a.prompt_id)
|
|
expect(c.prompt_id).not.toBe(a.prompt_id)
|
|
expect(c.provider).toBe("google")
|
|
expect(c.model).toBe("gemini-4-argon")
|
|
})
|
|
|
|
test("the session header goes no further than the plugin, and names nothing it was made from", async () => {
|
|
const got = await sent(ARGON, gemini(turns.first), { [_internal.SESSION]: "ses_abc" })
|
|
const text = JSON.stringify(got)
|
|
expect(text).not.toContain("ses_abc")
|
|
expect(text).not.toContain(_internal.SESSION)
|
|
expect(text).not.toContain("4242")
|
|
})
|
|
|
|
test("two sessions, or two accounts, are two threads", async () => {
|
|
const a = await sent(ARGON, gemini(turns.first), { [_internal.SESSION]: "ses_abc" })
|
|
const b = await sent(ARGON, gemini(turns.first), { [_internal.SESSION]: "ses_xyz" })
|
|
const c = await sent(ARGON, gemini(turns.first), { [_internal.SESSION]: "ses_abc" }, { ...s, userId: "777" })
|
|
expect(b.thread_id).not.toBe(a.thread_id)
|
|
expect(c.thread_id).not.toBe(a.thread_id)
|
|
})
|
|
|
|
test("with no session, the first message the user sent names the thread", async () => {
|
|
const a = await sent(ARGON, gemini(turns.first))
|
|
const c = await sent(ARGON, gemini(turns.next))
|
|
expect(a.thread_id).toMatch(UUID)
|
|
expect(c.thread_id).toBe(a.thread_id)
|
|
expect(c.prompt_id).not.toBe(a.prompt_id)
|
|
})
|
|
|
|
test("every API's tool round shares its prompt, and the next message gets a new one", async () => {
|
|
const anthropic = (m) => ({ model: "claude-sonnet-5", max_tokens: 100, stream: true, messages: m })
|
|
const chat = (m) => ({ model: "gpt-5", stream: true, messages: m })
|
|
const responses = (m) => ({ model: "gpt-5", stream: true, input: m })
|
|
const cases = [
|
|
[
|
|
"/v1/messages",
|
|
anthropic([{ role: "user", content: "read a.go" }]),
|
|
anthropic([{ role: "user", content: "read a.go" }, { role: "assistant", content: [{ type: "tool_use", id: "t1", name: "Read", input: {} }] }, { role: "user", content: [{ type: "tool_result", tool_use_id: "t1", content: [{ type: "text", text: "package a" }] }] }]),
|
|
anthropic([{ role: "user", content: "read a.go" }, { role: "assistant", content: "ok" }, { role: "user", content: [{ type: "text", text: "now b.go" }] }]),
|
|
],
|
|
[
|
|
"/v1/chat/completions",
|
|
chat([{ role: "user", content: "read a.go" }]),
|
|
chat([{ role: "user", content: "read a.go" }, { role: "assistant", tool_calls: [{ id: "t1", type: "function", function: { name: "read", arguments: "{}" } }] }, { role: "tool", tool_call_id: "t1", content: "package a" }]),
|
|
chat([{ role: "user", content: "read a.go" }, { role: "assistant", content: "ok" }, { role: "user", content: [{ type: "text", text: "now b.go" }] }]),
|
|
],
|
|
[
|
|
"/v1/responses",
|
|
responses([{ role: "user", content: [{ type: "input_text", text: "read a.go" }] }]),
|
|
responses([{ role: "user", content: [{ type: "input_text", text: "read a.go" }] }, { type: "function_call", call_id: "t1", name: "read", arguments: "{}" }, { type: "function_call_output", call_id: "t1", output: "package a" }]),
|
|
responses([{ role: "user", content: [{ type: "input_text", text: "read a.go" }] }, { role: "assistant", content: [{ type: "output_text", text: "ok" }] }, { role: "user", content: [{ type: "input_text", text: "now b.go" }] }]),
|
|
],
|
|
]
|
|
for (const [path, first, round, next] of cases) {
|
|
const h = { [_internal.SESSION]: "ses_" + path }
|
|
const a = await sent(path, first, h)
|
|
const b = await sent(path, round, h)
|
|
const c = await sent(path, next, h)
|
|
expect([path, a.thread_id]).toEqual([path, expect.stringMatching(UUID)])
|
|
expect([path, b.thread_id, c.thread_id]).toEqual([path, a.thread_id, a.thread_id])
|
|
expect([path, b.prompt_id]).toEqual([path, a.prompt_id])
|
|
expect([path, c.prompt_id === a.prompt_id]).toEqual([path, false])
|
|
}
|
|
})
|
|
|
|
test("chat.headers names Zed's requests' session, and no other provider's", async () => {
|
|
const p = await ZedAuthPlugin({})
|
|
const out = { headers: {} }
|
|
await p["chat.headers"]({ sessionID: "ses_abc", model: { providerID: "zed", id: "gemini-4-argon" } }, out)
|
|
expect(out.headers[_internal.SESSION]).toBe("ses_abc")
|
|
const other = { headers: {} }
|
|
await p["chat.headers"]({ sessionID: "ses_abc", model: { providerID: "anthropic", id: "claude-sonnet-5" } }, other)
|
|
expect(other.headers).toEqual({})
|
|
})
|
|
|
|
test("the loader's fetch keeps a Request's headers, the session among them", async () => {
|
|
let got
|
|
globalThis.fetch = async (url, init) => {
|
|
const p = new URL(String(url)).pathname
|
|
if (p === "/client/llm_tokens") return Response.json({ token: "llm-token" })
|
|
if (p === "/completions") {
|
|
got = JSON.parse(init.body)
|
|
return new Response(JSON.stringify({ status: "stream_ended" }) + "\n", { headers: { "x-zed-server-supports-status-messages": "true" } })
|
|
}
|
|
return new Response("", { status: 404 })
|
|
}
|
|
const auth = { type: "oauth", refresh: JSON.stringify({ userId: "4242", access: "plain-access" }), access: "plain-access", expires: Date.now() + 3600e3 }
|
|
const p = await ZedAuthPlugin({})
|
|
const sdk = await p.auth.loader(async () => auth)
|
|
expect(typeof sdk.fetch).toBe("function")
|
|
const one = async (contents) => {
|
|
const req = new Request("https://cloud.zed.dev" + ARGON, { method: "POST", headers: { [_internal.SESSION]: "ses_abc" }, body: JSON.stringify(gemini(contents)) })
|
|
const res = await sdk.fetch(req)
|
|
await res.text().catch(() => {})
|
|
return got
|
|
}
|
|
const a = await one(turns.first)
|
|
const b = await one([{ role: "user", parts: [{ text: "something else entirely" }] }])
|
|
// the same session, though the first message differs: the header was read
|
|
expect(a.thread_id).toMatch(UUID)
|
|
expect(b.thread_id).toBe(a.thread_id)
|
|
})
|