feat(taskactions): retry a parked task on the current workflow definition

This commit is contained in:
sam committed 2026-10-17 13:35:12 +11:00
1 parent c80d4750a8
commit 9d7287c647
18 files changed
+134 -58

No files matched your search

+17 -5
View File
@@ -2074,9 +2074,12 @@ type ApplyOperatorTaskActionRequest struct {
// and chat carries no mode syntax -- an empty value means refresh_onto_base.
RetryMode string `protobuf:"bytes,8,opt,name=retry_mode,json=retryMode,proto3" json:"retry_mode,omitempty"`
// resume_from is the stage a retry starts at; empty runs every stage.
ResumeFrom string `protobuf:"bytes,9,opt,name=resume_from,json=resumeFrom,proto3" json:"resume_from,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
ResumeFrom string `protobuf:"bytes,9,opt,name=resume_from,json=resumeFrom,proto3" json:"resume_from,omitempty"`
// current_definition moves a retry onto the workflow's current stored
// definition instead of the one the run is pinned to.
CurrentDefinition bool `protobuf:"varint,10,opt,name=current_definition,json=currentDefinition,proto3" json:"current_definition,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *ApplyOperatorTaskActionRequest) Reset() {
@@ -2172,6 +2175,13 @@ func (x *ApplyOperatorTaskActionRequest) GetResumeFrom() string {
return ""
}
func (x *ApplyOperatorTaskActionRequest) GetCurrentDefinition() bool {
if x != nil {
return x.CurrentDefinition
}
return false
}
type ApplyOperatorTaskActionResponse struct {
state protoimpl.MessageState `protogen:"open.v1"`
TaskId int64 `protobuf:"varint,1,opt,name=task_id,json=taskId,proto3" json:"task_id,omitempty"`
@@ -3988,7 +3998,7 @@ const file_gateway_v1_chat_proto_rawDesc = "" +
"\x17ApplyTaskActionResponse\x12\x17\n" +
"\atask_id\x18\x01 \x01(\x03R\x06taskId\x12\x16\n" +
"\x06action\x18\x02 \x01(\tR\x06action\x12\x18\n" +
"\amessage\x18\x03 \x01(\tR\amessage\"\xae\x02\n" +
"\amessage\x18\x03 \x01(\tR\amessage\"\xdd\x02\n" +
"\x1eApplyOperatorTaskActionRequest\x12\x17\n" +
"\atask_id\x18\x01 \x01(\x03R\x06taskId\x12\x16\n" +
"\x06action\x18\x02 \x01(\tR\x06action\x12\x19\n" +
@@ -4001,7 +4011,9 @@ const file_gateway_v1_chat_proto_rawDesc = "" +
"\n" +
"retry_mode\x18\b \x01(\tR\tretryMode\x12\x1f\n" +
"\vresume_from\x18\t \x01(\tR\n" +
"resumeFrom\"l\n" +
"resumeFrom\x12-\n" +
"\x12current_definition\x18\n" +
" \x01(\bR\x11currentDefinition\"l\n" +
"\x1fApplyOperatorTaskActionResponse\x12\x17\n" +
"\atask_id\x18\x01 \x01(\x03R\x06taskId\x12\x16\n" +
"\x06action\x18\x02 \x01(\tR\x06action\x12\x18\n" +
+17 -6
View File
@@ -5746,10 +5746,13 @@ type RetryTaskRequest struct {
RetryMode string `protobuf:"bytes,4,opt,name=retry_mode,json=retryMode,proto3" json:"retry_mode,omitempty"`
// resume_from is the stage the attempt starts at, and resume_after the
// stage before it whose recorded results it starts with (task.Resume).
ResumeFrom string `protobuf:"bytes,5,opt,name=resume_from,json=resumeFrom,proto3" json:"resume_from,omitempty"`
ResumeAfter string `protobuf:"bytes,6,opt,name=resume_after,json=resumeAfter,proto3" json:"resume_after,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
ResumeFrom string `protobuf:"bytes,5,opt,name=resume_from,json=resumeFrom,proto3" json:"resume_from,omitempty"`
ResumeAfter string `protobuf:"bytes,6,opt,name=resume_after,json=resumeAfter,proto3" json:"resume_after,omitempty"`
// current_definition clears the pinned definition so the next dispatch
// pins the workflow's current stored version.
CurrentDefinition bool `protobuf:"varint,7,opt,name=current_definition,json=currentDefinition,proto3" json:"current_definition,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *RetryTaskRequest) Reset() {
@@ -5824,6 +5827,13 @@ func (x *RetryTaskRequest) GetResumeAfter() string {
return ""
}
func (x *RetryTaskRequest) GetCurrentDefinition() bool {
if x != nil {
return x.CurrentDefinition
}
return false
}
type RetryTaskResponse struct {
state protoimpl.MessageState `protogen:"open.v1"`
unknownFields protoimpl.UnknownFields
@@ -19825,7 +19835,7 @@ const file_state_v1_state_proto_rawDesc = "" +
"fromStatus\x12%\n" +
"\x05audit\x18\x03 \x01(\v2\x0f.state.v1.EventR\x05audit\"0\n" +
"\x13ArchiveTaskResponse\x12\x19\n" +
"\bevent_id\x18\x01 \x01(\x03R\aeventId\"\xcb\x01\n" +
"\bevent_id\x18\x01 \x01(\x03R\aeventId\"\xfa\x01\n" +
"\x10RetryTaskRequest\x12\x17\n" +
"\atask_id\x18\x01 \x01(\x03R\x06taskId\x12\x1f\n" +
"\vfrom_status\x18\x02 \x01(\tR\n" +
@@ -19835,7 +19845,8 @@ const file_state_v1_state_proto_rawDesc = "" +
"retry_mode\x18\x04 \x01(\tR\tretryMode\x12\x1f\n" +
"\vresume_from\x18\x05 \x01(\tR\n" +
"resumeFrom\x12!\n" +
"\fresume_after\x18\x06 \x01(\tR\vresumeAfter\"\x13\n" +
"\fresume_after\x18\x06 \x01(\tR\vresumeAfter\x12-\n" +
"\x12current_definition\x18\a \x01(\bR\x11currentDefinition\"\x13\n" +
"\x11RetryTaskResponse\"\x96\x01\n" +
"\x18RespondReviewGateRequest\x12\x17\n" +
"\atask_id\x18\x01 \x01(\x03R\x06taskId\x12\x1f\n" +
@@ -95,7 +95,7 @@ type TaskArchiver interface {
// is where the attempt starts; a resume whose After stage did not complete in
// its last run is refused with ErrResumeIncomplete.
type TaskRetryer interface {
RetryTask(ctx context.Context, taskID int64, fromStatus, workflow, retryMode string, resume task.Resume) error
RetryTask(ctx context.Context, taskID int64, fromStatus, workflow, retryMode string, resume task.Resume, currentDefinition bool) error
}
// ReviewGateResponder records the operator's review-gate answer. A re-review
+18 -4
View File
@@ -79,6 +79,9 @@ type ActionPayload struct {
// ResumeFrom is the stage a retry starts at, keeping the results of the
// stages before it. Empty runs every stage.
ResumeFrom string
// CurrentDefinition moves a retry onto the workflow's current stored
// definition rather than the version the run is pinned to.
CurrentDefinition bool
}
// Actor is who performed an action and the principal whose authority allowed
@@ -182,7 +185,9 @@ type Store interface {
Requeue(context.Context, int64, string, string) error
// RetryTask requeues a parked task, records the operator's worktree mode
// for the next dispatch and increments retry_count in one guarded write.
RetryTask(context.Context, int64, string, string, string, workflowtask.Resume) error
// currentDefinition drops the run's pinned definition so the next dispatch
// pins the workflow's current stored version.
RetryTask(context.Context, int64, string, string, string, workflowtask.Resume, bool) error
// RespondReviewGate is the guarded review gate response write
// (internal/domain/storecontract.ReviewGateResponder): it records the
// answered document, and for a re-review increments rereview_rounds and
@@ -440,7 +445,7 @@ func (s Service) applyRetry(ctx context.Context, task *Task, actor Actor, o outc
if !ok {
return o, fmt.Errorf("unknown retry mode %q: %w", requested, ErrConflict)
}
resume, err := resumePoint(task.Stages, payload.ResumeFrom)
resume, err := retryStart(task.Stages, payload)
if err != nil {
return o, fmt.Errorf("%w: %w", err, ErrConflict)
}
@@ -468,18 +473,27 @@ func (s Service) applyRetry(ctx context.Context, task *Task, actor Actor, o outc
}
return o, fmt.Errorf("%w: %s", ErrConflict, reason)
}
err = s.Store.RetryTask(ctx, task.ID, "parked", "", string(mode), resume)
err = s.Store.RetryTask(ctx, task.ID, "parked", "", string(mode), resume, payload.CurrentDefinition)
if errors.Is(err, storecontract.ErrResumeIncomplete) {
err = fmt.Errorf("%q did not complete in its last run, so there is nothing to resume from: %w", resume.After, ErrConflict)
}
o.event.Kind, o.event.Detail = events.KindTaskRetried, actor.describe("retried")
o.event.Data = map[string]any{"retry_count": task.RetryCount + 1, "previous_reason": task.ParkReason, "retry_mode": string(mode)}
o.event.Data = map[string]any{"retry_count": task.RetryCount + 1, "previous_reason": task.ParkReason, "retry_mode": string(mode), "current_definition": payload.CurrentDefinition}
if resume.From != "" {
o.event.Data["resume_from"] = resume.From
}
return o, err
}
// retryStart resolves where a retry starts. Resume stages name steps of the
// pinned definition, which a move to the current definition replaces.
func retryStart(stages []string, payload ActionPayload) (workflowtask.Resume, error) {
if payload.CurrentDefinition && payload.ResumeFrom != "" {
return workflowtask.Resume{}, errors.New("a retry on the current definition runs every step")
}
return resumePoint(stages, payload.ResumeFrom)
}
// resumePoint resolves a resume stage against the pinned definition's stages.
// Stage names are how runs are recorded, so a name two stages share cannot
// say which one to resume.
+10 -9
View File
@@ -97,15 +97,16 @@ func (c *Client) ApplyTaskAction(ctx context.Context, identity string, taskID in
func (c *Client) ApplyOperatorTaskAction(ctx context.Context, actor taskactions.Actor, taskID int64, action taskstate.Action, res taskactions.ActionPayload) (messaging.TaskActionResult, error) {
v, err := c.client.ApplyOperatorTaskAction(ctx, &pb.ApplyOperatorTaskActionRequest{
TaskId: taskID,
Action: string(action),
ActorId: string(actor.Identity),
ActorKind: string(actor.Kind),
PrincipalId: string(actor.Principal),
Instructions: res.Instructions,
Findings: res.Findings,
RetryMode: string(res.RetryMode),
ResumeFrom: res.ResumeFrom,
TaskId: taskID,
Action: string(action),
ActorId: string(actor.Identity),
ActorKind: string(actor.Kind),
PrincipalId: string(actor.Principal),
Instructions: res.Instructions,
Findings: res.Findings,
RetryMode: string(res.RetryMode),
ResumeFrom: res.ResumeFrom,
CurrentDefinition: res.CurrentDefinition,
})
if err != nil {
return messaging.TaskActionResult{}, taskActionError(err)
+5 -4
View File
@@ -116,10 +116,11 @@ func (s *server) ApplyOperatorTaskAction(ctx context.Context, r *pb.ApplyOperato
Principal: identity.IdentityID(r.PrincipalId),
}
v, err := s.chat.ApplyOperatorTaskAction(ctx, actor, r.TaskId, taskstate.Action(r.Action), taskactions.ActionPayload{
Instructions: r.Instructions,
Findings: r.Findings,
RetryMode: taskstate.RetryMode(r.RetryMode),
ResumeFrom: r.ResumeFrom,
Instructions: r.Instructions,
Findings: r.Findings,
RetryMode: taskstate.RetryMode(r.RetryMode),
ResumeFrom: r.ResumeFrom,
CurrentDefinition: r.CurrentDefinition,
})
if err != nil {
return nil, taskActionStatus(err)
@@ -890,17 +890,21 @@ UPDATE tasks SET status = 'queued', retry_count = retry_count + 1,
workflow = CASE WHEN $1::text = '' THEN workflow ELSE $1::text END,
retry_mode = $2::text,
resume_from = $3::text, resume_results = $4::jsonb,
workflow_definition_yaml = CASE WHEN $5::bool THEN '' ELSE workflow_definition_yaml END,
workflow_definition_digest = CASE WHEN $5::bool THEN '' ELSE workflow_definition_digest END,
workflow_definition_version = CASE WHEN $5::bool THEN 0 ELSE workflow_definition_version END,
park_reason = '', park_class = 'needs_human', updated_at = now()
WHERE id = $5 AND status = $6
WHERE id = $6 AND status = $7
`
type RetryTaskParams struct {
Workflow string
RetryMode string
ResumeFrom string
ResumeResults []byte
ID int64
FromStatus string
Workflow string
RetryMode string
ResumeFrom string
ResumeResults []byte
CurrentDefinition bool
ID int64
FromStatus string
}
func (q *Queries) RetryTask(ctx context.Context, arg RetryTaskParams) (int64, error) {
@@ -909,6 +913,7 @@ func (q *Queries) RetryTask(ctx context.Context, arg RetryTaskParams) (int64, er
arg.RetryMode,
arg.ResumeFrom,
arg.ResumeResults,
arg.CurrentDefinition,
arg.ID,
arg.FromStatus,
)
@@ -258,6 +258,9 @@ UPDATE tasks SET status = 'queued', retry_count = retry_count + 1,
workflow = CASE WHEN @workflow::text = '' THEN workflow ELSE @workflow::text END,
retry_mode = @retry_mode::text,
resume_from = @resume_from::text, resume_results = @resume_results::jsonb,
workflow_definition_yaml = CASE WHEN @current_definition::bool THEN '' ELSE workflow_definition_yaml END,
workflow_definition_digest = CASE WHEN @current_definition::bool THEN '' ELSE workflow_definition_digest END,
workflow_definition_version = CASE WHEN @current_definition::bool THEN 0 ELSE workflow_definition_version END,
park_reason = '', park_class = 'needs_human', updated_at = now()
WHERE id = @id AND status = @from_status;
+4 -2
View File
@@ -535,9 +535,11 @@ func (s *Store) Requeue(ctx context.Context, taskID int64, fromStatus, wf string
// transaction, under the same table check Requeue applies. retryMode is the
// operator's worktree choice for the next dispatch; the store persists it
// unread so the daemon's prepareWorkspace reads the mode the operator set.
// currentDefinition clears the pinned definition so the next dispatch pins
// the workflow's current stored version.
// A resume copies the results recorded after its After stage onto the row, in
// the same transaction, so the attempt starts from what that stage left.
func (s *Store) RetryTask(ctx context.Context, taskID int64, fromStatus, wf, retryMode string, resume task.Resume) error {
func (s *Store) RetryTask(ctx context.Context, taskID int64, fromStatus, wf, retryMode string, resume task.Resume, currentDefinition bool) error {
return s.requeue(ctx, taskID, fromStatus, wf, "retried "+wf, func(q *postgresdb.Queries) (int64, error) {
results, err := resumeResults(ctx, q, taskID, resume)
if err != nil {
@@ -545,7 +547,7 @@ func (s *Store) RetryTask(ctx context.Context, taskID int64, fromStatus, wf, ret
}
return q.RetryTask(ctx, postgresdb.RetryTaskParams{
ID: taskID, Workflow: wf, FromStatus: fromStatus, RetryMode: retryMode,
ResumeFrom: resume.From, ResumeResults: results,
ResumeFrom: resume.From, ResumeResults: results, CurrentDefinition: currentDefinition,
})
})
}
+2 -2
View File
@@ -260,10 +260,10 @@ func (c *Client) ArchiveTask(ctx context.Context, taskID int64, fromStatus strin
return r.EventId, nil
}
func (c *Client) RetryTask(ctx context.Context, taskID int64, fromStatus, wf, retryMode string, resume task.Resume) error {
func (c *Client) RetryTask(ctx context.Context, taskID int64, fromStatus, wf, retryMode string, resume task.Resume, currentDefinition bool) error {
_, err := c.client.RetryTask(ctx, &pb.RetryTaskRequest{
TaskId: taskID, FromStatus: fromStatus, Workflow: wf, RetryMode: retryMode,
ResumeFrom: resume.From, ResumeAfter: resume.After,
ResumeFrom: resume.From, ResumeAfter: resume.After, CurrentDefinition: currentDefinition,
})
return unmapError(err)
}
+1 -1
View File
@@ -458,7 +458,7 @@ func (s *server) ArchiveTask(ctx context.Context, r *pb.ArchiveTaskRequest) (*pb
}
func (s *server) RetryTask(ctx context.Context, r *pb.RetryTaskRequest) (*pb.RetryTaskResponse, error) {
if err := s.deps.Tasks.RetryTask(ctx, r.TaskId, r.FromStatus, r.Workflow, r.RetryMode, task.Resume{From: r.ResumeFrom, After: r.ResumeAfter}); err != nil {
if err := s.deps.Tasks.RetryTask(ctx, r.TaskId, r.FromStatus, r.Workflow, r.RetryMode, task.Resume{From: r.ResumeFrom, After: r.ResumeAfter}, r.CurrentDefinition); err != nil {
return nil, s.logErr("RetryTask", err)
}
return &pb.RetryTaskResponse{}, nil
+15 -11
View File
@@ -36,6 +36,8 @@ type actionRequest struct {
Findings []string `json:"findings,omitempty"`
RetryMode taskstate.RetryMode `json:"retry_mode,omitempty"`
ResumeFrom string `json:"resume_from,omitempty"`
// CurrentDefinition moves a retry onto the current stored definition.
CurrentDefinition bool `json:"current_definition,omitempty"`
}
type actorPayload struct {
@@ -118,10 +120,11 @@ func Register(nc *nats.Conn, service taskactions.Service, log *slog.Logger) (fun
return
}
err := service.Apply(context.Background(), req.Identity, req.Actor.actor(), req.TaskID, req.Action, taskactions.ActionPayload{
Instructions: req.Instructions,
Findings: req.Findings,
RetryMode: req.RetryMode,
ResumeFrom: req.ResumeFrom,
Instructions: req.Instructions,
Findings: req.Findings,
RetryMode: req.RetryMode,
ResumeFrom: req.ResumeFrom,
CurrentDefinition: req.CurrentDefinition,
})
natsrpc.Respond(msg, log, "taskactions", actionResponse{
Envelope: natsrpc.NewEnvelope(err),
@@ -149,13 +152,14 @@ func (c Client) ApplyChatTaskAction(ctx context.Context, scope *string, actor ta
return gateway.TaskActionResult{}, fmt.Errorf("task action connection is unavailable")
}
request := actionRequest{
Identity: scope,
TaskID: id,
Action: action,
Instructions: res.Instructions,
Findings: res.Findings,
RetryMode: res.RetryMode,
ResumeFrom: res.ResumeFrom,
Identity: scope,
TaskID: id,
Action: action,
Instructions: res.Instructions,
Findings: res.Findings,
RetryMode: res.RetryMode,
ResumeFrom: res.ResumeFrom,
CurrentDefinition: res.CurrentDefinition,
Actor: &actorPayload{
ID: string(actor.Identity),
Kind: string(actor.Kind),
+4 -1
View File
@@ -250,6 +250,9 @@ type taskActionRequest struct {
RetryMode string `json:"retry_mode"`
// ResumeFrom is the step a retry starts at; empty runs every step.
ResumeFrom string `json:"resume_from"`
// CurrentDefinition moves a retry onto the workflow's current stored
// definition.
CurrentDefinition bool `json:"current_definition"`
}
func (s *Server) handleTaskAction(w http.ResponseWriter, r *http.Request) {
@@ -313,7 +316,7 @@ func decodeTaskAction(w http.ResponseWriter, r *http.Request) (taskstate.Action,
http.Error(w, "a re-review needs instructions", http.StatusBadRequest)
return "", taskactions.ActionPayload{}, false
}
return action, taskactions.ActionPayload{Instructions: req.Instructions, Findings: req.Findings, RetryMode: taskstate.RetryMode(req.RetryMode), ResumeFrom: req.ResumeFrom}, true
return action, taskactions.ActionPayload{Instructions: req.Instructions, Findings: req.Findings, RetryMode: taskstate.RetryMode(req.RetryMode), ResumeFrom: req.ResumeFrom, CurrentDefinition: req.CurrentDefinition}, true
}
func taskMutation(action taskstate.Action) bool {