Git and forge
Eight Policies that read the git and forge commands a Bash call would run, and refuse commits and pushes to shared branches, lost stashes, merges only people should make.
Keep an agent's git habits safe when it works fast and in many places at once: subagents, worktrees, background tasks. These Policies make sure that:
- work happens on a branch of its own, named after its issue;
- nothing is pushed to a shared branch;
- a stash is never lost;
- merging and approving stay with people.
Each reads the commands a Bash call would actually run (after cd, with git -C, behind wrappers, in sh -c) and asks the repository with git itself.
Needs git, and a forge for some
push-target and forge-guard ask the configured forge about pull requests. Without one, forge-guard reads gh commands itself, and push-target's pull request check passes.
API reference
The defaults and every option, with its type: branch-name, push-target, stash-safety,
commit-subject, merge-method, readonly-agents, branch-switch,
forge-guard.
Start from the git conventions preset
GIT_CONVENTIONS holds branch-name, push-target, stash-safety, commit-subject, readonly-agents and branch-switch with their defaults. Spread it into policies. merge-method and forge-guard depend on a team's branches and process, so add them yourself:
import { defineConfig, GIT_CONVENTIONS } from '@microeinhundert/harness-kit';
import { Array } from '@microeinhundert/harness-kit/effect';
/**
* The branches no agent commits or pushes to directly.
*/
const SHARED_BRANCHES = ['main', 'develop'];
/**
* The entries of the preset this configuration writes itself, with options of its own.
*/
const OWN_ENTRIES = ['branch-name', 'push-target'];
/**
* The git conventions preset, with two shared branches for the branch and push guards and the team's task types; a
* squash merge refused onto the release branches; and merging or approving pull requests left to people.
*/
export default defineConfig({
policies: [
...Array.filter(GIT_CONVENTIONS, (entry) => {
return !Array.contains(OWN_ENTRIES, entry.id);
}),
{
id: 'branch-name',
options: {
protectedBranches: SHARED_BRANCHES,
taskTypes: ['feat', 'fix', 'chore', 'docs'],
},
use: 'branch-name',
},
{
id: 'push-target',
options: {
protectedBranches: SHARED_BRANCHES,
},
use: 'push-target',
},
{
id: 'merge-method',
options: {
rules: [
{
forbid: ['squash'],
onto: ['release/*'],
},
],
},
use: 'merge-method',
},
{
id: 'forge-guard',
use: 'forge-guard',
},
],
});To change an entry, map over the preset or filter it out and write your own: a second entry with the same id is an error.
The Policies at a glance
| Policy | Refuses | Main options |
|---|---|---|
branch-name | a commit on a shared branch; a branch's first commit unless it is named <task type>/<issue key>-<slug> | protectedBranches, taskTypes, slugPattern, exempt, deployBranches |
push-target | a push that writes a shared branch (also --mirror, --all, a bare push from one); a push to a copy of a branch that already has an open pull request | protectedBranches, allowDryRun, pullRequestHead |
stash-safety | the stash commands that drop entries: pop, drop, clear, branch | refuse |
commit-subject | after the fact, a new commit whose subject repeats an earlier one of the branch or only restates its slug | rules, skipPrefixes |
merge-method | a git merge or gh pr merge whose method a rule forbids onto its target branch | rules: [{ onto, forbid }] |
readonly-agents | git add, commit, push and any forge write from agents that should only read | agentTypes (default Explore) |
branch-switch | git switch and a git checkout that moves HEAD, from subagents, which share the session's checkout | forbid: subagents (default), all, none |
forge-guard | a forge command that merges or approves a pull request, and one it cannot read | refuse, unreadable |
branch-name
Keeps commits off shared branches and branch names tied to an issue. protectedBranches defaults to the repository's default branch, the one origin/HEAD names (else main). Deploy branches of the repository's project are left alone.
A commit on a shared branch passes while a merge is in progress, or when the same line first opens a branch of its own (git switch -c fix/abc-12-typo && git commit …).
Blocked: this commit would land on main, a shared branch.
Fix: Move the work onto a branch of its own first: git switch -c <task type>/<issue key>-<slug>, then commit.Blocked: the branch name feat/x does not follow <task type>/<issue key>-<slug>: its slug x does not open with a lowercased issue key, such as abc-123.
Fix: Rename the branch while it has no commits of its own: git branch -m <task type>/<issue key>-<slug>, then commit as a separate command.Turn it off by leaving its entry out, or filtering it out of GIT_CONVENTIONS.
push-target
Refuses a push that writes a shared branch, and a push to a copy of a branch that already has an open pull request.
Blocked: this push writes to main, a shared branch.
Fix: Move the work onto a branch of its own: git switch -c <task type>/<issue key>-<slug> --no-track origin/main, then push that branch. If you genuinely need to move a shared branch, run the push yourself, outside the agent.Turn it off by leaving its entry out, or filtering it out of GIT_CONVENTIONS.
stash-safety
Refuses the stash commands that drop entries, because a worktree shares its stash stack with the main checkout and an entry can be lost.
Blocked: git stash pop applies the newest entry and drops it. This main checkout shares its stack with every worktree of the repository, which holds no entries.
Fix: Look first: git stash list; use an entry without dropping it: git stash apply stash@{0}. If it must go, run git stash pop yourself, outside the agent.Turn it off by leaving its entry out, or filtering it out of GIT_CONVENTIONS.
commit-subject
Objects to a commit subject that repeats an earlier one on the branch or only restates its slug. It cannot refuse a commit that already happened, so it shows the agent an objection after the call:
The commit subject “Add docs” needs another wording: it repeats the subject of an earlier commit on this branch.
Fix: Say what this commit does: git commit --amend -m "<issue key> - <what this commit does>".Turn it off by leaving its entry out, or filtering it out of GIT_CONVENTIONS.
merge-method
Refuses a git merge or gh pr merge whose method a rule forbids onto its target branch. It is not in the preset: you write the rules (rules: [{ onto, forbid }]). With the configuration above, a squash merge onto release/1.0 is refused:
Blocked: this would put a squashed commit on release/1.0.
Fix: Merge with a merge commit instead: git merge --no-ff feat/abc-1-docs.Turn it off by removing its entry.
branch-switch
Refuses git switch and a git checkout that moves HEAD from subagents, which share the session's checkout. forbid is subagents (the default), all or none.
Blocked: git switch from the worker agent switches the branch of the checkout the session shares.
Fix: Work in a worktree of your own instead: use EnterWorktree, or have the agent spawned with isolation: "worktree".Turn it off by setting forbid to none, leaving its entry out, or filtering it out of GIT_CONVENTIONS.
readonly-agents
Refuses git add, commit, push and any forge write from the agent types in agentTypes (default Explore), so an agent that should only read cannot change anything. The agent hands the write back instead:
Blocked: git commit from the Explore agent, which only reads.
Fix: Do not route around this: report the block in your own output and hand the write back to whoever spawned you.Turn it off by leaving its entry out, or filtering it out of GIT_CONVENTIONS.
forge-guard
Refuses a forge command that merges or approves a pull request, and one it cannot read, so merging and approving stay with people.
Blocked by the forge guard: this command would merge a pull request, which only a person does here.
Fix: Leave the merge or approval to a person: say in your answer that the pull request is ready for them.Turn it off by removing its entry. Its refuse and unreadable options change what it refuses.
If a Policy cannot ask git
A Policy that cannot ask git (git missing, too slow, killed) fails by its fail mode: closed for all but commit-subject. The call is then refused, and the agent reads why. An entry's failMode changes this.
Related
- Stop checks and hand-off: pull requests and issue status at the end of a turn.
- GitHub: the forge these Policies ask.
- Troubleshooting: the common messages and how to clear them.
Commands
command-policy screens every shell command line against a command table, from ready-made presets for kubectl and package managers to an allow-list for one subagent.
Paths and new files
protected-paths, generated-paths, allowed-paths and new-file keep files where they belong and out of the agent's reach.