Skip to main content

mlua_swarm/
blueprint.rs

1//! Blueprint runner — glue that executes a flow.ir AST
2//! (`mlua_flow_ir::Node`) through the engine. Each `Step.ref` is run as a
3//! single task via `start_task` + `dispatch_attempt_with_run_ctx`, and
4//! the resulting `Pass` `Value` is written back to `Step.out`.
5//!
6//! **Fully-async chain.** Uses `mlua_flow_ir::eval_async` and
7//! `AsyncDispatcher`; `block_on` and `spawn_blocking` are never mixed in,
8//! so the whole stack stays consistent with the engine's tokio async
9//! world.
10//!
11//! # Usage
12//!
13//! ```ignore
14//! let dispatcher = EngineDispatcher::with_spawner(engine.clone(), op_token, spawner);
15//! let bp: mlua_flow_ir::Node = serde_json::from_str(BP_JSON)?;
16//! let final_ctx = mlua_flow_ir::eval_async(&bp, init_ctx, &dispatcher).await?;
17//! ```
18//!
19//! # Schema types (the IF crate)
20//!
21//! `Blueprint` / `AgentDef` / `AgentKind` and friends live in the
22//! `mlua_swarm_schema` crate and are re-exported from here.
23//! The `struct`/`enum` set that used to live directly in `src/blueprint.rs`
24//! has been moved into the IF crate to support extension discipline,
25//! versioning, and external consumers.
26
27use crate::core::config::CheckPolicy;
28use crate::core::engine::Engine;
29use crate::core::projection_placement::ProjectionPlacement;
30use crate::core::state::{DispatchOutcome, TaskSpec};
31use crate::core::step_naming::StepNaming;
32use crate::store::run::{RunContext, StepEntry};
33use crate::types::{now_unix, CapToken};
34use crate::worker::adapter::SpawnerAdapter;
35use async_trait::async_trait;
36pub mod compiler;
37pub mod loader;
38pub mod store;
39
40use mlua_flow_ir::{AsyncDispatcher, EvalError};
41use serde_json::{Map, Value};
42use std::collections::HashMap;
43use std::sync::Arc;
44
45// The schema types are owned by the IF crate (mlua-swarm-schema); we re-export them here.
46/// The schema-side `OperatorKind` (see `crate::core::ctx::OperatorKind` for the
47/// runtime duplicate consumed by `Engine`). Re-exported under an explicit
48/// alias so callers reading `Blueprint.operators[].kind` /
49/// `Blueprint.default_operator_kind` do not have to reach into
50/// `mlua_swarm_schema` directly.
51pub use mlua_swarm_schema::OperatorKind as SchemaOperatorKind;
52pub use mlua_swarm_schema::{
53    current_schema_version, default_global_agent_kind, resolve_bound_agents,
54    resolve_bound_agents_strict, resolve_runner, AgentDef, AgentKind, AgentMeta, AgentProfile,
55    AgentProviderCapability, AgentProviderManifest, AuditDef, AuditMode, BindOutcome, BindReceipt,
56    BindRequest, BindingAttestation, BindingBackend, BindingDigest, BindingDigestParseError,
57    Blueprint, BlueprintMetadata, BlueprintOrigin, BoundAgent, BoundAgentResolveError,
58    CompilerHints, CompilerStrategy, MetaDef, OperatorDef, ProjectionPlacementSpec, Runner,
59    RunnerDef, RunnerResolutionSource, RunnerResolveError, SpawnerHints, WorkerModel,
60    CURRENT_SCHEMA_VERSION,
61};
62
63/// Bridges `mlua_flow_ir::AsyncDispatcher` to the engine's
64/// `start_task` + `dispatch_attempt_with_run_ctx` pair. Holds one
65/// Operator session token and one `spawner`, and spins up a fresh task
66/// per `Step.ref`, using it as the agent name.
67///
68/// Constructed via `with_spawner`; each dispatch goes through
69/// `engine.dispatch_attempt_with_run_ctx(token, tid, spawner, run_ctx)`
70/// so that when the enclosing `RunContext` carries a `replay_store` /
71/// `replay_cursor`, replay-hit skip and Ctx-snapshot append happen
72/// transparently. Nothing is stashed on engine-global state, so
73/// multiple dispatchers can drive different Blueprints against the same
74/// `Engine` in parallel without racing.
75///
76/// Optionally carries a [`RunContext`] (via [`Self::with_run`], issue #13
77/// run_id propagation): when present, every dispatched step's `run_id` is
78/// exposed to the worker through `Ctx.meta.runtime["run_id"]`, and a
79/// [`StepEntry`] is appended to `RunRecord.step_entries` once the step's
80/// outcome is known (dispatch is synchronous end-to-end here, so there is
81/// no need for a separate event/notification mechanism — the entry is
82/// written with its final status in one call).
83///
84/// Also carries the GH #21 Phase 2 named `MetaDef` pool (via
85/// [`Self::with_step_metas`]) — the Step tier's dispatch-time resolver;
86/// see [`Self::dispatch`]'s doc for the full envelope contract.
87///
88/// GH #23: optionally carries the Blueprint's [`StepNaming`] table (via
89/// [`Self::with_step_naming`], built once by
90/// `blueprint::compiler::Compiler::compile` — see that type's doc for the
91/// full addressing-space narrative). When present, [`Self::dispatch`]
92/// snapshots the same `Arc` into `EngineState.step_namings` for every
93/// dispatched task, keyed by its freshly-minted `StepId` — the storage
94/// half of the "construct once, read many" contract; `Engine::step_naming_for`
95/// is the read-back accessor later consumers (GH #23 subtask-2/3) pull
96/// from.
97///
98/// GH #27 (follow-up to #23): optionally also carries the Blueprint's
99/// [`ProjectionPlacement`] resolver (via [`Self::with_projection_placement`],
100/// built once by `Compiler::compile`) — the SAME snapshot-then-read-back
101/// contract as [`StepNaming`] above, this time read back via
102/// `Engine::projection_placement_for`.
103pub struct EngineDispatcher {
104    engine: Engine,
105    op_token: CapToken,
106    spawner: Arc<dyn SpawnerAdapter>,
107    run_ctx: Option<RunContext>,
108    step_metas: HashMap<String, Value>,
109    step_naming: Option<Arc<StepNaming>>,
110    projection_placement: Option<Arc<ProjectionPlacement>>,
111    binding_digests: HashMap<String, BindingDigest>,
112    /// The resolved `check_policy` cascade value
113    /// (`launch request > blueprint > server config`, collapsed exactly once
114    /// in `TaskLaunchService::launch`). Threaded into EVERY spawned step's
115    /// `TaskSpec.check_policy` by [`Self::dispatch`]. `None` (the default via
116    /// [`Self::with_spawner`]) preserves pre-cascade behavior byte-for-byte
117    /// — the engine's submit-time sink then falls back to
118    /// `EngineCfg.check_policy` (the server-wide default).
119    check_policy: Option<CheckPolicy>,
120}
121
122impl EngineDispatcher {
123    /// Build a dispatcher with no run-level tracing (`run_ctx = None`),
124    /// no named `MetaDef`s (`step_metas` empty), and no [`StepNaming`]
125    /// table — the pre-existing behavior. Use [`Self::with_run`] /
126    /// [`Self::with_step_metas`] / [`Self::with_step_naming`] to opt into
127    /// any of them.
128    pub fn with_spawner(
129        engine: Engine,
130        op_token: CapToken,
131        spawner: Arc<dyn SpawnerAdapter>,
132    ) -> Self {
133        Self {
134            engine,
135            op_token,
136            spawner,
137            run_ctx: None,
138            step_metas: HashMap::new(),
139            step_naming: None,
140            projection_placement: None,
141            binding_digests: HashMap::new(),
142            check_policy: None,
143        }
144    }
145
146    /// Attach a [`RunContext`] (builder style) so every dispatched step is
147    /// traced into `RunRecord.step_entries` and exposes its `run_id` via
148    /// `Ctx.meta.runtime`.
149    pub fn with_run(mut self, run_ctx: RunContext) -> Self {
150        self.run_ctx = Some(run_ctx);
151        self
152    }
153
154    /// GH #21 Phase 2: attach the named `MetaDef` pool (`Blueprint.metas`,
155    /// resolved by `service::task_launch::derive_step_metas` into a
156    /// `name -> ctx` map) that [`Self::dispatch`] resolves `$step_meta.ref`
157    /// envelopes against. Unconditional to call — an empty map (the
158    /// pre-#21-Phase-2 default) makes every `$step_meta.ref` lookup miss
159    /// loudly, same as a Blueprint that never declares `Blueprint.metas`.
160    pub fn with_step_metas(mut self, step_metas: HashMap<String, Value>) -> Self {
161        self.step_metas = step_metas;
162        self
163    }
164
165    /// Attach the immutable `AgentDef.name -> BoundAgent.binding_digest`
166    /// table used to correlate persisted step traces with launch bindings.
167    pub fn with_binding_digests(mut self, binding_digests: HashMap<String, BindingDigest>) -> Self {
168        self.binding_digests = binding_digests;
169        self
170    }
171
172    /// GH #23: attach the Blueprint's [`StepNaming`] table (built once by
173    /// `blueprint::compiler::Compiler::compile`). `None` (the default via
174    /// [`Self::with_spawner`]) preserves pre-GH-#23 behavior byte-for-byte
175    /// — [`Self::dispatch`] simply skips the `EngineState.step_namings`
176    /// snapshot for every caller that never opts in (e.g. tests that build
177    /// an `EngineDispatcher` directly instead of going through
178    /// `service::task_launch::TaskLaunchService::launch`).
179    pub fn with_step_naming(mut self, step_naming: Arc<StepNaming>) -> Self {
180        self.step_naming = Some(step_naming);
181        self
182    }
183
184    /// GH #27 (follow-up to #23): attach the Blueprint's
185    /// [`ProjectionPlacement`] resolver (built once by
186    /// `blueprint::compiler::Compiler::compile`). `None` (the default via
187    /// [`Self::with_spawner`]) preserves pre-GH-#27 behavior byte-for-byte
188    /// — [`Self::dispatch`] simply skips the
189    /// `EngineState.projection_placements` snapshot for every caller that
190    /// never opts in, mirroring [`Self::with_step_naming`]'s contract.
191    pub fn with_projection_placement(
192        mut self,
193        projection_placement: Arc<ProjectionPlacement>,
194    ) -> Self {
195        self.projection_placement = Some(projection_placement);
196        self
197    }
198
199    /// Attach the resolved `check_policy` cascade value
200    /// (`launch request > blueprint > server config`, collapsed exactly once
201    /// by `TaskLaunchService::launch`). Every step [`Self::dispatch`] spawns
202    /// gets this value stamped onto its `TaskSpec.check_policy`, so a
203    /// Blueprint- or launch-declared policy reaches the engine's submit-time
204    /// sink for ALL steps (not just the first). `None` (the default via
205    /// [`Self::with_spawner`]) is a no-op — the sink then falls back to
206    /// `EngineCfg.check_policy` (server-wide default), byte-for-byte the
207    /// pre-cascade behavior.
208    pub fn with_check_policy(mut self, check_policy: Option<CheckPolicy>) -> Self {
209        self.check_policy = check_policy;
210        self
211    }
212}
213
214/// GH #21 Phase 2: resolve a `$step_meta` envelope embedded in a Step's
215/// evaluated `in` value into `(initial_directive, step_ctx)` — the Step
216/// tier's dispatch-time entry point, called from [`EngineDispatcher::dispatch`]
217/// BEFORE `Engine::start_task` (critical: `start_task` seeds
218/// `EngineState.prompts[(tid, 1)]` from `TaskSpec.initial_directive`, so
219/// stripping the envelope any later would leak `$step_meta` into the
220/// worker prompt AND the WS `Spawn.directive` text).
221///
222/// Contract:
223///
224/// - `input` is not a JSON `Object`, or is an `Object` with no
225///   `"$step_meta"` key → passthrough unchanged, `step_ctx = None`
226///   (pre-#21-Phase-2 Blueprints are byte-identical through this path).
227/// - `input` IS an `Object` with a `"$step_meta"` key: the key is always
228///   stripped (never reaches the returned directive). Everything past
229///   this point is loud — an error names the offending step (`ref_`) and,
230///   for an unresolved `ref`, the defined `step_metas` names:
231///   - the envelope itself must be an `Object` shaped
232///     `{"ref": Option<String>, "inline": Option<Object>}`; any other
233///     shape is a malformed-envelope error;
234///   - `ref` (when present and non-null) is looked up in `step_metas`; an
235///     unknown name is an error (no silent skip). The resolved `MetaDef`
236///     ctx must itself be an `Object` (or the lookup is treated as
237///     malformed);
238///   - `inline` (when present and non-null) must be an `Object`;
239///   - the resolved Step-tier ctx = the `ref`-resolved ctx shallow-merged
240///     with `inline`, **`inline` wins** key collisions.
241/// - Directive rule (applied to the remaining `Object`, after
242///   `"$step_meta"` is stripped): if it still contains an `"$in"` key,
243///   that value becomes the returned directive (other sibling keys are
244///   ignored for the directive — envelope-only input, e.g. one final
245///   `$step_meta` key, therefore never becomes an empty directive by
246///   accident just because more keys existed alongside it). Otherwise
247///   the whole remainder becomes the directive; an empty remainder
248///   becomes `Value::String(String::new())`.
249fn resolve_step_envelope(
250    step_metas: &HashMap<String, Value>,
251    ref_: &str,
252    input: Value,
253) -> Result<(Value, Option<Value>), EvalError> {
254    let mut obj = match input {
255        Value::Object(obj) => obj,
256        other => return Ok((other, None)),
257    };
258    let Some(envelope) = obj.remove("$step_meta") else {
259        return Ok((Value::Object(obj), None));
260    };
261    let envelope = match envelope {
262        Value::Object(map) => map,
263        other => {
264            return Err(EvalError::DispatcherError {
265                ref_: ref_.to_string(),
266                msg: format!(
267                    "malformed $step_meta envelope for step '{ref_}': expected an object, got {other}"
268                ),
269            });
270        }
271    };
272
273    let ref_ctx: Option<Map<String, Value>> = match envelope.get("ref") {
274        None | Some(Value::Null) => None,
275        Some(Value::String(name)) => {
276            let resolved = step_metas.get(name).cloned().ok_or_else(|| {
277                EvalError::DispatcherError {
278                    ref_: ref_.to_string(),
279                    msg: format!(
280                        "$step_meta.ref '{name}' (step '{ref_}') is not a defined Blueprint.metas entry (defined: {:?})",
281                        step_metas.keys().collect::<Vec<_>>()
282                    ),
283                }
284            })?;
285            match resolved {
286                Value::Object(map) => Some(map),
287                other => {
288                    return Err(EvalError::DispatcherError {
289                        ref_: ref_.to_string(),
290                        msg: format!(
291                            "malformed $step_meta: MetaDef '{name}'.ctx must be an object, got {other}"
292                        ),
293                    });
294                }
295            }
296        }
297        Some(other) => {
298            return Err(EvalError::DispatcherError {
299                ref_: ref_.to_string(),
300                msg: format!(
301                    "malformed $step_meta.ref (step '{ref_}'): expected a string, got {other}"
302                ),
303            });
304        }
305    };
306
307    let inline: Option<Map<String, Value>> = match envelope.get("inline") {
308        None | Some(Value::Null) => None,
309        Some(Value::Object(map)) => Some(map.clone()),
310        Some(other) => {
311            return Err(EvalError::DispatcherError {
312                ref_: ref_.to_string(),
313                msg: format!(
314                    "malformed $step_meta.inline (step '{ref_}'): expected an object, got {other}"
315                ),
316            });
317        }
318    };
319
320    let step_ctx = match (ref_ctx, inline) {
321        (None, None) => None,
322        (Some(base), None) => Some(Value::Object(base)),
323        (None, Some(inline)) => Some(Value::Object(inline)),
324        (Some(mut base), Some(inline)) => {
325            for (k, v) in inline {
326                base.insert(k, v);
327            }
328            Some(Value::Object(base))
329        }
330    };
331
332    // Directive rule — only reached once a `$step_meta` envelope was
333    // present in `input`.
334    let initial_directive = if let Some(in_value) = obj.remove("$in") {
335        in_value
336    } else if obj.is_empty() {
337        Value::String(String::new())
338    } else {
339        Value::Object(obj)
340    };
341
342    Ok((initial_directive, step_ctx))
343}
344
345#[async_trait]
346impl AsyncDispatcher for EngineDispatcher {
347    async fn dispatch(&self, ref_: &str, input: Value) -> Result<Value, EvalError> {
348        // issue #18: the evaluated Step.in value passes straight through
349        // as `TaskSpec.initial_directive` — no premature `Value → String`
350        // coercion here. Consumers that need a rendered `String` do so at
351        // their own late boundary: `Engine::start_task` /
352        // `Engine::dispatch_attempt_with_run_ctx` render it into the
353        // `EngineState.prompts` table for the Worker HTTP path
354        // (`/v1/worker/prompt`), and
355        // `operator_ws::session::default_spawn_directive_with_task_directive`
356        // renders it into the WS `Spawn.directive` reminder text.
357        //
358        // GH #21 Phase 2: BEFORE that pass-through, resolve_step_envelope
359        // strips + resolves any `$step_meta` envelope — see its doc for
360        // the full contract. Inputs without one flow through unchanged.
361        let (initial_directive, step_ctx) = resolve_step_envelope(&self.step_metas, ref_, input)?;
362        let tid = self
363            .engine
364            .start_task(
365                &self.op_token,
366                TaskSpec {
367                    agent: ref_.to_string(),
368                    initial_directive,
369                    step_ctx,
370                    // The resolved cascade value (collapsed
371                    // once in `TaskLaunchService::launch`), threaded onto
372                    // every spawned step's spec. `None` falls back to
373                    // `EngineCfg.check_policy` at the submit-time sink.
374                    check_policy: self.check_policy,
375                },
376            )
377            .await
378            .map_err(|e| EvalError::DispatcherError {
379                ref_: ref_.to_string(),
380                msg: format!("start_task: {e}"),
381            })?;
382
383        // GH #23: snapshot the (already-built, Blueprint-wide) StepNaming
384        // table into `EngineState.step_namings` keyed by this dispatch's
385        // freshly-minted `tid` — the storage half of the "construct once
386        // (`Compiler::compile`), read many (`Engine::step_naming_for`)"
387        // contract. `None` (no `with_step_naming` call) is a no-op, same
388        // fail-open convention as the `run_ctx` step_entry append below:
389        // a secondary-persistence failure here must never mask the
390        // primary dispatch outcome.
391        if let Some(step_naming) = self.step_naming.clone() {
392            let tid_for_naming = tid.clone();
393            if let Err(e) = self
394                .engine
395                .with_state("EngineDispatcher::dispatch.step_naming", move |s| {
396                    s.step_namings.insert(tid_for_naming, step_naming);
397                })
398                .await
399            {
400                tracing::warn!(
401                    task_id = %tid,
402                    error = %e,
403                    "EngineDispatcher::dispatch: failed to snapshot StepNaming into EngineState"
404                );
405            }
406        }
407
408        // GH #27 (follow-up to #23): same snapshot pattern as StepNaming
409        // above — stash the (already-built, Blueprint-wide)
410        // ProjectionPlacement resolver into `EngineState.projection_placements`
411        // keyed by this dispatch's `tid`. `None` (no
412        // `with_projection_placement` call) is a no-op, same fail-open
413        // convention as the `step_naming` snapshot: a secondary-persistence
414        // failure here must never mask the primary dispatch outcome.
415        if let Some(projection_placement) = self.projection_placement.clone() {
416            let tid_for_placement = tid.clone();
417            if let Err(e) = self
418                .engine
419                .with_state(
420                    "EngineDispatcher::dispatch.projection_placement",
421                    move |s| {
422                        s.projection_placements
423                            .insert(tid_for_placement, projection_placement);
424                    },
425                )
426                .await
427            {
428                tracing::warn!(
429                    task_id = %tid,
430                    error = %e,
431                    "EngineDispatcher::dispatch: failed to snapshot ProjectionPlacement into EngineState"
432                );
433            }
434        }
435
436        // Route dispatch through the replay-aware sibling. When
437        // `run_ctx` carries a `replay_cursor` populated by the caller
438        // (`POST /v1/runs/:id/resume`), a matching row short-circuits
439        // to `DispatchOutcome::Pass` without touching the spawner; when
440        // `run_ctx.replay_store` is `Some`, every fresh Pass appends
441        // one Ctx-snapshot row so a later resume can replay it. With
442        // `run_ctx = None` this collapses to the same behavior as the
443        // legacy `dispatch_attempt_with(..., None)` call.
444        let outcome = self
445            .engine
446            .dispatch_attempt_with_run_ctx(
447                &self.op_token,
448                &tid,
449                &self.spawner,
450                self.run_ctx.as_ref(),
451            )
452            .await;
453
454        // issue #13 run_id propagation: append one step_entry per dispatched
455        // step (`RunStore.append_step_entry` is append-only — there is no
456        // in-place update — so the entry is written once here, after the
457        // outcome is known, carrying its final status). Secondary
458        // persistence failures are logged and swallowed, matching
459        // `mse-server`'s `finalize_run` convention: they must not mask the
460        // primary dispatch outcome the flow eval already has in hand.
461        if let Some(rc) = &self.run_ctx {
462            let status = match &outcome {
463                Ok(DispatchOutcome::Pass(_)) => "passed",
464                Ok(DispatchOutcome::Blocked(_)) => "blocked",
465                Ok(DispatchOutcome::Suspended(_)) => "suspended",
466                Ok(DispatchOutcome::Cancelled) => "cancelled",
467                Ok(DispatchOutcome::Timeout) => "timeout",
468                Err(_) => "failed",
469            };
470            let entry = StepEntry {
471                step_id: tid.clone(),
472                step_ref: Some(ref_.to_string()),
473                status: Some(status.to_string()),
474                binding_digest: self.binding_digests.get(ref_).cloned(),
475                at: now_unix(),
476            };
477            if let Err(e) = rc.run_store.append_step_entry(&rc.run_id, entry).await {
478                tracing::warn!(
479                    run_id = %rc.run_id,
480                    step_id = %tid,
481                    error = %e,
482                    "EngineDispatcher::dispatch: append_step_entry failed"
483                );
484            }
485        }
486
487        match outcome {
488            Ok(DispatchOutcome::Pass(v)) => Ok(v),
489            Ok(DispatchOutcome::Blocked(v)) => Err(EvalError::DispatcherError {
490                ref_: ref_.to_string(),
491                msg: format!("blocked: {v}"),
492            }),
493            Ok(other) => Err(EvalError::DispatcherError {
494                ref_: ref_.to_string(),
495                msg: format!("non-terminal outcome: {:?}", other),
496            }),
497            Err(e) => Err(EvalError::DispatcherError {
498                ref_: ref_.to_string(),
499                msg: format!("dispatch_attempt: {e}"),
500            }),
501        }
502    }
503}
504
505// ──────────────────────────────────────────────────────────────────────────
506// issue #21 Phase 2: `resolve_step_envelope` unit tests + a dispatch-level
507// end-to-end leak-proof test
508// ──────────────────────────────────────────────────────────────────────────
509
510#[cfg(test)]
511mod tests {
512    use super::*;
513    use serde_json::json;
514
515    fn metas(pairs: &[(&str, Value)]) -> HashMap<String, Value> {
516        pairs
517            .iter()
518            .map(|(k, v)| (k.to_string(), v.clone()))
519            .collect()
520    }
521
522    #[test]
523    fn no_envelope_string_input_passes_through_unchanged() {
524        let (directive, step_ctx) =
525            resolve_step_envelope(&HashMap::new(), "scout", json!("plain string")).unwrap();
526        assert_eq!(directive, json!("plain string"));
527        assert_eq!(step_ctx, None);
528    }
529
530    #[test]
531    fn no_envelope_plain_object_input_passes_through_unchanged() {
532        let input = json!({ "foo": "bar" });
533        let (directive, step_ctx) =
534            resolve_step_envelope(&HashMap::new(), "scout", input.clone()).unwrap();
535        assert_eq!(directive, input);
536        assert_eq!(step_ctx, None);
537    }
538
539    #[test]
540    fn envelope_with_only_ref_resolves_that_metadef_ctx() {
541        let step_metas = metas(&[("heavy-scan", json!({ "work_dir": "/x" }))]);
542        let input = json!({ "$step_meta": { "ref": "heavy-scan" }, "$in": "go" });
543        let (directive, step_ctx) = resolve_step_envelope(&step_metas, "scout", input).unwrap();
544        assert_eq!(directive, json!("go"));
545        assert_eq!(step_ctx, Some(json!({ "work_dir": "/x" })));
546    }
547
548    #[test]
549    fn envelope_with_only_inline_uses_inline_verbatim() {
550        let input = json!({
551            "$step_meta": { "inline": { "work_dir": "/inline-only" } },
552            "$in": "go"
553        });
554        let (directive, step_ctx) = resolve_step_envelope(&HashMap::new(), "scout", input).unwrap();
555        assert_eq!(directive, json!("go"));
556        assert_eq!(step_ctx, Some(json!({ "work_dir": "/inline-only" })));
557    }
558
559    #[test]
560    fn inline_wins_over_ref_on_key_collision() {
561        let step_metas = metas(&[(
562            "heavy-scan",
563            json!({ "work_dir": "/ref", "extra": "from-ref" }),
564        )]);
565        let input = json!({
566            "$step_meta": {
567                "ref": "heavy-scan",
568                "inline": { "work_dir": "/inline-wins" }
569            },
570            "$in": "go"
571        });
572        let (_, step_ctx) = resolve_step_envelope(&step_metas, "scout", input).unwrap();
573        assert_eq!(
574            step_ctx,
575            Some(json!({ "work_dir": "/inline-wins", "extra": "from-ref" })),
576            "inline must win the collided key while ref-only keys survive the merge"
577        );
578    }
579
580    #[test]
581    fn dollar_in_rule_extracts_directive_and_ignores_other_sibling_keys() {
582        let input = json!({
583            "$step_meta": { "inline": { "k": "v" } },
584            "$in": "the real directive",
585            "unrelated_sibling": "ignored"
586        });
587        let (directive, step_ctx) = resolve_step_envelope(&HashMap::new(), "scout", input).unwrap();
588        assert_eq!(directive, json!("the real directive"));
589        assert_eq!(step_ctx, Some(json!({ "k": "v" })));
590    }
591
592    #[test]
593    fn no_dollar_in_remainder_becomes_the_directive() {
594        let input = json!({
595            "$step_meta": { "inline": { "k": "v" } },
596            "other_key": "other_value"
597        });
598        let (directive, _) = resolve_step_envelope(&HashMap::new(), "scout", input).unwrap();
599        assert_eq!(directive, json!({ "other_key": "other_value" }));
600    }
601
602    #[test]
603    fn empty_remainder_becomes_empty_string_directive() {
604        let input = json!({ "$step_meta": { "ref": "heavy-scan" } });
605        let step_metas = metas(&[("heavy-scan", json!({ "work_dir": "/x" }))]);
606        let (directive, step_ctx) = resolve_step_envelope(&step_metas, "scout", input).unwrap();
607        assert_eq!(directive, Value::String(String::new()));
608        assert_eq!(step_ctx, Some(json!({ "work_dir": "/x" })));
609    }
610
611    #[test]
612    fn unresolved_ref_is_a_loud_dispatcher_error_naming_ref_and_defined() {
613        let step_metas = metas(&[("known", json!({}))]);
614        let input = json!({ "$step_meta": { "ref": "unknown" }, "$in": "go" });
615        let err = resolve_step_envelope(&step_metas, "scout", input).unwrap_err();
616        match err {
617            EvalError::DispatcherError { ref_, msg } => {
618                assert_eq!(ref_, "scout");
619                assert!(
620                    msg.contains("unknown"),
621                    "message must name the unresolved ref: {msg}"
622                );
623                assert!(
624                    msg.contains("known"),
625                    "message must list defined names: {msg}"
626                );
627            }
628            other => panic!("expected DispatcherError, got {other:?}"),
629        }
630    }
631
632    #[test]
633    fn malformed_step_meta_not_an_object_is_a_loud_error() {
634        let input = json!({ "$step_meta": "not-an-object" });
635        let err = resolve_step_envelope(&HashMap::new(), "scout", input).unwrap_err();
636        assert!(matches!(err, EvalError::DispatcherError { .. }));
637    }
638
639    #[test]
640    fn malformed_ref_non_string_is_a_loud_error() {
641        let input = json!({ "$step_meta": { "ref": 42 } });
642        let err = resolve_step_envelope(&HashMap::new(), "scout", input).unwrap_err();
643        assert!(matches!(err, EvalError::DispatcherError { .. }));
644    }
645
646    #[test]
647    fn malformed_inline_non_object_is_a_loud_error() {
648        let input = json!({ "$step_meta": { "inline": "not-an-object" } });
649        let err = resolve_step_envelope(&HashMap::new(), "scout", input).unwrap_err();
650        assert!(matches!(err, EvalError::DispatcherError { .. }));
651    }
652
653    #[test]
654    fn ref_resolved_metadef_ctx_non_object_is_a_loud_error() {
655        let step_metas = metas(&[("bad", json!("not-an-object"))]);
656        let input = json!({ "$step_meta": { "ref": "bad" } });
657        let err = resolve_step_envelope(&step_metas, "scout", input).unwrap_err();
658        assert!(matches!(err, EvalError::DispatcherError { .. }));
659    }
660
661    /// End-to-end proof (issue #21 Phase 2 Done Criteria #5): a `$step_meta`
662    /// envelope must never reach `EngineState.prompts[(tid, 1)]` — the
663    /// resolve step runs BEFORE `start_task` seeds that table.
664    #[tokio::test]
665    async fn dispatch_step_meta_envelope_never_leaks_into_stored_prompt() {
666        use crate::blueprint::compiler::{RustFnInProcessSpawnerFactory, SpawnerFactory};
667        use crate::core::config::EngineCfg;
668        use crate::types::{Role, StepId};
669        use crate::worker::adapter::WorkerResult;
670        use std::sync::Mutex as StdMutex;
671        use std::time::Duration;
672
673        let captured_tid: Arc<StdMutex<Option<StepId>>> = Arc::new(StdMutex::new(None));
674        let captured_tid_for_fn = captured_tid.clone();
675        let factory = RustFnInProcessSpawnerFactory::new().register_fn("echo", move |inv| {
676            let captured_tid = captured_tid_for_fn.clone();
677            async move {
678                *captured_tid.lock().unwrap() = Some(inv.task_id.clone());
679                Ok(WorkerResult {
680                    value: json!({ "ok": true }),
681                    ok: true,
682                })
683            }
684        });
685        let def = AgentDef {
686            name: "scout".into(),
687            kind: AgentKind::RustFn,
688            spec: json!({ "fn_id": "echo" }),
689            profile: None,
690            meta: None,
691            runner: None,
692            runner_ref: None,
693            verdict: None,
694        };
695        let spawner = factory.build(&def, None).expect("build");
696
697        let engine = Engine::new(EngineCfg::default());
698        let token = engine
699            .attach("ut-op", Role::Operator, Duration::from_secs(30))
700            .await
701            .expect("attach");
702        let step_metas = metas(&[("heavy-scan", json!({ "work_dir": "/x" }))]);
703        let dispatcher = EngineDispatcher::with_spawner(engine.clone(), token, spawner)
704            .with_step_metas(step_metas);
705
706        let input = json!({
707            "$step_meta": { "ref": "heavy-scan" },
708            "$in": "do the thing"
709        });
710        let out = dispatcher
711            .dispatch("scout", input)
712            .await
713            .expect("dispatch ok");
714        assert_eq!(out, json!({ "ok": true }));
715
716        let tid = captured_tid
717            .lock()
718            .unwrap()
719            .clone()
720            .expect("task_id captured");
721        let stored_prompt = engine
722            .with_state("test.read_prompt", move |s| {
723                s.prompts.get(&(tid, 1)).cloned()
724            })
725            .await
726            .expect("with_state")
727            .expect("prompt recorded for attempt 1");
728        assert_eq!(
729            stored_prompt,
730            json!("do the thing"),
731            "the stored prompt must be the post-envelope directive, with no $step_meta leakage"
732        );
733    }
734}