Skip to content
mentoor

Topic 4 of 5 · 5 min read

Wiring checks into the agent loop

Goal: Decide where each check runs (agent hook, pre-commit or CI) so failures reach the agent before a human reviews the change.

Put each check where it can reach the agent soonest: fast checks in an agent hook, thorough checks at commit, and the full set in CI as the final gate. The goal is that the agent fixes its own errors before a human reads the diff.

Check yourself · 1 of 3

Why run lint in an agent hook instead of only in CI?

Choose one answer

Why it matters

If checks run only in CI, the agent finishes, pushes, and a red build comes back minutes later, often after the context is gone. If they run while the agent works, the error arrives while it still knows what it was doing. Early feedback is cheaper, and your review time goes to design, not to typos.

The three places

  1. Agent hook (seconds). Tools like Claude Code support hooks: a command that runs after the agent edits a file or before it ends its turn. If the hook exits with a blocking code and prints the error, the agent sees the output and keeps working. Good for formatting, lint and a type check of changed files.
  2. Pre-commit (under a minute). Runs on staged files at commit time, for example with the pre-commit framework or Husky. Good for secret scans, lint and a fast test subset.
  3. CI (minutes). Runs the full suite on a clean machine: all tests, full type check, SAST, dependency scan. This is the gate that cannot be skipped locally, and the one the branch rules enforce.

How to decide

Ask of each check: how long does it take, and how bad is it to miss? Slow and critical (a full SAST run) goes in CI. Quick and common (format, lint) goes in the hook. Must never leak (secrets) goes in pre-commit and CI both.

Before and after

{
  "hooks": {
    "PostToolUse": [{
      "matcher": "Edit|Write",
      "hooks": [{ "type": "command", "command": "npm run lint:changed && npm run typecheck" }]
    }]
  }
}

Before: the agent edits ten files, pushes, and CI fails on a type error. After: the hook prints the error after the edit, the agent sees it, and fixes it in the same session.

What goes wrong

  • Slow hooks. A two-minute hook after every edit makes the agent crawl. Keep it to the changed files.
  • Silent hooks. A hook that fails without printing a message gives the agent nothing to fix.
  • Only local gates. Local hooks can be skipped. CI with required status checks is the one that holds.
  • Output flood. Print the first errors, not ten thousand lines.

Examples

Lint after every edit

A team adds a hook that runs ESLint on the file the agent just edited. Within one session the agent sees no-floating-promises on line 31 and adds an await. The pull request arrives without that error, so the reviewer reads the logic, not the lint output.

Splitting the checks

Format and lint on changed files: agent hook, 3 seconds. Gitleaks on staged files: pre-commit, 2 seconds. Full tests, CodeQL and OSV-Scanner: CI, 12 minutes, required to merge. Each check sits where its cost and risk say it should.

Key points

  • Run fast checks in an agent hook so errors reach the agent while it still has the context.
  • Use pre-commit for secret scans and quick checks on staged files.
  • Keep the full test, SAST and dependency scans in CI as the gate that cannot be skipped.
  • Choose the place by two questions: how long the check takes and how costly a miss is.
  • A hook must print a clear, short error and exit with a blocking code, or the agent has nothing to act on.

Common mistakes

  • Running every check only in CI, so the agent never sees failures while it can fix them.
  • Putting a slow full-suite run in a per-edit hook until the agent is unusably slow.
  • Relying on local hooks alone, which anyone or any agent can skip, without a required CI check.
  • Writing hooks that exit with an error but print nothing the agent can read.

Check yourself · 2 of 3

Where should a 15-minute full test and SAST run go?

Choose one answer

Check yourself · 3 of 3

A hook fails the change but prints nothing. What is the problem?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic