fix(tmux): prevent gt done from killing itself during session cleanup (#821)

When gt done runs inside a tmux session (e.g., after polecat task
completion), calling KillSessionWithProcesses would kill the gt done
process itself before it could complete cleanup operations like writing
handoff state.

Add KillSessionWithProcessesExcluding() function that accepts a list of
PIDs to exclude from the kill sequence. Update selfKillSession to pass
its own PID, ensuring gt done completes before the session is destroyed.

Also fix both Kill*WithProcesses functions to ignore "session not found"
errors from KillSession - when we kill all processes in a session, tmux
may automatically destroy it before we explicitly call KillSession.

Co-authored-by: julianknutsen <julianknutsen@users.noreply.github>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Julian Knutsen
2026-01-20 19:34:20 -08:00
committed by GitHub
parent 4dd11d4ffa
commit f82477d6a6
3 changed files with 226 additions and 3 deletions

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/spf13/cobra"
@@ -746,9 +747,12 @@ func selfKillSession(townRoot string, roleInfo RoleInfo) error {
// Kill our own tmux session with proper process cleanup
// This will terminate Claude and all child processes, completing the self-cleaning cycle.
// We use KillSessionWithProcesses to ensure no orphaned processes are left behind.
// We use KillSessionWithProcessesExcluding to ensure no orphaned processes are left behind,
// while excluding our own PID to avoid killing ourselves before cleanup completes.
// The tmux kill-session at the end will terminate us along with the session.
t := tmux.NewTmux()
if err := t.KillSessionWithProcesses(sessionName); err != nil {
myPID := strconv.Itoa(os.Getpid())
if err := t.KillSessionWithProcessesExcluding(sessionName, []string{myPID}); err != nil {
return fmt.Errorf("killing session %s: %w", sessionName, err)
}