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.
This commit is contained in:
yetone
2026-10-03 16:30:42 +08:00
parent bf7ced3156
commit 16ec1b8ba9
3 changed files with 50 additions and 4 deletions
+15 -1
View File
@@ -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) {
+1 -1
View File
@@ -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",
+34 -2
View File
@@ -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\n<total_tokens>10000 tokens left</total_tokens>"
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: <total_tokens>15000000 tokens left</total_tokens>",
[{ 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 })