Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ All notable changes to this project are documented here, following

### Fixed

- Reject non-finite and negative numeric CLI options instead of scanning with
invalid thresholds or silently ignoring invalid waste gates.
- Reject unknown `--targets` values with a clear error instead of silently
skipping all ignore-file writes.

Expand Down
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 21 additions & 6 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,41 @@ function parse(argv) {
for (let i = 0; i < argv.length; i++) {
const a = argv[i];
const val = () => (a.includes("=") ? a.split("=")[1] : argv[++i]);
const has = (name) => a === name || a.startsWith(`${name}=`);
if (a === "-h" || a === "--help") o.help = true;
else if (a === "-v" || a === "--version") o.version = true;
else if (a === "--write") o.write = true;
else if (a.startsWith("--targets")) o.targets = val();
else if (a.startsWith("--price")) o.price = Number(val());
else if (a.startsWith("--max-tokens")) o.maxTokens = Number(val());
else if (a.startsWith("--top")) o.top = Number(val());
else if (a.startsWith("--format")) o.format = val();
else if (a.startsWith("--fail-on-waste")) o.failOnWaste = Number(val());
else if (has("--targets")) o.targets = val();
else if (has("--price")) o.price = Number(val());
else if (has("--max-tokens")) o.maxTokens = Number(val());
else if (has("--top")) o.top = Number(val());
else if (has("--format")) o.format = val();
else if (has("--fail-on-waste")) o.failOnWaste = Number(val());
else if (!a.startsWith("-")) o.path = a;
}
return o;
}

function numericOptionError(o) {
if (!Number.isFinite(o.price) || o.price < 0)
return "ctxtrim: invalid --price: expected a non-negative number\n";
if (!Number.isFinite(o.maxTokens) || o.maxTokens < 0)
return "ctxtrim: invalid --max-tokens: expected a non-negative number\n";
if (!Number.isInteger(o.top) || o.top < 0)
return "ctxtrim: invalid --top: expected a non-negative integer\n";
if (o.failOnWaste != null && (!Number.isFinite(o.failOnWaste) || o.failOnWaste < 0 || o.failOnWaste > 100))
return "ctxtrim: invalid --fail-on-waste: expected a percentage from 0 to 100\n";
return null;
}

export function run(argv, { version }) {
const o = parse(argv);
if (o.help) { process.stdout.write(HELP); return 0; }
if (o.version) { process.stdout.write(version + "\n"); return 0; }
const target = o.path || ".";
if (!existsSync(target)) { process.stderr.write(`ctxtrim: path not found: ${target}\n`); return 2; }
const numberError = numericOptionError(o);
if (numberError) { process.stderr.write(numberError); return 2; }
if (!["text", "json"].includes(o.format)) { process.stderr.write(`ctxtrim: unknown --format\n`); return 2; }
const targets = o.targets.split(",").map((s) => s.trim()).filter(Boolean);
const unknownTargets = targets.filter((s) => !TARGETS[s]);
Expand Down
20 changes: 18 additions & 2 deletions test/ctxtrim.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import fs, { mkdtempSync, rmSync, truncateSync, writeFileSync } from "node:fs";
import { existsSync } from "node:fs";
import fs, { existsSync, mkdtempSync, rmSync, truncateSync, writeFileSync } from "node:fs";
import { spawnSync } from "node:child_process";
import { syncBuiltinESMExports } from "node:module";
import { tmpdir } from "node:os";
Expand Down Expand Up @@ -137,6 +136,23 @@ test("clean repo (only source) reports nothing to trim", () => {
assert.equal(s.totals.trimTokens, 0);
});

test("CLI rejects non-finite and negative numeric options", () => {
const invalid = [
["--max-tokens", "abc"],
["--max-tokens", "-5"],
["--price", "-3"],
["--top", "-1"],
["--fail-on-waste", "abc"],
["--fail-on-waste", "-1"],
];

for (const args of invalid) {
const result = spawnSync(process.execPath, [cli, repo, ...args], { encoding: "utf8" });
assert.equal(result.status, 2, `${args.join(" ")} should fail with exit 2\n${result.stderr}`);
assert.match(result.stderr, /ctxtrim: invalid --/);
}
});

test("unknown --targets values fail before writing ignore files", (t) => {
const root = mkdtempSync(join(tmpdir(), "ctxtrim-invalid-target-"));
t.after(() => rmSync(root, { recursive: true, force: true }));
Expand Down
Loading