autoclaw: rewrite Claude Agent SDK identity lines, explain CN 406 (#253, isdoge)

Claude Code 2.1.30x opens its system prompt with "You are a Claude agent, built on Anthropic's Claude Agent SDK.". AutoClaw's gateways answer 406 to "Claude Agent SDK" anywhere in a system prompt. The author confirmed it on both CN (managed) and overseas (autoglm-api.autoglm.ai). FLAGGED now rewrites the identity line to "You are a coding assistant.", and any other mention, along with the "the " or "Anthropic's " before it, to "the agent framework". Only system and developer messages are rewritten. User, tool and assistant text, and identifiers like @anthropic-ai/claude-agent-sdk, ClaudeAgentSDK and claude_agent_sdk, are left as they are. The CN path's refusals other than 401/403 now go through explained(), so a 406 there carries REFUSED_HINT, which now lists Claude Agent SDK.

Run at the reviewed head a9bd38b1451b745d91019bf168e72f7d3c724779:
- bun test packages/autoclaw: 25 pass.
- index.mjs reverted to main: both new tests fail. index.mjs at 4ca51de: the exact-output test fails (it catches the "assistant.." double period). Restored: green.
- Probe of unflagged()/unflag() on the identity line, "running on" and "the" variants, upper and lower case, code identifiers, and every message role: output as described above.
- Full bun test in a temp HOME under $TMPDIR: 1855 pass, 0 fail. bun scripts/check.mjs: ok.
This commit is contained in:
狗带带子
2026-10-20 15:53:01 +08:00
committed by GitHub
parent 5b68df0057
commit edd9e30c81
2 changed files with 54 additions and 2 deletions
+8 -2
View File
@@ -42,6 +42,12 @@ const OFFICIAL_SYSTEM_PROMPT = 'You are AutoClaw. Answer the user directly and c
const WALLETS_PATH = `/agent-assetmgr/api/v2/wallets?biz_app_id=${PRODUCT}`;
const FLAGGED = [
// Claude Code 2.1.30x opens with an SDK identity line, not the CLI one;
// the CN gateway refuses "Claude Agent SDK" in any case with 406. Take the
// trailing period and the article/possessive so the replacement reads as
// a sentence, not "assistant.." or "Anthropic's the agent framework".
[/You are a Claude agent, built on Anthropic's Claude Agent SDK\.?/gi, "You are a coding assistant."],
[/(?:Anthropic's |the )?Claude Agent SDK/gi, "the agent framework"],
[/You are an AI agent powered by DeepSeek Harness/gi, "You are an AI agent powered by DeepSeek-Harness"],
[/You are Claude Code/gi, "You are the Claude Code CLI"],
[/You are Codex/gi, "You are the Codex agent"],
@@ -84,7 +90,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, ZCode, pi, 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, Claude Agent SDK, 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;
@@ -628,7 +634,7 @@ function makeAutoclawPlugin({ providerId, providerName, homeHost, loginMethod })
headers,
body: JSON.stringify(payload),
});
if (res.status !== 401 && res.status !== 403) return res;
if (res.status !== 401 && res.status !== 403) return explained(res);
try { res.body?.cancel?.(); } catch {}
return new Response(JSON.stringify({ error: { message: "AutoClaw 登录已失效", type: "invalid_request_error" } }), {
status: 401,
+46
View File
@@ -188,6 +188,19 @@ test("ZCode's and pi's identity lines are rewritten", () => {
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.")
})
// Claude Code 2.1.30x opens its system prompt with the SDK identity line,
// not the CLI one. The CN gateway refuses "Claude Agent SDK" in any case,
// wherever it sits; the bare phrase is matched so "running on" variants and
// later mentions are covered too.
test("Claude Agent SDK's identity line is rewritten", () => {
const sdk = "You are a Claude agent, built on Anthropic's Claude Agent SDK."
expect(unflagged(sdk)).toBe("You are a coding assistant.")
expect(unflagged("You are a Claude agent, running on Anthropic's Claude Agent SDK.")).toBe("You are a Claude agent, running on the agent framework.")
expect(unflagged("We build on the Claude Agent SDK; see docs.")).toBe("We build on the agent framework; see docs.")
// the same text in, the same text out: a cached prompt prefix stays put
expect(unflagged(unflagged(sdk))).toBe(unflagged(sdk))
})
// 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", () => {
@@ -267,6 +280,39 @@ test("overseas loader().fetch drops X-Harness-Type and explains 406", async () =
})
test("CN loader rewrites the Claude Agent SDK identity and explains its 406", async () => {
const realFetch = globalThis.fetch
const calls = []
globalThis.fetch = async (input, init) => {
calls.push({ url: String(input), headers: new Headers(init?.headers), body: JSON.parse(String(init?.body ?? "{}")) })
return new Response(JSON.stringify({ error: { code: null, message: "406 Not Acceptable", type: "api_error" } }), { status: 406 })
}
try {
const hooks = await AutoclawCnAuthPlugin({ client: { auth: { set: async () => {} } } })
const loader = await hooks.auth.loader(async () => ({ type: "oauth", access: "a1" }))
const body = JSON.stringify({
model: "zaicoding_glm-5.3",
messages: [
{ role: "system", content: "You are a Claude agent, built on Anthropic's Claude Agent SDK." },
{ role: "user", content: "hi" },
],
})
const refused = await loader.fetch("x", { method: "POST", body })
expect(refused.status).toBe(406)
expect(refused.headers.get("X-Magpie-Sign-In")).toBe("kept")
expect((await refused.json()).error.message).toContain(REFUSED_HINT)
const sent = calls.at(-1)
expect(sent.url).toBe("https://autoglm-acceleration-api.zhipuai.cn/autoclaw-proxy/proxy/autoclaw/chat/completions")
expect(sent.body.messages[0].content.startsWith(OFFICIAL_SYSTEM_PROMPT)).toBe(true)
expect(sent.body.messages[0].content).not.toMatch(/Claude Agent SDK/i)
expect(sent.headers.get("X-Authorization")).toBe("Bearer a1")
} finally {
globalThis.fetch = realFetch
}
})
test("refreshTokens orders hosts by the token's source_id and falls back on 400002", async () => {
const realFetch = globalThis.fetch
const requests = []