Skip to content

fix(#204): don't walk into file I/O against a store that was never created - #218

Merged
fdaviddpt merged 1 commit into
mainfrom
fix/204-nested-hook-recursion
Jul 29, 2026
Merged

fdaviddpt merged 1 commit into
mainfrom
fix/204-nested-hook-recursion

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

What this changes

Only the secondary defect in #204: bootstrap-dirs.sh leaking a bash diagnostic to stderr at every session start when the memory root is unwritable. The recursion that the issue leads with is already fixed on main — see the re-derivation below.

What I re-derived, and what came out different

The report is from outside the allowlist and cites 0.8.8 line numbers, so I re-checked every claim against current main (0.9.0) rather than trusting it. Two of its three central claims are correct; its headline conclusion is stale.

Confirmed, live, on this machine. I drove a real nested claude -p using the plugin's own _child_env(), from cwd=$TMPDIR, against a scratch project carrying a hook that dumps its environment:

MARKER=[1] PROJDIR=[/private/tmp/envprop.xHTz] ENTRY=[sdk-cli]
MARKER=[1] PROJDIR=[/private/tmp/envprop.xHTz] ENTRY=[sdk-cli]

Three things fall out of those two lines:

  1. The old guard's premise really was false. CLAUDE_PROJECT_DIR is not absent in the child. _child_env() strips it from the inherited environment and Claude Code sets it afresh from the sandbox cwd — as the physical /private/... path, exactly as reported. So resolve-paths.sh succeeded and the soft-fail never fired.
  2. A one-hook fix would indeed have been insufficient. Two hooks fired (SessionStart and UserPromptSubmit), each of which alone scaffolds the directory. Verified rather than taken on trust.
  3. The marker approach is sound — REMEMBER_NESTED_SUMMARIZER=1 propagates intact into the nested session's hook environment. This was the assumption the whole design rests on and the one that would have collapsed it, so it needed observing rather than assuming.

Different from the issue: the fix it asks for is already merged, twice over, at both layers.

The report's primary recommendation was --setting-sources '' alone. That alone would have been the wrong call, and main is right not to have taken it that way: an older CLI exits non-zero on an unknown option, which becomes a RuntimeError, which means no session ever saves again — a cosmetic junk directory traded for a silent total outage. main handles this by failing open (dropping isolation and retrying, loudly). Which is precisely why the marker guard matters: it is the layer that still holds on the degraded path, where --setting-sources has been dropped and the user's hooks are live. Two independent layers, one of which can be degraded away and one of which cannot.

tests/test_nested_summarizer.py already pins all of this, including the misfire case. I added nothing there.

The part that had not been picked up

The issue's second half. bootstrap-dirs.sh:

mkdir -p "$REMEMBER_DIR/tmp" … 2>/dev/null      # best-effort, status never checked
[ -f … ] || echo '*' > "$REMEMBER_DIR/.gitignore" 2>/dev/null

The 2>/dev/null does not do what it looks like it does. The shell opens a redirection target before running the command, so the failure to open it is reported by the shell itself — outside the scope of the redirect being asked to hide it. Reproduced standalone before touching anything:

$ bash -c "echo '*' > leak/nodir/.gitignore 2>/dev/null"
bash: leak/nodir/.gitignore: No such file or directory     ← leaks
$ bash -c "{ echo '*' > leak/nodir/.gitignore; } 2>/dev/null"
                                                           ← silent

Red, before the fix existed

>       assert not _SHELL_DIAGNOSTIC.search(result.stderr)
E       AssertionError: bootstrap-dirs.sh leaked a shell diagnostic to stderr, which
E       surfaces as a non-blocking hook failure at session start:
E         .../scripts/bootstrap-dirs.sh: line 101: .../readonly-project/.remember/.gitignore: No such file or directory
E         session-start-hook: ERROR — failed to source .../scripts/log.sh

1 failed, 1 passed

The one that passed is the regression control — it passes before and after, which is the point of it.

Green, after

tests/test_bootstrap_readonly_root.py::test_unwritable_memory_root_does_not_leak_a_shell_error PASSED
tests/test_bootstrap_readonly_root.py::test_a_writable_project_still_gets_its_full_tree PASSED
tests/test_nested_summarizer.py ... 8 passed
10 passed

Full suite: 947 passed, 41 skipped, coverage 93.98%.

The fix, and why at that layer

In bootstrap-dirs.sh, not in the hooks: it is the only place that knows whether the store was created, and all three hooks reach the write through it. The .gitignore write is gated on [ -d "$REMEMBER_DIR" ], and the redirect is brace-wrapped so the shell's diagnostic lands inside the suppressed scope — which also covers the residual race where the store disappears between the check and the write, and there is no directory test left to make.

How I ruled out the misfire case

The failure mode that would be far worse than the bug is a guard that silently no-ops a real session — turning "stray directory" into "memory stops working". Three things rule it out:

  1. The guard's condition is the store's own existence. [ -d "$REMEMBER_DIR" ] is false only when mkdir -p just failed. In a normal session the directory was created microseconds earlier by the line above; there is no environment, config mode or platform in which a healthy session reaches that test with the directory missing.
  2. It gates one write, not the script. No return, no exit, no early bail. Everything else in bootstrap — migration, the stderr redirect, every export — is untouched and still runs. The blast radius is a single .gitignore write.
  3. Pinned by a test that fails if it ever misfires. test_a_writable_project_still_gets_its_full_tree asserts a normal project still gets tmp/, logs/, logs/autonomous/, a .gitignore containing *, and === REMEMBER === in stdout. If the guard ever swallowed a real session, that test goes red rather than the user going quietly memoryless.

I also left exit 127 alone deliberately. On such a root log.sh cannot be sourced and session-start-hook.sh says so with its own diagnostic instead of dying later on an undefined function — the documented contract tolerating rc in (0, 127), pinned in test_path_resolution.py. Suppressing a bash-level leak is not a licence to suppress the plugin's deliberate reporting, so the test asserts (0, 127) rather than demanding 0.

Scope

Part of #204, not Closes. The code side of this issue is now complete — recursion fixed at both layers on main, secondary leak fixed here — but the maintainer comments scope what remains as confirm on a current cache, and that is a delivery question this PR cannot settle: the nested summarizer loads the plugin from the marketplace cache, not from a checkout.

Worth recording, though: the verification at the top of this PR did not need a refreshed cache. Driving the hook directly with the child's real environment sidesteps the cache entirely and answers the mechanism question — the marker propagates, both hooks fire, CLAUDE_PROJECT_DIR is set from the sandbox cwd. What a rolled-forward cache would add is end-to-end confirmation, not new information about whether the guard works.

Reported by @BlockX-P2P.

…eated

bootstrap-dirs.sh ran `mkdir -p "$REMEMBER_DIR/…" 2>/dev/null` best-effort,
never checked the status, then wrote .gitignore into that directory. On an
unwritable memory root — a session opened at `/`, where legacy mode makes
REMEMBER_DIR `//.remember` on macOS's read-only root volume — the mkdir
failed silently and the write hit a directory that did not exist.

The `2>/dev/null` did not suppress the result. The shell opens a redirection
target BEFORE running the command, so the failure to open the target is
reported by the shell itself, outside the scope of the redirect meant to
silence it. Users saw

  bootstrap-dirs.sh: line 101: //.remember/.gitignore: No such file or directory

as a non-blocking hook failure at every session start.

The write is now gated on the store existing, and the redirect is wrapped in
braces so the shell's own diagnostic falls inside the suppressed scope — which
also covers the residual race (store removed between the check and the write)
where no directory test is left to make.

Not changed: the `exit 127` that follows on such a root. With no writable store
log.sh cannot be sourced, and session-start-hook.sh reports that deliberately
rather than crashing later on an undefined function — the documented
degraded-env contract tolerating rc in (0, 127). Silencing a bash-level leak is
not a licence to silence the plugin's own reporting.

tests/test_bootstrap_readonly_root.py pins both directions: the leak is gone,
and a writable project still gets tmp/, logs/, logs/autonomous/, .gitignore and
its context injection. A bootstrap that quietly no-ops itself would be a worse
bug than the noise it fixed.

The nested-summarizer recursion in the same issue was already closed by #202
and #205, both verified present and correct on main; this is the part of
@BlockX-P2P's report that had not been picked up.

Co-Authored-By: Max <noreply>
@fdaviddpt
fdaviddpt force-pushed the fix/204-nested-hook-recursion branch from 8ddab31 to 12148dc Compare July 29, 2026 13:59
@fdaviddpt
fdaviddpt merged commit f1dd59e into main Jul 29, 2026
12 checks passed
@fdaviddpt
fdaviddpt deleted the fix/204-nested-hook-recursion branch July 29, 2026 14:11
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