package cmd import ( "fmt" "path/filepath" "time" "github.com/spf13/cobra" "github.com/warrenronsiek/ctask/internal/config" "github.com/warrenronsiek/ctask/internal/workspace" ) var attachCmd = &cobra.Command{ Use: "attach ", Short: "Attach to a workspace via tmux (always uses persistent session, regardless of CTASK_SESSION_MODE)", Args: cobra.ExactArgs(1), SilenceUsage: true, RunE: runAttach, } var ( attachAgent string attachForce bool ) func init() { attachCmd.Flags().StringVarP(&attachAgent, "agent", "a", "", "Override agent command") attachCmd.Flags().BoolVar(&attachForce, "force", false, "Skip active-session and stale-workspace warnings (owner-create path only)") attachCmd.ValidArgsFunction = completeWorkspaces(completionActive) rootCmd.AddCommand(attachCmd) } func runAttach(cmd *cobra.Command, args []string) error { roots := config.SearchRoots() // Active-only resolution per spec ยง9 (matches resume's completion filter). ws := resolveOne(roots, args[0], false) // updated_at bump (existing v0.4 behavior). now := time.Now().UTC().Truncate(time.Second) ws.Meta.UpdatedAt = now metaPath := filepath.Join(ws.Path, "task.yaml") if err := workspace.WriteMetaLocked(metaPath, ws.Meta); err != nil { return fmt.Errorf("updating metadata: %w", err) } resolved, err := resolveEntryAgent(ws.Meta.Agent, attachAgent) if err != nil { return err } return runWorkspaceEntry(WorkspaceEntryOptions{ WsPath: ws.Path, WsRoot: ws.Root, WsMeta: ws.Meta, ResolvedAgent: resolved, Shell: false, // attach defaults to agent Force: attachForce, AlwaysPersistent: true, // attach is always tmux, regardless of env CommandName: "attach", }) }