feat(staterpc): carry the review gate and re-review rounds, with one guarded response write
The operator-approval gate needs its conversation with the operator persisted before it waits and its answer threaded back (docs/prds/pr-review-operator-response.md, Decision 2). review_gate is a text column beside review_payload, encoded by the package that owns the type; rereview_rounds is a bigint counted only by the new guarded write. RespondReviewGate is the one write beside RetryTask/BeginRemediation: a transition-table guard, one from-status check, an optional round increment, and a requeue in one transaction. The re-review cap is enforced in the row under the guard's lock, and a refusal leaves the task waiting and the row untouched, surfaced as ErrRereviewCapReached (added to the wire error table and the conformance battery). The column joins the column-listing ListTaskSummaries projection and UpdateTask, so the dashboard reads the offer and the store round-trips the count.
This commit is contained in:
1 parent
97a81dcc63
commit
4b4459f0cf
17 files changed
+1486
-814
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -52,6 +52,7 @@ const (
|
||||
StateStoreService_RecoverStale_FullMethodName = "/state.v1.StateStoreService/RecoverStale"
|
||||
StateStoreService_ArchiveTask_FullMethodName = "/state.v1.StateStoreService/ArchiveTask"
|
||||
StateStoreService_RetryTask_FullMethodName = "/state.v1.StateStoreService/RetryTask"
|
||||
StateStoreService_RespondReviewGate_FullMethodName = "/state.v1.StateStoreService/RespondReviewGate"
|
||||
StateStoreService_BeginRemediation_FullMethodName = "/state.v1.StateStoreService/BeginRemediation"
|
||||
StateStoreService_UpdateReviewPayload_FullMethodName = "/state.v1.StateStoreService/UpdateReviewPayload"
|
||||
StateStoreService_SetReviewCursors_FullMethodName = "/state.v1.StateStoreService/SetReviewCursors"
|
||||
@@ -183,6 +184,13 @@ type StateStoreServiceClient interface {
|
||||
// Archive / Retry
|
||||
ArchiveTask(ctx context.Context, in *ArchiveTaskRequest, opts ...grpc.CallOption) (*ArchiveTaskResponse, error)
|
||||
RetryTask(ctx context.Context, in *RetryTaskRequest, opts ...grpc.CallOption) (*RetryTaskResponse, error)
|
||||
// RespondReviewGate is the operator's answer to the review gate
|
||||
// (docs/prds/pr-review-operator-response.md, Decision 2): one guarded
|
||||
// requeue that records the answer, increments rereview_rounds for a
|
||||
// re-review, and refuses a re-review past cap with the ErrRereviewCapReached
|
||||
// wire sentinel. Admin-only, like every other operator surface: a
|
||||
// task-scoped grant never reaches it.
|
||||
RespondReviewGate(ctx context.Context, in *RespondReviewGateRequest, opts ...grpc.CallOption) (*RespondReviewGateResponse, error)
|
||||
BeginRemediation(ctx context.Context, in *BeginRemediationRequest, opts ...grpc.CallOption) (*BeginRemediationResponse, error)
|
||||
UpdateReviewPayload(ctx context.Context, in *UpdateReviewPayloadRequest, opts ...grpc.CallOption) (*UpdateReviewPayloadResponse, error)
|
||||
SetReviewCursors(ctx context.Context, in *SetReviewCursorsRequest, opts ...grpc.CallOption) (*SetReviewCursorsResponse, error)
|
||||
@@ -640,6 +648,16 @@ func (c *stateStoreServiceClient) RetryTask(ctx context.Context, in *RetryTaskRe
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *stateStoreServiceClient) RespondReviewGate(ctx context.Context, in *RespondReviewGateRequest, opts ...grpc.CallOption) (*RespondReviewGateResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(RespondReviewGateResponse)
|
||||
err := c.cc.Invoke(ctx, StateStoreService_RespondReviewGate_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *stateStoreServiceClient) BeginRemediation(ctx context.Context, in *BeginRemediationRequest, opts ...grpc.CallOption) (*BeginRemediationResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(BeginRemediationResponse)
|
||||
@@ -1354,6 +1372,13 @@ type StateStoreServiceServer interface {
|
||||
// Archive / Retry
|
||||
ArchiveTask(context.Context, *ArchiveTaskRequest) (*ArchiveTaskResponse, error)
|
||||
RetryTask(context.Context, *RetryTaskRequest) (*RetryTaskResponse, error)
|
||||
// RespondReviewGate is the operator's answer to the review gate
|
||||
// (docs/prds/pr-review-operator-response.md, Decision 2): one guarded
|
||||
// requeue that records the answer, increments rereview_rounds for a
|
||||
// re-review, and refuses a re-review past cap with the ErrRereviewCapReached
|
||||
// wire sentinel. Admin-only, like every other operator surface: a
|
||||
// task-scoped grant never reaches it.
|
||||
RespondReviewGate(context.Context, *RespondReviewGateRequest) (*RespondReviewGateResponse, error)
|
||||
BeginRemediation(context.Context, *BeginRemediationRequest) (*BeginRemediationResponse, error)
|
||||
UpdateReviewPayload(context.Context, *UpdateReviewPayloadRequest) (*UpdateReviewPayloadResponse, error)
|
||||
SetReviewCursors(context.Context, *SetReviewCursorsRequest) (*SetReviewCursorsResponse, error)
|
||||
@@ -1580,6 +1605,9 @@ func (UnimplementedStateStoreServiceServer) ArchiveTask(context.Context, *Archiv
|
||||
func (UnimplementedStateStoreServiceServer) RetryTask(context.Context, *RetryTaskRequest) (*RetryTaskResponse, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method RetryTask not implemented")
|
||||
}
|
||||
func (UnimplementedStateStoreServiceServer) RespondReviewGate(context.Context, *RespondReviewGateRequest) (*RespondReviewGateResponse, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method RespondReviewGate not implemented")
|
||||
}
|
||||
func (UnimplementedStateStoreServiceServer) BeginRemediation(context.Context, *BeginRemediationRequest) (*BeginRemediationResponse, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method BeginRemediation not implemented")
|
||||
}
|
||||
@@ -2381,6 +2409,24 @@ func _StateStoreService_RetryTask_Handler(srv interface{}, ctx context.Context,
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _StateStoreService_RespondReviewGate_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(RespondReviewGateRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(StateStoreServiceServer).RespondReviewGate(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: StateStoreService_RespondReviewGate_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(StateStoreServiceServer).RespondReviewGate(ctx, req.(*RespondReviewGateRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _StateStoreService_BeginRemediation_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(BeginRemediationRequest)
|
||||
if err := dec(in); err != nil {
|
||||
@@ -3615,6 +3661,10 @@ var StateStoreService_ServiceDesc = grpc.ServiceDesc{
|
||||
MethodName: "RetryTask",
|
||||
Handler: _StateStoreService_RetryTask_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "RespondReviewGate",
|
||||
Handler: _StateStoreService_RespondReviewGate_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "BeginRemediation",
|
||||
Handler: _StateStoreService_BeginRemediation_Handler,
|
||||
|
||||
@@ -38,6 +38,7 @@ type TaskStore interface {
|
||||
TaskQueries
|
||||
TaskArchiver
|
||||
TaskRetryer
|
||||
ReviewGateResponder
|
||||
RemediationStarter
|
||||
}
|
||||
|
||||
@@ -126,6 +127,21 @@ type TaskRetryer interface {
|
||||
RetryTask(ctx context.Context, taskID int64, fromStatus, workflow string) error
|
||||
}
|
||||
|
||||
// ReviewGateResponder is the operator's answer to the review gate
|
||||
// (docs/prds/pr-review-operator-response.md, Decision 2): one guarded write
|
||||
// beside RetryTask/BeginRemediation that fills in the review gate document
|
||||
// the caller computed (the outcome, the selection or the instructions), and
|
||||
// -- for a re-review only -- increments rereview_rounds and requeues, under
|
||||
// one transition-table guard and one from-status check. A re-review that has
|
||||
// spent its rounds is refused with ErrRereviewCapReached and writes nothing,
|
||||
// so the cap cannot be raced past; an approve requeues with no counter.
|
||||
//
|
||||
// The caller computes the gate document from the task row it read; the write
|
||||
// is the only place rereview_rounds ever increments.
|
||||
type ReviewGateResponder interface {
|
||||
RespondReviewGate(ctx context.Context, taskID int64, fromStatus, gate string, rereview bool, maxRounds int) error
|
||||
}
|
||||
|
||||
// RemediationStarter queues a review-triggered remediation for an
|
||||
// Archie-owned open-PR task: one guarded transition that carries the
|
||||
// JSON-encoded review unit in the same write, so a claimed remediation
|
||||
@@ -511,6 +527,11 @@ var (
|
||||
// stored for an org/service pair -- the setup terminal has not
|
||||
// captured one yet.
|
||||
ErrHarnessSecretNotFound = errors.New("store: harness secret not found")
|
||||
// ErrRereviewCapReached is returned when a review gate re-review would
|
||||
// round past prreview.MaxRereviewRounds (docs/prds/pr-review-operator-
|
||||
// response.md, Decision 2). The task stays waiting and its gate row is
|
||||
// unchanged. The dashboard answers it 409, like every other conflict.
|
||||
ErrRereviewCapReached = errors.New("store: re-review cap reached")
|
||||
|
||||
// ErrResourceNotFound is returned when a control-plane resource kind has
|
||||
// no stored document. It is in-process only (not on the gRPC wire): the
|
||||
|
||||
@@ -128,6 +128,24 @@ type Task struct {
|
||||
// budget: one shared counter made N operator retries eat the
|
||||
// review-remediation budget and vice versa.
|
||||
RemediationRounds int `json:"remediation_rounds"`
|
||||
// ReviewGate is the operator-approval gate's whole conversation with
|
||||
// the operator, persisted as one JSON document (prreview.EncodeReviewGate):
|
||||
// the offer the gate writes before it waits for an operator -- the scored
|
||||
// findings, the head SHA, the pull request's identity and the workflow the
|
||||
// wait resumes -- and the answer the response path fills in -- the outcome,
|
||||
// the selected findings' keys for an approve and the instructions for a
|
||||
// re-review. Without it, "post the selected findings" cannot mean the
|
||||
// findings the operator saw: the resumed run recomputes them with agent
|
||||
// calls (docs/prds/pr-review-operator-response.md, "The review the operator
|
||||
// answers"). Empty for a task that never reached the gate.
|
||||
ReviewGate string `json:"review_gate"`
|
||||
// RereviewRounds counts how many operator re-reviews the pr-review gate
|
||||
// granted, incremented in the same guarded write that requeues the
|
||||
// re-review and capped at prreview.MaxRereviewRounds
|
||||
// (docs/prds/pr-review-operator-response.md, Decision 2). Deliberately
|
||||
// separate from both RetryCount and RemediationRounds -- one shared
|
||||
// counter made unrelated retries draw another phase's budget down.
|
||||
RereviewRounds int `json:"rereview_rounds"`
|
||||
// CallParentTaskID is the task whose workflow.call step started this
|
||||
// one; 0 on a root run. A caller reads only the tasks it started,
|
||||
// through WorkflowCallStatus.
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
-- +goose Up
|
||||
-- The operator-approval gate's conversation with the operator
|
||||
-- (docs/prds/pr-review-operator-response.md). review_gate is the JSON
|
||||
-- document the gate writes before it waits -- the scored findings, the head
|
||||
-- SHA, the pull request's identity and the workflow the wait resumes -- and
|
||||
-- the answer the response path fills in. The text type mirrors review_payload,
|
||||
-- its sibling column: the package that owns the type (internal/domain/
|
||||
-- workflow/prreview) owns the encoding, so the store never parses it.
|
||||
ALTER TABLE tasks ADD COLUMN review_gate text NOT NULL DEFAULT '';
|
||||
|
||||
-- rereview_rounds counts granted operator re-reviews, capped by the guarded
|
||||
-- response write itself (prreview.MaxRereviewRounds). Deliberately separate
|
||||
-- from retry_count and remediation_rounds: one shared counter made unrelated
|
||||
-- retries draw another phase's budget down.
|
||||
ALTER TABLE tasks ADD COLUMN rereview_rounds bigint NOT NULL DEFAULT 0;
|
||||
|
||||
-- +goose Down
|
||||
ALTER TABLE tasks DROP COLUMN rereview_rounds;
|
||||
ALTER TABLE tasks DROP COLUMN review_gate;
|
||||
@@ -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 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 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)
|
||||
SELECT
|
||||
@@ -41,7 +41,7 @@ SELECT
|
||||
caller.id, caller.call_depth + 1
|
||||
FROM caller
|
||||
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
|
||||
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
|
||||
`
|
||||
|
||||
type EnqueueCallTaskParams struct {
|
||||
@@ -113,6 +113,8 @@ func (q *Queries) EnqueueCallTask(ctx context.Context, arg EnqueueCallTaskParams
|
||||
&i.CallParentTaskID,
|
||||
&i.CallDepth,
|
||||
&i.Outputs,
|
||||
&i.ReviewGate,
|
||||
&i.RereviewRounds,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -393,6 +393,8 @@ type Task struct {
|
||||
CallParentTaskID int64
|
||||
CallDepth int32
|
||||
Outputs string
|
||||
ReviewGate string
|
||||
RereviewRounds int64
|
||||
}
|
||||
|
||||
type ToolCall struct {
|
||||
|
||||
@@ -48,7 +48,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
|
||||
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
|
||||
`
|
||||
|
||||
type ClaimByIssueParams struct {
|
||||
@@ -101,6 +101,8 @@ func (q *Queries) ClaimByIssue(ctx context.Context, arg ClaimByIssueParams) (Tas
|
||||
&i.CallParentTaskID,
|
||||
&i.CallDepth,
|
||||
&i.Outputs,
|
||||
&i.ReviewGate,
|
||||
&i.RereviewRounds,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -115,7 +117,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
|
||||
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
|
||||
`
|
||||
|
||||
// FOR UPDATE SKIP LOCKED replaces the SQLite single-writer assumption: two
|
||||
@@ -166,6 +168,8 @@ func (q *Queries) ClaimNextTask(ctx context.Context) (Task, error) {
|
||||
&i.CallParentTaskID,
|
||||
&i.CallDepth,
|
||||
&i.Outputs,
|
||||
&i.ReviewGate,
|
||||
&i.RereviewRounds,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -276,7 +280,7 @@ VALUES (
|
||||
),
|
||||
$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
|
||||
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
|
||||
`
|
||||
|
||||
type InsertChatTaskParams struct {
|
||||
@@ -348,6 +352,8 @@ func (q *Queries) InsertChatTask(ctx context.Context, arg InsertChatTaskParams)
|
||||
&i.CallParentTaskID,
|
||||
&i.CallDepth,
|
||||
&i.Outputs,
|
||||
&i.ReviewGate,
|
||||
&i.RereviewRounds,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -431,7 +437,7 @@ const listTaskSummaries = `-- name: ListTaskSummaries :many
|
||||
SELECT id, owner, repo, issue_number, title, status, workflow,
|
||||
pr_number, tokens_used, iterations, attempt, park_reason, retry_count,
|
||||
created_at, updated_at, plan, source, identity, binding_id, binding_version,
|
||||
outputs
|
||||
outputs, review_gate, rereview_rounds
|
||||
FROM tasks ORDER BY updated_at DESC LIMIT $1
|
||||
`
|
||||
|
||||
@@ -457,6 +463,8 @@ type ListTaskSummariesRow struct {
|
||||
BindingID string
|
||||
BindingVersion int64
|
||||
Outputs string
|
||||
ReviewGate string
|
||||
RereviewRounds int64
|
||||
}
|
||||
|
||||
// The dashboard's list. This projection is deliberately narrow: Plan gates
|
||||
@@ -464,6 +472,10 @@ type ListTaskSummariesRow struct {
|
||||
// back would send rows without them. outputs rides it because the task row's
|
||||
// own structured result is part of the summary a caller or the dashboard
|
||||
// reads (docs/prds/workflow-call-outputs.md, "Storage and wire").
|
||||
// review_gate and rereview_rounds join it deliberately: the offer the gate
|
||||
// wrote is what the operator reads before answering it
|
||||
// (docs/prds/pr-review-operator-response.md, "The review the operator
|
||||
// answers"), and the round count is the cap's visible half.
|
||||
func (q *Queries) ListTaskSummaries(ctx context.Context, limit int32) ([]ListTaskSummariesRow, error) {
|
||||
rows, err := q.db.Query(ctx, listTaskSummaries, limit)
|
||||
if err != nil {
|
||||
@@ -495,6 +507,8 @@ func (q *Queries) ListTaskSummaries(ctx context.Context, limit int32) ([]ListTas
|
||||
&i.BindingID,
|
||||
&i.BindingVersion,
|
||||
&i.Outputs,
|
||||
&i.ReviewGate,
|
||||
&i.RereviewRounds,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -579,6 +593,57 @@ func (q *Queries) RequeueTask(ctx context.Context, arg RequeueTaskParams) (int64
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const rereviewRounds = `-- name: RereviewRounds :one
|
||||
SELECT rereview_rounds FROM tasks WHERE id = $1
|
||||
`
|
||||
|
||||
// Read under the lock LockTaskStatus takes in the same transaction, so the
|
||||
// cap decision and the increment it guards cannot interleave with a second
|
||||
// response write.
|
||||
func (q *Queries) RereviewRounds(ctx context.Context, id int64) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, rereviewRounds, id)
|
||||
var rereview_rounds int64
|
||||
err := row.Scan(&rereview_rounds)
|
||||
return rereview_rounds, err
|
||||
}
|
||||
|
||||
const respondReviewGateTask = `-- name: RespondReviewGateTask :execrows
|
||||
UPDATE tasks SET status = 'queued',
|
||||
review_gate = $1::text,
|
||||
rereview_rounds = rereview_rounds + CASE WHEN $2::bool THEN 1 ELSE 0 END,
|
||||
park_reason = '', park_class = 'needs_human', updated_at = now()
|
||||
WHERE id = $3 AND status = $4
|
||||
AND ($2::bool = false OR rereview_rounds < $5::bigint)
|
||||
`
|
||||
|
||||
type RespondReviewGateTaskParams struct {
|
||||
Gate string
|
||||
Rereview bool
|
||||
ID int64
|
||||
FromStatus string
|
||||
Cap int64
|
||||
}
|
||||
|
||||
// The review gate response write (docs/prds/pr-review-operator-response.md,
|
||||
// Decision 2): one guarded requeue beside RetryTask/BeginRemediation. The
|
||||
// from-status guard and the transition table live in the store's transaction
|
||||
// around this statement; the rereview cap is enforced HERE, in the row, so
|
||||
// two simultaneous re-reviews cannot spend a round the cap forbids. An
|
||||
// approve passes rereview=false and bumps nothing.
|
||||
func (q *Queries) RespondReviewGateTask(ctx context.Context, arg RespondReviewGateTaskParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, respondReviewGateTask,
|
||||
arg.Gate,
|
||||
arg.Rereview,
|
||||
arg.ID,
|
||||
arg.FromStatus,
|
||||
arg.Cap,
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const retryTask = `-- name: RetryTask :execrows
|
||||
UPDATE tasks SET status = 'queued', retry_count = retry_count + 1,
|
||||
workflow = CASE WHEN $1::text = '' THEN workflow ELSE $1::text END,
|
||||
@@ -635,7 +700,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 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 FROM tasks WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) TaskByID(ctx context.Context, id int64) (Task, error) {
|
||||
@@ -680,12 +745,14 @@ func (q *Queries) TaskByID(ctx context.Context, id int64) (Task, error) {
|
||||
&i.CallParentTaskID,
|
||||
&i.CallDepth,
|
||||
&i.Outputs,
|
||||
&i.ReviewGate,
|
||||
&i.RereviewRounds,
|
||||
)
|
||||
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 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 FROM tasks WHERE owner = $1 AND repo = $2 AND issue_number = $3
|
||||
`
|
||||
|
||||
type TaskByIssueParams struct {
|
||||
@@ -736,12 +803,14 @@ func (q *Queries) TaskByIssue(ctx context.Context, arg TaskByIssueParams) (Task,
|
||||
&i.CallParentTaskID,
|
||||
&i.CallDepth,
|
||||
&i.Outputs,
|
||||
&i.ReviewGate,
|
||||
&i.RereviewRounds,
|
||||
)
|
||||
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 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 FROM tasks WHERE owner = $1 AND repo = $2 AND pr_number = $3 AND status = $4
|
||||
`
|
||||
|
||||
type TaskByPRParams struct {
|
||||
@@ -801,6 +870,8 @@ func (q *Queries) TaskByPR(ctx context.Context, arg TaskByPRParams) (Task, error
|
||||
&i.CallParentTaskID,
|
||||
&i.CallDepth,
|
||||
&i.Outputs,
|
||||
&i.ReviewGate,
|
||||
&i.RereviewRounds,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -862,7 +933,7 @@ UPDATE tasks SET workflow = $2, branch = $3, plan = $4, notes = $5,
|
||||
watch_comment_id = $10, retry_count = $11, remediation_rounds = $12,
|
||||
review_payload = $13, workflow_definition_version = $14,
|
||||
workflow_definition_digest = $15, workflow_definition_yaml = $16,
|
||||
outputs = $17,
|
||||
outputs = $17, review_gate = $18, rereview_rounds = $19,
|
||||
updated_at = now()
|
||||
WHERE id = $1
|
||||
`
|
||||
@@ -885,6 +956,8 @@ type UpdateTaskParams struct {
|
||||
WorkflowDefinitionDigest string
|
||||
WorkflowDefinitionYaml string
|
||||
Outputs string
|
||||
ReviewGate string
|
||||
RereviewRounds int64
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateTask(ctx context.Context, arg UpdateTaskParams) error {
|
||||
@@ -906,6 +979,8 @@ func (q *Queries) UpdateTask(ctx context.Context, arg UpdateTaskParams) error {
|
||||
arg.WorkflowDefinitionDigest,
|
||||
arg.WorkflowDefinitionYaml,
|
||||
arg.Outputs,
|
||||
arg.ReviewGate,
|
||||
arg.RereviewRounds,
|
||||
)
|
||||
return err
|
||||
}
|
||||
@@ -34,10 +34,14 @@ SELECT * FROM tasks WHERE owner = $1 AND repo = $2 AND pr_number = $3 AND status
|
||||
-- back would send rows without them. outputs rides it because the task row's
|
||||
-- own structured result is part of the summary a caller or the dashboard
|
||||
-- reads (docs/prds/workflow-call-outputs.md, "Storage and wire").
|
||||
-- review_gate and rereview_rounds join it deliberately: the offer the gate
|
||||
-- wrote is what the operator reads before answering it
|
||||
-- (docs/prds/pr-review-operator-response.md, "The review the operator
|
||||
-- answers"), and the round count is the cap's visible half.
|
||||
SELECT id, owner, repo, issue_number, title, status, workflow,
|
||||
pr_number, tokens_used, iterations, attempt, park_reason, retry_count,
|
||||
created_at, updated_at, plan, source, identity, binding_id, binding_version,
|
||||
outputs
|
||||
outputs, review_gate, rereview_rounds
|
||||
FROM tasks ORDER BY updated_at DESC LIMIT $1;
|
||||
|
||||
-- name: CountTasksByStatus :many
|
||||
@@ -80,10 +84,16 @@ UPDATE tasks SET workflow = $2, branch = $3, plan = $4, notes = $5,
|
||||
watch_comment_id = $10, retry_count = $11, remediation_rounds = $12,
|
||||
review_payload = $13, workflow_definition_version = $14,
|
||||
workflow_definition_digest = $15, workflow_definition_yaml = $16,
|
||||
outputs = $17,
|
||||
outputs = $17, review_gate = $18, rereview_rounds = $19,
|
||||
updated_at = now()
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: RereviewRounds :one
|
||||
-- Read under the lock LockTaskStatus takes in the same transaction, so the
|
||||
-- cap decision and the increment it guards cannot interleave with a second
|
||||
-- response write.
|
||||
SELECT rereview_rounds FROM tasks WHERE id = $1;
|
||||
|
||||
-- name: LockTaskStatus :one
|
||||
-- Locks the task's row and returns its current status, so the staleness and
|
||||
-- transition-table checks that decide a guarded write hold against a
|
||||
@@ -165,6 +175,20 @@ UPDATE tasks SET status = 'queued', retry_count = retry_count + 1,
|
||||
park_reason = '', park_class = 'needs_human', updated_at = now()
|
||||
WHERE id = @id AND status = @from_status;
|
||||
|
||||
-- name: RespondReviewGateTask :execrows
|
||||
-- The review gate response write (docs/prds/pr-review-operator-response.md,
|
||||
-- Decision 2): one guarded requeue beside RetryTask/BeginRemediation. The
|
||||
-- from-status guard and the transition table live in the store's transaction
|
||||
-- around this statement; the rereview cap is enforced HERE, in the row, so
|
||||
-- two simultaneous re-reviews cannot spend a round the cap forbids. An
|
||||
-- approve passes rereview=false and bumps nothing.
|
||||
UPDATE tasks SET status = 'queued',
|
||||
review_gate = @gate::text,
|
||||
rereview_rounds = rereview_rounds + CASE WHEN @rereview::bool THEN 1 ELSE 0 END,
|
||||
park_reason = '', park_class = 'needs_human', updated_at = now()
|
||||
WHERE id = @id AND status = @from_status
|
||||
AND (@rereview::bool = false OR rereview_rounds < @cap::bigint);
|
||||
|
||||
-- name: ArchiveTaskDelete :execrows
|
||||
DELETE FROM tasks WHERE id = $1 AND status = $2;
|
||||
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
)
|
||||
|
||||
// Tests for the review gate response write
|
||||
// (docs/prds/pr-review-operator-response.md, Decision 2): one guarded
|
||||
// requeue beside RetryTask/BeginRemediation. The rereview cap is enforced in
|
||||
// the row, so a race cannot spend a round the cap forbids.
|
||||
|
||||
// waitingTaskWithGate claims a task, runs it to waiting_human and writes a
|
||||
// gate document on it -- the state an operator's response acts on.
|
||||
func waitingTaskWithGate(t *testing.T, s *Store) *workflow.Task {
|
||||
t.Helper()
|
||||
execution := runningTask(t, s)
|
||||
if err := s.Transition(t.Context(), execution.ID, workflow.StatusRunning, workflow.StatusWaitingHuman, "awaiting operator review"); err != nil {
|
||||
t.Fatalf("Transition to waiting_human: %v", err)
|
||||
}
|
||||
if err := s.Update(t.Context(), &workflow.Task{
|
||||
ID: execution.ID, Workflow: execution.Workflow, ReviewGate: `{"findings":[{"title":"f0"}]}`,
|
||||
}); err != nil {
|
||||
t.Fatalf("write gate offer: %v", err)
|
||||
}
|
||||
refreshed, err := s.TaskByID(t.Context(), execution.ID)
|
||||
if err != nil || refreshed == nil {
|
||||
t.Fatalf("TaskByID = (%v, %v)", refreshed, err)
|
||||
}
|
||||
return refreshed
|
||||
}
|
||||
|
||||
func TestRespondReviewGateIncrementsOnlyForARereview(t *testing.T) {
|
||||
s := storeFor(t)
|
||||
task := waitingTaskWithGate(t, s)
|
||||
|
||||
if err := s.RespondReviewGate(t.Context(), task.ID, workflow.StatusWaitingHuman, `{"outcome":"approve"}`, false, 2); err != nil {
|
||||
t.Fatalf("RespondReviewGate(approve): %v", err)
|
||||
}
|
||||
got, err := s.TaskByID(t.Context(), task.ID)
|
||||
if err != nil || got == nil {
|
||||
t.Fatalf("TaskByID = (%v, %v)", got, err)
|
||||
}
|
||||
if got.Status != workflow.StatusQueued {
|
||||
t.Fatalf("status after approve = %q, want %q: the response requeues", got.Status, workflow.StatusQueued)
|
||||
}
|
||||
if got.RereviewRounds != 0 {
|
||||
t.Fatalf("rereview_rounds after approve = %d, want 0: an approve counts no round", got.RereviewRounds)
|
||||
}
|
||||
if got.ReviewGate != `{"outcome":"approve"}` {
|
||||
t.Fatalf("review_gate after approve = %q, want the caller's document", got.ReviewGate)
|
||||
}
|
||||
if got.Workflow != task.Workflow {
|
||||
t.Fatalf("workflow after approve = %q, want %q (the wait resumes the workflow the wait names)", got.Workflow, task.Workflow)
|
||||
}
|
||||
|
||||
// Requeue again to waiting, then answer with a re-review: the counter moves.
|
||||
if err := s.Transition(t.Context(), task.ID, workflow.StatusQueued, workflow.StatusRunning, ""); err != nil {
|
||||
t.Fatalf("claim again: %v", err)
|
||||
}
|
||||
if err := s.Transition(t.Context(), task.ID, workflow.StatusRunning, workflow.StatusWaitingHuman, ""); err != nil {
|
||||
t.Fatalf("wait again: %v", err)
|
||||
}
|
||||
if err := s.RespondReviewGate(t.Context(), task.ID, workflow.StatusWaitingHuman, `{"outcome":"rereview","instructions":"look again"}`, true, 2); err != nil {
|
||||
t.Fatalf("RespondReviewGate(rereview): %v", err)
|
||||
}
|
||||
got, err = s.TaskByID(t.Context(), task.ID)
|
||||
if err != nil || got == nil {
|
||||
t.Fatalf("TaskByID = (%v, %v)", got, err)
|
||||
}
|
||||
if got.RereviewRounds != 1 {
|
||||
t.Fatalf("rereview_rounds after one re-review = %d, want 1 (and it must survive the store round trip)", got.RereviewRounds)
|
||||
}
|
||||
if got.Status != workflow.StatusQueued {
|
||||
t.Fatalf("status after re-review = %q, want %q", got.Status, workflow.StatusQueued)
|
||||
}
|
||||
if got.ReviewGate != `{"outcome":"rereview","instructions":"look again"}` {
|
||||
t.Fatalf("review_gate after re-review = %q, want the caller's document", got.ReviewGate)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRespondReviewGateAtCapRefusedAndStaysWaiting(t *testing.T) {
|
||||
s := storeFor(t)
|
||||
task := waitingTaskWithGate(t, s)
|
||||
|
||||
// Spend both rounds: two waiting_human gates answered with re-reviews.
|
||||
for range 2 {
|
||||
if err := s.RespondReviewGate(t.Context(), task.ID, workflow.StatusWaitingHuman, `{"outcome":"rereview"}`, true, 2); err != nil {
|
||||
t.Fatalf("RespondReviewGate(reviewer round %d): %v", task.RereviewRounds, err)
|
||||
}
|
||||
if err := s.Transition(t.Context(), task.ID, workflow.StatusQueued, workflow.StatusRunning, ""); err != nil {
|
||||
t.Fatalf("claim again: %v", err)
|
||||
}
|
||||
if err := s.Transition(t.Context(), task.ID, workflow.StatusRunning, workflow.StatusWaitingHuman, ""); err != nil {
|
||||
t.Fatalf("wait again: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
before, err := s.TaskByID(t.Context(), task.ID)
|
||||
if err != nil || before == nil {
|
||||
t.Fatalf("TaskByID = (%v, %v)", before, err)
|
||||
}
|
||||
err = s.RespondReviewGate(t.Context(), task.ID, workflow.StatusWaitingHuman, `{"outcome":"rereview"}`, true, 2)
|
||||
if !errors.Is(err, storecontract.ErrRereviewCapReached) {
|
||||
t.Fatalf("RespondReviewGate at cap = %v, want ErrRereviewCapReached", err)
|
||||
}
|
||||
after, err := s.TaskByID(t.Context(), task.ID)
|
||||
if err != nil || after == nil {
|
||||
t.Fatalf("TaskByID = (%v, %v)", after, err)
|
||||
}
|
||||
if after.Status != workflow.StatusWaitingHuman {
|
||||
t.Fatalf("status after refused re-review = %q, want %q: the task stays waiting", after.Status, workflow.StatusWaitingHuman)
|
||||
}
|
||||
if after.RereviewRounds != before.RereviewRounds || after.ReviewGate != before.ReviewGate {
|
||||
t.Fatalf("refused re-review changed the row: rounds %d->%d, gate %q->%q",
|
||||
before.RereviewRounds, after.RereviewRounds, before.ReviewGate, after.ReviewGate)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRespondReviewGateStaleFromStatusWritesNothing(t *testing.T) {
|
||||
s := storeFor(t)
|
||||
task := waitingTaskWithGate(t, s)
|
||||
|
||||
err := s.RespondReviewGate(t.Context(), task.ID, workflow.StatusParked, `{"outcome":"approve"}`, false, 2)
|
||||
if !errors.Is(err, storecontract.ErrStaleTransition) {
|
||||
t.Fatalf("RespondReviewGate from parked = %v, want ErrStaleTransition", err)
|
||||
}
|
||||
got, err := s.TaskByID(t.Context(), task.ID)
|
||||
if err != nil || got == nil {
|
||||
t.Fatalf("TaskByID = (%v, %v)", got, err)
|
||||
}
|
||||
if got.Status != workflow.StatusWaitingHuman || got.ReviewGate != `{"findings":[{"title":"f0"}]}` {
|
||||
t.Fatalf("refused write changed the row: status=%q gate=%q", got.Status, got.ReviewGate)
|
||||
}
|
||||
}
|
||||
@@ -121,6 +121,8 @@ func taskFromRow(t postgresdb.Task) *workflow.Task {
|
||||
Inputs: inputs,
|
||||
Outputs: outputs,
|
||||
ReviewPayload: t.ReviewPayload,
|
||||
ReviewGate: t.ReviewGate,
|
||||
RereviewRounds: int(t.RereviewRounds),
|
||||
ParkClass: t.ParkClass,
|
||||
RemediationRounds: int(t.RemediationRounds),
|
||||
CallParentTaskID: t.CallParentTaskID,
|
||||
@@ -318,6 +320,8 @@ func (s *Store) Update(ctx context.Context, t *workflow.Task) error {
|
||||
RetryCount: int64(t.RetryCount),
|
||||
RemediationRounds: int64(t.RemediationRounds),
|
||||
ReviewPayload: t.ReviewPayload,
|
||||
ReviewGate: t.ReviewGate,
|
||||
RereviewRounds: int64(t.RereviewRounds),
|
||||
WorkflowDefinitionVersion: t.WorkflowDefinitionVersion,
|
||||
WorkflowDefinitionDigest: t.WorkflowDefinitionDigest,
|
||||
WorkflowDefinitionYaml: t.WorkflowDefinitionYAML,
|
||||
@@ -406,6 +410,53 @@ func (s *Store) RetryTask(ctx context.Context, taskID int64, fromStatus, wf stri
|
||||
})
|
||||
}
|
||||
|
||||
// RespondReviewGate is the review gate response write
|
||||
// (docs/prds/pr-review-operator-response.md, Decision 2): one guarded
|
||||
// requeue that records the operator's answer, increments rereview_rounds for
|
||||
// a re-review only, and writes one audit row -- all in one transaction. The
|
||||
// cap is checked under the row lock the transition guard takes, so two
|
||||
// simultaneous re-reviews cannot both spend the last round.
|
||||
func (s *Store) RespondReviewGate(ctx context.Context, taskID int64, fromStatus, gate string, rereview bool, maxRounds int) error {
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
q := postgresdb.New(tx)
|
||||
|
||||
if err := guardTransition(ctx, q, taskID, fromStatus, workflow.StatusQueued); err != nil {
|
||||
return err
|
||||
}
|
||||
// The row is locked by the guard read, so the cap decision and the
|
||||
// increment below cannot interleave with a second response write.
|
||||
rounds, err := q.RereviewRounds(ctx, taskID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rereview && rounds >= int64(maxRounds) {
|
||||
return storecontract.ErrRereviewCapReached
|
||||
}
|
||||
n, err := q.RespondReviewGateTask(ctx, postgresdb.RespondReviewGateTaskParams{
|
||||
ID: taskID, FromStatus: fromStatus, Gate: gate, Rereview: rereview, Cap: int64(maxRounds),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
return storecontract.ErrStaleTransition
|
||||
}
|
||||
detail := "operator approved the review gate"
|
||||
if rereview {
|
||||
detail = "operator requested a re-review"
|
||||
}
|
||||
if err := q.InsertTransition(ctx, postgresdb.InsertTransitionParams{
|
||||
TaskID: taskID, FromStatus: fromStatus, ToStatus: workflow.StatusQueued, Detail: detail,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
// requeue carries the guarded requeue write Requeue and RetryTask share: the
|
||||
// transition-table check, the update guarded on the from status, and one audit
|
||||
// row. update is the query that differs between the two callers; detail is the
|
||||
@@ -561,6 +612,7 @@ func (s *Store) Tasks(ctx context.Context, limit int) ([]workflow.Task, error) {
|
||||
CreatedAt: r.CreatedAt, UpdatedAt: r.UpdatedAt, Plan: r.Plan, Source: r.Source,
|
||||
Identity: r.Identity, BindingID: r.BindingID, BindingVersion: int(r.BindingVersion),
|
||||
Outputs: outputs,
|
||||
ReviewGate: r.ReviewGate, RereviewRounds: int(r.RereviewRounds),
|
||||
})
|
||||
}
|
||||
return tasks, nil
|
||||
|
||||
@@ -212,6 +212,13 @@ func (c *Client) RetryTask(ctx context.Context, taskID int64, fromStatus, wf str
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) RespondReviewGate(ctx context.Context, taskID int64, fromStatus, gate string, rereview bool, maxRounds int) error {
|
||||
_, err := c.client.RespondReviewGate(ctx, &pb.RespondReviewGateRequest{
|
||||
TaskId: taskID, FromStatus: fromStatus, Gate: gate, Rereview: rereview, Cap: int64(maxRounds),
|
||||
})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) BeginRemediation(ctx context.Context, taskID int64, payload string) error {
|
||||
_, err := c.client.BeginRemediation(ctx, &pb.BeginRemediationRequest{TaskId: taskID, Payload: payload})
|
||||
return unmapError(err)
|
||||
|
||||
@@ -149,6 +149,57 @@ func TestStateStoreConformance(t *testing.T) {
|
||||
if got := triggered.EffectivePRNumber(); got != 7 {
|
||||
t.Fatalf("queued task EffectivePRNumber = %d, want 7 -- inputs did not round trip", got)
|
||||
}
|
||||
// Review gate response: one guarded requeue records the answer,
|
||||
// counts a re-review, and refuses past the cap with the sentinel
|
||||
// intact across the hop (docs/prds/pr-review-operator-response.md,
|
||||
// Decision 2). The trigger task carries the gate so the main
|
||||
// battery's task row keeps its own path.
|
||||
if err := c.Transition(ctx, triggered.ID, "queued", "running", "started"); err != nil {
|
||||
t.Fatalf("claim trigger task: %v", err)
|
||||
}
|
||||
if err := c.Transition(ctx, triggered.ID, "running", "waiting_human", "awaiting review"); err != nil {
|
||||
t.Fatalf("wait trigger task: %v", err)
|
||||
}
|
||||
if err := c.RespondReviewGate(ctx, triggered.ID, "waiting_human", `{"outcome":"approve"}`, false, 2); err != nil {
|
||||
t.Fatalf("RespondReviewGate(approve): %v", err)
|
||||
}
|
||||
gated, err := c.TaskByID(ctx, triggered.ID)
|
||||
if err != nil || gated == nil {
|
||||
t.Fatalf("TaskByID after approve: %+v %v", gated, err)
|
||||
}
|
||||
if gated.Status != "queued" || gated.ReviewGate != `{"outcome":"approve"}` || gated.RereviewRounds != 0 {
|
||||
t.Fatalf("approve row = status:%q gate:%q rounds:%d, want queued, the document, 0", gated.Status, gated.ReviewGate, gated.RereviewRounds)
|
||||
}
|
||||
// Two re-reviews spend the cap; the third is refused and leaves
|
||||
// the task waiting with its round count and gate intact.
|
||||
for round := 1; round <= 2; round++ {
|
||||
if err := c.Transition(ctx, triggered.ID, "queued", "running", "started"); err != nil {
|
||||
t.Fatalf("re-claim %d: %v", round, err)
|
||||
}
|
||||
if err := c.Transition(ctx, triggered.ID, "running", "waiting_human", "awaiting review"); err != nil {
|
||||
t.Fatalf("re-wait %d: %v", round, err)
|
||||
}
|
||||
if err := c.RespondReviewGate(ctx, triggered.ID, "waiting_human", `{"outcome":"rereview"}`, true, 2); err != nil {
|
||||
t.Fatalf("RespondReviewGate(rereview %d): %v", round, err)
|
||||
}
|
||||
got, err := c.TaskByID(ctx, triggered.ID)
|
||||
if err != nil || got == nil || got.RereviewRounds != round {
|
||||
t.Fatalf("rereview %d rounds = %+v %v, want %d across the wire", round, got, err, round)
|
||||
}
|
||||
}
|
||||
if err := c.Transition(ctx, triggered.ID, "queued", "running", "started"); err != nil {
|
||||
t.Fatalf("final re-claim: %v", err)
|
||||
}
|
||||
if err := c.Transition(ctx, triggered.ID, "running", "waiting_human", "awaiting review"); err != nil {
|
||||
t.Fatalf("final re-wait: %v", err)
|
||||
}
|
||||
if err := c.RespondReviewGate(ctx, triggered.ID, "waiting_human", `{"outcome":"rereview"}`, true, 2); !errors.Is(err, storecontract.ErrRereviewCapReached) {
|
||||
t.Fatalf("RespondReviewGate at cap = %v, want ErrRereviewCapReached across the wire", err)
|
||||
}
|
||||
capped, err := c.TaskByID(ctx, triggered.ID)
|
||||
if err != nil || capped == nil || capped.Status != "waiting_human" || capped.RereviewRounds != 2 {
|
||||
t.Fatalf("refused re-review row = %+v %v, want waiting_human at 2 rounds", capped, err)
|
||||
}
|
||||
if err := c.Transition(ctx, task.ID, task.Status, "running", "started"); err != nil {
|
||||
t.Fatalf("Transition: %v", err)
|
||||
}
|
||||
|
||||
@@ -77,6 +77,7 @@ func TestUnmapErrorSentinelFidelity(t *testing.T) {
|
||||
rehyd error
|
||||
}{
|
||||
{name: "harness secret not found", store: storecontract.ErrHarnessSecretNotFound, rehyd: storecontract.ErrHarnessSecretNotFound},
|
||||
{name: "re-review cap reached", store: storecontract.ErrRereviewCapReached, rehyd: storecontract.ErrRereviewCapReached},
|
||||
{name: "stale transition", store: storecontract.ErrStaleTransition, rehyd: storecontract.ErrStaleTransition},
|
||||
{name: "illegal transition", store: storecontract.ErrIllegalTransition, rehyd: storecontract.ErrIllegalTransition},
|
||||
{name: "invalid step", store: storecontract.ErrInvalidStep, rehyd: storecontract.ErrInvalidStep},
|
||||
|
||||
@@ -342,6 +342,13 @@ func (s *server) RetryTask(ctx context.Context, r *pb.RetryTaskRequest) (*pb.Ret
|
||||
return &pb.RetryTaskResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *server) RespondReviewGate(ctx context.Context, r *pb.RespondReviewGateRequest) (*pb.RespondReviewGateResponse, error) {
|
||||
if err := s.deps.Tasks.RespondReviewGate(ctx, r.TaskId, r.FromStatus, r.Gate, r.Rereview, int(r.Cap)); err != nil {
|
||||
return nil, s.logErr("RespondReviewGate", err)
|
||||
}
|
||||
return &pb.RespondReviewGateResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *server) BeginRemediation(ctx context.Context, r *pb.BeginRemediationRequest) (*pb.BeginRemediationResponse, error) {
|
||||
if err := s.deps.Tasks.BeginRemediation(ctx, r.TaskId, r.Payload); err != nil {
|
||||
return nil, s.logErr("BeginRemediation", err)
|
||||
|
||||
@@ -77,6 +77,8 @@ func taskProto(t *task.Task) *pb.Task { //nolint:dupl // mirror-image field-by-f
|
||||
OutputsJson: outputsJSON(t.Outputs),
|
||||
CreatedAt: timestamp(t.CreatedAt), UpdatedAt: timestamp(t.UpdatedAt),
|
||||
ReviewPayload: t.ReviewPayload,
|
||||
ReviewGate: t.ReviewGate,
|
||||
RereviewRounds: int64(t.RereviewRounds),
|
||||
ReviewCursor: t.ReviewCursor,
|
||||
ParkClass: t.ParkClass,
|
||||
RemediationRounds: int32(t.RemediationRounds),
|
||||
@@ -105,6 +107,8 @@ func taskValue(t *pb.Task) *task.Task { //nolint:dupl // see taskProto above
|
||||
Outputs: outputsValue(t.OutputsJson),
|
||||
CreatedAt: timeValue(t.CreatedAt), UpdatedAt: timeValue(t.UpdatedAt),
|
||||
ReviewPayload: t.ReviewPayload,
|
||||
ReviewGate: t.ReviewGate,
|
||||
RereviewRounds: int(t.RereviewRounds),
|
||||
ReviewCursor: t.ReviewCursor,
|
||||
ParkClass: t.ParkClass,
|
||||
RemediationRounds: int(t.RemediationRounds),
|
||||
@@ -406,7 +410,11 @@ const (
|
||||
msgSourcePathTaken = "source path taken"
|
||||
msgSourceSigning = "source signing stale"
|
||||
msgHarnessSecretNotFound = "harness secret not found"
|
||||
msgInternal = "state store: internal error"
|
||||
// msgRereviewCapReached is the canonical message of
|
||||
// storecontract.ErrRereviewCapReached on the wire. Changing it breaks
|
||||
// errors.Is on the client without changing behaviour visibly.
|
||||
msgRereviewCapReached = "store: re-review cap reached"
|
||||
msgInternal = "state store: internal error"
|
||||
// msgTaskLogsUnavailable is the public phrase for "this service has no
|
||||
// task-log reader". It is a wire contract like the sentinels above: the
|
||||
// client rehydrates logging.ErrTaskLogsUnavailable from (Unavailable, this
|
||||
@@ -524,6 +532,7 @@ var wireErrors = []struct {
|
||||
{storecontract.ErrCallDepthExceeded, codes.FailedPrecondition, storecontract.ErrCallDepthExceeded.Error()},
|
||||
{storecontract.ErrSourcePathTaken, codes.AlreadyExists, msgSourcePathTaken},
|
||||
{storecontract.ErrHarnessSecretNotFound, codes.NotFound, msgHarnessSecretNotFound},
|
||||
{storecontract.ErrRereviewCapReached, codes.FailedPrecondition, msgRereviewCapReached},
|
||||
{storepkg.ErrNotFound, codes.NotFound, storepkg.ErrNotFound.Error()},
|
||||
{storepkg.ErrInstalled, codes.AlreadyExists, storepkg.ErrInstalled.Error()},
|
||||
{storepkg.ErrRequired, codes.FailedPrecondition, storepkg.ErrRequired.Error()},
|
||||
|
||||
@@ -68,6 +68,13 @@ service StateStoreService {
|
||||
// Archive / Retry
|
||||
rpc ArchiveTask(ArchiveTaskRequest) returns (ArchiveTaskResponse);
|
||||
rpc RetryTask(RetryTaskRequest) returns (RetryTaskResponse);
|
||||
// RespondReviewGate is the operator's answer to the review gate
|
||||
// (docs/prds/pr-review-operator-response.md, Decision 2): one guarded
|
||||
// requeue that records the answer, increments rereview_rounds for a
|
||||
// re-review, and refuses a re-review past cap with the ErrRereviewCapReached
|
||||
// wire sentinel. Admin-only, like every other operator surface: a
|
||||
// task-scoped grant never reaches it.
|
||||
rpc RespondReviewGate(RespondReviewGateRequest) returns (RespondReviewGateResponse);
|
||||
rpc BeginRemediation(BeginRemediationRequest) returns (BeginRemediationResponse);
|
||||
rpc UpdateReviewPayload(UpdateReviewPayloadRequest) returns (UpdateReviewPayloadResponse);
|
||||
rpc SetReviewCursors(SetReviewCursorsRequest) returns (SetReviewCursorsResponse);
|
||||
@@ -341,6 +348,18 @@ message Task {
|
||||
// column that write drops (docs/prds/workflow-call-outputs.md, "Storage and
|
||||
// wire").
|
||||
string outputs_json = 38;
|
||||
// review_gate is the operator-approval gate's whole conversation with the
|
||||
// operator (docs/prds/pr-review-operator-response.md): the offer the gate
|
||||
// wrote before waiting, and the answer the response path filled in. It is
|
||||
// opaque here -- internal/domain/workflow/prreview owns the encoding -- but
|
||||
// it must cross so the dashboard can render the offer and the resumed run
|
||||
// can read the answer.
|
||||
string review_gate = 39;
|
||||
// rereview_rounds counts granted operator re-reviews, capped by
|
||||
// prreview.MaxRereviewRounds in the guarded response write. It is separate
|
||||
// from retry_count and remediation_rounds for the same reason those are
|
||||
// separate from each other.
|
||||
int64 rereview_rounds = 40;
|
||||
}
|
||||
|
||||
// Event mirrors internal/events.Event. Data is carried as a JSON object
|
||||
@@ -692,6 +711,22 @@ message RetryTaskRequest {
|
||||
}
|
||||
message RetryTaskResponse {}
|
||||
|
||||
message RespondReviewGateRequest {
|
||||
int64 task_id = 1;
|
||||
string from_status = 2;
|
||||
// gate is the whole review_gate document the caller computed: the offer
|
||||
// with the answer filled in. The store persists it unread.
|
||||
string gate = 3;
|
||||
// rereview increments rereview_rounds in the same write; an approve must
|
||||
// pass false and bumps nothing.
|
||||
bool rereview = 4;
|
||||
// cap is the domain's re-review bound (prreview.MaxRereviewRounds). It
|
||||
// crosses so the guard is enforced where the row is locked rather than by
|
||||
// the caller's stale read.
|
||||
int64 cap = 5;
|
||||
}
|
||||
message RespondReviewGateResponse {}
|
||||
|
||||
message BeginRemediationRequest {
|
||||
int64 task_id = 1;
|
||||
string payload = 2;
|
||||
|
||||
Reference in new issue
Block a user