Skip to content

Commit 5070175

Browse files
Ingwannulidge-jun
authored andcommitted
fix(moonshot): intersect nested schema bounds
1 parent 913f844 commit 5070175

3 files changed

Lines changed: 99 additions & 2 deletions

File tree

‎src/adapters/openai-chat.ts‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,8 @@ function intersectBound(target: unknown, sibling: unknown, direction: "max" | "m
11131113
* Compose two `properties` maps. A property named in BOTH the referenced target and the
11141114
* node is the same conjunction problem `required` had: letting the sibling win discards
11151115
* the target's constraints for that member. Merge the two member schemas so neither side
1116-
* loses its keywords, and let the node narrow on a genuine conflict.
1116+
* loses its keywords. Shared member bounds are the same conjunction one level down,
1117+
* and nested object members recurse through this helper instead of replacing the target.
11171118
*/
11181119
function composeProperties(
11191120
target: Record<string, unknown>,
@@ -1127,7 +1128,20 @@ function composeProperties(
11271128
const member: Record<string, unknown> = Object.create(null) as Record<string, unknown>;
11281129
for (const [k, v] of Object.entries(existing)) member[k] = v;
11291130
for (const [k, v] of Object.entries(sub)) {
1130-
member[k] = k === "required" ? unionRequired(member[k], v) : v;
1131+
if (k === "required") {
1132+
member[k] = unionRequired(member[k], v);
1133+
continue;
1134+
}
1135+
if (k === "properties" && isXaiObjectSchema(member[k]) && isXaiObjectSchema(v)) {
1136+
member[k] = composeProperties(member[k] as Record<string, unknown>, v);
1137+
continue;
1138+
}
1139+
const boundDirection = MOONSHOT_BOUND_KEYWORDS[k];
1140+
if (boundDirection && k in member) {
1141+
member[k] = intersectBound(member[k], v, boundDirection);
1142+
continue;
1143+
}
1144+
member[k] = v;
11311145
}
11321146
combined[name] = member;
11331147
continue;

‎structure/04_transports-and-sidecars.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,21 @@ the shared Ark hostname is too broad because the two endpoint families reject op
10931093

10941094
## Chat structured-output compatibility
10951095

1096+
First-party Kimi and Moonshot Chat destinations normalize a `$ref` with sibling keywords because
1097+
their wire rejects that valid JSON Schema 2020-12 shape. Inlining preserves conjunction semantics:
1098+
`required` members are unioned, lower numeric bounds take the maximum, upper numeric bounds take the
1099+
minimum, and overlapping `properties` recurse with the same rules. The walk remains depth-, node-,
1100+
and expansion-bounded. Unresolvable or cyclic references keep the existing bare-`$ref` fallback,
1101+
and unrelated OpenAI-compatible providers retain the caller's schema unchanged.
1102+
1103+
[Decision Log]
1104+
- 목적과 의도: Make Moonshot's compatibility rewrite remove rejected sibling `$ref` shapes without silently weakening a tool schema.
1105+
- 기존 구현 및 제약 조건: The target and sibling both apply under JSON Schema 2020-12, but a shallow shared-property merge let sibling bounds replace stricter target bounds; Moonshot still requires the local bounded rewrite.
1106+
- 검토한 주요 대안: Keep shallow sibling precedence; emit `allOf`; intersect only top-level bounds; recursively compose the supported set-valued and ordered assertions.
1107+
- 선택한 방식: Reuse the existing bound and required intersection rules recursively for overlapping object properties inside the first-party destination gate.
1108+
- 다른 대안 대신 이 방식을 선택한 이유: Shallow precedence weakens constraints, while a new `allOf` wire shape needs separate provider evidence; recursive composition fixes the demonstrated loss without broadening normalization to custom providers.
1109+
- 장점, 단점 및 영향: Looser siblings cannot relax nested constraints and tighter siblings still narrow them; non-ordered conflicting keywords retain the existing sibling precedence and are not treated as a complete JSON Schema algebra.
1110+
10961111
The `openai-chat` adapter translates Responses `text.format` and Chat Completions
10971112
`response_format` through one internal format, then emits `response_format` on the upstream chat
10981113
wire. That remains the default because silently returning prose breaks clients that requested a

‎tests/moonshot-tool-schema.test.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,74 @@ describe("Moonshot tool schema normalization (issue #2673)", () => {
325325
expect(shared.type).toBe("string");
326326
});
327327

328+
test("intersects bounds when both sides define the same property", async () => {
329+
const parameters = await emittedParameters("https://api.moonshot.ai/v1", {
330+
name: "shared_property_bounds_tool",
331+
parameters: {
332+
type: "object",
333+
$defs: {
334+
Base: {
335+
type: "object",
336+
properties: {
337+
looserSibling: { type: "string", minLength: 5, maxLength: 10 },
338+
tighterSibling: { type: "string", minLength: 1, maxLength: 100 },
339+
},
340+
},
341+
},
342+
properties: {
343+
value: {
344+
$ref: "#/$defs/Base",
345+
properties: {
346+
looserSibling: { type: "string", minLength: 1, maxLength: 99 },
347+
tighterSibling: { type: "string", minLength: 5, maxLength: 10 },
348+
},
349+
},
350+
},
351+
},
352+
});
353+
354+
const value = (parameters?.properties as Record<string, Record<string, unknown>>).value!;
355+
const properties = value.properties as Record<string, Record<string, unknown>>;
356+
expect(properties.looserSibling).toMatchObject({ minLength: 5, maxLength: 10 });
357+
expect(properties.tighterSibling).toMatchObject({ minLength: 5, maxLength: 10 });
358+
});
359+
360+
test("intersects bounds recursively inside shared object properties", async () => {
361+
const parameters = await emittedParameters("https://api.moonshot.ai/v1", {
362+
name: "nested_shared_property_bounds_tool",
363+
parameters: {
364+
type: "object",
365+
$defs: {
366+
Base: {
367+
type: "object",
368+
properties: {
369+
shared: {
370+
type: "object",
371+
properties: { leaf: { type: "string", minLength: 5, maxLength: 10 } },
372+
},
373+
},
374+
},
375+
},
376+
properties: {
377+
value: {
378+
$ref: "#/$defs/Base",
379+
properties: {
380+
shared: {
381+
type: "object",
382+
properties: { leaf: { type: "string", minLength: 1, maxLength: 99 } },
383+
},
384+
},
385+
},
386+
},
387+
},
388+
});
389+
390+
const value = (parameters?.properties as Record<string, Record<string, unknown>>).value!;
391+
const shared = (value.properties as Record<string, Record<string, unknown>>).shared!;
392+
const leaf = (shared.properties as Record<string, Record<string, unknown>>).leaf!;
393+
expect(leaf).toMatchObject({ minLength: 5, maxLength: 10 });
394+
});
395+
328396
test("leaves data-valued keywords alone, even when they look like schemas", async () => {
329397
// `enum` lists VALUES. Recursing into it treated a literal object carrying a "$ref"
330398
// string as a reference node and stripped the key, silently changing a value the tool

0 commit comments

Comments
 (0)