Skip to content

CLI-247 Post tool use#159

Merged
kirill-knize-sonarsource merged 1 commit intotask/kk/CLI-244-245-callback-infrastructurefrom
task/kk/CLI-247-post-tool-use
Apr 15, 2026
Merged

CLI-247 Post tool use#159
kirill-knize-sonarsource merged 1 commit intotask/kk/CLI-244-245-callback-infrastructurefrom
task/kk/CLI-247-post-tool-use

Conversation

@kirill-knize-sonarsource
Copy link
Copy Markdown
Member

No description provided.

@kirill-knize-sonarsource kirill-knize-sonarsource changed the base branch from master to task/kk/CLI-244-245-callback-infrastructure April 8, 2026 18:02
@sonar-review-alpha
Copy link
Copy Markdown
Contributor

sonar-review-alpha Bot commented Apr 8, 2026

Summary

CLI-247: PostToolUse Hook Handler Implementation

This PR implements the claude-post-tool-use hook command, which runs SonarQube Analysis As a Service (SQAA) on files after Claude Agent edits or writes them.

What changed:

  • New handler in agent-post-tool-use.ts that receives tool use metadata via stdin (tool name + file path), invokes the SQAA API, and outputs results as structured JSON
  • Command registration in command-tree.ts updated from a stub to call the handler; --project is now required
  • 14 unit tests covering all code paths (success, auth failures, missing params, unparseable input, etc.)
  • 2 integration tests against a fake SonarCloud server

Why: Enables the agent system to report code quality issues immediately after edits, providing real-time feedback to Claude as it modifies files. This is a building block in the callback infrastructure (follows CLI-244/245).

What reviewers should know

Where to start:

  • Primary logic: src/cli/commands/hook/agent-post-tool-use.ts — read the handler end-to-end; it's the core of this PR
  • Command wiring: src/cli/command-tree.ts lines 271–272 — shows how the CLI invokes the handler
  • Test coverage: tests/unit/hook-post-tool-use.test.ts — exhaustive unit tests demonstrating all code paths and edge cases

Non-obvious design choices:

  1. Non-blocking failure mode: The handler is designed to fail silently. It returns without output on auth failures, missing params, unparseable input, or SQAA errors. This is intentional — the hook should never block or crash the agent's tool flow.
  2. Multiple guards before API call: Auth validation, project key presence, cloud-only check, orgKey presence, and file existence are all checked separately before attempting SQAA analysis. This defensive pattern prevents unnecessary API calls.
  3. Output format: Successful analyses output a JSON structure with hookSpecificOutput.hookEventName = "PostToolUse" and additionalContext containing formatted analysis results (issues, errors, and a summary).
  4. SonarCloud-only: Explicitly checks connectionType === 'cloud' and requires orgKey — this handler does not support on-premise SonarQube servers.

Gotchas:

  • --project is now required (was optional in the stub). Callers must provide it or the hook will silently return.
  • Only 'Edit' and 'Write' tools trigger analysis; other tools (Read, etc.) are skipped.
  • The handler catches all exceptions from SQAA analysis and logs them at debug level — review integration tests to see how error scenarios are handled.

  • Generate Walkthrough
  • Generate Diagram

🗣️ Give feedback

@hashicorp-vault-sonar-prod
Copy link
Copy Markdown

hashicorp-vault-sonar-prod Bot commented Apr 8, 2026

CLI-247

sonar-review-alpha[bot]

This comment was marked as resolved.

@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-244-245-callback-infrastructure branch 2 times, most recently from e5bc9f8 to 6f2f053 Compare April 8, 2026 19:17
@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-247-post-tool-use branch from bbd8e20 to c8fca3a Compare April 8, 2026 19:34
sonar-review-alpha[bot]

This comment was marked as outdated.

@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-244-245-callback-infrastructure branch from 6f2f053 to aa6bc64 Compare April 8, 2026 19:58
@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-247-post-tool-use branch 2 times, most recently from d64356e to 80c4a76 Compare April 8, 2026 20:02
@kirill-knize-sonarsource kirill-knize-sonarsource changed the base branch from task/kk/CLI-244-245-callback-infrastructure to task/kk/CLI-246-prompt-submit April 8, 2026 20:05
sonar-review-alpha[bot]

This comment was marked as outdated.

@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-247-post-tool-use branch from 80c4a76 to 4853f25 Compare April 8, 2026 20:18
@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-246-prompt-submit branch from 143d5f1 to 31cb0f1 Compare April 8, 2026 20:18
@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-247-post-tool-use branch from 4853f25 to e2211ff Compare April 8, 2026 20:30
@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-246-prompt-submit branch 2 times, most recently from 140c6be to 020d2e9 Compare April 8, 2026 21:02
@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-247-post-tool-use branch from e2211ff to 581f8af Compare April 8, 2026 21:02
sonar-review-alpha[bot]

This comment was marked as outdated.

@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-247-post-tool-use branch from 581f8af to 00bea22 Compare April 8, 2026 21:34
@kirill-knize-sonarsource kirill-knize-sonarsource changed the base branch from task/kk/CLI-246-prompt-submit to task/kk/CLI-244-245-callback-infrastructure April 8, 2026 21:39
Copy link
Copy Markdown
Contributor

@sonar-review-alpha sonar-review-alpha Bot left a comment

Choose a reason for hiding this comment

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

Conclusion: The hook implementation itself is clean and the unit test suite is thorough. One logic duplication worth fixing, and the quality gate needs addressing before merge.

SonarQube Cloud status: Coverage on new code is at 69.1%, below the required 80% threshold. The integration test added in this PR targets an analyze sqaa edge case (unregistered project with --branch) rather than exercising the new agent-post-tool-use hook — so coverage of agent-post-tool-use.ts comes entirely from unit tests. See the reviewer guide above for details.

🗣️ Give feedback

hookSpecificOutput: { hookEventName: 'PostToolUse', additionalContext: text },
}) + '\n',
);
} catch (err) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Logic duplication: formatSqaaResult reimplements the same logic as displaySqaaResults in src/cli/commands/analyze/sqaa.ts (line 162). Both iterate over issues with the same [idx+1] message (line startLine) structure and handle the errors array the same way. They've already diverged: displaySqaaResults puts the rule on a separate Rule: X line, while this version inlines it as [rule].

If the output format needs to change (e.g. adding severity, effort, or a new field), both functions must be updated. Extract shared formatting logic — for example a buildSqaaIssueLines(issues, errors): string[] helper in a shared module — and have each caller apply its own output target on top.

  • Mark as noise

@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-247-post-tool-use branch from 00bea22 to 959f7da Compare April 8, 2026 22:10
sonar-review-alpha[bot]

This comment was marked as outdated.

@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-247-post-tool-use branch from 959f7da to 01e509f Compare April 8, 2026 22:27
sonar-review-alpha[bot]

This comment was marked as outdated.

@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-244-245-callback-infrastructure branch 2 times, most recently from e44c9d3 to d117a3a Compare April 8, 2026 23:34
@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-247-post-tool-use branch from 01e509f to ae124e1 Compare April 8, 2026 23:52
@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-244-245-callback-infrastructure branch 9 times, most recently from f4cbc21 to 468e45c Compare April 9, 2026 14:33
@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-247-post-tool-use branch from ae124e1 to 523d6f8 Compare April 9, 2026 14:47
sonar-review-alpha[bot]

This comment was marked as outdated.

@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-244-245-callback-infrastructure branch 3 times, most recently from 9191141 to 129cc50 Compare April 13, 2026 09:34
@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-247-post-tool-use branch 2 times, most recently from 675cd92 to cf59297 Compare April 13, 2026 12:04
sonar-review-alpha[bot]

This comment was marked as outdated.

@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-247-post-tool-use branch from cf59297 to 13f14cd Compare April 13, 2026 13:27
sonar-review-alpha[bot]

This comment was marked as outdated.

@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-247-post-tool-use branch from 13f14cd to cfd19fa Compare April 14, 2026 15:35
@sonarqubecloud
Copy link
Copy Markdown

Copy link
Copy Markdown
Contributor

@sonar-review-alpha sonar-review-alpha Bot left a comment

Choose a reason for hiding this comment

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

LGTM! ✅

The previously flagged logic duplication between formatSqaaResult (this file) and displaySqaaResults in src/cli/commands/analyze/sqaa.ts is still open — no shared helper has been extracted.

🗣️ Give feedback

Copy link
Copy Markdown
Contributor

@eray-felek-sonarsource eray-felek-sonarsource left a comment

Choose a reason for hiding this comment

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

Tested and LGTM

@kirill-knize-sonarsource kirill-knize-sonarsource merged commit c20e0df into task/kk/CLI-244-245-callback-infrastructure Apr 15, 2026
13 checks passed
@kirill-knize-sonarsource kirill-knize-sonarsource deleted the task/kk/CLI-247-post-tool-use branch April 15, 2026 09:42
kirill-knize-sonarsource added a commit that referenced this pull request Apr 16, 2026
* CLI-244 sonar callback — command infrastructure

* CLI-245 PreToolUse secrets scanner callback

* CLI-246 Move prompt submit hook from shell scripts (#158)

* CLI-247 PostToolUse SQAA analysis callback (#159)

* CLI-248 git hooks (#160)

* CLI-298 git pre push (#175)
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