Skip to content

CLI-247 Post tool use#159

Open
kirill-knize-sonarsource wants to merge 1 commit intotask/kk/CLI-244-245-callback-infrastructurefrom
task/kk/CLI-247-post-tool-use
Open

CLI-247 Post tool use#159
kirill-knize-sonarsource wants to merge 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

This PR implements the claude-post-tool-use hook handler, which triggers SQAA (SonarQube Static Analysis Analysis) on files after an agent edits or writes them.

Previously, the hook command was a no-op stub. This PR replaces it with a functional handler that:

  • Reads tool metadata (tool name and file path) from stdin
  • Validates the tool is Edit or Write and the file exists
  • Checks authentication is set up for SonarCloud
  • Calls the existing SQAA API to analyze the file
  • Outputs results (issues/errors) as JSON for the agent to see

The implementation is defensive by design—it silently returns without output if authentication is missing, the file doesn't exist, the tool is not Edit/Write, or analysis fails. This prevents hook failures from blocking agent workflows.

What reviewers should know

Start with the main implementation: src/cli/commands/hook/agent-post-tool-use.ts is the entry point. The agentPostToolUse function orchestrates the flow: parse stdin → validate conditions → call SQAA API → format and output results.

Understand the defensive design: The function uses early returns to skip processing without output. This is intentional—see the comments around each condition (tool name, file existence, auth). The handler should never crash a hook.

Review the command registration: src/cli/command-tree.ts shows how the function is wired into the CLI. Note the change from optional to required --project option.

Focus on tests for behavior clarity: The unit tests in tests/unit/hook-post-tool-use.test.ts comprehensively cover both happy-path (analysis succeeds) and edge cases (no auth, missing file, unparseable stdin). The integration tests in tests/integration/specs/hook/hook-agent-post-tool-use.test.ts verify end-to-end behavior. These tests document the expected behavior better than comments do.

Note the result format: The output is wrapped in hookSpecificOutput with hookEventName: 'PostToolUse' and additionalContext containing formatted issue/error text. This is how the agent receives feedback from the hook.


  • 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
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 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 from 523d6f8 to 675cd92 Compare April 13, 2026 11:54
@kirill-knize-sonarsource kirill-knize-sonarsource force-pushed the task/kk/CLI-247-post-tool-use branch from 675cd92 to cf59297 Compare April 13, 2026 12:04
@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 logic duplication flagged in the previous round (the formatSqaaResult formatter re-implementing the same structure as displaySqaaResults) is still open and unresolved.

🗣️ Give feedback

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.

1 participant