Skip to main content

turul_a2a_aws_lambda/
lib.rs

1//! AWS Lambda adapter for turul-a2a.
2//!
3//! Thin wrapper: converts Lambda events to axum requests, delegates to the
4//! same Router, converts responses back. Per ADR-008/ADR-009:
5//! - Authorizer context mapped via synthetic headers with anti-spoofing
6//! - Streaming supported via durable event store (D3)
7//! - SSE responses are buffered: task executes, events are collected, returned as one response
8//! - `POST /message:stream` executes the task within the Lambda invocation and returns all events
9//! - `GET /tasks/{id}:subscribe` is for tasks that are **not** in a terminal
10//!   state. Within one invocation it emits the initial `Task` snapshot,
11//!   replays stored events via `Last-Event-ID`, and closes when the task
12//!   reaches a terminal state. Subscribing to an already-terminal task
13//!   returns `UnsupportedOperationError` per A2A v1.0 §3.1.6 / ADR-010
14//!   §4.3. For retrieving a terminal task's final state use `GetTask`.
15//!
16//! # Push-notification delivery — external triggers are mandatory
17//!
18//! Push delivery on Lambda (ADR-013) is architecturally different
19//! from the binary server. The request Lambda installed via
20//! [`LambdaA2aServerBuilder`] still constructs a `PushDispatcher`
21//! when `push_delivery_store` is wired, but any `tokio::spawn`
22//! continuation it emits post-return is **opportunistic only** — the
23//! Lambda execution environment may be frozen indefinitely between
24//! invocations, so nothing can depend on that continuation completing
25//! (ADR-013 §4.4).
26//!
27//! Correctness for push delivery on Lambda is carried by:
28//!
29//! 1. The atomic pending-dispatch marker written inside the request
30//!    Lambda's commit transaction (ADR-013 §4.3 — opt in via
31//!    `StorageImpl::with_push_dispatch_enabled(true)`).
32//! 2. [`LambdaStreamRecoveryHandler`] — DynamoDB Streams trigger on
33//!    `a2a_push_pending_dispatches`. DynamoDB backends only.
34//! 3. [`LambdaScheduledRecoveryHandler`] — EventBridge Scheduler
35//!    backstop. **Required for all backends**; it is the sole
36//!    recovery path for SQLite / PostgreSQL / in-memory deployments.
37//!
38//! Without at least the scheduled worker, push delivery on Lambda is
39//! not durable — a marker written on a cold invocation may never be
40//! consumed. The example wiring lives in
41//! `examples/lambda-stream-worker` and
42//! `examples/lambda-scheduled-worker`.
43//!
44//! Lambda streaming is request-scoped (not persistent SSE connections). The durable
45//! event store ensures events survive across invocations. Clients reconnect with
46//! `Last-Event-ID` for continuation.
47//!
48//! # Cross-instance cancellation
49//!
50//! Lambda invocations are stateless and short-lived, so the Lambda
51//! adapter does **not** run the persistent cross-instance cancel
52//! poller that `A2aServer::run()` spawns. Cancellation behaviour on
53//! the Lambda adapter:
54//!
55//! - **Marker writes** — `CancelTask` on a Lambda invocation writes
56//!   the cancel marker to the shared backend (DynamoDB / PostgreSQL).
57//!   This works.
58//! - **Propagation to a live executor on the SAME Lambda invocation** —
59//!   works via the same-instance token-trip path in `core_cancel_task`.
60//! - **Propagation to a live executor on a DIFFERENT Lambda invocation
61//!   (warm container)** — **not currently supported**. There is no
62//!   persistent poller to observe markers written by other
63//!   invocations. A subsequent invocation whose handler reads the
64//!   marker directly may act on it, but that is not a substitute for
65//!   the server runtime's live propagation.
66//!
67//! The builder still **requires** an `A2aCancellationSupervisor`
68//! implementation on the same backend so that marker writes reach
69//! the correct backend and a future polling-adapter variant can
70//! consume them. If a deployment passes a non-matching supervisor,
71//! `build()` rejects the configuration.
72
73mod adapter;
74mod auth;
75mod no_streaming;
76mod scheduled_recovery;
77mod stream_recovery;
78
79#[cfg(test)]
80mod builder_tests;
81#[cfg(test)]
82mod scheduled_recovery_tests;
83#[cfg(test)]
84mod stream_recovery_tests;
85
86pub use adapter::{axum_to_lambda_response, lambda_to_axum_request};
87pub use auth::{AuthorizerMapping, LambdaAuthorizerMiddleware};
88pub use no_streaming::NoStreamingLayer;
89pub use scheduled_recovery::{
90    LambdaScheduledRecoveryConfig, LambdaScheduledRecoveryHandler, LambdaScheduledRecoveryResponse,
91};
92pub use stream_recovery::LambdaStreamRecoveryHandler;
93
94use std::sync::Arc;
95
96use turul_a2a::error::A2aError;
97use turul_a2a::executor::AgentExecutor;
98use turul_a2a::middleware::{A2aMiddleware, MiddlewareStack};
99use turul_a2a::push::A2aPushDeliveryStore;
100use turul_a2a::router::{AppState, build_router};
101use turul_a2a::server::RuntimeConfig;
102use turul_a2a::storage::{
103    A2aAtomicStore, A2aCancellationSupervisor, A2aEventStore, A2aPushNotificationStorage,
104    A2aTaskStorage,
105};
106use turul_a2a::streaming::TaskEventBroker;
107
108/// Builder for Lambda A2A handler.
109///
110/// Use `.storage(my_storage)` to supply a single backend implementing all storage traits.
111/// This satisfies ADR-009's same-backend requirement and enables streaming via durable
112/// event store.
113///
114/// For push-notification delivery, wire `.storage()` with a backend that implements
115/// [`A2aPushDeliveryStore`] (all four first-party backends do). The atomic store
116/// must also opt in via `with_push_dispatch_enabled(true)` (ADR-013 §4.3); the
117/// builder rejects configurations where the flag and the delivery store disagree.
118pub struct LambdaA2aServerBuilder {
119    executor: Option<Arc<dyn AgentExecutor>>,
120    task_storage: Option<Arc<dyn A2aTaskStorage>>,
121    push_storage: Option<Arc<dyn A2aPushNotificationStorage>>,
122    event_store: Option<Arc<dyn A2aEventStore>>,
123    atomic_store: Option<Arc<dyn A2aAtomicStore>>,
124    cancellation_supervisor: Option<Arc<dyn A2aCancellationSupervisor>>,
125    push_delivery_store: Option<Arc<dyn A2aPushDeliveryStore>>,
126    middleware: Vec<Arc<dyn A2aMiddleware>>,
127    runtime_config: RuntimeConfig,
128}
129
130impl LambdaA2aServerBuilder {
131    pub fn new() -> Self {
132        Self {
133            executor: None,
134            task_storage: None,
135            push_storage: None,
136            event_store: None,
137            atomic_store: None,
138            cancellation_supervisor: None,
139            push_delivery_store: None,
140            middleware: vec![],
141            runtime_config: RuntimeConfig::default(),
142        }
143    }
144
145    pub fn executor(mut self, exec: impl AgentExecutor + 'static) -> Self {
146        self.executor = Some(Arc::new(exec));
147        self
148    }
149
150    /// Set all storage from a single backend instance.
151    ///
152    /// This is the **preferred** method — a single struct implementing all
153    /// storage traits guarantees the same-backend requirement (ADR-009)
154    /// AND ensures the cancellation supervisor reads the same backend
155    /// the cancel marker is written to. A mismatch here (e.g., DynamoDB
156    /// task storage + in-memory cancellation supervisor) silently
157    /// breaks cross-instance cancellation.
158    ///
159    /// The bound also requires [`A2aPushDeliveryStore`] so push-delivery
160    /// infrastructure is wired by the same call. Backends that implement
161    /// push delivery MUST also opt in via `with_push_dispatch_enabled(true)`
162    /// on the storage instance; the builder rejects the inconsistent cases
163    /// (ADR-013 §4.3).
164    pub fn storage<S>(mut self, storage: S) -> Self
165    where
166        S: A2aTaskStorage
167            + A2aPushNotificationStorage
168            + A2aEventStore
169            + A2aAtomicStore
170            + A2aCancellationSupervisor
171            + A2aPushDeliveryStore
172            + Clone
173            + 'static,
174    {
175        self.task_storage = Some(Arc::new(storage.clone()));
176        self.push_storage = Some(Arc::new(storage.clone()));
177        self.event_store = Some(Arc::new(storage.clone()));
178        self.atomic_store = Some(Arc::new(storage.clone()));
179        self.cancellation_supervisor = Some(Arc::new(storage.clone()));
180        self.push_delivery_store = Some(Arc::new(storage));
181        self
182    }
183
184    /// Set task storage individually. Must be paired with event_store/atomic_store
185    /// AND `cancellation_supervisor()` for same-backend compliance.
186    pub fn task_storage(mut self, s: impl A2aTaskStorage + 'static) -> Self {
187        self.task_storage = Some(Arc::new(s));
188        self
189    }
190
191    /// Set push notification storage individually.
192    pub fn push_storage(mut self, s: impl A2aPushNotificationStorage + 'static) -> Self {
193        self.push_storage = Some(Arc::new(s));
194        self
195    }
196
197    /// Set event store individually. Must match task_storage backend.
198    pub fn event_store(mut self, s: impl A2aEventStore + 'static) -> Self {
199        self.event_store = Some(Arc::new(s));
200        self
201    }
202
203    /// Set atomic store individually. Must match task_storage backend.
204    pub fn atomic_store(mut self, s: impl A2aAtomicStore + 'static) -> Self {
205        self.atomic_store = Some(Arc::new(s));
206        self
207    }
208
209    /// Set the cancellation supervisor individually. Must match
210    /// task_storage backend — otherwise `:cancel` writes the marker to
211    /// one backend and the supervisor reads from another, silently
212    /// breaking cross-instance cancellation. Prefer `.storage()` for
213    /// unified wiring. Consumed by `core_cancel_task` (ADR-012).
214    pub fn cancellation_supervisor(mut self, s: impl A2aCancellationSupervisor + 'static) -> Self {
215        self.cancellation_supervisor = Some(Arc::new(s));
216        self
217    }
218
219    /// Set the push delivery store individually (ADR-011 §10 / ADR-013).
220    ///
221    /// Prefer `.storage()` for unified wiring. The delivery store MUST
222    /// be on the same backend as `.task_storage(...)` — the builder
223    /// rejects mismatches. Passing a delivery store also requires the
224    /// atomic store to have opted in via `with_push_dispatch_enabled(true)`
225    /// (ADR-013 §4.3).
226    pub fn push_delivery_store(mut self, store: impl A2aPushDeliveryStore + 'static) -> Self {
227        self.push_delivery_store = Some(Arc::new(store));
228        self
229    }
230
231    /// Override the runtime configuration (timeouts, retry budgets,
232    /// push tuning). Defaults to [`RuntimeConfig::default()`]. The
233    /// builder validates push settings (claim expiry vs retry horizon,
234    /// ADR-011 §10.3) when `.push_delivery_store(...)` is wired.
235    pub fn runtime_config(mut self, cfg: RuntimeConfig) -> Self {
236        self.runtime_config = cfg;
237        self
238    }
239
240    pub fn middleware(mut self, mw: Arc<dyn A2aMiddleware>) -> Self {
241        self.middleware.push(mw);
242        self
243    }
244
245    pub fn build(self) -> Result<LambdaA2aHandler, A2aError> {
246        let executor = self
247            .executor
248            .ok_or(A2aError::Internal("executor is required".into()))?;
249        let task_storage = self.task_storage.ok_or(A2aError::Internal(
250            "task_storage is required for Lambda".into(),
251        ))?;
252        let push_storage = self.push_storage.ok_or(A2aError::Internal(
253            "push_storage is required for Lambda".into(),
254        ))?;
255        let event_store: Arc<dyn A2aEventStore> = self.event_store.ok_or(A2aError::Internal(
256            "event_store is required for Lambda (use .storage() for unified backend)".into(),
257        ))?;
258        let atomic_store: Arc<dyn A2aAtomicStore> = self.atomic_store.ok_or(A2aError::Internal(
259            "atomic_store is required for Lambda (use .storage() for unified backend)".into(),
260        ))?;
261        // ADR-012: the cancellation supervisor MUST be supplied. No
262        // InMemoryA2aStorage fallback: that would silently break
263        // cross-instance cancellation by reading markers from a
264        // different backend than they were written to.
265        let cancellation_supervisor: Arc<dyn A2aCancellationSupervisor> =
266            self.cancellation_supervisor.ok_or(A2aError::Internal(
267                "cancellation_supervisor is required for Lambda. Use .storage() for unified \
268                 backend wiring, or .cancellation_supervisor(...) alongside the individual \
269                 storage setters. Omitting it would silently break cross-instance \
270                 cancellation in ADR-012."
271                    .into(),
272            ))?;
273
274        // Same-backend enforcement (ADR-009 + ADR-012).
275        let task_backend = task_storage.backend_name();
276        let push_backend = push_storage.backend_name();
277        let event_backend = event_store.backend_name();
278        let atomic_backend = atomic_store.backend_name();
279        let supervisor_backend = cancellation_supervisor.backend_name();
280        if task_backend != push_backend
281            || task_backend != event_backend
282            || task_backend != atomic_backend
283            || task_backend != supervisor_backend
284        {
285            return Err(A2aError::Internal(format!(
286                "Storage backend mismatch: task={task_backend}, push={push_backend}, \
287                 event={event_backend}, atomic={atomic_backend}, \
288                 cancellation_supervisor={supervisor_backend}. \
289                 ADR-009 requires all storage traits to share the same backend. \
290                 ADR-012 requires the cancellation supervisor on the same backend \
291                 so cross-instance cancel markers are observable. \
292                 Use .storage() for unified backend."
293            )));
294        }
295
296        let push_delivery_store = self.push_delivery_store;
297
298        // Push-dispatch consistency (ADR-013 §4.3): the atomic store's
299        // opt-in flag and the presence of `push_delivery_store` MUST
300        // agree. Both-true = push fully wired; both-false = non-push
301        // deployment. Mixed cases are build errors — this mirrors the
302        // main server builder (server/mod.rs).
303        match (
304            push_delivery_store.is_some(),
305            atomic_store.push_dispatch_enabled(),
306        ) {
307            (true, true) | (false, false) => {}
308            (true, false) => {
309                return Err(A2aError::Internal(
310                    "push_delivery_store wired but atomic_store.push_dispatch_enabled() \
311                     is false. Call .with_push_dispatch_enabled(true) on the backend \
312                     storage before passing it to .storage()."
313                        .into(),
314                ));
315            }
316            (false, true) => {
317                return Err(A2aError::Internal(
318                    "atomic_store.push_dispatch_enabled() is true but no \
319                     push_delivery_store is wired. Pending-dispatch markers would be \
320                     written with no consumer, imposing load-bearing infra for no \
321                     benefit. If you need to populate markers for an external \
322                     consumer, open an issue for a distinctly-named opt-in — for now, \
323                     this configuration is rejected."
324                        .into(),
325                ));
326            }
327        }
328
329        let runtime_config = self.runtime_config;
330
331        // Push dispatcher wiring (ADR-011 §10 / ADR-013 §7). When
332        // `push_delivery_store` is present, validate its backend matches,
333        // validate the retry-horizon invariant, and construct the
334        // `PushDispatcher`. Under Lambda the dispatcher runs
335        // opportunistically post-return (ADR-013 §4.4); correctness
336        // comes from the atomic marker + stream/scheduler recovery.
337        let push_dispatcher: Option<Arc<turul_a2a::push::PushDispatcher>> =
338            if let Some(delivery) = push_delivery_store.as_ref() {
339                let push_delivery_backend = delivery.backend_name();
340                if task_backend != push_delivery_backend {
341                    return Err(A2aError::Internal(format!(
342                        "Storage backend mismatch: task={task_backend}, \
343                         push_delivery={push_delivery_backend}. \
344                         ADR-009 requires all storage traits to share the same backend."
345                    )));
346                }
347
348                // ADR-011 §10.3: claim expiry must exceed the worst-case
349                // retry horizon so a live claim is not mis-classified as
350                // stale mid-retry.
351                let retry_horizon = runtime_config
352                    .push_backoff_cap
353                    .saturating_mul(runtime_config.push_max_attempts as u32);
354                if runtime_config.push_claim_expiry <= retry_horizon {
355                    return Err(A2aError::Internal(format!(
356                        "push_claim_expiry ({:?}) must be greater than retry horizon \
357                         (push_max_attempts={} * push_backoff_cap={:?} = {:?}). \
358                         Raise push_claim_expiry or lower push_max_attempts/push_backoff_cap.",
359                        runtime_config.push_claim_expiry,
360                        runtime_config.push_max_attempts,
361                        runtime_config.push_backoff_cap,
362                        retry_horizon
363                    )));
364                }
365
366                let mut delivery_cfg = turul_a2a::push::delivery::PushDeliveryConfig::default();
367                delivery_cfg.max_attempts = runtime_config.push_max_attempts as u32;
368                delivery_cfg.backoff_base = runtime_config.push_backoff_base;
369                delivery_cfg.backoff_cap = runtime_config.push_backoff_cap;
370                delivery_cfg.backoff_jitter = runtime_config.push_backoff_jitter;
371                delivery_cfg.request_timeout = runtime_config.push_request_timeout;
372                delivery_cfg.connect_timeout = runtime_config.push_connect_timeout;
373                delivery_cfg.read_timeout = runtime_config.push_read_timeout;
374                delivery_cfg.claim_expiry = runtime_config.push_claim_expiry;
375                delivery_cfg.max_payload_bytes = runtime_config.push_max_payload_bytes;
376                delivery_cfg.allow_insecure_urls = runtime_config.allow_insecure_push_urls;
377
378                let instance_id = format!("a2a-lambda-{}", uuid::Uuid::now_v7());
379                let worker = turul_a2a::push::delivery::PushDeliveryWorker::new(
380                    delivery.clone(),
381                    delivery_cfg,
382                    None,
383                    instance_id,
384                )
385                .map_err(|e| A2aError::Internal(format!("push worker build failed: {e}")))?;
386
387                Some(Arc::new(turul_a2a::push::PushDispatcher::new(
388                    Arc::new(worker),
389                    push_storage.clone(),
390                    task_storage.clone(),
391                )))
392            } else {
393                None
394            };
395
396        let state = AppState {
397            executor,
398            task_storage,
399            push_storage,
400            event_store,
401            atomic_store,
402            event_broker: TaskEventBroker::new(),
403            middleware_stack: Arc::new(MiddlewareStack::new(self.middleware)),
404            runtime_config,
405            in_flight: Arc::new(turul_a2a::server::in_flight::InFlightRegistry::new()),
406            cancellation_supervisor,
407            push_delivery_store,
408            push_dispatcher,
409        };
410
411        let router = build_router(state);
412
413        Ok(LambdaA2aHandler { router })
414    }
415}
416
417impl Default for LambdaA2aServerBuilder {
418    fn default() -> Self {
419        Self::new()
420    }
421}
422
423/// Lambda handler wrapping the axum Router.
424#[derive(Clone)]
425pub struct LambdaA2aHandler {
426    router: axum::Router,
427}
428
429impl LambdaA2aHandler {
430    pub fn builder() -> LambdaA2aServerBuilder {
431        LambdaA2aServerBuilder::new()
432    }
433
434    /// Handle a Lambda HTTP request.
435    pub async fn handle(
436        &self,
437        event: lambda_http::Request,
438    ) -> Result<lambda_http::Response<lambda_http::Body>, lambda_http::Error> {
439        let axum_req = lambda_to_axum_request(event)?;
440
441        let axum_resp = tower::ServiceExt::oneshot(self.router.clone(), axum_req)
442            .await
443            .map_err(|e| lambda_http::Error::from(format!("Router error: {e}")))?;
444
445        axum_to_lambda_response(axum_resp).await
446    }
447}