Files
archie/internal/domain/eda/playbook/coordinator.go
T
sam e8356ed1dd feat(eda): playbook dispatch idempotency ledger
Add the playbook_dispatches at-most-once ledger (docs/prds/eda-playbook-engine.md
gap 2) and the record-then-invoke coordinator helper, with the cross-process
plumbing the State Store boundary requires.

- proto: RecordPlaybookDispatch and DeletePlaybookDispatches RPCs (string keys)
- storecontract: PlaybookDispatcher interface (no *sql.Tx, reuses ErrAlreadyDispatched)
- store: playbook_dispatches table + INSERT OR IGNORE dedup + delete-by-playbook
- staterpc: client/server methods and Deps field; error sentinel survives the hop
- eda/playbook: InvokeOnce record-before-invoke helper, keyed on
  (playbook, version, event, action) with a "1" action-position fallback

The ledger is administrative-only: task-scoped grants never reach it, matching
TaskGrants' Update/Transition/InsertEvent allowlist. Not wired into the daemon
workflow pin; the production call site arrives with the first side-effecting
position.
2026-09-22 04:36:28 +10:00

36 lines
1.3 KiB
Go

package playbook
import (
"context"
"errors"
"log/slog"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
)
// InvokeOnce runs one playbook action at most once per
// (playbook, version, event, action) tuple, record-before-invoke: the ledger
// row is committed before fn runs, so a redelivered action (the same
// structural key) skips without repeating a non-revocable side effect
// (docs/prds/eda-playbook-engine.md gap 2). The structural key is computed
// from Decision.PlaybookID, Decision.Version, input.TaskID (the event_id) and
// Decision.ActionID, falling back to the 1-based action position ("1") when
// the action declares no id -- it is never a CEL expression.
func InvokeOnce(ctx context.Context, ledger storecontract.PlaybookDispatcher, log *slog.Logger, decision Decision, input DispatchInput, fn func(context.Context) error) error {
actionID := decision.ActionID
if actionID == "" {
actionID = "1"
}
err := ledger.RecordPlaybookDispatch(ctx, decision.PlaybookID, decision.Version, input.TaskID, actionID)
if err != nil {
if errors.Is(err, storecontract.ErrAlreadyDispatched) {
if log != nil {
log.Debug("playbook action already dispatched; skipping", "playbook", decision.PlaybookID, "version", decision.Version, "event_id", input.TaskID, "action_id", actionID)
}
return nil
}
return err
}
return fn(ctx)
}