Skip to Content
harness-kit

Migrating hand-written hooks

Replacing a project's hook scripts with harness-kit Policies, with this repository's own configuration as the worked example.

You'll: replace the hook scripts registered in .claude/settings.json with one command line and a list of Policies. Hand-written hooks each read the payload themselves and must get Claude Code's exit codes right: a crash, or an exit code other than 0 and 2, lets the tool call through. The harness-kit hook fails closed.

Before you start

  • harness-kit is installed and a first harness.config.ts works (quick start).
  • You can read each existing hook script, and you know which events and tools it is registered for.
  • Settings are rewritten by harness-kit settings sync; every other setting is kept.

List what each hook decides

For each hook script, write down the event and tools it is registered for, what it refuses or adds to the context, and what happens when it fails. Most map to a built-in Policy:

A hook script that…becomes
refuses commands a subagent may not runcommand-policy, with agentTypes and an allow-list
keeps edits out of generated or protected filesprotected-paths, generated-paths or allowed-paths
runs lint or tests before the agent stopsstop-command, scoped to the files that changed
checks the checkout or the toolchain at session startworkspace-links, worktree-health, toolchain
formats an edited fileformat-on-edit, with a formatter preset
guards git: branch names, pushes, stashes, commitsthe entries of GIT_CONVENTIONS
anything elsea Policy in code, or an external Policy in your language

Write the configuration

This repository's own hooks went through these steps. Its session-start scripts became codegraph, workspace-links and worktree-health. The script that screened the commands of its verifier subagent became three command-policy entries for that agent type. A stop check reminds an agent that changed harness-kit to update these docs:

harness.config.ts
import { defineConfig } from '@microeinhundert/harness-kit';

/**
 * What the verifier reads when a command is refused: the commands it may run.
 */
const VERIFIER_COMMANDS = 'The verifier only runs: npm run check|lint|typecheck|test|fmt:check|boundaries, with no option but --filter and --; git status|diff|log|show, without --output; node -v.';

/**
 * The files of harness-kit that make up its public surface, relative to the repository root: its published entries,
 * its command line and flags, its configuration's Schema, the constructors of its built-in Policies and the Schemas of
 * their options, its presets and its protocol, the modules the docs site's reference is generated from
 * (`projects/harness-kit/packages/harness-kit/tooling/reference/`). A change to one updates the kit's docs in the same change (AGENTS.md);
 * `projects/harness-kit/packages/harness-kit/tests/app/cli.repository-docs.test.ts` fails when the reference reads a file this list misses.
 */
const HARNESS_KIT_PUBLIC_SURFACE = [
  'src/app/harness-kit.ts',
  'src/app/testing.ts',
  'src/app/github-adapter.ts',
  'src/app/jira-adapter.ts',
  'src/app/cli/*-command.ts',
  'src/app/cli/*-flag.ts',
  'src/features/config/model/built-in-constructors.ts',
  'src/features/config/model/harness-config.ts',
  'src/features/*/lib/*-policy-constructor.ts',
  'src/features/*/lib/guard-constructors.ts',
  'src/features/codegraph/lib/codegraph-constructor.ts',
  'src/features/format/lib/format-on-edit-constructor.ts',
  'src/features/paths/lib/*-constructor.ts',
  'src/features/power/lib/keep-awake-constructor.ts',
  'src/features/protection/lib/*-constructor.ts',
  'src/features/pull-request/lib/pull-request-constructor.ts',
  'src/features/references/lib/references-constructor.ts',
  'src/features/rules/lib/rules-constructor.ts',
  'src/features/status-sync/lib/status-sync-constructor.ts',
  'src/features/stop-command/lib/stop-command-constructor.ts',
  'src/features/toolchain/lib/toolchain-constructor.ts',
  'src/features/tracker-fields/lib/tracker-fields-constructor.ts',
  'src/features/work-saved/lib/work-saved-constructor.ts',
  'src/features/workspace/lib/workspace-links-constructor.ts',
  'src/features/worktree/lib/worktree-health-constructor.ts',
  'src/features/*/model/*-options.ts',
  'src/features/*/model/*-operations.ts',
  'src/features/hook/model/decide-operation.ts',
  'src/features/agents/model/branch-switch.ts',
  'src/features/agents/model/readonly-agents.ts',
  'src/features/codegraph/model/codegraph-context.ts',
  'src/features/command/model/command-presets.ts',
  'src/features/command/model/command-table.ts',
  'src/features/forge/model/forge-guard.ts',
  'src/features/format/model/formatter.ts',
  'src/features/git/model/branch-name.ts',
  'src/features/git/model/commit-subject.ts',
  'src/features/git/model/git-conventions.ts',
  'src/features/git/model/merge-method.ts',
  'src/features/git/model/push-target.ts',
  'src/features/git/model/stash-safety.ts',
  'src/features/power/model/keep-awake.ts',
  'src/features/work-saved/model/work-saved.ts',
  'src/features/workspace/model/workspace-links.ts',
  'src/features/worktree/model/worktree-health.ts',
  'src/shared/lib/protocol-schema-print.ts',
  'src/shared/model/protocol-message.ts',
  'src/shared/model/protocol-schema.ts',
].map((file) => {
  return `projects/harness-kit/packages/harness-kit/${file}`;
});

/**
 * The public surface as git pathspecs, each a glob as `whenChanged` reads it.
 */
const SURFACE_PATHSPECS = HARNESS_KIT_PUBLIC_SURFACE.map((glob) => {
  return `':(glob)${glob}'`;
}).join(' ');

/**
 * Fails when the kit's docs did not change since the diff base (neither a tracked file of `projects/harness-kit/packages/harness-kit/docs/`
 * differs from it nor a new one is there), naming each surface file that changed: the stop check blocks once for each
 * state of its output, so a later change to another surface file blocks again.
 */
const HARNESS_KIT_DOCS_CHECK = [
  'if git diff --quiet {diff_base} -- projects/harness-kit/packages/harness-kit/docs && [ -z "$(git ls-files --others --exclude-standard -- projects/harness-kit/packages/harness-kit/docs)" ]; then',
  'echo "harness-kit\'s public surface changed, but projects/harness-kit/packages/harness-kit/docs did not: update the slice\'s file in projects/harness-kit/packages/harness-kit/docs (and the pages of projects/harness-kit/apps/docs/content/docs that cover it) in the same change, or say in the hand-off why no doc changes. Changed:";',
  `{ git diff --name-only {diff_base} -- ${SURFACE_PATHSPECS}; git ls-files --others --exclude-standard -- ${SURFACE_PATHSPECS}; } | sort -u;`,
  'exit 1;',
  'fi',
].join(' ');

/**
 * The repository's Claude Code hooks, which `harness-kit settings sync` writes into `.claude/settings.json`:
 *
 * - At a session start, the checkout the session works in syncs its CodeGraph index, and is checked for workspace
 *   packages it would load from another checkout (`@microeinhundert/*` and the kit's own `@microeinhundert/harness-kit`) and,
 *   in a worktree, for a stale base and a full disk. A subagent started in a worktree, which gets no session start,
 *   gets the same two checks once, at its first Bash or editing call there.
 * - A worktree agent's call of the CodeGraph MCP tool, whose server serves the main checkout, must name the worktree
 *   as `projectPath`; the worktree gets its own index, built in the background or synced before the call.
 * - The `verifier` subagent runs only the repository's checks, read-only git commands and `node -v`, writes no file and
 *   sets no variable. The project's `PreToolUse` hook screens its calls, as it screens those of every agent, by the
 *   payload's `agent_type`.
 * - At a stop, a session that changed harness-kit's public surface but not its docs is reminded to update them, once
 *   for each state of the change.
 *
 * The kit's own `self-protection` and `hook-bypass` Policies run beside them.
 */
export default defineConfig({
  policies: [
    {
      id: 'codegraph',
      use: 'codegraph',
    },
    {
      id: 'workspace-links',
      use: 'workspace-links',
    },
    {
      id: 'worktree-health',
      use: 'worktree-health',
    },
    {
      id: 'verifier-commands',
      options: {
        agentTypes: ['verifier'],
        allow: ['npm run check', 'npm run lint', 'npm run typecheck', 'npm run test', 'npm run fmt:check', 'npm run boundaries', 'git status', 'git diff', 'git log', 'git show', 'node'],
        default: 'deny',
        denyArgs: ['--output(?:=.*)?'],
        denyAssignments: true,
        denyWrites: true,
        message: VERIFIER_COMMANDS,
      },
      use: 'command-policy',
    },
    {
      // The allow-list above admits `npm run <check>` by its words, which leave options out: this table refuses every
      // option but `--` and turbo's `--filter`, so no argument passed on to a check writes files (`--fix`, `-u`,
      // `--graph=…`, `--write`).
      id: 'verifier-npm',
      options: {
        agentTypes: ['verifier'],
        denyArgs: ['-(?!-$|-filter(?:=.*)?$).*'],
        message: VERIFIER_COMMANDS,
        tool: 'npm',
      },
      use: 'command-policy',
    },
    {
      // The allow-list above admits `node` by its program alone, since `-v` is an option: this table admits no argument
      // but the version flag.
      id: 'verifier-node',
      options: {
        agentTypes: ['verifier'],
        denyArgs: ['(?!-v$|--version$).*'],
        message: VERIFIER_COMMANDS,
        tool: 'node',
      },
      use: 'command-policy',
    },
    {
      id: 'harness-kit-docs',
      options: {
        diffBase: [
          'session-start',
          {
            mergeBase: 'origin/main',
          },
        ],
        run: HARNESS_KIT_DOCS_CHECK,
        whenChanged: HARNESS_KIT_PUBLIC_SURFACE,
      },
      use: 'stop-command',
    },
  ],
});

The verifier's agent file declares no hook of its own: the project's PreToolUse entry fires inside subagents too, and a Policy with agentTypes runs only for those agents.

Replace the hook entries

Remove the old scripts' entries from .claude/settings.json and let the kit write its own:

npx harness-kit settings sync

It writes one entry per event a Policy runs for, matching only the tools the Policies name. Commit the configuration and the settings together, and have CI run harness-kit settings check, which fails when the two drift apart.

Check it

npx harness-kit config check
npx harness-kit explain < payload.json

config check names every problem of the configuration with its fix. explain prints each Policy that runs for a saved call, with its Decision and timing, and the merged answer (Debugging a configuration).

Then port the cases your old scripts were written for into tests of the new configuration (Testing a configuration), so the migration keeps every check it replaced.

If something goes wrong

  • The old hook still runs. Its entry is still in .claude/settings.json; remove it and run harness-kit settings sync again.
  • Nearly every call is refused. The configuration does not load. Run harness-kit config check; Debugging a configuration goes through it.
  • A call your script refused now passes. No Policy covers it yet. Write a test for the call, then add a Policy until the test passes.

On this page