-
Notifications
You must be signed in to change notification settings - Fork 2
refactor(disk-hygiene): share the unreferenced plugin-cache-version logic with claude-ops #5632
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2f33c0d
refactor(disk-hygiene): share the unreferenced plugin-cache-version r…
kyle-sexton 8ba6ace
Merge remote-tracking branch 'origin/main' into refactor/5221-share-p…
kyle-sexton f0eb4fe
chore(disk-hygiene): version and changelog for the shared plugin-cach…
kyle-sexton 7802885
fix(disk-hygiene): decode the plugin registry strictly and point the …
kyle-sexton 8f3ea42
chore: merge origin/main into refactor/5221-share-plugin-cache-versions
kyle-sexton 6b99754
fix(disk-hygiene): restore the blank line before the 0.41.0 changelog…
kyle-sexton 9ed595b
chore: merge origin/main into refactor/5221-share-plugin-cache-versions
kyle-sexton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| """Which plugin cache version directories does no installPath reference? | ||
|
|
||
| Claude Code records every installed plugin in ``plugins/installed_plugins.json`` | ||
| and keeps each version under ``plugins/cache/<marketplace>/<plugin>/<version>``. | ||
| A version no ``installPath`` names is orphaned: Claude Code stamps it with a | ||
| ``.orphaned_at`` marker (epoch milliseconds) and removes it after a window. | ||
|
|
||
| This module reads the registry and the markers and answers that question. It is | ||
| pure and read-only: every read goes through a caller-supplied ``read`` function | ||
| taking a path relative to the Claude directory, so each caller keeps its own | ||
| read guard. Its callers shape the answer into their own rows. | ||
|
|
||
| Python 3.11+, standard library only. This file is carried byte-identical by | ||
| every plugin that uses it (scripts/cross-plugin-source-registry.txt). | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import datetime as dt | ||
| import json | ||
| from collections.abc import Callable | ||
| from pathlib import Path | ||
| from typing import Any, NamedTuple | ||
|
|
||
| INSTALLED_PLUGINS = "plugins/installed_plugins.json" | ||
|
|
||
| # Days after an update or uninstall that Claude Code removes an orphaned plugin | ||
| # version, counted from its `.orphaned_at` marker. Basis: | ||
| # https://code.claude.com/docs/en/plugins/loading.md ("Cleanup of previous versions"), | ||
| # verified 2026-09-30; recheck when that section or a Claude Code changelog entry | ||
| # changes the window. | ||
| ORPHAN_SWEEP_DAYS = 14 | ||
|
|
||
| _DAY = 86400.0 | ||
|
|
||
| Reader = Callable[[str], str] | ||
|
|
||
|
|
||
| class Registry(NamedTuple): | ||
| """What the registry says about this cache. | ||
|
|
||
| ``doubt`` is empty when the registry can vouch for the cache. Otherwise it | ||
| says why not, and no version may be called unreferenced: a missing registry | ||
| is not evidence that every directory is orphaned. ``foreign`` marks the case | ||
| where the registry parsed but none of its paths lies under this cache. | ||
| """ | ||
|
|
||
| referenced: frozenset[Path] | ||
| installs: bool | ||
| doubt: str | ||
| foreign: bool | ||
|
|
||
|
|
||
| def install_paths(data: object) -> list[str]: | ||
| """Every `installPath` in the parsed registry, whatever scope or project entry holds it.""" | ||
| if isinstance(data, dict): | ||
| own = data.get("installPath") | ||
| found = [own] if isinstance(own, str) else [] | ||
| return found + [p for v in data.values() for p in install_paths(v)] | ||
| if isinstance(data, list): | ||
| return [p for v in data for p in install_paths(v)] | ||
| return [] | ||
|
|
||
|
|
||
| def resolve(path: str | Path) -> Path | None: | ||
| try: | ||
| return Path(path).expanduser().resolve() | ||
| except (OSError, RuntimeError): | ||
| return None | ||
|
|
||
|
|
||
| def load_registry(read: Reader, root: Path, label: str = INSTALLED_PLUGINS) -> Registry: | ||
| """Read the registry under ``root`` and resolve the paths it references. | ||
|
|
||
| ``label`` names the registry in ``doubt``. | ||
| """ | ||
| cache = root / "plugins" / "cache" | ||
| try: | ||
| data: Any = json.loads(read(INSTALLED_PLUGINS)) | ||
| except (OSError, ValueError) as exc: | ||
| return Registry( | ||
| frozenset(), False, f"{label} unreadable ({type(exc).__name__})", False | ||
| ) | ||
| if not isinstance(data, dict) or not isinstance(data.get("plugins"), dict): | ||
| return Registry(frozenset(), False, f"{label} has no `plugins` object", False) | ||
| referenced = frozenset( | ||
| p for p in map(resolve, install_paths(data)) if p is not None | ||
| ) | ||
| installs = bool(data["plugins"]) | ||
| cache_resolved = resolve(cache) | ||
| if installs and not any(cache_resolved in p.parents for p in referenced): | ||
| doubt = f"no installPath in {label} lies under {cache}" | ||
| return Registry(referenced, installs, doubt, True) | ||
| return Registry(referenced, installs, "", False) | ||
|
|
||
|
|
||
| def orphan_marker(read: Reader, version_rel: str, now: float) -> dict[str, Any] | None: | ||
| """The age of a version's ``.orphaned_at`` marker, or None when it is missing or unparsable.""" | ||
| try: | ||
| epoch = int(read(f"{version_rel}/.orphaned_at").strip()) / 1000 | ||
| age = (now - epoch) / _DAY | ||
| return { | ||
| "orphaned_at": dt.datetime.fromtimestamp(epoch, dt.timezone.utc).isoformat( | ||
| timespec="seconds" | ||
| ), | ||
| "marker_age_days": round(age, 1), | ||
| "past_sweep_window": age >= ORPHAN_SWEEP_DAYS, | ||
| } | ||
| except (OSError, ValueError, OverflowError): | ||
| return None |
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.