Skip to content

Fix #1058: walk ancestors for write-handoff.sh's session hint - #1081

Merged
fdaviddpt merged 6 commits into
mainfrom
fix/1058
Nov 1, 2026
Merged

fdaviddpt merged 6 commits into
mainfrom
fix/1058

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

What

scripts/write-handoff.sh's #776 fix only compared two candidates ($(pwd) and the git toplevel) when disambiguating which memory store to write a handoff note into, when CLAUDE_PROJECT_DIR is unset. A Bash cd that moves $(pwd) deeper than the session's actual start directory leaves the session-keyed hint tmp/handoff-path.<session-id> in an ancestor strictly between the two candidates, invisible to the #776 check -- so the write silently misrouted to the enclosing repository's own store, replacing its pending handoff.

Replaced the two-candidate check with a loop walking every ancestor from $(pwd) up to the git toplevel, taking the first (closest) ancestor whose own resolved store already carries this session's hint. Falls back to the toplevel, unchanged, when no ancestor in the walk has a hint -- the existing #743 fallback is untouched.

A self-review round (Explore + oss:auditor, both spawned read-only against the main commit) returned two low-severity findings, both fixed in the follow-up commit: a stale #776 comment that no longer described the walk's "both candidates have a hint" case, and a string-equality termination test that would not match when the walk reaches the toplevel through a symlinked ancestor (fixed with a -ef device+inode test instead).

Closes #1058

Tests

Extended tests/test_write_handoff_subdirectory_project_776.py with a new fixture (repo -> sub (holds the hint) -> deep) and a new test class covering the repro plus a positive control (no hint anywhere still falls back to the git root, per #743).

RED: python3 -m pytest tests/test_write_handoff_subdirectory_project_776.py -q against the new tests with the old two-candidate code -> 1 failed, 3 passed.

GREEN: same command -> 4 passed in 0.90s.

Also re-pointed a line-pinned allowlist entry in tests/test_locale_ranges_695.py whose target line shifted when the new header comment was added. Ran the full 13-file sibling set that references write-handoff.sh: 366 passed, 1 skipped, before and after the self-review follow-up commit.

Did not run the repo's full pytest suite -- per this repo's own rule, that is CI's job across the OS/interpreter matrix, not a local gate.

Adjacent (not fixed here)

scripts/doctor.sh has the identical two-candidate toplevel-vs-$(pwd) fallback pattern (its own #802 fix) with no #776-equivalent session-hint disambiguation at all. Lower stakes (it's read-only diagnostic reporting), but the same class of staleness. Recommend filing as a follow-up issue; left out of this diff as a different file/function that would roughly double its size.

Docs

README.md surveyed (.oss.json names it as a docs target); it only mentions session-start-hook.sh at the hook-table level and never documents this internal write-handoff.sh resolution logic, so no doc change was needed.

🤖 Generated with Claude Code

🤖 Generated with Claude Code

[AI-generated]

fdaviddpt and others added 2 commits November 1, 2026 00:11
…st two candidates

#776 disambiguated write-handoff.sh's CLAUDE_PROJECT_DIR fallback by comparing
only two candidates -- $(pwd) and the git toplevel -- for this session's own
handoff hint. When a Bash `cd` moved $(pwd) past the subdirectory project a
session actually started in, the hint lived in neither candidate: it lived in
an ancestor strictly between them, invisible to the #776 check. The write then
fell through to the enclosing repository's own store, silently replacing that
project's pending handoff.

Replaced the two-candidate check with a walk of every directory from $(pwd) up
to the git toplevel, taking the first (closest) one whose own resolved store
carries the session-keyed hint. This subsumes the #776 check -- walking from
$(pwd) reaches it first when $(pwd) itself is the hint-holder -- and still
falls back to the toplevel, unchanged, when no ancestor in the walk has a hint
at all.

Added a three-level repro (repo -> sub -> deep) to
tests/test_write_handoff_subdirectory_project_776.py, paired with a positive
control that keeps the #743 no-hint-anywhere fallback unchanged. Re-pointed
tests/test_locale_ranges_695.py's line-pinned allowlist entry for
write-handoff.sh's existing session-id letter-range check, which moved from
line 87 to line 97 because of the new comment block.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
…ancestor walk

Two findings from this lane's own Explore review of 91e5cc1, both low severity, no misroute in either case:

1. The pre-existing #776 comment still said "neither or both agree -> toplevel, unchanged", which no longer describes the new walk's "both" case (it now stops at $(pwd) first). Reworded to say so, noting the "both" shape remains the one #776 itself called unreachable in the ordinary case.

2. The new walk's "up to the git toplevel" comment was not actually enforced: `git rev-parse --show-toplevel` resolves symlinks but bash's own $(pwd) does not, so a symlinked ancestor (e.g. macOS's /tmp -> /private/tmp) would never string-match $_WH_GIT_ROOT and the walk would silently fall through to its "/" backstop instead -- harmless (each ancestor's hint check is independent) but needlessly expensive. Switched the comparison from a string `=` test to `-ef` (same device+inode), which matches regardless of which path spelling got there.

Re-ran the full sibling test set (366 passed, 1 skipped) to confirm no regression.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
fdaviddpt and others added 2 commits November 1, 2026 00:37
…n, doctor.sh parity

Independent review on PR #1081 (fix #1058) found four real issues in the ancestor-walk fix:

1. MEDIUM (write-handoff.sh): the walk's "closest ancestor with a hint wins" rule regressed #776
   behaviour when a `--resume` republished the session-keyed hint at a DIFFERENT directory than
   the one the session originally started in -- session-start-hook.sh's own stale-hint reap only
   sweeps the CURRENT REMEMBER_DIR's own tmp/, never another directory's, so the earlier
   directory's hint survives for its full grace window. Fixed by walking the FULL ancestor chain
   and picking the FRESHEST hint by mtime, not just the nearest -- confirmed red against the
   pre-fix (3dbf161) walk, green after.

2. LOW (write-handoff.sh): the walk's termination test used a string `=` against "/" only, which
   can loop forever on an MSYS/Windows UNC path where `dirname "//server"` returns the same
   string. Replaced with a "no further progress" check (next dirname equals current dir) --
   reasoned, not observed, since no MSYS environment was available to exercise it directly.

3. LOW (doctor.sh): `_doctor_resolve_project_dir_candidate` (#827) still used the old #776-era
   two-candidate rule, so `/remember:doctor` could report a different store than the one
   write-handoff.sh actually writes into. Ported the same ancestor-walk + freshest-mtime logic,
   with its own red/green test pair mirroring write-handoff.sh's.

4. MEDIUM (write-handoff.sh, nested git repos / no-git siblings): the review's own recommended
   direction -- pinning CLAUDE_PROJECT_DIR at SessionStart via $CLAUDE_ENV_FILE so /remember never
   guesses from cwd at all -- is NOT implemented here. That mechanism does not exist anywhere in
   this codebase today (confirmed: zero references to CLAUDE_ENV_FILE in the whole tree), and
   implementing it safely needs verifying Claude Code's actual env-file semantics, which is a
   larger, separate change deserving its own issue and its own review rather than a rider on this
   one. The "walk bounded by the innermost git toplevel" and "no git at all falls back to $(pwd)"
   behaviours this finding also names are pre-existing #743/#776 architecture (unchanged by either
   #1058 or this follow-up, not a regression introduced by the ancestor walk), so left as-is.

Extended both tests/test_write_handoff_subdirectory_project_776.py and
tests/test_doctor_subdirectory_project_827.py with a resume-shaped fixture (stale hint at a
nearer ancestor, fresh hint at the toplevel) plus a positive control (only one hint anywhere must
still win, guarding against a "freshest" rewrite that silently needs two candidates to work at
all). Re-ran the full 13-file sibling set plus the locale-ranges scanner: 370 passed, 1 skipped.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
…tor.sh coverage gap

Round-2 self-review (fix_commit_scope.py: needs-second-pass, afe3033 touched 5 files) over the
previous commit's own freshest-mtime/UNC-safe fix found two real issues, both now fixed:

1. Explore reviewer (2 findings): two comment blocks in scripts/write-handoff.sh -- the top-of-file
   DESCRIPTION header and the pre-`if` block right above the walk -- still described the OLD
   "closest ancestor wins" rule afe3033 itself replaced with "freshest hint by mtime wins." Reworded
   both to describe the actual behaviour; doctor.sh's own comment and the changelog entry were
   already accurate, so nothing else needed touching.

2. oss:auditor (Class B finding, ranked misreports/non-blocking but cheap enough to fix directly):
   doctor.sh's new TestDoctorDeeperCwdThanSessionStartDir1058 test class never put a hint at more
   than one ancestor at once, so it could not distinguish "freshest wins" from "nearest wins" the
   way write-handoff.sh's sibling test (TestFreshestHintWinsOnResume1058Review) does -- reverting
   doctor.sh's walk to the old nearest-wins rule would not have failed either existing doctor.sh
   test. Added the missing TestDoctorFreshestHintWinsOnResume1058Review class, mirroring
   write-handoff.sh's test exactly (stale hint backdated via os.utime, fresh hint at the toplevel,
   plus a positive control). Confirmed red against the pre-afe3033 (3dbf161) two-candidate doctor.sh,
   green against the current one.

Both reviewers' tree_snapshot compare returned clean. Re-ran the full 13-file sibling set: 372
passed, 1 skipped (one transient teardown error from a concurrent lane's own worktree churn on the
shared clone, non-reproducible on immediate re-run, unrelated to this diff).

Co-Authored-By: Claude Sonnet 5 <[email protected]>
fdaviddpt and others added 2 commits November 1, 2026 01:12
…ar/stale-far regression test

Per the maintainer ruling on PR #1081: #1058 ships as the incremental ancestor-walk fix
(freshest-mtime tie-break, UNC-safe termination); the pointer-file redesign is tracked
separately as #1083, confirmed to exist and read in full before citing it here.

1. Rewrote changelog.d/1058.fixed.md to state the actual guarantee (ancestor walk,
   freshest-hint tie-break, UNC-safe termination, doctor.sh parity) and name the known limits
   that remain open: resume-into-a-non-ancestor-directory, nested repo/submodule, and non-git
   sibling (all three pre-existing #743/#776 architecture, tracked by #1083) plus a fourth limit
   #1083 does not name -- same-second mtime ties -- flagged as not yet filed rather than implying
   #1083 covers it.

2. Added test_a_fresh_near_hint_beats_a_stale_far_one_for_the_right_reason to
   TestFreshestHintWinsOnResume1058Review: the mirror image of the existing resume regression
   test, with the NEAR ancestor's hint fresh/correct and the FAR (toplevel) one stale. Confirmed
   this test alone would also pass against the old nearest-wins code (nearest happens to be
   correct here) -- its value is only in the PAIR with the existing resume test, which together
   can only both pass under a genuine mtime comparison, not under either naive "nearest" or
   "farthest" rule alone.

Re-ran the 13-file sibling set: 373 passed, 1 skipped.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@fdaviddpt
fdaviddpt merged commit 58df702 into main Nov 1, 2026
19 checks passed
@fdaviddpt
fdaviddpt deleted the fix/1058 branch November 1, 2026 01:21
fdaviddpt added a commit that referenced this pull request Nov 1, 2026
Resolved tests/test_locale_ranges_695.py: session-start-hook.sh line 307 from this branch,
write-handoff.sh line 97 from main (#1081); both match the merged files and the test passes.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
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.

write-handoff.sh still misroutes after #776 when the Bash cwd is deeper than a subdirectory start dir

1 participant