factory 0.1.12: Claude Code with a global CLAUDE.md or a SessionStart hook, and Claude Desktop, reach Factory again (yetone/magpie#634)
Factory refused three generated phrases the adapter left as they were: - the instructions reminder's "(user's private global instructions for all projects)" naming ~/.claude/CLAUDE.md, now "(global instructions)" in that reminder only; - Claude 5's runtime context after a SessionStart hook's output, as a system message or folded into the user's turn: the context after the hook is adapted as it is alone, the hook's output left as it is; - the model line inside a system prompt (Claude Desktop's), adapted as in a reminder. Three tests cover them and fail without the change; bun test passes.
This commit is contained in:
@@ -604,7 +604,20 @@ const SYSTEM_MODEL_CONTEXT = /(^|\n\n)You are powered by the model (?:named )?[^
|
||||
const SYSTEM_MODEL_UPDATE = /^You are powered by the model (?:named )?[^\n<>]+\.(?=\n\n|$)/
|
||||
const SYSTEM_ENV_UPDATE = /^# Environment update\n(?: {1,2}- [^\n]*\n)+(?=\n|$)/
|
||||
const SYSTEM_TOKEN_CONTEXT = /(?:^|\n\n)<total_tokens>\d+ tokens left<\/total_tokens>(?=\n\n|$)/
|
||||
// A SessionStart hook's output comes ahead of the generated context in the
|
||||
// same message (Claude Code 2.1.288 with a hook installed, #634): the
|
||||
// context after it is adapted as it would be on its own, the hook's output
|
||||
// is left as it is.
|
||||
const HOOK_OUTPUT = /^SessionStart:[^\n]* hook success:/
|
||||
const HOOK_CONTEXT = "\n# Environment\nYou have been invoked in the following environment:"
|
||||
function systemContext(text) {
|
||||
if (HOOK_OUTPUT.test(text)) {
|
||||
const at = text.indexOf(HOOK_CONTEXT)
|
||||
return at < 0 ? text : text.slice(0, at + 1) + generatedContext(text.slice(at + 1))
|
||||
}
|
||||
return generatedContext(text)
|
||||
}
|
||||
function generatedContext(text) {
|
||||
const environment = SYSTEM_ENV_CONTEXT.test(text)
|
||||
if (!environment && !((SYSTEM_MODEL_UPDATE.test(text) || SYSTEM_ENV_UPDATE.test(text)) && SYSTEM_TOKEN_CONTEXT.test(text))) return text
|
||||
let out = text.replace(SYSTEM_MODEL_CONTEXT, (paragraph) => paragraph
|
||||
@@ -624,6 +637,22 @@ function systemContext(text) {
|
||||
return out
|
||||
}
|
||||
|
||||
// The model line inside a system prompt (Claude Desktop's, #634) is the
|
||||
// same generated sentence as in a reminder; only that line changes.
|
||||
const SYSTEM_MODEL_LINE = /(^|\n)You are powered by the model [^\n<>]+/g
|
||||
function systemModelLine(text) {
|
||||
return text.replace(SYSTEM_MODEL_LINE, (line) => line
|
||||
.replace("You are powered by the model named", "Current model name:")
|
||||
.replace("You are powered by the model", "Current model:")
|
||||
.replace("The exact model ID is", "Model ID:")
|
||||
.replace("Assistant knowledge cutoff is", "Model knowledge cutoff:"))
|
||||
}
|
||||
|
||||
// The reminder carrying CLAUDE.md files names the global one with a phrase
|
||||
// Factory refuses; only that phrase in the generated reminder changes.
|
||||
const INSTRUCTIONS_REMINDER = "<system-reminder>\nCodebase and user instructions are shown below"
|
||||
const GLOBAL_INSTRUCTIONS = "(user's private global instructions for all projects)"
|
||||
|
||||
// Factory also refuses these fixed client phrases when they are quoted in
|
||||
// tool output, e.g. while reading this adapter's source. Keep the original
|
||||
// text recoverable: a JSON string with explicit decoding instructions,
|
||||
@@ -676,6 +705,12 @@ function anthropicBody(body) {
|
||||
if (block?.type === "text" && CLAUDE_IDENTITIES.has(block.text)) {
|
||||
block.text = DROID_LINE
|
||||
changed = true
|
||||
} else if (block?.type === "text") {
|
||||
const adapted = systemModelLine(block.text)
|
||||
if (adapted !== block.text) {
|
||||
block.text = adapted
|
||||
changed = true
|
||||
}
|
||||
}
|
||||
kept.push(block)
|
||||
}
|
||||
@@ -728,7 +763,17 @@ function anthropicBody(body) {
|
||||
continue
|
||||
}
|
||||
if (block?.type !== "text" || typeof block.text !== "string") continue
|
||||
if (ENV_REMINDER.test(block.text)) {
|
||||
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)) {
|
||||
// a system message folded into the user's turn on its way here
|
||||
const adapted = systemContext(block.text)
|
||||
if (adapted !== block.text) {
|
||||
block.text = adapted
|
||||
changed = true
|
||||
}
|
||||
} else if (ENV_REMINDER.test(block.text)) {
|
||||
block.text = block.text.replace("# Environment", "# Runtime context")
|
||||
.replace("You have been invoked in the following environment:", "The session environment is:")
|
||||
changed = true
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-factory-auth",
|
||||
"version": "0.1.11",
|
||||
"version": "0.1.12",
|
||||
"description": "Sign in to a Factory (Droid) subscription from OpenCode or magpie",
|
||||
"type": "module",
|
||||
"main": "./index.mjs",
|
||||
|
||||
@@ -419,3 +419,45 @@ test("leaves ordinary tool results and fixed phrases outside tool-result content
|
||||
await l.fetch(url, { method: "POST", body })
|
||||
expect(seen[0].body).toBe(body)
|
||||
})
|
||||
|
||||
// #634: shapes Claude Code 2.1.288 and Claude Desktop send that Factory refused.
|
||||
test("adapts runtime context after a SessionStart hook's output, as a system message or folded into the user's turn", async () => {
|
||||
const { l, seen } = await loaded()
|
||||
const hook = "SessionStart:startup hook success: Memory loaded.\n# Environment notes from the hook stay as they are.\n"
|
||||
const context = "# Environment\nYou have been invoked in the following environment: \n - Primary working directory: /tmp/project\n - Platform: darwin\n\nYou are powered by the model named Sonnet 5.5. The exact model ID is factory/claude-sonnet-5-5.\n\nThe following skills are available for use with the Skill tool:\n\n" + configSkill
|
||||
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("not Claude", "not the assistant")
|
||||
const text = hook + "\n" + context
|
||||
for (const message of [{ role: "system", content: text }, { role: "user", content: [{ type: "text", text: "Reply OK." }, { type: "text", text }] }]) {
|
||||
await l.fetch(url, { method: "POST", body: JSON.stringify({ system: droid, messages: [message] }) })
|
||||
const got = JSON.parse(seen.at(-1).body).messages[0]
|
||||
const out = typeof got.content === "string" ? got.content : got.content[1].text
|
||||
expect(out).toBe(hook + "\n" + adapted)
|
||||
await l.fetch(url, { method: "POST", body: seen.at(-1).body })
|
||||
expect(seen.at(-1).body).toBe(seen.at(-2).body)
|
||||
}
|
||||
// a hook's output with no generated context after it is left alone
|
||||
const body = JSON.stringify({ system: droid, messages: [{ role: "system", content: hook + "\nYou are powered by the model named X." }] })
|
||||
await l.fetch(url, { method: "POST", body })
|
||||
expect(seen.at(-1).body).toBe(body)
|
||||
})
|
||||
|
||||
test("renames the global CLAUDE.md in the instructions reminder only", async () => {
|
||||
const { l, seen } = await loaded()
|
||||
const reminder = "<system-reminder>\nCodebase and user instructions are shown below. Be sure to adhere to these instructions.\n\nContents of /home/u/.claude/CLAUDE.md (user's private global instructions for all projects):\n\nUse tabs.\n</system-reminder>"
|
||||
const pasted = "Why does Claude Code write (user's private global instructions for all projects)?"
|
||||
await l.fetch(url, { method: "POST", body: JSON.stringify({ system: droid, messages: [{ role: "user", content: [{ type: "text", text: reminder }, { type: "text", text: pasted }] }] }) })
|
||||
const content = JSON.parse(seen.at(-1).body).messages[0].content
|
||||
expect(content[0].text).toBe(reminder.replace("(user's private global instructions for all projects)", "(global instructions)"))
|
||||
expect(content[1].text).toBe(pasted)
|
||||
})
|
||||
|
||||
test("adapts the model line inside Claude Desktop's system prompt", async () => {
|
||||
const { l, seen } = await loaded()
|
||||
const prompt = "<application_details>\nClaude Desktop.\n</application_details>\nYou are powered by the model named Sonnet 5.5. The exact model ID is factory/claude-sonnet-5-5.\nKeep the rest verbatim."
|
||||
await l.fetch(url, { method: "POST", body: JSON.stringify({ system: [{ type: "text", text: "You are a Claude agent, built on Anthropic's Claude Agent SDK." }, { type: "text", text: prompt }], messages: [{ role: "user", content: "hi" }] }) })
|
||||
expect(JSON.parse(seen.at(-1).body).system).toEqual([{ type: "text", text: droid }, { type: "text", text: prompt.replace("You are powered by the model named", "Current model name:").replace("The exact model ID is", "Model ID:") }])
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user