Files
plugins/packages/devin/schema.test.mjs
T
yetone fbee8a0abf devin 0.1.10: a tool's root union whose branches are unions themselves keeps their fields, so a model can create a Codex automation (yetone/magpie#1271)
objectRoot dropped any anyOf/oneOf branch that was itself a union. Codex
desktop's automation_update (zod 4: root anyOf of view, delete, and create
and update, each a union on kind) folded to only view's mode and id, both
required, so the model was offered a tool that could only view. Found
while fixing the same fold in magpie's built-in for #1271.

A nested union branch is now folded first (up to 8 deep); a field the
root doesn't define takes every distinct schema the branches give it, as
anyOf when they differ; a field every branch requires stays required.

Verified: schema.test.mjs's new test fails on 0.1.9 (no "name" among
[id mode]) and passes now; HOME=$(mktemp -d) bun test in packages/devin:
39 pass, 0 fail.
2026-10-08 16:51:46 +08:00

124 lines
5.0 KiB
JavaScript

// A tool whose parameters have anyOf, oneOf or allOf at the root goes to
// Devin as a plain object (magpie#1196): Devin's Claude models answered
// every request offering one with 502 "There is an issue with this request,
// please try a different model", and Codex desktop offers one in codex_app's
// automation_update. The schema is folded as magpie's built-in folds it for
// Anthropic (provider.ObjectRoot, magpie#646).
import { expect, test } from "bun:test"
import { _internal } from "./index.mjs"
const dec = new TextDecoder()
// sent is each tool sent with the request: name -> its schema, parsed.
const sent = (b) =>
Object.fromEntries(
_internal
.fields(b)
.filter((f) => f.num === 10 && f.wire === 2)
.map((f) => {
const g = _internal.fields(f.data)
return [dec.decode(g.find((h) => h.num === 1).data), JSON.parse(dec.decode(g.find((h) => h.num === 3).data))]
}),
)
// the parameters of the issue's minimal reproduction, byte for byte
const ISSUE = `{
"type": "object",
"properties": {"a": {"type": "string"}},
"oneOf": [{"properties": {"a": {"type": "string"}}}, {"properties": {"b": {"type": "string"}}}]
}`
const chatWith = (parameters) => ({
messages: [{ role: "user", content: "ok" }],
tools: [{ type: "function", function: { name: "t", description: "t", parameters } }],
})
for (const k of ["oneOf", "anyOf", "allOf"]) {
test(`a root ${k} goes to Devin as a plain object`, () => {
const parameters = JSON.parse(ISSUE.replace('"oneOf"', `"${k}"`))
const before = JSON.stringify(parameters)
const got = sent(_internal.build(chatWith(parameters), "claude-opus-5-5-high", "k")).t
expect(got).toEqual({ type: "object", properties: { a: { type: "string" }, b: { type: "string" } } })
// the caller's own schema is left as it came
expect(JSON.stringify(parameters)).toBe(before)
})
}
test("a schema with no root combinator is sent as it came", () => {
const parameters = {
type: "object",
properties: { a: { anyOf: [{ type: "string" }, { type: "null" }] }, n: { type: "integer" } },
required: ["n"],
$defs: { x: { type: "string" } },
}
const got = sent(_internal.build(chatWith(parameters), "claude-opus-5-5-high", "k")).t
expect(got).toEqual(parameters)
})
test("allOf's branches are all merged, required and all", () => {
expect(
_internal.objectRoot({
type: "object",
properties: { id: { type: "string" } },
required: ["id"],
allOf: [{ $ref: "#/definitions/when" }, { properties: { id: { type: "integer" } } }],
definitions: { when: { properties: { when: { type: "string" } }, required: ["when"] } },
}),
).toEqual({
type: "object",
properties: { id: { type: "string" }, when: { type: "string" } },
required: ["id", "when"],
definitions: { when: { properties: { when: { type: "string" } }, required: ["when"] } },
})
})
test("anyOf's object branches are merged, a field every one requires stays required, $refs are followed", () => {
const byName = { type: "object", properties: { name: { type: "string" }, when: { type: "string" } }, required: ["name", "when"] }
expect(
_internal.objectRoot({
anyOf: [
{ $ref: "#/$defs/byName" },
{ type: "object", properties: { name: { type: "string" }, tag: { type: "string" } }, required: ["name", "tag"] },
{ type: "string" },
{ $ref: "#/$defs/missing" },
],
$defs: { byName },
}),
).toEqual({
type: "object",
properties: { name: { type: "string" }, when: { type: "string" }, tag: { type: "string" } },
required: ["name"],
$defs: { byName },
})
})
test("objectRoot keeps the root's own required beside anyOf's branches", () => {
const { objectRoot } = _internal
const out = objectRoot({
type: "object",
properties: { id: { type: "string" } },
required: ["id"],
anyOf: [
{ type: "object", properties: { a: { type: "string" } }, required: ["a"] },
{ type: "object", properties: { b: { type: "string" } }, required: ["b"] },
],
})
expect(out.required).toEqual(["id"])
expect(Object.keys(out.properties).sort()).toEqual(["a", "b", "id"])
})
// Codex desktop's automation_update (testdata: what ChatGPT.app 26.930's
// zod builds) has create and update branches that are unions themselves;
// they were dropped, leaving only view's fields with id required, so the
// model could never create an automation (magpie#1271).
test("a branch that is itself a union is folded, not dropped", () => {
const { readFileSync } = require("node:fs")
const schema = JSON.parse(readFileSync(new URL("./testdata/codex_automation_update_schema.json", import.meta.url), "utf8"))
const ps = _internal.objectRoot(schema)
expect(ps.type).toBe("object")
expect(ps.anyOf).toBeUndefined()
for (const k of ["mode", "id", "name", "prompt", "rrule", "status", "kind"]) expect(Object.keys(ps.properties)).toContain(k)
expect(ps.required).toEqual(["mode"])
for (const v of ["view", "delete"]) expect(JSON.stringify(ps.properties.mode)).toContain(v)
})