Files
archie/internal/infrastructure/postgres/tasks_outputs_test.go
T
sam 4afefe087e feat(workflow): declared workflow outputs for wait:true callers
A workflow declares named, typed outputs beside its inputs
(task.WorkflowInterface.Outputs). A run writes each one once through a
capture tool named after the output, or from a deterministic Go stage via
TaskContext.WriteOutput; the finish write persists them on tasks.outputs
before the outcome transition. A wait:true caller receives its successful
callee's outputs through WorkflowCallStatus and publishes one as its own
through a call step's outputs assignment.

Build authority: docs/prds/workflow-call-outputs.md.

- save time: WorkflowInterface.Validate checks output names and types;
  checkCallOutputs checks a call's assignment against both declarations
  and refuses outputs over a wait:false call.
- run time: undeclared, mistyped and required-but-missing values park the
  run; published callee values are re-checked against the caller's type.
- storage/wire: tasks.outputs (goose 0024), reset by each claim so the set
  is attempt-scoped; an encoded set past the structured-payload bound is
  refused, not clipped. Task.outputs_json and
  WorkflowCallStatusResponse.outputs_json carry it; task.Caller keeps its
  two methods.
- examples: firewall-contain calls firewall-investigate and publishes its
  summary as its own output.
2026-10-02 02:09:03 +10:00

130 lines
4.1 KiB
Go

// Declared workflow outputs on the task row: the attempt-scoped structured
// results a run writes (docs/prds/workflow-call-outputs.md, "Storage and
// wire").
package postgres
import (
"strings"
"testing"
"github.com/samcharles93/archie-core/internal/domain/workflow"
)
// newStoreForOutputs is a migrated pool with the Store built over it.
func newStoreForOutputs(t *testing.T) *Store {
t.Helper()
pool, _ := migrated(t)
return New(pool)
}
// TestTaskOutputs pins the task row half of the outputs model:
//
// - Update persists the run's written set, decoded exactly on the way out;
// - an encoded set past the structured-payload bound is refused, not
// clipped (clipped JSON does not parse);
// - a claim starts the next attempt with an empty set, so nothing a
// retried attempt does not rewrite survives.
func TestTaskOutputs(t *testing.T) {
t.Run("Update persists the run's outputs and TaskByID reads them back", func(t *testing.T) {
st := newStoreForOutputs(t)
ctx := t.Context()
task, err := st.EnqueueChatTask(ctx, "acme", "widget", "callee", "body", "w", "", nil)
if err != nil {
t.Fatal(err)
}
if _, err := st.ClaimNext(ctx); err != nil {
t.Fatal(err)
}
row, err := st.TaskByID(ctx, task.ID)
if err != nil {
t.Fatal(err)
}
row.Outputs = map[string]any{"contained": true, "n": "3"}
if err := st.Update(ctx, row); err != nil {
t.Fatalf("Update: %v", err)
}
back, err := st.TaskByID(ctx, task.ID)
if err != nil {
t.Fatal(err)
}
if back.Outputs["contained"] != true || back.Outputs["n"] != "3" {
t.Fatalf("row outputs = %+v, want the written set read back", back.Outputs)
}
})
t.Run("an encoded set past the structured-payload bound is refused", func(t *testing.T) {
st := newStoreForOutputs(t)
ctx := t.Context()
task, err := st.EnqueueChatTask(ctx, "acme", "widget", "callee", "body", "w", "", nil)
if err != nil {
t.Fatal(err)
}
if _, err := st.ClaimNext(ctx); err != nil {
t.Fatal(err)
}
row, err := st.TaskByID(ctx, task.ID)
if err != nil {
t.Fatal(err)
}
row.Outputs = map[string]any{"summary": strings.Repeat("x", 5000)}
err = st.Update(ctx, row)
if err == nil {
t.Fatal("Update accepted a set past the structured-payload bound; it must be refused, not clipped")
}
if !strings.Contains(err.Error(), "outputs") {
t.Fatalf("refusal = %v, want it to name the outputs", err)
}
})
t.Run("a retried attempt starts with an empty set", func(t *testing.T) {
st := newStoreForOutputs(t)
ctx := t.Context()
task, err := st.EnqueueChatTask(ctx, "acme", "widget", "callee", "body", "w", "", nil)
if err != nil {
t.Fatal(err)
}
first, err := st.ClaimNext(ctx)
if err != nil || first == nil {
t.Fatalf("first claim: (%+v, %v)", first, err)
}
if first.Outputs != nil {
t.Fatalf("first claim outputs = %+v, want the empty set the fresh attempt starts with", first.Outputs)
}
row, err := st.TaskByID(ctx, task.ID)
if err != nil {
t.Fatal(err)
}
row.Outputs = map[string]any{"contained": true}
if err := st.Update(ctx, row); err != nil {
t.Fatalf("Update: %v", err)
}
back, err := st.TaskByID(ctx, task.ID)
if err != nil || back.Outputs["contained"] != true {
t.Fatalf("setup: the attempt's outputs did not reach the row: (%+v, %v)", back.Outputs, err)
}
if err := st.Transition(ctx, task.ID, workflow.StatusRunning, workflow.StatusParked, "needs a look"); err != nil {
t.Fatal(err)
}
if err := st.RetryTask(ctx, task.ID, workflow.StatusParked, ""); err != nil {
t.Fatal(err)
}
second, err := st.ClaimNext(ctx)
if err != nil || second == nil {
t.Fatalf("retried claim: (%+v, %v)", second, err)
}
if task.ID != second.ID {
t.Skipf("another queued task claimed first; the idempotent queue handed out %d", second.ID)
}
if second.Outputs != nil {
t.Fatalf("retried attempt outputs = %+v, want the empty set", second.Outputs)
}
final, err := st.TaskByID(ctx, task.ID)
if err != nil {
t.Fatal(err)
}
if _, still := final.Outputs["contained"]; still {
t.Fatalf("row outputs = %+v, want the emptiness the new attempt starts with", final.Outputs)
}
})
}