turul-a2a 0.2.2

A2A Protocol v1.0 server framework
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
//! Server-level A2A error types with HTTP and JSON-RPC mapping.
//!
//! Each variant maps to an exact HTTP status code, JSON-RPC error code,
//! and google.rpc.ErrorInfo reason string using wire constants from
//! `turul_a2a_types::wire::errors`.

use serde_json::{Value, json};
use turul_a2a_types::ErrorInfo;
use turul_a2a_types::wire::errors;

/// Server-level error for A2A operations.
///
/// Each variant corresponds to an A2A-specific error type from the spec
/// with exact HTTP status, JSON-RPC code, and ErrorInfo reason mappings.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum A2aError {
    #[error("Task not found: {task_id}")]
    TaskNotFound { task_id: String },

    #[error("Task not cancelable: {task_id}")]
    TaskNotCancelable { task_id: String },

    #[error("Push notifications not supported")]
    PushNotificationNotSupported,

    #[error("Unsupported operation: {message}")]
    UnsupportedOperation { message: String },

    #[error("Content type not supported: {content_type}")]
    ContentTypeNotSupported { content_type: String },

    #[error("Invalid agent response: {message}")]
    InvalidAgentResponse { message: String },

    #[error("Extended agent card not configured")]
    ExtendedAgentCardNotConfigured,

    #[error("Extension support required: {extension}")]
    ExtensionSupportRequired { extension: String },

    #[error("Version not supported: {version}")]
    VersionNotSupported { version: String },

    #[error("Invalid request: {message}")]
    InvalidRequest { message: String },

    #[error("Internal error: {0}")]
    Internal(String),
}

impl A2aError {
    /// HTTP status code per spec Section 5.4.
    pub fn http_status(&self) -> u16 {
        match self {
            Self::TaskNotFound { .. } => errors::HTTP_TASK_NOT_FOUND,
            Self::TaskNotCancelable { .. } => errors::HTTP_TASK_NOT_CANCELABLE,
            Self::PushNotificationNotSupported => errors::HTTP_PUSH_NOTIFICATION_NOT_SUPPORTED,
            Self::UnsupportedOperation { .. } => errors::HTTP_UNSUPPORTED_OPERATION,
            Self::ContentTypeNotSupported { .. } => errors::HTTP_CONTENT_TYPE_NOT_SUPPORTED,
            Self::InvalidAgentResponse { .. } => errors::HTTP_INVALID_AGENT_RESPONSE,
            Self::ExtendedAgentCardNotConfigured => errors::HTTP_EXTENDED_AGENT_CARD_NOT_CONFIGURED,
            Self::ExtensionSupportRequired { .. } => errors::HTTP_EXTENSION_SUPPORT_REQUIRED,
            Self::VersionNotSupported { .. } => errors::HTTP_VERSION_NOT_SUPPORTED,
            Self::InvalidRequest { .. } => 400,
            Self::Internal(_) => 500,
        }
    }

    /// JSON-RPC error code per spec Section 5.4 (-32001 to -32009).
    /// Non-A2A errors use standard JSON-RPC codes.
    pub fn jsonrpc_code(&self) -> i32 {
        match self {
            Self::TaskNotFound { .. } => errors::JSONRPC_TASK_NOT_FOUND,
            Self::TaskNotCancelable { .. } => errors::JSONRPC_TASK_NOT_CANCELABLE,
            Self::PushNotificationNotSupported => errors::JSONRPC_PUSH_NOTIFICATION_NOT_SUPPORTED,
            Self::UnsupportedOperation { .. } => errors::JSONRPC_UNSUPPORTED_OPERATION,
            Self::ContentTypeNotSupported { .. } => errors::JSONRPC_CONTENT_TYPE_NOT_SUPPORTED,
            Self::InvalidAgentResponse { .. } => errors::JSONRPC_INVALID_AGENT_RESPONSE,
            Self::ExtendedAgentCardNotConfigured => {
                errors::JSONRPC_EXTENDED_AGENT_CARD_NOT_CONFIGURED
            }
            Self::ExtensionSupportRequired { .. } => errors::JSONRPC_EXTENSION_SUPPORT_REQUIRED,
            Self::VersionNotSupported { .. } => errors::JSONRPC_VERSION_NOT_SUPPORTED,
            Self::InvalidRequest { .. } => -32602, // Invalid params
            Self::Internal(_) => errors::JSONRPC_INTERNAL_ERROR,
        }
    }

    /// ErrorInfo reason string (UPPER_SNAKE_CASE, no "Error" suffix).
    /// Returns `None` for non-A2A errors (InvalidRequest, Internal).
    pub fn error_reason(&self) -> Option<&'static str> {
        match self {
            Self::TaskNotFound { .. } => Some(errors::REASON_TASK_NOT_FOUND),
            Self::TaskNotCancelable { .. } => Some(errors::REASON_TASK_NOT_CANCELABLE),
            Self::PushNotificationNotSupported => {
                Some(errors::REASON_PUSH_NOTIFICATION_NOT_SUPPORTED)
            }
            Self::UnsupportedOperation { .. } => Some(errors::REASON_UNSUPPORTED_OPERATION),
            Self::ContentTypeNotSupported { .. } => Some(errors::REASON_CONTENT_TYPE_NOT_SUPPORTED),
            Self::InvalidAgentResponse { .. } => Some(errors::REASON_INVALID_AGENT_RESPONSE),
            Self::ExtendedAgentCardNotConfigured => {
                Some(errors::REASON_EXTENDED_AGENT_CARD_NOT_CONFIGURED)
            }
            Self::ExtensionSupportRequired { .. } => {
                Some(errors::REASON_EXTENSION_SUPPORT_REQUIRED)
            }
            Self::VersionNotSupported { .. } => Some(errors::REASON_VERSION_NOT_SUPPORTED),
            _ => None,
        }
    }

    /// gRPC status per spec Section 5.4, as the `google.rpc.Code` enum name.
    /// The gRPC binding maps it to a status code; the HTTP+JSON binding
    /// emits it as the AIP-193 `error.status` field.
    pub fn grpc_status(&self) -> &'static str {
        match self {
            Self::TaskNotFound { .. } => errors::GRPC_TASK_NOT_FOUND,
            Self::TaskNotCancelable { .. } => errors::GRPC_TASK_NOT_CANCELABLE,
            Self::PushNotificationNotSupported => errors::GRPC_PUSH_NOTIFICATION_NOT_SUPPORTED,
            Self::UnsupportedOperation { .. } => errors::GRPC_UNSUPPORTED_OPERATION,
            Self::ContentTypeNotSupported { .. } => errors::GRPC_CONTENT_TYPE_NOT_SUPPORTED,
            Self::InvalidAgentResponse { .. } => errors::GRPC_INVALID_AGENT_RESPONSE,
            Self::ExtendedAgentCardNotConfigured => errors::GRPC_EXTENDED_AGENT_CARD_NOT_CONFIGURED,
            Self::ExtensionSupportRequired { .. } => errors::GRPC_EXTENSION_SUPPORT_REQUIRED,
            Self::VersionNotSupported { .. } => errors::GRPC_VERSION_NOT_SUPPORTED,
            Self::InvalidRequest { .. } => "INVALID_ARGUMENT",
            Self::Internal(_) => "INTERNAL",
        }
    }

    /// Build the google.rpc.ErrorInfo JSON object for this error.
    /// Returns `None` for non-A2A errors.
    pub fn error_info(&self) -> Option<Value> {
        self.error_reason()
            .map(|reason| ErrorInfo::new(reason, errors::ERROR_DOMAIN).into())
    }

    /// Build the HTTP error response body: the `google.rpc.Status` JSON
    /// shape of AIP-193 (`code`, `status`, `message`, `details`), as in the
    /// spec Section 11.6 example.
    pub fn to_http_error_body(&self) -> Value {
        let mut body = json!({
            "error": {
                "code": self.http_status(),
                "status": self.grpc_status(),
                "message": self.to_string(),
            }
        });

        if let Some(info) = self.error_info() {
            body["error"]["details"] = json!([info]);
        }

        body
    }

    /// Build the JSON-RPC error object.
    pub fn to_jsonrpc_error(&self, id: Option<&Value>) -> Value {
        let mut error = json!({
            "code": self.jsonrpc_code(),
            "message": self.to_string(),
        });

        if let Some(info) = self.error_info() {
            error["data"] = json!([info]);
        }

        json!({
            "jsonrpc": "2.0",
            "id": id.cloned().unwrap_or(Value::Null),
            "error": error,
        })
    }
}

/// Map A2aError onto turul-rpc's `JsonRpcErrorObject` (code/message/data only).
/// The full JSON-RPC envelope (`jsonrpc`/`id`/`error`) is built by the
/// dispatcher or by `to_jsonrpc_error` above for non-dispatcher paths.
impl turul_rpc::r#async::ToJsonRpcError for A2aError {
    fn to_error_object(&self) -> turul_rpc::error::JsonRpcErrorObject {
        turul_rpc::error::JsonRpcErrorObject {
            code: self.jsonrpc_code() as i64,
            message: self.to_string(),
            data: self.error_info().map(|info| json!([info])),
        }
    }
}

impl From<crate::storage::A2aStorageError> for A2aError {
    fn from(err: crate::storage::A2aStorageError) -> Self {
        use crate::storage::A2aStorageError;
        match err {
            A2aStorageError::TaskNotFound(id) => A2aError::TaskNotFound { task_id: id },
            // A task already terminal (raw state-machine signal or the
            // terminal-write CAS loser) or an illegal transition cannot be
            // moved by the caller: TaskNotCancelable, naming the task.
            A2aStorageError::TerminalState { task_id, .. }
            | A2aStorageError::TerminalStateAlreadySet { task_id, .. }
            | A2aStorageError::InvalidTransition { task_id, .. } => {
                A2aError::TaskNotCancelable { task_id }
            }
            A2aStorageError::InvalidListRequest(message) => A2aError::InvalidRequest { message },
            other => A2aError::Internal(other.to_string()),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::storage::A2aStorageError;
    use turul_a2a_types::TaskState;

    /// One A2A error with its expected wire mapping, transcribed literally
    /// from the spec Section 5.4 table (gRPC status by `google.rpc.Code`
    /// name).
    struct Row {
        err: A2aError,
        http: u16,
        jsonrpc: i32,
        grpc: &'static str,
        reason: &'static str,
    }

    fn spec_table() -> Vec<Row> {
        vec![
            Row {
                err: A2aError::TaskNotFound {
                    task_id: "t".into(),
                },
                http: 404,
                jsonrpc: -32001,
                grpc: "NOT_FOUND",
                reason: "TASK_NOT_FOUND",
            },
            Row {
                err: A2aError::TaskNotCancelable {
                    task_id: "t".into(),
                },
                http: 400,
                jsonrpc: -32002,
                grpc: "FAILED_PRECONDITION",
                reason: "TASK_NOT_CANCELABLE",
            },
            Row {
                err: A2aError::PushNotificationNotSupported,
                http: 400,
                jsonrpc: -32003,
                grpc: "FAILED_PRECONDITION",
                reason: "PUSH_NOTIFICATION_NOT_SUPPORTED",
            },
            Row {
                err: A2aError::UnsupportedOperation {
                    message: "x".into(),
                },
                http: 400,
                jsonrpc: -32004,
                grpc: "FAILED_PRECONDITION",
                reason: "UNSUPPORTED_OPERATION",
            },
            Row {
                err: A2aError::ContentTypeNotSupported {
                    content_type: "x".into(),
                },
                http: 400,
                jsonrpc: -32005,
                grpc: "INVALID_ARGUMENT",
                reason: "CONTENT_TYPE_NOT_SUPPORTED",
            },
            Row {
                err: A2aError::InvalidAgentResponse {
                    message: "x".into(),
                },
                http: 500,
                jsonrpc: -32006,
                grpc: "INTERNAL",
                reason: "INVALID_AGENT_RESPONSE",
            },
            Row {
                err: A2aError::ExtendedAgentCardNotConfigured,
                http: 400,
                jsonrpc: -32007,
                grpc: "FAILED_PRECONDITION",
                reason: "EXTENDED_AGENT_CARD_NOT_CONFIGURED",
            },
            Row {
                err: A2aError::ExtensionSupportRequired {
                    extension: "x".into(),
                },
                http: 400,
                jsonrpc: -32008,
                grpc: "FAILED_PRECONDITION",
                reason: "EXTENSION_SUPPORT_REQUIRED",
            },
            Row {
                err: A2aError::VersionNotSupported {
                    version: "x".into(),
                },
                http: 400,
                jsonrpc: -32009,
                grpc: "FAILED_PRECONDITION",
                reason: "VERSION_NOT_SUPPORTED",
            },
        ]
    }

    #[test]
    fn every_a2a_error_maps_exactly_per_spec_table() {
        let table = spec_table();
        assert_eq!(table.len(), 9, "spec Section 5.4 defines nine A2A errors");
        for row in table {
            let err = &row.err;
            assert_eq!(err.http_status(), row.http, "HTTP status for {err}");
            assert_eq!(err.jsonrpc_code(), row.jsonrpc, "JSON-RPC code for {err}");
            assert_eq!(err.grpc_status(), row.grpc, "gRPC status for {err}");
            assert_eq!(err.error_reason(), Some(row.reason), "reason for {err}");

            let info = json!({
                "@type": "type.googleapis.com/google.rpc.ErrorInfo",
                "reason": row.reason,
                "domain": "a2a-protocol.org",
            });

            let body = err.to_http_error_body();
            assert_eq!(body["error"]["code"], row.http, "{err}");
            assert_eq!(body["error"]["status"], row.grpc, "{err}");
            assert_eq!(body["error"]["details"], json!([info]), "{err}");

            let rpc = err.to_jsonrpc_error(Some(&json!(7)));
            assert_eq!(rpc["error"]["code"], row.jsonrpc, "{err}");
            assert_eq!(rpc["error"]["data"], json!([info]), "{err}");

            #[cfg(feature = "grpc")]
            {
                use tonic_types::StatusExt;
                let status = crate::grpc::error::a2a_to_status(row.err);
                let code = match row.grpc {
                    "NOT_FOUND" => tonic::Code::NotFound,
                    "FAILED_PRECONDITION" => tonic::Code::FailedPrecondition,
                    "INVALID_ARGUMENT" => tonic::Code::InvalidArgument,
                    "INTERNAL" => tonic::Code::Internal,
                    other => panic!("unexpected status {other}"),
                };
                assert_eq!(status.code(), code, "gRPC code for {}", row.reason);
                let info = status.get_details_error_info().expect("ErrorInfo");
                assert_eq!(info.reason, row.reason);
                assert_eq!(info.domain, "a2a-protocol.org");
            }
        }
    }

    #[test]
    fn non_a2a_errors_map_to_standard_codes_without_error_info() {
        let invalid = A2aError::InvalidRequest {
            message: "x".into(),
        };
        assert_eq!(invalid.http_status(), 400);
        assert_eq!(invalid.jsonrpc_code(), -32602);
        assert_eq!(invalid.grpc_status(), "INVALID_ARGUMENT");
        assert!(invalid.error_info().is_none());
        assert!(
            invalid.to_http_error_body()["error"]
                .get("details")
                .is_none()
        );

        let internal = A2aError::Internal("x".into());
        assert_eq!(internal.http_status(), 500);
        assert_eq!(internal.jsonrpc_code(), -32603);
        assert_eq!(internal.grpc_status(), "INTERNAL");
        assert!(internal.error_info().is_none());
    }

    #[test]
    fn http_error_body_matches_spec_example_shape() {
        // Spec Section 11.6 example: code, status, message, details.
        let body = A2aError::TaskNotFound {
            task_id: "t-123".into(),
        }
        .to_http_error_body();
        let error = body["error"].as_object().unwrap();
        let mut keys: Vec<&str> = error.keys().map(String::as_str).collect();
        keys.sort_unstable();
        assert_eq!(keys, ["code", "details", "message", "status"]);
        assert_eq!(body["error"]["status"], "NOT_FOUND");
        assert!(body["error"]["message"].as_str().unwrap().contains("t-123"));
    }

    #[test]
    fn error_bodies_are_byte_stable() {
        let err = A2aError::TaskNotFound {
            task_id: "t-1".into(),
        };
        assert_eq!(
            err.to_http_error_body().to_string(),
            r#"{"error":{"code":404,"details":[{"@type":"type.googleapis.com/google.rpc.ErrorInfo","domain":"a2a-protocol.org","reason":"TASK_NOT_FOUND"}],"message":"Task not found: t-1","status":"NOT_FOUND"}}"#
        );
        assert_eq!(
            err.to_jsonrpc_error(Some(&json!(7))).to_string(),
            r#"{"error":{"code":-32001,"data":[{"@type":"type.googleapis.com/google.rpc.ErrorInfo","domain":"a2a-protocol.org","reason":"TASK_NOT_FOUND"}],"message":"Task not found: t-1"},"id":7,"jsonrpc":"2.0"}"#
        );
    }

    #[test]
    fn jsonrpc_error_envelope_carries_request_id() {
        let resp = A2aError::TaskNotCancelable {
            task_id: "t-456".into(),
        }
        .to_jsonrpc_error(Some(&json!(42)));
        assert_eq!(resp["jsonrpc"], "2.0");
        assert_eq!(resp["id"], 42);
    }

    #[test]
    fn jsonrpc_error_null_id_when_none() {
        let resp = A2aError::Internal("oops".into()).to_jsonrpc_error(None);
        assert!(resp["id"].is_null());
        assert!(resp["error"].get("data").is_none());
    }

    // =========================================================
    // Storage error mapping — every arm
    // =========================================================

    fn not_cancelable_id(err: A2aError) -> String {
        match err {
            A2aError::TaskNotCancelable { task_id } => task_id,
            other => panic!("expected TaskNotCancelable, got {other:?}"),
        }
    }

    #[test]
    fn storage_task_not_found_maps_to_task_not_found() {
        let err = A2aError::from(A2aStorageError::TaskNotFound("t-1".into()));
        assert!(matches!(err, A2aError::TaskNotFound { ref task_id } if task_id == "t-1"));
    }

    #[test]
    fn storage_terminal_state_maps_to_not_cancelable_with_task_id() {
        let err = A2aError::from(A2aStorageError::TerminalState {
            task_id: "t-2".into(),
            state: TaskState::Completed,
        });
        assert_eq!(not_cancelable_id(err), "t-2");
    }

    #[test]
    fn storage_terminal_cas_loser_maps_to_not_cancelable_with_task_id() {
        let err = A2aError::from(A2aStorageError::TerminalStateAlreadySet {
            task_id: "t-3".into(),
            current_state: "TASK_STATE_CANCELED".into(),
        });
        assert_eq!(not_cancelable_id(err), "t-3");
    }

    #[test]
    fn storage_invalid_transition_maps_to_not_cancelable_with_task_id() {
        let err = A2aError::from(A2aStorageError::InvalidTransition {
            task_id: "t-4".into(),
            current: TaskState::Submitted,
            requested: TaskState::InputRequired,
        });
        assert_eq!(not_cancelable_id(err), "t-4");
    }

    #[test]
    fn other_storage_errors_map_to_internal() {
        for storage_err in [
            A2aStorageError::OwnerMismatch {
                task_id: "t".into(),
            },
            A2aStorageError::TenantMismatch {
                task_id: "t".into(),
            },
            A2aStorageError::ConcurrentModification("c".into()),
            A2aStorageError::PushConfigNotFound("p".into()),
            A2aStorageError::DatabaseError("d".into()),
            A2aStorageError::SerializationError("s".into()),
            A2aStorageError::Generic("g".into()),
        ] {
            let text = storage_err.to_string();
            match A2aError::from(storage_err) {
                A2aError::Internal(msg) => assert_eq!(msg, text),
                other => panic!("expected Internal for {text}, got {other:?}"),
            }
        }
    }
}