fix: address Copilot review round 17 — shared branch narrowing, null admissibility

- The name-based narrowing is extracted as `narrowBySuppliedNames` and used
  by the CLI coercion as well as the form: an undiscriminated union whose
  branches type a shared name differently was losing the coercion for every
  argument, including the ones that identify the branch unambiguously. The
  type-agreement fallback now also polls only the const-compatible branches.
- TUI missingRequiredFields accepts `null` only where the property admits it,
  matching the web gate: branch fields are rendered optional, so a
  `default: null` on a non-nullable required field reached the check and the
  call went out with a value the schema rejects.

Signed-off-by: cliffhall <cliff@futurescale.com>
This commit is contained in:
cliffhall 2026-08-26 06:38:46 -04:00
commit 95030cfdb6
3 changed files with 117 additions and 22 deletions

View file

@ -819,6 +819,24 @@ describe("schemaToForm", () => {
expect(missingRequiredFields(schema, {})).toEqual(["constructor"]);
});
it("accepts null only where the schema admits it", () => {
// Branch fields render optional, so a `default: null` on a non-nullable
// required field reaches this check — and `type: "string"` rejects it.
const strict = {
type: "object",
properties: { a: { type: "string" } },
required: ["a"],
};
expect(missingRequiredFields(strict, { a: null })).toEqual(["a"]);
const nullable = {
type: "object",
properties: { a: { type: ["string", "null"] } },
required: ["a"],
};
expect(missingRequiredFields(nullable, { a: null })).toEqual([]);
});
it("checks the root's own required fields when there is no union", () => {
const schema = {
type: "object",

View file

@ -4,6 +4,7 @@
import type { FormStructure, FormSection, FormField } from "ink-form";
import {
admitsNull,
isStringEnum,
normalizeNullableUnion,
} from "@inspector/core/json/nullableUnion.js";
@ -461,12 +462,25 @@ export function missingRequiredFields(
branches.length === 0
? base
: branches[selectedBranchIndex(base, branches, rawValues)]!.schema;
const properties = effective.properties ?? {};
return (effective.required ?? []).filter((name) => {
// `hasOwn` first: an argument legally named `constructor` would otherwise
// resolve to the inherited one and read as supplied, and the call would go
// out without it.
if (!Object.hasOwn(decoded, name)) return true;
const value = decoded[name];
if (value === null) {
// `null` counts as supplied only where the schema admits it — the same
// test the web gate applies. A branch field is rendered optional here, so
// a `default: null` on a non-nullable one reaches this check and would
// otherwise send a value the schema rejects.
const property = properties[name];
return (
typeof property !== "object" ||
property === null ||
!admitsNull(property)
);
}
return value === undefined || value === "";
});
}

View file

@ -156,6 +156,31 @@ describe("JSON Utils", () => {
).toEqual({ kind: "a", value: 3 });
});
it("identifies the branch from the argument names supplied (#2123)", () => {
const undiscriminated: Tool = {
name: "undiscriminated",
inputSchema: {
type: "object",
anyOf: [
{
type: "object",
properties: { a: { type: "string" }, value: { type: "number" } },
},
{
type: "object",
properties: { b: { type: "string" }, value: { type: "boolean" } },
},
],
},
};
// `a` belongs to the first branch alone, so `value` is that branch's
// number — falling straight through to cross-branch type agreement would
// drop the coercion and send "3".
expect(
convertToolParameters(undiscriminated, { a: "x", value: "3" }),
).toEqual({ a: "x", value: 3 });
});
it("leaves an ambiguously typed argument uncoerced (#2123)", () => {
const ambiguous: Tool = {
name: "ambiguous",