awaken-server-contract 0.6.0

Server and store boundary contracts 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
//! Published registry graph validation contracts and default validator.

use std::collections::{BTreeMap, HashMap, HashSet};
use std::sync::Arc;

use async_trait::async_trait;
use futures::future::BoxFuture;
use serde::{Deserialize, Serialize};
use thiserror::Error;

use awaken_runtime_contract::registry_spec::{AgentSpec, ModelPoolSpec, ModelSpec};

use super::versioned_registry::{
    PinnedRegistryEntry, PinnedRegistryManifest, VersionRef, VersionedRecord,
    VersionedRegistryError, VersionedRegistryStore,
};

// Pinnable kinds share one source of truth with the manifest builder.
pub use super::pinned_registry::{
    REGISTRY_KIND_AGENT, REGISTRY_KIND_MODEL, REGISTRY_KIND_MODEL_POOL, REGISTRY_KIND_PROVIDER,
};

// Server-only registry kinds (validated/published but not pinned in run manifests).
pub const REGISTRY_KIND_SKILL: &str = "skill";
pub const REGISTRY_KIND_TOOL: &str = "tool";
pub const REGISTRY_KIND_PLUGIN_CONFIG: &str = "plugin_config";

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum VersionSelector {
    LatestPublication {
        scope_id: String,
    },
    Publication {
        scope_id: String,
        snapshot_version: u64,
    },
    Exact {
        scope_id: String,
        kind: String,
        id: String,
        version: u64,
    },
    Manifest {
        scope_id: String,
        manifest: PinnedRegistryManifest,
    },
}

#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq, Eq)]
pub enum RegistryReferencePolicy {
    #[default]
    SameScopeOnly,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct RegistryGraphValidationRequest {
    pub root: VersionSelector,
    #[serde(default)]
    pub reference_policy: RegistryReferencePolicy,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct RegistryGraphValidationReport {
    pub entries: Vec<PinnedRegistryEntry>,
}

#[derive(Debug, Clone, Error, Serialize, Deserialize, PartialEq, Eq)]
pub enum RegistryGraphValidationError {
    #[error("missing registry resource {kind}/{id}")]
    MissingResource { kind: String, id: String },
    #[error("missing registry version {kind}/{id}@{version}")]
    MissingVersion {
        kind: String,
        id: String,
        version: u64,
    },
    #[error("archived registry reference {kind}/{id}{version_suffix}", version_suffix = version.map_or(String::new(), |version| format!("@{version}")))]
    ArchivedReference {
        kind: String,
        id: String,
        version: Option<u64>,
    },
    #[error(
        "content hash mismatch for {kind}/{id}@{version}: expected {expected}, actual {actual}"
    )]
    ContentHashMismatch {
        kind: String,
        id: String,
        version: u64,
        expected: String,
        actual: String,
    },
    #[error("registry graph cycle detected: {path:?}")]
    CycleDetected { path: Vec<VersionRef> },
    #[error("invalid registry reference {kind}/{id}: {reason}")]
    InvalidReference {
        kind: String,
        id: String,
        reason: String,
    },
    #[error("registry validation backend error: {0}")]
    Backend(String),
}

impl From<VersionedRegistryError> for RegistryGraphValidationError {
    fn from(error: VersionedRegistryError) -> Self {
        Self::Backend(error.to_string())
    }
}

#[async_trait]
pub trait RegistryGraphValidator: Send + Sync {
    async fn validate(
        &self,
        request: RegistryGraphValidationRequest,
    ) -> Result<RegistryGraphValidationReport, RegistryGraphValidationError>;
}

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

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

    async fn root_context(
        &self,
        root: VersionSelector,
    ) -> Result<ValidationContext, RegistryGraphValidationError> {
        match root {
            VersionSelector::LatestPublication { scope_id } => {
                let manifest = self
                    .store
                    .latest_pinned_manifest(&scope_id)
                    .await?
                    .ok_or_else(|| RegistryGraphValidationError::MissingResource {
                        kind: "publication".to_string(),
                        id: "latest".to_string(),
                    })?;
                ValidationContext::from_manifest(scope_id, manifest, false, true)
            }
            VersionSelector::Publication {
                scope_id,
                snapshot_version,
            } => {
                let manifest = 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,
                    })?;
                ValidationContext::from_manifest(scope_id, manifest, false, false)
            }
            VersionSelector::Manifest { scope_id, manifest } => {
                ValidationContext::from_manifest(scope_id, manifest, false, false)
            }
            VersionSelector::Exact {
                scope_id,
                kind,
                id,
                version,
            } => {
                let record = self.load_record(&scope_id, &kind, &id, version).await?;
                let entry = PinnedRegistryEntry {
                    kind,
                    id,
                    version,
                    content_hash: record.content_hash,
                };
                ValidationContext::from_entries(scope_id, vec![entry], true, false)
            }
        }
    }

    async fn load_record(
        &self,
        scope_id: &str,
        kind: &str,
        id: &str,
        version: u64,
    ) -> Result<VersionedRecord<serde_json::Value>, RegistryGraphValidationError> {
        self.store
            .get(scope_id, kind, id, version)
            .await?
            .ok_or_else(|| RegistryGraphValidationError::MissingVersion {
                kind: kind.to_string(),
                id: id.to_string(),
                version,
            })
    }

    fn validate_entry<'a>(
        &'a self,
        context: &'a mut ValidationContext,
        entry: PinnedRegistryEntry,
    ) -> BoxFuture<'a, Result<(), RegistryGraphValidationError>> {
        Box::pin(async move {
            let key = ResourceKey::from_entry(&entry);
            if let Some(existing) = context.accepted.get(&key) {
                if existing.version == entry.version && existing.content_hash == entry.content_hash
                {
                    return Ok(());
                }
                return Err(RegistryGraphValidationError::InvalidReference {
                    kind: entry.kind,
                    id: entry.id,
                    reason: "conflicting versions for the same resource".to_string(),
                });
            }

            if let Some(position) = context.visiting.iter().position(|visited| {
                visited.kind == entry.kind
                    && visited.id == entry.id
                    && visited.version == entry.version
            }) {
                let mut path = context.visiting[position..].to_vec();
                path.push(VersionRef {
                    kind: entry.kind,
                    id: entry.id,
                    version: entry.version,
                });
                return Err(RegistryGraphValidationError::CycleDetected { path });
            }

            let record = self
                .validate_stored_entry(
                    &context.scope_id,
                    &entry,
                    context.reject_archived_explicit_entries,
                )
                .await?;
            context.visiting.push(VersionRef {
                kind: entry.kind.clone(),
                id: entry.id.clone(),
                version: entry.version,
            });
            let dependencies = self
                .dependencies_for_record(context, &entry, &record)
                .await?;
            for dependency in dependencies {
                self.validate_entry(context, dependency).await?;
            }
            context.visiting.pop();
            context.accepted.insert(key, entry);
            Ok(())
        })
    }

    async fn validate_stored_entry(
        &self,
        scope_id: &str,
        entry: &PinnedRegistryEntry,
        reject_archived: bool,
    ) -> Result<VersionedRecord<serde_json::Value>, RegistryGraphValidationError> {
        if reject_archived {
            self.reject_archived(scope_id, &entry.kind, &entry.id, Some(entry.version))
                .await?;
        }
        let record = self
            .load_record(scope_id, &entry.kind, &entry.id, entry.version)
            .await?;
        // ADR-0035 D3/D9: re-derive the SHA-256 from canonical_json_bytes
        // and compare to the stored content_hash before trusting either
        // column. Without this the manifest hash becomes decorative.
        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,
            });
        }
        Ok(record)
    }

    async fn reject_archived(
        &self,
        scope_id: &str,
        kind: &str,
        id: &str,
        version: Option<u64>,
    ) -> Result<(), RegistryGraphValidationError> {
        let state = self.store.resource_state(scope_id, kind, id).await?;
        let state = state.ok_or_else(|| RegistryGraphValidationError::MissingResource {
            kind: kind.to_string(),
            id: id.to_string(),
        })?;
        if state.archived_at_ms.is_some() {
            return Err(RegistryGraphValidationError::ArchivedReference {
                kind: kind.to_string(),
                id: id.to_string(),
                version,
            });
        }
        Ok(())
    }

    async fn dependencies_for_record(
        &self,
        context: &ValidationContext,
        entry: &PinnedRegistryEntry,
        record: &VersionedRecord<serde_json::Value>,
    ) -> Result<Vec<PinnedRegistryEntry>, RegistryGraphValidationError> {
        match entry.kind.as_str() {
            REGISTRY_KIND_AGENT => self.agent_dependencies(context, entry, record).await,
            REGISTRY_KIND_MODEL => self.model_dependencies(context, entry, record).await,
            REGISTRY_KIND_MODEL_POOL => self.model_pool_dependencies(context, entry, record).await,
            REGISTRY_KIND_PROVIDER
            | REGISTRY_KIND_SKILL
            | REGISTRY_KIND_TOOL
            | REGISTRY_KIND_PLUGIN_CONFIG => Ok(Vec::new()),
            _ => Err(RegistryGraphValidationError::InvalidReference {
                kind: entry.kind.clone(),
                id: entry.id.clone(),
                reason: "unsupported registry kind".to_string(),
            }),
        }
    }

    async fn agent_dependencies(
        &self,
        context: &ValidationContext,
        entry: &PinnedRegistryEntry,
        record: &VersionedRecord<serde_json::Value>,
    ) -> Result<Vec<PinnedRegistryEntry>, RegistryGraphValidationError> {
        let spec = serde_json::from_value::<AgentSpec>(record.value.clone()).map_err(|error| {
            RegistryGraphValidationError::InvalidReference {
                kind: entry.kind.clone(),
                id: entry.id.clone(),
                reason: format!("invalid AgentSpec: {error}"),
            }
        })?;
        if spec.id != entry.id {
            return Err(RegistryGraphValidationError::InvalidReference {
                kind: entry.kind.clone(),
                id: entry.id.clone(),
                reason: format!("AgentSpec.id {} does not match registry id", spec.id),
            });
        }

        let mut dependencies = Vec::new();
        if spec.endpoint.is_none() {
            dependencies.push(
                self.resolve_model_or_pool_reference(context, &spec.model_id)
                    .await?,
            );
        }
        for delegate_id in &spec.delegates {
            dependencies.push(
                self.resolve_reference_entry(context, REGISTRY_KIND_AGENT, delegate_id)
                    .await?,
            );
        }
        Ok(dependencies)
    }

    async fn model_dependencies(
        &self,
        context: &ValidationContext,
        entry: &PinnedRegistryEntry,
        record: &VersionedRecord<serde_json::Value>,
    ) -> Result<Vec<PinnedRegistryEntry>, RegistryGraphValidationError> {
        let spec = serde_json::from_value::<ModelSpec>(record.value.clone()).map_err(|error| {
            RegistryGraphValidationError::InvalidReference {
                kind: entry.kind.clone(),
                id: entry.id.clone(),
                reason: format!("invalid ModelSpec: {error}"),
            }
        })?;
        if spec.id != entry.id {
            return Err(RegistryGraphValidationError::InvalidReference {
                kind: entry.kind.clone(),
                id: entry.id.clone(),
                reason: format!("ModelSpec.id {} does not match registry id", spec.id),
            });
        }
        Ok(vec![
            self.resolve_reference_entry(context, REGISTRY_KIND_PROVIDER, &spec.provider_id)
                .await?,
        ])
    }

    async fn model_pool_dependencies(
        &self,
        context: &ValidationContext,
        entry: &PinnedRegistryEntry,
        record: &VersionedRecord<serde_json::Value>,
    ) -> Result<Vec<PinnedRegistryEntry>, RegistryGraphValidationError> {
        let spec =
            serde_json::from_value::<ModelPoolSpec>(record.value.clone()).map_err(|error| {
                RegistryGraphValidationError::InvalidReference {
                    kind: entry.kind.clone(),
                    id: entry.id.clone(),
                    reason: format!("invalid ModelPoolSpec: {error}"),
                }
            })?;
        if spec.id != entry.id {
            return Err(RegistryGraphValidationError::InvalidReference {
                kind: entry.kind.clone(),
                id: entry.id.clone(),
                reason: format!("ModelPoolSpec.id {} does not match registry id", spec.id),
            });
        }
        let mut dependencies = Vec::with_capacity(spec.members.len());
        for member in &spec.members {
            dependencies.push(
                self.resolve_reference_entry(context, REGISTRY_KIND_MODEL, &member.model_id)
                    .await?,
            );
        }
        Ok(dependencies)
    }

    /// Resolve an agent's `model_id`, which may name either a single model or a
    /// pool (pools share the model id namespace). An id that resolves to *both*
    /// a model and a pool is ambiguous and rejected, matching the runtime
    /// resolver's `AmbiguousModelReference`; resolving to exactly one is
    /// returned; resolving to neither reports a missing model.
    async fn resolve_model_or_pool_reference(
        &self,
        context: &ValidationContext,
        id: &str,
    ) -> Result<PinnedRegistryEntry, RegistryGraphValidationError> {
        let model = self
            .try_reference_entry(context, REGISTRY_KIND_MODEL, id)
            .await?;
        let pool = self
            .try_reference_entry(context, REGISTRY_KIND_MODEL_POOL, id)
            .await?;
        match (model, pool) {
            (Some(_), Some(_)) => Err(RegistryGraphValidationError::InvalidReference {
                kind: REGISTRY_KIND_MODEL.to_string(),
                id: id.to_string(),
                reason: "id resolves to both a model and a model pool".to_string(),
            }),
            (Some(entry), None) | (None, Some(entry)) => Ok(entry),
            (None, None) => Err(RegistryGraphValidationError::MissingResource {
                kind: REGISTRY_KIND_MODEL.to_string(),
                id: id.to_string(),
            }),
        }
    }

    /// Like [`resolve_reference_entry`](Self::resolve_reference_entry) but
    /// returns `Ok(None)` when the resource is absent instead of an error, so a
    /// caller can try an alternative kind. Other failures (archived, store)
    /// still propagate.
    async fn try_reference_entry(
        &self,
        context: &ValidationContext,
        kind: &str,
        id: &str,
    ) -> Result<Option<PinnedRegistryEntry>, RegistryGraphValidationError> {
        let key = ResourceKey::new(kind, id);
        if let Some(entry) = context.candidate_entries.get(&key) {
            return Ok(Some(entry.clone()));
        }
        if !context.allow_current_reference_resolution {
            return Ok(None);
        }
        // Honor the soft contract: a resource that simply does not exist for
        // this kind is `Ok(None)` so the caller can try the other kind (an id
        // may name a model OR a pool). `reject_archived` reports absence as
        // `MissingResource`, so probe existence first and only run the archived
        // check when the resource is actually present — an archived resource
        // still propagates as an error.
        if self
            .store
            .resource_state(&context.scope_id, kind, id)
            .await?
            .is_none()
        {
            return Ok(None);
        }
        self.reject_archived(&context.scope_id, kind, id, None)
            .await?;
        let Some(record) = self.store.current(&context.scope_id, kind, id).await? else {
            return Ok(None);
        };
        Ok(Some(PinnedRegistryEntry {
            kind: kind.to_string(),
            id: id.to_string(),
            version: record.version,
            content_hash: record.content_hash,
        }))
    }

    async fn resolve_reference_entry(
        &self,
        context: &ValidationContext,
        kind: &str,
        id: &str,
    ) -> Result<PinnedRegistryEntry, RegistryGraphValidationError> {
        let key = ResourceKey::new(kind, id);
        if let Some(entry) = context.candidate_entries.get(&key) {
            return Ok(entry.clone());
        }
        // ADR-0035 D9: pinned-manifest/publication validation must fail
        // closed when a transitive reference is absent. Only `Exact` opts
        // into expanding through the store's current pointer, and its
        // output is frozen into a manifest before execution begins.
        if !context.allow_current_reference_resolution {
            return Err(RegistryGraphValidationError::MissingResource {
                kind: kind.to_string(),
                id: id.to_string(),
            });
        }
        self.reject_archived(&context.scope_id, kind, id, None)
            .await?;
        let record = self
            .store
            .current(&context.scope_id, kind, id)
            .await?
            .ok_or_else(|| RegistryGraphValidationError::MissingResource {
                kind: kind.to_string(),
                id: id.to_string(),
            })?;
        Ok(PinnedRegistryEntry {
            kind: kind.to_string(),
            id: id.to_string(),
            version: record.version,
            content_hash: record.content_hash,
        })
    }
}

#[async_trait]
impl RegistryGraphValidator for StandardRegistryGraphValidator {
    async fn validate(
        &self,
        request: RegistryGraphValidationRequest,
    ) -> Result<RegistryGraphValidationReport, RegistryGraphValidationError> {
        match request.reference_policy {
            RegistryReferencePolicy::SameScopeOnly => {}
        }
        let mut context = self.root_context(request.root).await?;
        let roots = context.root_entries.clone();
        for entry in roots {
            self.validate_entry(&mut context, entry).await?;
        }
        Ok(context.into_report())
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct ResourceKey {
    kind: String,
    id: String,
}

impl ResourceKey {
    fn new(kind: &str, id: &str) -> Self {
        Self {
            kind: kind.to_string(),
            id: id.to_string(),
        }
    }

    fn from_entry(entry: &PinnedRegistryEntry) -> Self {
        Self::new(&entry.kind, &entry.id)
    }
}

struct ValidationContext {
    scope_id: String,
    root_entries: Vec<PinnedRegistryEntry>,
    candidate_entries: HashMap<ResourceKey, PinnedRegistryEntry>,
    accepted: BTreeMap<ResourceKey, PinnedRegistryEntry>,
    visiting: Vec<VersionRef>,
    /// Allow the validator to resolve missing references against the
    /// store's current pointer. ADR-0035 D9 forbids this for run-time
    /// resume materialization (Manifest, Publication, LatestPublication
    /// must observe only the frozen entries). It is only enabled for
    /// `Exact`, where the caller asked to expand a single root into its
    /// reachable graph and the resulting `report.entries` will be saved
    /// as the persisted manifest before run execution starts.
    allow_current_reference_resolution: bool,
    reject_archived_explicit_entries: bool,
}

impl ValidationContext {
    fn from_manifest(
        scope_id: String,
        manifest: PinnedRegistryManifest,
        allow_current_reference_resolution: bool,
        reject_archived_explicit_entries: bool,
    ) -> Result<Self, RegistryGraphValidationError> {
        Self::from_entries(
            scope_id,
            manifest.entries,
            allow_current_reference_resolution,
            reject_archived_explicit_entries,
        )
    }

    fn from_entries(
        scope_id: String,
        entries: Vec<PinnedRegistryEntry>,
        allow_current_reference_resolution: bool,
        reject_archived_explicit_entries: bool,
    ) -> Result<Self, RegistryGraphValidationError> {
        let mut candidate_entries = HashMap::new();
        let mut seen = HashSet::new();
        for entry in &entries {
            if entry.version == 0 {
                return Err(RegistryGraphValidationError::InvalidReference {
                    kind: entry.kind.clone(),
                    id: entry.id.clone(),
                    reason: "version cannot be 0".to_string(),
                });
            }
            let key = ResourceKey::from_entry(entry);
            if !seen.insert(key.clone()) {
                return Err(RegistryGraphValidationError::InvalidReference {
                    kind: entry.kind.clone(),
                    id: entry.id.clone(),
                    reason: "duplicate manifest entry".to_string(),
                });
            }
            candidate_entries.insert(key, entry.clone());
        }
        Ok(Self {
            scope_id,
            root_entries: entries,
            candidate_entries,
            accepted: BTreeMap::new(),
            visiting: Vec::new(),
            allow_current_reference_resolution,
            reject_archived_explicit_entries,
        })
    }

    fn into_report(self) -> RegistryGraphValidationReport {
        RegistryGraphValidationReport {
            entries: self.accepted.into_values().collect(),
        }
    }
}