fix(postgres): queue a review that arrives during a remediation
A review reaction that landed while a remediate run owned the task failed the pr_open guard, was acked and dropped, and the poll cursor advanced past it. Runs against one task are serialised, so the unit is appended to tasks.pending_reviews instead; the transition that returns the task to pr_open promotes the oldest waiting review back to queued. A re-delivered unit is absorbed by payload equality, never queued twice.
This commit is contained in:
1 parent
70322bd5fb
commit
e0db40963e
9 files changed
+256
-15
No files matched your search
@@ -132,10 +132,10 @@ func (c *reactionConsumer) handleReview(ctx context.Context, msg eventbus.Messag
|
|||||||
}
|
}
|
||||||
if err := c.remediations.BeginRemediation(ctx, task.ID, payload); err != nil {
|
if err := c.remediations.BeginRemediation(ctx, task.ID, payload); err != nil {
|
||||||
if errors.Is(err, storecontract.ErrStaleTransition) {
|
if errors.Is(err, storecontract.ErrStaleTransition) {
|
||||||
// Already queued or claimed: a re-delivered review, or a second
|
// The task left the states a remediation can start or queue
|
||||||
// reaction for a review whose unit is pending. The guarded
|
// behind, so the reaction resolves to no work. A review that
|
||||||
// transition is the dedup.
|
// arrives mid-run is absorbed by the store, not dropped here.
|
||||||
c.log.Debug("remediation already pending", "task", task.ID, "review", e.ReviewID)
|
c.log.Debug("task does not accept remediation", "task", task.ID, "review", e.ReviewID)
|
||||||
_ = msg.Ack()
|
_ = msg.Ack()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -102,7 +102,10 @@ type ReviewGateResponder interface {
|
|||||||
|
|
||||||
// RemediationStarter queues a review-triggered remediation for an Archie-owned
|
// RemediationStarter queues a review-triggered remediation for an Archie-owned
|
||||||
// open-PR task: one guarded transition that carries the JSON-encoded review
|
// open-PR task: one guarded transition that carries the JSON-encoded review
|
||||||
// unit in the same write, so a claimed remediation always has its input.
|
// unit in the same write, so a claimed remediation always has its input. Runs
|
||||||
|
// for one task are serialised, so a distinct review that arrives while a
|
||||||
|
// remediation owns the task is queued behind it and remediated once that run
|
||||||
|
// finishes; a re-delivered unit is absorbed, not queued twice.
|
||||||
// UpdateReviewPayload appends late-arriving comments to a still-queued unit.
|
// UpdateReviewPayload appends late-arriving comments to a still-queued unit.
|
||||||
type RemediationStarter interface {
|
type RemediationStarter interface {
|
||||||
BeginRemediation(ctx context.Context, taskID int64, payload string) error
|
BeginRemediation(ctx context.Context, taskID int64, payload string) error
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
-- tasks.pending_reviews holds the review units that arrive while a
|
||||||
|
-- remediation already owns the task. Remediation runs for one task are
|
||||||
|
-- serialised (docs/prds/pr-review-remediation.md decision 5), so a distinct
|
||||||
|
-- review is appended here instead of dropped, and the oldest is promoted into
|
||||||
|
-- review_payload when the run that owned the task returns it to pr_open.
|
||||||
|
-- review_payload stays the active unit and is written by the run's own
|
||||||
|
-- Update; only this column carries the ones waiting behind it.
|
||||||
|
|
||||||
|
-- +goose Up
|
||||||
|
|
||||||
|
ALTER TABLE tasks ADD COLUMN pending_reviews jsonb NOT NULL DEFAULT '[]'::jsonb;
|
||||||
|
|
||||||
|
-- +goose Down
|
||||||
|
|
||||||
|
ALTER TABLE tasks DROP COLUMN pending_reviews;
|
||||||
@@ -24,7 +24,7 @@ func (q *Queries) CallStatusDetail(ctx context.Context, taskID int64) (string, e
|
|||||||
|
|
||||||
const enqueueCallTask = `-- name: EnqueueCallTask :one
|
const enqueueCallTask = `-- name: EnqueueCallTask :one
|
||||||
WITH caller AS (
|
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 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 FROM tasks t WHERE t.id = $1 FOR UPDATE
|
||||||
)
|
)
|
||||||
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, workflow, source, identity, org_id, workspace_id, inputs, call_parent_task_id, call_depth)
|
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, workflow, source, identity, org_id, workspace_id, inputs, call_parent_task_id, call_depth)
|
||||||
SELECT
|
SELECT
|
||||||
@@ -41,7 +41,7 @@ SELECT
|
|||||||
caller.id, caller.call_depth + 1
|
caller.id, caller.call_depth + 1
|
||||||
FROM caller
|
FROM caller
|
||||||
WHERE caller.status = 'running' AND caller.call_depth + 1 <= $7::int
|
WHERE caller.status = 'running' AND caller.call_depth + 1 <= $7::int
|
||||||
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
|
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
|
||||||
`
|
`
|
||||||
|
|
||||||
type EnqueueCallTaskParams struct {
|
type EnqueueCallTaskParams struct {
|
||||||
@@ -110,6 +110,7 @@ func (q *Queries) EnqueueCallTask(ctx context.Context, arg EnqueueCallTaskParams
|
|||||||
&i.RetryMode,
|
&i.RetryMode,
|
||||||
&i.ResumeFrom,
|
&i.ResumeFrom,
|
||||||
&i.ResumeResults,
|
&i.ResumeResults,
|
||||||
|
&i.PendingReviews,
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
@@ -422,6 +422,7 @@ type Task struct {
|
|||||||
RetryMode string
|
RetryMode string
|
||||||
ResumeFrom string
|
ResumeFrom string
|
||||||
ResumeResults []byte
|
ResumeResults []byte
|
||||||
|
PendingReviews []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
type ToolCall struct {
|
type ToolCall struct {
|
||||||
|
|||||||
@@ -10,6 +10,33 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const appendPendingReview = `-- name: AppendPendingReview :execrows
|
||||||
|
UPDATE tasks SET pending_reviews = CASE
|
||||||
|
WHEN review_payload = $2::text THEN pending_reviews
|
||||||
|
WHEN pending_reviews @> jsonb_build_array($2::text) THEN pending_reviews
|
||||||
|
ELSE pending_reviews || jsonb_build_array($2::text)
|
||||||
|
END,
|
||||||
|
updated_at = now()
|
||||||
|
WHERE id = $1 AND status IN ('queued', 'running') AND workflow = 'remediate'
|
||||||
|
`
|
||||||
|
|
||||||
|
type AppendPendingReviewParams struct {
|
||||||
|
ID int64
|
||||||
|
Column2 string
|
||||||
|
}
|
||||||
|
|
||||||
|
// A distinct review that arrived while a remediation owned the task. The two
|
||||||
|
// containment tests are the dedup: a re-delivered reaction, or a poll that
|
||||||
|
// re-publishes a review already queued, appends nothing, and one already
|
||||||
|
// active in review_payload is not queued to run a second time.
|
||||||
|
func (q *Queries) AppendPendingReview(ctx context.Context, arg AppendPendingReviewParams) (int64, error) {
|
||||||
|
result, err := q.db.Exec(ctx, appendPendingReview, arg.ID, arg.Column2)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return result.RowsAffected(), nil
|
||||||
|
}
|
||||||
|
|
||||||
const archiveTaskDelete = `-- name: ArchiveTaskDelete :execrows
|
const archiveTaskDelete = `-- name: ArchiveTaskDelete :execrows
|
||||||
DELETE FROM tasks WHERE id = $1 AND status = $2
|
DELETE FROM tasks WHERE id = $1 AND status = $2
|
||||||
`
|
`
|
||||||
@@ -51,7 +78,7 @@ func (q *Queries) BeginRemediationTask(ctx context.Context, arg BeginRemediation
|
|||||||
const claimByIssue = `-- name: ClaimByIssue :one
|
const claimByIssue = `-- name: ClaimByIssue :one
|
||||||
UPDATE tasks SET status = 'running', attempt = attempt + 1, outputs = '', updated_at = now()
|
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'
|
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
|
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
|
||||||
`
|
`
|
||||||
|
|
||||||
type ClaimByIssueParams struct {
|
type ClaimByIssueParams struct {
|
||||||
@@ -109,6 +136,7 @@ func (q *Queries) ClaimByIssue(ctx context.Context, arg ClaimByIssueParams) (Tas
|
|||||||
&i.RetryMode,
|
&i.RetryMode,
|
||||||
&i.ResumeFrom,
|
&i.ResumeFrom,
|
||||||
&i.ResumeResults,
|
&i.ResumeResults,
|
||||||
|
&i.PendingReviews,
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
@@ -123,7 +151,7 @@ WHERE id = (
|
|||||||
FOR UPDATE SKIP LOCKED
|
FOR UPDATE SKIP LOCKED
|
||||||
LIMIT 1
|
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
|
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
|
||||||
`
|
`
|
||||||
|
|
||||||
// FOR UPDATE SKIP LOCKED replaces the SQLite single-writer assumption: two
|
// FOR UPDATE SKIP LOCKED replaces the SQLite single-writer assumption: two
|
||||||
@@ -179,6 +207,7 @@ func (q *Queries) ClaimNextTask(ctx context.Context) (Task, error) {
|
|||||||
&i.RetryMode,
|
&i.RetryMode,
|
||||||
&i.ResumeFrom,
|
&i.ResumeFrom,
|
||||||
&i.ResumeResults,
|
&i.ResumeResults,
|
||||||
|
&i.PendingReviews,
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
@@ -289,7 +318,7 @@ VALUES (
|
|||||||
),
|
),
|
||||||
$8
|
$8
|
||||||
)
|
)
|
||||||
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
|
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
|
||||||
`
|
`
|
||||||
|
|
||||||
type InsertChatTaskParams struct {
|
type InsertChatTaskParams struct {
|
||||||
@@ -366,6 +395,7 @@ func (q *Queries) InsertChatTask(ctx context.Context, arg InsertChatTaskParams)
|
|||||||
&i.RetryMode,
|
&i.RetryMode,
|
||||||
&i.ResumeFrom,
|
&i.ResumeFrom,
|
||||||
&i.ResumeResults,
|
&i.ResumeResults,
|
||||||
|
&i.PendingReviews,
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
@@ -572,6 +602,27 @@ func (q *Queries) ParkTask(ctx context.Context, arg ParkTaskParams) (int64, erro
|
|||||||
return result.RowsAffected(), nil
|
return result.RowsAffected(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const promotePendingReview = `-- name: PromotePendingReview :execrows
|
||||||
|
UPDATE tasks SET status = 'queued', workflow = 'remediate', park_reason = '',
|
||||||
|
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()
|
||||||
|
WHERE id = $1 AND status = 'pr_open' AND workflow = 'remediate'
|
||||||
|
AND jsonb_array_length(pending_reviews) > 0
|
||||||
|
`
|
||||||
|
|
||||||
|
// The run that owned the task returned it to pr_open with reviews still
|
||||||
|
// waiting: the oldest becomes the active payload and the task is queued
|
||||||
|
// again, so it is remediated after the run that just finished.
|
||||||
|
func (q *Queries) PromotePendingReview(ctx context.Context, id int64) (int64, error) {
|
||||||
|
result, err := q.db.Exec(ctx, promotePendingReview, id)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return result.RowsAffected(), nil
|
||||||
|
}
|
||||||
|
|
||||||
const recoverStaleTasks = `-- name: RecoverStaleTasks :execrows
|
const recoverStaleTasks = `-- name: RecoverStaleTasks :execrows
|
||||||
UPDATE tasks SET status = 'queued', updated_at = now() WHERE status = 'running'
|
UPDATE tasks SET status = 'queued', updated_at = now() WHERE status = 'running'
|
||||||
`
|
`
|
||||||
@@ -724,7 +775,7 @@ func (q *Queries) StampTaskBinding(ctx context.Context, arg StampTaskBindingPara
|
|||||||
}
|
}
|
||||||
|
|
||||||
const taskByID = `-- name: TaskByID :one
|
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 FROM tasks WHERE id = $1
|
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 FROM tasks WHERE id = $1
|
||||||
`
|
`
|
||||||
|
|
||||||
func (q *Queries) TaskByID(ctx context.Context, id int64) (Task, error) {
|
func (q *Queries) TaskByID(ctx context.Context, id int64) (Task, error) {
|
||||||
@@ -774,12 +825,13 @@ func (q *Queries) TaskByID(ctx context.Context, id int64) (Task, error) {
|
|||||||
&i.RetryMode,
|
&i.RetryMode,
|
||||||
&i.ResumeFrom,
|
&i.ResumeFrom,
|
||||||
&i.ResumeResults,
|
&i.ResumeResults,
|
||||||
|
&i.PendingReviews,
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
|
||||||
const taskByIssue = `-- name: TaskByIssue :one
|
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 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 FROM tasks WHERE owner = $1 AND repo = $2 AND issue_number = $3
|
||||||
`
|
`
|
||||||
|
|
||||||
type TaskByIssueParams struct {
|
type TaskByIssueParams struct {
|
||||||
@@ -835,12 +887,13 @@ func (q *Queries) TaskByIssue(ctx context.Context, arg TaskByIssueParams) (Task,
|
|||||||
&i.RetryMode,
|
&i.RetryMode,
|
||||||
&i.ResumeFrom,
|
&i.ResumeFrom,
|
||||||
&i.ResumeResults,
|
&i.ResumeResults,
|
||||||
|
&i.PendingReviews,
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
|
||||||
const taskByPR = `-- name: TaskByPR :one
|
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 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 FROM tasks WHERE owner = $1 AND repo = $2 AND pr_number = $3 AND status = $4
|
||||||
`
|
`
|
||||||
|
|
||||||
type TaskByPRParams struct {
|
type TaskByPRParams struct {
|
||||||
@@ -905,6 +958,7 @@ func (q *Queries) TaskByPR(ctx context.Context, arg TaskByPRParams) (Task, error
|
|||||||
&i.RetryMode,
|
&i.RetryMode,
|
||||||
&i.ResumeFrom,
|
&i.ResumeFrom,
|
||||||
&i.ResumeResults,
|
&i.ResumeResults,
|
||||||
|
&i.PendingReviews,
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -158,6 +158,31 @@ RETURNING *;
|
|||||||
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()
|
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';
|
WHERE id = $1 AND status = 'pr_open';
|
||||||
|
|
||||||
|
-- name: AppendPendingReview :execrows
|
||||||
|
-- A distinct review that arrived while a remediation owned the task. The two
|
||||||
|
-- containment tests are the dedup: a re-delivered reaction, or a poll that
|
||||||
|
-- re-publishes a review already queued, appends nothing, and one already
|
||||||
|
-- active in review_payload is not queued to run a second time.
|
||||||
|
UPDATE tasks SET pending_reviews = CASE
|
||||||
|
WHEN review_payload = $2::text THEN pending_reviews
|
||||||
|
WHEN pending_reviews @> jsonb_build_array($2::text) THEN pending_reviews
|
||||||
|
ELSE pending_reviews || jsonb_build_array($2::text)
|
||||||
|
END,
|
||||||
|
updated_at = now()
|
||||||
|
WHERE id = $1 AND status IN ('queued', 'running') AND workflow = 'remediate';
|
||||||
|
|
||||||
|
-- name: PromotePendingReview :execrows
|
||||||
|
-- The run that owned the task returned it to pr_open with reviews still
|
||||||
|
-- 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',
|
||||||
|
review_payload = pending_reviews->>0, pending_reviews = pending_reviews - 0,
|
||||||
|
retry_mode = 'continue_pushed_work', resume_from = '', resume_results = '{}',
|
||||||
|
updated_at = now()
|
||||||
|
WHERE id = $1 AND status = 'pr_open' AND workflow = 'remediate'
|
||||||
|
AND jsonb_array_length(pending_reviews) > 0;
|
||||||
|
|
||||||
-- name: UpdateReviewPayloadTask :execrows
|
-- name: UpdateReviewPayloadTask :execrows
|
||||||
UPDATE tasks SET review_payload = $2, updated_at = now()
|
UPDATE tasks SET review_payload = $2, updated_at = now()
|
||||||
WHERE id = $1 AND status = 'queued' AND workflow = 'remediate';
|
WHERE id = $1 AND status = 'queued' AND workflow = 'remediate';
|
||||||
|
|||||||
@@ -0,0 +1,106 @@
|
|||||||
|
package postgres_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||||
|
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||||
|
"github.com/samcharles93/archie-core/internal/taskstate"
|
||||||
|
)
|
||||||
|
|
||||||
|
// A review that arrives while a remediation owns the task is queued behind it
|
||||||
|
// and remediated when that run finishes, in arrival order. A re-delivered
|
||||||
|
// review is absorbed rather than queued to run a second time.
|
||||||
|
func TestSecondReviewDuringRemediationIsQueued(t *testing.T) {
|
||||||
|
ctx := t.Context()
|
||||||
|
db := pgstore.Open(t)
|
||||||
|
id := openTaskAtPROpen(t, db)
|
||||||
|
|
||||||
|
first, second := reviewUnit(t, 11), reviewUnit(t, 22)
|
||||||
|
|
||||||
|
if err := db.BeginRemediation(ctx, id, first); err != nil {
|
||||||
|
t.Fatalf("begin first remediation: %v", err)
|
||||||
|
}
|
||||||
|
if err := db.BeginRemediation(ctx, id, second); err != nil {
|
||||||
|
t.Fatalf("review arriving during remediation was refused, not queued: %v", err)
|
||||||
|
}
|
||||||
|
if err := db.BeginRemediation(ctx, id, first); err != nil {
|
||||||
|
t.Fatalf("re-delivered review was refused: %v", err)
|
||||||
|
}
|
||||||
|
assertActiveReview(t, db, id, first)
|
||||||
|
|
||||||
|
// The first round runs and finishes; the second, still waiting, is
|
||||||
|
// promoted behind it.
|
||||||
|
runRemediationRound(t, db, id)
|
||||||
|
assertActiveReview(t, db, id, second)
|
||||||
|
|
||||||
|
// The second round runs and finds nothing left waiting.
|
||||||
|
runRemediationRound(t, db, id)
|
||||||
|
task, err := db.TaskByID(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if task.Status != taskstate.PROpen {
|
||||||
|
t.Fatalf("status after both rounds = %q, want %q (a review was queued twice)", task.Status, taskstate.PROpen)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// openTaskAtPROpen enqueues a task, claims it and opens its pull request.
|
||||||
|
func openTaskAtPROpen(t *testing.T, db *pgstore.TaskDB) int64 {
|
||||||
|
t.Helper()
|
||||||
|
ctx := t.Context()
|
||||||
|
created, err := db.EnqueueChatTask(ctx, "acme", "widget", "a task", "", "tdd", "", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := db.ClaimNext(ctx); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := db.Transition(ctx, created.ID, taskstate.Running, taskstate.PROpen, "opened the pull request"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
return created.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
// runRemediationRound claims the queued task and finishes one round of it.
|
||||||
|
func runRemediationRound(t *testing.T, db *pgstore.TaskDB, id int64) {
|
||||||
|
t.Helper()
|
||||||
|
ctx := t.Context()
|
||||||
|
run, err := db.ClaimNext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if run == nil || run.ID != id {
|
||||||
|
t.Fatalf("claim: got %v, want task %d queued", run, id)
|
||||||
|
}
|
||||||
|
if err := db.Transition(ctx, id, taskstate.Running, taskstate.PROpen, "remediated a review round"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// assertActiveReview asserts the task is queued on payload, which is the
|
||||||
|
// review a remediation would run next.
|
||||||
|
func assertActiveReview(t *testing.T, db *pgstore.TaskDB, id int64, payload string) {
|
||||||
|
t.Helper()
|
||||||
|
task, err := db.TaskByID(t.Context(), id)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if task.Status != taskstate.Queued || task.Workflow != "remediate" {
|
||||||
|
t.Fatalf("task is %q on workflow %q, want a queued remediation", task.Status, task.Workflow)
|
||||||
|
}
|
||||||
|
if task.ReviewPayload != payload {
|
||||||
|
t.Fatalf("active review = %s, want %s", task.ReviewPayload, payload)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func reviewUnit(t *testing.T, reviewID int64) string {
|
||||||
|
t.Helper()
|
||||||
|
payload, err := workflow.EncodeReviewUnit(workflow.ReviewUnit{
|
||||||
|
ReviewID: reviewID, State: "requested_changes", Body: "please change this",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
return payload
|
||||||
|
}
|
||||||
@@ -241,6 +241,24 @@ func (s *Store) Transition(ctx context.Context, taskID int64, from, to, detail s
|
|||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
// A remediate run that finished with reviews still waiting behind it
|
||||||
|
// hands the task straight back to the queue, oldest first. The run itself
|
||||||
|
// cannot know they are there: they arrived while it held the task, and
|
||||||
|
// only the state store owns both halves of the queue.
|
||||||
|
if to == workflow.StatusPROpen {
|
||||||
|
promoted, err := q.PromotePendingReview(ctx, taskID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if promoted > 0 {
|
||||||
|
if err := q.InsertTransition(ctx, postgresdb.InsertTransitionParams{
|
||||||
|
TaskID: taskID, FromStatus: workflow.StatusPROpen, ToStatus: workflow.StatusQueued,
|
||||||
|
Detail: "queued the next review",
|
||||||
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return tx.Commit(ctx)
|
return tx.Commit(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -312,7 +330,10 @@ func (s *Store) Update(ctx context.Context, t *workflow.Task) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// BeginRemediation queues a remediate run carrying the review unit in the same
|
// BeginRemediation queues a remediate run carrying the review unit in the same
|
||||||
// guarded write, so a claimed remediation always has its input.
|
// guarded write, so a claimed remediation always has its input. When a
|
||||||
|
// remediation already owns the task, the unit is queued behind the run in
|
||||||
|
// flight instead of refused: a review that arrives mid-remediation is
|
||||||
|
// remediated after it, never dropped.
|
||||||
func (s *Store) BeginRemediation(ctx context.Context, taskID int64, payload string) error {
|
func (s *Store) BeginRemediation(ctx context.Context, taskID int64, payload string) error {
|
||||||
tx, err := s.pool.Begin(ctx)
|
tx, err := s.pool.Begin(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -322,7 +343,22 @@ func (s *Store) BeginRemediation(ctx context.Context, taskID int64, payload stri
|
|||||||
q := postgresdb.New(tx)
|
q := postgresdb.New(tx)
|
||||||
|
|
||||||
if err := guardTransition(ctx, q, taskID, workflow.StatusPROpen, workflow.StatusQueued); err != nil {
|
if err := guardTransition(ctx, q, taskID, workflow.StatusPROpen, workflow.StatusQueued); err != nil {
|
||||||
return err
|
if !errors.Is(err, storecontract.ErrStaleTransition) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// A remediation already owns the task. Runs against one task are
|
||||||
|
// serialised, so queue the review behind the current one rather than
|
||||||
|
// dropping it; Transition promotes it when that run finishes.
|
||||||
|
n, err := q.AppendPendingReview(ctx, postgresdb.AppendPendingReviewParams{
|
||||||
|
ID: taskID, Column2: clip(payload, 4000),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
return storecontract.ErrStaleTransition
|
||||||
|
}
|
||||||
|
return tx.Commit(ctx)
|
||||||
}
|
}
|
||||||
n, err := q.BeginRemediationTask(ctx, postgresdb.BeginRemediationTaskParams{
|
n, err := q.BeginRemediationTask(ctx, postgresdb.BeginRemediationTaskParams{
|
||||||
ID: taskID, ReviewPayload: clip(payload, 4000),
|
ID: taskID, ReviewPayload: clip(payload, 4000),
|
||||||
|
|||||||
Reference in new issue
Block a user