;; lib/host/execute.sx — the EXECUTE-fold: a SECOND domain over the SAME composition core
;; as the render-fold (lib/host/compose.sx), proving the algebra is domain-agnostic
;; (plans/composition-objects.md steps 7-8). Now that the core (host/comp-fold: the seq/alt/
;; each dispatch + when-predicates + each-source + context-environment + recursion) is shared,
;; a whole new domain is just a DOMAIN DICT + a leaf function:
;;
;;   render   {:empty ""        :combine str    …}  leaf -> markup;  fold -> HTML string
;;   execute  {:empty (list)    :combine concat …}  leaf -> effect;  fold -> effect log
;;
;; seq = steps in order, alt+when = branch, each = for-each — all from the core, unchanged.
;; Only the leaf semantics (effect vs markup) and the accumulator (list vs string) are new.
;; So the behaviour model (Slice 9) is "an execute-fold over a composition object", not a
;; separate system — the same structure an author edits as a document.

;; resolve an effect argument against the context: (field K) reads the :item/ctx value via
;; the SAME resolver the render-fold uses; anything else is a literal.
(define host/exec--arg
  (fn (a ctx)
    (if (and (= (type-of a) "list") (= (str (first a)) "field"))
      (host/comp--field (first (rest a)) ctx)
      a)))

;; the execute-fold's LEAF: an (effect VERB ARG…) node records one effect {:verb :args};
;; anything else contributes no effects. (The core handles seq/alt/each.)
(define host/exec--leaf
  (fn (node ctx dom)
    (if (not (= (type-of node) "list"))
      (list)
      (let ((h (str (first node))) (args (rest node)))
        (if (= h "effect")
          (list {:verb (str (first args)) :args (map (fn (a) (host/exec--arg a ctx)) (rest args))})
          (list))))))

;; the execute DOMAIN: effects concatenate into a log; the depth guard yields a max-depth
;; effect. host/comp-fold (compose.sx) supplies the seq/alt/each walk + when + each source.
(define host/exec--dom
  {:empty (list) :combine concat :overflow (list {:verb "max-depth" :args (list)}) :leaf host/exec--leaf})

;; public entry: execute a composition node against a context -> the effect log (the run).
(define host/exec-run (fn (node ctx) (host/comp-fold node ctx host/exec--dom)))
