Skip to content

fix(mcp): treat a handler on another drive as outside the package - #20711

Closed
CrysisDeu wants to merge 2 commits into
mainfrom
fix/serves-managed-tools-cross-drive
Closed

CrysisDeu wants to merge 2 commits into
mainfrom
fix/serves-managed-tools-cross-drive

Conversation

@CrysisDeu

@CrysisDeu CrysisDeu commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: Backend Tests (Windows) goes green again on fork pull requests.

Since #20530 (9c65772) landed on 2026-10-15 05:44Z, Backend Tests (Windows) (7) is red on EVERY fork PR against main. mcp_shared.serves_managed_tools decides whether the managed tool policy applies by asking os.path.commonpath([handler_file, package_dir]) whether the handler's code sits under the package. On a GitHub-hosted windows-latest runner (which fork PRs use) the checkout lives on D:\a\... while pytest's tmp_path lives on C:\Users\..., so commonpath raises ValueError: Paths don't have the same drive and the surrounding except (OSError, ValueError) answers True (managed). A stand-in app module in tmp_path is therefore judged to be Kiro Crew's own code, and these two tests fail:

  • test/test_app_mcp_tool_policy_scope.py::TestServesManagedTools::test_a_handler_outside_the_package_is_not_managed
  • test/test_app_mcp_tool_policy_scope.py::TestAppServerSkipsThePolicyRead::test_an_app_tool_runs_when_no_identity_can_be_attested

Example red: https://github.com/kirodotdev/KiroCrew/actions/runs/38691894797/job/116289405538 (PR #20638). In-repo PRs run on the fleet's CodeBuild Windows runners where checkout and temp both live on C:, so nobody in-repo sees it.

The same bug also hits a real user: an app installed on a different drive from the Kiro Crew package is wrongly held to the managed tool policy, which is the exact defect #20530 set out to fix.

Why it matters

Every fork contributor has a required check red through no fault of their own, and the maintainers cannot tell a real Windows regression from this one until it is fixed. A user whose app lives on another drive than the Kiro Crew install gets every app tool call refused on the shared ACP runtime.

Not a goal

  • Changing what serves_managed_tools does for handlers with no code location or for an OSError from realpath (both still fail closed to True).
  • Replacing os.path.commonpath with pathlib; the one-exception fix is the smallest change that makes the containment check drive-safe.
  • Changing which runner fork PRs use.

What changed (motivation → approach → change)

Symptom: cross-drive ValueError from commonpath → root cause: the catch-all except turns a known answer ("not under the package directory") into the fail-closed True → change: the containment check moves into _inside_package_dir(real, package_dir). On a commonpath ValueError it compares the two paths' drives (_nt_drive: case-folded, \\?\ / \\?\UNC\ prefix stripped): different drives is a known False; the same drive spelled two ways, or no drives at all, stays the fail-closed True. realpath keeps its own except (OSError, ValueError): return True, so an unresolvable co_filename (embedded NUL) still fails closed.

Backwards compatibility

Compatible: handlers inside the package, partials and bound methods of package code, and handlers with no code location answer exactly as before. Only a handler on a different drive changes, from True (wrong) to False.

Removes nothing: no key, record, path or public function is removed or renamed.

Tests

Added to test/test_app_mcp_tool_policy_scope.py::TestServesManagedTools (handlers are built from a code object via types.FunctionType(code.replace(co_filename=...)), no exec):

  • test_a_handler_on_another_drive_is_not_managed (every platform): with os.path.commonpath raising the two-drive ValueError, a C: handler against a D: package dir is False, also when spelled \\?\C:. Red on origin/main (no _inside_package_dir; serves_managed_tools answers True for the other-drive handler), green here.
  • test_a_refusal_between_spellings_of_one_drive_keeps_the_policy: \\?\D: and d: against a D: package dir stay True.
  • test_a_refusal_with_no_drives_at_all_keeps_the_policy: two POSIX paths under a refusing commonpath stay True.
  • test_a_handler_whose_path_cannot_resolve_keeps_the_policy: with os.path.realpath raising ValueError (what an embedded NUL does on POSIX), the answer stays True.
  • test_a_handler_on_another_drive_letter_is_not_managed_on_windows (Windows only, no stub): a handler whose code file sits on a drive letter the package does not use is False.

Evidence (worktree venv, Python 3.12, Linux, head 6f8201c):

  • pytest -n0 test/test_app_mcp_tool_policy_scope.py: 10 passed, 1 skipped.
  • D12 repeat/shuffle on test_app_mcp_tool_policy_scope.py + test_mcp_shared.py (220 ids): 20x -n0 seeded shuffles, 0 red; 10x -n 4 --dist loadgroup seeded shuffles, 0 red.
  • scripts/local-gate.py backend (related): 4394 passed, 3 skipped, 5 xfailed. The frontend lane did not run (worktree has no website/node_modules); the diff is backend-only.
  • check_black_formatting.py, check_subprocess_encoding.py, isort --check-only, flake8, mypy src/kiro_crew (0 issues in 2431 files), check_comment_history.py, check_brand_name.py, check_file_line_cap.py, test_flake_pattern_ratchet.py (56 passed): all green.

Manual verification

N/A — unit coverage sufficient: the failing CI job is itself the reproduction, and the Windows-only test exercises the real two-drive path on the hosted runner for this PR.

Related Issues

Follow-up to #20530.

Pattern harvest

Rule candidate: review-prompt
Pattern: os.path.commonpath (or any path-containment check) wrapped in an except that returns the permissive/fail-closed default; on Windows two absolute paths on different drives raise ValueError, so the except silently flips the answer only on hosted two-drive runners.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

Contribution License Agreement

Contributions are accepted under the project's Apache-2.0 license; see the Licensing section of CONTRIBUTING.md.

@CrysisDeu
CrysisDeu requested review from a team and KiroCrewCaptain as code owners October 15, 2026 07:41
@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

Revision 6f8201cab2d467fd8997c5030f03222aab80b0e1 touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Advisory — does not block merge.

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Oct 15, 2026
serves_managed_tools decides whether the managed tool policy applies by
asking os.path.commonpath whether the handler's code file sits under
the package directory. commonpath raises ValueError when the two paths
sit on different drives, and the catch-all except answered True
(managed). On a GitHub-hosted Windows runner the checkout lives on D:
while pytest's tmp_path lives on C:, so every stand-in app module was
judged managed and Backend Tests (Windows) went red on every fork PR.

A file on another drive cannot be inside the package directory, so a
ValueError from commonpath is a known False, not an unknown. OSError
from realpath still fails closed to True.
@CrysisDeu
CrysisDeu force-pushed the fix/serves-managed-tools-cross-drive branch from 0c13b59 to 3aa491f Compare October 15, 2026 08:54
@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

GPT 6.1 completed its review of 6f8201cab2d467fd8997c5030f03222aab80b0e1 and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 6f8201c

False positive or not applicable? A repository writer can comment:
/ai-review override gpt 6f8201cab2d467fd8997c5030f03222aab80b0e1: <one-sentence reason>

@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

Design-level review of 6f8201cab2d467fd8997c5030f03222aab80b0e1 — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: PASS

Check that Backend Tests (Windows) (7) passes on this fork PR's hosted runner. That run is the only real test of the two-drive path.

[DESIGN-REVIEWED] 6f8201c

Description read

[DESCRIPTION-READ] b1ea18a262782f6802b277e23fb76b86ab695b1f2fcb9e9fc7ce89c61a4860b8

That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of 6f8201cab2d467fd8997c5030f03222aab80b0e1 — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

First-Principles-Verdict: PASS

Before merging, confirm that Backend Tests (Windows) (7) passes on a GitHub-hosted fork runner. That job is the only real two-drive reproduction; every local check ran on Linux.

What this change ships

Inventory (4 items) — 4 justified

Intent: make the Windows backend tests pass again on fork PRs by treating an app handler on a different drive as outside the Kiro Crew package. FIX. Evidence: a linked red run, two named failing tests, and a new test that fails on base.

  1. A handler on a different drive from the package is now judged not managed, so the tool policy no longer refuses its calls. — justified
  2. Two spellings of the same drive (\\?\D: beside d:, and the \\?\UNC\ form) still keep the policy. — justified
  3. A path with no drive at all, and a path realpath cannot resolve, still keep the policy. — justified
  4. Five new tests, including one Windows-only test with no stub that exercises a real second drive letter. — justified

[FIRST-PRINCIPLES-REVIEWED] 6f8201c

Description read

[DESCRIPTION-READ] 7691ac9fee8d9aa4b6c0b5d0ca71171cff7f7a67fa776dd5d145489bccf78c18

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

Reviewed 6f8201cab2d467fd8997c5030f03222aab80b0e1 — this comment is updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] 6f8201c

Verdict parsed from the review's SHA-scoped output markers for commit 6f8201cab2d467fd8997c5030f03222aab80b0e1.

False positive or not applicable? A repository writer can comment:
/ai-review override fable 6f8201cab2d467fd8997c5030f03222aab80b0e1: <one-sentence reason>

A commonpath refusal between two spellings of one drive (\\?\C: beside
C:) keeps the fail-closed True; only a real drive mismatch is False. A
realpath ValueError (embedded NUL in co_filename) stays fail-closed.
The cross-drive tests build their handler with a code object rather
than exec().
@CrysisDeu
CrysisDeu force-pushed the fix/serves-managed-tools-cross-drive branch from 3aa491f to 6f8201c Compare October 15, 2026 09:35
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Oct 15, 2026
@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision and removed readiness: checking Automated validation is still running labels Oct 15, 2026
@github-actions github-actions Bot added the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Oct 15, 2026
@CrysisDeu

Copy link
Copy Markdown
Contributor Author

Superseded by #20802 (merged 2026-10-15 17:16Z), which lands the same drive-safe containment check in serves_managed_tools (_path_is_under: different drives → False, any other commonpath refusal → fail-closed True, realpath ValueError → True) with equivalent tests. Closing to avoid a duplicate.

@CrysisDeu CrysisDeu closed this Oct 15, 2026
@CrysisDeu
CrysisDeu deleted the fix/serves-managed-tools-cross-drive branch October 15, 2026 17:25
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge conflict Branch has merge conflicts with its base — author must resolve before merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant