argonauts-crew 0.1.0

Argonauts Code run engine: a workflow runner for Understand, planning, dividing, task pipelines, merge, and the run review.
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
//! Tags, notes, resets, and fast-forwards: the git side of fallback checkpoints, authorship
//! notes, `/rewind`, and landing.

use std::path::Path;

use super::{Git, GitError, NOTES_REF, failed};

/// A refused merge or checkout, in git's own words. Its last line is only "Aborting", and its
/// first error says why.
fn failed_at_first_error(args: &[&str], out: &std::process::Output) -> GitError {
    let stderr = String::from_utf8_lossy(&out.stderr);
    let why = stderr
        .lines()
        .map(str::trim)
        .find(|line| line.starts_with("error:") || line.starts_with("fatal:"))
        .or_else(|| stderr.lines().map(str::trim).rfind(|line| !line.is_empty()))
        .unwrap_or("")
        .to_owned();
    GitError::Failed {
        args: args.join(" "),
        status: out.status.to_string(),
        stderr: why,
    }
}

impl Git {
    /// Moves the checked-out branch to `rev` when that is a fast-forward. False when it isn't.
    pub async fn fast_forward(&self, worktree: &Path, rev: &str) -> Result<bool, GitError> {
        let target = self.resolve(worktree, rev).await?;
        if let Some(head) = self.head_commit(worktree).await?
            && !self.is_ancestor(worktree, &head, &target).await?
        {
            return Ok(false);
        }
        let args = ["merge", "--ff-only", "--quiet", &target];
        let out = self.output(worktree, &args, &[]).await?;
        if out.status.success() {
            return Ok(true);
        }
        Err(failed_at_first_error(&args, &out))
    }

    pub async fn is_ancestor(
        &self,
        dir: &Path,
        ancestor: &str,
        descendant: &str,
    ) -> Result<bool, GitError> {
        let args = ["merge-base", "--is-ancestor", ancestor, descendant];
        let out = self.output(dir, &args, &[]).await?;
        match out.status.code() {
            Some(0) => Ok(true),
            Some(1) => Ok(false),
            _ => Err(failed(&args, &out)),
        }
    }

    /// The best common ancestor of two commits, or `None` when their histories are unrelated.
    pub async fn merge_base(
        &self,
        dir: &Path,
        a: &str,
        b: &str,
    ) -> Result<Option<String>, GitError> {
        self.optional(dir, &["merge-base", a, b]).await
    }

    /// The tree SHA of `rev`, which evidence binds approvals to.
    pub async fn tree_of(&self, dir: &Path, rev: &str) -> Result<String, GitError> {
        let spec = format!("{rev}^{{tree}}");
        self.run(dir, &["rev-parse", "--verify", &spec]).await
    }

    /// A commit of `tree` on top of `parent`, made without a worktree or an index. No branch
    /// points at it until the caller moves one.
    pub async fn commit_tree(
        &self,
        dir: &Path,
        tree: &str,
        parent: &str,
        message: &str,
    ) -> Result<String, GitError> {
        let identity = self.identity_env(dir).await;
        let args = ["commit-tree", tree, "-p", parent, "-m", message];
        let out = self.output(dir, &args, &identity).await?;
        if !out.status.success() {
            return Err(failed(&args, &out));
        }
        Ok(String::from_utf8_lossy(&out.stdout).trim().to_owned())
    }

    /// Writes into the files of `checkout` what changed from `base` to `rev`, and moves
    /// nothing else: no branch, and not your index, since git works out the change in an
    /// index of its own at `scratch`. Like a fast-forward it is all or nothing, and refuses
    /// while a file of yours is different from `base` where the change would write, unless it is
    /// one of `holding`, which hold what `rev` has there already and are left as they are.
    pub async fn write_files(
        &self,
        checkout: &Path,
        scratch: &Path,
        base: &str,
        rev: &str,
        holding: &[String],
    ) -> Result<(), GitError> {
        let index = scratch.to_string_lossy();
        let with_index = [("GIT_INDEX_FILE", index.as_ref())];
        let written = async {
            self.in_index(checkout, &["read-tree", base], &with_index)
                .await?;
            // Files that still match `base` count as untouched only once git has seen them.
            // It exits 1 for each file of yours that is different, which the merge judges.
            let refresh = ["update-index", "-q", "--refresh"];
            self.output(checkout, &refresh, &with_index).await?;
            self.take_as_they_are(checkout, rev, holding, &with_index)
                .await?;
            let merge = ["read-tree", "-m", "-u", base, rev];
            self.in_index(checkout, &merge, &with_index).await
        }
        .await;
        if let Err(err) = std::fs::remove_file(scratch)
            && err.kind() != std::io::ErrorKind::NotFound
        {
            tracing::warn!("removing {}: {err}", scratch.display());
        }
        written
    }

    /// Runs `args` on the index `with_index` names, and says what git refused in its words.
    pub(super) async fn in_index(
        &self,
        checkout: &Path,
        args: &[&str],
        with_index: &[(&str, &str)],
    ) -> Result<(), GitError> {
        let out = self.output(checkout, args, with_index).await?;
        if out.status.success() {
            Ok(())
        } else {
            Err(failed_at_first_error(args, &out))
        }
    }

    /// Your files in `checkout` that no longer hold what `base` has, changed or gone, of those
    /// the change from `base` to `rev` writes. It's what [`Self::write_files`] refuses over,
    /// found the way it works: the files are compared with `base` in an index of git's own at
    /// `scratch`, so it holds in a checkout whose own index never held `base`.
    pub async fn differing_in_the_way(
        &self,
        checkout: &Path,
        scratch: &Path,
        base: &str,
        rev: &str,
    ) -> Result<Vec<String>, GitError> {
        let index = scratch.to_string_lossy();
        let with_index = [("GIT_INDEX_FILE", index.as_ref())];
        let differing = async {
            let read = ["read-tree", base];
            let out = self.output(checkout, &read, &with_index).await?;
            if !out.status.success() {
                return Err(failed(&read, &out));
            }
            // Exits 1 for each file that is different, which the next step lists.
            let refresh = ["update-index", "-q", "--refresh"];
            self.output(checkout, &refresh, &with_index).await?;
            let list = ["diff-files", "--name-only", "--no-renames", "-z"];
            let out = self.output(checkout, &list, &with_index).await?;
            if !out.status.success() {
                return Err(failed(&list, &out));
            }
            Ok(String::from_utf8_lossy(&out.stdout).into_owned())
        }
        .await;
        if let Err(err) = std::fs::remove_file(scratch)
            && err.kind() != std::io::ErrorKind::NotFound
        {
            tracing::warn!("removing {}: {err}", scratch.display());
        }
        let differing = differing?;
        // Both sides of a move, since the old name is written too: it goes away.
        let written = [
            "diff",
            "--name-only",
            "--no-renames",
            "--no-ext-diff",
            "-z",
            base,
            rev,
        ];
        let written = self.run(checkout, &written).await?;
        let written: std::collections::BTreeSet<&str> = written.split('\0').collect();
        Ok(differing
            .split('\0')
            .filter(|path| !path.is_empty() && written.contains(path))
            .map(str::to_owned)
            .collect())
    }

    /// Whether `checkout` already holds the change from `base` to `rev`: every file it writes
    /// has `rev`'s bytes, and every file it removes is gone. So a landing as files that was
    /// cut short after the writing isn't taken for your own edits in its way.
    pub async fn holds_change(
        &self,
        checkout: &Path,
        scratch: &Path,
        base: &str,
        rev: &str,
    ) -> Result<bool, GitError> {
        let unwritten = self
            .differing_in_the_way(checkout, scratch, rev, base)
            .await?;
        if !unwritten.is_empty() {
            return Ok(false);
        }
        let removed = [
            "diff",
            "--name-only",
            "--no-renames",
            "--no-ext-diff",
            "-z",
            "--diff-filter=D",
            base,
            rev,
        ];
        let removed = self.run(checkout, &removed).await?;
        Ok(removed
            .split('\0')
            .filter(|path| !path.is_empty())
            .all(|path| std::fs::symlink_metadata(checkout.join(path)).is_err()))
    }

    /// The commit the ref `name` points at, such as `refs/argonauts/run-01/base`. `None` when
    /// there's no such ref.
    pub async fn ref_commit(&self, dir: &Path, name: &str) -> Result<Option<String>, GitError> {
        let spec = format!("{name}^{{commit}}");
        self.optional(dir, &["rev-parse", "--verify", "--quiet", &spec])
            .await
    }

    /// An annotated tag whose message records the fallback. Tagging the same commit again is a
    /// no-op, so a step re-entered after a crash doesn't fail; a tag on another commit is an
    /// error, since a checkpoint never moves.
    pub async fn tag(
        &self,
        dir: &Path,
        name: &str,
        rev: &str,
        message: &str,
    ) -> Result<(), GitError> {
        let commit = self.resolve(dir, rev).await?;
        if let Some(existing) = self.tag_target(dir, name).await? {
            if existing == commit {
                return Ok(());
            }
            return Err(GitError::Failed {
                args: format!("tag {name}"),
                status: "refused".to_owned(),
                stderr: format!("tag {name} already points at {existing}"),
            });
        }
        let identity = self.identity_env(dir).await;
        let args = [
            "tag",
            "--annotate",
            "--no-sign",
            "-m",
            message,
            name,
            &commit,
        ];
        let out = self.output(dir, &args, &identity).await?;
        if out.status.success() {
            Ok(())
        } else {
            Err(failed(&args, &out))
        }
    }

    /// The commit a tag points at, or `None` when there is no such tag.
    pub async fn tag_target(&self, dir: &Path, name: &str) -> Result<Option<String>, GitError> {
        let spec = format!("refs/tags/{name}^{{commit}}");
        self.optional(dir, &["rev-parse", "--verify", "--quiet", &spec])
            .await
    }

    /// Adds or replaces the note on `rev` under [`NOTES_REF`].
    pub async fn add_note(&self, dir: &Path, rev: &str, text: &str) -> Result<(), GitError> {
        let identity = self.identity_env(dir).await;
        let notes_ref = format!("--ref={NOTES_REF}");
        let args = ["notes", &notes_ref, "add", "--force", "-m", text, rev];
        let out = self.output(dir, &args, &identity).await?;
        if out.status.success() {
            Ok(())
        } else {
            Err(failed(&args, &out))
        }
    }

    pub async fn note(&self, dir: &Path, rev: &str) -> Result<Option<String>, GitError> {
        let notes_ref = format!("--ref={NOTES_REF}");
        self.optional(dir, &["notes", &notes_ref, "show", rev])
            .await
    }

    pub async fn reset_hard(&self, worktree: &Path, rev: &str) -> Result<(), GitError> {
        let commit = self.resolve(worktree, rev).await?;
        self.run(worktree, &["reset", "--hard", "--quiet", &commit])
            .await
            .map(drop)
    }

    /// Points `branch` at `new` only while it still points at `old`, for a branch that isn't
    /// checked out. Git refuses when someone moved it in between.
    pub async fn update_branch(
        &self,
        dir: &Path,
        branch: &str,
        new: &str,
        old: &str,
    ) -> Result<(), GitError> {
        let reference = format!("refs/heads/{branch}");
        self.run(dir, &["update-ref", &reference, new, old])
            .await
            .map(drop)
    }

    /// The names of the repository's remotes.
    pub async fn remotes(&self, dir: &Path) -> Result<Vec<String>, GitError> {
        let out = self.run(dir, &["remote"]).await?;
        Ok(out.lines().map(str::to_owned).collect())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::testutil::{TestRepo, git_test_env};

    fn git() -> Git {
        Git::with_env(git_test_env())
    }

    fn commit(repo: &TestRepo, file: &str, text: &str) -> String {
        repo.write(file, text);
        repo.git(&["add", file]);
        repo.git(&["commit", "--quiet", "-m", file]);
        repo.git(&["rev-parse", "HEAD"])
    }

    #[tokio::test]
    async fn ancestry_and_merge_base() {
        let repo = TestRepo::new();
        let git = git();
        let first = repo.git(&["rev-parse", "HEAD"]);
        let second = commit(&repo, "a.txt", "a");
        assert!(git.is_ancestor(&repo.root, &first, &second).await.unwrap());
        assert!(!git.is_ancestor(&repo.root, &second, &first).await.unwrap());
        assert!(git.is_ancestor(&repo.root, &second, &second).await.unwrap());

        repo.git(&["checkout", "--quiet", "-b", "side", &first]);
        let side = commit(&repo, "b.txt", "b");
        assert_eq!(
            git.merge_base(&repo.root, &second, &side).await.unwrap(),
            Some(first)
        );
        let err = git.is_ancestor(&repo.root, "nope", &side).await;
        assert!(err.is_err());
    }

    #[tokio::test]
    async fn fast_forward_only_when_possible() {
        let repo = TestRepo::new();
        let git = git();
        let first = repo.git(&["rev-parse", "HEAD"]);
        let ahead = commit(&repo, "a.txt", "a");
        repo.git(&["reset", "--hard", "--quiet", &first]);

        assert!(git.fast_forward(&repo.root, &ahead).await.unwrap());
        assert_eq!(repo.git(&["rev-parse", "HEAD"]), ahead);
        assert!(repo.root.join("a.txt").exists());

        repo.git(&["checkout", "--quiet", "-b", "side", &first]);
        commit(&repo, "b.txt", "b");
        assert!(!git.fast_forward(&repo.root, &ahead).await.unwrap());
        assert!(!repo.root.join("a.txt").exists());
    }

    #[tokio::test]
    async fn tree_of_matches_git() {
        let repo = TestRepo::new();
        let tree = git().tree_of(&repo.root, "HEAD").await.unwrap();
        assert_eq!(tree, repo.git(&["rev-parse", "HEAD^{tree}"]));
        assert_eq!(tree.len(), 40);
    }

    #[tokio::test]
    async fn tags_are_annotated_and_never_move() {
        let repo = TestRepo::new();
        let git = git();
        let first = repo.git(&["rev-parse", "HEAD"]);
        let name = "argonauts/run-01/task-001/fallback-1";
        assert_eq!(git.tag_target(&repo.root, name).await.unwrap(), None);

        git.tag(
            &repo.root,
            name,
            "HEAD",
            "from: claude-code/opus\nreason: usage limit",
        )
        .await
        .unwrap();
        assert_eq!(
            git.tag_target(&repo.root, name).await.unwrap(),
            Some(first.clone())
        );
        assert_eq!(repo.git(&["cat-file", "-t", name]), "tag");
        let message = repo.git(&["tag", "-l", "--format=%(contents)", name]);
        assert!(message.contains("reason: usage limit"), "{message}");

        // The branch moves on; the tag stays, and tagging the same commit again is fine.
        let second = commit(&repo, "a.txt", "a");
        git.tag(&repo.root, name, &first, "again").await.unwrap();
        assert!(git.tag(&repo.root, name, &second, "moved").await.is_err());
        assert_eq!(git.tag_target(&repo.root, name).await.unwrap(), Some(first));
    }

    #[tokio::test]
    async fn notes_are_added_replaced_and_read() {
        let repo = TestRepo::new();
        let git = git();
        assert_eq!(git.note(&repo.root, "HEAD").await.unwrap(), None);
        git.add_note(&repo.root, "HEAD", "by codex/gpt-5.6")
            .await
            .unwrap();
        git.add_note(&repo.root, "HEAD", "by agy/gemini-3.6")
            .await
            .unwrap();
        assert_eq!(
            git.note(&repo.root, "HEAD").await.unwrap().as_deref(),
            Some("by agy/gemini-3.6")
        );
        // Notes live under their own ref, so the default notes and the message stay clean.
        assert!(!repo.git(&["for-each-ref", NOTES_REF]).is_empty());
        assert_eq!(repo.git(&["log", "-1", "--format=%B"]), "init");
    }

    #[tokio::test]
    async fn reset_hard_discards_later_work() {
        let repo = TestRepo::new();
        let git = git();
        let first = repo.git(&["rev-parse", "HEAD"]);
        commit(&repo, "a.txt", "a");
        repo.write("README.md", "dirty\n");
        git.reset_hard(&repo.root, &first).await.unwrap();
        assert_eq!(repo.git(&["rev-parse", "HEAD"]), first);
        assert!(!repo.root.join("a.txt").exists());
        assert!(git.tracked_changes(&repo.root).await.unwrap().is_empty());
    }

    #[tokio::test]
    async fn update_branch_checks_the_old_value() {
        let repo = TestRepo::new();
        let git = git();
        let first = repo.git(&["rev-parse", "HEAD"]);
        let second = commit(&repo, "a.txt", "a");
        repo.git(&["branch", "target", &first]);

        assert!(
            git.update_branch(&repo.root, "target", &second, &second)
                .await
                .is_err()
        );
        git.update_branch(&repo.root, "target", &second, &first)
            .await
            .unwrap();
        assert_eq!(repo.git(&["rev-parse", "target"]), second);

        // An empty old value creates a branch, and never overwrites one.
        git.update_branch(&repo.root, "kept", &first, "")
            .await
            .unwrap();
        assert!(
            git.update_branch(&repo.root, "kept", &second, "")
                .await
                .is_err()
        );
        assert_eq!(repo.git(&["rev-parse", "kept"]), first);
    }

    #[tokio::test]
    async fn remotes_are_listed() {
        let repo = TestRepo::new();
        let git = git();
        assert!(git.remotes(&repo.root).await.unwrap().is_empty());
        repo.git(&["remote", "add", "origin", "https://example.com/repo.git"]);
        assert_eq!(git.remotes(&repo.root).await.unwrap(), ["origin"]);
    }
}