Files
ai-workflow-skill/packages/inbox-runtime/internal/cli/inbox/claim.go
T

89 lines
2.2 KiB
Go

package inbox
import (
"errors"
"fmt"
"ai-workflow-skill/packages/coord-core/protocol"
"ai-workflow-skill/packages/coord-core/store"
"github.com/spf13/cobra"
)
type claimOptions struct {
agent string
threadID string
leaseSeconds int
}
func newClaimCmd(root *rootOptions) *cobra.Command {
opts := &claimOptions{}
cmd := &cobra.Command{
Use: "claim",
Short: "Acquire a lease on a pending thread",
Long: helpLong(
"Use claim to acquire the active worker lease for one thread.",
"claim is the step that turns a candidate thread into owned work.",
"After claim, the worker should inspect the thread, send an in_progress update when real work starts, and finish with done or fail.",
"Only one active lease may exist per thread at a time.",
),
Example: ` inbox --db .agents/coord.db claim --agent worker-a --thread thr_123
inbox --db .agents/coord.db claim --agent worker-a --thread thr_123 --lease-seconds 1800`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
agent := opts.agent
if agent == "" {
agent = root.agent
}
if agent == "" {
return protocol.InvalidInput("agent is required", nil)
}
sqlDB, err := openInboxDB(ctx, root.dbPath)
if err != nil {
return err
}
defer sqlDB.Close()
s := store.NewInboxStore(sqlDB)
result, err := s.ClaimThread(ctx, store.ClaimInput{
ThreadID: opts.threadID,
Agent: agent,
LeaseSeconds: opts.leaseSeconds,
})
if err != nil {
if errors.Is(err, store.ErrLeaseConflict) {
return fmt.Errorf("lease conflict: %w", err)
}
return err
}
resp := protocol.Success{
OK: true,
Command: "claim",
Data: map[string]any{
"thread": result.Thread,
"message": result.Message,
},
}
if root.json {
return protocol.WriteJSON(cmd.OutOrStdout(), resp)
}
_, err = fmt.Fprintf(cmd.OutOrStdout(), "claimed thread %s\n", result.Thread.ThreadID)
return err
},
}
cmd.Flags().StringVar(&opts.agent, "agent", "", "Claiming agent")
cmd.Flags().StringVar(&opts.threadID, "thread", "", "Thread ID")
cmd.Flags().IntVar(&opts.leaseSeconds, "lease-seconds", 900, "Lease duration in seconds")
_ = cmd.MarkFlagRequired("thread")
return cmd
}