choreo-daemon 0.1.0

Agentic coding assistant — daemon, TUI, and bridges
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
use gix::{
    ObjectId,
    bstr::BString,
    prelude::ObjectIdExt,
    progress::Discard,
    status::{Item as StatusItem, UntrackedFiles},
    worktree::IndexPersistedOrInMemory,
};
use std::{
    fmt::Write as _,
    io,
    path::{Path, PathBuf},
};

use super::ToolError;
use zlob::{ZlobFlags, ZlobPattern};

mod commit;
mod diff;
mod log;
mod push;
mod show;
mod stage;
mod status;

pub(crate) use commit::GitCommit;
pub use commit::{GitCommitArgs, execute_git_commit_tool};
pub(crate) use diff::{GitDiff, append_fenced_diff};
pub use diff::{GitDiffArgs, execute_git_diff_tool};
pub(crate) use log::GitLog;
pub use log::{GitLogArgs, execute_git_log_tool};
pub(crate) use push::GitPush;
pub use push::{GitPushArgs, execute_git_push_tool};
pub(crate) use show::GitShow;
pub use show::{GitShowArgs, execute_git_show_tool};
pub(crate) use stage::GitAdd;
pub use stage::{GitAddArgs, execute_git_add_tool};
pub(crate) use status::GitStatus;
pub use status::{GitRepoArgs, execute_git_status_tool};

pub(crate) fn open_repo(
    repo_path: Option<&str>,
    working_dir: Option<&std::path::Path>,
) -> Result<gix::Repository, ToolError> {
    let path = repo_path.unwrap_or(".").trim();
    let path = if path.is_empty() { "." } else { path };
    let resolved = super::resolve_path(path, working_dir);
    gix::discover(&resolved).map_err(|error| {
        ToolError::Other(format!(
            "failed to open git repository from {}: {error}",
            resolved.display()
        ))
    })
}

pub(crate) fn repo_work_dir(repo: &gix::Repository) -> &Path {
    repo.workdir().unwrap_or_else(|| repo.git_dir())
}

pub(crate) fn repo_work_dir_display(repo: &gix::Repository) -> String {
    repo_work_dir(repo).display().to_string()
}

pub(crate) fn describe_head(repo: &gix::Repository) -> Result<String, ToolError> {
    if let Some(name) = repo.head_name().map_err(io::Error::other)? {
        return Ok(name.shorten().to_string());
    }
    match repo.head_id() {
        Ok(id) => Ok(format!("detached at {}", shorten_id(repo, id.detach())?)),
        Err(_) => Ok("unborn HEAD".to_string()),
    }
}

pub(crate) fn shorten_id(repo: &gix::Repository, id: ObjectId) -> Result<String, ToolError> {
    Ok(id
        .attach(repo)
        .shorten()
        .map_err(io::Error::other)?
        .to_string())
}

pub(crate) fn path_from_bytes(path: &[u8]) -> String {
    String::from_utf8_lossy(path).into_owned()
}

pub(crate) fn sort_and_dedup(lines: &mut Vec<String>) {
    lines.sort();
    lines.dedup();
}

pub(crate) fn write_section(out: &mut String, title: &str, lines: &[String]) {
    let _ = writeln!(out, "{title}:");
    if lines.is_empty() {
        let _ = writeln!(out, "  (none)");
        return;
    }
    for line in lines {
        let _ = writeln!(out, "  {line}");
    }
}

pub(crate) fn yes_no(value: bool) -> &'static str {
    if value { "yes" } else { "no" }
}

pub(crate) fn append_command_output(out: &mut String, label: &str, content: &str) {
    if content.is_empty() {
        return;
    }
    let _ = writeln!(out);
    let _ = writeln!(out, "{label}:");
    let _ = writeln!(out, "{content}");
}

pub(crate) fn run_git_command(
    repo: &gix::Repository,
    args: &[String],
) -> Result<std::process::Output, ToolError> {
    std::process::Command::new("git")
        .args(args)
        .current_dir(repo_work_dir(repo))
        .output()
        .map_err(|error| ToolError::Other(format!("failed to run git {}: {error}", args.join(" "))))
}

pub(crate) fn normalize_nonempty_argument<'a>(
    value: &'a str,
    name: &str,
) -> Result<&'a str, ToolError> {
    let value = value.trim();
    if value.is_empty() {
        Err(ToolError::Other(format!("{name} must not be empty")))
    } else {
        Ok(value)
    }
}

pub(crate) fn current_branch_name(repo: &gix::Repository) -> Result<String, ToolError> {
    repo.head_name()
        .map_err(io::Error::other)?
        .map(|name| name.shorten().to_string())
        .ok_or_else(|| {
            ToolError::Other("branch must be provided when HEAD is detached".to_string())
        })
}

pub(crate) fn load_mutable_index(repo: &gix::Repository) -> Result<gix::index::File, ToolError> {
    match repo
        .index_or_load_from_head_or_empty()
        .map_err(io::Error::other)?
    {
        IndexPersistedOrInMemory::Persisted(index) => Ok((**index).clone()),
        IndexPersistedOrInMemory::InMemory(index) => Ok(index),
    }
}

pub(crate) fn collect_cached_diff_lines(
    repo: &gix::Repository,
    pathspec: &[String],
) -> Result<Vec<String>, ToolError> {
    let iter = repo
        .status(Discard)
        .map_err(io::Error::other)?
        .untracked_files(UntrackedFiles::None)
        .into_iter(Vec::<BString>::new())
        .map_err(io::Error::other)?;

    let mut lines = Vec::new();
    for item in iter {
        let item = item.map_err(io::Error::other)?;
        if let StatusItem::TreeIndex(change) = item {
            let path = path_from_bytes(change.location().as_ref());
            if pathspec_matches(pathspec, &path) {
                lines.push(format_tree_index_change(&change));
            }
        }
    }
    Ok(lines)
}

pub(crate) fn pathspec_patterns(pathspec: &[String]) -> Vec<BString> {
    pathspec
        .iter()
        .map(|spec| BString::from(spec.as_str()))
        .collect()
}

pub(crate) fn pathspec_matches(pathspec: &[String], path: &str) -> bool {
    if pathspec.is_empty() {
        return true;
    }
    pathspec.iter().any(|spec| {
        let spec = spec.trim();
        !spec.is_empty()
            && (path == spec
                || path.starts_with(spec.strip_suffix('/').unwrap_or(spec))
                || simple_glob_matches(spec, path))
    })
}

pub(crate) fn simple_glob_matches(pattern: &str, text: &str) -> bool {
    // Early-out for patterns without glob wildcards — these are handled by
    // the exact-match and prefix-match checks in pathspec_matches.
    if !zlob::has_wildcards(pattern, ZlobFlags::RECOMMENDED) {
        return false;
    }
    ZlobPattern::compile(pattern, ZlobFlags::RECOMMENDED)
        .map(|p| p.matches_default(text))
        .unwrap_or(false)
}

/// Given a repo, an optional explicit `repo_path`, and the session
/// `working_dir`, compute the relative path from the repo worktree root
/// to the directory that pathspecs are relative to.
///
/// Returns `None` when no prefix is needed (pathspecs are already
/// repo-root-relative).
///
/// This is used during path-prefixing for `git_add` and `git_diff` so
/// that pathspecs provided relative to the session working directory
/// (or an explicit subdirectory path) are correctly remapped to the repo
/// root before being passed to gix's pathspec matching.
pub(crate) fn resolve_pathspec_prefix(
    repo: &gix::Repository,
    repo_path: Option<&str>,
    working_dir: Option<&Path>,
) -> Result<Option<String>, ToolError> {
    let Some(workdir) = repo.workdir() else {
        return Ok(None);
    };

    // Canonicalize the worktree root too: `working_dir` below is canonical,
    // and gix's `workdir()` returns the path exactly as it was stored at
    // discovery (no symlink resolution). On macOS `/var` is a symlink to
    // `/private/var`, so comparing the two raw paths would make `strip_prefix`
    // below silently fail and return `None` for every subdirectory.
    let workdir = workdir
        .canonicalize()
        .unwrap_or_else(|_| workdir.to_path_buf());

    // Canonicalize working_dir once so all path resolution below is
    // consistent with repo.workdir() (which always returns the real,
    // canonical path).  This handles symlinks (e.g. macOS /var → /private/var).
    let working_dir = match working_dir {
        Some(wd) => Some(wd.canonicalize().map_err(|e| {
            ToolError::Other(format!(
                "failed to canonicalize working directory '{}': {e}",
                wd.display()
            ))
        })?),
        None => None,
    };

    let base: PathBuf = match repo_path {
        Some(rp) => {
            let trimmed = rp.trim();
            if trimmed.is_empty() || trimmed == "." {
                return Ok(None);
            }
            let candidate = Path::new(trimmed);
            if candidate.is_absolute() {
                candidate.to_path_buf()
            } else if let Some(wd) = working_dir {
                wd.join(candidate)
            } else {
                candidate.to_path_buf()
            }
        }
        None => match working_dir {
            Some(wd) => wd,
            None => return Ok(None),
        },
    };

    // Canonicalize `base` as well so it is on the same footing as the
    // canonicalized `workdir` above: an absolute `repo_path` (or one joined
    // onto a raw working dir) may carry the unresolved `/var` → `/private/var`
    // prefix, which would otherwise make `strip_prefix` fail. Falls back to
    // the raw path when it doesn't exist yet (callers can path-prefix
    // not-yet-created paths).
    let base = base.canonicalize().unwrap_or(base);

    let Ok(prefix) = base.strip_prefix(workdir) else {
        return Ok(None);
    };
    if prefix.as_os_str().is_empty() {
        return Ok(None);
    }

    Ok(Some(
        prefix
            .components()
            .map(|component| component.as_os_str().to_string_lossy())
            .collect::<Vec<_>>()
            .join("/"),
    ))
}

/// Filter out the special `"."` and `"./"` pathspecs since gix does not interpret
/// them the way real git does ("everything in the current directory").  With an
/// empty pathspec list, gix matches all files, which is the correct behaviour.
///
/// Called by both `git_diff` and `git_add` when the user's working directory
/// coincides with the repo root (no prefix to prepend).
pub(crate) fn filter_repo_root_pathspecs(pathspec: Vec<String>) -> Vec<String> {
    pathspec
        .into_iter()
        .filter(|spec| spec != "." && spec != "./")
        .collect()
}

pub(crate) fn format_tree_index_change(change: &gix::diff::index::Change) -> String {
    use gix::diff::index::ChangeRef;
    match change {
        ChangeRef::Addition { location, .. } => format!("A {}", path_from_bytes(location.as_ref())),
        ChangeRef::Deletion { location, .. } => format!("D {}", path_from_bytes(location.as_ref())),
        ChangeRef::Modification {
            location,
            previous_entry_mode,
            entry_mode,
            ..
        } => {
            let prefix = if previous_entry_mode != entry_mode {
                "T"
            } else {
                "M"
            };
            format!("{prefix} {}", path_from_bytes(location.as_ref()))
        }
        ChangeRef::Rewrite {
            source_location,
            location,
            copy,
            ..
        } => {
            let from = path_from_bytes(source_location.as_ref());
            let to = path_from_bytes(location.as_ref());
            if *copy {
                format!("C {from} -> {to}")
            } else {
                format!("R {from} -> {to}")
            }
        }
    }
}

pub(crate) fn format_index_worktree_change(change: &gix::status::index_worktree::Item) -> String {
    use gix::status::index_worktree::Item;
    match change {
        Item::Modification { .. } => match change.summary() {
            Some(summary) => format!(
                "{} {}",
                worktree_summary_code(summary),
                path_from_bytes(change.rela_path().as_ref())
            ),
            None => format!("M {}", path_from_bytes(change.rela_path().as_ref())),
        },
        Item::DirectoryContents { entry, .. } => {
            let path = path_from_bytes(entry.rela_path.as_ref());
            if matches!(entry.status, gix::dir::entry::Status::Untracked) {
                format!("?? {path}")
            } else {
                format!("DIR {path}")
            }
        }
        Item::Rewrite { source, copy, .. } => {
            let from = path_from_bytes(source.rela_path().as_ref());
            let to = path_from_bytes(change.rela_path().as_ref());
            if *copy {
                format!("C {from} -> {to}")
            } else {
                format!("R {from} -> {to}")
            }
        }
    }
}

pub(crate) fn worktree_summary_code(
    summary: gix::status::index_worktree::iter::Summary,
) -> &'static str {
    use gix::status::index_worktree::iter::Summary;
    match summary {
        Summary::Added => "A",
        Summary::Removed => "D",
        Summary::Modified => "M",
        Summary::Copied => "C",
        Summary::Renamed => "R",
        Summary::TypeChange => "T",
        Summary::Conflict => "U",
        Summary::IntentToAdd => "I",
    }
}

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

    /// Create a temporary git repository with a subdirectory at `sub_path`
    /// relative to the repo root, and return the tempdir, subdirectory path,
    /// and opened gix repository.
    fn init_repo_with_subdir(sub_path: &str) -> (tempfile::TempDir, PathBuf, gix::Repository) {
        let tmp = tempfile::tempdir().unwrap();
        let repo_root = tmp.path();

        // Initialize a bare-bones git repo.
        std::process::Command::new("git")
            .args(["init", "-q"])
            .current_dir(repo_root)
            .output()
            .unwrap();

        // Configure a minimal user so commits work.
        std::process::Command::new("git")
            .args(["config", "user.email", "test@test"])
            .current_dir(repo_root)
            .output()
            .unwrap();
        std::process::Command::new("git")
            .args(["config", "user.name", "test"])
            .current_dir(repo_root)
            .output()
            .unwrap();

        // Create the subdirectory.
        let sub = repo_root.join(sub_path);
        std::fs::create_dir_all(&sub).unwrap();

        let repo = gix::discover(repo_root).unwrap();
        (tmp, sub, repo)
    }

    #[test]
    fn test_resolve_pathspec_prefix_none_when_working_dir_is_repo_root() {
        let (_tmp, _sub, repo) = init_repo_with_subdir("sub");
        let workdir = repo.workdir().unwrap().to_path_buf();
        let result = resolve_pathspec_prefix(&repo, None, Some(&workdir)).unwrap();
        // Working dir = repo root → no prefix needed.
        assert_eq!(result, None);
    }

    #[test]
    fn test_resolve_pathspec_prefix_returns_subdir_prefix() {
        let (_tmp, sub, repo) = init_repo_with_subdir("sub");
        let result = resolve_pathspec_prefix(&repo, None, Some(&sub)).unwrap();
        assert_eq!(result.as_deref(), Some("sub"));
    }

    #[test]
    fn test_resolve_pathspec_prefix_nested_subdir() {
        let (_tmp, sub, repo) = init_repo_with_subdir("a/b/c");
        let result = resolve_pathspec_prefix(&repo, None, Some(&sub)).unwrap();
        assert_eq!(result.as_deref(), Some("a/b/c"));
    }

    #[test]
    fn test_resolve_pathspec_prefix_none_when_no_working_dir() {
        let (_tmp, _sub, repo) = init_repo_with_subdir("sub");
        let result = resolve_pathspec_prefix(&repo, None, None).unwrap();
        assert_eq!(result, None);
    }

    #[test]
    fn test_resolve_pathspec_prefix_with_explicit_repo_path() {
        let (_tmp, _sub, repo) = init_repo_with_subdir("sub");
        let workdir = repo.workdir().unwrap().to_path_buf();
        // Pass repo_path = "sub" explicitly — should resolve to the same prefix.
        let result = resolve_pathspec_prefix(&repo, Some("sub"), Some(&workdir)).unwrap();
        assert_eq!(result.as_deref(), Some("sub"));
    }

    #[test]
    fn test_resolve_pathspec_prefix_empty_repo_path_returns_none() {
        let (_tmp, _sub, repo) = init_repo_with_subdir("sub");
        let workdir = repo.workdir().unwrap().to_path_buf();
        let result = resolve_pathspec_prefix(&repo, Some(""), Some(&workdir)).unwrap();
        assert_eq!(result, None);
    }

    #[test]
    fn test_resolve_pathspec_prefix_dot_repo_path_returns_none() {
        let (_tmp, _sub, repo) = init_repo_with_subdir("sub");
        let workdir = repo.workdir().unwrap().to_path_buf();
        let result = resolve_pathspec_prefix(&repo, Some("."), Some(&workdir)).unwrap();
        assert_eq!(result, None);
    }

    #[test]
    fn test_resolve_pathspec_prefix_absolute_repo_path() {
        let (_tmp, sub, repo) = init_repo_with_subdir("sub");
        let result = resolve_pathspec_prefix(&repo, Some(sub.to_str().unwrap()), None).unwrap();
        assert_eq!(result.as_deref(), Some("sub"));
    }

    #[test]
    fn test_filter_repo_root_pathspecs_removes_dot() {
        let result = filter_repo_root_pathspecs(vec![".".into()]);
        assert!(result.is_empty());
    }

    #[test]
    fn test_filter_repo_root_pathspecs_removes_dot_slash() {
        let result = filter_repo_root_pathspecs(vec!["./".into()]);
        assert!(result.is_empty());
    }

    #[test]
    fn test_filter_repo_root_pathspecs_preserves_other() {
        let result = filter_repo_root_pathspecs(vec!["src/".into(), "Cargo.toml".into()]);
        assert_eq!(result, vec!["src/", "Cargo.toml"]);
    }

    #[test]
    fn test_filter_repo_root_pathspecs_mixed() {
        let result = filter_repo_root_pathspecs(vec![".".into(), "src/".into(), "./".into()]);
        assert_eq!(result, vec!["src/"]);
    }

    #[test]
    fn test_filter_repo_root_pathspecs_empty() {
        let result = filter_repo_root_pathspecs(vec![]);
        assert!(result.is_empty());
    }

    #[test]
    fn test_filter_repo_root_pathspecs_keeps_dot_prefix() {
        // Only exact "." and "./" are filtered — not "./foo".
        let result = filter_repo_root_pathspecs(vec!["./foo.rs".into()]);
        assert_eq!(result, vec!["./foo.rs"]);
    }

    #[test]
    fn test_filter_repo_root_pathspecs_keeps_subdir_dot() {
        // "./bar/.gitkeep" is not a bare "./" — it must be kept.
        let result = filter_repo_root_pathspecs(vec!["./bar/.gitkeep".into()]);
        assert_eq!(result, vec!["./bar/.gitkeep"]);
    }

    #[test]
    fn test_resolve_pathspec_prefix_bare_repo_no_workdir_returns_none() {
        let tmp = tempfile::tempdir().unwrap();
        let repo_root = tmp.path();

        // Create a bare repo.
        std::process::Command::new("git")
            .args(["init", "-q", "--bare"])
            .current_dir(repo_root)
            .output()
            .unwrap();

        let repo = gix::discover(repo_root).unwrap();
        // Bare repos have no workdir.
        assert!(repo.workdir().is_none());

        let result = resolve_pathspec_prefix(&repo, None, None).unwrap();
        assert_eq!(result, None);
    }
}