diff --git a/packages/factory/README.md b/packages/factory/README.md index 138b307..6923e71 100644 --- a/packages/factory/README.md +++ b/packages/factory/README.md @@ -139,6 +139,10 @@ Standalone skill updates can instead start with the generated skill-list header and end with a token marker, for example when leaving auto mode. That complete form receives the same adaptation while retaining the skill descriptions and mode-change instructions. +The skill list can also come alone as a `system` message, with only its +entries: no header, no reminder wrapper and no token marker, as text blocks +or a string. A `system` message that opens with an entry gets the same +change to the known `update-config` line; everything else in it stays. When runtime notifications precede the skill update, the adapter recognizes the complete skill-header and bullet-list paragraphs inside a token-terminated bundle. It preserves MCP connection errors, other notifications, mode updates diff --git a/packages/factory/index.mjs b/packages/factory/index.mjs index 6eb4da0..3d12c30 100644 --- a/packages/factory/index.mjs +++ b/packages/factory/index.mjs @@ -658,6 +658,15 @@ const SKILL_REMINDER = /^\nThe following skills are available f 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") +// Claude Code also sends the skill list alone as a system message: the +// entries only, no header, no wrapper and no token tail, +// as text blocks or a string (#72). Only a system message that is such a +// 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) +} + // Claude 5 also sends its runtime metadata as a string-content system // message, instead of user-message reminders. Preserve that role and all // instructions. A model switch sends a separate update without the @@ -975,7 +984,7 @@ function anthropicBody(body) { for (const message of request.messages) { if (message?.role === "system" && typeof message.content === "string") { - const adapted = clientOpening(systemContext(message.content)) + const adapted = bareSkills(clientOpening(systemContext(message.content))) if (adapted !== message.content) { message.content = adapted changed = true @@ -988,7 +997,7 @@ function anthropicBody(body) { 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 = clientOpening(systemContext(block.text)) + const adapted = bareSkills(clientOpening(systemContext(block.text))) if (adapted !== block.text) { block.text = adapted changed = true diff --git a/packages/factory/request.test.mjs b/packages/factory/request.test.mjs index 5cce05c..500d1e1 100644 --- a/packages/factory/request.test.mjs +++ b/packages/factory/request.test.mjs @@ -1258,3 +1258,41 @@ test("preserves quoted, incomplete and hook-owned restored-context turns", async await l.fetch(url, { method: "POST", body }) expect(seen.at(-1).body).toBe(body) }) + +// #72 (Vigilans): Claude Code 2.1.x sends the skill list alone as a system +// message, entries only, as text blocks and later as a string. The +// reporter's reduced body, byte for byte; Factory answered it 403 and gave +// 200 with "not Claude" as "not the assistant". +const bareConfigSkill = '- 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. 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 files. Examples: "allow npm commands", "add bq permission to global", "move permission to user settings", "set DEBUG=true", "when claude stops show X". For simple settings like theme/model, suggest the /config command.' + +test("a skill list sent alone as a system message has the update-config line adapted (#72)", async () => { + const { l, seen } = await loaded() + const fixed = bareConfigSkill.replace("not Claude", "not the assistant") + const list = "- custom: Keep not Claude in this user's description.\n" + bareConfigSkill + "\n- other: Another skill." + for (const [text, want] of [[bareConfigSkill, fixed], [list, list.replace(bareConfigSkill, fixed)]]) { + for (const content of [[{ type: "text", text }], text]) { + const request = { model: "factory/claude-opus-5-5", max_tokens: 32, stream: true, messages: [ + { role: "user", content: [{ type: "text", text: "hi" }] }, + { role: "system", content }, + ] } + await l.fetch(url, { method: "POST", body: JSON.stringify(request) }) + const sent = JSON.parse(seen.at(-1).body) + expect(sent.messages[1].content).toEqual(typeof content === "string" ? want : [{ type: "text", text: want }]) + expect(sent.messages[0]).toEqual(request.messages[0]) + expect(JSON.stringify(sent)).not.toContain("not Claude, so") + } + } +}) + +test("only a system message that is a skill list is adapted (#72)", async () => { + const { l, seen } = await loaded() + for (const [role, text] of [ + ["user", bareConfigSkill], + ["system", "Explain this:\n" + bareConfigSkill], + ["system", bareConfigSkill.replace("- update-config:", "- custom-config:")], + ]) { + const request = { model: "factory/claude-opus-5-5", max_tokens: 32, messages: [{ role: "user", content: "hi" }, { role, content: text }] } + await l.fetch(url, { method: "POST", body: JSON.stringify(request) }) + expect(JSON.parse(seen.at(-1).body).messages[1].content).toBe(text) + } +})