factory 0.1.18: pi's opening sentence reaches Factory as its first half (yetone/magpie#952, mxzs123) — pi 1.0.4 got 403 "Factory: Forbidden" on DeepSeek (chat completions) and Claude (Messages) for "hi"; the reporter's replays through magpie found the refused part: "You are an expert coding assistant operating inside pi, a coding agent harness." whole (either half passes, and pi's full request with it cut to "You are an expert coding assistant." is answered on both routes). That sentence, at the start of a line of a system or developer prompt (chat completions' messages, Responses' instructions and input, Messages' system blocks and system messages), is cut so; the rest of pi's prompt, its tools and a user quoting the sentence go on as they are. pi.test.mjs is new (pi's two captured shapes; 3 of its 4 fail on 0.1.17, the 4th checks what is left alone); bun test (85 pass) and check.mjs factory pass. Not tried against Factory itself (no Factory account here).
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
Reference in New Issue
Block a user