zcode: name the conversation in x-session-id as a bare UUID, so BigModel's prompt cache hits (#388)
ZCode's client names every request's conversation in x-session-id: a bare UUID, the same on each turn (3.14.3 xst/Tst). BigModel keeps a prompt's cache for the requests that name the same session. The plugin sent none, so a long conversation seldom hit the cache (#387). Every zcode request, on the Start Plan and on Coding Plan or team seats alike, now carries it: - the agent's session (chat.headers' sessionID, which magpie fills) goes as it is when it is a UUID, lower-cased; - any other id (magpie-<hex>, ses_..., sess_...) goes as a stable UUID made from its sha256; - with no session, a UUID made from the device. metadata.user_id's session_id is the same UUID, in dress() and marked(). Reviewed at 48013c0, which addresses all four points of the earlier review (UUID shape, session_id in user_id, rebase onto 0.1.30, no "Closes #387"): - TMPDIR=<scratch> HOME=$(mktemp -d) bun test in packages/zcode: 125 pass, 0 fail. bun scripts/check.mjs zcode passes. Merges cleanly onto main. - With main's index.mjs and the PR's tests: 17 fail (all 7 new session tests, plus the start, userid and dual assertions on the header). - Probe through the plugin's chat.headers and loader fetch, as magpie's host calls them (host.js passes sessionID and sets out.headers before the fetch): magpie-<hex> twice gives the same UUID in the header and in user_id.session_id, and ses_other gives a different UUID. None contains magpie, ses_ or sess_. Not tried: a real BigModel request (the author's hit-rate numbers are the evidence: 3/12 before, 24/24 after through magpie).
This commit is contained in:
@@ -119,7 +119,8 @@ const reminder = {
|
||||
},
|
||||
],
|
||||
}
|
||||
const userID = JSON.stringify({ device_id: DEVICE, account_uuid: "", session_id: "" })
|
||||
// the device, and the conversation as x-session-id names it; the agent named none
|
||||
const userID = () => JSON.stringify({ device_id: DEVICE, account_uuid: "", session_id: _internal.sessionOf("", DEVICE) })
|
||||
|
||||
// a request as OpenCode's @ai-sdk/anthropic makes one, cache marks and all
|
||||
const agentBody = {
|
||||
@@ -215,6 +216,8 @@ test("a Start Plan request carries ZCode's headers, and only those", async () =>
|
||||
"x-zcode-session-type": "main",
|
||||
"x-zcode-trace-id": h["x-zcode-trace-id"],
|
||||
"x-zcode-query-source": "main_turn",
|
||||
// the conversation, as ZCode names it; the agent told none (#387)
|
||||
"x-session-id": _internal.sessionOf("", DEVICE),
|
||||
})
|
||||
expect(h["x-api-key"]).toBeUndefined()
|
||||
expect(h["x-device-mid"]).toBeUndefined()
|
||||
@@ -250,7 +253,7 @@ test("a Start Plan request from Claude Code goes with ZCode's headers alone, wit
|
||||
const h = own(r.headers)
|
||||
for (const k of Object.keys(CLAUDE_CODE)) if (!TRANSPORT.has(k)) expect(h[k]).toBeUndefined()
|
||||
expect(Object.keys(h).sort()).toEqual(
|
||||
["authorization", "anthropic-version", "content-type", "http-referer", "user-agent", "x-zcode-app-version", "x-title", "x-release-channel", "x-client-language", "x-client-timezone", "x-zcode-agent", "x-platform", "x-os-category", "x-os-version", "x-request-id", "x-zcode-session-type", "x-zcode-trace-id", "x-zcode-query-source"].sort(),
|
||||
["authorization", "anthropic-version", "content-type", "http-referer", "user-agent", "x-zcode-app-version", "x-title", "x-release-channel", "x-client-language", "x-client-timezone", "x-zcode-agent", "x-platform", "x-os-category", "x-os-version", "x-request-id", "x-zcode-session-type", "x-zcode-trace-id", "x-zcode-query-source", "x-session-id"].sort(),
|
||||
)
|
||||
expect(r.headers.accept).not.toBe(CLAUDE_CODE.accept)
|
||||
}
|
||||
@@ -277,7 +280,7 @@ test("a Start Plan request's body is dressed as ZCode's", async () => {
|
||||
{ role: "user", content: [{ type: "tool_result", tool_use_id: "t1", content: "x" }, { type: "text", text: "and now?", cache_control: eph }] },
|
||||
],
|
||||
tools: [{ name: "read", description: "Read a file", input_schema: { type: "object" } }],
|
||||
metadata: { other: "kept", user_id: userID },
|
||||
metadata: { other: "kept", user_id: userID() },
|
||||
stream: true,
|
||||
})
|
||||
// the blocks' texts, by the plugin's hashes
|
||||
@@ -287,7 +290,7 @@ test("a Start Plan request's body is dressed as ZCode's", async () => {
|
||||
expect(dyn.startsWith("\n\n" + PROMPT.beforeEnvironment + "\n\n# Environment\n")).toBe(true)
|
||||
expect(dyn.endsWith("\n\n" + PROMPT.afterEnvironment + "\n\nYou are opencode, an agent.\n\nProject instructions.")).toBe(true)
|
||||
expect(sha(dyn.slice(2, 2 + 3065))).toBe("bbdc66b399d297ff")
|
||||
expect(JSON.parse(b.metadata.user_id)).toEqual({ device_id: DEVICE, account_uuid: "", session_id: "" })
|
||||
expect(b.metadata.user_id).toBe(userID())
|
||||
// exactly one cache mark in the turns, and none on the tools
|
||||
expect((r.body.match(/cache_control/g) ?? []).length).toBe(4)
|
||||
})
|
||||
@@ -301,7 +304,7 @@ test("a BigModel sign-in's Start Plan names bigmodel-api; a string turn takes th
|
||||
max_tokens: 100,
|
||||
system: zcodeSystem("bigmodel-api/GLM-5.2", undefined, [" Be brief. "]),
|
||||
messages: [reminder, { role: "user", content: [{ type: "text", text: "hello", cache_control: eph }] }],
|
||||
metadata: { user_id: userID },
|
||||
metadata: { user_id: userID() },
|
||||
})
|
||||
})
|
||||
|
||||
@@ -310,7 +313,7 @@ test("a Start Plan body with no model has no powered-by line; a blank system add
|
||||
expect(JSON.parse(r.body)).toEqual({
|
||||
system: zcodeSystem(""),
|
||||
messages: [reminder, { role: "user", content: [{ type: "text", text: "x", cache_control: eph }] }],
|
||||
metadata: { user_id: userID },
|
||||
metadata: { user_id: userID() },
|
||||
})
|
||||
})
|
||||
|
||||
@@ -365,16 +368,19 @@ test("magpie's word on the folder goes to no vendor on a Coding Plan request eit
|
||||
})
|
||||
|
||||
// the GLM Coding Plan and team seats: as the agent sent it, as 0.1.6 sent
|
||||
// it, but for metadata.user_id naming the account's device as ZCode's
|
||||
// client does on every plan (#387)
|
||||
// it, but for metadata.user_id naming the account's device and x-session-id
|
||||
// the conversation, one UUID in both, as ZCode's client does on every plan
|
||||
// (#387); the agent named no session, so it is the device's
|
||||
const agentText = JSON.stringify(agentBody)
|
||||
const sentAsIs = (r, key, path, device) => {
|
||||
expect(r.path).toBe(path)
|
||||
const b = JSON.parse(r.body)
|
||||
expect(JSON.parse(b.metadata.user_id)).toEqual({ device_id: device, account_uuid: "", session_id: "" })
|
||||
const { "x-session-id": sid, ...h } = own(r.headers)
|
||||
expect(sid).toBe(_internal.sessionOf("", device))
|
||||
expect(JSON.parse(b.metadata.user_id)).toEqual({ device_id: device, account_uuid: "", session_id: sid })
|
||||
delete b.metadata.user_id
|
||||
expect(b).toEqual(agentBody)
|
||||
expect(own(r.headers)).toEqual({
|
||||
expect(h).toEqual({
|
||||
authorization: "Bearer " + key,
|
||||
"x-api-key": key,
|
||||
"anthropic-version": "2023-06-01",
|
||||
|
||||
Reference in New Issue
Block a user