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

+83
View File
@@ -0,0 +1,83 @@
import { z } from "zod";
const digits = (count: number): string =>
count === 0 ? "" : count === 1 ? "[0-9]" : `[0-9]{${count}}`;
const digitRange = (low: number, high: number): string =>
low === high ? String(low) : `[${low}-${high}]`;
/** Lexicographic bounds for equal-width, unsigned decimal strings. */
const sameWidthRange = (low: string, high: string): string => {
if (low === high) return low;
const width = low.length;
if (low === "0".repeat(width) && high === "9".repeat(width))
return digits(width);
const first = Number(low[0]);
const last = Number(high[0]);
if (width === 1) return digitRange(first, last);
if (
low.slice(1) === "0".repeat(width - 1) &&
high.slice(1) === "9".repeat(width - 1)
)
return `${digitRange(first, last)}${digits(width - 1)}`;
if (first === last)
return `${first}${sameWidthRange(low.slice(1), high.slice(1))}`;
const branches = [
`${first}${sameWidthRange(low.slice(1), "9".repeat(width - 1))}`,
];
if (first + 1 <= last - 1)
branches.push(`${digitRange(first + 1, last - 1)}${digits(width - 1)}`);
branches.push(
`${last}${sameWidthRange("0".repeat(width - 1), high.slice(1))}`,
);
return `(?:${branches.join("|")})`;
};
/** Keep the floor of an open-ended domain, whose tokens may grow past it. */
const openEndedPattern = (minimum: number): string => {
if (minimum === 0) return "^[0-9]+$";
const low = String(minimum);
const atFloorWidth = sameWidthRange(low, "9".repeat(low.length));
return `^0*(?:${atFloorWidth}|[1-9][0-9]{${String(low.length)},})$`;
};
/**
* Publish the domain's numeric range on the decimal-token branch without
* changing parser diagnostics. Refinements that are not a range stay
* parser-only, so an advertised schema can admit a token the parser rejects;
* it never rejects a token the parser admits.
*/
export const decimalTokenPattern = (domain: z.ZodNumber): string => {
const schema = z.toJSONSchema(domain);
const minimum = Math.max(
0,
Math.ceil(schema.minimum ?? 0),
typeof schema.exclusiveMinimum === "number"
? Math.floor(schema.exclusiveMinimum) + 1
: 0,
);
const maximum = Math.min(
Math.floor(schema.maximum ?? Infinity),
typeof schema.exclusiveMaximum === "number"
? Math.ceil(schema.exclusiveMaximum) - 1
: Infinity,
);
// Non-integer floors and ceilings have no safe-integer range to publish.
if (!Number.isSafeInteger(minimum)) return "^[0-9]+$";
if (maximum === Infinity) return openEndedPattern(minimum);
if (!Number.isSafeInteger(maximum)) return "^[0-9]+$";
if (minimum > maximum) return "^(?!)$";
const low = String(minimum);
const high = String(maximum);
const branches: string[] = [];
for (let width = low.length; width <= high.length; width++) {
branches.push(
sameWidthRange(
width === low.length ? low : `1${"0".repeat(width - 1)}`,
width === high.length ? high : "9".repeat(width),
),
);
}
// Tokens retain the CLI's decimal leading-zero convention.
return `^0*(?:${branches.join("|")})$`;
};
+7 -3
View File
@@ -1,5 +1,8 @@
import { z } from "zod";
import { declaredTokenPattern } from "../domain/schemaConstraints.js";
import { decimalTokenPattern } from "./decimalTokenPattern.js";
import { OPEN_BINARY_TOOL_CONTRACT } from "../contracts/sessionToolContracts.js";
import type { CliSessionTarget } from "./contractCliDescription.js";
@@ -14,9 +17,10 @@ type DecimalWrapper =
};
const decimalToken = (domain: z.ZodNumber) =>
z
.string()
.regex(DECIMAL_INTEGER_TOKEN, "Expected a decimal integer")
declaredTokenPattern(
z.string().regex(DECIMAL_INTEGER_TOKEN, "Expected a decimal integer"),
new RegExp(decimalTokenPattern(domain), "u"),
)
.pipe(z.coerce.number<string>())
.pipe(domain);
+91
View File
@@ -0,0 +1,91 @@
import { Ajv2020 } from "ajv/dist/2020.js";
import { z } from "zod";
import { expect, it } from "vitest";
import { cliDecimalField } from "./cli/options.js";
it("keeps advertised decimal bounds aligned across decimal-width transitions", () => {
const ajv = new Ajv2020({ strict: false });
for (const [minimum, maximum] of [
[0, 0],
[0, 16],
[1, 9],
[8, 12],
[76, 256],
[99, 100],
[256, 65536],
[9007199254740980, Number.MAX_SAFE_INTEGER],
] as const) {
const domain = z.number().int().min(minimum).max(maximum);
const field = cliDecimalField(domain);
const validate = ajv.compile(z.toJSONSchema(field));
const candidates = new Set([
...Array.from({ length: 1001 }, (_, index) => index),
minimum - 1,
minimum,
minimum + 1,
maximum - 1,
maximum,
maximum + 1,
]);
for (const value of candidates) {
if (value < 0) continue;
for (const token of [String(value), `000${value}`]) {
expect(validate(token), `${minimum}..${maximum}: ${token}`).toBe(
field.safeParse(token).success,
);
}
}
for (const token of ["", "1e2", "0x10", "+1", "1.0", "1\n", " 1"])
expect(validate(token), token).toBe(false);
}
});
it("retains exclusive bounds, wrappers, and parser-only refinements", () => {
const ajv = new Ajv2020({ strict: false });
const field = cliDecimalField(
z.number().int().gt(0).lt(10).nullable().default(2).readonly(),
);
const validate = ajv.compile(z.toJSONSchema(field));
for (const value of ["0", "10", "00010", 0, 10])
expect(validate(value)).toBe(false);
for (const value of ["1", "09", "0009", 1, 9, null])
expect(validate(value)).toBe(true);
expect(field.parse(undefined)).toBe(2);
expect(field.parse("09")).toBe(9);
expect(field.parse(null)).toBeNull();
const refined = cliDecimalField(
z
.number()
.int()
.min(1)
.max(10)
.refine((value) => value % 2 === 0),
);
expect(ajv.compile(z.toJSONSchema(refined))("3")).toBe(true);
expect(refined.safeParse("3").success).toBe(false);
});
it("keeps a positive floor when the domain has no ceiling", () => {
for (const minimum of [1, 5, 10, 256]) {
const field = cliDecimalField(z.number().min(minimum));
const validate = new Ajv2020({ strict: false }).compile(
z.toJSONSchema(field),
);
for (const value of [
minimum - 1,
minimum,
minimum + 1,
65536,
9007199254740992,
1e20,
]) {
for (const token of [String(value), `000${String(value)}`])
expect(validate(token), `${String(minimum)}..: ${token}`).toBe(
field.safeParse(token).success,
);
}
expect(validate("")).toBe(false);
}
});
+10
View File
@@ -158,6 +158,16 @@ const declaredPattern = (expression: RegExp): string => {
return expression.source;
};
/**
* Declare the pattern an argv token grammar accepts. The grammar's own check
* still rejects every token the pattern excludes, so the advertisement states
* the accepted set without adding a second runtime check.
*/
export const declaredTokenPattern = <Schema extends z.ZodString>(
schema: Schema,
pattern: RegExp,
): Schema => schema.meta({ pattern: declaredPattern(pattern) });
/**
* Check `predicate` and declare it as an `allOf` of patterns built from the
* same expressions, annotated with how the declaration relates to it.