host P1: types DECLARE behavior, runner DERIVED (LIVE-VERIFIED)

Generalizes the hardcoded publish trigger into declared, capability-routed behavior.

- Types carry :behavior — flat string-keyed bindings {"verb" "type" "dag"} on the type-post
  (persist-safe, like :type-relations). The "article" type declares on-create → the "publish" DAG.
- host/blog--load-behaviors! gathers ALL posts' declarations into a registry at boot (serve.sh); the
  trigger match (host/blog--triggers :match = host/blog--match-behaviors) consults it. Hardcoded
  create+article trigger removed.
- Runner DERIVED (DEBT #2 fixed): match resolves :dag via host/blog--dag-registry and picks the
  runner via host/flow--select-runner over host/blog--runner-fleet ([exec-runner]; RA joins at
  RA-live). Each binding carries its :runner; behavior/-run-binding now uses the binding's runner
  (else the engine default) — so the capability model drives the LIVE engine.
- The type-def view shows each behavior + its derived runner (host/blog--behavior-lines).

LIVE PROOF: /article shows 'on create → publish DAG · needs {effect, branch} · runner: synchronous
(exec-fold)'; publishing on blog.rose-ash.com fired /flows validate+notify via the DECLARED path.
blog 213/213 (+3 P1), full host conformance 610/610. FINDING: load-behaviors! scans all posts, not
is-type?-filtered (article failed is-type? on the durable store though it passed in-memory).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-02 16:34:39 +00:00
parent c21be815f3
commit 9d29295820
5 changed files with 115 additions and 16 deletions

View File

@@ -41,13 +41,16 @@
(define behavior/-empty-trace (define behavior/-empty-trace
{:emitted (list) :ran (list) :effects (list) :suspended (list) :failed (list) :seen (list)}) {:emitted (list) :ran (list) :effects (list) :suspended (list) :failed (list) :seen (list)})
;; run one trigger binding: execute its DAG with the FULL env, then branch on :status. ;; run one trigger binding: execute its DAG with the FULL env, then branch on :status. The runner
;; is the binding's DERIVED runner (get binding :runner) if the registry resolved one (capability
;; selection — P1), else the engine's default :runner.
(define behavior/-run-binding (define behavior/-run-binding
(fn (engine activity binding depth acc) (fn (engine activity binding depth acc)
(let ((env {:activity activity :actor (get activity :actor) (let ((env {:activity activity :actor (get activity :actor)
:ctx (behavior/-ctx-of engine activity) :effects (behavior/-effects engine) :ctx (behavior/-ctx-of engine activity) :effects (behavior/-effects engine)
:binding binding})) :binding binding}))
(let ((result ((get (behavior/-runner engine) :run) (get binding :dag) env))) (let ((runner (or (get binding :runner) (behavior/-runner engine))))
(let ((result ((get runner :run) (get binding :dag) env)))
(let ((acc1 (assoc acc :ran (concat (get acc :ran) (list result)))) (let ((acc1 (assoc acc :ran (concat (get acc :ran) (list result))))
(status (or (get result :status) "done"))) (status (or (get result :status) "done")))
(cond (cond
@@ -62,7 +65,7 @@
(fn (a eff) (fn (a eff)
(behavior/-dispatch-effect engine eff depth (behavior/-dispatch-effect engine eff depth
(assoc a :effects (concat (get a :effects) (list eff))))) (assoc a :effects (concat (get a :effects) (list eff)))))
acc1 (or (get result :effects) (list)))))))))) acc1 (or (get result :effects) (list)))))))))))
;; dispatch one effect via the driver; recurse on any NEW activities it emits (the loop closes). ;; dispatch one effect via the driver; recurse on any NEW activities it emits (the loop closes).
(define behavior/-dispatch-effect (define behavior/-dispatch-effect

View File

@@ -161,6 +161,43 @@
(define host/blog--publish-ctx (define host/blog--publish-ctx
(fn (activity) {"category" (get activity :category) "slug" (get activity :slug)})) (fn (activity) {"category" (get activity :category) "slug" (get activity :slug)}))
;; ── P1: types DECLARE behavior; the runner is DERIVED from the DAG's capabilities ──────
;; A type-post carries :behavior = a list of flat string-keyed bindings {"verb" "type" "dag"} (like
;; :type-relations). At boot they're gathered into a registry the trigger match consults. :dag NAMES
;; a registered behavior DAG; the runner is chosen by host/flow--select-runner over the fleet — an
;; {effect,branch} composition → exec-runner; a {suspend} DAG → RA once RA-live adds it to the fleet.
(define host/blog--dag-registry {"publish" host/blog--publish-dag}) ;; name -> behavior DAG
(define host/blog--dag-of (fn (name) (get host/blog--dag-registry name)))
;; the runner fleet, cheapest-first. exec-runner only until RA-live stands up a persistent kernel.
(define host/blog--runner-fleet (list host/flow--exec-runner))
;; per-type behavior declaration, stored on the type-post (string-keyed → persist-safe).
(define host/blog--type-behavior (fn (type) (or (get (host/blog-get type) :behavior) (list))))
(define host/blog--set-type-behavior!
(fn (type bindings)
(let ((r (host/blog-get type))) (when r (host/blog--write! type (merge r {:behavior bindings}))))))
;; the behavior REGISTRY: every declared binding, gathered at boot (like load-edges!). Scans ALL
;; posts for a :behavior declaration — robust to is-type? state differences across the durable store
;; (a post without :behavior contributes nothing).
(define host/blog--behaviors (list))
(define host/blog--load-behaviors!
(fn ()
(set! host/blog--behaviors
(reduce (fn (acc t) (concat acc (host/blog--type-behavior t)))
(list) (host/blog-slugs)))))
;; match an activity against the registry → resolved bindings {:dag :runner}, runner DERIVED by caps.
;; (A binding whose DAG needs a capability no fleet runner has is SKIPPED — a soft bind failure.)
(define host/blog--match-behaviors
(fn (activity)
(reduce
(fn (acc b)
(if (and (= (get b "verb") (get activity :verb))
(= (get b "type") (get activity :object-type)))
(let ((dag (host/blog--dag-of (get b "dag"))))
(let ((runner (host/flow--select-runner host/blog--runner-fleet dag)))
(if (nil? runner) acc (concat acc (list {:dag dag :runner runner})))))
acc))
(list) host/blog--behaviors)))
;; ── P0.3: the seam WIRED on the live host ────────────────────────────── ;; ── P0.3: the seam WIRED on the live host ──────────────────────────────
;; The publish ENGINE = the execute-fold runner (flows.sx) + a local-SX on-publish trigger registry ;; The publish ENGINE = the execute-fold runner (flows.sx) + a local-SX on-publish trigger registry
;; + an in-process transport (the activity log = the event source) + the host driver (records each ;; + an in-process transport (the activity log = the event source) + the host driver (records each
@@ -173,10 +210,11 @@
(define host/blog--transport (define host/blog--transport
{:emit (fn (a) (set! host/blog--activity-log (concat host/blog--activity-log (list a)))) {:emit (fn (a) (set! host/blog--activity-log (concat host/blog--activity-log (list a))))
:deliver (fn () (list))}) ;; nothing inbound yet — P0 is synchronous :deliver (fn () (list))}) ;; nothing inbound yet — P0 is synchronous
;; P1: the trigger match consults the behavior REGISTRY (built from types' declarations), and each
;; matched binding carries its DERIVED runner (capability selection). Was a hardcoded create+article.
(define host/blog--triggers (define host/blog--triggers
{:register! (fn (spec dag hint) nil) {:register! (fn (spec dag hint) nil)
:match (fn (a) (if (and (= (get a :verb) "create") (= (get a :object-type) "article")) :match host/blog--match-behaviors})
(list {:dag host/blog--publish-dag}) (list)))})
;; P0.3b: the flow log is DURABLE — string-keyed records (dodge the keyword/persist top-level split), ;; P0.3b: the flow log is DURABLE — string-keyed records (dodge the keyword/persist top-level split),
;; persisted to the blog store under one key, so /flows survives a restart. Boot-loaded via ;; persisted to the blog store under one key, so /flows survives a restart. Boot-loaded via
;; host/blog-load-flowlog!. (Whole-list rewrite per effect — fine at P0 volume; cap/rotate later.) ;; host/blog-load-flowlog!. (Whole-list rewrite per effect — fine at P0 volume; cap/rotate later.)
@@ -1401,6 +1439,11 @@
;; tagged — but NOT subtyped (subtype-of is for types). The relation editors + relate ;; tagged — but NOT subtyped (subtype-of is for types). The relation editors + relate
;; handler read this; the metamodel types declare none, so they keep every kind. ;; handler read this; the metamodel types declare none, so they keep every kind.
(host/blog--set-type-relations! "article" (list "related" "is-a" "tagged")) (host/blog--set-type-relations! "article" (list "related" "is-a" "tagged"))
;; P1: the "article" type DECLARES its behavior — on-publish (a create of an article) runs the
;; "publish" DAG. The runner is derived from the DAG's caps ({effect,branch} → exec-runner). This
;; replaces the hardcoded trigger; host/blog--load-behaviors! gathers it into the registry at boot.
(host/blog--set-type-behavior! "article"
(list {"verb" "create" "type" "article" "dag" "publish"}))
;; ── cards-as-types: the blog content block vocabulary (kg-cards / content-on-sx ;; ── cards-as-types: the blog content block vocabulary (kg-cards / content-on-sx
;; block kinds) as metamodel types. "card" is the root; each card kind is a subtype ;; block kinds) as metamodel types. "card" is the root; each card kind is a subtype
;; with its own fields. These define the editor's card palette + the radar migrator's ;; with its own fields. These define the editor's card palette + the radar migrator's
@@ -2053,6 +2096,26 @@
;; the READ-ONLY type definition, shown on a type's PUBLIC page so anyone can read what the ;; the READ-ONLY type definition, shown on a type's PUBLIC page so anyone can read what the
;; type is: its fields, each Composition field's block grammar, and the relations its instances ;; type is: its fields, each Composition field's block grammar, and the relations its instances
;; may use. (The edit page's host/blog--type-def-editor is the writable form of the same data.) ;; may use. (The edit page's host/blog--type-def-editor is the writable form of the same data.)
;; P1: render a type's declared BEHAVIOR bindings + the DERIVED runner for each (visible, not
;; hand-set — the sync/durable classification falls out of the DAG's required capabilities).
(define host/blog--behavior-lines
(fn (slug)
(let ((bs (host/blog--type-behavior slug)))
(if (empty? bs) ""
(cons (quote div)
(cons (quote (:style "margin:0.4em 0 0"))
(cons (quote (b "Behavior: "))
(map (fn (bd)
(let ((dag (host/blog--dag-of (get bd "dag"))))
(let ((runner (host/flow--select-runner host/blog--runner-fleet dag))
(caps (host/flow--required-caps dag)))
(quasiquote (span :style "display:block;font-size:0.9em;color:#555"
(unquote (str "on " (get bd "verb") " → " (get bd "dag") " DAG · needs {"
(join ", " caps) "} · runner: "
(if (nil? runner) "NONE (capability unmet)"
(if (contains? (get runner :capabilities) "suspend")
"durable (RA)" "synchronous (exec-fold)")))))))))
bs))))))))
(define host/blog--type-def-view (define host/blog--type-def-view
(fn (slug) (fn (slug)
(let ((fields (host/blog-fields-of slug)) (let ((fields (host/blog-fields-of slug))
@@ -2074,7 +2137,8 @@
(unquote (if (> (len fields) 0) (unquote (if (> (len fields) 0)
(cons (quote ul) (append (quasiquote (:style "margin:0.3em 0")) rows)) (cons (quote ul) (append (quasiquote (:style "margin:0.3em 0")) rows))
(quote (p :style "color:#999;margin:0" "No declared fields.")))) (quote (p :style "color:#999;margin:0" "No declared fields."))))
(p :style "margin:0.4em 0 0" (b "Instances may be linked by: ") (unquote (join ", " rels))))))))) (p :style "margin:0.4em 0 0" (b "Instances may be linked by: ") (unquote (join ", " rels)))
(unquote (host/blog--behavior-lines slug))))))))
;; the first n elements of a list. ;; the first n elements of a list.
(define host/blog--take (define host/blog--take
(fn (xs n) (let loop ((ys xs) (k n) (acc (list))) (fn (xs n) (let loop ((ys xs) (k n) (acc (list)))

View File

@@ -185,6 +185,11 @@ EPOCH=1
echo "(epoch $EPOCH)" echo "(epoch $EPOCH)"
echo "(eval \"(host/blog-seed-types!)\")" echo "(eval \"(host/blog-seed-types!)\")"
EPOCH=$((EPOCH+1)) EPOCH=$((EPOCH+1))
# P1: gather the types' declared :behavior bindings into the registry the trigger match
# consults (so publishing an article fires its declared on-publish DAG, runner derived).
echo "(epoch $EPOCH)"
echo "(eval \"(host/blog--load-behaviors!)\")"
EPOCH=$((EPOCH+1))
# Seed a live demo of the composition fold (plans/composition-objects.md): /compose-demo # Seed a live demo of the composition fold (plans/composition-objects.md): /compose-demo
# is one composition object rendered by host/comp-render — renders differently by context. # is one composition object rendered by host/comp-render — renders differently by context.
echo "(epoch $EPOCH)" echo "(epoch $EPOCH)"

View File

@@ -175,6 +175,17 @@
;; the relation metadata into the in-memory cache the same way. ;; the relation metadata into the in-memory cache the same way.
(host/blog-seed-types!) (host/blog-seed-types!)
(host/blog-load-rel-kinds!) (host/blog-load-rel-kinds!)
(host/blog--load-behaviors!) ;; P1: gather types' declared behaviors into the registry (as boot does)
(host-bl-test "P1: load-behaviors! gathers the article type's declared on-publish binding"
(list (>= (len host/blog--behaviors) 1)
(contains? (map (fn (b) (get b "dag")) host/blog--behaviors) "publish"))
(list true true))
(host-bl-test "P1: match-behaviors resolves a create/article activity → a binding with a DERIVED runner"
(let ((ms (host/blog--match-behaviors {:verb "create" :object-type "article"})))
(list (len ms) (get (get (first ms) :runner) :capabilities)))
(list 1 (list "effect" "branch" "each")))
(host-bl-test "P1: a non-article activity matches nothing"
(len (host/blog--match-behaviors {:verb "create" :object-type "note"})) 0)
(host-bl-test "relate no auth -> redirect to login" (host-bl-test "relate no auth -> redirect to login"
(dream-status (host-bl-wapp (host-bl-send "POST" "/my-first-post/relate" nil (dream-status (host-bl-wapp (host-bl-send "POST" "/my-first-post/relate" nil
"application/x-www-form-urlencoded" "other=another-one"))) 303) "application/x-www-form-urlencoded" "other=another-one"))) 303)

View File

@@ -197,16 +197,25 @@ without touching the DAG or the wiring.
boundary). RECOMMENDATION: a narrow **RA SPIKE** next — prove one dispatch + one suspend/resume/ boundary). RECOMMENDATION: a narrow **RA SPIKE** next — prove one dispatch + one suspend/resume/
pump cycle in isolation — de-risks the whole durable/federated half before building P1/P2 on it. pump cycle in isolation — de-risks the whole durable/federated half before building P1/P2 on it.
## P1 — types DECLARE behavior (generalize) ## P1 — types DECLARE behavior (generalize) — DONE + LIVE-VERIFIED 2026-07-02
<!-- PREREQ (review): wire host/flow--bind into the engine (DEBT #2). Note: derivation is trivial until RA gives a 2nd runner. --> - [x] The type carries :behavior — a list of flat string-keyed bindings {"verb" "type" "dag"}
- [ ] The type carries :behavior = [{:on {:verb :object-type :guard} :dag <ref>}] — edited in the (persist-safe, like :type-relations), stored on the type-post (host/blog--type-behavior /
type-def editor beside grammar + relations. NO runner hint (the runner is DERIVED from the DAG's set-type-behavior!). The "article" type declares {"verb" "create" "type" "article" "dag" "publish"}.
required capabilities). - [x] The behavior REGISTRY (host/blog--behaviors) is gathered at boot from ALL posts' declarations
- [ ] host/blog--engine-for(object) builds the seam engine from the object's type bindings + (host/blog--load-behaviors!, in serve.sh after seed-types!); the trigger match (host/blog--triggers
registers the triggers, selecting each DAG's MINIMUM runner via artdag/analyze (required-caps) — :match = host/blog--match-behaviors) consults it. The hardcoded create+article trigger is GONE.
a `{effect}`-only DAG runs synchronously; a `wait` node pulls in the Erlang runner automatically. - [x] The runner is DERIVED, not hinted (DEBT #2 fixed): match-behaviors resolves the :dag via a DAG
- [ ] The type-def editor can SHOW each behavior's required capabilities + which runner it resolves registry (host/blog--dag-registry) and picks the runner via host/flow--select-runner over the fleet
to (the derived sync/durable/distributed classification, visible not hand-set). (host/blog--runner-fleet = [exec-runner]; RA joins at RA-live). Each binding carries its :runner;
behavior/-run-binding uses it. An {effect,branch} publish-DAG → exec-runner; a {suspend} DAG would
route to RA (proven in ra 9/9 with a 2-runner fleet).
- [x] The type-def view SHOWS each behavior + its derived runner (host/blog--behavior-lines): LIVE at
blog.rose-ash.com/article — "on create → publish DAG · needs {effect, branch} · runner:
synchronous (exec-fold)". Derived + visible, not hand-set.
- LIVE PROOF: published on blog.rose-ash.com → /flows fired validate+notify via the DECLARED path
(registry + derived runner). blog 213/213, full conformance 610/610. FINDING: load-behaviors! must
scan ALL posts, not filter by is-type? (article didn't pass is-type? on the durable store though it
did in-memory) — the type declaration is authoritative, the is-type? classification isn't reliable enough.
## P2 — state-change → activity emission (ALL events, not just publish) ## P2 — state-change → activity emission (ALL events, not just publish)
<!-- PREREQ (review): fix activity identity (DEBT #1) — :id must not be the bare CID, or relation events false-dedup. --> <!-- PREREQ (review): fix activity identity (DEBT #1) — :id must not be the bare CID, or relation events false-dedup. -->
@@ -282,6 +291,13 @@ covers everything until a DAG's cost/latency/placement forces the substrate.
activities), so business logic can change state, which federates, which triggers more flows. activities), so business logic can change state, which federates, which triggers more flows.
## Progress log (newest first) ## Progress log (newest first)
- 2026-07-02 — P1 DONE + LIVE-VERIFIED. Types DECLARE :behavior (stored on the type-post, gathered
into a registry at boot); the trigger match consults the registry; the runner is DERIVED via
host/flow--select-runner over the fleet (DEBT #2 fixed — no hardcoded trigger, no runner hint). The
/article page shows the declared behavior + derived runner. Published live → /flows fired via the
declared path. blog 213/213, conformance 610/610. Finding: load-behaviors! scans ALL posts (not
is-type?-filtered — unreliable on the durable store). NEXT: RA-live (persistent kernel wires RA into
the fleet → durable bindings route to RA), or P2 (all state-change → activity emission).
- 2026-07-02 — RA RUNNER BUILT + tested (module + integration). lib/host/ra.sx = a pure-SX seam - 2026-07-02 — RA RUNNER BUILT + tested (module + integration). lib/host/ra.sx = a pure-SX seam
runner with injected erl-eval (loads in the plain host, mock-testable); marshals our activity → runner with injected erl-eval (loads in the plain host, mock-testable); marshals our activity →
Erlang, drives flow_store, parses done/suspended → the runner contract. Dual-runner ROUTING in Erlang, drives flow_store, parses done/suspended → the runner contract. Dual-runner ROUTING in