Skip to content

fix(native): let Claude Code load project plugins in a first session and in worktree-only installs (#5176) - #5184

Closed
Daniel Meppiel (danielmeppiel) wants to merge 8 commits into
mainfrom
fix-5176-claude-known-marketplace
Closed

Daniel Meppiel (danielmeppiel) wants to merge 8 commits into
mainfrom
fix-5176-claude-known-marketplace

Conversation

@danielmeppiel

@danielmeppiel Daniel Meppiel (danielmeppiel) commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Description

At project scope apm install declares a native plugin in .claude/settings.json under the relative marketplace path ./.agents/plugins. Claude Code has two problems with that:

Fix (real-harness shape A):

  • The committed relative .claude/settings.json declaration is unchanged.
  • After a project-scope install that declares Claude native plugins, new owner native_plugins/claude_marketplaces.py upserts ${CLAUDE_CONFIG_DIR:-~/.claude}/plugins/known_marketplaces.json with {"<apm-p-name>": {"source": {"source": "directory", "path": "<abs checkout>/.agents/plugins"}, "installLocation": "<same>", "lastUpdated": "<ISO8601>Z"}}. It is called once from claude_plugins/registrar.py::reconcile_native_plugins, after the settings commit.
  • The write is merge-only and atomic: one entry changes, and no other entry is touched. It is idempotent: an entry already naming this root, APM's or Claude's own, is left byte for byte. An unreadable registry is left alone, with a warning.
  • APM records each entry it wrote in $APM_HOME/claude-known-marketplaces.json. When a reconcile leaves no Claude native plugin in the project (apm uninstall, or the last plugin removed), APM removes only the entry it recorded, and only while that entry still names the same root.
  • An install in a linked git worktree prints [!] This checkout is a git worktree: Claude Code may load the main checkout's copy of its native plugins (anthropics/claude-code#111812). See https://microsoft.github.io/apm/consumer/install-plugins/#known-gaps.
  • Docs (consumer/install-plugins.md): a paragraph on the registry entry and Claude Code's folder-trust gate. The worktree known-gap row now cites Project plugin marketplace with a relative directory path resolves against the main checkout in a git worktree anthropics/claude-code#111812, and the "newly declared plugin loads from the second session" gap row is removed because this fixes it.

Lifecycle I5 write sets (W3d): exactly two new home paths are admitted for a project command, with no wildcard. Each module's own allowed-home tuple gets them: tests/lifecycle/rows.py _PROJECT_HOME_ALLOWED, tests/lifecycle/security/test_gate_removal.py _PROJECT_HOME_ALLOWED, tests/lifecycle/scenarios/_kit.py PROJECT_HOME_ALLOWED and tests/lifecycle/journeys/test_journeys.py _HOME_ALLOWED. tests/lifecycle/security/test_integrity.py reuses the rows.py tuple.

  • .claude/plugins/known_marketplaces.json: Claude's registry. The lifecycle world sets CLAUDE_CONFIG_DIR=$HOME/.claude.
  • .apm/claude-known-marketplaces.json: APM's ledger of the entry it wrote.

Lane: once #5201 lands, scripts/real_harness/expected.json flips claude-normal-project, claude-worktree-project, claude-headless-project and claude-headless-wt-branch-project to LOADS. claude-headless-wt-after-main-project stays PARTIAL (the known gap). That file is not on main yet, so the flip follows in this PR after #5201 merges.

Trust scope: APM registers the project marketplace only for a folder Claude already trusts. In CI or claude-code-action, an untrusted checkout does not auto-load project plugins. This is by design; use user scope or Claude's own trust setting.

Issue and approved scope

Issue: Fixes #5176 for folders Claude already trusts. Refs #5180 (outcome 1, a worktree-only install, is fixed; outcome 2, a worktree loading the main checkout's copy, stays open as a known gap: anthropics/claude-code#111812, with an install-time warning per ruling PD-47).

Human scope-approval comment: #5176 is status/accepted, with v0.39.0 scope from the maintainer lead. #5180 is assigned with #5176 as one design; the fix shape follows the real-harness runs (shape A).

Does this PR complete the issue, or what remains? It completes #5176. For #5180, outcome 2 remains a documented gap; the lane flip follows #5201.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Maintenance / refactor

Testing

  • Tested locally
  • All existing tests pass (targeted; CI runs the full suite)
  • Added tests for new functionality (if applicable)

Lifecycle cells in tests/integration/test_claude_known_marketplace_lifecycle.py (real CLI, isolated HOME). They were red on main and are green now:

  • test_project_install_lists_the_marketplace_for_claude_with_its_absolute_path[home|CLAUDE_CONFIG_DIR]: a fresh install writes the absolute entry under ~/.claude or CLAUDE_CONFIG_DIR (and only there); a re-install changes no byte.
  • test_install_and_uninstall_touch_only_apms_entry: another marketplace entry survives install and uninstall unchanged; uninstall removes APM's entry and the plugin root.
  • test_a_checkout_install_lists_its_own_plugin_root[main|worktree]: an install in a worktree alone lists that worktree's own absolute root and prints the worktree warning once; a main-checkout install prints none.

Validation (one targeted run at a time):

  • APM_BINARY_PATH=$(uv run which apm) uv run --extra dev pytest tests/integration/test_claude_known_marketplace_lifecycle.py tests/lifecycle/scenarios/test_ownership_siblings.py -n 0: 100 passed after merging main.
  • Neighbours (every module using reconcile_native_plugins, extraKnownMarketplaces or a lifecycle allowed-home set): 994 passed. The 12 tests/lifecycle/security/test_integrity.py failures fail the same way on this branch's base with every change stashed (a marketplace source_digest golden), so they are not from this change.
  • Lint mirror, all clean: uvx [email protected] check and format --check; scripts/lint_architecture_boundaries.py --root .; scripts/check_complexity_ratchet.py --base-ref origin/main; python -m tests.quality.taxonomy --changed; scripts/check_test_assertions.py; scripts/check_exact_test_duplicates.py; pylint R0801; scripts/lint-auth-signals.sh; python -m tests.lifecycle.generator --check; tests/quality/test_lifecycle_bug_ledger.py (new row bugs/5176.json).
  • Owner map: native_plugins/claude_marketplaces.py is added to the claude-native-plugin-registration selectors.

Changelog

  • One fragment per section, changelog.d/<pr>.<section>.md, holding
    one line: user-visible effect first, 25 words or fewer, ending with
    (#PR); breaking changes only under upgrade notes; no Highlights; no
    new lines under ## [Unreleased]. See changelog.d/README.md and
    .github/instructions/changelog.instructions.md.
  • N/A -- internal-only change (CI, tests, refactor).

Spec conformance (OpenAPM v0.1)

  • Spec edit
  • Manifest edit
  • Test edit
  • CONFORMANCE.{md,json} regenerated
  • N/A -- this PR does not change OpenAPM-observable behaviour.

The committed project declaration (req-na-003, req-na-007) is unchanged. The user-registry entry is a per-machine harness side effect outside the committed manifest and lock; if the Spec Mode B gate asks, a waiver line will name that.

…n loading

#5176: a project install must list its marketplace in Claude Code's
known_marketplaces.json with the absolute path (HOME and CLAUDE_CONFIG_DIR),
idempotently, touching no other entry, and uninstall removes only APM's.
#5180: after installs in two checkouts of one repository, each checkout's
marketplace must map to its own absolute plugin root.

Co-authored-by: Copilot App <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
@danielmeppiel Daniel Meppiel (danielmeppiel) added this to the 0.39.0 milestone Nov 23, 2026
Co-authored-by: Copilot App <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Inherited CLAUDE_CONFIG_DIR can make tests inspect the wrong registry and risk modifying a real profile, while inherited worktree bindings and preservation of pre-existing marketplace entries without APM ownership receipts remain untested.

3 open issues
What changed in this PR

This PR adds regression tests for Claude Code’s first-session and worktree plugin-loading issues (#5176 and #5180). The current diff establishes expected registry behavior; the production fix remains pending.

Changes:

  • Adds five lifecycle cases covering absolute marketplace paths, CLAUDE_CONFIG_DIR, reinstall idempotence, uninstall isolation, and checkout-specific mappings.
  • Adds a changelog fragment describing the intended loading fix.
File Description
tests/​integration/​test_claude_known_marketplace_lifecycle.py Adds real-CLI tests for marketplace registration and cleanup across isolated profiles and worktrees.
changelog.d/​5184.fixed.md Announces the intended first-session and worktree plugin-loading fix.

Review effort: Balanced

@danielmeppiel Daniel Meppiel (danielmeppiel) changed the title fix(native): let Claude Code load project plugins in a first session and in worktrees (#5176, #5180) fix(native): let Claude Code load project plugins in a first session and in worktree-only installs (#5176) Nov 23, 2026
Claude Code fully loads a native plugin's skills and hooks in a first session
only from a marketplace its user-level plugins/known_marketplaces.json lists.
A project-scope install with the Claude target now upserts that entry with
the checkout's absolute plugin root (CLAUDE_CONFIG_DIR honoured), keeping the
committed relative declaration. The write is merge-only, atomic and
idempotent; an APM ledger under APM_HOME records the entry so uninstall
removes only APM's. An install in a git worktree warns that Claude Code may
load the main checkout's copy (anthropics/claude-code#111812). The lifecycle
write sets admit exactly the two new home paths.

Co-authored-by: Copilot App <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: Copilot App <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: Copilot App <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unresolved registry collisions and ownership-tracking flaws can overwrite or delete entries APM does not own, while improperly isolated integration tests can modify a developer’s real Claude configuration.

7 open issues

Review effort: Balanced

…ckout

Review folds for the user-level registry write:
- add an entry only when none exists under the name; keep every existing
  entry (another clone or worktree, or Claude's own) byte for byte, warning
  when it names another root; accept only ^apm-p-[0-9a-f]{8}$ names;
- refuse a linked registry location with user_config_crosses_link, as the
  MCP user configs do;
- re-read before writing and redo the merge once if the file changed;
- treat an unreadable registry or receipt as a warning, never as empty;
- warn and continue on any registry error after the committed declaration,
  including a relative CLAUDE_CONFIG_DIR; keep non-ASCII values and print
  ASCII-safe warnings.

Co-authored-by: Copilot App <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Unresolved ownership-scoping and concurrency flaws can delete unowned Claude registry entries or lose marketplace registrations and the receipts needed for cleanup.

5 open issues
4 previously missed issues

In code that hasn't changed since last review

High severity Serialize concurrent marketplace registration and removal

src/​apm_cli/​native_plugins/​claude_marketplaces.py:122

Concurrent installs sharing HOME can both pass this check, then replace the registry with stale snapshots and lose another project's marketplace. The shared receipt has the same problem in _record() (lines 143–146): publishing an earlier ownership snapshot can erase another install's receipt, preventing later cleanup. Serialize registration and removal across APM processes from the ownership read through registry and receipt publication; an APM-only lock still cannot protect against Claude Code's independent writes.

High severity Track registry paths in ownership receipts

src/​apm_cli/​native_plugins/​claude_marketplaces.py:196

Ownership is checked only by marketplace name and checkout root, but the receipt does not identify the registry APM wrote. Install with CLAUDE_CONFIG_DIR=A, then uninstall with CLAUDE_CONFIG_DIR=B where Claude already has its own entry for the same checkout: this deletes B's unowned entry and discards A's receipt, leaving A's entry behind. Record the registry path alongside each owned entry and only remove entries from the registry covered by that receipt; preserve independent receipts when switching profiles.

Medium severity Distinguish null registry values from missing keys

src/​apm_cli/​native_plugins/​claude_marketplaces.py:165

An existing JSON null value is treated as an absent entry because dict.get() returns None in both cases. For a registry containing this marketplace name with a null value, install overwrites the existing value and claims ownership instead of preserving it and warning, contrary to the add-only behavior. Check whether the key exists, independently of its value.

Medium severity Recover when ownership recording fails after registration

src/​apm_cli/​native_plugins/​claude_marketplaces.py:186

If _record() fails after the registry entry is published, for example because APM_HOME is unwritable, the entry remains without ownership evidence. Reinstalling after fixing permissions preserves the existing entry without recording it, so uninstall cannot remove it. Make both writes recoverable through ownership journaling or safe rollback of the newly inserted entry, without claiming pre-existing Claude entries.

2 resolved since last review

Review effort: Balanced

… its registry

The receipt now records the root and the absolute registry path. It is written
before the registry entry and undone if publishing fails. Uninstall removes an
entry only from the registry the receipt names. Lifecycle cells no longer
inherit CLAUDE_CONFIG_DIR.

apm-spec-waiver: user-level Claude registry bookkeeping is not governed by an OpenAPM requirement

Co-authored-by: Copilot App <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Ownership receipts can lose track of entries across configuration directories or incorrectly claim concurrently created Claude entries, causing uninstall to leave APM entries behind or delete entries APM never created.

2 open issues
5 resolved since last review

Review effort: Balanced

Comment on lines +179 to +197
def add(known: dict[str, Any]) -> bool:
if name in known:
return False
now = datetime.now(UTC).isoformat(timespec="milliseconds").replace("+00:00", "Z")
known[name] = {
"source": {"source": "directory", "path": location},
"installLocation": location,
"lastUpdated": now,
}
return True

_record({**owned, name: {"root": location, "registry": str(target.absolute())}})
try:
_merge(target, add)
except BaseException:
_record(owned)
raise
if _registered_path(_read(target)[1].get(name)) != location:
_record(owned)
}
return True

_record({**owned, name: {"root": location, "registry": str(target.absolute())}})
…safety rules (#5176 review)

Real-CLI cells over the registry owner: a same-name entry for another
directory and a second clone are kept byte for byte with a warning; a
relative CLAUDE_CONFIG_DIR warns and the install succeeds; a linked ~/.claude,
plugins/ or registry is never written; a malformed, non-object or JSONC
registry is left unchanged; uninstall removes only an entry the receipt owns
for this root; an unreadable receipt is never overwritten; a unicode and
space root is recorded as is. A component cell covers the one-retry merge.
Docs name the keep-and-warn cases.

apm-spec-waiver: per-machine harness registry side effect outside the committed manifest and lock; req-na-003 and req-na-007 declarations unchanged

Co-authored-by: Copilot App <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
@danielmeppiel

Copy link
Copy Markdown
Collaborator Author

Superseded by a new pull request from branch fix-5176-claude-trusted-marketplace (same scope, plus registering only for folders Claude already trusts). Review continues there.

@danielmeppiel

Copy link
Copy Markdown
Collaborator Author

Superseded by #5230.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Unresolved ownership-tracking issues can cause uninstall to delete Claude-created entries after a no-op merge or leave APM-created entries behind when installations across profiles overwrite receipts.

2 open issues

Review effort: Balanced

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.

[BUG] Project-scope native plugin: Claude Code's first session loads the plugin's MCP server but not its skills or hooks

2 participants