# Concepts

> bay gives each name one meaning, and these pages use each name only in that meaning.

Source: https://getbay.io/docs/concepts (HTML).

bay uses one name for each idea. A change is one intended outcome, and its draft lives in a view. bay builds a candidate from the draft and main, and a runner merges it when its checks pass. Evidence records what each check saw.

## Main

Main is the one branch bay merges into: the remote's default branch, such as `main` or `dev`. It is one linear branch that only grows, and each merge adds one child of the checked base. bay never forces a push to main. Agents create no branches: each change is bay's own record, edited in a view bay makes. Branches after main, such as `staging` and `production`, are promotion targets. Work that must never reach main goes in a lane.

> To serve another branch, make it the default on the host.
> Every clone follows, with no step of its own.

## Change

A change is a stable id for one intended outcome. A new implementation keeps the same id. A change id is eight lower-case base32 characters that bay generates. An intent says what the work is, in a few words, or gives a card id. One intent claims one change, even when several starts race for it. Only the same words, in the same case and spacing, name the same intent.

> bay keeps each change's record under `refs/bay/changes/<change>`, and reading it from the remote never moves local work back.
> When the remote's record moved apart from this clone's, bay keeps the local head under `refs/bay/kept/<change>/`.
> These kept refs stay in that clone, are never pushed, and are yours to inspect or delete.
> A runner that holds a change beats its lease on `refs/bay/lease/<change>`, replaced in place, never on the change's history.

## View

A view is the sparse checkout of the declared paths for one change. It also holds the agent product's hook settings, such as `.claude/settings.json`, when main holds that file. An absent file in a view is not a deletion. Each view is a Git worktree of your clone, so views on one clone share its objects. After `bay start`, the view works offline. A machine's `core.autocrlf` setting never changes the line endings of a view's files.

> `--paths .` or `--paths /` declares the whole tree: a full checkout that takes new files anywhere.
> `bay start <change> --paths` declares more paths: the view then holds them too, and the change keeps its place.

### A view is its actor's

A view belongs to the actor that made it: `BAY_ACTOR`, else your user name. In another actor's view, `bay merge` and `bay save` refuse with `VIEW_NOT_YOURS`, which names that actor. `bay start <change>` refuses another actor's change the same way, with or without a view here. With agent hooks on, the hook refuses an edit or a `git commit` there too. Work in a view of your own, or take the change over with `bay start <change> --adopt`. Then you become its actor: `detail.adopted` is `true`, and `detail.adopted_from` names the actor before.

```sh
bay start c7k2m4qa --adopt   # take the change and its view over
```

> Take a change over only when its actor gave it up or is gone.
> Ask it first, such as with `bay talk`.
> An abandoned, superseded or `no_effect` change is yours at once.
> A change its actor holds is yours after one takeover window with no move or heartbeat.
> Sessions with no `BAY_ACTOR` on one account are one actor.

### When a view goes

- Each `bay start` releases this machine's views of changes that merged, were abandoned, or ended as `no_effect`.
- It releases such a view at once: its work is on main, on the change, or under `refs/bay/view-kept/<change>`.
- It keeps a view with edits, untracked files, a lock, or an unmerged commit that bay did not place.
- When `bay start` must make a view again, files no draft holds, ignored ones too, are moved aside, never deleted.
- It also keeps the view you run `bay start` in, and the view of a live session on this machine.
- It keeps a folder that is no valid worktree of this clone, and a view that `git worktree remove` refuses.

```sh
# A retention window after its change ended, a view kept for its unsaved work is saved, then freed.
git checkout refs/bay/view-kept/<change> -- <path>   # bring one file of a freed view back
ls .git/bay/view-kept/<change>/                       # its loose ignored files, such as .env, never committed
```

> When a view's first checkout stopped before git wrote its index, bay judges the view against its commit.
> Only files that differ from that commit, or that it lacks, count as unsaved work.
> Files that the view's ignore rules match are not work, and they go with the view.
> An ended change's kept view loses the directories its ignore rules match, but loose files such as `.env` stay.
> The result names each released change under `detail.released_views`.
> A sandbox lane change's view goes sooner, after its lane's `window_s`, one day by default.

### Views in a blob-less clone

A clone from `git clone --filter=blob:none --no-checkout` holds commits and trees, but no file contents. Views on it share one store, and each view fetches only the files it needs, after every `bay start`. With Git 2.45 or newer, Git never fetches a missing object on its own inside bay's reads. bay fetches what its own reads need, such as `.bay/config.toml`, by name from the clone's promisor remote. When it cannot fetch `.bay/config.toml`, the command fails and names that file. Use Git 2.48 or newer for such a clone.

> A view's files are its declared paths plus the agent hook settings file.
> Git still fetches on purpose in a view's checkout, `merge-tree`, the undo's `read-tree` and a check's copy of the candidate.
> Below Git 2.48, `bay doctor` marks the `shared-cache` row `refused`, but no command stops you.
> A `.bay/policy.toml` that bay cannot fetch fails the merge with a fault that names it, never `POLICY_MISSING`.
> Then `bay status` shows `detail.red` and `detail.quarantines` as objects with `unread` and the reason, never as clear.
> When bay cannot fetch a `live.json`, the fleet map shows `readable: false`, and `bay start` fails rather than adopt.

### Warm a view's history

Set `[view] warm_history = true` in `.bay/config.toml` to fetch the history of a view's declared paths. Then `bay start` fetches every past version of those paths with `git backfill`, also when it picks up a change. So `git log -p` and `git blame` on those paths need no fetch, also offline. `detail.history` says how much: its `state`, the objects missing before and after, and the bytes fetched. Below Git 2.54, bay skips the warm-up, and `detail.history` names that floor. A warm-up that fails says `failed` there, and the start still succeeds.

> It is off by default, so a view costs only what its task reads.
> A change with no declared paths skips it, because its history is the whole repository's.
> `state` is `warmed`, `partial`, `skipped` or `failed`, and one warm-up runs for at most 10 minutes.

## Candidate

The draft is the work in the view. bay builds the candidate from the draft and the current main. The candidate is one exact result: a Git tree and the commit that carries it. bay checks the candidate and merges that same commit.

## Check

A check is a command that tests a candidate and returns JSON. Each check reports `pass`, `product_failure`, `infra_failure` or `unknown`. Unknown is never a pass. With `retry_on_failure`, a failure followed by a pass is `flaky`, never a plain pass. A runner asks an unknown check again once main moves, on up to three candidates. `.bay/policy.toml` on main lists the required checks.

## Evidence

Evidence is one record of what one check, review or runtime observation saw on one exact result. With a registry that signs everything, a producer key signs each check record, and a reviewer key each review. bay keeps evidence in Git notes under `refs/notes/bay/evidence/<producer>`. Evidence counts only for the exact tree it names, and some checks also name the exact commit or base tree. Results say `trust: signed` with a key registry, `trust: approvals` when it signs only approvals, and `trust: unsigned` without one. A producer outside bay, such as a reviewer, writes its records with `bay evidence record`.

> A clone with no `bay.producerName` takes its own name, `local-<id>`, the first time it writes unsigned evidence.
> It keeps that name in its local Git config, so two machines never write one evidence ref.
> Two live runners never share one: bay refuses a runner started under another's name with `PRODUCER_SHARED`.

## Runner

A runner is the process that finishes a merge after the agent leaves. It claims the operation, runs the checks, and merges the candidate.

On a main branch from `bay host` with a registry, only a runner's `runner` key can move main. This holds in the default signing mode. Under `signing = "approvals"`, the host keeps main append-only, as with no registry. On other hosts, use the host's branch protection. `bay runner run` is one runner, and a CI system can be another.

## Merge

To merge is to put one checked candidate on main. `bay merge` freezes the draft, builds the candidate, submits it, and returns at once. A runner then checks the candidate and merges it. The push to main is the only commit point. A change has merged only when its commit is on main. When the work is already on main, bay merges nothing, and the result's state is `no_effect`.

> `bay merge` pushes in seconds a change no pre-merge check covers, or any under `unchecked = true` except one to `.bay/`.
> On a red main's paths, it first runs that red check.
> Offline, on a fault, or with no runner key where the repository signs everything, it stays submitted for a runner.
> Each merge commit carries trailers: `Change-Id`, `Revision-Id`, `Request-Id`, `Candidate-Id`, `Registry-Id` and `Bay-Epoch`.
> It also carries the intent and the actor, under keys that `[trailers]` can rename, and `Bay-Session`.
> `Requested-by:`, `Fixes:`, `Reverts:`, `Supersedes:`, `Bay-After:` and `Bay-Batch:` appear when they apply.

### Lists every change adds to

Some files are lists that every change adds a line to, such as cards or a registry. Two open changes that each add a line would conflict. Mark such a file `merge=union` in main's `.gitattributes`, and bay keeps both sides' lines. bay reads the attributes from main, never from the change, so a change cannot mark its own files.

```sh
# .gitattributes on main
cards.md merge=union
```

### What main's commit says

When the agent commits in its view before `bay merge`, main's commit keeps that message. The agent's first line titles main's commit, followed by `: change <id>`. With no message, the intent titles it. The agent's other words come next, then bay's trailers, then the agent's own trailers. An `Open:` trailer names something left undone, or a step that needs a person. bay never judges the words, and an agent's message never sets one of bay's own trailers.

Main's commit keeps the draft's author, unless bay wrote the draft. Then `[runner] author` sets it, else bay's own.

```sh
$ git log -1 --format=%B main
Fix the login page: keep the email after a failed sign-in: change lruimn6q

The form now refills the email field and shows the error under it.

Change-Id: lruimn6q
Revision-Id: 35ea68ce96d5045cfeb221b86331785d4917d0e4
Request-Id: fcd0a42056007b9b00717178bf13ca6efaa70a21
Candidate-Id: lruimn6q.1.1
Registry-Id: none
Bay-Epoch: 1
Intent: fix the login page@1
Actor: ada
Bay-Session: s-afiebih5
Open: ask design whether the error text should be red
```

## Refusal

Most refusals are results with `decision` set to `blocked`. `UPDATE_UNVERIFIED` is `failed`. It carries one fixed reason code, such as `CHECK_FAILED`. `allowed` lists what you can still do. `retry_when` says what must happen before a retry can succeed, such as `after_fix` or `after_wait`. A retry with no change in the cause gets the same refusal and does no work.

## Keys

Keys are optional. With a registry, six classes of key sign in bay: `agent`, `runner`, `producer`, `reviewer`, `approver` and `registry`. An agent key signs a request to merge, and a runner's `runner` key signs the commit that merges into main. A producer key signs check evidence, a reviewer key a review, and an approver key an approval or waiver. A registry key adds and removes keys. One key belongs to exactly one class.

## The program, the skill and hook entries

bay puts three things in place, and each has its own home. The program is `bay` itself, at `~/.local/bin/bay` on each machine. The skill is bay's guide for an agent: one copy in each Claude config folder, under `skills/bay/`. Hook entries are lines in a project's settings file, such as `.claude/settings.json`, that run `bay hook`. That file is on main, so every clone gets the entries once their change merges. Roll bay out to many machines puts all three in place, in order.
