feat: detect stale worktree branches when main advances mid-session #136
Labels
No labels
domain:backend
domain:devops
domain:frontend
status:approved
status:in-progress
status:needs-fix
status:qa
type:bug
type:devops
type:feature
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
ldraney/claude-custom#136
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Type
Feature
Lineage
Discovered scope from plan-pal-e-agency Phase 16.
Siblings #193 (pre-spawn freshness), #194 (post-merge worktree cleanup), #195 (repo list fix) are done.
This ticket covers only the remaining gap not addressed by those siblings.
Repo
forgejo_admin/claude-customUser Story
As a dev agent working in a worktree branch
I want to know when
mainhas advanced due to external merges (other agents, CI, manual pushes) during my active sessionSo that I can avoid creating PRs against a stale base and reduce rebase churn
Context
What already exists (DO NOT duplicate):
post-merge-rebase.sh— PostToolUse ongh pr merge: fetches and fast-forwards local main after the current agent merges a PR.post-mcp-merge-rebase.sh— PostToolUse onmcp__forgejo__merge_approved_pr: same for Forgejo MCP merges.check-claude-custom-clean.sh— SessionStart: auto-pulls claude-custom main on session start.cleanup-worktrees.sh— SessionStart: removes stale worktrees older than 7 days.check-agent-spawn.sh— PreToolUse on Task: validates agent spawn schema (no freshness logic).The gap: All existing freshness mechanisms fire at session boundaries (SessionStart) or after the current agent's own merges (PostToolUse). There is no detection when
mainadvances due to external activity (a sibling agent merges, CI pushes, manual push) during an active session. The agent continues working against a stale base, discovers the divergence only at PR creation time, and must then rebase — sometimes with conflicts that waste the entire session.Note on #193: Board item #507 (pre-spawn freshness) is marked
done, buthooks/pre-spawn-freshness.shdoes not exist in the repo and no reference exists insettings.json. If that work was never merged, pre-spawn freshness may need a separate ticket to reopen. This ticket does NOT absorb #193's scope — it covers only mid-session detection.Resolved: Notification-only approach. Flag stale worktrees without touching them. Auto-rebase rejected — risks merge conflicts mid-agent-work.
File Targets
Files the agent should create or modify:
hooks/check-branch-freshness.sh— new hook: fetches origin/main, compares merge-base of current branch against origin/main HEAD, emits warning if divergedsettings.json— wire the new hook (trigger event TBD based on decision above)Files the agent should NOT touch:
hooks/post-merge-rebase.sh— handles same-session merges, working correctlyhooks/post-mcp-merge-rebase.sh— handles MCP merges, working correctlyhooks/check-agent-spawn.sh— spawn validation, not freshnessplugins/— config-only directory, not for custom automationAcceptance Criteria
origin/mainhas advanced past the current branch's merge-base during an active session, the agent receives a clear warning message indicating the branch is stale and how many commits behindgh pr create/mcp__forgejo__submit_pr, or a periodic SessionStart-like cadence)Test Expectations
gh pr merge,post-merge-rebase.shstill fires correctly (no interference)Constraints
set -euo pipefail, read$INPUTfrom stdin viajq, emit structured JSON for PreToolUse or plain stderr for notificationsorigin) and Forgejo (forgejo) remotes — detect the correct remote for maingit fetch origin main --quietonly, not a full fetchChecklist
Related
worktree-workflowSOP in pal-e-docsconvention-worktree-isolationin pal-e-docsScope Review: NEEDS_REFINEMENT
Review note:
review-241-2026-03-27The ticket has all required template sections but needs refinement before it's agent-ready.
Key issues:
arch:hookslabel on board item #241 — traceability gapplugins/worktree-rebase/is not a valid location (plugins/ is a plugin manager config dir, not custom code). All automation lives inhooks/. Replace with e.g.hooks/check-branch-freshness.shpost-merge-rebase.shandpost-mcp-merge-rebase.shalready fast-forward local main after merges. The remaining gap is specifically external merges (other agents, CI). The Context section should acknowledge what already worksSee full review in pal-e-docs:
review-241-2026-03-27Issue body updated per scope review corrections.
Scope Review: NEEDS_REFINEMENT (v2)
Review note:
review-241-2026-03-27-v2The issue body is the literal string
$NEW_BODY— a shell variable that was never expanded during the previous refinement update (comment #9200). The entire ticket spec is destroyed and the ticket is unexecutable.Blocking issues:
hooks/pre-spawn-freshness.shdoes not exist in the repo and is not wired insettings.json. Verify whether the PR was actually merged — if not, #136 may need to absorb that scope or #193 should be reopened.Labels are correct (no changes needed).
Worktree flow: auto-rebase branches when main advancesto feat: detect stale worktree branches when main advances mid-sessionScope Review: READY (v3)
Review note:
review-241-2026-03-27-v3Rewritten body resolves all prior review findings. All template sections present and complete. Traceability triangle verified (story:dev-execute, arch:hooks, Forgejo issue open). File targets confirmed against codebase. Notification-only decision is resolved. 2 file targets, 1 repo, 4 ACs — well within single-agent scope.
Minor nit (non-blocking):
356c83a, hook wired insettings.jsonon main). Consider updating to avoid confusing the implementing agent. The paragraph already correctly scopes #136 away from #193, so this is informational only.