Ghostlight/sidecar/claude-sdk/test/schema.test.ts

137 lines
4.3 KiB
TypeScript
Raw Normal View History

// Spec test 17: every schema Ghostlight's one emitter actually produces
// converts, and everything outside that grammar throws. `schemas.json` is
// written by the Rust test `schema_fixtures_match_the_checked_in_sidecar_grammar`,
// so the two halves cannot drift apart.
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import test from "node:test";
import { z } from "zod";
Make Rust the one argument validator on the SDK transport too The sidecar registered a closed Zod schema, so the SDK judged arguments before Rust ever saw them: an extra key was stripped, a missing required property was refused with an MCP error, and the call never reached the handler. The connector path decodes the same argument in Rust, where `deny_unknown_fields` and required-field decoding turn a bad argument into the gap the evaluator records. `toolInputSchema` now builds a loose object whose every property catches, so nothing is stripped and nothing is refused. The conversion survives only to advertise: the in-process MCP server derives the schema the model sees from this object, and that schema still carries every property, type, bound, and `required` name the emitter produced. What is deleted is the strictness that made it a second validator -- `.strict()`, the closed-object requirement, and the every-property-is-required requirement. One case cannot be carried through as written: a value outside a property's own type empties that property, so it reaches Rust as an absent field rather than a wrong one. Rust still names the gap; the SDK still does not answer the call. The undispatched-tool_use protocol violation stays, because no argument reaches it any more but a call never offered to the handler still does. `resultFault` is the result-subtype mapping lifted out of `runQuery`, so `error_max_turns` becoming an ordinary round and every other subtype becoming its named fault are both asserted without a credential. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-06 02:34:09 +02:00
import { toolInputSchema } from "../src/schema.ts";
const emitted: { name: string; parameters_json: string }[] = JSON.parse(
readFileSync(fileURLToPath(new URL("./schemas.json", import.meta.url)), "utf8"),
);
test("every emitted tool schema converts", () => {
assert.ok(emitted.length > 0, "the schema fixture is empty");
for (const entry of emitted) {
Make Rust the one argument validator on the SDK transport too The sidecar registered a closed Zod schema, so the SDK judged arguments before Rust ever saw them: an extra key was stripped, a missing required property was refused with an MCP error, and the call never reached the handler. The connector path decodes the same argument in Rust, where `deny_unknown_fields` and required-field decoding turn a bad argument into the gap the evaluator records. `toolInputSchema` now builds a loose object whose every property catches, so nothing is stripped and nothing is refused. The conversion survives only to advertise: the in-process MCP server derives the schema the model sees from this object, and that schema still carries every property, type, bound, and `required` name the emitter produced. What is deleted is the strictness that made it a second validator -- `.strict()`, the closed-object requirement, and the every-property-is-required requirement. One case cannot be carried through as written: a value outside a property's own type empties that property, so it reaches Rust as an absent field rather than a wrong one. Rust still names the gap; the SDK still does not answer the call. The undispatched-tool_use protocol violation stays, because no argument reaches it any more but a call never offered to the handler still does. `resultFault` is the result-subtype mapping lifted out of `runQuery`, so `error_max_turns` becoming an ordinary round and every other subtype becoming its named fault are both asserted without a credential. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-06 02:34:09 +02:00
const schema = toolInputSchema(entry.parameters_json, entry.name);
const declared = Object.keys(
JSON.parse(entry.parameters_json).properties as Record<string, unknown>,
).sort();
Make Rust the one argument validator on the SDK transport too The sidecar registered a closed Zod schema, so the SDK judged arguments before Rust ever saw them: an extra key was stripped, a missing required property was refused with an MCP error, and the call never reached the handler. The connector path decodes the same argument in Rust, where `deny_unknown_fields` and required-field decoding turn a bad argument into the gap the evaluator records. `toolInputSchema` now builds a loose object whose every property catches, so nothing is stripped and nothing is refused. The conversion survives only to advertise: the in-process MCP server derives the schema the model sees from this object, and that schema still carries every property, type, bound, and `required` name the emitter produced. What is deleted is the strictness that made it a second validator -- `.strict()`, the closed-object requirement, and the every-property-is-required requirement. One case cannot be carried through as written: a value outside a property's own type empties that property, so it reaches Rust as an absent field rather than a wrong one. Rust still names the gap; the SDK still does not answer the call. The undispatched-tool_use protocol violation stays, because no argument reaches it any more but a call never offered to the handler still does. `resultFault` is the result-subtype mapping lifted out of `runQuery`, so `error_max_turns` becoming an ordinary round and every other subtype becoming its named fault are both asserted without a credential. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-06 02:34:09 +02:00
assert.deepEqual(Object.keys(schema.shape).sort(), declared, entry.name);
}
});
test("a property's description reaches the advertised schema", () => {
const schema = toolInputSchema(
JSON.stringify({
type: "object",
additionalProperties: false,
required: ["due", "kind"],
properties: {
due: { type: "integer", minimum: 0, maximum: 9, description: "a clock reading" },
kind: { type: "string", enum: ["a"], description: "which shape" },
},
}),
"described",
);
const advertised = z.toJSONSchema(schema, { io: "input" }) as {
properties: Record<string, { description?: string }>;
};
assert.equal(advertised.properties.due?.description, "a clock reading");
assert.equal(advertised.properties.kind?.description, "which shape");
});
Make Rust the one argument validator on the SDK transport too The sidecar registered a closed Zod schema, so the SDK judged arguments before Rust ever saw them: an extra key was stripped, a missing required property was refused with an MCP error, and the call never reached the handler. The connector path decodes the same argument in Rust, where `deny_unknown_fields` and required-field decoding turn a bad argument into the gap the evaluator records. `toolInputSchema` now builds a loose object whose every property catches, so nothing is stripped and nothing is refused. The conversion survives only to advertise: the in-process MCP server derives the schema the model sees from this object, and that schema still carries every property, type, bound, and `required` name the emitter produced. What is deleted is the strictness that made it a second validator -- `.strict()`, the closed-object requirement, and the every-property-is-required requirement. One case cannot be carried through as written: a value outside a property's own type empties that property, so it reaches Rust as an absent field rather than a wrong one. Rust still names the gap; the SDK still does not answer the call. The undispatched-tool_use protocol violation stays, because no argument reaches it any more but a call never offered to the handler still does. `resultFault` is the result-subtype mapping lifted out of `runQuery`, so `error_max_turns` becoming an ordinary round and every other subtype becoming its named fault are both asserted without a credential. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-06 02:34:09 +02:00
test("a converted schema carries every property's own type", () => {
const schema = toolInputSchema(
JSON.stringify({
type: "object",
additionalProperties: false,
required: ["detail", "count", "flag", "choice", "maybe", "many"],
properties: {
detail: { type: "string" },
count: { type: "integer", minimum: 1, maximum: 4 },
flag: { type: "boolean" },
choice: { type: "string", enum: ["a", "b"] },
maybe: { anyOf: [{ type: "string" }, { type: "null" }] },
many: { type: "array", items: { type: "string" } },
},
}),
"sample",
);
Make Rust the one argument validator on the SDK transport too The sidecar registered a closed Zod schema, so the SDK judged arguments before Rust ever saw them: an extra key was stripped, a missing required property was refused with an MCP error, and the call never reached the handler. The connector path decodes the same argument in Rust, where `deny_unknown_fields` and required-field decoding turn a bad argument into the gap the evaluator records. `toolInputSchema` now builds a loose object whose every property catches, so nothing is stripped and nothing is refused. The conversion survives only to advertise: the in-process MCP server derives the schema the model sees from this object, and that schema still carries every property, type, bound, and `required` name the emitter produced. What is deleted is the strictness that made it a second validator -- `.strict()`, the closed-object requirement, and the every-property-is-required requirement. One case cannot be carried through as written: a value outside a property's own type empties that property, so it reaches Rust as an absent field rather than a wrong one. Rust still names the gap; the SDK still does not answer the call. The undispatched-tool_use protocol violation stays, because no argument reaches it any more but a call never offered to the handler still does. `resultFault` is the result-subtype mapping lifted out of `runQuery`, so `error_max_turns` becoming an ordinary round and every other subtype becoming its named fault are both asserted without a credential. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-06 02:34:09 +02:00
const admitted = schema.parse({
detail: "ok",
count: 2,
flag: true,
choice: "a",
maybe: null,
many: ["a"],
}) as Record<string, unknown>;
assert.deepEqual(admitted, {
detail: "ok",
count: 2,
flag: true,
choice: "a",
maybe: null,
many: ["a"],
});
// A value outside a property's own type empties that property and nothing
// else. Rust decides what an absent field means.
const dropped = schema.parse({ detail: "ok", count: 9, choice: "c" }) as Record<
string,
unknown
>;
assert.equal(dropped.detail, "ok");
assert.equal(dropped.count, undefined);
assert.equal(dropped.choice, undefined);
});
test("anything outside the grammar throws", () => {
const refused = [
{ oneOf: [{ type: "string" }] },
{ allOf: [{ type: "string" }] },
{ $ref: "#/definitions/thing" },
{ description: "no type at all" },
{ type: "number" },
];
for (const node of refused) {
assert.throws(
() =>
Make Rust the one argument validator on the SDK transport too The sidecar registered a closed Zod schema, so the SDK judged arguments before Rust ever saw them: an extra key was stripped, a missing required property was refused with an MCP error, and the call never reached the handler. The connector path decodes the same argument in Rust, where `deny_unknown_fields` and required-field decoding turn a bad argument into the gap the evaluator records. `toolInputSchema` now builds a loose object whose every property catches, so nothing is stripped and nothing is refused. The conversion survives only to advertise: the in-process MCP server derives the schema the model sees from this object, and that schema still carries every property, type, bound, and `required` name the emitter produced. What is deleted is the strictness that made it a second validator -- `.strict()`, the closed-object requirement, and the every-property-is-required requirement. One case cannot be carried through as written: a value outside a property's own type empties that property, so it reaches Rust as an absent field rather than a wrong one. Rust still names the gap; the SDK still does not answer the call. The undispatched-tool_use protocol violation stays, because no argument reaches it any more but a call never offered to the handler still does. `resultFault` is the result-subtype mapping lifted out of `runQuery`, so `error_max_turns` becoming an ordinary round and every other subtype becoming its named fault are both asserted without a credential. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-06 02:34:09 +02:00
toolInputSchema(
JSON.stringify({
type: "object",
additionalProperties: false,
required: ["field"],
properties: { field: node },
}),
"refused",
),
`${JSON.stringify(node)} was accepted`,
);
}
});
Make Rust the one argument validator on the SDK transport too The sidecar registered a closed Zod schema, so the SDK judged arguments before Rust ever saw them: an extra key was stripped, a missing required property was refused with an MCP error, and the call never reached the handler. The connector path decodes the same argument in Rust, where `deny_unknown_fields` and required-field decoding turn a bad argument into the gap the evaluator records. `toolInputSchema` now builds a loose object whose every property catches, so nothing is stripped and nothing is refused. The conversion survives only to advertise: the in-process MCP server derives the schema the model sees from this object, and that schema still carries every property, type, bound, and `required` name the emitter produced. What is deleted is the strictness that made it a second validator -- `.strict()`, the closed-object requirement, and the every-property-is-required requirement. One case cannot be carried through as written: a value outside a property's own type empties that property, so it reaches Rust as an absent field rather than a wrong one. Rust still names the gap; the SDK still does not answer the call. The undispatched-tool_use protocol violation stays, because no argument reaches it any more but a call never offered to the handler still does. `resultFault` is the result-subtype mapping lifted out of `runQuery`, so `error_max_turns` becoming an ordinary round and every other subtype becoming its named fault are both asserted without a credential. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-06 02:34:09 +02:00
test("an open object and a partly optional one convert and refuse nothing", () => {
// Openness and optionality are Rust's judgements, so neither is a
// registration failure here.
const open = toolInputSchema(
JSON.stringify({ type: "object", required: [], properties: {} }),
"open",
);
Make Rust the one argument validator on the SDK transport too The sidecar registered a closed Zod schema, so the SDK judged arguments before Rust ever saw them: an extra key was stripped, a missing required property was refused with an MCP error, and the call never reached the handler. The connector path decodes the same argument in Rust, where `deny_unknown_fields` and required-field decoding turn a bad argument into the gap the evaluator records. `toolInputSchema` now builds a loose object whose every property catches, so nothing is stripped and nothing is refused. The conversion survives only to advertise: the in-process MCP server derives the schema the model sees from this object, and that schema still carries every property, type, bound, and `required` name the emitter produced. What is deleted is the strictness that made it a second validator -- `.strict()`, the closed-object requirement, and the every-property-is-required requirement. One case cannot be carried through as written: a value outside a property's own type empties that property, so it reaches Rust as an absent field rather than a wrong one. Rust still names the gap; the SDK still does not answer the call. The undispatched-tool_use protocol violation stays, because no argument reaches it any more but a call never offered to the handler still does. `resultFault` is the result-subtype mapping lifted out of `runQuery`, so `error_max_turns` becoming an ordinary round and every other subtype becoming its named fault are both asserted without a credential. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-06 02:34:09 +02:00
assert.deepEqual(open.parse({ anything: 1 }), { anything: 1 });
const partial = toolInputSchema(
JSON.stringify({
type: "object",
additionalProperties: false,
required: ["a"],
properties: { a: { type: "string" }, b: { type: "string" } },
}),
"partial",
);
Make Rust the one argument validator on the SDK transport too The sidecar registered a closed Zod schema, so the SDK judged arguments before Rust ever saw them: an extra key was stripped, a missing required property was refused with an MCP error, and the call never reached the handler. The connector path decodes the same argument in Rust, where `deny_unknown_fields` and required-field decoding turn a bad argument into the gap the evaluator records. `toolInputSchema` now builds a loose object whose every property catches, so nothing is stripped and nothing is refused. The conversion survives only to advertise: the in-process MCP server derives the schema the model sees from this object, and that schema still carries every property, type, bound, and `required` name the emitter produced. What is deleted is the strictness that made it a second validator -- `.strict()`, the closed-object requirement, and the every-property-is-required requirement. One case cannot be carried through as written: a value outside a property's own type empties that property, so it reaches Rust as an absent field rather than a wrong one. Rust still names the gap; the SDK still does not answer the call. The undispatched-tool_use protocol violation stays, because no argument reaches it any more but a call never offered to the handler still does. `resultFault` is the result-subtype mapping lifted out of `runQuery`, so `error_max_turns` becoming an ordinary round and every other subtype becoming its named fault are both asserted without a credential. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-06 02:34:09 +02:00
assert.deepEqual(partial.parse({ b: "set" }), { b: "set" });
});