diff --git a/packages/factory/index.mjs b/packages/factory/index.mjs index 0f0ef0a..ca8cac9 100644 --- a/packages/factory/index.mjs +++ b/packages/factory/index.mjs @@ -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. diff --git a/packages/factory/package.json b/packages/factory/package.json index 911a89e..e1bb42f 100644 --- a/packages/factory/package.json +++ b/packages/factory/package.json @@ -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", diff --git a/packages/factory/request.test.mjs b/packages/factory/request.test.mjs index 4986f15..032b8ae 100644 --- a/packages/factory/request.test.mjs +++ b/packages/factory/request.test.mjs @@ -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") +})