Files
plugins/packages/factory/tool-names.mjs
T
yetone bb849910ac factory 0.1.50: tools Factory refuses by name go out under droid's names, so dsh is not refused
@a1pha1nthefog on X (2026-11-19): dsh (DeepSeek Harness) through magpie on
Factory got "This request was blocked by Factory's usage policy check"
(content_policy_rejected) on its first turn, the user message "1". It reads
like dsh's prompt or skills are blocked, but they are not. Since 2026-11-18
Factory refuses any request that declares a tool under certain exact names,
on every route (#1291, #1306), and dsh 0.2.1 declares six of them: bash,
read, write, edit, glob and grep. It is the request's tool names, not the
account, the model, or dsh's prompt text.

This takes heggria's #1314 (closed unmerged) with credit: tool-names.mjs
declares such a tool under droid's own name for the job (Execute, Read,
Create, Edit, Glob, Grep, Task) or, when droid has none or the request
already declares that name, under its own name with a per-process suffix.
It renames earlier calls, tool_choice, allowed_tools and Gemini's function
names to match, and gives the caller its own names back in JSON and SSE
replies. Only name fields change: descriptions, schemas, arguments and the
prompt go as dsh wrote them. On top of #1314, agent, Shell and StrReplace,
which #1291 also saw refused on 11-18, are in the list, and
testdata/dsh-0.2.1-chat.json is dsh 0.2.1's real first request (captured
as it left dsh in a sandbox HOME, then sanitized), tested on chat
completions and on Messages.

Co-authored-by: heggria <34475327+heggria@users.noreply.github.com>

Run (mote, at 140c513b plus this change): HOME=$(mktemp -d) bun test is
9112 pass, 20 skip, 0 fail (9132 tests, 639 files); bun test
packages/factory is 216 pass; bun scripts/check.mjs factory passes.
Real dsh 0.2.1 in its sandbox HOME, run headless against the plugin's fetch
with a fake Factory that refuses the 11-18 names: it declared Execute, Edit,
Glob, Grep, Read and Create, its Execute call came back as bash, dsh ran
"echo dsh-ran-$((6*7))" and the fake saw "dsh-ran-42" in the tool result.

Broken by hand: with aliasToolNames taken out of send(), 12 of
tool-names.test.mjs's 14 tests fail, both dsh ones among them.

Not tried: a real Factory account. None is here, so Factory's own answer to
the renamed request is not seen. dannykkg reported #1314's renames
accepted with a real account (magpie#6994, 11-19).
2026-11-19 16:48:10 +08:00

235 lines
11 KiB
JavaScript

import { parseToolJSON } from "./anthropic-tools.mjs"
// Factory refuses a request whose tool list declares certain names, with a
// 403 content_policy_rejected, on every route (Anthropic Messages, OpenAI
// Responses and chat completions) and on both api.factory.ai and
// prem.factory.ai. Probed one tool at a time with an empty description: only
// the declared name decides, matched exactly and case-sensitively. A name
// that appears only in history (tool_use, function_call), in tool_choice or
// in text is not refused. The list moves: from 2026-11-17 22:30 PST it held
// Claude Code's, OpenCode's and Codex's names; from 2026-11-18 06:00 UTC it
// also holds the names first used in their place (BashTool, bash_tool, ...)
// and Claude Code's task tools, and no longer Codex's exec_command,
// write_stdin and Shell or NotebookEdit, EnterPlanMode, ExitPlanMode and
// get_weather. plugins#1291 also saw agent, Shell and StrReplace refused
// on 2026-11-18 07:04 UTC; they are kept, since a name renamed that Factory
// takes costs nothing and one missed refuses the whole request.
export const REFUSED_TOOL_NAMES = new Set([
"Agent", "Bash", "TaskCreate", "TaskGet", "TaskList", "TaskUpdate", "Write",
"Shell", "StrReplace",
"agent", "bash", "edit", "glob", "grep", "read", "run_shell", "task", "write",
"AgentTool", "BashTool", "WriteTool", "bash_tool", "edit_tool", "read_tool", "write_tool",
])
// DROID_NAMES is the tool of Factory's own client, droid, that does the
// same job, which Factory takes as declared: a refused tool goes out under
// it, unless the request already declares that name or another refused
// tool took it first.
export const DROID_NAMES = new Map([
["Bash", "Execute"], ["bash", "Execute"], ["run_shell", "Execute"], ["BashTool", "Execute"], ["bash_tool", "Execute"],
["Write", "Create"], ["write", "Create"], ["WriteTool", "Create"], ["write_tool", "Create"],
["Shell", "Execute"],
["Agent", "Task"], ["agent", "Task"], ["AgentTool", "Task"], ["task", "Task"],
["read", "Read"], ["read_tool", "Read"],
["edit", "Edit"], ["edit_tool", "Edit"],
["glob", "Glob"], ["grep", "Grep"],
])
// A refused tool droid has no name for (Claude Code's TaskCreate, ...), or
// one whose droid name is taken, goes out as its own name with a suffix
// drawn from this process (TaskCreate_3f9a): the same throughout the
// process, so a conversation reads one name and its prompt cache holds, but
// no fixed name a list could hold.
const SALT = crypto.randomUUID()
export function suffixed(name) {
let h = 0x811c9dc5
for (const c of SALT + "\0" + name) h = Math.imul(h ^ c.charCodeAt(0), 0x01000193) >>> 0
return name + "_" + (h & 0xffff).toString(16).padStart(4, "0")
}
const isObject = (v) => v != null && typeof v === "object" && !Array.isArray(v)
function text(body) {
if (typeof body === "string") return body
if (body instanceof ArrayBuffer) return Buffer.from(body).toString("utf8")
if (ArrayBuffer.isView(body)) return Buffer.from(body.buffer, body.byteOffset, body.byteLength).toString("utf8")
return null
}
// declared lists every place a request declares a tool by name, as
// [holder, key] pairs: Anthropic's custom tools, OpenAI's function tools
// (Responses' flat ones and chat completions' nested ones) and Gemini's
// function declarations. Anthropic's server tools (a type other than
// custom) keep the names Anthropic gives them.
function declared(request) {
const out = []
for (const tool of Array.isArray(request.tools) ? request.tools : []) {
if (!isObject(tool)) continue
if (tool.type === "function" && isObject(tool.function)) out.push([tool.function, "name"])
else if (typeof tool.name === "string" && (tool.type == null || tool.type === "custom" || tool.type === "function")) out.push([tool, "name"])
if (Array.isArray(tool.functionDeclarations)) for (const d of tool.functionDeclarations) if (isObject(d)) out.push([d, "name"])
}
return out
}
// uses lists where a request names a declared tool again: tool_choice,
// earlier calls in the history and Gemini's function responses and
// allowed names, so the model reads one name throughout.
function uses(request) {
const out = []
const choice = request.tool_choice
if (isObject(choice)) {
if (isObject(choice.function)) out.push([choice.function, "name"])
else if (typeof choice.name === "string") out.push([choice, "name"])
// Responses' allowed_tools lists tools as they are declared
for (const t of Array.isArray(choice.tools) ? choice.tools : []) {
if (isObject(t?.function)) out.push([t.function, "name"])
else if (isObject(t) && typeof t.name === "string") out.push([t, "name"])
}
}
for (const m of Array.isArray(request.messages) ? request.messages : []) {
if (!isObject(m)) continue
if (Array.isArray(m.content)) for (const b of m.content) if (isObject(b) && b.type === "tool_use") out.push([b, "name"])
if (Array.isArray(m.tool_calls)) for (const c of m.tool_calls) if (isObject(c?.function)) out.push([c.function, "name"])
}
for (const item of Array.isArray(request.input) ? request.input : []) {
if (isObject(item) && (item.type === "function_call" || item.type === "custom_tool_call")) out.push([item, "name"])
}
for (const c of Array.isArray(request.contents) ? request.contents : []) {
for (const p of Array.isArray(c?.parts) ? c.parts : []) {
if (isObject(p?.functionCall)) out.push([p.functionCall, "name"])
if (isObject(p?.functionResponse)) out.push([p.functionResponse, "name"])
}
}
const allowed = request.toolConfig?.functionCallingConfig?.allowedFunctionNames
if (Array.isArray(allowed)) for (let i = 0; i < allowed.length; i++) out.push([allowed, i])
return out
}
// aliasToolNames renames each declared tool Factory refuses, and every
// later mention of it in the request, to a name it takes: droid's own for
// the job (Execute for Bash, Create for Write, ...), else a suffixed one,
// never one the request already declares. It returns the body and the names
// to restore in the reply (alias → original). A body with nothing to rename
// goes on byte for byte.
export function aliasToolNames(body) {
const aliases = new Map()
const raw = text(body)
if (raw == null) return { body, aliases }
let request
try { request = parseToolJSON(raw) } catch { return { body, aliases } }
if (!isObject(request)) return { body, aliases }
const decl = declared(request)
if (!decl.some(([h, k]) => REFUSED_TOOL_NAMES.has(h[k]))) return { body, aliases }
const taken = new Set(decl.map(([h, k]) => h[k]))
const renamed = new Map()
for (const [holder, key] of decl) {
const name = holder[key]
if (!REFUSED_TOOL_NAMES.has(name)) continue
if (!renamed.has(name)) {
let alias = DROID_NAMES.get(name)
if (alias == null || taken.has(alias)) alias = suffixed(name)
while (taken.has(alias) || REFUSED_TOOL_NAMES.has(alias)) alias += "_"
taken.add(alias)
renamed.set(name, alias)
aliases.set(alias, name)
}
holder[key] = renamed.get(name)
}
for (const [holder, key] of uses(request)) if (renamed.has(holder[key])) holder[key] = renamed.get(holder[key])
return { body: JSON.stringify(request), aliases }
}
// restore puts the original names back wherever a reply names a tool: a
// "name" field holding an alias (Anthropic's tool_use, OpenAI's function
// calls in Responses and chat completions, Gemini's functionCall). A call's
// own arguments are the model's data and stay as they are, as they do in a
// stream, where they arrive as JSON text.
const ARGUMENTS = new Set(["input", "args", "arguments"])
function restore(value, aliases) {
let changed = false
const walk = (v) => {
if (Array.isArray(v)) { for (const x of v) walk(x); return }
if (!isObject(v)) return
for (const [k, x] of Object.entries(v)) {
if (k === "name" && typeof x === "string" && aliases.has(x)) { v[k] = aliases.get(x); changed = true }
else if (!ARGUMENTS.has(k)) walk(x)
}
}
walk(value)
return changed
}
function headersOf(res) {
const headers = new Headers(res.headers)
headers.delete("content-length")
headers.delete("content-encoding")
return { status: res.status, statusText: res.statusText, headers }
}
function eventStream(body, aliases) {
const reader = body.getReader()
// Bytes Factory sends that are not UTF-8 go on as U+FFFD rather than
// failing the reply.
const decoder = new TextDecoder("utf-8")
const encoder = new TextEncoder()
let pending = "", ended = false
const frame = (raw) => {
const lines = raw.split(/\r\n|\n|\r/)
const data = lines.filter((l) => l.startsWith("data:")).map((l) => l.slice(5).replace(/^ /, "")).join("\n")
if (!data.includes("\"name\"")) return raw
let event
try { event = parseToolJSON(data) } catch { return raw }
if (!restore(event, aliases)) return raw
// the event's data lines become one; its other fields and ending stay
const eol = /\r\n|\n|\r/.exec(raw)?.[0] ?? "\n"
const ending = /(?:\r\n|\n|\r)+$/.exec(raw)?.[0] ?? ""
const body = raw.slice(0, raw.length - ending.length).split(/\r\n|\n|\r/)
let first = true
const out = []
for (const l of body) {
if (!l.startsWith("data:")) out.push(l)
else if (first) { out.push("data: " + JSON.stringify(event)); first = false }
}
return out.join(eol) + ending
}
return new ReadableStream({
async pull(controller) {
try {
while (true) {
const boundary = /\r\n\r\n|\r\n\n|\n\r\n|\n\n|\r\r/.exec(pending)
if (boundary || (ended && pending)) {
const end = boundary ? boundary.index + boundary[0].length : pending.length
const raw = pending.slice(0, end)
pending = pending.slice(end)
controller.enqueue(encoder.encode(frame(raw)))
return
}
if (ended) { controller.close(); return }
const chunk = await reader.read()
pending += decoder.decode(chunk.value, { stream: !chunk.done })
ended = chunk.done
}
} catch (e) {
controller.error(e)
await reader.cancel(e).catch(() => {})
}
},
cancel(reason) { return reader.cancel(reason) },
})
}
// restoreToolNames gives the caller its own tool names back in Factory's
// reply, streamed or whole. Without aliases, or for a failed or bodiless
// reply, the response goes on untouched.
export async function restoreToolNames(res, aliases) {
if (!aliases.size || !res.ok || !res.body) return res
const type = res.headers.get("content-type") ?? ""
if (type.includes("text/event-stream")) return new Response(eventStream(res.body, aliases), headersOf(res))
if (!type.includes("json")) return res
const raw = await res.text()
let value
try { value = parseToolJSON(raw) } catch { return new Response(raw, headersOf(res)) }
return new Response(restore(value, aliases) ? JSON.stringify(value) : raw, headersOf(res))
}