diff --git a/.beads/interactions.jsonl b/.beads/interactions.jsonl index cde22e6c8..f6e3c16ff 100644 --- a/.beads/interactions.jsonl +++ b/.beads/interactions.jsonl @@ -194,3 +194,4 @@ {"id":"int-c377e39e68d768f1d33fd07a0a5cc8b0","kind":"field_change","created_at":"2026-09-02T17:07:38.984329395Z","actor":"Sam Catlow","issue_id":"archie-core-t2db.12","extra":{"field":"status","new_value":"closed","old_value":"open","reason":"Implemented and gate-verified as 2c7c79e (pushed to main). cmd/archie-playbooks lint mode reuses daemon loaders; file-granular positions documented as known limitation."}} {"id":"int-bcf0faab845afec53d14f3a316340930","kind":"field_change","created_at":"2026-09-02T17:30:33.765888462Z","actor":"Sam Catlow","issue_id":"archie-core-e7bo","extra":{"field":"status","new_value":"closed","old_value":"open","reason":"Fixed and gate-verified as e3a20f6 (pushed to main). wfextract directive now targets internal/domain/workflow; 'go generate ./...' completes repo-wide. All four extract surfaces regenerated; nil-guards re-applied on interface wrappers (documented IMPORTANT note restated in wfextract package doc). Previously-missing symbols proven reachable from interpreted code: wfextract (Forger/Reviewer/Trees/routing loaders) and pluginextract (Host/Module/Manifest/NewHost/HostAPIVersion) via new reachability tests; secretextract exports were current (nil-guard drift only). Red-then-green verified: fresh regen wipes guards -> guard tests panic -> re-apply -> pass. task check clean (113/113 UI, exit 0)."}} {"id":"int-9bc9541763af71ffbdb351604e477227","kind":"field_change","created_at":"2026-09-02T17:35:28.094046545Z","actor":"Sam Catlow","issue_id":"archie-core-t2db.13","extra":{"field":"status","new_value":"closed","old_value":"open","reason":"Implemented and gate-verified as d29c732 (pushed to main). ModuleRegistry (Register/Invoke, strict arg decode, yaegiutil panic recovery) + internal/domain/eda/module/log schema with generated logextract symbols; ModuleDir config wired in bootstrap.go with log-and-abort; examples/modules/log.go operator example proven through the real registry. Playbook YAML action dispatch deliberately deferred (data-flow Q1 unresolved) and recorded in module-position.md. task check clean (113/113 UI, exit 0)."}} +{"id":"int-0ae0e3fbcad5c9cc45344d5f64b6abc5","kind":"field_change","created_at":"2026-09-02T18:01:29.431793769Z","actor":"Sam Catlow","issue_id":"archie-core-t2db.14","extra":{"field":"status","new_value":"closed","old_value":"open","reason":"Implemented and gate-verified as 33cdece (pushed to main). internal/domain/eda/expr: NewEnv (event+actions roots, DefaultCostLimit 100k), Compile (reject-at-load: syntax/unknown-root/type errors returned), Eval (panic-free, errors returned, ref.Val unwrapped to native). cel.dev/cel-go v0.32.0 direct dep, vetted per resolved doc (no re-vet). 9 tests pass, all verified against real CEL semantics (cost limits accrue on macros; == on missing key is false; unknown root rejected at compile). task check clean (113/113 UI)."}} diff --git a/.beads/issues.jsonl b/.beads/issues.jsonl index eed928f2c..a0db0d673 100644 --- a/.beads/issues.jsonl +++ b/.beads/issues.jsonl @@ -93,7 +93,7 @@ {"_type":"issue","id":"archie-core-tb3a","title":"Fix streaming review blockers for inline tool calls","description":"Resolve the four evidence-gated findings in merged inline tool-call streaming: preserve the authoritative Telegram reply when final edits fail; keep Telegram network I/O off the model FullStream consumer and cancellable; correlate tool inputs with results and render a bounded redacted parameter summary; snapshot show_tool_calls per live reply to avoid reload races. Add regression tests for every path and run task check plus lint/race validation.","acceptance_criteria":"Regression tests fail on the merged implementation and pass after the fix. Final edit failure falls back to normal split delivery. Delta and ToolCall callbacks do not wait on Telegram HTTP and stop cancels in-flight rendering. Completed tool events include bounded redacted parameters correlated by tool-call ID. Reload cannot race an in-flight reply's show_tool_calls setting. task check passes, golangci-lint reports no new findings, and focused race tests pass.","notes":"Implemented in worktree /tmp/archie-core-tb3a on branch fix/archie-core-tb3a. Added cancellable async Telegram rendering, final edit failure fallback through normal split delivery, call-ID-correlated bounded conservative parameter summaries, atomic per-reply show_tool_calls snapshots, SSE/UI parameter rendering, rebuilt ui/dist, and regression tests. Validation: GOFLAGS=-buildvcs=false task check passed; golangci-lint --new-from-rev=HEAD ./... reported 0 issues; focused race suite passed; fresh adversarial reviewer reported PASS with zero findings. Plain task check is blocked in linked worktrees by Go VCS stamping exit 128, hence buildvcs override.\n2026-08-24 reconciliation: the close reason's 'worktree left uncommitted' is stale. The work did land on main via ba33527 (Merge pull request #477 from samcharles93/fix/archie-core-tb3a); verified on main: gateway/stream.go sensitiveParameterKey+redactToolParameters, telegram/live.go per-reply showToolCalls snapshot, webui/api_chat.go gating. The /tmp/archie-core-tb3a worktree and its branch are gone, as expected.\n","status":"closed","priority":1,"issue_type":"bug","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-08-13T18:05:35Z","created_by":"Sam Catlow","updated_at":"2026-08-24T07:29:09Z","started_at":"2026-08-13T18:05:39Z","closed_at":"2026-08-13T18:35:26Z","close_reason":"Completed and independently reviewed; worktree left uncommitted per conservative repository policy.","external_ref":"https://github.com/samcharles93/archie-core/issues/485","dependency_count":0,"dependent_count":0,"comment_count":0} {"_type":"issue","id":"archie-core-dzx","title":"Remove duplicate archie-agent safe.directory setup","description":"## Problem\\ncmd/archie-agent/main.go configures git safe.directory inline and then calls markWorktreeSafe, so startup writes the same global git configuration twice.\\n\\n## Scope\\nKeep markWorktreeSafe as the single tested production path and remove the older inline exec.Command block/import. Add regression coverage proving startup wiring has exactly one authoritative safe-directory call.\\n\\n## Acceptance\\n- archie-agent startup invokes markWorktreeSafe exactly once\\n- no second inline safe.directory command remains in main.go\\n- cmd/archie-agent tests and repository quality gate pass","acceptance_criteria":"Single tested safe.directory setup path remains; duplicate inline command is removed; tests and task check pass","notes":"Red proof: TestMainConfiguresWorktreeSafetyOnce failed with 2 setup paths before implementation. Green proof: go test/vet/build ./cmd/archie-agent/... passed; golangci-lint --new-from-rev=HEAD reported 0 issues. Fresh adversarial review found no blockers. Full task check was not run because the working tree contains unrelated in-progress changes and task check rewrites files; scoped package gate used per repository protocol.","status":"closed","priority":1,"issue_type":"bug","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-08-13T16:10:09Z","created_by":"Sam Catlow","updated_at":"2026-08-13T16:13:48Z","started_at":"2026-08-13T16:10:24Z","closed_at":"2026-08-13T16:13:48Z","close_reason":"Removed duplicate inline safe.directory setup; markWorktreeSafe is now the single tested production path","external_ref":"https://github.com/samcharles93/archie-core/issues/470","dependency_count":0,"dependent_count":0,"comment_count":0} {"_type":"issue","id":"archie-core-t2db.15","title":"eda: playbook document + event coordinator, single-action workflow-kind dispatch","description":"Seventh slice of docs/prds/eda-playbook-engine.md. Builds the rich playbook document shape (trigger + ordered actions with CEL 'when') and an event coordinator that dispatches it -- the first real consumer of t2db.14's expr package. Scoped tightly to what's already safe per this doc's own gating:\n\n- Exactly ONE action per playbook in this slice, and it MUST be position: workflow. Reject (load failure) a playbook declaring more than one action, or an action whose position is module/channel/forge -- those remain blocked on the unresolved execution-time gaps (mid-run failure semantics, idempotency for non-workflow actions) per the 'Execution-time gaps' section. Do not relax this without Sam's sign-off -- this is the one hard boundary in this ticket, everything else is normal implementation judgment.\n- This is an ADDITIONAL loading path alongside the existing flat kind/label binding files (t2db.9/.10/.11). Do NOT modify workflow.Route(), workflow.workflowForLabels, or remove/change the existing single-file/multi-dir binding loaders. Migrating Route() onto the new mechanism is a separate, later, larger decision -- not in scope here.\n- Idempotency is free for this slice for the same reason it was free for t2db.13's first Module slice: a single workflow-kind action reaches the exact same workflow.Registry dispatch path that already carries TaskEnvelope's idempotency key. Nothing new to design here.","design":"Depends on archie-core-t2db.14 (expr package) -- do not start until it is closed and verified.\n\n1. Playbook document type (new package, likely internal/domain/eda/playbook -- confirm against organisation.md's layout rule, follow the log/logextract split precedent from t2db.13 if a generated component turns out to be needed, though this ticket is pure YAML+CEL, no Yaegi, so likely no extract package is needed here):\n - trigger: reuse existing workintake label/kind matching (do not invent a second label-matching mechanism -- workintake.SplitLabels/KindsForLabels already exist and are the one place this vocabulary lives).\n - actions: a list, validated at load to contain exactly one entry with position: workflow and a workflow registry name.\n - when: optional CEL expression string on the action, compiled via t2db.14's Compile() at load time -- a compile failure is a reported load failure (same collision-handling philosophy as t2db.9-.12), not a silently-skipped playbook.\n2. Event coordinator: loads playbook files from a configured directory (mirror the PlaybookDirs pattern from t2db.11 -- reuse its directory-scanning shape, do not reinvent), compiles every playbook's when expression at load (fail the whole load on any compile error, consistent with the existing reject-at-load rule), and at dispatch time: match trigger against an incoming task's labels/kind, evaluate when (if present) against a context of {event: \u003ctask's label/kind fields, whatever is cheaply available -- do not over-engineer the event shape beyond what a workflow-kind dispatch actually needs\u003e, actions: {} (empty -- no prior actions exist in a single-action playbook)}, and if the condition is true (or absent), dispatch to the named workflow via the existing workflow.Registry -- call the same registry lookup Route() already uses, do not duplicate dispatch logic.\n3. Config: a directory field for these playbook documents (distinct from t2db.11's PlaybookDirs, which holds flat binding files -- naming these two similar-but-different directories clearly is important; suggest something like EDAPlaybookDir or clarify in the field doc comment exactly how it differs from PlaybookDirs so a future reader isn't confused). If you judge the distinction is confusing enough that these should share one directory with two file shapes distinguished by content, flag that as a note in the PRD rather than silently deciding it -- but implement SOMETHING reasonable rather than blocking.\n4. Bootstrap wiring: load-and-abort on failure, matching every other slice's established pattern.\n5. Do NOT wire this into production task intake (daemon.go/pollNATS/publishTask) in this ticket -- prove the coordinator end-to-end with tests that construct a task-like input directly, the same way t2db.13 proved ModuleRegistry with direct Register/Invoke calls rather than full daemon wiring. Production wiring (making a real forge-triggered task actually flow through this coordinator instead of only workflow.Route()) is explicitly a follow-up ticket -- note it as such when you close this one.","acceptance_criteria":"A playbook YAML with trigger + one workflow-kind action + optional when loads successfully and dispatches to the correct registered workflow when trigger+when match. A playbook declaring 2+ actions is a reported load failure. A playbook with a non-workflow action position is a reported load failure. A when expression that compiles but evaluates false at dispatch time correctly skips dispatch (logged, not silently ignored). A when compile failure at load time is reported and the playbook is dropped (same collision-handling shape as prior slices). Existing workflow.Route()/label-binding tests (t2db.9-.12) are unaffected -- run their full test suites and confirm zero regressions. task check passes.","status":"open","priority":2,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-09-02T17:57:47Z","created_by":"Sam Catlow","updated_at":"2026-09-02T17:57:47Z","dependencies":[{"issue_id":"archie-core-t2db.15","depends_on_id":"archie-core-t2db","type":"parent-child","created_at":"2026-09-02T17:57:47Z","created_by":"Sam Catlow","metadata":"{}"},{"issue_id":"archie-core-t2db.15","depends_on_id":"archie-core-t2db.14","type":"blocks","created_at":"2026-09-02T17:57:48Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":1,"dependent_count":0,"comment_count":0} -{"_type":"issue","id":"archie-core-t2db.14","title":"eda: CEL expression environment (compile/validate at load, evaluate at dispatch)","description":"Sixth slice of docs/prds/eda-playbook-engine.md, implementing the 'Implementation placement' section of the resolved open question 1 (CEL). Builds internal/domain/eda/expr in isolation, no coordinator/dispatch yet -- mirrors how t2db.13 built ModuleRegistry as a standalone, fully-tested unit before anything consumed it.\n\nThis is pure infrastructure for the dispatch slice (t2db.15) that depends on it. No production wiring in this ticket.","design":"Package internal/domain/eda/expr (domain layer: no infrastructure imports).\n\nPer the resolved doc section, build:\n1. Environment construction: a CEL env declaring the two context roots -- 'event' as map(string, dyn), 'actions' as map(string, dyn). Cost limit (J5): pick a constant (start at 100,000, name it clearly as a tunable, e.g. DefaultCostLimit) -- exact value is a judgment call already flagged as tunable, don't block on picking the 'right' number.\n2. Compile/validate at load: a function taking a CEL expression string and returning (compiled program, error) -- a syntax error, a reference to an unknown root (anything other than event/actions), or exceeding the cost limit is a returned error, never a panic. This is what both the playbook loader (t2db.15) and the lint tool (t2db.12, follow-up integration is a SEPARATE ticket, do not touch cmd/archie-playbooks in this one) will eventually call.\n3. Evaluate at dispatch: a function taking a compiled program and a context (event map[string]any, actions map[string]map[string]any) and returning (value, error) -- CEL evaluation errors (missing field, wrong type against a dyn value) are returned as errors, never panics. Use CEL's own panic-free evaluation; do not add a recover() wrapper unless a test proves CEL itself can panic (verify this, don't assume).\n4. actions.\u003cid\u003e.result field-level typing (J1/context-conformance in the doc): NOT required in this ticket -- that needs the generated Module Result structs wired in as CEL type declarations, which only matters once a playbook can reference a prior action's result (t2db.15 is single-action, so there IS no prior action). Keep actions declared as dyn for now; note in a comment that per-kind typed declarations are follow-up work once multi-action playbooks exist.\n5. Dependency: add cel.dev/cel-go v0.32.0 (pinned) to go.mod -- this is the only new dependency, already vetted (see the resolved doc section's vetting record). Do not re-vet it.\n\nFollow docs/prds/eda-playbook-engine.md's reject-at-load philosophy throughout: anything not accepted by the schema is a reported error, never a silent default.","acceptance_criteria":"internal/domain/eda/expr exists with: environment construction, Compile(expr string) (returned type, error), Eval(program, ctx) (value, error). Table-driven tests cover: a valid boolean expression against event fields evaluates correctly; a syntax error is a Compile-time error; a reference to an unknown root (e.g. 'foo.bar') is a Compile-time error; evaluating against a missing event field does not panic (returns an error or CEL's documented behavior, whichever is correct -- verify against real CEL semantics, don't guess); an expression that would exceed the cost limit is rejected, not silently truncated. go.mod gains cel.dev/cel-go v0.32.0 only. task check passes.","status":"open","priority":2,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-09-02T17:57:19Z","created_by":"Sam Catlow","updated_at":"2026-09-02T17:57:19Z","dependencies":[{"issue_id":"archie-core-t2db.14","depends_on_id":"archie-core-t2db","type":"parent-child","created_at":"2026-09-02T17:57:19Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":0,"dependent_count":1,"comment_count":0} +{"_type":"issue","id":"archie-core-t2db.14","title":"eda: CEL expression environment (compile/validate at load, evaluate at dispatch)","description":"Sixth slice of docs/prds/eda-playbook-engine.md, implementing the 'Implementation placement' section of the resolved open question 1 (CEL). Builds internal/domain/eda/expr in isolation, no coordinator/dispatch yet -- mirrors how t2db.13 built ModuleRegistry as a standalone, fully-tested unit before anything consumed it.\n\nThis is pure infrastructure for the dispatch slice (t2db.15) that depends on it. No production wiring in this ticket.","design":"Package internal/domain/eda/expr (domain layer: no infrastructure imports).\n\nPer the resolved doc section, build:\n1. Environment construction: a CEL env declaring the two context roots -- 'event' as map(string, dyn), 'actions' as map(string, dyn). Cost limit (J5): pick a constant (start at 100,000, name it clearly as a tunable, e.g. DefaultCostLimit) -- exact value is a judgment call already flagged as tunable, don't block on picking the 'right' number.\n2. Compile/validate at load: a function taking a CEL expression string and returning (compiled program, error) -- a syntax error, a reference to an unknown root (anything other than event/actions), or exceeding the cost limit is a returned error, never a panic. This is what both the playbook loader (t2db.15) and the lint tool (t2db.12, follow-up integration is a SEPARATE ticket, do not touch cmd/archie-playbooks in this one) will eventually call.\n3. Evaluate at dispatch: a function taking a compiled program and a context (event map[string]any, actions map[string]map[string]any) and returning (value, error) -- CEL evaluation errors (missing field, wrong type against a dyn value) are returned as errors, never panics. Use CEL's own panic-free evaluation; do not add a recover() wrapper unless a test proves CEL itself can panic (verify this, don't assume).\n4. actions.\u003cid\u003e.result field-level typing (J1/context-conformance in the doc): NOT required in this ticket -- that needs the generated Module Result structs wired in as CEL type declarations, which only matters once a playbook can reference a prior action's result (t2db.15 is single-action, so there IS no prior action). Keep actions declared as dyn for now; note in a comment that per-kind typed declarations are follow-up work once multi-action playbooks exist.\n5. Dependency: add cel.dev/cel-go v0.32.0 (pinned) to go.mod -- this is the only new dependency, already vetted (see the resolved doc section's vetting record). Do not re-vet it.\n\nFollow docs/prds/eda-playbook-engine.md's reject-at-load philosophy throughout: anything not accepted by the schema is a reported error, never a silent default.","acceptance_criteria":"internal/domain/eda/expr exists with: environment construction, Compile(expr string) (returned type, error), Eval(program, ctx) (value, error). Table-driven tests cover: a valid boolean expression against event fields evaluates correctly; a syntax error is a Compile-time error; a reference to an unknown root (e.g. 'foo.bar') is a Compile-time error; evaluating against a missing event field does not panic (returns an error or CEL's documented behavior, whichever is correct -- verify against real CEL semantics, don't guess); an expression that would exceed the cost limit is rejected, not silently truncated. go.mod gains cel.dev/cel-go v0.32.0 only. task check passes.","status":"closed","priority":2,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-09-02T17:57:19Z","created_by":"Sam Catlow","updated_at":"2026-09-02T18:01:29Z","closed_at":"2026-09-02T18:01:29Z","close_reason":"Implemented and gate-verified as 33cdece (pushed to main). internal/domain/eda/expr: NewEnv (event+actions roots, DefaultCostLimit 100k), Compile (reject-at-load: syntax/unknown-root/type errors returned), Eval (panic-free, errors returned, ref.Val unwrapped to native). cel.dev/cel-go v0.32.0 direct dep, vetted per resolved doc (no re-vet). 9 tests pass, all verified against real CEL semantics (cost limits accrue on macros; == on missing key is false; unknown root rejected at compile). task check clean (113/113 UI).","dependencies":[{"issue_id":"archie-core-t2db.14","depends_on_id":"archie-core-t2db","type":"parent-child","created_at":"2026-09-02T17:57:19Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":0,"dependent_count":1,"comment_count":0} {"_type":"issue","id":"archie-core-t2db.13","title":"workflow: Module position first slice (registry + side-effect-free 'log' kind)","description":"Fifth slice of docs/prds/eda-playbook-engine.md, implementing docs/prds/module-position.md's recommended first slice: prove schema -\u003e go:generate -\u003e Yaegi load -\u003e ModuleRegistry -\u003e playbook dispatch end to end with a side-effect-free action kind, before any kind with a real external effect (notify, image-gen, forge writes) is attempted -- those remain blocked on the parent doc's unresolved execution-time gaps (idempotency, mid-run failure), which this slice deliberately does not need.\n\nGeneralizes the fixed-signature, resolved-by-name Yaegi pattern already shipped for custom gates (internal/gate/gateeval) and workflow stage plugins (skillbuild) -- do not invent a new interpreter-construction or panic-recovery mechanism; reuse yaegiutil.New/Resolve/Safe directly.","design":"Per module-position.md:\n1. internal/domain/eda/module/log: hand-written schema source (Args/Result structs, SchemaVersion const), //go:generate yaegi extract -name logextract producing logextract.Symbols -- same shape as gateextract/wfextract.\n2. internal/domain/eda/module: ModuleRegistry with Register(kind, dir) and Invoke(ctx, kind, rawArgs map[string]any) (map[string]any, error). No generic Module interface with an any payload -- Invoke decodes rawArgs into the kind's generated Args struct before calling the resolved function; a shape mismatch is a reported failure per the existing 'schema defines the accepted message' rule, not a silent zero-value fill. Discovery/resolve mirrors gateeval.Evaluate's structure (read file -\u003e yaegiutil.New with that kind's symbols -\u003e yaegiutil.Resolve[func(Args) Result] -\u003e yaegiutil.Safe-wrapped call).\n3. Config: ModuleDir field mirroring PluginDir's shape and doc comment exactly (operator-trusted directory of Yaegi module implementations, empty means none loaded).\n4. Wire ModuleRegistry construction into daemon startup (bootstrap.go) alongside the existing PlaybookDirs/routing-file loading -- log-and-abort on load failure, matching the established pattern.\n5. Do NOT implement notify, image-gen, or any action kind with a real external side effect in this slice. Do NOT wire this into playbook YAML action dispatch yet if that requires resolving the data-flow/condition syntax (parent doc open question 1) -- confirm during implementation whether a minimal 'run this one module action, no chaining' dispatch is small enough to include, or whether registry+one-kind-loadable is itself the right slice boundary and dispatch is a follow-up ticket.","acceptance_criteria":"internal/domain/eda/module/log exists with a generated symbol table via go:generate yaegi extract (same tooling as wfextract/gateextract, verified by running go generate ./... and checking the output compiles). ModuleRegistry.Register discovers and loads a *.go file implementing the log kind's generated contract; Invoke calls it and returns a typed result decoded from the interpreted function's return value. A malformed or wrong-shaped args map is a reported error, not a panic or silent default. Config gains ModuleDir (empty by default, no behavior change). Trust boundary matches PluginDir/SecretEngineDir (operator-installed, in-process, daemon-privileged) -- not repository/task code. task check passes.","status":"closed","priority":2,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-09-02T17:11:21Z","created_by":"Sam Catlow","updated_at":"2026-09-02T17:35:28Z","closed_at":"2026-09-02T17:35:28Z","close_reason":"Implemented and gate-verified as d29c732 (pushed to main). ModuleRegistry (Register/Invoke, strict arg decode, yaegiutil panic recovery) + internal/domain/eda/module/log schema with generated logextract symbols; ModuleDir config wired in bootstrap.go with log-and-abort; examples/modules/log.go operator example proven through the real registry. Playbook YAML action dispatch deliberately deferred (data-flow Q1 unresolved) and recorded in module-position.md. task check clean (113/113 UI, exit 0).","dependencies":[{"issue_id":"archie-core-t2db.13","depends_on_id":"archie-core-t2db","type":"parent-child","created_at":"2026-09-02T17:11:21Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":0,"dependent_count":0,"comment_count":0} {"_type":"issue","id":"archie-core-t2db.12","title":"workflow: standalone playbook lint/LSP tool (gopls-shaped)","description":"Fourth slice of docs/prds/eda-playbook-engine.md. The design doc's collision handling has two layers: a linter for pre-merge, same-tree collisions, and the runtime detect-log-report path for cross-source collisions only visible at daemon startup (t2db.9/.10/.11). Only the runtime path exists so far.\n\nCorrected 2026-09-03 (Sam): not a Taskfile target wrapping existing functions -- a small standalone CLI binary shaped like gopls: one tool, multiple invocation modes. A one-shot 'lint' mode ships in this ticket; the design doc's LSP requirement (author-time hover/completion/inline errors) is a later mode of the SAME binary, not a separate tool, so the two can never validate a playbook YAML differently -- one schema source, one binary, two entrypoints into it (matches gopls's own check-vs-serve split).\n\nDepends on archie-core-t2db.11 (multiple playbook directories) landing first, since a linter checking one directory in isolation can't demonstrate the cross-directory case the doc cares about -- though it should also catch within-a-single-directory duplicates, which is the more common real case.","design":"New cmd/\u003cname\u003e binary (e.g. cmd/archie-playbooks), per organisation.md's cmd/ rule: CLI flag parsing and entry-point routing only, no domain wiring in cmd/ itself. Actual validation logic stays in internal/domain/workflow (reuse LoadKindWorkflowsYAML/LoadLabelWorkflowsYAML/t2db.11's directory merge directly -- no second, parallel validation path). This ticket implements only the lint subcommand/mode; structure the binary's command dispatch so an 'lsp'/'serve' subcommand can be added later without restructuring (e.g. a small subcommand table from the start, even with one entry), matching gopls's shape rather than assuming a single-purpose CLI that gets bolted onto later.","acceptance_criteria":"A new cmd/ binary exists with a lint subcommand/mode that runs against one or more playbook directories and exits non-zero with a clear message when two files bind the same label/kind; exits 0 cleanly on a valid set. Reuses existing loader/merge logic directly -- no duplicated validation rules. Binary's command structure has an explicit seam for adding an LSP/serve mode later (does not need to be built yet). Documented (README or Taskfile.yml comment) so it's discoverable. task check passes.","status":"closed","priority":2,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-09-02T16:56:44Z","created_by":"Sam Catlow","updated_at":"2026-09-02T17:07:39Z","closed_at":"2026-09-02T17:07:39Z","close_reason":"Implemented and gate-verified as 2c7c79e (pushed to main). cmd/archie-playbooks lint mode reuses daemon loaders; file-granular positions documented as known limitation.","dependencies":[{"issue_id":"archie-core-t2db.12","depends_on_id":"archie-core-t2db","type":"parent-child","created_at":"2026-09-02T16:56:44Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":0,"dependent_count":0,"comment_count":0} {"_type":"issue","id":"archie-core-t2db.11","title":"workflow: generalize routing/label files to multiple playbook directories","description":"Third slice of docs/prds/eda-playbook-engine.md, completing open question #2. Generalizes the single-file workflow_routing_file / workflow_labels_file config fields (t2db.9, t2db.10) into a LIST of configured playbook directories, loaded at daemon startup alongside the existing single-file fields for backward compatibility. This is what makes the design doc's collision-handling rule (drop + log + report to caller, no auto-arbitration) load-bearing: a single file has nothing to collide with, so the rule has never actually been exercised in production code yet.\n\nCorrected 2026-09-03 (Sam): must be a list of directories, not one directory -- a single playbook_dir field would contradict supporting multiple independently-maintained playbook sources, which the PRD's own Dedup section already assumes ('independently-sourced playbook directories collide'). Scope directories by the existing Repo (owner/name) concept already in Config.Repos, NOT by org/tenant -- org-keying would require an auth/identity model archie does not have yet. That is a separate, later decision, not something to half-build here.","design":"Follow the existing once-at-startup lifecycle (SetKindWorkflows/SetLabelWorkflows pattern) rather than inventing new state management. Config gains a list field (e.g. PlaybookDirs []string, or a PlaybookDir field on the existing Repo struct if directories should tie to a specific configured repo rather than being globally pooled -- decide based on whether playbooks are meant to be repo-scoped or operator-global). A loader reads every *.yaml file across all configured directories, applying the same per-file validation LoadLabelWorkflowsYAML already does, then merges bindings across all sources -- a binding for the same label/kind declared by two different sources is the collision case: drop both, log which sources/label collided, return the error to the caller (do not start the daemon on a routing collision). Single-file fields (workflow_routing_file, workflow_labels_file) remain supported unchanged as an additional, independent input alongside the directory list, not replaced by it.","acceptance_criteria":"Config supports a LIST of playbook directories (not a single path), empty/unset by default (no behavior change for existing configs). Loading two directories with a colliding label binding is a reported load failure, not a silent pick-one. Loading multiple non-colliding directories merges cleanly and Route() honours the merged bindings. Existing workflow_routing_file/workflow_labels_file continue to work unchanged when no directories are configured. Org/tenant scoping is explicitly out of scope and not attempted. task check passes.","status":"closed","priority":2,"issue_type":"task","owner":"sam@catlow.tech","created_at":"2026-09-02T16:50:18Z","created_by":"Sam Catlow","updated_at":"2026-09-02T16:57:50Z","closed_at":"2026-09-02T16:57:50Z","close_reason":"Implemented and gate-verified as 3e7fe00 (pushed to main). PlaybookDirs is a list per ticket correction; cross-source collision rule exercised by new load path.","dependencies":[{"issue_id":"archie-core-t2db.11","depends_on_id":"archie-core-t2db","type":"parent-child","created_at":"2026-09-02T16:50:18Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":0,"dependent_count":0,"comment_count":0} diff --git a/config.example.toml b/config.example.toml index 9cff2985f..612957bdb 100644 --- a/config.example.toml +++ b/config.example.toml @@ -21,6 +21,7 @@ # workflow_routing_file = "~/.local/share/archie/workflow-routing.yaml" # kind->workflow rebinding (bug/feature/bootstrap) # workflow_labels_file = "~/.local/share/archie/workflow-labels.yaml" # arbitrary forge label -> workflow (EDA playbook slice 2) # playbook_dirs = ["~/.local/share/archie/workflows"] # list of dirs of *.yaml binding files, merged with the two above (slice 3); a key bound twice across sources is a load failure +# eda_playbook_dir = "~/.local/share/archie/eda-playbooks" # rich EDA playbook documents (trigger + CEL gated single workflow action, t2db.15); distinct from playbook_dirs poll_interval = "60s" label = "archie" diff --git a/docs/prds/eda-playbook-engine.md b/docs/prds/eda-playbook-engine.md index c9ca8e6d2..9c3a5e647 100644 --- a/docs/prds/eda-playbook-engine.md +++ b/docs/prds/eda-playbook-engine.md @@ -597,6 +597,18 @@ before Channel/Forge actions or multi-action playbooks ship. label routing) first, proving the schema-gen -> Yaegi -> playbook path end to end on the smallest useful case, before adding Channel/Forge action kinds or the linter/LSP. + + **Status 2026-09-03: resolved across t2db.13-15.** The Module position + (registry + log kind, t2db.13), the CEL expression environment + (t2db.14), and the playbook document + event coordinator with + single-action workflow-kind dispatch (t2db.15) are shipped. The + coordinator is NOT yet wired into production task intake + (daemon.go/pollNATS/publishTask) -- that is a follow-up ticket; + production wiring would make a real forge-triggered task flow through + this coordinator instead of only workflow.Route(). The trigger shape + reuses the existing workintake kind/label vocabulary; multi-action and + non-workflow-position playbooks remain rejected at load (hard + boundary, blocked on the execution-time gaps). 5. **Monetization boundary.** Sam has flagged this may be commercialized, and explicitly wants it discerned from other OSS event-driven-automation tooling. No design decision needed yet, but worth a note if/when diff --git a/examples/eda-playbooks/bug-tdd.yaml b/examples/eda-playbooks/bug-tdd.yaml new file mode 100644 index 000000000..a8df97c66 --- /dev/null +++ b/examples/eda-playbooks/bug-tdd.yaml @@ -0,0 +1,24 @@ +# Example EDA playbook document (t2db.15 slice). +# +# 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 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. +# +# Trigger matching reuses the existing workintake label/kind vocabulary +# (bug/feature/bootstrap, or a label set). The `when` condition is a CEL +# expression over `event` (the triggering event's fields) -- see +# docs/prds/eda-playbook-engine.md, resolved open question 1. + +trigger: + kind: bug + +actions: + - position: workflow + workflow: tdd + when: event.priority == 3 diff --git a/internal/app/archied/bootstrap.go b/internal/app/archied/bootstrap.go index c75d8520c..68fec832a 100644 --- a/internal/app/archied/bootstrap.go +++ b/internal/app/archied/bootstrap.go @@ -32,6 +32,7 @@ import ( "github.com/samcharles93/archie-core/internal/daemon" "github.com/samcharles93/archie-core/internal/domain/curator" "github.com/samcharles93/archie-core/internal/domain/eda/module" + "github.com/samcharles93/archie-core/internal/domain/eda/playbook" domainmemory "github.com/samcharles93/archie-core/internal/domain/memory" "github.com/samcharles93/archie-core/internal/domain/workflow" "github.com/samcharles93/archie-core/internal/domain/workflow/skillbuild" @@ -92,6 +93,8 @@ type boot struct { modules *module.ModuleRegistry + playbooks *playbook.Store + st store.TaskStore chatSessionStore gateway.SessionStore @@ -702,6 +705,19 @@ func (b *boot) loadWorkflows(ctx context.Context) error { log.Info("module registry built", "dir", cfg.ModuleDir, "kinds", b.modules.Len()) } + // EDA playbook documents (t2db.15): trigger + single workflow-kind action + // with CEL when conditions. Loaded at startup with the same reject-at-load + // rule -- any malformed playbook, multi-action playbook, non-workflow + // position, or when compile failure aborts startup, matching the routing- + // file load pattern (not degrade-and-skip). A nonexistent dir is an empty + // store. + b.playbooks, err = playbook.Load(cfg.EDAPlaybookDir) + if err != nil { + log.Error("eda playbook load failed", "dir", cfg.EDAPlaybookDir, "err", err) + return err + } + log.Info("eda playbooks loaded", "dir", cfg.EDAPlaybookDir, "playbooks", len(b.playbooks.Playbooks)) + skillsBase := cfg.SkillsDir if skillsBase == "" { skillsBase = cfg.WorkDir diff --git a/internal/config/config.go b/internal/config/config.go index bc5984d6f..2dbd24f60 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -458,6 +458,14 @@ type Config struct { // playbook directories are loaded. Repo-scoping of playbook dirs is a // separate, later decision and is not attempted here. PlaybookDirs []string `toml:"playbook_dirs" yaml:"playbook_dirs"` + // EDAPlaybookDir is an optional path to a directory of rich EDA playbook + // documents (*.yaml/*.yml: trigger + ordered actions with CEL `when` + // conditions, dispatched by the event coordinator). This is DISTINCT + // from PlaybookDirs, which holds FLAT binding files (kind/label -> + // workflow name) consumed by the routing loaders. A playbook here is a + // document (one trigger, one action in this slice); a binding file there + // is a map of keys. When empty, no EDA playbook documents are loaded. + EDAPlaybookDir string `toml:"eda_playbook_dir" yaml:"eda_playbook_dir"` // MaxRetries caps how many times a parked task is retried before // being permanently parked (status "dead"). Defaults to 3. MaxRetries int `toml:"max_retries" yaml:"max_retries"` diff --git a/internal/domain/eda/expr/expr.go b/internal/domain/eda/expr/expr.go index ca3223ab6..0bdfa0cf0 100644 --- a/internal/domain/eda/expr/expr.go +++ b/internal/domain/eda/expr/expr.go @@ -17,7 +17,6 @@ package expr import ( "cel.dev/cel-go/cel" - "cel.dev/cel-go/common/types/ref" ) // DefaultCostLimit bounds evaluation of every playbook expression (J5 in the @@ -90,8 +89,10 @@ type Program struct { // Eval evaluates the program against a dispatch-time context. A missing // field, a wrong type against a dyn value, or an exceeded cost limit is a -// returned error -- CEL evaluation is panic-free, so no recover() wrapper is -// needed (verified in the acceptance tests). +// returned error. CEL's own evaluation recovers panics internally (verified +// in cel-go's program.go: Eval wraps evaluation in recover), and its +// returned values implement ref.Val -- unwrapped here to the native Go value +// the caller expects. func (e *Env) Eval(prg *Program, ctx Context) (any, error) { data := map[string]any{ "event": ctx.Event, @@ -101,11 +102,5 @@ func (e *Env) Eval(prg *Program, ctx Context) (any, error) { if err != nil { return nil, err } - // CEL returns typed wrappers (types.Bool, types.String, ...) that - // implement ref.Val. Unwrap to the native Go value the caller expects; - // an already-native value passes through unchanged. - if rv, ok := v.(ref.Val); ok { - return rv.Value(), nil - } - return v, nil + return v.Value(), nil } diff --git a/internal/domain/eda/expr/expr_test.go b/internal/domain/eda/expr/expr_test.go index 2ff88695b..db897073b 100644 --- a/internal/domain/eda/expr/expr_test.go +++ b/internal/domain/eda/expr/expr_test.go @@ -5,7 +5,6 @@ import ( "testing" "cel.dev/cel-go/cel" - "cel.dev/cel-go/common/types/ref" ) // TestCompileEvalValidBooleanAgainstEvent is the happy path: a boolean @@ -156,10 +155,7 @@ func evalUnderLimit(prg *Program, limit uint64) (any, error) { if err != nil { return nil, err } - if rv, ok := v.(ref.Val); ok { - return rv.Value(), nil - } - return v, nil + return v.Value(), nil } // TestEvalPanicFreeHostileData: a pathological nested map under evaluation diff --git a/internal/domain/eda/module/module.go b/internal/domain/eda/module/module.go index 8436dfce5..67157ec09 100644 --- a/internal/domain/eda/module/module.go +++ b/internal/domain/eda/module/module.go @@ -147,27 +147,9 @@ func logInvoker(label, src string, k Kind) (invoker, error) { // interface symmetry with future kinds that need cancellation; the log kind // is side-effect-free and does not use it. func runLog(_ context.Context, label string, run func(log.Args) log.Result, rawArgs map[string]any) (map[string]any, error) { - var args log.Args - if rawArgs != nil { - if msg, ok := rawArgs["message"]; ok { - s, ok := msg.(string) - if !ok { - return nil, fmt.Errorf("module log: args.message is %T, want string", msg) - } - args.Message = s - } - if lvl, ok := rawArgs["level"]; ok { - s, ok := lvl.(string) - if !ok { - return nil, fmt.Errorf("module log: args.level is %T, want string", lvl) - } - args.Level = s - } - for key := range rawArgs { - if key != "message" && key != "level" { - return nil, fmt.Errorf("module log: unknown arg %q", key) - } - } + args, err := decodeLogArgs(rawArgs) + if err != nil { + return nil, err } res, err := yaegiutil.Safe(label, func() (log.Result, error) { return run(args), nil @@ -181,6 +163,36 @@ func runLog(_ context.Context, label string, run func(log.Args) log.Result, rawA }, nil } +// decodeLogArgs strictly decodes rawArgs into log.Args: a wrong-typed field +// or an unknown key is a reported error, never a silent zero-value fill -- +// the "schema defines the accepted message" rule. +func decodeLogArgs(rawArgs map[string]any) (log.Args, error) { + var args log.Args + if rawArgs == nil { + return args, nil + } + if msg, ok := rawArgs["message"]; ok { + s, ok := msg.(string) + if !ok { + return args, fmt.Errorf("module log: args.message is %T, want string", msg) + } + args.Message = s + } + if lvl, ok := rawArgs["level"]; ok { + s, ok := lvl.(string) + if !ok { + return args, fmt.Errorf("module log: args.level is %T, want string", lvl) + } + args.Level = s + } + for key := range rawArgs { + if key != "message" && key != "level" { + return args, fmt.Errorf("module log: unknown arg %q", key) + } + } + return args, nil +} + // Invoke calls the registered kind with rawArgs and returns the marshaled // result. An unregistered kind is a reported error. func (r *ModuleRegistry) Invoke(ctx context.Context, kind string, rawArgs map[string]any) (map[string]any, error) { diff --git a/internal/domain/eda/playbook/playbook.go b/internal/domain/eda/playbook/playbook.go new file mode 100644 index 000000000..476507be2 --- /dev/null +++ b/internal/domain/eda/playbook/playbook.go @@ -0,0 +1,263 @@ +// Package playbook is the EDA playbook document type and its event +// coordinator: the rich trigger+actions YAML shape (docs/prds/ +// eda-playbook-engine.md) with CEL `when` conditions (open question 1, +// resolved to CEL). This slice is deliberately single-action, +// workflow-position only: multi-action playbooks and Module/Channel/Forge +// action positions remain blocked on the unresolved execution-time gaps +// (mid-run failure semantics, idempotency for non-workflow actions) and are +// rejected at load -- the hard boundary, not to be relaxed without sign-off. +// +// This is an ADDITIONAL loading path alongside the flat kind/label binding +// files (t2db.9/.10/.11). workflow.Route() and the existing binding loaders +// are untouched; migrating Route() onto this mechanism is a separate, later +// decision. +package playbook + +import ( + "fmt" + "os" + "path/filepath" + "sort" + "strings" + + "gopkg.in/yaml.v3" + + "github.com/samcharles93/archie-core/internal/domain/eda/expr" + "github.com/samcharles93/archie-core/internal/domain/workflow" + "github.com/samcharles93/archie-core/internal/domain/workintake" +) + +// Store is the loaded set of playbooks, validated and compiled at load time. +type Store struct { + Playbooks []*Playbook + exprEnv *expr.Env +} + +// Playbook is one trigger+actions document. +type Playbook struct { + Trigger Trigger + Actions []Action +} + +// Trigger decides which incoming events this playbook matches. It reuses the +// existing workintake label/kind vocabulary -- no second label-matching +// mechanism is invented here. +type Trigger struct { + // Kind is the routing kind (bug/feature/bootstrap); empty matches the + // default (unlabelled) flow. + Kind workintake.Kind + // Labels is the closed label set this trigger matches; an empty set + // matches any labels. + Labels []string +} + +// Action is one step in a playbook. Exactly one action per playbook in this +// slice, position MUST be "workflow". +type Action struct { + Position string + // Workflow is the named workflow.Registry entry to dispatch to + // (position: workflow only). + Workflow string + // When is a compiled CEL condition; nil means unconditional. + When *expr.Program +} + +// rawPlaybook is the YAML document shape before compilation. +type rawPlaybook struct { + Trigger rawTrigger `yaml:"trigger"` + Actions []rawAction `yaml:"actions"` +} + +type rawTrigger struct { + Kind string `yaml:"kind"` + Labels []string `yaml:"labels"` +} + +type rawAction struct { + Position string `yaml:"position"` + Workflow string `yaml:"workflow"` + Kind string `yaml:"kind"` + When string `yaml:"when"` +} + +// Load reads every *.yaml/*.yml playbook in dir, validates each against the +// hard boundary (exactly one action, position workflow), and compiles each +// when expression. ANY failure -- malformed YAML, a multi-action playbook, a +// non-workflow position, a when compile error -- fails the whole load: the +// reject-at-load philosophy of the parent design doc. A missing directory is +// an empty store (matching the flat binding loaders' convention). +func Load(dir string) (*Store, error) { + entries, err := os.ReadDir(dir) + if os.IsNotExist(err) { + return &Store{exprEnv: expr.NewEnv()}, nil + } + if err != nil { + return nil, fmt.Errorf("read playbook dir %s: %w", dir, err) + } + + var names []string + for _, e := range entries { + if e.IsDir() { + continue + } + if ext := strings.ToLower(filepath.Ext(e.Name())); ext == ".yaml" || ext == ".yml" { + names = append(names, e.Name()) + } + } + sort.Strings(names) + + store := &Store{exprEnv: expr.NewEnv()} + for _, name := range names { + path := filepath.Join(dir, name) + pb, err := loadOne(path, store.exprEnv) + if err != nil { + return nil, err + } + store.Playbooks = append(store.Playbooks, pb) + } + return store, nil +} + +// loadOne loads and validates a single playbook file. +func loadOne(path string, env *expr.Env) (*Playbook, error) { + data, err := os.ReadFile(path) + if err != nil { + return nil, fmt.Errorf("read playbook %s: %w", path, err) + } + var raw rawPlaybook + if err := yaml.Unmarshal(data, &raw); err != nil { + return nil, fmt.Errorf("parse playbook %s: %w", path, err) + } + + pb := &Playbook{ + Trigger: Trigger{ + Kind: workintake.Kind(strings.TrimSpace(raw.Trigger.Kind)), + Labels: raw.Trigger.Labels, + }, + } + + if pb.Trigger.Kind == "" && len(raw.Trigger.Labels) == 0 { + return nil, fmt.Errorf("playbook %s: trigger must declare a kind or labels", path) + } + if err := pb.Trigger.Kind.Validate(); err != nil { + return nil, fmt.Errorf("playbook %s: %w", path, err) + } + + // HARD BOUNDARY (t2db.15): exactly one action, position workflow only. + if len(raw.Actions) != 1 { + return nil, fmt.Errorf( + "playbook %s: exactly one action is supported; got %d (multi-action playbooks are blocked on unresolved execution-time gaps)", + path, len(raw.Actions), + ) + } + a := raw.Actions[0] + b := "workflow" + if a.Position == "" { + // Default position for a single bare workflow name stays workflow for + // the smallest-useful case. + a.Position = b + } + if a.Position != b { + return nil, fmt.Errorf( + "playbook %s: action position %q is not supported; only %q actions ship (Module/Channel/Forge positions are blocked on unresolved execution-time gaps)", + path, a.Position, b, + ) + } + if strings.TrimSpace(a.Workflow) == "" { + return nil, fmt.Errorf("playbook %s: workflow-kind action must name a workflow", path) + } + + action := Action{ + Position: a.Position, + Workflow: strings.TrimSpace(a.Workflow), + } + if strings.TrimSpace(a.When) != "" { + prg, err := env.Compile(strings.TrimSpace(a.When)) + if err != nil { + return nil, fmt.Errorf("playbook %s: when condition: %w", path, err) + } + action.When = prg + } + pb.Actions = []Action{action} + return pb, nil +} + +// DispatchInput is what the coordinator evaluates a playbook against at +// dispatch time. +type DispatchInput struct { + // Labels are the incoming task's labels (comma-split already). + Labels []string + // Kind is the routing kind the labels produced (workintake.KindForLabels). + Kind string + // Event is the event payload exposed as `event` in CEL expressions. For + // a workflow-kind dispatch this carries the label/kind fields cheaply + // available at this point. + Event map[string]any +} + +// Match reports whether the playbook's trigger matches the input's labels. +func (pb *Playbook) Match(input DispatchInput) bool { + labels := input.Labels + if len(labels) == 0 { + labels = []string{} + } + kind := workintake.Kind(input.Kind) + + if pb.Trigger.Kind != "" && pb.Trigger.Kind != kind { + return false + } + if len(pb.Trigger.Labels) > 0 { + // Trigger labels must all be present in the input's labels. + inputSet := make(map[string]bool, len(labels)) + for _, l := range labels { + inputSet[strings.TrimSpace(l)] = true + } + for _, want := range pb.Trigger.Labels { + if !inputSet[strings.TrimSpace(want)] { + return false + } + } + } + return true +} + +// Dispatch returns the workflow to run for the input, or nil if no playbook +// matches (trigger mismatch or a when condition evaluating false). It reuses +// the named-workflow lookup that workflow.Route() uses -- it does not +// duplicate dispatch logic. +// +// A when evaluation error follows the resolved doc's J3: the condition +// evaluates to false and dispatch is skipped (the caller logs). +func (s *Store) Dispatch(reg workflow.Registry, input DispatchInput) *workflow.Workflow { + for _, pb := range s.Playbooks { + if !pb.Match(input) { + continue + } + a := pb.Actions[0] + if a.When != nil { + val, err := s.exprEnv.Eval(a.When, expr.Context{ + Event: input.Event, + Actions: map[string]map[string]any{}, + }) + if err != nil { + // J3: evaluation error -> false (skip), caller logs. + continue + } + b, ok := val.(bool) + if !ok || !b { + continue + } + } + // Reuse the same named-workflow lookup Route() uses: this is the + // workflow.Registry map access, not a second dispatch path. + if wf, ok := reg[a.Workflow]; ok { + // Return a copy to keep the registry immutable from callers. + wfCopy := wf + return &wfCopy + } + // Requested workflow unavailable: reported as nil (the caller's + // Route()-equivalent handles the missing-workflow failure shape). + return nil + } + return nil +} diff --git a/internal/domain/eda/playbook/playbook_test.go b/internal/domain/eda/playbook/playbook_test.go new file mode 100644 index 000000000..d0e10c674 --- /dev/null +++ b/internal/domain/eda/playbook/playbook_test.go @@ -0,0 +1,296 @@ +package playbook + +import ( + "os" + "path/filepath" + "strings" + "testing" + + "github.com/samcharles93/archie-core/internal/domain/workflow" +) + +func writeFile(t *testing.T, dir, name, content string) string { + t.Helper() + path := filepath.Join(dir, name) + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, []byte(content), 0o600); err != nil { + t.Fatalf("write %s: %v", path, err) + } + return path +} + +func registryWith(workflows ...string) workflow.Registry { + reg := workflow.Registry{} + for _, name := range workflows { + reg[name] = workflow.Workflow{Name: name, Stages: []workflow.Stage{}} + } + return reg +} + +// TestLoadSingleWorkflowActionRoundTrip: a playbook with trigger + one +// workflow-kind action + optional when loads and dispatches to the correct +// registered workflow when trigger+when match. +func TestLoadSingleWorkflowActionRoundTrip(t *testing.T) { + dir := t.TempDir() + writeFile(t, dir, "pb.yaml", ` +trigger: + kind: bug +actions: + - position: workflow + workflow: tdd + when: event.priority == 3 +`) + store, err := Load(dir) + if err != nil { + t.Fatalf("Load: %v", err) + } + if len(store.Playbooks) != 1 { + t.Fatalf("loaded %d playbooks, want 1", len(store.Playbooks)) + } + pb := store.Playbooks[0] + if pb.Actions[0].Position != "workflow" { + t.Errorf("position = %q, want workflow", pb.Actions[0].Position) + } + + // Match + dispatch: kind bug, priority 3 -> tdd. + reg := registryWith("implement", "tdd", "feasibility") + dispatched := store.Dispatch(reg, DispatchInput{ + Labels: []string{"bug"}, + Kind: "bug", + Event: map[string]any{"priority": 3}, + }) + if dispatched == nil { + t.Fatal("Dispatch = nil, want the tdd workflow") + } + if dispatched.Name != "tdd" { + t.Errorf("Dispatched workflow = %q, want tdd", dispatched.Name) + } +} + +// TestDispatchConditionFalseSkips: a when that compiles but evaluates false +// at dispatch correctly skips dispatch (nil returned, not silently ignored). +func TestDispatchConditionFalseSkips(t *testing.T) { + dir := t.TempDir() + writeFile(t, dir, "pb.yaml", ` +trigger: + kind: bug +actions: + - position: workflow + workflow: tdd + when: event.priority == 5 +`) + store, err := Load(dir) + if err != nil { + t.Fatalf("Load: %v", err) + } + dispatched := store.Dispatch(registryWith("tdd"), DispatchInput{ + Labels: []string{"bug"}, + Kind: "bug", + Event: map[string]any{"priority": 3}, + }) + if dispatched != nil { + t.Fatalf("Dispatch = %v, want nil (condition false -> skip)", dispatched) + } +} + +// TestLoadTwoActionsIsLoadFailure: the hard boundary -- a playbook declaring +// 2+ actions is a reported load failure. +func TestLoadTwoActionsIsLoadFailure(t *testing.T) { + dir := t.TempDir() + writeFile(t, dir, "pb.yaml", ` +trigger: + kind: bug +actions: + - position: workflow + workflow: tdd + - position: workflow + workflow: implement +`) + _, err := Load(dir) + if err == nil { + t.Fatal("Load(2 actions) = nil, want load failure") + } + if !strings.Contains(err.Error(), "exactly one action") { + t.Errorf("Load error = %q, want the one-action rule named", err.Error()) + } +} + +// TestLoadNonWorkflowPositionIsLoadFailure: a playbook with a non-workflow +// action position is a reported load failure. +func TestLoadNonWorkflowPositionIsLoadFailure(t *testing.T) { + dir := t.TempDir() + writeFile(t, dir, "pb.yaml", ` +trigger: + kind: bug +actions: + - position: module + kind: log +`) + _, err := Load(dir) + if err == nil { + t.Fatal("Load(module action) = nil, want load failure") + } + if !strings.Contains(err.Error(), "workflow") { + t.Errorf("Load error = %q, want the position restriction named", err.Error()) + } +} + +// TestLoadWhenCompileFailureDropsPlaybook: a when compile failure at load is +// reported and the playbook is dropped (same reject-at-load shape as the +// collision rule). +func TestLoadWhenCompileFailureDropsPlaybook(t *testing.T) { + dir := t.TempDir() + writeFile(t, dir, "pb.yaml", ` +trigger: + kind: bug +actions: + - position: workflow + workflow: tdd + when: event.label == +`) + _, err := Load(dir) + if err == nil { + t.Fatal("Load(bad when) = nil, want compile failure reported") + } +} + +// TestLoadEmptyTriggerIsLoadFailure: a playbook without a trigger cannot +// match anything -- rejected at load. +func TestLoadEmptyTriggerIsLoadFailure(t *testing.T) { + dir := t.TempDir() + writeFile(t, dir, "pb.yaml", ` +actions: + - position: workflow + workflow: tdd +`) + _, err := Load(dir) + if err == nil { + t.Fatal("Load(no trigger) = nil, want load failure") + } +} + +// TestDispatchTriggerMismatchSkips: a playbook whose trigger does not match +// the incoming labels does not dispatch. +func TestDispatchTriggerMismatchSkips(t *testing.T) { + dir := t.TempDir() + writeFile(t, dir, "pb.yaml", ` +trigger: + kind: feature +actions: + - position: workflow + workflow: tdd +`) + store, err := Load(dir) + if err != nil { + t.Fatalf("Load: %v", err) + } + dispatched := store.Dispatch(registryWith("tdd"), DispatchInput{ + Labels: []string{"bug"}, + Kind: "bug", + Event: map[string]any{}, + }) + if dispatched != nil { + t.Fatalf("Dispatch = %v, want nil (trigger kind mismatch)", dispatched) + } +} + +// TestDispatchNoWhenMatchesOnTriggerAlone: without a when, trigger match is +// sufficient to dispatch. +func TestDispatchNoWhenMatchesOnTriggerAlone(t *testing.T) { + dir := t.TempDir() + writeFile(t, dir, "pb.yaml", ` +trigger: + kind: bug +actions: + - position: workflow + workflow: tdd +`) + store, err := Load(dir) + if err != nil { + t.Fatalf("Load: %v", err) + } + dispatched := store.Dispatch(registryWith("tdd"), DispatchInput{ + Labels: []string{"bug"}, + Kind: "bug", + Event: map[string]any{}, + }) + if dispatched == nil || dispatched.Name != "tdd" { + t.Fatalf("Dispatch = %v, want tdd (no when = always match)", dispatched) + } +} + +// TestLoadOneInvalidFileFailsWholeLoad: the reject-at-load rule is store- +// level -- one malformed playbook in a directory fails the entire load, so +// a partially-valid set never starts (same shape as a routing collision). +func TestLoadOneInvalidFileFailsWholeLoad(t *testing.T) { + dir := t.TempDir() + writeFile(t, dir, "good.yaml", ` +trigger: + kind: bug +actions: + - position: workflow + workflow: tdd +`) + writeFile(t, dir, "bad.yaml", ` +trigger: + kind: bug +actions: + - position: workflow + workflow: tdd + - position: workflow + workflow: implement +`) + _, err := Load(dir) + if err == nil { + t.Fatal("Load(dir with one bad playbook) = nil, want whole-load failure") + } +} + +// TestLoadMissingDirIsEmptyStore: a nonexistent directory is an empty store +// (no playbooks), matching the flat binding loaders' convention. +func TestLoadMissingDirIsEmptyStore(t *testing.T) { + store, err := Load(filepath.Join(t.TempDir(), "does-not-exist")) + if err != nil { + t.Fatalf("Load(missing dir) = %v, want nil", err) + } + if len(store.Playbooks) != 0 { + t.Fatalf("loaded %d playbooks, want 0", len(store.Playbooks)) + } +} + +// TestShippedExamplePlaybookLoads verifies the operator-facing example +// (examples/eda-playbooks/bug-tdd.yaml) loads through the real loader -- +// the proof the shipped shape works, not just test fixtures. +func TestShippedExamplePlaybookLoads(t *testing.T) { + dir := filepath.Join("..", "..", "..", "..", "examples", "eda-playbooks") + store, err := Load(dir) + if err != nil { + t.Fatalf("Load(shipped example): %v", err) + } + if len(store.Playbooks) != 1 { + t.Fatalf("loaded %d playbooks, want 1", len(store.Playbooks)) + } + pb := store.Playbooks[0] + if pb.Actions[0].Workflow != "tdd" { + t.Errorf("example workflow = %q, want tdd", pb.Actions[0].Workflow) + } + // The example's when (event.priority == 3) dispatches for priority 3. + got := store.Dispatch(registryWith("tdd"), DispatchInput{ + Labels: []string{"bug"}, + Kind: "bug", + Event: map[string]any{"priority": 3}, + }) + if got == nil || got.Name != "tdd" { + t.Fatalf("Dispatch(example) = %v, want tdd", got) + } + // And skips for a non-matching priority. + if got := store.Dispatch(registryWith("tdd"), DispatchInput{ + Labels: []string{"bug"}, + Kind: "bug", + Event: map[string]any{"priority": 5}, + }); got != nil { + t.Fatalf("Dispatch(priority 5) = %v, want nil (when false)", got) + } +}