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
//! Optional ProtocolReplayLog and projector relay attachments.

use std::collections::HashMap;
use std::sync::{Arc, OnceLock, Weak};
use std::time::Duration;

use awaken_server_contract::contract::event_store::EventLookup;
use awaken_server_contract::contract::outbox::{
    OUTBOX_LANE_CANONICAL, OUTBOX_LANE_PROTOCOL_REPLAY, OUTBOX_TARGET_A2A_WEBHOOK,
    OUTBOX_TARGET_PROTOCOL_FANOUT, OUTBOX_TARGET_PROTOCOL_PROJECTOR, OutboxStore,
};
use awaken_server_contract::contract::protocol_replay_log::{
    ProtocolReplayLog, ProtocolReplayLookup,
};
use parking_lot::Mutex;
use tokio::task::JoinHandle;
use tokio_util::sync::CancellationToken;

use crate::app::{ReplayBufferEntry, ReplayBufferMap, ServerState};
use crate::outbox_relay::{OutboxRelay, OutboxRelayConfig, OutboxRelayError};
use crate::protocol_fanout::{ProtocolReplayFanoutPublisher, ProtocolReplayFanoutRelayHandler};
use crate::protocol_projector::CanonicalOutboxProtocolProjector;
use crate::protocols::a2a::push_outbox::A2aPushWebhookRelayHandler;

type ProtocolReplayRegistry = HashMap<usize, ProtocolReplayAttachment>;

#[derive(Clone)]
struct ProtocolReplayAttachment {
    replay_buffers: Weak<Mutex<HashMap<String, ReplayBufferEntry>>>,
    log: Option<Arc<dyn ProtocolReplayLog>>,
    projector_relay: Option<ProtocolProjectorRelayAttachment>,
    fanout_relay: Option<ProtocolFanoutRelayAttachment>,
    a2a_push_relay: Option<A2aPushWebhookRelayAttachment>,
}

#[derive(Clone)]
struct ProtocolProjectorRelayAttachment {
    outbox: Arc<dyn OutboxStore>,
    event_lookup: Arc<dyn EventLookup>,
    replay_writer:
        Arc<dyn awaken_server_contract::contract::protocol_replay_log::ProtocolReplayWriter>,
    config: ProtocolProjectorRelayConfig,
}

#[derive(Clone)]
struct ProtocolFanoutRelayAttachment {
    outbox: Arc<dyn OutboxStore>,
    replay_lookup: Arc<dyn ProtocolReplayLookup>,
    publisher: Arc<dyn ProtocolReplayFanoutPublisher>,
    config: ProtocolFanoutRelayConfig,
}

#[derive(Clone)]
struct A2aPushWebhookRelayAttachment {
    outbox: Arc<dyn OutboxStore>,
    config: A2aPushWebhookRelayConfig,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProtocolProjectorRelayConfig {
    pub relay: OutboxRelayConfig,
    pub idle_sleep: Duration,
    pub error_sleep: Duration,
}

impl Default for ProtocolProjectorRelayConfig {
    fn default() -> Self {
        Self {
            relay: OutboxRelayConfig {
                lane: OUTBOX_LANE_CANONICAL.to_string(),
                target: OUTBOX_TARGET_PROTOCOL_PROJECTOR.to_string(),
                consumer_id: "protocol-projector".to_string(),
                batch_limit: 100,
                lease_ms: 30_000,
                retry_delay_ms: 1_000,
                max_retry_delay_ms: 30_000,
            },
            idle_sleep: Duration::from_millis(250),
            error_sleep: Duration::from_secs(1),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProtocolFanoutRelayConfig {
    pub relay: OutboxRelayConfig,
    pub idle_sleep: Duration,
    pub error_sleep: Duration,
}

impl Default for ProtocolFanoutRelayConfig {
    fn default() -> Self {
        Self {
            relay: OutboxRelayConfig {
                lane: OUTBOX_LANE_PROTOCOL_REPLAY.to_string(),
                target: OUTBOX_TARGET_PROTOCOL_FANOUT.to_string(),
                consumer_id: "protocol-fanout".to_string(),
                batch_limit: 100,
                lease_ms: 30_000,
                retry_delay_ms: 1_000,
                max_retry_delay_ms: 30_000,
            },
            idle_sleep: Duration::from_millis(250),
            error_sleep: Duration::from_secs(1),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct A2aPushWebhookRelayConfig {
    pub relay: OutboxRelayConfig,
    pub idle_sleep: Duration,
    pub error_sleep: Duration,
}

impl Default for A2aPushWebhookRelayConfig {
    fn default() -> Self {
        Self {
            relay: OutboxRelayConfig {
                lane: OUTBOX_LANE_PROTOCOL_REPLAY.to_string(),
                target: OUTBOX_TARGET_A2A_WEBHOOK.to_string(),
                consumer_id: "a2a-push-webhook".to_string(),
                batch_limit: 100,
                lease_ms: 30_000,
                retry_delay_ms: 1_000,
                max_retry_delay_ms: 30_000,
            },
            idle_sleep: Duration::from_millis(250),
            error_sleep: Duration::from_secs(1),
        }
    }
}

pub struct ProtocolRelayHandle {
    task: JoinHandle<()>,
    cancel: CancellationToken,
    name: &'static str,
}

pub type ProtocolProjectorRelayHandle = ProtocolRelayHandle;
pub type ProtocolFanoutRelayHandle = ProtocolRelayHandle;
pub type A2aPushWebhookRelayHandle = ProtocolRelayHandle;

impl ProtocolRelayHandle {
    pub async fn shutdown(self) {
        self.shutdown_with_timeout(Duration::from_secs(30)).await;
    }

    pub async fn shutdown_with_timeout(self, timeout: Duration) {
        self.cancel.cancel();
        let mut task = self.task;
        match tokio::time::timeout(timeout, &mut task).await {
            Ok(Ok(())) => {}
            Ok(Err(error)) if error.is_cancelled() => {}
            Ok(Err(error)) => {
                tracing::warn!(error = %error, relay = self.name, "outbox relay failed during shutdown");
            }
            Err(_) => {
                tracing::warn!(
                    relay = self.name,
                    timeout_ms = timeout.as_millis(),
                    "outbox relay shutdown timed out; aborting task and relying on lease retry"
                );
                task.abort();
                if let Err(error) = task.await
                    && !error.is_cancelled()
                {
                    tracing::warn!(error = %error, relay = self.name, "outbox relay failed after abort");
                }
            }
        }
    }
}

pub(crate) struct ProtocolRelayHandles {
    handles: Vec<ProtocolRelayHandle>,
}

impl ProtocolRelayHandles {
    pub async fn shutdown(self) {
        for handle in self.handles {
            handle.shutdown().await;
        }
    }
}

static PROTOCOL_REPLAY_REGISTRY: OnceLock<Mutex<ProtocolReplayRegistry>> = OnceLock::new();

fn registry() -> &'static Mutex<ProtocolReplayRegistry> {
    PROTOCOL_REPLAY_REGISTRY.get_or_init(|| Mutex::new(HashMap::new()))
}

fn key(replay_buffers: &ReplayBufferMap) -> usize {
    Arc::as_ptr(replay_buffers) as usize
}

fn prune(registry: &mut ProtocolReplayRegistry) {
    registry.retain(|_, attachment| attachment.replay_buffers.upgrade().is_some());
}

#[must_use]
pub fn with_protocol_replay_log(
    state: ServerState,
    log: Arc<dyn ProtocolReplayLog>,
) -> ServerState {
    let mut registry = registry().lock();
    prune(&mut registry);
    let weak = Arc::downgrade(&state.protocol.replay_buffers);
    registry
        .entry(key(&state.protocol.replay_buffers))
        .and_modify(|attachment| attachment.log = Some(log.clone()))
        .or_insert_with(|| ProtocolReplayAttachment {
            replay_buffers: weak,
            log: Some(log),
            projector_relay: None,
            fanout_relay: None,
            a2a_push_relay: None,
        });
    state
}

pub fn protocol_replay_log(state: &ServerState) -> Option<Arc<dyn ProtocolReplayLog>> {
    protocol_replay_log_for_buffers(&state.protocol.replay_buffers)
}

pub fn protocol_replay_log_for_buffers(
    replay_buffers: &ReplayBufferMap,
) -> Option<Arc<dyn ProtocolReplayLog>> {
    let mut registry = registry().lock();
    prune(&mut registry);
    registry
        .get(&key(replay_buffers))
        .and_then(|attachment| attachment.log.as_ref().map(Arc::clone))
}

pub fn with_protocol_projector_relay(
    state: ServerState,
    outbox: Arc<dyn OutboxStore>,
    event_lookup: Arc<dyn EventLookup>,
    replay_writer: Arc<
        dyn awaken_server_contract::contract::protocol_replay_log::ProtocolReplayWriter,
    >,
    config: ProtocolProjectorRelayConfig,
) -> Result<ServerState, OutboxRelayError> {
    config.relay.validate()?;
    let mut registry = registry().lock();
    prune(&mut registry);
    let weak = Arc::downgrade(&state.protocol.replay_buffers);
    registry
        .entry(key(&state.protocol.replay_buffers))
        .and_modify(|attachment| {
            attachment.projector_relay = Some(ProtocolProjectorRelayAttachment {
                outbox: outbox.clone(),
                event_lookup: event_lookup.clone(),
                replay_writer: replay_writer.clone(),
                config: config.clone(),
            });
        })
        .or_insert_with(|| ProtocolReplayAttachment {
            replay_buffers: weak,
            log: None,
            projector_relay: Some(ProtocolProjectorRelayAttachment {
                outbox,
                event_lookup,
                replay_writer,
                config,
            }),
            fanout_relay: None,
            a2a_push_relay: None,
        });
    Ok(state)
}

pub fn with_protocol_fanout_relay(
    state: ServerState,
    outbox: Arc<dyn OutboxStore>,
    replay_lookup: Arc<dyn ProtocolReplayLookup>,
    publisher: Arc<dyn ProtocolReplayFanoutPublisher>,
    config: ProtocolFanoutRelayConfig,
) -> Result<ServerState, OutboxRelayError> {
    config.relay.validate()?;
    let mut registry = registry().lock();
    prune(&mut registry);
    let weak = Arc::downgrade(&state.protocol.replay_buffers);
    registry
        .entry(key(&state.protocol.replay_buffers))
        .and_modify(|attachment| {
            attachment.fanout_relay = Some(ProtocolFanoutRelayAttachment {
                outbox: outbox.clone(),
                replay_lookup: replay_lookup.clone(),
                publisher: publisher.clone(),
                config: config.clone(),
            });
        })
        .or_insert_with(|| ProtocolReplayAttachment {
            replay_buffers: weak,
            log: None,
            projector_relay: None,
            fanout_relay: Some(ProtocolFanoutRelayAttachment {
                outbox,
                replay_lookup,
                publisher,
                config,
            }),
            a2a_push_relay: None,
        });
    Ok(state)
}

pub fn with_a2a_push_webhook_relay(
    mut state: ServerState,
    outbox: Arc<dyn OutboxStore>,
    config: A2aPushWebhookRelayConfig,
) -> Result<ServerState, OutboxRelayError> {
    state.protocol.a2a_push_outbox = outbox.clone();
    state.protocol.a2a_push_relay_config = config.clone();
    register_a2a_push_webhook_relay_for_buffers(&state.protocol.replay_buffers, outbox, config)?;
    Ok(state)
}

pub(crate) fn register_a2a_push_webhook_relay_for_buffers(
    replay_buffers: &ReplayBufferMap,
    outbox: Arc<dyn OutboxStore>,
    config: A2aPushWebhookRelayConfig,
) -> Result<(), OutboxRelayError> {
    config.relay.validate()?;
    let mut registry = registry().lock();
    prune(&mut registry);
    let weak = Arc::downgrade(replay_buffers);
    registry
        .entry(key(replay_buffers))
        .and_modify(|attachment| {
            attachment.a2a_push_relay = Some(A2aPushWebhookRelayAttachment {
                outbox: outbox.clone(),
                config: config.clone(),
            });
        })
        .or_insert_with(|| ProtocolReplayAttachment {
            replay_buffers: weak,
            log: None,
            projector_relay: None,
            fanout_relay: None,
            a2a_push_relay: Some(A2aPushWebhookRelayAttachment { outbox, config }),
        });
    Ok(())
}

/// Move every relay attachment (log, projector, fanout, A2A push) from `old`
/// replay buffers to `new`.
///
/// The registry is keyed by replay-buffer identity, so replacing a state's
/// protocol module — which swaps in fresh `replay_buffers` — would otherwise
/// orphan any attachment registered before the swap. `start_protocol_relays`
/// looks attachments up by the current buffers, so an orphaned projector/fanout/
/// log relay would silently never start.
pub(crate) fn migrate_protocol_attachments(old: &ReplayBufferMap, new: &ReplayBufferMap) {
    if Arc::ptr_eq(old, new) {
        return;
    }
    let mut registry = registry().lock();
    prune(&mut registry);
    let Some(mut attachment) = registry.remove(&key(old)) else {
        return;
    };
    attachment.replay_buffers = Arc::downgrade(new);
    registry.insert(key(new), attachment);
}

pub fn a2a_push_webhook_outbox_for_buffers(
    replay_buffers: &ReplayBufferMap,
) -> Option<Arc<dyn OutboxStore>> {
    let mut registry = registry().lock();
    prune(&mut registry);
    registry
        .get(&key(replay_buffers))
        .and_then(|attachment| attachment.a2a_push_relay.as_ref())
        .map(|attachment| Arc::clone(&attachment.outbox))
}

pub(crate) async fn start_protocol_relays(
    state: &ServerState,
) -> Result<ProtocolRelayHandles, OutboxRelayError> {
    let mut handles = Vec::new();
    if let Some(handle) = start_protocol_projector_relay(state)? {
        handles.push(handle);
    }
    match start_protocol_fanout_relay(state) {
        Ok(Some(handle)) => handles.push(handle),
        Ok(None) => {}
        Err(error) => {
            ProtocolRelayHandles { handles }.shutdown().await;
            return Err(error);
        }
    }
    match start_a2a_push_webhook_relay(state) {
        Ok(Some(handle)) => handles.push(handle),
        Ok(None) => {}
        Err(error) => {
            ProtocolRelayHandles { handles }.shutdown().await;
            return Err(error);
        }
    }
    Ok(ProtocolRelayHandles { handles })
}

pub(crate) fn start_protocol_projector_relay(
    state: &ServerState,
) -> Result<Option<ProtocolProjectorRelayHandle>, OutboxRelayError> {
    let attachment = {
        let mut registry = registry().lock();
        prune(&mut registry);
        registry
            .get(&key(&state.protocol.replay_buffers))
            .and_then(|attachment| attachment.projector_relay.clone())
    };
    let Some(attachment) = attachment else {
        return Ok(None);
    };
    let handler = Arc::new(CanonicalOutboxProtocolProjector::new_all_protocols(
        attachment.event_lookup,
        attachment.replay_writer,
    ));
    let relay = OutboxRelay::new(attachment.outbox, handler, attachment.config.relay.clone())?;
    let config = attachment.config;
    let cancel = CancellationToken::new();
    Ok(Some(ProtocolRelayHandle {
        task: tokio::spawn(run_outbox_relay(
            relay,
            config.idle_sleep,
            config.error_sleep,
            "protocol projector relay",
            cancel.clone(),
        )),
        cancel,
        name: "protocol projector relay",
    }))
}

pub(crate) fn start_protocol_fanout_relay(
    state: &ServerState,
) -> Result<Option<ProtocolFanoutRelayHandle>, OutboxRelayError> {
    let attachment = {
        let mut registry = registry().lock();
        prune(&mut registry);
        registry
            .get(&key(&state.protocol.replay_buffers))
            .and_then(|attachment| attachment.fanout_relay.clone())
    };
    let Some(attachment) = attachment else {
        return Ok(None);
    };
    let handler = Arc::new(ProtocolReplayFanoutRelayHandler::new(
        attachment.replay_lookup,
        attachment.publisher,
    ));
    let relay = OutboxRelay::new(attachment.outbox, handler, attachment.config.relay.clone())?;
    let config = attachment.config;
    let cancel = CancellationToken::new();
    Ok(Some(ProtocolRelayHandle {
        task: tokio::spawn(run_outbox_relay(
            relay,
            config.idle_sleep,
            config.error_sleep,
            "protocol fanout relay",
            cancel.clone(),
        )),
        cancel,
        name: "protocol fanout relay",
    }))
}

pub(crate) fn start_a2a_push_webhook_relay(
    state: &ServerState,
) -> Result<Option<A2aPushWebhookRelayHandle>, OutboxRelayError> {
    let attachment = {
        let mut registry = registry().lock();
        prune(&mut registry);
        registry
            .get(&key(&state.protocol.replay_buffers))
            .and_then(|attachment| attachment.a2a_push_relay.clone())
    };
    let Some(attachment) = attachment else {
        return Ok(None);
    };
    let handler = Arc::new(A2aPushWebhookRelayHandler::new(reqwest::Client::new()));
    let relay = OutboxRelay::new(attachment.outbox, handler, attachment.config.relay.clone())?;
    let config = attachment.config;
    let cancel = CancellationToken::new();
    Ok(Some(ProtocolRelayHandle {
        task: tokio::spawn(run_outbox_relay(
            relay,
            config.idle_sleep,
            config.error_sleep,
            "a2a push webhook relay",
            cancel.clone(),
        )),
        cancel,
        name: "a2a push webhook relay",
    }))
}

pub async fn tick_a2a_push_webhook_outbox_for_buffers(
    replay_buffers: &ReplayBufferMap,
) -> Result<(), OutboxRelayError> {
    let attachment = {
        let mut registry = registry().lock();
        prune(&mut registry);
        registry
            .get(&key(replay_buffers))
            .and_then(|attachment| attachment.a2a_push_relay.clone())
    };
    let Some(attachment) = attachment else {
        return Ok(());
    };
    let handler = Arc::new(A2aPushWebhookRelayHandler::new(reqwest::Client::new()));
    OutboxRelay::new(attachment.outbox, handler, attachment.config.relay)?
        .tick()
        .await
        .map(|_| ())
}

async fn run_outbox_relay(
    relay: OutboxRelay,
    idle_sleep: Duration,
    error_sleep: Duration,
    name: &'static str,
    cancel: CancellationToken,
) {
    loop {
        if cancel.is_cancelled() {
            break;
        }
        match relay.tick().await {
            Ok(stats) if stats.claimed == 0 => wait(idle_sleep).await,
            Ok(_) => {}
            Err(error) => {
                tracing::warn!(error = %error, relay = name, "outbox relay tick failed");
                wait(error_sleep).await;
            }
        }
        if cancel.is_cancelled() {
            break;
        }
    }
}

async fn wait(duration: Duration) {
    if duration.is_zero() {
        tokio::task::yield_now().await;
    } else {
        tokio::time::sleep(duration).await;
    }
}

#[cfg(test)]
mod tests;