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:
wendyeq
2026-10-26 16:02:38 +08:00
committed by GitHub
parent 6bd998a3f8
commit 2b56b3bc4f
6 changed files with 185 additions and 27 deletions
+4 -2
View File
@@ -246,8 +246,10 @@ test("a model the gift does not serve goes to the coding plan as sent", async ()
const m = sent.at(-1)
expect(m.origin).toBe("https://api.z.ai")
expect(m.path).toBe("/api/anthropic/v1/messages")
// as sent, but for the sign-in's device, as ZCode names it (#387)
expect(JSON.parse(m.body)).toEqual({ ...JSON.parse(body("GLM-5.3")), metadata: { user_id: JSON.stringify({ device_id: DEVICE, account_uuid: "", session_id: "" }) } })
// as sent, but for the sign-in's device and the conversation, as ZCode names them (#387)
const session = _internal.sessionOf("", DEVICE)
expect(m.headers["x-session-id"]).toBe(session)
expect(JSON.parse(m.body)).toEqual({ ...JSON.parse(body("GLM-5.3")), metadata: { user_id: JSON.stringify({ device_id: DEVICE, account_uuid: "", session_id: session }) } })
expect(m.headers["x-api-key"]).toBe(KEY)
expect(m.headers["x-title"]).toBeUndefined()
})