diff --git a/packages/factory/content-policy.test.mjs b/packages/factory/content-policy.test.mjs index 75cf2fc..7ce7720 100644 --- a/packages/factory/content-policy.test.mjs +++ b/packages/factory/content-policy.test.mjs @@ -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", () => { diff --git a/packages/factory/index.mjs b/packages/factory/index.mjs index e45820e..5d20cd8 100644 --- a/packages/factory/index.mjs +++ b/packages/factory/index.mjs @@ -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 diff --git a/packages/factory/package.json b/packages/factory/package.json index 36f3ba1..1df550e 100644 --- a/packages/factory/package.json +++ b/packages/factory/package.json @@ -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",