ralph-workflow 0.7.18

PROMPT-driven multi-agent orchestrator for git repos
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
// Claude event formatting methods.
//
// Contains all the format_*_event methods for the ClaudeParser.

impl ClaudeParser {
    fn hash_string(text: &str) -> u64 {
        crate::json_parser::boundary::compute_hash_str(text)
    }

    /// Format a system event
    fn format_system_event(
        &self,
        subtype: Option<&String>,
        session_id: Option<String>,
        cwd: Option<String>,
    ) -> String {
        if subtype.map(std::string::String::as_str) == Some("init") {
            format_system_init_event(&self.display_name, self.colors, session_id, cwd)
        } else {
            let subtype_str = subtype.map_or("system", |s| s.as_str());
            let terminal_mode = *self.state.terminal_mode.borrow();
            format_system_other_event(&self.display_name, self.colors, subtype_str, terminal_mode)
        }
    }

}

fn format_system_init_event(
    prefix: &str,
    c: Colors,
    session_id: Option<String>,
    cwd: Option<String>,
) -> String {
    let sid = session_id.unwrap_or_else(|| "unknown".to_string());
    let base = format!(
        "{}[{}]{} {}Session started{} {}({:.8}...){}\n",
        c.dim(),
        prefix,
        c.reset(),
        c.cyan(),
        c.reset(),
        c.dim(),
        sid,
        c.reset()
    );
    if let Some(cwd) = cwd {
        let extra = format!(
            "{}[{}]{} {}Working dir: {}{}\n",
            c.dim(),
            prefix,
            c.reset(),
            c.dim(),
            cwd,
            c.reset()
        );
        format!("{base}{extra}")
    } else {
        base
    }
}

fn format_system_other_event(
    prefix: &str,
    c: Colors,
    subtype_str: &str,
    terminal_mode: TerminalMode,
) -> String {
    // In full TTY mode, streaming output uses an in-place update pattern which can leave
    // the cursor positioned on an active line. System events (like `status`) can arrive
    // at any time; clearing the line defensively avoids leaving remnants (e.g. "statusead").
    if terminal_mode == TerminalMode::Full {
        use crate::json_parser::delta_display::CLEAR_LINE;
        format!(
            "{}\r{}[{}]{} {}{}{}\n",
            CLEAR_LINE,
            c.dim(),
            prefix,
            c.reset(),
            c.cyan(),
            subtype_str,
            c.reset()
        )
    } else {
        format!(
            "{}[{}]{} {}{}{}\n",
            c.dim(),
            prefix,
            c.reset(),
            c.cyan(),
            subtype_str,
            c.reset()
        )
    }
}

impl ClaudeParser {
    /// Extract content from assistant message for hash-based deduplication.
    ///
    /// This includes both text and `tool_use` blocks, normalized for comparison.
    /// `Tool_use` blocks are serialized in a deterministic way (name + sorted input JSON)
    /// to ensure semantically identical tool calls produce the same hash.
    ///
    /// # Returns
    /// A tuple of (`normalized_content`, `tool_names_by_index`) where:
    /// - `normalized_content`: The concatenated normalized content (text + `tool_use` markers)
    /// - `tool_names_by_index`: Map from content block index to tool name (for `tool_use` blocks)
    fn extract_text_content_for_hash(
        message: Option<&crate::json_parser::types::AssistantMessage>,
    ) -> Option<(String, std::collections::HashMap<usize, String>)> {
        message?.content.as_ref().map(|content| {
            let (normalized_parts, tool_names): (
                Vec<String>,
                std::collections::HashMap<usize, String>,
            ) = content.iter().enumerate().fold(
                (Vec::new(), std::collections::HashMap::new()),
                |(mut parts, mut names), (index, block)| {
                    match block {
                        ContentBlock::Text { text } => {
                            if let Some(text) = text.as_deref() {
                                parts.push(text.to_string());
                            }
                        }
                        ContentBlock::ToolUse { name, input } => {
                            if let Some(name_str) = name.as_deref() {
                                names.insert(index, name_str.to_string());
                            }

                            let normalized = format!(
                                "TOOL_USE:{}:{}",
                                name.as_deref().unwrap_or(""),
                                input
                                    .as_ref()
                                    .map(|v| {
                                        if v.is_object() {
                                            serde_json::to_string(v).ok()
                                        } else if v.is_string() {
                                            v.as_str().map(std::string::ToString::to_string)
                                        } else {
                                            serde_json::to_string(v).ok()
                                        }
                                        .unwrap_or_default()
                                    })
                                    .unwrap_or_default()
                            );
                            parts.push(normalized);
                        }
                        _ => {}
                    }
                    (parts, names)
                },
            );

            (normalized_parts.join(""), tool_names)
        })
    }

    fn is_duplicate_by_message_id(
        message: Option<&crate::json_parser::types::AssistantMessage>,
        session: &StreamingSession,
    ) -> bool {
        let Some(ast_msg_id) = message.and_then(|m| m.id.as_ref()) else {
            return false;
        };
        session.is_duplicate_final_message(ast_msg_id)
            || (session.get_current_message_id() == Some(ast_msg_id)
                && session.has_any_streamed_content())
    }

    fn is_duplicate_by_content(
        content_for_hash: &Option<(String, std::collections::HashMap<usize, String>)>,
        session: &StreamingSession,
    ) -> bool {
        let Some((ref text, ref tool_names)) = *content_for_hash else {
            return false;
        };
        if text.is_empty() {
            return false;
        }
        session.is_assistant_content_rendered(Self::hash_string(text))
            || session.is_duplicate_by_hash(text, Some(tool_names))
    }

    /// Check if this assistant message is a duplicate of already-streamed content.
    fn is_duplicate_assistant_message(
        &self,
        message: Option<&crate::json_parser::types::AssistantMessage>,
    ) -> bool {
        let session = self.state.streaming_session.borrow();
        if Self::is_duplicate_by_message_id(message, &session) {
            return true;
        }
        let content_for_hash = Self::extract_text_content_for_hash(message);
        if Self::is_duplicate_by_content(&content_for_hash, &session) {
            return true;
        }
        session.has_any_streamed_content()
    }

    /// Format a text content block for assistant output.
    fn format_text_block(&self, out: &mut String, text: &str, prefix: &str, colors: Colors) {
        let limit = self.verbosity.truncate_limit("text");
        let preview = truncate_text(text, limit);
        let _ = writeln!(
            out,
            "{}[{}]{} {}{}{}",
            colors.dim(),
            prefix,
            colors.reset(),
            colors.white(),
            preview,
            colors.reset()
        );
    }

    fn format_tool_input_preview(
        &self,
        out: &mut String,
        input_val: &serde_json::Value,
        prefix: &str,
        colors: Colors,
    ) {
        let input_str = format_tool_input(input_val);
        let limit = self.verbosity.truncate_limit("tool_input");
        let preview = truncate_text(&input_str, limit);
        if !preview.is_empty() {
            out.push_str(&crate::json_parser::types::format_dim_continuation_line(
                &preview, prefix, colors,
            ));
        }
    }

    /// Format a tool use content block for assistant output.
    fn format_tool_use_block(
        &self,
        out: &mut String,
        tool: Option<&String>,
        input: Option<&serde_json::Value>,
        prefix: &str,
        colors: Colors,
    ) {
        let tool_name = tool.cloned().unwrap_or_else(|| "unknown".to_string());
        let _ = writeln!(
            out,
            "{}[{}]{} {}Tool{}: {}{}{}",
            colors.dim(),
            prefix,
            colors.reset(),
            colors.magenta(),
            colors.reset(),
            colors.bold(),
            tool_name,
            colors.reset(),
        );
        self.maybe_append_tool_input_preview(out, input, prefix, colors);
    }

    fn maybe_append_tool_input_preview(
        &self,
        out: &mut String,
        input: Option<&serde_json::Value>,
        prefix: &str,
        colors: Colors,
    ) {
        if let Some(input_val) = input.filter(|_| self.verbosity.show_tool_input()) {
            self.format_tool_input_preview(out, input_val, prefix, colors);
        }
    }

    /// Format a tool result content block for assistant output.
    fn format_tool_result_block(
        &self,
        out: &mut String,
        content: &serde_json::Value,
        prefix: &str,
        colors: Colors,
    ) {
        let content_str = match content {
            serde_json::Value::String(s) => s.clone(),
            other => other.to_string(),
        };
        let limit = self.verbosity.truncate_limit("tool_result");
        out.push_str(
            &crate::json_parser::types::format_tool_output_lines(&content_str, limit, prefix, colors),
        );
    }

    /// Format all content blocks from an assistant message.
    fn format_content_blocks(
        &self,
        out: &mut String,
        content: &[ContentBlock],
        prefix: &str,
        colors: Colors,
    ) {
        content.iter().for_each(|block| match block {
            ContentBlock::Text { text } => {
                if let Some(text) = text {
                    self.format_text_block(out, text, prefix, colors);
                }
            }
            ContentBlock::ToolUse { name, input } => {
                self.format_tool_use_block(out, name.as_ref(), input.as_ref(), prefix, colors);
            }
            ContentBlock::ToolResult { content } => {
                if let Some(content) = content {
                    self.format_tool_result_block(out, content, prefix, colors);
                }
            }
            ContentBlock::Unknown => {}
        });
    }

    fn mark_assistant_message_rendered(
        &self,
        message: Option<&crate::json_parser::types::AssistantMessage>,
        msg: &crate::json_parser::types::AssistantMessage,
    ) {
        self.state
            .with_session_mut(|session: &mut StreamingSession| {
                if let Some(ref message_id) = msg.id {
                    session.mark_message_pre_rendered(message_id);
                }
                if let Some((text_content, _)) = Self::extract_text_content_for_hash(message) {
                    if !text_content.is_empty() {
                        let content_hash =
                            crate::json_parser::boundary::compute_hash_str(&text_content);
                        session.mark_assistant_content_rendered(content_hash);
                    }
                }
            });
    }

    fn render_assistant_message(
        &self,
        message: Option<&crate::json_parser::types::AssistantMessage>,
    ) -> String {
        let mut out = String::new();
        let Some(msg) = message else { return out };
        let Some(content) = msg.content.as_ref() else { return out };
        self.format_content_blocks(&mut out, content, &self.display_name, self.colors);
        if !out.is_empty() {
            self.mark_assistant_message_rendered(message, msg);
        }
        out
    }

    /// Format an assistant event
    fn format_assistant_event(
        &self,
        message: Option<&crate::json_parser::types::AssistantMessage>,
    ) -> String {
        // CRITICAL FIX: When ANY content has been streamed via deltas,
        // the Assistant event should NOT display it again.
        // The Assistant event represents the "complete" message, but if we've
        // already shown the streaming deltas, showing it again causes duplication.
        if self.is_duplicate_assistant_message(message) {
            return String::new();
        }
        self.render_assistant_message(message)
    }

    /// Format a user event
    fn format_user_event(&self, message: Option<crate::json_parser::types::UserMessage>) -> String {
        self.extract_user_text(message)
            .map(|preview| {
                let c = &self.colors;
                let prefix = &self.display_name;
                format!(
                    "{}[{}]{} {}User{}: {}{}{}\n",
                    c.dim(),
                    prefix,
                    c.reset(),
                    c.blue(),
                    c.reset(),
                    c.dim(),
                    preview,
                    c.reset()
                )
            })
            .unwrap_or_default()
    }

    fn extract_user_text(
        &self,
        message: Option<crate::json_parser::types::UserMessage>,
    ) -> Option<String> {
        let content = message?.content?;
        if let Some(ContentBlock::Text { text: Some(text) }) = content.first() {
            let limit = self.verbosity.truncate_limit("user");
            Some(truncate_text(text, limit).to_string())
        } else {
            None
        }
    }

    fn format_result_success_line(
        &self,
        duration_m: u64,
        duration_s_rem: u64,
        turns: u32,
        cost: f64,
    ) -> String {
        let c = &self.colors;
        let prefix = &self.display_name;
        format!(
            "{}[{}]{} {}{} Completed{} {}({}m {}s, {} turns, ${:.4}){}\n",
            c.dim(),
            prefix,
            c.reset(),
            c.green(),
            CHECK,
            c.reset(),
            c.dim(),
            duration_m,
            duration_s_rem,
            turns,
            cost,
            c.reset()
        )
    }

    fn format_result_error_line(
        &self,
        subtype: Option<String>,
        error: Option<String>,
        duration_m: u64,
        duration_s_rem: u64,
    ) -> String {
        format_result_error_line_impl(
            &self.display_name,
            self.colors,
            subtype,
            error,
            duration_m,
            duration_s_rem,
        )
    }

    fn append_result_summary(&self, base: &str, summary: &str) -> String {
        let c = &self.colors;
        let limit = self.verbosity.truncate_limit("result");
        let preview = truncate_text(summary, limit);
        format!(
            "{}\n{}Result summary:{}\n{}{}{}",
            base,
            c.bold(),
            c.reset(),
            c.dim(),
            preview,
            c.reset()
        )
    }

    /// Format a result event
    fn format_result_event(
        &self,
        subtype: Option<String>,
        duration_ms: Option<u64>,
        total_cost_usd: Option<f64>,
        num_turns: Option<u32>,
        result: Option<String>,
        error: Option<String>,
    ) -> String {
        let duration_total_secs = duration_ms.unwrap_or(0) / 1000;
        let duration_m = duration_total_secs / 60;
        let duration_s_rem = duration_total_secs % 60;
        let cost = total_cost_usd.unwrap_or(0.0);
        let turns = num_turns.unwrap_or(0);

        let base = if subtype.as_deref() == Some("success") {
            self.format_result_success_line(duration_m, duration_s_rem, turns, cost)
        } else {
            self.format_result_error_line(subtype, error, duration_m, duration_s_rem)
        };

        result
            .as_deref()
            .map_or(base.clone(), |r| self.append_result_summary(&base, r))
    }
}

fn format_result_error_line_impl(
    prefix: &str,
    c: Colors,
    subtype: Option<String>,
    error: Option<String>,
    duration_m: u64,
    duration_s_rem: u64,
) -> String {
    let err = error.unwrap_or_else(|| "unknown error".to_string());
    let subtype_str = subtype.unwrap_or_else(|| "error".to_string());
    format!(
        "{}[{}]{} {}{} {}{}: {} {}({}m {}s){}\n",
        c.dim(),
        prefix,
        c.reset(),
        c.red(),
        CROSS,
        subtype_str,
        c.reset(),
        err,
        c.dim(),
        duration_m,
        duration_s_rem,
        c.reset()
    )
}