HookStackGitHub
Back to catalogue
ValidationTaskCompleted

Test suite gate before task completion

Blocks a task from being marked as completed if the test suite fails. The agent receives the test output as feedback and must fix the failures before it can close the task, enforcing a green-build policy for every unit of work.

Use cases

  • Prevent agents from closing tasks with broken tests in multi-agent pipelines
  • Enforce a green-build policy without human review for every agent task
  • Surface test failures back to the agent as actionable feedback

Providers & tags

Claude Code
#tasks#tests#quality-gate#agent-teams#ci

settings.json fragment

{
  "hooks": {
    "TaskCompleted": [
      {
        "hooks": [
          {
            "command": "node $CLAUDE_PROJECT_DIR/.claude/hooks/task-completed-test-gate.mjs",
            "type": "command"
          }
        ]
      }
    ]
  }
}

Script · .claude/hooks/task-completed-test-gate.mjs

#!/usr/bin/env node
// Bloque la complétion d'une tâche si les tests échouent (TaskCompleted)
import { readFileSync } from 'fs';
import { execSync } from 'child_process';
import { fileURLToPath } from 'url';

function defaultExec(cmd) {
  return execSync(cmd, { stdio: 'pipe', timeout: 120_000 });
}

export function run(input, { exec = defaultExec } = {}) {
  try {
    exec('npm test --if-present 2>&1');
    return null;
  } catch (e) {
    const out = (e.stdout ?? e.stderr ?? e.message).toString().slice(0, 800);
    return {
      exitCode: 2,
      message: `Tests must pass before completing "${input.task_subject}".\n${out}`,
    };
  }
}

/* v8 ignore next 6 */
if (process.argv[1] === fileURLToPath(import.meta.url)) {
  const input = JSON.parse(readFileSync(0, 'utf8'));
  const result = run(input);
  if (result) {
    process.stderr.write(result.message);
    process.exit(result.exitCode);
  }
}