diff --git a/internal/app/archied/outcome_post.go b/internal/app/archied/outcome_post.go index e51785225..2bcba95ea 100644 --- a/internal/app/archied/outcome_post.go +++ b/internal/app/archied/outcome_post.go @@ -85,7 +85,7 @@ func postPendingOutcomes(ctx context.Context, tasks storecontract.OutcomePoster, continue } } - if err := tasks.MarkOutcomePosted(ctx, t.ID); err != nil { + if err := tasks.MarkOutcomePosted(ctx, t.ID, t.Status); err != nil { log.Warn("mark task outcome posted", "task", t.ID, "err", err) } if delivered { diff --git a/internal/app/archied/outcome_post_test.go b/internal/app/archied/outcome_post_test.go index da0f30ec0..b12aafd10 100644 --- a/internal/app/archied/outcome_post_test.go +++ b/internal/app/archied/outcome_post_test.go @@ -18,7 +18,7 @@ func (s *outcomeStore) UnpostedOutcomes(context.Context) ([]*task.Task, error) { return []*task.Task{{ID: 7, Origin: "web:chat-1"}}, nil } -func (s *outcomeStore) MarkOutcomePosted(_ context.Context, id int64) error { +func (s *outcomeStore) MarkOutcomePosted(_ context.Context, id int64, _ string) error { s.posted = append(s.posted, id) return nil } diff --git a/internal/contracts/state/v1/state.pb.go b/internal/contracts/state/v1/state.pb.go index fd60f3547..5fd11fadb 100644 --- a/internal/contracts/state/v1/state.pb.go +++ b/internal/contracts/state/v1/state.pb.go @@ -3830,8 +3830,10 @@ func (x *UnpostedOutcomesResponse) GetTasks() []*Task { } type MarkOutcomePostedRequest struct { - state protoimpl.MessageState `protogen:"open.v1"` - TaskId int64 `protobuf:"varint,1,opt,name=task_id,json=taskId,proto3" json:"task_id,omitempty"` + state protoimpl.MessageState `protogen:"open.v1"` + TaskId int64 `protobuf:"varint,1,opt,name=task_id,json=taskId,proto3" json:"task_id,omitempty"` + // status is the task status whose state was posted. + Status string `protobuf:"bytes,2,opt,name=status,proto3" json:"status,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } @@ -3873,6 +3875,13 @@ func (x *MarkOutcomePostedRequest) GetTaskId() int64 { return 0 } +func (x *MarkOutcomePostedRequest) GetStatus() string { + if x != nil { + return x.Status + } + return "" +} + type MarkOutcomePostedResponse struct { state protoimpl.MessageState `protogen:"open.v1"` unknownFields protoimpl.UnknownFields @@ -19858,9 +19867,10 @@ const file_state_v1_state_proto_rawDesc = "" + "\x05tasks\x18\x01 \x03(\v2\x0e.state.v1.TaskR\x05tasks\"\x19\n" + "\x17UnpostedOutcomesRequest\"@\n" + "\x18UnpostedOutcomesResponse\x12$\n" + - "\x05tasks\x18\x01 \x03(\v2\x0e.state.v1.TaskR\x05tasks\"3\n" + + "\x05tasks\x18\x01 \x03(\v2\x0e.state.v1.TaskR\x05tasks\"K\n" + "\x18MarkOutcomePostedRequest\x12\x17\n" + - "\atask_id\x18\x01 \x01(\x03R\x06taskId\"\x1b\n" + + "\atask_id\x18\x01 \x01(\x03R\x06taskId\x12\x16\n" + + "\x06status\x18\x02 \x01(\tR\x06status\"\x1b\n" + "\x19MarkOutcomePostedResponse\"\xfc\x01\n" + "\x16EnqueueChatTaskRequest\x12\x14\n" + "\x05owner\x18\x01 \x01(\tR\x05owner\x12\x12\n" + diff --git a/internal/domain/storecontract/storecontract.go b/internal/domain/storecontract/storecontract.go index 690715f04..424d0f3a4 100644 --- a/internal/domain/storecontract/storecontract.go +++ b/internal/domain/storecontract/storecontract.go @@ -129,8 +129,8 @@ type OutcomePoster interface { // UnpostedOutcomes returns finished chat tasks whose outcome has not been // posted to the conversation that created them. UnpostedOutcomes(ctx context.Context) ([]*task.Task, error) - // MarkOutcomePosted records that a task's outcome was posted. - MarkOutcomePosted(ctx context.Context, taskID int64) error + // MarkOutcomePosted records that the task's state at status was posted. + MarkOutcomePosted(ctx context.Context, taskID int64, status string) error } // TaskQueries groups read-only task accessors. diff --git a/internal/infrastructure/postgres/migrations/0056_outcome_posted_status.sql b/internal/infrastructure/postgres/migrations/0056_outcome_posted_status.sql new file mode 100644 index 000000000..e3c19bdf9 --- /dev/null +++ b/internal/infrastructure/postgres/migrations/0056_outcome_posted_status.sql @@ -0,0 +1,18 @@ +-- A chat task's conversation is told about each state worth reporting (an +-- approval request, an open pull request, a final outcome). Recording which +-- status was posted, rather than whether anything was, makes every status +-- change due for posting without each write path having to reset a flag. + +-- +goose Up +ALTER TABLE tasks ADD COLUMN outcome_posted_status text NOT NULL DEFAULT ''; +UPDATE tasks SET outcome_posted_status = status WHERE outcome_posted; +DROP INDEX IF EXISTS idx_tasks_outcome_unposted; +ALTER TABLE tasks DROP COLUMN outcome_posted; +CREATE INDEX idx_tasks_outcome_unposted ON tasks (id) WHERE origin <> '' AND outcome_posted_status <> status; + +-- +goose Down +DROP INDEX IF EXISTS idx_tasks_outcome_unposted; +ALTER TABLE tasks ADD COLUMN outcome_posted boolean NOT NULL DEFAULT false; +UPDATE tasks SET outcome_posted = outcome_posted_status = status; +ALTER TABLE tasks DROP COLUMN outcome_posted_status; +CREATE INDEX idx_tasks_outcome_unposted ON tasks (id) WHERE origin <> '' AND NOT outcome_posted; diff --git a/internal/infrastructure/postgres/postgresdb/call_tasks.sql.go b/internal/infrastructure/postgres/postgresdb/call_tasks.sql.go index 57674f062..b3b7e8b8e 100644 --- a/internal/infrastructure/postgres/postgresdb/call_tasks.sql.go +++ b/internal/infrastructure/postgres/postgresdb/call_tasks.sql.go @@ -24,7 +24,7 @@ func (q *Queries) CallStatusDetail(ctx context.Context, taskID int64) (string, e const enqueueCallTask = `-- name: EnqueueCallTask :one WITH caller AS ( - SELECT t.id, t.owner, t.repo, t.issue_number, t.title, t.body, t.labels, t.status, t.workflow, t.branch, t.plan, t.notes, t.pr_number, t.tokens_used, t.iterations, t.attempt, t.park_reason, t.watch_comment_id, t.park_class, t.remediation_rounds, t.retry_count, t.source, t.identity, t.binding_id, t.binding_version, t.review_payload, t.workflow_definition_version, t.workflow_definition_digest, t.workflow_definition_yaml, t.created_at, t.updated_at, t.review_cursor, t.inputs, t.org_id, t.workspace_id, t.call_parent_task_id, t.call_depth, t.outputs, t.review_gate, t.rereview_rounds, t.retry_mode, t.resume_from, t.resume_results, t.pending_reviews, t.origin, t.call_key, t.outcome_posted, t.binding_signature, t.binding_addr, t.park_blocker FROM tasks t WHERE t.id = $1 FOR UPDATE + SELECT t.id, t.owner, t.repo, t.issue_number, t.title, t.body, t.labels, t.status, t.workflow, t.branch, t.plan, t.notes, t.pr_number, t.tokens_used, t.iterations, t.attempt, t.park_reason, t.watch_comment_id, t.park_class, t.remediation_rounds, t.retry_count, t.source, t.identity, t.binding_id, t.binding_version, t.review_payload, t.workflow_definition_version, t.workflow_definition_digest, t.workflow_definition_yaml, t.created_at, t.updated_at, t.review_cursor, t.inputs, t.org_id, t.workspace_id, t.call_parent_task_id, t.call_depth, t.outputs, t.review_gate, t.rereview_rounds, t.retry_mode, t.resume_from, t.resume_results, t.pending_reviews, t.origin, t.call_key, t.binding_signature, t.binding_addr, t.park_blocker, t.outcome_posted_status FROM tasks t WHERE t.id = $1 FOR UPDATE ), number AS ( INSERT INTO chat_issue_numbers (owner, repo, last_number) SELECT caller.owner, caller.repo, $9::bigint + 1 FROM caller @@ -43,7 +43,7 @@ SELECT FROM caller, number WHERE caller.status = 'running' AND caller.call_depth + 1 <= $8::int ON CONFLICT (call_parent_task_id, call_key) WHERE call_key <> '' AND status NOT IN ('dead', 'closed_wont_do') DO UPDATE SET call_key = EXCLUDED.call_key -RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key, outcome_posted, binding_signature, binding_addr, park_blocker +RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key, binding_signature, binding_addr, park_blocker, outcome_posted_status ` type EnqueueCallTaskParams struct { @@ -132,10 +132,10 @@ func (q *Queries) EnqueueCallTask(ctx context.Context, arg EnqueueCallTaskParams &i.PendingReviews, &i.Origin, &i.CallKey, - &i.OutcomePosted, &i.BindingSignature, &i.BindingAddr, &i.ParkBlocker, + &i.OutcomePostedStatus, ) return i, err } diff --git a/internal/infrastructure/postgres/postgresdb/models.go b/internal/infrastructure/postgres/postgresdb/models.go index 0111dbe59..348c64638 100644 --- a/internal/infrastructure/postgres/postgresdb/models.go +++ b/internal/infrastructure/postgres/postgresdb/models.go @@ -538,10 +538,10 @@ type Task struct { PendingReviews []byte Origin string CallKey string - OutcomePosted bool BindingSignature string BindingAddr string ParkBlocker string + OutcomePostedStatus string } type ToolCall struct { diff --git a/internal/infrastructure/postgres/postgresdb/tasks.sql.go b/internal/infrastructure/postgres/postgresdb/tasks.sql.go index 9a12dbff4..7b0a5a709 100644 --- a/internal/infrastructure/postgres/postgresdb/tasks.sql.go +++ b/internal/infrastructure/postgres/postgresdb/tasks.sql.go @@ -13,7 +13,7 @@ import ( ) const activeTasksByOrigin = `-- name: ActiveTasksByOrigin :many -SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key, outcome_posted, binding_signature, binding_addr, park_blocker FROM tasks +SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key, binding_signature, binding_addr, park_blocker, outcome_posted_status FROM tasks WHERE origin = $1 AND origin <> '' AND status IN ('queued', 'running') AND ($2::text IS NULL OR org_id = $2) ORDER BY id ` @@ -80,10 +80,10 @@ func (q *Queries) ActiveTasksByOrigin(ctx context.Context, arg ActiveTasksByOrig &i.PendingReviews, &i.Origin, &i.CallKey, - &i.OutcomePosted, &i.BindingSignature, &i.BindingAddr, &i.ParkBlocker, + &i.OutcomePostedStatus, ); err != nil { return nil, err } @@ -140,7 +140,7 @@ func (q *Queries) ArchiveTaskDelete(ctx context.Context, arg ArchiveTaskDeletePa } const beginRemediationTask = `-- name: BeginRemediationTask :execrows -UPDATE tasks SET status = 'queued', workflow = 'remediate', park_reason = '', park_class = 'needs_human', review_payload = $2, outcome_posted = false, retry_mode = 'continue_pushed_work', resume_from = '', resume_results = '{}', updated_at = now() +UPDATE tasks SET status = 'queued', workflow = 'remediate', park_reason = '', park_class = 'needs_human', review_payload = $2, retry_mode = 'continue_pushed_work', resume_from = '', resume_results = '{}', updated_at = now() WHERE id = $1 AND status = 'pr_open' ` @@ -163,7 +163,7 @@ func (q *Queries) BeginRemediationTask(ctx context.Context, arg BeginRemediation const claimByIssue = `-- name: ClaimByIssue :one UPDATE tasks SET status = 'running', attempt = attempt + 1, outputs = '', updated_at = now() WHERE owner = $1 AND repo = $2 AND issue_number = $3 AND status = 'queued' -RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key, outcome_posted, binding_signature, binding_addr, park_blocker +RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key, binding_signature, binding_addr, park_blocker, outcome_posted_status ` type ClaimByIssueParams struct { @@ -224,10 +224,10 @@ func (q *Queries) ClaimByIssue(ctx context.Context, arg ClaimByIssueParams) (Tas &i.PendingReviews, &i.Origin, &i.CallKey, - &i.OutcomePosted, &i.BindingSignature, &i.BindingAddr, &i.ParkBlocker, + &i.OutcomePostedStatus, ) return i, err } @@ -242,7 +242,7 @@ WHERE id = ( FOR UPDATE SKIP LOCKED LIMIT 1 ) -RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key, outcome_posted, binding_signature, binding_addr, park_blocker +RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key, binding_signature, binding_addr, park_blocker, outcome_posted_status ` // FOR UPDATE SKIP LOCKED replaces the SQLite single-writer assumption: two @@ -301,10 +301,10 @@ func (q *Queries) ClaimNextTask(ctx context.Context) (Task, error) { &i.PendingReviews, &i.Origin, &i.CallKey, - &i.OutcomePosted, &i.BindingSignature, &i.BindingAddr, &i.ParkBlocker, + &i.OutcomePostedStatus, ) return i, err } @@ -422,7 +422,7 @@ FROM number, (SELECT COALESCE( 'org-sys' ) AS id) AS owner_org WHERE $11::text IS NULL OR owner_org.id = $11 -RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key, outcome_posted, binding_signature, binding_addr, park_blocker +RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key, binding_signature, binding_addr, park_blocker, outcome_posted_status ` type InsertChatTaskParams struct { @@ -515,10 +515,10 @@ func (q *Queries) InsertChatTask(ctx context.Context, arg InsertChatTaskParams) &i.PendingReviews, &i.Origin, &i.CallKey, - &i.OutcomePosted, &i.BindingSignature, &i.BindingAddr, &i.ParkBlocker, + &i.OutcomePostedStatus, ) return i, err } @@ -750,11 +750,17 @@ func (q *Queries) LockTaskStatus(ctx context.Context, id int64) (string, error) } const markOutcomePosted = `-- name: MarkOutcomePosted :exec -UPDATE tasks SET outcome_posted = true WHERE id = $1 +UPDATE tasks SET outcome_posted_status = $2 WHERE id = $1 ` -func (q *Queries) MarkOutcomePosted(ctx context.Context, id int64) error { - _, err := q.db.Exec(ctx, markOutcomePosted, id) +type MarkOutcomePostedParams struct { + ID int64 + OutcomePostedStatus string +} + +// Records the status whose state reached the task's conversation. +func (q *Queries) MarkOutcomePosted(ctx context.Context, arg MarkOutcomePostedParams) error { + _, err := q.db.Exec(ctx, markOutcomePosted, arg.ID, arg.OutcomePostedStatus) return err } @@ -788,7 +794,7 @@ func (q *Queries) ParkTask(ctx context.Context, arg ParkTaskParams) (int64, erro const promotePendingReview = `-- name: PromotePendingReview :execrows UPDATE tasks SET status = 'queued', workflow = 'remediate', park_reason = '', - park_class = 'needs_human', outcome_posted = false, + park_class = 'needs_human', review_payload = pending_reviews->>0, pending_reviews = pending_reviews - 0, retry_mode = 'continue_pushed_work', resume_from = '', resume_results = '{}', updated_at = now() @@ -992,7 +998,7 @@ func (q *Queries) StampTaskBinding(ctx context.Context, arg StampTaskBindingPara } const taskByID = `-- name: TaskByID :one -SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key, outcome_posted, binding_signature, binding_addr, park_blocker FROM tasks WHERE id = $1 AND ($2::text IS NULL OR org_id = $2) +SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key, binding_signature, binding_addr, park_blocker, outcome_posted_status FROM tasks WHERE id = $1 AND ($2::text IS NULL OR org_id = $2) ` type TaskByIDParams struct { @@ -1050,16 +1056,16 @@ func (q *Queries) TaskByID(ctx context.Context, arg TaskByIDParams) (Task, error &i.PendingReviews, &i.Origin, &i.CallKey, - &i.OutcomePosted, &i.BindingSignature, &i.BindingAddr, &i.ParkBlocker, + &i.OutcomePostedStatus, ) return i, err } const taskByIssue = `-- name: TaskByIssue :one -SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key, outcome_posted, binding_signature, binding_addr, park_blocker FROM tasks WHERE owner = $1 AND repo = $2 AND issue_number = $3 +SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key, binding_signature, binding_addr, park_blocker, outcome_posted_status FROM tasks WHERE owner = $1 AND repo = $2 AND issue_number = $3 AND ($4::text IS NULL OR org_id = $4) ` @@ -1125,16 +1131,16 @@ func (q *Queries) TaskByIssue(ctx context.Context, arg TaskByIssueParams) (Task, &i.PendingReviews, &i.Origin, &i.CallKey, - &i.OutcomePosted, &i.BindingSignature, &i.BindingAddr, &i.ParkBlocker, + &i.OutcomePostedStatus, ) return i, err } const taskByPR = `-- name: TaskByPR :one -SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key, outcome_posted, binding_signature, binding_addr, park_blocker FROM tasks WHERE owner = $1 AND repo = $2 AND pr_number = $3 AND status = $4 +SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key, binding_signature, binding_addr, park_blocker, outcome_posted_status FROM tasks WHERE owner = $1 AND repo = $2 AND pr_number = $3 AND status = $4 AND ($5::text IS NULL OR org_id = $5) ` @@ -1205,10 +1211,10 @@ func (q *Queries) TaskByPR(ctx context.Context, arg TaskByPRParams) (Task, error &i.PendingReviews, &i.Origin, &i.CallKey, - &i.OutcomePosted, &i.BindingSignature, &i.BindingAddr, &i.ParkBlocker, + &i.OutcomePostedStatus, ) return i, err } @@ -1216,7 +1222,6 @@ func (q *Queries) TaskByPR(ctx context.Context, arg TaskByPRParams) (Task, error const transitionTask = `-- name: TransitionTask :execrows UPDATE tasks SET status = $2, - outcome_posted = CASE WHEN status IN ('waiting_approval', 'pr_open') THEN false ELSE outcome_posted END, park_reason = CASE WHEN $2 = 'parked' THEN $3 ELSE park_reason END, park_class = CASE WHEN $2 = 'parked' THEN $4 ELSE park_class END, park_blocker = CASE WHEN $2 = 'parked' THEN '' ELSE park_blocker END, @@ -1234,8 +1239,6 @@ type TransitionTaskParams struct { // Arriving at 'parked' also records why. The class is normalized by the // caller, so an unknown value persists as needs_human rather than as itself. -// Leaving a state the conversation was told about (an approval request, an -// open pull request) clears outcome_posted, so the next state is posted too. func (q *Queries) TransitionTask(ctx context.Context, arg TransitionTaskParams) (int64, error) { result, err := q.db.Exec(ctx, transitionTask, arg.ID, @@ -1251,8 +1254,8 @@ func (q *Queries) TransitionTask(ctx context.Context, arg TransitionTaskParams) } const unpostedOutcomes = `-- name: UnpostedOutcomes :many -SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key, outcome_posted, binding_signature, binding_addr, park_blocker FROM tasks -WHERE origin <> '' AND NOT outcome_posted +SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key, binding_signature, binding_addr, park_blocker, outcome_posted_status FROM tasks +WHERE origin <> '' AND outcome_posted_status <> status AND status IN ('merged', 'rejected', 'dead', 'closed_wont_do', 'completed', 'waiting_approval', 'pr_open') AND ($1::text IS NULL OR org_id = $1) ORDER BY id @@ -1317,10 +1320,10 @@ func (q *Queries) UnpostedOutcomes(ctx context.Context, scopeOrg pgtype.Text) ([ &i.PendingReviews, &i.Origin, &i.CallKey, - &i.OutcomePosted, &i.BindingSignature, &i.BindingAddr, &i.ParkBlocker, + &i.OutcomePostedStatus, ); err != nil { return nil, err } diff --git a/internal/infrastructure/postgres/queries/tasks.sql b/internal/infrastructure/postgres/queries/tasks.sql index 5682a0055..dcab2ed99 100644 --- a/internal/infrastructure/postgres/queries/tasks.sql +++ b/internal/infrastructure/postgres/queries/tasks.sql @@ -105,14 +105,15 @@ ORDER BY id; -- Chat tasks that finished, wait for approval or have a pull request open, -- whose state has not reached their conversation yet. SELECT * FROM tasks -WHERE origin <> '' AND NOT outcome_posted +WHERE origin <> '' AND outcome_posted_status <> status AND status IN ('merged', 'rejected', 'dead', 'closed_wont_do', 'completed', 'waiting_approval', 'pr_open') AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org)) ORDER BY id LIMIT 50; -- name: MarkOutcomePosted :exec -UPDATE tasks SET outcome_posted = true WHERE id = $1; +-- Records the status whose state reached the task's conversation. +UPDATE tasks SET outcome_posted_status = $2 WHERE id = $1; -- name: StampTaskBinding :exec -- The capture's signature result is spelled as the access context spells it. @@ -152,11 +153,8 @@ SELECT status FROM tasks WHERE id = $1 FOR UPDATE; -- name: TransitionTask :execrows -- Arriving at 'parked' also records why. The class is normalized by the -- caller, so an unknown value persists as needs_human rather than as itself. --- Leaving a state the conversation was told about (an approval request, an --- open pull request) clears outcome_posted, so the next state is posted too. UPDATE tasks SET status = $2, - outcome_posted = CASE WHEN status IN ('waiting_approval', 'pr_open') THEN false ELSE outcome_posted END, park_reason = CASE WHEN $2 = 'parked' THEN $3 ELSE park_reason END, park_class = CASE WHEN $2 = 'parked' THEN $4 ELSE park_class END, park_blocker = CASE WHEN $2 = 'parked' THEN '' ELSE park_blocker END, @@ -210,7 +208,7 @@ RETURNING *; -- A remediation must continue the branch its pull request lives on, so it -- records that dispatch mode explicitly (taskstate.RetryContinuePushedWork) -- rather than leaving prepareWorkspace to infer it from the workflow. -UPDATE tasks SET status = 'queued', workflow = 'remediate', park_reason = '', park_class = 'needs_human', review_payload = $2, outcome_posted = false, retry_mode = 'continue_pushed_work', resume_from = '', resume_results = '{}', updated_at = now() +UPDATE tasks SET status = 'queued', workflow = 'remediate', park_reason = '', park_class = 'needs_human', review_payload = $2, retry_mode = 'continue_pushed_work', resume_from = '', resume_results = '{}', updated_at = now() WHERE id = $1 AND status = 'pr_open'; -- name: AppendPendingReview :execrows @@ -231,7 +229,7 @@ WHERE id = $1 AND status IN ('queued', 'running') AND workflow = 'remediate'; -- waiting: the oldest becomes the active payload and the task is queued -- again, so it is remediated after the run that just finished. UPDATE tasks SET status = 'queued', workflow = 'remediate', park_reason = '', - park_class = 'needs_human', outcome_posted = false, + park_class = 'needs_human', review_payload = pending_reviews->>0, pending_reviews = pending_reviews - 0, retry_mode = 'continue_pushed_work', resume_from = '', resume_results = '{}', updated_at = now() diff --git a/internal/infrastructure/postgres/store.go b/internal/infrastructure/postgres/store.go index f2e02c137..2ac2f9129 100644 --- a/internal/infrastructure/postgres/store.go +++ b/internal/infrastructure/postgres/store.go @@ -954,9 +954,10 @@ func (s *Store) UnpostedOutcomes(ctx context.Context) ([]*workflow.Task, error) return out, nil } -// MarkOutcomePosted records that a task's outcome reached its conversation. -func (s *Store) MarkOutcomePosted(ctx context.Context, taskID int64) error { - if err := s.queries().MarkOutcomePosted(ctx, taskID); err != nil { +// MarkOutcomePosted records that the task's state at status reached its +// conversation. +func (s *Store) MarkOutcomePosted(ctx context.Context, taskID int64, status string) error { + if err := s.queries().MarkOutcomePosted(ctx, postgresdb.MarkOutcomePostedParams{ID: taskID, OutcomePostedStatus: status}); err != nil { return fmt.Errorf("store: mark outcome posted: %w", err) } return nil diff --git a/internal/infrastructure/staterpc/client.go b/internal/infrastructure/staterpc/client.go index 4d172b439..6606669a7 100644 --- a/internal/infrastructure/staterpc/client.go +++ b/internal/infrastructure/staterpc/client.go @@ -98,8 +98,8 @@ func (c *Client) UnpostedOutcomes(ctx context.Context) ([]*task.Task, error) { return mapValues(r.Tasks, taskValue), nil } -func (c *Client) MarkOutcomePosted(ctx context.Context, taskID int64) error { - _, err := c.client.MarkOutcomePosted(ctx, &pb.MarkOutcomePostedRequest{TaskId: taskID}) +func (c *Client) MarkOutcomePosted(ctx context.Context, taskID int64, status string) error { + _, err := c.client.MarkOutcomePosted(ctx, &pb.MarkOutcomePostedRequest{TaskId: taskID, Status: status}) return unmapError(err) } diff --git a/internal/infrastructure/staterpc/server.go b/internal/infrastructure/staterpc/server.go index 827bf14c1..365f92b31 100644 --- a/internal/infrastructure/staterpc/server.go +++ b/internal/infrastructure/staterpc/server.go @@ -226,7 +226,7 @@ func (s *server) UnpostedOutcomes(ctx context.Context, _ *pb.UnpostedOutcomesReq } func (s *server) MarkOutcomePosted(ctx context.Context, r *pb.MarkOutcomePostedRequest) (*pb.MarkOutcomePostedResponse, error) { - if err := s.deps.Tasks.MarkOutcomePosted(ctx, r.TaskId); err != nil { + if err := s.deps.Tasks.MarkOutcomePosted(ctx, r.TaskId, r.Status); err != nil { return nil, s.logErr("MarkOutcomePosted", err) } return &pb.MarkOutcomePostedResponse{}, nil diff --git a/proto/state/v1/state.proto b/proto/state/v1/state.proto index b337aab8a..4eab022c8 100644 --- a/proto/state/v1/state.proto +++ b/proto/state/v1/state.proto @@ -713,7 +713,11 @@ message ActiveTasksByOriginResponse { repeated Task tasks = 1; } message UnpostedOutcomesRequest {} message UnpostedOutcomesResponse { repeated Task tasks = 1; } -message MarkOutcomePostedRequest { int64 task_id = 1; } +message MarkOutcomePostedRequest { + int64 task_id = 1; + // status is the task status whose state was posted. + string status = 2; +} message MarkOutcomePostedResponse {} message EnqueueChatTaskRequest {