fix: clean stale git worktrees in P4 housekeeping (Closes #11)

Add unconditional worktree cleanup to factory supervisor:
- Remove review + dev worktrees older than 2h with no active agent
- Use git worktree remove --force instead of rm -rf
- Run git worktree prune every poll to clear dangling refs

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
johba 2026-03-15 16:05:29 +01:00
parent 5c6293fdab
commit e1101894d6

View file

@ -292,6 +292,22 @@ if [ -n "$STALE_CLAUDES" ]; then
fixed "Killed stale claude processes: $(echo $STALE_CLAUDES | wc -w) procs" fixed "Killed stale claude processes: $(echo $STALE_CLAUDES | wc -w) procs"
fi fi
# Clean stale git worktrees (>2h, no active agent)
NOW_TS=$(date +%s)
for wt in /tmp/${PROJECT_NAME}-worktree-* /tmp/${PROJECT_NAME}-review-*; do
[ -d "$wt" ] || continue
WT_AGE_MIN=$(( (NOW_TS - $(stat -c %Y "$wt")) / 60 ))
if [ "$WT_AGE_MIN" -gt 120 ]; then
# Skip if an agent is still using it
WT_BASE=$(basename "$wt")
if ! pgrep -f "$WT_BASE" >/dev/null 2>&1; then
git -C "$PROJECT_REPO_ROOT" worktree remove --force "$wt" 2>/dev/null && \
fixed "Removed stale worktree: $wt (${WT_AGE_MIN}min old)" || true
fi
fi
done
git -C "$PROJECT_REPO_ROOT" worktree prune 2>/dev/null || true
# Rotate factory log if >5MB # Rotate factory log if >5MB
for logfile in "${FACTORY_ROOT}"/{dev,review,factory}/*.log; do for logfile in "${FACTORY_ROOT}"/{dev,review,factory}/*.log; do
if [ -f "$logfile" ]; then if [ -f "$logfile" ]; then