aws-samples / aws-samples/sample-autonomous-cloud-coding-agents

chore(dx): worktree hygiene automation — rebase on main drift

Open
#197 0 comments 0 reactions 1 assignee Claimed by @ClintEastman02 View on GitHub
enhancement tooling
Dominant language
TypeScript
Stars
143
Forks
46
Avg merge
3d 9h
Merged PRs (30d)
20

Description

## Problem
Worktrees created from `origin/main` drift silently as other branches merge to main. By the time a PR is pushed, the branch may be multiple commits behind, causing:
- Unnecessary merge conflicts at PR time
- CI running against stale base (masking integration issues)
- Manual `git fetch && git rebase` before every push

Example: the `fix/194-ts-isolated-modules` worktree was 1 commit behind `origin/main` at push time because PR #171 (ESLint 10) merged after branch creation.

## Acceptance criteria
- [ ] Worktrees are created from the latest `origin/main` (enforce `git fetch origin main` before `git worktree add`)
- [ ] Active worktrees are periodically rebased or at minimum warned about staleness
- [ ] No data loss — only rebase clean worktrees (no uncommitted changes)

## Suggested implementation

### Option A: mise task `worktree:sync` (recommended)
Add a mise task that iterates active worktrees and rebases them on `origin/main`:

```bash
# mise.toml
[tasks."worktree:sync"]
description = "Fetch origin/main and rebase all active worktrees"
run = """
git fetch origin main
for wt in $(git worktree list --porcelain | grep '^worktree ' | grep -v '$(git rev-parse --show-toplevel)$' | sed 's/^worktree //'); do
branch=$(git -C "$wt" rev-parse --abbrev-ref HEAD 2>/dev/null)
if [ -z "$(git -C "$wt" status --porcelain)" ]; then
echo "Rebasing $branch in $wt..."
git -C "$wt" rebase origin/main || git -C "$wt" rebase --abort
else
echo "SKIP $branch (dirty working tree)"
fi
done
"""
```

### Option B (optional): SessionStart hook for staleness warning
A Claude Code hook or startup script that checks worktree age and warns:

```bash
# Check each worktree's distance from origin/main
for wt in $(git worktree list --porcelain | grep '^worktree ' | sed 's/^worktree //'); do
behind=$(git -C "$wt" rev-list --count HEAD..origin/main 2>/dev/null)
if [ "$behind" -gt 0 ]; then
echo "⚠ $wt is $behind commit(s) behind origin/main"
fi
done
```

### Option C (optional): pre-push rebase
Add to the pre-push hook: if the branch is behind `origin/main`, auto-rebase before pushing (abort if conflicts).

## Labels
`enhancement`, `developer-experience`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.