Files
ai-workflow-skill/packages/orch-runtime/internal/cli/orch/cancel.go
T

77 lines
2.0 KiB
Go

package orch
import (
"fmt"
"ai-workflow-skill/packages/coord-core/protocol"
"ai-workflow-skill/packages/coord-core/store"
"github.com/spf13/cobra"
)
type cancelOptions struct {
runID string
taskID string
reason string
}
func newCancelCmd(root *rootOptions) *cobra.Command {
opts := &cancelOptions{}
cmd := &cobra.Command{
Use: "cancel",
Short: "Cancel a task or an entire run",
Long: helpLong(
"Use cancel to stop one task or an entire run.",
"Pass --task when you want to cancel one task inside the run; omit it when you want to cancel the whole run.",
"Use a reason when later readers need to understand why the work was stopped.",
),
Example: ` orch --db .agents/coord.db cancel --run blog_mvp_001 --task T3 --reason "Superseded by a new plan."
orch --db .agents/coord.db cancel --run blog_mvp_001 --reason "User cancelled the entire request."`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
sqlDB, err := openOrchDB(ctx, root.dbPath)
if err != nil {
return err
}
defer sqlDB.Close()
result, err := store.NewOrchStore(sqlDB).Cancel(ctx, store.CancelControlInput{
RunID: opts.runID,
TaskID: opts.taskID,
Reason: opts.reason,
})
if err != nil {
return err
}
resp := protocol.Success{
OK: true,
Command: "cancel",
Data: map[string]any{
"run": result.Run,
"cancelled_tasks": result.CancelledTasks,
},
}
if root.json {
return protocol.WriteJSON(cmd.OutOrStdout(), resp)
}
if opts.taskID != "" {
_, err = fmt.Fprintf(cmd.OutOrStdout(), "cancelled task %s in run %s\n", opts.taskID, opts.runID)
return err
}
_, err = fmt.Fprintf(cmd.OutOrStdout(), "cancelled run %s (%d tasks)\n", opts.runID, len(result.CancelledTasks))
return err
},
}
cmd.Flags().StringVar(&opts.runID, "run", "", "Run ID")
cmd.Flags().StringVar(&opts.taskID, "task", "", "Optional task ID")
cmd.Flags().StringVar(&opts.reason, "reason", "", "Cancellation reason")
_ = cmd.MarkFlagRequired("run")
return cmd
}