CLI-815 Install Claude Code CAG hook#569
Conversation
|
This change is part of the following stack: Change managed by git-spice. |
✅ Deploy Preview for sonarqube-cli canceled.
|
|
Confirmed — |
Avoid leaving Claude PostToolUse hook resources active when context augmentation setup aborts before the feature is recorded.
Make rollback of extra context augmentation resources best-effort so cleanup errors do not mask the original tool integrate failure. Also, make log a warning if remove fails.
Install Claude PostToolUse and PostToolUseFailure CAG hooks through sonar context __hook Claude so CAG can dispatch on the hook event carried by the payload.
01768c9 to
9171f36
Compare
When Claude CAG was already recorded, a retry could skip hook resources as already applied but still rerun tool integrate. If that operation failed, the rollback removed the existing hook script/settings while state continued to report the feature as installed. Pass the owning integration id into the CAG feature and only roll back extra resources when the feature was not already recorded for the same integration, scope, and target root. First-install failures still clean up partial hook resources. Add regression coverage for a failing retry preserving the existing Claude CAG hooks.
This reverts commit 9abb91c.
This reverts commit 5fbab74.
This reverts commit cdd0c0e.
| expectSkillFile(harness, CLAUDE_SKILL_PATH, false); | ||
| expectClaudeCagHookInstalled(harness); |
There was a problem hiding this comment.
While working on this, I realized that a failing call to runToolIntegrateCommand would currently keep the skill file and the hooks installed. I think this is a declarative framework issue, not only a Context Augmentation issue for the following reasons, so I decided to keep the fix out of scope for this PR.
Today IntegrationInstaller.applyFeatureWithUniqueDependencies() applies resources first, then operations, and the feature is only recorded in state after both phases complete. If an operation fails, any resources already written by that feature remain on disk, and the feature is not recorded (as shown by this test). On a later sonar integrate run, the framework treats the feature as not installed, so deselecting it only declines installation; it does not call removeFeature(). I think that makes the problem even worse, because it leaves the user with no CLI-driven cleanup path for those partial resources.
Note that this problem doesn't seem to impact only "Context Augmentation", but a couple of other cases as well:
- Context Augmentation can leave generated skill resources if
context-augmentation-tool-integratefails. After this PR, we are leaving hooks as well. - Native Git global hooks can leave the generated hook file if
configure-global-hooks-pathfails. - Pre-commit framework integration can leave the
.pre-commit-config.yamlpatch ifactivate-hookfails.
A reasonable framework fix that I could think about would be to make a single feature application transactional around operation failures (i.e. rollback these resources in case of an operation fail):
- The framework snapshots target file content before applying each resource and restore it if a later operation fails before rethrowing the error, or:
- model partial installs in state so the CLI can offer cleanup on the next run.
Would any of these ideas make sense? Any thoughts are appreciated.
The hook is not about posttool only anymore
Code Review ✅ Approved 3 resolved / 3 findingsImplements Claude Code CAG hooks for lifecycle event tracking, while resolving template naming inconsistencies and improving error handling during rollback. No issues found. ✅ 3 resolved✅ Edge Case: Rollback aborts and masks original error if remove() throws
✅ Quality: CAG hook template named "PostTool" but used for Pre/PostFailure too
✅ Quality: Verify PostToolUse matcher tool names (PowerShell/Monitor)
OptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Gitar |
|



Depends on SonarSource/sonar-context-augmentation#325
Forward
PreToolUse,PostToolUseandPostToolUseFailureevents to context augmentation, with patterns that match the following tools:Bash,PowerShell,MonitorandRead.Summary by Gitar
PostToolUseandPostToolUseFailurehooks for Claude Code.settings.jsonpatching and ensured coexistence with existing SQAA hooks.CLAUDE.mdto detail the new Claude Code hook implementation and lifecycle.This will update automatically on new commits.