meerkat-mob 0.8.9

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
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
//! Controlling-host portable member-spec compiler (multi-host mobs phase 3,
//! design-spec-compiler-dispatch §3.2).
//!
//! Pure and actor-state-free: identical inputs compile to an identical
//! [`PortableMemberSpec`] and canonical digest. Consumed by `enqueue_spawn`,
//! `spawn_from_policy_inline`, and revival re-materialization. The member
//! host re-runs `build_agent_config` over the DECODED spec — every field row
//! below states its `build_agent_config` twin so "one compiler, one owner"
//! stays checkable.
//!
//! House rules: typed errors only (no silent fallbacks); residual `Inherit`
//! (prompt or policy) is a typed compile failure, never a resolve-to-
//! unrestricted mapping (DEC-P3F-3); secret-bearing value maps are
//! fail-closed rejects, never stripped (R5/A10).

use std::collections::BTreeMap;

use meerkat_contracts::wire::portable_member_spec_digest;
use meerkat_contracts::wire::supervisor_bridge::WireOpaqueJson;
use meerkat_contracts::wire::{
    PortableDefinitionExtract, PortableMemberSpec, PortableSkillSource, PortableSpawnOverlay,
    PortableSystemPrompt, WireMobToolAuthorityContext, WireNonPortableResourceKind,
    WireResolvedToolAccessPolicy, WireSpawnContinuityIntent,
};

use crate::definition::{MobDefinition, SkillSource};
use crate::error::MobError;
use crate::identity::{
    DesiredExecution, DesiredMemberMaterial, DesiredMemberOverlay, IdentityProfileMemberDeclaration,
};
use crate::ids::{AgentIdentity, MobId, ProfileName};
use crate::portable_profile::{project_portable_profile, wire_runtime_mode};
use crate::profile::Profile;

/// R3 case-3 base-prompt seam (ADJ-2): resolves the CONTROLLING host's
/// assembled base system prompt (config/AGENTS.md chain) for remote spawns
/// whose profile carries no skills and no per-spawn prompt override.
///
/// Implemented by the facade over the `AgentFactory` prompt chain and
/// injected at `MobBuilder::with_spawn_base_prompt_source`. ABSENT on a
/// case-3 remote spawn ⇒ typed compile failure — never a silent `Inherit`
/// (which would diverge remote members from local semantics invisibly).
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
pub trait SpawnBasePromptSource: Send + Sync {
    /// Resolve the controlling host's base system prompt text. An EMPTY
    /// resolved base is a truthful answer (compiles to `Disable`); a
    /// resolution fault must be a typed error.
    async fn resolve_base_system_prompt(&self) -> Result<String, MobError>;
}

/// A static, pre-resolved base prompt (explicit injection; also the test
/// seam). The facade implementation resolves the factory chain instead.
pub struct StaticSpawnBasePromptSource(pub String);

#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl SpawnBasePromptSource for StaticSpawnBasePromptSource {
    async fn resolve_base_system_prompt(&self) -> Result<String, MobError> {
        Ok(self.0.clone())
    }
}

/// ADJ-2 facade-chain implementation: resolves the controlling host's base
/// system prompt through the ONE meerkat prompt-assembly precedence chain
/// (`system_prompt_file` → config inline → AGENTS.md → default) — exactly
/// what the factory's `Inherit` resolution produces for a local skill-less
/// member, minus the per-session appended sections (skill inventory, tool
/// instructions), which are session facts, not base-prompt facts.
///
/// Lives in meerkat-mob (which depends on the facade) because the facade
/// cannot name this crate's trait; composition surfaces (CLI, embedders)
/// construct it with the SAME config + context root their factory uses.
#[cfg(not(target_arch = "wasm32"))]
pub struct FactoryChainSpawnBasePromptSource {
    config: std::sync::Arc<meerkat::Config>,
    context_root: Option<std::path::PathBuf>,
}

#[cfg(not(target_arch = "wasm32"))]
impl FactoryChainSpawnBasePromptSource {
    pub fn new(
        config: std::sync::Arc<meerkat::Config>,
        context_root: Option<std::path::PathBuf>,
    ) -> Self {
        Self {
            config,
            context_root,
        }
    }
}

#[cfg(not(target_arch = "wasm32"))]
#[async_trait::async_trait]
impl SpawnBasePromptSource for FactoryChainSpawnBasePromptSource {
    async fn resolve_base_system_prompt(&self) -> Result<String, MobError> {
        meerkat::assemble_system_prompt(
            &self.config,
            &meerkat::SystemPromptOverride::Inherit,
            self.context_root.as_deref(),
            &[],
            "",
        )
        .await
        .map_err(|error| {
            MobError::WiringError(format!(
                "controlling-host base system prompt resolution failed: {error}"
            ))
        })
    }
}

/// Inputs to [`compile_portable_member_spec`] — the same resolved material
/// `build_agent_config` would receive, plus the overlay inputs straight from
/// the spawn-spec destructure.
pub(crate) struct CompileMemberSpecParams<'a> {
    pub mob_id: &'a MobId,
    pub profile_name: &'a ProfileName,
    pub agent_identity: &'a AgentIdentity,
    /// AFTER override/realm/inherited-open resolution.
    pub profile: &'a Profile,
    pub definition: &'a MobDefinition,
    pub context: Option<&'a serde_json::Value>,
    pub labels: Option<&'a BTreeMap<String, String>>,
    pub additional_instructions: Option<&'a Vec<String>>,
    pub system_prompt_override: Option<&'a super::handle::SpawnSystemPromptOverride>,
    pub tool_access_policy: Option<&'a meerkat_core::ops::ToolAccessPolicy>,
    pub auth_binding: Option<&'a meerkat_core::AuthBindingRef>,
    pub budget_limits: Option<&'a meerkat_core::BudgetLimits>,
    /// The SELECTED runtime mode (spec-or-profile resolution).
    pub runtime_mode: crate::MobRuntimeMode,
    pub continuity_intent: &'a super::handle::SpawnContinuityIntent,
    /// R3 case-3 seam (ADJ-2). `None` ⇒ case-3 remote spawns fail typed.
    pub base_prompt: Option<&'a dyn SpawnBasePromptSource>,
    /// Non-portable resolve-to-disabled record. v1: always empty — the
    /// inherited-filter case is hard-denied at the ladder (ADJ-6), so the
    /// projection field stays a typed always-empty record.
    pub non_portable_disabled: Vec<WireNonPortableResourceKind>,
}

/// Inputs for compiling the sole durable, authority-free member material.
///
/// `resolved_profile` is present only when the declaration names a canonical
/// definition/realm profile. A declaration carrying `profile_override`
/// already supplies the exact portable profile and therefore must not also
/// carry a second mutable profile copy.
pub(crate) struct CompileDesiredMemberMaterialParams<'a> {
    pub agent_identity: &'a AgentIdentity,
    pub declaration: &'a IdentityProfileMemberDeclaration,
    pub resolved_profile: Option<&'a Profile>,
    pub definition: &'a MobDefinition,
    pub base_prompt: Option<&'a dyn SpawnBasePromptSource>,
    pub non_portable_disabled: Vec<WireNonPortableResourceKind>,
}

/// Canonical wire ordering for an unordered tool-name set: the digest-covered
/// spec must hash identically for identical policies.
fn sorted_tool_names(names: &meerkat_core::types::ToolNameSet) -> Vec<String> {
    let mut sorted: Vec<String> = names.iter().map(|name| name.as_str().to_string()).collect();
    sorted.sort();
    sorted
}

/// Compile the exact authority-free desired material sealed by identity
/// intent. This function never receives a mob id, continuity target, operator
/// authority, callback dispatcher, or secret value.
pub(crate) async fn compile_desired_member_material(
    params: CompileDesiredMemberMaterialParams<'_>,
) -> Result<DesiredMemberMaterial, MobError> {
    let CompileDesiredMemberMaterialParams {
        agent_identity,
        declaration,
        resolved_profile,
        definition,
        base_prompt,
        non_portable_disabled,
    } = params;

    declaration.validate().map_err(|error| {
        MobError::WiringError(format!(
            "identity declaration for '{agent_identity}' is invalid: {error}"
        ))
    })?;

    let declared_runtime_mode =
        if matches!(declaration.execution, DesiredExecution::External { .. }) {
            Some(meerkat_contracts::wire::WireMobRuntimeMode::TurnDriven)
        } else {
            declaration.runtime_mode
        };
    let mut portable_profile = match (&declaration.profile_override, resolved_profile) {
        (Some(portable), None) => {
            let mut portable = portable.clone();
            if let Some(model) = &declaration.model_override {
                portable.model.clone_from(model);
            }
            if let Some(external_addressable) = declaration.external_addressable_override {
                portable.external_addressable = external_addressable;
            }
            portable
        }
        (None, Some(profile)) => {
            let mut profile = profile.clone();
            if let Some(model) = &declaration.model_override {
                profile.model.clone_from(model);
            }
            if let Some(external_addressable) = declaration.external_addressable_override {
                profile.external_addressable = external_addressable;
            }
            let runtime_mode = declared_runtime_mode
                .map(domain_runtime_mode)
                .unwrap_or(profile.runtime_mode);
            project_portable_profile(
                &profile,
                runtime_mode,
                &definition.models,
                agent_identity.as_str(),
                declaration.profile_name.as_str(),
                non_portable_disabled,
            )
            .map_err(MobError::WiringError)?
        }
        (Some(_), Some(_)) => {
            return Err(MobError::Internal(format!(
                "identity declaration compiler received both portable and resolved profiles for '{agent_identity}'"
            )));
        }
        (None, None) => {
            return Err(MobError::Internal(format!(
                "identity declaration compiler received no resolved profile for '{agent_identity}'"
            )));
        }
    };

    let runtime_mode = declared_runtime_mode.unwrap_or(portable_profile.runtime_mode);
    portable_profile.runtime_mode = runtime_mode;
    let skills = compile_inline_skills(
        definition,
        &portable_profile.skills,
        &declaration.profile_name,
    )
    .await?;
    let system_prompt = resolve_portable_system_prompt(
        agent_identity,
        &declaration.profile_name,
        &portable_profile.skills,
        &skills,
        declaration.system_prompt_override.as_ref(),
        base_prompt,
    )
    .await?;

    let mut profile_names = definition
        .profiles
        .keys()
        .map(|profile| profile.as_str().to_string())
        .collect::<Vec<_>>();
    if profile_names
        .binary_search_by(|name| name.as_str().cmp(declaration.profile_name.as_str()))
        .is_err()
    {
        profile_names.push(declaration.profile_name.as_str().to_string());
        profile_names.sort();
    }
    let mut required_local_callback_tools = declaration.required_local_callback_tools.clone();
    required_local_callback_tools.sort_by(|left, right| left.name.cmp(&right.name));
    let material = DesiredMemberMaterial {
        profile_name: declaration.profile_name.clone(),
        profile: portable_profile,
        definition_extract: PortableDefinitionExtract {
            models: definition.models.clone(),
            image_generation_provider: definition.image_generation_provider,
            skills,
            profile_names,
        },
        overlay: DesiredMemberOverlay {
            context: declaration.context.clone(),
            labels: declaration.labels.clone(),
            additional_instructions: declaration.additional_instructions.clone(),
            system_prompt,
            tool_access_policy: declaration.tool_access_policy.clone(),
            auth_binding: declaration.auth_binding.clone(),
            budget_limits: declaration.budget_limits.clone(),
            runtime_mode,
        },
        required_env_keys: declaration.required_env_keys.clone(),
        required_local_callback_tools,
        execution: declaration.execution.clone(),
    };
    material.validate().map_err(|error| {
        MobError::WiringError(format!(
            "compiled identity material for '{agent_identity}' is invalid: {error}"
        ))
    })?;
    Ok(material)
}

/// Lower one already sealed authority-free desired member into the existing
/// local resume mechanic. This path never recompiles caller material and
/// never carries the one-shot initial delivery; the identity reconciler owns
/// that effect separately by stable `InputId`.
pub(crate) fn spawn_spec_from_desired_member(
    identity: &AgentIdentity,
    session: &crate::identity::DesiredSessionTarget,
    member: &crate::identity::DesiredMemberSpec,
) -> Result<super::handle::SpawnMemberSpec, MobError> {
    use meerkat_contracts::wire::{PortableSystemPrompt, WireResolvedToolAccessPolicy};

    member.validate().map_err(|error| {
        MobError::WiringError(format!(
            "sealed desired member for '{identity}' is invalid: {error}"
        ))
    })?;
    let material = &member.material;
    let profile = crate::portable_profile::rehydrate_portable_profile(&material.profile).map_err(
        |error| {
            MobError::WiringError(format!(
                "sealed desired profile for '{identity}' cannot be materialized: {error}"
            ))
        },
    )?;
    let mut spec =
        super::handle::SpawnMemberSpec::new(material.profile_name.clone(), identity.clone());
    spec.initial_message = None;
    spec.launch_mode = crate::launch::MemberLaunchMode::Resume {
        bridge_session_id: session.session_id.clone(),
    };
    spec.runtime_mode = Some(domain_runtime_mode(material.overlay.runtime_mode));
    spec.context = material
        .overlay
        .context
        .as_ref()
        .map(|value| value.to_value())
        .transpose()
        .map_err(|error| {
            MobError::WiringError(format!(
                "sealed desired context for '{identity}' is malformed: {error}"
            ))
        })?;
    spec.labels = material.overlay.labels.clone();
    spec.additional_instructions = material.overlay.additional_instructions.clone();
    spec.tool_access_policy =
        material
            .overlay
            .tool_access_policy
            .as_ref()
            .map(|policy| match policy {
                WireResolvedToolAccessPolicy::AllowList(names) => {
                    meerkat_core::ops::ToolAccessPolicy::AllowList(names.iter().cloned().collect())
                }
                WireResolvedToolAccessPolicy::DenyList(names) => {
                    meerkat_core::ops::ToolAccessPolicy::DenyList(names.iter().cloned().collect())
                }
            });
    spec.auth_binding = material.overlay.auth_binding.clone().map(Into::into);
    spec.budget_limits = material.overlay.budget_limits.clone();
    spec.system_prompt_override = Some(match &material.overlay.system_prompt {
        PortableSystemPrompt::Set { text } => {
            super::handle::SpawnSystemPromptOverride::Replace(text.clone())
        }
        PortableSystemPrompt::Disable => super::handle::SpawnSystemPromptOverride::Disable,
    });
    spec.override_profile = Some(profile);
    spec.continuity_intent = super::handle::SpawnContinuityIntent::DurableIdentity {
        continuity_key: identity.as_str().to_string(),
    };
    match &material.execution {
        DesiredExecution::ControllingSession => {
            spec.binding = Some(crate::RuntimeBinding::Session);
        }
        DesiredExecution::PlacedSession { .. } => {
            return Err(MobError::WiringError(format!(
                "identity '{identity}' placed-session recovery is not available in the local convergence slice"
            )));
        }
        DesiredExecution::AnyBoundHostSession => {
            return Err(MobError::WiringError(format!(
                "identity '{identity}' requires a fresh bound-host observation before materialization"
            )));
        }
        DesiredExecution::External { .. } => {
            return Err(MobError::WiringError(format!(
                "identity '{identity}' requires fresh external ceremony/trust before materialization"
            )));
        }
    }
    Ok(spec)
}

fn domain_runtime_mode(mode: meerkat_contracts::wire::WireMobRuntimeMode) -> crate::MobRuntimeMode {
    match mode {
        meerkat_contracts::wire::WireMobRuntimeMode::AutonomousHost => {
            crate::MobRuntimeMode::AutonomousHost
        }
        meerkat_contracts::wire::WireMobRuntimeMode::TurnDriven => {
            crate::MobRuntimeMode::TurnDriven
        }
    }
}

async fn compile_inline_skills(
    definition: &MobDefinition,
    skill_names: &[String],
    profile_name: &ProfileName,
) -> Result<BTreeMap<String, PortableSkillSource>, MobError> {
    let mut skills = BTreeMap::new();
    for skill_name in skill_names {
        let source = definition.skills.get(skill_name).ok_or_else(|| {
            MobError::WiringError(format!(
                "profile '{profile_name}' references skill '{skill_name}' that is not defined in the mob definition"
            ))
        })?;
        let inline = match source {
            SkillSource::Inline { content } => PortableSkillSource::Inline {
                content: content.clone(),
            },
            SkillSource::Path { path } => {
                #[cfg(not(target_arch = "wasm32"))]
                {
                    let content = tokio::fs::read_to_string(path).await.map_err(|error| {
                        MobError::WiringError(format!(
                            "failed to read skill file '{path}' for skill '{skill_name}' while compiling portable member material: {error}"
                        ))
                    })?;
                    PortableSkillSource::Inline { content }
                }
                #[cfg(target_arch = "wasm32")]
                {
                    return Err(MobError::WiringError(format!(
                        "file-based skill path '{path}' cannot be compiled into portable member material on wasm32"
                    )));
                }
            }
        };
        skills.insert(skill_name.clone(), inline);
    }
    Ok(skills)
}

async fn resolve_portable_system_prompt(
    agent_identity: &AgentIdentity,
    profile_name: &ProfileName,
    skill_names: &[String],
    skills: &BTreeMap<String, PortableSkillSource>,
    override_value: Option<&PortableSystemPrompt>,
    base_prompt: Option<&dyn SpawnBasePromptSource>,
) -> Result<PortableSystemPrompt, MobError> {
    if let Some(override_value) = override_value {
        return Ok(override_value.clone());
    }
    let assembled = assemble_inline_skill_prompt(skill_names, skills);
    if !assembled.is_empty() {
        return Ok(PortableSystemPrompt::Set { text: assembled });
    }
    let Some(base_prompt) = base_prompt else {
        return Err(MobError::WiringError(format!(
            "member material for '{agent_identity}' needs the controlling host's base system prompt (profile '{profile_name}' has no skills and no prompt override) but no SpawnBasePromptSource is wired; inject one via MobBuilder::with_spawn_base_prompt_source (never a silent Inherit)"
        )));
    };
    let base = base_prompt.resolve_base_system_prompt().await?;
    if base.is_empty() {
        Ok(PortableSystemPrompt::Disable)
    } else {
        Ok(PortableSystemPrompt::Set { text: base })
    }
}

fn resolved_tool_access_policy(
    policy: Option<&meerkat_core::ops::ToolAccessPolicy>,
    agent_identity: &AgentIdentity,
) -> Result<Option<WireResolvedToolAccessPolicy>, MobError> {
    match policy {
        None => Ok(None),
        Some(meerkat_core::ops::ToolAccessPolicy::AllowList(names)) => Ok(Some(
            WireResolvedToolAccessPolicy::AllowList(sorted_tool_names(names)),
        )),
        Some(meerkat_core::ops::ToolAccessPolicy::DenyList(names)) => Ok(Some(
            WireResolvedToolAccessPolicy::DenyList(sorted_tool_names(names)),
        )),
        Some(meerkat_core::ops::ToolAccessPolicy::Inherit) => Err(MobError::WiringError(format!(
            "tool access policy for member '{agent_identity}' is still Inherit at material compile; callers must resolve it before the actor seals desired material"
        ))),
    }
}

/// Output of the compiler: the digest-covered spec, its canonical digest
/// (computed EXACTLY ONCE, here — every later appearance is the machine's
/// copy or the wire echo, never a shell recompute), and the minted operator
/// authority when `profile.tools.mob`.
#[derive(Debug)]
pub(crate) struct CompiledMemberSpec {
    pub spec: PortableMemberSpec,
    pub digest: String,
    /// Minted iff `profile.tools.mob`; durably recorded before dispatch
    /// (ADJ-15 — write-site lives with the spawn ladder open, same
    /// transition witness as the spec's authority context).
    pub operator_authority: Option<meerkat_core::service::MobToolAuthorityContext>,
}

pub(crate) async fn compile_portable_member_spec(
    params: CompileMemberSpecParams<'_>,
) -> Result<CompiledMemberSpec, MobError> {
    let CompileMemberSpecParams {
        mob_id,
        profile_name,
        agent_identity,
        profile,
        definition,
        context,
        labels,
        additional_instructions,
        system_prompt_override,
        tool_access_policy,
        auth_binding,
        budget_limits,
        runtime_mode,
        continuity_intent,
        base_prompt,
        non_portable_disabled,
    } = params;

    let system_prompt_override =
        system_prompt_override.map(|override_value| match override_value {
            super::handle::SpawnSystemPromptOverride::Replace(prompt) => {
                PortableSystemPrompt::Set {
                    text: prompt.clone(),
                }
            }
            super::handle::SpawnSystemPromptOverride::Disable => PortableSystemPrompt::Disable,
        });
    let tool_access_policy = resolved_tool_access_policy(tool_access_policy, agent_identity)?;
    let declaration = IdentityProfileMemberDeclaration {
        profile_name: profile_name.clone(),
        profile_override: None,
        model_override: None,
        external_addressable_override: None,
        context: context.map(WireOpaqueJson::from_value),
        labels: labels.cloned(),
        additional_instructions: additional_instructions.cloned(),
        system_prompt_override,
        tool_access_policy,
        auth_binding: auth_binding.cloned().map(Into::into),
        budget_limits: budget_limits.cloned(),
        runtime_mode: Some(wire_runtime_mode(runtime_mode)),
        required_env_keys: Vec::new(),
        required_local_callback_tools: Vec::new(),
        execution: DesiredExecution::ControllingSession,
    };
    let material = compile_desired_member_material(CompileDesiredMemberMaterialParams {
        agent_identity,
        declaration: &declaration,
        resolved_profile: Some(profile),
        definition,
        base_prompt,
        non_portable_disabled,
    })
    .await?;

    // Operator authority: mint via the build.rs:249-273 path, then PROJECT
    // to the wire (visibility composition only; dispatch authority is
    // re-resolved controlling-side from the durable record, R6).
    let operator_authority = if profile.tools.mob {
        let (_override_mob, authority) =
            crate::build::resolve_profile_mob_operator_access(profile, None);
        match authority {
            Some(authority_context) => {
                let profile_names = definition
                    .profiles
                    .keys()
                    .map(|profile| profile.as_str().to_string())
                    .collect::<Vec<_>>();
                let authority_context =
                    meerkat_runtime::mob_operator_authority::grant_spawn_profiles_in_mob(
                        &authority_context,
                        mob_id.as_str(),
                        profile_names,
                    )
                    .map_err(|error| {
                        MobError::Internal(format!(
                            "generated mob operator authority rejected default spawn profiles for remote spawn of '{agent_identity}': {error}"
                        ))
                    })?;
                // A placed member's ONLY channel to its mob is the upcall
                // lane; the phase exit gate (plan §17 T-19: upcall
                // `list_members` / `retire_member` complete) requires the
                // re-minted authority to carry manage scope of the CURRENT
                // mob, so the durable record's facts include it.
                Some(
                    meerkat_runtime::mob_operator_authority::grant_manage_mob(
                        &authority_context,
                        mob_id.as_str(),
                    )
                    .map_err(|error| {
                        MobError::Internal(format!(
                            "generated mob operator authority rejected current-mob manage scope for remote spawn of '{agent_identity}': {error}"
                        ))
                    })?,
                )
            }
            None => None,
        }
    } else {
        None
    };
    let mob_tool_authority_context = operator_authority
        .as_ref()
        .map(project_mob_tool_authority_context)
        .transpose()?;
    let DesiredMemberMaterial {
        profile: portable_profile,
        definition_extract,
        overlay,
        required_env_keys,
        ..
    } = material;
    let DesiredMemberOverlay {
        context,
        labels,
        additional_instructions,
        system_prompt,
        tool_access_policy,
        auth_binding,
        budget_limits,
        runtime_mode,
    } = overlay;

    let spec = PortableMemberSpec {
        mob_id: mob_id.as_str().to_string(),
        profile_name: profile_name.as_str().to_string(),
        agent_identity: agent_identity.as_str().to_string(),
        profile: portable_profile,
        definition_extract,
        overlay: PortableSpawnOverlay {
            context,
            labels,
            additional_instructions,
            system_prompt,
            tool_access_policy,
            mob_tool_authority_context,
            auth_binding,
            budget_limits,
            runtime_mode,
            continuity_intent: wire_continuity_intent(continuity_intent),
        },
        required_env_keys,
    };

    let digest = portable_member_spec_digest(&spec).map_err(|error| {
        MobError::Internal(format!(
            "portable member spec for '{agent_identity}' failed canonical digest computation: {error}"
        ))
    })?;

    Ok(CompiledMemberSpec {
        spec,
        digest,
        operator_authority,
    })
}

/// Prompt assembly over the ALREADY-INLINED skill map — byte-identical to
/// `build::assemble_system_prompt` output for the same profile (the member
/// host re-derives it from `definition_extract.skills`).
fn assemble_inline_skill_prompt(
    skill_names: &[String],
    skills: &BTreeMap<String, PortableSkillSource>,
) -> String {
    let mut sections = Vec::new();
    for skill_name in skill_names {
        if let Some(PortableSkillSource::Inline { content }) = skills.get(skill_name) {
            sections.push(content.as_str());
        }
    }
    sections.join("\n\n")
}

/// Project the sealed domain authority context onto the wire mirror through
/// its documented serde lane. The seal never serializes; the projection
/// cannot mint authority (portable_spec.rs doc contract).
pub(crate) fn project_mob_tool_authority_context(
    authority: &meerkat_core::service::MobToolAuthorityContext,
) -> Result<WireMobToolAuthorityContext, MobError> {
    let value = serde_json::to_value(authority).map_err(|error| {
        MobError::Internal(format!(
            "mob tool authority context failed wire projection encode: {error}"
        ))
    })?;
    serde_json::from_value(value).map_err(|error| {
        MobError::Internal(format!(
            "mob tool authority context failed wire projection decode: {error}"
        ))
    })
}

fn wire_continuity_intent(
    intent: &super::handle::SpawnContinuityIntent,
) -> WireSpawnContinuityIntent {
    match intent {
        super::handle::SpawnContinuityIntent::Ephemeral => WireSpawnContinuityIntent::Ephemeral,
        super::handle::SpawnContinuityIntent::DurableIdentity { continuity_key } => {
            WireSpawnContinuityIntent::DurableIdentity {
                continuity_key: continuity_key.clone(),
            }
        }
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
    use super::*;
    use crate::profile::ToolConfig;

    fn worker_profile() -> Profile {
        Profile {
            model: "claude-haiku-4-5-20251001".to_string(),
            provider: None,
            self_hosted_server_id: None,
            image_generation_provider: None,
            auto_compact_threshold: None,
            resume_overrides: Vec::new(),
            skills: vec![],
            tools: ToolConfig {
                comms: true,
                ..ToolConfig::default()
            },
            peer_description: "compiler unit worker".to_string(),
            external_addressable: true,
            backend: None,
            runtime_mode: crate::MobRuntimeMode::TurnDriven,
            max_inline_peer_notifications: None,
            output_schema: None,
            provider_params: None,
        }
    }

    fn definition() -> MobDefinition {
        MobDefinition::explicit(MobId::from("compiler-unit"))
    }

    fn params<'a>(
        mob_id: &'a MobId,
        profile_name: &'a ProfileName,
        identity: &'a AgentIdentity,
        profile: &'a Profile,
        definition: &'a MobDefinition,
        base: Option<&'a dyn SpawnBasePromptSource>,
        continuity: &'a super::super::handle::SpawnContinuityIntent,
    ) -> CompileMemberSpecParams<'a> {
        CompileMemberSpecParams {
            mob_id,
            profile_name,
            agent_identity: identity,
            profile,
            definition,
            context: None,
            labels: None,
            additional_instructions: None,
            system_prompt_override: None,
            tool_access_policy: None,
            auth_binding: None,
            budget_limits: None,
            runtime_mode: crate::MobRuntimeMode::TurnDriven,
            continuity_intent: continuity,
            base_prompt: base,
            non_portable_disabled: Vec::new(),
        }
    }

    /// U1: compile determinism + digest recompute agreement.
    #[tokio::test]
    async fn compile_is_deterministic_and_digest_recomputes() {
        let mob_id = MobId::from("compiler-unit");
        let profile_name = ProfileName::from("worker");
        let identity = AgentIdentity::from("b2");
        let profile = worker_profile();
        let definition = definition();
        let base = StaticSpawnBasePromptSource("base prompt".to_string());
        let continuity = super::super::handle::SpawnContinuityIntent::Ephemeral;
        let first = compile_portable_member_spec(params(
            &mob_id,
            &profile_name,
            &identity,
            &profile,
            &definition,
            Some(&base),
            &continuity,
        ))
        .await
        .expect("compile");
        let second = compile_portable_member_spec(params(
            &mob_id,
            &profile_name,
            &identity,
            &profile,
            &definition,
            Some(&base),
            &continuity,
        ))
        .await
        .expect("compile twice");
        assert_eq!(first.spec, second.spec);
        assert_eq!(first.digest, second.digest);
        assert_eq!(
            first.digest,
            portable_member_spec_digest(&first.spec).expect("recompute"),
        );
    }

    #[tokio::test]
    async fn desired_material_compiler_seals_overrides_and_canonical_callback_tools() {
        let identity = AgentIdentity::from("worker-a");
        let profile = worker_profile();
        let definition = definition();
        let declaration = IdentityProfileMemberDeclaration {
            profile_name: ProfileName::from("worker"),
            profile_override: None,
            model_override: Some("claude-opus-4-8".to_string()),
            external_addressable_override: Some(false),
            context: None,
            labels: None,
            additional_instructions: None,
            system_prompt_override: Some(PortableSystemPrompt::Disable),
            tool_access_policy: None,
            auth_binding: None,
            budget_limits: None,
            runtime_mode: Some(meerkat_contracts::wire::WireMobRuntimeMode::TurnDriven),
            required_env_keys: Vec::new(),
            required_local_callback_tools: vec![
                crate::identity::DesiredLocalCallbackTool::new(
                    "zeta",
                    "Tool zeta",
                    serde_json::json!({}),
                )
                .unwrap(),
                crate::identity::DesiredLocalCallbackTool::new(
                    "alpha",
                    "Tool alpha",
                    serde_json::json!({}),
                )
                .unwrap(),
            ],
            execution: DesiredExecution::ControllingSession,
        };
        let material = compile_desired_member_material(CompileDesiredMemberMaterialParams {
            agent_identity: &identity,
            declaration: &declaration,
            resolved_profile: Some(&profile),
            definition: &definition,
            base_prompt: None,
            non_portable_disabled: Vec::new(),
        })
        .await
        .expect("compile desired material");

        assert_eq!(material.profile.model, "claude-opus-4-8");
        assert!(!material.profile.external_addressable);
        assert_eq!(
            material
                .required_local_callback_tools
                .iter()
                .map(|tool| tool.name.as_str())
                .collect::<Vec<_>>(),
            vec!["alpha", "zeta"]
        );
        material.validate().expect("sealed material is canonical");

        let session_id = meerkat_core::SessionId::new();
        let session = crate::identity::DesiredSessionTarget {
            session_id: session_id.clone(),
            lineage_id: meerkat_core::SessionLineageId::for_session(&session_id),
            lineage_generation: meerkat_core::SessionGeneration::INITIAL,
            authority_policy: crate::identity::DesiredSessionAuthorityPolicy::CreateIfAbsent,
        };
        let intent_for = |material| crate::identity::IdentityIntent::Present {
            identity: identity.clone(),
            session: session.clone(),
            member: Box::new(crate::identity::DesiredMemberSpec {
                material,
                initial_delivery: None,
            }),
            owned_wiring: Default::default(),
        };
        let original_digest = intent_for(material.clone()).digest().unwrap();
        let mut changed = material;
        changed.required_local_callback_tools[0]
            .description
            .push_str(" changed");
        changed.validate().unwrap();
        assert_ne!(original_digest, intent_for(changed).digest().unwrap());
    }

    /// U3(iii): base source absent on a case-3 spawn is a typed failure.
    #[tokio::test]
    async fn case_three_without_base_source_fails_typed() {
        let mob_id = MobId::from("compiler-unit");
        let profile_name = ProfileName::from("worker");
        let identity = AgentIdentity::from("b2");
        let profile = worker_profile();
        let definition = definition();
        let continuity = super::super::handle::SpawnContinuityIntent::Ephemeral;
        let error = compile_portable_member_spec(params(
            &mob_id,
            &profile_name,
            &identity,
            &profile,
            &definition,
            None,
            &continuity,
        ))
        .await
        .expect_err("case-3 without a base source must fail typed");
        assert!(matches!(error, MobError::WiringError(_)), "got {error:?}");
    }

    /// U3(iv): empty resolved base compiles Disable; non-empty compiles Set.
    #[tokio::test]
    async fn base_prompt_resolution_maps_to_set_or_disable() {
        let mob_id = MobId::from("compiler-unit");
        let profile_name = ProfileName::from("worker");
        let identity = AgentIdentity::from("b2");
        let profile = worker_profile();
        let definition = definition();
        let continuity = super::super::handle::SpawnContinuityIntent::Ephemeral;
        let empty = StaticSpawnBasePromptSource(String::new());
        let compiled = compile_portable_member_spec(params(
            &mob_id,
            &profile_name,
            &identity,
            &profile,
            &definition,
            Some(&empty),
            &continuity,
        ))
        .await
        .expect("compile");
        assert!(matches!(
            compiled.spec.overlay.system_prompt,
            PortableSystemPrompt::Disable
        ));
        let non_empty = StaticSpawnBasePromptSource("You are a helpful mob member.".to_string());
        let compiled = compile_portable_member_spec(params(
            &mob_id,
            &profile_name,
            &identity,
            &profile,
            &definition,
            Some(&non_empty),
            &continuity,
        ))
        .await
        .expect("compile");
        assert!(matches!(
            compiled.spec.overlay.system_prompt,
            PortableSystemPrompt::Set { .. }
        ));
    }

    /// U4: residual Inherit at compile is a typed error (never the
    /// build.rs Inherit→None fold).
    #[tokio::test]
    async fn residual_inherit_policy_fails_typed() {
        let mob_id = MobId::from("compiler-unit");
        let profile_name = ProfileName::from("worker");
        let identity = AgentIdentity::from("b2");
        let profile = worker_profile();
        let definition = definition();
        let base = StaticSpawnBasePromptSource("base".to_string());
        let continuity = super::super::handle::SpawnContinuityIntent::Ephemeral;
        let mut p = params(
            &mob_id,
            &profile_name,
            &identity,
            &profile,
            &definition,
            Some(&base),
            &continuity,
        );
        let inherit = meerkat_core::ops::ToolAccessPolicy::Inherit;
        p.tool_access_policy = Some(&inherit);
        let error = compile_portable_member_spec(p)
            .await
            .expect_err("Inherit at spec-mint must fail typed");
        assert!(matches!(error, MobError::WiringError(_)), "got {error:?}");
    }

    /// U5: provider pinning — profile.provider wins; unknown model with no
    /// provider fails typed at compile.
    #[tokio::test]
    async fn unresolvable_provider_fails_at_compile() {
        let mob_id = MobId::from("compiler-unit");
        let profile_name = ProfileName::from("worker");
        let identity = AgentIdentity::from("b2");
        let mut profile = worker_profile();
        profile.model = "totally-unknown-model-xyz".to_string();
        let definition = definition();
        let base = StaticSpawnBasePromptSource("base".to_string());
        let continuity = super::super::handle::SpawnContinuityIntent::Ephemeral;
        let error = compile_portable_member_spec(params(
            &mob_id,
            &profile_name,
            &identity,
            &profile,
            &definition,
            Some(&base),
            &continuity,
        ))
        .await
        .expect_err("unknown model with no provider must fail at compile");
        assert!(matches!(error, MobError::WiringError(_)), "got {error:?}");
    }

    /// U6 (budget single carrier, ADJ-1 / T-U2): the budget lands in
    /// `overlay.budget_limits` and nowhere else in the serialized spec.
    #[tokio::test]
    async fn budget_lands_in_overlay_only() {
        let mob_id = MobId::from("compiler-unit");
        let profile_name = ProfileName::from("worker");
        let identity = AgentIdentity::from("b2");
        let profile = worker_profile();
        let definition = definition();
        let base = StaticSpawnBasePromptSource("base".to_string());
        let continuity = super::super::handle::SpawnContinuityIntent::Ephemeral;
        let limits = meerkat_core::BudgetLimits {
            max_tokens: None,
            max_duration: None,
            max_tool_calls: Some(3),
        };
        let mut p = params(
            &mob_id,
            &profile_name,
            &identity,
            &profile,
            &definition,
            Some(&base),
            &continuity,
        );
        p.budget_limits = Some(&limits);
        let compiled = compile_portable_member_spec(p).await.expect("compile");
        assert_eq!(compiled.spec.overlay.budget_limits.as_ref(), Some(&limits));
        let rendered = serde_json::to_value(&compiled.spec)
            .expect("spec serializes")
            .to_string();
        assert_eq!(rendered.matches("budget").count(), 1);
    }
}