Skip to content

fix(hooks): stop the directory-context walk at the real working directory - #4177

Merged
Yeachan-Heo merged 2 commits into
Yeachan-Heo:devfrom
JayOfTheKeyboard:fix/directory-injector-sibling-prefix
Sep 29, 2026
Merged

Yeachan-Heo merged 2 commits into
Yeachan-Heo:devfrom
JayOfTheKeyboard:fix/directory-injector-sibling-prefix

Conversation

@JayOfTheKeyboard

Copy link
Copy Markdown
Contributor

The problem, measured

findContextFilesUp in src/hooks/directory-readme-injector/index.ts decides whether the walk is still inside the working directory with a string prefix:

if (!parent.startsWith(workingDirectory)) break;

So for a working directory of /work/app, /work/app-legacy counts as inside. When Claude reads a file in a sibling directory that shares the name as a prefix (app and app-legacy, api and api-v2, a repo and its repo-fix2 worktree), the PostToolUse hook climbs to that sibling's root and injects its README.md and AGENTS.md, labelled as [Project README: ...].

Reproduction through the shipped hook (scripts/post-tool-directory-context-injector.mjs), with this repo checked out as two sibling worktrees, omc-fix (the working directory) and omc-fix2. One Read of omc-fix2/src/hooks/rules-injector/matcher.ts:

Context files injected Characters
dev (470714f) omc-fix2/AGENTS.md, omc-fix2/README.md, omc-fix2/src/AGENTS.md, omc-fix2/src/hooks/AGENTS.md 53,663
this PR none 0

A trailing separator on the working directory had the opposite effect. With /work/app/ the prefix test failed for /work/app itself, so the walk stopped before the root and the root AGENTS.md was never injected.

Fix

Test containment with path.relative in a small isWithinWorkingDirectory helper, the way rules-injector/finder.ts already does. A parent is inside when the relative path is not .., does not start with ../, and is not absolute (the last case is a Windows path on another drive). Nothing else in the walk changed.

Not changed

  • A file outside the working directory still gets the context files in its own directory, because the loop checks the start directory before it checks the boundary. That was the behaviour before this PR for any outside file. So reading app-legacy/src/a.ts still injects app-legacy/src/AGENTS.md if there is one; only the climb above it stops. The table's "none" row is 0 because rules-injector/ has no context file of its own. Whether outside files should get nothing is your call, so I left it alone.
  • dist/ is not rebuilt, per CONTRIBUTING. The shipped hook picks this up at the next build.

Test

Four new cases in src/__tests__/directory-context-injector.test.ts, under working directory boundary:

  • a sibling that shares the name as a prefix is not walked (both getContextFilesForFile and processToolExecution)
  • a file outside the working directory does not climb above it
  • a trailing separator on the working directory still reaches the root
  • a directory named ..cache inside the working directory is still walked through

On dev, the sibling and trailing-separator cases fail (2 failed, 19 passed). With the fix, all 21 pass.

Mutations, each run on its own against the new tests:

Mutation Result
call site back to parent.startsWith(workingDirectory) 2 red (sibling, trailing separator)
rel.startsWith('..') without the separator 1 red (..cache)
drop the rel !== '..' check 1 red (outside file climbing up)
drop the !isAbsolute(rel) check green on Linux. It only matters for Windows paths on different drives, where path.relative returns an absolute path. I kept it so that case still stops as it did before.

Validation

…tory

findContextFilesUp kept climbing while the parent path started with the
working directory string, so for a working directory of app it also
walked app-legacy, app2 or a sibling worktree such as repo-fix2, and
injected their README.md and AGENTS.md as "Project" context. Reading one
file in a sibling worktree of this repo injected 53,663 characters of
its root and src context files.

Test containment with path.relative instead of a string prefix, the
same way the rules-injector finder does. That also keeps a trailing
separator on the working directory from cutting the walk short before
the root.
@JayOfTheKeyboard
JayOfTheKeyboard force-pushed the fix/directory-injector-sibling-prefix branch from 13bc687 to 76f2926 Compare September 29, 2026 23:16
@Yeachan-Heo

Copy link
Copy Markdown
Owner

Reviewed. Switching the boundary check from a string prefix to path.relative fixes the sibling-prefix leak and the trailing-separator miss, and it matches how rules-injector/finder.ts already does it. Your tests cover the four edge cases, and the mutation table is a nice touch. The inventory is already regenerated. We'll merge once every check on 76f2926f1d has completed green.

—
[repo owner's gaebal-gajae (clawdbot) 🦞]

@Yeachan-Heo
Yeachan-Heo merged commit 21ea695 into Yeachan-Heo:dev Sep 29, 2026
20 checks passed
@Yeachan-Heo

Copy link
Copy Markdown
Owner

Merged into dev, pinned to 76f2926f1d after every check completed green. Thanks again @JayOfTheKeyboard, that's three solid fixes in a row!

—
[repo owner's gaebal-gajae (clawdbot) 🦞]

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