diff --git a/packages/factory/README.md b/packages/factory/README.md index 01ca184..044f255 100644 --- a/packages/factory/README.md +++ b/packages/factory/README.md @@ -128,9 +128,14 @@ The fixed Claude identity is recognized as a complete first line too when magpie joins it with the following system instructions into one text block. For the built-in `update-config` skill, the adapter changes only the known -self-reference "not Claude" to "not the assistant" inside a complete -generated skill-list reminder. Its configuration instructions, other -skill descriptions, tools and caller text are preserved. +self-reference "not Claude" to "not the assistant" inside that skill's entry +in a generated skill list. Claude Code renders the entry two ways: as a +system message (`- update-config: …`) and, in the `claude-code-guide` agent's +own system prompt under `# User's Current Configuration`, with the slash that +invokes it (`- /update-config: …`). Both spellings are adapted, and only the +sentence Factory refuses changes, so the entry's name and the rest of the list +stay. Its configuration instructions, other skill descriptions, tools and +caller text are preserved. Claude 5 can send runtime metadata in string-content `system` messages instead of user-message reminders. The plugin adapts the known environment, diff --git a/packages/factory/index.mjs b/packages/factory/index.mjs index 1e13b88..cc0efac 100644 --- a/packages/factory/index.mjs +++ b/packages/factory/index.mjs @@ -953,11 +953,29 @@ const CLAUDE_IDENTITIES = new Set([ const ENV_REMINDER = /^\n# Environment\nYou have been invoked in the following environment:[ \t]*\n(?: {1,2}- [^\n]*\n)+<\/system-reminder>$/ const MODEL_REMINDER = /^\nYou are powered by the model (?:named )?[^\n<>]+\.\n<\/system-reminder>$/ const SKILL_REMINDER = /^\nThe following skills are available for use with the Skill tool:\n\n(?:(?!<\/?system-reminder>)[\s\S])*\n<\/system-reminder>$/ -// Factory also refuses this fixed self-reference in Claude Code 2.1.287's -// built-in update-config description. Keep its instructions and the rest -// of the skill list intact; only the known line in a complete listing changes. -const CONFIG_SKILL_METADATA = '- update-config: Use this skill to configure the Claude Code harness via settings.json. Automated behaviors ("from now on when X", "each time X", "whenever X", "before/after X") require hooks configured in settings.json - the harness executes these, not Claude, so memory/preferences cannot fulfill them.' -const CONFIG_SKILL_COMPAT = CONFIG_SKILL_METADATA.replace("not Claude", "not the assistant") +// Factory also refuses this fixed self-reference in Claude Code's built-in +// update-config description. Keep its instructions and the rest of the skill +// list intact; only that entry changes. +// +// Claude Code renders the entry two ways: as a system message ("- update- +// config: …") and, in the claude-code-guide agent's own system prompt under +// "# User's Current Configuration", with the slash that invokes it +// ("- /update-config: …"). Match the entry by its name in either spelling and +// change only the refused sentence in it, so a rewording of the rest of the +// description cannot defeat the match. The sentence is the built-in +// description's own text, so no other entry carries it by accident. +const CONFIG_SKILL_ENTRY = /^- \/?update-config: / +const CONFIG_SKILL_REFUSED = "the harness executes these, not Claude, so memory/preferences cannot fulfill them." +const CONFIG_SKILL_SAFE = CONFIG_SKILL_REFUSED.replace("not Claude", "not the assistant") + +// configSkillText repairs that entry wherever the text carries a skill list. +function configSkillText(text) { + if (!text.includes(CONFIG_SKILL_REFUSED)) return text + return text + .split("\n") + .map((line) => (CONFIG_SKILL_ENTRY.test(line) ? line.replace(CONFIG_SKILL_REFUSED, CONFIG_SKILL_SAFE) : line)) + .join("\n") +} // Claude Code also sends the skill list alone as a system message: the // entries only, no header, no wrapper and no token tail, @@ -965,7 +983,7 @@ const CONFIG_SKILL_COMPAT = CONFIG_SKILL_METADATA.replace("not Claude", "not the // list (it opens with an entry) changes, and in it only the known line. function bareSkills(text) { if (!text.startsWith("- ")) return text - return ("\n" + text).replace("\n" + CONFIG_SKILL_METADATA, "\n" + CONFIG_SKILL_COMPAT).slice(1) + return configSkillText(text) } // Claude 5 also sends its runtime metadata as a string-content system @@ -1018,7 +1036,7 @@ function announcedSkills(text) { const list = parts[i + 1] // Skill descriptions can contain continuation lines. if (!list.startsWith("- ")) continue - parts[i + 1] = ("\n" + list).replace("\n" + CONFIG_SKILL_METADATA, "\n" + CONFIG_SKILL_COMPAT).slice(1) + parts[i + 1] = configSkillText(list) } return parts.join("\n\n") } @@ -1069,7 +1087,7 @@ function announcedContext(text, start = 1) { } else if (SYSTEM_MODEL_UPDATE.test(part)) { parts[i] = systemModelLine(part) } else if (parts[i - 1] === "The following skills are available for use with the Skill tool:") { - parts[i] = ("\n" + part).replace("\n" + CONFIG_SKILL_METADATA, "\n" + CONFIG_SKILL_COMPAT).slice(1) + parts[i] = configSkillText(part) } } return parts.join("\n\n") @@ -1123,7 +1141,7 @@ function generatedContext(text) { const skills = out.indexOf(skillHeader) if (skills >= 0) { const start = skills + skillHeader.length - 1 - out = out.slice(0, start) + out.slice(start).replace("\n" + CONFIG_SKILL_METADATA, "\n" + CONFIG_SKILL_COMPAT) + out = out.slice(0, start) + configSkillText(out.slice(start)) } return out } @@ -1302,7 +1320,7 @@ function claudeIdentity(text) { // promptText adapts a system prompt's text. function promptText(text) { - return clientOpening(systemModelLine(claudeIdentity(text))) + return configSkillText(clientOpening(systemModelLine(claudeIdentity(text)))) } // systemMessageText adapts a system message's text after the prompt. @@ -1327,7 +1345,7 @@ function userBlockText(text) { .replace("The exact model ID is", "Model ID:") .replace("Assistant knowledge cutoff is", "Model knowledge cutoff:") } - if (SKILL_REMINDER.test(text)) return text.replace("\n" + CONFIG_SKILL_METADATA, "\n" + CONFIG_SKILL_COMPAT) + if (SKILL_REMINDER.test(text)) return configSkillText(text) return text } diff --git a/packages/factory/package.json b/packages/factory/package.json index b2439b6..91522ca 100644 --- a/packages/factory/package.json +++ b/packages/factory/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-factory-auth", - "version": "0.1.32", + "version": "0.1.33", "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 f48db3a..4986f15 100644 --- a/packages/factory/request.test.mjs +++ b/packages/factory/request.test.mjs @@ -1541,6 +1541,17 @@ test("a skill list sent alone as a system message has the update-config line ada } }) +test("recognizes the update-config entry with or without the invoking slash (#72)", async () => { + const { l, seen } = await loaded() + const slashed = bareConfigSkill.replace("- update-config:", "- /update-config:") + const want = (text) => text.replace("not Claude", "not the assistant") + for (const text of [bareConfigSkill, slashed]) { + const request = { model: "factory/claude-opus-5-5", max_tokens: 32, messages: [{ role: "user", content: "hi" }, { role: "system", content: text }] } + await l.fetch(url, { method: "POST", body: JSON.stringify(request) }) + expect(JSON.parse(seen.at(-1).body).messages[1].content).toBe(want(text)) + } +}) + test("only a system message that is a skill list is adapted (#72)", async () => { const { l, seen } = await loaded() for (const [role, text] of [ @@ -1572,3 +1583,82 @@ test("a streamed request goes without context_management, an unstreamed one keep await l.fetch(url, { method: "POST", body: JSON.stringify({ ...request, stream: false }) }) expect(JSON.parse(seen.at(-1).body).context_management).toEqual(request.context_management) }) + +// Claude Code 2.1.302's built-in claude-code-guide agent builds its own +// system prompt and appends a "# User's Current Configuration" section to it, +// rendering each custom skill as "- /: ". The system +// message that carries the same list elsewhere has no slash. Both spellings +// hold the same update-config description, whose self-reference Factory +// refuses: the agent's first request came back 403 "Factory: Forbidden" until +// that one phrase was reworded. The description below is byte-for-byte the one +// the agent sent, and the section is the template the CLI builds it from. +const configDescription = + 'Use this skill to configure the Claude Code harness via settings.json. Automated behaviors ("from now on when X", "each time X", "whenever X", "before/after X") require hooks configured in settings.json - the harness executes these, not Claude, so memory/preferences cannot fulfill them. Also use for: permissions ("allow X", "add permission", "move permission to"), env vars ("set X=Y"), hook troubleshooting, or any changes to settings.json/settings.local.json. Examples: "allow npm commands", "add bq permission to global settings", "move permission to user settings", "set DEBUG=true", "when claude stops show X". For simple settings like theme/model, suggest the /config command.' +const guidePrompt = [ + "You are the Claude guide agent. Your primary responsibility is helping users understand and use Claude Code, the Claude Agent SDK, and the Claude API effectively.", + "", + "---", + "", + "# User's Current Configuration", + "", + "The user has the following custom setup in their environment:", + "", + "**Available custom skills in this project:**", + "- /update-config: " + configDescription, + "- /custom: Keep not Claude in this user's description.", + "", + "**Configured MCP servers:**", + "- lcu", + "", + "When answering questions, consider these configured features and proactively suggest them when relevant.", +].join("\n") + +test("adapts the update-config self-reference in a system prompt's configuration section", async () => { + const { l, seen } = await loaded() + // The entry is named the same either way, so both spellings adapt wherever + // the list arrives; only the slash rendering is what Claude Code sends today. + for (const text of [guidePrompt, guidePrompt.replace("- /update-config:", "- update-config:")]) { + const system = [ + { type: "text", text: "You are a Claude agent, built on Anthropic's Claude Agent SDK." }, + { type: "text", text, cache_control: { type: "ephemeral" } }, + ] + const body = JSON.stringify({ model: "factory/claude-haiku-5-5", max_tokens: 32, stream: true, system, messages: [{ role: "user", content: "hi" }] }) + await l.fetch(url, { method: "POST", body }) + const sent = JSON.parse(seen.at(-1).body) + expect(sent.system).toEqual([ + { type: "text", text: droid }, + { ...system[1], text: text.replace(configDescription, configDescription.replace("not Claude", "not the assistant")) }, + ]) + expect(JSON.stringify(sent)).not.toContain("not Claude, so") + } +}) + +test("leaves the refused sentence in a system prompt outside the update-config entry alone", async () => { + const { l, seen } = await loaded() + const sentence = "the harness executes these, not Claude, so memory/preferences cannot fulfill them." + const system = [ + { type: "text", text: droid }, + { type: "text", text: [ + "You are the Claude guide agent.", + "", + "The user's own skill description reads: " + sentence, + "", + "**Available custom skills in this project:**", + "- /custom-config: " + sentence, + "- /update-config-help: " + sentence, + ].join("\n") }, + ] + const body = JSON.stringify({ model: "factory/claude-haiku-5-5", max_tokens: 32, system, messages: [{ role: "user", content: "hi" }] }) + await l.fetch(url, { method: "POST", body }) + expect(seen[0].body).toBe(body) +}) + +test("adapting a system prompt's configuration section is idempotent", async () => { + const { l, seen } = await loaded() + const body = JSON.stringify({ model: "factory/claude-haiku-5-5", max_tokens: 32, system: [{ type: "text", text: guidePrompt }], messages: [{ role: "user", content: "hi" }] }) + await l.fetch(url, { method: "POST", body }) + expect(seen[0].body).not.toBe(body) + expect(JSON.parse(seen[0].body).system.at(-1).text).toContain("not the assistant, so memory/preferences") + await l.fetch(url, { method: "POST", body: seen[0].body }) + expect(seen[1].body).toBe(seen[0].body) +})