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.
This commit is contained in:
yetone
2026-10-08 16:51:46 +08:00
parent a6910fd50a
commit fbee8a0abf
4 changed files with 424 additions and 12 deletions
+25 -11
View File
@@ -997,10 +997,15 @@ const joinNonEmpty = (...s) => s.filter(Boolean).join("\n\n")
// (provider.ObjectRoot): allOf's branches are all merged, properties and
// required alike; of anyOf's and oneOf's object branches the properties are
// merged, a field each of them requires stays required, and the other
// branches go. Branches may be local $refs. A schema already a plain object
// branches go; a branch that is a union itself is folded first, so its
// fields are kept (magpie#1271). Branches may be local $refs. A schema already a plain object
// is returned as it came; the caller's own is never changed.
function objectRoot(ps) {
if (!ps || typeof ps !== "object" || Array.isArray(ps)) return ps
return foldRoot(ps, ps, 0)
}
function foldRoot(root, ps, depth) {
if (!isObj(ps)) return ps
const has = (k) => Object.hasOwn(ps, k)
if (ps.type === "object" && !has("anyOf") && !has("oneOf") && !has("allOf")) return ps
const out = { ...ps }
@@ -1012,39 +1017,48 @@ function objectRoot(ps) {
for (const k of ["$defs", "definitions"]) {
const pre = "#/" + k + "/"
if (b.$ref.startsWith(pre)) {
const d = ps[k]?.[b.$ref.slice(pre.length)]
const d = root[k]?.[b.$ref.slice(pre.length)]
return isObj(d) ? d : null
}
}
return null
}
const merge = (b) => {
if (isObj(b.properties)) for (const [k, v] of Object.entries(b.properties)) if (!Object.hasOwn(props, k)) props[k] = v
}
for (const b of Array.isArray(ps.allOf) ? ps.allOf : []) {
const bm = ref(b)
if (!bm) continue
merge(bm)
if (isObj(bm.properties)) for (const [k, v] of Object.entries(bm.properties)) if (!Object.hasOwn(props, k)) props[k] = v
if (Array.isArray(bm.required)) required.push(...bm.required)
}
delete out.allOf
const branches = []
for (const k of ["anyOf", "oneOf"]) {
for (const b of Array.isArray(ps[k]) ? ps[k] : []) {
const bm = ref(b)
if (!bm || (bm.type !== "object" && !Object.hasOwn(bm, "properties"))) continue
let bm = ref(b)
if (!bm) continue
// a branch that is itself a union (zod's discriminated create and
// update in automation_update) is folded first, not dropped
if (depth < 8 && ["anyOf", "oneOf", "allOf"].some((u) => Object.hasOwn(bm, u))) bm = foldRoot(root, bm, depth + 1)
if (bm.type !== "object" && !Object.hasOwn(bm, "properties")) continue
branches.push(bm)
}
delete out[k]
}
// a field every branch requires stays required, beside the root's and
// allOf's own, which no branch can drop
// allOf's own, which no branch can drop; a field the root doesn't define
// takes each schema the branches give it, as anyOf when they differ
let common = null
const from = new Map()
for (const b of branches) {
merge(b)
for (const [k, v] of Object.entries(isObj(b.properties) ? b.properties : {})) {
if (Object.hasOwn(props, k)) continue
const vs = from.get(k) ?? []
if (!vs.some((w) => JSON.stringify(w) === JSON.stringify(v))) vs.push(v)
from.set(k, vs)
}
const br = Array.isArray(b.required) ? b.required : []
common = common === null ? [...br] : common.filter((r) => br.includes(r))
}
for (const [k, vs] of from) props[k] = vs.length === 1 ? vs[0] : { anyOf: vs }
for (const r of common ?? []) if (!required.includes(r)) required.push(r)
required = [...new Set(required)]
out.type = "object"
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-devin-auth",
"version": "0.1.9",
"version": "0.1.10",
"description": "Devin subscriptions (the devin CLI's account) in OpenCode and magpie",
"type": "module",
"main": "./index.mjs",
+15
View File
@@ -106,3 +106,18 @@ test("objectRoot keeps the root's own required beside anyOf's branches", () => {
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)
})
@@ -0,0 +1,383 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"anyOf": [
{
"type": "object",
"properties": {
"mode": {
"type": "string",
"const": "view"
},
"id": {
"$ref": "#/$defs/__schema0"
}
},
"required": [
"mode",
"id"
],
"additionalProperties": false
},
{
"anyOf": [
{
"type": "object",
"properties": {
"name": {
"$ref": "#/$defs/__schema2"
},
"prompt": {
"$ref": "#/$defs/__schema3"
},
"rrule": {
"$ref": "#/$defs/__schema4"
},
"status": {
"$ref": "#/$defs/__schema5"
},
"notificationPolicy": {
"$ref": "#/$defs/__schema6"
},
"kind": {
"$ref": "#/$defs/__schema7"
},
"projectId": {
"$ref": "#/$defs/__schema8"
},
"model": {
"$ref": "#/$defs/__schema9"
},
"reasoningEffort": {
"$ref": "#/$defs/__schema10"
},
"mode": {
"$ref": "#/$defs/__schema11"
},
"destination": {
"type": "string",
"const": "local"
},
"executionEnvironment": {
"type": "string",
"const": "local"
}
},
"required": [
"name",
"prompt",
"rrule",
"status",
"kind",
"projectId",
"model",
"reasoningEffort",
"mode",
"executionEnvironment"
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"name": {
"$ref": "#/$defs/__schema2"
},
"prompt": {
"$ref": "#/$defs/__schema3"
},
"rrule": {
"$ref": "#/$defs/__schema4"
},
"status": {
"$ref": "#/$defs/__schema5"
},
"notificationPolicy": {
"$ref": "#/$defs/__schema6"
},
"kind": {
"$ref": "#/$defs/__schema12"
},
"destination": {
"$ref": "#/$defs/__schema13"
},
"targetThreadId": {
"$ref": "#/$defs/__schema14"
},
"mode": {
"$ref": "#/$defs/__schema11"
}
},
"required": [
"name",
"prompt",
"rrule",
"status",
"kind",
"mode"
],
"additionalProperties": false
}
]
},
{
"anyOf": [
{
"type": "object",
"properties": {
"name": {
"$ref": "#/$defs/__schema2"
},
"prompt": {
"$ref": "#/$defs/__schema3"
},
"rrule": {
"$ref": "#/$defs/__schema15"
},
"status": {
"$ref": "#/$defs/__schema5"
},
"notificationPolicy": {
"$ref": "#/$defs/__schema6"
},
"kind": {
"$ref": "#/$defs/__schema7"
},
"projectId": {
"$ref": "#/$defs/__schema8"
},
"model": {
"$ref": "#/$defs/__schema9"
},
"reasoningEffort": {
"$ref": "#/$defs/__schema10"
},
"mode": {
"$ref": "#/$defs/__schema16"
},
"id": {
"$ref": "#/$defs/__schema0"
},
"destination": {
"type": "string",
"enum": [
"local",
"worktree"
]
},
"executionEnvironment": {
"description": "env",
"type": "string",
"enum": [
"worktree",
"local"
]
},
"localEnvironmentConfigPath": {
"anyOf": [
{
"type": "string",
"minLength": 1
},
{
"type": "null"
}
]
}
},
"required": [
"name",
"prompt",
"rrule",
"status",
"kind",
"projectId",
"model",
"reasoningEffort",
"mode",
"id",
"executionEnvironment"
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"name": {
"$ref": "#/$defs/__schema2"
},
"prompt": {
"$ref": "#/$defs/__schema3"
},
"rrule": {
"$ref": "#/$defs/__schema15"
},
"status": {
"$ref": "#/$defs/__schema5"
},
"notificationPolicy": {
"$ref": "#/$defs/__schema6"
},
"kind": {
"$ref": "#/$defs/__schema12"
},
"destination": {
"$ref": "#/$defs/__schema13"
},
"targetThreadId": {
"$ref": "#/$defs/__schema14"
},
"mode": {
"$ref": "#/$defs/__schema16"
},
"id": {
"$ref": "#/$defs/__schema0"
}
},
"required": [
"name",
"prompt",
"rrule",
"status",
"kind",
"mode",
"id"
],
"additionalProperties": false
}
]
},
{
"type": "object",
"properties": {
"mode": {
"type": "string",
"const": "delete"
},
"id": {
"$ref": "#/$defs/__schema0"
}
},
"required": [
"mode",
"id"
],
"additionalProperties": false
}
],
"$defs": {
"__schema0": {
"description": "Automation id.",
"$ref": "#/$defs/__schema1"
},
"__schema1": {
"type": "string",
"minLength": 1
},
"__schema2": {
"description": "name",
"$ref": "#/$defs/__schema1"
},
"__schema3": {
"description": "prompt",
"$ref": "#/$defs/__schema1"
},
"__schema4": {
"description": "rrule create",
"$ref": "#/$defs/__schema1"
},
"__schema5": {
"description": "status",
"type": "string",
"enum": [
"ACTIVE",
"PAUSED"
]
},
"__schema6": {
"description": "np",
"anyOf": [
{
"type": "string",
"enum": [
"all",
"failed_runs_only"
]
},
{
"type": "null"
}
]
},
"__schema7": {
"description": "cron",
"type": "string",
"const": "cron"
},
"__schema8": {
"description": "project",
"anyOf": [
{
"type": "string",
"minLength": 1
},
{
"type": "null"
}
]
},
"__schema9": {
"description": "model",
"$ref": "#/$defs/__schema1"
},
"__schema10": {
"description": "effort",
"type": "string",
"enum": [
"none",
"minimal",
"low",
"medium",
"high",
"xhigh",
"max",
"ultra"
]
},
"__schema11": {
"type": "string",
"enum": [
"create",
"suggested_create"
]
},
"__schema12": {
"description": "hb",
"type": "string",
"const": "heartbeat"
},
"__schema13": {
"description": "dest",
"type": "string",
"enum": [
"local",
"thread"
]
},
"__schema14": {
"format": "uuid",
"description": "tt",
"$ref": "#/$defs/__schema1"
},
"__schema15": {
"description": "rrule",
"$ref": "#/$defs/__schema1"
},
"__schema16": {
"type": "string",
"enum": [
"update",
"suggested_update"
]
}
}
}