fix(eda): tie module results to checker and tighten action ids
This commit is contained in:
11 files changed
+434
-83
No files matched your search
@@ -628,10 +628,9 @@ the side-effecting positions are gated.
|
||||
`body`, `source`, built by `playbookInput` in `internal/daemon`.
|
||||
|
||||
The trigger shape reuses the existing workintake kind/label vocabulary.
|
||||
Multi-action and non-workflow-position playbooks are rejected at load
|
||||
(hard boundary), so Module/Channel/Forge positions have no production
|
||||
dispatch path: the gap-2 `playbook_dispatches` ledger must land before a
|
||||
side-effecting position can fire.
|
||||
The single-action hard boundary is superseded by
|
||||
`docs/prds/multi-action-playbooks.md` (two-shape rule); Module, Channel,
|
||||
and Forge positions still have no production dispatch path.
|
||||
## Standing constraint
|
||||
|
||||
This may be commercialized, and is to be discernible from other open-source
|
||||
|
||||
@@ -46,9 +46,10 @@ external side effect, and the Channel and Forge positions, are out of scope.
|
||||
|
||||
Each Module kind owns a hand-written `Args` and `Result` struct
|
||||
(`internal/domain/eda/module/log` is the worked example); `ModuleRegistry.Invoke`
|
||||
returns the result as `map[string]any`. The run marshals that map into the
|
||||
kind's `Result` before a later expression reads it, so the value the checker
|
||||
validates and the value the run reads have one definition.
|
||||
returns the result as a flat `map[string]any`. `ModuleRegistry.DecodeResult`
|
||||
(`internal/domain/eda/module`) is the single site that converts that map into
|
||||
the kind's `Result` struct before a later expression reads it, so the value the
|
||||
checker validates and the value the run reads have one definition.
|
||||
|
||||
`actions.<id>` is typed by the kind of the action that declares `<id>`:
|
||||
`{ result: <KindResult> }`. `event` stays `dyn` (J4,
|
||||
@@ -67,12 +68,13 @@ naming the playbook and the offending action, for:
|
||||
- a `result` field the referenced kind's `Result` does not define;
|
||||
- a reference to an earlier action that produces no result;
|
||||
- an unknown `position`, an unknown `kind`, or a `module` action with no `kind`;
|
||||
- `args` that do not type-check against the kind's `Args` schema, or an arg key
|
||||
the schema does not define.
|
||||
- an arg KEY the kind's `Args` schema does not define. Arg VALUE type-checking
|
||||
is tracked as `archie-core-t2db` debt, not shipped here.
|
||||
|
||||
The last two are new. Declaring the kind's `Args` and `Result` schemas to CEL at
|
||||
load makes an arg or result typo a load failure rather than a dispatch failure,
|
||||
which is the field-level rejection `playbook-expression-syntax.md` chose CEL for.
|
||||
Declaring the kind's `Result` schema to CEL at load makes a result-field typo a
|
||||
load failure rather than a dispatch failure. Matching arg keys against the
|
||||
kind's `Args` schema makes an arg-key typo a load failure; arg value
|
||||
type-checking is tracked as `archie-core-t2db` debt.
|
||||
|
||||
## The environment is per playbook
|
||||
|
||||
@@ -88,40 +90,26 @@ This document makes an action playbook load and its references type-check. It
|
||||
does not invoke a Module kind. Invocation, the `playbook_dispatches` gate, and
|
||||
stop-on-first-failure across actions are `archie-core-t2db.31`.
|
||||
|
||||
## Decisions required
|
||||
## Decisions
|
||||
|
||||
**D1 — dispatch before `t2db.31`.** An action playbook has no run path yet.
|
||||
Either the definition-pin dispatch considers only workflow playbooks and reports
|
||||
no match for an action playbook (loaded and validated, not routed), or the loader
|
||||
refuses an action playbook until `t2db.31` lands. The first ships the loader and
|
||||
the checker now but leaves a playbook that does nothing; the second keeps
|
||||
"loadable means runnable" but cannot ship this work alone. Recommendation: the
|
||||
first, with the daemon logging once that an action playbook has no run path.
|
||||
**D1 — dispatch before `t2db.31`.** Settled: an action playbook loads and
|
||||
validates but is not routed; the definition-pin dispatch reports no match for it,
|
||||
and the daemon logs a visible warning naming it.
|
||||
|
||||
**D2 — mixing positions.** May one playbook mix `workflow` and `module` actions?
|
||||
A `workflow` action yields a task, not a result, so it cannot feed
|
||||
`actions.<id>.result`, and its "invoke" is the definition pin rather than an
|
||||
action call. Recommendation: one shape per playbook for this slice.
|
||||
**D2 — mixing positions.** Settled: one shape per playbook. A `workflow` action
|
||||
yields a task, not a result, so it cannot feed `actions.<id>.result`.
|
||||
|
||||
**D3 — the CEL typing mechanism.** Two candidates: build a per-playbook
|
||||
environment whose `actions` root is an object type with one field per declared
|
||||
id, each field typed by the kind's `Result` (cel-go native or provider-defined
|
||||
types); or keep `actions` as `map(string, dyn)` and validate every static
|
||||
`actions.<id>.result.<field>` path after compiling against an id-to-result-fields
|
||||
map, in the same AST walk that already classifies `actions` references. The
|
||||
first is CEL's own checker and costs a per-playbook environment; the second
|
||||
keeps one environment and one compile path but checks fields outside CEL.
|
||||
Recommendation: the first if cel-go can declare the ids' types without a custom
|
||||
`ref.Val` adapter; otherwise the second, which is a bounded path check over a
|
||||
schema map, not the re-implemented type system the parent document rejected.
|
||||
**D3 — the CEL typing mechanism.** Settled: a per-playbook environment whose
|
||||
`actions` root is an object type with one field per declared id, each field typed
|
||||
by the kind's `Result` struct. `internal/domain/eda/expr` implements it with the
|
||||
generic `actionResultProvider`.
|
||||
|
||||
**D4 — `args` against the kind's `Args` schema.** This document assumes the kind's
|
||||
`Args` schema is declared to CEL as well, so an arg typo or wrong type fails the
|
||||
load. Confirm, or split that check into its own bead.
|
||||
**D4 — `args` against the kind's `Args` schema.** Settled: arg KEY checking ships
|
||||
now. Arg value type-checking is tracked as `archie-core-t2db` debt.
|
||||
|
||||
**D5 — where the result map becomes the `Result` struct.** `ModuleRegistry.Invoke`
|
||||
returns `map[string]any`. Confirm the marshalling site: the coordinator, the
|
||||
registry, or the kind itself.
|
||||
**D5 — where the result map becomes the `Result` struct.** Settled:
|
||||
`ModuleRegistry` stays schema-agnostic. `ModuleRegistry.DecodeResult`
|
||||
(`internal/domain/eda/module`) is the conversion site.
|
||||
|
||||
## Verification
|
||||
|
||||
@@ -129,6 +117,7 @@ registry, or the kind itself.
|
||||
loads; the same read of a field the kind's `Result` does not define fails the
|
||||
load naming the field; a read of an id no earlier action declares fails; a
|
||||
dynamically indexed `actions` read fails.
|
||||
- An `args` key or value the kind's `Args` schema rejects fails the load.
|
||||
- An arg KEY the kind's `Args` schema does not define fails the load. Arg VALUE
|
||||
type-checking is tracked as `archie-core-t2db` debt.
|
||||
- The one-action `workflow` playbook shape, its dispatch, and its existing
|
||||
load-time checks are unchanged.
|
||||
@@ -2,13 +2,13 @@
|
||||
#
|
||||
# Install: copy this file into the configured eda_playbook_dir (default
|
||||
# ~/.config/archie/eda-playbooks) and restart archied. It is loaded at
|
||||
# startup with the reject-at-load rule: any malformed playbook, multi-action
|
||||
# playbook, non-workflow action position, or `when` compile failure aborts
|
||||
# startup with the reject-at-load rule: any malformed playbook, a mixed
|
||||
# workflow/module action shape, or a `when`/args compile failure aborts
|
||||
# startup with a reported error -- a partially-valid set never runs.
|
||||
#
|
||||
# This slice supports exactly ONE action per playbook, position: workflow.
|
||||
# Multi-action playbooks and Module/Channel/Forge action positions stay
|
||||
# blocked on the unresolved execution-time gaps and are rejected at load.
|
||||
# This file is the workflow playbook shape: exactly one action, position:
|
||||
# workflow. Action playbooks (one or more module actions) load and type-check
|
||||
# but do not execute yet -- see docs/prds/multi-action-playbooks.md.
|
||||
#
|
||||
# A trigger matches on either axis, and both are checked when both are given:
|
||||
#
|
||||
|
||||
@@ -780,7 +780,7 @@ func (b *boot) loadEDAPlaybooks(cfg config.Config, log *slog.Logger) error {
|
||||
return err
|
||||
}
|
||||
for _, pb := range b.playbooks.Playbooks {
|
||||
if len(pb.Actions) != 1 || pb.Actions[0].Position != "workflow" {
|
||||
if pb.IsActionPlaybook() {
|
||||
log.Warn("eda action playbook loaded: action playbooks do not execute yet", "playbook", pb.ID)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,3 +48,33 @@ actions:
|
||||
t.Errorf("warning output was logged at debug, want a visible warning: %q", out)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadEDAPlaybooksNoWarningOnWorkflowPlaybook is the inverse of the D1
|
||||
// warning: a workflow playbook uses the same two-shape predicate but must not
|
||||
// be logged as a non-routing action playbook.
|
||||
func TestLoadEDAPlaybooksNoWarningOnWorkflowPlaybook(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "pb.yaml"), []byte(`trigger:
|
||||
kind: bug
|
||||
actions:
|
||||
- position: workflow
|
||||
workflow: tdd
|
||||
`), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
log := slog.New(slog.NewTextHandler(&buf, nil))
|
||||
b := &boot{cfg: config.Config{EDAPlaybookDir: dir}, modules: module.New()}
|
||||
if err := b.loadEDAPlaybooks(b.cfg, log); err != nil {
|
||||
t.Fatalf("loadEDAPlaybooks: %v", err)
|
||||
}
|
||||
|
||||
out := buf.String()
|
||||
if strings.Contains(out, "do not execute yet") {
|
||||
t.Errorf("workflow playbook was warned as an action playbook: %q", out)
|
||||
}
|
||||
if strings.Contains(out, "level=WARN") {
|
||||
t.Errorf("workflow playbook produced a warning: %q", out)
|
||||
}
|
||||
}
|
||||
@@ -197,12 +197,12 @@ type Daemon struct {
|
||||
// bindings the daemon loaded; nil means built-in defaults.
|
||||
KindWorkflows workflow.KindWorkflows
|
||||
LabelWorkflows workflow.LabelWorkflows
|
||||
// Playbooks is the loaded EDA playbook set (trigger + one workflow-kind
|
||||
// action, internal/domain/eda/playbook). It is consulted before the
|
||||
// Playbooks is the loaded EDA playbook set (workflow and action
|
||||
// playbooks, internal/domain/eda/playbook). It is consulted before the
|
||||
// kind/label bindings when a task's workflow definition is pinned: a
|
||||
// playbook is an operator's explicit rule for one trigger, where the
|
||||
// bindings are a table of defaults. Nil means no playbooks are loaded and
|
||||
// routing is exactly the binding behaviour.
|
||||
// workflow playbook is an operator's explicit rule for one trigger, where
|
||||
// the bindings are a table of defaults. Nil means no playbooks are loaded
|
||||
// and routing is exactly the binding behaviour.
|
||||
Playbooks interface {
|
||||
Dispatch(playbook.DispatchInput) (playbook.Decision, bool)
|
||||
}
|
||||
|
||||
@@ -36,8 +36,9 @@ const DefaultCostLimit = 100_000
|
||||
// DeclaredResult is one prior action's result the environment exposes to a
|
||||
// later expression. ID is the action's declared id, read as
|
||||
// `actions.<ID>.result.<field>`; Type is the reflect.Type of that action
|
||||
// kind's Go Result struct (the value the run marshals into the per-id
|
||||
// `{result: ...}` map before a later expression reads it).
|
||||
// kind's Go Result struct. `module.ModuleRegistry.DecodeResult` is the site
|
||||
// that converts Invoke's flat result map back into this struct before a later
|
||||
// expression reads it.
|
||||
type DeclaredResult struct {
|
||||
ID string
|
||||
Type reflect.Type
|
||||
@@ -61,8 +62,9 @@ type Context struct {
|
||||
Event map[string]any
|
||||
// Actions holds prior actions' results keyed by the action's id as
|
||||
// declared in the playbook. Each id maps to the per-id wrapper
|
||||
// `{"result": <KindResult struct>}` the run produces, so a later
|
||||
// expression reads `actions.<id>.result.<field>`.
|
||||
// `{"result": <KindResult struct>}` built by
|
||||
// `module.ModuleRegistry.DecodeResult`, so a later expression reads
|
||||
// `actions.<id>.result.<field>`.
|
||||
Actions map[string]map[string]any
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/traefik/yaegi/interp"
|
||||
|
||||
@@ -212,7 +213,10 @@ func decodeLogArgs(rawArgs map[string]any) (log.Args, error) {
|
||||
}
|
||||
|
||||
// Invoke calls the registered kind with rawArgs and returns the marshaled
|
||||
// result. An unregistered kind is a reported error.
|
||||
// result. An unregistered kind is a reported error. Invoke is deliberately
|
||||
// schema-agnostic: it returns the flat map[string]any the kind produces and
|
||||
// does not re-apply the Result schema. DecodeResult is the single conversion
|
||||
// site that re-applies it for the typed CEL environment.
|
||||
func (r *ModuleRegistry) Invoke(ctx context.Context, kind string, rawArgs map[string]any) (map[string]any, error) {
|
||||
inv, ok := r.kinds[kind]
|
||||
if !ok {
|
||||
@@ -224,3 +228,71 @@ func (r *ModuleRegistry) Invoke(ctx context.Context, kind string, rawArgs map[st
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// DecodeResult converts Invoke's flat result map into the kind's registered
|
||||
// Result struct. Invoke stays schema-agnostic and returns map[string]any; this
|
||||
// method is the one place the Result schema is re-applied so the value a later
|
||||
// CEL expression reads is the same Go struct the expression environment typed
|
||||
// (`actions.<id>.result.<field>`). The map is keyed by the lower-cased Go
|
||||
// field name, the same spelling expr registers as the CEL field name. An
|
||||
// unknown field or a wrong-typed value is a reported error.
|
||||
func (r *ModuleRegistry) DecodeResult(kind string, raw map[string]any) (any, error) {
|
||||
k, ok := registry[kind]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("module: unknown kind %q", kind)
|
||||
}
|
||||
return decodeResultStruct(kind, k.resultType, raw)
|
||||
}
|
||||
|
||||
// decodeResultStruct marshals raw into a new value of the kind's Result
|
||||
// struct type, keyed by the lower-cased Go field name -- the same spelling
|
||||
// expr registers as the CEL field name.
|
||||
func decodeResultStruct(kind string, t reflect.Type, raw map[string]any) (any, error) {
|
||||
if t == nil {
|
||||
return nil, fmt.Errorf("module %s: result schema is not set", kind)
|
||||
}
|
||||
if t.Kind() == reflect.Pointer {
|
||||
t = t.Elem()
|
||||
}
|
||||
if t.Kind() != reflect.Struct {
|
||||
return nil, fmt.Errorf("module %s: result type %s is not a struct", kind, t)
|
||||
}
|
||||
|
||||
out := reflect.New(t).Elem()
|
||||
fields := make(map[string]reflect.Value, t.NumField())
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
f := t.Field(i)
|
||||
if !out.Field(i).CanSet() {
|
||||
continue
|
||||
}
|
||||
fields[strings.ToLower(f.Name)] = out.Field(i)
|
||||
}
|
||||
for key, val := range raw {
|
||||
fv, ok := fields[key]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("module %s: unknown result field %q", kind, key)
|
||||
}
|
||||
if err := setResultField(kind, key, fv, val); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return out.Interface(), nil
|
||||
}
|
||||
|
||||
// setResultField assigns val to fv, converting compatible Go types. A nil or
|
||||
// incompatible value is a reported error, never a silent zero-value fill.
|
||||
func setResultField(kind, field string, fv reflect.Value, val any) error {
|
||||
if val == nil {
|
||||
return fmt.Errorf("module %s: result.%s is nil, want %s", kind, field, fv.Type())
|
||||
}
|
||||
rv := reflect.ValueOf(val)
|
||||
if rv.Type().AssignableTo(fv.Type()) {
|
||||
fv.Set(rv)
|
||||
return nil
|
||||
}
|
||||
if rv.Type().ConvertibleTo(fv.Type()) {
|
||||
fv.Set(rv.Convert(fv.Type()))
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("module %s: result.%s is %T, want %s", kind, field, val, fv.Type())
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/eda/expr"
|
||||
"github.com/samcharles93/archie-core/internal/domain/eda/module/log"
|
||||
)
|
||||
|
||||
@@ -63,16 +64,9 @@ func TestRegisterUnknownKindIsError(t *testing.T) {
|
||||
|
||||
func TestKindSchema(t *testing.T) {
|
||||
r := New()
|
||||
argsType, resultType, ok := r.KindSchema("log")
|
||||
if !ok {
|
||||
if _, _, ok := r.KindSchema("log"); !ok {
|
||||
t.Fatal("KindSchema(log) = not ok, want the built-in log contract")
|
||||
}
|
||||
if argsType != reflect.TypeFor[log.Args]() {
|
||||
t.Errorf("KindSchema(log) args = %v, want log.Args", argsType)
|
||||
}
|
||||
if resultType != reflect.TypeFor[log.Result]() {
|
||||
t.Errorf("KindSchema(log) result = %v, want log.Result", resultType)
|
||||
}
|
||||
if _, _, ok := r.KindSchema("notify"); ok {
|
||||
t.Fatal("KindSchema(notify) = ok, want false for an unregistered kind")
|
||||
}
|
||||
@@ -122,6 +116,59 @@ func Run( {{{`)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDecodeResultEndToEnd ties Invoke's real flat result map through the
|
||||
// schema-aware conversion into the typed expr environment: the value the
|
||||
// checker validates and the value the run reads have one definition.
|
||||
func TestDecodeResultEndToEnd(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
writeModule(t, dir, `package main
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/domain/eda/module/log"
|
||||
|
||||
func Run(a log.Args) log.Result {
|
||||
return log.Result{Written: a.Message != "", Level: a.Level}
|
||||
}
|
||||
`)
|
||||
|
||||
r := New()
|
||||
if err := r.Register("log", dir); err != nil {
|
||||
t.Fatalf("Register: %v", err)
|
||||
}
|
||||
flat, err := r.Invoke(context.Background(), "log", map[string]any{"message": "hello", "level": "info"})
|
||||
if err != nil {
|
||||
t.Fatalf("Invoke: %v", err)
|
||||
}
|
||||
|
||||
decoded, err := r.DecodeResult("log", flat)
|
||||
if err != nil {
|
||||
t.Fatalf("DecodeResult: %v", err)
|
||||
}
|
||||
|
||||
env := expr.NewEnv(expr.DeclaredResult{ID: "build", Type: reflect.TypeFor[log.Result]()})
|
||||
prg, err := env.Compile(`actions.build.result.written`)
|
||||
if err != nil {
|
||||
t.Fatalf("Compile: %v", err)
|
||||
}
|
||||
got, err := env.Eval(prg, expr.Context{
|
||||
Actions: map[string]map[string]any{
|
||||
"build": {"result": decoded},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Eval: %v", err)
|
||||
}
|
||||
if b, ok := got.(bool); !ok || !b {
|
||||
t.Fatalf("Eval(actions.build.result.written) = %#v, want true", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeResultUnknownFieldIsError(t *testing.T) {
|
||||
r := New()
|
||||
if _, err := r.DecodeResult("log", map[string]any{"written": true, "bogus": "x"}); err == nil {
|
||||
t.Fatal("DecodeResult(unknown field) = nil, want error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvokePanicRecovered(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
writeModule(t, dir, `package main
|
||||
|
||||
@@ -86,8 +86,9 @@ type Action struct {
|
||||
Position string
|
||||
// ID is an optional stable identifier for this action. When present it
|
||||
// is the key later actions read this action's result under
|
||||
// (actions.<id>); the shape is the shared stable-identifier grammar
|
||||
// (internal/plugin/host.go, internal/domain/workflow/vocabulary.go).
|
||||
// (actions.<id>). Workflow actions use the shared stable-identifier
|
||||
// grammar; module actions must use a CEL identifier so the id can be
|
||||
// read through `actions.<id>` field selection.
|
||||
ID string
|
||||
// Kind is the module kind name (position: module only); empty for a
|
||||
// workflow action.
|
||||
@@ -129,18 +130,24 @@ type rawAction struct {
|
||||
Args map[string]string `yaml:"args"`
|
||||
}
|
||||
|
||||
// actionIDPattern is the stable-identifier shape an action id must match when
|
||||
// declared: a lowercase, dotted/dashed identifier (shared with
|
||||
// actionIDPattern is the stable-identifier shape a WORKFLOW action id must
|
||||
// match when declared: a lowercase, dotted/dashed identifier (shared with
|
||||
// internal/plugin/host.go:21 and internal/domain/workflow/vocabulary.go:19),
|
||||
// so an id has exactly one spelling and cannot smuggle whitespace or case into
|
||||
// the vocabulary two processes compare.
|
||||
// the vocabulary two processes compare. Module action ids are stricter (see
|
||||
// actionIDCELPattern) because they must also be CEL field selections.
|
||||
var actionIDPattern = regexp.MustCompile(`^[a-z][a-z0-9]*(?:[.-][a-z0-9]+)*$`)
|
||||
|
||||
// validateActionIDs enforces the id shape and uniqueness across a playbook's
|
||||
// raw actions. An absent id is fine (ids are optional); a declared id must
|
||||
// match the stable-identifier grammar and may not repeat. With action
|
||||
// playbooks now loadable, duplicate ids are reachable through Load, so this
|
||||
// helper is the load-boundary's id gate for both shapes.
|
||||
// actionIDCELPattern is the id shape for MODULE actions: a lowercase CEL
|
||||
// identifier. `actions.<id>` is CEL field selection, and a `.` or `-` in an id
|
||||
// has no field-selection spelling, so a dotted/dashed id would load yet could
|
||||
// never be referenced.
|
||||
var actionIDCELPattern = regexp.MustCompile(`^[a-z][a-z0-9_]*$`)
|
||||
|
||||
// validateActionIDs enforces the WORKFLOW id shape and uniqueness. An absent
|
||||
// id is fine (ids are optional); a declared id must match the
|
||||
// stable-identifier grammar. The workflow shape has exactly one action, so
|
||||
// the uniqueness check is a guard against future shape changes.
|
||||
func validateActionIDs(actions []rawAction) error {
|
||||
seen := make(map[string]struct{}, len(actions))
|
||||
for _, a := range actions {
|
||||
@@ -159,6 +166,28 @@ func validateActionIDs(actions []rawAction) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateModuleActionIDs enforces the MODULE id shape and uniqueness. A
|
||||
// module id must be a CEL identifier (not the looser workflow
|
||||
// stable-identifier grammar) and may not repeat; the error names the
|
||||
// playbook and the offending id.
|
||||
func validateModuleActionIDs(path string, actions []rawAction) error {
|
||||
seen := make(map[string]struct{}, len(actions))
|
||||
for i, a := range actions {
|
||||
id := a.ID
|
||||
if id == "" {
|
||||
continue
|
||||
}
|
||||
if !actionIDCELPattern.MatchString(id) {
|
||||
return fmt.Errorf("playbook %s: action %d id %q is not a valid CEL identifier (want %s)", path, i+1, id, actionIDCELPattern.String())
|
||||
}
|
||||
if _, dup := seen[id]; dup {
|
||||
return fmt.Errorf("playbook %s: duplicate action id %q", path, id)
|
||||
}
|
||||
seen[id] = struct{}{}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Load reads every *.yaml/*.yml playbook in dir, validates each against the
|
||||
// two-shape boundary (exactly one workflow action, or one or more module
|
||||
// actions; never mixed), and compiles each when expression and args value
|
||||
@@ -232,9 +261,6 @@ func loadOne(dir, path string, schemas KindSchemas) (*Playbook, error) {
|
||||
if err := pb.Trigger.Kind.Validate(); err != nil {
|
||||
return nil, fmt.Errorf("playbook %s: %w", path, err)
|
||||
}
|
||||
if err := validateActionIDs(raw.Actions); err != nil {
|
||||
return nil, fmt.Errorf("playbook %s: %w", path, err)
|
||||
}
|
||||
|
||||
actions, err := compileActions(path, raw.Actions, schemas)
|
||||
if err != nil {
|
||||
@@ -294,6 +320,9 @@ func compileActions(path string, raw []rawAction, schemas KindSchemas) ([]Action
|
||||
// The env has no prior result ids, so a workflow `when` or `args` value that
|
||||
// reads `actions.<id>` fails at compile -- the unchanged workflow behaviour.
|
||||
func compileWorkflowActions(path string, raw []rawAction) ([]Action, error) {
|
||||
if err := validateActionIDs(raw); err != nil {
|
||||
return nil, fmt.Errorf("playbook %s: %w", path, err)
|
||||
}
|
||||
a := raw[0]
|
||||
if strings.TrimSpace(a.Workflow) == "" {
|
||||
return nil, fmt.Errorf("playbook %s: workflow-kind action must name a workflow", path)
|
||||
@@ -326,6 +355,9 @@ func compileWorkflowActions(path string, raw []rawAction) ([]Action, error) {
|
||||
// Result structs, so a later action can read an earlier result and a forward
|
||||
// or field-typo read fails at compile (multi-action-playbooks.md, D3).
|
||||
func compileModuleActions(path string, raw []rawAction, schemas KindSchemas) ([]Action, error) {
|
||||
if err := validateModuleActionIDs(path, raw); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
actions := make([]Action, 0, len(raw))
|
||||
var declared []expr.DeclaredResult
|
||||
for _, a := range raw {
|
||||
@@ -402,7 +434,9 @@ func compileArgs(path, kind string, raw map[string]string, env *expr.Env, argsSc
|
||||
// validateArgsKeys rejects an args key the kind's Args struct does not define
|
||||
// (multi-action-playbooks.md, D4), so an arg typo is a load failure rather
|
||||
// than a dispatch-time shape mismatch. Go field names are lower-cased to the
|
||||
// YAML spelling (Message -> message), matching the module's strict decode.
|
||||
// YAML spelling (Message -> message), and the YAML key is compared verbatim:
|
||||
// the decoder also matches literal lower-case keys, so `Message` is rejected
|
||||
// here rather than loading and then failing at dispatch.
|
||||
func validateArgsKeys(path, kind string, argsSchema reflect.Type, raw map[string]string) error {
|
||||
t := argsSchema
|
||||
if t.Kind() == reflect.Pointer {
|
||||
@@ -412,8 +446,13 @@ func validateArgsKeys(path, kind string, argsSchema reflect.Type, raw map[string
|
||||
for field := range t.Fields() {
|
||||
fields[strings.ToLower(field.Name)] = struct{}{}
|
||||
}
|
||||
keys := make([]string, 0, len(raw))
|
||||
for key := range raw {
|
||||
if _, ok := fields[strings.ToLower(key)]; !ok {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
for _, key := range keys {
|
||||
if _, ok := fields[key]; !ok {
|
||||
return fmt.Errorf("playbook %s: %s kind args[%q] is not a declared Args field", path, kind, key)
|
||||
}
|
||||
}
|
||||
@@ -464,6 +503,15 @@ type DispatchInput struct {
|
||||
Event map[string]any
|
||||
}
|
||||
|
||||
// IsActionPlaybook reports whether pb is an action playbook (one or more
|
||||
// module actions), as opposed to a workflow playbook (exactly one workflow
|
||||
// action). It is the single two-shape predicate shared by Dispatch (which
|
||||
// skips action playbooks) and the daemon's D1 load warning (which logs them),
|
||||
// so the two can never disagree.
|
||||
func (pb *Playbook) IsActionPlaybook() bool {
|
||||
return len(pb.Actions) != 1 || pb.Actions[0].Position != "workflow"
|
||||
}
|
||||
|
||||
// Match reports whether the playbook's trigger matches the input's labels.
|
||||
func (pb *Playbook) Match(input DispatchInput) bool {
|
||||
labels := input.Labels
|
||||
@@ -539,7 +587,7 @@ func (s *Store) Dispatch(input DispatchInput) (Decision, bool) {
|
||||
if !pb.Match(input) {
|
||||
continue
|
||||
}
|
||||
if len(pb.Actions) != 1 || pb.Actions[0].Position != "workflow" {
|
||||
if pb.IsActionPlaybook() {
|
||||
// Action playbook: loaded and validated, not routed (D1).
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package playbook
|
||||
|
||||
import (
|
||||
"context"
|
||||
"maps"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -526,7 +527,7 @@ func TestLoadWhenActionsReferenceFails(t *testing.T) {
|
||||
when string
|
||||
wantID string
|
||||
}{
|
||||
{name: "undeclared field selection", when: `actions.a.result.x == true`, wantID: "a"},
|
||||
{name: "undeclared field selection", when: `actions.build.result.x == true`, wantID: "build"},
|
||||
{name: "dynamic index on actions", when: `actions["a"].result.x == true`},
|
||||
{name: "non-literal index key", when: `actions[key].result.x == true`},
|
||||
{name: "in operator on actions", when: `"notify" in actions`},
|
||||
@@ -711,6 +712,32 @@ func TestCompileArgsReportsFirstKeyDeterministically(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidateArgsKeysReportsFirstKeyDeterministically: validateArgsKeys
|
||||
// ranges a map, so it must sort keys before reporting -- otherwise the
|
||||
// offending key named depends on map iteration order.
|
||||
func TestValidateArgsKeysReportsFirstKeyDeterministically(t *testing.T) {
|
||||
argsType, _, ok := testSchemas(t).KindSchema("log")
|
||||
if !ok {
|
||||
t.Fatal("KindSchema(log) = not ok")
|
||||
}
|
||||
raw := map[string]string{
|
||||
"z.bad": "x",
|
||||
"a.bad": "x",
|
||||
}
|
||||
for range 64 {
|
||||
err := validateArgsKeys("pb.yaml", "log", argsType, raw)
|
||||
if err == nil {
|
||||
t.Fatal("validateArgsKeys = nil, want error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), `args["a.bad"]`) {
|
||||
t.Fatalf("validateArgsKeys error = %q, want the alphabetically-first key args[%q]", err.Error(), "a.bad")
|
||||
}
|
||||
if strings.Contains(err.Error(), `args["z.bad"]`) {
|
||||
t.Fatalf("validateArgsKeys error = %q, must name the alphabetically-first key, not %q", err.Error(), "z.bad")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestEvalArgsMissingEventFieldReturnsError: evaluating an args value that
|
||||
// reads a missing event field returns an error (never a panic).
|
||||
func TestEvalArgsMissingEventFieldReturnsError(t *testing.T) {
|
||||
@@ -1036,6 +1063,134 @@ actions:
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadActionPlaybookRejectsNonCELID: a module action id must be a CEL
|
||||
// identifier, so a dotted/dashed id (legal for a workflow id) loads nowhere.
|
||||
func TestLoadActionPlaybookRejectsNonCELID(t *testing.T) {
|
||||
for _, id := range []string{"build.step", "build-step", "Build"} {
|
||||
t.Run(id, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
writeFile(t, dir, "pb.yaml", "\ntrigger:\n kind: bug\nactions:\n - id: "+id+"\n position: module\n kind: log\n args:\n message: '\"hello\"'\n")
|
||||
_, err := Load(dir, testSchemas(t))
|
||||
if err == nil {
|
||||
t.Fatalf("Load(id %q) = nil, want load failure", id)
|
||||
}
|
||||
for _, want := range []string{"pb.yaml", id} {
|
||||
if !strings.Contains(err.Error(), want) {
|
||||
t.Errorf("Load error = %q, want it to name %q", err.Error(), want)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadWorkflowActionIDKeepsStableIdentifierGrammar: the CEL-identifier
|
||||
// restriction is module-only. A workflow action keeps the looser
|
||||
// stable-identifier grammar (dots/dashes) unchanged.
|
||||
func TestLoadWorkflowActionIDKeepsStableIdentifierGrammar(t *testing.T) {
|
||||
for _, id := range []string{"build.step", "build-step"} {
|
||||
t.Run(id, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
writeFile(t, dir, "pb.yaml", "\ntrigger:\n kind: bug\nactions:\n - position: workflow\n workflow: tdd\n id: "+id+"\n")
|
||||
if _, err := Load(dir, testSchemas(t)); err != nil {
|
||||
t.Fatalf("Load(workflow id %q) = %v, want nil error (workflow id grammar unchanged)", id, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadActionPlaybookRejectsMisspelledResultField: a Result field the
|
||||
// referenced kind does not define is a load failure naming the playbook and
|
||||
// the field -- the same unknown-field check, exercised at the Load boundary.
|
||||
func TestLoadActionPlaybookRejectsMisspelledResultField(t *testing.T) {
|
||||
misspelled := "wri" + "ten"
|
||||
dir := t.TempDir()
|
||||
writeFile(t, dir, "pb.yaml", "\ntrigger:\n kind: bug\nactions:\n - id: build\n position: module\n kind: log\n args:\n message: '\"build started\"'\n - id: done\n position: module\n kind: log\n args:\n message: '\"done\"'\n when: actions.build.result."+misspelled+" == true\n")
|
||||
_, err := Load(dir, testSchemas(t))
|
||||
if err == nil {
|
||||
t.Fatal("Load(misspelled result field) = nil, want load failure")
|
||||
}
|
||||
for _, want := range []string{"pb.yaml", misspelled, "undefined field"} {
|
||||
if !strings.Contains(err.Error(), want) {
|
||||
t.Errorf("Load error = %q, want it to contain %q", err.Error(), want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadActionPlaybookArgsKeysMatchDecoder ties the loader's accepted args
|
||||
// key set to the module decoder's: the loader accepts only lower-case keys,
|
||||
// the decoder decodes only lower-case keys, and the two meet when the
|
||||
// evaluated args are passed to Invoke.
|
||||
func TestLoadActionPlaybookArgsKeysMatchDecoder(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
writeFile(t, dir, "pb.yaml", `
|
||||
trigger:
|
||||
kind: bug
|
||||
actions:
|
||||
- id: build
|
||||
position: module
|
||||
kind: log
|
||||
args:
|
||||
message: '"hello"'
|
||||
level: '"info"'
|
||||
`)
|
||||
store, err := Load(dir, testSchemas(t))
|
||||
if err != nil {
|
||||
t.Fatalf("Load: %v", err)
|
||||
}
|
||||
got, err := store.EvalArgs(store.Playbooks[0].Actions[0], DispatchInput{Event: map[string]any{}})
|
||||
if err != nil {
|
||||
t.Fatalf("EvalArgs: %v", err)
|
||||
}
|
||||
want := map[string]any{"message": "hello", "level": "info"}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("EvalArgs = %#v, want %#v", got, want)
|
||||
}
|
||||
|
||||
moduleDir := t.TempDir()
|
||||
writeFile(t, moduleDir, "log.go", `package main
|
||||
|
||||
import "github.com/samcharles93/archie-core/internal/domain/eda/module/log"
|
||||
|
||||
func Run(a log.Args) log.Result {
|
||||
return log.Result{Written: a.Message != "", Level: a.Level}
|
||||
}
|
||||
`)
|
||||
r := module.New()
|
||||
if err := r.Register("log", moduleDir); err != nil {
|
||||
t.Fatalf("Register: %v", err)
|
||||
}
|
||||
res, err := r.Invoke(context.Background(), "log", got)
|
||||
if err != nil {
|
||||
t.Fatalf("Invoke(evaluated args): %v", err)
|
||||
}
|
||||
if res["written"] != true {
|
||||
t.Errorf("written = %v, want true", res["written"])
|
||||
}
|
||||
if res["level"] != "info" {
|
||||
t.Errorf("level = %q, want info", res["level"])
|
||||
}
|
||||
|
||||
// The decoder matches literal lower-case keys, so a capitalized key the
|
||||
// loader accepted under the old lower-casing rule must now fail the load.
|
||||
badDir := t.TempDir()
|
||||
writeFile(t, badDir, "pb.yaml", `
|
||||
trigger:
|
||||
kind: bug
|
||||
actions:
|
||||
- position: module
|
||||
kind: log
|
||||
args:
|
||||
Message: '"hello"'
|
||||
`)
|
||||
_, err = Load(badDir, testSchemas(t))
|
||||
if err == nil {
|
||||
t.Fatal("Load(capitalized arg key) = nil, want load failure")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "Message") {
|
||||
t.Errorf("Load error = %q, want the capitalized key named", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadActionPlaybookRejectsActionReferences is the classifier-enumeration
|
||||
// proof (E): every `actions` spelling the old classifier rejected must still
|
||||
// be rejected for a typed action playbook. All but the bare `actions` value
|
||||
@@ -1043,6 +1198,15 @@ actions:
|
||||
// read is the one spelling CEL does not reject, kept behind the retained
|
||||
// resolvable check in expr.ActionReferences.
|
||||
func TestLoadActionPlaybookRejectsActionReferences(t *testing.T) {
|
||||
// Positive control: the same two-action fixture with a valid `when` must
|
||||
// load, so the rejections below fail because of the `when` spelling and
|
||||
// not because the action-playbook shape itself is rejected.
|
||||
controlDir := t.TempDir()
|
||||
writeFile(t, controlDir, "pb.yaml", "\ntrigger:\n kind: bug\nactions:\n - id: build\n position: module\n kind: log\n args:\n message: '\"build started\"'\n - id: done\n position: module\n kind: log\n args:\n message: '\"done\"'\n when: actions.build.result.written == true\n")
|
||||
if _, err := Load(controlDir, testSchemas(t)); err != nil {
|
||||
t.Fatalf("Load(valid when control) = %v, want nil error", err)
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
when string
|
||||
|
||||
Reference in new issue
Block a user