feat(eda): run action playbooks at pin time through the at-most-once dispatch ledger

This commit is contained in:
sam committed 2026-09-23 22:00:52 +10:00
1 parent 6046948ff0
commit 2bc4b8db32
11 files changed
+517 -95

No files matched your search

+19
View File
@@ -220,7 +220,11 @@ type Daemon struct {
// and routing is exactly the binding behaviour.
Playbooks interface {
Dispatch(playbook.DispatchInput) (playbook.Decision, bool)
Run(context.Context, storecontract.PlaybookDispatcher, *slog.Logger, playbook.DispatchInput) error
}
// PlaybookLedger is the at-most-once gate action playbooks run through.
// Nil means action playbooks do not run: they never fire unguarded.
PlaybookLedger storecontract.PlaybookDispatcher
// WorkflowDefinitions supplies the active database definitions. A task is
// pinned once before dispatch; retries reuse the task's stored YAML.
@@ -1552,6 +1556,7 @@ func (d *Daemon) pinWorkflowDefinition(ctx context.Context, task *workflow.Task)
}
return nil
}
d.runActionPlaybooks(ctx, task)
if d.WorkflowDefinitions == nil {
return d.pinWorkflowFromCollection(ctx, task, workflow.ShippedDefinitions(), 0)
}
@@ -1562,6 +1567,20 @@ func (d *Daemon) pinWorkflowDefinition(ctx context.Context, task *workflow.Task)
return d.pinWorkflowFromCollection(ctx, task, collection, version)
}
// runActionPlaybooks runs the action playbooks matching a task that is about
// to be pinned, which happens once per task (docs/prds/action-playbook-run.md).
// A run never affects routing and its failure never fails the task. A task
// that already names a workflow is the approval requeue and is skipped, as
// it is for workflow playbooks.
func (d *Daemon) runActionPlaybooks(ctx context.Context, task *workflow.Task) {
if d.Playbooks == nil || d.PlaybookLedger == nil || task.Workflow != "" {
return
}
if err := d.Playbooks.Run(ctx, d.PlaybookLedger, d.Log, playbookInput(task)); err != nil {
d.Log.Warn("action playbook run failed", "task", task.ID, "err", err)
}
}
// resolveWorkflowID picks the definition id to pin. A playbook whose trigger
// matches the task decides, ahead of the kind/label bindings; anything else
// falls through to workflow.ResolveWorkflowID unchanged.
+80
View File
@@ -0,0 +1,80 @@
package daemon
import (
"context"
"errors"
"log/slog"
"testing"
"github.com/samcharles93/archie-core/internal/domain/eda/playbook"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/store"
)
// runRecorder is a playbook set that matches no workflow playbook and records
// each action-playbook run, failing it when err is set.
type runRecorder struct {
runs []playbook.DispatchInput
err error
}
func (r *runRecorder) Dispatch(playbook.DispatchInput) (playbook.Decision, bool) {
return playbook.Decision{}, false
}
func (r *runRecorder) Run(_ context.Context, _ storecontract.PlaybookDispatcher, _ *slog.Logger, input playbook.DispatchInput) error {
r.runs = append(r.runs, input)
return r.err
}
type nopPlaybookLedger struct{}
func (nopPlaybookLedger) RecordPlaybookDispatch(context.Context, string, string, string, string) error {
return nil
}
func (nopPlaybookLedger) DeletePlaybookDispatches(context.Context, string) error { return nil }
func TestPinWorkflowDefinitionRunsActionPlaybooks(t *testing.T) {
tests := []struct {
name string
ledger storecontract.PlaybookDispatcher
workflow string
runErr error
wantRuns int
}{
{name: "runs once with a ledger", ledger: nopPlaybookLedger{}, wantRuns: 1},
{name: "a failed run leaves routing alone", ledger: nopPlaybookLedger{}, runErr: errors.New("boom"), wantRuns: 1},
{name: "never runs without a ledger", wantRuns: 0},
{name: "skips a task that already names a workflow", ledger: nopPlaybookLedger{}, workflow: "tdd", wantRuns: 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
resources := store.OpenTest(t)
defer resources.Close()
task := forgeTask(t, resources, "bug")
task.Workflow = tt.workflow
recorder := &runRecorder{err: tt.runErr}
d := &Daemon{
Store: resources,
WorkflowDefinitions: routableDefinitions(),
Log: slog.New(slog.DiscardHandler),
Playbooks: recorder,
PlaybookLedger: tt.ledger,
}
if err := d.pinWorkflowDefinition(t.Context(), task); err != nil {
t.Fatal(err)
}
// A second pin reuses the stored definition and must not run again.
if err := d.pinWorkflowDefinition(t.Context(), task); err != nil {
t.Fatal(err)
}
if len(recorder.runs) != tt.wantRuns {
t.Fatalf("runs = %d, want %d", len(recorder.runs), tt.wantRuns)
}
if task.Workflow != "tdd" {
t.Fatalf("pinned workflow = %q, want tdd (the kind binding, untouched by the run)", task.Workflow)
}
})
}
}