Skip to content

Clean up old plugin versions from cache after auto-update#48

Open
kaghni wants to merge 6 commits intomainfrom
fix/plugin-cache-cleanup
Open

Clean up old plugin versions from cache after auto-update#48
kaghni wants to merge 6 commits intomainfrom
fix/plugin-cache-cleanup

Conversation

@kaghni
Copy link
Copy Markdown
Collaborator

@kaghni kaghni commented Apr 14, 2026

Summary

  • After a successful auto-update, removes all old cached version directories, keeping only the newly installed version
  • Applies to both Claude Code (~/.claude/plugins/cache/hivemind/hivemind/) and Codex (~/.codex/plugins/cache/.../hivemind/) caches
  • Cleanup failures are logged but never block the session

Test plan

  • Typecheck passes
  • Build succeeds
  • Simulated cache cleanup correctly removes old dirs and keeps latest

@claude
Copy link
Copy Markdown

claude bot commented Apr 14, 2026

Claude finished @kaghni's task in 3m 37s —— View job


Review

  • Codex cleanup deletes the newly-updated plugin (src/hooks/codex/session-start.ts lines 200–215): The Codex update copies files in-place into the existing versioned directory (e.g. 0.6.22/), which keeps its old name. e.name !== latest ("0.6.22" !== "0.6.23") is always true, so the block deletes the freshly-updated plugin every time. Fix this →
  • Cleanup runs even when the update silently failed: findCmd always exits 0 (the last command, rm -rf "$TMPDIR", succeeds regardless of whether git clone worked), so execSync(findCmd) never throws. This cleanup can wipe existing plugin dirs when the underlying update never actually installed anything.

The Claude Code path (src/hooks/session-start.ts) looks correct — claude plugin update is a proper package manager that creates a new versioned directory, so keeping only latest and discarding old ones is safe there.


Comment on lines +200 to +215
// Clean up old cached versions
try {
const cacheBase = join(homedir(), ".codex", "plugins", "cache");
const hivemindDirs = execSync(`find "${cacheBase}" -maxdepth 3 -name "hivemind" -type d 2>/dev/null`, { encoding: "utf-8" }).trim().split("\n").filter(Boolean);
for (const hDir of hivemindDirs) {
const entries = readdirSync(hDir, { withFileTypes: true });
for (const e of entries) {
if (e.isDirectory() && e.name !== latest) {
rmSync(join(hDir, e.name), { recursive: true, force: true });
log(`cache cleanup: removed old version ${e.name}`);
}
}
}
} catch (e: any) {
log(`cache cleanup failed: ${e.message}`);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The Codex update copies files in-place into the existing version directory (which keeps its old name, e.g. "0.6.22"), so e.name !== latest ("0.6.22" !== "0.6.23") is always true — this block will delete the freshly-updated plugin on every successful auto-update, leaving the cache empty. Additionally, findCmd always exits 0 (the final rm -rf "$TMPDIR" succeeds even when git clone fails), so execSync(findCmd) never throws and this cleanup can also wipe existing plugin dirs when the update silently failed.

Remove this cleanup block for Codex — the in-place copy strategy never creates a new versioned directory, so there is nothing stale to remove.

Suggested change
// Clean up old cached versions
try {
const cacheBase = join(homedir(), ".codex", "plugins", "cache");
const hivemindDirs = execSync(`find "${cacheBase}" -maxdepth 3 -name "hivemind" -type d 2>/dev/null`, { encoding: "utf-8" }).trim().split("\n").filter(Boolean);
for (const hDir of hivemindDirs) {
const entries = readdirSync(hDir, { withFileTypes: true });
for (const e of entries) {
if (e.isDirectory() && e.name !== latest) {
rmSync(join(hDir, e.name), { recursive: true, force: true });
log(`cache cleanup: removed old version ${e.name}`);
}
}
}
} catch (e: any) {
log(`cache cleanup failed: ${e.message}`);
}
// Codex update is in-place (files copied into the existing versioned dir);
// no stale version directories are created, so no cache cleanup is needed.

kaghni added 6 commits April 14, 2026 14:40
After a successful auto-update, remove all cached version directories
except the newly installed one. Prevents unbounded cache growth at
~/.claude/plugins/cache/hivemind/hivemind/ and
~/.codex/plugins/cache/.../hivemind/.
The Codex update uses find to dynamically locate the hivemind cache
directory. The cleanup should match that approach instead of hardcoding
the path.
Add || true to each scope's update command so a failure in one scope
(e.g. managed) doesn't abort the entire update and skip cache cleanup.
Codex update does cp -r into the same directory (no versioned
subdirectories), so there are no old versions to clean up.
@kaghni kaghni force-pushed the fix/plugin-cache-cleanup branch from f69e7c4 to 3dd49bc Compare April 14, 2026 21:40
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