87 lines
2.6 KiB
Go
87 lines
2.6 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 reassignOptions struct {
|
|
runID string
|
|
taskID string
|
|
toAgent string
|
|
reason string
|
|
}
|
|
|
|
func newReassignCmd(root *rootOptions) *cobra.Command {
|
|
opts := &reassignOptions{}
|
|
|
|
cmd := &cobra.Command{
|
|
Use: "reassign",
|
|
Short: "Reassign a blocked or failed task to another worker",
|
|
Long: helpLong(
|
|
"Use reassign to cancel the current active attempt and create a new attempt for another worker.",
|
|
"Like retry, reassign preserves the prior execution contract: analysis attempts stay analysis-only, and code attempts receive a fresh worktree.",
|
|
"Use reassign when the worker target should change, not just when the same worker should try again.",
|
|
),
|
|
Example: ` orch --db .agents/coord.db reassign --run blog_mvp_001 --task T3 --to worker-b --reason "Worker-a is blocked on a missing dependency."`,
|
|
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()
|
|
|
|
s := store.NewOrchStore(sqlDB)
|
|
task, attempt, err := s.GetTaskWithLatestAttempt(ctx, opts.runID, opts.taskID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
result, err := s.ReassignTask(ctx, store.ReassignInput{
|
|
RunID: opts.runID,
|
|
TaskID: opts.taskID,
|
|
ToAgent: opts.toAgent,
|
|
Reason: opts.reason,
|
|
PrepareWorkspace: newAttemptReuseWorkspacePreparer(cmd, task, attempt),
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
resp := protocol.Success{
|
|
OK: true,
|
|
Command: "reassign",
|
|
Data: map[string]any{
|
|
"task": result.Task,
|
|
"attempt": result.Attempt,
|
|
"thread": result.Thread,
|
|
"message": result.Message,
|
|
"previous_attempt": result.PreviousAttempt,
|
|
},
|
|
}
|
|
if root.json {
|
|
return protocol.WriteJSON(cmd.OutOrStdout(), resp)
|
|
}
|
|
|
|
_, err = fmt.Fprintf(cmd.OutOrStdout(), "reassigned task %s to %s as attempt %d\n", result.Task.TaskID, result.Attempt.AssignedTo, result.Attempt.AttemptNo)
|
|
return err
|
|
},
|
|
}
|
|
|
|
cmd.Flags().StringVar(&opts.runID, "run", "", "Run ID")
|
|
cmd.Flags().StringVar(&opts.taskID, "task", "", "Task ID")
|
|
cmd.Flags().StringVar(&opts.toAgent, "to", "", "Destination worker agent")
|
|
cmd.Flags().StringVar(&opts.reason, "reason", "", "Reason for reassignment")
|
|
_ = cmd.MarkFlagRequired("run")
|
|
_ = cmd.MarkFlagRequired("task")
|
|
_ = cmd.MarkFlagRequired("to")
|
|
|
|
return cmd
|
|
}
|