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
//! UI events for user-facing display.
//!
//! `UIEvent` is separate from `PipelineEvent` to maintain reducer purity.
//! These events are emitted by effect handlers alongside `PipelineEvents`
//! and are displayed to users but do not affect pipeline state or checkpoints.
use super::event::PipelinePhase;
use serde::{Deserialize, Serialize};
/// Types of XML output for semantic rendering.
///
/// Each XML type has a dedicated renderer that transforms raw XML
/// into user-friendly terminal output.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum XmlOutputType {
/// Development result XML (status, summary, files changed).
DevelopmentResult,
/// Development plan XML (steps, critical files, risks).
DevelopmentPlan,
/// Review issues XML (list of issues or no-issues-found).
ReviewIssues,
/// Fix result XML (status, summary of fixes).
FixResult,
/// Commit message XML (subject, body).
CommitMessage,
}
/// Context for XML output events.
///
/// Provides additional context like iteration or pass number
/// for more informative rendering.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct XmlOutputContext {
/// Development iteration number (1-based).
pub iteration: Option<u32>,
/// Review pass number (1-based).
pub pass: Option<u32>,
/// Optional code snippets to enrich rendering (e.g., review issues).
///
/// This allows semantic renderers to show relevant code context even when the
/// issue description itself does not embed a fenced code block.
#[serde(default)]
pub snippets: Vec<XmlCodeSnippet>,
}
/// A code snippet associated with a file and line range.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct XmlCodeSnippet {
/// File path (workspace-relative).
pub file: String,
/// 1-based starting line number (inclusive).
pub line_start: u32,
/// 1-based ending line number (inclusive).
pub line_end: u32,
/// Snippet content (may include newlines).
pub content: String,
}
/// UI events for user-facing display during pipeline execution.
///
/// These events do NOT affect pipeline state or checkpoints.
/// They are purely for terminal display and programmatic observation.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum UIEvent {
/// Phase transition occurred.
PhaseTransition {
from: Option<PipelinePhase>,
to: PipelinePhase,
},
/// Development iteration progress.
IterationProgress { current: u32, total: u32 },
/// Review pass progress.
ReviewProgress { pass: u32, total: u32 },
/// Agent activity notification.
AgentActivity { agent: String, message: String },
/// Cloud-mode git push completed.
PushCompleted {
remote: String,
branch: String,
commit_sha: String,
},
/// Cloud-mode git push failed.
///
/// The error string MUST already be redacted (no credentials).
PushFailed {
remote: String,
branch: String,
error: String,
},
/// Cloud-mode pull request created.
PullRequestCreated { url: String, number: u32 },
/// Cloud-mode pull request creation failed.
///
/// The error string MUST already be redacted (no credentials).
PullRequestFailed { error: String },
/// XML output requiring semantic rendering.
///
/// Phase functions emit raw XML content through this event,
/// and the event loop renders it with appropriate semantic formatting.
XmlOutput {
/// The type of XML output (determines renderer).
xml_type: XmlOutputType,
/// The raw XML content to render.
content: String,
/// Optional context like iteration or pass number.
context: Option<XmlOutputContext>,
},
/// Prompt replay observability event (RFC-007 Short-term #3).
///
/// Emitted by handlers after each `get_stored_or_generate_prompt` call.
/// Allows audit, debugging, and detection of unexpected replay behavior.
///
/// This event does NOT affect pipeline state or checkpoints.
PromptReplayHit {
/// String representation of the `PromptScopeKey` used for history lookup.
key: String,
/// `true` if the prompt was found in checkpoint history (replayed),
/// `false` if freshly generated.
was_replayed: bool,
},
}
impl UIEvent {
/// Get emoji indicator for phase.
#[must_use]
pub const fn phase_emoji(phase: &PipelinePhase) -> &'static str {
match phase {
PipelinePhase::Planning => "📋",
PipelinePhase::Development => "🔨",
PipelinePhase::Review => "👀",
PipelinePhase::CommitMessage => "📝",
PipelinePhase::FinalValidation => "✅",
PipelinePhase::Finalizing => "🔄",
PipelinePhase::Complete => "🎉",
PipelinePhase::AwaitingDevFix => "🔧",
PipelinePhase::Interrupted => "⏸️",
}
}
/// Format event for terminal display.
///
/// This method delegates to the rendering module for actual formatting.
/// Prefer calling `rendering::render_ui_event()` directly in new code.
#[must_use]
pub fn format_for_display(&self) -> String {
crate::rendering::render_ui_event(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_phase_transition_display() {
let event = UIEvent::PhaseTransition {
from: Some(PipelinePhase::Planning),
to: PipelinePhase::Development,
};
let display = event.format_for_display();
assert!(display.contains("🔨"));
assert!(display.contains("Development"));
}
#[test]
fn test_iteration_progress_display() {
let event = UIEvent::IterationProgress {
current: 2,
total: 5,
};
let display = event.format_for_display();
assert!(display.contains("2/5"));
}
#[test]
fn test_review_progress_display() {
let event = UIEvent::ReviewProgress { pass: 1, total: 3 };
let display = event.format_for_display();
assert!(display.contains("1/3"));
assert!(display.contains("Review pass"));
}
#[test]
fn test_agent_activity_display() {
let event = UIEvent::AgentActivity {
agent: "claude".to_string(),
message: "Processing request".to_string(),
};
let display = event.format_for_display();
assert!(display.contains("[claude]"));
assert!(display.contains("Processing request"));
}
#[test]
fn test_ui_event_serialization() {
let event = UIEvent::PhaseTransition {
from: None,
to: PipelinePhase::Planning,
};
let json = serde_json::to_string(&event).unwrap();
let deserialized: UIEvent = serde_json::from_str(&json).unwrap();
assert_eq!(event, deserialized);
}
#[test]
fn test_phase_emoji_all_phases() {
// Exhaustive test ensures every phase has an emoji
assert_eq!(UIEvent::phase_emoji(&PipelinePhase::Planning), "📋");
assert_eq!(UIEvent::phase_emoji(&PipelinePhase::Development), "🔨");
assert_eq!(UIEvent::phase_emoji(&PipelinePhase::Review), "👀");
assert_eq!(UIEvent::phase_emoji(&PipelinePhase::CommitMessage), "📝");
assert_eq!(UIEvent::phase_emoji(&PipelinePhase::FinalValidation), "✅");
assert_eq!(UIEvent::phase_emoji(&PipelinePhase::Finalizing), "🔄");
assert_eq!(UIEvent::phase_emoji(&PipelinePhase::Complete), "🎉");
assert_eq!(UIEvent::phase_emoji(&PipelinePhase::AwaitingDevFix), "🔧");
assert_eq!(UIEvent::phase_emoji(&PipelinePhase::Interrupted), "⏸️");
}
#[test]
fn test_prompt_replay_hit_replayed_display() {
let event = UIEvent::PromptReplayHit {
key: "planning_1".to_string(),
was_replayed: true,
};
let display = event.format_for_display();
assert!(display.contains("planning_1"));
assert!(
display.contains("Replayed")
|| display.contains("replay")
|| display.contains("stored")
);
}
#[test]
fn test_prompt_replay_hit_fresh_display() {
let event = UIEvent::PromptReplayHit {
key: "development_2".to_string(),
was_replayed: false,
};
let display = event.format_for_display();
assert!(display.contains("development_2"));
assert!(
display.contains("fresh")
|| display.contains("Generated")
|| display.contains("prompt")
);
}
#[test]
fn test_prompt_replay_hit_serialization() {
let event = UIEvent::PromptReplayHit {
key: "commit_message_attempt_iter1_1".to_string(),
was_replayed: false,
};
let json = serde_json::to_string(&event).unwrap();
let deserialized: UIEvent = serde_json::from_str(&json).unwrap();
assert_eq!(event, deserialized);
}
#[test]
fn test_phase_transition_from_none() {
// Test initial phase transition with no previous phase
let event = UIEvent::PhaseTransition {
from: None,
to: PipelinePhase::Planning,
};
let display = event.format_for_display();
assert!(display.contains("📋"));
assert!(display.contains("Planning"));
}
// =========================================================================
// XmlOutput Tests
// =========================================================================
#[test]
fn test_xml_output_type_serialization() {
let xml_type = XmlOutputType::DevelopmentResult;
let json = serde_json::to_string(&xml_type).unwrap();
let deserialized: XmlOutputType = serde_json::from_str(&json).unwrap();
assert_eq!(xml_type, deserialized);
}
#[test]
fn test_xml_output_context_default() {
let context = XmlOutputContext::default();
assert!(context.iteration.is_none());
assert!(context.pass.is_none());
assert!(context.snippets.is_empty());
}
#[test]
fn test_xml_output_context_with_values() {
let context = XmlOutputContext {
iteration: Some(2),
pass: Some(1),
snippets: Vec::new(),
};
assert_eq!(context.iteration, Some(2));
assert_eq!(context.pass, Some(1));
}
#[test]
fn test_xml_output_event_serialization() {
let event = UIEvent::XmlOutput {
xml_type: XmlOutputType::ReviewIssues,
content: "<ralph-issues><ralph-issue>Test</ralph-issue></ralph-issues>".to_string(),
context: Some(XmlOutputContext {
iteration: None,
pass: Some(1),
snippets: Vec::new(),
}),
};
let json = serde_json::to_string(&event).unwrap();
let deserialized: UIEvent = serde_json::from_str(&json).unwrap();
assert_eq!(event, deserialized);
}
#[test]
fn test_xml_output_types_all_variants() {
// Ensure all variants are distinct
let variants = [
XmlOutputType::DevelopmentResult,
XmlOutputType::DevelopmentPlan,
XmlOutputType::ReviewIssues,
XmlOutputType::FixResult,
XmlOutputType::CommitMessage,
];
assert!(
variants.iter().enumerate().all(|(i, v1)| {
variants
.iter()
.enumerate()
.all(|(j, v2)| i == j || v1 != v2)
}),
"All XmlOutputType variants should be distinct"
);
}
}