feat: session lifecycle wrapper with manifest capture and session logging

Refactor new/resume/open to use session.Run() which wraps child process launch with pre/post manifest capture and append-only session logging to logs/sessions.log. Bump version to 0.2.0.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-06 09:56:21 -04:00
parent 57f345ae2b
commit 10ab9efc80
7 changed files with 435 additions and 32 deletions
+13 -14
View File
@@ -5,14 +5,15 @@ import (
"github.com/spf13/cobra"
"github.com/warrenronsiek/ctask/internal/config"
"github.com/warrenronsiek/ctask/internal/session"
"github.com/warrenronsiek/ctask/internal/shell"
"github.com/warrenronsiek/ctask/internal/workspace"
)
var newCmd = &cobra.Command{
Use: "new [title]",
Short: "Create a new task workspace and launch the agent",
Long: "Create a new task workspace and launch the agent. If title is omitted, generates task-HHMMSS.",
Use: "new [title]",
Short: "Create a new task workspace and launch the agent",
Long: "Create a new task workspace and launch the agent. If title is omitted, generates task-HHMMSS.",
Args: cobra.MaximumNArgs(1),
SilenceUsage: true,
RunE: runNew,
@@ -28,7 +29,7 @@ var (
func init() {
newCmd.Flags().StringVarP(&newCategory, "category", "c", "general", "Workspace category subdirectory")
newCmd.Flags().BoolVar(&newContainer, "container", false, "Launch in container sandbox (v0.2)")
newCmd.Flags().BoolVar(&newContainer, "container", false, "Launch in container sandbox (deferred)")
newCmd.Flags().BoolVar(&newShell, "shell", false, "Open interactive shell instead of agent")
newCmd.Flags().StringVarP(&newAgent, "agent", "a", "", "Command to exec as the agent")
newCmd.Flags().BoolVar(&newNoLaunch, "no-launch", false, "Create workspace only, do not launch")
@@ -72,14 +73,12 @@ func runNew(cmd *cobra.Command, args []string) error {
envVars := config.EnvVars(ws.Meta.Slug, ws.Meta.Mode, root, ws.Path, ws.Meta.Category)
if newShell {
return shell.ExecShell(ws.Path, envVars, ws.Meta.Slug, ws.Meta.Mode)
}
// Agent mode: print banner and exec
for _, line := range shell.BannerLines(ws.Meta.Mode, ws.Meta.Slug, ws.Path) {
fmt.Println(line)
}
return shell.ExecAgent(agent, ws.Path, envVars)
return session.Run(session.LaunchOpts{
WsDir: ws.Path,
EnvVars: envVars,
Agent: agent,
Mode: ws.Meta.Mode,
Slug: ws.Meta.Slug,
Shell: newShell,
})
}
+9 -3
View File
@@ -7,7 +7,7 @@ import (
"github.com/spf13/cobra"
"github.com/warrenronsiek/ctask/internal/config"
"github.com/warrenronsiek/ctask/internal/shell"
"github.com/warrenronsiek/ctask/internal/session"
"github.com/warrenronsiek/ctask/internal/workspace"
)
@@ -40,6 +40,12 @@ func runOpen(cmd *cobra.Command, args []string) error {
envVars := config.EnvVars(ws.Meta.Slug, ws.Meta.Mode, root, ws.Path, ws.Meta.Category)
// Spawn interactive subshell (not agent, not cd in parent)
return shell.ExecShell(ws.Path, envVars, ws.Meta.Slug, ws.Meta.Mode)
return session.Run(session.LaunchOpts{
WsDir: ws.Path,
EnvVars: envVars,
Agent: ws.Meta.Agent,
Mode: ws.Meta.Mode,
Slug: ws.Meta.Slug,
Shell: true, // open always launches shell
})
}
+18 -14
View File
@@ -7,6 +7,7 @@ import (
"github.com/spf13/cobra"
"github.com/warrenronsiek/ctask/internal/config"
"github.com/warrenronsiek/ctask/internal/session"
"github.com/warrenronsiek/ctask/internal/shell"
"github.com/warrenronsiek/ctask/internal/workspace"
)
@@ -26,20 +27,25 @@ var (
)
func init() {
resumeCmd.Flags().BoolVar(&resumeContainer, "container", false, "Resume in container mode (v0.2)")
resumeCmd.Flags().BoolVar(&resumeContainer, "container", false, "Resume in container mode (deferred)")
resumeCmd.Flags().BoolVar(&resumeShell, "shell", false, "Open shell instead of agent")
resumeCmd.Flags().StringVarP(&resumeAgent, "agent", "a", "", "Override agent command")
rootCmd.AddCommand(resumeCmd)
}
func runResume(cmd *cobra.Command, args []string) error {
if resumeContainer {
return doResume(args[0], resumeContainer, resumeShell, resumeAgent)
}
// doResume is the shared resume logic used by both resume and last commands.
func doResume(query string, container, useShell bool, agentOverride string) error {
if container {
fmt.Println(shell.ContainerNotice())
return nil
}
root := config.ResolveRoot()
ws := resolveOne(root, args[0], false)
ws := resolveOne(root, query, false)
// Update updated_at
now := time.Now().UTC().Truncate(time.Second)
@@ -49,21 +55,19 @@ func runResume(cmd *cobra.Command, args []string) error {
return fmt.Errorf("updating metadata: %w", err)
}
agent := resumeAgent
agent := agentOverride
if agent == "" {
agent = ws.Meta.Agent
}
envVars := config.EnvVars(ws.Meta.Slug, ws.Meta.Mode, root, ws.Path, ws.Meta.Category)
if resumeShell {
return shell.ExecShell(ws.Path, envVars, ws.Meta.Slug, ws.Meta.Mode)
}
// Agent mode
for _, line := range shell.BannerLines(ws.Meta.Mode, ws.Meta.Slug, ws.Path) {
fmt.Println(line)
}
return shell.ExecAgent(agent, ws.Path, envVars)
return session.Run(session.LaunchOpts{
WsDir: ws.Path,
EnvVars: envVars,
Agent: agent,
Mode: ws.Meta.Mode,
Slug: ws.Meta.Slug,
Shell: useShell,
})
}
+1 -1
View File
@@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"
)
var version = "0.1.0"
var version = "0.2.0"
var rootCmd = &cobra.Command{
Use: "ctask",