atman-runtime 1.8.0

atman flow execution runtime: evaluator, tool dispatch, provider dispatch, executor, memory stores
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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
use std::path::PathBuf;
use std::time::Duration;

use git2::{BranchType, Commit, Diff, DiffFormat, Repository, Status, StatusOptions};

use crate::error::RuntimeError;
use crate::stream::StreamFrame;
use crate::tool::{ApprovalLevel, BoxFut, Tier, Tool, ToolArgs, ToolCtx, ToolResult};
use crate::value::Value;

pub struct GitStatus;

pub struct GitShow;

pub struct GitLog;

impl Tool for GitLog {
    fn name(&self) -> &str {
        "git.log"
    }

    fn tier(&self) -> Tier {
        Tier::Zero
    }

    fn description(&self) -> Option<&str> {
        Some("List recent commits and preview the patch for the newest commit.")
    }

    fn input_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "limit": {"type": "integer", "default": 20, "minimum": 1, "maximum": 100, "description": "Maximum commits to return."},
                "cwd": {"type": "string", "description": "Optional working dir; defaults to current process directory."}
            }
        })
    }

    fn call<'a>(&'a self, args: ToolArgs, ctx: &'a ToolCtx) -> BoxFut<'a, ToolResult> {
        Box::pin(async move {
            let limit = extract_optional_int(&args, "limit")
                .unwrap_or(20)
                .clamp(1, 100) as usize;
            let cwd = extract_cwd(&args, "git.log cwd")?;
            let repo = Repository::open(&cwd)
                .map_err(|e| RuntimeError::ToolFailed(format!("git.log: {e}")))?;
            let mut revwalk = repo
                .revwalk()
                .map_err(|e| RuntimeError::ToolFailed(format!("git.log revwalk: {e}")))?;
            revwalk
                .push_head()
                .map_err(|e| RuntimeError::ToolFailed(format!("git.log head: {e}")))?;

            let mut commits = Vec::new();
            let mut preview_diff = String::new();
            let mut preview_files = Vec::new();
            for oid in revwalk.take(limit) {
                let oid = oid.map_err(|e| RuntimeError::ToolFailed(format!("git.log oid: {e}")))?;
                let commit = repo
                    .find_commit(oid)
                    .map_err(|e| RuntimeError::ToolFailed(format!("git.log commit: {e}")))?;
                let diff = commit_diff(&repo, &commit, "git.log")?;
                let stats = diff
                    .stats()
                    .map_err(|e| RuntimeError::ToolFailed(format!("git.log stats: {e}")))?;
                if commits.is_empty() {
                    preview_files = diff_files(&diff, "git.log")?;
                    preview_diff = diff_patch(&diff, "git.log")?;
                }
                commits.push(commit_entry(&commit, &stats));
            }

            if let Some(tx) = &ctx.stream_tx
                && !preview_diff.is_empty()
            {
                let _ = tx.send(StreamFrame::DiffPreview {
                    title: "git log HEAD".into(),
                    old_content: None,
                    new_content: None,
                    unified_diff: Some(preview_diff.clone()),
                    run_id: ctx.flow_run_id.as_ref().map(|r| r.0.to_string()),
                });
            }

            Ok(Value::Struct(vec![
                ("commits".into(), Value::List(commits)),
                ("diff".into(), Value::Str(preview_diff)),
                (
                    "files".into(),
                    Value::List(preview_files.into_iter().map(Value::Str).collect()),
                ),
            ]))
        })
    }
}

impl Tool for GitShow {
    fn name(&self) -> &str {
        "git.show"
    }

    fn tier(&self) -> Tier {
        Tier::Zero
    }

    fn description(&self) -> Option<&str> {
        Some("Show the patch introduced by one commit.")
    }

    fn input_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "sha": {"type": "string", "description": "Commit SHA or rev."},
                "cwd": {"type": "string", "description": "Optional working dir; defaults to current process directory."}
            },
            "required": ["sha"]
        })
    }

    fn call<'a>(&'a self, args: ToolArgs, ctx: &'a ToolCtx) -> BoxFut<'a, ToolResult> {
        Box::pin(async move {
            let sha = extract_string(&args, "sha", 0)?;
            let cwd = extract_cwd(&args, "git.show cwd")?;
            let repo = Repository::open(&cwd)
                .map_err(|e| RuntimeError::ToolFailed(format!("git.show: {e}")))?;
            let object = repo
                .revparse_single(&sha)
                .map_err(|e| RuntimeError::ToolFailed(format!("git.show rev: {e}")))?;
            let commit = object
                .peel_to_commit()
                .map_err(|e| RuntimeError::ToolFailed(format!("git.show commit: {e}")))?;
            let diff = commit_diff(&repo, &commit, "git.show")?;
            let files = diff_files(&diff, "git.show")?;
            let body = diff_patch(&diff, "git.show")?;
            let resolved = commit.id().to_string();
            if let Some(tx) = &ctx.stream_tx {
                let _ = tx.send(StreamFrame::DiffPreview {
                    title: format!("git show {sha}"),
                    old_content: None,
                    new_content: None,
                    unified_diff: Some(body.clone()),
                    run_id: ctx.flow_run_id.as_ref().map(|r| r.0.to_string()),
                });
            }
            Ok(Value::Struct(vec![
                ("sha".into(), Value::Str(resolved)),
                ("diff".into(), Value::Str(body)),
                (
                    "files".into(),
                    Value::List(files.into_iter().map(Value::Str).collect()),
                ),
            ]))
        })
    }
}

impl Tool for GitStatus {
    fn name(&self) -> &str {
        "git.status"
    }

    fn tier(&self) -> Tier {
        Tier::Zero
    }

    fn description(&self) -> Option<&str> {
        Some("Show working tree status: staged, unstaged, and untracked files.")
    }

    fn input_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "cwd": {"type": "string", "description": "Optional working dir; defaults to current process directory."}
            }
        })
    }

    fn call<'a>(&'a self, args: ToolArgs, _ctx: &'a ToolCtx) -> BoxFut<'a, ToolResult> {
        Box::pin(async move {
            let cwd = extract_cwd(&args, "git.status cwd")?;
            let repo = Repository::open(&cwd)
                .map_err(|e| RuntimeError::ToolFailed(format!("git.status: {e}")))?;
            let mut opts = StatusOptions::new();
            opts.include_untracked(true)
                .renames_head_to_index(true)
                .renames_index_to_workdir(true);
            let statuses = repo
                .statuses(Some(&mut opts))
                .map_err(|e| RuntimeError::ToolFailed(format!("git.status: {e}")))?;
            let mut staged = Vec::new();
            let mut unstaged = Vec::new();
            let mut untracked = Vec::new();
            for entry in statuses.iter() {
                let status = entry.status();
                let Some(path) = entry.path().map(str::to_string) else {
                    continue;
                };
                if status.is_wt_new() {
                    untracked.push(Value::Str(path.clone()));
                }
                if let Some(label) = index_status(status) {
                    staged.push(status_entry(path.clone(), label));
                }
                if let Some(label) = worktree_status(status) {
                    unstaged.push(status_entry(path, label));
                }
            }
            Ok(Value::Struct(vec![
                ("staged".into(), Value::List(staged)),
                ("unstaged".into(), Value::List(unstaged)),
                ("untracked".into(), Value::List(untracked)),
            ]))
        })
    }
}

pub struct GitAdd;

impl Tool for GitAdd {
    fn name(&self) -> &str {
        "git.add"
    }

    fn tier(&self) -> Tier {
        Tier::Two
    }

    fn description(&self) -> Option<&str> {
        Some("Stage files for commit. Pass specific paths — do NOT stage everything blindly.")
    }

    fn input_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "paths": {"type": "array", "items": {"type": "string"}, "description": "File paths to stage."},
                "cwd": {"type": "string", "description": "Optional working dir."}
            },
            "required": ["paths"]
        })
    }

    fn call<'a>(&'a self, args: ToolArgs, _ctx: &'a ToolCtx) -> BoxFut<'a, ToolResult> {
        Box::pin(async move {
            let paths = extract_string_list(&args, "paths")?;
            let cwd = extract_cwd(&args, "git.add cwd")?;
            let repo = Repository::open(&cwd)
                .map_err(|e| RuntimeError::ToolFailed(format!("git.add: {e}")))?;
            let mut index = repo
                .index()
                .map_err(|e| RuntimeError::ToolFailed(format!("git.add index: {e}")))?;
            for p in &paths {
                index
                    .add_path(std::path::Path::new(p))
                    .map_err(|e| RuntimeError::ToolFailed(format!("git.add {p}: {e}")))?;
            }
            index
                .write()
                .map_err(|e| RuntimeError::ToolFailed(format!("git.add write: {e}")))?;
            Ok(Value::Struct(vec![(
                "staged".into(),
                Value::List(paths.into_iter().map(Value::Str).collect()),
            )]))
        })
    }
}

pub struct GitCommit;

impl Tool for GitCommit {
    fn name(&self) -> &str {
        "git.commit"
    }

    fn tier(&self) -> Tier {
        Tier::Two
    }

    fn description(&self) -> Option<&str> {
        Some("Commit staged changes. Use 'amend: true' to amend the last commit.")
    }

    fn input_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "message": {"type": "string", "description": "Commit message."},
                "amend": {"type": "boolean", "default": false, "description": "Amend the last commit instead of creating a new commit."},
                "cwd": {"type": "string", "description": "Optional working dir; defaults to current process directory."}
            },
            "required": ["message"]
        })
    }

    fn call<'a>(&'a self, args: ToolArgs, _ctx: &'a ToolCtx) -> BoxFut<'a, ToolResult> {
        Box::pin(async move {
            let message = extract_string(&args, "message", 0)?;
            let amend = extract_optional_bool(&args, "amend").unwrap_or(false);
            let cwd = extract_cwd(&args, "git.commit cwd")?;
            let repo = Repository::open(&cwd)
                .map_err(|e| RuntimeError::ToolFailed(format!("git.commit: {e}")))?;
            let files_count = staged_count(&repo, "git.commit")?;
            let mut index = repo
                .index()
                .map_err(|e| RuntimeError::ToolFailed(format!("git.commit: {e}")))?;
            let tree_id = index
                .write_tree()
                .map_err(|e| RuntimeError::ToolFailed(format!("git.commit: {e}")))?;
            let tree = repo
                .find_tree(tree_id)
                .map_err(|e| RuntimeError::ToolFailed(format!("git.commit: {e}")))?;
            let sig = repo
                .signature()
                .map_err(|e| RuntimeError::ToolFailed(format!("git.commit signature: {e}")))?;
            let head = repo
                .head()
                .map_err(|e| RuntimeError::ToolFailed(format!("git.commit head: {e}")))?;
            let parent = head
                .peel_to_commit()
                .map_err(|e| RuntimeError::ToolFailed(format!("git.commit head: {e}")))?;
            let oid = if amend {
                parent
                    .amend(
                        Some("HEAD"),
                        Some(&sig),
                        Some(&sig),
                        None,
                        Some(&message),
                        Some(&tree),
                    )
                    .map_err(|e| RuntimeError::ToolFailed(format!("git.commit amend: {e}")))?
            } else {
                repo.commit(Some("HEAD"), &sig, &sig, &message, &tree, &[&parent])
                    .map_err(|e| RuntimeError::ToolFailed(format!("git.commit: {e}")))?
            };
            index
                .write()
                .map_err(|e| RuntimeError::ToolFailed(format!("git.commit index: {e}")))?;
            Ok(Value::Struct(vec![
                ("sha".into(), Value::Str(oid.to_string())),
                ("message".into(), Value::Str(message)),
                ("files_count".into(), Value::Int(files_count)),
            ]))
        })
    }
}

pub struct GitBranch;

impl Tool for GitBranch {
    fn name(&self) -> &str {
        "git.branch"
    }

    fn tier(&self) -> Tier {
        Tier::Two
    }

    fn description(&self) -> Option<&str> {
        Some("Create and/or checkout a git branch.")
    }

    fn input_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "name": {"type": "string", "description": "Branch name."},
                "create": {"type": "boolean", "default": true, "description": "Create the branch before checkout."},
                "checkout": {"type": "boolean", "default": true, "description": "Checkout the branch."},
                "cwd": {"type": "string", "description": "Optional working dir; defaults to current process directory."}
            },
            "required": ["name"]
        })
    }

    fn call<'a>(&'a self, args: ToolArgs, _ctx: &'a ToolCtx) -> BoxFut<'a, ToolResult> {
        Box::pin(async move {
            let name = extract_string(&args, "name", 0)?;
            let create = extract_optional_bool(&args, "create").unwrap_or(true);
            let checkout = extract_optional_bool(&args, "checkout").unwrap_or(true);
            let cwd = extract_cwd(&args, "git.branch cwd")?;
            let repo = Repository::open(&cwd)
                .map_err(|e| RuntimeError::ToolFailed(format!("git.branch: {e}")))?;
            if create {
                let head = repo
                    .head()
                    .and_then(|h| h.peel_to_commit())
                    .map_err(|e| RuntimeError::ToolFailed(format!("git.branch head: {e}")))?;
                repo.branch(&name, &head, false)
                    .map_err(|e| RuntimeError::ToolFailed(format!("git.branch: {e}")))?;
            } else {
                repo.find_branch(&name, BranchType::Local)
                    .map_err(|e| RuntimeError::ToolFailed(format!("git.branch: {e}")))?;
            }
            if checkout {
                repo.set_head(&format!("refs/heads/{name}"))
                    .map_err(|e| RuntimeError::ToolFailed(format!("git.branch checkout: {e}")))?;
            }
            Ok(Value::Struct(vec![
                ("branch".into(), Value::Str(name)),
                ("created".into(), Value::Bool(create)),
                ("checked_out".into(), Value::Bool(checkout)),
            ]))
        })
    }
}

pub struct GitPush;

impl Tool for GitPush {
    fn name(&self) -> &str {
        "git.push"
    }

    fn tier(&self) -> Tier {
        Tier::Three
    }

    fn approval_level(&self, _args: &ToolArgs, _ctx: &ToolCtx) -> ApprovalLevel {
        ApprovalLevel::Dangerous
    }

    fn description(&self) -> Option<&str> {
        Some("Push current branch to remote. Requires approval.")
    }

    fn input_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "remote": {"type": "string", "default": "origin", "description": "Remote name."},
                "branch": {"type": "string", "description": "Branch name; defaults to current branch."},
                "cwd": {"type": "string", "description": "Optional working dir; defaults to current process directory."}
            }
        })
    }

    fn call<'a>(&'a self, args: ToolArgs, _ctx: &'a ToolCtx) -> BoxFut<'a, ToolResult> {
        Box::pin(async move {
            let remote =
                extract_optional_string(&args, "remote").unwrap_or_else(|| "origin".into());
            let cwd = extract_cwd(&args, "git.push cwd")?;
            let branch = match extract_optional_string(&args, "branch") {
                Some(branch) => branch,
                None => current_branch(&cwd)?,
            };
            let mut child = tokio::process::Command::new("git");
            child.args(["push", &remote, &branch]).current_dir(&cwd);
            let output = tokio::time::timeout(Duration::from_secs(300), child.output())
                .await
                .map_err(|_| RuntimeError::ToolFailed("git.push timeout after 300s".into()))?
                .map_err(|e| RuntimeError::ToolFailed(format!("git.push spawn: {e}")))?;
            let stdout = String::from_utf8_lossy(&output.stdout);
            let stderr = String::from_utf8_lossy(&output.stderr);
            let combined = match (stdout.is_empty(), stderr.is_empty()) {
                (true, true) => String::new(),
                (false, true) => stdout.into_owned(),
                (true, false) => stderr.into_owned(),
                (false, false) => format!("{stdout}\n{stderr}"),
            };
            Ok(Value::Struct(vec![
                ("ok".into(), Value::Bool(output.status.success())),
                ("remote".into(), Value::Str(remote)),
                ("branch".into(), Value::Str(branch)),
                ("output".into(), Value::Str(combined)),
            ]))
        })
    }
}

fn extract_cwd(args: &ToolArgs, label: &str) -> Result<PathBuf, RuntimeError> {
    match args.named("cwd") {
        Some(Value::Path(p)) => Ok(p.clone()),
        Some(Value::Str(s)) => Ok(PathBuf::from(s)),
        Some(other) => Err(RuntimeError::TypeMismatch {
            expected: "string".into(),
            actual: other.kind_name().into(),
        }),
        None => {
            std::env::current_dir().map_err(|e| RuntimeError::ToolFailed(format!("{label}: {e}")))
        }
    }
}

fn extract_string(args: &ToolArgs, name: &str, pos: usize) -> Result<String, RuntimeError> {
    let value = match args.named(name) {
        Some(v) => v,
        None => args.positional(pos)?,
    };
    match value {
        Value::Str(s) => Ok(s.clone()),
        other => Err(RuntimeError::TypeMismatch {
            expected: "string".into(),
            actual: other.kind_name().into(),
        }),
    }
}

fn extract_string_list(args: &ToolArgs, name: &str) -> Result<Vec<String>, RuntimeError> {
    match args.named(name) {
        Some(Value::List(items)) => items
            .iter()
            .map(|v| match v {
                Value::Str(s) => Ok(s.clone()),
                other => Err(RuntimeError::TypeMismatch {
                    expected: "string".into(),
                    actual: other.kind_name().into(),
                }),
            })
            .collect(),
        Some(other) => Err(RuntimeError::TypeMismatch {
            expected: "list<string>".into(),
            actual: other.kind_name().into(),
        }),
        None => Err(RuntimeError::MissingArg(name.into())),
    }
}

fn extract_optional_string(args: &ToolArgs, name: &str) -> Option<String> {
    match args.named(name)? {
        Value::Str(s) => Some(s.clone()),
        _ => None,
    }
}

fn extract_optional_bool(args: &ToolArgs, name: &str) -> Option<bool> {
    match args.named(name)? {
        Value::Bool(b) => Some(*b),
        _ => None,
    }
}

fn extract_optional_int(args: &ToolArgs, name: &str) -> Option<i64> {
    match args.named(name)? {
        Value::Int(n) => Some(*n),
        _ => None,
    }
}

fn commit_diff<'repo>(
    repo: &'repo Repository,
    commit: &Commit<'repo>,
    tool: &str,
) -> Result<Diff<'repo>, RuntimeError> {
    let new_tree = commit
        .tree()
        .map_err(|e| RuntimeError::ToolFailed(format!("{tool} tree: {e}")))?;
    let old_tree = if commit.parent_count() == 0 {
        None
    } else {
        Some(
            commit
                .parent(0)
                .and_then(|p| p.tree())
                .map_err(|e| RuntimeError::ToolFailed(format!("{tool} parent: {e}")))?,
        )
    };
    repo.diff_tree_to_tree(old_tree.as_ref(), Some(&new_tree), None)
        .map_err(|e| RuntimeError::ToolFailed(format!("{tool} diff: {e}")))
}

fn diff_files(diff: &Diff<'_>, tool: &str) -> Result<Vec<String>, RuntimeError> {
    let mut files = Vec::new();
    diff.foreach(
        &mut |delta, _| {
            let path = delta
                .new_file()
                .path()
                .or_else(|| delta.old_file().path())
                .map(|p| p.to_string_lossy().into_owned());
            if let Some(path) = path
                && !files.contains(&path)
            {
                files.push(path);
            }
            true
        },
        None,
        None,
        None,
    )
    .map_err(|e| RuntimeError::ToolFailed(format!("{tool} files: {e}")))?;
    Ok(files)
}

fn diff_patch(diff: &Diff<'_>, tool: &str) -> Result<String, RuntimeError> {
    let mut body = String::new();
    diff.print(DiffFormat::Patch, |_delta, _hunk, line| {
        match line.origin() {
            'F' | 'H' => body.push_str(&String::from_utf8_lossy(line.content())),
            '+' | '-' | ' ' => {
                body.push(line.origin());
                body.push_str(&String::from_utf8_lossy(line.content()));
            }
            _ => body.push_str(&String::from_utf8_lossy(line.content())),
        }
        true
    })
    .map_err(|e| RuntimeError::ToolFailed(format!("{tool} patch: {e}")))?;
    Ok(body)
}

fn commit_entry(commit: &Commit<'_>, stats: &git2::DiffStats) -> Value {
    let author = commit.author();
    let author_name = author.name().unwrap_or_default();
    let author_email = author.email().unwrap_or_default();
    let author_display = if author_email.is_empty() {
        author_name.to_string()
    } else if author_name.is_empty() {
        author_email.to_string()
    } else {
        format!("{author_name} <{author_email}>")
    };
    Value::Struct(vec![
        ("sha".into(), Value::Str(commit.id().to_string())),
        ("author".into(), Value::Str(author_display)),
        (
            "date".into(),
            Value::Str(commit.time().seconds().to_string()),
        ),
        (
            "message".into(),
            Value::Str(commit.summary().unwrap_or_default().to_string()),
        ),
        (
            "stats".into(),
            Value::Struct(vec![
                ("files".into(), Value::Int(stats.files_changed() as i64)),
                ("insertions".into(), Value::Int(stats.insertions() as i64)),
                ("deletions".into(), Value::Int(stats.deletions() as i64)),
            ]),
        ),
    ])
}

fn index_status(status: Status) -> Option<&'static str> {
    if status.is_index_new() {
        Some("new")
    } else if status.is_index_modified() {
        Some("modified")
    } else if status.is_index_deleted() {
        Some("deleted")
    } else if status.is_index_renamed() {
        Some("renamed")
    } else {
        None
    }
}

fn worktree_status(status: Status) -> Option<&'static str> {
    if status.is_wt_modified() {
        Some("modified")
    } else if status.is_wt_deleted() {
        Some("deleted")
    } else if status.is_wt_renamed() {
        Some("renamed")
    } else {
        None
    }
}

fn status_entry(path: String, status: &str) -> Value {
    Value::Struct(vec![
        ("path".into(), Value::Str(path)),
        ("status".into(), Value::Str(status.into())),
    ])
}

fn staged_count(repo: &Repository, tool: &str) -> Result<i64, RuntimeError> {
    let mut opts = StatusOptions::new();
    opts.include_untracked(false).renames_head_to_index(true);
    let statuses = repo
        .statuses(Some(&mut opts))
        .map_err(|e| RuntimeError::ToolFailed(format!("{tool}: {e}")))?;
    Ok(statuses
        .iter()
        .filter(|entry| index_status(entry.status()).is_some())
        .count() as i64)
}

fn current_branch(cwd: &std::path::Path) -> Result<String, RuntimeError> {
    let repo =
        Repository::open(cwd).map_err(|e| RuntimeError::ToolFailed(format!("git.push: {e}")))?;
    let head = repo
        .head()
        .map_err(|e| RuntimeError::ToolFailed(format!("git.push head: {e}")))?;
    head.shorthand()
        .map(str::to_string)
        .ok_or_else(|| RuntimeError::ToolFailed("git.push: detached HEAD has no branch".into()))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::git::GitCli;
    use std::path::Path;

    fn have_git() -> bool {
        GitCli::ensure_available().is_ok()
    }

    fn seed_two_commits(dir: &Path) {
        let cli = GitCli::at(dir);
        cli.init("main").unwrap();
        for (k, v) in [
            ("user.email", "t@atman.local"),
            ("user.name", "atman test"),
            ("commit.gpgsign", "false"),
        ] {
            cli.run(&["config", k, v]).unwrap();
        }
        std::fs::write(dir.join("a.txt"), "one\n").unwrap();
        cli.add_all().unwrap();
        cli.commit("initial").unwrap();
        std::fs::write(dir.join("a.txt"), "one\ntwo\n").unwrap();
        cli.add_all().unwrap();
        cli.commit("second").unwrap();
    }

    #[tokio::test]
    async fn log_returns_limited_commits_and_head_patch() {
        if !have_git() {
            eprintln!("skip: git not on PATH");
            return;
        }
        let tmp = tempfile::tempdir().unwrap();
        seed_two_commits(tmp.path());
        let ctx = ToolCtx::new();
        let args = ToolArgs {
            positional: Vec::new(),
            named: vec![
                ("limit".into(), Value::Int(1)),
                (
                    "cwd".into(),
                    Value::Str(tmp.path().to_string_lossy().into()),
                ),
            ],
        };

        let value = GitLog.call(args, &ctx).await.unwrap();
        let commits = value.field("commits").unwrap();
        let Value::List(commits) = commits else {
            panic!("expected commits list: {commits:?}");
        };
        assert_eq!(commits.len(), 1);
        let head = &commits[0];
        assert!(matches!(head.field("message"), Some(Value::Str(s)) if s == "second"));
        assert!(matches!(head.field("sha"), Some(Value::Str(sha)) if sha.len() == 40));
        let stats = head.field("stats").unwrap();
        assert!(matches!(stats.field("files"), Some(Value::Int(1))));
        assert!(matches!(stats.field("insertions"), Some(Value::Int(1))));
        let diff = value.field("diff").unwrap();
        assert!(
            matches!(diff, Value::Str(s) if s.contains("+two")),
            "diff={diff:?}"
        );
    }
}