meerkat-mob 0.8.1

Multi-agent orchestration runtime for Meerkat
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
//! Profile and tool configuration for mob members.
//!
//! A `Profile` defines the template for spawning a member: which model to use,
//! which skills to load, tool configuration, and communication settings.

use crate::backend::MobBackendKind;
use crate::runtime_mode::MobRuntimeMode;
use serde::{Deserialize, Serialize};

/// Tool configuration for a mob member profile.
///
/// Controls which tool categories are enabled for members spawned
/// from this profile.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct ToolConfig {
    /// Enable built-in tools (file read, etc.).
    #[serde(default)]
    pub builtins: bool,
    /// Enable shell execution tool.
    #[serde(default)]
    pub shell: bool,
    /// Enable comms tools (peer messaging).
    #[serde(default)]
    pub comms: bool,
    /// Enable memory/semantic search tools.
    #[serde(default)]
    pub memory: bool,
    /// Expose the realm-scoped WorkGraph commitment tools to this member.
    #[serde(default)]
    pub workgraph: bool,
    /// Enable mob management tools (spawn, retire, wire, unwire, list).
    #[serde(default)]
    pub mob: bool,
    /// Enable schedule tools (create, list, update, pause, resume, delete).
    #[serde(default)]
    pub schedule: bool,
    /// Enable assistant image generation tools.
    #[serde(default)]
    pub image_generation: bool,
    /// MCP server names this profile connects to.
    #[serde(default)]
    pub mcp: Vec<String>,
    /// Declarative MCP server configs for members built from this profile.
    ///
    /// Durable on the profile (unlike the in-process-only per-spawn
    /// `SpawnMemberSpec.external_tools` overlay), so revival recomposes the
    /// member's MCP tools from the same profile that built it. These are
    /// already profile-scoped; the `mcp` name allowlist above gates only the
    /// mob-wide default external-tools provider (where an empty list means
    /// the full host surface).
    #[serde(default)]
    pub mcp_servers: Vec<meerkat_core::mcp_config::McpServerConfig>,
    /// Named Rust tool bundles wired by the mob runtime.
    ///
    /// String names referencing `Arc<dyn AgentToolDispatcher>` instances
    /// registered at mob construction time. Not serializable — must be
    /// re-registered on resume.
    #[serde(default)]
    pub rust_bundles: Vec<String>,
}

/// Binding for a profile in a mob definition.
///
/// Profiles can be defined inline (the existing behavior) or reference
/// a reusable realm-scoped profile by name.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(untagged)]
pub enum ProfileBinding {
    /// Reference to a realm-scoped profile by name.
    /// Must be listed before `Inline` for correct untagged deserialization
    /// (a `{"realm_profile":"x"}` object must not be consumed as an `Inline` variant).
    RealmRef {
        /// Name of the realm profile to reference.
        realm_profile: String,
    },
    /// Inline profile definition (original behavior).
    /// Boxed: `Profile` is large; keeps the untagged wire shape unchanged.
    Inline(Box<Profile>),
}

impl ProfileBinding {
    /// Returns the inline profile if this is an `Inline` binding.
    pub fn as_inline(&self) -> Option<&Profile> {
        match self {
            Self::Inline(p) => Some(p),
            Self::RealmRef { .. } => None,
        }
    }

    /// Returns a mutable reference to the inline profile.
    pub fn as_inline_mut(&mut self) -> Option<&mut Profile> {
        match self {
            Self::Inline(p) => Some(p),
            Self::RealmRef { .. } => None,
        }
    }

    /// Returns the realm profile name if this is a `RealmRef` binding.
    pub fn realm_ref_name(&self) -> Option<&str> {
        match self {
            Self::RealmRef { realm_profile } => Some(realm_profile),
            Self::Inline(_) => None,
        }
    }
}

/// Agent-owned spawn tooling mode for child members.
///
/// Controls how the child's tool surface is determined at spawn time.
/// External/public spawn remains role-based; this enum is for agent-owned spawns.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(tag = "mode", rename_all = "snake_case")]
pub enum SpawnTooling {
    /// Inherit the parent's currently visible tools (ToolScope snapshot).
    InheritParent {
        /// Optional allow-list overlay: narrows the inherited set to only these tools.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        allow_overlay: Option<Vec<String>>,
        /// Optional deny-list overlay: removes these tools from the inherited set.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        deny_overlay: Option<Vec<String>>,
    },
    /// Minimal: only comms tools (send_message, send_request, send_response, peers).
    Minimal,
    /// Use a specific profile for model/tool resolution.
    Profile {
        /// Source of the profile.
        source: Box<ProfileSource>,
        /// Optional allow-list overlay.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        allow_overlay: Option<Vec<String>>,
        /// Optional deny-list overlay.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        deny_overlay: Option<Vec<String>>,
    },
}

/// Source of a profile for spawn tooling resolution.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ProfileSource {
    /// Reference a realm-scoped reusable profile by name.
    RealmProfile {
        /// Name of the realm profile.
        name: String,
    },
    /// Inline profile definition.
    /// Boxed: `Profile` is large; keeps the tagged wire shape unchanged.
    Inline(Box<Profile>),
}

/// Profile template for spawning mob members.
///
/// Each profile defines the model, skills, tool configuration, and
/// communication properties for a class of mob members.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct Profile {
    /// LLM model name (e.g. "claude-opus-4-8").
    pub model: String,
    /// Explicit typed provider for this profile's model.
    ///
    /// Parsed fail-closed at profile ingress into the closed
    /// [`meerkat_core::Provider`] vocabulary (unknown names reject the
    /// profile). Required for uncatalogued model ids that no registry entry
    /// owns; for catalogued ids the registry rejects a conflicting owner at
    /// build time.
    #[serde(default)]
    pub provider: Option<meerkat_core::Provider>,
    /// Durable self-hosted server binding for configured self-hosted aliases.
    ///
    /// Only meaningful together with `provider = "self_hosted"` and a
    /// `[self_hosted.models]` entry in the host config.
    #[serde(default)]
    pub self_hosted_server_id: Option<String>,
    /// Configured default provider for `Auto` image-generation targets.
    ///
    /// Overrides the mob-level default. When neither is set, `Auto` resolves
    /// via the session's effective text provider.
    #[serde(default)]
    pub image_generation_provider: Option<meerkat_core::Provider>,
    /// Per-profile auto-compaction threshold override (tokens, non-zero).
    ///
    /// `NonZeroU64` fails closed at ingress: a zero threshold rejects the
    /// profile instead of silently disabling compaction. When set, this wins
    /// over the global config knob and model-aware context-window scaling.
    #[serde(default)]
    pub auto_compact_threshold: Option<std::num::NonZeroU64>,
    /// Profile fields that win over durable session metadata on resume.
    ///
    /// Surfaces the typed [`meerkat_core::service::ResumeOverrideMask`]: a
    /// listed field is re-applied from the (possibly updated) profile when a
    /// durable member session resumes, instead of being restored from
    /// persisted metadata. Unlisted fields keep durable truth.
    #[serde(default)]
    pub resume_overrides: Vec<ResumeOverrideField>,
    /// Skill references to load for this profile.
    #[serde(default)]
    pub skills: Vec<String>,
    /// Tool configuration.
    #[serde(default)]
    pub tools: ToolConfig,
    /// Human-readable description of this member's role, visible to peers.
    #[serde(default)]
    pub peer_description: String,
    /// Whether this member can receive turns from external callers.
    #[serde(default)]
    pub external_addressable: bool,
    /// Optional backend override for this profile.
    ///
    /// If unset, runtime uses `definition.backend.default`.
    #[serde(default)]
    pub backend: Option<MobBackendKind>,
    /// Runtime mode for members spawned from this profile.
    ///
    /// Defaults to autonomous keep-alive behavior when omitted.
    #[serde(default)]
    pub runtime_mode: MobRuntimeMode,
    /// Maximum peer-count threshold for inline peer lifecycle context injection.
    ///
    /// - `None`: use runtime default
    /// - `0`: never inline peer lifecycle notifications
    /// - `-1`: always inline peer lifecycle notifications
    /// - `>0`: inline only when post-drain peer count is <= threshold
    /// - `<-1`: invalid
    #[serde(default)]
    pub max_inline_peer_notifications: Option<i32>,
    /// Optional JSON Schema for structured output extraction.
    ///
    /// When set, the agent session is configured with an `OutputSchema` that
    /// forces the LLM to respond with validated JSON conforming to this schema.
    ///
    /// Typed owner: a validated, normalized [`meerkat_core::MeerkatSchema`].
    /// The schema is validated ONCE at profile ingress — deserialization fails
    /// closed on an invalid schema (non-object root) — so a profile holding an
    /// invalid schema can no longer be persisted or transmitted and rejected
    /// only later at spawn time.
    #[serde(default, deserialize_with = "deserialize_output_schema")]
    pub output_schema: Option<meerkat_core::MeerkatSchema>,
    /// Optional provider-specific parameters passed to the LLM adapter.
    ///
    /// This maps directly to `AgentBuildConfig.provider_params` and is useful
    /// for model/provider knobs such as Gemini `thinking_budget` or OpenAI
    /// `reasoning_effort`.
    #[serde(default)]
    pub provider_params: Option<meerkat_core::lifecycle::run_primitive::ProviderParamsOverride>,
}

/// Profile fields that may override durable session metadata on resume.
///
/// Typed, closed vocabulary parsed fail-closed at profile ingress; maps onto
/// the corresponding bits of [`meerkat_core::service::ResumeOverrideMask`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
pub enum ResumeOverrideField {
    /// Re-apply the profile `model` on resume.
    Model,
    /// Re-apply the profile `provider` (and self-hosted binding) on resume.
    Provider,
    /// Re-apply the profile `provider_params` on resume.
    ProviderParams,
}

impl Profile {
    /// Project the declared `resume_overrides` into the typed core mask.
    pub fn resume_override_mask(&self) -> meerkat_core::service::ResumeOverrideMask {
        let mut mask = meerkat_core::service::ResumeOverrideMask::default();
        for field in &self.resume_overrides {
            match field {
                ResumeOverrideField::Model => mask.model = true,
                ResumeOverrideField::Provider => mask.provider = true,
                ResumeOverrideField::ProviderParams => mask.provider_params = true,
            }
        }
        mask
    }
}

/// Validate-at-ingress deserializer for [`Profile::output_schema`]: the raw
/// JSON is parsed into a [`meerkat_core::MeerkatSchema`] exactly once, failing
/// closed on an invalid schema instead of ferrying an unvalidated `Value`
/// through persistence and wire until spawn time.
fn deserialize_output_schema<'de, D>(
    deserializer: D,
) -> Result<Option<meerkat_core::MeerkatSchema>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    Option::<serde_json::Value>::deserialize(deserializer)?
        .map(|value| meerkat_core::MeerkatSchema::new(value).map_err(serde::de::Error::custom))
        .transpose()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_tool_config_serde_roundtrip() {
        let config = ToolConfig {
            builtins: true,
            shell: false,
            comms: true,
            memory: false,
            workgraph: true,
            mob: true,
            schedule: true,
            image_generation: true,
            mcp: vec!["server-a".to_string(), "server-b".to_string()],
            mcp_servers: vec![],
            rust_bundles: vec!["custom-tools".to_string()],
        };
        let json = serde_json::to_string(&config).unwrap();
        let parsed: ToolConfig = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, config);
    }

    #[test]
    fn test_tool_config_toml_roundtrip() {
        let config = ToolConfig {
            builtins: true,
            shell: true,
            comms: false,
            memory: false,
            workgraph: false,
            mob: false,
            schedule: false,
            image_generation: false,
            mcp: vec!["mcp-server".to_string()],
            mcp_servers: vec![],
            rust_bundles: Vec::new(),
        };
        let toml_str = toml::to_string(&config).unwrap();
        let parsed: ToolConfig = toml::from_str(&toml_str).unwrap();
        assert_eq!(parsed, config);
    }

    #[test]
    fn test_profile_serde_roundtrip() {
        let profile = Profile {
            model: "claude-opus-4-8".to_string(),
            provider: None,
            self_hosted_server_id: None,
            image_generation_provider: None,
            auto_compact_threshold: None,
            resume_overrides: Vec::new(),
            skills: vec!["orchestrator-skill".to_string()],
            tools: ToolConfig {
                builtins: true,
                shell: false,
                comms: true,
                memory: false,
                workgraph: true,
                mob: true,
                schedule: false,
                image_generation: false,
                mcp: vec![],
                mcp_servers: vec![],
                rust_bundles: vec![],
            },
            peer_description: "Orchestrates worker agents".to_string(),
            external_addressable: true,
            backend: None,
            runtime_mode: MobRuntimeMode::AutonomousHost,
            max_inline_peer_notifications: None,
            output_schema: None,
            provider_params: None,
        };
        let json = serde_json::to_string(&profile).unwrap();
        let parsed: Profile = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, profile);
    }

    #[test]
    fn test_profile_toml_roundtrip() {
        let profile = Profile {
            model: "gpt-5.2".to_string(),
            provider: None,
            self_hosted_server_id: None,
            image_generation_provider: None,
            auto_compact_threshold: None,
            resume_overrides: Vec::new(),
            skills: vec!["worker-skill".to_string()],
            tools: ToolConfig {
                builtins: false,
                shell: true,
                comms: true,
                memory: false,
                workgraph: false,
                mob: false,
                schedule: false,
                image_generation: false,
                mcp: vec!["code-server".to_string()],
                mcp_servers: vec![],
                rust_bundles: vec!["custom".to_string()],
            },
            peer_description: "Writes code".to_string(),
            external_addressable: false,
            backend: Some(MobBackendKind::External),
            runtime_mode: MobRuntimeMode::TurnDriven,
            max_inline_peer_notifications: Some(20),
            output_schema: None,
            provider_params: None,
        };
        let toml_str = toml::to_string(&profile).unwrap();
        let parsed: Profile = toml::from_str(&toml_str).unwrap();
        assert_eq!(parsed, profile);
    }

    #[test]
    fn test_tool_config_defaults() {
        let config = ToolConfig::default();
        assert!(!config.builtins);
        assert!(!config.shell);
        assert!(!config.comms);
        assert!(!config.memory);
        assert!(!config.workgraph);
        assert!(!config.mob);
        assert!(!config.schedule);
        assert!(config.mcp.is_empty());
        assert!(config.rust_bundles.is_empty());
    }

    #[test]
    fn test_profile_default_fields_from_toml() {
        let toml_str = r#"
model = "claude-sonnet-4-5"
"#;
        let profile: Profile = toml::from_str(toml_str).unwrap();
        assert_eq!(profile.model, "claude-sonnet-4-5");
        assert!(profile.skills.is_empty());
        assert_eq!(profile.tools, ToolConfig::default());
        assert_eq!(profile.peer_description, "");
        assert!(!profile.external_addressable);
        assert_eq!(profile.backend, None);
        assert_eq!(profile.runtime_mode, MobRuntimeMode::AutonomousHost);
        assert_eq!(profile.max_inline_peer_notifications, None);
        assert_eq!(profile.provider_params, None);
    }

    #[test]
    fn test_profile_toml_parses_zero_inline_threshold() {
        let toml_str = r#"
model = "claude-sonnet-4-5"
max_inline_peer_notifications = 0
"#;
        let profile: Profile = toml::from_str(toml_str).unwrap();
        assert_eq!(profile.max_inline_peer_notifications, Some(0));
    }

    #[test]
    fn test_profile_toml_parses_always_inline_threshold() {
        let toml_str = r#"
model = "claude-sonnet-4-5"
max_inline_peer_notifications = -1
"#;
        let profile: Profile = toml::from_str(toml_str).unwrap();
        assert_eq!(profile.max_inline_peer_notifications, Some(-1));
    }

    #[test]
    fn test_profile_output_schema_validates_at_ingress() {
        // A well-formed object schema parses into the typed owner, normalized
        // once at deserialization.
        let profile: Profile = serde_json::from_str(
            r#"{"model":"claude-sonnet-4-5","output_schema":{"type":"object"}}"#,
        )
        .unwrap();
        assert!(profile.output_schema.is_some());

        // Regression: an invalid schema (non-object root) fails CLOSED at
        // profile ingress — it can no longer be persisted/transmitted and
        // rejected only later at spawn time.
        assert!(
            serde_json::from_str::<Profile>(
                r#"{"model":"claude-sonnet-4-5","output_schema":"not an object"}"#,
            )
            .is_err(),
            "an invalid output_schema must be rejected at profile deserialization"
        );
    }

    #[test]
    fn test_profile_toml_parses_typed_provider_and_self_hosted_binding() {
        let toml_str = r#"
model = "claude-internal-preview"
provider = "anthropic"
"#;
        let profile: Profile = toml::from_str(toml_str).unwrap();
        assert_eq!(profile.provider, Some(meerkat_core::Provider::Anthropic));
        assert_eq!(profile.self_hosted_server_id, None);

        let toml_str = r#"
model = "gemma-4-31b"
provider = "self_hosted"
self_hosted_server_id = "local"
"#;
        let profile: Profile = toml::from_str(toml_str).unwrap();
        assert_eq!(profile.provider, Some(meerkat_core::Provider::SelfHosted));
        assert_eq!(profile.self_hosted_server_id.as_deref(), Some("local"));
    }

    #[test]
    fn test_profile_provider_parse_is_fail_closed() {
        let toml_str = r#"
model = "claude-internal-preview"
provider = "antropic"
"#;
        assert!(
            toml::from_str::<Profile>(toml_str).is_err(),
            "unknown provider names must reject the profile at ingress"
        );
    }

    #[test]
    fn test_profile_toml_parses_image_generation_provider() {
        let toml_str = r#"
model = "claude-opus-4-8"
image_generation_provider = "gemini"
"#;
        let profile: Profile = toml::from_str(toml_str).unwrap();
        assert_eq!(
            profile.image_generation_provider,
            Some(meerkat_core::Provider::Gemini)
        );
    }

    #[test]
    fn test_profile_auto_compact_threshold_rejects_zero() {
        let toml_str = r#"
model = "claude-opus-4-8"
auto_compact_threshold = 0
"#;
        assert!(
            toml::from_str::<Profile>(toml_str).is_err(),
            "zero compaction threshold must fail closed at profile ingress"
        );

        let profile: Profile = toml::from_str(
            r#"
model = "claude-opus-4-8"
auto_compact_threshold = 60000
"#,
        )
        .unwrap();
        assert_eq!(
            profile.auto_compact_threshold,
            std::num::NonZeroU64::new(60_000)
        );
    }

    #[test]
    fn test_profile_resume_overrides_parse_and_project_to_mask() {
        let toml_str = r#"
model = "claude-opus-4-8"
resume_overrides = ["model", "provider"]
"#;
        let profile: Profile = toml::from_str(toml_str).unwrap();
        assert_eq!(
            profile.resume_overrides,
            vec![ResumeOverrideField::Model, ResumeOverrideField::Provider]
        );
        let mask = profile.resume_override_mask();
        assert!(mask.model);
        assert!(mask.provider);
        assert!(!mask.provider_params);
        assert!(!mask.max_tokens);
    }

    #[test]
    fn test_profile_resume_overrides_reject_unknown_fields() {
        let toml_str = r#"
model = "claude-opus-4-8"
resume_overrides = ["model", "everything"]
"#;
        assert!(
            toml::from_str::<Profile>(toml_str).is_err(),
            "resume_overrides vocabulary is closed; unknown entries must fail"
        );
    }

    #[test]
    fn test_profile_toml_parses_provider_params() {
        // K2: profile provider_params parse fail-closed into the typed
        // `ProviderParamsOverride` carrier at profile ingress.
        let toml_str = r#"
model = "gemini-3-pro-preview"
provider_params = { provider_tag = { provider = "gemini", thinking_budget = 8192, top_k = 20 } }
"#;
        let profile: Profile = toml::from_str(toml_str).unwrap();
        assert_eq!(
            profile.provider_params,
            Some(
                meerkat_core::lifecycle::run_primitive::ProviderParamsOverride {
                    provider_tag: Some(
                        meerkat_core::lifecycle::run_primitive::ProviderTag::Gemini(
                            meerkat_core::lifecycle::run_primitive::GeminiProviderTag {
                                thinking_budget: Some(8192),
                                top_k: Some(20),
                                ..Default::default()
                            },
                        )
                    ),
                    ..Default::default()
                }
            )
        );
    }

    #[test]
    fn test_profile_toml_rejects_legacy_flat_provider_params() {
        // K2 fail-closed: the retired flat JSON-bag form is rejected at
        // profile parse, not at the first LLM call.
        let toml_str = r#"
model = "gemini-3-pro-preview"
provider_params = { thinking_budget = 8192, top_k = 20 }
"#;
        toml::from_str::<Profile>(toml_str)
            .expect_err("legacy flat provider_params must fail profile parse");
    }

    // -----------------------------------------------------------------------
    // ProfileBinding
    // -----------------------------------------------------------------------

    #[test]
    fn profile_binding_inline_roundtrip() {
        let profile = Profile {
            model: "claude-opus-4-8".to_string(),
            provider: None,
            self_hosted_server_id: None,
            image_generation_provider: None,
            auto_compact_threshold: None,
            resume_overrides: Vec::new(),
            ..Profile {
                model: String::new(),
                provider: None,
                self_hosted_server_id: None,
                image_generation_provider: None,
                auto_compact_threshold: None,
                resume_overrides: Vec::new(),
                skills: vec![],
                tools: ToolConfig::default(),
                peer_description: String::new(),
                external_addressable: false,
                backend: None,
                runtime_mode: MobRuntimeMode::AutonomousHost,
                max_inline_peer_notifications: None,
                output_schema: None,
                provider_params: None,
            }
        };
        let binding = ProfileBinding::Inline(Box::new(profile.clone()));
        let json = serde_json::to_string(&binding).unwrap();
        let parsed: ProfileBinding = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.as_inline().unwrap().model, "claude-opus-4-8");
    }

    #[test]
    fn profile_binding_realm_ref_roundtrip() {
        let binding = ProfileBinding::RealmRef {
            realm_profile: "worker-v2".to_string(),
        };
        let json = serde_json::to_string(&binding).unwrap();
        assert!(json.contains("realm_profile"));
        let parsed: ProfileBinding = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.realm_ref_name(), Some("worker-v2"));
        assert!(parsed.as_inline().is_none());
    }

    #[test]
    fn profile_binding_backward_compat_raw_profile_deserializes_as_inline() {
        // A raw Profile JSON (no realm_profile key) should deserialize as Inline
        let profile_json = r#"{"model":"claude-sonnet-4-5"}"#;
        let binding: ProfileBinding = serde_json::from_str(profile_json).unwrap();
        assert!(binding.as_inline().is_some());
        assert_eq!(binding.as_inline().unwrap().model, "claude-sonnet-4-5");
    }

    #[test]
    fn profile_binding_realm_ref_not_confused_with_inline() {
        // A realm_profile-only object should NOT be consumed as Inline
        let ref_json = r#"{"realm_profile":"my-profile"}"#;
        let binding: ProfileBinding = serde_json::from_str(ref_json).unwrap();
        assert!(binding.realm_ref_name().is_some());
        assert!(binding.as_inline().is_none());
    }

    // -----------------------------------------------------------------------
    // SpawnTooling
    // -----------------------------------------------------------------------

    #[test]
    fn spawn_tooling_inherit_parent_roundtrip() {
        let tooling = SpawnTooling::InheritParent {
            allow_overlay: Some(vec!["shell".into()]),
            deny_overlay: Some(vec!["memory_search".into()]),
        };
        let json = serde_json::to_string(&tooling).unwrap();
        let parsed: SpawnTooling = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, tooling);
    }

    #[test]
    fn spawn_tooling_minimal_roundtrip() {
        let tooling = SpawnTooling::Minimal;
        let json = serde_json::to_string(&tooling).unwrap();
        let parsed: SpawnTooling = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, tooling);
    }

    #[test]
    fn spawn_tooling_profile_realm_roundtrip() {
        let tooling = SpawnTooling::Profile {
            source: Box::new(ProfileSource::RealmProfile {
                name: "worker-v2".into(),
            }),
            allow_overlay: None,
            deny_overlay: Some(vec!["dangerous_tool".into()]),
        };
        let json = serde_json::to_string(&tooling).unwrap();
        let parsed: SpawnTooling = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, tooling);
    }

    #[test]
    fn spawn_tooling_profile_inline_roundtrip() {
        let profile = Profile {
            model: "claude-sonnet-4-5".into(),
            provider: None,
            self_hosted_server_id: None,
            image_generation_provider: None,
            auto_compact_threshold: None,
            resume_overrides: Vec::new(),
            skills: vec![],
            tools: ToolConfig::default(),
            peer_description: String::new(),
            external_addressable: false,
            backend: None,
            runtime_mode: MobRuntimeMode::AutonomousHost,
            max_inline_peer_notifications: None,
            output_schema: None,
            provider_params: None,
        };
        let tooling = SpawnTooling::Profile {
            source: Box::new(ProfileSource::Inline(Box::new(profile))),
            allow_overlay: None,
            deny_overlay: None,
        };
        let json = serde_json::to_string(&tooling).unwrap();
        let parsed: SpawnTooling = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, tooling);
    }
}