Merge crew/pi-dashboard-sweep: agent-call children, call steps, and the waited-on callee sweep

# Conflicts:
#	internal/domain/workflow/workflow.go
#	internal/infrastructure/postgres/queries/steps.sql
This commit is contained in:
sam committed 2026-09-27 07:16:35 +10:00
commit b3e9159dce
16 files changed
+565 -91

No files matched your search

@@ -272,3 +272,95 @@ func TestRecoverStaleInterruptsRunningSteps(t *testing.T) {
t.Errorf("interrupted event = %+v, want a stage_finish row attributed to the interrupted attempt", interrupted[0])
}
}
// A wait:true call step is how the store knows the caller is still waiting on
// its callee: it closes only when the callee ends, so a non-terminal call
// step is a callee the caller waits on. Cancelling the caller sweeps each
// such callee through its own CancelExecution -- the callee's own lifecycle
// -- while a finished call step's callee (wait:false, or already ended) runs
// on. Trees never cross runs: a call step naming a task that is not the
// caller's callee cannot even be recorded (StartStep verifies the callee's
// call_parent_task_id).
func TestCancelExecutionCancelsTheCalleesTheCallerWaitsOn(t *testing.T) {
s := storeFor(t)
caller := runningExecution(t, s)
// The callee: started through the call linkage the way a wait:true step
// does (the store derives the callee from the caller's own row), running
// under its own lifecycle, with a running step of its own.
callee, err := s.StartCall(t.Context(), caller.ID, "implement", nil)
if err != nil {
t.Fatalf("EnqueueCallTask: %v", err)
}
if _, err := s.ClaimNext(t.Context()); err != nil {
t.Fatalf("ClaimNext callee: %v", err)
}
// The claim increments the attempt; read the row back the way the
// container's own request carries it.
callee, err = s.TaskByID(t.Context(), callee.ID)
if err != nil || callee == nil {
t.Fatalf("TaskByID(callee): %+v %v", callee, err)
}
if _, _, err := s.StartStep(t.Context(), task.StepStart{
ExecutionID: callee.ID, Attempt: callee.Attempt, Kind: task.StepKindStage, Name: "implement",
}); err != nil {
t.Fatalf("callee StartStep: %v", err)
}
// The caller's wait:true call step names the callee and is still running.
if _, _, err := s.StartStep(t.Context(), task.StepStart{
ExecutionID: caller.ID, Attempt: caller.Attempt, Kind: task.StepKindCall,
Name: "workflow.call", CalledExecutionID: callee.ID,
}); err != nil {
t.Fatalf("call step: %v", err)
}
// A wait:false call step's callee runs on: its call step already ended.
unwaited, err := s.StartCall(t.Context(), caller.ID, "implement", nil)
if err != nil {
t.Fatal(err)
}
if _, err := s.ClaimNext(t.Context()); err != nil {
t.Fatalf("ClaimNext no-wait callee: %v", err)
}
noWaitStep, _, err := s.StartStep(t.Context(), task.StepStart{
ExecutionID: caller.ID, Attempt: caller.Attempt, Kind: task.StepKindCall,
Name: "workflow.call", CalledExecutionID: unwaited.ID,
})
if err != nil {
t.Fatalf("no-wait call step: %v", err)
}
if _, err := s.FinishStep(t.Context(), task.StepFinish{
StepID: noWaitStep, ExecutionID: caller.ID,
From: taskstate.StepRunning, To: taskstate.StepSucceeded, Detail: "started; no wait",
}); err != nil {
t.Fatalf("close the no-wait call step: %v", err)
}
if _, err := s.CancelExecution(t.Context(), caller.ID, "stopped by operator", taskstate.Parked); err != nil {
t.Fatalf("CancelExecution: %v", err)
}
waited, err := s.TaskByID(t.Context(), callee.ID)
if err != nil || waited == nil {
t.Fatalf("TaskByID(callee): %+v %v", waited, err)
}
if waited.Status != taskstate.Parked {
t.Fatalf("the waited-on callee = %q, want parked under its own lifecycle", waited.Status)
}
waitedSteps, err := s.TaskEvents(t.Context(), callee.ID)
if err != nil {
t.Fatalf("callee TaskEvents: %v", err)
}
if len(waitedSteps) == 0 {
t.Error("the callee's steps were not cancelled with their events")
}
unwaitedAfter, err := s.TaskByID(t.Context(), unwaited.ID)
if err != nil || unwaitedAfter == nil {
t.Fatalf("TaskByID no-wait callee: %+v %v", unwaitedAfter, err)
}
if unwaitedAfter.Status != taskstate.Running {
t.Errorf("the wait:false callee = %q, want it running on", unwaitedAfter.Status)
}
}
@@ -357,3 +357,60 @@ func (q *Queries) StartStepExecution(ctx context.Context, id int64) (int64, erro
}
return result.RowsAffected(), nil
}
const stepCalleeParent = `-- name: StepCalleeParent :one
SELECT call_parent_task_id FROM tasks WHERE id = $1
`
// The caller linkage a call step's callee must carry: the callee's
// call_parent_task_id names the execution whose tree records the call. A
// call step naming a task that is not this execution's callee is refused,
// because a cancel of that execution would otherwise cancel work the
// caller never started.
func (q *Queries) StepCalleeParent(ctx context.Context, id int64) (int64, error) {
row := q.db.QueryRow(ctx, stepCalleeParent, id)
var call_parent_task_id int64
err := row.Scan(&call_parent_task_id)
return call_parent_task_id, err
}
const waitingCallSteps = `-- name: WaitingCallSteps :many
SELECT id, called_execution_id FROM step_executions
WHERE execution_id = $1 AND attempt = $2
AND kind = 'call' AND status IN ('pending', 'running') AND called_execution_id <> 0
`
type WaitingCallStepsParams struct {
ExecutionID int64
Attempt int64
}
type WaitingCallStepsRow struct {
ID int64
CalledExecutionID int64
}
// The call steps of the execution's current attempt that are still open and
// name a callee: a call step closes when the callee ends, so an open one is
// a callee the caller waits on (wait:true). The callee sweep of CancelExecution
// walks these, because a callee whose call step already closed -- wait:false,
// or one that ended before the cancel arrived -- runs on.
func (q *Queries) WaitingCallSteps(ctx context.Context, arg WaitingCallStepsParams) ([]WaitingCallStepsRow, error) {
rows, err := q.db.Query(ctx, waitingCallSteps, arg.ExecutionID, arg.Attempt)
if err != nil {
return nil, err
}
defer rows.Close()
var items []WaitingCallStepsRow
for rows.Next() {
var i WaitingCallStepsRow
if err := rows.Scan(&i.ID, &i.CalledExecutionID); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
@@ -16,6 +16,14 @@ INSERT INTO step_executions (org_id, workspace_id, execution_id, attempt, parent
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, 'pending')
RETURNING id;
-- name: StepCalleeParent :one
-- The caller linkage a call step's callee must carry: the callee's
-- call_parent_task_id names the execution whose tree records the call. A
-- call step naming a task that is not this execution's callee is refused,
-- because a cancel of that execution would otherwise cancel work the
-- caller never started.
SELECT call_parent_task_id FROM tasks WHERE id = $1;
-- name: StartStepExecution :execrows
-- The pending -> running half of StartStep, guarded by the row's own status:
-- the step transition table routes the pair, so a row that is not pending is
@@ -70,3 +78,12 @@ SELECT id, execution_id, attempt, parent_id, depth, kind, name, status, detail,
FROM step_executions
WHERE execution_id = @execution_id::bigint AND (@attempt::bigint = 0 OR attempt = @attempt::bigint)
ORDER BY attempt, id;
-- name: WaitingCallSteps :many
-- The call steps of the execution's current attempt that are still open and
-- name a callee: a call step closes when the callee ends, so an open one is
-- a callee the caller waits on (wait:true). The callee sweep of CancelExecution
-- walks these, because a callee whose call step already closed -- wait:false,
-- or one that ended before the cancel arrived -- runs on.
SELECT id, called_execution_id FROM step_executions
WHERE execution_id = $1 AND attempt = $2
AND kind = 'call' AND status IN ('pending', 'running') AND called_execution_id <> 0;
+55 -1
View File
@@ -56,6 +56,9 @@ func (s *Store) StartStep(ctx context.Context, start task.StepStart) (int64, eve
// execution no longer runs is describing a run that is over.
return 0, events.Event{}, storecontract.ErrStaleTransition
}
if err := s.verifyCallStep(ctx, q, start); err != nil {
return 0, events.Event{}, err
}
depth, err := s.startParent(ctx, q, start)
if err != nil {
@@ -72,7 +75,7 @@ func (s *Store) StartStep(ctx context.Context, start task.StepStart) (int64, eve
OrgID: execution.OrgID, WorkspaceID: execution.WorkspaceID,
ExecutionID: execution.ID, Attempt: execution.Attempt,
ParentID: stepParentID(start.ParentID), Depth: int32(depth.depth),
Kind: start.Kind, Name: start.Name, CalledExecutionID: 0,
Kind: start.Kind, Name: start.Name, CalledExecutionID: start.CalledExecutionID,
})
if err != nil {
return 0, events.Event{}, err
@@ -99,6 +102,28 @@ func (s *Store) StartStep(ctx context.Context, start task.StepStart) (int64, eve
return stepID, event, nil
}
// verifyCallStep refuses a call step whose callee is not this run's own:
// the callee's call_parent_task_id must name the recording execution, or the
// call step cannot record (a missing callee is the caller's stale view).
// Without this check a cancel of the recording run would sweep work it never
// started.
func (s *Store) verifyCallStep(ctx context.Context, q *postgresdb.Queries, start task.StepStart) error {
if start.CalledExecutionID == 0 {
return nil
}
calleeParent, err := q.StepCalleeParent(ctx, start.CalledExecutionID)
if errors.Is(err, pgx.ErrNoRows) {
return storecontract.ErrStaleTransition
}
if err != nil {
return err
}
if calleeParent != start.ExecutionID {
return storecontract.ErrStaleTransition
}
return nil
}
// stepParent resolves the enclosing StepExecution a child step names: its
// row is locked in the same transaction, the tree must stay inside one
// execution and one attempt, and depth is derived from the parent, never
@@ -293,6 +318,15 @@ func (s *Store) CancelExecution(ctx context.Context, taskID int64, reason, to st
return nil, fmt.Errorf("%w: %s -> %s is not a transition",
storecontract.ErrIllegalTransition, execution.Status, to)
}
// Read the wait:true callees before the sweep below closes them: an open
// call step is a callee the caller waits on, and one whose call step
// already closed -- wait:false, or one that ended first -- runs on.
waiting, err := q.WaitingCallSteps(ctx, postgresdb.WaitingCallStepsParams{
ExecutionID: taskID, Attempt: execution.Attempt,
})
if err != nil {
return nil, err
}
steps, err := q.CancelAttemptSteps(ctx, postgresdb.CancelAttemptStepsParams{
ExecutionID: taskID, Attempt: execution.Attempt,
@@ -330,5 +364,25 @@ func (s *Store) CancelExecution(ctx context.Context, taskID int64, reason, to st
for _, step := range steps {
ids = append(ids, step.ID)
}
if err := s.cancelWaitedCallees(ctx, waiting, reason, to); err != nil {
return ids, err
}
return ids, nil
}
// cancelWaitedCallees cancels each callee the caller was still waiting on
// through its own CancelExecution, under its own lifecycle, in the caller's
// write's aftermath -- the callee's own steps and its own move to the same
// destination. A chain of calls is bounded by the call depth the enqueue
// enforces, so the recursion is finite; a callee that cannot be cancelled is
// reported, and the caller's own record stands either way.
func (s *Store) cancelWaitedCallees(ctx context.Context, waiting []postgresdb.WaitingCallStepsRow, reason, to string) error {
var failures []error
for _, call := range waiting {
if _, err := s.CancelExecution(ctx, call.CalledExecutionID, reason, to); err != nil {
failures = append(failures, fmt.Errorf("cancel callee %d: %w", call.CalledExecutionID, err))
}
}
return errors.Join(failures...)
}