aether-wisp 0.6.2

A terminal UI for AI coding agents via the Agent Client Protocol (ACP)
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
use std::collections::HashMap;
use std::fmt::Write;
use std::path::{Path, PathBuf};
use thiserror::Error;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DiffDocument {
    pub repo_root: PathBuf,
    pub files: Vec<FileDiff>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FileDiff {
    pub old_path: Option<String>,
    pub path: String,
    pub status: FileStatus,
    pub staged: StageState,
    pub hunks: Vec<Hunk>,
    pub binary: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Hunk {
    pub header: String,
    pub old_start: usize,
    pub old_count: usize,
    pub new_start: usize,
    pub new_count: usize,
    pub lines: Vec<PatchLine>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PatchLine {
    pub kind: PatchLineKind,
    pub text: String,
    pub old_line_no: Option<usize>,
    pub new_line_no: Option<usize>,
}

impl PatchLine {
    pub fn added(text: impl Into<String>, new_line_no: usize) -> Self {
        Self { kind: PatchLineKind::Added, text: text.into(), old_line_no: None, new_line_no: Some(new_line_no) }
    }

    pub fn removed(text: impl Into<String>, old_line_no: usize) -> Self {
        Self { kind: PatchLineKind::Removed, text: text.into(), old_line_no: Some(old_line_no), new_line_no: None }
    }

    pub fn context(text: impl Into<String>, old_line_no: usize, new_line_no: usize) -> Self {
        Self {
            kind: PatchLineKind::Context,
            text: text.into(),
            old_line_no: Some(old_line_no),
            new_line_no: Some(new_line_no),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum DiffScope {
    Unstaged,
    Staged,
    #[default]
    Both,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileStatus {
    Modified,
    Added,
    Deleted,
    Renamed,
    Untracked,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StageState {
    Unstaged,
    Staged,
    PartiallyStaged,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PatchLineKind {
    HunkHeader,
    Context,
    Added,
    Removed,
    Meta,
}

#[derive(Debug, Error)]
pub enum GitDiffError {
    #[error("Not a git repository")]
    NotARepository,
    #[error("Git command failed: {stderr}")]
    CommandFailed { stderr: String },
    #[error("Failed to parse diff: {0}")]
    ParseError(String),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct PatchAnchor {
    pub file_index: usize,
    pub hunk: usize,
    pub line: usize,
}

#[derive(Debug, Clone)]
pub struct CommentContext {
    pub file_path: String,
    pub line_text: String,
    pub line_number: Option<usize>,
    pub line_kind: PatchLineKind,
}

#[derive(Debug, Clone)]
pub struct QueuedComment {
    pub anchor: PatchAnchor,
    pub body: String,
    pub context: CommentContext,
}

#[derive(Debug, Clone, Default)]
pub struct ReviewQueue {
    comments: Vec<QueuedComment>,
}

impl ReviewQueue {
    pub fn is_empty(&self) -> bool {
        self.comments.is_empty()
    }

    pub fn len(&self) -> usize {
        self.comments.len()
    }

    pub fn clear(&mut self) {
        self.comments.clear();
    }

    pub fn push(&mut self, comment: QueuedComment) {
        self.comments.push(comment);
    }

    pub fn pop(&mut self) -> Option<QueuedComment> {
        self.comments.pop()
    }

    pub fn comments_for_file(&self, file_path: &str) -> impl Iterator<Item = &QueuedComment> {
        self.comments.iter().filter(move |c| c.context.file_path == file_path)
    }

    pub fn comments(&self) -> &[QueuedComment] {
        &self.comments
    }

    pub fn format_prompt(&self) -> String {
        let mut prompt = String::from("I'm reviewing the working tree diff. Here are my comments:\n");
        let mut file_order: Vec<&str> = Vec::new();
        let mut grouped: HashMap<&str, Vec<&QueuedComment>> = HashMap::new();

        for comment in &self.comments {
            let path = comment.context.file_path.as_str();
            if !grouped.contains_key(path) {
                file_order.push(path);
            }
            grouped.entry(path).or_default().push(comment);
        }

        for file_path in file_order {
            let file_comments = grouped.get(file_path).expect("group exists for ordered path");
            write!(prompt, "\n## `{file_path}`\n").unwrap();

            for comment in file_comments {
                let kind_label = match comment.context.line_kind {
                    PatchLineKind::Added => "added",
                    PatchLineKind::Removed => "removed",
                    PatchLineKind::Context => "context",
                    PatchLineKind::HunkHeader => "header",
                    PatchLineKind::Meta => "meta",
                };
                let line_ref = match comment.context.line_number {
                    Some(n) => format!("Line {n} ({kind_label})"),
                    None => kind_label.to_string(),
                };
                write!(prompt, "\n**{line_ref}:** `{}`\n> {}\n", comment.context.line_text, comment.body).unwrap();
            }
        }

        prompt
    }
}

impl DiffDocument {
    /// Normalizes command output into the model consumed by both review renderers.
    ///
    /// Git execution deliberately happens outside this type. Keeping this operation
    /// synchronous makes parsing deterministic and lets tests exercise it without a
    /// repository or subprocess.
    pub fn from_git_output(
        repo_root: PathBuf,
        diff_output: &str,
        status_output: &str,
        untracked_files: impl IntoIterator<Item = (String, Vec<u8>)>,
        scope: DiffScope,
    ) -> Result<Self, GitDiffError> {
        let mut files = if diff_output.trim().is_empty() { Vec::new() } else { parse_unified_diff(diff_output)? };

        if scope.includes_untracked() {
            files.extend(untracked_files.into_iter().map(|(path, bytes)| build_untracked_file_diff(path, &bytes)));
        }

        let status_map = parse_porcelain_status(status_output);
        for file in &mut files {
            file.staged = status_map.get(&file.path).copied().unwrap_or(StageState::Unstaged);
        }
        files.sort_by(|left, right| left.path.cmp(&right.path));
        Ok(Self { repo_root, files })
    }
}

impl DiffScope {
    pub fn label(self) -> &'static str {
        match self {
            Self::Unstaged => "Unstaged",
            Self::Staged => "Staged",
            Self::Both => "Both",
        }
    }

    pub fn next(self) -> Self {
        match self {
            Self::Both => Self::Unstaged,
            Self::Unstaged => Self::Staged,
            Self::Staged => Self::Both,
        }
    }

    pub fn includes_untracked(self) -> bool {
        !matches!(self, Self::Staged)
    }
}

impl FileStatus {
    pub fn marker(self) -> char {
        match self {
            Self::Modified => 'M',
            Self::Added => 'A',
            Self::Deleted => 'D',
            Self::Renamed => 'R',
            Self::Untracked => '?',
        }
    }

    pub fn label(self) -> &'static str {
        match self {
            Self::Modified => "modified",
            Self::Added => "new file",
            Self::Deleted => "deleted",
            Self::Renamed => "renamed",
            Self::Untracked => "untracked",
        }
    }
}

impl FileDiff {
    /// Builds the canonical model used for ACP tool previews and Git reviews.
    pub fn from_texts(path: impl Into<String>, old: &str, new: &str) -> Self {
        let path = path.into();
        let status = match (old.is_empty(), new.is_empty()) {
            (true, false) => FileStatus::Added,
            (false, true) => FileStatus::Deleted,
            _ => FileStatus::Modified,
        };
        let old_lines: Vec<&str> = old.lines().collect();
        let new_lines: Vec<&str> = new.lines().collect();
        let mut patch_lines = Vec::new();
        let mut old_number = 0;
        let mut new_number = 0;

        for operation in similar::TextDiff::from_lines(old, new).ops() {
            match *operation {
                similar::DiffOp::Equal { old_index, new_index: _, len } => {
                    for offset in 0..len {
                        old_number += 1;
                        new_number += 1;
                        patch_lines.push(PatchLine::context(
                            source_line(&old_lines, old_index + offset),
                            old_number,
                            new_number,
                        ));
                    }
                }
                similar::DiffOp::Delete { old_index, old_len, .. } => {
                    for offset in 0..old_len {
                        old_number += 1;
                        patch_lines.push(PatchLine::removed(source_line(&old_lines, old_index + offset), old_number));
                    }
                }
                similar::DiffOp::Insert { new_index, new_len, .. } => {
                    for offset in 0..new_len {
                        new_number += 1;
                        patch_lines.push(PatchLine::added(source_line(&new_lines, new_index + offset), new_number));
                    }
                }
                similar::DiffOp::Replace { old_index, old_len, new_index, new_len } => {
                    for offset in 0..old_len {
                        old_number += 1;
                        patch_lines.push(PatchLine::removed(source_line(&old_lines, old_index + offset), old_number));
                    }
                    for offset in 0..new_len {
                        new_number += 1;
                        patch_lines.push(PatchLine::added(source_line(&new_lines, new_index + offset), new_number));
                    }
                }
            }
        }

        trim_patch_context(&mut patch_lines);
        let hunks = if patch_lines.is_empty() {
            Vec::new()
        } else {
            let old_start = patch_lines.iter().find_map(|line| line.old_line_no).unwrap_or(0);
            let new_start = patch_lines.iter().find_map(|line| line.new_line_no).unwrap_or(0);
            let old_count = patch_lines.iter().filter(|line| line.old_line_no.is_some()).count();
            let new_count = patch_lines.iter().filter(|line| line.new_line_no.is_some()).count();
            let header = format!("@@ -{old_start},{old_count} +{new_start},{new_count} @@");
            let mut lines = Vec::with_capacity(patch_lines.len() + 1);
            lines.push(PatchLine {
                kind: PatchLineKind::HunkHeader,
                text: header.clone(),
                old_line_no: None,
                new_line_no: None,
            });
            lines.extend(patch_lines);
            vec![Hunk { header, old_start, old_count, new_start, new_count, lines }]
        };

        Self {
            old_path: (status != FileStatus::Added).then(|| path.clone()),
            path,
            status,
            staged: StageState::Unstaged,
            hunks,
            binary: false,
        }
    }

    pub fn additions(&self) -> usize {
        self.hunks.iter().map(Hunk::additions).sum()
    }

    pub fn deletions(&self) -> usize {
        self.hunks.iter().map(Hunk::deletions).sum()
    }

    pub fn language(&self) -> &str {
        Path::new(&self.path).extension().and_then(|extension| extension.to_str()).unwrap_or_default()
    }
}

impl Hunk {
    pub fn additions(&self) -> usize {
        self.lines.iter().filter(|line| line.kind == PatchLineKind::Added).count()
    }

    pub fn deletions(&self) -> usize {
        self.lines.iter().filter(|line| line.kind == PatchLineKind::Removed).count()
    }
}

pub const EMPTY_TREE: &str = "4b825dc642cb6eb9a060e54bf8d69288fbee4904";

pub fn parse_porcelain_status(input: &str) -> HashMap<String, StageState> {
    let mut map = HashMap::new();
    let mut tokens = input.split('\0').filter(|token| !token.is_empty());

    while let Some(record) = tokens.next() {
        if record.len() < 3 {
            continue;
        }
        let bytes = record.as_bytes();
        let index = bytes[0] as char;
        let worktree = bytes[1] as char;
        let path = if matches!(index, 'R' | 'C') || matches!(worktree, 'R' | 'C') {
            tokens.next().unwrap_or(&record[3..])
        } else {
            &record[3..]
        };

        let state = match (index, worktree) {
            ('?', '?') | (' ', _) => StageState::Unstaged,
            (_, ' ') => StageState::Staged,
            _ => StageState::PartiallyStaged,
        };
        map.insert(path.to_string(), state);
    }

    map
}

pub(crate) fn build_untracked_file_diff(path: String, bytes: &[u8]) -> FileDiff {
    if bytes.iter().take(8192).any(|byte| *byte == 0) {
        return binary_untracked(path);
    }
    let Ok(content) = std::str::from_utf8(bytes) else {
        return binary_untracked(path);
    };
    let source_lines: Vec<&str> = content.lines().collect();
    let line_count = source_lines.len();
    let header = format!("@@ -0,0 +1,{line_count} @@");
    let mut lines =
        vec![PatchLine { kind: PatchLineKind::HunkHeader, text: header.clone(), old_line_no: None, new_line_no: None }];
    lines.extend(source_lines.iter().enumerate().map(|(index, text)| PatchLine {
        kind: PatchLineKind::Added,
        text: (*text).to_string(),
        old_line_no: None,
        new_line_no: Some(index + 1),
    }));

    FileDiff {
        old_path: None,
        path,
        status: FileStatus::Untracked,
        staged: StageState::Unstaged,
        hunks: vec![Hunk { header, old_start: 0, old_count: 0, new_start: 1, new_count: line_count, lines }],
        binary: false,
    }
}

pub(crate) fn binary_untracked(path: String) -> FileDiff {
    FileDiff {
        old_path: None,
        path,
        status: FileStatus::Untracked,
        staged: StageState::Unstaged,
        hunks: Vec::new(),
        binary: true,
    }
}

pub fn parse_unified_diff(input: &str) -> Result<Vec<FileDiff>, GitDiffError> {
    split_diff_files(input).into_iter().map(parse_file_diff).collect()
}

fn split_diff_files(input: &str) -> Vec<&str> {
    let mut chunks = Vec::new();
    let mut start = None;
    let mut line_start = 0;

    while line_start < input.len() {
        let line_end = input[line_start..].find('\n').map_or(input.len(), |index| line_start + index + 1);
        let line = &input[line_start..line_end];
        if line.starts_with("diff --git ") {
            if let Some(chunk_start) = start {
                chunks.push(&input[chunk_start..line_start]);
            }
            start = Some(line_start);
        }
        line_start = line_end;
    }
    if let Some(chunk_start) = start {
        chunks.push(&input[chunk_start..]);
    }
    chunks
}

fn parse_file_diff(chunk: &str) -> Result<FileDiff, GitDiffError> {
    let lines: Vec<&str> = chunk.lines().collect();
    let Some(header) = lines.first() else {
        return Err(GitDiffError::ParseError("Empty diff chunk".to_string()));
    };
    let (old_path, new_path) = parse_diff_header(header)?;
    let (status, binary, rename_from, hunk_start) = scan_file_metadata(&lines);
    let hunks = if binary { Vec::new() } else { parse_file_hunks(&lines[hunk_start..])? };

    Ok(FileDiff {
        old_path: resolve_old_path(status, rename_from, old_path),
        path: new_path,
        status,
        staged: StageState::Unstaged,
        hunks,
        binary,
    })
}

fn scan_file_metadata(lines: &[&str]) -> (FileStatus, bool, Option<String>, usize) {
    let mut status = FileStatus::Modified;
    let mut binary = false;
    let mut rename_from = None;
    let mut index = 1;
    while index < lines.len() {
        let line = lines[index];
        if line.starts_with("new file mode") {
            status = FileStatus::Added;
        } else if line.starts_with("deleted file mode") {
            status = FileStatus::Deleted;
        } else if let Some(path) = line.strip_prefix("rename from ") {
            status = FileStatus::Renamed;
            rename_from = Some(path.to_string());
        } else if line.starts_with("rename to ") {
            status = FileStatus::Renamed;
        } else if line.starts_with("Binary files ") {
            binary = true;
        } else if line.starts_with("@@") {
            break;
        }
        index += 1;
    }
    (status, binary, rename_from, index)
}

fn parse_file_hunks(lines: &[&str]) -> Result<Vec<Hunk>, GitDiffError> {
    let mut hunks = Vec::new();
    let mut index = 0;
    while index < lines.len() {
        if lines[index].starts_with("@@") {
            let (hunk, consumed) = parse_hunk(&lines[index..])?;
            hunks.push(hunk);
            index += consumed;
        } else {
            index += 1;
        }
    }
    Ok(hunks)
}

fn resolve_old_path(status: FileStatus, rename_from: Option<String>, old_path: String) -> Option<String> {
    match status {
        FileStatus::Added | FileStatus::Untracked => None,
        FileStatus::Renamed => rename_from.or(Some(old_path)),
        _ => Some(old_path),
    }
}

fn parse_diff_header(line: &str) -> Result<(String, String), GitDiffError> {
    let rest = line
        .strip_prefix("diff --git ")
        .ok_or_else(|| GitDiffError::ParseError(format!("Invalid diff header: {line}")))?;
    if let Some((old, new)) = rest.split_once(" b/") {
        Ok((old.strip_prefix("a/").unwrap_or(old).to_string(), new.to_string()))
    } else {
        Err(GitDiffError::ParseError(format!("Cannot parse paths from: {line}")))
    }
}

fn source_line<'a>(lines: &[&'a str], index: usize) -> &'a str {
    lines.get(index).copied().unwrap_or("")
}

fn trim_patch_context(lines: &mut Vec<PatchLine>) {
    const CONTEXT: usize = 3;
    let Some(first_change) = lines.iter().position(|line| line.kind != PatchLineKind::Context) else {
        lines.clear();
        return;
    };
    let last_change = lines.iter().rposition(|line| line.kind != PatchLineKind::Context).unwrap_or(first_change);
    let start = first_change.saturating_sub(CONTEXT);
    let end = (last_change + CONTEXT + 1).min(lines.len());
    lines.drain(end..);
    lines.drain(..start);
}

fn parse_hunk(lines: &[&str]) -> Result<(Hunk, usize), GitDiffError> {
    let header = lines[0];
    let (old_start, old_count, new_start, new_count) = parse_hunk_header(header)?;
    let mut patch_lines = vec![PatchLine {
        kind: PatchLineKind::HunkHeader,
        text: header.to_string(),
        old_line_no: None,
        new_line_no: None,
    }];
    let mut old_line = old_start;
    let mut new_line = new_start;
    let mut index = 1;

    while index < lines.len() && !lines[index].starts_with("@@") {
        let line = lines[index];
        let patch_line = if let Some(text) = line.strip_prefix('+') {
            let result = PatchLine::added(text, new_line);
            new_line += 1;
            result
        } else if let Some(text) = line.strip_prefix('-') {
            let result = PatchLine::removed(text, old_line);
            old_line += 1;
            result
        } else if let Some(text) = line.strip_prefix(' ') {
            let result = PatchLine::context(text, old_line, new_line);
            old_line += 1;
            new_line += 1;
            result
        } else if line.starts_with('\\') {
            PatchLine { kind: PatchLineKind::Meta, text: line.to_string(), old_line_no: None, new_line_no: None }
        } else {
            let result = PatchLine::context(line, old_line, new_line);
            old_line += 1;
            new_line += 1;
            result
        };
        patch_lines.push(patch_line);
        index += 1;
    }

    Ok((Hunk { header: header.to_string(), old_start, old_count, new_start, new_count, lines: patch_lines }, index))
}

fn parse_hunk_header(header: &str) -> Result<(usize, usize, usize, usize), GitDiffError> {
    let invalid = || GitDiffError::ParseError(format!("Invalid hunk header: {header}"));
    let rest = header.strip_prefix("@@ -").ok_or_else(invalid)?;
    let end = rest.find(" @@").ok_or_else(invalid)?;
    let (old_range, new_range) = rest[..end].split_once(" +").ok_or_else(invalid)?;
    let (old_start, old_count) = parse_range(old_range).ok_or_else(invalid)?;
    let (new_start, new_count) = parse_range(new_range).ok_or_else(invalid)?;
    Ok((old_start, old_count, new_start, new_count))
}

fn parse_range(value: &str) -> Option<(usize, usize)> {
    if let Some((start, count)) = value.split_once(',') {
        Some((start.parse().ok()?, count.parse().ok()?))
    } else {
        Some((value.parse().ok()?, 1))
    }
}