Skip to content

Commit 8edbec0

Browse files
committed
Merge PR dlorenc#336: Detect running Claude before restart + lowercase errors
2 parents aae14e4 + b50f723 commit 8edbec0

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

internal/cli/cli.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"runtime/debug"
1212
"strconv"
1313
"strings"
14+
"syscall"
1415
"time"
1516

1617
"github.com/google/uuid"
@@ -5495,6 +5496,7 @@ func (c *CLI) localRepair(verbose bool) error {
54955496
}
54965497

54975498
// restartClaude restarts Claude in the current agent context.
5499+
// It checks if Claude is already running and provides helpful error messages if so.
54985500
// It auto-detects whether to use --resume or --session-id based on session history.
54995501
func (c *CLI) restartClaude(args []string) error {
55005502
// Infer agent context from cwd
@@ -5518,6 +5520,41 @@ func (c *CLI) restartClaude(args []string) error {
55185520
return fmt.Errorf("agent has no session ID - try removing and recreating the agent")
55195521
}
55205522

5523+
// Check if Claude is already running
5524+
if agent.PID > 0 {
5525+
// Check if the process is still alive
5526+
process, err := os.FindProcess(agent.PID)
5527+
if err == nil {
5528+
// Send signal 0 to check if process exists (doesn't actually signal, just checks)
5529+
err = process.Signal(syscall.Signal(0))
5530+
if err == nil {
5531+
// Process is still running - provide helpful error
5532+
return fmt.Errorf("claude is already running (PID %d) in this context.\n\nTo restart:\n 1. Exit Claude first (Ctrl+D or /exit)\n 2. Then run 'multiclaude claude' again\n\nOr attach to the running session:\n multiclaude attach %s", agent.PID, agentName)
5533+
}
5534+
}
5535+
}
5536+
5537+
// Get repo for tmux session info
5538+
repo, exists := st.GetRepo(repoName)
5539+
if !exists {
5540+
return fmt.Errorf("repo '%s' not found in state", repoName)
5541+
}
5542+
5543+
// Double-check: get the current PID in the tmux pane to detect any running process
5544+
tmuxClient := tmux.NewClient()
5545+
currentPID, err := tmuxClient.GetPanePID(context.Background(), repo.TmuxSession, agent.TmuxWindow)
5546+
if err == nil && currentPID > 0 {
5547+
// Check if this PID is alive and different from what we checked above
5548+
if currentPID != agent.PID {
5549+
if process, err := os.FindProcess(currentPID); err == nil {
5550+
if err := process.Signal(syscall.Signal(0)); err == nil {
5551+
// There's a different running process in the pane
5552+
return fmt.Errorf("a process (PID %d) is already running in this tmux pane.\n\nTo restart:\n 1. Exit the current process first\n 2. Then run 'multiclaude claude' again\n\nOr attach to view:\n multiclaude attach %s", currentPID, agentName)
5553+
}
5554+
}
5555+
}
5556+
}
5557+
55215558
// Get the prompt file path (stored as ~/.multiclaude/prompts/<agent-name>.md)
55225559
promptFile := filepath.Join(c.paths.Root, "prompts", agentName+".md")
55235560

0 commit comments

Comments
 (0)