refactor(schema): reuse freshly reduced output schemas (#17429)

This commit is contained in:
Vincent Koc
2026-12-06 15:17:10 +08:00
committed by GitHub
parent e522f8c5b4
commit 737c2a9250
+10 -21
View File
@@ -128,11 +128,11 @@ const reduceObjectComposition = (
* retaining that identity would misstate what the advertised document is.
* References are already inlined, so no identity is needed to resolve them.
*/
const withoutContentBoundIdentity = (
const inlineOutput = (
schema: Record<string, unknown>,
): Record<string, unknown> => {
): CompactedOutputSchema => {
const { $id: _id, $anchor: _anchor, ...advertised } = schema;
return advertised;
return { schema: advertised, presentation: "inline" };
};
/**
@@ -151,10 +151,7 @@ export const compactAdvertisedOutputSchema = (
references,
});
if (inline !== undefined && schemaDepth(inline) <= COMPACT_SCHEMA_MAX_DEPTH)
return {
schema: withoutContentBoundIdentity(inline),
presentation: "inline",
};
return inlineOutput(inline);
// An inlined schema that only exceeds the depth limit keeps every fitting
// branch. Pruning returns nothing when widening a child under oneOf or not
// could reject a valid result.
@@ -165,10 +162,7 @@ export const compactAdvertisedOutputSchema = (
schemaDepth(pruned) <= COMPACT_SCHEMA_MAX_DEPTH &&
serializedBytes(pruned) <= budgetBytes
)
return {
schema: withoutContentBoundIdentity(pruned),
presentation: "inline",
};
return inlineOutput(pruned);
}
// SDK 2.4.0 stamps `type: "object"` on a `$ref` union before this projection
// sees it. That union has no root `properties`; keep each alternative's
@@ -182,14 +176,12 @@ export const compactAdvertisedOutputSchema = (
keyword === undefined
? undefined
: reduceObjectComposition(schema, keyword, references);
const composedSchema =
composed === undefined ? undefined : withoutContentBoundIdentity(composed);
if (
composedSchema !== undefined &&
serializedBytes(composedSchema) <= budgetBytes &&
schemaDepth(composedSchema) <= COMPACT_SCHEMA_MAX_DEPTH
composed !== undefined &&
serializedBytes(composed) <= budgetBytes &&
schemaDepth(composed) <= COMPACT_SCHEMA_MAX_DEPTH
)
return { schema: composedSchema, presentation: "reduced" };
return { schema: composed, presentation: "reduced" };
// Every REA output contract is an object at the root. A non-object root still
// advertises an accepting shape rather than a projection that could reject
// valid canonical output.
@@ -198,10 +190,7 @@ export const compactAdvertisedOutputSchema = (
? reduceToShallowProperties(schema, OUTPUT_REDUCED_ANNOTATION, references)
: jsonValueSchema();
if (serializedBytes(reduced) <= budgetBytes)
return {
schema: withoutContentBoundIdentity(reduced),
presentation: "reduced",
};
return { schema: reduced, presentation: "reduced" };
// A root description can itself be large; keep the advertisement inside the
// budget instead of carrying it into a shape with no nested detail left.
const minimal = reduceToShallowProperties(