factory: adapt Claude 5 system-role runtime metadata (#8)

This commit is contained in:
XIQI
2026-10-02 23:03:32 +08:00
committed by GitHub
parent e2c72b2ae7
commit a51d4d2ec7
4 changed files with 92 additions and 3 deletions
+8 -2
View File
@@ -86,6 +86,11 @@ 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.
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.
Use magpie's Claude Code integration to select the Factory provider.
magpie manages the provider-specific client settings, including capability
and permission configuration. Fields such as `safeguards` and
@@ -97,5 +102,6 @@ The same metadata adaptation applies to `/messages` and
inference. Serving the counting endpoint still depends on the host and
upstream. Model and feature availability depend on the Factory account,
organization region and upstream API. Connectivity has been verified with
Sonnet 4.6, Sonnet 5.5 and Opus 5.5, and a two-turn Read tool call with
Sonnet 4.6 using Claude Code's default tool set.
Sonnet 4.6, Sonnet 5.5 and Opus 5.5. Two-turn Read tool calls with Claude
Code's default tool set have been verified with Sonnet 4.6 and Sonnet 5.5,
including Sonnet 5.5 selected through a routing group.
+31
View File
@@ -589,6 +589,29 @@ const SKILL_REMINDER = /^<system-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 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.
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
function systemContext(text) {
if (!SYSTEM_ENV_CONTEXT.test(text)) return text
let out = text.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) {
const start = skills + skillHeader.length - 1
out = out.slice(0, start) + out.slice(start).replace("\n" + CONFIG_SKILL_METADATA, "\n" + CONFIG_SKILL_COMPAT)
}
return out
}
function anthropicBody(body) {
let request
try {
@@ -646,6 +669,14 @@ function anthropicBody(body) {
if (changed) request.system = kept
for (const message of request.messages) {
if (message?.role === "system" && typeof message.content === "string") {
const adapted = systemContext(message.content)
if (adapted !== message.content) {
message.content = adapted
changed = true
}
continue
}
if (message?.role !== "user" || !Array.isArray(message.content)) continue
for (const block of message.content) {
if (block?.type !== "text" || typeof block.text !== "string") continue
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-factory-auth",
"version": "0.1.7",
"version": "0.1.8",
"description": "Sign in to a Factory (Droid) subscription from OpenCode or magpie",
"type": "module",
"main": "./index.mjs",
+52
View File
@@ -247,3 +247,55 @@ test("preserves a skill listing inside tool results and is idempotent after adap
await l.fetch(url, { method: "POST", body: seen[0].body })
expect(seen[1].body).toBe(seen[0].body)
})
test("adapts Claude 5 system-role runtime context without changing its role or instructions", async () => {
const { l, seen } = await loaded()
const environment = "# Environment\nYou have been invoked in the following environment: \n - Primary working directory: /tmp/project\n - Additional working directories:\n - /extra/one\n - C:\\extra two\n - Platform: darwin\n"
const skills = "The following skills are available for use with the Skill tool:\n\n" + configSkill + "\n- custom: Keep not Claude in this user's description."
for (const model of ["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.", "You are powered by the model group/claude-default."]) {
const context = environment + "\n" + model + "\n\n" + skills + "\n\nToday's date is 2026-10-02.\n\nKeep these session instructions verbatim."
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("You are powered by the model", "Current model:")
.replace("The exact model ID is", "Model ID:")
.replace("Assistant knowledge cutoff is", "Model knowledge cutoff:")
.replace("not Claude", "not the assistant")
const request = {
system: [{ type: "text", text: droid }],
messages: [{ role: "user", content: [{ type: "text", text: "Reply OK.", cache_control: { type: "ephemeral" } }] }, { role: "system", content: context }],
tools: [{ name: "Read", input_schema: { type: "object", properties: { path: { type: "string" } } } }],
thinking: { type: "adaptive" }, stream: true,
}
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: [request.messages[0], { role: "system", content: adapted }] })
}
}
})
test("leaves ordinary system messages and incomplete context outside the generated shape unchanged", async () => {
const { l, seen } = await loaded()
const context = "# Environment\nYou have been invoked in the following environment:\n - Platform: linux\n\nYou are powered by the model custom-model."
for (const message of [
{ role: "user", content: context },
{ role: "assistant", content: context },
{ 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 }] },
]) {
const body = JSON.stringify({ system: droid, messages: [message] })
await l.fetch(url, { method: "POST", body })
expect(seen.at(-1).body).toBe(body)
}
})
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."
await l.fetch(url, { method: "POST", body: JSON.stringify({ system: droid, messages: [{ role: "system", content: context }] }) })
expect(JSON.parse(seen[0].body).messages[0].content).toBe(context.replace("# Environment", "# Runtime context").replace("You have been invoked in the following environment:", "The session environment is:"))
await l.fetch(url, { method: "POST", body: seen[0].body })
expect(seen[1].body).toBe(seen[0].body)
})