factory 0.1.40: a usage policy refusal is a 400 with Factory's code, not a 403 (#494)

sweanng (yetone/magpie, Discord #general, 10-29): ZCode on Factory's GLM
5.3 got Factory's usage policy check refusing the conversation (403,
code content_policy_rejected). The plugin handed it on as a 403
permission_error with code null, so magpie passed the 403 through and
ZCode showed "Provider authentication failed" (reason=auth_failed
status=403), as if the account were broken.

The plugin now answers that refusal (only content_policy_rejected
without an x-factory-upstream-error header, as droid 0.245.0 tells it)
as a 400 invalid_request_error with code content_policy_rejected kept on
the error, on both the Anthropic (/llm/a) and OpenAI (/llm/o) routes,
with Factory's words and the "start a new session" advice as before.
Every other 403 stays a 403 permission_error. magpie reads the code as
a refusal of the request (yetone/magpie, same day).

Run (on mote): HOME=$(mktemp -d) bun test packages/factory: 178 pass,
0 fail. bun scripts/check.mjs: ok.
Broken by hand: with index.mjs back at main, content-policy.test.mjs
fails 3 of 6 (both 403 shapes on /llm/a and the new /llm/o case).
Not tried: a real Factory refusal; the refusal's body comes from
doubl4door's and sweanng's reports, not a live capture.
This commit is contained in:
yetone
2026-10-29 17:51:44 +08:00
committed by GitHub
parent 77c257c0c6
commit 374b1fe186
3 changed files with 41 additions and 7 deletions
+26 -4
View File
@@ -24,7 +24,7 @@ const noOrg = {
premBaseHost: "",
}
async function refused(body, headers = {}) {
async function refused(body, headers = {}, url = messages) {
const seen = []
let asked = 0
globalThis.fetch = async (url) => {
@@ -37,8 +37,8 @@ async function refused(body, headers = {}) {
let saved = { ...noOrg }
const hooks = await FactoryAuthPlugin({ client: { auth: { set: async ({ body }) => (saved = body) } } })
const l = await hooks.auth.loader(async () => saved)
const req = `{"model":"claude-opus-5-5","max_tokens":10,"messages":[{"role":"user","content":"hi"}]}`
const res = await l.fetch(messages, { method: "POST", headers: { "content-type": "application/json" }, body: req })
const req = url === messages ? `{"model":"claude-opus-5-5","max_tokens":10,"messages":[{"role":"user","content":"hi"}]}` : `{"model":"glm-5.3","messages":[{"role":"user","content":"hi"}]}`
const res = await l.fetch(url, { method: "POST", headers: { "content-type": "application/json" }, body: req })
return { res, text: await res.text(), seen }
}
@@ -52,7 +52,12 @@ const policy = (where) =>
for (const where of ["top", "error"]) {
test(`a content_policy_rejected 403 (${where}) is said for what it is and not sent again`, async () => {
const { res, text, seen } = await refused(policy(where))
expect(res.status).toBe(403)
// a refusal of the request, not of the account: 400 with Factory's code
// kept, never the 403 permission_error agents read as a failed sign-in
expect(res.status).toBe(400)
const err = JSON.parse(text).error
expect(err.type).toBe("invalid_request_error")
expect(err.code).toBe("content_policy_rejected")
expect(seen.filter((p) => p === "/api/llm/a/v1/messages").length).toBe(1)
expect(seen.filter((p) => p === "/api/cli/whoami").length).toBeLessThan(2)
expect(text).toContain("content_policy_rejected")
@@ -61,11 +66,28 @@ for (const where of ["top", "error"]) {
})
}
// sweanng (yetone/magpie, Discord): ZCode on Factory's GLM 5.3, an OpenAI
// route, showed the refusal as "Provider authentication failed"
test("a content_policy_rejected 403 on the OpenAI route is a 400 with Factory's code", async () => {
const { res, text, seen } = await refused(policy("top"), {}, "https://api.factory.ai/api/llm/o/v1/chat/completions")
expect(res.status).toBe(400)
expect(JSON.parse(text).error).toMatchObject({ type: "invalid_request_error", code: "content_policy_rejected" })
expect(text).toContain("reads the whole conversation")
expect(seen.filter((p) => p === "/api/llm/o/v1/chat/completions").length).toBe(1)
})
test("the code under an upstream error header stays Factory's 403", async () => {
const { res, text } = await refused(policy("top"), { "x-factory-upstream-error": "1" })
expect(res.status).toBe(403)
expect(JSON.parse(text).error.type).toBe("permission_error")
})
test("any other 403 to an account with no org still asks whoami and tries again", async () => {
const { seen, text } = await refused(JSON.stringify({ detail: "Forbidden" }))
expect(seen.filter((p) => p === "/api/cli/whoami").length).toBe(2)
expect(seen.filter((p) => p === "/api/llm/a/v1/messages").length).toBe(2)
expect(text).not.toContain("reads the whole conversation")
expect(JSON.parse(text).error.type).toBe("permission_error")
})
test("the code is Factory's only without an upstream error header", () => {
+14 -2
View File
@@ -785,12 +785,15 @@ function tooLarge(text, left) {
// errorReply is an error as the API the request was for words one:
// Anthropic's on /llm/a/, OpenAI's on the others.
function errorReply(url, status, type, message, headers = new Headers(), requestId) {
// code is the vendor's error code, kept on the error: magpie and OpenCode
// tell a refusal of the request apart by it (content_policy_rejected).
function errorReply(url, status, type, message, headers = new Headers(), requestId, code) {
headers.set("content-type", "application/json")
headers.delete("content-length")
headers.delete("content-encoding")
const anthropic = new URL(url).pathname.includes("/llm/a/")
const body = anthropic ? { type: "error", error: { type, message } } : { error: { message, type, code: null } }
const body = anthropic ? { type: "error", error: { type, message } } : { error: { message, type, code: code ?? null } }
if (anthropic && code) body.error.code = code
// Anthropic's error body carries the request id beside the error
if (anthropic && requestId) body.request_id = requestId
return new Response(JSON.stringify(body), { status, headers })
@@ -2088,6 +2091,15 @@ export const FactoryAuthPlugin = async ({ client, magpie }) => {
// the refusal, then what to do about it, as provider.Explain joins them
const ids = upstreamIds(text, res.headers)
const msg = `${vendorError(text, statusLine(res.status))}${ids.said} — ${why}`
if (contentRefused(res.status, text, res.headers)) {
// the usage policy check refused what the request says, not
// the account: a 400 invalid_request_error with Factory's
// code, as OpenAI words a content refusal. Its 403
// permission_error read to agents as a failed sign-in
// (yetone/magpie, sweanng: ZCode's "Provider
// authentication failed", reason=auth_failed status=403)
return answer(errorReply(url, 400, "invalid_request_error", msg, new Headers(res.headers), ids.id, "content_policy_rejected"))
}
return answer(errorReply(url, res.status, "permission_error", msg, new Headers(res.headers), ids.id))
} catch (e) {
// Factory refused to renew the sign-in: the built-in failed the
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-factory-auth",
"version": "0.1.39",
"version": "0.1.40",
"description": "Sign in to a Factory (Droid) subscription from OpenCode or magpie",
"type": "module",
"main": "./index.mjs",