awaken-server 0.6.0

Multi-protocol HTTP server with SSE, mailbox, and protocol adapters for Awaken
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
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
use std::sync::Arc;

use crate::services::pinned_registry::{
    PinnedAgentSpecRegistry, PinnedModelRegistry, PinnedRegistryError, PinnedSpecMap,
};
use awaken_runtime::registry::RegistryHandle;
use awaken_runtime::resolution::{
    PersistenceRequirement, RegistryResolutionScope, ResolutionRequest, ResolveError,
    ResolvedRunPlan, Resolver,
};
use awaken_server_contract::contract::versioned_registry::VersionedRecord;
use awaken_server_contract::skill_spec::SkillSpec;
use awaken_server_contract::tool_spec::ToolSpec;
use awaken_server_contract::{
    AgentSpec, ModelPoolSpec, ModelSpec, PinnedRegistryEntry, PinnedRegistryManifest, ProviderSpec,
    REGISTRY_KIND_AGENT, REGISTRY_KIND_MODEL, REGISTRY_KIND_MODEL_POOL,
    REGISTRY_KIND_PLUGIN_CONFIG, REGISTRY_KIND_PROVIDER, REGISTRY_KIND_SKILL, REGISTRY_KIND_TOOL,
    RegistryGraphValidationError, RegistryGraphValidationRequest, RegistryGraphValidator,
    ScopeContext, ScopeError, ScopeId, StandardRegistryGraphValidator, VersionSelector,
    VersionedRegistryError, VersionedRegistryStore,
};
use serde::de::DeserializeOwned;
use serde_json::Value;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum FrozenRegistryMaterializationError {
    #[error("registry graph validation failed: {0}")]
    Graph(#[from] RegistryGraphValidationError),
    #[error("versioned registry error: {0}")]
    Registry(#[from] VersionedRegistryError),
    #[error("pinned registry error: {0}")]
    Pinned(#[from] PinnedRegistryError),
    #[error("invalid frozen registry graph: {0}")]
    InvalidGraph(String),
}

#[non_exhaustive]
pub struct FrozenAgentRegistry {
    pub manifest: PinnedRegistryManifest,
    pub agents: Arc<PinnedAgentSpecRegistry>,
    /// Pinned model bindings reachable from the manifest (ADR-0035 D8).
    pub models: Arc<PinnedSpecMap<ModelSpec>>,
    /// Pinned model pools reachable from the manifest. A pool shares the model
    /// id namespace, so durable runs resolve it like a model (ADR-0042).
    pub pools: Arc<PinnedSpecMap<ModelPoolSpec>>,
    /// Pinned provider specs reachable from the manifest.
    pub providers: Arc<PinnedSpecMap<ProviderSpec>>,
    /// Pinned skill specs reachable from the manifest.
    pub skills: Arc<PinnedSpecMap<SkillSpec>>,
    /// Pinned tool specs reachable from the manifest.
    pub tools: Arc<PinnedSpecMap<ToolSpec>>,
    /// Pinned plugin-config payloads. Stored as raw `serde_json::Value`
    /// because plugin_config payload shapes are extension-specific.
    pub plugin_configs: Arc<PinnedSpecMap<Value>>,
}

impl FrozenAgentRegistry {
    /// Build a `RegistrySet` suitable for `RunActivation.pinned_registry_set`
    /// (ADR-0035 D9). `live` supplies the runtime objects we cannot
    /// rebuild from specs alone (tool/provider/plugin executors / backend
    /// factories); agents and models are sourced from the frozen pins so
    /// resolution honors the pinned graph.
    #[must_use]
    pub fn to_registry_set(
        &self,
        live: &awaken_runtime::registry::RegistrySet,
    ) -> awaken_runtime::registry::RegistrySet {
        let models = Arc::new(PinnedModelRegistry::new(
            self.models.clone(),
            self.pools.clone(),
        )) as Arc<dyn awaken_runtime::registry::ModelRegistry>;
        awaken_runtime::registry::RegistrySet {
            agents: self.agents.clone() as Arc<dyn awaken_runtime::registry::AgentSpecRegistry>,
            models,
            tools: live.tools.clone(),
            providers: live.providers.clone(),
            plugins: live.plugins.clone(),
            backends: live.backends.clone(),
        }
    }
}

pub struct FrozenAgentRegistryMaterializer {
    store: Arc<dyn VersionedRegistryStore>,
    validator: StandardRegistryGraphValidator,
}

pub struct ScopedServerResolver {
    scope_id: ScopeId,
    materializer: FrozenAgentRegistryMaterializer,
    registry_handle: RegistryHandle,
}

#[derive(Clone)]
pub struct ScopedServerResolverFactory {
    store: Arc<dyn VersionedRegistryStore>,
    registry_handle: RegistryHandle,
}

impl ScopedServerResolverFactory {
    #[must_use]
    pub fn new(store: Arc<dyn VersionedRegistryStore>, registry_handle: RegistryHandle) -> Self {
        Self {
            store,
            registry_handle,
        }
    }

    #[must_use]
    pub fn resolver_for_scope(&self, scope_id: ScopeId) -> Arc<dyn Resolver> {
        Arc::new(ScopedServerResolver::new(
            scope_id,
            self.store.clone(),
            self.registry_handle.clone(),
        ))
    }

    #[must_use]
    pub fn resolver_for_context(&self, scope: &ScopeContext) -> Arc<dyn Resolver> {
        self.resolver_for_scope(scope.scope_id.clone())
    }
}

impl ScopedServerResolver {
    #[must_use]
    pub fn new(
        scope_id: ScopeId,
        store: Arc<dyn VersionedRegistryStore>,
        registry_handle: RegistryHandle,
    ) -> Self {
        Self {
            scope_id,
            materializer: FrozenAgentRegistryMaterializer::new(store),
            registry_handle,
        }
    }

    #[must_use]
    pub fn from_scope_context(
        scope: ScopeContext,
        store: Arc<dyn VersionedRegistryStore>,
        registry_handle: RegistryHandle,
    ) -> Self {
        Self::new(scope.scope_id, store, registry_handle)
    }

    #[must_use]
    pub fn scope_id(&self) -> &ScopeId {
        &self.scope_id
    }
}

#[async_trait::async_trait]
impl Resolver for ScopedServerResolver {
    async fn resolve(
        &self,
        mut request: ResolutionRequest,
    ) -> Result<ResolvedRunPlan, ResolveError> {
        let live = self.registry_handle.snapshot().into_registries();
        if matches!(request.resolution_scope, RegistryResolutionScope::Live)
            && request.features.requested_persistence == PersistenceRequirement::NotRequired
        {
            return awaken_runtime::registry::resolve::RegistrySetResolver::new(live)
                .resolve(request)
                .await;
        }
        let selector = match request.resolution_scope.clone() {
            RegistryResolutionScope::Live => VersionSelector::LatestPublication {
                scope_id: self.scope_id.as_str().to_string(),
            },
            // The runtime carries an opaque resolution id; for the published
            // path it is the publication snapshot version.
            RegistryResolutionScope::Pinned(resolution_id) => match resolution_id.parse::<u64>() {
                Ok(snapshot_version) => VersionSelector::Publication {
                    scope_id: self.scope_id.as_str().to_string(),
                    snapshot_version,
                },
                Err(error) => {
                    return Err(ResolveError::Runtime(format!(
                        "invalid pinned registry resolution id '{resolution_id}': {error}"
                    )));
                }
            },
        };
        let frozen = self
            .materializer
            .materialize(selector)
            .await
            .map_err(|error| ResolveError::Runtime(error.to_string()))?;
        let snapshot_version = frozen.manifest.registry_snapshot_version.ok_or_else(|| {
            ResolveError::Runtime(
                "published registry manifest is missing registry_snapshot_version".to_string(),
            )
        })?;
        request.resolution_scope = RegistryResolutionScope::Pinned(snapshot_version.to_string());
        awaken_runtime::registry::resolve::RegistrySetResolver::new_replayable_snapshot(
            frozen.to_registry_set(&live),
        )
        .resolve(request)
        .await
    }
}

pub struct LatestPublicationResolver {
    inner: ScopedServerResolver,
}

impl LatestPublicationResolver {
    #[must_use]
    pub fn new(
        scope_id: impl Into<String>,
        store: Arc<dyn VersionedRegistryStore>,
        registry_handle: RegistryHandle,
    ) -> Self {
        Self::try_new(scope_id, store, registry_handle).expect("scope_id must be valid")
    }

    pub fn try_new(
        scope_id: impl Into<String>,
        store: Arc<dyn VersionedRegistryStore>,
        registry_handle: RegistryHandle,
    ) -> Result<Self, ScopeError> {
        Ok(Self {
            inner: ScopedServerResolver::new(
                ScopeId::new(scope_id.into())?,
                store,
                registry_handle,
            ),
        })
    }

    #[must_use]
    pub fn scope_id(&self) -> &ScopeId {
        self.inner.scope_id()
    }
}

#[async_trait::async_trait]
impl Resolver for LatestPublicationResolver {
    async fn resolve(&self, request: ResolutionRequest) -> Result<ResolvedRunPlan, ResolveError> {
        self.inner.resolve(request).await
    }
}

impl FrozenAgentRegistryMaterializer {
    #[must_use]
    pub fn new(store: Arc<dyn VersionedRegistryStore>) -> Self {
        Self {
            validator: StandardRegistryGraphValidator::new(Arc::clone(&store)),
            store,
        }
    }

    pub async fn materialize(
        &self,
        selector: VersionSelector,
    ) -> Result<FrozenAgentRegistry, FrozenRegistryMaterializationError> {
        let base_manifest = self.base_manifest(&selector).await?;
        let scope_id = selector_scope_id(&selector);
        let report = self
            .validator
            .validate(RegistryGraphValidationRequest {
                root: selector,
                reference_policy: Default::default(),
            })
            .await?;
        let manifest = PinnedRegistryManifest {
            publication_id: base_manifest
                .as_ref()
                .and_then(|manifest| manifest.publication_id.clone()),
            registry_snapshot_version: base_manifest
                .as_ref()
                .and_then(|manifest| manifest.registry_snapshot_version),
            entries: report.entries.clone(),
        };
        let agents = self.load_pinned_agents(&scope_id, &report.entries).await?;
        let models = self
            .load_pinned_kind::<ModelSpec>(
                &scope_id,
                &report.entries,
                REGISTRY_KIND_MODEL,
                |spec| spec.id.clone(),
            )
            .await?;
        let pools = self
            .load_pinned_kind::<ModelPoolSpec>(
                &scope_id,
                &report.entries,
                REGISTRY_KIND_MODEL_POOL,
                |spec| spec.id.clone(),
            )
            .await?;
        let providers = self
            .load_pinned_kind::<ProviderSpec>(
                &scope_id,
                &report.entries,
                REGISTRY_KIND_PROVIDER,
                |spec| spec.id.clone(),
            )
            .await?;
        let skills = self
            .load_pinned_kind::<SkillSpec>(
                &scope_id,
                &report.entries,
                REGISTRY_KIND_SKILL,
                |spec| spec.id.clone(),
            )
            .await?;
        let tools = self
            .load_pinned_kind::<ToolSpec>(&scope_id, &report.entries, REGISTRY_KIND_TOOL, |spec| {
                spec.id.clone()
            })
            .await?;
        // plugin_config payloads have no canonical Rust type, so they are
        // keyed by the pinned entry id rather than an inner field.
        let plugin_configs = self
            .load_pinned_kind::<Value>(
                &scope_id,
                &report.entries,
                REGISTRY_KIND_PLUGIN_CONFIG,
                |_| String::new(),
            )
            .await?;
        Ok(FrozenAgentRegistry {
            manifest,
            agents: Arc::new(agents),
            models: Arc::new(models),
            pools: Arc::new(pools),
            providers: Arc::new(providers),
            skills: Arc::new(skills),
            tools: Arc::new(tools),
            plugin_configs: Arc::new(plugin_configs),
        })
    }

    async fn load_pinned_kind<T: DeserializeOwned>(
        &self,
        scope_id: &str,
        entries: &[PinnedRegistryEntry],
        kind: &'static str,
        spec_id: impl Fn(&T) -> String,
    ) -> Result<PinnedSpecMap<T>, FrozenRegistryMaterializationError> {
        let mut map: PinnedSpecMap<T> = PinnedSpecMap::new(kind);
        for entry in entries.iter().filter(|entry| entry.kind == kind) {
            let record = self
                .store
                .get(scope_id, &entry.kind, &entry.id, entry.version)
                .await?
                .ok_or_else(|| RegistryGraphValidationError::MissingVersion {
                    kind: entry.kind.clone(),
                    id: entry.id.clone(),
                    version: entry.version,
                })?;
            self.verify_record_against_entry(&record, entry)?;
            let spec: T = serde_json::from_value(record.value).map_err(|error| {
                RegistryGraphValidationError::InvalidReference {
                    kind: entry.kind.clone(),
                    id: entry.id.clone(),
                    reason: format!("invalid {kind} spec: {error}"),
                }
            })?;
            let derived_id = spec_id(&spec);
            let key = if derived_id.is_empty() {
                entry.id.clone()
            } else {
                derived_id
            };
            map.insert(key, spec, entry.clone())?;
        }
        Ok(map)
    }

    fn verify_record_against_entry(
        &self,
        record: &VersionedRecord<Value>,
        entry: &PinnedRegistryEntry,
    ) -> Result<(), FrozenRegistryMaterializationError> {
        record
            .verify_content_hash()
            .map_err(|error| RegistryGraphValidationError::Backend(error.to_string()))?;
        if record.content_hash != entry.content_hash {
            return Err(RegistryGraphValidationError::ContentHashMismatch {
                kind: entry.kind.clone(),
                id: entry.id.clone(),
                version: entry.version,
                expected: entry.content_hash.clone(),
                actual: record.content_hash.clone(),
            }
            .into());
        }
        Ok(())
    }

    async fn base_manifest(
        &self,
        selector: &VersionSelector,
    ) -> Result<Option<PinnedRegistryManifest>, FrozenRegistryMaterializationError> {
        match selector {
            VersionSelector::LatestPublication { scope_id } => Ok(self
                .store
                .latest_pinned_manifest(scope_id)
                .await?
                .ok_or_else(|| RegistryGraphValidationError::MissingResource {
                    kind: "publication".to_string(),
                    id: "latest".to_string(),
                })?
                .into()),
            VersionSelector::Publication {
                scope_id,
                snapshot_version,
            } => Ok(self
                .store
                .pinned_manifest_for_publication(scope_id, *snapshot_version)
                .await?
                .ok_or_else(|| RegistryGraphValidationError::MissingVersion {
                    kind: "publication".to_string(),
                    id: scope_id.clone(),
                    version: *snapshot_version,
                })?
                .into()),
            VersionSelector::Manifest { manifest, .. } => Ok(Some(manifest.clone())),
            VersionSelector::Exact { .. } => Ok(None),
        }
    }

    async fn load_pinned_agents(
        &self,
        scope_id: &str,
        entries: &[PinnedRegistryEntry],
    ) -> Result<PinnedAgentSpecRegistry, FrozenRegistryMaterializationError> {
        let mut pinned_agents = Vec::new();
        for entry in entries
            .iter()
            .filter(|entry| entry.kind == REGISTRY_KIND_AGENT)
        {
            let record = self
                .store
                .get(scope_id, &entry.kind, &entry.id, entry.version)
                .await?
                .ok_or_else(|| RegistryGraphValidationError::MissingVersion {
                    kind: entry.kind.clone(),
                    id: entry.id.clone(),
                    version: entry.version,
                })?;
            // ADR-0035 D9: resume must recompute the hash and reject any
            // record whose stored bytes no longer match its content_hash,
            // and also reject any drift between the pinned entry hash and
            // the stored hash. The graph validator already runs this check,
            // but loading happens separately and a concurrent column rewrite
            // would otherwise be loaded without notice.
            self.verify_record_against_entry(&record, entry)?;
            let spec = serde_json::from_value::<AgentSpec>(record.value).map_err(|error| {
                RegistryGraphValidationError::InvalidReference {
                    kind: entry.kind.clone(),
                    id: entry.id.clone(),
                    reason: format!("invalid AgentSpec: {error}"),
                }
            })?;
            pinned_agents.push((spec, entry.clone()));
        }
        if pinned_agents.is_empty() {
            return Err(FrozenRegistryMaterializationError::InvalidGraph(
                "frozen agent registry requires at least one agent".to_string(),
            ));
        }
        Ok(PinnedAgentSpecRegistry::from_pinned_agents(pinned_agents)?)
    }
}

fn selector_scope_id(selector: &VersionSelector) -> String {
    match selector {
        VersionSelector::LatestPublication { scope_id }
        | VersionSelector::Publication { scope_id, .. }
        | VersionSelector::Exact { scope_id, .. }
        | VersionSelector::Manifest { scope_id, .. } => scope_id.clone(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use awaken_runtime::registry::{
        AgentSpecRegistry, MapAgentSpecRegistry, MapModelRegistry, MapPluginSource,
        MapProviderRegistry, MapToolRegistry, RegistrySet,
    };
    use awaken_runtime::registry::{BackendRegistry, MapBackendRegistry};
    use awaken_runtime::resolution::{
        DelegatePersistence, ExecutionRole, HandoffTranscriptRef, ResolutionTarget, RunFeatureSet,
    };
    use awaken_server_contract::contract::executor::{
        InferenceExecutionError, InferenceRequest, LlmExecutor,
    };
    use awaken_server_contract::contract::identity::RunIdentity;
    use awaken_server_contract::contract::inference::{StopReason, StreamResult, TokenUsage};
    use awaken_server_contract::contract::versioned_registry::PublishOutcome;
    use awaken_server_contract::{ModelSpec, ProviderSpec, VersionRef};
    use awaken_stores::InMemoryVersionedRegistryStore;
    use serde_json::{Value, json};

    #[tokio::test]
    async fn materializes_latest_publication_into_pinned_agent_registry() {
        let store = InMemoryVersionedRegistryStore::new();
        let provider = publish_provider(&store, "provider-1").await;
        let model = publish_model(&store, "model-1", "provider-1").await;
        let delegate = publish_agent(&store, agent("delegate", "model-1", [])).await;
        let root = publish_agent(&store, agent("root", "model-1", ["delegate"])).await;
        store
            .create_publication(
                "default",
                "pub-1",
                refs([&provider, &model, &delegate, &root]),
                Vec::new(),
                None,
                json!({}),
            )
            .await
            .unwrap();

        let materializer = FrozenAgentRegistryMaterializer::new(Arc::new(store));
        let frozen = materializer
            .materialize(VersionSelector::LatestPublication {
                scope_id: "default".to_string(),
            })
            .await
            .unwrap();

        assert_eq!(frozen.manifest.publication_id.as_deref(), Some("pub-1"));
        assert_eq!(frozen.manifest.registry_snapshot_version, Some(1));
        assert_eq!(frozen.agents.get_agent("root").unwrap().id, "root");
        assert_eq!(
            frozen.agents.pin_for_agent("delegate").unwrap().version,
            delegate.version
        );
    }

    #[tokio::test]
    async fn scoped_server_resolver_resolves_delegate_from_current_scope() {
        let store = InMemoryVersionedRegistryStore::new();
        let provider = publish_provider(&store, "provider-1").await;
        let model = publish_model(&store, "model-1", "provider-1").await;
        let delegate = publish_agent(&store, agent("delegate", "model-1", [])).await;
        let root = publish_agent(&store, agent("root", "model-1", ["delegate"])).await;
        store
            .create_publication(
                "default",
                "pub-1",
                refs([&provider, &model, &delegate, &root]),
                Vec::new(),
                None,
                json!({}),
            )
            .await
            .unwrap();

        let resolver = ScopedServerResolver::from_scope_context(
            ScopeContext::default_scope(),
            Arc::new(store),
            live_registry_handle(),
        );
        let plan = resolver
            .resolve(nested_request(ResolutionTarget::Delegate {
                agent_id: "delegate".to_string(),
                parent_run: RunIdentity::for_thread("thread-1"),
                persistence: DelegatePersistence::Ephemeral,
            }))
            .await
            .unwrap();

        assert_eq!(resolver.scope_id().as_str(), "default");
        assert_eq!(plan.role(), ExecutionRole::Delegate);
        assert_eq!(plan.agent_spec().id, "delegate");
        // A published-registry resolution is replayable: it carries a
        // server-issued resolution id (the publication snapshot version).
        assert!(plan.resolution_id().is_some());
    }

    #[tokio::test]
    async fn scoped_server_resolver_resolves_handoff_from_current_scope() {
        let store = InMemoryVersionedRegistryStore::new();
        let provider = publish_provider(&store, "provider-1").await;
        let model = publish_model(&store, "model-1", "provider-1").await;
        let handoff = publish_agent(&store, agent("handoff", "model-1", [])).await;
        let root = publish_agent(&store, agent("root", "model-1", [])).await;
        store
            .create_publication(
                "default",
                "pub-1",
                refs([&provider, &model, &handoff, &root]),
                Vec::new(),
                None,
                json!({}),
            )
            .await
            .unwrap();

        let resolver = ScopedServerResolver::new(
            ScopeId::default_scope(),
            Arc::new(store),
            live_registry_handle(),
        );
        let plan = resolver
            .resolve(nested_request(ResolutionTarget::Handoff {
                agent_id: "handoff".to_string(),
                from_agent: "root".to_string(),
                transcript_ref: HandoffTranscriptRef {
                    run_id: "run-1".to_string(),
                },
            }))
            .await
            .unwrap();

        assert_eq!(plan.role(), ExecutionRole::Handoff);
        assert_eq!(plan.agent_spec().id, "handoff");
        assert!(matches!(plan, ResolvedRunPlan::Replayable(_)));
    }

    #[tokio::test]
    async fn scoped_server_resolver_round_trips_resolution_id_on_resume() {
        let store = InMemoryVersionedRegistryStore::new();
        let provider = publish_provider(&store, "provider-1").await;
        let model = publish_model(&store, "model-1", "provider-1").await;
        let root = publish_agent(&store, agent("root", "model-1", [])).await;
        store
            .create_publication(
                "default",
                "pub-1",
                refs([&provider, &model, &root]),
                Vec::new(),
                None,
                json!({}),
            )
            .await
            .unwrap();

        let resolver = ScopedServerResolver::new(
            ScopeId::default_scope(),
            Arc::new(store),
            live_registry_handle(),
        );

        // First (live) resolution pins the latest publication and surfaces an
        // opaque resolution id (the publication snapshot version).
        let plan = resolver
            .resolve(nested_request(ResolutionTarget::Root {
                agent_id: "root".into(),
                thread_id: "thread-1".into(),
            }))
            .await
            .unwrap();
        let resolution_id = plan
            .resolution_id()
            .expect("published resolution is pinned")
            .to_string();

        // Resuming with that id re-selects the same publication via the
        // `Pinned` path (VersionSelector::Publication), round-tripping the id.
        let mut resume = nested_request(ResolutionTarget::Root {
            agent_id: "root".into(),
            thread_id: "thread-1".into(),
        });
        resume.resolution_scope = RegistryResolutionScope::Pinned(resolution_id.clone());
        let resumed = resolver.resolve(resume).await.unwrap();
        assert_eq!(resumed.resolution_id(), Some(resolution_id.as_str()));
        assert_eq!(resumed.agent_spec().id, "root");
    }

    #[tokio::test]
    async fn scoped_server_resolver_rejects_invalid_pinned_resolution_id() {
        let store = InMemoryVersionedRegistryStore::new();
        let provider = publish_provider(&store, "provider-1").await;
        let model = publish_model(&store, "model-1", "provider-1").await;
        let root = publish_agent(&store, agent("root", "model-1", [])).await;
        store
            .create_publication(
                "default",
                "pub-1",
                refs([&provider, &model, &root]),
                Vec::new(),
                None,
                json!({}),
            )
            .await
            .unwrap();

        let resolver = ScopedServerResolver::new(
            ScopeId::default_scope(),
            Arc::new(store),
            live_registry_handle(),
        );
        let mut resume = nested_request(ResolutionTarget::Root {
            agent_id: "root".into(),
            thread_id: "thread-1".into(),
        });
        resume.resolution_scope = RegistryResolutionScope::Pinned("not-a-version".into());

        let error = match resolver.resolve(resume).await {
            Ok(_) => panic!("invalid pinned resolution id must fail"),
            Err(error) => error,
        };
        assert!(
            error
                .to_string()
                .contains("invalid pinned registry resolution id"),
            "unexpected error: {error}"
        );
    }

    #[tokio::test]
    async fn scoped_server_resolver_factory_binds_scope_per_resolver() {
        let store = Arc::new(InMemoryVersionedRegistryStore::new());
        let scope_a_provider = publish_provider_in_scope(&store, "scope-a", "provider-1").await;
        let scope_a_model =
            publish_model_in_scope(&store, "scope-a", "model-1", "provider-1").await;
        let scope_a_root =
            publish_agent_in_scope(&store, "scope-a", agent_with_prompt("root", "model-1", "a"))
                .await;
        store
            .create_publication(
                "scope-a",
                "pub-a",
                refs([&scope_a_provider, &scope_a_model, &scope_a_root]),
                Vec::new(),
                None,
                json!({}),
            )
            .await
            .unwrap();

        let scope_b_provider = publish_provider_in_scope(&store, "scope-b", "provider-1").await;
        let scope_b_model =
            publish_model_in_scope(&store, "scope-b", "model-1", "provider-1").await;
        let scope_b_root =
            publish_agent_in_scope(&store, "scope-b", agent_with_prompt("root", "model-1", "b"))
                .await;
        store
            .create_publication(
                "scope-b",
                "pub-b",
                refs([&scope_b_provider, &scope_b_model, &scope_b_root]),
                Vec::new(),
                None,
                json!({}),
            )
            .await
            .unwrap();

        let factory = ScopedServerResolverFactory::new(store, live_registry_handle());
        let plan_a = factory
            .resolver_for_scope(ScopeId::new("scope-a").unwrap())
            .resolve(nested_request(ResolutionTarget::Root {
                agent_id: "root".into(),
                thread_id: "thread-a".into(),
            }))
            .await
            .unwrap();
        let plan_b = factory
            .resolver_for_scope(ScopeId::new("scope-b").unwrap())
            .resolve(nested_request(ResolutionTarget::Root {
                agent_id: "root".into(),
                thread_id: "thread-b".into(),
            }))
            .await
            .unwrap();

        assert_eq!(plan_a.agent_spec().system_prompt, "a");
        assert_eq!(plan_b.agent_spec().system_prompt, "b");
    }

    #[tokio::test]
    async fn latest_publication_resolver_rejects_invalid_scope_id() {
        let store = InMemoryVersionedRegistryStore::new();
        let result =
            LatestPublicationResolver::try_new(" ", Arc::new(store), live_registry_handle());

        assert!(matches!(result, Err(ScopeError::Empty)));
    }

    #[tokio::test]
    async fn materializes_pool_reachable_from_agent() {
        let store = InMemoryVersionedRegistryStore::new();
        let provider = publish_provider(&store, "provider-1").await;
        let m0 = publish_model(&store, "m0", "provider-1").await;
        let m1 = publish_model(&store, "m1", "provider-1").await;
        let pool = publish_model_pool(&store, "pool-1", ["m0", "m1"]).await;
        let root = publish_agent(&store, agent("root", "pool-1", [])).await;
        store
            .create_publication(
                "default",
                "pub-1",
                refs([&provider, &m0, &m1, &pool, &root]),
                Vec::new(),
                None,
                json!({}),
            )
            .await
            .unwrap();

        let materializer = FrozenAgentRegistryMaterializer::new(Arc::new(store));
        let frozen = materializer
            .materialize(VersionSelector::LatestPublication {
                scope_id: "default".to_string(),
            })
            .await
            .unwrap();

        // The pool and its member models are frozen for the run.
        assert_eq!(frozen.pools.get("pool-1").unwrap().members.len(), 2);
        assert!(frozen.models.get("m0").is_some());
        assert!(frozen.models.get("m1").is_some());
        assert!(frozen.manifest.entries.iter().any(|entry| {
            entry.kind == awaken_server_contract::REGISTRY_KIND_MODEL_POOL && entry.id == "pool-1"
        }));
    }

    #[tokio::test]
    async fn materializes_exact_agent_with_current_references() {
        let store = InMemoryVersionedRegistryStore::new();
        publish_provider(&store, "provider-1").await;
        publish_model(&store, "model-1", "provider-1").await;
        let root = publish_agent(&store, agent("root", "model-1", [])).await;

        let materializer = FrozenAgentRegistryMaterializer::new(Arc::new(store));
        let frozen = materializer
            .materialize(VersionSelector::Exact {
                scope_id: "default".to_string(),
                kind: "agent".to_string(),
                id: "root".to_string(),
                version: root.version,
            })
            .await
            .unwrap();

        assert!(frozen.manifest.publication_id.is_none());
        assert_eq!(frozen.agents.pin_for_agent("root").unwrap().version, 1);
        assert!(frozen.manifest.entries.iter().any(|entry| {
            entry.kind == awaken_server_contract::REGISTRY_KIND_MODEL && entry.id == "model-1"
        }));
    }

    #[tokio::test]
    async fn rejects_graphs_without_agents() {
        let store = InMemoryVersionedRegistryStore::new();
        let provider = publish_provider(&store, "provider-1").await;
        let manifest = PinnedRegistryManifest {
            publication_id: None,
            registry_snapshot_version: None,
            entries: vec![provider],
        };
        let materializer = FrozenAgentRegistryMaterializer::new(Arc::new(store));
        let error = materialization_error(
            materializer
                .materialize(VersionSelector::Manifest {
                    scope_id: "default".to_string(),
                    manifest,
                })
                .await,
        );

        assert!(matches!(
            error,
            FrozenRegistryMaterializationError::InvalidGraph(message)
                if message.contains("at least one agent")
        ));
    }

    /// ADR-0035 D9: the resume path must reject a manifest whose stored
    /// `content_hash` diverges from the published canonical bytes. The
    /// validator alone is unit-tested in `registry_graph_validator.rs`,
    /// but only the materializer guarantees that the resume entry point
    /// actually runs the check before handing a frozen registry to the
    /// runtime.
    #[tokio::test]
    async fn materialize_rejects_manifest_drift() {
        let store = InMemoryVersionedRegistryStore::new();
        let provider = publish_provider(&store, "provider-1").await;
        let model = publish_model(&store, "model-1", "provider-1").await;
        let root = publish_agent(&store, agent("root", "model-1", [])).await;

        let mut tampered_root = root.clone();
        tampered_root.content_hash = "sha256:deadbeef".to_string();
        let manifest = PinnedRegistryManifest {
            publication_id: None,
            registry_snapshot_version: None,
            entries: vec![tampered_root, model, provider],
        };

        let materializer = FrozenAgentRegistryMaterializer::new(Arc::new(store));
        let error = materialization_error(
            materializer
                .materialize(VersionSelector::Manifest {
                    scope_id: "default".to_string(),
                    manifest,
                })
                .await,
        );

        match error {
            FrozenRegistryMaterializationError::Graph(
                RegistryGraphValidationError::ContentHashMismatch {
                    kind, id, expected, ..
                },
            ) => {
                assert_eq!(kind, "agent");
                assert_eq!(id, "root");
                assert_eq!(expected, "sha256:deadbeef");
            }
            other => panic!("expected Graph(ContentHashMismatch), got {other:?}"),
        }
    }

    struct StubExecutor;

    #[async_trait::async_trait]
    impl LlmExecutor for StubExecutor {
        async fn execute(
            &self,
            _request: InferenceRequest,
        ) -> Result<StreamResult, InferenceExecutionError> {
            Ok(StreamResult {
                content: Vec::new(),
                tool_calls: Vec::new(),
                usage: Some(TokenUsage::default()),
                stop_reason: Some(StopReason::EndTurn),
                has_incomplete_tool_calls: false,
            })
        }

        fn name(&self) -> &str {
            "stub"
        }
    }

    fn nested_request(target: ResolutionTarget) -> ResolutionRequest {
        ResolutionRequest {
            target,
            resolution_scope: RegistryResolutionScope::Live,
            overrides: None,
            frontend_tools: Vec::new(),
            features: RunFeatureSet {
                requested_persistence: PersistenceRequirement::CheckpointRequired,
                ..Default::default()
            },
        }
    }

    fn live_registry_handle() -> RegistryHandle {
        let mut providers = MapProviderRegistry::new();
        providers
            .register_provider("provider-1", Arc::new(StubExecutor))
            .unwrap();
        RegistryHandle::new(RegistrySet {
            agents: Arc::new(MapAgentSpecRegistry::new()),
            tools: Arc::new(MapToolRegistry::new()),
            models: Arc::new(MapModelRegistry::new()),
            providers: Arc::new(providers),
            plugins: Arc::new(MapPluginSource::new()),
            backends: Arc::new(MapBackendRegistry::with_default_remote_backends())
                as Arc<dyn BackendRegistry>,
        })
    }

    async fn publish_agent(
        store: &InMemoryVersionedRegistryStore,
        spec: AgentSpec,
    ) -> PinnedRegistryEntry {
        let id = spec.id.clone();
        publish(store, "agent", &id, serde_json::to_value(spec).unwrap()).await
    }

    async fn publish_agent_in_scope(
        store: &InMemoryVersionedRegistryStore,
        scope_id: &str,
        spec: AgentSpec,
    ) -> PinnedRegistryEntry {
        let id = spec.id.clone();
        publish_in_scope(
            store,
            scope_id,
            "agent",
            &id,
            serde_json::to_value(spec).unwrap(),
        )
        .await
    }

    async fn publish_model(
        store: &InMemoryVersionedRegistryStore,
        id: &str,
        provider_id: &str,
    ) -> PinnedRegistryEntry {
        let spec = ModelSpec::new(id, provider_id, "upstream");
        publish(store, "model", id, serde_json::to_value(spec).unwrap()).await
    }

    async fn publish_model_in_scope(
        store: &InMemoryVersionedRegistryStore,
        scope_id: &str,
        id: &str,
        provider_id: &str,
    ) -> PinnedRegistryEntry {
        let spec = ModelSpec::new(id, provider_id, "upstream");
        publish_in_scope(
            store,
            scope_id,
            "model",
            id,
            serde_json::to_value(spec).unwrap(),
        )
        .await
    }

    async fn publish_model_pool<'a>(
        store: &InMemoryVersionedRegistryStore,
        id: &str,
        members: impl IntoIterator<Item = &'a str>,
    ) -> PinnedRegistryEntry {
        let spec = ModelPoolSpec::new(id, members);
        publish(store, "model_pool", id, serde_json::to_value(spec).unwrap()).await
    }

    async fn publish_provider(
        store: &InMemoryVersionedRegistryStore,
        id: &str,
    ) -> PinnedRegistryEntry {
        let spec = ProviderSpec {
            id: id.to_string(),
            adapter: "openai".to_string(),
            ..Default::default()
        };
        publish(store, "provider", id, serde_json::to_value(spec).unwrap()).await
    }

    async fn publish_provider_in_scope(
        store: &InMemoryVersionedRegistryStore,
        scope_id: &str,
        id: &str,
    ) -> PinnedRegistryEntry {
        let spec = ProviderSpec {
            id: id.to_string(),
            adapter: "openai".to_string(),
            ..Default::default()
        };
        publish_in_scope(
            store,
            scope_id,
            "provider",
            id,
            serde_json::to_value(spec).unwrap(),
        )
        .await
    }

    async fn publish(
        store: &InMemoryVersionedRegistryStore,
        kind: &str,
        id: &str,
        value: Value,
    ) -> PinnedRegistryEntry {
        publish_in_scope(store, "default", kind, id, value).await
    }

    async fn publish_in_scope(
        store: &InMemoryVersionedRegistryStore,
        scope_id: &str,
        kind: &str,
        id: &str,
        value: Value,
    ) -> PinnedRegistryEntry {
        let outcome = store
            .publish_resource(scope_id, kind, id, value, 1, json!({}))
            .await
            .unwrap();
        let record = match outcome {
            PublishOutcome::Created(record) | PublishOutcome::Noop(record) => record,
        };
        PinnedRegistryEntry {
            kind: kind.to_string(),
            id: id.to_string(),
            version: record.version,
            content_hash: record.content_hash,
        }
    }

    fn agent<'a>(
        id: &str,
        model_id: &str,
        delegates: impl IntoIterator<Item = &'a str>,
    ) -> AgentSpec {
        AgentSpec {
            id: id.to_string(),
            model_id: model_id.to_string(),
            system_prompt: "system".to_string(),
            delegates: delegates.into_iter().map(str::to_string).collect(),
            ..Default::default()
        }
    }

    fn agent_with_prompt(id: &str, model_id: &str, system_prompt: &str) -> AgentSpec {
        AgentSpec {
            id: id.to_string(),
            model_id: model_id.to_string(),
            system_prompt: system_prompt.to_string(),
            ..Default::default()
        }
    }

    fn refs<'a>(entries: impl IntoIterator<Item = &'a PinnedRegistryEntry>) -> Vec<VersionRef> {
        entries
            .into_iter()
            .map(|entry| VersionRef {
                kind: entry.kind.clone(),
                id: entry.id.clone(),
                version: entry.version,
            })
            .collect()
    }

    fn materialization_error(
        result: Result<FrozenAgentRegistry, FrozenRegistryMaterializationError>,
    ) -> FrozenRegistryMaterializationError {
        match result {
            Ok(_) => panic!("expected frozen registry materialization error"),
            Err(error) => error,
        }
    }
}