Skip to content

fix: guard session-end-hook.sh log/report_error calls the same way #365 fixed the other two hooks - #374

Merged
fdaviddpt merged 2 commits into
mainfrom
fix/372
Aug 27, 2026
Merged

fdaviddpt merged 2 commits into
mainfrom
fix/372

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Closes #372.

What was broken

scripts/session-end-hook.sh sources scripts/log.sh with stderr suppressed and then calls log/report_error unguarded (lines 148, 158, 164, 209). log.sh returns before it defines log(), report_error() or dispatch() when it cannot create $REMEMBER_DIR/logs -- so on a store that can never be created, both calls hit an undefined function. On macOS log additionally resolves to Apple's /usr/bin/log unified-logging CLI even when undefined as a shell function (type log is true either way), so the first call dumps 19 lines of usage text to stderr and exits 64; the second, report_error, fails outright with command not found. Neither changes the hook's exit status, which stays 0 either way -- so this was fully silent to anything that only checks returncode, including /remember:doctor, which reads hook-errors.log and reports nothing wrong.

Sibling hooks post-tool-hook.sh and user-prompt-hook.sh already carry a declare -F guard for exactly this (#365, added 15 minutes before #368 registered this fourth hook without it) -- declare -F, not type/command -v, is required specifically because of the macOS /usr/bin/log collision.

The judgment call

Unlike the no-op stubs the hot paths in the other two hooks install, the fallback here is not a no-op: session-end-hook.sh's own docstring (EXIT CODES) promises a failed flush is "reported loudly ... rather than swallowed silently", and the guarded report_error call is reachable only on the path where that promise is being broken. log.sh's own log() documents "Falls back to stderr if log file is unwritable" -- the stub reproduces exactly that fallback, for exactly that reason: hook-errors.log and the notices channel user-prompt-hook.sh reads (#200, #253) both live under $REMEMBER_DIR, the same directory that failed to get created, so stderr is the only channel actually available. Confirmed by reading bootstrap-dirs.sh:225-231, which only redirects this hook's own stderr into hook-errors.log once that directory exists.

Tests

The existing test at tests/test_session_end_hook_345.py:227 (test_missing_remember_dir_on_a_real_project_still_exits_zero) removes .remember but leaves the project directory itself writable -- bootstrap-dirs.sh's mkdir -p recreates it successfully, so this test never actually reaches the guarded report_error call, contrary to a literal reading of the issue's framing. Verified empirically. It is now the paired must-not-fire positive control ("WARNING" not in result.stderr).

A new test, test_must_fire_unwritable_store_reports_a_warning, chmods the project directory itself to 0o555 after removing .remember, which genuinely forces bootstrap-dirs.sh's mkdir to fail. Watched red against the unfixed script (reproducing log: Unknown subcommand 'hook' then report_error: command not found, exactly the issue's mechanism) and green against the fix.

The sibling at line 200 (test_no_project_at_all_still_exits_zero) does not have the same defect: with no CLAUDE_PROJECT_DIR, resolve-paths.sh's soft-fail mode exits the hook at line 144, before log.sh is even sourced -- a different, pre-existing, out-of-scope early-exit path. Left unmodified.

Review

Both spawned reviewers (Explore + oss:auditor) converged on the same finding: the new must-fire test's os.chmod(0o555) technique had no root-uid skip guard, unlike five sibling test files in this repo that use the identical technique. root ignores directory write-permission bits, so under a root CI runner the test would fail for an unrelated reason instead of skipping. Fixed with the matching @pytest.mark.skipif(hasattr(os, "geteuid") and os.geteuid() == 0, ...) guard, in a second commit, along with a stale "line 158" reference in the new guard's own comment (drifted after other comment lines were added around it).

Below-bar item (no reachable caller beyond a doc nicety): none.

Docs

README.md (the repo's only docs_targets entry) gets two new sentences distinguishing this narrow "store could never be created at all" case from the broader flush-failure case the file already documents -- the warning here goes to stderr, not hook-errors.log, and will not show up in /remember:doctor. A changelog fragment is at changelog.d/372.fixed.md, checked with .oss/assemble_changelog.py --check.

…eady fixed the other two hooks (#372)

log.sh returns before defining log(), report_error() or dispatch() when
$REMEMBER_DIR/logs cannot be created; session-end-hook.sh called both
unguarded, so on an unwritable store its documented "reported loudly,
never swallowed" promise was inverted exactly where it mattered -- a
`command not found` on the degraded path, and on macOS a shelled-out
call to Apple's /usr/bin/log first. The declare -F guard now matches
post-tool-hook.sh and user-prompt-hook.sh, with fallbacks that still
write a WARNING to stderr instead of going silent, since neither
hook-errors.log nor the notice channel is reachable on this path.

Co-Authored-By: Max <noreply>
Review found two adjacent nits in the #372 fix: the new must-fire test
relied on os.chmod(0o555) to force mkdir to fail, but this repo's own
convention for that exact technique (five other test files) skips
under root, since root ignores directory write-permission bits -- add
the matching geteuid()==0 skip guard. The guard's own comment in
session-end-hook.sh referenced "line 158" for the report_error call by
line number, which drifted as comment lines were added around it --
reworded to name the branch instead of a line number.

Co-Authored-By: Max <noreply>
@fdaviddpt
fdaviddpt merged commit 44a5f0d into main Aug 27, 2026
13 checks passed
@fdaviddpt
fdaviddpt deleted the fix/372 branch August 27, 2026 12:53
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.

session-end-hook.sh calls log/report_error unguarded, so its one documented promise is inverted on the only path it was written for

1 participant