Skip to main content

mlua_swarm/service/
task_launch.rs

1//! `TaskLaunchService` — the domain service that runs a Blueprint flow
2//! to completion through the engine.
3//!
4//! Responsibilities:
5//! 1. Compile the Blueprint and link it into a `SpawnerAdapter` (via
6//!    `service::linker::link`, wrapped by `EngineDispatcher::with_spawner`).
7//! 2. Acquire an Operator session (via `engine.attach`).
8//! 3. Run flow.ir's `eval_async_externs` through an `EngineDispatcher`
9//!    (threading the service-held `call_extern` registry) and return
10//!    the final `ctx`.
11//! 4. If any step fails (dispatcher error), the eval errors and
12//!    the failure propagates as-is.
13//!
14//! Callers on the Application layer never touch the engine directly —
15//! `bind`, `start_task`, and `eval_async` all stay inside the Service.
16//!
17//! A single-task-spawn API (calling `start_task` directly) is
18//! deliberately absent here: a single spawn can be modeled as a
19//! one-Step flow, and we do not want two interfaces for the same
20//! shape.
21
22use crate::binding::{
23    attest_bound_agents, binding_requests, validate_bound_agent_snapshots, AgentBindingProvider,
24    LegacyWorkerBindingPolicy, UnboundAgent,
25};
26use crate::blueprint::compiler::{materialize_bound_blueprint, CompileError, Compiler};
27use crate::blueprint::{
28    resolve_bound_agents, AuditDef, Blueprint, BoundAgent, EngineDispatcher, Runner,
29};
30use crate::core::agent_context::ContextPolicy;
31use crate::core::config::CheckPolicy;
32use crate::core::ctx::OperatorKind;
33use crate::core::engine::Engine;
34use crate::core::errors::EngineError;
35use crate::middleware::agent_context::AgentContextMiddleware;
36use crate::middleware::project_name_alias::ProjectNameAliasMiddleware;
37use crate::middleware::task_input::TaskInputMiddleware;
38use crate::middleware::worker_binding::WorkerBindingMiddleware;
39use crate::middleware::{AfterRunAuditMiddleware, SpawnerStack};
40use crate::operator::WorkerBinding;
41use crate::service::linker;
42use crate::store::run::{RunContext, SnapshotOrigin};
43use crate::types::{CapToken, Role};
44use mlua_flow_ir::{Externs, NoExterns};
45use serde::{Deserialize, Serialize};
46use serde_json::Value;
47use std::collections::HashMap;
48use std::sync::Arc;
49use std::time::Duration;
50use thiserror::Error;
51
52/// Derive the "BP Agent-level" tier of the `OperatorKind` cascade from a
53/// Blueprint: for every `AgentDef` whose `spec.operator_ref` resolves to an
54/// `OperatorDef` with a `Some` `kind`, map `AgentDef.name -> OperatorKind`.
55///
56/// Deliberately **not** filtered by `AgentDef.kind == AgentKind::Operator`:
57/// the `OperatorKind` cascade is a middleware-level cross-cutting concern
58/// (spawn_hook / senior_bridge / operator-delegate gating via `Ctx.operator`),
59/// orthogonal to the Worker IMPL axis that `AgentKind` expresses (see the
60/// crate root doc, "Operator is delivered as a cross-cutting overlay through
61/// `Ctx` plus middleware"). A `RustFn` / `Lua` / `Subprocess` agent can
62/// equally declare `spec.operator_ref` to opt into a BP-declared
63/// `OperatorKind` without changing its Worker IMPL. Agents without an
64/// `operator_ref`, an unresolved `operator_ref`, or an `OperatorDef.kind =
65/// None` are simply absent from the map (= that tier falls through for
66/// them). This is a separate, independent consumer of `Blueprint.operators`
67/// from the design-time `operator_ref` validation in
68/// `blueprint::compiler::Compiler::compile` (issue: `OperatorDef`
69/// first-class treatment), which only checks the reference resolves for
70/// `AgentKind::Operator` agents and is unaffected by this function.
71/// Build the `agent name → WorkerBinding` map from
72/// `Blueprint.agents[].profile.worker_binding` — the launch-time sibling of
73/// the compile-time resolution in `OperatorSpawnerFactory::build`. Consumed
74/// by `WorkerBindingMiddleware` so the delegate axis
75/// (`OperatorDelegateMiddleware`) can resolve the binding via `ctx.agent`
76/// like every other agent-keyed table (`CompiledAgentTable.routes` idiom).
77/// Agents without a declared binding are simply absent (no silent default).
78#[cfg(test)]
79pub(crate) fn derive_worker_bindings(blueprint: &Blueprint) -> HashMap<String, WorkerBinding> {
80    // Kept as a test-facing compatibility name. Production resolves once
81    // and calls `worker_bindings_from_bound_agents` with that snapshot.
82    let bound_agents = resolve_bound_agents(blueprint)
83        .expect("derive_worker_bindings requires a Blueprint with resolvable Runner refs");
84    worker_bindings_from_bound_agents(&bound_agents)
85}
86
87fn worker_bindings_from_bound_agents(
88    bound_agents: &[BoundAgent],
89) -> HashMap<String, WorkerBinding> {
90    bound_agents
91        .iter()
92        .filter_map(|bound| match &bound.runner {
93            Some(Runner::WsOperator { variant, tools })
94            | Some(Runner::WsClaudeCode { variant, tools }) => Some((
95                bound.agent.name.clone(),
96                WorkerBinding {
97                    variant: variant.clone(),
98                    tools: tools.clone(),
99                    request_digest: Some(bound.binding_digest.clone()),
100                    requested_model: bound.agent.profile.as_ref().and_then(|p| p.model.clone()),
101                },
102            )),
103            _ => None,
104        })
105        .collect()
106}
107
108/// Attest a freshly resolved snapshot (or enforce the strict-without-provider
109/// gate) exactly once, applied identically on both first-resolution paths in
110/// [`load_or_resolve_bound_agents`].
111///
112/// `strict` = [`crate::blueprint::CompilerStrategy::strict_binding`]:
113///
114/// - With a provider: every `Bound` outcome is validated and pinned; any
115///   `Unbound` agent fails the launch when `strict`, or (non-strict) is
116///   reported through a `tracing::warn!` and, if a `RunContext` is present, a
117///   `RunRecord.degradations` entry (the existing append-only channel). The
118///   agent stays `DeclarationOnly` either way.
119/// - Without a provider: a `strict` Blueprint that declares any Runner-backed
120///   agent fails fast (`PreDispatch`) because nothing can attest it; a
121///   non-strict Blueprint runs `DeclarationOnly` (the embed use case).
122async fn attest_or_gate_fresh(
123    bound_agents: &mut [BoundAgent],
124    binding_provider: Option<&dyn AgentBindingProvider>,
125    strict: bool,
126    run_ctx: Option<&RunContext>,
127) -> Result<(), TaskLaunchError> {
128    match binding_provider {
129        Some(provider) => {
130            let unbound = attest_bound_agents(provider, bound_agents, strict)
131                .await
132                .map_err(|error| TaskLaunchError::PreDispatch(error.to_string()))?;
133            for agent in &unbound {
134                record_unbound_degradation(agent, run_ctx).await;
135            }
136            Ok(())
137        }
138        None => {
139            if strict && !binding_requests(bound_agents).is_empty() {
140                return Err(TaskLaunchError::PreDispatch(format!(
141                    "strict_binding requires a binding provider but none is injected; \
142                     {} Runner-backed agent(s) cannot be attested",
143                    binding_requests(bound_agents).len()
144                )));
145            }
146            Ok(())
147        }
148    }
149}
150
151/// Record one non-strict unattested agent: a `tracing::warn!` always, plus a
152/// `RunRecord.degradations` append when a `RunContext` carries a run store.
153/// Observational only — a failed append is itself logged and never fails the
154/// launch (degradation recording must not gate a launch the strict decision
155/// already let through).
156async fn record_unbound_degradation(agent: &UnboundAgent, run_ctx: Option<&RunContext>) {
157    tracing::warn!(
158        agent = %agent.agent,
159        reason = %agent.reason,
160        "binding_unattested: agent runs DeclarationOnly (strict_binding is off)"
161    );
162    let Some(run_ctx) = run_ctx else {
163        return;
164    };
165    let entry = crate::store::run::DegradationEntry {
166        tool: "binding".to_string(),
167        error: agent.reason.clone(),
168        fallback: "DeclarationOnly".to_string(),
169        note: Some(format!(
170            "agent '{}' launched without a binding attestation (strict_binding off)",
171            agent.agent
172        )),
173        step_ref: None,
174        attempt: None,
175        at: crate::types::now_unix(),
176    };
177    if let Err(error) = run_ctx
178        .run_store
179        .append_degradation(&run_ctx.run_id, entry)
180        .await
181    {
182        tracing::warn!(
183            agent = %agent.agent,
184            %error,
185            "binding_unattested: failed to record degradation entry"
186        );
187    }
188}
189
190async fn load_or_resolve_bound_agents(
191    blueprint: &Blueprint,
192    run_ctx: Option<&RunContext>,
193    binding_provider: Option<&dyn AgentBindingProvider>,
194    legacy_worker_binding_policy: LegacyWorkerBindingPolicy,
195) -> Result<(Vec<BoundAgent>, SnapshotOrigin), TaskLaunchError> {
196    // Strict binding is a BP-level opt-in only (server config / launch
197    // request cascade is intentionally out of scope for this change).
198    let strict = blueprint.strategy.strict_binding;
199    let resolve_fresh = || match legacy_worker_binding_policy {
200        LegacyWorkerBindingPolicy::Allow => resolve_bound_agents(blueprint),
201        LegacyWorkerBindingPolicy::Reject => {
202            crate::blueprint::resolve_bound_agents_strict(blueprint)
203        }
204    };
205    let Some(run_ctx) = run_ctx else {
206        // No Run context (embed use): nothing to persist, and no snapshot
207        // to backfill — this is an initial launch by definition.
208        let mut bound_agents = resolve_fresh().map_err(CompileError::from)?;
209        attest_or_gate_fresh(&mut bound_agents, binding_provider, strict, None).await?;
210        return Ok((bound_agents, SnapshotOrigin::Launch));
211    };
212
213    let record = run_ctx
214        .run_store
215        .get(&run_ctx.run_id)
216        .await
217        .map_err(|e| TaskLaunchError::PreDispatch(format!("load Run binding snapshot: {e}")))?;
218    if let Some(input_json) = record.input_json.as_deref() {
219        let snapshot: Value = serde_json::from_str(input_json).map_err(|e| {
220            TaskLaunchError::PreDispatch(format!("decode Run launch snapshot: {e}"))
221        })?;
222        if let Some(value) = snapshot.get("bound_agents") {
223            let bound_agents: Vec<BoundAgent> =
224                serde_json::from_value(value.clone()).map_err(|e| {
225                    TaskLaunchError::PreDispatch(format!("decode Run BoundAgent snapshot: {e}"))
226                })?;
227            validate_bound_agent_snapshots(&bound_agents).map_err(|error| {
228                TaskLaunchError::PreDispatch(format!("validate Run BoundAgent snapshot: {error}"))
229            })?;
230            // [Crux D2-b] The fast path reads the PERSISTED origin marker
231            // rather than re-deriving it from `run_ctx.resume`, so a
232            // backfilled Run stays `resume_backfill` (and keeps its legacy
233            // replay keys) across every subsequent resume — the outcome is a
234            // function of the pinned snapshot, not of how many times the Run
235            // was resumed. An absent marker maps to `resume_backfill` (the
236            // safe side — see `SnapshotOrigin::from_snapshot`).
237            let origin = SnapshotOrigin::from_snapshot(&snapshot);
238            return Ok((bound_agents, origin));
239        }
240    }
241
242    let mut bound_agents = resolve_fresh().map_err(CompileError::from)?;
243    attest_or_gate_fresh(&mut bound_agents, binding_provider, strict, Some(run_ctx)).await?;
244    // [Crux D1-b] The origin is decided by the RunContext's explicit `resume`
245    // flag ALONE — never inferred from replay-cursor / step-entry presence,
246    // whose wiring is free to change. A resume / rerun-from re-derives the
247    // snapshot from the current Blueprint (no launch-time pin); an initial
248    // launch persists the authoritative pin.
249    let origin = if run_ctx.resume {
250        SnapshotOrigin::ResumeBackfill
251    } else {
252        SnapshotOrigin::Launch
253    };
254    if let Some(input_json) = record.input_json {
255        let mut snapshot: Value = serde_json::from_str(&input_json).map_err(|e| {
256            TaskLaunchError::PreDispatch(format!("decode Run launch snapshot: {e}"))
257        })?;
258        let object = snapshot.as_object_mut().ok_or_else(|| {
259            TaskLaunchError::PreDispatch("Run launch snapshot must be a JSON object".to_string())
260        })?;
261        // [Crux D1-a] `bound_agents` and its `bound_agents_origin` marker are
262        // written into the SAME snapshot object and persisted in the SAME
263        // `set_input_json` call — never two writes, so no intermediate state
264        // can carry one without the other.
265        object.insert(
266            "bound_agents".to_string(),
267            serde_json::to_value(&bound_agents).map_err(|e| {
268                TaskLaunchError::PreDispatch(format!("encode Run BoundAgent snapshot: {e}"))
269            })?,
270        );
271        object.insert(
272            crate::store::run::BOUND_AGENTS_ORIGIN_KEY.to_string(),
273            serde_json::to_value(origin).map_err(|e| {
274                TaskLaunchError::PreDispatch(format!("encode Run BoundAgent origin: {e}"))
275            })?,
276        );
277        run_ctx
278            .run_store
279            .set_input_json(
280                &run_ctx.run_id,
281                serde_json::to_string(&snapshot).map_err(|e| {
282                    TaskLaunchError::PreDispatch(format!("encode Run launch snapshot: {e}"))
283                })?,
284            )
285            .await
286            .map_err(|e| {
287                TaskLaunchError::PreDispatch(format!("persist Run BoundAgent snapshot: {e}"))
288            })?;
289    }
290    // A resume that had to backfill its bindings is an observed degradation
291    // (the binding identity is no longer a launch-time pin). Append-only,
292    // never fails the launch — mirrors `record_unbound_degradation`.
293    if origin == SnapshotOrigin::ResumeBackfill {
294        record_backfill_degradation(run_ctx, blueprint.id.as_str()).await;
295    }
296    Ok((bound_agents, origin))
297}
298
299/// Record one binding backfill: a pre-binding-snapshot Run was resumed (or
300/// reran) and its `bound_agents` were re-derived from the current Blueprint
301/// rather than restored from a launch-time pin. Same append-only,
302/// never-fail-the-launch posture as [`record_unbound_degradation`] — a failed
303/// append is logged and swallowed (the launch has already committed to
304/// running).
305async fn record_backfill_degradation(run_ctx: &RunContext, blueprint_id: &str) {
306    tracing::warn!(
307        run_id = %run_ctx.run_id,
308        blueprint = %blueprint_id,
309        "binding_backfill: resumed Run had no binding snapshot; bound_agents \
310         re-derived from the current Blueprint (not a launch-time pin)"
311    );
312    let entry = crate::store::run::DegradationEntry {
313        tool: "binding".to_string(),
314        error: "run resumed without a launch-pinned binding snapshot".to_string(),
315        fallback: "resume_backfill".to_string(),
316        note: Some(format!(
317            "run '{}' backfilled bound_agents from Blueprint '{}' at resume time",
318            run_ctx.run_id, blueprint_id
319        )),
320        step_ref: None,
321        attempt: None,
322        at: crate::types::now_unix(),
323    };
324    if let Err(error) = run_ctx
325        .run_store
326        .append_degradation(&run_ctx.run_id, entry)
327        .await
328    {
329        tracing::warn!(
330            run_id = %run_ctx.run_id,
331            %error,
332            "binding_backfill: failed to record degradation entry"
333        );
334    }
335}
336
337/// GH #34 — extract the Blueprint-declared after-run audit hooks
338/// (`Blueprint.audits`), the launch-time input to `AfterRunAuditMiddleware`.
339/// Trivial extraction (unlike [`derive_worker_bindings`] / the agent-context
340/// derivers below, no per-agent lookup is needed — `AuditDef.agent` is a
341/// plain agent-name string already validated against `Blueprint.agents` at
342/// `Compiler::compile` time). `[]` (every pre-#34 Blueprint) means "no
343/// audit layer at all" — see the conditional `.layer(...)` wiring in
344/// [`TaskLaunchService::launch`] (invariant #4: byte-identical behavior).
345fn derive_audits(blueprint: &Blueprint) -> Vec<AuditDef> {
346    blueprint.audits.clone()
347}
348
349/// Issue #21 Phase 1: build the agent-context supply axis's "BP Global" +
350/// "BP Agent-level" context tiers from a Blueprint — the launch-time
351/// sibling of [`derive_worker_bindings`] (same "no silent default"
352/// discipline: an agent's entry is present only when it declares one).
353/// Consumed by `AgentContextMiddleware`, which shallow-merges the two
354/// tiers per spawn (agent wins) and inserts the result into
355/// `ctx.meta.runtime` only-if-absent (see
356/// `crate::middleware::agent_context`'s module doc for the full merge +
357/// precedence narrative).
358///
359/// - `.0` (global) = [`Blueprint::default_agent_ctx`], unchanged.
360/// - `.1` (per-agent) = `AgentDef.name -> AgentMeta.ctx`, entry present
361///   only for agents whose `meta` is `Some` and who declare a `ctx`
362///   (directly via `meta.ctx`, and/or indirectly via
363///   [`AgentMeta::meta_ref`] — GH #21 Phase 2, see below).
364///
365/// # GH #21 Phase 2: `AgentMeta.meta_ref` resolution
366///
367/// When an agent declares `meta.meta_ref`, it is resolved against
368/// [`derive_step_metas`]'s pool and used as the BASE layer UNDER the
369/// agent's own inline `meta.ctx` (inline wins on key collision, shallow
370/// merge — see [`shallow_merge_inline_wins`]). An unresolved `meta_ref`
371/// at this point means the caller launched a Blueprint that bypassed
372/// `Compiler::compile`'s validation (the loud gate for this case, see
373/// `blueprint::compiler::Compiler::compile`'s `UnresolvedMetaRef` check);
374/// this function stays defensive and never panics — it logs a warning and
375/// skips the base layer, letting the agent's own inline `ctx` (if any)
376/// stand alone.
377pub(crate) fn derive_agent_ctx(blueprint: &Blueprint) -> (Option<Value>, HashMap<String, Value>) {
378    let global = blueprint.default_agent_ctx.clone();
379    let meta_pool = derive_step_metas(blueprint);
380    let per_agent = blueprint
381        .agents
382        .iter()
383        .filter_map(|ad| {
384            let meta = ad.meta.as_ref()?;
385            let inline = meta.ctx.clone();
386            let base = meta.meta_ref.as_ref().and_then(|name| {
387                let resolved = meta_pool.get(name).cloned();
388                if resolved.is_none() {
389                    tracing::warn!(
390                        agent = %ad.name,
391                        meta_ref = %name,
392                        "derive_agent_ctx: AgentMeta.meta_ref names an undefined Blueprint.metas entry; skipping the base layer"
393                    );
394                }
395                resolved
396            });
397            let merged = match (base, inline) {
398                (None, None) => None,
399                (Some(base), None) => Some(base),
400                (None, Some(inline)) => Some(inline),
401                (Some(base), Some(inline)) => Some(shallow_merge_inline_wins(base, inline)),
402            };
403            merged.map(|ctx| (ad.name.clone(), ctx))
404        })
405        .collect();
406    (global, per_agent)
407}
408
409/// GH #21 Phase 2: shallow-merge `base` with `inline`, `inline` winning
410/// key collisions. Both sides being JSON `Object`s is the meaningful case
411/// (per-key merge); a non-`Object` `inline` is used as-is (it "wins"
412/// entirely — the malformed-shape case is left to
413/// `AgentContextMiddleware`'s own tier merge, which already warns + skips
414/// a non-`Object` tier value downstream, never failing the spawn).
415pub(crate) fn shallow_merge_inline_wins(base: Value, inline: Value) -> Value {
416    match (base, inline) {
417        (Value::Object(mut base), Value::Object(inline)) => {
418            for (k, v) in inline {
419                base.insert(k, v);
420            }
421            Value::Object(base)
422        }
423        (_, inline) => inline,
424    }
425}
426
427/// GH #21 Phase 2: build the `Blueprint.metas` named pool (`MetaDef.name
428/// -> MetaDef.ctx`) — the launch-time sibling of [`derive_worker_bindings`]
429/// / [`derive_agent_ctx`], resolving the Step tier's shared pool instead
430/// of a per-agent map. Consumed by `EngineDispatcher::with_step_metas`
431/// (the Step tier's `$step_meta.ref` resolver) and, indirectly, by
432/// [`derive_agent_ctx`]'s `AgentMeta.meta_ref` resolution (the Agent
433/// tier shares the same pool).
434fn derive_step_metas(blueprint: &Blueprint) -> HashMap<String, Value> {
435    blueprint
436        .metas
437        .iter()
438        .map(|m| (m.name.clone(), m.ctx.clone()))
439        .collect()
440}
441
442/// Issue #21 Phase 1: build the [`ContextPolicy`] cascade's "BP Global" +
443/// "BP Agent-level" tiers from a Blueprint — same shape and discipline as
444/// [`derive_agent_ctx`], from `Blueprint.default_context_policy` /
445/// `AgentMeta.context_policy` instead. Consumed by
446/// `AgentContextMiddleware`, which resolves the effective policy per spawn
447/// (per-agent tier outranks the BP-global one; pass-all when neither is
448/// declared for the dispatching agent).
449fn derive_context_policies(
450    blueprint: &Blueprint,
451) -> (Option<ContextPolicy>, HashMap<String, ContextPolicy>) {
452    let default_policy = blueprint.default_context_policy.clone();
453    let per_agent = blueprint
454        .agents
455        .iter()
456        .filter_map(|ad| {
457            let meta = ad.meta.as_ref()?;
458            let policy = meta.context_policy.clone()?;
459            Some((ad.name.clone(), policy))
460        })
461        .collect();
462    (default_policy, per_agent)
463}
464
465/// Issue #19 ST3: shallow-merge the "BP Global" default `init_ctx`
466/// (`Blueprint.default_init_ctx`) with the Task-level `init_ctx` — the
467/// second layer of the (eventual 4-layer) init-ctx cascade, following the
468/// same "BP default, Task overrides" shape as the `OperatorKind` cascade
469/// (see `derive_bp_agent_kinds` / `TaskLaunchInput::operator_kind`).
470///
471/// Semantics (deliberately a single rule, no deep merge / JSON Patch):
472///
473/// - `bp_default = None` → `task_init_ctx` passes through unchanged
474///   (pre-#19 Blueprints keep today's exact behavior).
475/// - Both sides are `Value::Object` → shallow key-wise merge, Task wins
476///   on collision (`task_init_ctx`'s keys are applied last).
477/// - `task_init_ctx` is present but not an `Object` (`Null` / `String` /
478///   `Array` / `Number` / `Bool`) → Task fully replaces the BP default;
479///   the caller's non-Object seed is respected as-is.
480fn merge_init_ctx(bp_default: Option<&Value>, task_init_ctx: &Value) -> Value {
481    match (bp_default, task_init_ctx) {
482        (Some(Value::Object(bp_map)), Value::Object(task_map)) => {
483            let mut merged = bp_map.clone();
484            for (k, v) in task_map {
485                merged.insert(k.clone(), v.clone());
486            }
487            Value::Object(merged)
488        }
489        (None, _) => task_init_ctx.clone(),
490        (_, task) => task.clone(),
491    }
492}
493
494/// Issue #19 ST4: 3-layer shallow-merge of the init-ctx cascade — BP
495/// default → Task → Run (lowest to highest priority). Built by chaining
496/// [`merge_init_ctx`] twice rather than introducing a distinct 3-way merge
497/// algorithm, so the Run layer inherits exactly the same "shallow Object
498/// merge, non-Object fully replaces" rule [`merge_init_ctx`] already
499/// established for the BP/Task pair (see its doc for the full semantics).
500///
501/// - `run_override: None` is a no-op — the BP+Task merge passes through
502///   unchanged, so `POST /v1/tasks/:id/runs` with no body (or a body that
503///   omits `init_ctx_override`) preserves today's rekick behavior
504///   byte-for-byte.
505/// - `run_override: Some(_)` layers on top exactly like `task_init_ctx`
506///   layers on top of `bp_default` above: both `Object` → shallow
507///   key-wise merge with Run winning collisions; Run non-`Object` →
508///   fully replaces the BP+Task merge.
509pub fn merge_init_ctx_3layer(
510    bp_default: Option<&Value>,
511    task_init_ctx: &Value,
512    run_override: Option<&Value>,
513) -> Value {
514    let bp_task = merge_init_ctx(bp_default, task_init_ctx);
515    match run_override {
516        Some(run) => merge_init_ctx(Some(&bp_task), run),
517        None => bp_task,
518    }
519}
520
521fn derive_bp_agent_kinds(blueprint: &Blueprint) -> HashMap<String, OperatorKind> {
522    let mut out = HashMap::new();
523    if blueprint.operators.is_empty() {
524        return out;
525    }
526    for agent in &blueprint.agents {
527        let Some(op_ref) = agent.spec.get("operator_ref").and_then(|v| v.as_str()) else {
528            continue;
529        };
530        let Some(op_def) = blueprint.operators.iter().find(|o| o.name == op_ref) else {
531            continue;
532        };
533        if let Some(kind) = op_def.kind {
534            out.insert(agent.name.clone(), OperatorKind::from(kind));
535        }
536    }
537    out
538}
539
540/// Failure modes of [`TaskLaunchService::launch`].
541#[derive(Debug, Error)]
542pub enum TaskLaunchError {
543    /// `Compiler::compile` rejected the Blueprint.
544    #[error("compile: {0}")]
545    Compile(#[from] CompileError),
546    /// `Engine::attach_with_ids` failed.
547    #[error("engine: {0}")]
548    Engine(#[from] EngineError),
549    /// A `Step` inside `flow.ir`'s `eval_async` produced a dispatcher
550    /// error, or a sub-flow raised.
551    ///
552    /// GH #76 error surface: struct variant carrying structured failure detail lifted
553    /// off the eval boundary — `failed_step` / `verdict_value` come from
554    /// the [`crate::store::run::RunContext::last_failure`] breadcrumb the
555    /// dispatcher's Blocked arm writes; `partial_ctx` comes from the same
556    /// `RunContext`'s [`crate::store::run::RunContext::snapshot_partial_ctx`]
557    /// reconstruction of the step-entry trace persisted so far. All three
558    /// new fields are `Option` because dispatch may error via a path that
559    /// does not go through the Blocked arm (upstream flow-ir eval errors,
560    /// e.g. a malformed AST or an unresolved `CallExtern` — `run_ctx`
561    /// itself may be `None`, or the flow may fail before any step was
562    /// dispatched). `Display` preserves the pre-#76 `"flow eval: {message}"`
563    /// prefix byte-for-byte for backwards-compatible stringification.
564    #[error("flow eval: {message}")]
565    FlowEval {
566        /// The stringified underlying error (`EvalError::to_string()` on
567        /// the current write path). Preserves the pre-#76 message text.
568        message: String,
569        /// The `Step.ref` of the step whose Blocked outcome aborted the
570        /// flow, when the dispatcher's Blocked arm was the abort site.
571        /// `None` for abort paths that do not go through the dispatcher
572        /// (see the enum variant doc).
573        failed_step: Option<String>,
574        /// The verdict `Value` the aborting step carried
575        /// (`DispatchOutcome::Blocked(v)`'s `v`). `None` for the same
576        /// reasons as `failed_step`.
577        verdict_value: Option<Value>,
578        /// In-tree partial_ctx surfaces step-entry log (step_id →
579        /// status/binding_digest). Full value-level partial_ctx requires
580        /// upstream mlua-flow-ir support to expose `storage.snapshot()`
581        /// on error. See [`crate::store::run::RunContext::snapshot_partial_ctx`]
582        /// for the reconstructed JSON shape. `None` when `run_ctx` was
583        /// unavailable at the map_err site (e.g. `TaskLaunchService::launch`
584        /// called without a `RunContext`).
585        partial_ctx: Option<Value>,
586    },
587    /// Pre-dispatch validation failed: the launch was rejected before any
588    /// step was dispatched. Raised when the effective check_policy
589    /// (launch request > blueprint > server config) is Strict and the
590    /// launch supplied neither project_root nor work_dir — a strict task
591    /// would deterministically fail at its first submit-time file
592    /// materialize, so the launch fails fast instead.
593    #[error("pre-dispatch: {0}")]
594    PreDispatch(String),
595}
596
597/// Canonical bag of Task-level fields (`project_root` / `work_dir` /
598/// `task_metadata`) — [`TaskLaunchInput::task_input`]'s type.
599///
600/// Issue #19 ST2: replaces the ST1 `resolve_task_level_init_ctx`
601/// fold-back-into-`init_ctx` bridge (removed from
602/// `mlua-swarm-server`'s `run_flow_form`). Callers resolve these three
603/// fields once at the wire boundary — sibling body field first, falling
604/// back to the legacy shape (same three keys nested directly inside
605/// `init_ctx`) only there — and hand the result straight through here;
606/// `init_ctx` itself is no longer mutated to carry them, so it stays a
607/// pure flow-ir eval seed identical to whatever the caller sent.
608///
609/// Each field is independently optional — see
610/// [`crate::middleware::task_input::TaskInputMiddleware::new_from_fields`],
611/// which this is built for.
612///
613/// Issue #19 ST4: also `Serialize`/`Deserialize` so it can travel over the
614/// wire as `RunKickRequest.task_input_override` (`mlua-swarm-server`'s
615/// `tasks` module) and be snapshotted into `TaskRecord.task_input_spec`
616/// (JSON) for rekick to resolve back out of. Every field is
617/// `#[serde(default)]` so a caller may omit any subset (or send `{}`) and
618/// still deserialize.
619#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, schemars::JsonSchema)]
620pub struct TaskInputSpec {
621    /// Task-level project root path.
622    #[serde(default)]
623    pub project_root: Option<String>,
624    /// Task-level working directory path.
625    #[serde(default)]
626    pub work_dir: Option<String>,
627    /// Task-level arbitrary metadata bag (a JSON object, or `None`).
628    #[serde(default)]
629    #[schemars(with = "Option<Value>")]
630    pub task_metadata: Option<Value>,
631}
632
633/// Input to [`TaskLaunchService::launch`].
634#[derive(Debug, Clone)]
635pub struct TaskLaunchInput {
636    /// The Blueprint to compile, link, and run.
637    pub blueprint: Blueprint,
638    /// Caller-supplied id for the Operator that owns this run.
639    pub operator_id: String,
640    /// The Operator's role for this run.
641    pub role: Role,
642    /// How long the attached session is allowed to live.
643    pub ttl: Duration,
644    /// "Runtime Global" tier of the `OperatorKind` cascade. `Some(_)` is
645    /// always an explicit request — including `Some(OperatorKind::Automate)`
646    /// — that outranks the BP-level tiers (`OperatorDef.kind` /
647    /// `Blueprint.default_operator_kind`); `None` leaves it unspecified so
648    /// those tiers / the final default decide. Under `MainAi` or
649    /// `Composite`, `MainAIMiddleware`'s `spawn_hook` before/after
650    /// callbacks become effective. See
651    /// `crate::core::ctx::collapse_operator_kind`.
652    pub operator_kind: Option<OperatorKind>,
653    /// `SeniorBridge` registry ID. `None` — no bridge; `Some(id)` —
654    /// attach a bridge previously registered via
655    /// `engine.register_senior_bridge`.
656    pub bridge_id: Option<String>,
657    /// `SpawnHook` registry ID. Same shape as above, via
658    /// `engine.register_spawn_hook`.
659    pub hook_id: Option<String>,
660    /// Operator registry ID — used on the path that hands the whole
661    /// spawn off to an external Operator. Name previously registered
662    /// with `engine.register_operator`; resolved by
663    /// `OperatorDelegateMiddleware`, which — for `kind = MainAi` or
664    /// `Composite` — bypasses `inner.spawn` and calls
665    /// `operator.execute`.
666    pub operator_backend_id: Option<String>,
667    /// Run-scoped Operator session pin — the session id (`S-<hex>`) this
668    /// launch binds its whole Spawn stream to, independent of which axis
669    /// carries the spawn.
670    ///
671    /// A Blueprint's `spec.operator_ref` names a logical role; the role's
672    /// holder is process-global, so with two drivers on one process "the
673    /// role" and "the session this launch belongs to" are different facts.
674    /// `Some(sid)` makes the second one authoritative for this launch: the
675    /// binding provider attests through the pinned session
676    /// ([`crate::binding::AgentBindingProvider::pinned_to_session`]) and the
677    /// compiler resolves every `kind = Operator` agent against it
678    /// ([`Compiler::compile_bound_pinned`]). A pin naming no live session
679    /// fails the launch rather than falling back to the role.
680    ///
681    /// `None` (the default via [`Self::automate`]) leaves both resolutions
682    /// exactly as they were — role lookup, byte-for-byte.
683    ///
684    /// Orthogonal to [`Self::operator_backend_id`]: that field is the
685    /// opt-in `operator_delegate` layer's session-wide backend, which
686    /// bypasses the per-agent spawners entirely when a Blueprint declares
687    /// the layer. A launch may carry both; the delegate layer keeps winning
688    /// where it applies, and the pin decides the AgentSpec axis underneath.
689    pub operator_pin: Option<String>,
690    /// "Runtime Agent-level" tier (highest priority) of the `OperatorKind`
691    /// cascade — per-agent override, keyed by `AgentDef.name`. Empty by
692    /// default (no override for any agent). See
693    /// `crate::core::ctx::collapse_operator_kind` for the full tier list.
694    pub operator_kind_overrides: HashMap<String, OperatorKind>,
695    /// The initial `ctx` (JSON `Value`) that flow.ir's `eval_async`
696    /// starts from. Every `Step.in` `$.<path>` reference reads from
697    /// here. Issue #19 ST2: a pure flow-ir eval seed — no Task-level
698    /// field is folded into it anymore; see [`Self::task_input`].
699    pub init_ctx: Value,
700    /// Task-level canonical fields (issue #19 ST2). `Some` layers a
701    /// [`crate::middleware::task_input::TaskInputMiddleware`] (built via
702    /// [`crate::middleware::task_input::TaskInputMiddleware::new_from_fields`])
703    /// onto the spawner stack just before spawn; `None` is a no-op,
704    /// identical to today's behavior for callers with no Task-level
705    /// fields to propagate.
706    pub task_input: Option<TaskInputSpec>,
707    /// Issue #13 run_id propagation: when `Some`, every step this launch
708    /// dispatches is traced into `RunRecord.step_entries` and exposes its
709    /// `run_id` via `Ctx.meta.runtime["run_id"]` (see
710    /// `EngineDispatcher::with_run`). `None` (the default via
711    /// [`Self::automate`]) preserves the pre-existing behavior — no run
712    /// tracing.
713    pub run_ctx: Option<RunContext>,
714    /// The "launch request" tier (tier 1, highest
715    /// priority) of the `check_policy` cascade
716    /// (`launch request > blueprint > server config`).
717    /// [`TaskLaunchService::launch`]
718    /// collapses `check_policy.or(blueprint.check_policy)` exactly once and
719    /// threads the result into every spawned step's `TaskSpec.check_policy`.
720    /// `None` (the default via [`Self::automate`]) leaves this tier
721    /// unspecified so the Blueprint tier / server-wide default decide —
722    /// backward-compat with every pre-cascade caller.
723    ///
724    /// [`TaskLaunchService::launch`] also collapses this same cascade one
725    /// step further
726    /// (adding the server-wide `EngineCfg.check_policy` tier) into a
727    /// pre-dispatch guard: when the resulting effective policy is
728    /// [`CheckPolicy::Strict`] and neither [`Self::task_input`]'s
729    /// `project_root` nor `work_dir` is set, the launch is rejected with
730    /// `TaskLaunchError::PreDispatch` before any step is dispatched — a
731    /// strict task with no resolvable root would deterministically fail
732    /// at its first submit-time file materialize anyway. Setting this
733    /// field to `Some(CheckPolicy::Warn)` on the launch-request tier is
734    /// the escape hatch: it outranks a Blueprint- or server-declared
735    /// Strict and lets the guard pass.
736    pub check_policy: Option<CheckPolicy>,
737}
738
739impl TaskLaunchInput {
740    /// Helper for existing callers on the default path — no hooks and no
741    /// per-agent `OperatorKind` overrides. Leaves the "Runtime Global" tier
742    /// unspecified (`None`), so the BP-level tiers / final default
743    /// (`OperatorKind::Automate`) decide — this preserves today's
744    /// behaviour for every existing caller without silently forcing
745    /// `Automate` as an explicit override that would outrank a BP-declared
746    /// `MainAi`/`Composite` kind. `run_ctx` and `task_input` both default
747    /// to `None` (no run tracing, no Task-level fields); construct the
748    /// struct literal directly to set either.
749    pub fn automate(
750        blueprint: Blueprint,
751        operator_id: impl Into<String>,
752        role: Role,
753        ttl: Duration,
754        init_ctx: Value,
755    ) -> Self {
756        Self {
757            blueprint,
758            operator_id: operator_id.into(),
759            role,
760            ttl,
761            operator_kind: None,
762            bridge_id: None,
763            hook_id: None,
764            operator_backend_id: None,
765            operator_pin: None,
766            operator_kind_overrides: HashMap::new(),
767            init_ctx,
768            task_input: None,
769            run_ctx: None,
770            check_policy: None,
771        }
772    }
773}
774
775/// Result of a successful [`TaskLaunchService::launch`] call.
776#[derive(Debug, Clone)]
777pub struct TaskLaunchOutput {
778    /// The capability token for the attached session.
779    pub token: CapToken,
780    /// The final `ctx` after the flow ran — every `Step.out` has
781    /// been written. Application-layer callers pull the outcome out
782    /// of this `Value` and fold it into a domain status.
783    pub final_ctx: Value,
784}
785
786/// Domain service that compiles, links, and runs a Blueprint's flow to
787/// completion through the [`Engine`]. See the module doc for the full
788/// responsibility list.
789pub struct TaskLaunchService {
790    engine: Engine,
791    compiler: Compiler,
792    /// `call_extern` registry threaded into flow eval. Defaults to
793    /// [`NoExterns`] (= every `call_extern` in a Blueprint raises
794    /// `ExternError`); hosts opt in via [`Self::with_externs`] with an
795    /// `ExternMap` of pure value-shape functions.
796    externs: Arc<dyn Externs + Send + Sync>,
797    /// Optional execution-environment binding implementation. When present,
798    /// fresh Run snapshots require a complete, Core-validated receipt set.
799    binding_provider: Option<Arc<dyn AgentBindingProvider>>,
800    /// Whether fresh Blueprint declarations may use the deprecated
801    /// `profile.worker_binding` Runner fallback.
802    legacy_worker_binding_policy: LegacyWorkerBindingPolicy,
803}
804
805impl TaskLaunchService {
806    /// Build a service bound to one `Engine` and one `Compiler`.
807    pub fn new(engine: Engine, compiler: Compiler) -> Self {
808        Self {
809            engine,
810            compiler,
811            externs: Arc::new(NoExterns),
812            binding_provider: None,
813            legacy_worker_binding_policy: LegacyWorkerBindingPolicy::default(),
814        }
815    }
816
817    /// Replace the `call_extern` registry (builder style). Entries MUST be
818    /// pure functions — no side effects, no flow control; effectful work
819    /// belongs to `Step` / agents, not externs (flow-ir canonical contract).
820    pub fn with_externs(mut self, externs: Arc<dyn Externs + Send + Sync>) -> Self {
821        self.externs = externs;
822        self
823    }
824
825    /// Inject the execution-environment binding provider. Platform plugins
826    /// and Operator/MainAI implementations use this same interface; Core
827    /// retains receipt validation and digest ownership.
828    pub fn with_binding_provider(mut self, provider: Arc<dyn AgentBindingProvider>) -> Self {
829        self.binding_provider = Some(provider);
830        self
831    }
832
833    /// Configure the migration gate for deprecated `profile.worker_binding`.
834    pub fn with_legacy_worker_binding_policy(mut self, policy: LegacyWorkerBindingPolicy) -> Self {
835        self.legacy_worker_binding_policy = policy;
836        self
837    }
838
839    /// The bound `Engine`.
840    pub fn engine(&self) -> &Engine {
841        &self.engine
842    }
843
844    /// The bound `Compiler`.
845    pub fn compiler(&self) -> &Compiler {
846        &self.compiler
847    }
848
849    /// Run the Blueprint's flow to completion and return the final
850    /// `ctx`.
851    ///
852    /// Failure paths:
853    ///
854    /// - `compiler.compile` failure → `TaskLaunchError::Compile`.
855    /// - `engine.attach` failure → `TaskLaunchError::Engine`.
856    /// - A `Step` inside `flow eval` producing a dispatcher error, or
857    ///   a sub-flow raising, → `TaskLaunchError::FlowEval`. There is
858    ///   no silent partial-success completion; failures always
859    ///   propagate.
860    pub async fn launch(
861        &self,
862        mut input: TaskLaunchInput,
863    ) -> Result<TaskLaunchOutput, TaskLaunchError> {
864        // After the stateless-executor refactor, the
865        // caller (Service) does compile + link +
866        // `EngineDispatcher::with_spawner` itself; the engine no longer
867        // holds any global spawner state to touch. The link path (base
868        // `SpawnerAdapter` +
869        // `LayerRegistry` resolution + `SpawnerStack` wrapping) is
870        // concentrated inside `service::linker::link` — Service
871        // scatter is intentionally prevented.
872        // A pinned launch attests through the pinned session instead of the
873        // logical role's current holder, when the injected provider knows
874        // how to do that (`pinned_to_session` defaults to `None`, so an
875        // unpinned launch — and any provider without a session concept —
876        // keeps using `self.binding_provider` unchanged).
877        let pinned_binding_provider: Option<Arc<dyn AgentBindingProvider>> = input
878            .operator_pin
879            .as_deref()
880            .and_then(|pin| self.binding_provider.as_ref()?.pinned_to_session(pin));
881        let binding_provider = pinned_binding_provider
882            .as_deref()
883            .or(self.binding_provider.as_deref());
884        let (bound_agents, snapshot_origin) = load_or_resolve_bound_agents(
885            &input.blueprint,
886            input.run_ctx.as_ref(),
887            binding_provider,
888            self.legacy_worker_binding_policy,
889        )
890        .await?;
891        let binding_digests: HashMap<String, crate::blueprint::BindingDigest> = bound_agents
892            .iter()
893            .map(|bound| (bound.agent.name.clone(), bound.binding_digest.clone()))
894            .collect();
895        if let Some(run_ctx) = input.run_ctx.take() {
896            // [Crux D2-a] A `launch`-origin Run attaches the binding digests to
897            // the RunContext so replay keys distinguish the same step/input run
898            // under different bindings (the property the strict-binding series
899            // introduced). A `resume_backfill`-origin Run does NOT: its
900            // pre-upgrade replay log was hashed WITHOUT binding digests, so
901            // leaving `RunContext.binding_digests` empty makes the engine's
902            // `input_hash` fall back to the legacy form (`binding_digests.get`
903            // → None, see `Engine::dispatch_attempt_with_run_ctx`) and the old
904            // log hits verbatim. This is per-Run, keyed on the snapshot's
905            // origin — it does NOT disable digest keying for launch Runs.
906            input.run_ctx = Some(match snapshot_origin {
907                SnapshotOrigin::Launch => run_ctx.with_binding_digests(binding_digests.clone()),
908                SnapshotOrigin::ResumeBackfill => run_ctx,
909            });
910        }
911        input.blueprint = materialize_bound_blueprint(&input.blueprint, &bound_agents);
912        let compiled = self.compiler.compile_bound_pinned(
913            &input.blueprint,
914            &bound_agents,
915            input.operator_pin.as_deref(),
916        )?;
917        // GH #50 (Subtask 2 follow-up): merge this Blueprint's compiled
918        // `AgentDef.verdict` contracts into the engine's runtime registry —
919        // see `Engine::register_verdict_contracts`'s doc for the additive
920        // (last-write-wins per agent name) semantics. This is the ONLY
921        // production call site; every other consumer
922        // (`Engine::verdict_contract_for_task`, and through it
923        // `mlua-swarm-server`'s `worker_submit` / `worker_artifact`
924        // submit-time gate) reads from what this line populates.
925        self.engine
926            .register_verdict_contracts(compiled.router.verdict_contracts.clone());
927        let spawner = linker::link(
928            compiled.router.clone(),
929            &input.blueprint.spawner_hints.layers,
930            &self.engine,
931        );
932        // GH #20 Contract C: materialize an `AgentContextView` exactly
933        // once per spawn, innermost relative to every other layer below
934        // (alias / worker-binding / task-input all insert `ctx.meta.runtime`
935        // keys this layer must observe, so it is added FIRST — later
936        // `.layer()` calls become outer, see `middleware::SpawnerStack`).
937        // Unconditional (always layered): every Blueprint gets this layer
938        // even when it declares no agent-context supply tiers at all
939        // (`derive_agent_ctx` / `derive_context_policies` both return
940        // empty state then, matching the pre-#21 `AgentContextMiddleware`
941        // `Default` behavior byte-for-byte). GH #21 Phase 1: the
942        // receptacle named in the #20 comment above is now wired —
943        // `Blueprint.default_agent_ctx` / `default_context_policy` and
944        // `AgentMeta.ctx` / `context_policy` feed this layer's merge +
945        // policy resolution (see `middleware::agent_context`'s module doc
946        // for the full narrative).
947        let (agent_ctx_global, agent_ctx_per_agent) = derive_agent_ctx(&input.blueprint);
948        let (context_policy_default, context_policy_per_agent) =
949            derive_context_policies(&input.blueprint);
950        let spawner = SpawnerStack::new(spawner)
951            .layer(AgentContextMiddleware::new(
952                agent_ctx_global,
953                agent_ctx_per_agent,
954                context_policy_default,
955                context_policy_per_agent,
956            ))
957            .build();
958        // When `Blueprint.metadata.project_name_alias` is Some, layer a
959        // `ProjectNameAliasMiddleware` on top of the stack that injects the
960        // alias into `Ctx.meta.runtime.project_name_alias` just before spawn.
961        // Downstream operators (for example, the server crate's
962        // `Operator.execute`) read `ctx.meta.runtime.get("project_name_alias")`
963        // and expand it into the Spawn directive prompt body.
964        let spawner = if let Some(alias) = input.blueprint.metadata.project_name_alias.as_deref() {
965            SpawnerStack::new(spawner)
966                .layer(ProjectNameAliasMiddleware::new(alias))
967                .build()
968        } else {
969            spawner
970        };
971        // Layer the Blueprint-baked worker bindings (same ctx.meta.runtime
972        // inject shape as the alias layer above) so the delegate axis can
973        // resolve per-agent variants — see `derive_worker_bindings`.
974        let worker_bindings = worker_bindings_from_bound_agents(&bound_agents);
975        let spawner = if worker_bindings.is_empty() {
976            spawner
977        } else {
978            SpawnerStack::new(spawner)
979                .layer(WorkerBindingMiddleware::new(worker_bindings))
980                .build()
981        };
982        // GH #34: Blueprint-declared after-run audit hooks — same
983        // conditional-layering shape as the alias / worker-binding blocks
984        // above. Empty `Blueprint.audits` (every pre-#34 Blueprint) means
985        // no layer at all (invariant #4: byte-identical behavior). The
986        // router handle handed to `AfterRunAuditMiddleware` is
987        // `compiled.router` — the raw name→adapter table `Compiler::compile`
988        // built (NOT this progressively-wrapped `spawner`) — so an audit
989        // agent's own dispatch never re-enters this same layer (see
990        // `AfterRunAuditMiddleware`'s module doc, Recursion guard section).
991        let audit_defs = derive_audits(&input.blueprint);
992        let spawner = if audit_defs.is_empty() {
993            spawner
994        } else {
995            SpawnerStack::new(spawner)
996                .layer(AfterRunAuditMiddleware::new(
997                    audit_defs,
998                    compiled.router.clone(),
999                ))
1000                .build()
1001        };
1002
1003        // Task-level execution context (`project_root` / `work_dir` /
1004        // `task_metadata`) — same conditional-layering shape as the alias /
1005        // worker-binding blocks above. Issue #19 ST2: read directly off
1006        // `input.task_input` (already resolved by the caller) instead of
1007        // extracting it back out of `input.init_ctx` — `init_ctx` is a pure
1008        // flow-ir eval seed now, never folded with these keys.
1009        let spawner = match input.task_input.as_ref().and_then(|spec| {
1010            TaskInputMiddleware::new_from_fields(
1011                spec.project_root.clone(),
1012                spec.work_dir.clone(),
1013                spec.task_metadata.clone(),
1014            )
1015        }) {
1016            Some(task_input) => SpawnerStack::new(spawner).layer(task_input).build(),
1017            None => spawner,
1018        };
1019
1020        // "BP Agent-level" (`OperatorDef.kind` via `operator_ref`) + "BP
1021        // Global" (`Blueprint.default_operator_kind`) tiers of the
1022        // `OperatorKind` cascade, baked here (the only point that has both
1023        // the resolved Blueprint and the launch-time overrides in scope).
1024        let bp_agent_kinds = derive_bp_agent_kinds(&input.blueprint);
1025        let bp_global_kind = input
1026            .blueprint
1027            .default_operator_kind
1028            .map(OperatorKind::from);
1029
1030        let token = self
1031            .engine
1032            .attach_with_ids(
1033                input.operator_id,
1034                input.role,
1035                input.ttl,
1036                input.operator_kind,
1037                input.bridge_id,
1038                input.hook_id,
1039                input.operator_backend_id,
1040                input.operator_kind_overrides,
1041                bp_agent_kinds,
1042                bp_global_kind,
1043            )
1044            .await?;
1045        // Collapse the `check_policy` cascade EXACTLY ONCE
1046        // here: `launch request > blueprint > server config` (highest to
1047        // lowest priority). `input.check_policy` is the launch-request tier;
1048        // `input.blueprint.check_policy` is the Blueprint tier; a `None`
1049        // result leaves the engine's submit-time sink to fall back to the
1050        // server-wide `EngineCfg.check_policy` (tier 3) on its own — the
1051        // engine's existing `task_policy.unwrap_or(server_policy)` resolution
1052        // is deliberately NOT duplicated here (no double resolution). The
1053        // resolved value is threaded (via `with_check_policy`) into EVERY
1054        // spawned step's `TaskSpec`, not just the first.
1055        let resolved_check_policy = input.check_policy.or(input.blueprint.check_policy);
1056        // Pre-dispatch guard: collapse the same cascade one step further
1057        // (adding the server tier, `EngineCfg.check_policy`, via
1058        // `self.engine.cfg()`) into a SEPARATE local used only for this
1059        // check — `resolved_check_policy` above (the Option stamped onto
1060        // every dispatched step's `TaskSpec`) is left untouched, so the
1061        // "TaskSpec = None -> engine falls back to server default at the
1062        // submit-time sink" contract (cascade test case 4) keeps holding.
1063        // When the effective policy is Strict and the launch supplied
1064        // neither `project_root` nor `work_dir`, a strict task would
1065        // deterministically fail at its first submit-time file
1066        // materialize — fail the launch fast instead of dispatching a
1067        // step that can only ever hit that wall. `check_policy: "warn"` on
1068        // the launch-request tier is the escape hatch (it wins the
1069        // cascade before this fallback ever applies).
1070        let effective_check_policy =
1071            resolved_check_policy.unwrap_or(self.engine.cfg().check_policy);
1072        if effective_check_policy == CheckPolicy::Strict {
1073            let roots_missing = input
1074                .task_input
1075                .as_ref()
1076                .map(|t| t.project_root.is_none() && t.work_dir.is_none())
1077                .unwrap_or(true);
1078            if roots_missing {
1079                return Err(TaskLaunchError::PreDispatch(
1080                    "check_policy=strict requires project_root or work_dir, but the launch \
1081                     supplied neither"
1082                        .to_string(),
1083                ));
1084            }
1085        }
1086        let dispatcher =
1087            EngineDispatcher::with_spawner(self.engine.clone(), token.clone(), spawner);
1088        let dispatcher = dispatcher.with_check_policy(resolved_check_policy);
1089        // GH #76 error surface: clone the `RunContext` (cheap — every field is either
1090        // `Arc<...>` or a scalar) so the eval-boundary `map_err` closure
1091        // can still read `last_failure` / call `snapshot_partial_ctx()`
1092        // after the original is moved into the dispatcher. Sibling to the
1093        // existing `token.clone()` pattern above — the `map_err`
1094        // structured-error lift is now a co-owner of the RunContext state
1095        // the dispatcher writes to.
1096        let map_err_run_ctx = input.run_ctx.clone();
1097        let dispatcher = match input.run_ctx {
1098            Some(run_ctx) => dispatcher.with_run(run_ctx),
1099            None => dispatcher,
1100        };
1101        // GH #21 Phase 2: attach the Step tier's named `MetaDef` pool.
1102        // Unconditional — an empty map (every pre-#21-Phase-2 Blueprint)
1103        // is a no-op, matching `EngineDispatcher::with_spawner`'s default.
1104        let dispatcher = dispatcher.with_step_metas(derive_step_metas(&input.blueprint));
1105        let dispatcher = dispatcher.with_binding_digests(binding_digests);
1106        // GH #23: attach the `StepNaming` table `Compiler::compile` already
1107        // built once for this Blueprint (the sole construction site — see
1108        // `core::step_naming::StepNaming::from_blueprint`'s doc).
1109        // Unconditional — every compile produces one, undeclared Blueprints
1110        // included (canonical falls back to `Step.ref` byte-for-byte).
1111        let dispatcher = dispatcher.with_step_naming(compiled.step_naming.clone());
1112        // GH #27 (follow-up to #23): attach the `ProjectionPlacement`
1113        // resolver `Compiler::compile` already built once for this
1114        // Blueprint (the sole construction site — see
1115        // `core::projection_placement::ProjectionPlacement::from_spec`'s
1116        // doc). Unconditional — every compile produces one, undeclared
1117        // Blueprints included (resolves to `ProjectionPlacement::default()`).
1118        let dispatcher =
1119            dispatcher.with_projection_placement(compiled.projection_placement.clone());
1120        // Issue #19 ST3: BP default + Task init_ctx → merged init_ctx (the
1121        // 2-layer slice of the eventual 4-layer cascade; Run override is
1122        // ST4 carry). `input.blueprint.default_init_ctx` is `None` for
1123        // every pre-#19 Blueprint, so `merge_init_ctx` is a no-op then and
1124        // this preserves today's behavior byte-for-byte.
1125        let merged_init_ctx =
1126            merge_init_ctx(input.blueprint.default_init_ctx.as_ref(), &input.init_ctx);
1127        let eval_result = mlua_flow_ir::eval_async_externs(
1128            &input.blueprint.flow,
1129            merged_init_ctx,
1130            &dispatcher,
1131            &*self.externs,
1132        )
1133        .await;
1134        let final_ctx = match eval_result {
1135            Ok(v) => v,
1136            Err(e) => {
1137                // GH #76 error surface: lift the eval error into the structured
1138                // `TaskLaunchError::FlowEval { .. }` variant. `message`
1139                // preserves the pre-#76 stringified error byte-for-byte
1140                // (the `Display` impl still emits `"flow eval: {message}"`,
1141                // so callers that only match on the stringified error keep
1142                // working). `failed_step` + `verdict_value` are lifted off
1143                // the `RunContext.last_failure` breadcrumb the dispatcher's
1144                // Blocked arm wrote (see `EngineDispatcher::dispatch` for
1145                // the write side); `partial_ctx` is reconstructed from the
1146                // step-entry trace persisted so far via
1147                // `RunContext::snapshot_partial_ctx` — see that method's
1148                // doc for the "metadata-level, not value-level" caveat
1149                // and the upstream mlua-flow-ir carry.
1150                //
1151                // Every new field is `None` when `run_ctx` was not
1152                // supplied by the caller (a legacy `TaskLaunchService::launch`
1153                // call site with no run tracing), or when the abort path
1154                // did not go through the dispatcher's Blocked arm (e.g.
1155                // flow-ir raised `EvalError` before dispatch).
1156                let (failed_step, verdict_value) = match &map_err_run_ctx {
1157                    Some(rc) => {
1158                        let slot = rc.last_failure.lock().ok().and_then(|g| g.clone());
1159                        match slot {
1160                            Some(lf) => (
1161                                lf.step_ref.clone().or_else(|| Some(lf.step_id.to_string())),
1162                                Some(lf.verdict_value.clone()),
1163                            ),
1164                            None => (None, None),
1165                        }
1166                    }
1167                    None => (None, None),
1168                };
1169                let partial_ctx = match &map_err_run_ctx {
1170                    Some(rc) => Some(rc.snapshot_partial_ctx().await),
1171                    None => None,
1172                };
1173                return Err(TaskLaunchError::FlowEval {
1174                    message: e.to_string(),
1175                    failed_step,
1176                    verdict_value,
1177                    partial_ctx,
1178                });
1179            }
1180        };
1181        Ok(TaskLaunchOutput { token, final_ctx })
1182    }
1183}
1184
1185// ──────────────────────────────────────────────────────────────────────────
1186// UT
1187// ──────────────────────────────────────────────────────────────────────────
1188
1189#[cfg(test)]
1190mod tests {
1191    use super::*;
1192    use crate::blueprint::compiler::{RustFnInProcessSpawnerFactory, SpawnerRegistry};
1193    use crate::blueprint::{
1194        current_schema_version, resolve_runner, AgentDef, AgentKind, AgentMeta, AgentProfile,
1195        BlueprintMetadata, CompilerHints, CompilerStrategy, MetaDef, Runner,
1196    };
1197    use crate::core::config::EngineCfg;
1198    use crate::worker::adapter::{WorkerError, WorkerResult};
1199    use mlua_flow_ir::{Expr, JoinMode, Node as FlowNode};
1200    use serde_json::json;
1201    use std::sync::Arc;
1202
1203    fn path(s: &str) -> Expr {
1204        Expr::Path {
1205            at: s.parse().expect("literal test path"),
1206        }
1207    }
1208    fn step(ref_: &str, in_: Expr, out: Expr) -> FlowNode {
1209        FlowNode::Step {
1210            ref_: ref_.to_string(),
1211            in_,
1212            out,
1213        }
1214    }
1215
1216    fn agent(name: &str, fn_id: &str) -> AgentDef {
1217        AgentDef {
1218            name: name.to_string(),
1219            kind: AgentKind::RustFn,
1220            spec: json!({ "fn_id": fn_id }),
1221            profile: None,
1222            meta: Some(AgentMeta::default()),
1223            runner: None,
1224            runner_ref: None,
1225            verdict: None,
1226        }
1227    }
1228
1229    fn build_service(factory: RustFnInProcessSpawnerFactory) -> TaskLaunchService {
1230        let engine = Engine::new(EngineCfg::default());
1231        let mut reg = SpawnerRegistry::new();
1232        reg.register::<RustFnInProcessSpawnerFactory>(Arc::new(factory));
1233        let compiler = Compiler::new(reg);
1234        TaskLaunchService::new(engine, compiler)
1235    }
1236
1237    /// Same as [`build_service`] but with a caller-supplied [`EngineCfg`] —
1238    /// used by the pre-dispatch guard's server-tier test (T4), which needs
1239    /// a non-default `EngineCfg.check_policy`.
1240    fn build_service_with_cfg(
1241        factory: RustFnInProcessSpawnerFactory,
1242        cfg: EngineCfg,
1243    ) -> TaskLaunchService {
1244        let engine = Engine::new(cfg);
1245        let mut reg = SpawnerRegistry::new();
1246        reg.register::<RustFnInProcessSpawnerFactory>(Arc::new(factory));
1247        let compiler = Compiler::new(reg);
1248        TaskLaunchService::new(engine, compiler)
1249    }
1250
1251    fn bp(flow: FlowNode, agents: Vec<AgentDef>) -> Blueprint {
1252        Blueprint {
1253            schema_version: current_schema_version(),
1254            id: "ut".into(),
1255            flow,
1256            agents,
1257            operators: vec![],
1258            metas: vec![],
1259            hints: CompilerHints::default(),
1260            strategy: CompilerStrategy::default(),
1261            metadata: BlueprintMetadata::default(),
1262            spawner_hints: Default::default(),
1263            default_agent_kind: AgentKind::Operator,
1264            default_operator_kind: None,
1265            default_init_ctx: None,
1266            default_agent_ctx: None,
1267            default_context_policy: None,
1268            projection_placement: None,
1269            audits: vec![],
1270            degradation_policy: None,
1271            runners: vec![],
1272            default_runner: None,
1273            subprocesses: vec![],
1274            check_policy: None,
1275            blueprint_ref_includes: Vec::new(),
1276        }
1277    }
1278
1279    fn launch_input(blueprint: Blueprint, init_ctx: Value) -> TaskLaunchInput {
1280        TaskLaunchInput::automate(
1281            blueprint,
1282            "ut-op",
1283            Role::Operator,
1284            Duration::from_secs(30),
1285            init_ctx,
1286        )
1287    }
1288
1289    // ──────────────────────────────────────────────────────────────
1290    // GH #34: `derive_audits` + the conditional `AfterRunAuditMiddleware`
1291    // `.layer(...)` wiring in `TaskLaunchService::launch`
1292    // ──────────────────────────────────────────────────────────────
1293
1294    #[test]
1295    fn derive_audits_empty_by_default() {
1296        let blueprint = bp(
1297            step("echo", path("$.input"), path("$.out")),
1298            vec![agent("echo", "echo")],
1299        );
1300        assert!(
1301            derive_audits(&blueprint).is_empty(),
1302            "audits_absent_no_layer: an undeclared audits Vec must stay empty"
1303        );
1304    }
1305
1306    #[test]
1307    fn derive_audits_returns_blueprint_audits_verbatim() {
1308        let mut blueprint = bp(
1309            step("echo", path("$.input"), path("$.out")),
1310            vec![agent("echo", "echo")],
1311        );
1312        blueprint.audits = vec![crate::blueprint::AuditDef {
1313            agent: "auditor".to_string(),
1314            steps: None,
1315            mode: crate::blueprint::AuditMode::Async,
1316        }];
1317        let got = derive_audits(&blueprint);
1318        assert_eq!(got.len(), 1);
1319        assert_eq!(got[0].agent, "auditor");
1320    }
1321
1322    #[tokio::test]
1323    async fn launch_appends_audit_artifact_when_audits_declared() {
1324        use crate::blueprint::{AuditDef, AuditMode};
1325
1326        let factory = RustFnInProcessSpawnerFactory::new()
1327            .register_fn("echo", |inv| async move {
1328                Ok(WorkerResult {
1329                    value: json!({ "echoed": inv.prompt }),
1330                    ok: true,
1331                    stats: None,
1332                })
1333            })
1334            .register_fn("audit-fn", |_inv| async move {
1335                Ok(WorkerResult {
1336                    value: json!({ "finding": "clean" }),
1337                    ok: true,
1338                    stats: None,
1339                })
1340            });
1341        let svc = build_service(factory);
1342        let mut blueprint = bp(
1343            step("echo", path("$.input"), path("$.out")),
1344            vec![agent("echo", "echo"), agent("auditor", "audit-fn")],
1345        );
1346        blueprint.audits = vec![AuditDef {
1347            agent: "auditor".to_string(),
1348            steps: None,
1349            mode: AuditMode::Sync,
1350        }];
1351        let out = svc
1352            .launch(launch_input(blueprint, json!({ "input": "hi" })))
1353            .await
1354            .expect("launch ok — audits must never alter the audited step's outcome");
1355        assert_eq!(out.final_ctx["out"]["echoed"], "hi");
1356
1357        let audited_task_id = svc
1358            .engine()
1359            .with_state("test.find_audited_task", |s| {
1360                s.tasks
1361                    .iter()
1362                    .find(|(_, t)| t.spec.agent == "echo")
1363                    .map(|(id, _)| id.clone())
1364            })
1365            .await
1366            .expect("with_state")
1367            .expect("the echo task must exist");
1368        let tail = svc.engine().output_tail(&audited_task_id, 1).await;
1369        let found = tail.iter().any(|ev| {
1370            matches!(
1371                ev,
1372                crate::worker::output::OutputEvent::Artifact { name, .. } if name == "audit:echo"
1373            )
1374        });
1375        assert!(
1376            found,
1377            "launch() must wire AfterRunAuditMiddleware end-to-end when Blueprint.audits is declared"
1378        );
1379    }
1380
1381    #[tokio::test]
1382    async fn launch_single_step_writes_out_path() {
1383        let factory = RustFnInProcessSpawnerFactory::new().register_fn("echo", |inv| async move {
1384            Ok(WorkerResult {
1385                value: json!({ "echoed": inv.prompt }),
1386                ok: true,
1387                stats: None,
1388            })
1389        });
1390        let svc = build_service(factory);
1391        let blueprint = bp(
1392            step("echo", path("$.input"), path("$.out")),
1393            vec![agent("echo", "echo")],
1394        );
1395        let out = svc
1396            .launch(launch_input(blueprint, json!({ "input": "hi" })))
1397            .await
1398            .expect("launch ok");
1399        assert_eq!(out.final_ctx["out"]["echoed"], "hi");
1400    }
1401
1402    // ──────────────────────────────────────────────────────────────
1403    // check_policy cascade (launch > blueprint > server)
1404    // T2 (cascade 4-case) / T3 (end-to-end strict) / T4 (backward compat)
1405    // ──────────────────────────────────────────────────────────────
1406
1407    /// Launch a single-echo Blueprint with the given launch- and
1408    /// Blueprint-tier `check_policy`, then read back the `check_policy` that
1409    /// the dispatcher stamped onto the dispatched step's `TaskSpec`. The
1410    /// launch may complete (Silent / Warn / None → fail-open) — the in-process
1411    /// RustFn worker fire-and-forgets its submit — so the task and its
1412    /// resolved spec exist regardless of the launch outcome.
1413    ///
1414    /// `task_input` carries a `work_dir` unconditionally (a dummy path, not
1415    /// resolved on disk) so the pre-dispatch guard (a strict effective
1416    /// policy with no roots supplied rejects before dispatch) never fires
1417    /// here — this helper's whole point is "reach dispatch and read back
1418    /// the stamp", so every case (including the two whose
1419    /// `bp_policy`/`launch_policy` alone resolve to Strict) must dispatch
1420    /// uniformly. The guard's own rejection behavior is proven separately
1421    /// (T3/T4 and `strict_blueprint_without_roots_is_rejected_pre_dispatch`).
1422    async fn dispatched_check_policy(
1423        launch_policy: Option<CheckPolicy>,
1424        bp_policy: Option<CheckPolicy>,
1425    ) -> Option<CheckPolicy> {
1426        let factory = RustFnInProcessSpawnerFactory::new().register_fn("echo", |inv| async move {
1427            Ok(WorkerResult {
1428                value: json!({ "echoed": inv.prompt }),
1429                ok: true,
1430                stats: None,
1431            })
1432        });
1433        let svc = build_service(factory);
1434        let mut blueprint = bp(
1435            step("echo", path("$.input"), path("$.out")),
1436            vec![agent("echo", "echo")],
1437        );
1438        blueprint.check_policy = bp_policy;
1439        let mut input = launch_input(blueprint, json!({ "input": "hi" }));
1440        input.check_policy = launch_policy;
1441        input.task_input = Some(TaskInputSpec {
1442            project_root: None,
1443            work_dir: Some("/dispatched-check-policy-test-root".to_string()),
1444            task_metadata: None,
1445        });
1446        let _ = svc.launch(input).await;
1447        svc.engine()
1448            .with_state("test.read_dispatched_check_policy", |s| {
1449                s.tasks
1450                    .values()
1451                    .find(|t| t.spec.agent == "echo")
1452                    .and_then(|t| t.spec.check_policy)
1453            })
1454            .await
1455            .expect("with_state")
1456    }
1457
1458    /// T2 case 1: launch `Some(Silent)` + BP `Some(Strict)` → TaskSpec
1459    /// `Some(Silent)` (the launch-request tier outranks the Blueprint tier).
1460    #[tokio::test]
1461    async fn cascade_launch_tier_wins_over_blueprint_tier() {
1462        assert_eq!(
1463            dispatched_check_policy(Some(CheckPolicy::Silent), Some(CheckPolicy::Strict)).await,
1464            Some(CheckPolicy::Silent),
1465        );
1466    }
1467
1468    /// T2 case 2: launch `None` + BP `Some(Strict)` → TaskSpec `Some(Strict)`
1469    /// (the Blueprint tier takes effect when the launch tier is unset).
1470    #[tokio::test]
1471    async fn cascade_blueprint_tier_used_when_launch_absent() {
1472        assert_eq!(
1473            dispatched_check_policy(None, Some(CheckPolicy::Strict)).await,
1474            Some(CheckPolicy::Strict),
1475        );
1476    }
1477
1478    /// T2 case 3: launch `Some(Strict)` + BP `None` → TaskSpec `Some(Strict)`
1479    /// (the launch tier alone resolves when the Blueprint tier is unset).
1480    #[tokio::test]
1481    async fn cascade_launch_tier_alone_when_blueprint_absent() {
1482        assert_eq!(
1483            dispatched_check_policy(Some(CheckPolicy::Strict), None).await,
1484            Some(CheckPolicy::Strict),
1485        );
1486    }
1487
1488    /// T2 case 4: launch `None` + BP `None` → TaskSpec `None`. NOT omitted as
1489    /// "trivial": this is the backward-compat proof — the server-fallback
1490    /// path (`EngineCfg.check_policy` decides at the submit-time sink) is
1491    /// preserved byte-for-byte because the carrier stays `None`.
1492    #[tokio::test]
1493    async fn cascade_both_none_preserves_server_fallback() {
1494        assert_eq!(dispatched_check_policy(None, None).await, None);
1495    }
1496
1497    /// Repurposed 2026-07-16 for the pre-dispatch guard's new contract
1498    /// (the launch-time validation stage of the check_policy cascade
1499    /// work). This test used
1500    /// to prove a strict + no-roots launch dispatched a step that then hit
1501    /// `EngineError::CheckPolicyStrict` at submit time — exactly the path
1502    /// the pre-dispatch guard now forecloses (a strict launch with no
1503    /// resolvable root is rejected BEFORE dispatch instead, see
1504    /// [`TaskLaunchService::launch`]'s guard). The two sub-assertions this
1505    /// test used to make are independently covered elsewhere: the
1506    /// cascade-resolved Strict reaching the dispatched `TaskSpec` is
1507    /// covered by the `cascade_*` tests above; the submit-time sink
1508    /// surfacing `CheckPolicyStrict` on an unresolved root is covered by
1509    /// `crate::core::engine::tests::submit_output_final_check_policy_strict_surfaces_error_when_root_unresolved`
1510    /// (seeds the task directly at the engine layer, bypassing `launch`).
1511    /// This test now asserts the NEW contract directly.
1512    #[tokio::test]
1513    async fn strict_blueprint_without_roots_is_rejected_pre_dispatch() {
1514        let factory = RustFnInProcessSpawnerFactory::new().register_fn("echo", |inv| async move {
1515            Ok(WorkerResult {
1516                value: json!({ "echoed": inv.prompt }),
1517                ok: true,
1518                stats: None,
1519            })
1520        });
1521        let svc = build_service(factory);
1522        let mut blueprint = bp(
1523            step("echo", path("$.input"), path("$.out")),
1524            vec![agent("echo", "echo")],
1525        );
1526        blueprint.check_policy = Some(CheckPolicy::Strict);
1527        // No task_input → no work_dir/project_root ever resolves.
1528        let err = svc
1529            .launch(launch_input(blueprint, json!({ "input": "hi" })))
1530            .await
1531            .expect_err("strict check_policy + no roots must be rejected before dispatch");
1532        match err {
1533            TaskLaunchError::PreDispatch(message) => {
1534                assert!(
1535                    message.contains("strict"),
1536                    "message must identify the strict-requires-roots condition: {message}"
1537                );
1538            }
1539            other => panic!("expected TaskLaunchError::PreDispatch, got {other:?}"),
1540        }
1541
1542        // No step was ever dispatched — the guard fires after
1543        // `engine.attach_with_ids` (the token mint) but before the
1544        // dispatcher is ever built / `eval_async_externs` runs.
1545        let dispatched = svc
1546            .engine()
1547            .with_state("test.no_echo_task_dispatched", |s| {
1548                s.tasks.values().any(|t| t.spec.agent == "echo")
1549            })
1550            .await
1551            .expect("with_state");
1552        assert!(
1553            !dispatched,
1554            "the pre-dispatch guard must reject before any step is dispatched"
1555        );
1556    }
1557
1558    /// T4 (cascade backward-compat): backward compat — with NO check_policy
1559    /// anywhere (BP tier + launch tier both `None`), the launch resolves to
1560    /// the server default (Warn) and completes fail-open exactly as before
1561    /// this change (the warn-mode materialize skip never turns a
1562    /// successful submit into a failure).
1563    ///
1564    /// This is ALSO the pre-dispatch guard's backward-compat case (T5):
1565    /// `task_input` is `None` via [`launch_input`]/[`TaskLaunchInput::automate`],
1566    /// so the guard's effective policy resolves to `Warn` (server default,
1567    /// [`EngineCfg::default`]) and never fires — the guard changes nothing
1568    /// about this pre-existing default-path behavior.
1569    #[tokio::test]
1570    async fn launch_without_any_check_policy_completes_fail_open() {
1571        let factory = RustFnInProcessSpawnerFactory::new().register_fn("echo", |inv| async move {
1572            Ok(WorkerResult {
1573                value: json!({ "echoed": inv.prompt }),
1574                ok: true,
1575                stats: None,
1576            })
1577        });
1578        let svc = build_service(factory);
1579        let blueprint = bp(
1580            step("echo", path("$.input"), path("$.out")),
1581            vec![agent("echo", "echo")],
1582        );
1583        assert_eq!(blueprint.check_policy, None, "BP tier must be unset");
1584        let out = svc
1585            .launch(launch_input(blueprint, json!({ "input": "hi" })))
1586            .await
1587            .expect("warn-mode fail-open must let the launch complete");
1588        assert_eq!(out.final_ctx["out"]["echoed"], "hi");
1589    }
1590
1591    // ──────────────────────────────────────────────────────────────────
1592    // pre-dispatch validation guard:
1593    // `TaskLaunchService::launch` rejects BEFORE dispatch when the
1594    // effective check_policy is Strict and neither `project_root` nor
1595    // `work_dir` is supplied. T3/T4/T6 live here (T1/T2 are
1596    // handler-level, in `mlua-swarm-server`'s `projection.rs`; T5 is the
1597    // `launch_without_any_check_policy_completes_fail_open` test above;
1598    // the guard-rejection end-to-end case is
1599    // `strict_blueprint_without_roots_is_rejected_pre_dispatch` above,
1600    // Option A's repurpose of the former stage-1 T3).
1601    // ──────────────────────────────────────────────────────────────────
1602
1603    /// T3 (Crux 3, escape hatch): a Blueprint declaring `check_policy:
1604    /// strict` is overridden by the launch-request tier's `check_policy:
1605    /// Some(Warn)` — tier 1 wins the cascade before the guard's
1606    /// effective-policy fallback ever applies, so the guard passes and the
1607    /// launch dispatches normally even though `task_input` is `None` (no
1608    /// project_root/work_dir at all). Regression guard against a future
1609    /// "the guard judges by the BP tier alone, not the effective/cascaded
1610    /// value" narrowing.
1611    #[tokio::test]
1612    async fn strict_blueprint_with_launch_warn_override_bypasses_pre_dispatch_guard() {
1613        let factory = RustFnInProcessSpawnerFactory::new().register_fn("echo", |inv| async move {
1614            Ok(WorkerResult {
1615                value: json!({ "echoed": inv.prompt }),
1616                ok: true,
1617                stats: None,
1618            })
1619        });
1620        let svc = build_service(factory);
1621        let mut blueprint = bp(
1622            step("echo", path("$.input"), path("$.out")),
1623            vec![agent("echo", "echo")],
1624        );
1625        blueprint.check_policy = Some(CheckPolicy::Strict);
1626        let mut input = launch_input(blueprint, json!({ "input": "hi" }));
1627        input.check_policy = Some(CheckPolicy::Warn);
1628        assert!(input.task_input.is_none(), "no roots supplied at all");
1629        let out = svc
1630            .launch(input)
1631            .await
1632            .expect("launch-tier warn override must bypass the pre-dispatch guard");
1633        assert_eq!(out.final_ctx["out"]["echoed"], "hi");
1634    }
1635
1636    /// T4 (Crux 2, server tier): with BOTH the launch- and Blueprint-tier
1637    /// `check_policy` unset, the server-wide `EngineCfg.check_policy` (the
1638    /// third cascade tier, read via `self.engine.cfg()`) alone must drive
1639    /// the guard — proof the guard does not stop at the "BP/launch 2-tier"
1640    /// shortcut Crux 2 forbids.
1641    #[tokio::test]
1642    async fn server_tier_strict_alone_triggers_pre_dispatch_guard() {
1643        let factory = RustFnInProcessSpawnerFactory::new().register_fn("echo", |inv| async move {
1644            Ok(WorkerResult {
1645                value: json!({ "echoed": inv.prompt }),
1646                ok: true,
1647                stats: None,
1648            })
1649        });
1650        let svc = build_service_with_cfg(
1651            factory,
1652            EngineCfg {
1653                check_policy: CheckPolicy::Strict,
1654                ..EngineCfg::default()
1655            },
1656        );
1657        let blueprint = bp(
1658            step("echo", path("$.input"), path("$.out")),
1659            vec![agent("echo", "echo")],
1660        );
1661        assert_eq!(blueprint.check_policy, None, "BP tier must be unset");
1662        let input = launch_input(blueprint, json!({ "input": "hi" }));
1663        assert!(input.check_policy.is_none(), "launch tier must be unset");
1664        assert!(input.task_input.is_none(), "no roots supplied");
1665        let err = svc.launch(input).await.expect_err(
1666            "server-tier Strict alone (BP/launch tiers both unset) must trigger the guard",
1667        );
1668        match err {
1669            TaskLaunchError::PreDispatch(message) => {
1670                assert!(
1671                    message.contains("strict"),
1672                    "expected the strict-requires-roots message, got: {message}"
1673                );
1674            }
1675            other => panic!("expected TaskLaunchError::PreDispatch, got {other:?}"),
1676        }
1677    }
1678
1679    /// T6 (guard condition, branch 2 of 3): `task_input: Some(_)` with
1680    /// BOTH `project_root` and `work_dir` absent is still `roots_missing`
1681    /// — the outer `Some` alone must not short-circuit the check (branch 1,
1682    /// `task_input: None`, is covered by
1683    /// `strict_blueprint_without_roots_is_rejected_pre_dispatch` above).
1684    #[tokio::test]
1685    async fn pre_dispatch_guard_rejects_when_task_input_present_but_roots_both_none() {
1686        let factory = RustFnInProcessSpawnerFactory::new().register_fn("echo", |inv| async move {
1687            Ok(WorkerResult {
1688                value: json!({ "echoed": inv.prompt }),
1689                ok: true,
1690                stats: None,
1691            })
1692        });
1693        let svc = build_service(factory);
1694        let mut blueprint = bp(
1695            step("echo", path("$.input"), path("$.out")),
1696            vec![agent("echo", "echo")],
1697        );
1698        blueprint.check_policy = Some(CheckPolicy::Strict);
1699        let mut input = launch_input(blueprint, json!({ "input": "hi" }));
1700        input.task_input = Some(TaskInputSpec {
1701            project_root: None,
1702            work_dir: None,
1703            task_metadata: Some(json!({ "unrelated": true })),
1704        });
1705        let err = svc
1706            .launch(input)
1707            .await
1708            .expect_err("Some(TaskInputSpec) with both roots None must still be roots_missing");
1709        assert!(
1710            matches!(err, TaskLaunchError::PreDispatch(_)),
1711            "expected TaskLaunchError::PreDispatch, got {err:?}"
1712        );
1713    }
1714
1715    /// T6 (guard condition, branch 3 of 3): `work_dir: Some(_)` alone
1716    /// (with `project_root: None`) is NOT `roots_missing` — either root
1717    /// being present is sufficient, so the guard passes and the launch
1718    /// dispatches normally.
1719    #[tokio::test]
1720    async fn pre_dispatch_guard_passes_when_work_dir_present_and_project_root_absent() {
1721        let factory = RustFnInProcessSpawnerFactory::new().register_fn("echo", |inv| async move {
1722            Ok(WorkerResult {
1723                value: json!({ "echoed": inv.prompt }),
1724                ok: true,
1725                stats: None,
1726            })
1727        });
1728        let svc = build_service(factory);
1729        let mut blueprint = bp(
1730            step("echo", path("$.input"), path("$.out")),
1731            vec![agent("echo", "echo")],
1732        );
1733        blueprint.check_policy = Some(CheckPolicy::Strict);
1734        let mut input = launch_input(blueprint, json!({ "input": "hi" }));
1735        input.task_input = Some(TaskInputSpec {
1736            project_root: None,
1737            work_dir: Some("/repo/work".to_string()),
1738            task_metadata: None,
1739        });
1740        let out = svc
1741            .launch(input)
1742            .await
1743            .expect("work_dir alone must satisfy the guard's roots_missing check");
1744        assert_eq!(out.final_ctx["out"]["echoed"], "hi");
1745    }
1746
1747    #[tokio::test]
1748    async fn launch_three_step_seq_threads_ctx_forward() {
1749        let factory = RustFnInProcessSpawnerFactory::new()
1750            .register_fn("upper", |inv| async move {
1751                let s = serde_json::from_str::<String>(&inv.prompt).unwrap_or(inv.prompt);
1752                Ok(WorkerResult {
1753                    value: json!(s.to_uppercase()),
1754                    ok: true,
1755                    stats: None,
1756                })
1757            })
1758            .register_fn("suffix", |inv| async move {
1759                let s = serde_json::from_str::<String>(&inv.prompt).unwrap_or(inv.prompt);
1760                Ok(WorkerResult {
1761                    value: json!(format!("{s}!")),
1762                    ok: true,
1763                    stats: None,
1764                })
1765            })
1766            .register_fn("wrap", |inv| async move {
1767                let s = serde_json::from_str::<String>(&inv.prompt).unwrap_or(inv.prompt);
1768                Ok(WorkerResult {
1769                    value: json!(format!("[{s}]")),
1770                    ok: true,
1771                    stats: None,
1772                })
1773            });
1774        let svc = build_service(factory);
1775        let flow = FlowNode::Seq {
1776            children: vec![
1777                step("upper", path("$.in"), path("$.s1")),
1778                step("suffix", path("$.s1"), path("$.s2")),
1779                step("wrap", path("$.s2"), path("$.s3")),
1780            ],
1781        };
1782        let blueprint = bp(
1783            flow,
1784            vec![
1785                agent("upper", "upper"),
1786                agent("suffix", "suffix"),
1787                agent("wrap", "wrap"),
1788            ],
1789        );
1790        let out = svc
1791            .launch(launch_input(blueprint, json!({ "in": "hello" })))
1792            .await
1793            .expect("launch ok");
1794        assert_eq!(out.final_ctx["s1"], "HELLO");
1795        assert_eq!(out.final_ctx["s2"], "HELLO!");
1796        assert_eq!(out.final_ctx["s3"], "[HELLO!]");
1797    }
1798
1799    #[tokio::test]
1800    async fn launch_fanout_join_all_parallel_completes() {
1801        use std::sync::atomic::{AtomicU32, Ordering};
1802        let counter = Arc::new(AtomicU32::new(0));
1803        let max_seen = Arc::new(AtomicU32::new(0));
1804        let counter_clone = counter.clone();
1805        let max_clone = max_seen.clone();
1806
1807        // Each worker bumps the inflight counter up, sleeps 50ms, then bumps it down.
1808        // When parallel execution is working, max inflight exceeds 1.
1809        let factory = RustFnInProcessSpawnerFactory::new().register_fn("para", move |inv| {
1810            let counter = counter_clone.clone();
1811            let max_seen = max_clone.clone();
1812            async move {
1813                let now = counter.fetch_add(1, Ordering::SeqCst) + 1;
1814                let mut prev = max_seen.load(Ordering::SeqCst);
1815                while now > prev {
1816                    match max_seen.compare_exchange(prev, now, Ordering::SeqCst, Ordering::SeqCst) {
1817                        Ok(_) => break,
1818                        Err(p) => prev = p,
1819                    }
1820                }
1821                tokio::time::sleep(Duration::from_millis(50)).await;
1822                counter.fetch_sub(1, Ordering::SeqCst);
1823                let s = serde_json::from_str::<String>(&inv.prompt).unwrap_or(inv.prompt);
1824                Ok(WorkerResult {
1825                    value: json!(format!("did:{s}")),
1826                    ok: true,
1827                    stats: None,
1828                })
1829            }
1830        });
1831        let svc = build_service(factory);
1832        let flow = FlowNode::Fanout {
1833            items: path("$.items"),
1834            bind: path("$.item"),
1835            body: Box::new(step("para", path("$.item"), path("$.r"))),
1836            join: JoinMode::All,
1837            out: path("$.results"),
1838        };
1839        let blueprint = bp(flow, vec![agent("para", "para")]);
1840        let out = svc
1841            .launch(launch_input(
1842                blueprint,
1843                json!({ "items": ["a", "b", "c", "d"] }),
1844            ))
1845            .await
1846            .expect("launch ok");
1847        let results = out.final_ctx["results"].as_array().expect("array");
1848        assert_eq!(results.len(), 4);
1849        for (i, expected) in ["a", "b", "c", "d"].iter().enumerate() {
1850            assert_eq!(results[i]["r"], json!(format!("did:{expected}")));
1851        }
1852        let max = max_seen.load(Ordering::SeqCst);
1853        assert!(
1854            max >= 2,
1855            "expected parallel execution (max inflight >= 2), got {max}"
1856        );
1857    }
1858
1859    #[tokio::test]
1860    async fn launch_propagates_worker_error_as_flow_eval_err() {
1861        let factory = RustFnInProcessSpawnerFactory::new()
1862            .register_fn("ok", |inv| async move {
1863                Ok(WorkerResult {
1864                    value: json!(inv.prompt),
1865                    ok: true,
1866                    stats: None,
1867                })
1868            })
1869            .register_fn("boom", |_inv| async move {
1870                Err(WorkerError::Failed("intentional boom".into()))
1871            });
1872        let svc = build_service(factory);
1873        let flow = FlowNode::Seq {
1874            children: vec![
1875                step("ok", path("$.input"), path("$.s1")),
1876                step("boom", path("$.s1"), path("$.s2")),
1877                step("ok", path("$.s2"), path("$.s3")),
1878            ],
1879        };
1880        let blueprint = bp(flow, vec![agent("ok", "ok"), agent("boom", "boom")]);
1881        let err = svc
1882            .launch(launch_input(blueprint, json!({ "input": "x" })))
1883            .await
1884            .expect_err("expected fail");
1885        match err {
1886            TaskLaunchError::FlowEval { message: msg, .. } => {
1887                assert!(
1888                    msg.contains("boom") || msg.contains("intentional"),
1889                    "expected error to mention worker failure, got: {msg}"
1890                );
1891            }
1892            other => panic!("expected FlowEval error, got {other:?}"),
1893        }
1894    }
1895
1896    #[tokio::test]
1897    async fn launch_resolves_call_extern_via_registered_externs() {
1898        let factory = RustFnInProcessSpawnerFactory::new().register_fn("echo", |inv| async move {
1899            Ok(WorkerResult {
1900                value: json!({ "echoed": inv.prompt }),
1901                ok: true,
1902                stats: None,
1903            })
1904        });
1905        let mut externs = mlua_flow_ir::ExternMap::new();
1906        externs.register("fmt.greet", |args: &[Value]| {
1907            let name = args[0].as_str().unwrap_or("?");
1908            Ok(json!(format!("hello, {name}")))
1909        });
1910        let svc = build_service(factory).with_externs(Arc::new(externs));
1911        let flow = step(
1912            "echo",
1913            Expr::CallExtern {
1914                ref_: "fmt.greet".into(),
1915                args: vec![path("$.who")],
1916            },
1917            path("$.out"),
1918        );
1919        let blueprint = bp(flow, vec![agent("echo", "echo")]);
1920        let out = svc
1921            .launch(launch_input(blueprint, json!({ "who": "swarm" })))
1922            .await
1923            .expect("launch ok");
1924        assert_eq!(out.final_ctx["out"]["echoed"], json!("hello, swarm"));
1925    }
1926
1927    #[tokio::test]
1928    async fn launch_call_extern_without_registry_fails_as_flow_eval() {
1929        let factory = RustFnInProcessSpawnerFactory::new().register_fn("echo", |inv| async move {
1930            Ok(WorkerResult {
1931                value: json!(inv.prompt),
1932                ok: true,
1933                stats: None,
1934            })
1935        });
1936        let svc = build_service(factory); // default NoExterns
1937        let flow = step(
1938            "echo",
1939            Expr::CallExtern {
1940                ref_: "fmt.greet".into(),
1941                args: vec![],
1942            },
1943            path("$.out"),
1944        );
1945        let blueprint = bp(flow, vec![agent("echo", "echo")]);
1946        let err = svc
1947            .launch(launch_input(blueprint, json!({})))
1948            .await
1949            .expect_err("expected fail");
1950        match err {
1951            TaskLaunchError::FlowEval { message: msg, .. } => {
1952                assert!(msg.contains("extern"), "expected extern error, got: {msg}");
1953            }
1954            other => panic!("expected FlowEval error, got {other:?}"),
1955        }
1956    }
1957
1958    // ──────────────────────────────────────────────────────────────────
1959    // GH #50 (Subtask 2 follow-up): `TaskLaunchService::launch`'s
1960    // `compiler.compile` → `engine.register_verdict_contracts(...)` call
1961    // site — task_launch-level end-to-end (compile → register →
1962    // `Engine::verdict_contract_for_task` resolves it). The full HTTP
1963    // submit-time-422 round trip is covered separately: handler-level in
1964    // `crates/mlua-swarm-server/src/worker.rs`'s own `#[cfg(test)] mod
1965    // tests` GH #50 section (which seeds `Engine::register_verdict_contracts`
1966    // directly, bypassing this launch path since `mlua-swarm-server`
1967    // cannot depend on this crate's private test helpers) and
1968    // process-boundary-HTTP in
1969    // `crates/mlua-swarm-server/tests/verdict_contract.rs`. This test is
1970    // the missing link between those two: it exercises the REAL
1971    // `TaskLaunchService::launch` call site (not a hand-rolled duplicate
1972    // of its two lines) end-to-end through a real `Compiler::compile`,
1973    // proving the production wiring this follow-up added actually
1974    // populates the registry `Engine::verdict_contract_for_task` reads.
1975    // ──────────────────────────────────────────────────────────────────
1976
1977    #[tokio::test]
1978    async fn launch_registers_the_blueprints_verdict_contracts_into_the_engine() {
1979        let factory = RustFnInProcessSpawnerFactory::new().register_fn("gate", |inv| async move {
1980            Ok(WorkerResult {
1981                value: json!(inv.prompt),
1982                ok: true,
1983                stats: None,
1984            })
1985        });
1986        let svc = build_service(factory);
1987        let mut gate_agent = agent("gate", "gate");
1988        gate_agent.verdict = Some(mlua_swarm_schema::VerdictContract {
1989            channel: mlua_swarm_schema::VerdictChannel::Body,
1990            values: vec!["PASS".to_string(), "BLOCKED".to_string()],
1991        });
1992        let flow = step("gate", path("$.input"), path("$.out"));
1993        let blueprint = bp(flow, vec![gate_agent]);
1994
1995        let out = svc
1996            .launch(launch_input(blueprint, json!({ "input": "PASS" })))
1997            .await
1998            .expect("launch ok");
1999        assert_eq!(out.final_ctx["out"], json!("PASS"));
2000
2001        // `EngineDispatcher::dispatch` calls `engine.start_task` for every
2002        // dispatched Step (`TaskSpec.agent = ref_`) — this single-Step
2003        // Blueprint against a fresh per-test `Engine` (`build_service`)
2004        // leaves exactly one entry in `EngineState.tasks`.
2005        let task_id = svc
2006            .engine()
2007            .with_state("test.find_dispatched_task_id", |s| {
2008                s.tasks.keys().next().cloned()
2009            })
2010            .await
2011            .expect("with_state")
2012            .expect("launch must have dispatched exactly one Step (one TaskState)");
2013
2014        let contract = svc
2015            .engine()
2016            .verdict_contract_for_task(&task_id)
2017            .await
2018            .expect(
2019                "TaskLaunchService::launch must have merged this Blueprint's compiled \
2020                 verdict_contracts into the engine's runtime registry \
2021                 (Engine::register_verdict_contracts, called right after \
2022                 compiler.compile succeeds) — verdict_contract_for_task resolving None \
2023                 here means that production wiring regressed",
2024            );
2025        assert_eq!(contract.channel, mlua_swarm_schema::VerdictChannel::Body);
2026        assert_eq!(
2027            contract.values,
2028            vec!["PASS".to_string(), "BLOCKED".to_string()]
2029        );
2030    }
2031
2032    // ──────────────────────────────────────────────────────────────────
2033    // issue #13 run_id propagation (`TaskLaunchInput.run_ctx`)
2034    // ──────────────────────────────────────────────────────────────────
2035
2036    #[tokio::test]
2037    async fn launch_with_run_ctx_appends_one_step_entry_per_dispatched_step() {
2038        use crate::store::run::{InMemoryRunStore, RunContext, RunRecord, RunStatus, RunStore};
2039        use crate::types::{RunId, TaskId};
2040
2041        let factory = RustFnInProcessSpawnerFactory::new()
2042            .register_fn("upper", |inv| async move {
2043                Ok(WorkerResult {
2044                    value: json!(inv.prompt.to_uppercase()),
2045                    ok: true,
2046                    stats: None,
2047                })
2048            })
2049            .register_fn("suffix", |inv| async move {
2050                let s = serde_json::from_str::<String>(&inv.prompt).unwrap_or(inv.prompt);
2051                Ok(WorkerResult {
2052                    value: json!(format!("{s}!")),
2053                    ok: true,
2054                    stats: None,
2055                })
2056            });
2057        let svc = build_service(factory);
2058        let flow = FlowNode::Seq {
2059            children: vec![
2060                step("upper", path("$.in"), path("$.s1")),
2061                step("suffix", path("$.s1"), path("$.s2")),
2062            ],
2063        };
2064        let blueprint = bp(
2065            flow,
2066            vec![agent("upper", "upper"), agent("suffix", "suffix")],
2067        );
2068
2069        let run_store: Arc<dyn RunStore> = Arc::new(InMemoryRunStore::new());
2070        let run_id = RunId::new();
2071        run_store
2072            .create(RunRecord {
2073                id: run_id.clone(),
2074                task_id: TaskId::new(),
2075                status: RunStatus::Running,
2076                step_entries: Vec::new(),
2077                degradations: Vec::new(),
2078                operator_sid: None,
2079                result_ref: None,
2080                input_json: Some("{}".to_string()),
2081                created_at: 0,
2082                updated_at: 0,
2083            })
2084            .await
2085            .expect("seed RunRecord");
2086
2087        let mut input = launch_input(blueprint, json!({ "in": "hi" }));
2088        input.run_ctx = Some(RunContext::new(run_id.clone(), run_store.clone()));
2089
2090        let out = svc.launch(input).await.expect("launch ok");
2091        assert_eq!(out.final_ctx["s2"], "HI!");
2092
2093        let run = run_store.get(&run_id).await.expect("run present");
2094        assert_eq!(
2095            run.step_entries.len(),
2096            2,
2097            "expected one step_entry per dispatched step, got {:?}",
2098            run.step_entries
2099        );
2100        assert_eq!(run.step_entries[0].step_ref, Some("upper".to_string()));
2101        assert_eq!(run.step_entries[0].status, Some("passed".to_string()));
2102        assert!(run.step_entries[0].binding_digest.is_some());
2103        assert_eq!(run.step_entries[1].step_ref, Some("suffix".to_string()));
2104        assert_eq!(run.step_entries[1].status, Some("passed".to_string()));
2105        assert!(run.step_entries[1].binding_digest.is_some());
2106        let snapshot: Value = serde_json::from_str(run.input_json.as_deref().unwrap()).unwrap();
2107        assert_eq!(snapshot["bound_agents"].as_array().unwrap().len(), 2);
2108    }
2109
2110    #[tokio::test]
2111    async fn run_snapshot_reuses_bound_agent_after_blueprint_mutation() {
2112        use crate::store::run::{InMemoryRunStore, RunContext, RunRecord, RunStatus, RunStore};
2113        use crate::types::{RunId, TaskId};
2114
2115        let run_store: Arc<dyn RunStore> = Arc::new(InMemoryRunStore::new());
2116        let run_id = RunId::new();
2117        run_store
2118            .create(RunRecord {
2119                id: run_id.clone(),
2120                task_id: TaskId::new(),
2121                status: RunStatus::Running,
2122                step_entries: Vec::new(),
2123                degradations: Vec::new(),
2124                operator_sid: None,
2125                result_ref: None,
2126                input_json: Some("{}".to_string()),
2127                created_at: 0,
2128                updated_at: 0,
2129            })
2130            .await
2131            .unwrap();
2132        let run_ctx = RunContext::new(run_id, run_store);
2133        let mut original_agent = agent("worker", "worker");
2134        original_agent.profile = Some(crate::blueprint::AgentProfile {
2135            system_prompt: "original role".to_string(),
2136            ..Default::default()
2137        });
2138        let mut blueprint = bp(
2139            step("worker", path("$.input"), path("$.out")),
2140            vec![original_agent],
2141        );
2142
2143        let (original, _) = load_or_resolve_bound_agents(
2144            &blueprint,
2145            Some(&run_ctx),
2146            None,
2147            LegacyWorkerBindingPolicy::Allow,
2148        )
2149        .await
2150        .unwrap();
2151        blueprint.agents[0].profile.as_mut().unwrap().system_prompt = "mutated role".to_string();
2152        let (restored, _) = load_or_resolve_bound_agents(
2153            &blueprint,
2154            Some(&run_ctx),
2155            None,
2156            LegacyWorkerBindingPolicy::Allow,
2157        )
2158        .await
2159        .unwrap();
2160
2161        assert_eq!(restored[0].binding_digest, original[0].binding_digest);
2162        assert_eq!(
2163            restored[0].agent.profile.as_ref().unwrap().system_prompt,
2164            "original role"
2165        );
2166    }
2167
2168    #[tokio::test]
2169    async fn strict_migration_policy_rejects_fresh_legacy_worker_binding() {
2170        let mut legacy_agent = agent("worker", "worker");
2171        legacy_agent.profile = Some(AgentProfile {
2172            worker_binding: Some("legacy-worker".to_string()),
2173            ..Default::default()
2174        });
2175        let blueprint = bp(
2176            step("worker", path("$.input"), path("$.out")),
2177            vec![legacy_agent],
2178        );
2179
2180        let error =
2181            load_or_resolve_bound_agents(&blueprint, None, None, LegacyWorkerBindingPolicy::Reject)
2182                .await
2183                .expect_err("strict migration policy must reject fallback");
2184        assert!(error
2185            .to_string()
2186            .contains("deprecated profile.worker_binding"));
2187    }
2188
2189    #[tokio::test]
2190    async fn run_snapshot_calls_binding_provider_only_on_first_resolution() {
2191        use crate::binding::{AgentBindingProvider, BindingProviderError};
2192        use crate::blueprint::{BindOutcome, BindReceipt, BindRequest};
2193        use crate::store::run::{InMemoryRunStore, RunContext, RunRecord, RunStatus, RunStore};
2194        use crate::types::{RunId, TaskId};
2195        use std::sync::atomic::{AtomicUsize, Ordering};
2196
2197        struct CountingProvider(AtomicUsize);
2198
2199        #[async_trait::async_trait]
2200        impl AgentBindingProvider for CountingProvider {
2201            async fn bind(
2202                &self,
2203                requests: &[BindRequest],
2204            ) -> Result<Vec<BindOutcome>, BindingProviderError> {
2205                self.0.fetch_add(1, Ordering::SeqCst);
2206                Ok(requests
2207                    .iter()
2208                    .map(|request| BindOutcome::Bound {
2209                        receipt: BindReceipt {
2210                            agent: request.agent.clone(),
2211                            request_digest: request.request_digest.clone(),
2212                            provider_id: "operator-main-ai".to_string(),
2213                            provider_revision: Some("test".to_string()),
2214                            resolved_model: request.requested_model.clone(),
2215                            effective_tools: request.requested_tools.clone(),
2216                            launch_variant: request.launch_variant.clone(),
2217                            capability_snapshot_digest: None,
2218                        },
2219                    })
2220                    .collect())
2221            }
2222        }
2223
2224        let run_store: Arc<dyn RunStore> = Arc::new(InMemoryRunStore::new());
2225        let run_id = RunId::new();
2226        run_store
2227            .create(RunRecord {
2228                id: run_id.clone(),
2229                task_id: TaskId::new(),
2230                status: RunStatus::Running,
2231                step_entries: Vec::new(),
2232                degradations: Vec::new(),
2233                operator_sid: None,
2234                result_ref: None,
2235                input_json: Some("{}".to_string()),
2236                created_at: 0,
2237                updated_at: 0,
2238            })
2239            .await
2240            .unwrap();
2241        let run_ctx = RunContext::new(run_id, run_store);
2242        let mut blueprint = bp(
2243            step("worker", path("$.input"), path("$.out")),
2244            vec![agent("worker", "worker")],
2245        );
2246        blueprint.agents[0].runner = Some(Runner::WsClaudeCode {
2247            variant: "mse-worker".to_string(),
2248            tools: vec!["Read".to_string()],
2249        });
2250        let provider = CountingProvider(AtomicUsize::new(0));
2251
2252        let (first, _) = load_or_resolve_bound_agents(
2253            &blueprint,
2254            Some(&run_ctx),
2255            Some(&provider),
2256            LegacyWorkerBindingPolicy::Allow,
2257        )
2258        .await
2259        .unwrap();
2260        let (restored, _) = load_or_resolve_bound_agents(
2261            &blueprint,
2262            Some(&run_ctx),
2263            Some(&provider),
2264            LegacyWorkerBindingPolicy::Allow,
2265        )
2266        .await
2267        .unwrap();
2268
2269        assert_eq!(provider.0.load(Ordering::SeqCst), 1);
2270        assert!(first[0].attestation.is_some());
2271        assert_eq!(restored, first);
2272    }
2273
2274    // ──────────────────────────────────────────────────────────────────
2275    // D1: snapshot origin marker (`bound_agents_origin`)
2276    // ──────────────────────────────────────────────────────────────────
2277
2278    /// An initial launch (RunContext `resume == false`) that has to resolve
2279    /// fresh persists `bound_agents_origin: "launch"` alongside
2280    /// `bound_agents`, and records NO backfill degradation — a first-time
2281    /// pin is not a degradation.
2282    #[tokio::test]
2283    async fn fresh_resolve_on_launch_persists_launch_origin_no_degradation() {
2284        use crate::store::run::{
2285            InMemoryRunStore, RunContext, RunRecord, RunStatus, RunStore, BOUND_AGENTS_ORIGIN_KEY,
2286        };
2287        use crate::types::{RunId, TaskId};
2288
2289        let run_store: Arc<dyn RunStore> = Arc::new(InMemoryRunStore::new());
2290        let run_id = RunId::new();
2291        run_store
2292            .create(RunRecord {
2293                id: run_id.clone(),
2294                task_id: TaskId::new(),
2295                status: RunStatus::Running,
2296                step_entries: Vec::new(),
2297                degradations: Vec::new(),
2298                operator_sid: None,
2299                result_ref: None,
2300                input_json: Some("{}".to_string()),
2301                created_at: 0,
2302                updated_at: 0,
2303            })
2304            .await
2305            .unwrap();
2306        // No `.with_resume()` — this is an initial launch.
2307        let run_ctx = RunContext::new(run_id.clone(), run_store.clone());
2308        let blueprint = bp(
2309            step("worker", path("$.input"), path("$.out")),
2310            vec![agent("worker", "worker")],
2311        );
2312
2313        load_or_resolve_bound_agents(
2314            &blueprint,
2315            Some(&run_ctx),
2316            None,
2317            LegacyWorkerBindingPolicy::Allow,
2318        )
2319        .await
2320        .expect("launch resolve ok");
2321
2322        let run = run_store.get(&run_id).await.expect("run present");
2323        let snapshot: Value = serde_json::from_str(run.input_json.as_deref().unwrap()).unwrap();
2324        assert!(
2325            snapshot["bound_agents"].is_array(),
2326            "bound_agents must be persisted"
2327        );
2328        assert_eq!(snapshot[BOUND_AGENTS_ORIGIN_KEY], json!("launch"));
2329        assert_eq!(
2330            SnapshotOrigin::from_snapshot(&snapshot),
2331            SnapshotOrigin::Launch
2332        );
2333        assert!(
2334            run.degradations.is_empty(),
2335            "an initial-launch resolve is not a degradation"
2336        );
2337    }
2338
2339    /// A resume (RunContext `resume == true`) that has to backfill a
2340    /// pre-binding-snapshot Run persists `bound_agents_origin:
2341    /// "resume_backfill"` and appends exactly one backfill degradation
2342    /// (`fallback: "resume_backfill"`).
2343    #[tokio::test]
2344    async fn backfill_on_resume_persists_resume_origin_and_records_degradation() {
2345        use crate::store::run::{
2346            InMemoryRunStore, RunContext, RunRecord, RunStatus, RunStore, BOUND_AGENTS_ORIGIN_KEY,
2347        };
2348        use crate::types::{RunId, TaskId};
2349
2350        let run_store: Arc<dyn RunStore> = Arc::new(InMemoryRunStore::new());
2351        let run_id = RunId::new();
2352        run_store
2353            .create(RunRecord {
2354                id: run_id.clone(),
2355                task_id: TaskId::new(),
2356                status: RunStatus::Running,
2357                step_entries: Vec::new(),
2358                degradations: Vec::new(),
2359                operator_sid: None,
2360                result_ref: None,
2361                input_json: Some("{}".to_string()),
2362                created_at: 0,
2363                updated_at: 0,
2364            })
2365            .await
2366            .unwrap();
2367        let run_ctx = RunContext::new(run_id.clone(), run_store.clone()).with_resume();
2368        let blueprint = bp(
2369            step("worker", path("$.input"), path("$.out")),
2370            vec![agent("worker", "worker")],
2371        );
2372
2373        load_or_resolve_bound_agents(
2374            &blueprint,
2375            Some(&run_ctx),
2376            None,
2377            LegacyWorkerBindingPolicy::Allow,
2378        )
2379        .await
2380        .expect("resume backfill ok");
2381
2382        let run = run_store.get(&run_id).await.expect("run present");
2383        let snapshot: Value = serde_json::from_str(run.input_json.as_deref().unwrap()).unwrap();
2384        assert_eq!(snapshot[BOUND_AGENTS_ORIGIN_KEY], json!("resume_backfill"));
2385        assert_eq!(
2386            SnapshotOrigin::from_snapshot(&snapshot),
2387            SnapshotOrigin::ResumeBackfill
2388        );
2389        assert_eq!(
2390            run.degradations.len(),
2391            1,
2392            "a resume backfill must record exactly one degradation"
2393        );
2394        assert_eq!(run.degradations[0].tool, "binding");
2395        assert_eq!(run.degradations[0].fallback, "resume_backfill");
2396    }
2397
2398    // ──────────────────────────────────────────────────────────────────
2399    // C1: `strict_binding` gate + optional attestation
2400    // ──────────────────────────────────────────────────────────────────
2401
2402    /// A Runner-backed Blueprint whose single `worker` agent binds through a
2403    /// WS Operator variant `mse-worker` requiring tool `Read`.
2404    fn runner_blueprint(strict_binding: bool) -> Blueprint {
2405        let mut blueprint = bp(
2406            step("worker", path("$.input"), path("$.out")),
2407            vec![agent("worker", "worker")],
2408        );
2409        blueprint.strategy.strict_binding = strict_binding;
2410        blueprint.agents[0].runner = Some(Runner::WsClaudeCode {
2411            variant: "mse-worker".to_string(),
2412            tools: vec!["Read".to_string()],
2413        });
2414        blueprint
2415    }
2416
2417    /// Provider that leaves every request `Unbound` — models a missing /
2418    /// manifest-less execution environment.
2419    struct AlwaysUnboundProvider;
2420
2421    #[async_trait::async_trait]
2422    impl AgentBindingProvider for AlwaysUnboundProvider {
2423        async fn bind(
2424            &self,
2425            requests: &[crate::blueprint::BindRequest],
2426        ) -> Result<Vec<crate::blueprint::BindOutcome>, crate::binding::BindingProviderError>
2427        {
2428            Ok(requests
2429                .iter()
2430                .map(|request| crate::blueprint::BindOutcome::Unbound {
2431                    agent: request.agent.clone(),
2432                    reason: "no capability manifest submitted".to_string(),
2433                })
2434                .collect())
2435        }
2436    }
2437
2438    /// Non-strict + a provider that cannot attest → launch resolution
2439    /// succeeds, the agent stays `DeclarationOnly`, and the gap is recorded
2440    /// as a `RunRecord.degradations` entry.
2441    #[tokio::test]
2442    async fn non_strict_unbound_agent_runs_declaration_only_with_degradation() {
2443        use crate::store::run::{InMemoryRunStore, RunContext, RunRecord, RunStatus, RunStore};
2444        use crate::types::{RunId, TaskId};
2445
2446        let run_store: Arc<dyn RunStore> = Arc::new(InMemoryRunStore::new());
2447        let run_id = RunId::new();
2448        run_store
2449            .create(RunRecord {
2450                id: run_id.clone(),
2451                task_id: TaskId::new(),
2452                status: RunStatus::Running,
2453                step_entries: Vec::new(),
2454                degradations: Vec::new(),
2455                operator_sid: None,
2456                result_ref: None,
2457                input_json: Some("{}".to_string()),
2458                created_at: 0,
2459                updated_at: 0,
2460            })
2461            .await
2462            .unwrap();
2463        let run_ctx = RunContext::new(run_id.clone(), run_store.clone());
2464
2465        let (bound, _) = load_or_resolve_bound_agents(
2466            &runner_blueprint(false),
2467            Some(&run_ctx),
2468            Some(&AlwaysUnboundProvider),
2469            LegacyWorkerBindingPolicy::Allow,
2470        )
2471        .await
2472        .expect("non-strict launch must succeed even without an attestation");
2473        assert!(
2474            bound[0].attestation.is_none(),
2475            "an unattested agent must stay DeclarationOnly"
2476        );
2477
2478        let run = run_store.get(&run_id).await.expect("run present");
2479        assert_eq!(run.degradations.len(), 1, "expected one degradation entry");
2480        assert_eq!(run.degradations[0].tool, "binding");
2481        assert_eq!(run.degradations[0].fallback, "DeclarationOnly");
2482        assert!(run.degradations[0].error.contains("no capability manifest"));
2483    }
2484
2485    /// Strict + a provider that cannot attest → launch fails, and the error
2486    /// message names the agent and its requested launch variant / tools so an
2487    /// Operator can generate a satisfying manifest.
2488    #[tokio::test]
2489    async fn strict_unbound_agent_fails_with_requirements_in_message() {
2490        let error = load_or_resolve_bound_agents(
2491            &runner_blueprint(true),
2492            None,
2493            Some(&AlwaysUnboundProvider),
2494            LegacyWorkerBindingPolicy::Allow,
2495        )
2496        .await
2497        .expect_err("strict + Unbound must reject the launch");
2498        match error {
2499            TaskLaunchError::PreDispatch(message) => {
2500                assert!(message.contains("worker"), "message: {message}");
2501                assert!(message.contains("mse-worker"), "message: {message}");
2502                assert!(message.contains("Read"), "message: {message}");
2503            }
2504            other => panic!("expected PreDispatch, got {other:?}"),
2505        }
2506    }
2507
2508    /// Strict + no provider at all → launch fails fast: nothing can attest the
2509    /// Runner-backed agent.
2510    #[tokio::test]
2511    async fn strict_without_provider_rejects_runner_backed_launch() {
2512        let error = load_or_resolve_bound_agents(
2513            &runner_blueprint(true),
2514            None,
2515            None,
2516            LegacyWorkerBindingPolicy::Allow,
2517        )
2518        .await
2519        .expect_err("strict + no provider must reject a Runner-backed launch");
2520        match error {
2521            TaskLaunchError::PreDispatch(message) => {
2522                assert!(
2523                    message.contains("strict_binding requires a binding provider"),
2524                    "message: {message}"
2525                );
2526            }
2527            other => panic!("expected PreDispatch, got {other:?}"),
2528        }
2529    }
2530
2531    /// Strict + a correct manifest → the agent is Attested (the pre-C1 pass
2532    /// path still holds under the strict gate).
2533    #[tokio::test]
2534    async fn strict_with_correct_manifest_attests_the_agent() {
2535        use crate::binding::ManifestBindingProvider;
2536        use crate::blueprint::{AgentProviderCapability, AgentProviderManifest};
2537
2538        let provider = ManifestBindingProvider::new(AgentProviderManifest {
2539            provider_id: "operator-main-ai".to_string(),
2540            provider_revision: Some("1".to_string()),
2541            capabilities: vec![AgentProviderCapability {
2542                launch_variant: Some("mse-worker".to_string()),
2543                resolved_model: None,
2544                effective_tools: vec!["Read".to_string()],
2545                capability_snapshot_digest: None,
2546            }],
2547        });
2548        let (bound, _) = load_or_resolve_bound_agents(
2549            &runner_blueprint(true),
2550            None,
2551            Some(&provider),
2552            LegacyWorkerBindingPolicy::Allow,
2553        )
2554        .await
2555        .expect("strict launch with a correct manifest must attest");
2556        assert!(
2557            bound[0].attestation.is_some(),
2558            "a correctly attested agent must carry its attestation"
2559        );
2560    }
2561
2562    // ──────────────────────────────────────────────────────────────────
2563    // C2: spawn-frame self-check inputs (request_digest / requested_model)
2564    // ──────────────────────────────────────────────────────────────────
2565
2566    /// The launch-path `WorkerBinding` map carries the requesting side's
2567    /// self-check inputs: the immutable snapshot's `binding_digest` and the
2568    /// profile's declared model, so a non-strict Operator can compare the
2569    /// spawn frame against its own environment.
2570    #[test]
2571    fn worker_bindings_carry_request_digest_and_model() {
2572        let mut blueprint = runner_blueprint(false);
2573        blueprint.agents[0].profile = Some(AgentProfile {
2574            model: Some("claude-sonnet".to_string()),
2575            ..Default::default()
2576        });
2577        let bound = resolve_bound_agents(&blueprint).expect("resolvable Runner refs");
2578        let bindings = worker_bindings_from_bound_agents(&bound);
2579
2580        let wb = bindings.get("worker").expect("worker binding present");
2581        assert_eq!(
2582            wb.request_digest.as_ref(),
2583            Some(&bound[0].binding_digest),
2584            "the spawn frame must carry the immutable snapshot digest"
2585        );
2586        assert!(wb
2587            .request_digest
2588            .as_ref()
2589            .unwrap()
2590            .as_str()
2591            .starts_with("sha256:"));
2592        assert_eq!(wb.requested_model.as_deref(), Some("claude-sonnet"));
2593    }
2594
2595    /// A Runner-backed agent whose profile declares no model leaves
2596    /// `requested_model` `None` while still carrying the digest.
2597    #[test]
2598    fn worker_bindings_omit_model_when_profile_has_none() {
2599        let bound = resolve_bound_agents(&runner_blueprint(false)).expect("resolvable Runner refs");
2600        let bindings = worker_bindings_from_bound_agents(&bound);
2601        let wb = bindings.get("worker").expect("worker binding present");
2602        assert!(wb.request_digest.is_some());
2603        assert!(wb.requested_model.is_none());
2604    }
2605
2606    #[tokio::test]
2607    async fn launch_without_run_ctx_appends_no_step_entries() {
2608        // `run_ctx: None` (the `automate()` default) must not touch any
2609        // `RunStore` — this is the pre-existing no-tracing behavior, kept
2610        // as a regression guard alongside the `Some` case above.
2611        let factory = RustFnInProcessSpawnerFactory::new().register_fn("echo", |inv| async move {
2612            Ok(WorkerResult {
2613                value: json!(inv.prompt),
2614                ok: true,
2615                stats: None,
2616            })
2617        });
2618        let svc = build_service(factory);
2619        let blueprint = bp(
2620            step("echo", path("$.input"), path("$.out")),
2621            vec![agent("echo", "echo")],
2622        );
2623        let input = launch_input(blueprint, json!({ "input": "hi" }));
2624        assert!(
2625            input.run_ctx.is_none(),
2626            "automate() defaults run_ctx to None"
2627        );
2628        let out = svc.launch(input).await.expect("launch ok");
2629        assert_eq!(out.final_ctx["out"], "hi");
2630    }
2631
2632    // ──────────────────────────────────────────────────────────────────
2633    // issue #19 ST2: `TaskLaunchInput.task_input` (direct-sibling-read
2634    // replacement for the ST1 `from_init_ctx(&input.init_ctx)` call)
2635    // ──────────────────────────────────────────────────────────────────
2636
2637    #[tokio::test]
2638    async fn launch_with_task_input_leaves_init_ctx_object_seed_unmutated() {
2639        // Issue #19 ST2 invariant: `init_ctx` is a pure flow-ir eval seed —
2640        // `task_input` must not be folded into it. Regression guard for the
2641        // ST1 `resolve_task_level_init_ctx` fold-back this subtask removes:
2642        // if it ever crept back in here, `project_root` / `work_dir` /
2643        // `task_metadata` would leak into `final_ctx` as extra top-level
2644        // keys nobody wrote via a `Step.out`.
2645        let factory = RustFnInProcessSpawnerFactory::new().register_fn("echo", |inv| async move {
2646            Ok(WorkerResult {
2647                value: json!({ "echoed": inv.prompt }),
2648                ok: true,
2649                stats: None,
2650            })
2651        });
2652        let svc = build_service(factory);
2653        let blueprint = bp(
2654            step("echo", path("$.input"), path("$.out")),
2655            vec![agent("echo", "echo")],
2656        );
2657        let mut input = launch_input(blueprint, json!({ "input": "hi" }));
2658        input.task_input = Some(TaskInputSpec {
2659            project_root: Some("/repo".to_string()),
2660            work_dir: Some("/repo/work".to_string()),
2661            task_metadata: Some(json!({ "issue": 19 })),
2662        });
2663        let out = svc.launch(input).await.expect("launch ok");
2664        assert_eq!(out.final_ctx["out"]["echoed"], "hi");
2665        assert!(
2666            out.final_ctx.get("project_root").is_none(),
2667            "task_input must not be folded into the flow-ir ctx seed, got {:?}",
2668            out.final_ctx
2669        );
2670        assert!(out.final_ctx.get("work_dir").is_none());
2671        assert!(out.final_ctx.get("task_metadata").is_none());
2672    }
2673
2674    #[tokio::test]
2675    async fn launch_with_task_input_none_is_a_no_op() {
2676        let factory = RustFnInProcessSpawnerFactory::new().register_fn("echo", |inv| async move {
2677            Ok(WorkerResult {
2678                value: json!(inv.prompt),
2679                ok: true,
2680                stats: None,
2681            })
2682        });
2683        let svc = build_service(factory);
2684        let blueprint = bp(
2685            step("echo", path("$.input"), path("$.out")),
2686            vec![agent("echo", "echo")],
2687        );
2688        let mut input = launch_input(blueprint, json!({ "input": "hi" }));
2689        assert!(input.task_input.is_none(), "automate() defaults to None");
2690        input.task_input = None;
2691        let out = svc.launch(input).await.expect("launch ok");
2692        assert_eq!(out.final_ctx["out"], "hi");
2693    }
2694
2695    #[tokio::test]
2696    async fn launch_with_task_input_all_fields_absent_is_a_no_op() {
2697        // `Some(TaskInputSpec::default())` — outer Some, all 3 inner fields
2698        // None — must behave identically to `task_input: None` (mirrors
2699        // `TaskInputMiddleware::new_from_fields`'s own no-op contract).
2700        let factory = RustFnInProcessSpawnerFactory::new().register_fn("echo", |inv| async move {
2701            Ok(WorkerResult {
2702                value: json!(inv.prompt),
2703                ok: true,
2704                stats: None,
2705            })
2706        });
2707        let svc = build_service(factory);
2708        let blueprint = bp(
2709            step("echo", path("$.input"), path("$.out")),
2710            vec![agent("echo", "echo")],
2711        );
2712        let mut input = launch_input(blueprint, json!({ "input": "hi" }));
2713        input.task_input = Some(TaskInputSpec::default());
2714        let out = svc.launch(input).await.expect("launch ok");
2715        assert_eq!(out.final_ctx["out"], "hi");
2716    }
2717
2718    // ──────────────────────────────────────────────────────────────────
2719    // issue #19 ST3: `merge_init_ctx` (BP default + Task init_ctx)
2720    // ──────────────────────────────────────────────────────────────────
2721
2722    #[test]
2723    fn merge_init_ctx_bp_default_only_passes_through_when_task_is_empty_object() {
2724        let bp_default = json!({ "seeded": "from-bp" });
2725        let task = json!({});
2726        let merged = merge_init_ctx(Some(&bp_default), &task);
2727        assert_eq!(merged, json!({ "seeded": "from-bp" }));
2728    }
2729
2730    #[test]
2731    fn merge_init_ctx_task_only_passes_through_when_bp_default_is_empty_object() {
2732        let bp_default = json!({});
2733        let task = json!({ "seeded": "from-task" });
2734        let merged = merge_init_ctx(Some(&bp_default), &task);
2735        assert_eq!(merged, json!({ "seeded": "from-task" }));
2736    }
2737
2738    #[test]
2739    fn merge_init_ctx_both_objects_task_wins_on_key_collision() {
2740        let bp_default = json!({ "a": "bp", "b": "bp-only" });
2741        let task = json!({ "a": "task", "c": "task-only" });
2742        let merged = merge_init_ctx(Some(&bp_default), &task);
2743        assert_eq!(
2744            merged,
2745            json!({ "a": "task", "b": "bp-only", "c": "task-only" })
2746        );
2747    }
2748
2749    #[test]
2750    fn merge_init_ctx_non_object_task_fully_replaces_bp_default() {
2751        let bp_default = json!({ "seeded": "from-bp" });
2752        let task = json!("plain-string-seed");
2753        let merged = merge_init_ctx(Some(&bp_default), &task);
2754        assert_eq!(merged, json!("plain-string-seed"));
2755    }
2756
2757    #[test]
2758    fn merge_init_ctx_no_bp_default_is_a_no_op() {
2759        let task = json!({ "input": "hi" });
2760        let merged = merge_init_ctx(None, &task);
2761        assert_eq!(merged, task);
2762    }
2763
2764    // ──────────────────────────────────────────────────────────────────
2765    // issue #19 ST4: `merge_init_ctx_3layer` (BP default + Task + Run)
2766    // ──────────────────────────────────────────────────────────────────
2767
2768    #[test]
2769    fn merge_init_ctx_3layer_no_run_override_equals_bp_task_merge_only() {
2770        // `run_override: None` must be a pure pass-through of the BP+Task
2771        // merge — this is the `POST /v1/tasks/:id/runs` no-body rekick
2772        // path, which must preserve pre-#19 behavior byte-for-byte.
2773        let bp_default = json!({ "a": "bp", "b": "bp-only" });
2774        let task = json!({ "a": "task", "c": "task-only" });
2775        let three_layer = merge_init_ctx_3layer(Some(&bp_default), &task, None);
2776        let two_layer = merge_init_ctx(Some(&bp_default), &task);
2777        assert_eq!(three_layer, two_layer);
2778        assert_eq!(
2779            three_layer,
2780            json!({ "a": "task", "b": "bp-only", "c": "task-only" })
2781        );
2782    }
2783
2784    #[test]
2785    fn merge_init_ctx_3layer_run_object_wins_on_key_collision_over_bp_and_task() {
2786        let bp_default = json!({ "a": "bp", "b": "bp-only" });
2787        let task = json!({ "a": "task", "c": "task-only" });
2788        let run_override = json!({ "a": "run", "d": "run-only" });
2789        let merged = merge_init_ctx_3layer(Some(&bp_default), &task, Some(&run_override));
2790        assert_eq!(
2791            merged,
2792            json!({ "a": "run", "b": "bp-only", "c": "task-only", "d": "run-only" }),
2793            "Run wins on collision (a); BP-only (b) and Task-only (c) keys survive"
2794        );
2795    }
2796
2797    #[test]
2798    fn merge_init_ctx_3layer_run_non_object_fully_replaces_bp_task_merge() {
2799        let bp_default = json!({ "seeded": "from-bp" });
2800        let task = json!({ "seeded": "from-task" });
2801        let run_override = json!("plain-string-run-seed");
2802        let merged = merge_init_ctx_3layer(Some(&bp_default), &task, Some(&run_override));
2803        assert_eq!(merged, json!("plain-string-run-seed"));
2804    }
2805
2806    #[test]
2807    fn merge_init_ctx_3layer_no_bp_default_and_no_run_override_is_task_passthrough() {
2808        let task = json!({ "input": "hi" });
2809        let merged = merge_init_ctx_3layer(None, &task, None);
2810        assert_eq!(merged, task);
2811    }
2812
2813    #[tokio::test]
2814    async fn launch_merges_bp_default_init_ctx_into_task_init_ctx() {
2815        // End-to-end guard: `Blueprint.default_init_ctx` actually reaches
2816        // `eval_async_externs` — not merely unit-tested in isolation.
2817        let factory = RustFnInProcessSpawnerFactory::new().register_fn("echo", |inv| async move {
2818            Ok(WorkerResult {
2819                value: json!(inv.prompt),
2820                ok: true,
2821                stats: None,
2822            })
2823        });
2824        let svc = build_service(factory);
2825        let mut blueprint = bp(
2826            step("echo", path("$.greeting"), path("$.out")),
2827            vec![agent("echo", "echo")],
2828        );
2829        blueprint.default_init_ctx = Some(json!({ "greeting": "hello from bp" }));
2830        // Task supplies an empty object — BP default alone seeds `$.greeting`.
2831        let out = svc
2832            .launch(launch_input(blueprint, json!({})))
2833            .await
2834            .expect("launch ok");
2835        assert_eq!(out.final_ctx["out"], "hello from bp");
2836    }
2837
2838    // ──────────────────────────────────────────────────────────────────
2839    // issue #21 Phase 1: `derive_agent_ctx` / `derive_context_policies`
2840    // ──────────────────────────────────────────────────────────────────
2841
2842    fn agent_with_meta(name: &str, fn_id: &str, meta: AgentMeta) -> AgentDef {
2843        AgentDef {
2844            name: name.to_string(),
2845            kind: AgentKind::RustFn,
2846            spec: json!({ "fn_id": fn_id }),
2847            profile: None,
2848            meta: Some(meta),
2849            runner: None,
2850            runner_ref: None,
2851            verdict: None,
2852        }
2853    }
2854
2855    #[test]
2856    fn derive_agent_ctx_empty_blueprint_yields_empty_state() {
2857        let blueprint = bp(step("echo", path("$.in"), path("$.out")), vec![]);
2858        let (global, per_agent) = derive_agent_ctx(&blueprint);
2859        assert_eq!(global, None);
2860        assert!(per_agent.is_empty());
2861    }
2862
2863    #[test]
2864    fn derive_agent_ctx_populated_blueprint_yields_correct_maps() {
2865        let mut blueprint = bp(
2866            step("echo", path("$.in"), path("$.out")),
2867            vec![
2868                agent_with_meta(
2869                    "with-ctx",
2870                    "echo",
2871                    AgentMeta {
2872                        ctx: Some(json!({ "org_conventions": "x" })),
2873                        ..Default::default()
2874                    },
2875                ),
2876                agent("no-ctx", "echo"),
2877            ],
2878        );
2879        blueprint.default_agent_ctx = Some(json!({ "seeded": "from-bp" }));
2880        let (global, per_agent) = derive_agent_ctx(&blueprint);
2881        assert_eq!(global, Some(json!({ "seeded": "from-bp" })));
2882        assert_eq!(
2883            per_agent.len(),
2884            1,
2885            "agents without AgentMeta.ctx are absent, not defaulted to null: {per_agent:?}"
2886        );
2887        assert_eq!(
2888            per_agent.get("with-ctx"),
2889            Some(&json!({ "org_conventions": "x" }))
2890        );
2891        assert!(!per_agent.contains_key("no-ctx"));
2892    }
2893
2894    #[test]
2895    fn derive_context_policies_empty_blueprint_yields_empty_state() {
2896        let blueprint = bp(step("echo", path("$.in"), path("$.out")), vec![]);
2897        let (default_policy, per_agent) = derive_context_policies(&blueprint);
2898        assert_eq!(default_policy, None);
2899        assert!(per_agent.is_empty());
2900    }
2901
2902    #[test]
2903    fn derive_context_policies_populated_blueprint_yields_correct_maps() {
2904        let mut blueprint = bp(
2905            step("echo", path("$.in"), path("$.out")),
2906            vec![
2907                agent_with_meta(
2908                    "with-policy",
2909                    "echo",
2910                    AgentMeta {
2911                        context_policy: Some(ContextPolicy {
2912                            include: None,
2913                            exclude: vec!["work_dir".to_string()],
2914                            ..Default::default()
2915                        }),
2916                        ..Default::default()
2917                    },
2918                ),
2919                agent("no-policy", "echo"),
2920            ],
2921        );
2922        blueprint.default_context_policy = Some(ContextPolicy {
2923            include: Some(vec!["project_root".to_string()]),
2924            exclude: vec![],
2925            ..Default::default()
2926        });
2927        let (default_policy, per_agent) = derive_context_policies(&blueprint);
2928        assert_eq!(
2929            default_policy,
2930            Some(ContextPolicy {
2931                include: Some(vec!["project_root".to_string()]),
2932                exclude: vec![],
2933                ..Default::default()
2934            })
2935        );
2936        assert_eq!(per_agent.len(), 1);
2937        assert_eq!(
2938            per_agent.get("with-policy"),
2939            Some(&ContextPolicy {
2940                include: None,
2941                exclude: vec!["work_dir".to_string()],
2942                ..Default::default()
2943            })
2944        );
2945        assert!(!per_agent.contains_key("no-policy"));
2946    }
2947
2948    // ──────────────────────────────────────────────────────────────────
2949    // issue #21 Phase 2: `derive_step_metas` / `AgentMeta.meta_ref`
2950    // resolution inside `derive_agent_ctx`
2951    // ──────────────────────────────────────────────────────────────────
2952
2953    #[test]
2954    fn derive_step_metas_empty_blueprint_yields_empty_map() {
2955        let blueprint = bp(step("echo", path("$.in"), path("$.out")), vec![]);
2956        assert!(derive_step_metas(&blueprint).is_empty());
2957    }
2958
2959    #[test]
2960    fn derive_step_metas_populated_blueprint_yields_name_to_ctx_map() {
2961        let mut blueprint = bp(step("echo", path("$.in"), path("$.out")), vec![]);
2962        blueprint.metas = vec![
2963            MetaDef {
2964                name: "heavy-scan".to_string(),
2965                ctx: json!({ "work_dir": "/x" }),
2966            },
2967            MetaDef {
2968                name: "light-scan".to_string(),
2969                ctx: json!({ "work_dir": "/y" }),
2970            },
2971        ];
2972        let metas = derive_step_metas(&blueprint);
2973        assert_eq!(metas.len(), 2);
2974        assert_eq!(metas.get("heavy-scan"), Some(&json!({ "work_dir": "/x" })));
2975        assert_eq!(metas.get("light-scan"), Some(&json!({ "work_dir": "/y" })));
2976    }
2977
2978    #[test]
2979    fn derive_agent_ctx_meta_ref_resolves_as_base_under_inline_ctx() {
2980        let mut blueprint = bp(
2981            step("echo", path("$.in"), path("$.out")),
2982            vec![agent_with_meta(
2983                "with-meta-ref",
2984                "echo",
2985                AgentMeta {
2986                    ctx: Some(json!({ "work_dir": "/inline-wins" })),
2987                    meta_ref: Some("shared".to_string()),
2988                    ..Default::default()
2989                },
2990            )],
2991        );
2992        blueprint.metas = vec![MetaDef {
2993            name: "shared".to_string(),
2994            ctx: json!({ "work_dir": "/base", "extra": "from-pool" }),
2995        }];
2996        let (_, per_agent) = derive_agent_ctx(&blueprint);
2997        assert_eq!(
2998            per_agent.get("with-meta-ref"),
2999            Some(&json!({ "work_dir": "/inline-wins", "extra": "from-pool" })),
3000            "inline ctx must win the collided key while pool-only keys survive the merge"
3001        );
3002    }
3003
3004    #[test]
3005    fn derive_agent_ctx_meta_ref_alone_uses_pool_ctx_verbatim() {
3006        let mut blueprint = bp(
3007            step("echo", path("$.in"), path("$.out")),
3008            vec![agent_with_meta(
3009                "with-meta-ref-only",
3010                "echo",
3011                AgentMeta {
3012                    meta_ref: Some("shared".to_string()),
3013                    ..Default::default()
3014                },
3015            )],
3016        );
3017        blueprint.metas = vec![MetaDef {
3018            name: "shared".to_string(),
3019            ctx: json!({ "work_dir": "/base" }),
3020        }];
3021        let (_, per_agent) = derive_agent_ctx(&blueprint);
3022        assert_eq!(
3023            per_agent.get("with-meta-ref-only"),
3024            Some(&json!({ "work_dir": "/base" }))
3025        );
3026    }
3027
3028    #[test]
3029    fn derive_agent_ctx_unresolved_meta_ref_never_panics_and_falls_back_to_inline() {
3030        let blueprint = bp(
3031            step("echo", path("$.in"), path("$.out")),
3032            vec![agent_with_meta(
3033                "with-unresolved-meta-ref",
3034                "echo",
3035                AgentMeta {
3036                    ctx: Some(json!({ "work_dir": "/inline-only" })),
3037                    meta_ref: Some("missing".to_string()),
3038                    ..Default::default()
3039                },
3040            )],
3041        );
3042        // No `blueprint.metas` entries at all — `meta_ref` unresolved.
3043        let (_, per_agent) = derive_agent_ctx(&blueprint);
3044        assert_eq!(
3045            per_agent.get("with-unresolved-meta-ref"),
3046            Some(&json!({ "work_dir": "/inline-only" })),
3047            "an unresolved meta_ref must never panic; the agent's own inline ctx still applies"
3048        );
3049    }
3050
3051    // ──────────────────────────────────────────────────────────────────
3052    // GH #46 Milestone 2 Done Criteria #3 (semantics-match): `resolve_runner`
3053    // ──────────────────────────────────────────────────────────────────
3054
3055    /// `resolve_runner` (in `mlua-swarm-schema`) must synthesize the exact
3056    /// same `(variant, tools)` pair `derive_worker_bindings` does today for
3057    /// every agent whose Runner comes solely from the legacy
3058    /// `AgentProfile.worker_binding` fallback (tier 3 of the cascade) — a
3059    /// machine-checked guard against the two paths silently drifting apart
3060    /// once a future change touches one but forgets the other, mirroring
3061    /// `crate::core::explain`'s
3062    /// `explain_agent_ctx_matches_derive_agent_ctx_semantics` drift guard.
3063    /// This is a read-only cross-check: it exercises the schema crate's
3064    /// pure resolver against real Blueprints, without touching the launch
3065    /// path itself (Milestone 3 scope).
3066    #[test]
3067    fn resolve_runner_legacy_fallback_matches_derive_worker_bindings_semantics() {
3068        fn legacy_agent(name: &str, variant: &str, tools: Vec<&str>) -> AgentDef {
3069            AgentDef {
3070                name: name.to_string(),
3071                kind: AgentKind::Operator,
3072                spec: json!({}),
3073                profile: Some(AgentProfile {
3074                    worker_binding: Some(variant.to_string()),
3075                    tools: tools.into_iter().map(str::to_string).collect(),
3076                    ..Default::default()
3077                }),
3078                meta: None,
3079                runner: None,
3080                runner_ref: None,
3081                verdict: None,
3082            }
3083        }
3084
3085        let blueprint = bp(
3086            step("planner", path("$.in"), path("$.out")),
3087            vec![
3088                legacy_agent("planner", "planning-worker", vec!["Read", "Grep"]),
3089                legacy_agent("coder", "code-worker", vec![]),
3090                agent("no-binding", "echo"),
3091            ],
3092        );
3093
3094        let derived = derive_worker_bindings(&blueprint);
3095
3096        for agent_def in &blueprint.agents {
3097            let resolved = resolve_runner(&blueprint, agent_def).expect("no unresolved refs");
3098            match derived.get(&agent_def.name) {
3099                Some(binding) => {
3100                    assert_eq!(
3101                        resolved,
3102                        Some(Runner::WsClaudeCode {
3103                            variant: binding.variant.clone(),
3104                            tools: binding.tools.clone(),
3105                        }),
3106                        "resolve_runner must synthesize the same WsClaudeCode Runner \
3107                         derive_worker_bindings produces for agent '{}'",
3108                        agent_def.name
3109                    );
3110                }
3111                None => {
3112                    assert_eq!(
3113                        resolved, None,
3114                        "agent '{}' has no derive_worker_bindings entry, so resolve_runner \
3115                         must resolve to None too (no other tier declared)",
3116                        agent_def.name
3117                    );
3118                }
3119            }
3120        }
3121    }
3122
3123    #[test]
3124    fn ws_operator_runner_projects_into_the_existing_spawn_binding() {
3125        let mut blueprint = bp(
3126            step("reviewer", path("$.in"), path("$.out")),
3127            vec![agent("reviewer", "echo")],
3128        );
3129        blueprint.agents[0].runner = Some(Runner::WsOperator {
3130            variant: "mse-reviewer".to_string(),
3131            tools: vec!["Read".to_string(), "Grep".to_string()],
3132        });
3133
3134        let derived = derive_worker_bindings(&blueprint);
3135        let binding = derived
3136            .get("reviewer")
3137            .expect("ws_operator must feed the canonical spawn binding path");
3138        assert_eq!(binding.variant, "mse-reviewer");
3139        assert_eq!(binding.tools, ["Read", "Grep"]);
3140    }
3141
3142    // ──────────────────────────────────────────────────────────────────
3143    // D2: replay compat for backfilled Runs (origin drives digest keying)
3144    // ──────────────────────────────────────────────────────────────────
3145
3146    /// Build a single-step `echo` RustFn Blueprint plus a call counter its
3147    /// worker bumps on every real dispatch. A replay HIT never reaches the
3148    /// worker, so the counter is the "was this step actually executed?"
3149    /// probe.
3150    fn counting_echo_service() -> (TaskLaunchService, Arc<std::sync::atomic::AtomicUsize>) {
3151        use std::sync::atomic::{AtomicUsize, Ordering};
3152        let calls = Arc::new(AtomicUsize::new(0));
3153        let counter = calls.clone();
3154        let factory = RustFnInProcessSpawnerFactory::new().register_fn("echo", move |inv| {
3155            let counter = counter.clone();
3156            async move {
3157                counter.fetch_add(1, Ordering::SeqCst);
3158                Ok(WorkerResult {
3159                    value: json!({ "echoed": inv.prompt }),
3160                    ok: true,
3161                    stats: None,
3162                })
3163            }
3164        });
3165        (build_service(factory), calls)
3166    }
3167
3168    async fn seed_legacy_run(run_store: &Arc<dyn crate::store::run::RunStore>) -> crate::RunId {
3169        use crate::store::run::{RunRecord, RunStatus};
3170        use crate::types::TaskId;
3171        let run_id = crate::RunId::new();
3172        run_store
3173            .create(RunRecord {
3174                id: run_id.clone(),
3175                task_id: TaskId::new(),
3176                status: RunStatus::Running,
3177                step_entries: Vec::new(),
3178                degradations: Vec::new(),
3179                operator_sid: None,
3180                result_ref: None,
3181                // No `bound_agents` — a pre-binding-snapshot ("pre-upgrade")
3182                // Run whose resume must backfill.
3183                input_json: Some("{}".to_string()),
3184                created_at: 0,
3185                updated_at: 0,
3186            })
3187            .await
3188            .expect("seed legacy RunRecord");
3189        run_id
3190    }
3191
3192    /// [Crux D2 items 4 + 6] A pre-upgrade Run whose replay log was hashed
3193    /// WITHOUT binding digests replays cleanly through the full launch path
3194    /// on resume, and stays consistent across a second resume (the fast path
3195    /// reads the persisted `resume_backfill` origin, so no digests are ever
3196    /// mixed into the replay key).
3197    #[tokio::test]
3198    async fn backfilled_run_replays_legacy_keys_stably_across_two_resumes() {
3199        use crate::store::replay::{InMemoryReplayStore, ReplayCursor, ReplayStore};
3200        use crate::store::run::{InMemoryRunStore, RunContext, RunStore};
3201        use std::sync::atomic::Ordering;
3202        use std::sync::Mutex;
3203
3204        let (svc, echo_calls) = counting_echo_service();
3205        let blueprint = bp(
3206            step("echo", path("$.input"), path("$.out")),
3207            vec![agent("echo", "echo")],
3208        );
3209        let run_store: Arc<dyn RunStore> = Arc::new(InMemoryRunStore::new());
3210        let replay_store: Arc<dyn ReplayStore> = Arc::new(InMemoryReplayStore::new());
3211        let run_id = seed_legacy_run(&run_store).await;
3212
3213        // Phase 1 — first resume backfills the snapshot AND, because the
3214        // origin is `resume_backfill`, dispatches with legacy replay keys.
3215        // This is the step that generates the pre-upgrade-shaped replay row.
3216        let rc1 = RunContext::new(run_id.clone(), run_store.clone())
3217            .with_replay_store(replay_store.clone())
3218            .with_resume();
3219        let mut input1 = launch_input(blueprint.clone(), json!({ "input": "hi" }));
3220        input1.run_ctx = Some(rc1);
3221        let out1 = svc.launch(input1).await.expect("phase-1 resume launch ok");
3222        assert_eq!(out1.final_ctx["out"]["echoed"], "hi");
3223        assert_eq!(
3224            echo_calls.load(Ordering::SeqCst),
3225            1,
3226            "phase 1 dispatches the worker once (nothing to replay yet)"
3227        );
3228        let entries = replay_store
3229            .list_by_run(&run_id)
3230            .await
3231            .expect("list replay rows");
3232        assert_eq!(
3233            entries.len(),
3234            1,
3235            "phase 1 must log exactly one legacy-hashed replay row"
3236        );
3237        let run = run_store.get(&run_id).await.expect("run present");
3238        let snap: Value = serde_json::from_str(run.input_json.as_deref().unwrap()).unwrap();
3239        assert_eq!(
3240            SnapshotOrigin::from_snapshot(&snap),
3241            SnapshotOrigin::ResumeBackfill,
3242            "phase 1 must pin the snapshot as resume_backfill"
3243        );
3244
3245        // Phase 2 — second resume. The snapshot now carries bound_agents, so
3246        // load_or_resolve takes the fast path and reads the persisted
3247        // `resume_backfill` origin — digests are again withheld, the legacy
3248        // key matches, and the worker is NOT run a second time.
3249        let cursor = ReplayCursor::from_entries(entries);
3250        let rc2 = RunContext::new(run_id.clone(), run_store.clone())
3251            .with_replay_store(replay_store.clone())
3252            .with_replay_cursor(Arc::new(Mutex::new(cursor)))
3253            .with_resume();
3254        let mut input2 = launch_input(blueprint.clone(), json!({ "input": "hi" }));
3255        input2.run_ctx = Some(rc2);
3256        let out2 = svc.launch(input2).await.expect("phase-2 resume launch ok");
3257        assert_eq!(out2.final_ctx["out"]["echoed"], "hi");
3258        assert_eq!(
3259            echo_calls.load(Ordering::SeqCst),
3260            1,
3261            "phase 2 must REPLAY the legacy-hashed row — the worker must not run again"
3262        );
3263        let run2 = run_store.get(&run_id).await.expect("run present");
3264        let snap2: Value = serde_json::from_str(run2.input_json.as_deref().unwrap()).unwrap();
3265        assert_eq!(
3266            SnapshotOrigin::from_snapshot(&snap2),
3267            SnapshotOrigin::ResumeBackfill,
3268            "origin must stay resume_backfill across resumes (replay key stability)"
3269        );
3270    }
3271
3272    /// [Crux D2 item 5 / D2-a] A `launch`-origin Run mixes binding digests
3273    /// into its replay key, so a legacy-hashed (digest-free) replay row does
3274    /// NOT hit — the worker runs. Proves the fix is per-Run and does not
3275    /// disable digest keying for initial launches.
3276    #[tokio::test]
3277    async fn launch_origin_run_uses_digest_keys_and_misses_legacy_replay_row() {
3278        use crate::store::replay::{InMemoryReplayStore, ReplayCursor, ReplayStore};
3279        use crate::store::run::{InMemoryRunStore, RunContext, RunStore};
3280        use std::sync::atomic::Ordering;
3281        use std::sync::Mutex;
3282
3283        let (svc, echo_calls) = counting_echo_service();
3284        let blueprint = bp(
3285            step("echo", path("$.input"), path("$.out")),
3286            vec![agent("echo", "echo")],
3287        );
3288        let run_store: Arc<dyn RunStore> = Arc::new(InMemoryRunStore::new());
3289        let replay_store: Arc<dyn ReplayStore> = Arc::new(InMemoryReplayStore::new());
3290
3291        // Produce a legacy-hashed replay row via a backfill (resume) Run.
3292        let backfill_run = seed_legacy_run(&run_store).await;
3293        let rc_bf = RunContext::new(backfill_run.clone(), run_store.clone())
3294            .with_replay_store(replay_store.clone())
3295            .with_resume();
3296        let mut input_bf = launch_input(blueprint.clone(), json!({ "input": "hi" }));
3297        input_bf.run_ctx = Some(rc_bf);
3298        svc.launch(input_bf).await.expect("backfill launch ok");
3299        assert_eq!(echo_calls.load(Ordering::SeqCst), 1);
3300        let legacy_entries = replay_store
3301            .list_by_run(&backfill_run)
3302            .await
3303            .expect("list legacy rows");
3304        assert_eq!(legacy_entries.len(), 1);
3305
3306        // A fresh, `launch`-origin Run (no `with_resume`) fed a cursor built
3307        // from those legacy rows. Its replay key mixes in the binding digest,
3308        // so the legacy (digest-free) key MISSES and the worker runs again.
3309        let launch_run = seed_legacy_run(&run_store).await;
3310        let cursor = ReplayCursor::from_entries(legacy_entries);
3311        let rc_launch = RunContext::new(launch_run.clone(), run_store.clone())
3312            .with_replay_store(replay_store.clone())
3313            .with_replay_cursor(Arc::new(Mutex::new(cursor)));
3314        let mut input_launch = launch_input(blueprint.clone(), json!({ "input": "hi" }));
3315        input_launch.run_ctx = Some(rc_launch);
3316        svc.launch(input_launch)
3317            .await
3318            .expect("launch-origin launch ok");
3319        assert_eq!(
3320            echo_calls.load(Ordering::SeqCst),
3321            2,
3322            "a launch-origin Run keys replay by binding digest, so the \
3323             legacy-hashed row must MISS and the worker must run"
3324        );
3325        let run = run_store.get(&launch_run).await.expect("run present");
3326        let snap: Value = serde_json::from_str(run.input_json.as_deref().unwrap()).unwrap();
3327        assert_eq!(SnapshotOrigin::from_snapshot(&snap), SnapshotOrigin::Launch);
3328    }
3329
3330    // ──────────────────────────────────────────────────────────────────
3331    // GH #76 error surface: TaskLaunchError::FlowEval struct variant + partial_ctx
3332    // ──────────────────────────────────────────────────────────────────
3333
3334    /// The tuple → struct variant swap must keep the Display prefix
3335    /// (`"flow eval: <msg>"`) byte-for-byte, so callers that only match on
3336    /// the stringified error keep working.
3337    #[test]
3338    fn task_launch_error_flow_eval_struct_variant_display_preserves_prefix() {
3339        let err = TaskLaunchError::FlowEval {
3340            message: "dispatcher error at ref foo".to_string(),
3341            failed_step: Some("foo".to_string()),
3342            verdict_value: Some(json!({"verdict": "BLOCKED"})),
3343            partial_ctx: Some(json!({"steps": {}})),
3344        };
3345        assert_eq!(err.to_string(), "flow eval: dispatcher error at ref foo");
3346
3347        // All-`None` shape (upstream flow-ir eval error before dispatch)
3348        // must render identically to the previous tuple form for the same
3349        // message.
3350        let err_bare = TaskLaunchError::FlowEval {
3351            message: "unresolved extern".to_string(),
3352            failed_step: None,
3353            verdict_value: None,
3354            partial_ctx: None,
3355        };
3356        assert_eq!(err_bare.to_string(), "flow eval: unresolved extern");
3357    }
3358
3359    /// End-to-end via `TaskLaunchService::launch`: a `WorkerResult { ok: false }`
3360    /// step drives the dispatcher's Blocked arm, which writes the
3361    /// `RunContext.last_failure` breadcrumb. The map_err closure lifts it
3362    /// into `TaskLaunchError::FlowEval { failed_step, verdict_value, .. }`.
3363    #[tokio::test]
3364    async fn task_launch_flow_eval_error_carries_failed_step_and_verdict_value() {
3365        use crate::store::run::{InMemoryRunStore, RunContext, RunRecord, RunStatus, RunStore};
3366        use crate::types::{RunId, TaskId};
3367
3368        let factory = RustFnInProcessSpawnerFactory::new().register_fn("gate", |_inv| async move {
3369            Ok(WorkerResult {
3370                value: json!({ "verdict": "BLOCKED", "reason": "not applicable" }),
3371                ok: false,
3372                stats: None,
3373            })
3374        });
3375        let svc = build_service(factory);
3376        let blueprint = bp(
3377            step("gate", path("$.input"), path("$.out")),
3378            vec![agent("gate", "gate")],
3379        );
3380
3381        let run_store: Arc<dyn RunStore> = Arc::new(InMemoryRunStore::new());
3382        let run_id = RunId::new();
3383        run_store
3384            .create(RunRecord {
3385                id: run_id.clone(),
3386                task_id: TaskId::new(),
3387                status: RunStatus::Running,
3388                step_entries: Vec::new(),
3389                degradations: Vec::new(),
3390                operator_sid: None,
3391                result_ref: None,
3392                input_json: Some("{}".to_string()),
3393                created_at: 0,
3394                updated_at: 0,
3395            })
3396            .await
3397            .expect("seed RunRecord");
3398
3399        let mut input = launch_input(blueprint, json!({ "input": "hi" }));
3400        input.run_ctx = Some(RunContext::new(run_id, run_store));
3401
3402        let err = svc.launch(input).await.expect_err("expected FlowEval");
3403        match err {
3404            TaskLaunchError::FlowEval {
3405                message,
3406                failed_step,
3407                verdict_value,
3408                partial_ctx,
3409            } => {
3410                assert!(
3411                    message.contains("blocked"),
3412                    "expected message to mention blocked, got: {message}"
3413                );
3414                assert_eq!(
3415                    failed_step,
3416                    Some("gate".to_string()),
3417                    "failed_step should be the Blueprint step ref, not the opaque StepId"
3418                );
3419                let vv = verdict_value.expect("verdict_value must be Some for Blocked");
3420                assert_eq!(vv["verdict"], "BLOCKED");
3421                assert_eq!(vv["reason"], "not applicable");
3422                // With a RunContext supplied, partial_ctx is always Some
3423                // (may be an empty steps map if no step_entry was appended
3424                // yet, but the reconstruction ran).
3425                assert!(
3426                    partial_ctx.is_some(),
3427                    "partial_ctx must be Some when a RunContext was supplied"
3428                );
3429            }
3430            other => panic!("expected FlowEval, got {other:?}"),
3431        }
3432    }
3433
3434    /// 3-stage chain, stage 2 blocks. The reconstructed `partial_ctx` from
3435    /// the run_store's step-entry log must include the stage 1 (passed)
3436    /// entry AND the stage 2 (blocked) entry — proving that the
3437    /// dispatcher's step-entry writes are visible to the eval-boundary
3438    /// snapshot regardless of subsequent abort.
3439    #[tokio::test]
3440    async fn task_launch_flow_eval_error_partial_ctx_reconstructs_from_run_store() {
3441        use crate::store::run::{InMemoryRunStore, RunContext, RunRecord, RunStatus, RunStore};
3442        use crate::types::{RunId, TaskId};
3443
3444        let factory = RustFnInProcessSpawnerFactory::new()
3445            .register_fn("upper", |inv| async move {
3446                Ok(WorkerResult {
3447                    value: json!(inv.prompt.to_uppercase()),
3448                    ok: true,
3449                    stats: None,
3450                })
3451            })
3452            .register_fn("gate", |_inv| async move {
3453                Ok(WorkerResult {
3454                    value: json!({ "verdict": "BLOCKED" }),
3455                    ok: false,
3456                    stats: None,
3457                })
3458            })
3459            .register_fn("never", |inv| async move {
3460                Ok(WorkerResult {
3461                    value: json!(inv.prompt),
3462                    ok: true,
3463                    stats: None,
3464                })
3465            });
3466        let svc = build_service(factory);
3467        let flow = FlowNode::Seq {
3468            children: vec![
3469                step("upper", path("$.in"), path("$.s1")),
3470                step("gate", path("$.s1"), path("$.s2")),
3471                step("never", path("$.s2"), path("$.s3")),
3472            ],
3473        };
3474        let blueprint = bp(
3475            flow,
3476            vec![
3477                agent("upper", "upper"),
3478                agent("gate", "gate"),
3479                agent("never", "never"),
3480            ],
3481        );
3482
3483        let run_store: Arc<dyn RunStore> = Arc::new(InMemoryRunStore::new());
3484        let run_id = RunId::new();
3485        run_store
3486            .create(RunRecord {
3487                id: run_id.clone(),
3488                task_id: TaskId::new(),
3489                status: RunStatus::Running,
3490                step_entries: Vec::new(),
3491                degradations: Vec::new(),
3492                operator_sid: None,
3493                result_ref: None,
3494                input_json: Some("{}".to_string()),
3495                created_at: 0,
3496                updated_at: 0,
3497            })
3498            .await
3499            .expect("seed RunRecord");
3500
3501        let mut input = launch_input(blueprint, json!({ "in": "hi" }));
3502        input.run_ctx = Some(RunContext::new(run_id.clone(), run_store.clone()));
3503
3504        let err = svc.launch(input).await.expect_err("expected FlowEval");
3505        let partial_ctx = match err {
3506            TaskLaunchError::FlowEval { partial_ctx, .. } => {
3507                partial_ctx.expect("partial_ctx must be Some")
3508            }
3509            other => panic!("expected FlowEval, got {other:?}"),
3510        };
3511        let steps = partial_ctx
3512            .get("steps")
3513            .and_then(|v| v.as_object())
3514            .expect("partial_ctx.steps object");
3515        // stage 1 (upper) passed + stage 2 (gate) blocked: two entries.
3516        // stage 3 (never) is never dispatched because flow-ir stops after
3517        // the Blocked arm's `EvalError::DispatcherError`.
3518        assert_eq!(
3519            steps.len(),
3520            2,
3521            "expected 2 step_entries (upper passed + gate blocked), got: {steps:?}"
3522        );
3523        let mut status_by_ref: HashMap<String, String> = HashMap::new();
3524        for (_step_id, entry) in steps {
3525            let step_ref = entry
3526                .get("step_ref")
3527                .and_then(|v| v.as_str())
3528                .expect("step_ref present")
3529                .to_string();
3530            let status = entry
3531                .get("status")
3532                .and_then(|v| v.as_str())
3533                .expect("status present")
3534                .to_string();
3535            status_by_ref.insert(step_ref, status);
3536        }
3537        assert_eq!(
3538            status_by_ref.get("upper").map(String::as_str),
3539            Some("passed")
3540        );
3541        assert_eq!(
3542            status_by_ref.get("gate").map(String::as_str),
3543            Some("blocked")
3544        );
3545        assert!(
3546            !status_by_ref.contains_key("never"),
3547            "step 'never' must not appear — flow-ir stops dispatching after Blocked abort"
3548        );
3549    }
3550
3551    /// When `TaskLaunchService::launch` is called WITHOUT a `RunContext`
3552    /// (the legacy path), the map_err closure must still return a
3553    /// well-formed `FlowEval` — every new field is `None` because there is
3554    /// no breadcrumb source nor snapshot source. Regression test for the
3555    /// null-object path.
3556    #[tokio::test]
3557    async fn task_launch_flow_eval_error_without_run_ctx_has_none_fields() {
3558        let factory = RustFnInProcessSpawnerFactory::new().register_fn("gate", |_inv| async move {
3559            Ok(WorkerResult {
3560                value: json!({ "verdict": "BLOCKED" }),
3561                ok: false,
3562                stats: None,
3563            })
3564        });
3565        let svc = build_service(factory);
3566        let blueprint = bp(
3567            step("gate", path("$.input"), path("$.out")),
3568            vec![agent("gate", "gate")],
3569        );
3570        let err = svc
3571            .launch(launch_input(blueprint, json!({ "input": "hi" })))
3572            .await
3573            .expect_err("expected FlowEval");
3574        match err {
3575            TaskLaunchError::FlowEval {
3576                failed_step,
3577                verdict_value,
3578                partial_ctx,
3579                ..
3580            } => {
3581                assert_eq!(
3582                    failed_step, None,
3583                    "failed_step must be None without run_ctx"
3584                );
3585                assert_eq!(
3586                    verdict_value, None,
3587                    "verdict_value must be None without run_ctx"
3588                );
3589                assert_eq!(
3590                    partial_ctx, None,
3591                    "partial_ctx must be None without run_ctx"
3592                );
3593            }
3594            other => panic!("expected FlowEval, got {other:?}"),
3595        }
3596    }
3597}