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
use awaken_protocol_a2a::{ListPushNotificationConfigsResponse, PushNotificationConfig};
use awaken_server_contract::thread::Thread;
use axum::Json;
use axum::extract::{Path, Query, State};
use axum::http::{HeaderMap, StatusCode};
use axum::response::{IntoResponse, Response};
use uuid::Uuid;

use crate::app::{ProtocolModuleState, ProtocolRoutesState};

use super::common::{
    ensure_supported_version, load_thread_metadata_projection, parse_page_token,
    persist_thread_metadata, trim_to_option,
};
use super::error::A2aError;
use super::push_outbox::enqueue_push_notification;
use super::stream_projector::{InitialStreamEvent, TaskStreamProjector};
use super::task::{
    ensure_task_visible, load_task_snapshot, resolve_task, submitted_task, task_context_id,
};
use super::types::{
    BLOCKING_POLL_INTERVAL, DEFAULT_PAGE_SIZE, ListPushConfigsQuery, MAX_PAGE_SIZE,
    PUSH_CONFIGS_METADATA_KEY, StoredPushConfigs, TaskSnapshot,
};

pub(super) async fn a2a_create_push_config_default(
    State(st): State<ProtocolRoutesState>,
    Path(task_id): Path<String>,
    headers: HeaderMap,
    Json(payload): Json<PushNotificationConfig>,
) -> Result<Response, A2aError> {
    create_push_config(st, headers, None, task_id, payload)
        .await
        .map(IntoResponse::into_response)
}

pub(super) async fn a2a_create_push_config_tenant(
    State(st): State<ProtocolRoutesState>,
    Path((tenant, task_id)): Path<(String, String)>,
    headers: HeaderMap,
    Json(payload): Json<PushNotificationConfig>,
) -> Result<Response, A2aError> {
    create_push_config(st, headers, Some(tenant), task_id, payload)
        .await
        .map(IntoResponse::into_response)
}

pub(super) async fn a2a_list_push_configs_default(
    State(st): State<ProtocolRoutesState>,
    Path(task_id): Path<String>,
    headers: HeaderMap,
    Query(query): Query<ListPushConfigsQuery>,
) -> Result<Json<ListPushNotificationConfigsResponse>, A2aError> {
    list_push_configs(st, headers, None, task_id, query).await
}

pub(super) async fn a2a_list_push_configs_tenant(
    State(st): State<ProtocolRoutesState>,
    Path((tenant, task_id)): Path<(String, String)>,
    headers: HeaderMap,
    Query(query): Query<ListPushConfigsQuery>,
) -> Result<Json<ListPushNotificationConfigsResponse>, A2aError> {
    list_push_configs(st, headers, Some(tenant), task_id, query).await
}

pub(super) async fn a2a_get_push_config_default(
    State(st): State<ProtocolRoutesState>,
    Path((task_id, config_id)): Path<(String, String)>,
    headers: HeaderMap,
) -> Result<Response, A2aError> {
    get_push_config(st, headers, None, task_id, config_id)
        .await
        .map(IntoResponse::into_response)
}

pub(super) async fn a2a_get_push_config_tenant(
    State(st): State<ProtocolRoutesState>,
    Path((tenant, task_id, config_id)): Path<(String, String, String)>,
    headers: HeaderMap,
) -> Result<Response, A2aError> {
    get_push_config(st, headers, Some(tenant), task_id, config_id)
        .await
        .map(IntoResponse::into_response)
}

pub(super) async fn a2a_delete_push_config_default(
    State(st): State<ProtocolRoutesState>,
    Path((task_id, config_id)): Path<(String, String)>,
    headers: HeaderMap,
) -> Result<Response, A2aError> {
    delete_push_config(st, headers, None, task_id, config_id).await
}

pub(super) async fn a2a_delete_push_config_tenant(
    State(st): State<ProtocolRoutesState>,
    Path((tenant, task_id, config_id)): Path<(String, String, String)>,
    headers: HeaderMap,
) -> Result<Response, A2aError> {
    delete_push_config(st, headers, Some(tenant), task_id, config_id).await
}

async fn create_push_config(
    st: ProtocolRoutesState,
    headers: HeaderMap,
    tenant: Option<String>,
    task_id: String,
    payload: PushNotificationConfig,
) -> Result<Json<PushNotificationConfig>, A2aError> {
    ensure_supported_version(&headers)?;
    ensure_task_visible(&st, &task_id, tenant.as_deref()).await?;
    let config = normalize_push_config(payload, tenant.as_deref(), &task_id)?;
    upsert_push_notification_config(&st, &task_id, tenant.as_deref(), config.clone()).await?;
    spawn_push_notification_driver(st, task_id, tenant, config.clone());
    Ok(Json(config))
}

async fn list_push_configs(
    st: ProtocolRoutesState,
    headers: HeaderMap,
    tenant: Option<String>,
    task_id: String,
    query: ListPushConfigsQuery,
) -> Result<Json<ListPushNotificationConfigsResponse>, A2aError> {
    ensure_supported_version(&headers)?;
    ensure_task_visible(&st, &task_id, tenant.as_deref()).await?;

    let page_size = query
        .page_size
        .unwrap_or(DEFAULT_PAGE_SIZE)
        .clamp(1, MAX_PAGE_SIZE);
    let offset = parse_page_token(query.page_token.as_deref())?;
    let configs = load_push_notification_configs(&st, &task_id, tenant.as_deref()).await?;
    let total = configs.len();
    let items = configs
        .into_iter()
        .skip(offset)
        .take(page_size)
        .collect::<Vec<_>>();
    let next_offset = offset + items.len();

    Ok(Json(ListPushNotificationConfigsResponse {
        configs: items,
        next_page_token: if next_offset < total {
            next_offset.to_string()
        } else {
            String::new()
        },
    }))
}

async fn get_push_config(
    st: ProtocolRoutesState,
    headers: HeaderMap,
    tenant: Option<String>,
    task_id: String,
    config_id: String,
) -> Result<Json<PushNotificationConfig>, A2aError> {
    ensure_supported_version(&headers)?;
    ensure_task_visible(&st, &task_id, tenant.as_deref()).await?;
    let config = find_push_notification_config(&st, &task_id, tenant.as_deref(), &config_id)
        .await?
        .ok_or_else(|| A2aError::push_config_not_found(task_id.clone(), config_id.clone()))?;
    Ok(Json(config))
}

async fn delete_push_config(
    st: ProtocolRoutesState,
    headers: HeaderMap,
    tenant: Option<String>,
    task_id: String,
    config_id: String,
) -> Result<Response, A2aError> {
    ensure_supported_version(&headers)?;
    ensure_task_visible(&st, &task_id, tenant.as_deref()).await?;

    // Load the full config set (all tenants) and remove only the matching
    // (tenant, config id) entry. A tenant-scoped delete must not drop configs
    // owned by other tenants when the whole task list is persisted back.
    let mut configs = load_push_notification_configs(&st, &task_id, None).await?;
    let before = configs.len();
    configs.retain(|config| {
        !(config.id.as_deref() == Some(config_id.as_str())
            && config.agent_id.as_deref() == tenant.as_deref())
    });
    if configs.len() == before {
        return Err(A2aError::push_config_not_found(task_id, config_id));
    }
    save_push_notification_configs(&st, &task_id, configs).await?;
    Ok(StatusCode::NO_CONTENT.into_response())
}

pub(super) fn normalize_push_config(
    mut config: PushNotificationConfig,
    tenant: Option<&str>,
    task_id: &str,
) -> Result<PushNotificationConfig, A2aError> {
    let parsed_url = reqwest::Url::parse(&config.url)
        .map_err(|err| A2aError::invalid("pushNotificationConfig.url", err.to_string()))?;
    if !matches!(parsed_url.scheme(), "http" | "https") {
        return Err(A2aError::invalid(
            "pushNotificationConfig.url",
            "push notification URL must use http or https",
        ));
    }

    if let Some(existing_task_id) = trim_to_option(config.task_id.as_deref())
        && existing_task_id != task_id
    {
        return Err(A2aError::invalid(
            "pushNotificationConfig.taskId",
            "push notification taskId must match the enclosing task",
        ));
    }
    if let Some(existing_tenant) = trim_to_option(config.agent_id.as_deref())
        && tenant != Some(existing_tenant.as_str())
    {
        return Err(A2aError::invalid(
            "pushNotificationConfig.tenant",
            "push notification tenant must match the enclosing task tenant",
        ));
    }
    if let Some(authentication) = config.authentication.as_ref()
        && authentication.scheme.trim().is_empty()
    {
        return Err(A2aError::invalid(
            "pushNotificationConfig.authentication.scheme",
            "authentication scheme must not be empty",
        ));
    }

    config.id.get_or_insert_with(|| Uuid::now_v7().to_string());
    config.task_id = Some(task_id.to_string());
    config.agent_id = tenant.map(ToOwned::to_owned);
    Ok(config)
}

pub(super) async fn load_push_notification_configs(
    st: &ProtocolRoutesState,
    task_id: &str,
    tenant: Option<&str>,
) -> Result<Vec<PushNotificationConfig>, A2aError> {
    let Some(task) = resolve_task(st, task_id).await? else {
        return Ok(Vec::new());
    };
    let Some(thread) = st
        .run
        .store()
        .load_thread(&task.thread_id)
        .await
        .map_err(|e| A2aError::Internal(e.to_string()))?
    else {
        return Ok(Vec::new());
    };

    let mut configs = load_thread_push_notification_configs(&thread, task_id)?;
    if let Some(tenant) = tenant {
        configs.retain(|config| config.agent_id.as_deref() == Some(tenant));
    }
    configs.sort_by(|left, right| left.id.cmp(&right.id));
    Ok(configs)
}

async fn find_push_notification_config(
    st: &ProtocolRoutesState,
    task_id: &str,
    tenant: Option<&str>,
    config_id: &str,
) -> Result<Option<PushNotificationConfig>, A2aError> {
    Ok(load_push_notification_configs(st, task_id, tenant)
        .await?
        .into_iter()
        .find(|config| config.id.as_deref() == Some(config_id)))
}

async fn save_push_notification_configs(
    st: &ProtocolRoutesState,
    task_id: &str,
    configs: Vec<PushNotificationConfig>,
) -> Result<(), A2aError> {
    let Some(task) = resolve_task(st, task_id).await? else {
        return Err(A2aError::task_not_found(task_id.to_string()));
    };
    let thread_id = task.thread_id;
    let (exists, thread) = load_thread_metadata_projection(st, &thread_id).await?;
    save_thread_push_notification_configs(st, &thread_id, exists, thread, task_id, configs).await
}

/// Insert or replace `config` within the task's full config list, scoped to its
/// owning tenant. Matching is keyed on `(tenant, config id)` so a write for one
/// tenant never overwrites another tenant's config that shares a config id.
fn upsert_into(
    configs: &mut Vec<PushNotificationConfig>,
    tenant: Option<&str>,
    config: PushNotificationConfig,
) {
    if let Some(position) = configs
        .iter()
        .position(|existing| existing.id == config.id && existing.agent_id.as_deref() == tenant)
    {
        configs[position] = config;
    } else {
        configs.push(config);
    }
}

async fn upsert_push_notification_config(
    st: &ProtocolRoutesState,
    task_id: &str,
    tenant: Option<&str>,
    config: PushNotificationConfig,
) -> Result<(), A2aError> {
    // Load the full config set (all tenants) so a tenant-scoped write preserves
    // configs owned by other tenants when the whole task list is persisted back.
    let mut configs = load_push_notification_configs(st, task_id, None).await?;
    upsert_into(&mut configs, tenant, config);
    save_push_notification_configs(st, task_id, configs).await
}

pub(super) async fn upsert_push_notification_config_for_thread(
    st: &ProtocolRoutesState,
    thread_id: &str,
    task_id: &str,
    tenant: Option<&str>,
    config: PushNotificationConfig,
) -> Result<(), A2aError> {
    let (exists, thread) = load_thread_metadata_projection(st, thread_id).await?;
    // Operate on the full config set; `upsert_into` scopes the replace to the
    // owning tenant so other tenants' configs survive the whole-task write.
    let mut configs = load_thread_push_notification_configs(&thread, task_id)?;
    upsert_into(&mut configs, tenant, config);
    save_thread_push_notification_configs(st, thread_id, exists, thread, task_id, configs).await
}

fn load_thread_push_notification_configs(
    thread: &Thread,
    task_id: &str,
) -> Result<Vec<PushNotificationConfig>, A2aError> {
    let Some(value) = thread.metadata.custom.get(PUSH_CONFIGS_METADATA_KEY) else {
        return Ok(Vec::new());
    };

    let stored = decode_push_configs_metadata(value.clone(), task_id)?;
    Ok(stored.tasks.get(task_id).cloned().unwrap_or_default())
}

async fn save_thread_push_notification_configs(
    st: &ProtocolRoutesState,
    thread_id: &str,
    exists: bool,
    mut thread: Thread,
    task_id: &str,
    configs: Vec<PushNotificationConfig>,
) -> Result<(), A2aError> {
    let mut stored = match thread.metadata.custom.remove(PUSH_CONFIGS_METADATA_KEY) {
        Some(value) => decode_push_configs_metadata(value, task_id)?,
        None => StoredPushConfigs::default(),
    };
    if configs.is_empty() {
        stored.tasks.remove(task_id);
    } else {
        stored.tasks.insert(task_id.to_string(), configs);
    }
    if stored.tasks.is_empty() {
        thread.metadata.custom.remove(PUSH_CONFIGS_METADATA_KEY);
    } else {
        thread.metadata.custom.insert(
            PUSH_CONFIGS_METADATA_KEY.to_string(),
            serde_json::to_value(stored).map_err(|e| A2aError::Internal(e.to_string()))?,
        );
    }
    persist_thread_metadata(st, thread_id, exists, thread).await?;

    Ok(())
}

fn decode_push_configs_metadata(
    value: serde_json::Value,
    task_id: &str,
) -> Result<StoredPushConfigs, A2aError> {
    match serde_json::from_value::<StoredPushConfigs>(value.clone()) {
        Ok(stored) => Ok(stored),
        Err(stored_error) => {
            let legacy_configs = serde_json::from_value::<Vec<PushNotificationConfig>>(value)
                .map_err(|legacy_error| {
                    A2aError::Internal(format!(
                        "corrupt A2A push config metadata at {PUSH_CONFIGS_METADATA_KEY}: \
                         stored format error: {stored_error}; legacy format error: {legacy_error}"
                    ))
                })?;
            let mut stored = StoredPushConfigs::default();
            if !legacy_configs.is_empty() {
                stored.tasks.insert(task_id.to_string(), legacy_configs);
            }
            Ok(stored)
        }
    }
}

/// Releases the single-flight dedupe key for an A2A push driver when the driver
/// task ends. Using `Drop` covers normal completion, early error returns, task
/// panics, and task aborts — an explicit unregister at the end of the task body
/// would be skipped on panic/abort and leak the key, blocking re-registration.
struct PushDriverGuard {
    protocol: ProtocolModuleState,
    task_id: String,
    tenant: Option<String>,
    config_id: String,
}

impl Drop for PushDriverGuard {
    fn drop(&mut self) {
        self.protocol.unregister_a2a_push_driver(
            &self.task_id,
            self.tenant.as_deref(),
            &self.config_id,
        );
    }
}

pub(super) fn spawn_push_notification_driver(
    st: ProtocolRoutesState,
    task_id: String,
    tenant: Option<String>,
    config: PushNotificationConfig,
) {
    let config_id = config.id.clone().unwrap_or_default();
    if !st
        .protocol
        .register_a2a_push_driver(&task_id, tenant.as_deref(), &config_id)
    {
        return;
    }

    tokio::spawn(async move {
        let _guard = PushDriverGuard {
            protocol: st.protocol.clone(),
            task_id: task_id.clone(),
            tenant: tenant.clone(),
            config_id: config_id.clone(),
        };
        if let Err(err) = drive_push_notification(st, task_id, tenant, config_id).await {
            tracing::warn!(error = ?err, "A2A push notification driver stopped with error");
        }
    });
}

async fn drive_push_notification(
    st: ProtocolRoutesState,
    task_id: String,
    tenant: Option<String>,
    config_id: String,
) -> Result<(), A2aError> {
    let outbox = crate::protocol_replay_state::a2a_push_webhook_outbox_for_buffers(
        &st.protocol.replay_buffers,
    )
    .ok_or_else(|| {
        A2aError::Internal("A2A push notification outbox relay is not configured".to_string())
    })?;
    let mut projector = TaskStreamProjector::new(InitialStreamEvent::StatusUpdate);

    loop {
        let Some(config) =
            find_push_notification_config(&st, &task_id, tenant.as_deref(), &config_id).await?
        else {
            break;
        };

        let snapshot = load_task_snapshot(&st, &task_id, tenant.as_deref(), usize::MAX, true)
            .await?
            .unwrap_or(TaskSnapshot {
                task: submitted_task(
                    &task_id,
                    &task_context_id(&st, &task_id)
                        .await
                        .unwrap_or_else(|_| task_id.clone()),
                    tenant.as_deref(),
                ),
                updated_at_ms: 0,
                current_agent_id: tenant.clone(),
            });

        for response in projector.project(&snapshot) {
            enqueue_push_notification(outbox.as_ref(), &config, &response)
                .await
                .map_err(|error| A2aError::Internal(error.to_string()))?;
            if let Err(error) =
                crate::protocol_replay_state::tick_a2a_push_webhook_outbox_for_buffers(
                    &st.protocol.replay_buffers,
                )
                .await
            {
                tracing::warn!(
                    error = %error,
                    "A2A push notification outbox relay tick failed"
                );
            }
        }

        if snapshot.task.status.state.is_terminal() || snapshot.task.status.state.is_interrupted() {
            break;
        }

        tokio::time::sleep(BLOCKING_POLL_INTERVAL).await;
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::*;

    fn config(id: &str, tenant: Option<&str>, url: &str) -> PushNotificationConfig {
        PushNotificationConfig {
            agent_id: tenant.map(ToOwned::to_owned),
            id: Some(id.to_string()),
            task_id: Some("task-1".to_string()),
            url: url.to_string(),
            token: None,
            authentication: None,
        }
    }

    #[test]
    fn upsert_into_is_scoped_per_tenant() {
        let mut configs = Vec::new();
        // The same config id under two different tenants must coexist; a write
        // for one tenant must not overwrite another tenant's entry.
        upsert_into(
            &mut configs,
            Some("a"),
            config("shared", Some("a"), "http://a/1"),
        );
        upsert_into(
            &mut configs,
            Some("b"),
            config("shared", Some("b"), "http://b/1"),
        );
        assert_eq!(configs.len(), 2);

        // Updating tenant "a" replaces only its entry and leaves "b" untouched.
        upsert_into(
            &mut configs,
            Some("a"),
            config("shared", Some("a"), "http://a/2"),
        );
        assert_eq!(configs.len(), 2);
        let a = configs
            .iter()
            .find(|c| c.agent_id.as_deref() == Some("a"))
            .unwrap();
        assert_eq!(a.url, "http://a/2");
        let b = configs
            .iter()
            .find(|c| c.agent_id.as_deref() == Some("b"))
            .unwrap();
        assert_eq!(b.url, "http://b/1");
    }

    #[test]
    fn decode_push_configs_metadata_rejects_malformed_state() {
        let error = decode_push_configs_metadata(json!({"tasks": []}), "task-1")
            .expect_err("malformed push config metadata must fail closed");
        match error {
            A2aError::Internal(message) => {
                assert!(message.contains(PUSH_CONFIGS_METADATA_KEY));
                assert!(message.contains("stored format error"));
                assert!(message.contains("legacy format error"));
            }
            other => panic!("expected internal corruption error, got {other:?}"),
        }
    }

    #[test]
    fn decode_push_configs_metadata_preserves_legacy_task_list() {
        let stored =
            decode_push_configs_metadata(json!([config("cfg-1", None, "http://a/1")]), "task-1")
                .expect("legacy push config list must remain readable");
        assert_eq!(stored.tasks["task-1"][0].id.as_deref(), Some("cfg-1"));
    }

    #[test]
    fn push_driver_guard_releases_key_on_drop() {
        let protocol = ProtocolModuleState::new();
        assert!(protocol.register_a2a_push_driver("task-1", Some("a"), "cfg-1"));
        // A second registration is rejected while the driver is live.
        assert!(!protocol.register_a2a_push_driver("task-1", Some("a"), "cfg-1"));

        let guard = PushDriverGuard {
            protocol: protocol.clone(),
            task_id: "task-1".to_string(),
            tenant: Some("a".to_string()),
            config_id: "cfg-1".to_string(),
        };
        drop(guard);

        // Once the guard drops (e.g. driver task panicked or was aborted) the key
        // is released and re-registration succeeds.
        assert!(protocol.register_a2a_push_driver("task-1", Some("a"), "cfg-1"));
    }
}