Skip to content

AP-1582 Run changed-file checks before pushes - #8519

Open
erwan-leforestier-sonarsource wants to merge 4 commits into
ap/AP-1530from
ap/AP-1582
Open

erwan-leforestier-sonarsource wants to merge 4 commits into
ap/AP-1530from
ap/AP-1582

Conversation

@erwan-leforestier-sonarsource

@erwan-leforestier-sonarsource erwan-leforestier-sonarsource commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor
  • Run the existing mise checks that cover the changed files before agent and human pushes.
  • Report actionable findings while logging unavailable checks locally. Reuse a passing agent check in the Git hook to avoid running it twice.
  • Cover task selection and push behavior in CI.

Part of AP-1486. Implements AP-1582.


Summary by Gitar

  • Hook trust and setup:
    • Documented that agent hooks need project hook approval and mise run install:npm for Husky setup
  • Ignore rules:
    • Un-ignored .claude/settings.json and .codex/hooks.json in .gitignore
  • Git-state logging:
    • Results are written to agent-hooks/last.json under the Git directory, not committed
  • Agent hook wiring:
    • Added Claude, Codex, and opencode pre-push hooks that block findings through the pre-push.sh adapter
  • Repeat findings:
    • Same unchanged finding blocks twice, then warns once without blocking, resetting after related changes

This will update automatically on new commits.

@erwan-leforestier-sonarsource
erwan-leforestier-sonarsource marked this pull request as ready for review November 30, 2026 15:57
@hashicorp-vault-sonar-prod

hashicorp-vault-sonar-prod Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

AP-1582

Comment thread tools/agent-hooks/pre-push.mjs Resolved
Comment thread tools/agent-hooks/pre-push.mjs Resolved
Comment thread tools/agent-hooks/pre-push.mjs Resolved
Comment thread .codex/hooks.json Outdated
"hooks": [
{
"type": "command",
"command": "bash .agents/hooks/pre-push.sh",

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Edge Case: Codex/Tirion hook commands use a cwd-relative script path

.codex/hooks.json and .tirion/hooks.json run bash .agents/hooks/pre-push.sh, which only resolves when the agent session's working directory is the repository root. If the session starts in a subdirectory (e.g. packages/analysis), bash exits 127 with 'No such file or directory'. The hook then fails as non-blocking and no pre-push check runs. The Claude config avoids this by using $CLAUDE_PROJECT_DIR. Resolve the script from the git toplevel instead.

Resolve the script from the repository root:

"command": "bash "$(git rev-parse --show-toplevel)/.agents/hooks/pre-push.sh"",
  • Apply fix

Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎

Comment thread tools/agent-hooks/results.mjs Resolved
Comment thread tools/agent-hooks/environment.mjs Resolved
Comment thread tools/agent-hooks/pre-push.mjs Resolved
Comment on lines +21 to +22
const gitLocation = /^(?:GIT_DIR|GIT_WORK_TREE|GIT_COMMON_DIR)(?:=|$)/;
const directoryCommands = new Set(['cd', 'pushd', 'popd']);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Edge Case: Any cd/pushd now skips the agent check, even within the same checkout

changesCheckout returns false for the whole command as soon as any segment starts with cd, pushd or popd. It does not check where the command changes to. So cd /work/repo && git push, cd tools && git push and (cd sub && make); git push all skip the agent check, even though the push still runs in the checkout that owns the hook. Agents emit the cd <repo> && git push form all the time. BUILD.md (line 23) only says pushes that use Git global options like -C/--git-dir are left to Git, so the cd and GIT_DIR= cases are undocumented. Where Husky is not installed, the push gets no check at all. Fix: resolve the cd target against the payload cwd and skip only when it leaves the owning checkout, or at least document that any directory change skips the agent check.

Was this helpful? React with 👍 / 👎

Comment thread tools/agent-hooks/push-command.mjs Resolved
Comment thread tools/agent-hooks/results.mjs Resolved
@gitar-bot

gitar-bot Bot commented Nov 30, 2026

Copy link
Copy Markdown
Code Review 👍 Approved with suggestions 🔴 High risk 8 closed / 10 findings

Repository policy: agent and Husky hooks enforce changed-file checks before pushes, potentially blocking pushes on findings and changing required validation controls.

Adds changed-file mise checks to agent and human pushes, reporting actionable findings, logging unavailable checks locally, and reusing passing agent checks in the Git hook.

Approved with suggestions: Codex and Tirion hook commands use a cwd-relative .agents/hooks/pre-push.sh path that fails outside the repository root, and any cd/pushd in a push command currently skips the agent check even within the same checkout. Resolving the script from the git toplevel and scoping the cd skip to leaving the owning checkout would address both.

💡 Edge Case: Codex/Tirion hook commands use a cwd-relative script path

📄 .codex/hooks.json:9 📄 .tirion/hooks.json:10 📄 .claude/settings.json:9

.codex/hooks.json and .tirion/hooks.json run bash .agents/hooks/pre-push.sh, which only resolves when the agent session's working directory is the repository root. If the session starts in a subdirectory (e.g. packages/analysis), bash exits 127 with 'No such file or directory'. The hook then fails as non-blocking and no pre-push check runs. The Claude config avoids this by using $CLAUDE_PROJECT_DIR. Resolve the script from the git toplevel instead.

Resolve the script from the repository root
"command": "bash "$(git rev-parse --show-toplevel)/.agents/hooks/pre-push.sh"",
💡 Edge Case: Any cd/pushd now skips the agent check, even within the same checkout

📄 tools/agent-hooks/push-command.mjs:21-22 📄 tools/agent-hooks/push-command.mjs:37-42 📄 tools/agent-hooks/push-command.mjs:68-80 📄 docs/systems/infra/BUILD.md:23

changesCheckout returns false for the whole command as soon as any segment starts with cd, pushd or popd. It does not check where the command changes to. So cd /work/repo && git push, cd tools && git push and (cd sub && make); git push all skip the agent check, even though the push still runs in the checkout that owns the hook. Agents emit the cd <repo> && git push form all the time. BUILD.md (line 23) only says pushes that use Git global options like -C/--git-dir are left to Git, so the cd and GIT_DIR= cases are undocumented. Where Husky is not installed, the push gets no check at all. Fix: resolve the cd target against the payload cwd and skip only when it leaves the owning checkout, or at least document that any directory change skips the agent check.

✅ 8 closed
✅ Bug: Non-canonical license headers will fail license:headers

📄 tools/agent-hooks/pre-push.mjs:9-13 📄 tools/agent-hooks/pre-push.test.mjs:9-13
The repo's license:headers task runs com.mycila:license-maven-plugin:check over **/*.mjs (see pom.xml), and it expects the canonical header. In that header, line 10 reads WITHOUT ANY WARRANTY; without even and line 12 reads Sonar Source-Available License for more details., as in .github/tests/check-docs.test.mjs. Two new files don't match it:

  • tools/agent-hooks/pre-push.mjs line 12 says ...License Version 1 for more details.
  • tools/agent-hooks/pre-push.test.mjs line 10 uses a comma (WARRANTY, without), and line 12 repeats the line-7 text ...Version 1, as published by SonarSource Sàrl.

The plugin only ignores whitespace differences, so both files will fail the header check. This PR's own selection adds license:headers for any .mjs change, so the new hook will block its own pushes with a header finding (and CI will fail too).

✅ Bug: Internal time budgets exceed the 260s agent hook timeout

📄 tools/agent-hooks/pre-push.mjs:32-34 📄 tools/agent-hooks/pre-push.mjs:162-168 📄 tools/agent-hooks/pre-push.mjs:196-199 📄 tools/agent-hooks/environment.mjs:71-85 📄 tools/agent-hooks/environment.mjs:103-112 📄 .claude/settings.json:10 📄 .codex/hooks.json:10 📄 .tirion/hooks.json:11
The agent hooks set timeout: 260 (Claude, Codex, Tirion), but the script can legitimately take longer:

  • The single mise run gets taskTimeout = 240s.
  • Before that, readiness() runs a Maven probe for every selected test:plugin:* task: 4s offline, then 6s online, so up to 10s each. With all six modules that is about 60s.
  • On top of that come several git calls and a TypeScript preProcessFile pass over every package TS file.

When the agent's timeout fires first, the hook is killed and treated as a non-blocking failure. The push then goes ahead with no finding reported and no last.json result written. The orphaned node/mise process can keep running, and the Husky --git hook for the same push starts a second, concurrent mise run over the same lib/ output. Keep the total budget under the hook timeout, for example by deriving taskTimeout from a deadline that includes the preflight time, or by raising the hook timeouts accordingly.

✅ Edge Case: Main-module guard silently disables hooks under symlinked paths

📄 tools/agent-hooks/pre-push.mjs:271-273 📄 tools/agent-hooks/selection.mjs:153 🔗 link 🔗 link
Both entry points only run when process.argv[1] === import.meta.filename:

  • pre-push.mjs
  • selection.mjs --list

Node sets process.argv[1] with path.resolve() and does not resolve symlinks, but import.meta.filename is the realpath of the main module. pre-push.sh builds $root with plain pwd, which keeps symlinks. So when the checkout sits under a symlinked directory (common on macOS, e.g. /tmp → /private/tmp, or symlinked workspaces), the two values differ. In that case main() never runs: the hook exits 0, no checks run, and no last.json is written. Compare realpathSync(process.argv[1]) with import.meta.filename, or use pwd -P in the shell wrapper.

✅ Security: Task passes if its output contains a 'Finished in' line

📄 tools/agent-hooks/results.mjs:43-49 📄 tools/agent-hooks/results.mjs:66-79 📄 tools/agent-hooks/pre-push.mjs:162-169 📄 tools/agent-hooks/pre-push.mjs:199-213
classifyRun marks a task as finished when any of its [task] -prefixed lines starts with Finished in . classify then returns passed before it looks at any diagnostics. Nothing checks the exit status of the mise run process, and nothing else checks per-task failure.

The selected tasks run code from the branch, e.g. npm run bbf and node --test over changed test files. If that code prints a line starting with Finished in (on purpose or by accident), the task is recorded as passed even when it then reports a real diagnostic and fails. The finding is suppressed and the push is not blocked. If nothing else blocks, the agent run also writes a stamp, so the following Husky hook reuses it and skips the check.

The impact is limited to the local pre-push check; CI still runs. Still, the result should not depend on text the task under test can print. Match only the timing line that mise itself emits, e.g. the last prefixed line matching /^Finished in [\d.]+\s*(?:ms|s|m)$/. Also treat any task as failed when run.status !== 0 and that task shows diagnostics.

✅ Edge Case: Detached mise batch survives Ctrl-C on a human git push

📄 tools/agent-hooks/environment.mjs:91-105 📄 tools/agent-hooks/environment.mjs:62-76
runBounded now spawns with detached: !windows. On POSIX, that puts mise in its own session and process group. The group-kill in killTree is only reached from this module's own timer or output-limit path. When a developer presses Ctrl-C during git push, the terminal sends SIGINT to the foreground process group: git, the Husky sh, the bash .agents/hooks/pre-push.sh --git subshell, and node. Node has no SIGINT/SIGTERM handler, so it exits straight away and the setTimeout that would kill the group dies with it. The detached mise run --continue-on-error ... batch (Maven/node test runs) keeps running in the background, unbounded and unsupervised. The same happens if an agent kills the hook process early. Before this change, spawnSync children shared the foreground group and received the SIGINT directly. Fix: install SIGINT/SIGTERM/SIGHUP handlers while a bounded child is running. Each handler should call killTree(child) and then re-raise or exit.

...and 3 more closed from earlier reviews

🤖 Prompt for agents
Code Review: Adds changed-file mise checks to agent and human pushes, reporting actionable findings, logging unavailable checks locally, and reusing passing agent checks in the Git hook.
  
  Approved with suggestions: Codex and Tirion hook commands use a cwd-relative `.agents/hooks/pre-push.sh` path that fails outside the repository root, and any `cd`/`pushd` in a push command currently skips the agent check even within the same checkout. Resolving the script from the git toplevel and scoping the `cd` skip to leaving the owning checkout would address both.

1. 💡 Edge Case: Codex/Tirion hook commands use a cwd-relative script path
   Files: .codex/hooks.json:9, .tirion/hooks.json:10, .claude/settings.json:9

   `.codex/hooks.json` and `.tirion/hooks.json` run `bash .agents/hooks/pre-push.sh`, which only resolves when the agent session's working directory is the repository root. If the session starts in a subdirectory (e.g. `packages/analysis`), bash exits 127 with 'No such file or directory'. The hook then fails as non-blocking and no pre-push check runs. The Claude config avoids this by using `$CLAUDE_PROJECT_DIR`. Resolve the script from the git toplevel instead.

   Fix (Resolve the script from the repository root):
   "command": "bash "$(git rev-parse --show-toplevel)/.agents/hooks/pre-push.sh"",

2. 💡 Edge Case: Any cd/pushd now skips the agent check, even within the same checkout
   Files: tools/agent-hooks/push-command.mjs:21-22, tools/agent-hooks/push-command.mjs:37-42, tools/agent-hooks/push-command.mjs:68-80, docs/systems/infra/BUILD.md:23

   `changesCheckout` returns false for the whole command as soon as any segment starts with `cd`, `pushd` or `popd`. It does not check where the command changes to. So `cd /work/repo && git push`, `cd tools && git push` and `(cd sub && make); git push` all skip the agent check, even though the push still runs in the checkout that owns the hook. Agents emit the `cd <repo> && git push` form all the time. BUILD.md (line 23) only says pushes that use Git global options like `-C`/`--git-dir` are left to Git, so the `cd` and `GIT_DIR=` cases are undocumented. Where Husky is not installed, the push gets no check at all. Fix: resolve the `cd` target against the payload cwd and skip only when it leaves the owning checkout, or at least document that any directory change skips the agent check.

Verification Rules 4/4 met PRs 2 inspected

Functional validation No results

Rules 4 passed 3 not applicable

✅ Internal artifact registries
✅ Keep SonarJS documentation in sync
✅ Preserve SonarJS generated sources and public contracts
✅ Repository license compliance

Broader impact PRs 2 inspected
Cross-PR 2 PRs inspected 0 open impacts

PR 8518: AP-1530 Organize and verify SonarJS agent documentation Inspected
PR 8505: AP-1516 Run fine-grained mise checks in CI Inspected

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Gitar

This branch has not been deployed

No deployments
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