Skip to content

fix(mcp): let a worktree session read its sibling repos - #1478

Open
BSchneppe wants to merge 3 commits into
zzet:mainfrom
BSchneppe:fix/worktree-sibling-repo-read
Open

BSchneppe wants to merge 3 commits into
zzet:mainfrom
BSchneppe:fix/worktree-sibling-repo-read

Conversation

@BSchneppe

Copy link
Copy Markdown
Contributor

Problem

An agent working in a linked worktree couldn't read files from the other repos in its workspace. read(operation:"file") with an absolute or repo-prefixed path into a sibling repo failed with source path is outside the selected checkout. A session in the primary checkout read the same file without trouble. Agents then gave up on Gortex for that repo and fell back to shell reads.

What changes

  • A worktree session reads files in the other tracked repos of its session workspace the same way a primary-checkout session does.
  • Reads of the worktree's own files are unchanged, including their freshness proof.
  • Still refused, as before: repos outside the session workspace, and paths outside the checkout when the caller names it explicitly with view:{kind:"worktree"}.

🤖 Generated with Claude Code

A session whose cwd is a linked worktree could not read files of the
other tracked repos in its workspace: read(file) of an absolute or
repo-prefixed path into a sibling repo failed with "source path is
outside the selected checkout". A session in the primary checkout read
the same file fine.

The source lane picks its checkout from the session cwd and refuses
every path outside it. A cwd-bound file read that names a sibling repo
of the session workspace now leaves that lane and takes the ordinary
read path, the same one a primary-checkout session uses. Reads of the
worktree's own files keep the lane and its freshness proof; a repo
outside the session workspace and an explicit worktree selector keep
their refusal.

@zzet zzet left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Request changes for the P2 checkout-source routing issue below. The normal sibling-read behavior is useful, but aliases into the worktree family's own primary checkout must keep the existing source-lane refusal.

This finding is source-derived; no local runtime reproduction was performed. It is not a filesystem authorization/security escape: the tracked roots are already authorized. Please preserve genuine sibling reads and explicit worktree-selector behavior while correcting physical ownership classification.

Comment thread internal/mcp/source_request.go Resolved
A symlink beneath a sibling repo that resolves into the worktree family's
primary now keeps the source-lane refusal, as naming the primary does.
@BSchneppe
BSchneppe requested a review from zzet November 16, 2026 07:02

@zzet zzet left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous primary-checkout alias finding is addressed, including the distinct primary/worktree regression. One P2 remains for legitimate sibling reads through Windows directory junctions, as detailed inline.

This finding is source-derived from the pinned Go implementation and the request-routing flow; it was not runtime-reproduced on Windows. CI is green, but the new tests cover file symlinks rather than this directory-junction case. This is a functional checkout-source routing issue, not an authorization escape.

Comment thread internal/mcp/source_request.go Outdated
Comment on lines +198 to +200
resolved, err := filepath.EvalSymlinks(abs)
if err != nil {
return false

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Use junction-aware resolution for genuine sibling paths

A legitimate allowed sibling path such as sibling\junction\file.go, where junction points to another directory inside that same sibling, is rejected here on Windows. With default pinned Go1.27.3 behavior, a directory junction has ModeIrregular rather than ModeSymlink/ModeDir; filepath.EvalSymlinks returns ENOTDIR when it encounters that ancestor. This helper then returns false, retains the worktree source carrier, and read_file rejects the sibling as source path is outside the selected checkout.

Earlier routing does not canonicalize the path value. The ordinary physical guard can resolve and validate this in-sibling target, but it does not rewrite the path or remove the already-selected source carrier.

Please reuse the existing pathguard.EvalLinks junction-aware resolver while retaining the lexical/resolved ownership, workspace and fail-closed checks. Add a Windows directory-junction positive for a genuine sibling and a negative for a junction into the family's primary checkout. Existing file-symlink tests do not cover this case.

Source-derived; no Windows runtime reproduction is claimed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed: classification now resolves through pathguard.EvalLinks, keeping the lexical and resolved ownership, workspace and fail-closed checks. Added TestWorktreeSessionSiblingReadThroughJunction (Windows): it reads a genuine sibling through a junction and refuses a junction into the family's primary. Ran it on a Windows 11 machine: it passes with the change and fails without it.

filepath.EvalSymlinks fails on a path through a directory junction, so a
genuine sibling path beneath one stayed on the source lane and was refused.
pathguard.EvalLinks resolves it, keeping the same ownership checks.
@BSchneppe
BSchneppe requested a review from zzet November 16, 2026 11:50
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