zcode 0.1.30: a coding plan's requests name the device in metadata.user_id, as ZCode's do (#387)
wendyeq measured BigModel's GLM Coding Plan through magpie hitting the
prompt cache 1 time in 19 without metadata.user_id and 13 in 16 with it;
the official ZCode on the same account reads 95-98% of input from cache.
The plugin only put user_id on the Start Plan's dressed body; a coding
plan or team request went as the agent sent it, and Pi sends none.
ZCode 3.14.3 (glm/zcode.cjs) sets it on every Anthropic-format request,
not only the Start Plan's: Pst() returns MLs({deviceMid, sessionId}) for
any providerKind "anthropic", and vWr() puts it in
providerOptions.anthropic.metadata.userId for both generateText and
streamText. The value is {"device_id":<machine id>,"account_uuid":"",
"session_id":...}.
Now a coding plan or team request to .../v1/messages gets
metadata.user_id = {"device_id":<sign-in's saved device>,
"account_uuid":"","session_id":""}, the same value dress() puts on the
Start Plan's, when the agent sent no user_id of its own; other metadata
is kept and content-length dropped when the body changed. count_tokens
and non-Messages bodies go as they were. An account known only by a key
(a pasted API key, or a saved state with no device) gets a device id
hashed from the key, so it is the same on every start rather than
anyDevice, which was new per process.
Differences from ZCode, kept on purpose: ZCode overwrites any user_id
and fills session_id with its session (sess_ prefix stripped); this
keeps an agent's own user_id and sends session_id "" as dress() does,
one stable value per account.
Tests: userid.test.mjs (new, 6 tests; 5 red on origin/main's index.mjs,
the count_tokens one passes there as it should). start, toolsearch and
dual tests that pinned coding plan bodies byte for byte now expect the
agent's body plus the device's user_id.
Run at this tree: HOME=$(mktemp -d) bun test -> 2672 pass, 1 skip,
0 fail across 229 files; bun scripts/check.mjs ok.
Not tried: a real BigModel or Z.ai coding plan request, so the cache
hit rate after the change is the reporter's measurement, not mine.
This commit is contained in:
@@ -4,7 +4,8 @@
|
||||
// (#343), the day in a
|
||||
// <system-reminder> turn of its own, one cache mark in the turns, none on
|
||||
// the tools and the sign-in's device id in metadata.user_id. A GLM Coding
|
||||
// Plan or team request goes as the agent sent it. The requests go to a
|
||||
// Plan or team request goes as the agent sent it, but for that device id,
|
||||
// which ZCode puts on every request (#387). The requests go to a
|
||||
// local Bun.serve that records them; nothing reaches z.ai or bigmodel.
|
||||
import { afterAll, afterEach, beforeAll, expect, test } from "bun:test"
|
||||
import { createHash } from "node:crypto"
|
||||
@@ -12,13 +13,13 @@ import { arch, homedir, release, tmpdir } from "node:os"
|
||||
import { basename } from "node:path"
|
||||
import { realpathSync } from "node:fs"
|
||||
|
||||
let ZCodeAuthPlugin, PROMPT
|
||||
let ZCodeAuthPlugin, PROMPT, _internal
|
||||
let server
|
||||
const got = []
|
||||
let answer = null // what the fake answers instead, when set
|
||||
beforeAll(async () => {
|
||||
if (![tmpdir(), realpathSync(tmpdir())].some((t) => homedir().startsWith(t))) throw new Error("run with HOME=$(mktemp -d) bun test")
|
||||
;({ ZCodeAuthPlugin } = await import("./index.mjs"))
|
||||
;({ ZCodeAuthPlugin, _internal } = await import("./index.mjs"))
|
||||
PROMPT = (await import("./prompt.mjs")).default
|
||||
server = Bun.serve({
|
||||
port: 0,
|
||||
@@ -359,14 +360,20 @@ test("magpie's word on the folder goes to no vendor on a Coding Plan request eit
|
||||
await send({ type: "api", key: "one.secret", metadata: { site: "zai" } }, agentText, { place: { dir: "%2Fx", git: "no" } }),
|
||||
"one.secret",
|
||||
"/api.z.ai/api/anthropic/v1/messages",
|
||||
_internal.keyDevice("one.secret"),
|
||||
)
|
||||
})
|
||||
|
||||
// the GLM Coding Plan and team seats: as the agent sent it, as 0.1.6 sent it
|
||||
// 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)
|
||||
const agentText = JSON.stringify(agentBody)
|
||||
const sentAsIs = (r, key, path) => {
|
||||
const sentAsIs = (r, key, path, device) => {
|
||||
expect(r.path).toBe(path)
|
||||
expect(r.body).toBe(agentText)
|
||||
const b = JSON.parse(r.body)
|
||||
expect(JSON.parse(b.metadata.user_id)).toEqual({ device_id: device, account_uuid: "", session_id: "" })
|
||||
delete b.metadata.user_id
|
||||
expect(b).toEqual(agentBody)
|
||||
expect(own(r.headers)).toEqual({
|
||||
authorization: "Bearer " + key,
|
||||
"x-api-key": key,
|
||||
@@ -377,19 +384,19 @@ const sentAsIs = (r, key, path) => {
|
||||
}
|
||||
|
||||
test("a GLM Coding Plan key's request goes as the agent sent it", async () => {
|
||||
sentAsIs(await send({ type: "api", key: "one.secret", metadata: { site: "zai" } }, agentText), "one.secret", "/api.z.ai/api/anthropic/v1/messages")
|
||||
sentAsIs(await send({ type: "api", key: "one.secret", metadata: { site: "zai" } }, agentText), "one.secret", "/api.z.ai/api/anthropic/v1/messages", _internal.keyDevice("one.secret"))
|
||||
got.length = 0
|
||||
sentAsIs(await send({ type: "api", key: "two.secret", metadata: { site: "bigmodel" } }, agentText, { url: "https://open.bigmodel.cn/api/anthropic/v1/messages" }), "two.secret", "/open.bigmodel.cn/api/anthropic/v1/messages")
|
||||
sentAsIs(await send({ type: "api", key: "two.secret", metadata: { site: "bigmodel" } }, agentText, { url: "https://open.bigmodel.cn/api/anthropic/v1/messages" }), "two.secret", "/open.bigmodel.cn/api/anthropic/v1/messages", _internal.keyDevice("two.secret"))
|
||||
})
|
||||
|
||||
test("a team seat's request goes as the agent sent it, ZCode's token or not", async () => {
|
||||
const s = { site: "zai", base: "https://api.z.ai/api/anthropic", key: "seat.secret", token: "tok", org: "o1", project: "p1", jwt, device: DEVICE }
|
||||
const auth = { type: "oauth", access: s.key, refresh: JSON.stringify(s), expires: 0 }
|
||||
sentAsIs(await send(auth, agentText, { requests: 2 }), "seat.secret", "/api.z.ai/api/anthropic/v1/messages")
|
||||
sentAsIs(await send(auth, agentText, { requests: 2 }), "seat.secret", "/api.z.ai/api/anthropic/v1/messages", DEVICE)
|
||||
expect(got.filter((r) => r.path.endsWith("/billing/balance"))).toHaveLength(1)
|
||||
expect(got.filter((r) => r.path.endsWith("/subscription/list"))).toHaveLength(0)
|
||||
got.length = 0
|
||||
sentAsIs(await send({ ...auth, refresh: JSON.stringify({ ...s, jwt: "" }) }, agentText), "seat.secret", "/api.z.ai/api/anthropic/v1/messages")
|
||||
sentAsIs(await send({ ...auth, refresh: JSON.stringify({ ...s, jwt: "" }) }, agentText), "seat.secret", "/api.z.ai/api/anthropic/v1/messages", DEVICE)
|
||||
expect(got).toHaveLength(1)
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user