Branch & worktree runbook

Status: procedure reference. The rules live in AGENTS.md §4 and stay always-on; this file holds the how-to, the recovery steps, and the reasoning behind them. Relocated from §4 by BI-0020D511 Phase 1 — no rule was dropped, only its procedure moved. If you are looking for what you are required to do, read §4; if you are looking for how, you are in the right place.

Merge readiness

Verify mechanically. Run pnpm pr:health [<n>] before claiming a PR is green or mergeable — do not eyeball a subset of checks.

The checker asserts every check is terminal and passing (no pending, and not just an assumed “required 4” — Module Size Guard / CodeQL / UX-Fit Gate block too), mergeable != CONFLICTING, and zero unresolved review threads. That last one matters because gh pr checks does not surface review threads at all, and a single bot comment blocks merge — conversation resolution is a required condition. gh pr merge --admin does not bypass required checks or conversation resolution.

Full reference: docs/testing/pr-health.md.

Merge mechanics

Agents land with pnpm land; its publisher requests gh pr merge <n> --squash --auto for the exact current OPEN PR and head commit. Do not chain push, PR creation and queue requests by hand. main enforces a merge queue (the main-merge-queue ruleset) that brings the PR up to date and squash-merges once required checks pass. Consequences worth knowing:

Resume publication after a successful push (BI-31D31DEF).

If pnpm land already pushed a qualified commit but stopped during PR creation or queue observation, preserve that commit and its evidence. Do not repeat push, merge forward, re-run the gate or replay semantic review merely to resume the remaining publication steps. Historical MERGED or CLOSED PRs on the same branch do not represent its current publication.

The supported bounded resume is:

pnpm land -- --resume-publication --expected-head <full-qualified-sha> \
  --worktree <unchanged-governed-worktree> --title "fix: ..." --body-file <reviewed-body>

Run this from a qualified checkout containing the repaired helper. --worktree selects the unchanged candidate; the helper’s source version and the candidate’s source version are separate identities. Do not copy helper code into the candidate to obtain the command. Every child process reads the candidate’s working directory. Resume requires compile readiness, readable Git state, a clean named topic branch, exact local and remote HEAD, and a real current branch-bound canonical PASS. It checks current PR-body requirements and validates the existing semantic receipt under the helper’s current publication policy without recording or dispatching another review. A refusal remains a refusal.

The publisher selects at most one current OPEN PR for the repository, branch and base, verifies its exact head, and uses --match-head-commit on the protected queue request. It observes actual GraphQL queue membership separately from an auto-merge request: a queue entry can coexist with a null autoMergeRequest. Queued, merged and requested states are reported separately. Failed, partial or ambiguous reads remain unconfirmed; no second enable request is sent. Protected checks and merge-queue verification remain mandatory. Use pnpm pr:health <n> to read subsequent progress.

Private/public change segregation is content-based

Any measure of an install’s private delta over the community — the /ops/self-upgrade “Changes kept on your system” ledger, or any future variant — is computed from a three-dot tree diff <remote>/<branch>...<installBranch>: the content the install added since the merge-base. Never a commit-range (A..B), never git cherry / --cherry-pick.

The reason is the squash-merge above. Every merged PR gets a fresh SHA on main while the install branch keeps the original unsquashed commits, so a commit- or patch-range counts already-public work as private. Measured once at 4,707 commits, ~2,300 of them carrying public (#NNNN) PR numbers, while the honest content diff was 0 files. Content is the only squash-proof signal.

Reference: apps/web/lib/self-upgrade/local-changes-ledger.ts (BI-75C4A412).

Read-only work needs no worktree

Review, diagnosis, research and answering a question about the source change nothing, so they get no working copy. Create the room on the read-only shape (create_workroom with workShape: "source-inquiry@1.0.0") and read through read_source_at_version, search_source_at_version, list_source_directory and read_project_file, or git show origin/main:<path> against the root clone. A source-inquiry room cannot reach worktree provisioning: plan_workroom_worktree, claim_workroom_scope and adopt_worktree refuse it with worktree_provision_not_admitted, and so does the WorktreeCreate hook while the session holds such a room. When the inquiry turns out to need a change, claim the item with a delivery shape; that claim is the route to a worktree (design, BI-D1FB8C4D).

Creating a worktree

Create worktrees through the governed path, which admits and binds the Workroom before the worktree exists:

bash scripts/new-dev-worktree.sh <topic> [<prefix>]          # manual path

Claude Code’s auto-created worktrees go through the WorktreeCreate hook, which runs the same library (scripts/lib/worktree-provision.mjs). Both refuse, and create nothing, when the platform refuses or cannot be reached. Raw git worktree add bypasses the admission; a guard for it is BI-25A45D5C.

Break-glass. When the platform is down and an operator authorizes recovery (AGENTS.md §1), set DPF_WORKTREE_BREAK_GLASS="<operator: reason>" for that one command. The worktree is created unbound with a dpf-workroom-pending.json marker in its git dir; bind it with adopt_worktree once the platform is back. Break-glass never overrides a refusal: a refusal is an authorization answer, not an outage.

⟦runtime: install-local paths — the D:/ form is the Windows install shape⟧ The dedicated sibling base (<root>-worktrees/<topic>) is canonical for all three host CLI surfaces (Claude Code, Codex, Grok) per the 2026-06-05 unified-delivery-surfaces decision #1 — not Claude Code’s default .claude/worktrees/<random> nesting, and not the older D:/DPF-<topic> alongside-the-clone form.

Never share a working tree across sessions: it causes index/HEAD collisions and cross-thread file sweeps.

Seeding a new worktree

Claude Code’s dpf connector is the dpf-platform plugin, so every worktree has it without a per-worktree file; no project .mcp.json is copied (a second dpf entry would load beside the plugin’s, BI-5201141C). .vscode/mcp.json is gitignored local connection configuration, so git worktree add does not carry it across.

From inside the new worktree, run scripts/dpf-bootstrap-agent-toolchain.ps1 (Windows) or bash scripts/dpf-bootstrap-agent-toolchain.sh (macOS / Linux). It copies .vscode/mcp.json from the root clone when present, converges Claude Code + Codex CLI plugin state, seeds kernel-tier memory, runs read-only MCP + smoke probes, and prints a readiness banner; the readiness record lands in ~/.dpf/agent-toolchain-state.json, never in install-state.json, so running it while a self-upgrade is draining is safe (BI-95DF1BFC). Re-running on a converged worktree is a no-op. Supported interactive connections default to OAuth, including Codex on HTTP loopback. An explicit process DPF_MCP_URL wins; otherwise setup uses the saved https user preference, then an HTTPS PUBLIC_URL. With none selected, the skill-pack updater’s canonical https://localhost/api/mcp/v1 applies (BI-772023BC). Only https endpoints persist for future sessions; Codex and Grok keep any loopback-http choice in their own configs (BI-8FD7C0C9). When the Grok command is installed, the same bootstrap creates one client_credentials client for the installing operator and refreshes ~/.grok/dpf-mcp-token before it expires. Grok reads that file; no token is pasted, and revoking the client in Admin stops later refreshes. Setup never mints a PAT because OAuth authorization is pending; the banner distinguishes written configuration from a verified connection. For explicit legacy credentials and client compatibility requirements, follow the MCP authorization runbook.

The legacy scripts/seed-worktree-mcp.{ps1,sh} and scripts/ensure-dpf-skill-pack.{ps1,sh} scripts now shim into the new bootstrap; both names continue to work for one release cycle. ⟦runtime: expiry UNANCHORED — names no release or date, so it cannot expire on its own; anchor it or drop the legacy names⟧

Restart Claude Code in the worktree afterwards so /mcp picks up the dpf connector. See docs/operations/install.md for the readiness states and what each one means.

On Windows, the landing controller resolves pnpm through the shared host-command invocation helper. It executes the package manager entrypoint through Node or its native executable, preserving literal arguments without shell interpolation. Resolver and spawn errors remain failures, as do nonzero child exits; the same controller still runs every delivery gate.

Routine client permissions

Bootstrap also converges routine Codex, Claude and Grok permissions for the explicitly named source worktree through the signed updater’s shared --permissions-only writer. Use that worktree as the client’s actual workspace; the installed runtime remains an MCP administration context. Existing restrictive profiles, deny/ask rules and managed requirements keep their authority. A saved configuration does not widen an already-running managed chat.

Routine independent review reuses the DEV task’s existing scope, budget, grants and consent. No new approval is needed solely for a delivery phase. Source tests, protected publication and actual authority refusals still apply. See routine development client permissions for backups, native diagnostics, parser compatibility and effective-session proof.

Verification readiness classification

The MCP seed scripts write .dpf-worktree-readiness.json with compile-ready or source-only.

The managed probe executes the repository’s exact packageManager pin even when an agent host supplies a newer pnpm. It also runs pnpm ignored-builds; any unclassified install script keeps the tree source-only and emits a stable dependencyPolicyReviewKey keyed by base SHA, package/version, and reason. Use that key as the backlog intake origin so the canonical ingester increments one occurrence instead of filing one review per worktree.

A successful managed install can retain qualified dependency evidence in this worktree’s Git metadata when pnpm omits its build record after a no-op install. Ordinary probes still check current artifacts, dependency resolution, workspace links, staleness and unclassified scripts. Changed install inputs or metadata, failed installs and missing or invalid evidence prevent reuse. This evidence is not a build/test result and grants no build-script permission. Each probe rediscovers workspace manifests, so editing, adding or removing a package invalidates prior evidence. Custom hooks, patches and unsupported workspace formats keep the normal managed-install path.

Compose project isolation

docker-compose.yml defaults to the root project dpf; linked worktrees must override it with an ignored .env value such as COMPOSE_PROJECT_NAME=dpf-<topic>. The worktree MCP seed scripts write this automatically.

Do not run docker compose up, docker compose down, or profile/harness Compose commands from a worktree until that worktree has a unique project name. CI and integration harnesses must use node scripts/dpf-compose.mjs with a unique COMPOSE_PROJECT_NAME. A down --volumes against the root dpf project requires an intentional recovery/reinstall context and DPF_ALLOW_DESTRUCTIVE_COMPOSE=1.

Freshness — a stale worktree base is a landmine

main moves fast. A topic worktree created days ago and never refreshed sits on an old base. That base carries files that have since moved on main (a since-migrated baseline, for example), which surface as modify/delete conflicts only at PR time — and it makes the checkout’s merge-base with origin/main disappear on a shallow clone.

Before serious implementation: git fetch origin main, confirm your base is current, rebase if not. The SessionStart worktree-freshness hook (scripts/hooks/worktree-freshness.{sh,ps1}) prints a warning automatically when it detects a stale base; silence with DPF_SKIP_WORKTREE_FRESHNESS=1.

A stale base also serves a stale RULEBOOK, which is the quieter half (BI-9A46E89C). CLAUDE.md imports AGENTS.md from the worktree, so an agent on an old base is governed by a superseded copy of the operating contract — and cannot detect that from the inside, because the stale copy does not know it is stale. The consequence is not a merge conflict but a false negative: every “does this already exist?” check runs against the old tree, so a rule, principle, helper or guard that exists on main reads as absent, and the agent rebuilds or re-proposes what is already canonical. Measured 2026-09-16 on a worktree 835 commits behind: AGENTS.md had gained “A gate that could not run is not a verdict” (2026-09-05) and 24 kernel-principle files had moved, none visible; that session rebuilt a helper that existed (git-changed-files.mjs) and proposed a principle that existed (report-only-the-verdict-you-reached). The hook therefore reports instruction-plane drift independently of the commit-count threshold — one commit can add a commandment — and names the false-negative consequence explicitly, because the commit count alone reads as “your code may conflict” and gets worked past. Review what moved with git diff $(git merge-base HEAD origin/main) origin/main -- AGENTS.md docs/founder-kernel/wiki/principles.

The durable fix is to create worktrees from a freshly-fetched origin/main in the first place.

Root-clone freshness — the shared root must stay on current main

This is a different concern from worktree-base freshness above: peer worktrees junction @dpf/* workspace packages into the shared root clone’s source tree, so if the root clone’s own checkout falls behind origin/main, pregate-preflight aborts with “Stale root clone detected (BI-A900EA3F)” for every worktree until the root is fast-forwarded — even worktrees whose own branch is perfectly current.

The SessionStart root-clone-freshness hook (packages/dpf-skill-pack/hooks/root-clone-freshness.mjs) fast-forwards the root clone to origin/main automatically at session start — fast-forward only, and only when the root is on main. It refuses (and prints an advisory) when the root is off-main or detached, because work may be stranded there and a forced move would corrupt it. Uncommitted files do not block it unless the incoming commits change one of them. A dirty file nothing upstream touches is carried across unchanged; an overlapping one is named in the refusal (BI-F676CC23 — one bootstrap-written .mcp.json used to freeze the root for days). Run it by hand any time with node scripts/root-clone-refresh.mjs [--json]; silence the hook with DPF_SKIP_ROOT_CLONE_REFRESH=1. This runs the same safe git merge --ff-only origin/main that the stale-root-clone detector already recommends, so it never conflicts with the root-clone delete/mutation guard.

Reaping safety — a live session’s worktree is never reaped

The governed worktree janitor (scripts/worktree-janitor.mjs) removes eligible checkouts. Provider SessionEnd never reaps a checkout. The event names the session cwd and in T3 it can fire between turns (BI-F52EF21B).

The SessionEnd reaper (scripts/hooks/session-reaper.sh) also terminates the ending session’s own node sidecars and releases the leases that session owns. It is scoped on purpose: a process counts as the session’s only when its command line names the session’s worktree outside node_modules (shared binaries prove nothing about ownership) and names no other worktree; and a lease is released only when its ownerSessionId is the ending session. A session started in the root clone therefore never reaps a sibling worktree’s local-CI gate run, which is how three gate runs died by SIGTERM on 2026-09-06 (BI-B0122A22).

Branches left without a worktree

The janitor deletes a branch only together with the worktree it removes, so a branch whose tree went some other way stays forever. scripts/branch-reaper.mjs (BI-88D75BA3) inventories those branches. Local deletion remains incomplete.

Rebasing on a shallow clone — never a bare git rebase origin/main

⟦runtime: precondition — shallow checkouts only; on a full clone the phantom replay does not occur⟧

This checkout is shallow (git rev-parse --is-shallow-repository → true), so a stale branch has no visible merge-base with origin/main. A bare git rebase origin/main then tries to replay thousands of phantom commits, starting from the repo’s Initial commit, and unwinds your working tree.

Housekeeping

If SessionStart prints DPF HOUSEKEEPING DUE, run it. The janitor-throttle hook (scripts/hooks/janitor-throttle.{sh,ps1}) fires at most once per 24h across all worktrees. Comply; don’t skip silently. Details are in the hook’s own header comment.

When a worktree is removed

The merge event, daily fleet sweep and T3 host settlement all use the same janitor. The common classifier refuses active sessions, active Workroom claims, pinned or dirty checkouts, active leases, open PRs and named branches without verifiable work beyond their base. A Workroom claim counts while the room is non-terminal and the server does not mark it reapable. A missing or shortened liveness verdict keeps the claim (BI-CAA84E43).

The janitor renames a checkout to a sibling quarantine before unregistering it and purging its files. A provider SessionEnd only logs its refusal. See the janitor schedule guide for the audit and interrupted-removal behavior.

Task-specific operating rules