Skip to content

Commit 8e89191

Browse files
committed
wip
1 parent 72e2907 commit 8e89191

2 files changed

Lines changed: 189 additions & 8 deletions

File tree

hack/cleanup_refactor_workers.sh

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
#!/bin/bash
2+
3+
# cleanup_refactor_workers.sh - Clean up worktrees and tmux sessions for refactor workers
4+
# Matches the exact configuration from launch_refactor_workers.sh
5+
6+
set -e
7+
8+
# Configuration - matches launch_refactor_workers.sh exactly
9+
declare -a CONTROLLER_NAMES=("agent" "task" "llm" "mcpserver" "contactchannel")
10+
declare -a SESSION_NAMES=("agent-refactor" "task-refactor" "llm-refactor" "mcpserver-refactor" "contactchannel-refactor")
11+
12+
# Colors for output
13+
RED='\033[0;31m'
14+
GREEN='\033[0;32m'
15+
YELLOW='\033[1;33m'
16+
BLUE='\033[0;34m'
17+
NC='\033[0m' # No Color
18+
19+
log_info() {
20+
echo -e "${BLUE}ℹ️ $1${NC}"
21+
}
22+
23+
log_success() {
24+
echo -e "${GREEN}$1${NC}"
25+
}
26+
27+
log_warning() {
28+
echo -e "${YELLOW}⚠️ $1${NC}"
29+
}
30+
31+
log_error() {
32+
echo -e "${RED}$1${NC}"
33+
}
34+
35+
cleanup_tmux_sessions() {
36+
log_info "Cleaning up tmux sessions..."
37+
38+
for session_name in "${SESSION_NAMES[@]}"; do
39+
if tmux has-session -t "$session_name" 2>/dev/null; then
40+
tmux kill-session -t "$session_name"
41+
log_success "Killed tmux session: $session_name"
42+
else
43+
log_warning "Session not found: $session_name"
44+
fi
45+
done
46+
}
47+
48+
cleanup_worktrees() {
49+
log_info "Cleaning up worktrees..."
50+
51+
# Get base directory name (should match create_worktree.sh logic)
52+
local repo_base_name=$(basename "$(pwd)")
53+
54+
for i in "${!CONTROLLER_NAMES[@]}"; do
55+
local controller_name="${CONTROLLER_NAMES[$i]}"
56+
local worktree_name="refactor-${controller_name}"
57+
local worktree_dir_name="${repo_base_name}_${worktree_name}"
58+
local worktree_path="$HOME/.humanlayer/worktrees/${worktree_dir_name}"
59+
60+
log_info "Processing ${controller_name} worktree..."
61+
62+
# Remove worktree if it exists
63+
if [ -d "$worktree_path" ]; then
64+
if git worktree remove "$worktree_path" 2>/dev/null; then
65+
log_success "Removed worktree: $worktree_path"
66+
else
67+
log_warning "Failed to remove worktree (may not be tracked): $worktree_path"
68+
# Force remove directory if git worktree remove failed
69+
rm -rf "$worktree_path"
70+
log_success "Force removed directory: $worktree_path"
71+
fi
72+
else
73+
log_warning "Worktree directory not found: $worktree_path"
74+
fi
75+
76+
# Delete branch if it exists
77+
if git show-ref --verify --quiet "refs/heads/${worktree_name}"; then
78+
if git branch -D "$worktree_name" 2>/dev/null; then
79+
log_success "Deleted branch: $worktree_name"
80+
else
81+
log_warning "Failed to delete branch: $worktree_name"
82+
fi
83+
else
84+
log_warning "Branch not found: $worktree_name"
85+
fi
86+
done
87+
88+
# Prune worktree registry to clean up any stale references
89+
log_info "Pruning git worktree registry..."
90+
if git worktree prune 2>/dev/null; then
91+
log_success "Pruned stale worktree references"
92+
else
93+
log_warning "Failed to prune worktree registry"
94+
fi
95+
}
96+
97+
list_status() {
98+
log_info "Current status..."
99+
100+
echo
101+
echo "Tmux sessions:"
102+
for session_name in "${SESSION_NAMES[@]}"; do
103+
if tmux has-session -t "$session_name" 2>/dev/null; then
104+
echo "$session_name (active)"
105+
else
106+
echo "$session_name (not found)"
107+
fi
108+
done
109+
110+
echo
111+
echo "Worktrees:"
112+
local repo_base_name=$(basename "$(pwd)")
113+
for controller_name in "${CONTROLLER_NAMES[@]}"; do
114+
local worktree_name="refactor-${controller_name}"
115+
local worktree_dir_name="${repo_base_name}_${worktree_name}"
116+
local worktree_path="$HOME/.humanlayer/worktrees/${worktree_dir_name}"
117+
118+
if [ -d "$worktree_path" ]; then
119+
echo "$controller_name ($worktree_path)"
120+
else
121+
echo "$controller_name (not found)"
122+
fi
123+
done
124+
125+
echo
126+
echo "Branches:"
127+
for controller_name in "${CONTROLLER_NAMES[@]}"; do
128+
local worktree_name="refactor-${controller_name}"
129+
if git show-ref --verify --quiet "refs/heads/${worktree_name}"; then
130+
echo "$worktree_name"
131+
else
132+
echo "$worktree_name (not found)"
133+
fi
134+
done
135+
}
136+
137+
# Main execution
138+
main() {
139+
case "${1:-all}" in
140+
"tmux"|"sessions")
141+
cleanup_tmux_sessions
142+
;;
143+
"worktrees"|"trees")
144+
cleanup_worktrees
145+
;;
146+
"status"|"list")
147+
list_status
148+
;;
149+
"all"|"")
150+
log_info "Starting cleanup of all refactor workers..."
151+
cleanup_tmux_sessions
152+
echo
153+
cleanup_worktrees
154+
echo
155+
log_success "Cleanup complete!"
156+
;;
157+
"help"|"-h"|"--help")
158+
echo "Usage: $0 [command]"
159+
echo
160+
echo "Commands:"
161+
echo " all Clean up both tmux sessions and worktrees (default)"
162+
echo " tmux Clean up only tmux sessions"
163+
echo " worktrees Clean up only worktrees and branches"
164+
echo " status Show current status of sessions and worktrees"
165+
echo " help Show this help message"
166+
echo
167+
echo "Controllers managed:"
168+
for name in "${CONTROLLER_NAMES[@]}"; do
169+
echo " - $name"
170+
done
171+
;;
172+
*)
173+
log_error "Unknown command: $1"
174+
log_info "Use '$0 help' for usage information"
175+
exit 1
176+
;;
177+
esac
178+
}
179+
180+
# Run main function
181+
main "$@"

hack/launch_refactor_workers.sh

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Key Instructions:
5151
4. PRESERVE EXISTING PATTERNS - Keep helper methods and validation logic
5252
5. USE EXISTING DEPENDENCIES - Don't create new files unnecessarily
5353
54-
Critical Rules for ${controller_name^} Controller:
54+
Critical Rules for ${controller_name} Controller:
5555
- Current controller: acp/internal/controller/${controller_name}/${controller_name}_controller.go
5656
- Create StateMachine: acp/internal/controller/${controller_name}/state_machine.go
5757
- Tests: acp/internal/controller/${controller_name}/${controller_name}_controller_test.go
@@ -135,14 +135,14 @@ main() {
135135
tmux new-session -d -s "$session_name" -c "$worktree_path"
136136
tmux split-window -h -t "$session_name" -c "$worktree_path"
137137

138-
# 5. Launch claude with prompt in first pane
139-
tmux send-keys -t "$session_name:0" "claude \"\$(cat prompt.md)\"" C-m
138+
# 5. Launch claude with prompt in first pane (left pane)
139+
tmux send-keys -t "$session_name:1.1" "claude \"\$(cat prompt.md)\"" C-m
140140

141-
# Set up second pane for troubleshooting
142-
tmux send-keys -t "$session_name:1" "echo 'Troubleshooting pane ready. Key commands:'" C-m
143-
tmux send-keys -t "$session_name:1" "echo 'make test - run tests'" C-m
144-
tmux send-keys -t "$session_name:1" "echo 'make check - run checks'" C-m
145-
tmux send-keys -t "$session_name:1" "echo 'cat plan.md - view the plan'" C-m
141+
# Set up second pane for troubleshooting (right pane)
142+
tmux send-keys -t "$session_name:1.2" "echo 'Troubleshooting pane ready. Key commands:'" C-m
143+
tmux send-keys -t "$session_name:1.2" "echo 'make test - run tests'" C-m
144+
tmux send-keys -t "$session_name:1.2" "echo 'make check - run checks'" C-m
145+
tmux send-keys -t "$session_name:1.2" "echo 'cat plan.md - view the plan'" C-m
146146

147147
log_success "Created tmux session: ${session_name}"
148148
done

0 commit comments

Comments
 (0)