Skip to content

SessionStart: 50-git-restore.sh no longer re-sources the whole config chain when git_restore is off - #678

Merged
fdaviddpt merged 3 commits into
mainfrom
fix/663
Sep 12, 2026
Merged

fdaviddpt merged 3 commits into
mainfrom
fix/663

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Closes #663. Part of #660.

What

hooks.d/before_session_start/50-git-restore.sh is dispatched as a CHILD process of scripts/log.sh's dispatch() during SessionStart. The parent (session-start-hook.sh) already sourced log.sh -- which resolves and merges the three-layer config and exports REMEMBER_CONFIG -- before dispatching, but the hook ignored that inheritance entirely: because _LIB_MEMORY_DIR_LOADED is not exported to a child process, source "$PIPELINE_DIR/scripts/log.sh" unconditionally re-ran the WHOLE merge (lib-memory-dir.sh's own mktemp + several jq calls, plus log.sh's one-pass flatten and its cache publish) before ever reading whether git_restore.enabled was even true -- on every dispatch, on every install with the (default) setting off.

Now the hook reads git_restore.enabled with a single jq (or python3) call straight out of the already-exported REMEMBER_CONFIG, before sourcing anything. If that says "false", it exits immediately. If it says "true", or the cheap read cannot answer at all (missing/unreadable REMEMBER_CONFIG, no jq/python3), it falls through unchanged to the original source log.sh + config() gate, which stays authoritative.

Separately, dispatch() (scripts/log.sh) now reads the current UID with the bash builtin $EUID instead of forking id -u, and folds the two per-hook subprocess calls (stat for ownership, find -perm -002 for world-writable) into one stat call.

Measured

Spawn-counting harness, external data_dir layout, real re-source (_LIB_MEMORY_DIR_LOADED deliberately unset), git_restore.enabled=false: 11 spawns before this fix, 2 after (tests/test_git_restore_no_resource_when_disabled_663.py). A positive control in the same fixture confirms git_restore.enabled=true still reaches the real restore logic (the layered merge and the git rev-list --left-right --count divergence check both still run).

The #669 benchmark (tests/test_session_start_windows_benchmark_669.py -k benchmark) does not move: its fixture exercises LEGACY layout, where this hook already exits at an earlier, pre-existing guard before reaching either the old or the new gate. The defect this issue names is specific to EXTERNAL data_dir layout.

Self-review finding, fixed in a follow-up commit on this branch

Both spawned reviewers (an independent Explore review and an oss:auditor audit) found the same real bug in the stat/find fold: GNU stat -c '%a' (unlike BSD's %Lp) prefixes the three permission digits with the setuid/setgid/sticky bit when one is set, so a hook at mode 1777/2777/4777 reported as the four-digit "1777", which the case "$hook_perm" in [0-7][0-7][0-7]) pattern did not match -- silently treating a genuinely world-writable (plus special-bit) hook as "cannot tell" and letting it run. Fixed by trimming to the last 3 characters before the pattern match; pinned by a new regression test that fakes stat to emit the GNU 4-digit shape regardless of platform (confirmed red without the trim, green with it).

below-bar note

No below-bar findings in this run -- both reviewer findings on the fold bug were fixed, not deferred.

🤖 Generated with Claude Code

https://claude.ai/code/session_01ScdCn6Xq622o2cRQhzVhi5

[AI-generated]

fdaviddpt and others added 3 commits September 12, 2026 14:33
…#663, part of #660)

session-start-hook.sh sources log.sh (and its lib-memory-dir.sh merge) before
dispatching before_session_start hooks, but 50-git-restore.sh ignored that
inherited work entirely: because _LIB_MEMORY_DIR_LOADED is not exported to the
child, sourcing log.sh unconditionally re-ran the whole three-layer config
merge (mktemp, per-layer jq reads, jq -s deep merge, log.sh's own one-pass
flatten) before ever checking whether git_restore.enabled was even true --
paid on every dispatch, on every install with the (default) setting off.

Now the hook reads git_restore.enabled with a single jq (or python) call
straight out of the already-merged REMEMBER_CONFIG the parent process already
exported, before sourcing anything. Only when that says "true" (or the cheap
read cannot answer at all) does it fall through to the unconditional
source + config() gate, unchanged, so correctness never depends on the fast
path. Measured (external data_dir layout, real re-source, disabled):
11 spawns before, 2 after; a positive control in the same fixture confirms
git_restore.enabled=true still reaches the real restore logic.

Also folds dispatch()'s two per-hook subprocess calls (stat for ownership,
find -perm -002 for world-writable) into one stat call, and replaces the
per-loop `$(id -u)` with the bash builtin $EUID.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01ScdCn6Xq622o2cRQhzVhi5
…U stat (#663 self-review)

Both the independent reviewer and the auditor spawned against dfe1f34 found
the same bug in the stat/find fold: GNU `stat -c '%a'` (unlike BSD's `%Lp`)
prefixes the three permission digits with the setuid/setgid/sticky bit
whenever one is set, so a hook at mode 1777/2777/4777 reports as the
four-digit "1777", not "777". The `case "$hook_perm" in [0-7][0-7][0-7])`
pattern only matched exactly three digits, so a four-digit value fell
through unmatched and was silently treated as "cannot tell" -- fail-open,
letting a genuinely world-writable (and additionally setuid/sticky) hook
run. The pre-fix `find -maxdepth 0 -perm -002` tested the bit directly and
never had this failure mode.

Fixed by trimming to the last 3 characters (`${hook_perm: -3}`) before the
pattern match and the arithmetic, which is a no-op for the ordinary 3-digit
case. Pinned by a new regression test that fakes `stat` to emit the GNU
4-digit shape regardless of the platform this test actually runs on
(confirmed red without the trim, green with it).

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

`${hook_perm: -3}` returns "" for a string shorter than three characters on
every bash (tested on 3.2), not the string unchanged as the comment claimed,
and GNU `%a` prints no leading zeros ("22" for mode 0022). Unreachable for an
executable hook, but the trim was never needed: `8#$mode & 2` ignores the
special bits by construction. Accept any all-octal string and test that.

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.

SessionStart: 50-git-restore.sh runs as a foreground child and re-sources the whole lib chain even when git_restore is disabled (Part of #660)

1 participant