Skip to content

feat(jq): support --arg and --argjson variables - #210

Merged
ferdingler merged 2 commits into
strands-agents:mainfrom
ferdingler:feat/jq-arg-argjson
Nov 12, 2026
Merged

ferdingler merged 2 commits into
strands-agents:mainfrom
ferdingler:feat/jq-arg-argjson

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Description

jq rejected --arg and --argjson, which agents use all the time to pass shell values into a filter. So something like this just failed:

$ jq --arg n x '.name == $n'
jq: invalid option   # before
true                 # after (with {"name":"x"} as input)

Now --arg binds a string and --argjson binds parsed JSON. Both can be repeated and work in any filter, and $ARGS.named has them too. Invalid JSON errors out with exit 2 like real jq, and when a name is repeated the first one wins, same as jq 1.8. No dependency changes. jaq already supports global variables, so this doesn't touch the jaq version (#187).

I compared the outputs against jq 1.8.1 and they match. The one difference is that $ARGS keys come out sorted, but that's already how this jq prints every object. I added tests for the new flags and the error cases, and the existing jq tests still pass. I left out --args/--jsonargs, so $ARGS.positional is always empty for now.

Related Issues

Part of #207

Documentation PR

N/A

Type of Change

New feature

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.

  • I ran the relevant test suites for the bindings I touched (cargo test --workspace --all-targets, pytest tests/python, npm test)
  • If I touched Rust, I ran cargo fmt and cargo clippy

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@ferdingler
ferdingler marked this pull request as ready for review November 12, 2026 19:33
@ferdingler
ferdingler requested a review from a team as a code owner November 12, 2026 19:33
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approve. --arg and --argjson match jq 1.8.1 in every case I checked, and I found no bugs.

I ran the full test suite at a147011: 2212 passed, 0 failed. An adversarial pass of about 50 edge cases found nothing caused by this diff. There are two optional nits below. The CI rollup shows FAILURE only because the first run was cancelled when it was re-triggered. In the current run every finished job has passed, and a few macOS jobs are still queued.

✅ What I verified
  • Tests: cargo test --workspace --all-targets at a147011 gives 2212 passed, 0 failed. All 13 new tests depend on the new flags.

  • Compared with jq 1.8.1 src/main.c (tag jq-1.8.1):

    • First binding wins, and a later --argjson value is never parsed (jv_object_has, lines 466 and 474). So --argjson a 1 --argjson a invalid gives 1.
    • Bad JSON prints jq: invalid JSON text passed to --argjson and exits 2 (line 477).
    • The missing-parameter messages are byte-identical (lines 463 and 471).
    • --arg ARGS x '$ARGS' leaves $ARGS as the builtin object, with named.ARGS = "x", the same as jq.
  • Edge cases that held:

    • Values starting with a dash: --arg a -x and --arg a --foo bind correctly.
    • Options can come after the filter.
    • Shadowing works with as $a, def f($a), and reduce.
    • Invalid --argjson input exits 2: '1 2', '', {bad, [1,].
    • Deep nesting (5000 levels) errors cleanly with no stack overflow.
    • 1000 --arg pairs run instantly.
    • Global-variable order in jaq matches Ctx::new (compile.rs:836, filter.rs:51-53).
  • Not findings, because main already behaves this way:

    • --argjson a 1e1000 is rejected (jq 1.8 accepts it).
    • 100000000000000000000001 prints as 1e+23.
    • $ARGS keys come out sorted.

    In each case echo <same> | jq . on stdin behaves identically, since both go through serde_json without preserve_order / arbitrary_precision.

Appendix: non-blocking (2)
  1. ⚪ No differential case backs the jq 1.8.1 parity claim. tests/differential/ is recorded against jq 1.8.1 (README.md:53) and already has jq_*.sh cases. The new tests hardcode their expected output instead. A small jq_arg.sh would make the parity claim checkable in CI. Good candidates: two variables, first-wins on a duplicate name, an --argjson object, and invalid JSON checked by exit code.
  2. ⚪ Help text alignment. The descriptions on the new lines in src/commands/jq.rs:16-17 start at column 28, while the other options start at column 22. For example:
      -j, --join-output   No newline after each output
          --arg NAME VALUE      Bind $NAME to the string VALUE
    
    Either align all the lines to the wider column or shorten the new ones. This is purely cosmetic.

Dropped: --arg=a x is accepted, while jq 1.8 rejects it because it matches options with strcmp. Accepting it does no harm.

Pre-existing and not caused by this PR: in debug builds, $a + 1 with --argjson a 9223372036854775807 panics on i64 overflow inside jaq-json (lib.rs:700). echo 9223372036854775807 | jq '. + 1' panics the same way, so the bug belongs to jaq-json, not this PR.

Evidence: strands-agents/shell/pr/210/cargo-test.log, edge-cases.txt, adversarial-log.txt.

@ferdingler

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. I checked both optional nits. The 13 integration cases already run in CI, and I also compared eight CLI cases against jq 1.8.1. That caught the duplicate --argjson case before review; the regression failed before the fix and passes now. I’m keeping the existing test coverage in this PR, since the proposed differential fixture would repeat those same cases.

I’m leaving the help spacing as-is too. The wider new entries keep both parameter names visible; realigning all the existing options would be formatting-only churn. The current CI run is still finishing its macOS jobs, and every finished job has passed.

@ferdingler
ferdingler merged commit a40f645 into strands-agents:main Nov 12, 2026
31 of 60 checks passed
@ferdingler
ferdingler deleted the feat/jq-arg-argjson branch November 12, 2026 23:08
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.

2 participants