kaish-kernel 0.8.2

Core kernel for kaish: lexer, parser, interpreter, and runtime
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
//! patch — Apply unified diffs to files.
//!
//! # Examples
//!
//! ```kaish
//! patch < changes.patch           # Apply patch from stdin
//! patch -p1 < changes.patch       # Strip 1 path component
//! patch -R < changes.patch        # Reverse the patch
//! patch --dry-run < changes.patch # Show what would change
//! patch file.txt < changes.patch  # Explicit target file
//! ```

use async_trait::async_trait;
use clap::{CommandFactory, Parser};
use std::path::Path;

use crate::ast::Value;
use crate::backend::PatchOp;
use crate::interpreter::{ExecResult, OutputData};
use crate::tools::{schema_from_clap, ExecContext, ToolCtx, GlobalFlags, Tool, ToolArgs, ToolSchema};

/// Patch tool: applies unified diffs to files.
pub struct Patch;

/// clap-derived argv layer for patch. See docs/clap-migration.md.
#[derive(Parser, Debug)]
#[command(name = "patch", about = "Apply unified diff to files")]
struct PatchArgs {
    /// Strip N leading path components (-p).
    #[arg(short = 'p')]
    p: Option<i64>,

    /// Reverse the patch (swap + and -).
    #[arg(short = 'R', long = "reverse")]
    reverse: bool,

    /// Show what would change without applying.
    #[arg(long = "dry-run", visible_alias = "dry_run")]
    dry_run: bool,

    /// Target file (overrides patch header).
    #[arg(long = "file")]
    file: Option<String>,

    #[command(flatten)]
    global: GlobalFlags,

    /// Sink — positional file path is the same value as `--file`; the kernel's
    /// `args.get_string("file", 0)` falls back to positional[0], so users can
    /// write either `patch --file foo.txt < diff` or `patch foo.txt < diff`.
    #[arg(hide = true)]
    rest: Vec<String>,
}

#[async_trait]
impl Tool for Patch {
    fn name(&self) -> &str {
        "patch"
    }

    fn schema(&self) -> ToolSchema {
        schema_from_clap(
            &PatchArgs::command(),
            "patch",
            "Apply unified diff to files",
            [
                ("Apply a patch", "patch < changes.patch"),
                ("Dry run", "patch --dry-run < changes.patch"),
                ("Strip path prefix", "patch -p1 < changes.patch"),
            ],
        )
    }

    async fn execute(&self, mut args: ToolArgs, ctx: &mut dyn ToolCtx) -> ExecResult {
        let Some(ctx) = ctx.as_any_mut().downcast_mut::<ExecContext>() else {
            return ExecResult::failure(1, "internal error: kernel builtin requires ExecContext");
        };
        // Tests poke args.flags.insert("dry-run") and args.named.insert("p", Int(1)).
        // `-R` flag and `--dry-run` flag work directly. The `p=1` form lands as
        // a single-char named entry which to_argv renders as `-p=1`; clap's
        // `Option<i64>` with short='p' handles that natively.
        args.flagify_bool_named();

        let parsed = match PatchArgs::try_parse_from(
            std::iter::once("patch".to_string()).chain(args.to_argv()),
        ) {
            Ok(p) => p,
            Err(e) => return ExecResult::failure(2, format!("patch: {e}")),
        };
        parsed.global.apply(ctx);

        // Read patch content from stdin
        let patch_content = ctx.stdin.take().unwrap_or_default();
        if patch_content.is_empty() {
            return ExecResult::failure(1, "patch: no input provided (use stdin)");
        }

        // Parse options
        let strip_level = parsed
            .p
            .map(|i| i as usize)
            .or_else(|| {
                args.get_named("p").and_then(|v| match v {
                    Value::Int(i) => Some(*i as usize),
                    Value::String(s) => s.parse().ok(),
                    _ => None,
                })
            })
            .unwrap_or(0);

        let reverse = parsed.reverse || args.has_flag("R");
        let dry_run = parsed.dry_run || args.has_flag("dry-run");
        let explicit_file = parsed.file.clone().or_else(|| args.get_string("file", 0));

        // Parse the unified diff
        let hunks = match parse_unified_diff(&patch_content) {
            Ok(h) => h,
            Err(e) => return ExecResult::failure(1, format!("patch: {}", e)),
        };

        if hunks.is_empty() {
            return ExecResult::failure(1, "patch: no valid hunks found in input");
        }

        let mut output = String::new();
        let mut total_applied = 0;

        // Group hunks by target file
        for file_hunks in group_by_file(&hunks) {
            let target_path = if let Some(ref explicit) = explicit_file {
                explicit.clone()
            } else {
                strip_path(&file_hunks.target_file, strip_level)
            };

            let resolved_path = ctx.resolve_path(&target_path);
            let path = Path::new(&resolved_path);

            // Read current file content
            let current_content = match ctx.backend.read(path, None).await {
                Ok(data) => String::from_utf8_lossy(&data).into_owned(),
                Err(e) => {
                    return ExecResult::failure(
                        1,
                        format!("patch: cannot read '{}': {}", target_path, e),
                    );
                }
            };

            // Convert hunks to PatchOps
            let ops = match hunks_to_patch_ops(&file_hunks.hunks, &current_content, reverse) {
                Ok(ops) => ops,
                Err(e) => {
                    return ExecResult::failure(
                        1,
                        format!("patch: failed to apply to '{}': {}", target_path, e),
                    );
                }
            };

            if dry_run {
                output.push_str(&format!(
                    "patching file {} ({} changes)\n",
                    target_path,
                    ops.len()
                ));
                for op in &ops {
                    output.push_str(&format!("  {}\n", describe_patch_op(op)));
                }
            } else {
                // Apply the patch
                if let Err(e) = ctx.backend.patch(path, &ops).await {
                    return ExecResult::failure(
                        1,
                        format!("patch: failed to apply to '{}': {}", target_path, e),
                    );
                }
                output.push_str(&format!("patching file {}\n", target_path));
                total_applied += ops.len();
            }
        }

        if !dry_run && total_applied > 0 {
            output.push_str(&format!("{} changes applied\n", total_applied));
        }

        ExecResult::with_output(OutputData::text(output.trim_end()))
    }
}

/// A single hunk from a unified diff.
#[derive(Debug, Clone)]
struct DiffHunk {
    /// Original file start line (1-indexed)
    old_start: usize,
    /// Original file line count
    old_count: usize,
    /// New file start line (1-indexed)
    new_start: usize,
    /// New file line count
    new_count: usize,
    /// Lines in this hunk
    lines: Vec<DiffLine>,
}

/// A single line in a diff hunk.
#[derive(Debug, Clone)]
enum DiffLine {
    Context(String),
    Delete(String),
    Insert(String),
}

/// Hunks grouped by file.
struct FileHunks {
    target_file: String,
    hunks: Vec<DiffHunk>,
}

/// Parse unified diff format into hunks.
fn parse_unified_diff(content: &str) -> Result<Vec<FileHunks>, String> {
    let mut result: Vec<FileHunks> = Vec::new();
    let mut current_file: Option<String> = None;
    let mut current_hunks: Vec<DiffHunk> = Vec::new();
    let mut current_hunk: Option<DiffHunk> = None;

    for line in content.lines() {
        // Detect file header (--- and +++)
        if line.starts_with("--- ") {
            // Save previous file if any
            if let Some(file) = current_file.take() {
                if let Some(hunk) = current_hunk.take() {
                    current_hunks.push(hunk);
                }
                if !current_hunks.is_empty() {
                    result.push(FileHunks {
                        target_file: file,
                        hunks: std::mem::take(&mut current_hunks),
                    });
                }
            }
            // Parse will continue with +++ line
        } else if line.starts_with("+++ ") {
            // Extract target filename (after +++ )
            let path = line
                .strip_prefix("+++ ")
                .unwrap_or("")
                .split('\t')
                .next()
                .unwrap_or("")
                .to_string();
            current_file = Some(path);
        } else if line.starts_with("@@ ") {
            // Save previous hunk if any
            if let Some(hunk) = current_hunk.take() {
                current_hunks.push(hunk);
            }
            // Parse hunk header: @@ -old_start,old_count +new_start,new_count @@
            current_hunk = Some(parse_hunk_header(line)?);
        } else if let Some(ref mut hunk) = current_hunk {
            // Parse hunk content
            if let Some(rest) = line.strip_prefix('-') {
                hunk.lines.push(DiffLine::Delete(rest.to_string()));
            } else if let Some(rest) = line.strip_prefix('+') {
                hunk.lines.push(DiffLine::Insert(rest.to_string()));
            } else if let Some(rest) = line.strip_prefix(' ') {
                hunk.lines.push(DiffLine::Context(rest.to_string()));
            } else if line.is_empty() || line == "\\ No newline at end of file" {
                // Handle empty context line or no-newline marker
                if line.is_empty() {
                    hunk.lines.push(DiffLine::Context(String::new()));
                }
            }
        }
    }

    // Save final hunk and file
    if let Some(hunk) = current_hunk {
        current_hunks.push(hunk);
    }
    if let Some(file) = current_file
        && !current_hunks.is_empty() {
            result.push(FileHunks {
                target_file: file,
                hunks: current_hunks,
            });
        }

    Ok(result)
}

/// Parse a hunk header like "@@ -1,3 +1,4 @@" or "@@ -1 +1,2 @@".
fn parse_hunk_header(line: &str) -> Result<DiffHunk, String> {
    // Remove @@ prefix and suffix
    let content = line
        .strip_prefix("@@ ")
        .and_then(|s| s.split(" @@").next())
        .ok_or_else(|| format!("invalid hunk header: {}", line))?;

    // Split into old and new parts
    let parts: Vec<&str> = content.split_whitespace().collect();
    if parts.len() < 2 {
        return Err(format!("invalid hunk header: {}", line));
    }

    let (old_start, old_count) = parse_range(parts[0].strip_prefix('-').unwrap_or(parts[0]))?;
    let (new_start, new_count) = parse_range(parts[1].strip_prefix('+').unwrap_or(parts[1]))?;

    Ok(DiffHunk {
        old_start,
        old_count,
        new_start,
        new_count,
        lines: Vec::new(),
    })
}

/// Parse a range like "1,3" or just "1" (which means count of 1).
fn parse_range(s: &str) -> Result<(usize, usize), String> {
    if let Some((start, count)) = s.split_once(',') {
        let start: usize = start.parse().map_err(|_| format!("invalid number: {}", s))?;
        let count: usize = count.parse().map_err(|_| format!("invalid number: {}", s))?;
        Ok((start, count))
    } else {
        let start: usize = s.parse().map_err(|_| format!("invalid number: {}", s))?;
        Ok((start, 1))
    }
}

/// Strip leading path components from a path.
fn strip_path(path: &str, level: usize) -> String {
    if level == 0 {
        return path.to_string();
    }

    let components: Vec<&str> = path.split('/').collect();
    if level >= components.len() {
        components.last().unwrap_or(&path).to_string()
    } else {
        components[level..].join("/")
    }
}

/// Group parsed hunks by file.
fn group_by_file(file_hunks: &[FileHunks]) -> Vec<&FileHunks> {
    // Already grouped by parse_unified_diff, just return references
    file_hunks.iter().collect()
}

/// Convert diff hunks to PatchOp operations.
fn hunks_to_patch_ops(
    hunks: &[DiffHunk],
    content: &str,
    reverse: bool,
) -> Result<Vec<PatchOp>, String> {
    let lines: Vec<&str> = content.lines().collect();
    let mut ops: Vec<PatchOp> = Vec::new();

    // Track line offset as we apply operations
    let mut line_offset: isize = 0;

    for (hunk_idx, hunk) in hunks.iter().enumerate() {
        // Validate hunk line counts match actual content
        let actual_old = hunk.lines.iter()
            .filter(|l| matches!(l, DiffLine::Context(_) | DiffLine::Delete(_)))
            .count();
        let actual_new = hunk.lines.iter()
            .filter(|l| matches!(l, DiffLine::Context(_) | DiffLine::Insert(_)))
            .count();
        if actual_old != hunk.old_count || actual_new != hunk.new_count {
            return Err(format!(
                "hunk {}: line count mismatch (header says -{}/+{}, actual -{}/+{})",
                hunk_idx + 1, hunk.old_count, hunk.new_count, actual_old, actual_new
            ));
        }

        let start_line = if reverse {
            hunk.new_start
        } else {
            hunk.old_start
        };

        let mut current_line = start_line;

        for diff_line in &hunk.lines {
            // Adjust line number based on previous operations
            let adjusted_line = (current_line as isize + line_offset) as usize;

            match diff_line {
                DiffLine::Context(expected) => {
                    // Verify context matches
                    if adjusted_line > 0 && adjusted_line <= lines.len() {
                        let actual = lines.get(adjusted_line - 1).unwrap_or(&"");
                        if *actual != expected {
                            return Err(format!(
                                "context mismatch at line {}: expected '{}', found '{}'",
                                adjusted_line, expected, actual
                            ));
                        }
                    }
                    current_line += 1;
                }
                DiffLine::Delete(expected) => {
                    if reverse {
                        // In reverse mode, delete becomes insert
                        ops.push(PatchOp::InsertLine {
                            line: adjusted_line,
                            content: expected.clone(),
                        });
                        line_offset += 1;
                    } else {
                        // Normal mode: delete the line
                        ops.push(PatchOp::DeleteLine {
                            line: adjusted_line,
                            expected: Some(expected.clone()),
                        });
                        line_offset -= 1;
                    }
                    if !reverse {
                        current_line += 1;
                    }
                }
                DiffLine::Insert(content_line) => {
                    if reverse {
                        // In reverse mode, insert becomes delete
                        ops.push(PatchOp::DeleteLine {
                            line: adjusted_line,
                            expected: Some(content_line.clone()),
                        });
                        line_offset -= 1;
                        current_line += 1;
                    } else {
                        // Normal mode: insert the line
                        ops.push(PatchOp::InsertLine {
                            line: adjusted_line,
                            content: content_line.clone(),
                        });
                        line_offset += 1;
                    }
                }
            }
        }
    }

    Ok(ops)
}

/// Describe a PatchOp for dry-run output.
fn describe_patch_op(op: &PatchOp) -> String {
    match op {
        PatchOp::InsertLine { line, content } => {
            format!("+{}: {}", line, truncate(content, 40))
        }
        PatchOp::DeleteLine { line, expected } => {
            let content = expected.as_deref().unwrap_or("?");
            format!("-{}: {}", line, truncate(content, 40))
        }
        PatchOp::ReplaceLine { line, content, .. } => {
            format!("~{}: {}", line, truncate(content, 40))
        }
        PatchOp::Insert { offset, content } => {
            format!("+@{}: {}", offset, truncate(content, 40))
        }
        PatchOp::Delete { offset, len, .. } => {
            format!("-@{}..{}", offset, offset + len)
        }
        PatchOp::Replace { offset, len, content, .. } => {
            format!("~@{}..{}: {}", offset, offset + len, truncate(content, 40))
        }
        PatchOp::Append { content } => {
            format!("+$: {}", truncate(content, 40))
        }
    }
}

/// Truncate a string for display.
fn truncate(s: &str, max: usize) -> String {
    if s.len() <= max {
        s.to_string()
    } else {
        format!("{}...", &s[..max.saturating_sub(3)])
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::vfs::{Filesystem, MemoryFs, VfsRouter};
    use std::sync::Arc;

    async fn make_test_ctx() -> ExecContext {
        let mut vfs = VfsRouter::new();
        let mem = MemoryFs::new();

        // Create test file
        mem.write(Path::new("test.txt"), b"line1\nline2\nline3\n")
            .await
            .unwrap();

        vfs.mount("/", mem);
        ExecContext::new(Arc::new(vfs))
    }

    fn simple_patch() -> String {
        // Note: context lines must have a leading space!
        concat!(
            "--- a/test.txt\n",
            "+++ b/test.txt\n",
            "@@ -1,3 +1,3 @@\n",
            " line1\n",
            "-line2\n",
            "+modified\n",
            " line3\n",
        )
        .to_string()
    }

    #[tokio::test]
    async fn test_patch_apply() {
        let mut ctx = make_test_ctx().await;
        ctx.stdin = Some(simple_patch());

        let mut args = ToolArgs::new();
        // Strip 'b/' prefix from target path
        args.named.insert("p".to_string(), Value::Int(1));
        let result = Patch.execute(args, &mut ctx).await;

        assert!(result.ok(), "patch failed: {}", result.err);
        assert!(result.text_out().contains("patching file"));

        // Verify the file was modified
        let content = ctx.backend.read(Path::new("/test.txt"), None).await.unwrap();
        let text = String::from_utf8_lossy(&content);
        assert!(text.contains("modified"), "file not modified: {}", text);
        assert!(!text.contains("line2"), "old line still present");
    }

    #[tokio::test]
    async fn test_patch_dry_run() {
        let mut ctx = make_test_ctx().await;
        ctx.stdin = Some(simple_patch());

        let mut args = ToolArgs::new();
        args.named.insert("p".to_string(), Value::Int(1));
        args.flags.insert("dry-run".to_string());

        let result = Patch.execute(args, &mut ctx).await;

        assert!(result.ok(), "dry-run failed: {}", result.err);
        assert!(result.text_out().contains("changes"), "output: {}", result.text_out());

        // Verify the file was NOT modified
        let content = ctx.backend.read(Path::new("/test.txt"), None).await.unwrap();
        let text = String::from_utf8_lossy(&content);
        assert!(text.contains("line2"), "file was modified in dry-run mode");
    }

    #[tokio::test]
    async fn test_patch_reverse() {
        let mut ctx = make_test_ctx().await;

        // First apply the patch
        ctx.stdin = Some(simple_patch());
        let mut args = ToolArgs::new();
        args.named.insert("p".to_string(), Value::Int(1));
        Patch.execute(args, &mut ctx).await;

        // Then reverse it
        ctx.stdin = Some(simple_patch());
        let mut args = ToolArgs::new();
        args.named.insert("p".to_string(), Value::Int(1));
        args.flags.insert("R".to_string());

        let result = Patch.execute(args, &mut ctx).await;

        assert!(result.ok(), "reverse patch failed: {}", result.err);

        // Verify original content restored
        let content = ctx.backend.read(Path::new("/test.txt"), None).await.unwrap();
        let text = String::from_utf8_lossy(&content);
        assert!(text.contains("line2"), "original not restored: {}", text);
    }

    #[tokio::test]
    async fn test_patch_strip_path() {
        assert_eq!(strip_path("a/b/c/file.txt", 0), "a/b/c/file.txt");
        assert_eq!(strip_path("a/b/c/file.txt", 1), "b/c/file.txt");
        assert_eq!(strip_path("a/b/c/file.txt", 2), "c/file.txt");
        assert_eq!(strip_path("a/b/c/file.txt", 3), "file.txt");
        assert_eq!(strip_path("a/b/c/file.txt", 10), "file.txt");
    }

    #[tokio::test]
    async fn test_patch_no_input() {
        let mut ctx = make_test_ctx().await;
        // No stdin

        let args = ToolArgs::new();
        let result = Patch.execute(args, &mut ctx).await;

        assert!(!result.ok());
        assert!(result.err.contains("no input"));
    }

    #[test]
    fn test_parse_hunk_header() {
        let hunk = parse_hunk_header("@@ -1,3 +1,4 @@").unwrap();
        assert_eq!(hunk.old_start, 1);
        assert_eq!(hunk.old_count, 3);
        assert_eq!(hunk.new_start, 1);
        assert_eq!(hunk.new_count, 4);

        let hunk = parse_hunk_header("@@ -1 +1,2 @@").unwrap();
        assert_eq!(hunk.old_start, 1);
        assert_eq!(hunk.old_count, 1);
        assert_eq!(hunk.new_start, 1);
        assert_eq!(hunk.new_count, 2);
    }

    #[test]
    fn test_parse_unified_diff() {
        let patch = simple_patch();
        let files = parse_unified_diff(&patch).unwrap();

        assert_eq!(files.len(), 1);
        assert_eq!(files[0].target_file, "b/test.txt");
        assert_eq!(files[0].hunks.len(), 1);

        let hunk = &files[0].hunks[0];
        assert_eq!(hunk.old_start, 1);
        assert_eq!(hunk.lines.len(), 4); // context + delete + insert + context
    }

    #[test]
    fn test_hunk_count_mismatch_detected() {
        // Header claims 2 old lines but only 1 context + 0 deletes = 1 old line
        let bad_patch = concat!(
            "--- a/test.txt\n",
            "+++ b/test.txt\n",
            "@@ -1,2 +1,1 @@\n",
            " line1\n",
        );
        let files = parse_unified_diff(bad_patch).unwrap();
        let content = "line1\nline2\nline3\n";
        let result = hunks_to_patch_ops(&files[0].hunks, content, false);
        assert!(result.is_err(), "should reject mismatched hunk counts");
        let err = result.unwrap_err();
        assert!(
            err.contains("line count mismatch"),
            "error should mention mismatch: {}",
            err
        );
    }
}