Skip to content

fix(cli): reject invalid token-window bounds - #5544

Closed
chqcjq wants to merge 3 commits into
morluto:mainfrom
chqcjq:fix/cli-token-window-bounds
Closed

chqcjq wants to merge 3 commits into
morluto:mainfrom
chqcjq:fix/cli-token-window-bounds

Conversation

@chqcjq

@chqcjq chqcjq commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #5532. CLI token limits and offsets must represent exact non-negative safe integers. Invalid windows return a complete INVALID_TOKEN_WINDOW diagnostic and exit 1 before command execution.

Previously negative/fractional values could produce inconsistent ranges or negative continuation offsets. The follow-up also rejects fractional spellings that JavaScript rounds to integers, including 1.0000000000000001, 9007199254740991.1, and underflow to zero.

Approach and scope

Validate each raw token spelling at the existing parser boundary, and expose the effective validity result to the output-argument validator. Decimal integrality is checked using coefficient digits and exponent placement, without exponent-sized padding. Remove the superseded number-only check. Preserve zero, exact decimal/exponent forms, integer radix forms, maximum-safe values and duplicate last-value precedence. No Incur dependency, MCP schema, provider capability, target authority or package metadata change. Maintainer scope decision: not applicable to this ordinary CLI input fix.

Review and CI follow-up

  • Commit 42a0dcb4c addresses the valid Codex P2 rounding finding. The added compiled CLI regression reproduces acceptance of the fractional spelling on the previous head and passes with the correction.
  • Merged main 74ed67da7 via ac8ea6256. The four old shard failures were stale recovery-text expectations in filteredFailureOutput.test.ts; they also fail with the original base implementation. Current main includes the upstream assertion/canonical-error corrections, and all five tests in that file now pass. No assertion was weakened locally to hide the failures.
  • Related fix(cli): clamp exhausted failure token-window start to the token total #5119 covers exhausted positive offsets; fix(cli): reject negative and fractional token window values #5578 overlaps the original negative/fractional-input fix. This updates the existing PR. Unicode slicing is outside this change.

Current executed validation

Windows, pinned Node 24.18.0/npm 11.16.0:

  • node scripts/run-development-tests.mjs focused tests/boundary/cli/cliTokenWindowBounds.test.ts tests/boundary/cli/cliOutput.test.ts tests/boundary/cli/filteredFailureOutput.test.ts — 45 passed.
  • Source-level probe of 192 generated decimal/exponent spellings against an independent exact BigInt oracle — passed. This is separate from the public CLI tests.
  • npm run check — passed.
  • npm run docs:check — passed, including 9 README consistency tests.
  • git diff --check and normal commit/pre-push checks — passed.

The full native-Windows npm run check:ci does not pass: its static/deep tasks, architecture checks and discovery of 2,106 test files pass, but the Python stage requires the POSIX-only resource module. The unchanged Python tests were instead executed in existing Ubuntu 24.04/WSL with Python 3.12.3:

  • python3 -m unittest bridge/native/test_rea_lldb_tracer.py bridge/pwndbg/test_core_context.py bridge/mitmproxy/test_capture.py -v — 41 passed.
  • python3 -m unittest discover -s experiments/analysis-efficiency -p 'test_*.py' -v — 91 passed, with a task-local official Node 24.18.0 Linux runtime verified against its published SHA-256. No global toolchain or repository test script was changed.

New-head remote CI/review is pending. The full JavaScript suite, macOS and performance benchmarks were not run. Python's WSL results do not establish Linux CLI or optional-provider coverage.

Author readiness

Human owner: @chqcjq

  • I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work.
  • I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps.
  • I declare this PR ready for initial maintainer review.

The human owner's initial readiness declarations are recorded above.

@chqcjq
chqcjq marked this pull request as ready for review October 20, 2026 14:41
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-23T01:32:44.108038Z 42a0dcb New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@greptile-apps

greptile-apps Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no blocking issues were found.

Summary

The PR rejects negative, fractional, and unsafe token-window bounds at the existing CLI input boundary.

  • The CLI rejects token limits and offsets that are not non-negative safe integers.

Reviews (2) · Last reviewed commit: "fix(cli): validate token bounds without ..." · Reviewed by Greptile

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f7c72000d0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/cliOutput.ts Outdated
["--token-limit", tokenLimit],
["--token-offset", tokenOffset],
] as const)
if (value !== undefined && (!Number.isSafeInteger(value) || value < 0))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Validate raw token bounds before Number rounding

parseCliOutputArguments has already applied Number(value) when this check runs, so inputs such as --token-limit 1.0000000000000001 and --token-offset 9007199254740991.1 are rounded to safe integers and this validation returns {ok: true}. Those fractional or out-of-range spellings therefore proceed as different window coordinates instead of producing the newly documented INVALID_TOKEN_WINDOW; retain and validate the raw token exactly before accepting it.

AGENTS.md reference: AGENTS.md:L31-L31

Useful? React with 👍 / 👎.

@CassieFox23

Copy link
Copy Markdown
Contributor

TL;DR: Fixed. Token values that are negative, fractional or too large are now refused. The error code is INVALID_TOKEN_WINDOW. Not fixed: my repro in #5531, where a non-numeric value like not-a-number still prints plain TOON even with --json. Your PR body already says #5531 is out of scope, so that issue stays open.

Thanks, chqcjq, for the fix. I tested head f7c7200 on Linux.

My #5531 repro, run on main ccc7da3 and on the head (Node 24.18.0, stdout captured):

Command (after capabilities) main head
--json --token-limit not-a-number exit 1, TOON same, exit 1, TOON
--json --token-offset nope exit 1, TOON same
--format jsonl --token-limit not-a-number exit 1, TOON same
--format json --token-limit not-a-number exit 1, TOON same
--json --format bogus exit 1, TOON same
--json --format (control) exit 1, JSON same

The cause, from reading the diff: a non-numeric value sets parseError. validateCliOutputArguments stops there. That happens before the new check runs.

What the PR does fix (head versus main):

Command (after capabilities) main head
--format toon --token-limit -5 exit 0, prints everything exit 1, INVALID_TOKEN_WINDOW
--json --token-limit 2.5 exit 1, UNSUPPORTED_OUTPUT_COMBINATION exit 1, INVALID_TOKEN_WINDOW
--format toon --token-offset -100 --token-limit 20 exit 0, "showing tokens -100–-80" exit 1, INVALID_TOKEN_WINDOW
--format toon --token-limit 5 exit 0, window 0–5 exit 0, window 0–5
Check Node 22.23.1 Node 24.18.0 Node 26.11.1
cliTokenWindowBounds.test.ts (2 new tests) plus cliOutput.test.ts (38 existing), on the head 40 passed 40 passed 40 passed
cliTokenWindowBounds.test.ts copied into main not run 1 failed, 1 passed not run
npm run typecheck on the head not run passed not run

On main, the failing test is "rejects incoherent token windows": --token-limit -5 exits 0 instead of 1. The other test, which checks zero and the last value winning, passes on main too, so it is a guard and not a repro.

Bot thread: the P2 about rounding before the check is real, but small. Number("1.0000000000000001") is 1. So --token-limit 1.0000000000000001 is accepted and shows tokens 0–1. --token-offset 9007199254740991.1 is accepted as 9007199254740991. The PR #5578 also accepts --token-limit 1.0000000000000001 (I ran it). From its diff it would accept the offset case too, which I did not run.

How it differs from #5578, which I reviewed earlier today (I ran #5578 at 1ab5663 on the same box):

Case #5544 #5578
Error code INVALID_TOKEN_WINDOW INVALID_TOKEN_VALUE
--token-limit=9007199254740992 exit 1, refused exit 0, accepted
--token-limit -5 --token-limit 5 exit 0, last value wins exit 1, refused
Negative, fractional refused refused
Non-numeric value still TOON still TOON
docs/cli.md updated not touched

Both PRs fix the same negative and fractional cases and both leave #5531 open. They edit the same function. A three-way text merge of src/cliOutput.ts shows 3 conflicts, so only one needs to land. The error code is the main decision.

Two other items the PR names are not changed by it. #5119 is a separate PR. It covers the reversed range when an offset is past the end. --token-offset 100000 --token-limit 5 still shows "tokens 100000–7620 of 7620" on the head. I did not test #5530.

Not run

Test setup
Commands
npm ci --ignore-scripts
npm run prepare
npm run build:cached
npm run test:prepare
npm run test:focused -- tests/boundary/cli/cliTokenWindowBounds.test.ts tests/boundary/cli/cliOutput.test.ts
npm run typecheck
node scripts/rea.mjs capabilities --json --token-limit not-a-number
node scripts/rea.mjs capabilities --json --token-offset nope
node scripts/rea.mjs capabilities --format jsonl --token-limit not-a-number
node scripts/rea.mjs capabilities --format json --token-limit not-a-number
node scripts/rea.mjs capabilities --json --format bogus
node scripts/rea.mjs capabilities --json --format
node scripts/rea.mjs capabilities --format toon --token-limit -5
node scripts/rea.mjs capabilities --format toon --token-limit 1.0000000000000001
node scripts/rea.mjs capabilities --format toon --token-limit=9007199254740992
node scripts/rea.mjs capabilities --format toon --token-limit -5 --token-limit 5

Install, prepare, build and test:prepare ran on Node 24, exit 0 each. The two test files ran once per Node version on the head, exit 0 each. On main only the new test file ran, on Node 24, exit 1. Each CLI command ran once per tree. Stdout was parsed with a JSON loader to decide JSON or not.

🤖 Written and tested with Claude Code (Sonnet 5.5, verified by Opus 5.5), posted with my OK 🦊

@CassieFox23 CassieFox23 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TL;DR: The rounding bug I found is fixed in 42a0dcb. The one red CI shard is a test on main that this PR does not touch. Two things are still open: the PR conflicts with main in docs/cli.md, and my #5531 repro is still out of scope.

Thanks, chqcjq, for taking the rounding finding. This is a static read of 42a0dcb, with no build and no run. My earlier test comment was on f7c7200.

Fixed

  • Rounding before the check. Before, --token-limit 1.0000000000000001 and --token-offset 9007199254740991.1 passed as safe integers, because Number() rounds them. Now isValidTokenBound (src/cliOutput.ts line 244) also reads the spelling and rejects any non-zero digit past the decimal point. Reading the code, both inputs from my last comment now return INVALID_TOKEN_WINDOW. That check is from reading the code; I did not run the build.
  • Test coverage. tests/boundary/cli/cliTokenWindowBounds.test.ts (lines 16 to 19) covers those two inputs plus 1e-400 and -1e-400. Lines 62 to 70 keep exact forms valid: 5.000, 500e-2, .5e1, 0x5 and the last-value-wins case. The CI log of Test shard (3/8) shows that file with 2 tests passing on this head.
  • Docs. docs/cli.md line 554 now says fractional text is not accepted just because it rounds to an integer.
  • Last-value-wins is kept. --token-limit -5 --token-limit 5 still shows tokens 0 to 5, as in my earlier table.

Still open

  • The branch conflicts with main. GitHub reports CONFLICTING. A text merge of main (9066444) with this head shows one conflict. It is in docs/cli.md near line 592. This PR's token paragraph sits where main added the --format md paragraph. src/cliOutput.ts merged without a conflict marker in that text merge. Whether it still behaves the same I have not checked.
  • #5531 is not fixed, and the PR never claimed it. A non-numeric value like not-a-number makes parseError true, and validateCliOutputArguments returns { ok: true } at src/cliOutput.ts line 289, before the new check. So --json --token-limit not-a-number should still print TOON. I did not re-run it on this head. The issue stays open.
  • Overlap with #5578. #5578 is still open and edits the same function. Only one of the two can land, and the error code (INVALID_TOKEN_WINDOW here, INVALID_TOKEN_VALUE there) is still a maintainer call. #5119 is also still open.

CI

CI required, Portable tests and Test shard (8/8) failed on this head (run 39884215959). Shard 8/8 failed in tests/process-global/commandFinalizationCancellation.test.ts. The failures were preserves the analysis-view result and exit status for bundle: SIGINT during projection and the SIGTERM variants. This PR does not change that file. Main's own push run on 74ed67d (run 39877590680) failed the same three tests. A fix for that file merged as #6431 at 3:30 AM UTC on 23 Oct, and neither 74ed67d nor this head has it. Merging main should clear it. Shards 1, 2, 3, 4, 5 and 7 were cancelled before they finished, so I count only what their logs show.

Not run

  • The new head: no build, no tests, no CLI runs.
  • Whether the merged result with current main passes.
  • Any macOS or Windows check.

🤖 Written with Claude Code (Sonnet 5.5, verified by Opus 5.5), posted with my OK 🦊

@morluto

morluto commented Oct 26, 2026 •

Copy link
Copy Markdown
Owner

Thank you for the follow-up on exact decimal spellings. I am keeping this PR open despite #5532 being closed: on main at ca3fd7b, cliOutputValueRejection checks Number.isSafeInteger(Number(value)), so a fractional spelling that rounds to an integer can pass that check. This PR's current description covers that remaining case, including 1.0000000000000001. If you would like to continue, could you focus the diff and regression coverage on exact spelling validation while retaining main's existing output contract? That comparison is source-derived; I have not run the current branch or a CLI reproduction in this pass.

@morluto morluto closed this Nov 7, 2026
@morluto morluto added area: cli CLI adapter and one-shot commands bug Something isn't working labels Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: cli CLI adapter and one-shot commands bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Negative and fractional token-window values produce inconsistent pages

3 participants