Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions bin/antigravity-direct.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,16 @@ const resolveModelAndEffort = (requestedModel, requestedEffort) => {
// Probed 2026-11-03 (opencode#1548): an ARRAY schema with neither items
// nor prefixItems is likewise rejected ("items: missing field"), so a
// bare {type: "array"} gets an unconstrained items synthesized below.
// Probed 2026-11-03 (opencode#1550): for Claude models the upstream
// re-serializes the proto Schema into an Anthropic input_schema, and
// Anthropic's validator rejects `anyOf` outright -- any branch count, any
// position, root or nested ("tools.N.custom.input_schema: JSON schema is
// invalid. It must match JSON Schema draft 2020-12") -- while accepting
// oneOf and allOf in every probed shape. Gemini models accept anyOf
// verbatim. So for Claude targets anyOf is emitted as oneOf below: tool
// unions are disjoint alternatives in practice (type unions, nullable
// encodings), where exactly-one is the faithful reading, and oneOf is
// the only union keyword the Anthropic side takes.
// ---------------------------------------------------------------------------

const SCHEMA_FIELDS = new Set([
Expand Down Expand Up @@ -376,7 +386,7 @@ const resolveSchemaRefs = (root) => {
return walkSchema(root, []);
};

const convertSchemaNode = (node) => {
const convertSchemaNode = (node, anthropic = false) => {
// Boolean schemas and stray values become an unconstrained object schema.
if (!isSchemaRecord(node)) return {};
const out = {};
Expand Down Expand Up @@ -407,23 +417,31 @@ const convertSchemaNode = (node) => {
if (k === 'properties') {
if (isSchemaRecord(v)) {
const props = {};
for (const [name, sub] of Object.entries(v)) defineValue(props, name, convertSchemaNode(sub));
for (const [name, sub] of Object.entries(v)) defineValue(props, name, convertSchemaNode(sub, anthropic));
out.properties = props;
}
continue;
}
if (k === 'anyOf' || k === 'oneOf' || k === 'allOf') {
if (Array.isArray(v)) out[k] = v.map(convertSchemaNode);
if (Array.isArray(v)) {
const branches = v.map(sub => convertSchemaNode(sub, anthropic));
if (k === 'allOf') out.allOf = branches;
// anyOf folds into oneOf for Anthropic targets (see the header
// probe note); a node carrying both keywords merges both branch
// sets into the single oneOf the Anthropic side accepts.
else if (k === 'oneOf' || anthropic) out.oneOf = out.oneOf ? [...out.oneOf, ...branches] : branches;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve conjunction when folding sibling union keywords

For a Claude tool property containing both anyOf and oneOf, concatenating their branches changes the original conjunction into a single exclusive union. For example, if both keywords contain [{type: "string"}, {type: "number"}], the original schema permits strings and numbers, but the emitted four-branch oneOf rejects every value because each valid value matches twice. Preserve the two converted groups separately under allOf, which the upstream accepts, rather than merging their alternatives.

Useful? React with 👍 / 👎.

else out.anyOf = branches;
}
continue;
}
if (k === 'not') { out.not = convertSchemaNode(v); continue; }
if (k === 'not') { out.not = convertSchemaNode(v, anthropic); continue; }
if (k === 'type' || k === 'required' || k === 'minimum' || k === 'maximum' || k === 'items' || k === 'prefixItems') continue; // handled outside the loop
out[k] = v; // scalar constraint fields: format, title, pattern, ...
}
if (minimum !== undefined) out.minimum = minimum;
if (maximum !== undefined) out.maximum = maximum;
if (items !== undefined) out.items = convertSchemaNode(items);
if (prefixItems !== undefined) out.prefixItems = (Array.isArray(prefixItems) ? prefixItems : [prefixItems]).map(convertSchemaNode);
if (items !== undefined) out.items = convertSchemaNode(items, anthropic);
if (prefixItems !== undefined) out.prefixItems = (Array.isArray(prefixItems) ? prefixItems : [prefixItems]).map(sub => convertSchemaNode(sub, anthropic));
// Type: uppercase it; collapse an array of types ("null" -> nullable,
// first concrete type wins); infer OBJECT when properties are present
// (the proto allows properties only for OBJECT).
Expand Down Expand Up @@ -455,8 +473,8 @@ const convertSchemaNode = (node) => {
return out;
};

const convertSchema = (schema) => {
const converted = convertSchemaNode(resolveSchemaRefs(isSchemaRecord(schema) ? schema : {}));
const convertSchema = (schema, anthropic = false) => {
const converted = convertSchemaNode(resolveSchemaRefs(isSchemaRecord(schema) ? schema : {}), anthropic);
// The parameters root must declare OBJECT (tool-schema.ts's objectRoot:
// providers reject a tool schema whose root does not declare it).
if (converted.type === undefined) converted.type = 'OBJECT';
Expand Down Expand Up @@ -500,7 +518,7 @@ const toolChoiceConfig = (toolChoice, declaredNames) => {
};

const buildV1Request = (body) => {
const { fullName, effort } = resolveModelAndEffort(body.model, body.reasoning_effort);
const { base, fullName, effort } = resolveModelAndEffort(body.model, body.reasoning_effort);
const messages = Array.isArray(body.messages) ? body.messages : [];
const systemParts = [];
const contents = [];
Expand Down Expand Up @@ -573,7 +591,7 @@ const buildV1Request = (body) => {
for (const t of body.tools) {
const fn = t && t.type === 'function' ? t.function : t;
if (fn && fn.name) {
decls.push({ name: fn.name, description: fn.description || '', parameters: convertSchema(fn.parameters || { type: 'object', properties: {} }) });
decls.push({ name: fn.name, description: fn.description || '', parameters: convertSchema(fn.parameters || { type: 'object', properties: {} }, base.startsWith('claude')) });
declaredNames.push(fn.name);
}
}
Expand Down
57 changes: 57 additions & 0 deletions bin/antigravity-direct.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,63 @@ test('bare array tool properties reach upstream with synthesized items (opencode
assertArraysCarryItems(decls[0].parameters, 'parameters');
});

// --- opencode#1550: anyOf folds to oneOf for Claude targets ---------------
// For Claude models the upstream re-serializes the proto Schema into an
// Anthropic input_schema, and Anthropic's validator rejects anyOf in any
// position ("tools.N.custom.input_schema: JSON schema is invalid. It must
// match JSON Schema draft 2020-12"), which 400'd workhorse sessions on
// the Claude leaf all day 2026-11-03 (any tool with a zod-union or
// nullable-anyOf property). Conversion for Claude targets must emit
// oneOf instead; Gemini targets keep anyOf verbatim.

const assertNoKey = (node, key, where) => {
if (Array.isArray(node)) { node.forEach((v, i) => assertNoKey(v, key, `${where}[${i}]`)); return; }
if (!node || typeof node !== 'object') return;
assert.ok(!(key in node), `${where}: unexpected ${key}`);
for (const [k, v] of Object.entries(node)) assertNoKey(v, key, `${where}.${k}`);
};

const UNION_TOOLS = [{
type: 'function',
function: {
name: 'probe_union',
description: 'union-shaped parameters',
parameters: {
type: 'object',
properties: {
nullable_note: { anyOf: [{ type: 'string' }, { type: 'null' }] },
either: { anyOf: [{ type: 'string' }, { type: 'number' }] },
listed: { type: 'array', items: { anyOf: [{ type: 'string' }, { type: 'boolean' }] } },
pick: { oneOf: [{ type: 'string' }, { type: 'number' }] }
}
}
}
}];

test('anyOf tool schemas fold to oneOf for claude models (opencode#1550)', async () => {
const { status } = await chat({ model: 'claude-sonnet-5-5', tools: UNION_TOOLS });
assert.equal(status, 200);
const decls = lastFunctionDeclarations();
assert.equal(decls.length, 1);
const props = decls[0].parameters.properties;
assert.deepEqual(props.nullable_note, { oneOf: [{ type: 'STRING' }, { type: 'NULL' }] });
assert.deepEqual(props.either, { oneOf: [{ type: 'STRING' }, { type: 'NUMBER' }] });
assert.deepEqual(props.listed, { type: 'ARRAY', items: { oneOf: [{ type: 'STRING' }, { type: 'BOOLEAN' }] } });
assert.deepEqual(props.pick, { oneOf: [{ type: 'STRING' }, { type: 'NUMBER' }] });
assertNoKey(decls[0].parameters, 'anyOf', 'parameters');
});

test('anyOf tool schemas stay anyOf for gemini models (opencode#1550)', async () => {
const { status } = await chat({ tools: UNION_TOOLS });
assert.equal(status, 200);
const decls = lastFunctionDeclarations();
const props = decls[0].parameters.properties;
assert.deepEqual(props.nullable_note, { anyOf: [{ type: 'STRING' }, { type: 'NULL' }] });
assert.deepEqual(props.either, { anyOf: [{ type: 'STRING' }, { type: 'NUMBER' }] });
assert.deepEqual(props.listed, { type: 'ARRAY', items: { anyOf: [{ type: 'STRING' }, { type: 'BOOLEAN' }] } });
assert.deepEqual(props.pick, { oneOf: [{ type: 'STRING' }, { type: 'NUMBER' }] });
});

// --- #2823: env parsing is strict -----------------------------------------
// A bad ANTIGRAVITY_DIRECT_PORT used to reach listen() as an anonymous
// ERR_SOCKET_BAD_PORT (or bind 0 / a hex-decoded port), and a bad
Expand Down