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 fromeffectdirectly. - 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:
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:
eventsandtoolsdecide which calls reachrun: here onlyPreToolUsecalls ofWriteandEdit.rungets the call as aHookInput, told apart by its_tag, the event. A tool call carriestoolNameandtoolInput, whosefilePathandcommandare read for you asOptions; the whole input is intoolInput.raw. Every call carriessessionId,cwdand, inside a subagent,agentType.- It answers a Decision, built with
DecisionSchema.cases.<Case>.make. ADenymust carry afix, which the agent reads after the reason. failMode: 'closed'refuses the call whenrunfails, dies or outlasts itstimeout.
When it fires, 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:
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:
| Decision | Where it has an effect |
|---|---|
Deny | PreToolUse (refuses the call), UserPromptSubmit (refuses the prompt) |
Ask | PreToolUse (Claude Code's permission prompt) |
Context | every event but Stop and SubagentStop |
BlockStop | Stop, SubagentStop (keeps the agent working); PostToolUse (shown as an objection) |
Allow | everywhere |
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
runthat 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 syncagain. - 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.