Skip to content

Never publish 120/50 as configured, and never lose a listener, on a store logs/ cannot write - #365

Merged
fdaviddpt merged 1 commit into
mainfrom
fix/358
Aug 27, 2026
Merged

fdaviddpt merged 1 commit into
mainfrom
fix/358

Conversation

@fdaviddpt

@fdaviddpt fdaviddpt commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

Both issues were filed by the release audit of v0.20.0..HEAD as misreports. Both are the same shape: scripts/log.sh returns early, before it sets anything, on a store whose logs/ directory it cannot create -- and two call sites downstream trusted that it had.

#358 -- a fabricated cooldown/threshold

_remember_env_cache_publish() in scripts/lib-env-cache.sh used to write REMEMBER_SAVE_COOLDOWN/REMEMBER_DELTA_THRESHOLD with shell defaults (${VAR:-120}, ${VAR:-50}) even when log.sh never set them. So an unwritable store silently published 120/50 into the env-resolution cache as though config.json had asked for exactly that -- and the load side cannot tell a default from a real answer, because both are just digits. Fixed by refusing the whole publish (return 0, no file written) unless both variables are genuinely non-empty, and removing the now-redundant :-120/:-50 defaults from the printf lines, so a default at the point of writing can never happen again.

#361 -- log/dispatch called on an undefined function

scripts/post-tool-hook.sh's fast path already guarded a log call with declare -F log >/dev/null 2>&1 || log() { :; } -- not type log, because on macOS /usr/bin/log (Apple's unified logging CLI) makes type log true whether or not a shell function exists -- and stubbed dispatch() outright. Its SLOW path, taken by every session's first tool call because there is nothing yet to replay, called both unconditionally. On a store whose logs/ cannot be created that shells out to the real log binary (22 lines of usage text on stderr) and then hits dispatch: command not found, from a hook documented "EXIT CODES: 0 Always" -- and an installed after_post_tool listener silently never runs. Fixed with the identical guard on the slow path's log call and a single declare -F dispatch guard placed right before the one dispatch "after_post_tool" call site (which correctly covers both branches -- a no-op on the fast path, where dispatch is already stubbed).

Adjacent, fixed in the same commit

While reproducing #361, the identical unguarded dispatch "after_user_prompt" call turned up in scripts/user-prompt-hook.sh's own slow path (reached only when a user has installed an after_user_prompt listener AND the store cannot write logs/ -- a narrower window than post-tool-hook.sh's, but the same file already open for #358's guard, the same mechanism, and a one-line fix). Given its own test (tests/test_prompt_hook_spawns.py::test_an_unwritable_logs_dir_does_not_break_after_user_prompt_dispatch), verified red before the fix and green after. Flagging it explicitly here rather than letting it ride silently: this is outside both issues' own text, in scope only because it is the same subsystem and the same file.

The judgment calls the brief asked me to make and state

Shared helper or not: I did not add one shared guard function used by both #358 and #361's fixes. #358 guards a publish (a value that outlives the process and must never lie); #361 guards a call (a function invocation that must never crash or shell out to a system binary). They are checked with the same primitive (declare -F / [ -n ] non-empty checks) but at different call sites with different consequences on failure, and a shared helper risked collapsing that distinction -- guarding the cheap half (the call) with the same weight as the expensive half (the durable file), or vice versa. Each fix is local to its own call site.

What the guard does when it fires: #358 skips the publish entirely rather than publishing without the two keys -- the load side (_remember_env_cache_load) already requires both keys non-empty and all-digits, so a cache missing them would be refused on load anyway; skipping the write outright is strictly cheaper for the identical outcome. #361 stubs log/dispatch as no-ops rather than exiting non-zero (unlike session-start-hook.sh's own exit 127 guard for the same underlying hazard) -- both post-tool-hook.sh and user-prompt-hook.sh are documented "EXIT CODES: 0 Always", and post-tool-hook.sh in particular must never block a tool call. An installed listener silently does not run on this one degraded call, which is the same tradeoff #358 makes (a stale-but-honest cache beats a live-but-fabricated one; a call that is silently skipped once beats a hook that crashes the agent's tool call).

Below-bar finding, in this same body

While reading scripts/post-tool-hook.sh around the fix, I noticed the FATAL echo in scripts/log.sh at the point of the early return (echo "FATAL: cannot create $REMEMBER_LOG_DIR" >&2) reaches the terminal unredirected on exactly the slow-path first-call case this PR fixes noise for -- it is not wrong, and it is the one honest signal a user gets that their store is broken, but it is one more line of stderr on an already-noisy failure mode nobody asked me to quiet further. I judged this below the bar to file or fix: it is a single honest diagnostic line, not a defect, and touching it would be a scope decision (should a hook this degraded print anything at all?) the two issues did not ask for. No class-level reachable bug here -- just a true statement worth having on record.

Closes #358
Closes #361

Verified by the maintainer

Independent red re-run, because the lane's own red is a claim I did not watch. Built a detached worktree at main (df3e35c), checked out only the three test files from fix/358 onto it so every source fix was absent, and ran them:

FAILED tests/test_env_cache_publish_unwritable_logs_358.py::test_an_unwritable_logs_dir_does_not_publish_a_default_cooldown_as_config
FAILED tests/test_post_tool_fast_path_350.py::test_the_slow_path_does_not_exec_system_log_or_call_undefined_dispatch
FAILED tests/test_prompt_hook_spawns.py::test_an_unwritable_logs_dir_does_not_break_after_user_prompt_dispatch
3 failed, 26 passed, 1 skipped

Three failures, one per fix — including the adjacent user-prompt-hook.sh one, which is the fix nobody filed an issue for and therefore the one with the weakest external check. The 26 pre-existing tests in those same files still passed with the fixes absent, which is the positive control: the harness was working, so the three failures are the tests biting rather than a broken run.

Observed on darwin / Python 3.13.15. The logs/-unwritable premise is built with POSIX permission semantics; whether it reproduces on Windows is reasoned, not observed.

Pre-flight premise checked before dispatch and both issues were still open against the code: declare -F log matched at post-tool-hook.sh:201 (the fast-path guard the issue says exists), and did not match at the slow path or at either publish site.

#358 -- _remember_env_cache_publish() defaulted REMEMBER_SAVE_COOLDOWN and
REMEMBER_DELTA_THRESHOLD to 120/50 at the point of writing, so a store whose
logs/ dir log.sh could not create published those digits as though config.json
had said so -- indistinguishable, on the load side, from a real answer. The
publish is now refused outright unless log.sh actually set both.

#361 -- post-tool-hook.sh's fast path already guarded `log` (`declare -F`, not
`type`, because macOS's own /usr/bin/log makes `type log` true regardless) and
stubbed `dispatch()` unconditionally. Its slow path -- taken by every session's
first tool call -- did neither, and shelled out to the system log binary and a
"command not found" dispatch on exactly the store #358 is about. Same guard
added there, and adjacent to it, the identical unguarded dispatch() call in
user-prompt-hook.sh's own slow path.

Closes #358, closes #361

Co-Authored-By: Max <noreply>
@fdaviddpt
fdaviddpt merged commit 98fbef5 into main Aug 27, 2026
13 checks passed
@fdaviddpt
fdaviddpt deleted the fix/358 branch August 27, 2026 11:08
fdaviddpt added a commit that referenced this pull request Aug 27, 2026
… fixed the other two hooks (#374)

* fix: guard session-end-hook.sh log/report_error the same way #365 already 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>

* fix: guard the new #372 test against root, and correct a stale line ref

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant