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
//! Event tracking for MapReduce execution
use anyhow::Result;
use chrono::{DateTime, Duration, Utc};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use super::agent::types::CleanupStatus;
/// Failure reasons for agent execution
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum FailureReason {
/// Agent execution timed out
Timeout,
/// Command failed with exit code
CommandFailed { exit_code: i32 },
/// Commit validation failed - required commit was not created
CommitValidationFailed { command: String },
/// Merge conflict or merge failure
MergeConflict,
/// Worktree creation or management error
WorktreeError,
/// Unknown or unclassified failure
Unknown,
}
/// Event logger for MapReduce job tracking
pub struct EventLogger {
_project_root: PathBuf,
_job_id: String,
_session_id: Option<String>,
verbosity: u8,
}
impl EventLogger {
/// Create a new event logger
pub fn new(
project_root: PathBuf,
job_id: String,
session_id: Option<String>,
verbosity: u8,
) -> Self {
Self {
_project_root: project_root,
_job_id: job_id,
_session_id: session_id,
verbosity,
}
}
/// Log an event with verbosity control
pub async fn log_event(&self, event: MapReduceEvent) -> Result<()> {
// Always log errors regardless of verbosity
if matches!(event, MapReduceEvent::AgentFailed { .. }) {
tracing::info!("MapReduce event: {:?}", event);
return Ok(());
}
// Only log verbose events in verbose mode (verbosity >= 1)
if self.verbosity >= 1 {
match &event {
MapReduceEvent::AgentCompleted {
agent_id,
item_id,
duration,
cleanup_status,
commits,
json_log_location,
..
} => {
// Truncate commit list if it's too long (unless verbosity >= 2)
let commits_display = if commits.len() > 10 && self.verbosity < 2 {
format!(
"[{:?}, {:?}, ... ({} more)]",
commits.first().unwrap_or(&String::new()),
commits.get(1).unwrap_or(&String::new()),
commits.len() - 2
)
} else {
format!("{:?}", commits)
};
tracing::info!(
"MapReduce event: AgentCompleted {{ agent_id: {:?}, item_id: {:?}, duration: {:?}, cleanup_status: {:?}, commits: {}, json_log_location: {:?} }}",
agent_id,
item_id,
duration,
cleanup_status,
commits_display,
json_log_location
);
}
_ => {
tracing::info!("MapReduce event: {:?}", event);
}
}
} else {
// In default mode, only log phase-level events
match event {
MapReduceEvent::MapPhaseStarted { .. }
| MapReduceEvent::MapPhaseCompleted { .. }
| MapReduceEvent::ReducePhaseStarted { .. }
| MapReduceEvent::ReducePhaseCompleted { .. } => {
tracing::info!("MapReduce event: {:?}", event);
}
// Suppress AgentStarted, AgentCompleted, and AgentFailed in default mode
// (AgentFailed is already handled above and always shown)
MapReduceEvent::AgentStarted { .. }
| MapReduceEvent::AgentCompleted { .. }
| MapReduceEvent::AgentFailed { .. } => {}
}
}
Ok(())
}
}
/// MapReduce execution events
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MapReduceEvent {
/// Map phase started
MapPhaseStarted {
total_items: usize,
timestamp: DateTime<Utc>,
},
/// Map phase completed
MapPhaseCompleted {
successful: usize,
failed: usize,
timestamp: DateTime<Utc>,
},
/// Agent started processing an item
AgentStarted {
agent_id: String,
item_id: String,
timestamp: DateTime<Utc>,
},
/// Agent completed processing
AgentCompleted {
agent_id: String,
item_id: String,
duration: Duration,
timestamp: DateTime<Utc>,
cleanup_status: Option<CleanupStatus>,
commits: Vec<String>,
json_log_location: Option<String>,
},
/// Agent failed processing
AgentFailed {
agent_id: String,
item_id: String,
error: String,
timestamp: DateTime<Utc>,
failure_reason: FailureReason,
json_log_location: Option<String>,
},
/// Reduce phase started
ReducePhaseStarted { timestamp: DateTime<Utc> },
/// Reduce phase completed
ReducePhaseCompleted { timestamp: DateTime<Utc> },
}
impl MapReduceEvent {
/// Create map phase started event
pub fn map_phase_started(total_items: usize) -> Self {
Self::MapPhaseStarted {
total_items,
timestamp: Utc::now(),
}
}
/// Create map phase completed event
pub fn map_phase_completed(successful: usize, failed: usize) -> Self {
Self::MapPhaseCompleted {
successful,
failed,
timestamp: Utc::now(),
}
}
/// Create agent started event
pub fn agent_started(agent_id: String, item_id: String) -> Self {
Self::AgentStarted {
agent_id,
item_id,
timestamp: Utc::now(),
}
}
/// Create agent completed event
pub fn agent_completed(
agent_id: String,
item_id: String,
duration: Duration,
cleanup_status: Option<CleanupStatus>,
commits: Vec<String>,
json_log_location: Option<String>,
) -> Self {
Self::AgentCompleted {
agent_id,
item_id,
duration,
timestamp: Utc::now(),
cleanup_status,
commits,
json_log_location,
}
}
/// Create agent failed event
pub fn agent_failed(
agent_id: String,
item_id: String,
error: String,
failure_reason: FailureReason,
json_log_location: Option<String>,
) -> Self {
Self::AgentFailed {
agent_id,
item_id,
error,
timestamp: Utc::now(),
failure_reason,
json_log_location,
}
}
/// Create reduce phase started event
pub fn reduce_phase_started() -> Self {
Self::ReducePhaseStarted {
timestamp: Utc::now(),
}
}
/// Create reduce phase completed event
pub fn reduce_phase_completed() -> Self {
Self::ReducePhaseCompleted {
timestamp: Utc::now(),
}
}
}