factory 0.1.37: a billing header joined with instructions loses only its own line, not the instructions after it (#302)

poer2023 (#302): anthropicBody() dropped every top-level system text
block, and a string system, that starts with
"x-anthropic-billing-header: cc_version=". When the header and the
instructions after it arrive joined in one block or one string (Claude
Code's auto-mode classifier policy, for one), the instructions went with
it and Factory got only the Droid identity.

Fix: only the header's first line goes. The rest of the text stays, and
the block keeps its other fields (cache_control and the rest); the rest
then goes through promptText like any other system block, so an identity
line after the header is still made Droid's. A block, or string, holding
only the header (or the header and blank lines) is still dropped.

Siblings: grep -rn "x-anthropic-billing-header" packages/ finds only one
other plugin that strips it, workbuddy (FLAGGED), which already removes
just the line with /x-anthropic-billing-header:[^\n]*\n?/gi and keeps the
rest (unflag.test.mjs covers "header\nhello" -> "hello"). kiro and
antigravity-cloak only carry it in fixtures or escape it. No other
plugin has the bug.

Test: request.test.mjs "a billing header joined with instructions loses
only its own line (#302)" takes the reporter's literal inputs (billing
"cc_version=2.1.300; cc_entrypoint=cli;" + "Preserve this task
instruction. SYSTEM_POLICY_SENTINEL"): joined block with cache_control,
joined string, separate blocks, billing only, billing plus blank lines,
on /v1/messages and /count_tokens, checks the exact system sent, that
messages and max_tokens are unchanged, and that a second pass of the
sent body is byte-for-byte; plus a header followed by Claude Code's
identity line.

Run:
- packages/factory: H=$(mktemp -d); HOME="$H" bun test (bun 1.4.2):
  172 pass, 0 fail across 16 files.
- Broken by hand: with index.mjs at origin/main 3d355d3 the new test
  fails (70 pass, 1 fail); with the fix, 71 pass.
- The reporter's offline repro-billing.mjs from the issue, run with
  node against the fixed index.mjs: all four cases pass (joined block,
  joined string, separate blocks, billing only), network false.

Not tried: a real Factory account or Claude Code's auto mode end to end;
the check stops at the body handed to the stubbed fetch.
This commit is contained in:
yetone
2026-10-22 11:11:00 +08:00
parent 3d355d349d
commit bb020ce804
3 changed files with 47 additions and 4 deletions
+9 -3
View File
@@ -1563,10 +1563,16 @@ function anthropicBody(body) {
continue
}
// Attribution is consumed here; Factory does not strip Anthropic's
// billing block and requires the Droid identity to be first.
if (block?.type === "text" && block.text?.startsWith("x-anthropic-billing-header: cc_version=")) {
// billing block and requires the Droid identity to be first. Only the
// header's own line goes: a block (or string system) that carries
// instructions after it, such as Claude Code's auto-mode classifier
// policy, keeps them and its other fields (#302).
if (block?.type === "text" && block.text.startsWith("x-anthropic-billing-header: cc_version=")) {
changed = true
continue
const end = block.text.indexOf("\n")
const rest = end < 0 ? "" : block.text.slice(end + 1)
if (rest.trim() === "") continue
block.text = rest
}
if (block?.type === "text") {
// The gateway may join the identity and instructions into one block.
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-factory-auth",
"version": "0.1.36",
"version": "0.1.37",
"description": "Sign in to a Factory (Droid) subscription from OpenCode or magpie",
"type": "module",
"main": "./index.mjs",
+37
View File
@@ -1662,3 +1662,40 @@ test("adapting a system prompt's configuration section is idempotent", async ()
await l.fetch(url, { method: "POST", body: seen[0].body })
expect(seen[1].body).toBe(seen[0].body)
})
// magpie-community/plugins#302, the reporter's offline repro: the billing
// header joined with the instructions after it, in one block or a string
// system, loses only its own line.
test("a billing header joined with instructions loses only its own line (#302)", async () => {
const { l, seen } = await loaded()
const billing = "x-anthropic-billing-header: cc_version=2.1.300; cc_entrypoint=cli;"
const policy = "Preserve this task instruction. SYSTEM_POLICY_SENTINEL"
const cases = [
["joined block", [{ type: "text", text: billing + "\n" + policy, cache_control: { type: "ephemeral" } }],
[{ type: "text", text: droid }, { type: "text", text: policy, cache_control: { type: "ephemeral" } }]],
["joined string", billing + "\n" + policy, [{ type: "text", text: droid }, { type: "text", text: policy }]],
["separate blocks", [{ type: "text", text: billing }, { type: "text", text: policy }], [{ type: "text", text: droid }, { type: "text", text: policy }]],
["billing only", [{ type: "text", text: billing }], [{ type: "text", text: droid }]],
["billing and a blank line", billing + "\n\n", [{ type: "text", text: droid }]],
]
for (const [name, system, want] of cases) {
const messages = [{ role: "user", content: "Hello" }]
for (const endpoint of [url, url + "/count_tokens"]) {
await l.fetch(endpoint, { method: "POST", body: JSON.stringify({ model: "claude-sonnet-5", system, messages, max_tokens: 64 }) })
const sent = JSON.parse(seen.at(-1).body)
expect([name, sent.system]).toEqual([name, want])
expect(sent.messages).toEqual(messages)
expect(sent.max_tokens).toBe(64)
// what was sent is what goes again unchanged
const once = seen.at(-1).body
await l.fetch(endpoint, { method: "POST", body: once })
expect(seen.at(-1).body).toBe(once)
}
}
// the identity after the header is still made Droid's
await l.fetch(url, { method: "POST", body: JSON.stringify({ model: "claude-sonnet-5", system: billing + "\nYou are Claude Code, Anthropic's official CLI for Claude.\n" + policy, messages: [{ role: "user", content: "Hello" }] }) })
const sent = JSON.parse(seen.at(-1).body).system
expect(JSON.stringify(sent)).not.toContain("x-anthropic-billing-header")
expect(sent[0].text).toStartWith(droid)
expect(JSON.stringify(sent)).toContain("SYSTEM_POLICY_SENTINEL")
})