Skip to content

fix(#902): refuse an unsafe REMEMBER_DIR before mkdir; date+pid autonomous save-log names - #912

Merged
fdaviddpt merged 7 commits into
mainfrom
fix/902
Oct 5, 2026
Merged

fdaviddpt merged 7 commits into
mainfrom
fix/902

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Closes #902

What the report described

On 0.33.0 (macOS, bash 3.2, iCloud-synced project), a failed seed write to logs/autonomous/save-HHMMSS.log was followed by thousands of directories under the project root. Each was named after that log file's content, so a log directory path had been resolved to a relative, newline-containing value and then mkdir -p'd under cwd.

What this changes

  1. Refuses an unsafe store path before any mkdir. log.sh refuses a REMEMBER_DIR that is not absolute, or that contains a newline or carriage return, before its mkdir -p "$REMEMBER_DIR/logs". post-tool-hook.sh applies the same check before its own mkdir -p "$REMEMBER_DIR/logs/autonomous", the call site the report names, which runs on the fast path before log.sh is sourced. Windows drive paths (C:/x, C:\x) are accepted, as in lib-memory-dir.sh.
  2. Unique save-log names. Autonomous save logs are now named save-YYYYMMDD-HHMMSS-PID.log, so a seed can never append to a same-time-of-day file left over from an earlier day. The readers (the retention sweep in save-session.sh, the docs) use the *.log glob and need no change.

What it does not claim

  • The root cause was not reproduced. The original "file content becomes the path" corruption did not reproduce on current main (v0.40.0): not through the cold resolver, not through the env-cache publish/load pair, and not end to end through the reported fast-path scenario. What was ruled out is documented in tests/test_remember_dir_guard_902.py. These changes are defence in depth: whatever produced the value on 0.33.0 can no longer turn it into directories under the project.
  • Retention already exists. The report's point that "only empty autonomous logs are swept" predates After #483's fix, logs/autonomous/session-end-*.log is never reclaimed: the -empty sweep was that directory's only retention #487: since then, thresholds.autonomous_log_retention_days (default 7) reclaims every *.log there by age.
  • Other unguarded sites. Other mkdir -p "$REMEMBER_DIR/..." sites in session-start, session-end and write-handoff.sh are not guarded here. Centralising the check across files is a follow-up, not this fix.
  • Size budget. session-start builds to 122,176 B, under the 122,880 B budget (Directory: keep hook scripts under the scanner's 128 KiB limit, and warn before an edit gets near it #904); check_release_tree OK.

Tests

  • New files. tests/test_remember_dir_guard_902.py and tests/test_save_log_date_pid_name_902.py: a planted relative path and a planted newline path are each refused, with no directory created under cwd; a normal absolute path and Windows drive paths still pass.
  • Updated. test_locale_ranges_695.py: its allowlist line numbers moved.
  • Run locally on macOS: 15 passed; bash -n passes under bash 5 and /bin/bash 3.2; both new test files parse as Python 3.9.
  • Linux and Windows are covered by CI only.

Cleaning up existing junk directories (for the reporter)

From the project root, list first and only then delete. This removes only directories whose name is one or more HH:MM:SS [post-tool] save triggered lines:

find . -maxdepth 2 -type d -name '[0-9][0-9]:[0-9][0-9]:[0-9][0-9] \[post-tool\] save triggered*' -print
# after checking the list:
find . -maxdepth 2 -type d -name '[0-9][0-9]:[0-9][0-9]:[0-9][0-9] \[post-tool\] save triggered*' -prune -exec rm -r {} +

🤖 Generated with Claude Code

[AI-generated]

fdaviddpt and others added 7 commits October 5, 2026 23:25
… names

A failed seed write to logs/autonomous/save-HHMMSS.log, followed by the
lazy log() call that sources log.sh, left REMEMBER_LOG_DIR's resolution
unvalidated: nothing stopped mkdir -p from building a directory tree under
cwd if REMEMBER_DIR ever carried a relative value or an embedded newline.
log.sh now refuses such a value outright and reports to hook-errors.log
instead.

Separately, autonomous save-log names carried only HHMMSS, so a seed could
land on a same-time-of-day file left over from an earlier day still inside
the retention window. Names now carry the date and the writing process's
PID (save-YYYYMMDD-HHMMSS-PID.log).

Could not reproduce the original path-content corruption on current main
(0.40.0) via the cold resolver, the env-cache publish/load pair, or an
end-to-end run of the reported scenario on the warm/fast path -- documented
in tests/test_remember_dir_guard_902.py's module docstring. The guard and
the naming fix are defense-in-depth per the maintainer's own accepted plan
on the issue, independent of whichever mechanism produced the corruption on
0.33.0.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Self-review (Explore + oss:auditor) on the previous commit found two real
gaps:

1. The REMEMBER_DIR guard added to log.sh only protects log.sh's own
   mkdir -p "$REMEMBER_DIR/logs". It did not protect
   post-tool-hook.sh's own mkdir -p "$REMEMBER_DIR/logs/autonomous" -- the
   literal call site issue #902 names -- which runs on the fast path
   BEFORE log.sh is ever sourced. Fixed with a twin
   _remember_dir_is_unsafe() defined directly in post-tool-hook.sh,
   gating that mkdir. Several other mkdir -p "$REMEMBER_DIR/..." sites
   remain unguarded elsewhere in the hook suite (session-start-hook.sh,
   session-end-hook.sh, write-handoff.sh); centralising the check across
   five files is an architecture decision out of this issue's scope and is
   reported to the maintainer separately rather than patched here.

2. The commit message, changelog and test docstring overclaimed that the
   guard's FATAL message "reports to hook-errors.log" -- every hook sources
   log.sh as `source ... 2>/dev/null`, which swallows that stderr for the
   fast/common paths. This is the same swallowing the pre-existing
   mkdir-failure FATAL already had (not a new regression), but the prose
   was wrong to assert otherwise; corrected in the test module's own
   docstring.

Also fixes an allowlist drift in test_locale_ranges_695.py: inserting the
new guard function shifted four pre-existing, already-allowlisted bracket
ranges in post-tool-hook.sh down by 10 lines each.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
38e7313's guard answered an unsafe REMEMBER_DIR with `return 1` before
log.sh had defined log(), dispatch(), report_error() or _remember_date.
Every hook sources log.sh as `source ... 2>/dev/null`, so the FATAL was
swallowed and SessionStart died 127 on its first call -- breaking #294's
newline-in-project-dir case, which must exit 0 and write a
status=unavailable record (PR #912 CI, every leg).

The guard now skips the mkdir and points MEMORY_LOG_FILE at /dev/null (at
source time and on the midnight rollover), and dispatch() skips its own
mkdir -p "$REMEMBER_DIR/tmp" for the same refused value -- reachable again
now that dispatch is defined -- falling back to its existing "output NOT
SHOWN" path. Safe absolute paths take the same branches as before.

Tests: the newline and relative must-fire cases now also assert the source
completes, every caller-facing function is defined, and log() writes
nowhere under cwd; a new case drives dispatch() to a real listener with a
relative REMEMBER_DIR and asserts no tmp/ appears under cwd (red without
the dispatch guard). The plain-path positive control asserts the daily log
still lands in <store>/logs/. Changelog fragment no longer claims the
refusal reaches hook-errors.log.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
check_release_tree.py FAILs a nested default expansion (${X:-${Y}...}),
and log.sh is compiled into every hook, so the two MEMORY_LOG_FILE
assignments added in the previous commit tripped it eight times in the
built tree. Same behaviour, spelled as an if/else.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…th guards

PR #912 at 50a8acc: 4 failures on every Windows leg, all in this lane's
tests, none in the hooks.

1. The cold/warm env-cache case and both save-log name tests drive
   post-tool-hook.sh through test_post_tool_hook_spawns' `_run`, which
   invokes a bare "bash" -- on the Windows runner that is the System32
   WSL launcher (rc 1, UTF-16 "... to install." on stdout). That module
   skips win32 for exactly this reason; the importing tests now carry the
   same skip instead of silently dropping it.

2. The twin-logic test printed windows=0 on Git Bash. It handed the
   extracted function to `bash -c SCRIPT`, so on Windows the script was
   carried in CreateProcess's one command-line string and re-split by the
   MSYS runtime. The same function text, sourced from log.sh on disk,
   accepted C:/fakestore in the same job
   (test_must_not_fire_windows_drive_colon_slash passed on both Windows
   legs), so the guard itself is not judging drive letters unsafe there.
   The test now runs from a script file, the way a hook is always read.

New: test_drive_letter_forms_accepted_by_both_guards, run with OSTYPE
msys, cygwin and unset: log.sh's guard and post-tool-hook.sh's twin both
accept C:/x, c:/x, C:\x and /c/x and both refuse relative/x, C:x and a
newline-bearing value. Mutation check: dropping the backslash from
log.sh's drive-letter bracket turns all three parametrisations red.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…e_902

test_windows_skip_triage_497 requires every module that blanket-skips on
win32 to be listed in docs/windows-skip-triage.md. The save-log name test
inherits test_post_tool_hook_spawns' bare-"bash" harness, which is the WSL
launcher on the Windows runner; listed as convertible.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@fdaviddpt
fdaviddpt merged commit 7bd48ce into main Oct 5, 2026
19 checks passed
fdaviddpt added a commit that referenced this pull request Oct 6, 2026
* docs(trap.d): log round-1 v0.41.0 release-audit findings

Gate 3's oss:release-auditor found two non-blocking (misreports) findings in
the v0.40.0..HEAD delta (#911/#912): the #902 FATAL refusal is unreadable
because log()'s own stderr fallback never fires once MEMORY_LOG_FILE is
/dev/null, and the #902 changelog fragment overclaims mkdir-guard coverage
since bootstrap-dirs.sh builds the same tree ahead of the guard on the slow
path. Logged rather than filed per findings.md's misreports routing; /oss:curate
decides later whether either becomes a rule.

Co-Authored-By: Max <noreply>

* docs(trap.d): clarify misreports routing note

Co-Authored-By: Max <noreply>
@fdaviddpt
fdaviddpt deleted the fix/902 branch October 31, 2026 17:18
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