From 16ec1b8ba946611ec21f19bb6a11212bfd0c20fc Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 3 Oct 2026 16:30:42 +0800 Subject: [PATCH] factory 0.1.14: Claude Code 2.1.288's runtime context sent as a system message of text blocks is adapted as the string one is (yetone/magpie#658) anthropicBody adapted a role:"system" message only when its content was a string, so Claude Code 2.1.288's context as [{type:"text",...}] reached Factory unadapted and was refused (403). Each text block of such a message now goes through systemContext, its other fields and non-text blocks kept; a user text block opening with the generated environment context (a system message folded into the user's turn) is adapted too. The top-level system already treats a string and an array alike, and the OpenAI route adapts no context. Tests: the #658 shape (fails on 0.1.13), string/array parity, idempotence; two tests that asserted array content was left alone now use text outside the generated shape. Not tried with a live Factory account. --- packages/factory/index.mjs | 16 +++++++++++++- packages/factory/package.json | 2 +- packages/factory/request.test.mjs | 36 +++++++++++++++++++++++++++++-- 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/packages/factory/index.mjs b/packages/factory/index.mjs index 2d26c9e..c962413 100644 --- a/packages/factory/index.mjs +++ b/packages/factory/index.mjs @@ -742,6 +742,20 @@ function anthropicBody(body) { } continue } + // Claude Code 2.1.288 also sends the same context as an array of text + // blocks (#658): each text block is adapted as the string would be, + // the blocks' other fields and any other block left as they are. + if (message?.role === "system" && Array.isArray(message.content)) { + for (const block of message.content) { + if (block?.type !== "text" || typeof block.text !== "string") continue + const adapted = systemContext(block.text) + if (adapted !== block.text) { + block.text = adapted + changed = true + } + } + continue + } if (message?.role !== "user" || !Array.isArray(message.content)) continue for (const block of message.content) { if (block?.type === "tool_result") { @@ -767,7 +781,7 @@ function anthropicBody(body) { if (block.text.startsWith(INSTRUCTIONS_REMINDER) && block.text.includes(GLOBAL_INSTRUCTIONS)) { block.text = block.text.replaceAll(GLOBAL_INSTRUCTIONS, "(global instructions)") changed = true - } else if (HOOK_OUTPUT.test(block.text)) { + } else if (HOOK_OUTPUT.test(block.text) || SYSTEM_ENV_CONTEXT.test(block.text)) { // a system message folded into the user's turn on its way here const adapted = systemContext(block.text) if (adapted !== block.text) { diff --git a/packages/factory/package.json b/packages/factory/package.json index 1c2c870..4cfff39 100644 --- a/packages/factory/package.json +++ b/packages/factory/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-factory-auth", - "version": "0.1.13", + "version": "0.1.14", "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 65e5a43..06a3166 100644 --- a/packages/factory/request.test.mjs +++ b/packages/factory/request.test.mjs @@ -283,7 +283,7 @@ test("leaves ordinary system messages and incomplete context outside the generat { role: "system", content: "Follow the user's instructions.\n\n" + context }, { role: "system", content: context.replace(" - Platform", " - Platform") }, { role: "system", content: "You are powered by the model custom-model." }, - { role: "system", content: [{ type: "text", text: context }] }, + { role: "system", content: [{ type: "text", text: "Follow the user's instructions.\n\n" + context }] }, ]) { const body = JSON.stringify({ system: droid, messages: [message] }) await l.fetch(url, { method: "POST", body }) @@ -291,6 +291,38 @@ test("leaves ordinary system messages and incomplete context outside the generat } }) +test("adapts Claude Code 2.1.288's system-role context sent as text blocks, as the string would be (#658)", async () => { + const { l, seen } = await loaded() + // the shape #658 captured from Claude Code 2.1.288 + const context = "# Environment\nYou have been invoked in the following environment: \n - Platform: darwin\n\nYou are powered by the model named Opus 5.5. The exact model ID is claude-opus-5-5. Assistant knowledge cutoff is June 2026.\n\n10000 tokens left" + const adapted = context.replace("# Environment", "# Runtime context") + .replace("You have been invoked in the following environment:", "The session environment is:") + .replace("You are powered by the model named", "Current model name:") + .replace("The exact model ID is", "Model ID:") + .replace("Assistant knowledge cutoff is", "Model knowledge cutoff:") + const other = { type: "image", source: { type: "base64", media_type: "image/png", data: "x" } } + const plain = { type: "text", text: "Keep these instructions verbatim." } + const request = { + system: [{ type: "text", text: droid }], + messages: [{ role: "user", content: [{ type: "text", text: "你好" }] }, { role: "system", content: [{ type: "text", text: context, cache_control: { type: "ephemeral" } }, plain, other] }], + stream: true, + } + for (const endpoint of [url, url + "/count_tokens"]) { + await l.fetch(endpoint, { method: "POST", body: JSON.stringify(request) }) + const sent = JSON.parse(seen.at(-1).body) + expect(sent).toEqual({ ...request, messages: [request.messages[0], { role: "system", content: [{ type: "text", text: adapted, cache_control: { type: "ephemeral" } }, plain, other] }] }) + // the same text as a string comes out the same + await l.fetch(endpoint, { method: "POST", body: JSON.stringify({ ...request, messages: [request.messages[0], { role: "system", content: context }] }) }) + expect(JSON.parse(seen.at(-1).body).messages[1].content).toBe(adapted) + // and adapting again changes nothing + await l.fetch(endpoint, { method: "POST", body: JSON.stringify(sent) }) + expect(seen.at(-1).body).toBe(JSON.stringify(sent)) + } + // folded into the user's turn on its way here + await l.fetch(url, { method: "POST", body: JSON.stringify({ system: droid, messages: [{ role: "user", content: [{ type: "text", text: "你好" }, { type: "text", text: context }] }] }) }) + expect(JSON.parse(seen.at(-1).body).messages[0].content).toEqual([{ type: "text", text: "你好" }, { type: "text", text: adapted }]) +}) + test("system-role context adaptation is idempotent and preserves non-metadata paragraphs", async () => { const { l, seen } = await loaded() const context = "# Environment\nYou have been invoked in the following environment:\n - Platform: linux\n\nUser quoted: You are powered by the model custom-model.\n\nKeep the phrase not Claude in the instructions." @@ -347,7 +379,7 @@ test("requires a model-update preamble and complete token metadata before adapti update.replace("15000000", "unknown"), "Explain this quoted metadata:\n\n" + update, model + "\n\nQuoted token marker: 15000000 tokens left", - [{ type: "text", text: update }], + [{ type: "text", text: "Explain this quoted metadata:\n\n" + update }], ]) { const body = JSON.stringify({ system: droid, messages: [{ role: "system", content }] }) await l.fetch(url, { method: "POST", body })