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
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use std::time::{Duration, SystemTime};

use awaken_runtime::registry::ModelCapabilityPatch;
use awaken_server_contract::ProviderSpec;
use awaken_server_contract::contract::executor::LlmExecutor;

/// Per-provider executor cache entry: the spec used to build the cached
/// executor and the executor itself.
pub(super) type ProviderExecutorCache = HashMap<String, (ProviderSpec, Arc<dyn LlmExecutor>)>;

/// Maximum age a provider-discovered capability snapshot may reach before it is
/// no longer served as runtime-trusted data. Discovery runs on every config
/// publish, so a successful provider stays refreshed well inside this window;
/// the TTL exists to bound how long a *stale* snapshot can keep driving the
/// modality guard and knowledge-cutoff context after the provider stops
/// returning usable `/models` data (discovery failures retain the last
/// snapshot, but only until it expires). Twelve hours keeps a provider that is
/// briefly unreachable trusted across short outages while ensuring metadata
/// that the provider has silently changed cannot stay trusted indefinitely.
const CAPABILITY_SNAPSHOT_TTL: Duration = Duration::from_secs(12 * 60 * 60);

/// Cached provider capability snapshot tagged with the provider signature it was
/// discovered under and the time it was discovered, so stale snapshots can be
/// expired by age.
#[derive(Clone)]
struct CachedCapabilitySnapshot {
    signature: String,
    discovered_at: SystemTime,
    capabilities: HashMap<String, ModelCapabilityPatch>,
}

impl CachedCapabilitySnapshot {
    fn is_expired(&self, now: SystemTime, ttl: Duration) -> bool {
        now.duration_since(self.discovered_at)
            .map(|age| age > ttl)
            // A `discovered_at` in the future (clock moved backwards) is not
            // older than the TTL, so treat it as still fresh.
            .unwrap_or(false)
    }
}

type ProviderCapabilityCache = HashMap<String, CachedCapabilitySnapshot>;

/// A computed-but-uncommitted capability cache. `resolved` is the snapshot map
/// handed to compilation; `cache` is the next cache state, committed via
/// [`ProviderRuntimeCache::commit_capabilities`] only once the publish
/// transaction (versioned publish + runtime swap) has succeeded — so a failed
/// publish never leaves discovered metadata in the trusted cache.
pub(super) struct StagedCapabilityCache {
    cache: ProviderCapabilityCache,
    pub(super) resolved: HashMap<String, HashMap<String, ModelCapabilityPatch>>,
}

#[derive(Default)]
pub(super) struct ProviderRuntimeCache {
    executors: ProviderExecutorCache,
    capabilities: ProviderCapabilityCache,
}

impl ProviderRuntimeCache {
    pub(super) fn executor_snapshot(&self) -> ProviderExecutorCache {
        self.executors.clone()
    }

    pub(super) fn replace_executors(&mut self, next: ProviderExecutorCache) {
        self.executors = next;
    }

    #[cfg(test)]
    pub(super) fn executor_provider(&self, provider_id: &str) -> Option<ProviderSpec> {
        self.executors
            .get(provider_id)
            .map(|(provider, _)| provider.clone())
    }

    /// Compute the next capability cache (retain still-valid snapshots, merge
    /// discovery) **without** committing it. The returned [`StagedCapabilityCache`]
    /// carries the resolved snapshot map for compilation; its cache is applied
    /// only by [`commit_capabilities`](Self::commit_capabilities) after the
    /// publish transaction succeeds, so a failed publish cannot pollute the
    /// trusted cache.
    pub(super) fn stage_capability_snapshots(
        &self,
        providers: &[ProviderSpec],
        discovered: HashMap<String, HashMap<String, ModelCapabilityPatch>>,
        attempted: &HashSet<String>,
        provider_signature: impl Fn(&ProviderSpec) -> String,
        now: SystemTime,
    ) -> StagedCapabilityCache {
        self.stage_capability_snapshots_with_ttl(
            providers,
            discovered,
            attempted,
            provider_signature,
            now,
            CAPABILITY_SNAPSHOT_TTL,
        )
    }

    fn stage_capability_snapshots_with_ttl(
        &self,
        providers: &[ProviderSpec],
        discovered: HashMap<String, HashMap<String, ModelCapabilityPatch>>,
        attempted: &HashSet<String>,
        provider_signature: impl Fn(&ProviderSpec) -> String,
        now: SystemTime,
        ttl: Duration,
    ) -> StagedCapabilityCache {
        let signatures = providers
            .iter()
            .map(|provider| (provider.id.clone(), provider_signature(provider)))
            .collect::<HashMap<_, _>>();
        // Retain a snapshot only while its provider signature is unchanged and
        // it is still within the TTL. An expired snapshot is dropped here so it
        // can neither be re-served nor retained across a later discovery
        // failure. This reads the current cache but does not mutate it.
        let mut staged: ProviderCapabilityCache = self
            .capabilities
            .iter()
            .filter(|(provider_id, snapshot)| {
                signatures
                    .get(*provider_id)
                    .is_some_and(|current| *current == snapshot.signature)
                    && !snapshot.is_expired(now, ttl)
            })
            .map(|(provider_id, snapshot)| (provider_id.clone(), snapshot.clone()))
            .collect();
        let discovered_provider_ids = discovered.keys().cloned().collect::<HashSet<_>>();
        for (provider_id, capabilities) in discovered {
            let Some(signature) = signatures.get(&provider_id) else {
                continue;
            };
            staged.insert(
                provider_id,
                CachedCapabilitySnapshot {
                    signature: signature.clone(),
                    discovered_at: now,
                    capabilities,
                },
            );
        }
        // Warn only when a retained snapshot is being served *because discovery
        // was attempted and failed* — not when discovery was unnecessary this
        // round (no referenced model needed it, or the endpoint was skipped),
        // which would be a false alarm.
        for provider_id in staged.keys() {
            if !discovered_provider_ids.contains(provider_id) && attempted.contains(provider_id) {
                tracing::warn!(
                    provider_id,
                    "using stale provider capability snapshot after discovery failure"
                );
            }
        }
        let resolved = staged
            .iter()
            .map(|(provider_id, snapshot)| (provider_id.clone(), snapshot.capabilities.clone()))
            .collect();
        StagedCapabilityCache {
            cache: staged,
            resolved,
        }
    }

    /// Commit a previously [`stage_capability_snapshots`](Self::stage_capability_snapshots)
    /// result, replacing the trusted capability cache. Called only after the
    /// publish transaction succeeds, alongside [`replace_executors`](Self::replace_executors).
    pub(super) fn commit_capabilities(&mut self, staged: StagedCapabilityCache) {
        self.capabilities = staged.cache;
    }

    /// Test convenience: stage and immediately commit, returning the resolved
    /// snapshot map — the pre-transactional behavior, used to exercise the
    /// retain/merge/expiry logic directly.
    #[cfg(test)]
    fn update_capability_snapshots_with_ttl(
        &mut self,
        providers: &[ProviderSpec],
        discovered: HashMap<String, HashMap<String, ModelCapabilityPatch>>,
        provider_signature: impl Fn(&ProviderSpec) -> String,
        now: SystemTime,
        ttl: Duration,
    ) -> HashMap<String, HashMap<String, ModelCapabilityPatch>> {
        // Model a normal pass where every discovered provider was attempted.
        let attempted: HashSet<String> = discovered.keys().cloned().collect();
        let staged = self.stage_capability_snapshots_with_ttl(
            providers,
            discovered,
            &attempted,
            provider_signature,
            now,
            ttl,
        );
        let resolved = staged.resolved.clone();
        self.commit_capabilities(staged);
        resolved
    }

    #[cfg(test)]
    fn update_capability_snapshots(
        &mut self,
        providers: &[ProviderSpec],
        discovered: HashMap<String, HashMap<String, ModelCapabilityPatch>>,
        provider_signature: impl Fn(&ProviderSpec) -> String,
        now: SystemTime,
    ) -> HashMap<String, HashMap<String, ModelCapabilityPatch>> {
        self.update_capability_snapshots_with_ttl(
            providers,
            discovered,
            provider_signature,
            now,
            CAPABILITY_SNAPSHOT_TTL,
        )
    }
}

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

    fn signature(provider: &ProviderSpec) -> String {
        provider.base_url.clone().unwrap_or_default()
    }

    fn patch(context_window: u32) -> ModelCapabilityPatch {
        ModelCapabilityPatch {
            context_window: Some(context_window),
            max_output_tokens: None,
            modalities: None,
            knowledge_cutoff: None,
        }
    }

    #[test]
    fn staged_capability_snapshot_is_not_served_until_committed() {
        // Models a publish whose discovery succeeded but whose later
        // compile/validate/publish/runtime-swap failed: the discovered metadata
        // is staged, never committed, and must not leak into the trusted cache
        // where a subsequent discovery failure could re-serve it.
        let provider = ProviderSpec {
            id: "p".into(),
            adapter: "openai".into(),
            base_url: Some("https://example.test/v1".into()),
            ..ProviderSpec::default()
        };
        let mut cache = ProviderRuntimeCache::default();
        let now = SystemTime::UNIX_EPOCH;
        let attempted = HashSet::from(["p".to_string()]);

        let staged = cache.stage_capability_snapshots(
            std::slice::from_ref(&provider),
            HashMap::from([(
                "p".into(),
                HashMap::from([("gpt-4o".into(), patch(128_000))]),
            )]),
            &attempted,
            signature,
            now,
        );
        assert!(staged.resolved.contains_key("p"));

        // Failed publish: stage is dropped, never committed. A later discovery
        // failure must see nothing.
        let after_failed_publish = cache.stage_capability_snapshots(
            std::slice::from_ref(&provider),
            HashMap::new(),
            &attempted,
            signature,
            now + Duration::from_secs(60),
        );
        assert!(
            after_failed_publish.resolved.is_empty(),
            "an uncommitted (failed-publish) snapshot must not be served"
        );

        // Committing a successful stage does retain across a later failure.
        let committed = cache.stage_capability_snapshots(
            std::slice::from_ref(&provider),
            HashMap::from([(
                "p".into(),
                HashMap::from([("gpt-4o".into(), patch(128_000))]),
            )]),
            &attempted,
            signature,
            now,
        );
        cache.commit_capabilities(committed);
        let after_commit = cache.stage_capability_snapshots(
            std::slice::from_ref(&provider),
            HashMap::new(),
            &attempted,
            signature,
            now + Duration::from_secs(60),
        );
        assert_eq!(
            after_commit.resolved["p"]["gpt-4o"].context_window,
            Some(128_000)
        );
    }

    #[test]
    fn capability_snapshot_merge_keeps_cached_snapshot_on_discovery_failure() {
        let provider = ProviderSpec {
            id: "p".into(),
            adapter: "openai".into(),
            base_url: Some("https://example.test/v1".into()),
            ..ProviderSpec::default()
        };
        let mut cache = ProviderRuntimeCache::default();
        let now = SystemTime::UNIX_EPOCH;
        let first = cache.update_capability_snapshots(
            std::slice::from_ref(&provider),
            HashMap::from([(
                "p".into(),
                HashMap::from([("gpt-4o".into(), patch(128_000))]),
            )]),
            signature,
            now,
        );
        // A discovery failure (empty map) within the TTL keeps the snapshot.
        let second = cache.update_capability_snapshots(
            std::slice::from_ref(&provider),
            HashMap::new(),
            signature,
            now + Duration::from_secs(60),
        );

        assert_eq!(first, second);
    }

    #[test]
    fn capability_snapshot_expires_after_ttl_on_discovery_failure() {
        let provider = ProviderSpec {
            id: "p".into(),
            adapter: "openai".into(),
            base_url: Some("https://example.test/v1".into()),
            ..ProviderSpec::default()
        };
        let mut cache = ProviderRuntimeCache::default();
        let ttl = Duration::from_secs(3_600);
        let now = SystemTime::UNIX_EPOCH;
        let first = cache.update_capability_snapshots_with_ttl(
            std::slice::from_ref(&provider),
            HashMap::from([(
                "p".into(),
                HashMap::from([("gpt-4o".into(), patch(128_000))]),
            )]),
            signature,
            now,
            ttl,
        );
        assert!(!first.is_empty());

        // A discovery failure past the TTL must drop the stale snapshot so it is
        // no longer served as runtime-trusted data.
        let expired = cache.update_capability_snapshots_with_ttl(
            std::slice::from_ref(&provider),
            HashMap::new(),
            signature,
            now + ttl + Duration::from_secs(1),
            ttl,
        );

        assert!(expired.is_empty());
    }

    #[test]
    fn capability_snapshot_within_ttl_is_still_served() {
        let provider = ProviderSpec {
            id: "p".into(),
            adapter: "openai".into(),
            base_url: Some("https://example.test/v1".into()),
            ..ProviderSpec::default()
        };
        let mut cache = ProviderRuntimeCache::default();
        let ttl = Duration::from_secs(3_600);
        let now = SystemTime::UNIX_EPOCH;
        cache.update_capability_snapshots_with_ttl(
            std::slice::from_ref(&provider),
            HashMap::from([(
                "p".into(),
                HashMap::from([("gpt-4o".into(), patch(128_000))]),
            )]),
            signature,
            now,
            ttl,
        );

        let still_fresh = cache.update_capability_snapshots_with_ttl(
            std::slice::from_ref(&provider),
            HashMap::new(),
            signature,
            now + ttl,
            ttl,
        );

        assert_eq!(still_fresh["p"]["gpt-4o"].context_window, Some(128_000));
    }

    #[test]
    fn capability_snapshot_empty_success_replaces_cached_snapshot() {
        let provider = ProviderSpec {
            id: "p".into(),
            adapter: "openai".into(),
            base_url: Some("https://example.test/v1".into()),
            ..ProviderSpec::default()
        };
        let mut cache = ProviderRuntimeCache::default();
        let now = SystemTime::UNIX_EPOCH;
        cache.update_capability_snapshots(
            std::slice::from_ref(&provider),
            HashMap::from([(
                "p".into(),
                HashMap::from([("gpt-4o".into(), patch(128_000))]),
            )]),
            signature,
            now,
        );

        let refreshed = cache.update_capability_snapshots(
            std::slice::from_ref(&provider),
            HashMap::from([("p".into(), HashMap::new())]),
            signature,
            now + Duration::from_secs(60),
        );

        assert_eq!(refreshed.get("p"), Some(&HashMap::new()));
    }

    #[test]
    fn capability_snapshot_merge_drops_cached_snapshot_after_provider_change() {
        let provider = ProviderSpec {
            id: "p".into(),
            adapter: "openai".into(),
            base_url: Some("https://example.test/v1".into()),
            ..ProviderSpec::default()
        };
        let changed = ProviderSpec {
            base_url: Some("https://other.example.test/v1".into()),
            ..provider.clone()
        };
        let mut cache = ProviderRuntimeCache::default();
        let now = SystemTime::UNIX_EPOCH;
        cache.update_capability_snapshots(
            std::slice::from_ref(&provider),
            HashMap::from([(
                "p".into(),
                HashMap::from([("gpt-4o".into(), patch(128_000))]),
            )]),
            signature,
            now,
        );
        let merged = cache.update_capability_snapshots(
            std::slice::from_ref(&changed),
            HashMap::new(),
            signature,
            now,
        );

        assert!(merged.is_empty());
    }
}