zcode 0.1.21: a Start Plan request sends the agent's system prompt at the end of ZCode's third system block, not as a fourth (#343)

fordlyn's report: on a Start Plan account zcode.z.ai now answers every
request whose system has more than three blocks with 405
{"code":3012,"msg":"request has been blocked due to unusual activity."}.
dress() put ZCode's three cached blocks first and then the agent's own
system texts as blocks 4, 5, ..., so every agent (all of them send a
system prompt) was turned away; only a system-less request passed.

What ZCode does, read from ZCode.app 3.14.3's glm/zcode.cjs
(ContextBuilder.assembleSystemMessages): it sends at most three system
messages, the cli_prefix section, the stable sections, and every dynamic
system section joined with "\n\n" into a third block that starts with
"\n\n". A section added to the builder (addSection) is dynamic by
default, so ZCode's own extra system text lands in the third block.
dress() now does the same: the agent's non-blank text blocks are joined
with "\n\n" after afterEnvironment in the third block, which keeps its
cache mark. The first two blocks stay byte for byte ZCode's, as the
issue's bisect requires. GLM Coding Plan and team requests are not
dressed and are unchanged.

Run: TMPDIR=<scratch> HOME=<scratch>/home bun test (2341 pass, 0 fail);
bun scripts/check.mjs (all ok). The new #343 test in start.test.mjs and
four updated layout tests are red on the old index.mjs (5 of 12 fail).
Not tried against the real API: no Start Plan account at hand. The
reporter's own harness got 200 for this shape (system "x", system plus
tools, 32 KB system, multi-turn).
This commit is contained in:
yetone
2026-10-23 23:22:58 +08:00
parent 61d058f6cf
commit 44036e2fcc
4 changed files with 60 additions and 27 deletions
+3 -2
View File
@@ -21,8 +21,9 @@ over Anthropic's Messages API. Provider id: `zcode`.
- zcode.z.ai serves the Start Plan only to requests that look like the
ZCode client's own, and refuses others with HTTP 405 "request has been
blocked due to unusual activity". So a Start Plan request goes as
ZCode's does: ZCode's headers, ZCode's system prompt ahead of the
agent's, the day in a `<system-reminder>` turn before the agent's
ZCode's does: ZCode's headers, ZCode's three system blocks with the
agent's system prompt at the end of the third (a fourth block is
refused, #343), the day in a `<system-reminder>` turn before the agent's
turns, and the sign-in's device id in `metadata.user_id`. GLM Coding
Plan and team requests go as the agent sent them. One it still turns
away (a 405, the block page or code 3012) comes back with what to do
+12 -9
View File
@@ -1484,7 +1484,8 @@ function contextPrefix() {
}
// dress is a Messages request body as ZCode sends it: ZCode's three cached
// system blocks, then the agent's own system text uncached; the context
// system blocks and no more, the agent's own system text at the end of the
// third (#343); the context
// prefix as the first turn; no cache mark in the turns but on the last
// block of the last; none on the tools; metadata.user_id naming the
// sign-in's device. site names the powered-by line's provider, place where
@@ -1502,18 +1503,20 @@ function dress(text, site, device, place) {
const cached = (t) => ({ type: "text", text: t, cache_control: { type: "ephemeral" } })
const provider = site === "bigmodel" ? "bigmodel-api" : "zai-api"
const model = typeof b.model === "string" ? b.model : ""
// The agent's own system text joins the third block, as ZCode's
// ContextBuilder (3.14.3, glm/zcode.cjs assembleSystemMessages) joins
// every dynamic system section, an added one too, with "\n\n" into
// that one block: ZCode never sends more than three. zcode.z.ai turns a
// fourth block away with 405/3012 (#343), and the first two must stay
// byte for byte ZCode's.
const own = []
if (typeof b.system === "string") {
if (b.system.trim()) own.push({ type: "text", text: b.system })
if (b.system.trim()) own.push(b.system)
} else if (Array.isArray(b.system)) {
for (const x of b.system) if (x?.type === "text" && typeof x.text === "string" && x.text) own.push({ type: "text", text: x.text })
for (const x of b.system) if (x?.type === "text" && typeof x.text === "string" && x.text.trim()) own.push(x.text)
}
b.system = [
cached(PROMPT.cliPrefix),
cached(PROMPT.stableSections.join("\n\n")),
cached("\n\n" + [PROMPT.beforeEnvironment, environment(provider, model, place), PROMPT.afterEnvironment].join("\n\n")),
...own,
]
const dynamic = [PROMPT.beforeEnvironment, environment(provider, model, place), PROMPT.afterEnvironment, ...own]
b.system = [cached(PROMPT.cliPrefix), cached(PROMPT.stableSections.join("\n\n")), cached("\n\n" + dynamic.join("\n\n"))]
b.messages = [contextPrefix(), ...b.messages]
let last = null
for (const m of b.messages) {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-zcode-auth",
"version": "0.1.20",
"version": "0.1.21",
"description": "OpenCode provider plugin: signs in to Z.ai's or BigModel's (智谱) GLM Coding Plan as ZCode does, including team seats and ZCode's Start Plan",
"type": "module",
"main": "./index.mjs",
+44 -15
View File
@@ -1,6 +1,7 @@
// A Start Plan request goes as the plugin zcode.z.ai serves the plan to
// (ARNO's "Freeflow", provider zcode-start) sends it: ZCode's headers, its
// three cached system blocks around the agent's own, the day in a
// three cached system blocks with the agent's own at the end of the third
// (#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
@@ -96,10 +97,12 @@ const environment = (powered, where = []) =>
...(powered ? [`- You are powered by the model named ${powered}.`] : []),
].join("\n")
const eph = { type: "ephemeral" }
const zcodeSystem = (powered, where) => [
// zcodeSystem is ZCode's three blocks, the agent's own system texts (own)
// joined to the end of the third
const zcodeSystem = (powered, where, own = []) => [
{ type: "text", text: PROMPT.cliPrefix, cache_control: eph },
{ type: "text", text: PROMPT.stableSections.join("\n\n"), cache_control: eph },
{ type: "text", text: "\n\n" + [PROMPT.beforeEnvironment, environment(powered, where), PROMPT.afterEnvironment].join("\n\n"), cache_control: eph },
{ type: "text", text: "\n\n" + [PROMPT.beforeEnvironment, environment(powered, where), PROMPT.afterEnvironment, ...own].join("\n\n"), cache_control: eph },
]
const reminder = {
role: "user",
@@ -135,6 +138,37 @@ const agentBody = {
stream: true,
}
const AGENT_OWN = ["You are opencode, an agent.", "Project instructions."]
// #343 (fordlyn): zcode.z.ai answers a Start Plan request whose system has
// a fourth block with 405 {"code":3012,"msg":"request has been blocked due
// to unusual activity."}, and every agent sends a system prompt. ZCode's
// ContextBuilder joins every dynamic system section into its third block
// and never sends more than three, so the agent's text goes there too.
test("a Start Plan request's system is ZCode's three blocks, the agent's text at the end of the third (#343)", async () => {
const cases = [
["x", ["x"]],
[[{ type: "text", text: "x", cache_control: eph }], ["x"]],
[[{ type: "text", text: "a" }, { type: "text", text: "" }, { type: "text", text: " " }, { type: "text", text: "b", cache_control: eph }], ["a", "b"]],
["s".repeat(32 * 1024), ["s".repeat(32 * 1024)]],
]
for (const [system, own] of cases) {
got.length = 0
const r = await send(start("zai"), JSON.stringify({ model: "GLM-5.3", max_tokens: 8, system, messages: [{ role: "user", content: "hi" }] }))
const b = JSON.parse(r.body)
expect(b.system.length).toBe(3)
expect(b.system).toEqual(zcodeSystem("zai-api/GLM-5.3", undefined, own))
// the first two are ZCode's byte for byte; the third ends with the agent's
expect(sha(b.system[0].text)).toBe("46dd360a22c87a92")
expect(sha(b.system[1].text)).toBe("49bda31511fc9670")
expect(b.system[2].text.endsWith(PROMPT.afterEnvironment + "\n\n" + own.join("\n\n"))).toBe(true)
}
// with no system of the agent's, the third block ends where ZCode's does
got.length = 0
const r = await send(start("zai"), JSON.stringify({ model: "GLM-5.3", max_tokens: 8, messages: [{ role: "user", content: "hi" }] }))
expect(JSON.parse(r.body).system).toEqual(zcodeSystem("zai-api/GLM-5.3"))
})
test("the prompt texts are the plugin's, byte for byte", () => {
expect(sha(PROMPT.cliPrefix)).toBe("46dd360a22c87a92")
expect(PROMPT.cliPrefix).toBe("You are ZCode, an interactive coding agent")
@@ -189,11 +223,7 @@ test("a Start Plan request's body is dressed as ZCode's", async () => {
model: "GLM-5.3-Flash",
max_tokens: 32000,
thinking: { type: "enabled", budget_tokens: 16000 },
system: [
...zcodeSystem("zai-api/GLM-5.3-Flash"),
{ type: "text", text: "You are opencode, an agent." },
{ type: "text", text: "Project instructions." },
],
system: zcodeSystem("zai-api/GLM-5.3-Flash", undefined, ["You are opencode, an agent.", "Project instructions."]),
messages: [
reminder,
{ role: "user", content: [{ type: "text", text: "hi" }] },
@@ -209,9 +239,8 @@ test("a Start Plan request's body is dressed as ZCode's", async () => {
expect(sha(b.system[1].text)).toBe("49bda31511fc9670")
const dyn = b.system[2].text
expect(dyn.startsWith("\n\n" + PROMPT.beforeEnvironment + "\n\n# Environment\n")).toBe(true)
expect(dyn.endsWith("\n\n" + PROMPT.afterEnvironment)).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(sha(dyn.slice(dyn.length - 1915))).toBe("1732b7f098a925d7")
expect(JSON.parse(b.metadata.user_id)).toEqual({ device_id: DEVICE, account_uuid: "", session_id: "" })
// exactly one cache mark in the turns, and none on the tools
expect((r.body.match(/cache_control/g) ?? []).length).toBe(4)
@@ -224,7 +253,7 @@ test("a BigModel sign-in's Start Plan names bigmodel-api; a string turn takes th
expect(JSON.parse(r.body)).toEqual({
model: "GLM-5.2",
max_tokens: 100,
system: [...zcodeSystem("bigmodel-api/GLM-5.2"), { type: "text", text: " Be brief. " }],
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 },
})
@@ -242,7 +271,7 @@ test("a Start Plan body with no model has no powered-by line; a blank system add
test("a Start Plan request made as a Request, its body a stream, is dressed too", async () => {
const r = await send(start("zai"), JSON.stringify(agentBody), { asRequest: true })
expect(r.headers["x-title"]).toBe("Z Code@cli")
expect(JSON.parse(r.body).system.slice(0, 3)).toEqual(zcodeSystem("zai-api/GLM-5.3-Flash"))
expect(JSON.parse(r.body).system).toEqual(zcodeSystem("zai-api/GLM-5.3-Flash", undefined, AGENT_OWN))
expect(Number(r.headers["content-length"])).toBe(Buffer.byteLength(r.body))
})
@@ -255,8 +284,8 @@ test("a Start Plan request made as a Request, its body a stream, is dressed too"
test("a Start Plan request says the agent's folder as magpie tells it, and no folder when not told (#86)", async () => {
const dir = "C:\\Users\\me\\My Proj 中文 & <x>"
let r = await send(start("zai"), JSON.stringify(agentBody), { place: { dir: encodeURIComponent(dir), git: "yes" } })
expect(JSON.parse(r.body).system.slice(0, 3)).toEqual(
zcodeSystem("zai-api/GLM-5.3-Flash", ["- Primary working directory: " + dir, "- Is a git repository: yes"]),
expect(JSON.parse(r.body).system).toEqual(
zcodeSystem("zai-api/GLM-5.3-Flash", ["- Primary working directory: " + dir, "- Is a git repository: yes"], AGENT_OWN),
)
expect(r.headers["x-magpie-work-dir"]).toBeUndefined()
expect(r.headers["x-magpie-work-git"]).toBeUndefined()
@@ -272,7 +301,7 @@ test("a Start Plan request says the agent's folder as magpie tells it, and no fo
// line, and never the plugin's own folder or shell
r = await send(start("zai"), JSON.stringify(agentBody), { place: { git: "yes" } })
const text = JSON.parse(r.body).system[2].text
expect(JSON.parse(r.body).system.slice(0, 3)).toEqual(zcodeSystem("zai-api/GLM-5.3-Flash"))
expect(JSON.parse(r.body).system).toEqual(zcodeSystem("zai-api/GLM-5.3-Flash", undefined, AGENT_OWN))
expect(text).not.toContain("Primary working directory")
expect(text).not.toContain("Is a git repository")
expect(text).not.toContain("- Shell:")