fix(cleanup): read worktree.baseBranch config before git default-branch detection#1925
Conversation
…g.yaml (#1419) The cleanup service called getDefaultBranch() three times without first consulting the repo's .archon/config.yaml. On repos that use 'master' (or any non-main default) without origin/HEAD set, this threw env_cleanup_error once per tracked environment on every startup — and the error's own remediation ("Set worktree.baseBranch in .archon/config.yaml") was ineffective because that setting was never read by this service. Changes: - Import loadRepoConfig in cleanup-service.ts - Add private resolveBaseBranch(repoPath, cwd) helper that prefers config.worktree.baseBranch (trimmed) and falls back to getDefaultBranch - Replace the three getDefaultBranch call sites in runScheduledCleanup, getWorktreeStatusBreakdown, and cleanupMergedWorktrees - Mock loadRepoConfig in cleanup-service.test.ts and add four regression tests covering: master via config, whitespace-trimmed config value, fallback when no config, and whitespace-only config value Fixes #1419
Comprehensive PR ReviewPR: #1925 · fix(cleanup): read worktree.baseBranch config before git default-branch detection SummaryThe fix is correct and minimal. The cleanup service was calling Verdict:
🟡 Medium Issues (Consider Before Merge)Two
|
| Issue | Location | Note |
|---|---|---|
type RepoConfigForTest duplicates a subset of RepoConfig |
cleanup-service.test.ts:97 |
Narrow local type is a defensible test pattern; risk of divergence is very low given worktree.baseBranch stays optional |
| Comments say "getDefaultBranch returns 'main'" | cleanup-service.test.ts:968,1005,1030 |
The path now goes through resolveBaseBranch → loadRepoConfig → getDefaultBranch; pre-existing style, not introduced by this PR |
✅ What's Good
- The
resolveBaseBranchcomment is one of the few places where a multi-line block comment is genuinely warranted: it names worktree.baseBranch in .archon/config.yaml is documented but never read — causes repeated env_cleanup_error on startup #1419, explains the historical dead-end, and documents the non-obvious constraint. - All 3
getDefaultBranchcall sites updated consistently — no partial fix left behind. - Whitespace-only
baseBranchtrimming is correct:' '.trim()is falsy, routing to git fallback as expected. - 4 regression tests cover all distinct paths: config-wins, fallback, whitespace-trim, whitespace-only-fallback.
- Scope is truly minimal: no interface changes, no DB schema changes, no config key additions.
- Full
bun run validatepasses on first run (7/7 checks).
Suggested Follow-up
- Consider a P3 issue for the
codebase.default_branchDB-column fallback path mentioned as "and/or" in worktree.baseBranch in .archon/config.yaml is documented but never read — causes repeated env_cleanup_error on startup #1419 — intentionally deferred here, scope was correct to omit it.
…gedWorktrees
Both describe blocks now call resolveBaseBranch (which calls loadRepoConfig)
after the PR fix, but their beforeEach blocks weren't updated to clear and
reset mockLoadRepoConfig. Any future test using the sticky mockResolvedValue
form in a prior suite would silently bleed in. Added mockLoadRepoConfig.mockClear()
and mockLoadRepoConfig.mockResolvedValue({}) to both, matching the pattern
already applied to the removeEnvironment and runScheduledCleanup suites.
Also updated stale inline comments from "getDefaultBranch returns 'main'" to
"resolveBaseBranch returns 'main' (no config → getDefaultBranch fallback)"
to reflect the new indirection.
⚡ Self-Fix Report (Aggressive)Status: COMPLETE Fixes Applied (3 total)
View all fixes
Tests Added(none — existing 46 tests pass; fix hardened their mock isolation) Skipped(none — all findings addressed) Suggested Follow-up Issues(none) Validation✅ Type check | ✅ Lint | ✅ Tests (46 passed) Self-fix by Archon · aggressive mode · fixes pushed to |
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughCleanup service now respects the ChangesBase branch resolution from config
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
getDefaultBranch()unconditionally at 3 call sites, causing repeatedenv_cleanup_errorlogs on every startup for repos whose default branch is notmainand whereorigin/HEADis not configured. The error message told users to setworktree.baseBranchin.archon/config.yaml, but that config value was never read by the cleanup service — making the suggestion useless.master-branch repo triggered a logged error per startup with no viable workaround. Prior fix attempts (fix(core): respect worktree.baseBranch in cleanup service default branch resolution #1423, fix(core): respect worktree.baseBranch in cleanup service #1424) were closed without landing.resolveBaseBranchhelper incleanup-service.tsthat readsworktree.baseBranchfrom.archon/config.yamlfirst, falling back togetDefaultBranch()only when nothing is configured. Replaced all 3getDefaultBranchcall sites withresolveBaseBranch. Added 4 regression tests.getDefaultBranchitself is unchanged;config-loader.tsis unchanged; no interface or DB schema changes; thecodebase.default_branchDB-column fallback (mentioned as "and/or" in the issue) was intentionally omitted to keep scope minimal.UX Journey
Before
After
Architecture Diagram
Before
After
Connection inventory:
cleanup-service.ts@archon/git:getDefaultBranchresolveBaseBranch; not called when config provides branchcleanup-service.tsconfig-loader.ts:loadRepoConfigworktree.baseBranchfirstresolveBaseBranch@archon/git:getDefaultBranchresolveBaseBranchconfig-loader.ts:loadRepoConfigLabel Snapshot
risk: lowsize: Scorecore:cleanup-serviceChange Metadata
bugcoreLinked Issue
Validation Evidence (required)
All checks passed on the first run:
bun run validatechecks passed; 4 new regression tests covering: master-branch-via-config, fallback-to-git-detection, whitespace-trimming, whitespace-only-fallback.Security Impact (required)
loadRepoConfigreads.archon/config.yamlin the repo CWD, which the cleanup service already has access to.Compatibility / Migration
worktree.baseBranchis not configured, behavior is identical to before.worktree.baseBranchnow actually works.Human Verification (required)
bun run validate.baseBranchcorrectly falls through to git detection;loadRepoConfigreturning{}(file absent) correctly falls through to git detection.master-branch repo — the regression tests cover this path at the unit level.Side Effects / Blast Radius (required)
runScheduledCleanup,getWorktreeStatusBreakdown,cleanupMergedWorktrees).loadRepoConfigis called once per tracked environment per cleanup cycle — this is a cheap local file read on a scheduled (non-hot) path, so performance impact is negligible.try/catchinrunScheduledCleanupcontinues to catch anyloadRepoConfigparse errors and log them asenv_cleanup_error(with a more useful message than before).Rollback Plan (required)
git revert 03fdd3e7— single commit, no migration to undo.worktree.baseBranchis set.env_cleanup_errorin server logs per tracked environment on startup (same as before the fix).Risks and Mitigations
loadRepoConfigthrows on a malformed.archon/config.yamlin a tracked repo.try/catchinrunScheduledCleanupat line ~375 already catches per-environment errors and logs them.getWorktreeStatusBreakdownandcleanupMergedWorktreespropagate the error to their callers, which already handle it. No new unhandled throw paths.Summary by CodeRabbit
New Features
Bug Fixes
#1419), including whitespace trimming and improved fallback handling.