Repository navigation
fix(hooks): stop the directory-context walk at the real working directory - #4177
Merged
Yeachan-Heo merged 2 commits intoSep 29, 2026
Conversation
…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
force-pushed
the
fix/directory-injector-sibling-prefix
branch
from
September 29, 2026 23:16
13bc687 to
76f2926
Compare
Owner
|
Reviewed. Switching the boundary check from a string prefix to — |
Owner
|
Merged into — |
17 of 23 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The problem, measured
findContextFilesUpinsrc/hooks/directory-readme-injector/index.tsdecides whether the walk is still inside the working directory with a string prefix:So for a working directory of
/work/app,/work/app-legacycounts as inside. When Claude reads a file in a sibling directory that shares the name as a prefix (appandapp-legacy,apiandapi-v2, a repo and itsrepo-fix2worktree), the PostToolUse hook climbs to that sibling's root and injects itsREADME.mdandAGENTS.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) andomc-fix2. OneReadofomc-fix2/src/hooks/rules-injector/matcher.ts:dev(470714f)omc-fix2/AGENTS.md,omc-fix2/README.md,omc-fix2/src/AGENTS.md,omc-fix2/src/hooks/AGENTS.mdA trailing separator on the working directory had the opposite effect. With
/work/app/the prefix test failed for/work/appitself, so the walk stopped before the root and the rootAGENTS.mdwas never injected.Fix
Test containment with
path.relativein a smallisWithinWorkingDirectoryhelper, the wayrules-injector/finder.tsalready 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
app-legacy/src/a.tsstill injectsapp-legacy/src/AGENTS.mdif there is one; only the climb above it stops. The table's "none" row is 0 becauserules-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, underworking directory boundary:getContextFilesForFileandprocessToolExecution)..cacheinside the working directory is still walked throughOn
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:
parent.startsWith(workingDirectory)rel.startsWith('..')without the separator..cache)rel !== '..'check!isAbsolute(rel)checkpath.relativereturns an absolute path. I kept it so that case still stops as it did before.Validation
vitest runon Linux: 15,203 passed, 64 failed, 28 skipped with this PR.devgave 15,206 passed, 57 failed on the same host. The failures are timing and tmux/Windows-contract tests that fail on this machine without the change too. The 10 that failed only on this branch touch session-end, team panes, run.cjs timeouts and the release verifier, not this hook. Rerun in isolation, 9 of them passed, and the last (session-end-timeout) passed on both trees on two more runs.tsc --noEmitclean,eslintclean on both changed filesgenerate:inventory --verifyok. The inventory is regenerated in a second commit, as in fix(hooks): skip every git global option in git-guardrails #4152, fix(hooks): read the XDG global config in the code-simplifier Stop hook #4160 and fix(rules-injector): let **/ match zero directories in rule globs #4161.