Skip to Content
harness-kit

Writing a custom Policy

Write a Policy in TypeScript beside the built-in ones, for a rule no built-in Policy covers.

You'll: write a rule of your own as a Policy in harness.config.ts, for a case no built-in Policy covers, and see what the agent reads when it fires.

Before you start

  • A working harness.config.ts (see the quick start).
  • A little Effect: Effect.succeed(decision) is enough for a rule that only looks at the call.
  • Import Effect from @microeinhundert/harness-kit/effect, the copy the hook has already loaded, never from effect directly.
  • A rule in another language, or one a team shares across repositories without TypeScript, is an external Policy instead.

Refuse a call

This Policy refuses every write of a .env file by the Write and Edit tools:

harness.config.ts
import { DecisionSchema, defineConfig, type HookInput } from '@microeinhundert/harness-kit';
import { Duration, Effect, Option, Predicate, String } from '@microeinhundert/harness-kit/effect';

/**
 * Whether a tool call writes a `.env` file.
 */
const writesEnvFile = (input: HookInput) => {
  if (!Predicate.isTagged(input, 'PreToolUse')) {
    return false;
  }

  return Option.exists(input.toolInput.filePath, String.endsWith('.env'));
};

/**
 * A Policy written in code beside the entries: it refuses every write of a `.env` file and allows every other call.
 */
export default defineConfig({
  policies: [
    {
      events: ['PreToolUse'],
      failMode: 'closed',
      id: 'no-env-files',
      run: (input) => {
        if (writesEnvFile(input)) {
          return Effect.succeed(
            DecisionSchema.cases.Deny.make({
              fix: 'Write the variable into .env.example and ask a person to set it.',
              reason: 'An agent does not write .env files, which hold secrets.',
            }),
          );
        }

        return Effect.succeed(DecisionSchema.cases.Allow.make({}));
      },
      timeout: Duration.seconds(1),
      tools: ['Write', 'Edit'],
    },
  ],
});

A Policy in code has an id, its events, optional tool and agent matchers, a fail mode and a timeout, and a run function that returns a Decision as an Effect. In this one:

  1. events and tools decide which calls reach run: here only PreToolUse calls of Write and Edit.
  2. run gets the call as a HookInput, told apart by its _tag, the event. A tool call carries toolName and toolInput, whose filePath and command are read for you as Options; the whole input is in toolInput.raw. Every call carries sessionId, cwd and, inside a subagent, agentType.
  3. It answers a Decision, built with DecisionSchema.cases.<Case>.make. A Deny must carry a fix, which the agent reads after the reason.
  4. failMode: 'closed' refuses the call when run fails, dies or outlasts its timeout.

When it fires, the agent reads:

What the agent reads
An agent does not write .env files, which hold secrets.
Fix: Write the variable into .env.example and ask a person to set it.

Check it with harness-kit explain or a test: see Debugging a configuration and Testing a configuration.

Add context instead of refusing

Not every rule refuses. This one adds the team's release checklist beside any prompt that mentions a release; the agent reads it with the prompt:

harness.config.ts
import { DecisionSchema, defineConfig } from '@microeinhundert/harness-kit';
import { Duration, Effect, Predicate, String } from '@microeinhundert/harness-kit/effect';

/**
 * What the agent reads beside a prompt that asks for a release.
 */
const RELEASE_CHECKLIST = 'Releases follow docs/releasing.md: bump the version in a pull request of its own, and let a person tag the merged commit.';

/**
 * A Policy written in code that adds context instead of refusing: a prompt that mentions a release gets the team's
 * checklist beside it.
 */
export default defineConfig({
  policies: [
    {
      events: ['UserPromptSubmit'],
      failMode: 'open',
      id: 'release-checklist',
      run: (input) => {
        if (Predicate.isTagged(input, 'UserPromptSubmit') && String.includes('release')(String.toLowerCase(input.prompt))) {
          return Effect.succeed(
            DecisionSchema.cases.Context.make({
              text: RELEASE_CHECKLIST,
            }),
          );
        }

        return Effect.succeed(DecisionSchema.cases.Allow.make({}));
      },
      timeout: Duration.seconds(1),
    },
  ],
});

It fails open: a checklist that cannot be added is no reason to refuse the prompt.

Choose the Decision for the event

A Decision only has an effect on some events:

DecisionWhere it has an effect
DenyPreToolUse (refuses the call), UserPromptSubmit (refuses the prompt)
AskPreToolUse (Claude Code's permission prompt)
Contextevery event but Stop and SubagentStop
BlockStopStop, SubagentStop (keeps the agent working); PostToolUse (shown as an objection)
Alloweverywhere

How a hook call is decided shows how each is displayed.

If something goes wrong

  • The configuration fails to load with an infinite timeout. A Policy whose timeout never ends fails the configuration, since Claude Code would let the call through once its hook timed out. Give it a short timeout.
  • A run that fails or throws. Its fail mode answers, with the Policy and the problem named. You do not need to catch everything.
  • The hook times out before your Policy does. The hook's own time limit grows with the longest Policy timeout when you run harness-kit settings sync again.
  • A shell command check misses cases. Prefer a built-in Policy with options when one comes close: it reads shell commands the way the shell does, which a hand-written check rarely gets right.

Keep run fast: every PreToolUse Policy runs before every matching tool call.

On this page