fix(workflow): key a call on its callee, and free a terminal child's key
A call's key was the step's declared path alone, so one task running a second definition at a colliding path -- workflow.handoff, human.approve and a definition re-pin all change it -- was handed the first callee's child, waited on the wrong workflow and published its outputs. The key now carries the callee too, while the step row and the call's events stay named and staged for the call site, so a rail pairing an event with its step still attaches them. A child that reached dead or closed_wont_do can never do the work and holds no result the caller can use, so it leaves the call key's index and frees the key for the caller's retry to start a fresh child. parked and rejected keep theirs: a parked child is recoverable by retrying it, and a rejected child ran. The id a call step must declare is now refused where the definition is compiled, which is also the path a pinned definition takes, so an id that spells a step type is accepted again and no stage needs a heuristic.
This commit is contained in:
1 parent
0101947131
commit
703aea4568
13 files changed
+302
-109
No files matched your search
@@ -1,14 +1,23 @@
|
||||
-- tasks.call_key is a workflow.call call site's durable identity: the call
|
||||
-- step's declared path within the calling run. With the parent it is the key
|
||||
-- EnqueueCallTask upserts on, so a retry of one call site -- a lost
|
||||
-- EnqueueCallTask reply, a failed step record, a requeued attempt -- returns
|
||||
-- the child already started instead of starting a second one. The index is
|
||||
-- partial: every task that is not a workflow.call callee carries the empty key.
|
||||
-- step's declared path within the calling run, plus the callee it starts. With
|
||||
-- the parent it is the key EnqueueCallTask upserts on, so a retry of one call
|
||||
-- site -- a lost EnqueueCallTask reply, a failed step record, a requeued
|
||||
-- attempt -- returns the child already started instead of starting a second
|
||||
-- one. The index is partial: every task that is not a workflow.call callee
|
||||
-- carries the empty key.
|
||||
--
|
||||
-- A child that reached dead or closed_wont_do (taskstate.Dead and
|
||||
-- taskstate.Declined) can never do the work and holds no result the caller can
|
||||
-- use, so it leaves the index and frees its key for the caller's retry to
|
||||
-- start a fresh child. parked and rejected stay in: a parked child is
|
||||
-- recoverable by retrying the child -- which is what the caller's own park
|
||||
-- names -- and a rejected child ran, so reusing it is what the call site wants.
|
||||
|
||||
-- +goose Up
|
||||
|
||||
ALTER TABLE tasks ADD COLUMN call_key text NOT NULL DEFAULT '';
|
||||
CREATE UNIQUE INDEX idx_tasks_call_key ON tasks (call_parent_task_id, call_key) WHERE call_key <> '';
|
||||
CREATE UNIQUE INDEX idx_tasks_call_key ON tasks (call_parent_task_id, call_key)
|
||||
WHERE call_key <> '' AND status NOT IN ('dead', 'closed_wont_do');
|
||||
|
||||
-- +goose Down
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ SELECT
|
||||
caller.id, caller.call_depth + 1, $7
|
||||
FROM caller
|
||||
WHERE caller.status = 'running' AND caller.call_depth + 1 <= $8::int
|
||||
ON CONFLICT (call_parent_task_id, call_key) WHERE call_key <> '' DO UPDATE SET call_key = EXCLUDED.call_key
|
||||
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
|
||||
`
|
||||
|
||||
@@ -60,9 +60,11 @@ type EnqueueCallTaskParams struct {
|
||||
//
|
||||
// The call key makes this the one write that can be retried: a call site that
|
||||
// already started its child gets that child back rather than a second one. The
|
||||
// upsert only fires for a keyed callee, and the WHERE below still decides
|
||||
// whether the caller may call at all -- a caller that is missing, not running
|
||||
// or past the depth limit inserts nothing and returns no row, conflict or not.
|
||||
// upsert only fires for a keyed callee whose child can still do the work -- a
|
||||
// child that ended dead or closed_wont_do freed its key -- and the WHERE below
|
||||
// still decides whether the caller may call at all: a caller that is missing,
|
||||
// not running or past the depth limit inserts nothing and returns no row,
|
||||
// conflict or not.
|
||||
func (q *Queries) EnqueueCallTask(ctx context.Context, arg EnqueueCallTaskParams) (Task, error) {
|
||||
row := q.db.QueryRow(ctx, enqueueCallTask,
|
||||
arg.ID,
|
||||
|
||||
@@ -3,9 +3,11 @@
|
||||
--
|
||||
-- The call key makes this the one write that can be retried: a call site that
|
||||
-- already started its child gets that child back rather than a second one. The
|
||||
-- upsert only fires for a keyed callee, and the WHERE below still decides
|
||||
-- whether the caller may call at all -- a caller that is missing, not running
|
||||
-- or past the depth limit inserts nothing and returns no row, conflict or not.
|
||||
-- upsert only fires for a keyed callee whose child can still do the work -- a
|
||||
-- child that ended dead or closed_wont_do freed its key -- and the WHERE below
|
||||
-- still decides whether the caller may call at all: a caller that is missing,
|
||||
-- not running or past the depth limit inserts nothing and returns no row,
|
||||
-- conflict or not.
|
||||
WITH caller AS (
|
||||
SELECT t.* FROM tasks t WHERE t.id = $1 FOR UPDATE
|
||||
)
|
||||
@@ -24,7 +26,7 @@ SELECT
|
||||
caller.id, caller.call_depth + 1, sqlc.arg('call_key')
|
||||
FROM caller
|
||||
WHERE caller.status = 'running' AND caller.call_depth + 1 <= sqlc.arg('max_depth')::int
|
||||
ON CONFLICT (call_parent_task_id, call_key) WHERE call_key <> '' DO UPDATE SET call_key = EXCLUDED.call_key
|
||||
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 *;
|
||||
|
||||
-- name: CallStatusDetail :one
|
||||
|
||||
@@ -783,8 +783,9 @@ func (s *Store) TaskByID(ctx context.Context, taskID int64) (*workflow.Task, err
|
||||
|
||||
// StartCall enqueues a workflow.call callee inheriting the caller's org,
|
||||
// workspace, identity and repo, and returns the callee already started under
|
||||
// callerTaskID and callKey when there is one. The caller must be running and
|
||||
// within workflow.MaxCallDepth.
|
||||
// callerTaskID and callKey when there is one. A child that ended dead or
|
||||
// declined freed its key, so such a call starts a fresh child. The caller must
|
||||
// be running and within workflow.MaxCallDepth.
|
||||
func (s *Store) StartCall(ctx context.Context, callerTaskID int64, callKey string, wf string, inputs map[string]any) (*workflow.Task, error) {
|
||||
encoded, err := task.EncodeInputs(inputs)
|
||||
if err != nil {
|
||||
|
||||
@@ -4,12 +4,14 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
"github.com/samcharles93/archie-core/internal/taskstate"
|
||||
)
|
||||
|
||||
// StartCall is idempotent per call site: the same caller and call key return
|
||||
// the child already started, so a retry of a lost EnqueueCallTask reply, or of
|
||||
// the stage that made the call, does not start a second child and run its work
|
||||
// twice. A different call site in the same run still gets its own child.
|
||||
// StartCall is idempotent per call key: the same caller and key return the
|
||||
// child already started, so a retry of a lost EnqueueCallTask reply, or of the
|
||||
// stage that made the call, does not start a second child and run its work
|
||||
// twice. Another call site -- the same declared path starting a different
|
||||
// workflow -- still gets its own child.
|
||||
func TestStartCallReturnsOneChildPerCallKey(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
db := pgstore.Open(t)
|
||||
@@ -21,23 +23,23 @@ func TestStartCallReturnsOneChildPerCallKey(t *testing.T) {
|
||||
t.Fatalf("claim: %v", err)
|
||||
}
|
||||
|
||||
first, err := db.StartCall(ctx, caller.ID, "check", "callee", nil)
|
||||
first, err := db.StartCall(ctx, caller.ID, "check@callee-a", "callee-a", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
retry, err := db.StartCall(ctx, caller.ID, "check", "callee", nil)
|
||||
retry, err := db.StartCall(ctx, caller.ID, "check@callee-a", "callee-a", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if retry.ID != first.ID {
|
||||
t.Fatalf("the retry started task %d, want the child already started, %d", retry.ID, first.ID)
|
||||
}
|
||||
other, err := db.StartCall(ctx, caller.ID, "verify", "callee", nil)
|
||||
other, err := db.StartCall(ctx, caller.ID, "check@callee-b", "callee-b", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if other.ID == first.ID {
|
||||
t.Fatalf("a different call key returned the first child, %d", first.ID)
|
||||
t.Fatalf("the same path starting another workflow returned the first child, %d", first.ID)
|
||||
}
|
||||
|
||||
var rows int
|
||||
@@ -49,3 +51,72 @@ func TestStartCallReturnsOneChildPerCallKey(t *testing.T) {
|
||||
t.Fatalf("the caller started %d children, want one per call key (2)", rows)
|
||||
}
|
||||
}
|
||||
|
||||
// A child that reached a hard terminal failure can never do the work, so it
|
||||
// frees its key: it left no result the caller can use, and the caller's retry
|
||||
// starting a fresh child duplicates nothing. A parked child keeps the key --
|
||||
// it is recoverable by retrying the child, which is what the caller's own park
|
||||
// names.
|
||||
func TestStartCallFreesTheKeyOfAHardTerminalChild(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
// move takes the child out of queued the way its real ending does.
|
||||
move func(t *testing.T, db *pgstore.TaskDB, childID int64)
|
||||
wantNew bool
|
||||
}{
|
||||
{
|
||||
name: "a declined child frees its key",
|
||||
move: func(t *testing.T, db *pgstore.TaskDB, childID int64) {
|
||||
t.Helper()
|
||||
if err := db.Transition(t.Context(), childID, taskstate.Queued, taskstate.Declined, "the operator declined it"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
},
|
||||
wantNew: true,
|
||||
},
|
||||
{
|
||||
name: "a parked child keeps its key",
|
||||
move: func(t *testing.T, db *pgstore.TaskDB, childID int64) {
|
||||
t.Helper()
|
||||
// Parked is reachable only from running, so the child is
|
||||
// claimed first, as a worker would claim it.
|
||||
claimed, err := db.ClaimNext(t.Context())
|
||||
if err != nil || claimed == nil || claimed.ID != childID {
|
||||
t.Fatalf("claim child %d: %v, got %v", childID, err, claimed)
|
||||
}
|
||||
if err := db.ParkTask(t.Context(), childID, taskstate.Running, "needs an operator", taskstate.ParkNeedsHuman); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
db := pgstore.Open(t)
|
||||
if _, err := db.EnqueueChatTask(ctx, "", "", "caller", "", "tdd", "", "", nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
caller, err := db.ClaimNext(ctx)
|
||||
if err != nil || caller == nil {
|
||||
t.Fatalf("claim: %v", err)
|
||||
}
|
||||
first, err := db.StartCall(ctx, caller.ID, "check@callee", "callee", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tt.move(t, db, first.ID)
|
||||
|
||||
retry, err := db.StartCall(ctx, caller.ID, "check@callee", "callee", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if tt.wantNew && retry.ID == first.ID {
|
||||
t.Fatalf("the retry returned the child that can never do the work, %d, want a fresh child", first.ID)
|
||||
}
|
||||
if !tt.wantNew && retry.ID != first.ID {
|
||||
t.Fatalf("the retry started task %d, want the parked child it can retry, %d", retry.ID, first.ID)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user