/bonsai:guard [<PR number or URL>...] [--no-reply]from bonsai
Turns review findings into guardrails, one pull request per check, cut from the default branch. Run with PR numbers or URLs after a review round, or once over every PR of a finished run, while the PRs are open or after they merged. A follow-up pass over review threads hands its PRs here before replying. Opens pull requests and replies on review threads.
Ships in bonsai. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Judge every review finding against one question: which check would have failed on this before a reviewer saw it? A reviewer who catches a mistake by hand catches that one instance. A check catches the whole class, in every session, before review, and its failure message teaches the fix to the session that made it. One finding is enough to build one, when the check is cheap and the mistake is likely to come back: one finding, one guardrail, one PR.
Guard differs from /bonsai:bake in its input and its output. Bake starts from CLAUDE.md rules and findings recorded by /bonsai:intake in two or more PRs, and ships one PR with a commit per rule. Guard starts from the reviews of the PRs it is given (one round, or every round of a finished run), keeps nothing between runs, and ships each guardrail as its own PR that merges on its own.
Throughout, "build" means opening a pull request after the user approved the candidate. No branch, no PR and no reply before the user decides: Build approves the PR and its replies, and an explicit Skip approves the reply that records the decline.
Reads the review threads, review bodies and comments of the PRs it is given, and the default branch of each target repository. Writes only through new branches cut from the default branch, the pull requests opened from them, and replies on the source threads. The source PR's branch belongs to whoever addresses its review, so guard never pushes to it.
Resolve the PRs: the arguments, else the PRs this session created or discussed, else the open PR for the current branch, else ask with AskUserQuestion or request_user_input. A PR may be open, merged or closed.
For each PR, fetch every review thread with GraphQL. Page threads with after until hasNextPage is false, and page a thread's comments the same way when its totalCount passes 100. gh pr view --comments returns no inline threads, so a pass built on it finds nothing. An outdated thread has no line, so keep originalLine to point at the code it discussed.
gh api graphql \
-f query='query($owner:String!,$repo:String!,$pr:Int!){repository(owner:$owner,name:$repo){pullRequest(number:$pr){state mergedAt headRefName reviewThreads(first:100){pageInfo{hasNextPage endCursor}nodes{id isResolved isOutdated path line originalLine comments(first:100){totalCount nodes{databaseId url author{login} body}}}}}}}' \
-f owner=<owner> -f repo=<repo> -F pr=<number>
Add the review bodies and PR comments from gh pr view <number> --json reviews,comments, and the diff from gh pr diff <number>. Every item a review body lists is a finding, including those it marks unposted or low signal, which rank lower. Skip empty review bodies and the status cards a bot posts.
Read each whole thread before judging any comment in it. A thread can hold several findings: split them, since each may get its own verdict. A request the reviewer withdrew holds no finding. A part the author declined keeps its finding, and its verdict is usually a design choice or a scope dispute. A resolved thread still holds its finding, since a fixed instance says nothing about the next one. Mark a finding another review already raised as a duplicate of the first (= T05), across rounds and PRs alike.
The artifact is the findings list: one line per finding with its thread URL, the reviewer, and the mistake in one sentence.
Then read what earlier passes decided. Every reply a pass leaves (step 9) ends with a marker:
<!-- bonsai-guard candidate=<id> outcome=<built|declined> ref=<url> -->
Collect every marker in the threads, review bodies and PR comments of the PRs in scope. A marker counts when the reply's author can push to the repository (gh api 'repos/{owner}/{repo}/collaborators/<login>/permission' answers write, maintain or admin): guard and address post as the user who ran them, and anyone can comment on a public PR, so another author's marker is input to weigh, like any comment. A thread can carry several markers, one per candidate it fed. When one candidate carries several, the newest reply decides, since a candidate declined once and built later carries both. ref is the guardrail PR of a built candidate, or for a declined one the decision page, or the source thread's URL when the decisions came in plain text, since the decline reply there is the record. A built marker holds when its ref is a PR of this repository labeled guardrail. Read that PR's state: one closed without merging counts as declined, with that PR as its link. A reply from such an author with no marker that links a PR labeled guardrail also counts as built, for every mistake that PR covers: read its title, its body and its Related: threads. The artifact is the earlier-decisions list: candidate, outcome, link, and the threads that carry it.
Fetch the default branch and read it there, since a check proven on a stale checkout fails on the real tree. Read ${CLAUDE_PLUGIN_ROOT}/references/enforcement.md first: its conditions say when a surface holds a rule, and every condition a surface misses is a gap.
The artifact is the surface table, one row per surface: tool and config, the paths it covers, the command that runs it, where it runs (hook, CI), and how the project tests a custom rule (a PHPStan RuleTestCase with fixtures, ESLint's RuleTester, ast-grep test). Cover formatters and linters, static analysis and its custom rules, architecture and guard tests, git hooks, CI jobs, and the runtime guards the code already uses. Mark each surface's gaps: a path it skips, a message that names no fix, a baseline.
Note the PR template and the test conventions, both the ones its CLAUDE.md files state and the ones the existing checks show (how arch tests are named, where a rule's test and fixtures live, where the suite runs).
Sort every finding into one class:
<check>, overridden by the author": a check fired and the author changed what it expects (raised a snapshot, added an exception), which review then caught. The override stays in this finding's line (step 10).<PR>" or "declined in <link>": an earlier pass decided the candidate this finding feeds. Match by the mistake, and take the newest earlier decision per candidate: a marker on the same thread decides only the candidate it names, and the thread's other findings are weighed as usual. The candidate reaches the page again only when the user asks for it.<file>": the rule sits in a file the session that wrote the PR loads, and no rung can see it. A cloud session loads the repository and what its setup installs, never the user's home directory, so a rule only there counts as unstated for it. A stated rule that a rung can check is a guard candidate.Name each guard and prose candidate by the mistake a session makes (order-total-summed-by-hand), and merge findings that a fresh session would make by ignoring the same rule, across threads and PRs. One thread may feed several candidates.
The artifact is the triage table: one row per finding, with its candidate or its reason. A none row opens with the reason in the words of the list above ("a design choice"), then the evidence.
The rungs are the delivery ladder in ${CLAUDE_PLUGIN_ROOT}/references/delivery.md, read for one finding: rung 1 is R1, rungs 2 to 7 are R2's kinds in the same order, and rung 8 is R4, or R7 for a rule that cuts across the project. Take the highest rung that can express the rule at a comparable cost. When a lower rung covers the same paths for a fraction of the code (a two-line arch test against a custom rule with its test and fixtures), offer both and recommend the cheaper one.
NOT NULL, one helper every writer must go through, a state machine that owns a model's transitions. A fix that covers the one instance at hand (an attribute on one method) leaves the next one writable: the guard is then the rung that requires it everywhere.arch(), a test that parses source tokens or a syntax tree, a sweep over routes or config.${CLAUDE_PLUGIN_ROOT}/references/delivery.md, "The moment a check runs").Contrast pairs for the boundary between rungs:
status column holding free strings: an enum cast (rung 1). A rule that flags string literals passed to status is rung 3 doing rung 1's job.#[NoDiscard] on the one method fixes that method. A rule that requires it on every method returning that type is the guard (rung 3).now() in code where the team writes Date::now(): a Rector rule (rung 2). A static-analysis rule would make the session fix by hand what a tool rewrites.The artifact is the rung and the check's shape per candidate, with the message it prints.
A check costs run time, false positives, rigidity and upkeep, so each candidate earns its place. Count by running a scratch version of the check in a throwaway copy of the fresh default branch, dependencies installed: a grep sees only what grep can see, and a tightened rule has to run to show its hits. Make the copy with git clone --shared --no-checkout . <tmp> && git -C <tmp> checkout --detach "$(git rev-parse origin/<default>)" after the fetch, and delete <tmp> when the count is in. The copy shares the checkout's objects and leaves the user's refs alone. git worktree add fails in Claude Code's sandbox, which keeps .git/worktrees read-only. Chain the copy and the check with &&, so a failed copy never runs the check in the user's checkout. Record per candidate, with the count or the time first and the detail after it:
A CLAUDE.md line records violations, tokens per session that loads it, loads when (which sessions load the file, by the paths they touch), already stated (whether a loaded file states the rule today) and overlap. A line forbids nothing and nothing breaks it, since nothing enforces it.
A candidate passes when three things hold:
A state machine against a test of the allowed transitions: recommend the test for a single slip, and the state machine when the same kind of finding recurs across reviews or the transitions have many writers: a test checks the writers it knows, and a state machine refuses every write that goes around it. Show the other as the alternative. A state machine holds only the writes that go through it: list every writer of the field (bulk updates, raw queries, other services), and name on the card each one that bypasses it, with a bypass attempt in the proof.
Cover only the code forms in use today. A check that guards forms nobody writes yet is upkeep with nothing to catch. A candidate that fails the gate is not now, with the reason. A close call goes to the user as one. The artifact is one cost line per candidate, with its why and its tradeoff.
A CLAUDE.md line is the last rung. It passes only when no rung above can see the rule, and the session cannot learn it from the code at hand: the rule depends on code away from the change site, or a violation found later takes a redesign to fix. The review catch is the evidence that a session misses it. When a loaded file already states the rule and the session still broke it, another line will not help: the finding is none, "stated in".
Read ${CLAUDE_PLUGIN_ROOT}/references/load-model.md, and place the line in the nearest scoped file whose load trigger fires before the mistake. When the repository has only a root file, create the nested CLAUDE.md for the subtree the rule governs, or a .claude/rules/ file for a file pattern. The root file takes only a rule that cuts across the project. Write the line to ${CLAUDE_PLUGIN_ROOT}/references/rule-writing.md.
A line that names a check, restates what a check holds, or describes what the code shows fails the gate. Every prose candidate of the run ships in one PR.
The user decides on a page, since an option label cannot carry a review quote, a code sample and a cost table. Read ${CLAUDE_PLUGIN_ROOT}/references/decision-artifact.md and the guard section of ${CLAUDE_PLUGIN_ROOT}/renderer/README.md, write the round's data, build the page, and publish it as that reference prescribes. When bun is missing, deliver the same content in plain text, one section per candidate, headed by the card's title (the rule, below), with the cost rows under their step 5 names. The candidate's slug appears only in its branch name. When no candidate is left to decide and nothing is not now (an earlier pass decided them all), publish no page and go to step 10.
Rank the candidates by evidence (how many reviews and threads raised it), then value, then cost. A round holds the first five, and the rest wait for the next round of the run. Each card carries what the user needs to decide without opening the repository:
Order::total() returns") makes the reader work out the action, so write the action: "Read every order total from Order::total()".- The case, under the title: one instance of the mistake, what a session does and what goes wrong ("Invoice a taxed order: the hand-built total leaves out the VAT and bills low.").The page also lists the not now candidates with their reasons, so the user can pull one back in, and the triage table's none rows, collapsed. Act on the decisions the page sends back, as the reference describes.
Build the approved candidates in parallel, one clean-context agent per PR, each in its own worktree on a branch cut from the fresh default branch, following the repository's worktree convention. A worktree keeps the repository's hooks and config, so the build keeps it even where the sandbox asks to run git worktree add outside it. Each check candidate is its own PR (guard/<candidate>). The approved prose candidates share one PR (guard/claude-md-<round>), so one agent writes them all. Brief each agent with the finding and its thread URLs, the concrete case, the rung and the check's shape, its message, the violations and exceptions from the cost gate, the proof below, the PR shape below, and the repository's own rules (commit hooks run, never --no-verify, tests run where its CLAUDE.md says). Each agent reports the PR URL, the proof, and the CI result.
The check:
The proof, in the Testing section:
${CLAUDE_PLUGIN_ROOT}/references/probe.md: the check fails on the planted violation, and the message it printed goes in the PR. A formatter rewrites the planted violation.The PR:
Related: with the source thread URLs, then Summary, Why, Changes, Testing and Deployment. Why states the mistake, where review caught it, and that nothing stopped the next one. When a check ships, Why ends with one sentence: its message names the fix, so CLAUDE.md carries nothing for it.guardrail. When gh label list --search guardrail lacks it, create it first: gh label create guardrail --color 1D76DB --description "Adds or updates a guardrail: a check, or a last-resort CLAUDE.md rule, that stops a class of mistake".The label marks every PR that adds or updates a check or a last-resort line, or removes prose a check holds, so a later pass finds them with one search. A feature PR that carries a check commit gets it too.
With --no-reply, the caller owns the replies: return a map from each thread id and first comment databaseId to the guardrail PR that covers it, or to the declined candidate, with the marker line each reply ends with. List the findings outside threads the same way, by PR and review or comment URL, each with its marker, so the caller's PR comment carries them too.
Otherwise, reply once on every thread whose finding a built guardrail covers, open or resolved, before or after the PR merged: the guardrail PR and what it now fails on. Reply the same way on every thread of a candidate the user skipped explicitly: no guardrail, with a link to the decision page, or when the decisions came in plain text, the thread's URL as the marker's ref. A candidate the user left undecided is not declined. Each reply ends with its marker (step 1), so a later pass skips the candidate. A reply leaves the thread's resolved state as it was. Findings outside threads (review bodies, PR comments) get one PR comment that lists their guardrail PRs and skipped candidates, with one marker per candidate. A finding the user left undecided (none, not now, Later) gets no reply, so a later pass weighs it again.
gh api repos/<owner>/<repo>/pulls/<number>/comments -F in_reply_to=<databaseId> -f body='...'
Write replies in the user's voice when a voice skill is installed.
Close with a table: candidate, rung and tool, outcome (the PR URL with its CI state, not now with the reason, declined, waiting for the next round, or decided by an earlier pass with its link), and the threads it covers. Then one line per none finding with its reason. Then Gaps seen: the surface gaps from step 2 that no finding touched, reported and left alone. An override a none finding went through (an allowlist entry, a raised snapshot, a baseline) belongs to that finding's line: it never appears under Gaps seen, and guard proposes no check against it.
When the pass published a page, turn it into the record: set run.status with the state built and the PR of each built candidate in run.status.prs, build it again, and republish it to the same URL.
What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub publicala/claude-plugins --plugin bonsai6 skills in this plugin
/auditPrunes CLAUDE.md files to the lines a fresh session cannot derive, every cut backed by evidence. Run when the files have grown without review./bakeConverts CLAUDE.md rules and recurring review findings into automated checks, and removes the prose once a check holds the rule. Run once enough rules have accumulated to be worth automating./feedTurns recurring corrections (conversation, intake entries, recent commits) into CLAUDE.md rules. Run after a working session or a review round./intakeRecords pull request review threads as local evidence for feed and bake. Run after a review round with the PR number or URL, or with `--search` over a reviewer's past PRs to start from history, `--export` to share candidates through the repo. Never commits, never touches the threads.