a2a_protocol_core 0.1.1

Pure A2A (Agent-to-Agent) protocol domain logic - WASM optimized
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
//! Task Storage Service
//!
//! Provides task storage and retrieval services for A2A protocol operations.
//! This is a pure domain service with no infrastructure dependencies.

use crate::{A2AError, A2AResult, data::message::Message, data::task::Task};
use std::collections::HashMap;
use std::sync::{Arc, RwLock};

/// **Context Information for Conversation Management**
///
/// Represents a conversation context that contains multiple tasks
/// following the A2A protocol specification.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ConversationContext {
    /// Unique context identifier (persistent across tasks)
    pub context_id: String,
    /// When the conversation started
    pub created_at: String,
    /// Last interaction time
    pub last_active: String,
    /// Number of tasks in this context
    pub task_count: u32,
}

impl ConversationContext {
    /// Create a new conversation context
    pub fn new(context_id: String) -> Self {
        let now = Self::current_timestamp();
        Self {
            context_id,
            created_at: now.clone(),
            last_active: now,
            task_count: 0,
        }
    }

    /// Update last active timestamp and increment task count
    pub fn update_activity(&mut self) {
        self.last_active = Self::current_timestamp();
        self.task_count += 1;
    }

    fn current_timestamp() -> String {
        #[cfg(feature = "time-stamps")]
        {
            chrono::Utc::now().to_rfc3339()
        }
        #[cfg(not(feature = "time-stamps"))]
        {
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs()
                .to_string()
        }
    }
}

/// **Enhanced Task Storage Service Interface**
///
/// Defines the contract for task storage operations with proper A2A protocol
/// support for context-based operations (1 context : N tasks relationship).
pub trait TaskStorage: Send + Sync {
    // ============= EXISTING METHODS (MAINTAINED FOR COMPATIBILITY) =============

    /// Store a new or updated task
    fn store_task(&self, task: Task) -> A2AResult<()>;

    /// Retrieve a task by ID
    fn get_task(&self, task_id: &str) -> A2AResult<Option<Task>>;

    /// Update an existing task
    fn update_task(&self, task: Task) -> A2AResult<()>;

    /// List all tasks (for tasks/list method)
    fn list_tasks(&self) -> A2AResult<Vec<Task>>;

    /// Remove a task (if needed for cleanup)
    fn remove_task(&self, task_id: &str) -> A2AResult<bool>;

    /// Check if a task exists
    fn task_exists(&self, task_id: &str) -> A2AResult<bool>;

    // ============= NEW CONTEXT-BASED OPERATIONS (A2A PROTOCOL) =============

    /// Get all tasks within a specific context (conversation)
    ///
    /// This is the key method for A2A protocol compliance - allows retrieving
    /// all tasks that belong to the same conversation context.
    fn get_tasks_by_context(&self, context_id: &str) -> A2AResult<Vec<Task>>;

    /// Get the latest (most recently created) task in a context
    ///
    /// Useful for conversation continuity and determining the most recent
    /// task state within a conversation.
    fn get_latest_task_in_context(&self, context_id: &str) -> A2AResult<Option<Task>>;

    /// Get conversation history from all tasks in a context
    ///
    /// Aggregates message history from all tasks in a conversation context
    /// for LLM context management as specified in A2A protocol.
    fn get_context_history(&self, context_id: &str) -> A2AResult<Vec<Message>>;

    /// Get or create conversation context
    ///
    /// Manages conversation context lifecycle - creates new context if it
    /// doesn't exist, returns existing context otherwise.
    fn get_or_create_context(&self, context_id: &str) -> A2AResult<ConversationContext>;

    /// Update context activity (called when new task is added)
    ///
    /// Updates the conversation context metadata when tasks are added
    /// to track conversation lifecycle.
    fn update_context_activity(&self, context_id: &str) -> A2AResult<()>;

    /// List all conversation contexts
    ///
    /// Returns all active conversation contexts for management/debugging.
    fn list_contexts(&self) -> A2AResult<Vec<ConversationContext>>;
}

/// In-Memory Task Storage Implementation
///
/// Provides a simple in-memory task storage for development and testing.
/// In production, this would be replaced with persistent storage.
///
/// **Enhanced A2A Protocol Support**: Now includes separate context tracking
/// for proper conversation management with 1:N context:task relationship.
#[derive(Debug, Clone)]
pub struct InMemoryTaskStorage {
    /// Task storage: task_id -> Task
    tasks: Arc<RwLock<HashMap<String, Task>>>,
    /// Context storage: context_id -> ConversationContext
    contexts: Arc<RwLock<HashMap<String, ConversationContext>>>,
    /// Task insertion order tracking: context_id -> Vec<task_id> (newest last)
    task_order: Arc<RwLock<HashMap<String, Vec<String>>>>,
}

impl InMemoryTaskStorage {
    /// Create a new in-memory task storage
    pub fn new() -> Self {
        Self {
            tasks: Arc::new(RwLock::new(HashMap::new())),
            contexts: Arc::new(RwLock::new(HashMap::new())),
            task_order: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Get the number of stored tasks (for testing)
    pub fn task_count(&self) -> usize {
        self.tasks.read().unwrap().len()
    }

    /// Get the number of stored contexts (for testing)
    pub fn context_count(&self) -> usize {
        self.contexts.read().unwrap().len()
    }

    /// Clear all tasks and contexts (for testing)
    pub fn clear(&self) {
        self.tasks.write().unwrap().clear();
        self.contexts.write().unwrap().clear();
        self.task_order.write().unwrap().clear();
    }
}

impl Default for InMemoryTaskStorage {
    fn default() -> Self {
        Self::new()
    }
}

impl TaskStorage for InMemoryTaskStorage {
    fn store_task(&self, task: Task) -> A2AResult<()> {
        let mut tasks = self
            .tasks
            .write()
            .map_err(|_| A2AError::internal("Failed to acquire task storage lock"))?;

        // Check if this is a new task (not an update)
        let is_new_task = !tasks.contains_key(&task.id);

        // Store the task
        tasks.insert(task.id.clone(), task.clone());

        // Update task order
        let mut task_order = self
            .task_order
            .write()
            .map_err(|_| A2AError::internal("Failed to acquire task order lock"))?;
        task_order
            .entry(task.context_id.clone())
            .or_insert_with(Vec::new)
            .push(task.id.clone());

        // Release task lock before acquiring context lock to avoid deadlock
        drop(tasks);
        drop(task_order);

        // Update context activity if this is a new task
        if is_new_task {
            let mut contexts = self
                .contexts
                .write()
                .map_err(|_| A2AError::internal("Failed to acquire context storage lock"))?;

            if let Some(context) = contexts.get_mut(&task.context_id) {
                context.update_activity();
            } else {
                // Create new context for this task
                let mut new_context = ConversationContext::new(task.context_id.clone());
                new_context.update_activity();
                contexts.insert(task.context_id.clone(), new_context);
            }
        }

        Ok(())
    }

    fn get_task(&self, task_id: &str) -> A2AResult<Option<Task>> {
        let tasks = self
            .tasks
            .read()
            .map_err(|_| A2AError::internal("Failed to acquire task storage lock"))?;

        Ok(tasks.get(task_id).cloned())
    }

    fn update_task(&self, task: Task) -> A2AResult<()> {
        let mut tasks = self
            .tasks
            .write()
            .map_err(|_| A2AError::internal("Failed to acquire task storage lock"))?;

        if tasks.contains_key(&task.id) {
            tasks.insert(task.id.clone(), task);
            Ok(())
        } else {
            Err(A2AError::method_execution_failed(
                "tasks/update",
                &format!("Task not found: {}", &task.id),
            ))
        }
    }

    fn list_tasks(&self) -> A2AResult<Vec<Task>> {
        let tasks = self
            .tasks
            .read()
            .map_err(|_| A2AError::internal("Failed to acquire task storage lock"))?;

        Ok(tasks.values().cloned().collect())
    }

    fn remove_task(&self, task_id: &str) -> A2AResult<bool> {
        let mut tasks = self
            .tasks
            .write()
            .map_err(|_| A2AError::internal("Failed to acquire task storage lock"))?;

        Ok(tasks.remove(task_id).is_some())
    }

    fn task_exists(&self, task_id: &str) -> A2AResult<bool> {
        let tasks = self
            .tasks
            .read()
            .map_err(|_| A2AError::internal("Failed to acquire task storage lock"))?;

        Ok(tasks.contains_key(task_id))
    }

    fn get_tasks_by_context(&self, context_id: &str) -> A2AResult<Vec<Task>> {
        let tasks = self
            .tasks
            .read()
            .map_err(|_| A2AError::internal("Failed to acquire task storage lock"))?;

        Ok(tasks
            .values()
            .cloned()
            .filter(|task| task.context_id == context_id)
            .collect())
    }

    fn get_latest_task_in_context(&self, context_id: &str) -> A2AResult<Option<Task>> {
        let task_order = self
            .task_order
            .read()
            .map_err(|_| A2AError::internal("Failed to acquire task order lock"))?;

        if let Some(task_ids) = task_order.get(context_id) {
            // Find the task with the highest insertion order (newest last)
            let latest_task_id = task_ids.last().cloned();
            if let Some(task_id) = latest_task_id {
                let tasks = self
                    .tasks
                    .read()
                    .map_err(|_| A2AError::internal("Failed to acquire task storage lock"))?;
                Ok(tasks.get(&task_id).cloned())
            } else {
                Ok(None)
            }
        } else {
            Ok(None)
        }
    }

    fn get_context_history(&self, context_id: &str) -> A2AResult<Vec<Message>> {
        let task_order = self
            .task_order
            .read()
            .map_err(|_| A2AError::internal("Failed to acquire task order lock"))?;
        let tasks = self
            .tasks
            .read()
            .map_err(|_| A2AError::internal("Failed to acquire task storage lock"))?;

        let mut messages: Vec<Message> = Vec::new();

        // Get tasks in insertion order
        if let Some(task_ids) = task_order.get(context_id) {
            for task_id in task_ids {
                if let Some(task) = tasks.get(task_id) {
                    if let Some(ref task_history) = task.history {
                        messages.extend(task_history.iter().cloned());
                    }
                }
            }
        }

        Ok(messages)
    }

    fn get_or_create_context(&self, context_id: &str) -> A2AResult<ConversationContext> {
        let mut contexts = self
            .contexts
            .write()
            .map_err(|_| A2AError::internal("Failed to acquire context storage lock"))?;

        if let Some(context) = contexts.get(context_id) {
            Ok(context.clone())
        } else {
            let new_context = ConversationContext::new(context_id.to_string());
            contexts.insert(context_id.to_string(), new_context.clone());
            Ok(new_context)
        }
    }

    fn update_context_activity(&self, context_id: &str) -> A2AResult<()> {
        let mut contexts = self
            .contexts
            .write()
            .map_err(|_| A2AError::internal("Failed to acquire context storage lock"))?;

        if let Some(context) = contexts.get_mut(context_id) {
            context.update_activity();
            Ok(())
        } else {
            Err(A2AError::method_execution_failed(
                "update_context_activity",
                &format!("Context not found: {}", context_id),
            ))
        }
    }

    fn list_contexts(&self) -> A2AResult<Vec<ConversationContext>> {
        let contexts = self
            .contexts
            .read()
            .map_err(|_| A2AError::internal("Failed to acquire context storage lock"))?;

        Ok(contexts.values().cloned().collect())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::data::task::TaskState;

    #[test]
    fn test_in_memory_storage_basic_operations() {
        let storage = InMemoryTaskStorage::new();

        // Create a test task
        let task = Task::new("test-context".to_string());
        let task_id = task.id.clone();

        // Store task
        storage.store_task(task.clone()).unwrap();
        assert_eq!(storage.task_count(), 1);

        // Retrieve task
        let retrieved = storage.get_task(&task_id).unwrap();
        assert!(retrieved.is_some());
        assert_eq!(retrieved.unwrap().id, task_id);

        // Check existence
        assert!(storage.task_exists(&task_id).unwrap());
        assert!(!storage.task_exists("non-existent").unwrap());

        // List tasks
        let tasks = storage.list_tasks().unwrap();
        assert_eq!(tasks.len(), 1);
        assert_eq!(tasks[0].id, task_id);

        // Remove task
        assert!(storage.remove_task(&task_id).unwrap());
        assert!(!storage.remove_task(&task_id).unwrap());
        assert_eq!(storage.task_count(), 0);
    }

    #[test]
    fn test_update_task() {
        let storage = InMemoryTaskStorage::new();

        // Create and store task
        let mut task = Task::new("test-context".to_string());
        let task_id = task.id.clone();
        storage.store_task(task.clone()).unwrap();

        // Update task state
        task.update_status(TaskState::Working);
        storage.update_task(task.clone()).unwrap();

        // Verify update
        let retrieved = storage.get_task(&task_id).unwrap().unwrap();
        assert_eq!(retrieved.status.state, TaskState::Working);
    }

    #[test]
    fn test_update_nonexistent_task() {
        let storage = InMemoryTaskStorage::new();
        let task = Task::new("test-context".to_string());

        // Try to update non-existent task
        let result = storage.update_task(task);
        assert!(result.is_err());
    }

    #[test]
    fn test_get_nonexistent_task() {
        let storage = InMemoryTaskStorage::new();
        let result = storage.get_task("non-existent").unwrap();
        assert!(result.is_none());
    }

    // NEW COMPREHENSIVE TESTS FOR 100% COVERAGE

    #[test]
    fn test_default_constructor() {
        let storage = InMemoryTaskStorage::default();
        assert_eq!(storage.task_count(), 0);
        assert_eq!(storage.context_count(), 0);

        // Ensure default and new() produce equivalent results
        let storage_new = InMemoryTaskStorage::new();
        assert_eq!(storage.task_count(), storage_new.task_count());
        assert_eq!(storage.context_count(), storage_new.context_count());
    }

    #[test]
    fn test_clear_functionality() {
        let storage = InMemoryTaskStorage::new();

        // Add multiple tasks
        let task1 = Task::new("context1".to_string());
        let task2 = Task::new("context2".to_string());
        let task3 = Task::new("context3".to_string());

        storage.store_task(task1).unwrap();
        storage.store_task(task2).unwrap();
        storage.store_task(task3).unwrap();

        assert_eq!(storage.task_count(), 3);
        assert_eq!(storage.context_count(), 3); // Contexts auto-created

        // Clear all tasks
        storage.clear();
        assert_eq!(storage.task_count(), 0);
        assert_eq!(storage.context_count(), 0);

        // Verify all tasks are gone
        let tasks = storage.list_tasks().unwrap();
        assert!(tasks.is_empty());
    }

    #[test]
    fn test_comprehensive_storage_workflow() {
        let storage = InMemoryTaskStorage::new();

        // Create multiple tasks
        let mut task1 = Task::new("workflow-test-1".to_string());
        let task2 = Task::new("workflow-test-2".to_string());
        let task1_id = task1.id.clone();
        let task2_id = task2.id.clone();

        // Store tasks
        storage.store_task(task1.clone()).unwrap();
        storage.store_task(task2.clone()).unwrap();
        assert_eq!(storage.task_count(), 2);
        assert_eq!(storage.context_count(), 2); // Two different contexts

        // Update first task
        task1.update_status(TaskState::Working);
        storage.update_task(task1.clone()).unwrap();

        // Verify updated task
        let retrieved_task1 = storage.get_task(&task1_id).unwrap().unwrap();
        assert_eq!(retrieved_task1.status.state, TaskState::Working);

        // List all tasks
        let all_tasks = storage.list_tasks().unwrap();
        assert_eq!(all_tasks.len(), 2);

        // Check task existence
        assert!(storage.task_exists(&task1_id).unwrap());
        assert!(storage.task_exists(&task2_id).unwrap());
        assert!(!storage.task_exists("non-existent-id").unwrap());

        // Remove one task
        assert!(storage.remove_task(&task1_id).unwrap());
        assert!(!storage.task_exists(&task1_id).unwrap());
        assert_eq!(storage.task_count(), 1);

        // Try to remove the same task again
        assert!(!storage.remove_task(&task1_id).unwrap());

        // Clear remaining tasks
        storage.clear();
        assert_eq!(storage.task_count(), 0);
        assert_eq!(storage.context_count(), 0); // Contexts also cleared
        assert!(!storage.task_exists(&task2_id).unwrap());
    }

    #[test]
    fn test_concurrent_storage_operations() {
        use std::sync::Arc;
        use std::thread;

        let storage = Arc::new(InMemoryTaskStorage::new());
        let mut handles = vec![];

        // Spawn multiple threads to test concurrent access
        for i in 0..5 {
            let storage_clone = Arc::clone(&storage);
            let handle = thread::spawn(move || {
                let task = Task::new(format!("concurrent-task-{}", i));
                let task_id = task.id.clone();

                // Store task
                storage_clone.store_task(task).unwrap();

                // Verify task exists
                assert!(storage_clone.task_exists(&task_id).unwrap());

                // Retrieve task
                let retrieved = storage_clone.get_task(&task_id).unwrap();
                assert!(retrieved.is_some());

                task_id
            });
            handles.push(handle);
        }

        // Wait for all threads to complete and collect task IDs
        let task_ids: Vec<String> = handles.into_iter().map(|h| h.join().unwrap()).collect();

        // Verify all tasks are stored
        assert_eq!(storage.task_count(), 5);

        // List all tasks
        let all_tasks = storage.list_tasks().unwrap();
        assert_eq!(all_tasks.len(), 5);

        // Verify each task ID is present
        for task_id in task_ids {
            assert!(storage.task_exists(&task_id).unwrap());
        }
    }

    #[test]
    fn test_edge_cases_and_error_conditions() {
        let storage = InMemoryTaskStorage::new();

        // Test updating non-existent task with detailed error message
        let non_existent_task = Task::new("non-existent-context".to_string());
        let update_result = storage.update_task(non_existent_task.clone());
        assert!(update_result.is_err());

        // Verify error message contains task ID
        let error = update_result.unwrap_err();
        let error_message = format!("{}", error);
        assert!(error_message.contains(&non_existent_task.id));

        // Test removing non-existent task returns false
        assert!(!storage.remove_task("definitely-not-there").unwrap());

        // Test empty storage operations
        assert_eq!(storage.task_count(), 0);
        assert_eq!(storage.context_count(), 0);
        assert!(storage.list_tasks().unwrap().is_empty());
        assert!(!storage.task_exists("any-id").unwrap());

        // Test storage after clear on empty storage
        storage.clear();
        assert_eq!(storage.task_count(), 0);
        assert_eq!(storage.context_count(), 0);
    }

    #[test]
    fn test_task_replacement_behavior() {
        let storage = InMemoryTaskStorage::new();

        // Create original task
        let mut original_task = Task::new("replacement-test".to_string());
        let task_id = original_task.id.clone();

        // Store original task
        storage.store_task(original_task.clone()).unwrap();
        assert_eq!(storage.task_count(), 1);
        assert_eq!(storage.context_count(), 1); // Context should be created

        // Update task state and re-store (replacement)
        original_task.update_status(TaskState::Completed);
        storage.store_task(original_task.clone()).unwrap();

        // Verify only one task exists (replacement, not addition)
        assert_eq!(storage.task_count(), 1);
        assert_eq!(storage.context_count(), 1); // Context count unchanged

        // Verify task has updated state
        let retrieved = storage.get_task(&task_id).unwrap().unwrap();
        assert_eq!(retrieved.status.state, TaskState::Completed);
    }

    // ============= NEW A2A PROTOCOL COMPLIANCE TESTS =============

    #[test]
    fn test_context_based_operations() {
        let storage = InMemoryTaskStorage::new();

        // Create tasks in different contexts
        let task1_ctx1 = Task::new("context-1".to_string());
        let task2_ctx1 = Task::new("context-1".to_string());
        let task1_ctx2 = Task::new("context-2".to_string());

        let _task1_ctx1_id = task1_ctx1.id.clone();
        let task2_ctx1_id = task2_ctx1.id.clone();
        let _task1_ctx2_id = task1_ctx2.id.clone();

        // Store tasks
        storage.store_task(task1_ctx1).unwrap();
        storage.store_task(task2_ctx1).unwrap();
        storage.store_task(task1_ctx2).unwrap();

        assert_eq!(storage.task_count(), 3);
        assert_eq!(storage.context_count(), 2);

        // Test get_tasks_by_context
        let ctx1_tasks = storage.get_tasks_by_context("context-1").unwrap();
        assert_eq!(ctx1_tasks.len(), 2);

        let ctx2_tasks = storage.get_tasks_by_context("context-2").unwrap();
        assert_eq!(ctx2_tasks.len(), 1);

        // Test get_latest_task_in_context
        let latest_ctx1 = storage.get_latest_task_in_context("context-1").unwrap();
        assert!(latest_ctx1.is_some());
        // Should be task2_ctx1 since it was created later
        assert_eq!(latest_ctx1.unwrap().id, task2_ctx1_id);

        // Test list_contexts
        let contexts = storage.list_contexts().unwrap();
        assert_eq!(contexts.len(), 2);

        // Verify context activity tracking
        let ctx1_context = contexts
            .iter()
            .find(|c| c.context_id == "context-1")
            .unwrap();
        assert_eq!(ctx1_context.task_count, 2);

        let ctx2_context = contexts
            .iter()
            .find(|c| c.context_id == "context-2")
            .unwrap();
        assert_eq!(ctx2_context.task_count, 1);
    }

    #[test]
    fn test_conversation_history_aggregation() {
        use crate::data::message::{Message, MessageRole, Part};

        let storage = InMemoryTaskStorage::new();

        // Create tasks with message history
        let mut task1 = Task::new("conversation-test".to_string());
        let mut task2 = Task::new("conversation-test".to_string());

        // Add messages to task1 history using with_id() to preserve specific IDs
        let msg1 = Message::with_id(
            "msg-1".to_string(),
            MessageRole::User,
            vec![Part::text("Hello")],
        );
        let msg2 = Message::with_id(
            "msg-2".to_string(),
            MessageRole::Agent,
            vec![Part::text("Hi there!")],
        );
        task1.add_to_history(msg1);
        task1.add_to_history(msg2);

        // Add messages to task2 history
        let msg3 = Message::with_id(
            "msg-3".to_string(),
            MessageRole::User,
            vec![Part::text("How are you?")],
        );
        let msg4 = Message::with_id(
            "msg-4".to_string(),
            MessageRole::Agent,
            vec![Part::text("I'm doing well!")],
        );
        task2.add_to_history(msg3);
        task2.add_to_history(msg4);

        // Store tasks
        storage.store_task(task1).unwrap();
        storage.store_task(task2).unwrap();

        // Test conversation history aggregation
        let history = storage.get_context_history("conversation-test").unwrap();
        assert_eq!(history.len(), 4);

        // Verify message order is maintained
        assert_eq!(history[0].message_id, "msg-1");
        assert_eq!(history[1].message_id, "msg-2");
        assert_eq!(history[2].message_id, "msg-3");
        assert_eq!(history[3].message_id, "msg-4");
    }

    #[test]
    fn test_context_lifecycle_management() {
        let storage = InMemoryTaskStorage::new();

        // Test get_or_create_context with new context
        let context1 = storage.get_or_create_context("new-context").unwrap();
        assert_eq!(context1.context_id, "new-context");
        assert_eq!(context1.task_count, 0);
        assert_eq!(storage.context_count(), 1);

        // Test get_or_create_context with existing context
        let context1_again = storage.get_or_create_context("new-context").unwrap();
        assert_eq!(context1_again.context_id, "new-context");
        assert_eq!(storage.context_count(), 1); // Should not create duplicate

        // Add a task to update context activity
        let task = Task::new("new-context".to_string());
        storage.store_task(task).unwrap();

        // Verify context was updated
        let updated_context = storage.get_or_create_context("new-context").unwrap();
        assert_eq!(updated_context.task_count, 1);

        // Test update_context_activity directly
        storage.update_context_activity("new-context").unwrap();
        let context_after_update = storage.get_or_create_context("new-context").unwrap();
        assert_eq!(context_after_update.task_count, 2);

        // Test update_context_activity with non-existent context
        let result = storage.update_context_activity("non-existent");
        assert!(result.is_err());
    }

    #[test]
    fn test_empty_context_operations() {
        let storage = InMemoryTaskStorage::new();

        // Test operations on empty storage
        let tasks = storage.get_tasks_by_context("empty-context").unwrap();
        assert!(tasks.is_empty());

        let latest = storage.get_latest_task_in_context("empty-context").unwrap();
        assert!(latest.is_none());

        let history = storage.get_context_history("empty-context").unwrap();
        assert!(history.is_empty());

        let contexts = storage.list_contexts().unwrap();
        assert!(contexts.is_empty());
    }

    #[test]
    fn test_a2a_protocol_conversation_flow() {
        // This test simulates the A2A protocol example from the specification
        let storage = InMemoryTaskStorage::new();

        // First interaction: Generate sailboat image
        let mut task1 = Task::new("ctx-conversation-abc".to_string());
        task1.id = "task-boat-gen-123".to_string();
        task1.update_status(TaskState::Completed);

        // Second interaction: Color the boat red (same context, new task)
        let mut task2 = Task::new("ctx-conversation-abc".to_string());
        task2.id = "task-boat-color-456".to_string();
        task2.update_status(TaskState::Completed);

        // Store both tasks
        storage.store_task(task1).unwrap();
        storage.store_task(task2).unwrap();

        // Verify A2A protocol compliance
        assert_eq!(storage.task_count(), 2);
        assert_eq!(storage.context_count(), 1);

        // Verify context contains both tasks
        let context_tasks = storage
            .get_tasks_by_context("ctx-conversation-abc")
            .unwrap();
        assert_eq!(context_tasks.len(), 2);

        // Verify latest task is the color modification
        let latest = storage
            .get_latest_task_in_context("ctx-conversation-abc")
            .unwrap();
        assert_eq!(latest.unwrap().id, "task-boat-color-456");

        // Verify context metadata
        let context = storage
            .get_or_create_context("ctx-conversation-abc")
            .unwrap();
        assert_eq!(context.task_count, 2);
        assert_eq!(context.context_id, "ctx-conversation-abc");
    }
}