Repository navigation
fix: replace Claude prompt hook with generated script - #493
Conversation
Move the Claude UserPromptSubmit hook out of an inline node -e command and into a generated project-local .claude/madar-user-prompt-submit.cjs script so Windows shell wrapping cannot truncate the command. Also tighten Claude uninstall and native-agent install verification to track the generated hook script lifecycle. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
📝 WalkthroughWalkthroughThis PR migrates Claude's ChangesClaude prompt applicability hook script migration
Sequence DiagramsequenceDiagram
participant install as installClaudeHook
participant write as writeClaudePromptHookScript
participant build as buildPromptApplicabilityHookScript
participant fs as File system
participant settings as .claude/settings.json
participant verify as inspectClaudeNativeAgentInstall
install->>write: prepare hook script
write->>build: generate script template
build-->>write: JavaScript source string
write->>fs: write .claude/madar-user-prompt-submit.cjs
install->>settings: update UserPromptSubmit hook command
verify->>fs: check if script exists
verify->>settings: read UserPromptSubmit hook presence
verify-->>verify: artifact check passes if script exists
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/infrastructure/compare.ts (1)
3163-3192:⚠️ Potential issue | 🟠 Major | ⚡ Quick winValidate the
UserPromptSubmitcommand, not just the script file.Line 3164 only tells us that some Madar
UserPromptSubmitentry exists. With the currentok: !hasUserPromptSubmitHook || existsSync(promptHookScriptPath)check, a stale or mutated hook command can still pass verification as long as.claude/madar-user-prompt-submit.cjshappens to exist. That makesinspectClaudeNativeAgentInstall()report the file-based migration as verified even when Claude is not actually executing the generated script, which then contaminates compare/benchmark readiness.Please key this artifact check off the actual
UserPromptSubmitcommand pointing at.claude/madar-user-prompt-submit.cjs, not only hook presence plus file existence.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/infrastructure/compare.ts` around lines 3163 - 3192, The check currently treats hasUserPromptSubmitHook as a boolean and only verifies file existence; instead retrieve the actual hook entry (e.g., const userPromptHook = findHookEntry(settingsPath, 'UserPromptSubmit')) and set the artifact's ok to validate that either no hook exists or the hook's command/target explicitly points to CLAUDE_PROMPT_HOOK_SCRIPT_RELATIVE_PATH (or endsWith 'madar-user-prompt-submit.cjs') AND the promptHookScriptPath exists; update the artifact for CLAUDE_PROMPT_HOOK_SCRIPT_RELATIVE_PATH to use userPromptHook and inspect its command/path rather than only checking !hasUserPromptSubmitHook || existsSync(promptHookScriptPath).
🧹 Nitpick comments (2)
tests/unit/install-templates.test.ts (1)
32-41: 💤 Low valueConsider a more defensive fallback when regex fails to match.
At line 40, if the
matchPayloadregex doesn't match, the function returns the entirehookScriptcontent. This could lead to false positives in tests if the expected strings appear elsewhere in the script rather than in the extracted payload.Consider returning an empty string or throwing an error for clarity:
const hookScript = readFileSync(hookScriptPath, 'utf8') const matchPayload = hookScript.match(/const matchPayload = ("(?:\\.|[^"])*")/) - return matchPayload?.[1] ? JSON.parse(matchPayload[1]) : hookScript + if (!matchPayload?.[1]) { + return '' + } + return JSON.parse(matchPayload[1])This makes test failures more obvious when the script format changes unexpectedly.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/unit/install-templates.test.ts` around lines 32 - 41, The current branch that handles command === 'node .claude/madar-user-prompt-submit.cjs' returns the full hookScript when the regex matchPayload fails, which can cause false positives; change the fallback so that if matchPayload?.[1] is falsy you return an empty string (or throw a clear error) instead of returning hookScript, i.e. update the logic around matchPayload and the final return to return '' on no match to make tests fail explicitly and avoid accidental matches.tests/unit/install.test.ts (1)
647-647: 💤 Low valueIs this package.json necessary for the test?
Line 647 writes a
package.jsonwithtype: 'module', but the hook script is a.cjsfile (CommonJS). Node can execute.cjsfiles regardless of the package.jsontypefield, and other tests in this file don't create a package.json.Was this line intended for this test, or is it leftover from an earlier iteration?
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/unit/install.test.ts` at line 647, The test calls writeFileSync(join(projectDir, 'package.json'), JSON.stringify({ type: 'module' }), 'utf8') which appears unnecessary for running a .cjs hook; remove that writeFileSync call (or replace it with an explicit comment) so the test no longer creates a package.json with "type": "module" unless there's a specific test assertion that requires it; look for the writeFileSync usage around the test setup with projectDir and remove or guard it to match other tests that don't create a package.json.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@src/infrastructure/compare.ts`:
- Around line 3163-3192: The check currently treats hasUserPromptSubmitHook as a
boolean and only verifies file existence; instead retrieve the actual hook entry
(e.g., const userPromptHook = findHookEntry(settingsPath, 'UserPromptSubmit'))
and set the artifact's ok to validate that either no hook exists or the hook's
command/target explicitly points to CLAUDE_PROMPT_HOOK_SCRIPT_RELATIVE_PATH (or
endsWith 'madar-user-prompt-submit.cjs') AND the promptHookScriptPath exists;
update the artifact for CLAUDE_PROMPT_HOOK_SCRIPT_RELATIVE_PATH to use
userPromptHook and inspect its command/path rather than only checking
!hasUserPromptSubmitHook || existsSync(promptHookScriptPath).
---
Nitpick comments:
In `@tests/unit/install-templates.test.ts`:
- Around line 32-41: The current branch that handles command === 'node
.claude/madar-user-prompt-submit.cjs' returns the full hookScript when the regex
matchPayload fails, which can cause false positives; change the fallback so that
if matchPayload?.[1] is falsy you return an empty string (or throw a clear
error) instead of returning hookScript, i.e. update the logic around
matchPayload and the final return to return '' on no match to make tests fail
explicitly and avoid accidental matches.
In `@tests/unit/install.test.ts`:
- Line 647: The test calls writeFileSync(join(projectDir, 'package.json'),
JSON.stringify({ type: 'module' }), 'utf8') which appears unnecessary for
running a .cjs hook; remove that writeFileSync call (or replace it with an
explicit comment) so the test no longer creates a package.json with "type":
"module" unless there's a specific test assertion that requires it; look for the
writeFileSync usage around the test setup with projectDir and remove or guard it
to match other tests that don't create a package.json.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 9f83f8a4-a32e-4d23-ace4-b2021076810a
📒 Files selected for processing (6)
src/infrastructure/compare.tssrc/infrastructure/install.tssrc/runtime/task-applicability.tstests/unit/compare-native-agent.test.tstests/unit/install-templates.test.tstests/unit/install.test.ts
Summary
Root cause
The previous minification-only fix was still shipping an installed UserPromptSubmit command around 8,857 characters long. That left it over the practical Windows shell budget, so the command could still be truncated and fail with unexpected EOF while looking for matching a quote.
Verification
Summary by CodeRabbit