Compare commits

...

4 Commits

Author SHA1 Message Date
diegosouzapw
3ee7ddb512 chore(cli): leave the translated CLI catalogs to the release refresh
Carrier for #18263 by @mcspanky802 (their fork does not allow maintainer edits). Since v3.8.54 a feature PR edits only bin/cli/locales/en.json; check:pr-derived-paths fails a PR that also writes the pt-BR, zh-CN and zh-TW catalogs. They go back to the base; the release refresh translates the new key, and the CLI shows it in English until then. The contributor's two commits are kept as authored.
2026-11-18 21:07:14 -03:00
diegosouzapw
4b9062d5eb Merge remote-tracking branch 'origin/release/v3.8.55' into fix/cli-limit-validation-18263 2026-11-18 20:44:01 -03:00
spanky
3f7b20170f docs(changelog): record CLI limit parsing fix 2026-10-26 14:55:58 -06:00
spanky
ad6fe67161 fix(cli): validate positive integer limits without radix corruption 2026-10-26 14:55:08 -06:00
7 changed files with 142 additions and 4 deletions

View File

@@ -4,6 +4,7 @@ import { setTimeout as sleep } from "node:timers/promises";
import { apiFetch, getBaseUrl } from "../api.mjs";
import { emit } from "../output.mjs";
import { t } from "../i18n.mjs";
import { parsePositiveInt } from "../utils/positiveInteger.mjs";
function fmtTs(v) {
if (!v) return "-";
@@ -97,7 +98,7 @@ export function registerBatches(program) {
batches
.command("list")
.option("--status <s>", t("batches.list.status"))
.option("--limit <n>", t("batches.list.limit"), parseInt, 50)
.option("--limit <n>", t("batches.list.limit"), parsePositiveInt, 50)
.action(async (opts, cmd) => {
const params = new URLSearchParams({ limit: String(opts.limit) });
if (opts.status) params.set("status", opts.status);

View File

@@ -4,6 +4,7 @@ import { createInterface } from "node:readline";
import { apiFetch, getBaseUrl } from "../api.mjs";
import { emit } from "../output.mjs";
import { t } from "../i18n.mjs";
import { parsePositiveInt } from "../utils/positiveInteger.mjs";
function fmtTs(v) {
if (!v) return "-";
@@ -49,7 +50,7 @@ export function registerFiles(program) {
files
.command("list")
.option("--purpose <p>", t("files.list.purpose"))
.option("--limit <n>", t("files.list.limit"), parseInt, 100)
.option("--limit <n>", t("files.list.limit"), parsePositiveInt, 100)
.action(async (opts, cmd) => {
const params = new URLSearchParams({ limit: String(opts.limit) });
if (opts.purpose) params.set("purpose", opts.purpose);

View File

@@ -13,6 +13,7 @@ import {
import { encryptCredential } from "../encryption.mjs";
import { openOmniRouteDb } from "../sqlite.mjs";
import { t } from "../i18n.mjs";
import { parsePositiveInt } from "../utils/positiveInteger.mjs";
import { registerProviderCrud } from "./provider-crud.mjs";
function publicConnection(connection) {
@@ -824,7 +825,7 @@ export function extendProvidersMetrics(providers) {
.option("--period <p>", t("providers.metrics.period"), "24h")
.option("--metric <m>", t("providers.metrics.metric"))
.option("--sort-by <field>", t("providers.metrics.sort"))
.option("--limit <n>", t("providers.metrics.limit"), parseInt, 50)
.option("--limit <n>", t("providers.metrics.limit"), parsePositiveInt, 50)
.option("--watch", t("providers.metrics.watch"))
.option("--compare <list>", t("providers.metrics.compare"))
.action(runProvidersMetrics);

View File

@@ -12,7 +12,8 @@
"dryRun": "[dry-run] would {action}",
"cancelled": "Cancelled.",
"jsonOpt": "Output as JSON",
"yesOpt": "Skip confirmation prompt"
"yesOpt": "Skip confirmation prompt",
"positiveIntegerRequired": "Limit must be a positive integer."
},
"setup": {
"title": "OmniRoute Setup",

View File

@@ -0,0 +1,18 @@
import { z } from "zod";
import { InvalidArgumentError } from "commander";
import { t } from "../i18n.mjs";
const positiveIntSchema = z
.string()
.trim()
.regex(/^[0-9]+$/)
.transform(Number)
.pipe(z.number().int().positive().max(Number.MAX_SAFE_INTEGER));
export function parsePositiveInt(value) {
const result = positiveIntSchema.safeParse(String(value ?? ""));
if (!result.success) {
throw new InvalidArgumentError(t("common.positiveIntegerRequired"));
}
return result.data;
}

View File

@@ -0,0 +1 @@
- **fix(cli):** Parse positive safe integer `--limit` values correctly for provider metrics, batch lists, and file lists instead of passing Commander's previous value to `parseInt` as a radix ([#18263](https://github.com/cheaper-inference/OmniRoute/pull/18263)) — thanks @mcspanky802.

View File

@@ -0,0 +1,115 @@
import assert from "node:assert/strict";
import { describe, it } from "node:test";
import { Command } from "commander";
import { registerBatches } from "../../bin/cli/commands/batches.mjs";
import { registerFiles } from "../../bin/cli/commands/files.mjs";
import { extendProvidersMetrics } from "../../bin/cli/commands/providers.mjs";
function findSubcommand(root: Command, path: string[]): Command {
let current: Command | undefined = root;
for (const segment of path) {
current = current?.commands.find((c) => c.name() === segment);
assert.ok(current, `Expected subcommand "${segment}" to exist`);
}
return current;
}
function buildProgramWithCapture(path: string[]) {
const program = new Command();
program.exitOverride();
program.configureOutput({
writeOut: () => {},
writeErr: () => {},
});
const providers = program.command("providers");
extendProvidersMetrics(providers);
registerBatches(program);
registerFiles(program);
const target = findSubcommand(program, path);
let capturedOpts: Record<string, unknown> | null = null;
target.action((opts: Record<string, unknown>) => {
capturedOpts = opts;
});
return {
parse: async (extraArgs: string[]) => {
capturedOpts = null;
await program.parseAsync(["node", "omniroute", ...path, ...extraArgs]);
return capturedOpts;
},
};
}
const TARGET_COMMANDS: Array<{
label: string;
path: string[];
defaultLimit: number;
}> = [
{ label: "providers metrics", path: ["providers", "metrics"], defaultLimit: 50 },
{ label: "batches list", path: ["batches", "list"], defaultLimit: 50 },
{ label: "files list", path: ["files", "list"], defaultLimit: 100 },
];
describe("CLI --limit positive integer parsing regression", () => {
for (const { label, path, defaultLimit } of TARGET_COMMANDS) {
describe(label, () => {
it(`uses default limit (${defaultLimit}) when --limit is omitted`, async () => {
const { parse } = buildProgramWithCapture(path);
const opts = await parse([]);
assert.ok(opts);
assert.equal(opts.limit, defaultLimit);
});
it("parses explicit valid positive integer --limit without radix corruption", async () => {
const { parse } = buildProgramWithCapture(path);
for (const [raw, expected] of [
["1", 1],
["10", 10],
["25", 25],
["250", 250],
[String(Number.MAX_SAFE_INTEGER), Number.MAX_SAFE_INTEGER],
] as const) {
const opts = await parse(["--limit", raw]);
assert.ok(opts);
assert.equal(opts.limit, expected);
assert.equal(Number.isNaN(opts.limit), false);
}
});
it("rejects zero, negative, malformed, and unsafe integer --limit values", async () => {
const { parse } = buildProgramWithCapture(path);
const invalidValues = [
"0",
"00",
"-1",
"-50",
"1.5",
"10abc",
"abc",
"",
" ",
"NaN",
"Infinity",
"1e5",
"0x10",
"9007199254740992",
"99999999999999999999",
];
for (const bad of invalidValues) {
await assert.rejects(
() => parse(["--limit", bad]),
(err: unknown) => {
assert.ok(err instanceof Error);
assert.match(err.message, /positive integer/i);
return true;
},
`Expected "${bad}" to be rejected for ${label}`
);
}
});
});
}
});