Skip to content

io.sh: a usage error from --limit or an on/off flag stops before the request (exit 2) - #31

Merged
makriman merged 2 commits into
mainfrom
fix/28-io-sh-usage-exit
Dec 5, 2026
Merged

makriman merged 2 commits into
mainfrom
fix/28-io-sh-usage-exit

Conversation

@makriman

@makriman makriman commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Problem

io.sh briefs --limit 999 (also abc, 0) prints the usage error on stderr, then runs the query anyway and exits 0. The CLI exits 2 and sends nothing. An agent that mistypes a limit gets exit 0 and a plausible page of results.

Root cause

limit_of and onoff report a bad value by calling usage, which exits 2. But every caller ran them inside a command substitution, for example l=$(limit_of 10). exit inside $(...) ends only the subshell, and io.sh runs with set -uo pipefail and no -e, so the parent shell carried on with an empty value.

The same pattern was at five more call sites, not only briefs:

  • digest --limit, topics list --limit, breaking recent --limit: the request had already gone out (validation came after it), then jq --argjson n "" failed and the command exited 0.
  • prefs set --digest maybe and --nudges maybe: the usage error was printed and a malformed body, {"digest":{"on":}}, was sent with PATCH. The exit code then depended on how the API answered that body.

Change

  • limit_of sets LIMIT and onoff sets ONOFF; both are called directly, so usage exits the script. A comment at the definitions says why they must never go inside $(...).
  • digest, topics list and breaking recent check --limit before the request, so a usage error sends nothing.
  • The CLI already exited 2 on these, but on digest, topics list and breaking recent it checked --limit after the request. It now checks first, so both clients send nothing on a usage error.
  • CHANGELOG.md (Unreleased) and a CONTRIBUTING.md line about the bats file.

Tests

  • test/usage-exit.test.mjs (in npm test): for the CLI and io.sh, every invalid --limit case and both on/off flags exit 2, print nothing on stdout, give the usage error, and the REST stub records no request. Valid values still work, and the PATCH body is checked. Before the fix: 2 of 3 tests fail (io.sh prefs set --digest maybe exits 6 after sending the malformed body; the CLI's digest --limit 0 reaches the API).
  • test/io-sh.bats (bats test/io-sh.bats): the same io.sh cases with a fake curl on PATH that logs each call, plus a check that no validator appears inside $(...). Before the fix: 4 of 6 fail. After: 6 of 6 pass, with jq and with IO_SH_NO_JQ=1.

Commands run on this branch:

  • npm test (Node 22.23.4, jq 1.7, with ../mcp installed): 35 tests, 35 pass, 0 skipped.
  • bats test/io-sh.bats (bats 1.14.0): 6 of 6 pass; IO_SH_NO_JQ=1 bats test/io-sh.bats: 6 of 6 pass.
  • npm pack --dry-run: the same 6 files. The version-strings check from ci/workflows/test.yml passes.
  • Not run: shellcheck (not installed here), Node 20 and 24.

For the reviewer

  • The bats file is not in npm test or the parked workflow yet, because bats is not a Node dependency. The Node test covers the same cases and does run in npm test.
  • topics list --limit without jq is still accepted and ignored (the trim needs jq). That is a separate parity gap, not this bug; it is not changed here.

Fixes #28

limit_of and onoff called usage (exit 2) but were run inside a command
substitution, where exit ends only the subshell. io.sh has no set -e, so
it printed the usage error, carried on with an empty value and exited 0.
They now set LIMIT and ONOFF and are called directly, and --limit is
checked before the request on digest, topics list and breaking recent.
The CLI also checks --limit before the request on those three commands.

Tests: test/usage-exit.test.mjs (CLI and io.sh against the stub) and
test/io-sh.bats (io.sh with a fake curl).
@makriman

makriman commented Dec 5, 2026

Copy link
Copy Markdown
Contributor Author

Review by Grok Bot (Indian Opinion QA agent)

Tested at head d781edc. Verdict: works.

Fixes: #28. A bad --limit or on/off value now exits 2 before any request, in both io.sh and the CLI.

What I checked

  • npm test: this head 35 tests, 34 pass, 0 fail, 1 skipped. Main: 32 tests, 31 pass. The skip is the existing MCP bridge test, which needs IO_MCP_DIR. It skips on main too.

  • bats test/io-sh.bats (bats from the distro package): 6 of 6 pass. With IO_SH_NO_JQ=1: 6 of 6 pass.

  • Failing first. I copied the new bats file and test/usage-exit.test.mjs onto main:

    • bats: 4 of 6 fail (tests 1, 2, 3 and 6).
    • node: 2 of 3 fail.

    The new tests do catch the bug.

  • Direct repro with a fake curl on PATH that logs each call:

io.sh command main this head
briefs --limit 999 usage error printed, exit 0, curl called exit 2, curl not called
digest --limit 0 usage error plus a jq error, exit 0, curl called exit 2, curl not called
prefs set --digest maybe usage error, exit 0, curl called (PATCH sent) exit 2, curl not called
briefs --limit 5 exit 0 exit 0
  • shellcheck io.sh: 5 notes on main and 5 on this head, the same ones (SC2015, SC2016). Nothing new.

Not checked

  • Node 20. I ran Node 24.22.1 only.

Review

  • The comment at the limit_of and onoff definitions explains the $(...) trap well, and the bats test that greps for validators inside command substitutions will stop it coming back.
  • Not blocking: bats isn't in npm test or the parked workflow, as the PR notes. Adding bats test/ as a step once CI lands would keep the two suites from drifting.

Automated checks (this head vs current main)

Command This PR main
npm ci exit 0 exit 0
npm test exit 0: ℹ tests 35; ℹ pass 34; ℹ fail 0 exit 0: ℹ tests 32; ℹ pass 31; ℹ fail 0
npm audit --omit=dev found 0 vulnerabilities found 0 vulnerabilities

I'm Grok Bot, the automated QA agent for Indian Opinion. This is a comment-only review. I don't approve, request changes or merge. Local stack: PHP 8.4.28, Composer 2.11.3, MariaDB 11.8.9, Node 24.22.1, WordPress 7.1.5.

@makriman
makriman marked this pull request as ready for review December 5, 2026 20:04
@makriman
makriman merged commit 20e83ce into main Dec 5, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

io.sh: briefs --limit with an invalid value prints the usage error but runs the query anyway and exits 0

1 participant