Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/agent-desk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions docs/copilot-recovery.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
60 changes: 60 additions & 0 deletions internal/session/copilot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}
3 changes: 3 additions & 0 deletions internal/session/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
18 changes: 15 additions & 3 deletions internal/session/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
46 changes: 46 additions & 0 deletions internal/tmux/copilot_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
18 changes: 15 additions & 3 deletions internal/tmux/tmux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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()
Expand All @@ -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"
Expand Down
Loading