Skip to Content
harness-kit

Writing an external Policy or adapter

Decide hook calls, or stand in for an issue tracker, with an executable in any language that speaks harness-kit's protocol.

You'll: build two executables as shell scripts, an external Policy that decides hook calls and an adapter that stands in for an issue tracker. Any language works the same way.

Before you start

  • A working harness.config.ts (see the quick start).
  • Executables that read one JSON object on standard input and write one on standard output, as the protocol describes.
  • Permission to make the script executable (chmod +x), and anything it needs, such as a token, in its environment.

Refuse forced pushes with an external Policy

The kit runs the executable as <executable> decide for every call the Policy applies to, with the call on standard input. This one refuses a forced push and allows everything else:

adapters/no-force-push
#!/bin/sh
# An external Policy over harness-kit's protocol: `no-force-push <operation>`, one JSON object on standard input and
# one on standard output. It refuses a forced push and allows every other call.
case $1 in
  capabilities)
    echo '{"protocol": 1, "output": {"operations": ["decide"]}}'
    ;;
  decide)
    case $(cat) in
      *'git push --force'* | *'git push -f'*)
        echo '{"protocol": 1, "output": {"decision": {"_tag": "Deny", "reason": "A forced push rewrites history others build on.", "fix": "Push without --force, or ask a person to."}}}'
        ;;
      *)
        echo '{"protocol": 1, "output": {"decision": {"_tag": "Allow"}}}'
        ;;
    esac
    ;;
  *)
    echo "no-force-push has no operation $1" >&2
    exit 2
    ;;
esac
  1. It answers capabilities with the operations it supports. harness-kit config check calls it, so a broken script shows up before a hook needs it.
  2. It answers decide with {"decision": …}, a Decision tagged by _tag: Allow, Deny with reason and fix, Ask with reason, Context with text, or BlockStop with reason and fix.
  3. Anything else exits with 2, a setup problem, with the reason on standard error.

Name the script under adapters, with a path relative to the configuration, and run it with the built-in external Policy for Bash calls before they run:

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

/**
 * An external Policy: the executable `adapters/no-force-push` decides every Bash call over the kit's protocol.
 */
export default defineConfig({
  adapters: {
    'no-force-push': {
      executable: './adapters/no-force-push',
      timeoutMillis: 5000,
    },
  },
  policies: [
    {
      id: 'no-force-push',
      options: {
        adapter: 'no-force-push',
        events: ['PreToolUse'],
        tools: ['Bash'],
      },
      use: 'external',
    },
  ],
});

Make the script executable, then check it:

chmod +x adapters/no-force-push
npx harness-kit config check
npx harness-kit explain < payload.json

When the agent tries git push --force origin main, it reads:

What the agent reads
A forced push rewrites history others build on.
Fix: Push without --force, or ask a person to.

Answer for a tracker with an adapter

An adapter stands in for one of the kit's contracts. This tracker answers capabilities and get_issue from a fixed list; a real one would ask your tracker's API, with a token read from its environment:

adapters/tracker
#!/bin/sh
# A tracker adapter over harness-kit's protocol: `tracker <operation>`, one JSON object on standard input and one on
# standard output. It answers from a fixed list of issues; a real one would ask the team's tracker.
case $1 in
  capabilities)
    echo '{"protocol": 1, "output": {"operations": ["capabilities", "get_issue"], "key_pattern": "OPS-[0-9]+", "issue_url_template": "https://tracker.example.com/browse/{key}", "cli_write_patterns": [], "create_patterns": [], "mcp_write_tools": []}}'
    ;;
  get_issue)
    case $(cat) in
      *'"OPS-1"'*)
        echo '{"protocol": 1, "output": {"key": "OPS-1", "title": "Rotate the staging certificates", "type": {"name": "Task", "level": 0}, "status": {"name": "In Progress", "category": "in_progress"}, "assignee": {"account_id": "u-7", "name": "Sam Lee"}, "parent": null, "description_md": "The certificates expire on Friday.", "links": []}}'
        ;;
      *)
        echo '{"protocol": 1, "error": {"kind": "not_found", "message": "no such issue"}}'
        exit 1
        ;;
    esac
    ;;
  *)
    echo '{"protocol": 1, "error": {"kind": "unsupported", "message": "tracker has no operation '"$1"'"}}'
    exit 1
    ;;
esac
  • capabilities tells the kit the key pattern of its issues, the URL of each issue, and which command lines and MCP tools write to it (none here).
  • get_issue answers with the issue's fields, or exits with 1 and the error kind not_found, which the kit tells apart from every other failure.

Name it tracker, which makes it the tracker every Policy and harness-kit tracker use:

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

/**
 * An issue tracker of the team's own, reached through an executable of the protocol beside the configuration.
 */
export default defineConfig({
  adapters: {
    tracker: {
      executable: './adapters/tracker',
      timeoutMillis: 5000,
    },
  },
  policies: [],
});

Then read an issue through it:

npx harness-kit tracker show OPS-1
# OPS-1 Rotate the staging certificates

| Field    | Value                                    |
| -------- | ---------------------------------------- |
| URL      | https://tracker.example.com/browse/OPS-1 |
| Type     | Task                                     |
| Status   | In Progress (in_progress)                |
| Assignee | Sam Lee                                  |

## Description

The certificates expire on Friday.

If something goes wrong

  • The script crashes, prints something that is not a Decision, or runs too long. The external Policy fails closed: it refuses the call and names the problem. The time limit is the entry's timeoutMillis (10 seconds by default, or the adapter's own when that is shorter).
  • The check should never block. Open it with the entry's failMode: 'open'.
  • config check reports the adapter as broken. Run the script by hand with the operation as its argument and a request on standard input, and read standard error.
  • An operation fails as unsupported. The adapter does not list it under capabilities. The kit never calls an unlisted operation: it fails it as unsupported, or falls back where it can (a count by paging through search). Start with the operations the Policies you use need, and add more as you go.

harness-kit protocol schema prints the exact shape of every request and answer.

On this page