From ce742470b22268742474f232d46f0634c9ad847c Mon Sep 17 00:00:00 2001 From: typebasedio Date: Fri, 10 Apr 2026 17:00:42 -0400 Subject: [PATCH] refactor(v0.3): consolidate cmd/last and cmd/delete onto MostRecentActive Both commands now call workspace.MostRecentActive(root) directly instead of inlining a ListWorkspaces scan + max-by-UpdatedAt loop. The cross-type behavior is identical to before this commit (it was already correct after the v0.3 union fix), but it is now locked down by the focused unit tests added in the previous commit and there is no duplicated selection logic. The (nil, nil) "no active workspaces" return is mapped to: - cmd/last: prints "No active workspaces found." and exits 1 - cmd/delete: silently skips the "most recent" note (the user is deleting some specific workspace by name; the note was always best-effort) --- cmd/delete.go | 17 +++-------------- cmd/last.go | 22 +++------------------- 2 files changed, 6 insertions(+), 33 deletions(-) diff --git a/cmd/delete.go b/cmd/delete.go index 28f561d..3e0d727 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -70,20 +70,9 @@ func runDelete(cmd *cobra.Command, args []string) error { fmt.Printf(" Workspace: %s\n", relPath) fmt.Printf(" Files: %d (%s)\n", fileCount, formatSize(totalSize)) - // Check if this is the most recently updated workspace. - // Spans both tasks and projects via Type=TypeAny. - results, _ := workspace.ListWorkspaces(root, workspace.ListOpts{ - IncludeArchived: false, - Limit: 0, - Type: workspace.TypeAny, - }) - if len(results) > 0 { - best := results[0] - for _, r := range results[1:] { - if r.Meta.UpdatedAt.After(best.Meta.UpdatedAt) { - best = r - } - } + // Check if this is the most recently updated workspace across both + // tasks and projects. + if best, _ := workspace.MostRecentActive(root); best != nil { absBest, _ := filepath.Abs(best.Path) if strings.EqualFold(absTarget, absBest) { fmt.Println() diff --git a/cmd/last.go b/cmd/last.go index 096d742..e1a0bbc 100644 --- a/cmd/last.go +++ b/cmd/last.go @@ -11,7 +11,7 @@ import ( var lastCmd = &cobra.Command{ Use: "last", - Short: "Resume the most recently updated workspace", + Short: "Resume the most recently updated workspace (task or project)", Args: cobra.NoArgs, SilenceUsage: true, RunE: runLast, @@ -31,30 +31,14 @@ func init() { func runLast(cmd *cobra.Command, args []string) error { root := config.ResolveRoot() - // Scan all non-archived workspaces (tasks AND projects) and find the most - // recently updated. - results, err := workspace.ListWorkspaces(root, workspace.ListOpts{ - IncludeArchived: false, - Limit: 0, - Type: workspace.TypeAny, - }) + best, err := workspace.MostRecentActive(root) if err != nil { return err } - - if len(results) == 0 { + if best == nil { fmt.Fprintln(os.Stderr, "No active workspaces found.") os.Exit(1) } - // Find the one with the most recent updated_at - best := results[0] - for _, r := range results[1:] { - if r.Meta.UpdatedAt.After(best.Meta.UpdatedAt) { - best = r - } - } - - // Delegate to resume logic using the slug return doResume(best.Meta.Slug, false, lastShell, lastAgent) }