frame 0.1.7

A markdown task tracker with a terminal UI for humans and a CLI for agents
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
//! Minimal git introspection used by the actor-identity layer.
//!
//! Frame's per-clone actor token lives in a gitignored file. To keep every git
//! *worktree* of a clone on one shared identity (rather than each worktree
//! auto-claiming its own token on first mint), the shared token is stored under
//! the git **common directory** — which `git rev-parse --git-common-dir`
//! resolves to the *same* path from the main working tree and every linked
//! worktree.
//!
//! The primary's `null` token predates that file and lives only in the main
//! working tree's local `frame/.actor`, so a linked worktree also needs to
//! locate its clone's **main working tree** ([`main_worktree_frame_dir`]) to
//! inherit that token instead of auto-claiming a new one, and to tell a linked
//! worktree from the main one at all ([`worktree_kind`]).

use std::path::{Path, PathBuf};
use std::process::Command;

/// The three repo paths the actor layer needs, all absolute and canonicalized.
#[derive(Debug, Clone)]
pub struct RepoPaths {
    /// This working tree's own git dir (`<main>/.git/worktrees/<name>` in a
    /// linked worktree, `<root>/.git` in the main one).
    pub git_dir: PathBuf,
    /// The git dir shared by every worktree of this clone.
    pub common_dir: PathBuf,
    /// This working tree's root.
    pub toplevel: PathBuf,
}

/// Which working tree of a clone `frame_dir` lives in.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WorktreeKind {
    /// The clone's main working tree (`git_dir == common_dir`).
    Main,
    /// A linked worktree (`git worktree add`). `main_root` is the clone's main
    /// working tree, or `None` when there isn't one to point at (a bare or
    /// `--separate-git-dir` repo).
    Linked { main_root: Option<PathBuf> },
    /// Not inside a git repository (or `git` is unavailable).
    NotGit,
}

/// Resolve [`RepoPaths`] for the repo containing `frame_dir`, or `None` when it
/// is not inside a git repository (or `git` is unavailable).
pub fn repo_paths(frame_dir: &Path) -> Option<RepoPaths> {
    // Run git from the project root (the parent of `frame/`). Relative results
    // (e.g. `.git` from the main worktree) resolve against that root.
    let root = frame_dir.parent()?;
    let output = Command::new("git")
        .arg("-C")
        .arg(root)
        .args([
            "rev-parse",
            "--absolute-git-dir",
            "--git-common-dir",
            "--show-toplevel",
        ])
        .output()
        .ok()?;
    if !output.status.success() {
        return None;
    }
    let text = String::from_utf8(output.stdout).ok()?;
    let mut lines = text.lines();
    let mut next = || -> Option<PathBuf> {
        let raw = lines.next()?.trim();
        if raw.is_empty() {
            return None;
        }
        let path = PathBuf::from(raw);
        let abs = if path.is_absolute() {
            path
        } else {
            root.join(path)
        };
        abs.canonicalize().ok()
    };
    Some(RepoPaths {
        git_dir: next()?,
        common_dir: next()?,
        toplevel: next()?,
    })
}

/// The absolute git common directory for the repo containing `frame_dir`, or
/// `None` when `frame_dir` is not inside a git repository (or `git` is
/// unavailable). All worktrees of one clone share a single common dir, so a file
/// placed there is visible to every worktree and to no other clone.
pub fn git_common_dir(frame_dir: &Path) -> Option<PathBuf> {
    repo_paths(frame_dir).map(|p| p.common_dir)
}

/// Which working tree of its clone `frame_dir` lives in. A linked worktree
/// reports the main working tree's root, derived from the common dir
/// (`<main>/.git` → `<main>`) and confirmed by checking that the candidate's
/// `.git` is that same common dir — so a bare or `--separate-git-dir` repo
/// yields `Linked { main_root: None }` rather than a guessed path.
pub fn worktree_kind(frame_dir: &Path) -> WorktreeKind {
    let Some(paths) = repo_paths(frame_dir) else {
        return WorktreeKind::NotGit;
    };
    if paths.git_dir == paths.common_dir {
        return WorktreeKind::Main;
    }
    let main_root = paths.common_dir.parent().filter(|root| {
        root.join(".git")
            .canonicalize()
            .is_ok_and(|g| g == paths.common_dir)
    });
    WorktreeKind::Linked {
        main_root: main_root.map(|r| r.to_path_buf()),
    }
}

/// The *main* working tree's copy of this project's frame directory, when
/// `frame_dir` is inside a linked worktree. `None` from the main worktree
/// itself, outside git, or when the main tree has no frame directory at the same
/// repo-relative path (a bare repo, or a project that only exists on this
/// worktree's branch).
pub fn main_worktree_frame_dir(frame_dir: &Path) -> Option<PathBuf> {
    let WorktreeKind::Linked { main_root } = worktree_kind(frame_dir) else {
        return None; // main worktree (its frame dir is the one we were given) or not git
    };
    let main_root = main_root?;
    let paths = repo_paths(frame_dir)?;
    let rel = frame_dir
        .canonicalize()
        .ok()?
        .strip_prefix(&paths.toplevel)
        .ok()?
        .to_path_buf();
    let candidate = main_root.join(rel);
    candidate.is_dir().then_some(candidate)
}

/// Run `git` in `toplevel` over `rel_paths` and collect the repo-relative paths
/// it prints. `None` when git is unavailable or errors out (exit 128); an exit
/// status of 1 with no output is a legitimate empty result, not a failure —
/// `check-ignore` uses it to mean "nothing matched".
fn git_paths(toplevel: &Path, args: &[&str], rel_paths: &[String]) -> Option<Vec<String>> {
    let output = Command::new("git")
        .arg("-C")
        .arg(toplevel)
        .args(args)
        .arg("--")
        .args(rel_paths)
        .output()
        .ok()?;
    if output.status.code() == Some(128) {
        return None;
    }
    let text = String::from_utf8(output.stdout).ok()?;
    Some(text.lines().map(|l| l.trim().to_string()).collect())
}

/// Which of `rel_paths` (repo-relative) git already **tracks** — i.e. they are in
/// the index and their contents are committed (or staged) into shared history.
pub fn tracked_paths(toplevel: &Path, rel_paths: &[String]) -> Option<Vec<String>> {
    git_paths(toplevel, &["ls-files", "--cached"], rel_paths)
}

/// Which of `rel_paths` (repo-relative) `.gitignore` covers. A *tracked* path is
/// reported as not ignored — which is the honest answer for "will this get
/// committed?", since ignore rules don't apply to files already in the index.
pub fn ignored_paths(toplevel: &Path, rel_paths: &[String]) -> Option<Vec<String>> {
    git_paths(toplevel, &["check-ignore"], rel_paths)
}

/// Which of `rel_paths` (repo-relative) have unstaged modifications — the working
/// tree copy differs from what is staged in the index.
fn modified_paths(toplevel: &Path, rel_paths: &[String]) -> Option<Vec<String>> {
    git_paths(toplevel, &["diff", "--name-only"], rel_paths)
}

/// Files and directories git leaves in a working tree's git dir while a
/// multi-step operation is unfinished. Each is removed when the operation
/// finishes or is aborted, so their presence means "git is mid-surgery".
const OPERATION_MARKERS: [&str; 6] = [
    "rebase-merge",     // rebase -i, rebase --merge (directory)
    "rebase-apply",     // rebase --apply, am (directory)
    "MERGE_HEAD",       // merge
    "CHERRY_PICK_HEAD", // cherry-pick
    "REVERT_HEAD",      // revert
    "BISECT_LOG",       // bisect
];

/// Whether git is part-way through a multi-step operation (rebase, merge,
/// cherry-pick, revert, bisect, am) on the working tree containing `frame_dir`.
///
/// The markers live in this worktree's own git dir, not the common dir, so a
/// rebase in one worktree does not report as in progress from another.
/// `false` when `frame_dir` is not in a repo, or git is unavailable.
pub fn operation_in_progress(frame_dir: &Path) -> bool {
    let Some(paths) = repo_paths(frame_dir) else {
        return false;
    };
    OPERATION_MARKERS
        .iter()
        .any(|marker| paths.git_dir.join(marker).exists())
}

/// Of `abs_paths`, those whose current contents **git itself wrote**: the path is
/// tracked and byte-identical to the index.
///
/// This is the discriminator between "a human edited this file" and "git put this
/// file here". `git restore`, `git checkout`, `git stash`, and the checkouts a
/// rebase performs all leave a file exactly matching the index; saving from an
/// editor leaves it differing from the index. Untracked files are never
/// reported — `git diff` ignores them, so "no diff" would otherwise be read as
/// "unmodified".
///
/// Empty when `frame_dir` is not in a repo or git is unavailable, which callers
/// should read as "no evidence git did this" rather than as an error.
pub fn index_clean_paths(frame_dir: &Path, abs_paths: &[PathBuf]) -> Vec<PathBuf> {
    let Some(paths) = repo_paths(frame_dir) else {
        return Vec::new();
    };
    // Canonicalize before relativizing: `toplevel` is canonical, and watcher
    // paths need not be (`/tmp` vs `/private/tmp` on macOS). A path that cannot
    // be canonicalized no longer exists, so it is not an index-clean file.
    let relativized: Vec<(String, PathBuf)> = abs_paths
        .iter()
        .filter_map(|p| {
            let canon = p.canonicalize().ok()?;
            let rel = canon.strip_prefix(&paths.toplevel).ok()?.to_str()?;
            Some((rel.to_string(), p.clone()))
        })
        .collect();
    if relativized.is_empty() {
        return Vec::new();
    }
    let rels: Vec<String> = relativized.iter().map(|(rel, _)| rel.clone()).collect();
    let (Some(tracked), Some(modified)) = (
        tracked_paths(&paths.toplevel, &rels),
        modified_paths(&paths.toplevel, &rels),
    ) else {
        return Vec::new();
    };
    relativized
        .into_iter()
        .filter(|(rel, _)| tracked.contains(rel) && !modified.contains(rel))
        .map(|(_, abs)| abs)
        .collect()
}

#[cfg(test)]
pub(crate) mod testutil {
    use std::path::{Path, PathBuf};
    use std::process::Command;

    fn git(cwd: &Path, args: &[&str]) -> bool {
        Command::new("git")
            .current_dir(cwd)
            .args(args)
            .stdout(std::process::Stdio::null())
            .stderr(std::process::Stdio::null())
            .status()
            .map(|s| s.success())
            .unwrap_or(false)
    }

    /// Build `<tmp>/main` (a git repo with one commit) and a linked worktree
    /// `<tmp>/wt`, each containing an empty `frame/` directory. Returns the two
    /// frame dirs `(main, worktree)`, or `None` when git is unavailable — every
    /// caller skips its assertions in that case rather than failing.
    pub(crate) fn repo_with_worktree(tmp: &Path) -> Option<(PathBuf, PathBuf)> {
        let main = tmp.join("main");
        std::fs::create_dir_all(&main).ok()?;
        if !git(&main, &["init", "-q"]) {
            return None;
        }
        // `git worktree add` needs a commit to point the new tree at.
        let committed = git(
            &main,
            &[
                "-c",
                "user.name=frame-test",
                "-c",
                "user.email=frame@test.invalid",
                "commit",
                "-q",
                "--allow-empty",
                "-m",
                "init",
            ],
        );
        if !committed {
            return None;
        }
        let wt = tmp.join("wt");
        if !git(&main, &["worktree", "add", "-q", "--detach", wt.to_str()?]) {
            return None;
        }
        let main_frame = main.join("frame");
        let wt_frame = wt.join("frame");
        std::fs::create_dir_all(&main_frame).ok()?;
        std::fs::create_dir_all(&wt_frame).ok()?;
        Some((main_frame, wt_frame))
    }

    /// Build `<tmp>/repo` as a git repo whose `frame/tracks/main.md` is committed
    /// at HEAD with no working-tree changes. Returns the frame dir, or `None`
    /// when git is unavailable.
    pub(crate) fn repo_with_committed_track(tmp: &Path) -> Option<PathBuf> {
        let root = tmp.join("repo");
        let tracks = root.join("frame").join("tracks");
        std::fs::create_dir_all(&tracks).ok()?;
        if !git(&root, &["init", "-q"]) {
            return None;
        }
        std::fs::write(tracks.join("main.md"), "# Main\n\n## Done\n").ok()?;
        if !git(&root, &["add", "."]) {
            return None;
        }
        let committed = git(
            &root,
            &[
                "-c",
                "user.name=frame-test",
                "-c",
                "user.email=frame@test.invalid",
                "commit",
                "-q",
                "-m",
                "init",
            ],
        );
        committed.then(|| root.join("frame"))
    }

    /// Run `git restore` over a path, as a user would to undo an edit.
    pub(crate) fn restore(root: &Path, rel: &str) -> bool {
        git(root, &["restore", "--", rel])
    }

    /// Stage and commit everything in `root`, so the working tree is settled.
    pub(crate) fn commit_all(root: &Path) -> bool {
        git(root, &["add", "-A"])
            && git(
                root,
                &[
                    "-c",
                    "user.name=frame-test",
                    "-c",
                    "user.email=frame@test.invalid",
                    "commit",
                    "-q",
                    "-m",
                    "wip",
                ],
            )
    }
}

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

    #[test]
    fn worktree_kind_distinguishes_main_from_linked() {
        let tmp = TempDir::new().unwrap();
        let Some((main_frame, wt_frame)) = testutil::repo_with_worktree(tmp.path()) else {
            return; // git unavailable
        };
        assert_eq!(worktree_kind(&main_frame), WorktreeKind::Main);
        match worktree_kind(&wt_frame) {
            WorktreeKind::Linked { main_root } => {
                let expected = main_frame.parent().unwrap().canonicalize().unwrap();
                assert_eq!(main_root.map(|r| r.canonicalize().unwrap()), Some(expected));
            }
            other => panic!("expected Linked, got {other:?}"),
        }
    }

    #[test]
    fn main_worktree_frame_dir_resolves_only_from_a_linked_worktree() {
        let tmp = TempDir::new().unwrap();
        let Some((main_frame, wt_frame)) = testutil::repo_with_worktree(tmp.path()) else {
            return;
        };
        // From the main tree there is nothing to point at.
        assert_eq!(main_worktree_frame_dir(&main_frame), None);
        // From the linked worktree it resolves to the main tree's frame dir.
        assert_eq!(
            main_worktree_frame_dir(&wt_frame).map(|p| p.canonicalize().unwrap()),
            Some(main_frame.canonicalize().unwrap())
        );
    }

    #[test]
    fn non_git_project_has_no_repo_paths() {
        let tmp = TempDir::new().unwrap();
        let frame_dir = tmp.path().join("frame");
        std::fs::create_dir_all(&frame_dir).unwrap();
        // A bare directory outside any repo: no common dir, no worktree kind.
        // (Guard against the tempdir itself sitting inside a repo.)
        if repo_paths(&frame_dir).is_none() {
            assert_eq!(worktree_kind(&frame_dir), WorktreeKind::NotGit);
            assert_eq!(git_common_dir(&frame_dir), None);
            assert_eq!(main_worktree_frame_dir(&frame_dir), None);
            // Outside a repo there is no evidence git did anything, so auto-clean
            // keeps its pre-existing behaviour rather than being suppressed.
            assert!(!operation_in_progress(&frame_dir));
            assert!(index_clean_paths(&frame_dir, &[frame_dir.join("tracks/main.md")]).is_empty());
        }
    }

    #[test]
    fn operation_in_progress_detects_file_and_directory_markers() {
        let tmp = TempDir::new().unwrap();
        let Some(frame_dir) = testutil::repo_with_committed_track(tmp.path()) else {
            return; // git unavailable
        };
        assert!(!operation_in_progress(&frame_dir), "settled repo");

        let git_dir = repo_paths(&frame_dir).unwrap().git_dir;

        // A merge leaves a marker *file*.
        std::fs::write(git_dir.join("MERGE_HEAD"), "").unwrap();
        assert!(operation_in_progress(&frame_dir), "merge in progress");
        std::fs::remove_file(git_dir.join("MERGE_HEAD")).unwrap();
        assert!(!operation_in_progress(&frame_dir), "merge finished");

        // A rebase leaves a marker *directory* — `.exists()` covers both.
        std::fs::create_dir(git_dir.join("rebase-merge")).unwrap();
        assert!(operation_in_progress(&frame_dir), "rebase in progress");
    }

    /// The discriminator behind auto-clean suppression: an editor save must look
    /// different from git putting a file back.
    #[test]
    fn index_clean_paths_separates_git_writes_from_editor_writes() {
        let tmp = TempDir::new().unwrap();
        let Some(frame_dir) = testutil::repo_with_committed_track(tmp.path()) else {
            return; // git unavailable
        };
        let root = frame_dir.parent().unwrap().to_path_buf();
        let track = frame_dir.join("tracks").join("main.md");

        // Committed and untouched: git owns this content.
        assert_eq!(
            index_clean_paths(&frame_dir, std::slice::from_ref(&track)),
            vec![track.clone()],
            "unmodified tracked file"
        );

        // Hand-edited: a person owns this content, so auto-clean should run.
        std::fs::write(&track, "# Main\n\n## Done\n\n- [x] `M-1` Ticked\n").unwrap();
        assert!(
            index_clean_paths(&frame_dir, std::slice::from_ref(&track)).is_empty(),
            "edited file must not read as a git write"
        );

        // `git restore` — the exact command that fought the TUI — puts it back.
        assert!(testutil::restore(&root, "frame/tracks/main.md"));
        assert_eq!(
            index_clean_paths(&frame_dir, std::slice::from_ref(&track)),
            vec![track],
            "restored file reads as a git write"
        );

        // Untracked files are never reported: `git diff` shows them no diff, and
        // reading that as "unmodified" would suppress cleaning brand-new tracks.
        let untracked = frame_dir.join("tracks").join("new.md");
        std::fs::write(&untracked, "# New\n").unwrap();
        assert!(
            index_clean_paths(&frame_dir, &[untracked]).is_empty(),
            "untracked file"
        );
    }
}