Prepare() treats worktree as ready from .git dir existence alone (archie)

Implements #57
This commit is contained in:
archie committed 2026-07-24 05:32:41 +00:00
1 parent 17307ef96e
commit d2d10685e7
4 files changed
+119 -3

No files matched your search

Executable
+2
View File
@@ -0,0 +1,2 @@
#!/bin/sh
echo "$ARCHIE_GIT_TOKEN"
+20 -3
View File
@@ -98,9 +98,9 @@ func (m *Manager) prepare(ctx context.Context, owner, repo, base string, issue i
dir = m.Dir(owner, repo, issue)
branch = archieBranch(issue, title, body, labels)
// Already prepared (daemon cloned before container acquire).
// Pull latest to avoid working on stale code.
if st, statErr := os.Stat(filepath.Join(dir, ".git")); statErr == nil && st.IsDir() {
// Already prepared: sentinel proves clone + checkout + config completed.
sentinel := filepath.Join(dir, ".archie-prepared")
if _, err := os.Stat(sentinel); err == nil {
if _, err := m.git(ctx, dir, "fetch", "origin"); err != nil {
return "", "", err
}
@@ -139,6 +139,23 @@ func (m *Manager) prepare(ctx context.Context, owner, repo, base string, issue i
return "", "", err
}
}
// Exclude the sentinel from git tracking via the local exclude file.
exclude := filepath.Join(dir, ".git", "info", "exclude")
f, err := os.OpenFile(exclude, os.O_APPEND|os.O_WRONLY, 0o644)
if err != nil {
return "", "", err
}
if _, err := f.WriteString(".archie-prepared\n"); err != nil {
f.Close()
return "", "", err
}
if err := f.Close(); err != nil {
return "", "", err
}
// Write sentinel to mark the worktree as fully prepared.
if err := os.WriteFile(filepath.Join(dir, ".archie-prepared"), nil, 0o644); err != nil {
return "", "", err
}
return dir, branch, nil
}
+96
View File
@@ -72,6 +72,11 @@ func TestPrepareCommitPushRoundTrip(t *testing.T) {
t.Fatalf("branch = %q", branch)
}
// Sentinel must exist after successful Prepare.
if _, err := os.Stat(filepath.Join(dir, ".archie-prepared")); err != nil {
t.Fatalf("sentinel missing after prepare: %v", err)
}
// Clean tree commits nothing.
changed, err := m.CommitAll(ctx, dir, "noop")
if err != nil || changed {
@@ -196,6 +201,97 @@ func TestPreparePersistentReusesRepoCacheAndCleanupExpiresIt(t *testing.T) {
}
}
func TestPrepareResumesAfterInterruptedClone(t *testing.T) {
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git not installed")
}
ctx := context.Background()
host := newLocalRemote(t, "acme", "interrupt")
m := &Manager{
WorkDir: t.TempDir(),
Token: "unused-for-file-remotes",
BotUser: "archie-bot",
BotEmail: "archie-bot@example.com",
BaseURL: "file://" + host,
}
dir := m.Dir("acme", "interrupt", 1)
// Simulate an interrupted clone: create parent dir with a bare-bones
// .git directory (via git init + remote + fetch) but no checked-out
// files and no .archie-prepared sentinel.
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
run := func(dir string, args ...string) {
t.Helper()
cmd := exec.Command("git", args...)
cmd.Dir = dir
cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git %v: %v\n%s", args, err, out)
}
}
run(dir, "init")
run(dir, "remote", "add", "origin", "file://"+filepath.Join(host, "acme", "interrupt.git"))
run(dir, "fetch", "origin")
// Confirm the broken state: .git exists but no sentinel and no README.md.
if _, err := os.Stat(filepath.Join(dir, ".git")); err != nil {
t.Fatalf(".git missing in simulated broken clone: %v", err)
}
if _, err := os.Stat(filepath.Join(dir, ".archie-prepared")); !os.IsNotExist(err) {
t.Fatal("sentinel unexpectedly exists in broken clone")
}
if _, err := os.Stat(filepath.Join(dir, "README.md")); !os.IsNotExist(err) {
t.Fatal("working tree files unexpectedly exist in broken clone")
}
// Prepare must detect the broken state, remove it, and do a fresh clone.
dir2, branch, err := m.Prepare(ctx, "acme", "interrupt", "main", 1, "feat: interrupted", "", "")
if err != nil {
t.Fatalf("prepare after interrupted clone: %v", err)
}
// The sentinel must exist after a successful Prepare.
if _, err := os.Stat(filepath.Join(dir2, ".archie-prepared")); err != nil {
t.Fatalf("sentinel missing after prepare: %v", err)
}
// The working tree must be fully functional: files from the remote exist.
if _, err := os.Stat(filepath.Join(dir2, "README.md")); err != nil {
t.Fatalf("working tree missing README.md after prepare: %v", err)
}
// CommitAll must work — the sentinel is excluded from git tracking.
changed, err := m.CommitAll(ctx, dir2, "noop")
if err != nil || changed {
t.Fatalf("clean CommitAll = (%v, %v), want (false, nil)", changed, err)
}
// Push must work.
if err := m.Push(ctx, dir2, branch); err != nil {
t.Fatalf("push: %v", err)
}
// A second Prepare call must short-circuit via the sentinel — no re-clone.
dir3, _, err := m.Prepare(ctx, "acme", "interrupt", "main", 1, "feat: interrupted", "", "")
if err != nil {
t.Fatalf("second prepare: %v", err)
}
if dir3 != dir2 {
t.Fatalf("second prepare created a different directory: %q vs %q", dir3, dir2)
}
if err := m.Cleanup("acme", "interrupt", 1); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(dir3); !os.IsNotExist(err) {
t.Fatal("cleanup left the worktree behind")
}
}
func TestCacheDirCannotEscapeWorkDir(t *testing.T) {
m := &Manager{WorkDir: t.TempDir()}
cache := m.cacheDir("../outside", `..\also-outside`)
+1
View File
@@ -0,0 +1 @@
{"id":5,"owner":"sam","repo":"archie-core","issue_number":57,"title":"Prepare() treats worktree as ready from .git dir existence alone","body":"## Problem\n\n`Manager.Prepare` in `internal/worktree/worktree.go:76-79` decides whether a task worktree is already fully prepared solely by checking that a `.git` directory exists:\n\n```go\n// Already prepared (daemon cloned before container acquire).\nif st, statErr := os.Stat(filepath.Join(dir, \".git\")); statErr == nil \u0026\u0026 st.IsDir() {\n return dir, branch, nil\n}\n```\n\n`git clone` creates the `.git` directory early in the clone process, before the checkout of the working tree completes, and well before the subsequent `checkout -b archie/issue-N` step (worktree.go:93) runs. If `archied` is killed or restarted (OOM, deploy, crash) while a clone is in progress, on the next start `Prepare()` will see the existing `.git` directory and short-circuit, assuming the worktree is fully set up — skipping the clone, the `checkout -b` branch creation, and any config steps.\n\n## Why it matters\n\nThis directly conflicts with the package's stated goal of being safely restartable: the package doc for `internal/daemon/daemon.go:3` states \"the daemon is restartable ... at any point.\" With this shortcut, a restart mid-clone leaves a `.git` directory but an incomplete/missing working tree and no `archie/issue-N` branch. Later calls to `CommitAll` (git add/status/commit) or `Push` (git push -u origin branch) will then operate on this broken state — failing with confusing errors, or in the worst case running against whatever branch actually exists (e.g. the base branch) rather than the intended `archie/issue-N` branch.\n\n## Suggested fix\n\nStrengthen the \"already prepared\" check to verify actual completion, not just `.git` existence, e.g.:\n- Confirm the target branch exists and is checked out (`git rev-parse --verify \u003cbranch\u003e` / check `HEAD`), and\n- Confirm the working tree is non-empty / clone actually finished (e.g. check for a marker file written only after the full clone+checkout sequence succeeds, or verify `git status` runs without error and matches expected branch).\n\nA simple robust option: write a sentinel file (e.g. `.archie-prepared`) as the very last step of the success path, and gate the early-return solely on that sentinel rather than on `.git` alone.\n\u003c/refined_body\u003e\n","labels":["agent:dead"],"workflow":"","branch":"feat/57-prepare-treats-worktree-as-ready-from-git-dir-existence-alon"}