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