From def6a83584d0ab3250e084c663e9301ae80ca6b6 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 25 Oct 2026 14:37:22 +0800 Subject: [PATCH] workbuddy 0.1.33: a oneOf of objects with no type reaches Kimi typed object (#378) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit congee949 found that kimi-k3.1-pro on workbuddy and workbuddy-ai answers 400 "Invalid request parameters" to a tool whose parameters hold a oneOf with no type. This happens even when every branch is an object, either as a $ref's target or deeper in it (dsh's REA tools). The same request with "type": "object" beside the oneOf gets 200, in three real dsh runs with all 198 tools. fitTools now runs typeUnions after fitShape. typeUnions gives such a oneOf type "object" when each branch is typed object, either directly or through a chain of local $refs. A union of other or mixed types, an anyOf, and a $ref that is missing, external or circular are left as they came. Only schema keywords are walked, so examples and defaults are not. Tests use the issue's literal parameters: the minimal $ref → oneOf, the nested oneOf that all three routes refused, and the REA $ref-branch case. They also cover what is left alone. Run: `bun test` in packages/workbuddy with a sandbox HOME/TMPDIR and a dead HTTPS_PROXY: 152 pass, 0 fail. `bun scripts/check.mjs workbuddy`: ok. scripts/try.sh with magpie origin/main 4dfd88206 lists workbuddy (15) and workbuddy-ai (16). Broken by hand: with the typeUnions call taken out of fitTools, tools.test.mjs goes 1 red; with $ref branches not followed, 1 red. Not tried: a real WorkBuddy account. The rule is the one the reporter verified on the live API. --- packages/workbuddy/index.mjs | 38 ++++++++++++++++++++++++++++- packages/workbuddy/package.json | 2 +- packages/workbuddy/tools.test.mjs | 40 +++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/packages/workbuddy/index.mjs b/packages/workbuddy/index.mjs index 5635325..8404a59 100644 --- a/packages/workbuddy/index.mjs +++ b/packages/workbuddy/index.mjs @@ -1039,6 +1039,41 @@ function objectRoot(ps, root = ps, depth = 0) { return true } +// Kimi (kimi-k3.1-pro) answers 400 "Invalid request parameters" to a oneOf +// that has no type, at the root of a $ref's target or deeper, even when every +// branch is an object (congee949, #378: dsh's REA tools, on workbuddy and +// workbuddy-ai alike; a "type": "object" beside it is taken). typeUnions gives +// such a oneOf type "object" when each branch is typed object itself or +// through a chain of local $refs, and changes nothing else: a union of other +// or mixed types, an anyOf, or a $ref that is missing, external or circular +// is left as it came. It reports whether it changed anything. +function typeUnions(s, root = s, depth = 0) { + if (!s || typeof s !== "object" || Array.isArray(s) || depth > 64) return false + let changed = false + if (!("type" in s) && Array.isArray(s.oneOf) && s.oneOf.length && s.oneOf.every((b) => objectBranch(root, b))) { + s.type = "object" + changed = true + } + for (const k of SUB_ONE) if (typeUnions(s[k], root, depth + 1)) changed = true + for (const k of SUB_LIST) for (const v of Array.isArray(s[k]) ? s[k] : []) if (typeUnions(v, root, depth + 1)) changed = true + for (const k of SUB_MAP) { + const m = s[k] + if (!m || typeof m !== "object" || Array.isArray(m)) continue + for (const v of Object.values(m)) if (typeUnions(v, root, depth + 1)) changed = true + } + return changed +} + +// objectBranch says whether b is typed object, itself or where its local +// $refs lead; a $ref seen before on the way is a cycle, and not. +function objectBranch(root, b, seen = []) { + if (!b || typeof b !== "object" || Array.isArray(b)) return false + if ("type" in b) return b.type === "object" + if (typeof b.$ref !== "string" || seen.includes(b.$ref) || seen.length > 16) return false + const t = localRef(root, b) + return t !== b && objectBranch(root, t, [...seen, b.$ref]) +} + // fitTools mends b's function tools in place: their parameters (fitSchema, // fitShape, objectRoot) and names. It reports whether it changed anything, // and the names it gave, each to the name the client knows (ours), so that the @@ -1072,6 +1107,7 @@ function fitTools(b) { } if (fitSchema(fn.parameters)) changed = true if (fitShape(fn.parameters)) changed = true + if (typeUnions(fn.parameters)) changed = true if (objectRoot(fn.parameters)) changed = true } if (theirs.size && Array.isArray(b.messages)) { @@ -2253,4 +2289,4 @@ export const WorkBuddyAuthPlugin = makePlugin(SITES.workbuddy) export const WorkBuddyAIAuthPlugin = makePlugin(SITES["workbuddy-ai"]) // for tests -export const _internal = { systemAsUser, systemKey, DEFAULT_SYSTEM, EDGE_READS, fitShape, replyAfterTools, AFTER_TOOLS_REFUSED, enterpriseUsageOf, notDesktop, textOnly, runsOutOf, packagesOf, endOf, enterpriseHeaders, enterpriseOf, liveModels, browserSignIn, withSystem, normalizeToolChoice, fitTools, fitted, renamed, unflagged, usageOf, desktopHeld, explained, REFUSED_HINT, RESTRICTED_HINT, EXHAUSTED_HINT, freeCredits, creditsOf, priceOf, fresh, SITES, renewing, renewed, SESSION, attend, turnKey, conversationKey, signatureOf, isMessages, withoutUnsigned, withoutSignedThinking, answeredBefore, nativeOf, modelOf, configModels, API_MESSAGES: NPM_MESSAGES, API_CHAT: NPM } +export const _internal = { typeUnions, systemAsUser, systemKey, DEFAULT_SYSTEM, EDGE_READS, fitShape, replyAfterTools, AFTER_TOOLS_REFUSED, enterpriseUsageOf, notDesktop, textOnly, runsOutOf, packagesOf, endOf, enterpriseHeaders, enterpriseOf, liveModels, browserSignIn, withSystem, normalizeToolChoice, fitTools, fitted, renamed, unflagged, usageOf, desktopHeld, explained, REFUSED_HINT, RESTRICTED_HINT, EXHAUSTED_HINT, freeCredits, creditsOf, priceOf, fresh, SITES, renewing, renewed, SESSION, attend, turnKey, conversationKey, signatureOf, isMessages, withoutUnsigned, withoutSignedThinking, answeredBefore, nativeOf, modelOf, configModels, API_MESSAGES: NPM_MESSAGES, API_CHAT: NPM } diff --git a/packages/workbuddy/package.json b/packages/workbuddy/package.json index 2f5b446..cdaf3d7 100644 --- a/packages/workbuddy/package.json +++ b/packages/workbuddy/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-workbuddy-auth", - "version": "0.1.32", + "version": "0.1.33", "description": "OpenCode provider plugins for WorkBuddy's plans: WorkBuddy (CodeBuddy, copilot.tencent.com) and WorkBuddy AI (www.workbuddy.ai)", "type": "module", "main": "./index.mjs", diff --git a/packages/workbuddy/tools.test.mjs b/packages/workbuddy/tools.test.mjs index 3ee3801..42b39cb 100644 --- a/packages/workbuddy/tools.test.mjs +++ b/packages/workbuddy/tools.test.mjs @@ -169,3 +169,43 @@ test("a property named enum, type or required is a name, not a keyword", () => { const body = chat([tool("t", structuredClone(p))]) expect(fitted(body).body).toBe(body) }) + +// Kimi K3.1 Pro answers 400 "Invalid request parameters" to a oneOf with no +// type, even of objects only, and takes it with "type": "object" beside it +// (congee949, #378). The parameters below are the issue's own. +const ab = [ + { type: "object", properties: { mode: { type: "string", const: "a" } } }, + { type: "object", properties: { mode: { type: "string", const: "b" } } }, +] + +test("a $ref's target that is a oneOf of objects with no type is typed object", () => { + const body = JSON.stringify({ model: "kimi-k3.1-pro", messages: [{ role: "user", content: "Do not call tools. Reply exactly OK." }], tools: [{ type: "function", function: { name: "probe", description: "Diagnostic placeholder.", parameters: { type: "object", properties: { x: { $ref: "#/$defs/X" } }, $defs: { X: { oneOf: ab } } } } }] }) + const ps = JSON.parse(fitted(body).body).tools[0].function.parameters + expect(ps.$defs.X).toEqual({ oneOf: ab, type: "object" }) + expect(ps.properties.x).toEqual({ $ref: "#/$defs/X" }) +}) + +test("a oneOf of objects inside a $ref's target, and one whose branches are $refs to objects, are typed object", () => { + const ps = { type: "object", properties: { x: { $ref: "#/$defs/X" } }, $defs: { X: { type: "object", properties: { y: { oneOf: ab } }, required: ["y"] } } } + expect(_internal.typeUnions(ps)).toBe(true) + expect(ps.$defs.X.properties.y.type).toBe("object") + const refs = { type: "object", properties: { s: { $ref: "#/$defs/__schema0" } }, $defs: { __schema0: { oneOf: [{ $ref: "#/$defs/__schema1" }, { $ref: "#/$defs/__schema3" }] }, __schema1: { type: "object", properties: { launch: { type: "boolean" } } }, __schema2: { $ref: "#/$defs/__schema1" }, __schema3: { $ref: "#/$defs/__schema4" }, __schema4: { type: "object", properties: { connect: { type: "string" } } } } } + expect(_internal.typeUnions(refs)).toBe(true) + expect(refs.$defs.__schema0.type).toBe("object") + expect(refs.$defs.__schema2.type).toBeUndefined() +}) + +test("a oneOf that is typed, mixed, an anyOf, or reaches a missing, external or circular $ref is left", () => { + const ps = { type: "object", properties: { + typed: { type: "object", oneOf: ab }, + mixed: { oneOf: [ab[0], { type: "string" }] }, + any: { anyOf: ab }, + missing: { oneOf: [ab[0], { $ref: "#/$defs/Nope" }] }, + external: { oneOf: [ab[0], { $ref: "other.json#/X" }] }, + loop: { oneOf: [ab[0], { $ref: "#/$defs/A" }] }, + examples: { type: "array", examples: [{ oneOf: ab }] }, + }, $defs: { A: { $ref: "#/$defs/B" }, B: { $ref: "#/$defs/A" } } } + const before = structuredClone(ps) + expect(_internal.typeUnions(ps)).toBe(false) + expect(ps).toEqual(before) +})