From 4d6dcca173230ab5954fea1550cd91cf1c2920c2 Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 6 Oct 2026 08:08:15 +0800 Subject: [PATCH] =?UTF-8?q?factory=200.1.18:=20pi's=20opening=20sentence?= =?UTF-8?q?=20reaches=20Factory=20as=20its=20first=20half=20(yetone/magpie?= =?UTF-8?q?#952,=20mxzs123)=20=E2=80=94=20pi=201.0.4=20got=20403=20"Factor?= =?UTF-8?q?y:=20Forbidden"=20on=20DeepSeek=20(chat=20completions)=20and=20?= =?UTF-8?q?Claude=20(Messages)=20for=20"hi";=20the=20reporter's=20replays?= =?UTF-8?q?=20through=20magpie=20found=20the=20refused=20part:=20"You=20ar?= =?UTF-8?q?e=20an=20expert=20coding=20assistant=20operating=20inside=20pi,?= =?UTF-8?q?=20a=20coding=20agent=20harness."=20whole=20(either=20half=20pa?= =?UTF-8?q?sses,=20and=20pi's=20full=20request=20with=20it=20cut=20to=20"Y?= =?UTF-8?q?ou=20are=20an=20expert=20coding=20assistant."=20is=20answered?= =?UTF-8?q?=20on=20both=20routes).=20That=20sentence,=20at=20the=20start?= =?UTF-8?q?=20of=20a=20line=20of=20a=20system=20or=20developer=20prompt=20?= =?UTF-8?q?(chat=20completions'=20messages,=20Responses'=20instructions=20?= =?UTF-8?q?and=20input,=20Messages'=20system=20blocks=20and=20system=20mes?= =?UTF-8?q?sages),=20is=20cut=20so;=20the=20rest=20of=20pi's=20prompt,=20i?= =?UTF-8?q?ts=20tools=20and=20a=20user=20quoting=20the=20sentence=20go=20o?= =?UTF-8?q?n=20as=20they=20are.=20pi.test.mjs=20is=20new=20(pi's=20two=20c?= =?UTF-8?q?aptured=20shapes;=203=20of=20its=204=20fail=20on=200.1.17,=20th?= =?UTF-8?q?e=204th=20checks=20what=20is=20left=20alone);=20bun=20test=20(8?= =?UTF-8?q?5=20pass)=20and=20check.mjs=20factory=20pass.=20Not=20tried=20a?= =?UTF-8?q?gainst=20Factory=20itself=20(no=20Factory=20account=20here).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/factory/index.mjs | 51 +++++++++++++++++--- packages/factory/package.json | 2 +- packages/factory/pi.test.mjs | 87 +++++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 8 deletions(-) create mode 100644 packages/factory/pi.test.mjs diff --git a/packages/factory/index.mjs b/packages/factory/index.mjs index 444266d..58c332d 100644 --- a/packages/factory/index.mjs +++ b/packages/factory/index.mjs @@ -274,6 +274,39 @@ function explain(status, text) { const DROID_LINE = "You are Droid, an AI software engineering agent built by Factory." +// pi (@earendil-works/pi-coding-agent 1.0.4) opens its system prompt with +// a sentence Factory refuses whole, on the OpenAI and Anthropic routes +// alike (yetone/magpie#952): either half of it passes, and pi's full +// request with the sentence cut to "You are an expert coding assistant." +// is answered. Only that sentence, at the start of a line of a system or +// developer prompt, changes; the rest of pi's prompt goes on as it is. +const PI_OPENING = /(^|\n)You are an expert coding assistant operating inside pi, a coding agent harness\./ +const PI_COMPAT = "You are an expert coding assistant." +function clientOpening(text) { + return typeof text === "string" ? text.replace(PI_OPENING, (_, at) => at + PI_COMPAT) : text +} + +// promptOpenings adapts clientOpening in the system and developer messages +// of msgs (chat completions' messages, Responses' input), their string +// content or text parts: false when nothing changed. +function promptOpenings(msgs) { + let changed = false + for (const m of Array.isArray(msgs) ? msgs : []) { + if (!m || typeof m !== "object" || (m.role !== "system" && m.role !== "developer")) continue + if (typeof m.content === "string") { + const adapted = clientOpening(m.content) + if (adapted !== m.content) (m.content = adapted), (changed = true) + } else if (Array.isArray(m.content)) { + for (const p of m.content) { + if (!p || typeof p !== "object" || typeof p.text !== "string") continue + const adapted = clientOpening(p.text) + if (adapted !== p.text) (p.text = adapted), (changed = true) + } + } + } + return changed +} + // droidChat opens msgs' first system message with droid's line, or puts one // before them with the line alone: false when it opens so already. function droidChat(msgs) { @@ -326,11 +359,15 @@ function droidBody(path, body) { if (path.endsWith("/responses")) { const v = m.instructions if (v !== undefined && v !== null && typeof v !== "string") return body // not something droid sends - const ins = v ?? "" - if (ins.startsWith(DROID_LINE)) return body - m.instructions = ins.trim() === "" ? DROID_LINE : DROID_LINE + "\n" + ins + const opened = promptOpenings(m.input) + const ins = clientOpening(v ?? "") + if (!ins.startsWith(DROID_LINE)) m.instructions = ins.trim() === "" ? DROID_LINE : DROID_LINE + "\n" + ins + else if (ins !== v || opened) m.instructions = ins + else return body } else if (path.endsWith("/chat/completions")) { - if (!Array.isArray(m.messages) || !droidChat(m.messages)) return body + if (!Array.isArray(m.messages)) return body + const opened = promptOpenings(m.messages) + if (!droidChat(m.messages) && !opened) return body } else return body return JSON.stringify(m) } @@ -711,7 +748,7 @@ function anthropicBody(body) { block.text = DROID_LINE changed = true } else if (block?.type === "text") { - const adapted = systemModelLine(block.text) + const adapted = clientOpening(systemModelLine(block.text)) if (adapted !== block.text) { block.text = adapted changed = true @@ -739,7 +776,7 @@ function anthropicBody(body) { for (const message of request.messages) { if (message?.role === "system" && typeof message.content === "string") { - const adapted = systemContext(message.content) + const adapted = clientOpening(systemContext(message.content)) if (adapted !== message.content) { message.content = adapted changed = true @@ -752,7 +789,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 = systemContext(block.text) + const adapted = clientOpening(systemContext(block.text)) if (adapted !== block.text) { block.text = adapted changed = true diff --git a/packages/factory/package.json b/packages/factory/package.json index c5e175e..74e5ebb 100644 --- a/packages/factory/package.json +++ b/packages/factory/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-factory-auth", - "version": "0.1.17", + "version": "0.1.18", "description": "Sign in to a Factory (Droid) subscription from OpenCode or magpie", "type": "module", "main": "./index.mjs", diff --git a/packages/factory/pi.test.mjs b/packages/factory/pi.test.mjs new file mode 100644 index 0000000..b047de2 --- /dev/null +++ b/packages/factory/pi.test.mjs @@ -0,0 +1,87 @@ +// yetone/magpie#952: pi 1.0.4 through magpie got 403 "Factory: Forbidden" +// on DeepSeek (chat completions) and Claude (Messages) for "hi". The +// reporter's replays found the refused part: pi's opening sentence, whole +// (either half passes). The shapes below are pi's two requests as the +// reporter captured them: a developer message on chat completions, one +// system text block with cache_control on Messages. +import { afterEach, expect, test } from "bun:test" +import { FactoryAuthPlugin, _internal } from "./index.mjs" + +const { DROID_LINE } = _internal +const real = globalThis.fetch +afterEach(() => (globalThis.fetch = real)) + +const API = "https://api.factory.ai" +const chat = API + "/api/llm/o/v1/chat/completions" +const responses = API + "/api/llm/o/v1/responses" +const messages = API + "/api/llm/a/v1/messages?beta=true" + +const OPENING = "You are an expert coding assistant operating inside pi, a coding agent harness." +const REST = " You help users by reading files, executing commands, editing code, and writing new files.\n\nAvailable tools:\n- read: Read file contents" +const PI = OPENING + REST +const ADAPTED = "You are an expert coding assistant." + REST + +async function loaded() { + const sent = [] + globalThis.fetch = async (url, init = {}) => { + const u = new URL(String(url)) + if (u.pathname === "/api/cli/whoami") return new Response(JSON.stringify({ userId: "u", orgId: "fac_D", email: "d@example.com", region: "" })) + sent.push({ url: String(url), body: init.body }) + return new Response('{"id":"ok"}') + } + let saved = { type: "oauth", access: "tok", refresh: "r", expires: Date.now() + 3_600_000, accountId: "d@example.com", activeOrganizationId: "fac_D", region: "", premBaseHost: "" } + const hooks = await FactoryAuthPlugin({ client: { auth: { set: async ({ body }) => (saved = body) } } }) + const l = await hooks.auth.loader(async () => saved) + return async (url, body) => { + const s = JSON.stringify(body) + const res = await l.fetch(url, { method: "POST", headers: { "content-type": "application/json", "user-agent": "pi (darwin 24.6.0; arm64)" }, body: s }) + expect(res.status).toBe(200) + return JSON.parse(sent.at(-1).body) + } +} + +const tools = ["read", "bash", "edit", "write"].map((name) => ({ type: "function", function: { name, description: name, parameters: { type: "object", properties: {} } } })) + +test("pi's opening sentence is cut to its first half on chat completions, the rest of its prompt kept", async () => { + const send = await loaded() + const b = await send(chat, { model: "deepseek-v4.1-flash", messages: [{ role: "developer", content: PI }, { role: "user", content: "hi" }], tools, stream: true }) + expect(b.messages).toEqual([{ role: "system", content: DROID_LINE }, { role: "developer", content: ADAPTED }, { role: "user", content: "hi" }]) + expect(b.tools).toEqual(tools) + // as a system message too, and in text parts + let c = await send(chat, { model: "kimi-k3", messages: [{ role: "system", content: PI }, { role: "user", content: "hi" }] }) + expect(c.messages[0]).toEqual({ role: "system", content: DROID_LINE + "\n" + ADAPTED }) + c = await send(chat, { model: "kimi-k3", messages: [{ role: "developer", content: [{ type: "text", text: PI }] }, { role: "user", content: "hi" }] }) + expect(c.messages[1]).toEqual({ role: "developer", content: [{ type: "text", text: ADAPTED }] }) +}) + +test("pi's opening sentence is adapted in Messages' system block, its cache_control kept", async () => { + const send = await loaded() + const b = await send(messages, { model: "claude-opus-5-5", max_tokens: 100, system: [{ type: "text", text: PI, cache_control: { type: "ephemeral" } }], messages: [{ role: "user", content: [{ type: "text", text: "hi" }] }] }) + expect(b.system).toEqual([{ type: "text", text: DROID_LINE }, { type: "text", text: ADAPTED, cache_control: { type: "ephemeral" } }]) + expect(b.messages).toEqual([{ role: "user", content: [{ type: "text", text: "hi" }] }]) +}) + +test("pi's opening sentence is adapted in Responses' instructions and input", async () => { + const send = await loaded() + let b = await send(responses, { model: "gpt-6.1", instructions: PI, input: [{ role: "user", content: "hi" }] }) + expect(b.instructions).toBe(DROID_LINE + "\n" + ADAPTED) + b = await send(responses, { model: "gpt-6.1", input: [{ role: "developer", content: PI }, { role: "user", content: "hi" }] }) + expect(b.instructions).toBe(DROID_LINE) + expect(b.input[0]).toEqual({ role: "developer", content: ADAPTED }) + // droid's own instructions are untouched unless the sentence is in them + b = await send(responses, { model: "gpt-6.1", instructions: DROID_LINE + "\n" + PI, input: [{ role: "user", content: "hi" }] }) + expect(b.instructions).toBe(DROID_LINE + "\n" + ADAPTED) +}) + +test("the sentence quoted by the user, or only part of it, goes on as it is", async () => { + const send = await loaded() + const user = "Why does pi say: " + OPENING + let b = await send(chat, { model: "kimi-k3", messages: [{ role: "system", content: "Be brief." }, { role: "user", content: user }] }) + expect(b.messages[1]).toEqual({ role: "user", content: user }) + b = await send(messages, { model: "claude-opus-5-5", max_tokens: 100, system: [{ type: "text", text: "You are an expert coding assistant operating inside pi." }], messages: [{ role: "user", content: [{ type: "text", text: user }] }] }) + expect(b.system[1].text).toBe("You are an expert coding assistant operating inside pi.") + expect(b.messages[0].content[0].text).toBe(user) + // mid-line in a system prompt: not pi's opening + b = await send(messages, { model: "claude-opus-5-5", max_tokens: 100, system: [{ type: "text", text: "Quote: " + OPENING }], messages: [{ role: "user", content: [{ type: "text", text: "hi" }] }] }) + expect(b.system[1].text).toBe("Quote: " + OPENING) +})