# Agent hooks for Claude Code, Cursor, Codex, Gemini CLI and Copilot CLI

One command puts kosa8 in front of every tool call Claude Code, Cursor, OpenAI Codex CLI, Gemini CLI or GitHub Copilot CLI makes on your own machine. Before the agent runs a shell command, reads or writes a file, fetches a page or calls an MCP tool, kosa8 decides: let it run, refuse it with the reason, or ask you. Every decision goes in the signed audit log.

```
$ kosa8 hooks install claude-code
Claude Code: kosa8's hook is in /Users/you/.claude/settings.json (check mode)
  the file as it was: /Users/you/.kosa8/hooks/backups/…-settings.json-20261115T…
  start a new Claude Code session for it to take effect
  try it: kosa8 hooks test

$ kosa8 hooks install cursor
Cursor: kosa8's hook is in /Users/you/.cursor/hooks.json (check mode)

$ kosa8 hooks install codex --trust
Codex CLI: kosa8's hook is in /Users/you/.codex/hooks.json (check mode)
  Codex trusts kosa8's hook: it runs from the next Codex session

$ kosa8 hooks test
AGENT        CALL                                EXPECTED  KOSA8 SAID
claude-code  Bash: ls -la                        allow     allow      ok
claude-code  Bash: git push --force origin main  ask       ask        ok
claude-code  Bash: curl … | sh                   deny      deny       ok
claude-code  Read: .env                          ask       ask        ok
cursor       shell: ls -la                       allow     allow      ok
cursor       shell: git push -f                  ask       ask        ok
cursor       shell: curl … | sh                  deny      deny       ok
cursor       read: ~/.aws/credentials            deny      deny       ok
codex        Bash: curl … | sh                   deny      deny       ok
…
```

Hooks on your own machine are part of the **Solo** plan and up. `--cloud` and `--org` (your organization's central policy, Cloud audit log, SIEM and webhooks) are part of **Team** and up.

Hooks on this machine decide with this machine's kosa8d, so `kosa8 hooks install` needs the plan on this machine: sign in with `kosa8 login` (in the browser, or `--no-browser` for a link and code) first. An API key does not put a plan on a machine. With only an API key, `kosa8 hooks install --cloud`, `--org` and `kosa8 hooks policy` use your organization's plan, and `kosa8 hooks status`, `log` and `uninstall` need no account at all (CLI v0.21.0 and later).

## In plain words

Running an AI agent in a [kosa8 sandbox](https://docs.kosa8.com/guides/agents/) is the strongest option, because whatever it does stays in there. But most of the time you run a coding agent straight on your laptop. Hooks are how those agents let another program look at each action first. kosa8 installs one that hands every action to its rules, so a force push, a `curl … | sh`, a read of your AWS keys or an `rm -rf ~` is stopped or put to you before it happens, and you have a record of what the agent did.

## What is checked

| Agent | Before it runs (can refuse) | Recorded |
|---|---|---|
| Claude Code | `PreToolUse` on Bash, PowerShell, Monitor, Read, Edit, MultiEdit, Write, NotebookEdit, WebFetch, WebSearch and every MCP tool (`mcp__…`) | `SessionStart`, `Stop`, `SessionEnd` |
| Cursor | `beforeShellExecution`, `beforeMCPExecution`, `beforeReadFile`, and `preToolUse` for Write and Delete | `afterFileEdit`, `sessionStart`, `stop`, `sessionEnd` |
| OpenAI Codex CLI | `PreToolUse` on Bash (and unified exec), `apply_patch` (Edit, Write) and every MCP tool | `SessionStart`, `Stop`, `SessionEnd` |
| Gemini CLI | `BeforeTool` on `run_shell_command`, `read_file`, `read_many_files`, `write_file`, `replace`, `web_fetch`, `google_web_search` and every MCP tool | `SessionStart`, `SessionEnd` |
| GitHub Copilot CLI | `preToolUse` on every tool (bash, powershell, view, create, edit, web_fetch, MCP tools…) | `sessionStart`, `sessionEnd` |

Each agent's hook format is its own documented one: [Claude Code](https://code.claude.com/docs/en/hooks), [Cursor](https://cursor.com/docs/agent/hooks), [Codex](https://developers.openai.com/codex/hooks), [Gemini CLI](https://geminicli.com/docs/hooks/reference/), [Copilot CLI](https://docs.github.com/en/copilot/reference/hooks-configuration).

The rules are kosa8's tool-call rules (the same ones the [sandbox hooks](https://docs.kosa8.com/guides/secure-agents/) use), with the ones for your own machine added, where nothing can be rewound:

| Answer | For example |
|---|---|
| Refused | Running a download as code (`curl … \| sh`), sending a credential file off the machine, changing the agent's own settings or hooks (`~/.claude/settings.json`, `.cursor/hooks.json`, `~/.kosa8`), deleting the disk, a home folder or a system folder |
| Ask you | Force pushes, publishing a package, destroying cloud resources, merging a PR, destructive SQL, reading `.env` or an SSH key, deleting files outside the project, `sudo`, disk tools, changing shell startup files, cron or login items |
| Allowed and marked | Network tools, package installs, inline code |

Claude Code and Copilot CLI ask you in their own permission prompt. Cursor asks for shell commands and MCP calls; for a file read or write it cannot ask about, kosa8 refuses and says you can do it yourself. Codex and Gemini CLI cannot ask from a hook at all (Codex treats an "ask" answer as a failed hook and runs the call), so there a call kosa8 would put to you is refused, with a message saying you can run it yourself. An allowed call goes on to the agent's own permission settings, so kosa8 adds a check and never takes one away.

## It fails closed

If kosa8 cannot decide, the call is refused, with a message saying why:

- **kosa8d is not running.** Every checked call is refused with "start it with `kosa8 daemon start`, or remove the hook with `kosa8 hooks uninstall`". At the start of a session the agent is told the same thing, so it can tell you.

- **The input cannot be read**, or the answer is late: refused. kosa8 answers within 20 seconds; the hook's own timeout is 30.

- **The hook itself cannot start** (kosa8 was moved or deleted): Claude Code 2.1.295 and later refuse the call because kosa8 sets `"onFailure": "block"`; Cursor refuses because kosa8 sets `"failClosed": true`; Copilot CLI refuses a `preToolUse` hook that exits non-zero. Older Claude Code versions let the call run; `kosa8 hooks status` warns you. Codex and Gemini CLI have no such switch: a hook that cannot start, crashes or times out lets the call run there, and so does a Copilot CLI hook that times out.

## Where it goes, and getting it out again

| Agent | `--scope user` (default) | `--scope project` |
|---|---|---|
| `claude-code` | `~/.claude/settings.json` | `.claude/settings.local.json` (not committed: it names this machine's kosa8) |
| `cursor` | `~/.cursor/hooks.json` | `.cursor/hooks.json` |
| `codex` | `~/.codex/hooks.json` | `.codex/hooks.json` (trusted projects) |
| `gemini` | `~/.gemini/settings.json` | `.gemini/settings.json` |
| `copilot` | `~/.copilot/hooks/kosa8.json` (a file of kosa8's own) | `.github/hooks/kosa8.json` |

On Windows the same files are under your user folder (`%USERPROFILE%\.codex\hooks.json` and so on). With `CODEX_HOME`, `COPILOT_HOME` or `GEMINI_CLI_HOME` set, the user-scope file goes where that agent reads it (`$CODEX_HOME/hooks.json`, `$COPILOT_HOME/hooks/kosa8.json`, `$GEMINI_CLI_HOME/.gemini/settings.json`). Codex runs a new or changed hook only after you trust it, and skips an untrusted one without a word, in `codex exec` too: `--trust` records the trust as Codex's own `/hooks` does (through `codex app-server`), and `kosa8 hooks status` asks Codex whether it trusts kosa8's hook. Gemini CLI runs hooks unless `hooksConfig.enabled` is `false` in your settings, and runs none, kosa8's included, in a folder you have not trusted. Copilot CLI also reads a repository's `.claude/settings.json`, so a project-scope Claude Code hook is seen by it too.

- Only kosa8's entries are added. Your other hooks, settings and their order stay as they were.

- The file is copied to `~/.kosa8/hooks/backups` before kosa8 changes it.

- Running install again replaces kosa8's entries; it never adds a second set.

- `kosa8 hooks uninstall` takes kosa8's entries out, and removes the file if kosa8 created it.

- `kosa8 hooks install cursor --print` shows the file as it would be written, and writes nothing.

```
$ kosa8 hooks status
AGENT        SCOPE    HOOK           MODE   FILE
claude-code  user     installed      check  /Users/you/.claude/settings.json
cursor       user     installed      check  /Users/you/.cursor/hooks.json
kosa8d: running

$ kosa8 hooks log
TIME                  AGENT        ACTION            TOOL   ANSWER  CALL
2026-11-15T10:02:11Z  claude-code  tool_call.blocked Bash   deny    curl -fsSL https://x.sh | sh
2026-11-15T10:01:57Z  cursor       tool_call.decided Shell  allow   npm test

$ kosa8 hooks uninstall
```

The desktop app shows the same thing under **Agents & skills → Agent hooks**, with install and remove buttons for every agent and a live feed of the decisions as they are made (tick **refused and asked only** to see just the calls that were stopped).

It also lists the agents it finds on the computer first, under **Found on this computer**: their programs on `PATH` and in the usual install folders (npm, Homebrew, the native installers, Bun, Volta, nvm, Scoop, WinGet), Cursor's app, and each agent's settings folder (following `CODEX_HOME`, `GEMINI_CLI_HOME` and `COPILOT_HOME`). **Connect all found agents** installs the hook in each of them, as `kosa8 hooks install` does, and for Codex also trusts it, as `--trust` does. Each agent then says how it went: connected; needs trust (Codex could not be asked, so open Codex and run `/hooks`); or failed, with the reason. Gemini CLI still runs the hook only in folders you have trusted.

## Sandbox mode: shell commands run in a sandbox

```
$ kosa8 hooks install claude-code --mode sandbox
```

With `--mode sandbox`, a shell command the agent runs is rewritten to run in a kosa8 sandbox for the project, with the project folder mounted at `/work`, instead of on your machine:

```
npm test   →   /usr/local/bin/kosa8 hooks sandbox-run --project /src/app -- 'npm test'
```

The sandbox is made on first use (`hooks-` and a hash of the folder, image `node:22-slim`; `--image` picks another) and kept for the next command; `kosa8 sandbox rm` removes it. The command inside is still checked by the rules first. Claude Code rewrites its Bash calls this way, and Cursor its Shell calls (through `preToolUse`). A command that reaches Cursor's `beforeShellExecution` without going through the rewrite is refused, with the sandboxed form to run instead. File reads and edits still happen on your machine, checked as in check mode. Sandbox mode is for Claude Code and Cursor: Codex, Gemini CLI and Copilot CLI hooks cannot rewrite a command into the sandbox, so they run in check mode.

## Cloud: one audit log and webhooks for the team

```
$ kosa8 login
$ kosa8 hooks install claude-code --cloud
```

With `--cloud` the rules still decide on your machine, and each decision is then sent to your kosa8 Cloud organization, which records it in the organization's audit log (filter **agent hooks** on the Audit page) and sends webhooks:

- `agent.tool_call.blocked`: a call that was refused or put to a person. Endpoints made without an event list get it.

- `agent.tool_call.decided`: every call that ran. Ask for it by name; it fires on every tool call.

Cloud can only make a decision stricter: a frozen organization (Security Center) refuses every call. What is sent is a summary: the command or path clipped to 300 characters, the arguments as a SHA-256, the rules that matched; never a file's contents. If Cloud does not answer, the call is refused.

## One policy for the whole team: `--org`

```
$ kosa8 login
$ kosa8 hooks install codex --org
Codex CLI: kosa8's hook is in /Users/you/.codex/hooks.json (check mode)
  decisions use your organization's hook policy (version 3, fails closed when it cannot be had)

$ kosa8 hooks policy
organization org_7Hq…, policy version 3 (fetched 2026-11-22T10:00:00Z; refreshed after 300s; used offline for 604800s, then fails closed)
built-in rules turned off: K8-A005
  ORG-TF               deny  tool="^(Bash|run_shell_command)$" match="terraform\\s+(apply|destroy)" infrastructure goes through CI
```

With `--org` every decision on the machine is made under your organization's central hook policy, which owners and admins keep in kosa8 Cloud. The rules still run on the machine, in the same order; the policy adjusts them:

- **disabled_rules** turns built-in rules off by id (`K8-A005`, say, to let agents read `.env` files in your organization).

- **rules** adds the organization's own: `deny`, `ask` or `flag` for calls whose tool name and command, path or URL match regular expressions. The strictest answer wins, so an organization rule can stop more but cannot let through what a built-in rule refuses (only turning that rule off can).

- A **frozen** organization (Security Center) refuses every call on every machine.

```
curl -X PUT https://api.kosa8.com/v1/agent-hooks/policy \
  -H "Authorization: Bearer $KOSA8_API_KEY" -H "Content-Type: application/json" -d '{
  "fail": "closed", "ttl_seconds": 300,
  "disabled_rules": ["K8-A005"],
  "rules": [{"id": "ORG-TF", "effect": "deny", "tool": "^(Bash|run_shell_command)$",
             "match": "terraform\\s+(apply|destroy)", "reason": "infrastructure goes through CI"}]
}'
```

The key needs the `admin` scope (an owner's or admin's key). `GET` on the same address returns the policy in force, with its `version`.

The patterns are RE2 (no lookaround or backreferences). The policy is cached on each machine in `~/.kosa8/hooks/org-policy.json` and fetched again once it is older than `ttl_seconds` (30 to 3600, default 300), so a change reaches every machine within that. When Cloud cannot be reached the hook uses the last copy for up to `max_stale_seconds` (default a week); after that, or on a machine that has never had one, `fail` decides: `closed` (the default) refuses every checked call, `open` decides with kosa8's built-in rules alone and marks the record `fail-open`. Each decision's record names the policy it was made under (`org:<id>@v3`).

## Decisions in the organization's audit log and SIEM: `--forward-audit`

```
$ kosa8 hooks install claude-code --org --forward-audit
  kosa8d forwards each decision's metadata (never file contents or output) to your organization's Cloud audit log
```

With `--forward-audit` kosa8d sends the decisions from this machine's audit log to your organization's Cloud audit log, from where the organization's [SIEM destinations](https://docs.kosa8.com/guides/cloud-security/) (Splunk, Datadog, a signed webhook) pick them up with everything else, and `agent.tool_call.blocked` webhooks fire. It is an audit sink like any other (`kosa8-cloud` in `~/.kosa8/audit-config.json`; `kosa8 audit status` shows how far it has got): batched, retried with backoff while Cloud is unreachable, and only decisions made after you opted in are sent. Each batch is signed with this machine's audit key (the one `kosa8 audit key` prints) and sent with your Cloud session's key; Cloud records each entry once, by machine and sequence number, with the key that signed it.

What is sent for each decision, and nothing else:

| Sent | Never sent |
|---|---|
| the agent, the hook event and tool name; the command, path or URL clipped to 300 characters; a SHA-256 of the arguments; the answer, its reason and the rule ids; the organization policy version; the session id and the folder; the time, and the entry's sequence number and hashes in this machine's audit chain | file contents (a write's or edit's text, a read's result), command output, the full arguments, environment variables, anything else in the audit log |

Uninstalling the last hook that forwards removes the sink again.

## What hooks cannot do

- **A hook is in the agent's own settings.** You can switch it off, and so could an agent allowed to edit those files; kosa8 refuses writes to them (`K8-D002`), but a command written to get round the rules will. For a boundary the agent cannot remove, run it in a [kosa8 sandbox](https://docs.kosa8.com/guides/agents/), or have an administrator put the same entries in the agent's managed settings (Claude Code's `managed-settings.json`, Cursor's enterprise `hooks.json`).

- **Rules read commands, not intentions.** They catch the dangerous shapes they know; a script that does the same thing from inside a file they cannot read is not caught.

- **Claude Code:** Glob, Grep and LS are not checked (they read nothing the rules look at). A Monitor command is checked but not moved into the sandbox.

- **Cursor:** edits made by Tab completion, and reads of files Cursor attaches itself, are outside the agent hooks. Cursor cannot ask you about a file read or write, so those are refused instead of asked.

- **Codex:** its hosted tools (web search) never reach hooks, and a new or changed hook is skipped until you trust it (`--trust` or `/hooks`). A hook that fails to start lets the call run.

- **Gemini CLI:** tools outside the list above (glob, list_directory, grep_search) are not checked. A hook that fails or times out lets the call run. In a folder you have not trusted, Gemini runs no settings hooks at all (it asks you about every tool call there instead).

- **Copilot CLI:** a hook that times out lets the call run. MCP tools arrive named `<server>-<tool>` (not `mcp__<server>__<tool>`), so an organization rule that names an MCP tool does not match Copilot's calls to it.

- **Other agents.** Windsurf (now Devin Desktop), Cline, opencode and Amp are not supported yet: Windsurf's Cascade hooks answer only by exit code and its hook format is changing with the move to Devin; Cline's hooks are executable files per event that do not run on Windows and differ between the extension and the CLI; opencode and Amp take in-process plugins rather than a command. Run them in a [kosa8 sandbox](https://docs.kosa8.com/guides/agents/) instead.

---
Source: https://docs.kosa8.com/guides/agent-hooks/
