-
Notifications
You must be signed in to change notification settings - Fork 31
fix(cli): clean up hooks from Claude settings on package postuninstal… #629
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
Open
AVPthegreat
wants to merge
1
commit into
FailproofAI:main
Choose a base branch
from
AVPthegreat:fix/postuninstall-remove-claude-hooks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+131
−0
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // @vitest-environment node | ||
| import { describe, it, expect, beforeEach, vi } from "vitest"; | ||
| import { cleanClaudeSettings } from "../../scripts/postuninstall.mjs"; | ||
|
|
||
| vi.mock("node:fs", async () => { | ||
| const actual = await vi.importActual<typeof import("node:fs")>("node:fs"); | ||
| let mockStore: Record<string, string> = {}; | ||
| return { | ||
| ...actual, | ||
| existsSync: vi.fn((path: string) => path in mockStore), | ||
| readFileSync: vi.fn((path: string) => mockStore[path] ?? "{}"), | ||
| writeFileSync: vi.fn((path: string, content: string) => { | ||
| mockStore[path] = content; | ||
| }), | ||
| __resetMockStore: () => { | ||
| mockStore = {}; | ||
| }, | ||
| __setMockFile: (path: string, content: string) => { | ||
| mockStore[path] = content; | ||
| }, | ||
| __getMockFile: (path: string) => mockStore[path], | ||
| }; | ||
| }); | ||
|
|
||
| describe("scripts/postuninstall", () => { | ||
| beforeEach(async () => { | ||
| vi.resetAllMocks(); | ||
| const fs = await import("node:fs"); | ||
| (fs as unknown as { __resetMockStore: () => void }).__resetMockStore(); | ||
| }); | ||
|
|
||
| it("returns 0 when settings file does not exist", async () => { | ||
| const count = cleanClaudeSettings("/nonexistent/settings.json"); | ||
| expect(count).toBe(0); | ||
| }); | ||
|
|
||
| it("removes failproofai hook entries from Claude settings and writes clean file", async () => { | ||
| const fs = await import("node:fs"); | ||
| const testPath = "/mock/.claude/settings.json"; | ||
| const initialSettings = { | ||
| hooks: { | ||
| PreToolUse: [ | ||
| { | ||
| hooks: [ | ||
| { | ||
| type: "command", | ||
| command: "failproofai --hook PreToolUse", | ||
| is_failproofai: true, | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
| (fs as unknown as { __setMockFile: (p: string, c: string) => void }).__setMockFile( | ||
| testPath, | ||
| JSON.stringify(initialSettings), | ||
| ); | ||
|
|
||
| const count = cleanClaudeSettings(testPath); | ||
| expect(count).toBe(1); | ||
|
|
||
| const updatedRaw = (fs as unknown as { __getMockFile: (p: string) => string }).__getMockFile(testPath); | ||
| const updated = JSON.parse(updatedRaw); | ||
| expect(updated.hooks).toBeUndefined(); | ||
| }); | ||
| }); |
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,63 @@ | ||
| #!/usr/bin/env node | ||
| /** | ||
| * Postuninstall script for failproofai. | ||
| * Automatically cleans up failproofai hook entries from Claude Code & CLI settings files | ||
| * when `npm uninstall -g failproofai` or `bun remove -g failproofai` is executed. | ||
| */ | ||
| import { readFileSync, writeFileSync, existsSync } from "node:fs"; | ||
| import { resolve } from "node:path"; | ||
| import { homedir } from "node:os"; | ||
|
|
||
| function isFailproofaiHook(hook) { | ||
| if (!hook || typeof hook !== "object") return false; | ||
| if (hook.is_failproofai === true || hook.__failproofai_hook === true) return true; | ||
| const cmd = typeof hook.command === "string" ? hook.command : ""; | ||
| return cmd.includes("failproofai") && cmd.includes("--hook"); | ||
| } | ||
|
|
||
| export function cleanClaudeSettings(settingsPath) { | ||
| if (!existsSync(settingsPath)) return 0; | ||
| try { | ||
| const raw = readFileSync(settingsPath, "utf8"); | ||
| const settings = JSON.parse(raw); | ||
| if (!settings.hooks) return 0; | ||
|
|
||
| let removed = 0; | ||
| for (const eventType of Object.keys(settings.hooks)) { | ||
| const matchers = settings.hooks[eventType]; | ||
| if (!Array.isArray(matchers)) continue; | ||
| for (let i = matchers.length - 1; i >= 0; i--) { | ||
| const matcher = matchers[i]; | ||
| if (!matcher.hooks) continue; | ||
| const before = matcher.hooks.length; | ||
| matcher.hooks = matcher.hooks.filter((h) => !isFailproofaiHook(h)); | ||
| removed += before - matcher.hooks.length; | ||
| if (matcher.hooks.length === 0) matchers.splice(i, 1); | ||
| } | ||
| if (matchers.length === 0) delete settings.hooks[eventType]; | ||
| } | ||
| if (Object.keys(settings.hooks).length === 0) delete settings.hooks; | ||
|
|
||
| writeFileSync(settingsPath, JSON.stringify(settings, null, 2) + "\n", "utf8"); | ||
| return removed; | ||
| } catch { | ||
| return 0; | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| const userClaudePath = resolve(homedir(), ".claude", "settings.json"); | ||
| const projectClaudePath = resolve(process.cwd(), ".claude", "settings.json"); | ||
| const localClaudePath = resolve(process.cwd(), ".claude", "settings.local.json"); | ||
|
|
||
| let count = 0; | ||
| count += cleanClaudeSettings(userClaudePath); | ||
| count += cleanClaudeSettings(projectClaudePath); | ||
| count += cleanClaudeSettings(localClaudePath); | ||
|
|
||
| if (count > 0) { | ||
| console.log(`[failproofai] Postuninstall: Cleaned up ${count} hook entry(ies) from Claude settings.`); | ||
| } | ||
| } catch { | ||
| // Fail-open: postuninstall script must never throw | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: FailproofAI/failproofai
Length of output: 50380
🏁 Script executed:
Repository: FailproofAI/failproofai
Length of output: 6150
Use a precise FailproofAI hook identity contract. The substring classifier will clean hooks just because they contain
failproofaiand--hook, e.g.my-failproofai-helper --hook PreToolUse. Match only explicit FailproofAI markers or the exact installer-generated command form, and add a test case that asserts non-FailproofAI hooks with similar text are preserved.📍 Affects 2 files
scripts/postuninstall.mjs#L11-L15(this comment)__tests__/scripts/postuninstall.test.ts#L37-L66🤖 Prompt for AI Agents
Source: Coding guidelines