Repository navigation
Commit 0631ca4
authored
fix: repair anyOf tool schemas rejected by the Kimi wire (#14)
## Related Issue
No existing issue — the problem is described below. This is a
reproducible bug fix with a focused diff.
## Problem
Every request to a Kimi / Moonshot model fails before the model runs:
```
400 tools.function.parameters is not a valid moonshot flavored json schema,
details: <At path 'root': when using anyOf, type should be defined in anyOf items
instead of the parent schema>
```
The wire validator refuses a tool schema that uses `anyOf` as a
*refinement* of the
node it sits on. Two rules are involved:
- `type` may not appear next to `anyOf` on the same node.
- No validation keyword (`properties`, `items`, `additionalProperties`,
…) may
appear on both a node and one of its `anyOf` branches.
Both are legal standard JSON Schema, so schemas that work on every other
provider
are rejected here. `TaskStop` trips them: it adds a hand-written
`anyOf: [{ required: ['task_id'] }, { required: ['shell_id'] }]` on top
of an
object schema that already declares `type` and `properties`. Because
tool
definitions are sent with every request, a single offending tool takes
down the
whole session.
`normalizePythinkerToolSchema` already existed for exactly this class of
provider-compatibility repair, but it only visited *nested* property
schemas and
deliberately skipped the root, so it never saw the offending node.
## What changed
The repair lives in the provider's schema normalizer rather than in
`TaskStop`, so
MCP- and plugin-contributed schemas are covered by the same pass — this
shape is
common enough that several other clients have hit it independently.
**Nested nodes — distribute.** A node's own constraints are pushed down
into each
`anyOf` branch, and branches that already declare a keyword keep their
own
(narrower) value. This is lossless: `P ∧ (B₁ ∨ B₂)` and `(P ∧ B₁) ∨ (P ∧
B₂)`
accept exactly the same instances. Sibling combinators (`allOf` /
`oneOf` / `not` /
`if`) are left in place — the validator does not read them, so moving
them would
only churn the schema.
**Root — fold away.** The root cannot use the branch form, because a
tool's
parameters must be a plain object: the wire separately requires
`parameters.type == "object"`, which cannot coexist with a root `anyOf`.
The two
constraints are jointly unsatisfiable, so the root's `anyOf` is dropped.
That only
ever widens what is accepted, and a root `anyOf` is in practice a "one
of these
fields is required" hint that the tool re-checks at run time (`TaskStop`
already
returns `Missing required parameter: task_id`). Branch properties are
merged into
the root first, so a schema that kept its arguments inside the branches
does not
silently lose them.
## Verification
Rather than asserting against an assumed reading of the spec, every
schema was
replayed through the provider's own validator:
- Reproduced the original 400 against the live endpoint, then confirmed
it is gone.
- Replayed **all 36 built-in tool schemas and all 70 tool schemas from
five
connected MCP servers** through the normalizer and into the provider's
validator:
106 accepted, 0 rejected. Before the change, `TaskStop` was the single
failure.
- Full `kosong` suite passes (1185 tests); `typecheck` clean.
- Each of the two new code paths was individually disabled to confirm
the new tests
actually fail without it.
## Checklist
- [x] I have read the
[CONTRIBUTING](https://github.com/Pythoughts-labs/pythinker-code/blob/main/CONTRIBUTING.md)
document.
- [x] I have linked a related issue, or explained the problem above.
- [x] I have added tests that prove my feature works.
- [x] Ran `gen-changesets` skill, or this PR needs no changeset.
- [x] Ran `gen-docs` skill, or this PR needs no doc update — no
user-facing behavior or CLI surface changes.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **Bug Fixes**
* Improved compatibility with Kimi and Moonshot models when tools use
combined schema types.
* Fixed invalid tool schema errors involving `anyOf`, `oneOf`, and
`allOf` combinations.
* Improved normalization of nested and object-based tool schemas while
preserving metadata, constraints, references, and definitions.
* Reduced duplicate schema alternatives and improved handling of
required fields.
* **Tests**
* Added comprehensive coverage for root and nested schema normalization,
cyclic definitions, and combinators.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->1 parent 5ec7157 commit 0631ca4
3 files changed
Lines changed: 388 additions & 31 deletions
File tree
- .changeset
- packages/kosong
- src/providers
- test/providers
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
117 | 117 | | |
118 | 118 | | |
119 | 119 | | |
120 | | - | |
| 120 | + | |
| 121 | + | |
121 | 122 | | |
122 | 123 | | |
123 | 124 | | |
| |||
128 | 129 | | |
129 | 130 | | |
130 | 131 | | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
131 | 135 | | |
132 | 136 | | |
133 | 137 | | |
134 | 138 | | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
135 | 210 | | |
136 | 211 | | |
137 | 212 | | |
| |||
252 | 327 | | |
253 | 328 | | |
254 | 329 | | |
| 330 | + | |
255 | 331 | | |
256 | 332 | | |
257 | 333 | | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
258 | 425 | | |
259 | 426 | | |
260 | 427 | | |
| |||
466 | 633 | | |
467 | 634 | | |
468 | 635 | | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
469 | 651 | | |
470 | 652 | | |
471 | 653 | | |
0 commit comments