diff --git a/cmd/agent-desk/main.go b/cmd/agent-desk/main.go index 1eb8fcfd..6586a960 100644 --- a/cmd/agent-desk/main.go +++ b/cmd/agent-desk/main.go @@ -25,6 +25,7 @@ import ( "github.com/millwright-software/agent-desk/internal/logging" "github.com/millwright-software/agent-desk/internal/session" "github.com/millwright-software/agent-desk/internal/statedb" + "github.com/millwright-software/agent-desk/internal/tmux" "github.com/millwright-software/agent-desk/internal/ui" "github.com/millwright-software/agent-desk/internal/update" ) @@ -1821,7 +1822,7 @@ func detectTool(cmd string) string { } cmd = strings.ToLower(cmd) - if fields := strings.Fields(cmd); len(fields) > 0 && filepath.Base(fields[0]) == "copilot" { + if tmux.IsCopilotCommand(cmd) { return "copilot" } switch { diff --git a/docs/copilot-recovery.md b/docs/copilot-recovery.md index 77dac651..ff0fbde4 100644 --- a/docs/copilot-recovery.md +++ b/docs/copilot-recovery.md @@ -10,6 +10,10 @@ conversation was used most recently. Select a Copilot entry and press **Shift+R**. Enter also starts recovery when its tmux session is gone. +Older entries that launch `copilot` but were labeled `claude` or `shell` are +recognized as Copilot when loaded. A Claude model selected inside Copilot does +not make it a Claude Code session or change where its history belongs. + If the entry has no stored Copilot conversation UUID, a recovery picker opens. It shows saved conversation names, UUIDs and update times. Type to filter, use Up/Down to navigate, and Tab to switch between the entry's folder and all folders. diff --git a/internal/session/copilot_test.go b/internal/session/copilot_test.go index 34a081e6..a07a2a1d 100644 --- a/internal/session/copilot_test.go +++ b/internal/session/copilot_test.go @@ -240,3 +240,63 @@ func TestCopilotPreparationIsStable(t *testing.T) { t.Fatal("preparation changed an already assigned identity") } } + +func TestCopilotLegacyToolClassificationMigration(t *testing.T) { + s := newTestStorage(t) + var entries []*Instance + for _, tool := range []string{"claude", "shell"} { + inst := NewInstanceWithTool("legacy-"+tool, "/shared/project", tool) + inst.Command = "copilot" + entries = append(entries, inst) + } + claude := NewInstanceWithTool("actual-claude", "/shared/project", "claude") + claude.Command = "claude" + entries = append(entries, claude) + if err := s.SaveWithGroups(entries, nil); err != nil { + t.Fatal(err) + } + lite, _, err := s.LoadLite() + if err != nil { + t.Fatal(err) + } + for _, entry := range lite { + if entry.Command == "copilot" && entry.Tool != "copilot" { + t.Fatalf("CLI load retained stale tool label: %+v", entry) + } + } + loaded, _, err := s.LoadWithGroups() + if err != nil { + t.Fatal(err) + } + for _, inst := range loaded { + if inst.Command == "claude" { + if inst.Tool != "claude" { + t.Fatal("migration changed an actual Claude session") + } + continue + } + if inst.Tool != "copilot" || inst.CopilotSessionID != "" || inst.CopilotSessionPending { + t.Fatalf("legacy entry was not migrated without inventing a conversation: %+v", inst) + } + if !errors.Is(inst.Restart(), ErrCopilotRecoveryRequired) { + t.Fatal("mislabeled legacy session bypassed recovery") + } + // Simulate a wrapper/model mention during status polling after reload. + inst.GetTmuxSession().Command = "env MODEL=claude copilot" + if got := inst.GetTmuxSession().ForceDetectTool(); got != "copilot" { + t.Fatalf("status polling changed Copilot back to %q", got) + } + } + if err := s.SaveWithGroups(loaded, nil); err != nil { + t.Fatal(err) + } + rows, err := s.db.LoadInstances() + if err != nil { + t.Fatal(err) + } + for _, row := range rows { + if row.Command == "copilot" && row.Tool != "copilot" { + t.Fatal("corrected CLI identity was not persisted") + } + } +} diff --git a/internal/session/instance.go b/internal/session/instance.go index 7fcc40f5..ea79c51b 100644 --- a/internal/session/instance.go +++ b/internal/session/instance.go @@ -1355,6 +1355,9 @@ func (i *Instance) loadCustomPatternsFromConfig() { // Keep detect patterns for DetectTool() (separate from busy/prompt detection) if toolDef := GetToolDef(i.Tool); toolDef != nil { i.tmuxSession.SetDetectPatterns(i.Tool, toolDef.DetectPatterns) + } else if i.Tool == "copilot" { + // A model banner (e.g. Claude) must not change which CLI owns the history. + i.tmuxSession.SetDetectPatterns("copilot", nil) } } diff --git a/internal/session/storage.go b/internal/session/storage.go index 2477a3c8..334062ae 100644 --- a/internal/session/storage.go +++ b/internal/session/storage.go @@ -402,6 +402,15 @@ func unixToTime(sec int64) time.Time { return time.Unix(sec, 0) } +func storedToolForCommand(tool, command string) string { + // Older releases classified Copilot as shell, or inferred Claude from its + // selected model. The launch executable is authoritative for these entries. + if tmux.IsCopilotCommand(command) { + return "copilot" + } + return tool +} + // LoadLite reads session data from SQLite without tmux reconnection. // This is a fast path for operations that only need to read session metadata // (e.g., finding current session by tmux name) without initializing full Instance objects. @@ -446,7 +455,7 @@ func (s *Storage) LoadLite() ([]*InstanceData, []*GroupData, error) { ParentSessionID: r.ParentSessionID, Command: r.Command, Wrapper: r.Wrapper, - Tool: r.Tool, + Tool: storedToolForCommand(r.Tool, r.Command), Status: Status(r.Status), CreatedAt: r.CreatedAt, LastAccessedAt: r.LastAccessed, @@ -536,7 +545,7 @@ func (s *Storage) LoadWithGroups() ([]*Instance, []*GroupData, error) { ParentSessionID: r.ParentSessionID, Command: r.Command, Wrapper: r.Wrapper, - Tool: r.Tool, + Tool: storedToolForCommand(r.Tool, r.Command), Status: Status(r.Status), CreatedAt: r.CreatedAt, LastAccessedAt: r.LastAccessed, @@ -708,7 +717,7 @@ func (s *Storage) convertToInstances(data *StorageData) ([]*Instance, []*GroupDa ParentSessionID: instData.ParentSessionID, Command: instData.Command, Wrapper: instData.Wrapper, - Tool: instData.Tool, + Tool: storedToolForCommand(instData.Tool, instData.Command), Status: instData.Status, CreatedAt: instData.CreatedAt, LastAccessedAt: instData.LastAccessedAt, @@ -741,6 +750,9 @@ func (s *Storage) convertToInstances(data *StorageData) ([]*Instance, []*GroupDa // Apply the persisted color scheme to the tmux session styling so a // reloaded session keeps its colors when next started/attached. applyColorSchemeToTmux(tmuxSess, instData.ColorScheme) + if inst.Tool == "copilot" { + inst.loadCustomPatternsFromConfig() + } // PERFORMANCE: Skip UpdateStatus at load time - use cached status from SQLite // The background worker will update status on first tick. diff --git a/internal/tmux/copilot_test.go b/internal/tmux/copilot_test.go new file mode 100644 index 00000000..d040719e --- /dev/null +++ b/internal/tmux/copilot_test.go @@ -0,0 +1,46 @@ +package tmux + +import ( + "testing" + "time" +) + +func TestIsCopilotCommand(t *testing.T) { + for _, tt := range []struct { + command string + want bool + }{ + {"copilot", true}, + {" copilot --resume=some-id ", true}, + {"/usr/local/bin/copilot --model claude-opus", true}, + {"claude", false}, + {"echo copilot", false}, + {"copilot-helper", false}, + {"", false}, + } { + if got := IsCopilotCommand(tt.command); got != tt.want { + t.Errorf("IsCopilotCommand(%q) = %v, want %v", tt.command, got, tt.want) + } + } +} + +func TestCopilotDetectionUsesCLIInsteadOfModel(t *testing.T) { + s := NewSession("copilot-detection", t.TempDir()) + for _, command := range []string{"copilot", "copilot --model claude-opus", "copilot --resume=test"} { + s.Command = command + if got := s.ForceDetectTool(); got != "copilot" { + t.Fatalf("%q detected as %q", command, got) + } + } +} + +func TestCopilotPinnedDetectionOverridesStaleModelDetection(t *testing.T) { + s := NewSession("copilot-wrapper", t.TempDir()) + s.Command = "env MODEL=claude copilot --resume=test" + s.detectedTool = "claude" + s.toolDetectedAt = time.Now() + s.SetDetectPatterns("copilot", nil) + if got := s.DetectTool(); got != "copilot" { + t.Fatalf("model detection overrode the pinned CLI: %q", got) + } +} diff --git a/internal/tmux/tmux.go b/internal/tmux/tmux.go index 5c6c746d..5c331659 100644 --- a/internal/tmux/tmux.go +++ b/internal/tmux/tmux.go @@ -659,6 +659,10 @@ func (s *Session) SetPatterns(p *ResolvedPatterns) { func (s *Session) SetDetectPatterns(toolName string, detectPatterns []string) { s.mu.Lock() defer s.mu.Unlock() + if s.customToolName != toolName { + s.detectedTool = "" + s.toolDetectedAt = time.Time{} + } s.customToolName = toolName s.customDetectPatterns = detectPatterns } @@ -1597,8 +1601,14 @@ func (s *Session) HasUpdated() (bool, error) { return false, nil } -// DetectTool detects which AI coding tool is running in the session -// Uses caching to avoid re-detection on every call +// IsCopilotCommand identifies the executable, not a model name or conversation text. +func IsCopilotCommand(command string) bool { + fields := strings.Fields(command) + return len(fields) > 0 && filepath.Base(fields[0]) == "copilot" +} + +// DetectTool detects which AI coding tool is running in the session. +// Uses caching to avoid re-detection on every call. func (s *Session) DetectTool() string { // Check cache first (read lock pattern for better concurrency) s.mu.Lock() @@ -1625,7 +1635,9 @@ func (s *Session) DetectTool() string { if s.Command != "" { cmdLower := strings.ToLower(s.Command) var tool string - if strings.Contains(cmdLower, "claude") { + if IsCopilotCommand(s.Command) { + tool = "copilot" + } else if strings.Contains(cmdLower, "claude") { tool = "claude" } else if strings.Contains(cmdLower, "gemini") { tool = "gemini"