Skip to content

Project context search stops at the home directory - #234

Merged
gadenbuie merged 3 commits into
mainfrom
fix/225-project-context-search-not-home
Oct 6, 2026
Merged

gadenbuie merged 3 commits into
mainfrom
fix/225-project-context-search-not-home

Conversation

@gadenbuie

Copy link
Copy Markdown
Collaborator

Fixes #225

Summary

path_find_in_project() walked up from the working directory until it found a project root marker (DESCRIPTION, .git, .vscode, .here, or an *.Rproj file) or the filesystem root. The home directory wasn't a stopping point, so when the working directory was under ~ but outside a project, the walk reached home and treated ~/AGENTS.md, ~/CLAUDE.md, or ~/btw.md as project files — and ~/btw.md instructions were then read twice: once as the project file, once as the user-level config.

This PR makes the home directory a hard stop for the upward search:

  • path_find_in_project() returns NULL as soon as the walk reaches the home directory — either fs::path_home() or fs::path_home_r(), which differ on Windows — without ever checking the home directory itself. Home-level files are handled only by the user-level lookup (path_find_user()); supporting ~/AGENTS.md there is tracked in the companion issue.
  • By design, a home working directory skips all project detection methods: a dotfiles repo with ~/.git or a ~/DESCRIPTION does not make home a project.
  • read_btw_file() no longer duplicates instructions when path_btw is set explicitly to the same file as the user-level config, e.g. btw_client(path_btw = "~/.btw/btw.md").
  • Fixed a latent bug in the *.Rproj root check: it listed the session's working directory instead of the directory being searched, so a stray *.Rproj in the session cwd could falsely mark unrelated directories as project roots, and roots marked only by an *.Rproj file could be walked past.

New internal helpers path_is_home() and path_same() share path_normalize_for_compare() for consistent path comparison, including Windows case-folding.

Verification

With a ~/AGENTS.md present and the working directory under ~ but outside a project, find_btw_context_file() previously returned ~/AGENTS.md as the project context file; it now returns NULL, and ~/btw.md instructions appear once in the system prompt instead of twice. The repro from the issue (using a temporary HOME):

tmp <- file.path(tempdir(), "fakehome")
dir.create(file.path(tmp, "scratch", "sub"), recursive = TRUE)
writeLines("home btw instructions", file.path(tmp, "btw.md"))
# with fs::path_home()/fs::path_home_r() mocked to `tmp`, from tmp/scratch/sub:
find_btw_context_file(search_user = FALSE)   # NULL (previously ~/AGENTS.md)
read_btw_file()$btw_system_prompt            # "home btw instructions", once not twice

New tests cover the home stop, project roots below home, the Windows split home roots, the *.Rproj fixes, and the path_btw dedup guard; the full test suite passes.

path_find_in_project() now returns NULL when the upward search reaches the
home directory (fs::path_home() or fs::path_home_r()), without checking
home itself. This keeps ~/AGENTS.md and ~/CLAUDE.md from being treated as
project context when the working directory is under ~ but outside a
project, and stops ~/btw.md from being read twice (as project file and
user-level config).

Also skip the user-level file in read_btw_file() when path_btw resolves
to the same file, and check *.Rproj project roots in the directory being
searched rather than the session working directory.

Fixes #225
- Share a path_normalize_for_compare() helper between path_is_home()
  and path_same() so Windows case-folding is consistent
- Rewrite the project-roots-below-home test so the root-marker stop is
  actually exercised and the searched directory is created first

Finding 1 (home-as-project detection) is intentional per #225: home
skips all project detection; home-level files are handled only by the
user-level lookup.
Move AGENTS.md below home and above the .git marker so the NULL result
can only come from the marker stop, not the home-directory stop, and
assert the same file is found from the same directory before the marker
exists.
@gadenbuie
gadenbuie marked this pull request as ready for review October 5, 2026 21:24
@gadenbuie
gadenbuie merged commit 6a27d99 into main Oct 6, 2026
20 of 25 checks passed
@gadenbuie
gadenbuie deleted the fix/225-project-context-search-not-home branch October 6, 2026 13:10
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.

Project context search shouldn't treat the home directory as a project

1 participant