autoclaw 0.2.2: ZCode's and pi's identity lines are rewritten for the CN managed gateway (#245, #236)

autoclaw-cn's managed gateway (autoglm-acceleration-api.zhipuai.cn) answers
406 when the system text carries an agent identity line FLAGGED doesn't
cover. Two more entries, in the table's own style:

- "You are ZCode" (any case, anywhere; #245's probes) -> "You are the ZCode
  agent". #245 found "You are an interactive ZCode agent ..." answered 200,
  so the gateway reads the literal run, which the rewrite breaks.
- "operating inside pi, a coding agent harness" -> "operating inside the pi
  coding agent harness" (#236). pi's line wasn't probed, so the rewrite
  breaks both "inside pi" and "pi, a coding agent harness" and keeps pi's
  name for the model.

Both rewrites are fixed strings, so the same prompt always leaves the same
way and a cached prefix stays put. Only system and developer messages are
rewritten, as before. Claude Code's entry is unchanged. REFUSED_HINT names
ZCode and pi.

No general fallback: agents such as opencode put the user's own AGENTS.md
into the system text, so a pattern like "You are X, a ... agent" would
rewrite the user's words, and nothing shows the gateway matches that shape.

Tests use each reporter's literal line: unflagged, unflag's role scoping
(ZCode in a system part rewritten, the same line in a user message and pi's
in a tool message untouched), and the CN managed path end to end.

Run (worktree at origin/main 47fbf63):
- bun test packages/autoclaw/: 23 pass, 0 fail
- bun scripts/check.mjs autoclaw: ok
- HOME=$(mktemp -d) bun test: 1808 pass, 1 fail, the trae stream.test
  "connection reset" under the full suite (#124, open); packages/trae/
  alone 160/160 three times
- broken by hand: ZCode entry removed -> 3 red; pi entry removed -> 2 red;
  unflag's role check removed -> 2 red
- not tried against the real gateway: no autoclaw-cn account is signed in
  here
This commit is contained in:
yetone
2026-10-19 17:42:06 +08:00
parent 47fbf634e7
commit dff29df402
3 changed files with 61 additions and 2 deletions
+3 -1
View File
@@ -50,6 +50,8 @@ const FLAGGED = [
[/You are Cursor/gi, "You are the Cursor agent"],
[/You are Cline/gi, "You are the Cline agent"],
[/You are Qwen Code/gi, "You are the Qwen Code agent"],
[/You are ZCode/gi, "You are the ZCode agent"],
[/operating inside pi, a coding agent harness/gi, "operating inside the pi coding agent harness"],
];
function unflagged(text) {
@@ -82,7 +84,7 @@ function unflag(messages) {
}
const REFUSED_HINT =
"AutoClaw refuses chats whose system prompt carries another agent's identity line (Claude Code, Codex, opencode, Cursor, Cline, Qwen Code, DeepSeek Harness); this plugin rewrites the ones it knows, so an unknown one needs another provider in this group";
"AutoClaw refuses chats whose system prompt carries another agent's identity line (Claude Code, Codex, opencode, Cursor, Cline, Qwen Code, ZCode, pi, DeepSeek Harness); this plugin rewrites the ones it knows, so an unknown one needs another provider in this group";
async function explained(res) {
if (res.status < 400) return res;
+57
View File
@@ -163,6 +163,31 @@ test("every flagged line's replacement is one the gateway still takes", () => {
expect(unflagged("Codex CLI is an open source project.")).not.toMatch(/Codex CLI/)
})
// ZCode (#245) and pi (#236) answered 406 on the CN managed gateway. The
// fixtures are each reporter's identity line, byte for byte. #245's probes
// found the gateway matching "You are ZCode" in any case, anywhere in the
// text; pi's line wasn't probed, so its rewrite breaks "inside pi, a coding
// agent harness" as a whole while keeping pi's name for the model.
test("ZCode's and pi's identity lines are rewritten", () => {
const zcode = "You are ZCode, an interactive coding agent"
expect(unflagged(zcode)).toBe("You are the ZCode agent, an interactive coding agent")
expect(unflagged("Note: you are zcode here.")).toBe("Note: You are the ZCode agent here.")
for (const t of ["You are ZCode.", "you are zcode", `Intro.\n${zcode}.`]) {
expect(unflagged(t)).not.toMatch(/You are ZCode/i)
}
const pi = "You are an expert coding assistant operating inside pi, a coding agent harness. You help users by reading files, executing commands, editing code, and writing new files."
expect(unflagged(pi)).toBe("You are an expert coding assistant operating inside the pi coding agent harness. You help users by reading files, executing commands, editing code, and writing new files.")
expect(unflagged(pi)).not.toMatch(/inside pi|pi, a coding agent harness/i)
// the same text in, the same text out: a cached prompt prefix stays put
expect(unflagged(unflagged(pi))).toBe(unflagged(pi))
expect(unflagged(unflagged(zcode))).toBe(unflagged(zcode))
// Claude Code's line is left to its own entry, exactly as before
expect(unflagged("You are Claude Code, Anthropic's official CLI for Claude.")).toBe("You are the Claude Code CLI, Anthropic's official CLI for Claude.")
})
// The gateway reads the system prompt, not the chat: the caller's own words
// in a user or assistant message stay as they are.
test("unflag rewrites system and developer messages only", () => {
@@ -171,12 +196,18 @@ test("unflag rewrites system and developer messages only", () => {
{ role: "developer", content: [{ type: "text", text: "You are Cursor" }] },
{ role: "user", content: "You are Codex" },
{ role: "assistant", content: [{ type: "text", text: "Codex CLI is what I use" }] },
{ role: "system", content: [{ type: "text", text: "You are ZCode, an interactive coding agent" }] },
{ role: "user", content: "You are ZCode, an interactive coding agent" },
{ role: "tool", content: "operating inside pi, a coding agent harness" },
]
expect(unflag(messages)).toBe(true)
expect(messages[0].content).toBe("You are the Claude Code CLI, Anthropic's official CLI for Claude.")
expect(messages[1].content[0].text).toBe("You are the Cursor agent")
expect(messages[2].content).toBe("You are Codex")
expect(messages[3].content[0].text).toBe("Codex CLI is what I use")
expect(messages[4].content[0].text).toBe("You are the ZCode agent, an interactive coding agent")
expect(messages[5].content).toBe("You are ZCode, an interactive coding agent")
expect(messages[6].content).toBe("operating inside pi, a coding agent harness")
// nothing left to change the second time
expect(unflag(messages)).toBe(false)
})
@@ -397,6 +428,32 @@ test("CN's managed gateway gets AutoClaw's official body and headers", async ()
} finally { globalThis.fetch = realFetch }
})
// #245's surface: ZCode through autoclaw-cn's managed gateway. The system
// text that leaves carries no "You are ZCode"; the user's own words do.
test("CN's managed gateway gets ZCode's and pi's lines rewritten", async () => {
const realFetch = globalThis.fetch
const calls = []
globalThis.fetch = async (input, init) => {
calls.push({ url: String(input), body: JSON.parse(String(init?.body ?? "{}")) })
return Response.json({ id: "x", object: "chat.completion", created: 1, model: "zaicoding_glm-5.3", choices: [{ index: 0, message: { role: "assistant", content: "OK" }, finish_reason: "stop" }] })
}
try {
const factory = await makePlugin("autoclaw-cn")
const hooks = await factory({ client: { auth: { set: async () => {} } } }, {})
const loader = await hooks.auth.loader(async () => ({ type: "oauth", access: "a1" }))
const zcode = "You are ZCode, an interactive coding agent"
const pi = "You are an expert coding assistant operating inside pi, a coding agent harness. You help users by reading files, executing commands, editing code, and writing new files."
for (const system of [zcode, pi]) {
await loader.fetch("x", { body: JSON.stringify({ model: "zaicoding_glm-5.3", stream: false, messages: [{ role: "system", content: system }, { role: "user", content: system }] }) })
const sent = calls.at(-1).body
expect(calls.at(-1).url).toBe("https://autoglm-acceleration-api.zhipuai.cn/autoclaw-proxy/proxy/autoclaw/chat/completions")
expect(sent.messages[0].content).toBe(`${OFFICIAL_SYSTEM_PROMPT}\n\n${unflagged(system)}`)
expect(sent.messages[0].content).not.toMatch(/You are ZCode|inside pi, a coding agent harness/i)
expect(sent.messages[1].content).toBe(system)
}
} finally { globalThis.fetch = realFetch }
})
function makePlugin(id) {
return _internal.makePluginForTest({
providerId: id,
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-autoclaw-auth",
"version": "0.2.1",
"version": "0.2.2",
"description": "OpenCode provider plugin for AutoClaw (Zhipu OpenClaw): oversea web sign-in (Google/z.ai) and CN phone SMS, multi-account pool, credit balance and daily check-in",
"type": "module",
"main": "./index.mjs",