codewhale-tui 0.9.8

Terminal UI for open-source and open-weight coding models
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
//! Structured, success-only File mutation receipts.
//!
//! Tool execution owns the exact before/after evidence. This module shapes
//! that evidence for the calm transcript without depending on whether an
//! approval modal happened to run.

use std::path::{Component, Path};

use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use serde_json::Value;

use crate::palette;
use crate::settings::InlineDiffMode;
use crate::tools::spec::ToolResult;
use crate::tui::diff_render;

use super::details_affordance_line;

const MAX_INLINE_DIFF_LINES: usize = 14;
const MAX_SUMMARY_CHARS: usize = 180;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileMutationOutcome {
    Created,
    Updated,
    Deleted,
    Renamed,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FileMutationFile {
    pub path: String,
    pub previous_path: Option<String>,
    pub outcome: FileMutationOutcome,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FileMutationReceipt {
    /// Raw execution-owned evidence exposed only through the explicit detail
    /// route. It is never painted into the ambient Work/transcript surface.
    pub exact_diff: String,
    /// Header-redacted copy safe for inline presentation.
    pub display_diff: String,
    pub files: Vec<FileMutationFile>,
    pub added: usize,
    pub deleted: usize,
}

impl FileMutationReceipt {
    /// Build a receipt only from an authoritative successful tool result.
    /// Failed/cancelled calls never carry a success diff into the transcript.
    #[must_use]
    pub fn from_success(workspace: &Path, result: &ToolResult) -> Option<Self> {
        if !result.success {
            return None;
        }
        let mutation = result.metadata.as_ref()?.get("mutation")?;
        let raw_diff = mutation.get("diff")?.as_str().unwrap_or("");
        let exact_diff = raw_diff.to_string();
        let display_diff = redact_diff_headers(workspace, raw_diff);
        let mut files = Vec::new();

        if let Some(entries) = mutation.get("files").and_then(Value::as_array) {
            for entry in entries {
                let Some(raw_path) = entry.get("path").and_then(Value::as_str) else {
                    continue;
                };
                let outcome = match entry.get("outcome").and_then(Value::as_str) {
                    Some("created") => FileMutationOutcome::Created,
                    Some("updated") => FileMutationOutcome::Updated,
                    Some("deleted") => FileMutationOutcome::Deleted,
                    _ => continue,
                };
                files.push(FileMutationFile {
                    path: privacy_safe_path(workspace, raw_path),
                    previous_path: None,
                    outcome,
                });
            }
        }
        if let Some(renames) = mutation.get("renames").and_then(Value::as_array) {
            for rename in renames {
                let Some(from) = rename.get("from").and_then(Value::as_str) else {
                    continue;
                };
                let Some(to) = rename.get("to").and_then(Value::as_str) else {
                    continue;
                };
                files.push(FileMutationFile {
                    path: privacy_safe_path(workspace, to),
                    previous_path: Some(privacy_safe_path(workspace, from)),
                    outcome: FileMutationOutcome::Renamed,
                });
            }
        }

        let summaries = diff_render::summarize_diff(&display_diff);
        let added = summaries.iter().map(|summary| summary.added).sum();
        let deleted = summaries.iter().map(|summary| summary.deleted).sum();
        if files.is_empty() && exact_diff.trim().is_empty() {
            return None;
        }
        Some(Self {
            exact_diff,
            display_diff,
            files,
            added,
            deleted,
        })
    }

    #[must_use]
    pub fn outcome_label(&self) -> String {
        let created = self.count(FileMutationOutcome::Created);
        let updated = self.count(FileMutationOutcome::Updated);
        let deleted = self.count(FileMutationOutcome::Deleted);
        let renamed = self.count(FileMutationOutcome::Renamed);
        let mut outcome_parts = Vec::new();
        push_count(&mut outcome_parts, created, "created");
        push_count(&mut outcome_parts, updated, "updated");
        push_count(&mut outcome_parts, deleted, "deleted");
        push_count(&mut outcome_parts, renamed, "renamed");

        if self.files.is_empty() {
            "Changed files".to_string()
        } else if self.files.len() == 1 {
            let file = &self.files[0];
            match file.outcome {
                FileMutationOutcome::Created => format!("Created {}", file.path),
                FileMutationOutcome::Updated => format!("Updated {}", file.path),
                FileMutationOutcome::Deleted => format!("Deleted {}", file.path),
                FileMutationOutcome::Renamed => format!(
                    "Renamed {} โ†’ {}",
                    file.previous_path.as_deref().unwrap_or("file"),
                    file.path
                ),
            }
        } else {
            format!("{} files ยท {}", self.files.len(), outcome_parts.join(" ยท "))
        }
    }

    #[must_use]
    pub fn semantic_summary(&self) -> String {
        let stats = format!("+{} -{}", self.added, self.deleted);
        let separator_chars = " ยท ".chars().count();
        let outcome_budget = MAX_SUMMARY_CHARS
            .saturating_sub(stats.chars().count())
            .saturating_sub(separator_chars);
        format!(
            "{} ยท {stats}",
            bounded_text(&self.outcome_label(), outcome_budget)
        )
    }

    #[must_use]
    pub fn inspect_text(&self) -> String {
        let summary = self.semantic_summary();
        if self.exact_diff.trim().is_empty() {
            format!("{summary}\n\n(no textual changes)")
        } else {
            format!("{summary}\n\n{}", self.exact_diff)
        }
    }

    pub fn render_inline(&self, width: u16, mode: InlineDiffMode) -> Vec<Line<'static>> {
        match mode {
            InlineDiffMode::Off => vec![exact_evidence_hint()],
            InlineDiffMode::Summary => vec![
                Line::from(Span::styled(
                    self.semantic_summary(),
                    Style::default()
                        .fg(palette::TEXT_PRIMARY)
                        .add_modifier(Modifier::BOLD),
                )),
                exact_evidence_hint(),
            ],
            InlineDiffMode::Full => {
                let mut lines = vec![Line::from(Span::styled(
                    self.semantic_summary(),
                    Style::default()
                        .fg(palette::TEXT_PRIMARY)
                        .add_modifier(Modifier::BOLD),
                ))];
                if !self.display_diff.trim().is_empty() {
                    let rendered = diff_render::render_diff_body_bounded(
                        &self.display_diff,
                        width,
                        MAX_INLINE_DIFF_LINES,
                    );
                    lines.extend(rendered.lines);
                    if rendered.omitted_rows > 0 {
                        let detail_hint =
                            crate::tui::key_shortcuts::tool_details_shortcut_action_hint("change");
                        lines.push(details_affordance_line(
                            &format!("+{} diff lines ยท {detail_hint}", rendered.omitted_rows),
                            Style::default().fg(palette::TEXT_MUTED).italic(),
                        ));
                    } else {
                        lines.push(exact_evidence_hint());
                    }
                } else {
                    lines.push(exact_evidence_hint());
                }
                lines
            }
        }
    }

    fn count(&self, outcome: FileMutationOutcome) -> usize {
        self.files
            .iter()
            .filter(|file| file.outcome == outcome)
            .count()
    }
}

fn exact_evidence_hint() -> Line<'static> {
    details_affordance_line(
        &crate::tui::key_shortcuts::tool_details_shortcut_action_hint("change"),
        Style::default().fg(palette::TEXT_MUTED).italic(),
    )
}

fn push_count(parts: &mut Vec<String>, count: usize, label: &str) {
    if count > 0 {
        parts.push(format!("{count} {label}"));
    }
}

fn bounded_text(value: &str, max_chars: usize) -> String {
    if value.chars().count() <= max_chars {
        return value.to_string();
    }
    if max_chars == 0 {
        return String::new();
    }
    let mut bounded = value
        .chars()
        .take(max_chars.saturating_sub(1))
        .collect::<String>();
    bounded.push('โ€ฆ');
    bounded
}

fn privacy_safe_path(workspace: &Path, raw: &str) -> String {
    let normalized = raw.replace('\\', "/");
    let workspace = workspace.to_string_lossy().replace('\\', "/");
    let relative = if Path::new(raw).is_absolute() || normalized.starts_with('/') {
        let prefix = workspace.trim_end_matches('/');
        if normalized == prefix {
            ""
        } else if let Some(relative) = normalized.strip_prefix(&format!("{prefix}/")) {
            relative
        } else {
            return "<external file>".to_string();
        }
    } else {
        normalized.as_str()
    };
    let path = Path::new(relative);
    if path.components().any(|component| {
        matches!(
            component,
            Component::ParentDir | Component::RootDir | Component::Prefix(_)
        )
    }) {
        return "<external file>".to_string();
    }
    let display = path.to_string_lossy().replace('\\', "/");
    if display.is_empty() {
        "<workspace>".to_string()
    } else {
        display
    }
}

fn redact_diff_headers(workspace: &Path, diff: &str) -> String {
    let mut redacted = diff
        .lines()
        .map(|line| {
            for prefix in ["--- ", "+++ ", "rename from ", "rename to "] {
                if let Some(raw) = line.strip_prefix(prefix) {
                    if raw == "/dev/null" {
                        return line.to_string();
                    }
                    let side = if raw.starts_with("a/") {
                        "a/"
                    } else if raw.starts_with("b/") {
                        "b/"
                    } else {
                        ""
                    };
                    let path = raw.strip_prefix(side).unwrap_or(raw);
                    return format!("{prefix}{side}{}", privacy_safe_path(workspace, path));
                }
            }
            if let Some(rest) = line.strip_prefix("diff --git ") {
                let mut paths = rest.split_whitespace();
                if let (Some(old), Some(new)) = (paths.next(), paths.next()) {
                    let old = old.strip_prefix("a/").unwrap_or(old);
                    let new = new.strip_prefix("b/").unwrap_or(new);
                    return format!(
                        "diff --git a/{} b/{}",
                        privacy_safe_path(workspace, old),
                        privacy_safe_path(workspace, new)
                    );
                }
            }
            line.to_string()
        })
        .collect::<Vec<_>>()
        .join("\n");
    if diff.ends_with('\n') {
        redacted.push('\n');
    }
    redacted
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn plain(lines: &[Line<'_>]) -> String {
        lines
            .iter()
            .map(|line| {
                line.spans
                    .iter()
                    .map(|span| span.content.as_ref())
                    .collect::<String>()
            })
            .collect::<Vec<_>>()
            .join("\n")
    }

    fn result(mutation: Value) -> ToolResult {
        ToolResult::success("ok").with_metadata(json!({ "mutation": mutation }))
    }

    #[test]
    fn creates_bounded_semantic_summary_and_redacts_external_headers() {
        let result = result(json!({
            "diff": "--- /Users/alice/private.rs\n+++ /Users/alice/private.rs\n@@ -0,0 +1 @@\n+secret\n",
            "files": [{ "path": "/Users/alice/private.rs", "outcome": "created" }],
            "renames": []
        }));
        let receipt =
            FileMutationReceipt::from_success(Path::new("/workspace"), &result).expect("receipt");
        assert_eq!(receipt.files[0].path, "<external file>");
        assert!(receipt.exact_diff.contains("alice"));
        assert!(!receipt.display_diff.contains("alice"));
        assert!(
            receipt
                .semantic_summary()
                .contains("Created <external file>")
        );
    }

    #[test]
    fn rename_and_multifile_outcomes_stay_semantic() {
        let result = result(json!({
            "diff": "diff --git a/old.rs b/new.rs\nrename from old.rs\nrename to new.rs\n--- a/lib.rs\n+++ b/lib.rs\n@@ -1 +1 @@\n-old\n+new\n",
            "files": [{ "path": "lib.rs", "outcome": "updated" }],
            "renames": [{ "from": "old.rs", "to": "new.rs" }]
        }));
        let receipt =
            FileMutationReceipt::from_success(Path::new("/workspace"), &result).expect("receipt");
        assert_eq!(receipt.files.len(), 2);
        assert_eq!(receipt.added, 1);
        assert_eq!(receipt.deleted, 1);
        assert_eq!(
            receipt.semantic_summary(),
            "2 files ยท 1 updated ยท 1 renamed ยท +1 -1"
        );
    }

    #[test]
    fn failed_results_never_become_success_receipts() {
        let failed = ToolResult::error("cancelled").with_metadata(json!({
            "mutation": {
                "diff": "--- a/a\n+++ b/a\n@@ -1 +1 @@\n-old\n+new\n",
                "files": [{ "path": "a", "outcome": "updated" }],
                "renames": []
            }
        }));
        assert!(FileMutationReceipt::from_success(Path::new("/workspace"), &failed).is_none());
    }

    #[test]
    fn inline_diff_modes_are_bounded_and_keep_the_exact_detail_route() {
        let additions = (0..30)
            .map(|index| format!("+line {index}"))
            .collect::<Vec<_>>()
            .join("\n");
        let result = result(json!({
            "diff": format!("--- a/src/lib.rs\n+++ b/src/lib.rs\n@@ -0,0 +1,30 @@\n{additions}\n"),
            "files": [{ "path": "src/lib.rs", "outcome": "created" }],
            "renames": []
        }));
        let receipt =
            FileMutationReceipt::from_success(Path::new("/workspace"), &result).expect("receipt");

        let full = receipt.render_inline(80, InlineDiffMode::Full);
        let full_text = plain(&full);
        assert!(full.len() <= MAX_INLINE_DIFF_LINES + 2, "{full_text}");
        assert!(full_text.contains("line 0"), "{full_text}");
        assert!(full_text.contains("diff lines"), "{full_text}");
        assert!(full_text.contains(":change"), "{full_text}");
        assert!(!full_text.contains("summary:"), "{full_text}");

        let summary = plain(&receipt.render_inline(80, InlineDiffMode::Summary));
        assert!(summary.contains("Created src/lib.rs"), "{summary}");
        assert!(summary.contains("+30 -0"), "{summary}");
        assert!(!summary.contains("line 0"), "{summary}");
        assert!(summary.contains(":change"), "{summary}");

        let off = plain(&receipt.render_inline(80, InlineDiffMode::Off));
        assert!(!off.contains("line 0"), "{off}");
        assert!(!off.contains("+30 -0"), "{off}");
        assert!(off.contains(":change"), "{off}");
    }

    #[test]
    fn full_mode_spends_its_bound_on_red_green_evidence() {
        let result = result(json!({
            "diff": "diff --git a/old.rs b/new.rs\nsimilarity index 100%\nrename from old.rs\nrename to new.rs\ndiff --git a/lib.rs b/lib.rs\n--- a/lib.rs\n+++ b/lib.rs\n@@ -1 +1 @@\n-old\n+new\n",
            "files": [{ "path": "lib.rs", "outcome": "updated" }],
            "renames": [{ "from": "old.rs", "to": "new.rs" }]
        }));
        let receipt =
            FileMutationReceipt::from_success(Path::new("/workspace"), &result).expect("receipt");
        let full_text = plain(&receipt.render_inline(80, InlineDiffMode::Full));

        assert!(full_text.contains("- old"), "{full_text}");
        assert!(full_text.contains("+ new"), "{full_text}");
        assert!(!full_text.contains("summary:"), "{full_text}");
    }

    #[test]
    fn bounded_summary_never_truncates_semantic_stats() {
        let long_path = format!("src/{}.rs", "whale".repeat(80));
        let result = result(json!({
            "diff": format!("--- a/{long_path}\n+++ b/{long_path}\n@@ -1 +1 @@\n-old\n+new\n"),
            "files": [{ "path": long_path, "outcome": "updated" }],
            "renames": []
        }));
        let receipt =
            FileMutationReceipt::from_success(Path::new("/workspace"), &result).expect("receipt");
        let summary = receipt.semantic_summary();
        assert!(summary.ends_with(" ยท +1 -1"), "{summary}");
        assert!(summary.chars().count() <= MAX_SUMMARY_CHARS);
    }

    #[test]
    fn failed_file_cell_never_paints_a_forged_success_receipt() {
        let receipt = FileMutationReceipt::from_success(
            Path::new("/workspace"),
            &result(json!({
                "diff": "--- a/a.rs\n+++ b/a.rs\n@@ -1 +1 @@\n-old\n+FORGED-SUCCESS\n",
                "files": [{ "path": "a.rs", "outcome": "updated" }],
                "renames": []
            })),
        )
        .expect("receipt");
        let cell = super::super::PatchSummaryCell {
            path: "a.rs".to_string(),
            summary: "editing".to_string(),
            status: super::super::ToolStatus::Failed,
            error: Some("cancelled".to_string()),
            receipt: Some(receipt),
        };

        let text = plain(&cell.render(
            80,
            true,
            super::super::RenderMode::Live,
            InlineDiffMode::Full,
        ));
        assert!(text.contains("cancelled"), "{text}");
        assert!(!text.contains("FORGED-SUCCESS"), "{text}");
    }
}