diff --git a/packages/factory/README.md b/packages/factory/README.md index e14577a..0892168 100644 --- a/packages/factory/README.md +++ b/packages/factory/README.md @@ -90,6 +90,11 @@ Claude 5 can send runtime metadata in string-content `system` messages instead of user-message reminders. The plugin adapts the known environment, model and configuration-skill metadata in that shape too, preserving the message role, environment values and session instructions. +After `/model` switches, Claude Code can send model-only system updates +without the initial environment paragraph. Updates beginning with the known +model paragraph and containing complete generated token metadata are +adapted too, including accumulated switches when resuming a conversation. +Token budgets, permission instructions and conversation history are preserved. Use magpie's Claude Code integration to select the Factory provider. magpie manages the provider-specific client settings, including capability diff --git a/packages/factory/index.mjs b/packages/factory/index.mjs index 14d51af..5fa084e 100644 --- a/packages/factory/index.mjs +++ b/packages/factory/index.mjs @@ -591,18 +591,24 @@ const CONFIG_SKILL_COMPAT = CONFIG_SKILL_METADATA.replace("not Claude", "not the // Claude 5 also sends its runtime metadata as a string-content system // message, instead of user-message reminders. Preserve that role and all -// instructions; only the known environment paragraph opens this adapter. +// instructions. A model switch sends a separate update without the +// environment paragraph; require its opening model paragraph and complete +// generated token context before adapting that shape. const SYSTEM_ENV_CONTEXT = /^# Environment\nYou have been invoked in the following environment:[ \t]*\n(?: {1,2}- [^\n]*\n)+(?=\n|$)/ const SYSTEM_MODEL_CONTEXT = /(^|\n\n)You are powered by the model (?:named )?[^\n<>]+\.(?=\n\n|$)/g +const SYSTEM_MODEL_UPDATE = /^You are powered by the model (?:named )?[^\n<>]+\.(?=\n\n|$)/ +const SYSTEM_TOKEN_CONTEXT = /(?:^|\n\n)\d+ tokens left<\/total_tokens>(?=\n\n|$)/ function systemContext(text) { - if (!SYSTEM_ENV_CONTEXT.test(text)) return text - let out = text.replace("# Environment", "# Runtime context") + const environment = SYSTEM_ENV_CONTEXT.test(text) + if (!environment && !(SYSTEM_MODEL_UPDATE.test(text) && SYSTEM_TOKEN_CONTEXT.test(text))) return text + let out = text.replace(SYSTEM_MODEL_CONTEXT, (paragraph) => paragraph + .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:")) + if (!environment) return out + out = out.replace("# Environment", "# Runtime context") .replace("You have been invoked in the following environment:", "The session environment is:") - .replace(SYSTEM_MODEL_CONTEXT, (paragraph) => paragraph - .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:")) const skillHeader = "\n\nThe following skills are available for use with the Skill tool:\n\n" const skills = out.indexOf(skillHeader) if (skills >= 0) { diff --git a/packages/factory/package.json b/packages/factory/package.json index d5a54fb..bb1fcd3 100644 --- a/packages/factory/package.json +++ b/packages/factory/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-factory-auth", - "version": "0.1.8", + "version": "0.1.9", "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 e67677d..5551001 100644 --- a/packages/factory/request.test.mjs +++ b/packages/factory/request.test.mjs @@ -299,3 +299,63 @@ test("system-role context adaptation is idempotent and preserves non-metadata pa await l.fetch(url, { method: "POST", body: seen[0].body }) expect(seen[1].body).toBe(seen[0].body) }) + +test("adapts accumulated model-switch updates when continuing a Claude 5 session", async () => { + const { l, seen } = await loaded() + const tokenContext = "15000000 tokens left" + const instructions = "## Auto Mode Active\n\nKeep all permission instructions verbatim.\n\nUser quoted: You are powered by the model custom-model." + const update = [ + "You are powered by the model named Sonnet 5.5. The exact model ID is factory/claude-sonnet-5-5. Assistant knowledge cutoff is June 2026.", + tokenContext, + "You are powered by the model group/claude-default.", + instructions, + tokenContext, + "You are powered by the model named Opus 5.5. The exact model ID is factory/claude-opus-5-5. Assistant knowledge cutoff is June 2026.", + "The following agent types are no longer available:\n- claude-code-guide", + tokenContext, + "USD budget: $0/$0.4; $0.4 remaining", + ].join("\n\n") + const messages = [ + { role: "user", content: "Continue the existing conversation." }, + { role: "system", content: "# Environment update\n - Primary working directory: /tmp/project (was /tmp/old)\n\n" + tokenContext }, + { role: "assistant", content: [{ type: "tool_use", id: "read_1", name: "Read", input: { path: "/tmp/project/proof" } }] }, + { role: "user", content: [{ type: "tool_result", tool_use_id: "read_1", content: update, cache_control: { type: "ephemeral" } }] }, + { role: "system", content: update }, + ] + const request = { system: droid, messages, tools: [{ name: "Read", input_schema: { type: "object" } }], stream: true } + const adapted = update.replaceAll("\n\nYou are powered by the model named", "\n\nCurrent model name:") + .replace(/^You are powered by the model named/, "Current model name:") + .replace("\n\nYou are powered by the model group/", "\n\nCurrent model: group/") + .replaceAll("The exact model ID is", "Model ID:") + .replaceAll("Assistant knowledge cutoff is", "Model knowledge cutoff:") + for (const endpoint of [url, url + "/count_tokens"]) { + await l.fetch(endpoint, { method: "POST", body: JSON.stringify(request) }) + expect(JSON.parse(seen.at(-1).body)).toEqual({ ...request, messages: [...messages.slice(0, -1), { role: "system", content: adapted }] }) + const once = seen.at(-1).body + await l.fetch(endpoint, { method: "POST", body: once }) + expect(seen.at(-1).body).toBe(once) + } +}) + +test("requires a model-update preamble and complete token metadata before adapting standalone system text", async () => { + const { l, seen } = await loaded() + const model = "You are powered by the model group/claude-default." + const update = model + "\n\n15000000 tokens left" + for (const content of [ + model, + update.replace("", ""), + update.replace("15000000", "unknown"), + "Explain this quoted metadata:\n\n" + update, + model + "\n\nQuoted token marker: 15000000 tokens left", + [{ type: "text", text: update }], + ]) { + const body = JSON.stringify({ system: droid, messages: [{ role: "system", content }] }) + await l.fetch(url, { method: "POST", body }) + expect(seen.at(-1).body).toBe(body) + } + for (const role of ["user", "assistant"]) { + const body = JSON.stringify({ system: droid, messages: [{ role, content: update }] }) + await l.fetch(url, { method: "POST", body }) + expect(seen.at(-1).body).toBe(body) + } +})