ccswarm 0.4.0

AI-powered multi-agent orchestration system with proactive intelligence, security monitoring, and session management
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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
/// Session-persistent Claude Code agent implementation for token efficiency
use anyhow::{Context, Result};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::{Duration, Instant};
use tokio::process::Command;
use tokio::sync::Mutex;
use tracing::{info, warn};
use uuid::Uuid;

use crate::agent::orchestrator::task_plan::{ParallelTask, ParallelTaskResult};
use crate::agent::orchestrator::{AgentOrchestrator, StepResult, StepType, TaskPlan, TaskStep};
use crate::agent::{AgentStatus, Task, TaskResult, TaskType};
use crate::config::ClaudeConfig;
use crate::identity::{AgentIdentity, IdentityMonitor, IdentityStatus};

/// Message in conversation history
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConversationMessage {
    pub timestamp: DateTime<Utc>,
    pub message_type: MessageType,
    pub content: String,
    pub task_id: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MessageType {
    IdentityEstablishment,
    TaskPrompt,
    Response,
    IdentityReminder,
    BatchStart,
    BatchEnd,
}

/// Claude Code session with state management
#[derive(Debug)]
pub struct ClaudeCodeSession {
    /// Session ID for tracking
    session_id: String,

    /// Working directory for Claude Code
    working_dir: PathBuf,

    /// Environment variables for the session
    env_vars: HashMap<String, String>,

    /// Claude configuration
    claude_config: ClaudeConfig,

    last_activity: Instant,

    /// Whether session is currently active
    is_active: bool,
}

impl ClaudeCodeSession {
    pub fn new(
        session_id: String,
        working_dir: PathBuf,
        env_vars: HashMap<String, String>,
        claude_config: ClaudeConfig,
    ) -> Self {
        Self {
            session_id,
            working_dir,
            env_vars,
            claude_config,
            last_activity: Instant::now(),
            is_active: true,
        }
    }

    /// Send a message to Claude Code and get response
    pub async fn send_message(&mut self, prompt: &str) -> Result<String> {
        self.last_activity = Instant::now();

        // For tests and simulation mode, return a mock response
        if !self.claude_config.use_real_api {
            return Ok(self.generate_mock_response(prompt));
        }

        let mut cmd = Command::new("claude");
        cmd.current_dir(&self.working_dir);

        // Add environment variables
        for (key, value) in &self.env_vars {
            cmd.env(key, value);
        }

        // Add Claude arguments
        cmd.arg("-p").arg(prompt);

        // Add output format
        cmd.arg("--output-format")
            .arg(self.claude_config.output_format.as_cli_arg());

        if self.claude_config.dangerous_skip {
            cmd.arg("--dangerously-skip-permissions");
        }

        // Add model
        cmd.arg("--model").arg(&self.claude_config.model);

        // Add append system prompt if specified
        if let Some(system_prompt) = &self.claude_config.append_system_prompt {
            cmd.arg("--append-system-prompt").arg(system_prompt);
        }

        // Add max turns if specified
        if let Some(max_turns) = self.claude_config.max_turns {
            cmd.arg("--max-turns").arg(max_turns.to_string());
        }

        let output = cmd
            .output()
            .await
            .context("Failed to execute Claude Code")?;

        if output.status.success() {
            Ok(String::from_utf8_lossy(&output.stdout).to_string())
        } else {
            Err(anyhow::anyhow!(
                "Claude Code execution failed: {}",
                String::from_utf8_lossy(&output.stderr)
            ))
        }
    }

    /// Generate a mock response for testing and simulation
    fn generate_mock_response(&self, prompt: &str) -> String {
        // Check if this is an identity establishment prompt
        if prompt.contains("🤖 **AGENT IDENTITY**") {
            return format!(
                "🤖 AGENT: {}\n📁 WORKSPACE: {}\n🎯 SCOPE: Identity established successfully",
                self.session_id,
                self.working_dir
                    .file_name()
                    .unwrap_or_default()
                    .to_string_lossy()
            );
        }

        // Generate a simple mock response for tasks
        if prompt.contains("## Current Task:") {
            return format!(
                "🤖 AGENT: {}\n📁 WORKSPACE: {}\n🎯 SCOPE: Task within agent boundaries\n\nTask completed successfully in simulation mode.",
                self.session_id,
                self.working_dir
                    .file_name()
                    .unwrap_or_default()
                    .to_string_lossy()
            );
        }

        // Default mock response
        format!(
            "🤖 AGENT: {}\n📁 WORKSPACE: {}\n🎯 SCOPE: General response\n\nMock response generated for testing.",
            self.session_id,
            self.working_dir
                .file_name()
                .unwrap_or_default()
                .to_string_lossy()
        )
    }

    /// Execute task with conversation context
    pub async fn execute_with_context(
        &mut self,
        task: &Task,
        history: &VecDeque<ConversationMessage>,
    ) -> Result<String> {
        // Build context-aware prompt
        let prompt = self.build_contextual_prompt(task, history);
        self.send_message(&prompt).await
    }

    /// Build prompt with conversation context
    fn build_contextual_prompt(
        &self,
        task: &Task,
        history: &VecDeque<ConversationMessage>,
    ) -> String {
        let mut prompt = String::new();

        // Add recent relevant context (last 3 messages)
        let recent_context: Vec<_> = history
            .iter()
            .rev()
            .take(3)
            .filter(|msg| {
                matches!(
                    msg.message_type,
                    MessageType::TaskPrompt | MessageType::Response
                )
            })
            .collect();

        if !recent_context.is_empty() {
            prompt.push_str("## Recent Context:\n");
            for msg in recent_context.iter().rev() {
                match msg.message_type {
                    MessageType::TaskPrompt => {
                        prompt.push_str(&format!(
                            "Previous Task: {}\n",
                            msg.content.lines().next().unwrap_or("")
                        ));
                    }
                    MessageType::Response => {
                        let summary = msg
                            .content
                            .lines()
                            .next()
                            .unwrap_or("")
                            .chars()
                            .take(100)
                            .collect::<String>();
                        prompt.push_str(&format!("Previous Result: {}...\n", summary));
                    }
                    _ => {}
                }
            }
            prompt.push('\n');
        }

        // Add current task
        prompt.push_str(&format!(
            "## Current Task:\n**{}**\n\n{}\n\nPriority: {:?}\nType: {:?}\n",
            task.description,
            task.details.as_deref().unwrap_or(""),
            task.priority,
            task.task_type
        ));

        prompt
    }

    pub fn session_id(&self) -> &str {
        &self.session_id
    }

    pub fn is_active(&self) -> bool {
        self.is_active
    }

    pub fn last_activity(&self) -> Instant {
        self.last_activity
    }

    pub fn shutdown(&mut self) {
        self.is_active = false;
    }
}

/// Session-persistent Claude Code agent for token efficiency
#[derive(Debug)]
pub struct PersistentClaudeAgent {
    /// Agent identity
    pub identity: AgentIdentity,

    /// Claude Code session
    session: Arc<Mutex<ClaudeCodeSession>>,

    /// Whether identity has been established
    identity_established: Arc<AtomicBool>,

    /// Conversation history with size limit
    conversation_history: Arc<Mutex<VecDeque<ConversationMessage>>>,

    /// Maximum history size (to prevent unbounded growth)
    max_history_size: usize,

    /// Current agent status
    pub status: AgentStatus,

    /// Current task being worked on
    pub current_task: Option<Task>,

    /// Task history
    pub task_history: Vec<(Task, TaskResult)>,

    pub last_activity: DateTime<Utc>,

    /// Session creation timestamp
    pub session_created_at: DateTime<Utc>,
}

impl PersistentClaudeAgent {
    /// Create a new persistent agent
    pub async fn new(identity: AgentIdentity, claude_config: ClaudeConfig) -> Result<Self> {
        let session_id = Uuid::new_v4().to_string();
        let session = ClaudeCodeSession::new(
            session_id,
            identity.workspace_path.clone(),
            identity.env_vars.clone(),
            claude_config,
        );

        Ok(Self {
            identity,
            session: Arc::new(Mutex::new(session)),
            identity_established: Arc::new(AtomicBool::new(false)),
            conversation_history: Arc::new(Mutex::new(VecDeque::new())),
            max_history_size: 50, // Keep last 50 messages
            status: AgentStatus::Initializing,
            current_task: None,
            task_history: Vec::new(),
            last_activity: Utc::now(),
            session_created_at: Utc::now(),
        })
    }

    /// Establish identity once per session lifecycle
    pub async fn establish_identity_once(&self) -> Result<()> {
        if self.identity_established.load(Ordering::Relaxed) {
            tracing::debug!(
                "Identity already established for agent: {}",
                self.identity.agent_id
            );
            return Ok(());
        }

        tracing::info!(
            "Establishing identity for agent: {}",
            self.identity.agent_id
        );

        // Generate lightweight identity prompt (200 tokens vs 2000+ in original)
        let compact_prompt = self.generate_compact_identity_prompt();

        // Send identity establishment message
        let mut session = self.session.lock().await;
        let response = session.send_message(&compact_prompt).await?;

        // Verify response contains identity markers
        if !self.verify_identity_response(&response) {
            return Err(anyhow::anyhow!("Failed to establish agent identity"));
        }

        // Record in conversation history
        let mut history = self.conversation_history.lock().await;
        history.push_back(ConversationMessage {
            timestamp: Utc::now(),
            message_type: MessageType::IdentityEstablishment,
            content: compact_prompt,
            task_id: None,
        });
        history.push_back(ConversationMessage {
            timestamp: Utc::now(),
            message_type: MessageType::Response,
            content: response,
            task_id: None,
        });

        // Trim history if needed
        self.trim_history(&mut history);

        self.identity_established.store(true, Ordering::Relaxed);
        tracing::info!(
            "Identity established successfully for agent: {}",
            self.identity.agent_id
        );

        Ok(())
    }

    /// Generate compact identity prompt (much smaller than full CLAUDE.md)
    fn generate_compact_identity_prompt(&self) -> String {
        format!(
            r#"🤖 **AGENT IDENTITY**: {}
📁 **WORKSPACE**: {}
🎯 **SPECIALIZATION**: {}
🚫 **BOUNDARIES**: Stay within {} scope only
✅ **RESPONSE FORMAT**: Always include identity header in responses:
   🤖 AGENT: {}
   📁 WORKSPACE: {}
   🎯 SCOPE: [Assessment of task fit]

You are a specialized {} agent. Maintain strict boundaries and provide concise, focused responses."#,
            self.identity.agent_id,
            self.identity
                .workspace_path
                .file_name()
                .unwrap_or_default()
                .to_string_lossy(),
            self.identity.specialization.name(),
            self.identity.specialization.name(),
            self.identity.specialization.name(),
            self.identity
                .workspace_path
                .file_name()
                .unwrap_or_default()
                .to_string_lossy(),
            self.identity.specialization.name(),
        )
    }

    /// Verify identity response contains required markers
    fn verify_identity_response(&self, response: &str) -> bool {
        response.contains("🤖 AGENT:") && response.contains(self.identity.specialization.name())
    }

    /// Execute a single task with session persistence
    pub async fn execute_task(&mut self, task: Task) -> Result<TaskResult> {
        self.status = AgentStatus::Working;
        self.current_task = Some(task.clone());
        self.last_activity = Utc::now();

        // Ensure identity is established
        self.establish_identity_once().await?;

        let start_time = Instant::now();
        let mut identity_monitor = IdentityMonitor::new(&self.identity.agent_id);

        // Execute task with conversation context
        let history = self.conversation_history.lock().await;
        let history_clone = history.clone();
        drop(history); // Release lock

        let mut session = self.session.lock().await;
        let response = session.execute_with_context(&task, &history_clone).await?;
        drop(session); // Release lock

        // Monitor identity in response
        let identity_status = identity_monitor.monitor_response(&response).await?;
        match identity_status {
            IdentityStatus::Healthy => {
                tracing::debug!("Identity maintained during task execution");
            }
            IdentityStatus::DriftDetected(msg) => {
                tracing::warn!("Identity drift detected: {}", msg);
                self.send_identity_reminder().await?;
            }
            IdentityStatus::BoundaryViolation(msg) => {
                return Err(anyhow::anyhow!("Boundary violation detected: {}", msg));
            }
            IdentityStatus::CriticalFailure(msg) => {
                return Err(anyhow::anyhow!("Critical identity failure: {}", msg));
            }
        }

        // Record conversation
        let mut history = self.conversation_history.lock().await;
        history.push_back(ConversationMessage {
            timestamp: Utc::now(),
            message_type: MessageType::TaskPrompt,
            content: format!("Task: {}", task.description),
            task_id: Some(task.id.clone()),
        });
        history.push_back(ConversationMessage {
            timestamp: Utc::now(),
            message_type: MessageType::Response,
            content: response.clone(),
            task_id: Some(task.id.clone()),
        });
        self.trim_history(&mut history);

        let result = TaskResult {
            success: true,
            output: serde_json::json!({
                "response": response,
                "agent": self.identity.agent_id,
                "task_id": task.id,
                "session_id": self.session.lock().await.session_id(),
            }),
            error: None,
            duration: start_time.elapsed(),
        };

        // Update status and history
        self.status = AgentStatus::WaitingForReview;
        self.current_task = None;
        self.task_history.push((task, result.clone()));

        Ok(result)
    }

    /// Execute multiple tasks in batch for maximum efficiency
    pub async fn execute_task_batch(&mut self, tasks: Vec<Task>) -> Result<Vec<TaskResult>> {
        if tasks.is_empty() {
            return Ok(Vec::new());
        }

        tracing::info!(
            "Executing batch of {} tasks for agent: {}",
            tasks.len(),
            self.identity.agent_id
        );

        // Ensure identity is established once for the entire batch
        self.establish_identity_once().await?;

        // Record batch start
        let mut history = self.conversation_history.lock().await;
        history.push_back(ConversationMessage {
            timestamp: Utc::now(),
            message_type: MessageType::BatchStart,
            content: format!("Starting batch of {} tasks", tasks.len()),
            task_id: None,
        });
        drop(history);

        let mut results = Vec::new();

        for (i, task) in tasks.into_iter().enumerate() {
            tracing::debug!(
                "Executing batch task {}/{} for agent: {}",
                i + 1,
                results.capacity(),
                self.identity.agent_id
            );

            // For batch processing, we can be more efficient by reusing context
            let result = self.execute_task(task).await?;
            results.push(result);
        }

        // Record batch end
        let mut history = self.conversation_history.lock().await;
        history.push_back(ConversationMessage {
            timestamp: Utc::now(),
            message_type: MessageType::BatchEnd,
            content: format!("Completed batch of {} tasks", results.len()),
            task_id: None,
        });
        self.trim_history(&mut history);

        tracing::info!(
            "Batch execution completed for agent: {}",
            self.identity.agent_id
        );
        Ok(results)
    }

    /// Send lightweight identity reminder instead of full re-establishment
    async fn send_identity_reminder(&self) -> Result<()> {
        let reminder_prompt = format!(
            "🤖 IDENTITY REMINDER: You are a {} agent working in {}. Maintain specialization boundaries.",
            self.identity.specialization.name(),
            self.identity
                .workspace_path
                .file_name()
                .unwrap_or_default()
                .to_string_lossy()
        );

        let mut session = self.session.lock().await;
        let _response = session.send_message(&reminder_prompt).await?;

        let mut history = self.conversation_history.lock().await;
        history.push_back(ConversationMessage {
            timestamp: Utc::now(),
            message_type: MessageType::IdentityReminder,
            content: reminder_prompt,
            task_id: None,
        });
        self.trim_history(&mut history);

        Ok(())
    }

    /// Trim conversation history to prevent unbounded growth
    fn trim_history(&self, history: &mut VecDeque<ConversationMessage>) {
        while history.len() > self.max_history_size {
            history.pop_front();
        }
    }

    /// Get session statistics
    pub async fn get_session_stats(&self) -> SessionStats {
        let session = self.session.lock().await;
        let history = self.conversation_history.lock().await;

        SessionStats {
            session_id: session.session_id().to_string(),
            created_at: self.session_created_at,
            last_activity: DateTime::from_timestamp(
                session.last_activity().elapsed().as_secs() as i64,
                0,
            )
            .unwrap_or(Utc::now()),
            identity_established: self.identity_established.load(Ordering::Relaxed),
            conversation_messages: history.len(),
            tasks_completed: self.task_history.len(),
            is_active: session.is_active(),
        }
    }

    /// Shutdown the persistent session
    pub async fn shutdown(&mut self) -> Result<()> {
        tracing::info!("Shutting down persistent agent: {}", self.identity.agent_id);
        self.status = AgentStatus::ShuttingDown;

        let mut session = self.session.lock().await;
        session.shutdown();

        Ok(())
    }
}

/// Session statistics for monitoring
#[derive(Debug, Serialize, Deserialize)]
pub struct SessionStats {
    pub session_id: String,
    pub created_at: DateTime<Utc>,
    pub last_activity: DateTime<Utc>,
    pub identity_established: bool,
    pub conversation_messages: usize,
    pub tasks_completed: usize,
    pub is_active: bool,
}

#[async_trait]
impl AgentOrchestrator for PersistentClaudeAgent {
    async fn orchestrate_task(&self, task: &Task) -> Result<TaskResult> {
        info!(
            "🎭 Persistent agent orchestrating task: {}",
            task.description
        );

        // Leverage persistent session for complex task orchestration
        let plan = self.analyze_task(task).await?;

        // Execute plan with session persistence for context continuity
        let mut context = HashMap::new();
        context.insert("task_id".to_string(), task.id.clone());
        context.insert("persistent_mode".to_string(), "true".to_string());
        context.insert("session_id".to_string(), {
            let session = self.session.lock().await;
            session.session_id().to_string()
        });

        for step in &plan.steps {
            let step_result = self.execute_step(step, &context).await?;

            // Update context with step results and preserve in session history
            context.insert(
                format!("step_{}_result", step.id),
                serde_json::to_string(&step_result.outputs)?,
            );

            if !step_result.success {
                warn!("Persistent agent step failed: {}", step.name);
                return Ok(TaskResult::failure(
                    format!("Persistent orchestration failed at step: {}", step.name),
                    Duration::from_secs(0),
                ));
            }
        }

        Ok(TaskResult::success(
            serde_json::json!({
                "orchestration": "persistent_success",
                "steps_completed": plan.steps.len(),
                "session_id": context.get("session_id")
            }),
            Duration::from_secs(15),
        ))
    }

    async fn analyze_task(&self, task: &Task) -> Result<TaskPlan> {
        let mut plan = TaskPlan::new(task.id.clone());

        // Persistent session-aware orchestration strategies
        match task.task_type {
            TaskType::Development => {
                // Long-running development workflow with session continuity
                plan.add_step(
                    TaskStep::new(
                        "persistent_context_analysis".to_string(),
                        "Analyze task with session context".to_string(),
                        StepType::Analysis,
                    )
                    .with_description(
                        "Analyze task using persistent session context and history".to_string(),
                    ),
                );
                plan.add_step(
                    TaskStep::new(
                        "iterative_development".to_string(),
                        "Execute with session persistence".to_string(),
                        StepType::Execution,
                    )
                    .with_description(
                        "Execute development task with session context continuity".to_string(),
                    ),
                );
                plan.add_step(
                    TaskStep::new(
                        "session_context_validation".to_string(),
                        "Validate within session context".to_string(),
                        StepType::Validation,
                    )
                    .with_description(
                        "Validate results within persistent session context".to_string(),
                    ),
                );
            }
            TaskType::Testing => {
                // Test execution with preserved test state
                plan.add_step(
                    TaskStep::new(
                        "test_session_setup".to_string(),
                        "Setup test session context".to_string(),
                        StepType::Analysis,
                    )
                    .with_description("Setup testing context using session history".to_string()),
                );
                plan.add_step(
                    TaskStep::new(
                        "persistent_test_execution".to_string(),
                        "Execute tests with session state".to_string(),
                        StepType::Execution,
                    )
                    .with_description("Execute tests with persistent session state".to_string()),
                );
                plan.add_step(
                    TaskStep::new(
                        "test_result_persistence".to_string(),
                        "Persist test results in session".to_string(),
                        StepType::Validation,
                    )
                    .with_description("Store test results in persistent session".to_string()),
                );
            }
            TaskType::Bugfix => {
                // Bug investigation with conversation history
                plan.add_step(
                    TaskStep::new(
                        "bug_context_analysis".to_string(),
                        "Analyze bug with session history".to_string(),
                        StepType::Analysis,
                    )
                    .with_description("Analyze bug using conversation history".to_string()),
                );
                plan.add_step(
                    TaskStep::new(
                        "iterative_debugging".to_string(),
                        "Debug with persistent context".to_string(),
                        StepType::Execution,
                    )
                    .with_description("Debug using persistent session context".to_string()),
                );
                plan.add_step(
                    TaskStep::new(
                        "fix_verification".to_string(),
                        "Verify fix with session validation".to_string(),
                        StepType::Validation,
                    )
                    .with_description("Verify fix using session context".to_string()),
                );
            }
            _ => {
                // Default persistent workflow
                plan.add_step(
                    TaskStep::new(
                        "persistent_analysis".to_string(),
                        "Analyze with session context".to_string(),
                        StepType::Analysis,
                    )
                    .with_description("Analyze task with session context".to_string()),
                );
                plan.add_step(
                    TaskStep::new(
                        "session_execution".to_string(),
                        "Execute with preserved context".to_string(),
                        StepType::Execution,
                    )
                    .with_description("Execute with preserved session context".to_string()),
                );
                plan.add_step(
                    TaskStep::new(
                        "persistent_validation".to_string(),
                        "Validate with session history".to_string(),
                        StepType::Validation,
                    )
                    .with_description("Validate using session history".to_string()),
                );
            }
        }

        Ok(plan)
    }

    async fn execute_step(
        &self,
        step: &TaskStep,
        context: &HashMap<String, String>,
    ) -> Result<StepResult> {
        info!("🎭 Persistent agent executing step: {}", step.name);

        match step.name.as_str() {
            "Analyze task with session context" => {
                self.execute_context_analysis_step(step, context).await
            }
            "Execute with session persistence" => {
                self.execute_iterative_development_step(step, context).await
            }
            "Validate within session context" => {
                self.execute_session_validation_step(step, context).await
            }
            "Execute tests with session state" => {
                self.execute_persistent_test_step(step, context).await
            }
            "Debug with persistent context" => {
                self.execute_iterative_debugging_step(step, context).await
            }
            _ => {
                // Default execution with session persistence
                self.execute_persistent_default_step(step, context).await
            }
        }
    }

    async fn review_and_adapt(
        &self,
        results: &[StepResult],
        current_plan: &mut TaskPlan,
    ) -> Result<TaskPlan> {
        info!(
            "🎭 Persistent agent reviewing and adapting plan based on {} results",
            results.len()
        );

        // Leverage conversation history for intelligent adaptation
        let history = self.conversation_history.lock().await;
        let failed_steps: Vec<_> = results.iter().filter(|r| !r.success).collect();

        if !failed_steps.is_empty() {
            warn!(
                "Persistent agent detected {} failed steps",
                failed_steps.len()
            );

            // Analyze failure patterns from session history
            let failure_analysis = self.analyze_failure_patterns(&history, &failed_steps);

            // Add adaptive recovery steps based on session context
            if failure_analysis.contains("context_loss") {
                let recovery_step = TaskStep::new(
                    "context_recovery".to_string(),
                    "Recover session context".to_string(),
                    StepType::Execution,
                )
                .with_description("Recover lost session context".to_string());
                current_plan.steps.push(recovery_step);
            }

            if failure_analysis.contains("identity_drift") {
                let identity_step = TaskStep::new(
                    "identity_reinforcement".to_string(),
                    "Reinforce agent identity".to_string(),
                    StepType::Execution,
                )
                .with_description("Reinforce agent identity and boundaries".to_string());
                current_plan.steps.push(identity_step);
            }

            let validation_step = TaskStep::new(
                "adaptive_validation".to_string(),
                "Validate adaptive recovery".to_string(),
                StepType::Validation,
            )
            .with_description("Validate recovery steps".to_string());
            current_plan.steps.push(validation_step);
        }

        Ok(current_plan.clone())
    }

    async fn execute_parallel_task(
        &self,
        task: &ParallelTask,
        _context: &HashMap<String, String>,
    ) -> Result<ParallelTaskResult> {
        // Execute parallel task with session context
        let execution_task = Task::new(
            task.id.clone(),
            task.name.clone(),
            crate::agent::Priority::Medium,
            TaskType::Development,
        );

        // Use conversation history for context
        let mut session = self.session.lock().await;
        let history = self.conversation_history.lock().await;
        let response = session
            .execute_with_context(&execution_task, &history)
            .await?;
        drop(session);
        drop(history);

        let success = !response.contains("error") && !response.contains("failed");

        Ok(ParallelTaskResult {
            task_id: task.id.clone(),
            success,
            output: response,
            error: if success {
                None
            } else {
                Some("Task execution failed".to_string())
            },
        })
    }

    async fn synthesize_results(
        &self,
        task: &Task,
        results: Vec<StepResult>,
    ) -> Result<TaskResult> {
        let total_steps = results.len();
        let successful_steps = results.iter().filter(|r| r.is_success()).count();
        let overall_success = successful_steps == total_steps;

        let summary = if overall_success {
            format!(
                "Persistent agent orchestration completed successfully: {}/{} steps succeeded",
                successful_steps, total_steps
            )
        } else {
            format!(
                "Persistent agent orchestration partially failed: {}/{} steps succeeded",
                successful_steps, total_steps
            )
        };

        let session_id = {
            let session = self.session.lock().await;
            session.session_id().to_string()
        };

        let output = serde_json::json!({
            "orchestration_type": "persistent",
            "task_id": task.id,
            "session_id": session_id,
            "total_steps": total_steps,
            "successful_steps": successful_steps,
            "conversation_history_length": self.conversation_history.lock().await.len(),
            "step_results": results.iter().map(|r| {
                serde_json::json!({
                    "step_id": r.step_id,
                    "success": r.success,
                    "summary": r.summary
                })
            }).collect::<Vec<_>>()
        });

        if overall_success {
            Ok(TaskResult::success(
                output,
                Duration::from_millis(results.iter().map(|r| r.duration_ms).sum()),
            ))
        } else {
            Ok(TaskResult::failure(
                summary,
                Duration::from_millis(results.iter().map(|r| r.duration_ms).sum()),
            ))
        }
    }
}

impl PersistentClaudeAgent {
    /// Execute context analysis step leveraging session history
    async fn execute_context_analysis_step(
        &self,
        _step: &TaskStep,
        context: &HashMap<String, String>,
    ) -> Result<StepResult> {
        let task_id = context
            .get("task_id")
            .unwrap_or(&"unknown".to_string())
            .clone();
        let session_id = context
            .get("session_id")
            .unwrap_or(&"unknown".to_string())
            .clone();

        // Analyze conversation history for relevant context
        let history = self.conversation_history.lock().await;
        let relevant_messages = history
            .iter()
            .filter(|msg| {
                matches!(
                    msg.message_type,
                    MessageType::TaskPrompt | MessageType::Response
                )
            })
            .count();

        Ok(StepResult::new(_step.id.clone())
            .with_summary(format!("Context analysis for task {} completed", task_id))
            .add_output("task_id".to_string(), task_id)
            .add_output("session_id".to_string(), session_id)
            .add_output(
                "history_messages".to_string(),
                relevant_messages.to_string(),
            ))
    }

    /// Execute iterative development step with session persistence
    async fn execute_iterative_development_step(
        &self,
        step: &TaskStep,
        _context: &HashMap<String, String>,
    ) -> Result<StepResult> {
        let task = Task::new(
            format!("persistent_dev_{}", step.id),
            step.description.clone(),
            crate::agent::Priority::Medium,
            TaskType::Development,
        );

        // Use session context for development
        let mut session = self.session.lock().await;
        let history = self.conversation_history.lock().await;
        let response = session.execute_with_context(&task, &history).await?;
        drop(session);
        drop(history);

        Ok(StepResult::new(step.id.clone())
            .with_summary("Iterative development step completed with session context".to_string())
            .add_output("development_response".to_string(), response)
            .add_output("session_context_used".to_string(), "true".to_string()))
    }

    /// Execute session validation step
    async fn execute_session_validation_step(
        &self,
        _step: &TaskStep,
        context: &HashMap<String, String>,
    ) -> Result<StepResult> {
        let session_id = context
            .get("session_id")
            .unwrap_or(&"unknown".to_string())
            .clone();

        // Validate session state and identity
        let identity_established = self.identity_established.load(Ordering::Relaxed);
        let history = self.conversation_history.lock().await;
        let session_health = !history.is_empty() && identity_established;

        Ok(StepResult::new(_step.id.clone())
            .with_summary(format!("Session validation completed for {}", session_id))
            .add_output("session_id".to_string(), session_id)
            .add_output(
                "identity_established".to_string(),
                identity_established.to_string(),
            )
            .add_output("session_healthy".to_string(), session_health.to_string())
            .add_output("conversation_length".to_string(), history.len().to_string()))
    }

    /// Execute persistent test step
    async fn execute_persistent_test_step(
        &self,
        step: &TaskStep,
        _context: &HashMap<String, String>,
    ) -> Result<StepResult> {
        let task = Task::new(
            format!("persistent_test_{}", step.id),
            step.description.clone(),
            crate::agent::Priority::Medium,
            TaskType::Testing,
        );

        // Execute tests with session persistence
        let mut session = self.session.lock().await;
        let history = self.conversation_history.lock().await;
        let response = session.execute_with_context(&task, &history).await?;
        drop(session);
        drop(history);

        // Parse test results from response
        let test_success = response.contains("") || response.contains("passed");

        Ok(StepResult::new(step.id.clone())
            .with_summary("Persistent test execution completed".to_string())
            .add_output("test_response".to_string(), response)
            .add_output("test_passed".to_string(), test_success.to_string())
            .add_output("persistent_execution".to_string(), "true".to_string()))
    }

    /// Execute iterative debugging step
    async fn execute_iterative_debugging_step(
        &self,
        step: &TaskStep,
        _context: &HashMap<String, String>,
    ) -> Result<StepResult> {
        let task = Task::new(
            format!("persistent_debug_{}", step.id),
            step.description.clone(),
            crate::agent::Priority::High,
            TaskType::Bugfix,
        );

        // Use conversation history for debugging context
        let mut session = self.session.lock().await;
        let history = self.conversation_history.lock().await;
        let response = session.execute_with_context(&task, &history).await?;
        drop(session);
        drop(history);

        // Analyze debugging progress
        let debug_progress = response.contains("identified")
            || response.contains("fixed")
            || response.contains("resolved");

        Ok(StepResult::new(step.id.clone())
            .with_summary("Iterative debugging step completed".to_string())
            .add_output("debug_response".to_string(), response)
            .add_output("progress_made".to_string(), debug_progress.to_string())
            .add_output("context_leveraged".to_string(), "true".to_string()))
    }

    /// Execute default step with session persistence
    async fn execute_persistent_default_step(
        &self,
        step: &TaskStep,
        context: &HashMap<String, String>,
    ) -> Result<StepResult> {
        let task_id = context
            .get("task_id")
            .unwrap_or(&"unknown".to_string())
            .clone();

        let task = Task::new(
            format!("persistent_default_{}_{}", step.id, task_id),
            step.description.clone(),
            crate::agent::Priority::Medium,
            TaskType::Development,
        );

        // Execute with full session context
        let mut session = self.session.lock().await;
        let history = self.conversation_history.lock().await;
        let response = session.execute_with_context(&task, &history).await?;
        drop(session);
        drop(history);

        Ok(StepResult::new(step.id.clone())
            .with_summary("Persistent default step completed".to_string())
            .add_output("response".to_string(), response)
            .add_output("session_context_preserved".to_string(), "true".to_string())
            .add_output("task_id".to_string(), task_id))
    }

    /// Analyze failure patterns from conversation history
    fn analyze_failure_patterns(
        &self,
        history: &VecDeque<ConversationMessage>,
        failed_steps: &[&StepResult],
    ) -> String {
        let mut patterns = Vec::new();

        // Check for context loss patterns
        if failed_steps
            .iter()
            .any(|step| step.errors.iter().any(|e| e.contains("context")))
        {
            patterns.push("context_loss");
        }

        // Check for identity drift in recent messages
        let recent_responses: Vec<_> = history
            .iter()
            .rev()
            .take(5)
            .filter(|msg| matches!(msg.message_type, MessageType::Response))
            .collect();

        if recent_responses
            .iter()
            .any(|msg| !msg.content.contains("🤖 AGENT:"))
        {
            patterns.push("identity_drift");
        }

        patterns.join(",")
    }
}