Skip to content

Fix jq-less _jq_fallback dropping --arg, spurious recovery re-save on every startup - #676

Merged
fdaviddpt merged 2 commits into
mainfrom
fix/667
Sep 12, 2026
Merged

fdaviddpt merged 2 commits into
mainfrom
fix/667

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

session_was_saved() (scripts/session-start-hook.sh) calls $JQ -r --arg id "$1" "$SAVED_QUERY" "$LAST_SAVE_FILE". When jq is not on PATH -- the default on a fresh Git for Windows / Git Bash install, related to #660 -- $JQ is _jq_fallback (scripts/detect-tools.sh), whose shim consumes every leading -* token into a variable it never reads, then treats the next positional argument as the query and the one after that as the file. Fed --arg's flag+value pair, that lands on query="id", file="$1" (the session id, not a real file), and the shim always prints nothing.

session_was_saved compared that empty output against "saved" and always got false, so every previous session read as unsaved on a jq-less install, and session-start-hook.sh's recovery block spawned save-session.sh --force in the background on every single startup, regardless of whether the session had actually been saved.

Fix

session_was_saved() gets its own jq-free branch (checked via [ "$JQ" = "_jq_fallback" ]) that reads last-save.json with one direct Python call carrying the session id as argv, mirroring $SAVED_QUERY's own isline semantics exactly (a JSON number, never a bool, finite, equal to its own floor). Every other $JQ/$JQ_BIN --arg call site under scripts/ already guards itself with command -v jq first, so this was the only call site that ever reached the fallback with --arg -- teaching the generic shim a full --arg parser would have been strictly more code for the same coverage.

Tests

tests/test_jq_fallback_arg_667.py drives the real hook via subprocess, with jq present and with jq masked off PATH (a real PATH-stripping repro, not a mock, using a shim dir to keep coreutils working per tests/spawn_counting.py's own convention). Paired negative ("saved" -> no re-save invocation) and positive control ("not saved" -> re-save invocation observed with the correct argv), plus a third case pinning a self-review finding (a malformed sessions shape must read as unsaved on both the jq and jq-free paths, matching jq's own hard-error-abort behaviour on that shape).

Red before the fix: the jq-absent negative case spuriously observed a save-session.sh --force invocation for a session that genuinely had been saved. Green after, 6/6.

Docs

changelog.d/667.fixed.md, and a paragraph in docs/windows.md (not itself a docs_targets entry -- .oss.json's docs_targets lists only README.md -- but directly named by this issue and covering exactly the code path fixed here; README.md itself was read and needs no change, its jq-recommendation language is still accurate and this fix does not make jq unnecessary elsewhere in the codebase).

Self-review

Explore and oss:auditor both reviewed the committed diff. Explore found a jq/Python parity gap in the new branch's handling of a malformed sessions shape (fixed, with a new pinning test). The auditor found that the new test's save-session.sh stub was written with default newline translation, risking a CRLF-corrupted shebang on Git Bash/MSYS windows-latest CI (fixed, written with newline="" instead, following the convention tests/spawn_counting.py's own make_shim_dir already established for the identical defect class).

Closes #667. Related to #660.

🤖 Generated with Claude Code

[AI-generated]

fdaviddpt and others added 2 commits September 12, 2026 12:24
… every startup (#667)

session_was_saved() (scripts/session-start-hook.sh) calls
`$JQ -r --arg id "$1" "$SAVED_QUERY" "$LAST_SAVE_FILE"`. When jq is not on
PATH (the default on a fresh Git for Windows / Git Bash install, related to
#660), $JQ is _jq_fallback (scripts/detect-tools.sh), whose shim consumes
every leading `-*` token into a variable it never reads, then treats the
next positional argument as the query and the one after that as the file --
fed --arg's pair, that lands on query="id", file="$1" (the session id, not
a real file), and the shim always prints nothing.  session_was_saved
compared that empty output against "saved" and always got false, so every
previous session read as unsaved and session-start-hook.sh's recovery block
spawned save-session.sh --force on every single jq-less startup, regardless
of whether the session had actually been saved.

Fix: session_was_saved() gets its own jq-free branch (checked via
`[ "$JQ" = "_jq_fallback" ]`) that reads last-save.json with one direct
Python call carrying the session id as argv, mirroring $SAVED_QUERY's own
isline semantics exactly (type, non-bool, finite, floor-equal). Every other
$JQ/$JQ_BIN --arg call site under scripts/ already guards itself with
`command -v jq` first, so this is the only call site that ever reached the
fallback with --arg; teaching the generic shim a --arg parser would have
been strictly more code for the same coverage.

Tests: tests/test_jq_fallback_arg_667.py, paired negative ("saved" ->
no re-save) and positive control ("not saved" -> re-save observed), each
run with jq present and jq masked off PATH. Red before the fix: the
jq-absent negative case spuriously observed a save-session.sh --force
invocation; green after.

Docs: changelog.d/667.fixed.md, and a one-paragraph note in
docs/windows.md (not itself a docs_targets entry, but directly named by
this issue and covering exactly the code path fixed here).

Related to #660.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01ScdCn6Xq622o2cRQhzVhi5
…t stub

Two findings from the Explore + oss:auditor pass over 869a901:

- Explore: the jq-free branch's `isinstance(sessions, dict)` guard silently
  fell through to the legacy `.session`/`.line` check whenever `sessions`
  was present but malformed (e.g. a list) -- whereas real jq's own
  `(.sessions // {})[$id]` throws a hard runtime error the instant
  `.sessions` is present but not an object, aborting the WHOLE
  `$SAVED_QUERY` program with no fallback. A corrupted `sessions` value
  sitting next to an otherwise-valid legacy `session`/`line` pair therefore
  read as "saved" on the new Python path but "unsaved" on real jq for the
  same file. Fixed with an explicit short-circuit matching jq's abort
  behaviour, pinned by a new parametrized test
  (test_a_malformed_sessions_shape_reads_as_unsaved_on_both_paths) run with
  jq present and jq absent both.

- oss:auditor: the new test's save-session.sh stub was written via
  Path.write_text's default universal-newline translation, which turns
  every `\n` into `\r\n` on Windows -- a broken `#!/bin/bash\r` shebang
  under Git Bash/MSYS, the exact defect class tests/spawn_counting.py's
  own make_shim_dir already exists to avoid (#669/#670). Unlike a shimmed
  COUNTED tool, this stub has no same-named real binary on PATH to
  silently fall through to, so a corrupted shebang would fail loudly with
  a "bad interpreter" error and break the positive-control assertion on
  windows-latest for a reason unrelated to #667. Fixed by writing it with
  newline="" instead.

Both findings were argued as worth fixing directly (mechanism stated,
one-file blast radius, same subsystem as the original fix) rather than
filed or logged to trap.d/.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01ScdCn6Xq622o2cRQhzVhi5
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.

_jq_fallback drops --arg, so jq-less installs report every previous session as unsaved and spawn save-session.sh --force on every start

1 participant