fix(cli): bound advertised decimal-token schemas by the domain range (#9808)

`cliDecimalField` published the domain range on the integer branch and only
the fixed `^[0-9]+$` guard on the decimal-token branch, and JSON Schema
bounds do not constrain strings. A client validating raw argv text against
`--schema` therefore admitted out-of-range tokens such as `--row-limit=0` or
`--block-size=255` that the parser rejects with `VALIDATION_ERROR`.

Publish the domain range on the token branch through the schema-constraint
owner, keeping the CLI's decimal leading-zero convention and the existing
parser diagnostics: the declaration is metadata, so accepted values and
`too_small`/`too_big` messages are unchanged. Non-range refinements such as
the power-of-two entropy block size stay parser-only, and an open-ended
domain still publishes its floor. The getting-started guide states the same
contract.

Fixes #9348
This commit is contained in:
IvenKoo authored and GitHub committed 2026-11-18 09:11:20 -05:00
1 parent 41cf9654da
commit 80f533da8a
9 files changed
+338 -61

No files matched your search

+45 -31
View File
@@ -22,7 +22,9 @@ const numericBranches = (schema: unknown): boolean => {
return false;
};
const bounds = (schema: unknown): Array<{ min: number; max: number }> => {
const bounds = (
schema: unknown,
): Array<{ min: number; max: number; candidate: number }> => {
if (!isRecord(schema)) return [];
const nested = ["anyOf", "oneOf", "allOf"].flatMap((key) =>
Array.isArray(schema[key]) ? schema[key].flatMap(bounds) : [],
@@ -47,7 +49,7 @@ const bounds = (schema: unknown): Array<{ min: number; max: number }> => {
? schema.default
: min;
return Number.isSafeInteger(preferred) && preferred >= min && preferred <= max
? [...nested, { min: preferred, max }]
? [...nested, { min, max, candidate: preferred }]
: nested;
};
@@ -147,14 +149,26 @@ describe("decimal integer CLI admission", () => {
// Decimal tokens are the only numeric spelling.
}
}
const range = bounds(item)[0];
const candidate =
bounds(item)[0]?.min ??
range?.candidate ??
(probed === undefined ? undefined : Number(probed));
if (candidate === undefined) {
failures.push(`${label}: no in-range decimal candidate`);
continue;
}
const decimal = String(candidate);
if (range !== undefined) {
for (const value of [range.min - 1, range.max + 1]) {
if (value < 0) continue;
for (const token of [String(value), `000${value}`]) {
if (validate(token))
failures.push(
`${label}: advertised schema accepts out-of-range ${token}`,
);
}
}
}
try {
expect(read(decimal)).toBe(candidate);
} catch (error) {
@@ -163,7 +177,7 @@ describe("decimal integer CLI admission", () => {
);
continue;
}
const admitsLeadingZeros = JSON.stringify(item).includes("^[0-9]+$");
const admitsLeadingZeros = validate(`0${decimal}`);
try {
const parsed = read(`0${decimal}`);
if (parsed !== candidate)
@@ -211,36 +225,36 @@ describe("decimal integer CLI advertisement", () => {
return JSON.parse(stdout) as unknown;
};
it("publishes the token grammar in --schema for arguments and options", async () => {
it("publishes the token range in --schema for arguments and options", async () => {
const validateToken = (schema: unknown, token: string): boolean =>
new Ajv2020({ strict: false }).compile(
schema as Parameters<Ajv2020["compile"]>[0],
)(token) === true;
const location = await schemaOf("trace-web-source-location");
expect(location).toMatchObject({
args: {
properties: {
line: {
description: "One-based generated line",
anyOf: [
{ type: "integer", minimum: 1 },
{ type: "string", pattern: "^[0-9]+$" },
],
},
},
},
const line = (location as { args: { properties: { line: unknown } } }).args
.properties.line;
expect(line).toMatchObject({
description: "One-based generated line",
anyOf: [{ type: "integer", minimum: 1 }, { type: "string" }],
});
expect(await schemaOf("inspect-web-network-capture")).toMatchObject({
options: {
properties: {
record: {
type: "array",
items: {
anyOf: [
{ type: "integer", minimum: 0 },
{ type: "string", pattern: "^[0-9]+$" },
],
},
},
},
},
for (const token of ["1", "0001"])
expect(validateToken(line, token), token).toBe(true);
for (const token of ["0", "0000"])
expect(validateToken(line, token), token).toBe(false);
const capture = await schemaOf("inspect-web-network-capture");
const record = (
capture as {
options: { properties: { record: { items: unknown } } };
}
).options.properties.record.items;
expect(record).toMatchObject({
anyOf: [{ type: "integer", minimum: 0 }, { type: "string" }],
});
for (const token of ["0", "03"])
expect(validateToken(record, token), token).toBe(true);
expect(validateToken(record, "-1")).toBe(false);
expect(await schemaOf("environment-serve")).toMatchObject({
options: {
properties: {
@@ -0,0 +1,52 @@
import { Ajv2020 } from "ajv/dist/2020.js";
import { expect } from "vitest";
import { cliTest } from "../../support/cli/cliFixture.js";
cliTest.for([
{
command: "inspect-sqlite-database",
field: "rowLimit",
flag: "--row-limit",
accepted: ["1", "0001", "9007199254740991"],
rejected: ["0", "0000", "9007199254740992"],
},
{
command: "inspect-byte-entropy",
field: "blockSize",
flag: "--block-size",
accepted: ["256", "00256", "65536"],
rejected: ["0", "255", "65537", "0065537"],
},
])(
"advertises and enforces $command decimal bounds",
async (entry, { cli }) => {
const discovery = await cli.run({
arguments: [entry.command, "--schema", "--format", "json"],
});
expect(discovery.exitCode).toBe(0);
const schema = JSON.parse(discovery.stdout).options.properties[entry.field];
const validate = new Ajv2020({ strict: false }).compile(schema);
for (const token of entry.accepted)
expect(validate(token), token).toBe(true);
for (const token of entry.rejected) {
expect(validate(token), token).toBe(false);
const execution = await cli.run({
arguments: [
entry.command,
"/nonexistent/decimal-bounds-target",
`${entry.flag}=${token}`,
"--format",
"json",
],
});
expect(execution.exitCode).toBe(1);
expect(JSON.parse(execution.stdout)).toMatchObject({
code: "VALIDATION_ERROR",
fieldErrors: expect.arrayContaining([
expect.objectContaining({ path: entry.field }),
]),
});
}
},
);