factory 0.1.36: a content_policy_rejected 403 says Factory's check reads the whole conversation, and isn't sent again

doubl4door (Discord, factory 0.1.34): Claude Code on Factory's Opus got
403 "This request was blocked by Factory's usage policy check" (code
content_policy_rejected), and the plugin's hint pointed at the org's model
policy and plan. droid 0.245.0 tells this refusal apart by that code, only
when Factory sent no x-factory-upstream-error header, and says the check
covers the whole conversation. contentRefused does the same: explain()
says to start a new session or remove the quoted text, and mendOrg's
whoami-and-resend is skipped, since the same conversation is refused
again under any org. Other 403s are handled as before.

Run (sandbox HOME):
- HOME=$(mktemp -d) bun test packages/factory: 171 pass, 0 fail
- bun scripts/check.mjs factory: ok; git diff --check clean
- Broken by hand: without the mendOrg skip 2 of content-policy's 4 tests
  fail; without explain's branch 3 fail.
Not tried against Factory's server (no Factory account here); the body
shapes tested are code at the top level and under error.
This commit is contained in:
yetone
2026-10-20 17:51:10 +08:00
parent 97aea1739a
commit 4564e890b2
3 changed files with 103 additions and 5 deletions
+80
View File
@@ -0,0 +1,80 @@
// doubl4door (Discord, factory 0.1.34): Claude Code on Factory's Opus got
// 403 "This request was blocked by Factory's usage policy check" with code
// content_policy_rejected, and the plugin's hint spoke of the org's model
// policy and plan. droid 0.245.0 tells this refusal apart by its code (and
// only without an x-factory-upstream-error header) and says the check reads
// the whole conversation. So the plugin says that, and doesn't ask whoami
// for an org and send the same refused conversation again.
import { afterEach, expect, test } from "bun:test"
import { FactoryAuthPlugin, _internal } from "./index.mjs"
const { contentRefused, explain } = _internal
const real = globalThis.fetch
afterEach(() => (globalThis.fetch = real))
const messages = "https://api.factory.ai/api/llm/a/v1/messages"
const noOrg = {
type: "oauth",
access: "tok",
refresh: "r",
expires: Date.now() + 3_600_000,
accountId: "d@example.com",
activeOrganizationId: "",
region: "",
premBaseHost: "",
}
async function refused(body, headers = {}) {
const seen = []
let asked = 0
globalThis.fetch = async (url) => {
const u = new URL(String(url))
seen.push(u.pathname)
// whoami names no org before the first request, and one after it
if (u.pathname === "/api/cli/whoami") return new Response(JSON.stringify({ userId: "user_k", orgId: asked++ ? "fac_K" : "", email: "d@example.com", region: "" }))
return new Response(body, { status: 403, headers: { "content-type": "application/json", ...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 })
return { res, text: await res.text(), seen }
}
const policy = (where) =>
JSON.stringify(
where === "top"
? { code: "content_policy_rejected", detail: "This request was blocked by Factory's usage policy check." }
: { error: { code: "content_policy_rejected", message: "This request was blocked by Factory's usage policy check." } },
)
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)
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")
expect(text).toContain("reads the whole conversation")
expect(text).not.toContain("model policy")
})
}
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")
})
test("the code is Factory's only without an upstream error header", () => {
const h = (o) => new Headers(o)
expect(contentRefused(403, policy("top"), h({}))).toBe(true)
expect(contentRefused(403, policy("top"), h({ "x-factory-upstream-error": "1" }))).toBe(false)
expect(contentRefused(400, policy("top"), h({}))).toBe(false)
expect(contentRefused(403, "Forbidden", h({}))).toBe(false)
expect(contentRefused(403, '{"error":{"code":"permission_denied"}}', h({}))).toBe(false)
expect(explain(403, policy("error"), h({}))).toContain("start a new session")
expect(explain(403, policy("error"), h({ "x-factory-upstream-error": "1" }))).toContain("model policy")
})
+22 -4
View File
@@ -403,10 +403,28 @@ async function reconcile(c) {
// "Requested active organization is not accessible by this user".
const orgRefused = (status, text) => status === 403 && text.toLowerCase().includes("active organization is not accessible")
// contentRefused is Factory's usage policy check refusing what the request
// says, not who sent it: a 403 with code "content_policy_rejected" (droid
// 0.245.0 tells it apart the same way, and only when Factory sent no
// x-factory-upstream-error header). Its check reads the whole conversation,
// so sending the request again, under any org, is refused again.
function contentRefused(status, text, headers) {
if (status !== 403 || headers?.get?.("x-factory-upstream-error")) return false
let v
try {
v = JSON.parse(text)
} catch {
return false
}
const err = v && typeof v === "object" && v.error && typeof v.error === "object" ? v.error : {}
return v?.code === "content_policy_rejected" || err.code === "content_policy_rejected"
}
// explain is what the user can do about a 403 Factory still answers once
// the request carries what droid sends.
function explain(status, text) {
function explain(status, text, headers) {
if (status !== 403) return ""
if (contentRefused(status, text, headers)) return "Factory's usage policy check reads the whole conversation, so this session's requests are refused again until the text that tripped it is gone: start a new session, or remove or reword what the conversation quotes (another agent's system prompt or instructions pasted in a message or a tool's output are the usual cause)"
if (orgRefused(status, text)) return "the Factory account's organization changed; remove the account in magpie and sign in to it again"
return "Factory refused the request. magpie adapts fixed client metadata on both OpenAI and Anthropic routes. Factory may still refuse unsupported fields or wrappers; check the upstream error, the organization's model policy, plan and regional provider availability, and compare the same model in Droid"
}
@@ -2020,12 +2038,12 @@ export const FactoryAuthPlugin = async ({ client }) => {
if (res.status === 413) return answer(await smaller(res))
if (res.status !== 403) return answer(res)
let text = await res.text()
if (await mendOrg(res.status, text, onRenew).catch(() => false)) {
if (!contentRefused(res.status, text, res.headers) && (await mendOrg(res.status, text, onRenew).catch(() => false))) {
res = await send(input, init, body, onRenew)
if (res.status !== 403) return answer(res)
text = await res.text()
}
const why = explain(res.status, text)
const why = explain(res.status, text, res.headers)
// 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}`
@@ -2044,4 +2062,4 @@ export const FactoryAuthPlugin = async ({ client }) => {
}
// for tests
export const _internal = { fastBody, FAST_BETA, limitWindows, windowEnd, dollars, vendorError, CORE, droidBody, DROID_LINE, geminiBody, leaveOutAttachments, LEFT_OUT, TOO_LARGE_HINT, MODELS, PROMOTIONS, rateOf, configModels, providerModel, providerModels }
export const _internal = { contentRefused, explain, fastBody, FAST_BETA, limitWindows, windowEnd, dollars, vendorError, CORE, droidBody, DROID_LINE, geminiBody, leaveOutAttachments, LEFT_OUT, TOO_LARGE_HINT, MODELS, PROMOTIONS, rateOf, configModels, providerModel, providerModels }
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-factory-auth",
"version": "0.1.35",
"version": "0.1.36",
"description": "Sign in to a Factory (Droid) subscription from OpenCode or magpie",
"type": "module",
"main": "./index.mjs",