Coordinate agents

Agents and hooks

How a coding agent learns bay, and what the hooks of each agent product add.

Coding agents use bay through its commands and the JSON result each one prints. The bay skill teaches an agent those commands. With Claude Code or Codex, bay's hooks also refuse a stale write before it happens. Without hooks, a stale change still cannot merge.

What each agent gets

Every agent product that runs shell commands gets the same merge promises. It works through bay's commands, their JSON results and the bay skill. An agent product with bay's hooks also gets early refusals, notices and live sessions in the fleet map.

Agentbay's hooksCommands, JSON results and skillEarly refusals, notices and fleet sessions
Claude CodeBuiltYesYes
CodexBuiltYesEarly refusals and fleet sessions. Its notices after session start go to stderr.
CursorNot yetYesNo
Grok BuildClaude Code's, which it runs in a repository that has themYesEarly refusals and locks for its shell commands and write edits. Its other tools go unjudged.
AntigravityNot yetYesNo
Gemini CLINot yetYesNo

Grok Build

Grok Build runs the hooks in a project's .claude/settings.json, so it calls bay's Claude Code hooks. bay knows its payload by Grok's own workspaceRoot and hookEventName fields. It reads Grok's run_terminal_command as Claude Code's shell, and Grok's write as an edit. So the deny list, stale writes, held paths and locks apply to those two tools. Grok drops asyncRewake, so there bay hook talk-wait returns at once and waits for no talk. bay judges no other Grok tool, and no recording yet shows how Grok acts when bay's hook fails.

What needs bay's hooks

  • Refusing a stale write, a path another live session holds, or a locked path, before the write happens.
  • Refusing dangerous Git commands before they run.
  • Session notices: refusals, failed checks, reds it owns, early conflicts, merges on its paths, talks, or no runner.
  • Waking an idle Claude Code session with the outcome of a merge it left queued.
  • A notice when main changed an instruction file.
  • Draft checkpoints when a session ends or sits idle.
  • Session rows in bay status --all, which come from each machine's live summary.

The bay skill

The bay skill tells the agent which command to run, and to pass --json and read the result. Teach your agent bay says where it installs, what it holds and how bay doctor checks it.

Tell any agent about bay

Paste this into the repository's AGENTS.md or CLAUDE.md. It works for any agent product that can run a shell command. With Claude Code, the bay skill from bay install gives the same rules.

text
## Merging changes with bay

This repository merges changes with bay (https://getbay.io/docs).

- Once per machine: curl -fsSL https://getbay.io/install.sh | sh. bay's hook entries come from main.
- Start each task with: bay start <intent> --paths <paths>. Work in the view it prints.
  <intent> says what the work is, in a few words, such as "fix the login page", or a card id.
- When the work is done, commit it in the view, with an Open: line for anything left undone.
  Then run: bay merge. It returns at once. Never push main or merge by hand.
  Give a change up with: bay abandon.
- To hear the outcome, run bay merge <change> --wait=3600 as a background task.
  Never read bay status again and again: each read costs a turn.
- Pass --json, and read stdout as one bay.result/1 JSON document. Act on its decision and reason.
  Unknown is not success.
- Read state with: bay status, or bay status --all for every session. Undo a merged change with: bay undo <change>.
- See what changed with: bay status --since <cursor>. Ask another agent for one thing with: bay talk.
- Fix a merged change with: bay start <intent> --fixes <change>.
- Never push, merge into or move main, or push a promotion target. Never run git reset --hard, git clean -f,
  git branch -D, git checkout . or git restore . Never force-push, push --all or --mirror, or fetch refs/bay/* elsewhere.
- Never edit or commit in another agent's view.

Turn on agent hooks

Name your agent product in the [agent] table of .bay/config.toml, and merge it into main. Fetch main first: bay install reads [agent] from main as this clone last fetched it. Then bay install adds hook entries that call bay hook <event>: .claude/settings.json for Claude Code, .codex/hooks.json for Codex. Merge that file into main once: every clone and view then runs the hooks, with no bay install of its own.

Read the hooks from bay, below, gives each product's version floor. bay doctor checks it. Without agent hooks, the hooks row of bay doctor reads off. Security and privacy says what bay still guards.

toml
[agent]
product = "claude"   # or "codex"

When bay's hook fails

bay's hook refuses a call only on purpose. When the hook fails or hangs, the call goes on. Main stays the guard: bay judges every change again at merge. The hook judges a tool call without starting Git, in tens of milliseconds. An entry an earlier bay wrote with onFailure: "block" gets a note in bay doctor. Run bay install again.

What Codex hooks lack

With product = "codex", bay install writes bay's entries to .codex/hooks.json. Commit it to main. Codex runs those entries only in a trusted project, and skips an untrusted project's hooks without a message. Before a write or a shell command, the hook's exit 2 denies the tool call, as with Claude Code. As with Claude Code, Codex runs the tool when bay's hook crashes, hangs or fails. Main stays the guard there too: bay judges every change again at merge.

Read the hooks from bay

bay help --json declares each agent product's hooks under detail.agents, from the tables bay's own code uses. Each entry names the product, its settings file and its version floor. Each event names the product's event, the bay hook moment it calls, and whether it blocks. on_failure says what the product does when the hook fails. cannot_see lists what the hook never judges, such as a write by a shell command. The table below comes from that list, so it says what bay's code writes.

sh
bay help --json | jq '.detail.agents[] | {product, floor, cannot_see}'
ProductSettings fileFloorEvents that can blockOn a machine without bay
Claude Code (claude).claude/settings.json2.1.302 or newerPreToolUse (on failure: continue), Stop (on failure: allow)allow: each entry first checks that bay is present
Codex (codex).codex/hooks.json0.153.2 or newerPreToolUse (on failure: allow)The hook fails, and the tool runs

Hook moments

Each hook entry runs bay hook <moment>, and the moment names what happened. One moment can serve two events of a product, such as session.wait. The table comes from the same list in bay help --json.

MomentClaude Code eventCodex event
session.startSessionStartSessionStart
turn.startUserPromptSubmitUserPromptSubmit
tool.beforePreToolUsePreToolUse
tool.askPreToolUseNone
step.endPostToolBatchPostToolUse
file.changeFileChangedNone
session.waitNotification, TeammateIdleNone
turn.endStopStop
subagent.startSubagentStartSubagentStart
subagent.endSubagentStopSubagentStop
talk-waitStopNone
session.endSessionEndSessionEnd
compact.beforePreCompactNone

What the hooks do

  • At session start, the hook prints the active change's state and the live changes that overlap its declared paths.
  • At session start, it also names each instruction file that main changed after the tree's commit.
  • Before a write, the hook refuses a write that bay already knows is stale.
  • Before a write or a shell command, it refuses a path that another live session in the same tree holds.
  • Before a shell command, the hook refuses a fixed list of dangerous Git command shapes.
  • Before an edit or a git commit, it refuses one in another agent's view, with VIEW_NOT_YOURS.
  • After each step, the hook records the files that the step wrote.

Lock files that never merge

Git cannot combine two edits of an image, a model or another binary file. When two sessions edit one, the work of one session goes to waste at merge. Mark such paths lockable in .gitattributes, the attribute Git LFS uses, and merge it into main. Then one session at a time edits each such file, across every machine. bay's hooks take a lock as an agent edits, bay save takes it for a person, and a merge needs it.

sh
# .gitattributes on main
art/** lockable
*.psd lockable

Take a lock

Before an edit tool writes a path main marks lockable, bay's hook takes the path's lock. The lock is a ref under refs/bay/locks/ on the remote, so every clone sees it. Of two machines that edit one path at once, exactly one takes the lock. bay refuses the other edit with PATH_HELD. The refusal names the holder's agent and session, and the lock's age. The holder's own later edits go through.

Only on the newest copy

bay grants a lock only on the newest copy of its file. When main changed the file after your work started, bay refuses the edit or the save with INPUT_CHANGED. Bring in the newest main first, with bay start <change> in a view, then edit the file.

A merge needs the lock

A change to a lockable file merges only while that change holds the file's lock. Otherwise bay refuses it with LOCK_NOT_HELD when it builds the candidate, before any check runs. So an edit that went around the lock, such as a shell write, never merges through bay. bay save in the main checkout takes the lock of each lockable file it saves, as an edit does. A released lock keeps its holder, so the change still merges after its turn, until another session takes it.

When a lock ends

A lock ends when its agent stops at the end of a turn, or its session ends. A stop that bay refuses, to ask the agent to save its edits, keeps the lock. Each step of the session renews its locks once they are half the takeover window old. A lock that no step renews within the window goes to the next session that edits its path. The takeover window is [runner] takeover_s in .bay/config.toml: twice lease_s, so 120 seconds by default. A lock that names no session, or one bay cannot read, goes to the next edit too.

When the remote does not answer

Without an answer from the remote, bay cannot tell whether another session holds the lock. So bay waits at most 10 seconds for the remote. Then it refuses an edit of a lockable path with REMOTE_UNREACHABLE, and lets other edits through. Edit another path, or try again once the remote answers.

What a lock cannot do

  • Without bay's hooks, an agent's edit meets no lock early, and bay refuses its merge with LOCK_NOT_HELD.
  • Claude Code and Codex refuse the edit. When bay's hook fails or hangs, both run it.
  • Only an edit tool that names its file takes a lock: Edit or Write in Claude Code, apply_patch in Codex.
  • A shell command's write takes no lock and meets none, so a script can still change a locked file.
  • A push to main outside bay checks no lock.

What the hooks tell you and save

  • When a merge of yours is refused, or its check fails or is unknown, the hook tells you once.
  • The hooks fetch talks at most every 30 seconds per clone, and name each new one once.
  • With Claude Code, an idle session wakes once with each outcome of a bay merge it left queued.
  • The outcome is merged, failed, or refused with a step for the agent.
  • A wait whose agent ended wakes no one.
  • While such a merge waits, the idle wait checks at least once a minute, also with talk off.
  • A session that reads bay status again within 15 minutes hears once that one background wait replaces it.

With [view] refresh_on_conflict = true, the idle wait also refreshes the view after a MERGE_CONFLICT refusal. It runs bay start &lt;change&gt; only when the session is idle and the view is the actor's. The view moves onto main with the conflict markers, and a second line names the paths. The refresh stops before the view moves when a write reached the view after its checkpoint. It stops too when the session started a turn, and the line then says why. bay before 0.7.78 refuses this key: update every machine that runs bay on the repository first.

  • Before a session stops with unsaved edits in its view, the hook asks it once to save them.
  • In a view, the hook saves a draft checkpoint when the session ends.
  • It also saves one at the first hook event [hooks] checkpoint_idle_s after the last checkpoint, 600 seconds by default.
  • So a session killed without a stop loses at most one interval of edits.

Blocked shell commands

The hook refuses these command shapes, also behind env, xargs, sudo, command and exec. It reads the script of bash -c, sh -c, zsh -c, dash -c and ksh -c, two shells deep. It refuses a deeper script that mentions Git. Git named inside quotes or a here-document is data. bay judges a command substitution as the commands it runs.

It matches shapes and never runs the shell, so another spelling of the same command can get past it. A normal push of a refs/bay/* ref stays allowed. A fetch of one bay ref, or into refs/bay/, stays allowed too.

text
git reset --hard
git clean -f
git clean -fd
git branch -D
git checkout .
git restore .
git push -f, or a cluster of short flags that holds f, such as -fu or -vf
git push --force, git push --force-with-lease, git push --force-if-includes
git push +<ref>
git push --all, git push --mirror
git push of refs/heads/<main>
git push of a [[target]] ref, such as refs/heads/staging
git push of anything but refs/bay/*, when bay cannot read main
git fetch that copies every refs/bay/* ref into another namespace, such as refs/bay/*:refs/remotes/x/*

When the hook cannot judge

bay install gives the tool.before hook a 30-second timeout. bay answers within about 15 seconds. When it cannot judge in time, it lets the call go on. bay denies a tool.before call whose payload it cannot read, with HOOK_INPUT_INVALID. Then bay doctor checks the hook entries, and bay update installs a newer bay.

The Claude Code hook command exits 0 on a machine without bay, so a committed settings file blocks nothing there. Where bay is installed, its deny still blocks. The deny also holds when Claude Code runs with bypassPermissions.

Name each agent

bay names the agent from BAY_ACTOR, else from your user name, else agent. actor_source in results and session rows says which: env, user-default or default. Without BAY_ACTOR, every session on one account gets one name, and a resumed session keeps the name it had. To require a name, set require_actor = true in the [agent] table of .bay/policy.toml. Then bay start refuses ACTOR_UNNAMED until BAY_ACTOR is set. With a key registry that signs everything, set BAY_ACTOR to the agent's enrolled name.

toml
# .bay/policy.toml
[agent]
require_actor = true

Name the person an agent works for

Set BAY_REQUESTER to name the person the agent works for. The change keeps the name from the start that gave it to its actor, and an adoption sets it again. Its merge names that person in a Requested-by trailer, a key that [trailers] can rename.

sh
BAY_REQUESTER="Dana Smith" bay start "fix the login page" --paths src/api

Tell agents on one account apart

Agents on one OS account can all read one file agent key, so its signature cannot tell them apart. Set require_actor_proof = true in the [agent] table of .bay/policy.toml to refuse that. Then bay merge refuses ACTOR_UNPROVEN while several agents share an agent key on this machine. bay also refuses a machine where it cannot judge the key, such as Windows with a file key.

The fix is a hardware agent key, or one OS account for each agent. It is off by default. The key-custody row of bay doctor shows it, and whether bay refuses this machine.

toml
# .bay/policy.toml
[agent]
require_actor_proof = true

Limit each agent's live changes

Set max_live_changes in the [agent] table of .bay/policy.toml to cap the live changes of one agent. Then bay start refuses WIP_LIMIT while the agent already holds that many. A change counts until it merges or ends, such as by bay abandon. A lane change counts too. detail.live_changes names each one. Merge or abandon one, then start again.

bay counts the change records this clone holds, and reads the limit from main. Adopting your own change adds none. Without the key, bay sets no limit.

toml
# .bay/policy.toml
[agent]
max_live_changes = 3

What a session records

Hook events add rows to the session's work record on this machine. A refused tool call shows as blocked(<code>), with an Agent Hooks deny verdict. Codes include SHELL_DENIED, PATH_HELD and HOOK_INPUT_INVALID. Each row names the session's permission_mode and actor_source. bay status --all shows both for every session.

Why a session waits

A session that needs a person shows why in bay status --all, under waiting_for. It names a question and the tool that asked it, or the product's permission or idle prompt. The question and the prompt's message stay in the session's own record on its machine. So an agent asks for a person's decision with its product's question tool, such as Claude Code's AskUserQuestion. bay only records the question. The product's dialog opens as it would without bay.

Hooks are an extra entry point

bay's own merges work the same without hooks, so a stale write still cannot merge. Without hooks, an agent's own git push can reach main around bay, unless main is protected. Security and privacy says what bay guards, and when. A session with hooks off shows as untracked in bay status --all. bay doctor is red with HOOKS_MISSING when .claude/settings.local.json or your user settings sets disableAllHooks or allowManagedHooksOnly. Another tool's hooks for bay's events run beside bay's, so doctor lists them but never turns red for them.

Not built yet

  • Hooks for Cursor, Antigravity and Gemini CLI, and for Grok Build's tools beyond its shell and write.
  • bay doctor naming each product's hook support and the interfaces it lacks.
  • Worktree ownership through the agent's own worktree events.