Files
archie/internal/infrastructure/postgres/workflow_call_test.go
T
sam 703aea4568 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.
2026-10-08 22:31:07 +11:00

123 lines
4.0 KiB
Go

package postgres_test
import (
"testing"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
"github.com/samcharles93/archie-core/internal/taskstate"
)
// 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)
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-a", "callee-a", nil)
if err != nil {
t.Fatal(err)
}
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, "check@callee-b", "callee-b", nil)
if err != nil {
t.Fatal(err)
}
if other.ID == first.ID {
t.Fatalf("the same path starting another workflow returned the first child, %d", first.ID)
}
var rows int
if err := db.Pool.QueryRow(ctx,
`SELECT count(*) FROM tasks WHERE call_parent_task_id = $1`, caller.ID).Scan(&rows); err != nil {
t.Fatal(err)
}
if rows != 2 {
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)
}
})
}
}