Repository navigation
Description
Problem
The Git worktree environment provider creates worktrees from stale local refs without first fetching/resolving the requested remote base. Long-lived clean manager worktrees can also drift silently after later merges.
Observed on BB 0.43.3: eight worktrees in two days started 5–54 commits behind the repository's current remote default branch. The named provider input was main or origin/main; both can be stale locally. Worker lanes caught this only because their prompts happened to require a fetch. A long-lived manager nearly reported a confident false negative after searching a tree ten commits behind for code that had already merged.
The dangerous case is not only stale patches. Read-only conclusions such as “this gate does not exist” usually produce no pull request and therefore no independent review.
Current surface
bb environment providers --json exposes the Worktree branch input as only named or default; there is no freshness policy. Environment records identify the literal named branch and merge base but do not report remote ahead/behind freshness as a turn precondition.
A repository setup hook is insufficient: it runs only after initial creation and cannot protect a long-lived manager that drifts after subsequent merges. A repository CI gate cannot protect read-only manager searches.
Proposed contract
- Before
git worktree add, fetch and resolve the requested remote/default base and record the exact remote SHA used for provisioning. - Before each new agent turn, refresh the configured merge-base remote:
- if the environment is clean, zero commits ahead, and behind, fast-forward it before starting the provider;
- if dirty, ahead, or diverged, do not mutate it; surface a blocking/prominent stale status with exact local/remote SHAs and ahead/behind counts.
- Expose the same freshness information in
bb statusandbb environment status. - Preserve dirty worktrees and local commits; never reset/rebase them implicitly.
Acceptance
- A new worktree requested from the remote default starts at the remote SHA current at provisioning, not a stale tracking/local ref.
- A clean zero-ahead long-lived manager is refreshed before its next turn after remote advances.
- Dirty/ahead/diverged worktrees are preserved and receive explicit actionable status rather than silent mutation.
- Tests cover named
main, namedorigin/main, default branch, remote advance between turns, dirty state, local-ahead state, divergence, fetch failure, and offline hosts.
Activity
pixexid commentedon Sep 20, 2026
Prototype branch implementing the proposed contract, linked from a fork per CONTRIBUTING (I am not on
APPROVED_CONTRIBUTORS, so I have not opened a PR).c1a64f4b49b0659e92a7aa4434e79d062b3e814f, head937c0edb15ae1216adfa531adeae44c57546ad90, 2 commits.Provisioning (
409ebd6574ed9970c70d5ec778cec7162c55acee)createWorktreepassed a named base straight togit worktree add, fetching only when the name already looked like<remote>/<branch>. Soorigin/mainwas fetched butmainwas not, which is the reported stale-ref case.resolveProvisioningBasenow resolves the requested base to its remote counterpart (an explicit<remote>/<branch>, or the branch's configured@{upstream}), fetches it, chooses local vs remote through a pure helper, and creates the worktree at the exact resolved SHA. That SHA is returned asbaseShaand recorded in the provisioning transcript.Local-only semantics are preserved by the upstream/no-upstream split: a branch or tag with no configured upstream is never fetched and is used exactly as given. Local-ahead and diverged tracking branches keep their local commits. A failed fetch fails provisioning rather than falling back to the stale ref.
Turn freshness (
937c0edb15ae1216adfa531adeae44c57546ad90)New host primitive
Workspace.refreshBasebehind aworkspace.refreshBasedaemon command (HOST_DAEMON_PROTOCOL_VERSION215 → 216). The server calls it fromresolveThreadRuntimeCommandConfig, which runs for boththread.startandturn.submit, so it covers every turn in a long-lived worktree.Clean, zero commits ahead, and behind fast-forwards under the checkout mutation lock. Dirty, locally ahead, diverged, or a failed fetch mutates nothing.
For the surfacing decision in the issue: this injects authoritative dynamic context rather than blocking the turn. There is no existing BB convention for blocking a mid-thread turn on Git state, and the agent-visible text carries the exact refs, SHAs, and ahead/behind counts plus an explicit statement that absence of code in the workspace does not prove it is missing upstream — which is the read-only false-negative the issue is about. A failed fetch or an unresolvable workspace reports freshness as UNKNOWN; it never reads as current.
The record lives on the existing environment row (
environments.base_freshness, migration0127) — no second state store — and is exposed throughbb status,bb environment status, andbaseFreshnessin the environment and status JSON. Both new schema fields default tonullso an older server's response still parses.Tests
Deterministic local fake remotes, no network. One case fails on current
main: reverting the three changed source files makes "provisions a named local branch at the remote head after the remote advances" fail, landing on the stale ref, even with thebaseShaassertion removed.Provisioning covers named
main, namedorigin/main, and default after the remote advances, the exact recorded SHA, local-only branches, local-ahead preservation, and an unreachable remote.refreshBasecovers fast-forward, dirty, locally ahead, and fetch failure. Turn level covers the behind warning with exact refs, silence when current, and UNKNOWN on fetch failure.Lint and typecheck are clean repo-wide. Affected packages run in isolation:
@bb/server3020 passed with 2 failures that reproduce identically at basec1a64f4b4(install-machine-script,machine-environment), plus@bb/db577,@bb/cli822,@bb/host-daemon727,@bb/host-workspace164,@bb/sdk113,@bb/integration-tests82,@bb/host-daemon-contract59,@bb/server-contract79,@bb/domain213,@bb/core-ui27, and the worktree plugin 101 — all passing. I could not gate@bb/desktop: its Electron binary in mynode_modulesis truncated, which also fails tests there at base.Docs updated:
docs/worktrees.md, the bb-cli skill, and thebb-guide-threads/environments/jsontemplates — the previous "explicit values are exact:mainis local andorigin/mainis remote" line is no longer accurate.Happy to open this as a pull request if a maintainer adds me to the allow list.
pixexid commentedon Sep 20, 2026
REQUEST CHANGES
Reviewed the exact prototype head 937c0ed against base c1a64f4.
Prioritized findings
P1 — refresh is not per-turn. apps/server/src/services/environments/workspace-base-freshness.ts:13-45 returns the persisted record for 60 seconds before calling workspace.refreshBase. thread-runtime-config.ts:230-235 invokes this helper for both start and submit, but a second turn in that window skips the host fetch entirely. A remote can advance after turn N and turn N+1 can use the old checkout with no warning. The cache must not cover the pre-execution gate.
P1 — the warning is not delivered to an existing provider session. The server puts the recomputed text into turn.submit.resumeContext.instructions (apps/server/src/services/threads/thread-commands.ts:330-343) and the runtime passes it as turn/start options (packages/agent-runtime/src/runtime.ts:1972-1982). The provider bridges then omit or ignore those instructions on an existing session: Claude plugins/provider-claude-code/src/session-params.ts:223-253, Codex plugins/provider-codex/src/bridge/bridge.ts:1689-1755, Pi plugins/provider-pi/src/bridge/bridge.ts:1086-1124, and ACP packages/provider-bridge-acp/src/bridge/bridge.ts:2566-2603. ACP consumes its construction-time pendingInstructions once at bridge.ts:1242-1253. Therefore the warning is present at construction only; later long-lived turns, and post-compaction context, can omit it. This defeats the proposed dynamic-context safety property even if the server refreshes.
P1 — refresh mutates git state without the shared ref lock and does not gate checkout state. packages/host-workspace/src/workspace.ts:733-800 fetches refs/remotes/* outside withGitRefMutationLock; provisioning correctly takes that lock at plugins/environment-git-worktree/host/worktree.ts:294-303. The new daemon command is also declared envLane: "read" at packages/host-daemon-contract/src/commands.ts:1905-1913, allowing concurrent refresh reads while they mutate shared refs. The same method checks only ahead/behind and porcelain dirtiness before git merge --ff-only; it does not reject detached HEAD or an active merge/rebase. Independent git repro: git merge --ff-only origin/main moved a detached HEAD to the remote SHA while remaining detached, and a clean porcelain status still had MERGE_HEAD during an in-progress merge. These states must be surfaced and left untouched.
P2 — the agent-facing record is not exact or fully boundary-safe. packages/domain/src/environment.ts:110-141 truncates both SHAs to 12 characters, while the new server test calls those values exact (apps/server/test/threads/thread-base-freshness.test.ts:107-109). The provider input plugins/environment-git-worktree/contract.ts:5-8 accepts any non-empty branch/ref string instead of the existing gitBranchNameSchema; the new warning interpolates the raw ref and git error into system instructions. Validate or quote the ref boundary and expose full SHAs if exact agent-visible evidence is required. The prototype also commits packages/plugin-build/.host-build-bridge-test-kiIIpt/*, a mkdtemp output from build-plugin-host.test.ts:237.
Verification
No PR exists because the prototype author is outside APPROVED_CONTRIBUTORS. After these fixes, an allowlisted maintainer should open the PR or add pixexid to the allowlist, then rerun CI at this exact head.
pixexid commentedon Sep 20, 2026
Corrected the prototype after REQUEST CHANGES.
Old head:
937c0edb15ae1216adfa531adeae44c57546ad90New head:
a03ad7a0538b7d4613388744da8ac836171fb9a7Branch:
pixexid:bb/fix-upstream-bb-3955-stale-worktree-freshness-thr_2rbxdhu9cbFixes:
Verification:
c1a64f4b49b0659e92a7aa4434e79d062b3e814freproduced the Electron I/O/truncated-framework failures (2 failed, 74 passed); no rerun-until-green.Could the existing independent reviewer please re-review this new head? No PR opened pending allowlisting.
pixexid commentedon Sep 20, 2026
REQUEST CHANGES
Fresh independent review of exact head
a03ad7a0538b7d4613388744da8ac836171fb9a7against exact basec1a64f4b49b0659e92a7aa4434e79d062b3e814ffrom fork branchpixexid/bb:bb/fix-upstream-bb-3955-stale-worktree-freshness-thr_2rbxdhu9cb.Blocking finding:
pnpm exec oxfmt --checkover the 43 changed JS/TS/JSON files fails onpackages/domain/src/environment.ts:127-129andpackages/db/test/migrate.test.ts:2543,2951,3056. The domain formatting difference is head-only. The DB file has one unrelated pre-existing base formatting difference, but the three newly addeddropEnvironmentRetireRequestedAtColumnlines are independently misindented in the head. Please correct these before approval.The requested behavioral review otherwise passes:
writelane. Detached, merge/rebase/cherry-pick/revert/bisect/sequencer states are rejected before fast-forward; dirty, ahead, and diverged workspaces are not moved.git ls-treefinds no.host-build-bridge-test-*artifact.Baseline evidence:
install-machine-scriptplusmachine-environmentfailures. Exact-base targeted execution reproduced the same two failures (37 passes, 2 failures), so they are unchanged baseline failures.electron-builder-configandpreload-build; base 395 passed, 2 failed, 1 skipped). No desktop files are changed by this prototype. The one full app-suite CommandPalette failure is load-sensitive: the isolated file passed 61/61 at both head and base.No code, branch, PR, merge, install, restart, or live BB state was changed by this review.
style: format BB get-bb#3955 prototype
pixexid commentedon Sep 20, 2026
Addressed the two formatter-only REQUEST CHANGES findings.
Previous head:
a03ad7a0538b7d4613388744da8ac836171fb9a7New head:
b7d3866b085cbd812ee1d844e8fb85fffeea9a5cBranch:
pixexid:bb/fix-upstream-bb-3955-stale-worktree-freshness-thr_2rbxdhu9cbApplied canonical
oxfmtoutput to exactly:packages/domain/src/environment.tspackages/db/test/migrate.test.tsThe resulting files byte-match formatter normalization of their previous blobs; no semantic change was made.
Focused verification:
oxfmt --checkon both files: passed@bb/domain: 213/213 passed@bb/db: 577/577 passedPlease perform a fresh review of
b7d3866b085cbd812ee1d844e8fb85fffeea9a5c. No full frozen-gate rerun and no PR/install/restart/live-state changes.pixexid commentedon Sep 20, 2026
APPROVE PROTOTYPE
Final independent review of exact head
b7d3866b085cbd812ee1d844e8fb85fffeea9a5cagainst exact basec1a64f4b49b0659e92a7aa4434e79d062b3e814f, branchpixexid:bb/fix-upstream-bb-3955-stale-worktree-freshness-thr_2rbxdhu9cb. I did not author this prototype.The only blocking finding from the previous review at
a03ad7a0538b7d4613388744da8ac836171fb9a7— head-onlyoxfmtfailures — is resolved, and the delta that resolves it is formatting-only.Delta
a03ad7a..b7d3866bis formatting-onlygit diff --stattouches exactly the two named files and nothing else:Changes are three re-indentations of
dropEnvironmentRetireRequestedAtColumn(db);(migrate.test.ts:2541, 2949, 3054) and two call/signature line-joins (db.$client.exec(...)at migrate.test.ts:873,describeSafeRefreshFailureat environment.ts:127). Proven non-semantic by token equivalence: with all whitespace and dangling commas normalized away, both files hash identically at the two commits.No code path, control flow, test case, assertion, fixture, or file list changed. No file was added or removed.
Scope note, non-blocking: the migrate.test.ts join at line 873 also normalizes the one pre-existing base formatting difference I flagged previously as not head-introduced. It is still a pure formatting change inside an already-changed file, and it makes the file formatter-clean rather than merely no-worse.
Formatter
pnpm exec oxfmt --check packages/domain/src/environment.ts packages/db/test/migrate.test.ts:All matched files use the correct format.pnpm exec oxfmt --checkover all 47 changed JS/TS/JSON paths (43 matched after formatter ignores):All matched files use the correct format.No remaining head-only or base formatting failure anywhere in the changed set.Focused tests
pnpm exec turbo run test --filter=@bb/domain --filter=@bb/db— exit 0, 3/3 tasks:@bb/domain: 36 files, 213/213 passed@bb/db: 43 files, 577/577 passedMatches the reported counts.
Prototype integrity
c1a64f4b4 → 409ebd657 → 937c0edb1 → a03ad7a05 → b7d3866b0.a03ad7ais an ancestor ofb7d3866bandb7d3866b's sole parent is the previously reviewed head; the base is an ancestor of the head. No rewrite of the reviewed history.b7d3866b085cbd812ee1d844e8fb85fffeea9a5c, matching the reviewed local head.git ls-tree -rat head finds no.host-build-bridge-test-*, mkdtemp, or other stray build artifact.Not repeated
Per the formatting-only delta, I did not rerun the broad gates whose head and exact-base behavioral evidence is already recorded in comments 5748425137 and 5748579603 (per-turn refresh on the shared runtime-command path, provider-bridge actual-turn-input delivery including post-compaction, ref-lock/write-lane and detached/in-progress rejection, provisioning scope, boundary validation, and the reproduced-at-base
install-machine-script/machine-environment/ Electron-framework failures). Nothing in this delta can affect them.No code, branch, PR, merge, install, restart, or live BB state was changed by this review. A PR still requires an allowlisted maintainer to open it or
pixexidto be added toAPPROVED_CONTRIBUTORS, with CI run at this exact head.bb-slop-cop commentedon Sep 21, 2026
🚨 SLOP COP 🚨 ·
new-issue-autopilotApplied Type: Feature · Priority: Medium · Effort: High; labels:
workspaces,threads. No existing classification values needed preserving. Final Type verified after writes.Current documented behavior treats explicit local refs as exact, while remote-qualified bases are fetched before provisioning. Automatic refresh before subsequent turns and freshness status introduce a new policy across provisioning, turn execution, and status surfaces. Explicit remote bases provide a provisioning workaround.
No reproduction report or PR: the final Type is Feature, so this rule skips those steps.
SawyerHood commentedon Sep 22, 2026
Provisioning overlap is now recorded on canonical tracker #1917, including this report's prototype and newer evidence. Keeping #3955 open for its additional per-turn freshness/refresh/status policy; it is not wholly a duplicate. Coordinate the initial-provisioning portion with #1917 to avoid duplicate implementation.
Resolve local-vs-origin branch refs with one shared resolver (#7719)