Skip to main content

mlua_swarm_schema/
lib.rs

1//! Blueprint schema — Swarm IF SoT (= the core type set that defines "how a Blueprint object is written").
2//!
3//! This crate provides **schema types + serde derives only** as a pure IF crate. Execution
4//! layers (SpawnerFactory / EngineDispatcher / Compiler) are not included here; consumers
5//! (the `mlua-swarm` crate) own them. External consumers, sibling worktrees, and
6//! future bundles can read/write Blueprints by depending on this single crate.
7//!
8//! # Versioning contract
9//!
10//! `Blueprint.schema_version` is tied to this crate's semver. It is fixed at 0.1.0 for now;
11//! during 0.x breaking changes are free, and 1.0 will freeze the schema.
12//!
13//! # IN-immutability (extension discipline)
14//!
15//! This crate is the IN side of the swarm layering and stays **plain serde
16//! data**: no compile pass, no field the engine macro-expands, no DSL
17//! dialect. Flow conds are written literally against the Flow.ir Expr set
18//! (`Eq($.<step>.verdict, Lit("blocked"))` — domain verdicts are plain
19//! strings in step output). Authoring sugar (builders) lives OUT on the
20//! consumer side; runtime behavior extension lives in the engine's
21//! `SpawnerLayer` middleware.
22//!
23//! # AgentKind handling (= internal SoT)
24//!
25//! [`AgentKind`] is the SoT for the SpawnerAdapter offering axis. It is a closed enum managed
26//! inside Swarm, extended by variant addition through **explicit maintenance**. String lookup
27//! or a `Custom` escape hatch is deliberately avoided (= structurally eliminates the "silly
28//! runtime typos" class of failures).
29//!
30//! # Examples
31//!
32//! Build a minimal [`Blueprint`] with a single [`AgentDef`] via struct literal:
33//!
34//! ```
35//! use mlua_swarm_schema::{
36//!     AgentDef, AgentKind, Blueprint, current_schema_version,
37//! };
38//! use mlua_flow_ir::{Expr, Node};
39//! use serde_json::json;
40//!
41//! let bp = Blueprint {
42//!     schema_version: current_schema_version(),
43//!     id: "hello".into(),
44//!     flow: Node::Step {
45//!         ref_: "greeter".into(),
46//!         in_: Expr::Lit { value: json!({"name": "world"}) },
47//!         out: Expr::Path { at: "$.greeting".into() },
48//!     },
49//!     agents: vec![AgentDef {
50//!         name: "greeter".into(),
51//!         kind: AgentKind::RustFn,
52//!         spec: json!({"fn_id": "hello_world"}),
53//!         profile: None,
54//!         meta: None,
55//!     }],
56//!     operators: vec![],
57//!     hints: Default::default(),
58//!     strategy: Default::default(),
59//!     metadata: Default::default(),
60//!     spawner_hints: Default::default(),
61//!     default_agent_kind: AgentKind::Operator,
62//!     default_operator_kind: None,
63//! };
64//!
65//! assert_eq!(bp.id, "hello");
66//! assert_eq!(bp.agents.len(), 1);
67//! assert_eq!(bp.strategy.strict_refs, true);
68//! ```
69//!
70//! Round-trip a [`Blueprint`] through JSON (= confirms `serde` derives and the
71//! `deny_unknown_fields` contract):
72//!
73//! ```
74//! use mlua_swarm_schema::{AgentKind, Blueprint, BlueprintMetadata};
75//! use mlua_flow_ir::{Expr, Node};
76//! use serde_json::json;
77//!
78//! let bp = Blueprint {
79//!     schema_version: mlua_swarm_schema::current_schema_version(),
80//!     id: "roundtrip".into(),
81//!     flow: Node::Seq { children: vec![] },
82//!     agents: vec![],
83//!     operators: vec![],
84//!     hints: Default::default(),
85//!     strategy: Default::default(),
86//!     metadata: BlueprintMetadata {
87//!         description: Some("roundtrip smoke".into()),
88//!         default_run_ttl_secs: Some(1800),
89//!         ..Default::default()
90//!     },
91//!     spawner_hints: Default::default(),
92//!     default_agent_kind: AgentKind::Operator,
93//!     default_operator_kind: None,
94//! };
95//!
96//! let json = serde_json::to_string(&bp).unwrap();
97//! let back: Blueprint = serde_json::from_str(&json).unwrap();
98//! assert_eq!(bp, back);
99//! assert_eq!(back.metadata.default_run_ttl_secs, Some(1800));
100//! ```
101
102#![warn(missing_docs)]
103
104use mlua_flow_ir::Node as FlowNode;
105use schemars::JsonSchema;
106use serde::{Deserialize, Serialize};
107use serde_json::Value;
108use std::collections::HashMap;
109
110// ──────────────────────────────────────────────────────────────────────────
111// Versioning
112// ──────────────────────────────────────────────────────────────────────────
113
114/// Current Blueprint schema version. Tied to this crate's semver.
115pub const CURRENT_SCHEMA_VERSION: &str = "0.1.0";
116
117fn default_schema_version() -> semver::Version {
118    current_schema_version()
119}
120
121/// Blueprint construction helper: returns the semver of the current schema version.
122/// Callers can write `schema_version: current_schema_version(),`.
123pub fn current_schema_version() -> semver::Version {
124    semver::Version::parse(CURRENT_SCHEMA_VERSION)
125        .expect("CURRENT_SCHEMA_VERSION must be valid semver")
126}
127
128// ──────────────────────────────────────────────────────────────────────────
129// Blueprint (top-level package)
130// ──────────────────────────────────────────────────────────────────────────
131
132/// Unified package of flow.ir + Swarm extension layers. The entry-point type of Swarm.
133#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, JsonSchema)]
134#[serde(deny_unknown_fields)]
135pub struct Blueprint {
136    /// Schema version (= tied to this crate's semver). Default = `CURRENT_SCHEMA_VERSION`.
137    /// Serialized as a semver string (e.g. `"0.1.0"`).
138    #[serde(default = "default_schema_version")]
139    #[schemars(with = "String")]
140    pub schema_version: semver::Version,
141    /// Blueprint identifier (= unique key within the caller's namespace).
142    pub id: String,
143    /// Embeds the flow.ir Node verbatim (= keeps flow.ir side unpolluted).
144    /// Opaque in the JSON Schema (the Node shape is owned by the `mlua-flow-ir`
145    /// crate, a separate repo; see its docs for the Node / Expr grammar).
146    #[schemars(with = "Value")]
147    pub flow: FlowNode,
148    /// Swarm extension layer: agent → backend mapping.
149    #[serde(default)]
150    pub agents: Vec<AgentDef>,
151    /// Swarm extension layer: **design-time definition** of Operator roles (first-class).
152    ///
153    /// `AgentDef.spec.operator_ref` references an `OperatorDef.name` (logical role name) in
154    /// this vec. Embedding runtime-generated IDs such as sid into the BP is forbidden
155    /// (= collapses the design-time vs runtime boundary). Runtime backend bindings are
156    /// established via the attach / register path; the BP side holds only logical names.
157    ///
158    /// Every `kind = Operator` agent must have its `spec.operator_ref` present in this
159    /// list — the compiler validates it at `compile()` time. May be `[]` only when the
160    /// Blueprint declares no Operator agents.
161    #[serde(default)]
162    pub operators: Vec<OperatorDef>,
163    /// Swarm extension layer: per-agent hints (interpreted by the Compiler).
164    #[serde(default)]
165    pub hints: CompilerHints,
166    /// Swarm extension layer: Compiler behavior strategy (strict / lenient).
167    #[serde(default)]
168    pub strategy: CompilerStrategy,
169    /// Blueprint metadata (description / origin / tags / ttl / version label / alias).
170    #[serde(default)]
171    pub metadata: BlueprintMetadata,
172    /// Swarm extension layer: hint keys of the layers to wrap around the SpawnerStack.
173    /// Resolved by the LayerRegistry at engine bind time (= unregistered keys are silently
174    /// skipped). Flow / Blueprint do not hold middleware implementations (e.g. MainAIMiddleware)
175    /// directly; they only declare required capabilities as string keys (= implementations
176    /// live in the engine-side LayerRegistry).
177    #[serde(default)]
178    pub spawner_hints: SpawnerHints,
179    /// BP-wide default `AgentKind` (= fallback when `AgentDef.kind` is omitted).
180    /// Four-layer cascade: (1) Schema impl Default = Operator, (2) CLI
181    /// `--default-agent-kind`, (3) this field (BP JSON literal), (4) `AgentDef.kind`
182    /// (per-agent literal). (5) `CompilerHints.kind_override` allows runtime override.
183    /// All default resolution flows through this path.
184    #[serde(default = "default_global_agent_kind")]
185    pub default_agent_kind: AgentKind,
186    /// BP-wide default `OperatorKind` (= the "BP Global" tier of the 4-tier
187    /// `OperatorKind` cascade). `None` when the Blueprint author does not
188    /// declare a default; the caller-side resolver then falls through to
189    /// the hardcoded `OperatorKind::default()` (Automate).
190    ///
191    /// # 4-tier cascade (highest to lowest priority)
192    ///
193    /// 1. Runtime Agent-level (per-agent override supplied at task-launch time)
194    /// 2. Runtime Global (the launch-time `operator_kind` request)
195    /// 3. BP Agent-level (`OperatorDef.kind`, resolved via `AgentDef.spec.operator_ref`)
196    /// 4. BP Global (this field)
197    /// 5. Default Fallback (`OperatorKind::default()` = Automate)
198    ///
199    /// The collapse itself is implemented once on the engine side and consumed
200    /// per-agent when resolving operator info.
201    #[serde(default, skip_serializing_if = "Option::is_none")]
202    pub default_operator_kind: Option<OperatorKind>,
203}
204
205/// Global default `AgentKind` at the Schema impl Default layer. Bottom of the 4-layer cascade.
206pub fn default_global_agent_kind() -> AgentKind {
207    AgentKind::Operator
208}
209
210/// Set of **capability hint keys** for the SpawnerLayer required by a Blueprint.
211///
212/// # Design rationale (= for the person who will reconstruct this later)
213///
214/// A Blueprint is a pure layer of flow.ir + agent name binding and holds no middleware
215/// **implementation**. Nevertheless there are cases where the caller must be told the BP
216/// needs certain **capabilities** — e.g. "MainAI hook required", "Operator delegate path
217/// required", operator role mode switching, presence/absence of senior escalation, and
218/// so on.
219///
220/// `spawner_hints.layers` is the place where those capabilities are declared as **string
221/// keys**. The engine-side `LayerRegistry` (= consumer crate) resolves key → factory and
222/// wraps the compiled routes with a `SpawnerStack`. The Blueprint does not import the
223/// concrete `MainAIMiddleware` type; it exposes intent through strings such as `"main_ai"`
224/// (= separates the pure Flow layer from implementation details).
225///
226/// # Canonical hint keys
227///
228/// - `"main_ai"` → `MainAIMiddleware` (= fires SpawnHook before/after when kind is MainAi/Composite)
229/// - `"senior_escalation"` → `SeniorEscalationMiddleware` (= fires SeniorBridge.ask on worker ok=false)
230/// - `"operator_delegate"` → `OperatorDelegateMiddleware` (= delegates the entire spawn to an external Operator.execute)
231///
232/// # Behavior of unregistered keys
233///
234/// If the engine-side LayerRegistry has no matching factory, the key is **silently skipped**
235/// (= lenient default). This preserves Blueprint portability (= an unsupported capability in
236/// another deployment falls back gracefully).
237#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, JsonSchema)]
238#[serde(deny_unknown_fields)]
239pub struct SpawnerHints {
240    /// Ordered list of layer hint keys to wrap around the SpawnerStack.
241    #[serde(default)]
242    pub layers: Vec<String>,
243}
244
245// ──────────────────────────────────────────────────────────────────────────
246// AgentDef / AgentKind / AgentProfile / AgentMeta
247// ──────────────────────────────────────────────────────────────────────────
248
249/// Maps an agent name to a Worker IMPL kind and its configuration. Referenced from flow.ir
250/// `Step.ref` by name.
251///
252/// # Design
253///
254/// `AgentDef.kind` directly expresses the **Worker IMPL axis** (= not the old Spawner axis).
255/// Dispatching to a host Spawner adapter (`InProcSpawner` / `ProcessSpawner` /
256/// `OperatorSpawner`) is done by an internal Resolver on the compiler side. The design goal
257/// is "do not make the caller aware of which Spawner hosts the Worker IMPL"; the caller
258/// (Blueprint author) sees only the WorkerIMPL viewpoint.
259///
260/// A Spawner-axis hint (= "which adapter would you prefer running this Worker on", as a
261/// priority list) will be added via a future `spawner_hint: Vec<Spawner>` field as a carry.
262/// The current internal Resolver is a fixed 1:1 mapping, so the field is unnecessary today.
263#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, JsonSchema)]
264#[serde(deny_unknown_fields)]
265pub struct AgentDef {
266    /// Agent name (= referenced from flow.ir `Step.ref`).
267    pub name: String,
268    /// Worker IMPL kind (= see [`AgentKind`]).
269    pub kind: AgentKind,
270    /// Free-form schema per kind. Interpreted by the SpawnerFactory.
271    #[serde(default)]
272    pub spec: Value,
273    /// Agent persona information (system_prompt / model / tools, etc.). Orthogonal to the
274    /// backend kind and is a first-class field. Expected to be populated by
275    /// `agent_md_loader` from the frontmatter + body of an `agent.md`. `None` = an agent
276    /// without a profile (= backend built solely from `spec`).
277    #[serde(default)]
278    pub profile: Option<AgentProfile>,
279    /// Agent-level metadata (description / version / tags).
280    #[serde(default)]
281    pub meta: Option<AgentMeta>,
282}
283
284/// Agent persona information. Orthogonal to the backend kind (Shell / InProc / Operator).
285///
286/// Populated by `agent_md_loader::load_dir` from the frontmatter and Markdown body of
287/// `agents/*.md` in agent-profiles. The backend (e.g. AgentBlockOperator) receives this
288/// struct at construction / dispatch time and consumes `system_prompt` as the LLM API
289/// system message and `model` / `tools` as configuration.
290///
291/// C-C-specific fields (`permissionMode` / `memory` / `abtest`, etc.) are dumped into
292/// `extras: Value`, and consumers that need them read them out. This is the escape hatch
293/// that keeps the schema future-proof rather than making it strict.
294#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default, JsonSchema)]
295#[serde(deny_unknown_fields)]
296pub struct AgentProfile {
297    /// Markdown body (= system prompt content).
298    #[serde(default)]
299    pub system_prompt: String,
300    /// LLM model identifier (e.g. `"sonnet"` / `"haiku"` / `"opus"`).
301    #[serde(default)]
302    pub model: Option<String>,
303    /// Reasoning effort (e.g. `"low"` / `"medium"` / `"high"`).
304    #[serde(default)]
305    pub effort: Option<String>,
306    /// List of available tool names (normalized from the CSV form in frontmatter).
307    #[serde(default)]
308    pub tools: Vec<String>,
309    /// Frontmatter `description`. A short one-line description.
310    #[serde(default)]
311    pub description: Option<String>,
312    /// C-C-specific / future-proof fields (permissionMode / memory / abtest / ...).
313    /// Shape is the leftover keys of the agent.md frontmatter dumped as a JSON object.
314    #[serde(default)]
315    pub extras: Value,
316    /// Content hash (blake3 32-byte hex) of the agent body (= `system_prompt`).
317    ///
318    /// # Purpose
319    ///
320    /// When the Enhance loop receives a Patch that replaces
321    /// `/agents/N/profile/system_prompt`, the post-hook in `patch_applier.lua`
322    /// recomputes this field (= new blake3 of the body) and updates it automatically.
323    /// This is the field that structurally prevents a Blueprint carrying a stale hash
324    /// from being committed.
325    ///
326    /// - `None` = hash not computed (= manually built agent, or a Blueprint predating this field)
327    /// - `Some(hex)` = latest hash at agent-profiles seed time or after PatchApplier
328    ///
329    /// Planned to be used as the cache-index key in `AgentStore`.
330    #[serde(default)]
331    pub version_hash: Option<String>,
332    /// Claude Code SubAgent definition name this agent binds to at spawn
333    /// time (e.g. "mse-worker-coder"). Why: the Blueprint is the single
334    /// source of truth for the declaration↔executor binding — an external
335    /// registry would duplicate what `tools` already declares and drift.
336    /// `None` is valid for agents whose operator backend never dispatches
337    /// a SubAgent (direct-LLM operators); WS thin-path operators require
338    /// it at compile time (see `Operator::requires_worker_binding`).
339    #[serde(default, skip_serializing_if = "Option::is_none")]
340    pub worker_binding: Option<String>,
341}
342
343/// SoT of the **Worker IMPL axis**. A closed enum managed inside Swarm and extended by
344/// variant addition through **explicit maintenance**. String lookup / escape hatches are
345/// deliberately not adopted.
346///
347/// This enum **expresses Worker IMPL directly**; dispatching to a host Spawner adapter is
348/// resolved by an internal Resolver on the compiler side (= callers see only the Worker
349/// IMPL viewpoint).
350///
351/// # Internal Resolver mapping (= currently a fixed 1:1, carry: priority list form)
352///
353/// | AgentKind | Host Spawner adapter |
354/// |---|---|
355/// | `Lua` | `InProcSpawner` (mlua VM eval) |
356/// | `RustFn` | `InProcSpawner` (Rust closure) |
357/// | `AgentBlock` | `InProcSpawner` (agent-block-core SDK in-process) |
358/// | `Subprocess` | `ProcessSpawner` (child process launch) |
359/// | `Operator` | `OperatorSpawner` (interactive role / Human-MainAI delegation) |
360#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash, JsonSchema)]
361#[serde(rename_all = "snake_case")]
362pub enum AgentKind {
363    /// Lua script eval through the mlua VM (= factory-side registry looked up by `spec.fn_id`).
364    Lua,
365    /// Rust closure (= factory-side registry looked up by `spec.fn_id`).
366    RustFn,
367    /// Headless LLM agent via the agent-block-core SDK (in-process).
368    AgentBlock,
369    /// Child-process launch (= `spec.program` + `args`, via the ProcessSpawner path).
370    Subprocess,
371    /// Interactive Operator role (= MainAI / Human delegation, `spec.operator_ref`).
372    Operator,
373}
374
375// ──────────────────────────────────────────────────────────────────────────
376// OperatorDef / OperatorKind
377// ──────────────────────────────────────────────────────────────────────────
378
379/// Kind axis of an Operator role (= "in which mode does this Operator run").
380/// Corresponds 1:1 with the engine's runtime `OperatorKind`. Kept as a schema
381/// duplicate so that BPs can be authored while depending only on this crate.
382#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default, JsonSchema)]
383#[serde(rename_all = "snake_case")]
384pub enum OperatorKind {
385    /// MainAI (= interactive AI Operator via WS client or SDK).
386    MainAi,
387    /// Automate (= normal spawn path, without human interception).
388    #[default]
389    Automate,
390    /// Composite (= MainAi + Automate running side by side).
391    Composite,
392}
393
394/// Design-time definition of an Operator role (first-class).
395///
396/// `AgentDef.spec.operator_ref` references this struct's `name` as a logical role name.
397/// Binding to a runtime backend (WS session / SDK / pool, etc.) is established via the
398/// attach path; the BP side only declares "under this logical name we expect an Operator
399/// of this Kind".
400///
401/// `spec` is an escape hatch for kind-specific config (WS endpoint / SDK profile / pool
402/// binding, etc.). Even when empty, declaring `name` + `kind` alone is enough for
403/// compile-time validation to succeed (= it guarantees that agent `operator_ref` values
404/// reference an existing definition).
405#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, JsonSchema)]
406#[serde(deny_unknown_fields)]
407pub struct OperatorDef {
408    /// Logical role name (= design-time symbol referenced from `AgentDef.spec.operator_ref`).
409    pub name: String,
410    /// Display name for UI / docs (optional).
411    #[serde(default)]
412    pub display_name: Option<String>,
413    /// Kind axis of the Operator (MainAi / Automate / Composite) — the "BP
414    /// Agent-level" tier of the 4-tier `OperatorKind` cascade (see
415    /// `Blueprint.default_operator_kind` for the full tier list). `None`
416    /// when this `OperatorDef` does not declare a kind; the resolver then
417    /// falls through to BP Global / Default Fallback for agents referencing
418    /// this role via `AgentDef.spec.operator_ref`.
419    #[serde(default)]
420    pub kind: Option<OperatorKind>,
421    /// Kind-specific config (WS endpoint / SDK profile / pool binding, etc.). Interpreted
422    /// by the factory.
423    #[serde(default)]
424    pub spec: Value,
425    /// Operator persona information (e.g. system_prompt template). Same shape as
426    /// `AgentDef.profile`. Used as a template when the Operator itself plays a "role".
427    /// If `None`, the agent-side profile is used instead.
428    #[serde(default)]
429    pub profile: Option<AgentProfile>,
430    /// Operator-level metadata (description / version / tags).
431    #[serde(default)]
432    pub meta: Option<AgentMeta>,
433}
434
435/// Agent / Operator level metadata (description / version / tags).
436#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default, JsonSchema)]
437#[serde(deny_unknown_fields)]
438pub struct AgentMeta {
439    /// Short human-readable description.
440    #[serde(default)]
441    pub description: Option<String>,
442    /// Free-form version label.
443    #[serde(default)]
444    pub version: Option<String>,
445    /// Tag list for classification / routing.
446    #[serde(default)]
447    pub tags: Vec<String>,
448}
449
450// ──────────────────────────────────────────────────────────────────────────
451// Compiler hints / strategy
452// ──────────────────────────────────────────────────────────────────────────
453
454/// Per-agent overrides / hints. Interpreted by the Compiler / SpawnerFactory; not required.
455#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default, JsonSchema)]
456#[serde(deny_unknown_fields)]
457pub struct CompilerHints {
458    /// Agent name → per-agent hint (= passed to `SpawnerFactory.build`).
459    #[serde(default)]
460    pub per_agent: HashMap<String, Value>,
461    /// Global hints (= e.g. parallel limit, default timeout, ...).
462    #[serde(default)]
463    pub global: Value,
464}
465
466/// Compiler behavior rules. Controls strict / lenient handling and default fallback.
467#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, JsonSchema)]
468#[serde(deny_unknown_fields)]
469pub struct CompilerStrategy {
470    /// If `true` (default), an unresolved `Step.ref` is an error; if `false`, it falls
471    /// through to the default Spawner.
472    #[serde(default = "default_true")]
473    pub strict_refs: bool,
474    /// If `true` (default), an `AgentKind` missing from the registry is an error; if
475    /// `false`, it is skipped.
476    #[serde(default = "default_true")]
477    pub strict_kind: bool,
478}
479
480fn default_true() -> bool {
481    true
482}
483
484impl Default for CompilerStrategy {
485    fn default() -> Self {
486        Self {
487            strict_refs: true,
488            strict_kind: true,
489        }
490    }
491}
492
493// ──────────────────────────────────────────────────────────────────────────
494// Blueprint metadata / origin
495// ──────────────────────────────────────────────────────────────────────────
496
497/// Blueprint-level metadata (description / origin / tags / ttl / version label / alias).
498#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default, JsonSchema)]
499#[serde(deny_unknown_fields)]
500pub struct BlueprintMetadata {
501    /// Short human-readable description of the Blueprint.
502    #[serde(default)]
503    pub description: Option<String>,
504    /// Provenance record (inline / file / algocline).
505    #[serde(default)]
506    pub origin: BlueprintOrigin,
507    /// Tag list for classification / routing.
508    #[serde(default)]
509    pub tags: Vec<String>,
510    /// Optional SemVer label (= match target for `TaskPipeline VersionSelector::SemVerReq`).
511    /// Example: `"1.2.3"`. Rewritten by `EnhanceAdapter` on PATCH/MINOR/MAJOR bumps.
512    #[serde(default, skip_serializing_if = "Option::is_none")]
513    pub version_label: Option<String>,
514    /// Optional LDS session alias label. The Swarm engine itself does not apply this
515    /// (= it is free-form content); the value is expanded into the Spawn directive and
516    /// reaches the MainAI. The MainAI is expected to establish a task session via
517    /// `mcp__lds__session_create(root=..., alias=<this>)`, and to inject
518    /// `LDS Session Alias: <this>` verbatim into the SubAgent dispatch prompt body.
519    /// The SubAgent body then calls `mcp__lds__session_start(alias=<this>)` with the
520    /// received alias. Worktree ownership is thereby unified under a single session, and
521    /// cross-SubAgent / cross-worktree ownership blocks (= `not owned by this session`)
522    /// cannot fire structurally.
523    #[serde(default, skip_serializing_if = "Option::is_none")]
524    pub project_name_alias: Option<String>,
525    /// Optional default TTL (seconds) for tasks dispatched via this BP. Estimated by the
526    /// Blueprint author from the flow shape (agent count × expected duration per agent).
527    /// If `POST /v1/tasks` supplies `ttl_secs` explicitly, the body value wins; otherwise
528    /// this metadata field is used as the default; if both are absent, the server global
529    /// default (`default_run_ttl()` = 1800s) applies. Not needed for short chains (~5 min);
530    /// recommended for long chains (14 agents × several minutes = 30-60 min).
531    #[serde(default, skip_serializing_if = "Option::is_none")]
532    pub default_run_ttl_secs: Option<u64>,
533}
534
535/// Provenance record of a Blueprint.
536#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default, JsonSchema)]
537#[serde(tag = "kind", rename_all = "snake_case")]
538pub enum BlueprintOrigin {
539    /// Inline construction, e.g. via a Rust struct literal or test code.
540    #[default]
541    Inline,
542    /// Loaded from a file.
543    File {
544        /// Source file path.
545        path: String,
546    },
547    /// Emitted by an algocline strategy (traced by `session_id`).
548    Algo {
549        /// Algocline session identifier.
550        session_id: String,
551    },
552}
553
554#[cfg(test)]
555mod tests {
556    use super::*;
557
558    #[test]
559    fn schema_version_default_parses() {
560        let v = default_schema_version();
561        assert_eq!(v.to_string(), "0.1.0");
562    }
563
564    #[test]
565    fn current_schema_version_const_matches() {
566        assert_eq!(CURRENT_SCHEMA_VERSION, "0.1.0");
567    }
568
569    #[test]
570    fn blueprint_json_schema_exports_key_properties() {
571        let schema = schemars::schema_for!(Blueprint);
572        let v = serde_json::to_value(&schema).expect("schema serializes");
573        let props = v["properties"].as_object().expect("object schema");
574        for key in [
575            "schema_version",
576            "id",
577            "flow",
578            "agents",
579            "operators",
580            "hints",
581            "strategy",
582            "metadata",
583            "spawner_hints",
584            "default_agent_kind",
585            "default_operator_kind",
586        ] {
587            assert!(props.contains_key(key), "missing property: {key}");
588        }
589        // semver override lands as a plain string
590        assert_eq!(v["properties"]["schema_version"]["type"], "string");
591        // enum variants (snake_case) survive into the schema (LLM author axis)
592        let dump = v.to_string();
593        assert!(dump.contains("agent_block"), "AgentKind variants in schema");
594        assert!(dump.contains("main_ai"), "OperatorKind variants in schema");
595        // nested defs are referenced (AgentDef reachable from agents[])
596        assert!(dump.contains("AgentDef"), "AgentDef definition in schema");
597    }
598
599    #[test]
600    fn agent_profile_worker_binding_roundtrips_when_some() {
601        let profile = AgentProfile {
602            worker_binding: Some("mse-worker-coder".to_string()),
603            ..Default::default()
604        };
605        let json = serde_json::to_value(&profile).expect("serializes");
606        assert_eq!(json["worker_binding"], "mse-worker-coder");
607        let back: AgentProfile = serde_json::from_value(json).expect("deserializes");
608        assert_eq!(back.worker_binding.as_deref(), Some("mse-worker-coder"));
609    }
610
611    #[test]
612    fn agent_profile_worker_binding_omitted_when_none() {
613        let profile = AgentProfile::default();
614        let json = serde_json::to_value(&profile).expect("serializes");
615        // `skip_serializing_if = "Option::is_none"` — the key must not appear at all.
616        assert!(
617            json.as_object().unwrap().get("worker_binding").is_none(),
618            "worker_binding key must be absent when None: {json}"
619        );
620        let back: AgentProfile = serde_json::from_value(json).expect("deserializes");
621        assert_eq!(back.worker_binding, None);
622    }
623}