Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
94.7 KB
Raw Blame
package daemon

import (
	"context"
	"encoding/json"
	"fmt"
	"io/fs"
	"log/slog"
	"net/http"
	"net/http/httptest"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"testing"
	"time"

	"github.com/multica-ai/multica/server/internal/daemon/execenv"
	"github.com/multica-ai/multica/server/internal/daemon/repocache"
)

// newGCTestDaemon creates a minimal Daemon for GC testing with a mock HTTP server.
func newGCTestDaemon(t *testing.T, handler http.Handler) *Daemon {
	t.Helper()
	srv := httptest.NewServer(handler)
	t.Cleanup(srv.Close)

	root := t.TempDir()
	cfg := Config{
		WorkspacesRoot:           root,
		GCEnabled:                true,
		GCInterval:               1 * time.Hour,
		GCTTL:                    5 * 24 * time.Hour,
		GCOrphanTTL:              30 * 24 * time.Hour,
		GCArtifactTTL:            12 * time.Hour,
		GCArtifactPatterns:       []string{"node_modules", ".next", ".turbo"},
		GCRepoMaintenanceEnabled: true,
	}
	d := New(cfg, slog.Default())
	d.client = NewClient(srv.URL)
	d.client.SetToken("test-token")
	return d
}

// createTaskDir creates a task directory with optional GC metadata.
func createTaskDir(t *testing.T, root, wsID, dirName string, meta *execenv.GCMeta) string {
	t.Helper()
	taskDir := filepath.Join(root, wsID, dirName)
	if err := os.MkdirAll(taskDir, 0o755); err != nil {
		t.Fatal(err)
	}
	// Production task directories end in the task key rather than the full
	// task ID. Keep human-readable fixture names while making the owner marker
	// obey the same path/identity contract enforced by GC.
	taskID := dirName
	if i := strings.LastIndex(dirName, "-"); i >= 0 && i+1 < len(dirName) {
		taskID = dirName[i+1:]
	}
	owner, err := json.Marshal(execenv.EnvRootOwner{WorkspaceID: wsID, TaskID: taskID})
	if err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(taskDir, ".task_owner"), owner, 0o644); err != nil {
		t.Fatal(err)
	}
	if meta != nil {
		data, _ := json.Marshal(meta)
		if err := os.WriteFile(filepath.Join(taskDir, ".gc_meta.json"), data, 0o644); err != nil {
			t.Fatal(err)
		}
	}
	return taskDir
}

// TestRunGC_StrandedTaskRootRecordsFollowTaskStatus guards the wiring AND the
// invariant that makes the sweep safe.
//
// A record whose env root never materialised has no other remover: cleanTaskDir
// only fires on a root it reclaims, and nothing else walks .task_roots. But
// absence of the root cannot authorise removal on its own — ResolveRootDir
// publishes the record BEFORE Prepare creates the directory, so a record that
// an aged-out re-dispatch is in the middle of reusing looks identical to a
// leftover. Deleting there hands the same task a second physical root, which is
// the orphaning the index exists to prevent.
//
// The age gate alone does not separate those two: an old record IS the state a
// stalled re-dispatch reads. Only the task's own status does, so the sweep asks
// the server and keeps anything non-terminal.
func TestRunGC_StrandedTaskRootRecordsFollowTaskStatus(t *testing.T) {
	root := t.TempDir()
	const workspaceID = "a05b0e10-ee7a-4603-a72d-a548b2390cb2"
	params := func(taskID string) execenv.RootDirParams {
		return execenv.RootDirParams{
			WorkspacesRoot: root, WorkspaceID: workspaceID,
			WorkspaceSlug: "Asset Feed", TaskID: taskID, IssueIdentifier: "MUL-6063",
		}
	}
	const (
		runningTask  = "5c57b65b-ee7a-4603-a72d-c760d45b2ed4"
		terminalTask = "5c57b65b-ee7a-4603-a72d-d871e56c3fe5"
		liveRootTask = "5c57b65b-ee7a-4603-a72d-b659c34a1dc3"
	)
	statuses := map[string]string{
		runningTask:  "running",
		terminalTask: "completed",
		// liveRootTask is terminal too: only its surviving root protects it.
		liveRootTask: "completed",
	}

	mux := http.NewServeMux()
	mux.HandleFunc("/api/daemon/tasks/", func(w http.ResponseWriter, r *http.Request) {
		taskID := strings.TrimSuffix(strings.TrimPrefix(r.URL.Path, "/api/daemon/tasks/"), "/gc-check")
		status, ok := statuses[taskID]
		if !ok {
			w.WriteHeader(http.StatusNotFound)
			return
		}
		json.NewEncoder(w).Encode(map[string]any{"status": status})
	})

	d := newGCTestDaemon(t, mux)
	d.cfg.WorkspacesRoot = root

	frozen := map[string]string{}
	for _, taskID := range []string{runningTask, terminalTask, liveRootTask} {
		resolved, err := execenv.ResolveRootDir(params(taskID))
		if err != nil {
			t.Fatalf("resolve root for %s: %v", taskID, err)
		}
		frozen[taskID] = resolved
	}
	// Only this one ever reached ClaimEnvRoot.
	if err := os.MkdirAll(frozen[liveRootTask], 0o755); err != nil {
		t.Fatalf("create live root: %v", err)
	}

	past := time.Now().Add(-31 * 24 * time.Hour)
	if err := filepath.WalkDir(filepath.Join(root, ".task_roots"), func(path string, _ fs.DirEntry, err error) error {
		if err != nil {
			return err
		}
		return os.Chtimes(path, past, past)
	}); err != nil {
		t.Fatalf("backdate task root index: %v", err)
	}

	d.runGC(context.Background())

	// Renaming is how we observe survival: a frozen record ignores the new
	// labels, a reclaimed one re-proposes from them.
	for _, tc := range []struct {
		taskID string
		want   bool
		why    string
	}{
		{runningTask, true, "a non-terminal task's frozen identity was swept; a re-dispatch would now get a second root"},
		{liveRootTask, true, "a record was swept while its env root still exists"},
		{terminalTask, false, "a terminal task's stranded record survived the sweep"},
	} {
		renamed := params(tc.taskID)
		renamed.WorkspaceSlug = "Renamed Workspace"
		renamed.IssueIdentifier = "NEW-6063"
		resolved, err := execenv.ResolveRootDir(renamed)
		if err != nil {
			t.Fatalf("resolve %s after GC: %v", tc.taskID, err)
		}
		if kept := resolved == frozen[tc.taskID]; kept != tc.want {
			t.Errorf("task %s: record kept = %v, want %v — %s", tc.taskID, kept, tc.want, tc.why)
		}
	}
}

func TestRunGC_MixedLayoutsUseMetadataWorkspaceIdentity(t *testing.T) {
	workspaceID := "a05b0e10-ee7a-4603-a72d-a548b2390cb2"
	legacyIssueID := "11111111-1111-1111-1111-111111111111"
	readableIssueID := "22222222-2222-2222-2222-222222222222"
	var batchRequests int

	mux := http.NewServeMux()
	mux.HandleFunc("/api/daemon/workspaces/"+workspaceID+"/issues/gc-check", func(w http.ResponseWriter, r *http.Request) {
		batchRequests++
		var body struct {
			IssueIDs []string `json:"issue_ids"`
		}
		if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
			t.Fatalf("decode batch request: %v", err)
		}
		issues := make([]map[string]any, 0, len(body.IssueIDs))
		for _, issueID := range body.IssueIDs {
			issues = append(issues, map[string]any{
				"id": issueID, "found": true, "status": "done",
				"updated_at": time.Now().Add(-10 * 24 * time.Hour),
			})
		}
		json.NewEncoder(w).Encode(map[string]any{"issues": issues})
	})

	d := newGCTestDaemon(t, mux)
	legacyDir := createTaskDir(t, d.cfg.WorkspacesRoot, workspaceID, "11111111", &execenv.GCMeta{
		Kind: execenv.GCKindIssue, IssueID: legacyIssueID, WorkspaceID: workspaceID,
		CompletedAt: time.Now().Add(-10 * 24 * time.Hour),
	})
	readableDir := createTaskDir(t, d.cfg.WorkspacesRoot, "asset-feed-a548b2390cb2", "mul-6063-b659c34a1dc3", &execenv.GCMeta{
		Kind: execenv.GCKindIssue, IssueID: readableIssueID, WorkspaceID: workspaceID,
		TaskID: "22222222-ee7a-4603-a72d-b659c34a1dc3", CompletedAt: time.Now().Add(-10 * 24 * time.Hour),
	})

	d.runGC(context.Background())

	if batchRequests != 2 {
		t.Fatalf("batch requests = %d, want one per physical layout", batchRequests)
	}
	for _, taskDir := range []string{legacyDir, readableDir} {
		if _, err := os.Stat(taskDir); !os.IsNotExist(err) {
			t.Fatalf("task dir %s was not reclaimed", taskDir)
		}
	}
}

func TestRunGC_PrunesOnlyTerminalAbandonedTaskRootRecords(t *testing.T) {
	t.Parallel()

	const (
		workspaceID     = "a05b0e10-ee7a-4603-a72d-a548b2390cb2"
		terminalTask    = "5c57b65b-ee7a-4603-a72d-b659c34a1dc3"
		runningTask     = "6d68c76c-ff8b-5704-b83e-c76ad45b2ed4"
		unavailableTask = "7e79d87d-008c-6805-c94f-d87be56c3fe5"
	)
	mux := http.NewServeMux()
	mux.HandleFunc("/api/daemon/tasks/", func(w http.ResponseWriter, r *http.Request) {
		status := "running"
		parts := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
		if len(parts) >= 2 && parts[len(parts)-2] == unavailableTask {
			http.Error(w, "temporarily unavailable", http.StatusServiceUnavailable)
			return
		}
		if len(parts) >= 2 && parts[len(parts)-2] == terminalTask {
			status = "failed"
		}
		json.NewEncoder(w).Encode(map[string]any{"status": status})
	})

	d := newGCTestDaemon(t, mux)
	d.cfg.GCOrphanTTL = time.Hour
	terminalParams := execenv.RootDirParams{
		WorkspacesRoot: d.cfg.WorkspacesRoot,
		WorkspaceID:    workspaceID,
		TaskID:         terminalTask,
	}
	runningParams := terminalParams
	runningParams.TaskID = runningTask
	unavailableParams := terminalParams
	unavailableParams.TaskID = unavailableTask
	terminalRoot, err := execenv.ResolveRootDir(terminalParams)
	if err != nil {
		t.Fatalf("resolve terminal root: %v", err)
	}
	runningRoot, err := execenv.ResolveRootDir(runningParams)
	if err != nil {
		t.Fatalf("resolve running root: %v", err)
	}
	unavailableRoot, err := execenv.ResolveRootDir(unavailableParams)
	if err != nil {
		t.Fatalf("resolve unavailable root: %v", err)
	}

	indexDir := filepath.Join(d.cfg.WorkspacesRoot, ".task_roots")
	entries, err := os.ReadDir(indexDir)
	if err != nil {
		t.Fatalf("read task root index: %v", err)
	}
	old := time.Now().Add(-2 * time.Hour)
	for _, entry := range entries {
		recordPath := filepath.Join(indexDir, entry.Name(), "root.json")
		if err := os.Chtimes(recordPath, old, old); err != nil {
			t.Fatalf("age task root record: %v", err)
		}
	}

	d.runGC(t.Context())

	terminalRenamed := terminalParams
	terminalRenamed.WorkspaceSlug = "Renamed Workspace"
	terminalRenamed.IssueIdentifier = "NEW-6063"
	terminalAfterGC, err := execenv.ResolveRootDir(terminalRenamed)
	if err != nil {
		t.Fatalf("resolve terminal root after GC: %v", err)
	}
	if terminalAfterGC == terminalRoot {
		t.Fatalf("terminal abandoned record still froze root %q", terminalAfterGC)
	}
	runningRenamed := runningParams
	runningRenamed.WorkspaceSlug = "Renamed Workspace"
	runningRenamed.IssueIdentifier = "NEW-6064"
	runningAfterGC, err := execenv.ResolveRootDir(runningRenamed)
	if err != nil {
		t.Fatalf("resolve running root after GC: %v", err)
	}
	if runningAfterGC != runningRoot {
		t.Fatalf("GC changed non-terminal task root from %q to %q", runningRoot, runningAfterGC)
	}
	unavailableRenamed := unavailableParams
	unavailableRenamed.WorkspaceSlug = "Renamed Workspace"
	unavailableRenamed.IssueIdentifier = "NEW-6065"
	unavailableAfterGC, err := execenv.ResolveRootDir(unavailableRenamed)
	if err != nil {
		t.Fatalf("resolve unavailable root after GC: %v", err)
	}
	if unavailableAfterGC != unavailableRoot {
		t.Fatalf("GC changed task root after an inconclusive status check from %q to %q", unavailableRoot, unavailableAfterGC)
	}
}

func TestShouldCleanTaskDir_DoneIssueOverTTL(t *testing.T) {
	t.Parallel()
	issueID := "11111111-1111-1111-1111-111111111111"

	mux := http.NewServeMux()
	mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		json.NewEncoder(w).Encode(map[string]any{
			"status":     "done",
			"updated_at": time.Now().Add(-10 * 24 * time.Hour), // 10 days ago
		})
	})

	d := newGCTestDaemon(t, mux)
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "task1", &execenv.GCMeta{
		IssueID:     issueID,
		WorkspaceID: "ws1",
		CompletedAt: time.Now().Add(-10 * 24 * time.Hour),
	})

	action := d.shouldCleanTaskDir(context.Background(), taskDir)
	if action != gcActionClean {
		t.Fatalf("expected gcActionClean, got %d", action)
	}
}

func TestShouldCleanTaskDir_CancelledIssueOverTTL(t *testing.T) {
	t.Parallel()
	issueID := "22222222-2222-2222-2222-222222222222"

	mux := http.NewServeMux()
	mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		json.NewEncoder(w).Encode(map[string]any{
			"status":     "cancelled",
			"updated_at": time.Now().Add(-6 * 24 * time.Hour),
		})
	})

	d := newGCTestDaemon(t, mux)
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "task2", &execenv.GCMeta{
		IssueID:     issueID,
		WorkspaceID: "ws1",
		CompletedAt: time.Now(),
	})

	action := d.shouldCleanTaskDir(context.Background(), taskDir)
	if action != gcActionClean {
		t.Fatalf("expected gcActionClean, got %d", action)
	}
}

func TestShouldCleanTaskDir_OpenIssueSkipped(t *testing.T) {
	t.Parallel()
	issueID := "33333333-3333-3333-3333-333333333333"

	mux := http.NewServeMux()
	mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		json.NewEncoder(w).Encode(map[string]any{
			"status":     "in_progress",
			"updated_at": time.Now().Add(-30 * 24 * time.Hour),
		})
	})

	d := newGCTestDaemon(t, mux)
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "task3", &execenv.GCMeta{
		IssueID:     issueID,
		WorkspaceID: "ws1",
		CompletedAt: time.Now(),
	})

	action := d.shouldCleanTaskDir(context.Background(), taskDir)
	if action != gcActionSkip {
		t.Fatalf("expected gcActionSkip for open issue, got %d", action)
	}
}

func TestShouldCleanTaskDir_DoneButRecentSkipped(t *testing.T) {
	t.Parallel()
	issueID := "44444444-4444-4444-4444-444444444444"

	mux := http.NewServeMux()
	mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		json.NewEncoder(w).Encode(map[string]any{
			"status":     "done",
			"updated_at": time.Now().Add(-1 * 24 * time.Hour), // 1 day ago, within TTL
		})
	})

	d := newGCTestDaemon(t, mux)
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "task4", &execenv.GCMeta{
		IssueID:     issueID,
		WorkspaceID: "ws1",
		CompletedAt: time.Now(),
	})

	action := d.shouldCleanTaskDir(context.Background(), taskDir)
	if action != gcActionSkip {
		t.Fatalf("expected gcActionSkip for recently-done issue, got %d", action)
	}
}

func TestShouldCleanTaskDir_NoMetaRecentSkipped(t *testing.T) {
	t.Parallel()

	d := newGCTestDaemon(t, http.NewServeMux())
	// No meta, fresh directory — should skip.
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "task5", nil)

	action := d.shouldCleanTaskDir(context.Background(), taskDir)
	if action != gcActionSkip {
		t.Fatalf("expected gcActionSkip for recent orphan, got %d", action)
	}
}

func TestShouldCleanTaskDir_NoMetaOldOrphan(t *testing.T) {
	t.Parallel()

	d := newGCTestDaemon(t, http.NewServeMux())
	d.cfg.GCOrphanTTL = 0 // treat all orphans as expired
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "task6", nil)

	action := d.shouldCleanTaskDir(context.Background(), taskDir)
	if action != gcActionOrphan {
		t.Fatalf("expected gcActionOrphan, got %d", action)
	}
}

func TestShouldCleanTaskDir_APIErrorSkipped(t *testing.T) {
	t.Parallel()
	issueID := "55555555-5555-5555-5555-555555555555"

	mux := http.NewServeMux()
	mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusInternalServerError)
	})

	d := newGCTestDaemon(t, mux)
	d.cfg.GCCompletedTaskTTL = time.Hour
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "task7", &execenv.GCMeta{
		IssueID:     issueID,
		WorkspaceID: "ws1",
		CompletedAt: time.Now().Add(-2 * time.Hour),
	})

	action := d.shouldCleanTaskDir(context.Background(), taskDir)
	if action != gcActionSkip {
		t.Fatalf("expected gcActionSkip on API error despite completed-task TTL, got %d", action)
	}
}

func TestShouldCleanTaskDir_Issue404OldOrphan(t *testing.T) {
	t.Parallel()
	issueID := "66666666-6666-6666-6666-666666666666"

	mux := http.NewServeMux()
	mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusNotFound)
		w.Write([]byte(`{"error":"issue not found"}`))
	})

	d := newGCTestDaemon(t, mux)
	d.cfg.GCOrphanTTL = 0 // treat orphans as immediately eligible
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "task8", &execenv.GCMeta{
		IssueID:     issueID,
		WorkspaceID: "ws1",
		CompletedAt: time.Now(),
	})

	action := d.shouldCleanTaskDir(context.Background(), taskDir)
	if action != gcActionOrphan {
		t.Fatalf("expected gcActionOrphan for unreachable issue past TTL, got %d", action)
	}
}

// TestShouldCleanTaskDir_Issue404RecentSkipped locks in the cross-workspace
// safety: the server returns 404 both for deleted issues and for workspaces
// the daemon token can't see, so a recent 404 must NOT trigger immediate
// cleanup — otherwise a token re-scope could wipe dirs whose issues are live.
func TestShouldCleanTaskDir_Issue404RecentSkipped(t *testing.T) {
	t.Parallel()
	issueID := "66666666-6666-6666-6666-666666666667"

	mux := http.NewServeMux()
	mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusNotFound)
		w.Write([]byte(`{"error":"not found"}`))
	})

	d := newGCTestDaemon(t, mux)
	// Default production OrphanTTL; taskDir mtime is now, so it's fresh.
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "fresh-404", &execenv.GCMeta{
		IssueID:     issueID,
		WorkspaceID: "ws1",
		CompletedAt: time.Now(),
	})

	action := d.shouldCleanTaskDir(context.Background(), taskDir)
	if action != gcActionSkip {
		t.Fatalf("expected gcActionSkip for recent 404 (cross-workspace safety), got %d", action)
	}
}

func TestCleanTaskDir_RemovesDirectory(t *testing.T) {
	t.Parallel()
	d := newGCTestDaemon(t, http.NewServeMux())
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "doomed", nil)
	writeFile(t, filepath.Join(taskDir, "workdir", "payload.bin"), 64)

	if _, err := os.Stat(taskDir); err != nil {
		t.Fatal("task dir should exist before cleanup")
	}

	if bytes, removed := d.cleanTaskDir(taskDir); !removed || bytes < 64 {
		t.Fatalf("reclaimed bytes = %d, want at least payload size", bytes)
	}

	if _, err := os.Stat(taskDir); !os.IsNotExist(err) {
		t.Fatal("task dir should be removed after cleanup")
	}
}

func TestRunGC_SharedRootPreservesForeignDirectories(t *testing.T) {
	t.Parallel()

	d := newGCTestDaemon(t, http.NewServeMux())
	d.cfg.GCOrphanTTL = 0
	projectDir := filepath.Join(d.cfg.WorkspacesRoot, "analyze-serenity-event-study-20260827")
	dataDir := filepath.Join(projectDir, "data")
	database := filepath.Join(dataDir, "research.sqlite")
	if err := os.MkdirAll(dataDir, 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(database, []byte("do not delete"), 0o644); err != nil {
		t.Fatal(err)
	}

	d.runGC(context.Background())

	if got, err := os.ReadFile(database); err != nil || string(got) != "do not delete" {
		t.Fatalf("GC mutated non-Multica data under a shared root: data=%q err=%v", got, err)
	}
}

// The incident regression above only proves the no-meta orphan path returns
// early. applyGCAction is the single gate every mutating action passes
// through, so pin all four: an unowned directory must survive full cleanup,
// orphan removal, pattern artifact cleanup and managed artifact cleanup alike.
func TestApplyGCAction_RefusesEveryMutationWithoutOwner(t *testing.T) {
	t.Parallel()

	for _, tc := range []struct {
		name   string
		action gcAction
	}{
		{"clean", gcActionClean},
		{"orphan", gcActionOrphan},
		{"artifacts", gcActionCleanArtifacts},
		{"managed artifacts", gcActionCleanManagedArtifacts},
	} {
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()

			d := newGCTestDaemon(t, http.NewServeMux())
			taskDir := filepath.Join(d.cfg.WorkspacesRoot, "source-repository", "data")
			// Give both artifact actions something they would delete if the
			// ownership gate were absent, so a passing test means refusal
			// rather than an empty fixture.
			artifactDir := filepath.Join(taskDir, d.cfg.GCArtifactPatterns[0])
			managedDir := filepath.Join(taskDir, execenv.ManagedReclaimableArtifactSubpaths()[0])
			for _, dir := range []string{artifactDir, managedDir} {
				if err := os.MkdirAll(dir, 0o755); err != nil {
					t.Fatal(err)
				}
				if err := os.WriteFile(filepath.Join(dir, "payload"), []byte("keep"), 0o644); err != nil {
					t.Fatal(err)
				}
			}
			database := filepath.Join(taskDir, "research.sqlite")
			if err := os.WriteFile(database, []byte("do not delete"), 0o644); err != nil {
				t.Fatal(err)
			}

			stats := &gcStats{byPattern: map[string]int{}}
			if removed := d.applyGCAction(context.Background(), taskDir, tc.action, stats); removed != 0 {
				t.Fatalf("applyGCAction reported %d removals on an unowned directory", removed)
			}
			if stats.cleaned != 0 || stats.orphaned != 0 || stats.artifactDirs != 0 || stats.bytesReclaimed != 0 {
				t.Fatalf("unowned mutation was counted: cleaned=%d orphaned=%d artifactDirs=%d bytes=%d",
					stats.cleaned, stats.orphaned, stats.artifactDirs, stats.bytesReclaimed)
			}
			for _, path := range []string{database, filepath.Join(artifactDir, "payload"), filepath.Join(managedDir, "payload")} {
				if _, err := os.Stat(path); err != nil {
					t.Fatalf("unowned content was mutated: %s: %v", path, err)
				}
			}
		})
	}
}

func TestCleanTaskDir_RefusesOwnerPathMismatch(t *testing.T) {
	t.Parallel()

	d := newGCTestDaemon(t, http.NewServeMux())
	taskDir := filepath.Join(d.cfg.WorkspacesRoot, "source-repository", "data")
	if err := os.MkdirAll(taskDir, 0o755); err != nil {
		t.Fatal(err)
	}
	owner, err := json.Marshal(execenv.EnvRootOwner{WorkspaceID: "workspace-id", TaskID: "task-id"})
	if err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(taskDir, ".task_owner"), owner, 0o644); err != nil {
		t.Fatal(err)
	}
	survivor := filepath.Join(taskDir, "research.sqlite")
	if err := os.WriteFile(survivor, []byte("keep"), 0o644); err != nil {
		t.Fatal(err)
	}

	if bytes, removed := d.cleanTaskDir(taskDir); removed || bytes != 0 {
		t.Fatalf("cleanTaskDir removed owner/path mismatch: removed=%v bytes=%d", removed, bytes)
	}
	if _, err := os.Stat(survivor); err != nil {
		t.Fatalf("owner/path mismatch data was not preserved: %v", err)
	}
}

func TestCleanTaskDir_AcceptsLegacyOwnerWithGCWorkspaceIdentity(t *testing.T) {
	t.Parallel()

	d := newGCTestDaemon(t, http.NewServeMux())
	taskDir := filepath.Join(d.cfg.WorkspacesRoot, "ws1", "task1")
	if err := os.MkdirAll(taskDir, 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(taskDir, ".task_owner"), []byte("task1"), 0o644); err != nil {
		t.Fatal(err)
	}
	meta, err := json.Marshal(execenv.GCMeta{Kind: execenv.GCKindIssue, WorkspaceID: "ws1"})
	if err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(taskDir, ".gc_meta.json"), meta, 0o644); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(taskDir, "payload"), []byte("owned"), 0o644); err != nil {
		t.Fatal(err)
	}

	if _, removed := d.cleanTaskDir(taskDir); !removed {
		t.Fatal("legacy task owner with matching GC workspace identity was not removed")
	}
	if _, err := os.Stat(taskDir); !os.IsNotExist(err) {
		t.Fatalf("legacy owned task directory still exists: %v", err)
	}
}

func TestCleanTaskDir_RemovesStableRootRecord(t *testing.T) {
	t.Parallel()

	root := t.TempDir()
	params := execenv.PrepareParams{
		WorkspacesRoot:  root,
		WorkspaceID:     "a05b0e10-ee7a-4603-a72d-a548b2390cb2",
		WorkspaceSlug:   "Asset Feed",
		TaskID:          "5c57b65b-ee7a-4603-a72d-b659c34a1dc3",
		IssueIdentifier: "MUL-6063",
		AgentName:       "GC Identity",
	}
	env, err := execenv.Prepare(params, slog.Default())
	if err != nil {
		t.Fatalf("Prepare: %v", err)
	}
	original := env.RootDir
	d := &Daemon{cfg: Config{WorkspacesRoot: root}, logger: slog.Default()}
	if bytes, removed := d.cleanTaskDir(original); !removed || bytes <= 0 {
		t.Fatalf("reclaimed bytes = %d, want owner metadata bytes", bytes)
	}

	resolved, err := execenv.ResolveRootDir(execenv.RootDirParams{
		WorkspacesRoot:  root,
		WorkspaceID:     params.WorkspaceID,
		WorkspaceSlug:   "Renamed Workspace",
		TaskID:          params.TaskID,
		IssueIdentifier: "NEW-6063",
	})
	if err != nil {
		t.Fatalf("ResolveRootDir after terminal GC: %v", err)
	}
	if resolved == original {
		t.Fatalf("terminal GC left stale root record %q", resolved)
	}
}

func TestGcWorkspace_CleansEmptyWorkspaceDir(t *testing.T) {
	t.Parallel()
	issueID := "77777777-7777-7777-7777-777777777777"

	mux := http.NewServeMux()
	mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		json.NewEncoder(w).Encode(map[string]any{
			"status":     "done",
			"updated_at": time.Now().Add(-10 * 24 * time.Hour),
		})
	})

	d := newGCTestDaemon(t, mux)
	wsDir := filepath.Join(d.cfg.WorkspacesRoot, "ws-empty")
	createTaskDir(t, d.cfg.WorkspacesRoot, "ws-empty", "only-task", &execenv.GCMeta{
		IssueID:     issueID,
		WorkspaceID: "ws-empty",
		CompletedAt: time.Now(),
	})

	d.gcWorkspace(context.Background(), wsDir, &gcStats{byPattern: map[string]int{}})

	if _, err := os.Stat(wsDir); !os.IsNotExist(err) {
		t.Fatal("empty workspace dir should be removed after all tasks cleaned")
	}
}

func TestGCWorkspace_BatchesAndDeduplicatesIssueChecks(t *testing.T) {
	doneID := "77777777-7777-7777-7777-777777777771"
	openID := "77777777-7777-7777-7777-777777777772"
	var batchRequests, legacyRequests int

	mux := http.NewServeMux()
	mux.HandleFunc("/api/daemon/workspaces/ws-batch/issues/gc-check", func(w http.ResponseWriter, r *http.Request) {
		batchRequests++
		if r.Method != http.MethodPost {
			t.Fatalf("batch method = %s, want POST", r.Method)
		}
		var body struct {
			IssueIDs []string `json:"issue_ids"`
		}
		if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
			t.Fatalf("decode batch request: %v", err)
		}
		if got, want := strings.Join(body.IssueIDs, ","), doneID+","+openID; got != want {
			t.Fatalf("batch issue_ids = %q, want %q", got, want)
		}
		json.NewEncoder(w).Encode(map[string]any{"issues": []map[string]any{
			{"id": doneID, "found": true, "status": "done", "updated_at": time.Now().Add(-10 * 24 * time.Hour)},
			{"id": openID, "found": true, "status": "in_progress", "updated_at": time.Now()},
		}})
	})
	mux.HandleFunc("/api/daemon/issues/", func(w http.ResponseWriter, r *http.Request) {
		legacyRequests++
		http.Error(w, "legacy endpoint must not be called", http.StatusInternalServerError)
	})

	d := newGCTestDaemon(t, mux)
	wsDir := filepath.Join(d.cfg.WorkspacesRoot, "ws-batch")
	doneA := createTaskDir(t, d.cfg.WorkspacesRoot, "ws-batch", "done-a", &execenv.GCMeta{
		IssueID: doneID, WorkspaceID: "ws-batch", CompletedAt: time.Now().Add(-10 * 24 * time.Hour),
	})
	doneB := createTaskDir(t, d.cfg.WorkspacesRoot, "ws-batch", "done-b", &execenv.GCMeta{
		IssueID: doneID, WorkspaceID: "ws-batch", CompletedAt: time.Now().Add(-10 * 24 * time.Hour),
	})
	open := createTaskDir(t, d.cfg.WorkspacesRoot, "ws-batch", "open", &execenv.GCMeta{
		IssueID: openID, WorkspaceID: "ws-batch", CompletedAt: time.Now(),
	})

	stats := &gcStats{byPattern: map[string]int{}}
	d.gcWorkspace(context.Background(), wsDir, stats)

	if batchRequests != 1 || legacyRequests != 0 {
		t.Fatalf("requests: batch=%d legacy=%d, want batch=1 legacy=0", batchRequests, legacyRequests)
	}
	for _, dir := range []string{doneA, doneB} {
		if _, err := os.Stat(dir); !os.IsNotExist(err) {
			t.Fatalf("done task dir %s was not removed", dir)
		}
	}
	if _, err := os.Stat(open); err != nil {
		t.Fatalf("open task dir should remain: %v", err)
	}
	if stats.cleaned != 2 || stats.skipped != 1 {
		t.Fatalf("stats = cleaned:%d skipped:%d, want 2/1", stats.cleaned, stats.skipped)
	}
}

func TestGCWorkspace_CompletedTaskTTLRemovesOpenIssue(t *testing.T) {
	t.Parallel()
	issueID := "77777777-7777-7777-7777-777777777770"

	mux := http.NewServeMux()
	mux.HandleFunc("/api/daemon/workspaces/ws-completed/issues/gc-check", func(w http.ResponseWriter, r *http.Request) {
		json.NewEncoder(w).Encode(map[string]any{"issues": []map[string]any{
			{"id": issueID, "found": true, "status": "in_review", "updated_at": time.Now()},
		}})
	})

	d := newGCTestDaemon(t, mux)
	d.cfg.GCCompletedTaskTTL = 24 * time.Hour
	d.cfg.GCArtifactTTL = 0
	wsDir := filepath.Join(d.cfg.WorkspacesRoot, "ws-completed")
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws-completed", "completed-open", &execenv.GCMeta{
		Kind:        execenv.GCKindIssue,
		IssueID:     issueID,
		WorkspaceID: "ws-completed",
		CompletedAt: time.Now().Add(-25 * time.Hour),
	})
	writeFile(t, filepath.Join(taskDir, "workdir", "checkout.bin"), 128)

	stats := &gcStats{byPattern: map[string]int{}}
	d.gcWorkspace(context.Background(), wsDir, stats)

	if _, err := os.Stat(taskDir); !os.IsNotExist(err) {
		t.Fatalf("completed open-issue task dir should be removed, stat error = %v", err)
	}
	if stats.cleaned != 1 || stats.skipped != 0 || stats.bytesReclaimed < 128 {
		t.Fatalf("unexpected stats after completed-task cleanup: %+v", stats)
	}
}

func TestGCWorkspace_OldServerFallbackIsCached(t *testing.T) {
	issueA := "77777777-7777-7777-7777-777777777773"
	issueB := "77777777-7777-7777-7777-777777777774"
	var batchRequests, legacyRequests int

	mux := http.NewServeMux()
	mux.HandleFunc("/api/daemon/workspaces/", func(w http.ResponseWriter, r *http.Request) {
		batchRequests++
		http.NotFound(w, r)
	})
	mux.HandleFunc("/api/daemon/issues/", func(w http.ResponseWriter, r *http.Request) {
		legacyRequests++
		json.NewEncoder(w).Encode(map[string]any{
			"status": "done", "updated_at": time.Now().Add(-10 * 24 * time.Hour),
		})
	})

	d := newGCTestDaemon(t, mux)
	for i, tc := range []struct {
		workspace string
		issueID   string
	}{{"ws-legacy-a", issueA}, {"ws-legacy-b", issueB}} {
		wsDir := filepath.Join(d.cfg.WorkspacesRoot, tc.workspace)
		taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, tc.workspace, fmt.Sprintf("task-%d", i), &execenv.GCMeta{
			IssueID: tc.issueID, WorkspaceID: tc.workspace, CompletedAt: time.Now().Add(-10 * 24 * time.Hour),
		})
		d.gcWorkspace(context.Background(), wsDir, &gcStats{byPattern: map[string]int{}})
		if _, err := os.Stat(taskDir); !os.IsNotExist(err) {
			t.Fatalf("legacy fallback did not clean %s", taskDir)
		}
	}

	if batchRequests != 1 {
		t.Fatalf("batch requests = %d, want 1 capability probe", batchRequests)
	}
	if legacyRequests != 2 {
		t.Fatalf("legacy requests = %d, want 2", legacyRequests)
	}
}

func TestGCWorkspace_BatchFailureDoesNotFanOutOrClean(t *testing.T) {
	issueID := "77777777-7777-7777-7777-777777777775"
	var batchRequests, legacyRequests int

	mux := http.NewServeMux()
	mux.HandleFunc("/api/daemon/workspaces/ws-fail/issues/gc-check", func(w http.ResponseWriter, r *http.Request) {
		batchRequests++
		http.Error(w, "temporary failure", http.StatusInternalServerError)
	})
	mux.HandleFunc("/api/daemon/issues/", func(w http.ResponseWriter, r *http.Request) {
		legacyRequests++
		json.NewEncoder(w).Encode(map[string]any{
			"status": "done", "updated_at": time.Now().Add(-10 * 24 * time.Hour),
		})
	})

	d := newGCTestDaemon(t, mux)
	d.cfg.GCCompletedTaskTTL = time.Hour
	wsDir := filepath.Join(d.cfg.WorkspacesRoot, "ws-fail")
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws-fail", "task", &execenv.GCMeta{
		IssueID: issueID, WorkspaceID: "ws-fail", CompletedAt: time.Now().Add(-10 * 24 * time.Hour),
	})
	stats := &gcStats{byPattern: map[string]int{}}
	d.gcWorkspace(context.Background(), wsDir, stats)

	if batchRequests != 1 || legacyRequests != 0 {
		t.Fatalf("requests: batch=%d legacy=%d, want batch=1 legacy=0", batchRequests, legacyRequests)
	}
	if _, err := os.Stat(taskDir); err != nil {
		t.Fatalf("task dir must remain on batch failure: %v", err)
	}
	if stats.cleaned != 0 || stats.orphaned != 0 || stats.skipped != 1 {
		t.Fatalf("unexpected stats after failure: %+v", stats)
	}
}

func TestGCWorkspace_ReclaimsLegacyCodexSandboxWithoutConfiguredPatterns(t *testing.T) {
	issueID := "77777777-7777-7777-7777-777777777776"

	mux := http.NewServeMux()
	mux.HandleFunc("/api/daemon/workspaces/ws-legacy-codex/issues/gc-check", func(w http.ResponseWriter, r *http.Request) {
		json.NewEncoder(w).Encode(map[string]any{"issues": []map[string]any{
			{"id": issueID, "found": true, "status": "in_progress", "updated_at": time.Now()},
		}})
	})

	d := newGCTestDaemon(t, mux)
	d.cfg.GCArtifactPatterns = nil
	wsDir := filepath.Join(d.cfg.WorkspacesRoot, "ws-legacy-codex")
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws-legacy-codex", "v0.4.0-task", nil)
	// Raw v0.4.0 metadata shape: no migration or newly introduced marker is
	// needed for an already-existing task to become eligible after upgrade.
	legacyMeta := fmt.Sprintf(
		`{"kind":"issue","issue_id":%q,"workspace_id":"ws-legacy-codex","completed_at":%q,"local_directory":true}`,
		issueID,
		time.Now().Add(-24*time.Hour).UTC().Format(time.RFC3339Nano),
	)
	if err := os.WriteFile(filepath.Join(taskDir, ".gc_meta.json"), []byte(legacyMeta), 0o644); err != nil {
		t.Fatal(err)
	}
	// A tiny representative file keeps the regression fast; the reported
	// Windows executable is about 325 MiB but has identical GC semantics.
	writeFile(t, filepath.Join(taskDir, "codex-home/.sandbox-bin/codex.exe"), 325)
	for _, rel := range []string{
		"codex-home/auth.json",
		"codex-home/config.toml",
		"codex-home/sessions/session.jsonl",
		"output/result.txt",
		"logs/task.log",
		"workdir/repo/.sandbox-bin/user-owned",
	} {
		writeFile(t, filepath.Join(taskDir, filepath.FromSlash(rel)), 10)
	}

	stats := &gcStats{byPattern: map[string]int{}}
	d.gcWorkspace(context.Background(), wsDir, stats)

	if _, err := os.Stat(filepath.Join(taskDir, "codex-home/.sandbox-bin")); !os.IsNotExist(err) {
		t.Fatalf("managed Codex sandbox should be removed, stat err=%v", err)
	}
	for _, rel := range []string{
		"codex-home/auth.json",
		"codex-home/config.toml",
		"codex-home/sessions/session.jsonl",
		"output/result.txt",
		"logs/task.log",
		"workdir/repo/.sandbox-bin/user-owned",
		".gc_meta.json",
	} {
		if _, err := os.Stat(filepath.Join(taskDir, filepath.FromSlash(rel))); err != nil {
			t.Errorf("expected %s to be preserved: %v", rel, err)
		}
	}
	managedPattern := managedArtifactPatternPrefix + "codex-home/.sandbox-bin"
	if stats.artifactDirs != 1 || stats.artifactRemoved != 1 || stats.bytesReclaimed != 325 || stats.skipped != 1 {
		t.Fatalf("unexpected managed cleanup stats: %+v", stats)
	}
	if stats.byPattern[managedPattern] != 1 {
		t.Fatalf("managed pattern stats = %+v, want %q=1", stats.byPattern, managedPattern)
	}
}

func TestShouldCleanTaskDir_OpenIssueArtifactCleanup(t *testing.T) {
	t.Parallel()
	issueID := "88888888-8888-8888-8888-888888888888"

	mux := http.NewServeMux()
	mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		json.NewEncoder(w).Encode(map[string]any{
			"status":     "in_progress",
			"updated_at": time.Now(),
		})
	})

	d := newGCTestDaemon(t, mux)
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "open-task", &execenv.GCMeta{
		IssueID:     issueID,
		WorkspaceID: "ws1",
		CompletedAt: time.Now().Add(-24 * time.Hour),
	})

	action := d.shouldCleanTaskDir(context.Background(), taskDir)
	if action != gcActionCleanArtifacts {
		t.Fatalf("expected gcActionCleanArtifacts for old completed task on open issue, got %d", action)
	}
}

func TestShouldCleanTaskDir_CompletedTaskTTLRemovesOpenIssue(t *testing.T) {
	t.Parallel()
	issueID := "88888888-8888-8888-8888-888888888880"

	mux := http.NewServeMux()
	mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		json.NewEncoder(w).Encode(map[string]any{
			"status":     "in_review",
			"updated_at": time.Now(),
		})
	})

	d := newGCTestDaemon(t, mux)
	d.cfg.GCCompletedTaskTTL = 24 * time.Hour
	d.cfg.GCArtifactTTL = 0
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "completed-open", &execenv.GCMeta{
		Kind:        execenv.GCKindIssue,
		IssueID:     issueID,
		WorkspaceID: "ws1",
		CompletedAt: time.Now().Add(-25 * time.Hour),
	})

	if action := d.shouldCleanTaskDir(context.Background(), taskDir); action != gcActionClean {
		t.Fatalf("expected completed-task TTL to clean open issue env, got %d", action)
	}

	recentTaskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "recently-completed-open", &execenv.GCMeta{
		Kind:        execenv.GCKindIssue,
		IssueID:     issueID,
		WorkspaceID: "ws1",
		CompletedAt: time.Now().Add(-23 * time.Hour),
	})
	if action := d.shouldCleanTaskDir(context.Background(), recentTaskDir); action != gcActionSkip {
		t.Fatalf("expected completed-task TTL to preserve a recent open-issue env, got %d", action)
	}
}

func TestShouldCleanTaskDir_CompletedTaskTTLUnknownIssueStatusFailsClosed(t *testing.T) {
	t.Parallel()
	issueID := "88888888-8888-8888-8888-888888888884"

	mux := http.NewServeMux()
	mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {
		json.NewEncoder(w).Encode(map[string]any{
			"status": "future_or_malformed_status", "updated_at": time.Now(),
		})
	})

	d := newGCTestDaemon(t, mux)
	d.cfg.GCCompletedTaskTTL = time.Hour
	d.cfg.GCArtifactTTL = 0
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "completed-unknown-status", &execenv.GCMeta{
		Kind:        execenv.GCKindIssue,
		IssueID:     issueID,
		WorkspaceID: "ws1",
		CompletedAt: time.Now().Add(-2 * time.Hour),
	})

	if action := d.shouldCleanTaskDir(context.Background(), taskDir); action != gcActionSkip {
		t.Fatalf("expected unknown issue status to fail closed, got %d", action)
	}
}

// MUL-7240 collapsed issue_status.category to four values and stopped
// projecting nonterminal custom statuses onto a built-in key, so the gc-check
// response started carrying keys this binary has never heard of. The full
// cleanup path required a status it recognized, which silently turned that path
// off for any workspace using a custom status — environment skeletons pile up
// for as long as the parent issue sits there. Deciding on the lifecycle
// category instead keeps it alive without teaching the daemon about custom
// keys. (MUL-7364)
func TestShouldCleanTaskDir_CompletedTaskTTLAcceptsCustomStatusByCategory(t *testing.T) {
	t.Parallel()
	issueID := "88888888-8888-8888-8888-88888888886a"

	mux := http.NewServeMux()
	mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {
		json.NewEncoder(w).Encode(map[string]any{
			"status": "human_review", "category": "started", "updated_at": time.Now(),
		})
	})

	d := newGCTestDaemon(t, mux)
	d.cfg.GCCompletedTaskTTL = time.Hour
	// Artifact cleanup does not consult the status at all, so switching it off
	// leaves the full-cleanup decision as the only thing this can measure.
	d.cfg.GCArtifactTTL = 0
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "completed-custom-status", &execenv.GCMeta{
		Kind:        execenv.GCKindIssue,
		IssueID:     issueID,
		WorkspaceID: "ws1",
		CompletedAt: time.Now().Add(-2 * time.Hour),
	})

	if action := d.shouldCleanTaskDir(context.Background(), taskDir); action != gcActionClean {
		t.Fatalf("expected full cleanup for a started-category custom status, got %d", action)
	}
}

// A category the daemon does not recognize is not an answer. It falls back to
// the legacy status enum, which fails closed on a raw custom key — data is kept
// rather than deleted on a response this binary cannot read. (MUL-7364)
func TestShouldCleanTaskDir_CompletedTaskTTLUnknownCategoryFallsBackAndFailsClosed(t *testing.T) {
	t.Parallel()
	issueID := "88888888-8888-8888-8888-88888888886b"

	mux := http.NewServeMux()
	mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {
		json.NewEncoder(w).Encode(map[string]any{
			"status": "human_review", "category": "lifecycle_from_the_future", "updated_at": time.Now(),
		})
	})

	d := newGCTestDaemon(t, mux)
	d.cfg.GCCompletedTaskTTL = time.Hour
	d.cfg.GCArtifactTTL = 0
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "completed-unknown-category", &execenv.GCMeta{
		Kind:        execenv.GCKindIssue,
		IssueID:     issueID,
		WorkspaceID: "ws1",
		CompletedAt: time.Now().Add(-2 * time.Hour),
	})

	if action := d.shouldCleanTaskDir(context.Background(), taskDir); action != gcActionSkip {
		t.Fatalf("expected unknown category to fail closed, got %d", action)
	}
}

// Terminality is a category question too: `closed` covers Cancelled and every
// custom status a workspace parks cancelled work on, and neither key is
// something this binary can enumerate. (MUL-7364)
func TestShouldCleanTaskDir_TerminalCategoryReclaimsWorkdir(t *testing.T) {
	t.Parallel()
	for _, tc := range []struct {
		name     string
		status   string
		category string
	}{
		{"custom done-category status", "gate_approved", "done"},
		{"custom closed-category status", "wont_fix", "closed"},
		{"built-in cancelled", "cancelled", "closed"},
	} {
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()
			issueID := "88888888-8888-8888-8888-88888888886c"

			mux := http.NewServeMux()
			mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {
				json.NewEncoder(w).Encode(map[string]any{
					"status": tc.status, "category": tc.category,
					"updated_at": time.Now().Add(-30 * 24 * time.Hour),
				})
			})

			d := newGCTestDaemon(t, mux)
			taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "terminal-category", &execenv.GCMeta{
				Kind: execenv.GCKindIssue, IssueID: issueID, WorkspaceID: "ws1",
				CompletedAt: time.Now().Add(-30 * 24 * time.Hour),
			})

			if action := d.shouldCleanTaskDir(context.Background(), taskDir); action != gcActionClean {
				t.Fatalf("expected terminal category to reclaim the workdir, got %d", action)
			}
		})
	}
}

func TestShouldCleanTaskDir_CompletedTaskTTLRequiresCompletionTime(t *testing.T) {
	t.Parallel()
	issueID := "88888888-8888-8888-8888-888888888881"

	mux := http.NewServeMux()
	mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {
		json.NewEncoder(w).Encode(map[string]any{
			"status": "blocked", "updated_at": time.Now(),
		})
	})

	d := newGCTestDaemon(t, mux)
	d.cfg.GCCompletedTaskTTL = time.Hour
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "missing-completion", &execenv.GCMeta{
		Kind: execenv.GCKindIssue, IssueID: issueID, WorkspaceID: "ws1",
	})

	if action := d.shouldCleanTaskDir(context.Background(), taskDir); action != gcActionSkip {
		t.Fatalf("expected task without completed_at to remain, got %d", action)
	}
}

func TestShouldCleanTaskDir_CompletedTaskTTLKeepsLocalDirectoryArtifactTTLSeparate(t *testing.T) {
	t.Parallel()
	issueID := "88888888-8888-8888-8888-888888888882"

	mux := http.NewServeMux()
	mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {
		json.NewEncoder(w).Encode(map[string]any{
			"status": "in_progress", "updated_at": time.Now(),
		})
	})

	d := newGCTestDaemon(t, mux)
	d.cfg.GCCompletedTaskTTL = time.Hour
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "local-directory-recent", &execenv.GCMeta{
		Kind:           execenv.GCKindIssue,
		IssueID:        issueID,
		WorkspaceID:    "ws1",
		CompletedAt:    time.Now().Add(-2 * time.Hour),
		LocalDirectory: true,
	})

	if action := d.shouldCleanTaskDir(context.Background(), taskDir); action != gcActionSkip {
		t.Fatalf("expected completed-task TTL not to accelerate local_directory artifact cleanup, got %d", action)
	}

	artifactEligibleDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "local-directory-artifact-eligible", &execenv.GCMeta{
		Kind:           execenv.GCKindIssue,
		IssueID:        issueID,
		WorkspaceID:    "ws1",
		CompletedAt:    time.Now().Add(-13 * time.Hour),
		LocalDirectory: true,
	})
	if action := d.shouldCleanTaskDir(context.Background(), artifactEligibleDir); action != gcActionCleanArtifacts {
		t.Fatalf("expected existing artifact TTL to remain effective for local_directory env, got %d", action)
	}
}

func TestShouldCleanTaskDir_CompletedTaskTTLPreservesActiveEnv(t *testing.T) {
	t.Parallel()
	issueID := "88888888-8888-8888-8888-888888888883"

	mux := http.NewServeMux()
	mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {
		json.NewEncoder(w).Encode(map[string]any{
			"status": "todo", "updated_at": time.Now(),
		})
	})

	d := newGCTestDaemon(t, mux)
	d.cfg.GCCompletedTaskTTL = time.Hour
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "active-completed", &execenv.GCMeta{
		Kind: execenv.GCKindIssue, IssueID: issueID, WorkspaceID: "ws1",
		CompletedAt: time.Now().Add(-2 * time.Hour),
	})
	defer d.markActiveEnvRoot(taskDir)()

	if action := d.shouldCleanTaskDir(context.Background(), taskDir); action != gcActionSkip {
		t.Fatalf("expected active env to remain despite completed-task TTL, got %d", action)
	}
}

func TestShouldCleanTaskDir_OpenIssueRecentTaskSkipped(t *testing.T) {
	t.Parallel()
	issueID := "88888888-8888-8888-8888-888888888889"

	mux := http.NewServeMux()
	mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		json.NewEncoder(w).Encode(map[string]any{
			"status":     "in_progress",
			"updated_at": time.Now(),
		})
	})

	d := newGCTestDaemon(t, mux)
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "fresh-task", &execenv.GCMeta{
		IssueID:     issueID,
		WorkspaceID: "ws1",
		CompletedAt: time.Now().Add(-1 * time.Minute),
	})

	if action := d.shouldCleanTaskDir(context.Background(), taskDir); action != gcActionSkip {
		t.Fatalf("expected gcActionSkip for fresh completed_at, got %d", action)
	}
}

func TestShouldCleanTaskDir_LegacyMetaUsesManagedOnlyFallback(t *testing.T) {
	t.Parallel()
	issueID := "88888888-8888-8888-8888-88888888888c"

	mux := http.NewServeMux()
	mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {
		json.NewEncoder(w).Encode(map[string]any{
			"status": "in_progress", "updated_at": time.Now(),
		})
	})

	d := newGCTestDaemon(t, mux)
	d.cfg.GCOrphanTTL = 72 * time.Hour
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "legacy-meta", &execenv.GCMeta{
		Kind: execenv.GCKindIssue, IssueID: issueID, WorkspaceID: "ws1",
	})
	old := time.Now().Add(-73 * time.Hour)
	if err := os.Chtimes(filepath.Join(taskDir, ".gc_meta.json"), old, old); err != nil {
		t.Fatal(err)
	}

	if action := d.shouldCleanTaskDir(context.Background(), taskDir); action != gcActionCleanManagedArtifacts {
		t.Fatalf("expected managed-only fallback for stale legacy meta, got %d", action)
	}

	recentDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "recent-legacy-meta", &execenv.GCMeta{
		Kind: execenv.GCKindIssue, IssueID: issueID, WorkspaceID: "ws1",
	})
	// Nested activity may leave taskDir's own mtime stale. A recently rewritten
	// metadata file is the authoritative fallback and must defer cleanup.
	if err := os.Chtimes(recentDir, old, old); err != nil {
		t.Fatal(err)
	}
	if action := d.shouldCleanTaskDir(context.Background(), recentDir); action != gcActionSkip {
		t.Fatalf("expected recent legacy meta to remain untouched, got %d", action)
	}
}

func TestShouldCleanTaskDir_ActiveEnvRootSkipsArtifactCleanup(t *testing.T) {
	t.Parallel()
	issueID := "88888888-8888-8888-8888-88888888888a"

	mux := http.NewServeMux()
	mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		json.NewEncoder(w).Encode(map[string]any{
			"status":     "in_progress",
			"updated_at": time.Now(),
		})
	})

	d := newGCTestDaemon(t, mux)
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "active-task", &execenv.GCMeta{
		IssueID:     issueID,
		WorkspaceID: "ws1",
		CompletedAt: time.Now().Add(-24 * time.Hour),
	})

	defer d.markActiveEnvRoot(taskDir)()

	if action := d.shouldCleanTaskDir(context.Background(), taskDir); action != gcActionSkip {
		t.Fatalf("expected gcActionSkip while task is active, got %d", action)
	}
}

func TestShouldCleanTaskDir_ActiveEnvRootSkipsFullCleanup(t *testing.T) {
	t.Parallel()
	issueID := "99999999-9999-9999-9999-999999999999"

	mux := http.NewServeMux()
	mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		// Done long enough ago to satisfy GCTTL — this would normally return
		// gcActionClean. But the env root is in use (e.g. a re-dispatched task
		// reuses the prior workdir of an already-done issue whose updated_at is
		// still stale, since a task re-claim doesn't advance it). Active-root
		// guard must override.
		json.NewEncoder(w).Encode(map[string]any{
			"status":     "done",
			"updated_at": time.Now().Add(-30 * 24 * time.Hour),
		})
	})

	d := newGCTestDaemon(t, mux)
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "active-done", &execenv.GCMeta{
		IssueID:     issueID,
		WorkspaceID: "ws1",
		CompletedAt: time.Now().Add(-30 * 24 * time.Hour),
	})

	defer d.markActiveEnvRoot(taskDir)()

	if action := d.shouldCleanTaskDir(context.Background(), taskDir); action != gcActionSkip {
		t.Fatalf("expected gcActionSkip on active env root with done+stale issue, got %d", action)
	}
}

func TestShouldCleanTaskDir_ActiveEnvRootSkipsOrphan404(t *testing.T) {
	t.Parallel()
	issueID := "99999999-9999-9999-9999-99999999999a"

	mux := http.NewServeMux()
	mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusNotFound)
		w.Write([]byte(`{"error":"not found"}`))
	})

	d := newGCTestDaemon(t, mux)
	d.cfg.GCOrphanTTL = 0 // would normally make this an immediate orphan delete
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "active-404", &execenv.GCMeta{
		IssueID:     issueID,
		WorkspaceID: "ws1",
		CompletedAt: time.Now(),
	})

	defer d.markActiveEnvRoot(taskDir)()

	if action := d.shouldCleanTaskDir(context.Background(), taskDir); action != gcActionSkip {
		t.Fatalf("expected gcActionSkip on active env root with 404 issue, got %d", action)
	}
}

func TestShouldCleanTaskDir_ActiveEnvRootSkipsNoMetaOrphan(t *testing.T) {
	t.Parallel()

	d := newGCTestDaemon(t, http.NewServeMux())
	d.cfg.GCOrphanTTL = 0
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "active-no-meta", nil)

	defer d.markActiveEnvRoot(taskDir)()

	if action := d.shouldCleanTaskDir(context.Background(), taskDir); action != gcActionSkip {
		t.Fatalf("expected gcActionSkip on active env root with no-meta orphan, got %d", action)
	}
}

func TestShouldCleanTaskDir_ArtifactTTLDisabled(t *testing.T) {
	t.Parallel()
	issueID := "88888888-8888-8888-8888-88888888888b"

	mux := http.NewServeMux()
	mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		json.NewEncoder(w).Encode(map[string]any{
			"status":     "in_progress",
			"updated_at": time.Now(),
		})
	})

	d := newGCTestDaemon(t, mux)
	d.cfg.GCArtifactTTL = 0
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "no-artifact-gc", &execenv.GCMeta{
		IssueID:     issueID,
		WorkspaceID: "ws1",
		CompletedAt: time.Now().Add(-100 * 24 * time.Hour),
	})

	if action := d.shouldCleanTaskDir(context.Background(), taskDir); action != gcActionSkip {
		t.Fatalf("expected gcActionSkip when artifact GC disabled, got %d", action)
	}
}

func TestShouldCleanTaskDir_ArtifactTTLDisabledSkipsLocalOrphanManagedCleanup(t *testing.T) {
	t.Parallel()
	issueID := "88888888-8888-8888-8888-88888888888d"

	mux := http.NewServeMux()
	mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {
		http.NotFound(w, r)
	})

	d := newGCTestDaemon(t, mux)
	d.cfg.GCArtifactTTL = 0
	d.cfg.GCOrphanTTL = 0
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "disabled-local-orphan", &execenv.GCMeta{
		Kind: execenv.GCKindIssue, IssueID: issueID, WorkspaceID: "ws1", LocalDirectory: true,
	})

	if action := d.shouldCleanTaskDir(context.Background(), taskDir); action != gcActionSkip {
		t.Fatalf("expected artifact TTL zero to disable managed local orphan cleanup, got %d", action)
	}
}

func TestCleanTaskArtifacts_RemovesOnlyMatchedDirs(t *testing.T) {
	t.Parallel()

	d := newGCTestDaemon(t, http.NewServeMux())
	taskDir := t.TempDir()

	// Create a synthetic project layout.
	mustMkdir := func(rel string) string {
		p := filepath.Join(taskDir, rel)
		if err := os.MkdirAll(p, 0o755); err != nil {
			t.Fatal(err)
		}
		return p
	}
	mustWrite := func(rel string, content string) {
		p := filepath.Join(taskDir, rel)
		if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
			t.Fatal(err)
		}
		if err := os.WriteFile(p, []byte(content), 0o644); err != nil {
			t.Fatal(err)
		}
	}

	mustMkdir("workdir/repo/src")
	mustWrite("workdir/repo/src/index.ts", "console.log('hi')")
	if out, err := exec.Command("git", "-C", filepath.Join(taskDir, "workdir/repo"), "init").CombinedOutput(); err != nil {
		t.Fatalf("git init: %v: %s", err, out)
	}
	mustWrite("workdir/repo/.git/objects/pack/test.pack", "binary")
	mustMkdir("workdir/repo/node_modules/lodash")
	mustWrite("workdir/repo/node_modules/lodash/index.js", "module.exports = {}")
	mustMkdir("workdir/repo/.next/cache")
	mustWrite("workdir/repo/.next/cache/page.html", "<html></html>")
	mustMkdir("workdir/repo/.turbo")
	mustWrite("workdir/repo/.turbo/log", "trace")
	mustMkdir("workdir/repo/dist") // not in default patterns — must be preserved
	mustWrite("workdir/repo/dist/main.js", "compiled")
	mustWrite(".gc_meta.json", `{"issue_id":"x"}`)
	mustMkdir("output")
	mustWrite("output/result.txt", "done")

	removed, bytes, perPattern := d.cleanTaskArtifacts(context.Background(), taskDir, []string{"node_modules", ".next", ".turbo"})

	if removed != 3 {
		t.Fatalf("expected 3 artifact dirs removed, got %d", removed)
	}
	if bytes <= 0 {
		t.Fatalf("expected non-zero bytes reclaimed, got %d", bytes)
	}
	if perPattern["node_modules"] != 1 || perPattern[".next"] != 1 || perPattern[".turbo"] != 1 {
		t.Fatalf("unexpected per-pattern counts: %+v", perPattern)
	}

	// Verify protected paths are intact.
	for _, rel := range []string{
		"workdir/repo/src/index.ts",
		"workdir/repo/.git/objects/pack/test.pack",
		"workdir/repo/dist/main.js",
		"output/result.txt",
		".gc_meta.json",
	} {
		if _, err := os.Stat(filepath.Join(taskDir, rel)); err != nil {
			t.Errorf("expected %s to be preserved, got %v", rel, err)
		}
	}

	// Verify removed paths are gone.
	for _, rel := range []string{
		"workdir/repo/node_modules",
		"workdir/repo/.next",
		"workdir/repo/.turbo",
	} {
		if _, err := os.Stat(filepath.Join(taskDir, rel)); !os.IsNotExist(err) {
			t.Errorf("expected %s to be removed, stat err=%v", rel, err)
		}
	}
}

func TestCleanTaskArtifacts_PreservesTrackedArtifactDirectory(t *testing.T) {
	t.Parallel()

	d := newGCTestDaemon(t, http.NewServeMux())
	taskDir := t.TempDir()
	repoDir := filepath.Join(taskDir, "workdir", "repo")
	tracked := filepath.Join(repoDir, ".next", "server", "page.js")
	untracked := filepath.Join(repoDir, "node_modules", "dependency", "index.js")
	for path, content := range map[string]string{
		tracked:   "committed build output",
		untracked: "regenerable dependency",
	} {
		if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
			t.Fatal(err)
		}
		if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
			t.Fatal(err)
		}
	}
	if out, err := exec.Command("git", "-C", repoDir, "init").CombinedOutput(); err != nil {
		t.Fatalf("git init: %v: %s", err, out)
	}
	if out, err := exec.Command("git", "-C", repoDir, "add", ".next/server/page.js").CombinedOutput(); err != nil {
		t.Fatalf("git add tracked artifact: %v: %s", err, out)
	}

	removed, _, perPattern := d.cleanTaskArtifacts(context.Background(), taskDir, []string{"node_modules", ".next"})

	if removed != 1 || perPattern["node_modules"] != 1 || perPattern[".next"] != 0 {
		t.Fatalf("removed=%d perPattern=%v, want only untracked node_modules removed", removed, perPattern)
	}
	if got, err := os.ReadFile(tracked); err != nil || string(got) != "committed build output" {
		t.Fatalf("tracked artifact was mutated: data=%q err=%v", got, err)
	}
	if _, err := os.Stat(filepath.Dir(filepath.Dir(untracked))); !os.IsNotExist(err) {
		t.Fatalf("untracked artifact directory survived: %v", err)
	}
}

func TestCleanTaskArtifacts_RejectsPatternsWithSeparators(t *testing.T) {
	t.Parallel()

	d := newGCTestDaemon(t, http.NewServeMux())
	taskDir := t.TempDir()
	if err := os.MkdirAll(filepath.Join(taskDir, "workdir", "node_modules"), 0o755); err != nil {
		t.Fatal(err)
	}

	removed, _, _ := d.cleanTaskArtifacts(context.Background(), taskDir, []string{"workdir/node_modules", "../etc"})
	if removed != 0 {
		t.Fatalf("expected 0 removals from separator-bearing patterns, got %d", removed)
	}
	if _, err := os.Stat(filepath.Join(taskDir, "workdir", "node_modules")); err != nil {
		t.Fatalf("dir should still exist, got %v", err)
	}
}

func TestCleanTaskArtifacts_DoesNotFollowSymlinks(t *testing.T) {
	t.Parallel()

	d := newGCTestDaemon(t, http.NewServeMux())
	taskDir := t.TempDir()
	outside := t.TempDir()
	keepFile := filepath.Join(outside, "keep.txt")
	if err := os.WriteFile(keepFile, []byte("safe"), 0o644); err != nil {
		t.Fatal(err)
	}

	if err := os.MkdirAll(filepath.Join(taskDir, "workdir"), 0o755); err != nil {
		t.Fatal(err)
	}
	linkPath := filepath.Join(taskDir, "workdir", "node_modules")
	if err := os.Symlink(outside, linkPath); err != nil {
		t.Skipf("symlink not supported: %v", err)
	}

	removed, _, _ := d.cleanTaskArtifacts(context.Background(), taskDir, []string{"node_modules"})
	if removed != 0 {
		t.Fatalf("expected 0 removals (symlinked node_modules), got %d", removed)
	}
	if _, err := os.Stat(keepFile); err != nil {
		t.Fatalf("symlinked target was deleted: %v", err)
	}
}

func TestCleanTaskArtifacts_ManagedPathIsExactAndDeduplicated(t *testing.T) {
	t.Parallel()

	d := newGCTestDaemon(t, http.NewServeMux())

	t.Run("managed only", func(t *testing.T) {
		taskDir := t.TempDir()
		writeFile(t, filepath.Join(taskDir, "codex-home/.sandbox-bin/codex.exe"), 300)
		writeFile(t, filepath.Join(taskDir, "workdir/repo/.sandbox-bin/keep"), 400)
		writeFile(t, filepath.Join(taskDir, "codex-home/auth.json"), 10)

		removed, bytes, perPattern := d.cleanTaskArtifacts(context.Background(), taskDir, nil)
		if removed != 1 || bytes != 300 {
			t.Fatalf("removed=%d bytes=%d, want 1/300", removed, bytes)
		}
		if perPattern[managedArtifactPatternPrefix+"codex-home/.sandbox-bin"] != 1 {
			t.Fatalf("unexpected per-pattern stats: %+v", perPattern)
		}
		for _, rel := range []string{"workdir/repo/.sandbox-bin/keep", "codex-home/auth.json"} {
			if _, err := os.Stat(filepath.Join(taskDir, filepath.FromSlash(rel))); err != nil {
				t.Errorf("expected %s to be preserved: %v", rel, err)
			}
		}
	})

	t.Run("explicit broad basename", func(t *testing.T) {
		taskDir := t.TempDir()
		writeFile(t, filepath.Join(taskDir, "codex-home/.sandbox-bin/codex.exe"), 300)
		writeFile(t, filepath.Join(taskDir, "workdir/repo/.sandbox-bin/cache"), 400)

		removed, bytes, perPattern := d.cleanTaskArtifacts(context.Background(), taskDir, []string{".sandbox-bin"})
		if removed != 2 || bytes != 700 {
			t.Fatalf("removed=%d bytes=%d, want 2/700 without double counting", removed, bytes)
		}
		if perPattern[managedArtifactPatternPrefix+"codex-home/.sandbox-bin"] != 1 || perPattern[".sandbox-bin"] != 1 {
			t.Fatalf("unexpected per-pattern stats: %+v", perPattern)
		}
	})
}

func TestCleanTaskArtifacts_ManagedPathDoesNotFollowSymlinks(t *testing.T) {
	t.Parallel()

	d := newGCTestDaemon(t, http.NewServeMux())

	for _, tc := range []struct {
		name     string
		linkPath string
	}{
		{name: "leaf", linkPath: "codex-home/.sandbox-bin"},
		{name: "parent", linkPath: "codex-home"},
	} {
		t.Run(tc.name, func(t *testing.T) {
			taskDir := t.TempDir()
			outside := t.TempDir()
			keepFile := filepath.Join(outside, "keep")
			writeFile(t, keepFile, 10)
			linkPath := filepath.Join(taskDir, filepath.FromSlash(tc.linkPath))
			if err := os.MkdirAll(filepath.Dir(linkPath), 0o755); err != nil {
				t.Fatal(err)
			}
			if err := os.Symlink(outside, linkPath); err != nil {
				t.Skipf("symlink not supported: %v", err)
			}

			removed, _, _ := d.cleanTaskArtifacts(context.Background(), taskDir, nil)
			if removed != 0 {
				t.Fatalf("removed=%d, want 0 for symlinked managed path", removed)
			}
			if _, err := os.Stat(keepFile); err != nil {
				t.Fatalf("symlink target was touched: %v", err)
			}
		})
	}
}

func TestActiveEnvRootRefcount(t *testing.T) {
	t.Parallel()

	d := newGCTestDaemon(t, http.NewServeMux())
	root := "/tmp/fake/env"

	if d.isActiveEnvRoot(root) {
		t.Fatal("expected inactive before mark")
	}
	releaseFirst := d.markActiveEnvRoot(root)
	releaseSecond := d.markActiveEnvRoot(root) // second mark from reuse path
	if !d.isActiveEnvRoot(root) {
		t.Fatal("expected active after mark")
	}
	releaseFirst()
	if !d.isActiveEnvRoot(root) {
		t.Fatal("expected still active after one unmark")
	}
	releaseSecond()
	if d.isActiveEnvRoot(root) {
		t.Fatal("expected inactive after both unmarks")
	}
}

func TestReserveEnvRootForGCIsExclusive(t *testing.T) {
	t.Parallel()

	d := newGCTestDaemon(t, http.NewServeMux())
	root := "/tmp/fake/gc-reservation"

	releaseActive := d.markActiveEnvRoot(root)
	if _, ok := d.reserveEnvRootForGC(root); ok {
		t.Fatal("GC reservation must fail while a task is active")
	}
	releaseActive()

	release, ok := d.reserveEnvRootForGC(root)
	if !ok {
		t.Fatal("expected GC reservation for inactive env root")
	}
	if _, ok := d.reserveEnvRootForGC(root); ok {
		t.Fatal("second GC reservation must fail while the first is held")
	}
	release()

	release, ok = d.reserveEnvRootForGC(root)
	if !ok {
		t.Fatal("expected GC reservation after release")
	}
	release()
}

func TestIsBareRepo(t *testing.T) {
	t.Parallel()

	t.Run("valid bare repo", func(t *testing.T) {
		dir := t.TempDir()
		os.WriteFile(filepath.Join(dir, "HEAD"), []byte("ref: refs/heads/main"), 0o644)
		os.MkdirAll(filepath.Join(dir, "objects"), 0o755)
		if !isBareRepo(dir) {
			t.Fatal("expected isBareRepo=true for dir with HEAD + objects/")
		}
	})

	t.Run("HEAD only", func(t *testing.T) {
		dir := t.TempDir()
		os.WriteFile(filepath.Join(dir, "HEAD"), []byte("ref: refs/heads/main"), 0o644)
		if isBareRepo(dir) {
			t.Fatal("expected isBareRepo=false for dir with only HEAD")
		}
	})

	t.Run("empty dir", func(t *testing.T) {
		dir := t.TempDir()
		if isBareRepo(dir) {
			t.Fatal("expected isBareRepo=false for empty dir")
		}
	})
}

func TestPruneWorktree_RemovesOnlyStaleAgentBranches(t *testing.T) {
	t.Parallel()

	d := newGCTestDaemon(t, http.NewServeMux())
	sourceRepo := createGCGitRepo(t)
	barePath := filepath.Join(t.TempDir(), "cache.git")

	runGitForGC(t, "", "clone", "--bare", sourceRepo, barePath)

	activeWorktree := filepath.Join(t.TempDir(), "active")
	activeBranch := "agent/live/12345678"
	staleBranch := "agent/stale/87654321"
	keepBranch := "main"

	runGitForGC(t, "", "-C", barePath, "worktree", "add", "-b", activeBranch, activeWorktree, "HEAD")
	runGitForGC(t, "", "-C", barePath, "branch", staleBranch, "HEAD")

	d.pruneWorktree(barePath)

	if gitRefExists(t, barePath, "refs/heads/"+staleBranch) {
		t.Fatalf("expected stale branch %q to be deleted", staleBranch)
	}
	if !gitRefExists(t, barePath, "refs/heads/"+activeBranch) {
		t.Fatalf("expected active branch %q to be preserved", activeBranch)
	}
	if !gitRefExists(t, barePath, "refs/heads/"+keepBranch) {
		t.Fatalf("expected non-agent branch %q to be preserved", keepBranch)
	}
}

func TestMaintainRepoCacheRunsLightCleanupWhileTaskActive(t *testing.T) {
	t.Parallel()

	d := newGCTestDaemon(t, http.NewServeMux())
	sourceRepo := createGCGitRepo(t)
	barePath := filepath.Join(t.TempDir(), "cache.git")
	runGitForGC(t, "", "clone", "--bare", sourceRepo, barePath)
	const staleBranch = "agent/stale/87654321"
	runGitForGC(t, "", "-C", barePath, "branch", staleBranch, "HEAD")

	d.activeTasks.Add(1)
	defer d.activeTasks.Add(-1)
	d.maintainRepoCache(context.Background(), barePath, &gcStats{})

	if gitRefExists(t, barePath, "refs/heads/"+staleBranch) {
		t.Fatalf("expected stale branch %q to be deleted while a task is active", staleBranch)
	}
	if _, err := os.Stat(filepath.Join(barePath, repoMaintenanceMarker)); err != nil {
		t.Fatalf("heavy maintenance should remain pending while a task is active: %v", err)
	}
}

// TestPruneWorktree_IgnoresLiteralAgentBranch ensures the GC pattern is scoped
// to the `agent/` namespace. A repo whose only `agent`-shaped ref is the
// literal `refs/heads/agent` (no slash) must be left untouched — the
// `for-each-ref` query is narrowed to `refs/heads/agent/` for that reason.
func TestPruneWorktree_IgnoresLiteralAgentBranch(t *testing.T) {
	t.Parallel()

	d := newGCTestDaemon(t, http.NewServeMux())
	sourceRepo := createGCGitRepo(t)
	barePath := filepath.Join(t.TempDir(), "cache.git")

	runGitForGC(t, "", "clone", "--bare", sourceRepo, barePath)
	runGitForGC(t, "", "-C", barePath, "branch", "agent", "HEAD")

	d.pruneWorktree(barePath)

	if !gitRefExists(t, barePath, "refs/heads/agent") {
		t.Fatal("expected literal `agent` branch outside the daemon namespace to be preserved")
	}
}

// TestPruneWorktree_SkipsMaintenanceWhenNothingDeleted pins the gate that
// keeps the heavy `gc --prune` step from running on every GC tick. Uses an
// unreachable loose blob backdated past the prune horizon as a sentinel: it
// survives when no agent branch was deleted (no maintenance), and disappears
// once a stale agent branch is reaped (maintenance ran).
func TestPruneWorktree_SkipsMaintenanceWhenNothingDeleted(t *testing.T) {
	t.Parallel()

	d := newGCTestDaemon(t, http.NewServeMux())
	sourceRepo := createGCGitRepo(t)
	barePath := filepath.Join(t.TempDir(), "cache.git")

	runGitForGC(t, "", "clone", "--bare", sourceRepo, barePath)

	// Park an active agent worktree so the scan has something to filter, and
	// to make sure pruneWorktree exercises the full code path.
	activeWorktree := filepath.Join(t.TempDir(), "active")
	runGitForGC(t, "", "-C", barePath, "worktree", "add", "-b", "agent/live/12345678", activeWorktree, "HEAD")

	sentinelPath := writeOldLooseBlob(t, barePath, "sentinel-content", 60*24*time.Hour)

	// No stale agent branch → no deletion → no `gc --prune`. The sentinel
	// blob must survive.
	d.pruneWorktree(barePath)
	if _, err := os.Stat(sentinelPath); err != nil {
		t.Fatalf("expected sentinel blob to survive when nothing was deleted: %v", err)
	}

	// Introduce a stale agent branch → deletion happens → maintenance runs →
	// `gc --prune=30.days` reaps the sentinel blob.
	runGitForGC(t, "", "-C", barePath, "branch", "agent/stale/87654321", "HEAD")
	d.pruneWorktree(barePath)
	if _, err := os.Stat(sentinelPath); !os.IsNotExist(err) {
		t.Fatalf("expected sentinel blob to be pruned after maintenance ran, stat err=%v", err)
	}
}

func TestPruneWorktree_RetriesPendingMaintenanceWithoutAnotherBranchDeletion(t *testing.T) {
	t.Parallel()

	d := newGCTestDaemon(t, http.NewServeMux())
	sourceRepo := createGCGitRepo(t)
	barePath := filepath.Join(t.TempDir(), "cache.git")
	runGitForGC(t, "", "clone", "--bare", sourceRepo, barePath)

	sentinelPath := writeOldLooseBlob(t, barePath, "pending-maintenance", 60*24*time.Hour)
	markerPath := filepath.Join(barePath, repoMaintenanceMarker)
	if err := os.WriteFile(markerPath, []byte("pending\n"), 0o600); err != nil {
		t.Fatal(err)
	}

	d.pruneWorktree(barePath)
	if _, err := os.Stat(sentinelPath); !os.IsNotExist(err) {
		t.Fatalf("expected pending maintenance to prune sentinel, stat err=%v", err)
	}
	if _, err := os.Stat(markerPath); !os.IsNotExist(err) {
		t.Fatalf("expected completed maintenance to clear pending marker, stat err=%v", err)
	}
}

// writeOldLooseBlob writes a dangling loose-object blob to the bare repo and
// backdates its mtime so `git gc --prune=30.days` will consider it prunable.
// Returns the absolute path to the loose object on disk.
func writeOldLooseBlob(t *testing.T, barePath, content string, age time.Duration) string {
	t.Helper()
	cmd := exec.Command("git", "-C", barePath, "hash-object", "-w", "--stdin")
	cmd.Stdin = strings.NewReader(content)
	out, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatalf("hash-object failed: %v: %s", err, out)
	}
	sha := strings.TrimSpace(string(out))
	if len(sha) < 4 {
		t.Fatalf("unexpected sha output: %q", sha)
	}
	loose := filepath.Join(barePath, "objects", sha[:2], sha[2:])
	if _, err := os.Stat(loose); err != nil {
		t.Fatalf("expected loose object at %s: %v", loose, err)
	}
	old := time.Now().Add(-age)
	if err := os.Chtimes(loose, old, old); err != nil {
		t.Fatalf("chtimes failed: %v", err)
	}
	return loose
}

func TestPruneWorktree_LegacyCacheFallbackSerializesWithCreateWorktree(t *testing.T) {
	t.Parallel()

	d := newGCTestDaemon(t, http.NewServeMux())
	sourceRepo := createGCGitRepo(t)
	cache := repocache.New(filepath.Join(d.cfg.WorkspacesRoot, ".repos"), slog.Default())
	if err := cache.Sync("ws1", []repocache.RepoInfo{{URL: sourceRepo}}); err != nil {
		t.Fatalf("cache sync failed: %v", err)
	}

	barePath := cache.Lookup("ws1", sourceRepo)
	if barePath == "" {
		t.Fatal("expected bare repo to be cached")
	}

	runGitForGC(t, "", "-C", barePath, "branch", "agent/stale/87654321", "HEAD")

	blockingCache := &blockingRepoCache{
		inner:   cache,
		entered: make(chan struct{}),
		release: make(chan struct{}),
	}
	d.repoCache = blockingCache

	pruneDone := make(chan struct{})
	go func() {
		d.pruneWorktree(barePath)
		close(pruneDone)
	}()

	select {
	case <-blockingCache.entered:
	case <-time.After(5 * time.Second):
		t.Fatal("timed out waiting for pruneWorktree to acquire repo lock")
	}

	createDone := make(chan error, 1)
	go func() {
		_, err := blockingCache.CreateWorktree(repocache.WorktreeParams{
			WorkspaceID: "ws1",
			RepoURL:     sourceRepo,
			WorkDir:     t.TempDir(),
			AgentName:   "tester",
			TaskID:      "11111111-1111-1111-1111-111111111111",
		})
		createDone <- err
	}()

	select {
	case err := <-createDone:
		t.Fatalf("CreateWorktree should wait for GC lock, returned early with err=%v", err)
	case <-time.After(200 * time.Millisecond):
	}

	close(blockingCache.release)

	select {
	case err := <-createDone:
		if err != nil {
			t.Fatalf("CreateWorktree failed after GC lock released: %v", err)
		}
	case <-time.After(5 * time.Second):
		t.Fatal("timed out waiting for CreateWorktree after releasing GC lock")
	}

	select {
	case <-pruneDone:
	case <-time.After(5 * time.Second):
		t.Fatal("timed out waiting for pruneWorktree to finish")
	}
}

func TestCleanupNewRepoMaintenanceLocksPreservesPreexistingFiles(t *testing.T) {
	t.Parallel()

	d := newGCTestDaemon(t, http.NewServeMux())
	barePath := t.TempDir()
	preexisting := filepath.Join(barePath, "logs", "refs", "heads", "main.lock")
	if err := os.MkdirAll(filepath.Dir(preexisting), 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(preexisting, []byte("existing"), 0o644); err != nil {
		t.Fatal(err)
	}
	before := snapshotRepoMaintenanceArtifacts(barePath)

	created := []string{
		filepath.Join(barePath, "refs", "remotes", "origin", "main.lock"),
		filepath.Join(barePath, "objects", "pack", "multi-pack-index.lock"),
		filepath.Join(barePath, "packed-refs.lock"),
		filepath.Join(barePath, "gc.pid"),
	}
	for _, path := range created {
		if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
			t.Fatal(err)
		}
		if err := os.WriteFile(path, []byte("new"), 0o644); err != nil {
			t.Fatal(err)
		}
	}
	outsideWhitelist := filepath.Join(barePath, "hooks", "user.lock")
	if err := os.MkdirAll(filepath.Dir(outsideWhitelist), 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(outsideWhitelist, []byte("keep"), 0o644); err != nil {
		t.Fatal(err)
	}

	d.cleanupNewRepoMaintenanceArtifacts(barePath, before)
	for _, path := range created {
		if _, err := os.Stat(path); !os.IsNotExist(err) {
			t.Errorf("new maintenance lock %s survived, stat error = %v", path, err)
		}
	}
	for _, path := range []string{preexisting, outsideWhitelist} {
		if _, err := os.Stat(path); err != nil {
			t.Errorf("unowned lock %s was removed: %v", path, err)
		}
	}
}

func TestPruneWorktreePreemptionCleansGitArtifactsBeforeTaskStarts(t *testing.T) {
	if _, err := os.Stat("/bin/sh"); err != nil {
		t.Skipf("requires a POSIX shell: %v", err)
	}
	realGit, err := exec.LookPath("git")
	if err != nil {
		t.Skipf("git not found: %v", err)
	}

	d := newGCTestDaemon(t, http.NewServeMux())
	sourceRepo := createGCGitRepo(t)
	cache := repocache.New(filepath.Join(d.cfg.WorkspacesRoot, ".repos"), slog.Default())
	if err := cache.Sync("ws1", []repocache.RepoInfo{{URL: sourceRepo}}); err != nil {
		t.Fatalf("cache sync failed: %v", err)
	}
	d.repoCache = cache
	barePath := cache.Lookup("ws1", sourceRepo)
	runGitForGC(t, "", "-C", barePath, "branch", "agent/stale/87654321", "HEAD")

	lockPath := filepath.Join(barePath, "refs", "remotes", "origin", "main.lock")
	if err := os.MkdirAll(filepath.Dir(lockPath), 0o755); err != nil {
		t.Fatal(err)
	}
	packDir := filepath.Join(barePath, "objects", "pack")
	newPack := filepath.Join(packDir, "tmp_pack_new")
	existingPack := filepath.Join(packDir, "tmp_pack_existing")
	if err := os.WriteFile(existingPack, []byte("existing"), 0o644); err != nil {
		t.Fatal(err)
	}
	startedPath := filepath.Join(t.TempDir(), "maintenance-started")
	binDir := t.TempDir()
	fakeGit := filepath.Join(binDir, "git")
	script := `#!/bin/sh
if [ "$3" = "gc" ]; then
  : > "$2/refs/remotes/origin/main.lock"
  : > "$2/objects/pack/tmp_pack_new"
  trap 'exit 143' TERM INT
  : > "$MULTICA_TEST_MAINTENANCE_STARTED"
  while :; do sleep 1; done
fi
exec "$MULTICA_TEST_REAL_GIT" "$@"
`
	if err := os.WriteFile(fakeGit, []byte(script), 0o755); err != nil {
		t.Fatal(err)
	}
	t.Setenv("MULTICA_TEST_REAL_GIT", realGit)
	t.Setenv("MULTICA_TEST_MAINTENANCE_STARTED", startedPath)
	t.Setenv("PATH", binDir+string(os.PathListSeparator)+os.Getenv("PATH"))

	pruneDone := make(chan struct{})
	go func() {
		d.pruneWorktree(barePath)
		close(pruneDone)
	}()
	deadline := time.Now().Add(5 * time.Second)
	for {
		if _, err := os.Stat(startedPath); err == nil {
			break
		}
		if time.Now().After(deadline) {
			t.Fatal("timed out waiting for heavy maintenance to start")
		}
		time.Sleep(10 * time.Millisecond)
	}

	// Match production task dispatch order: mark the task active, then cancel
	// maintenance and wait for its cleanup barrier before agent code starts.
	d.activeTasks.Add(1)
	defer d.activeTasks.Add(-1)
	cache.CancelMaintenance()
	select {
	case <-pruneDone:
	case <-time.After(5 * time.Second):
		t.Fatal("pruneWorktree did not finish after preemption")
	}
	if _, err := os.Stat(lockPath); !os.IsNotExist(err) {
		t.Fatalf("maintenance lock survived real task preemption, stat error = %v", err)
	}
	if _, err := os.Stat(newPack); !os.IsNotExist(err) {
		t.Fatalf("new temporary pack survived task preemption, stat error = %v", err)
	}
	if contents, err := os.ReadFile(existingPack); err != nil || string(contents) != "existing" {
		t.Fatalf("pre-existing temporary pack was changed: contents = %q, error = %v", contents, err)
	}
	if err := cache.Fetch(barePath); err != nil {
		t.Fatalf("fetch did not recover after preempted maintenance cleanup: %v", err)
	}
}

type blockingRepoCache struct {
	inner   *repocache.Cache
	entered chan struct{}
	release chan struct{}
}

func (c *blockingRepoCache) Lookup(workspaceID, url string) string {
	return c.inner.Lookup(workspaceID, url)
}

func (c *blockingRepoCache) BarePath(workspaceID, url string) string {
	return c.inner.BarePath(workspaceID, url)
}

func (c *blockingRepoCache) Sync(workspaceID string, repos []repocache.RepoInfo) error {
	return c.inner.Sync(workspaceID, repos)
}

func (c *blockingRepoCache) WithRepoLock(barePath string, fn func() error) error {
	return c.inner.WithRepoLock(barePath, func() error {
		close(c.entered)
		<-c.release
		return fn()
	})
}

func (c *blockingRepoCache) CreateWorktree(params repocache.WorktreeParams) (*repocache.WorktreeResult, error) {
	return c.inner.CreateWorktree(params)
}

// TestShouldCleanTaskDir_KindDispatch covers the four GCMeta kinds across
// active / terminal / 404 / non-terminal axes. Each entry stands up a mock
// server returning the expected payload (or 404) and asserts the action.
func TestShouldCleanTaskDir_KindDispatch(t *testing.T) {
	t.Parallel()

	const (
		issueID    = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaa01"
		chatID     = "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbb01"
		runID      = "cccccccc-cccc-cccc-cccc-cccccccccc01"
		quickTask  = "dddddddd-dddd-dddd-dddd-dddddddddd01"
		legacyMeta = "eeeeeeee-eeee-eeee-eeee-eeeeeeeeee01"
	)

	now := time.Now()
	overTTL := now.Add(-10 * 24 * time.Hour)
	withinTTL := now.Add(-1 * time.Hour)

	type serverResp struct {
		// Path to register on the mux. Empty entries are skipped (used for
		// 404 cases where the mux returns the default not-found handler).
		path   string
		status int
		body   map[string]any
	}

	cases := []struct {
		name    string
		meta    *execenv.GCMeta
		servers []serverResp
		want    gcAction
	}{
		// ---- chat ---------------------------------------------------------
		{
			name: "chat active session — never reclaimed",
			meta: &execenv.GCMeta{Kind: execenv.GCKindChat, ChatSessionID: chatID, WorkspaceID: "ws"},
			servers: []serverResp{{
				path: "/api/daemon/chat-sessions/" + chatID + "/gc-check",
				body: map[string]any{"status": "active", "updated_at": overTTL},
			}},
			want: gcActionSkip,
		},
		{
			name: "chat archived over TTL — clean",
			meta: &execenv.GCMeta{Kind: execenv.GCKindChat, ChatSessionID: chatID, WorkspaceID: "ws"},
			servers: []serverResp{{
				path: "/api/daemon/chat-sessions/" + chatID + "/gc-check",
				body: map[string]any{"status": "archived", "updated_at": overTTL},
			}},
			want: gcActionClean,
		},
		{
			name: "chat archived within TTL — skip",
			meta: &execenv.GCMeta{Kind: execenv.GCKindChat, ChatSessionID: chatID, WorkspaceID: "ws"},
			servers: []serverResp{{
				path: "/api/daemon/chat-sessions/" + chatID + "/gc-check",
				body: map[string]any{"status": "archived", "updated_at": withinTTL},
			}},
			want: gcActionSkip,
		},
		{
			name: "chat 404 — hard-deleted, clean immediately (no mtime gate)",
			meta: &execenv.GCMeta{Kind: execenv.GCKindChat, ChatSessionID: chatID, WorkspaceID: "ws"},
			servers: []serverResp{{
				path:   "/api/daemon/chat-sessions/" + chatID + "/gc-check",
				status: http.StatusNotFound,
			}},
			want: gcActionClean,
		},

		// ---- autopilot run -----------------------------------------------
		{
			name: "autopilot completed over TTL — clean",
			meta: &execenv.GCMeta{Kind: execenv.GCKindAutopilotRun, AutopilotRunID: runID, WorkspaceID: "ws"},
			servers: []serverResp{{
				path: "/api/daemon/autopilot-runs/" + runID + "/gc-check",
				body: map[string]any{"status": "completed", "completed_at": overTTL},
			}},
			want: gcActionClean,
		},
		{
			name: "autopilot issue_created counts as terminal",
			meta: &execenv.GCMeta{Kind: execenv.GCKindAutopilotRun, AutopilotRunID: runID, WorkspaceID: "ws"},
			servers: []serverResp{{
				path: "/api/daemon/autopilot-runs/" + runID + "/gc-check",
				body: map[string]any{"status": "issue_created", "completed_at": overTTL},
			}},
			want: gcActionClean,
		},
		{
			name: "autopilot running — skip",
			meta: &execenv.GCMeta{Kind: execenv.GCKindAutopilotRun, AutopilotRunID: runID, WorkspaceID: "ws"},
			servers: []serverResp{{
				path: "/api/daemon/autopilot-runs/" + runID + "/gc-check",
				body: map[string]any{"status": "running"},
			}},
			want: gcActionSkip,
		},
		{
			name: "autopilot pending — skip",
			meta: &execenv.GCMeta{Kind: execenv.GCKindAutopilotRun, AutopilotRunID: runID, WorkspaceID: "ws"},
			servers: []serverResp{{
				path: "/api/daemon/autopilot-runs/" + runID + "/gc-check",
				body: map[string]any{"status": "pending"},
			}},
			want: gcActionSkip,
		},
		{
			// The directory is never reused, so a terminal run is reclaimed on
			// sight — the recent completed_at no longer buys it a 24h reprieve.
			name: "autopilot completed within TTL — clean immediately (no 24h gate)",
			meta: &execenv.GCMeta{Kind: execenv.GCKindAutopilotRun, AutopilotRunID: runID, WorkspaceID: "ws"},
			servers: []serverResp{{
				path: "/api/daemon/autopilot-runs/" + runID + "/gc-check",
				body: map[string]any{"status": "completed", "completed_at": withinTTL},
			}},
			want: gcActionClean,
		},
		{
			// Terminal status with no completed_at stamp at all still cleans —
			// GC keys purely on the terminal status, not on any timestamp.
			name: "autopilot skipped with no completed_at — clean",
			meta: &execenv.GCMeta{Kind: execenv.GCKindAutopilotRun, AutopilotRunID: runID, WorkspaceID: "ws"},
			servers: []serverResp{{
				path: "/api/daemon/autopilot-runs/" + runID + "/gc-check",
				body: map[string]any{"status": "skipped"},
			}},
			want: gcActionClean,
		},
		{
			name: "autopilot failed — clean",
			meta: &execenv.GCMeta{Kind: execenv.GCKindAutopilotRun, AutopilotRunID: runID, WorkspaceID: "ws"},
			servers: []serverResp{{
				path: "/api/daemon/autopilot-runs/" + runID + "/gc-check",
				body: map[string]any{"status": "failed"},
			}},
			want: gcActionClean,
		},

		// ---- quick-create -------------------------------------------------
		{
			name: "quick_create completed task — clean immediately",
			meta: &execenv.GCMeta{Kind: execenv.GCKindQuickCreate, TaskID: quickTask, WorkspaceID: "ws"},
			servers: []serverResp{{
				path: "/api/daemon/tasks/" + quickTask + "/gc-check",
				body: map[string]any{"status": "completed", "completed_at": withinTTL},
			}},
			want: gcActionClean,
		},
		{
			name: "quick_create cancelled — clean",
			meta: &execenv.GCMeta{Kind: execenv.GCKindQuickCreate, TaskID: quickTask, WorkspaceID: "ws"},
			servers: []serverResp{{
				path: "/api/daemon/tasks/" + quickTask + "/gc-check",
				body: map[string]any{"status": "cancelled"},
			}},
			want: gcActionClean,
		},
		{
			name: "quick_create still running — skip",
			meta: &execenv.GCMeta{Kind: execenv.GCKindQuickCreate, TaskID: quickTask, WorkspaceID: "ws"},
			servers: []serverResp{{
				path: "/api/daemon/tasks/" + quickTask + "/gc-check",
				body: map[string]any{"status": "running"},
			}},
			want: gcActionSkip,
		},

		// ---- legacy meta (no kind) → issue path ---------------------------
		{
			name: "legacy meta with no kind defaults to issue path — done over TTL = clean",
			meta: &execenv.GCMeta{IssueID: legacyMeta, WorkspaceID: "ws"},
			servers: []serverResp{{
				path: "/api/daemon/issues/" + legacyMeta + "/gc-check",
				body: map[string]any{"status": "done", "updated_at": overTTL},
			}},
			want: gcActionClean,
		},
	}

	for _, tc := range cases {
		tc := tc
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()
			mux := http.NewServeMux()
			for _, s := range tc.servers {
				if s.path == "" {
					continue
				}
				resp := s
				mux.HandleFunc(resp.path, func(w http.ResponseWriter, r *http.Request) {
					if resp.status != 0 {
						w.WriteHeader(resp.status)
						return
					}
					w.Header().Set("Content-Type", "application/json")
					_ = json.NewEncoder(w).Encode(resp.body)
				})
			}
			d := newGCTestDaemon(t, mux)
			taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws", tc.name, tc.meta)
			got := d.shouldCleanTaskDir(context.Background(), taskDir)
			if got != tc.want {
				t.Fatalf("kind dispatch %q: want %d, got %d", tc.name, tc.want, got)
			}
		})
	}
}

func TestShouldCleanTaskDir_EmptyParentIDFallsBackToOrphanMTime(t *testing.T) {
	t.Parallel()

	cases := []struct {
		name string
		meta *execenv.GCMeta
	}{
		{
			name: "legacy issue meta",
			meta: &execenv.GCMeta{WorkspaceID: "ws"},
		},
		{
			name: "issue meta",
			meta: &execenv.GCMeta{Kind: execenv.GCKindIssue, WorkspaceID: "ws"},
		},
		{
			name: "chat meta",
			meta: &execenv.GCMeta{Kind: execenv.GCKindChat, WorkspaceID: "ws"},
		},
		{
			name: "autopilot run meta",
			meta: &execenv.GCMeta{Kind: execenv.GCKindAutopilotRun, WorkspaceID: "ws"},
		},
		{
			name: "quick create meta",
			meta: &execenv.GCMeta{Kind: execenv.GCKindQuickCreate, WorkspaceID: "ws"},
		},
	}

	for _, tc := range cases {
		tc := tc
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()

			requests := 0
			mux := http.NewServeMux()
			mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
				requests++
				http.Error(w, "unexpected request", http.StatusBadRequest)
			})

			d := newGCTestDaemon(t, mux)
			d.cfg.GCOrphanTTL = 365 * 24 * time.Hour
			taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws", tc.name, tc.meta)

			got := d.shouldCleanTaskDir(context.Background(), taskDir)
			if got != gcActionSkip {
				t.Fatalf("empty parent id should skip while under orphan TTL, got %d", got)
			}
			if requests != 0 {
				t.Fatalf("empty parent id should not call gc-check endpoint, got %d requests", requests)
			}

			old := time.Now().Add(-400 * 24 * time.Hour)
			if err := os.Chtimes(taskDir, old, old); err != nil {
				t.Fatalf("chtimes: %v", err)
			}
			got = d.shouldCleanTaskDir(context.Background(), taskDir)
			if got != gcActionOrphan {
				t.Fatalf("empty parent id over orphan TTL should orphan, got %d", got)
			}
			if requests != 0 {
				t.Fatalf("empty parent id should not call gc-check endpoint after mtime fallback, got %d requests", requests)
			}
		})
	}
}

func createGCGitRepo(t *testing.T) string {
	t.Helper()

	repoDir := t.TempDir()
	runGitForGC(t, repoDir, "init", "-b", "main")
	if err := os.WriteFile(filepath.Join(repoDir, "README.md"), []byte("hello\n"), 0o644); err != nil {
		t.Fatalf("write README: %v", err)
	}
	runGitForGC(t, repoDir, "add", "README.md")
	runGitForGC(t, repoDir, "commit", "-m", "initial commit")
	return repoDir
}

func runGitForGC(t *testing.T, dir string, args ...string) string {
	t.Helper()

	fullArgs := args
	if dir != "" {
		fullArgs = append([]string{"-C", dir}, args...)
	}
	cmd := exec.Command("git", fullArgs...)
	cmd.Env = append(os.Environ(),
		"GIT_AUTHOR_NAME=test", "[email protected]",
		"GIT_COMMITTER_NAME=test", "[email protected]",
	)
	out, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatalf("git %s failed: %s: %v", strings.Join(fullArgs, " "), out, err)
	}
	return strings.TrimSpace(string(out))
}

func gitRefExists(t *testing.T, repoPath, ref string) bool {
	t.Helper()

	cmd := exec.Command("git", "-C", repoPath, "show-ref", "--verify", "--quiet", ref)
	if err := cmd.Run(); err != nil {
		return false
	}
	return true
}

// TestShouldCleanTaskDir_ChatHardDeletedFreshMtime locks acceptance #3:
// when a user hard-deletes a chat session, the workdir must be reclaimed
// on the next GC cycle (≤ GCInterval), not deferred to GCOrphanTTL. A
// directory that was just created (mtime well within GCOrphanTTL) but
// whose chat session now 404s must therefore return gcActionClean.
func TestShouldCleanTaskDir_ChatHardDeletedFreshMtime(t *testing.T) {
	t.Parallel()
	chatID := "ffffffff-ffff-ffff-ffff-ffffffffff02"

	mux := http.NewServeMux()
	mux.HandleFunc(fmt.Sprintf("/api/daemon/chat-sessions/%s/gc-check", chatID), func(w http.ResponseWriter, r *http.Request) {
		// Simulate hard-deleted session (DeleteChatSession ran).
		w.WriteHeader(http.StatusNotFound)
	})

	d := newGCTestDaemon(t, mux)
	// Crank GCOrphanTTL up so the mtime path is unmistakably not in play —
	// the only way the directory gets reclaimed is the chat-404 fast path.
	d.cfg.GCOrphanTTL = 365 * 24 * time.Hour
	meta := &execenv.GCMeta{
		Kind:          execenv.GCKindChat,
		ChatSessionID: chatID,
		WorkspaceID:   "ws",
		CompletedAt:   time.Now(),
	}
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws", "hard-deleted-chat", meta)
	// taskDir mtime is now-ish — well within any sane GCOrphanTTL.

	if got := d.shouldCleanTaskDir(context.Background(), taskDir); got != gcActionClean {
		t.Fatalf("hard-deleted chat with fresh mtime must clean immediately, got %d", got)
	}
}

// An unarchived chat keeps its workdir even after the orphan TTL.
// Artifact cleanup can reclaim regenerable caches while preserving its data.
func TestShouldCleanTaskDir_ChatActiveResistsOldMtime(t *testing.T) {
	t.Parallel()
	chatID := "ffffffff-ffff-ffff-ffff-ffffffffff01"

	mux := http.NewServeMux()
	mux.HandleFunc(fmt.Sprintf("/api/daemon/chat-sessions/%s/gc-check", chatID), func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		_ = json.NewEncoder(w).Encode(map[string]any{
			"status":     "active",
			"updated_at": time.Now().Add(-100 * 24 * time.Hour),
		})
	})

	d := newGCTestDaemon(t, mux)
	d.cfg.GCOrphanTTL = 0 // every directory is "older than orphan TTL"
	meta := &execenv.GCMeta{
		Kind:          execenv.GCKindChat,
		ChatSessionID: chatID,
		WorkspaceID:   "ws",
		CompletedAt:   time.Now().Add(-200 * 24 * time.Hour),
	}
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws", "active-chat", meta)
	writeFile(t, filepath.Join(taskDir, "logs/run.log"), 32)
	writeFile(t, filepath.Join(taskDir, "output/result.md"), 32)
	if err := os.Chtimes(taskDir, time.Now().Add(-200*24*time.Hour), time.Now().Add(-200*24*time.Hour)); err != nil {
		t.Fatalf("chtimes: %v", err)
	}

	action := d.shouldCleanTaskDir(context.Background(), taskDir)
	if action == gcActionClean || action == gcActionOrphan {
		t.Fatalf("active chat session's directory must never be removed, got action %d", action)
	}
	d.applyGCAction(context.Background(), taskDir, action, &gcStats{byPattern: map[string]int{}})

	for _, rel := range []string{".", "logs/run.log", "output/result.md", ".gc_meta.json"} {
		if _, err := os.Stat(filepath.Join(taskDir, rel)); err != nil {
			t.Fatalf("active chat session must keep %s: %v", rel, err)
		}
	}
}

// TestGCMetaForTask covers the discriminator priority used by the daemon
// when selecting which GCMetaKind to write at task completion.
func TestGCMetaForTask(t *testing.T) {
	t.Parallel()
	cases := []struct {
		name string
		task Task
		want execenv.GCMetaKind
		idOK func(m execenv.GCMeta) bool
	}{
		{
			name: "chat task",
			task: Task{ID: "t1", WorkspaceID: "ws", ChatSessionID: "c1"},
			want: execenv.GCKindChat,
			idOK: func(m execenv.GCMeta) bool { return m.ChatSessionID == "c1" },
		},
		{
			name: "autopilot run task",
			task: Task{ID: "t2", WorkspaceID: "ws", AutopilotRunID: "r1"},
			want: execenv.GCKindAutopilotRun,
			idOK: func(m execenv.GCMeta) bool { return m.AutopilotRunID == "r1" },
		},
		{
			name: "issue task",
			task: Task{ID: "t3", WorkspaceID: "ws", IssueID: "i1"},
			want: execenv.GCKindIssue,
			idOK: func(m execenv.GCMeta) bool { return m.IssueID == "i1" },
		},
		{
			name: "quick-create task — issue_id always empty at WriteGCMeta time",
			task: Task{ID: "t4", WorkspaceID: "ws", QuickCreatePrompt: "do the thing"},
			want: execenv.GCKindQuickCreate,
			idOK: func(m execenv.GCMeta) bool { return m.TaskID == "t4" },
		},
		{
			name: "doc discussion task",
			task: Task{ID: "t6", WorkspaceID: "ws", DocID: "d1"},
			want: execenv.GCKindDoc,
			idOK: func(m execenv.GCMeta) bool { return m.DocID == "d1" },
		},
		{
			name: "chat wins over issue when both set (defensive ordering)",
			task: Task{ID: "t5", WorkspaceID: "ws", IssueID: "i1", ChatSessionID: "c1"},
			want: execenv.GCKindChat,
			idOK: func(m execenv.GCMeta) bool { return m.ChatSessionID == "c1" && m.IssueID == "" },
		},
	}
	for _, tc := range cases {
		tc := tc
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()
			meta, ok := gcMetaForTask(tc.task)
			if !ok {
				t.Fatalf("expected gcMetaForTask to recognize task, got ok=false")
			}
			if meta.Kind != tc.want {
				t.Fatalf("kind: want %q, got %q", tc.want, meta.Kind)
			}
			if !tc.idOK(meta) {
				t.Fatalf("ID field mismatch: %+v", meta)
			}
			if meta.WorkspaceID != "ws" {
				t.Fatalf("workspace_id: want %q, got %q", "ws", meta.WorkspaceID)
			}
			if meta.TaskID != tc.task.ID {
				t.Fatalf("task_id: want %q, got %q", tc.task.ID, meta.TaskID)
			}
		})
	}

	t.Run("unrecognized task — ok=false", func(t *testing.T) {
		t.Parallel()
		_, ok := gcMetaForTask(Task{ID: "tX", WorkspaceID: "ws"})
		if ok {
			t.Fatal("expected gcMetaForTask to return ok=false for task with no IDs")
		}
	})
}

// TestShouldCleanTaskDir_LocalDirectoryNeverClean confirms the GC loop
// never removes the envRoot of a local_directory task even when the parent
// issue is long-since done. Artifact-pattern cleanup is the most that
// should ever happen, so output/ and logs/ stay around for the user.
func TestShouldCleanTaskDir_LocalDirectoryNeverClean(t *testing.T) {
	t.Parallel()
	issueID := "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaa1"

	mux := http.NewServeMux()
	mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		json.NewEncoder(w).Encode(map[string]any{
			"status":     "done",
			"updated_at": time.Now().Add(-30 * 24 * time.Hour),
		})
	})

	d := newGCTestDaemon(t, mux)
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "local-task", &execenv.GCMeta{
		Kind:           execenv.GCKindIssue,
		IssueID:        issueID,
		WorkspaceID:    "ws1",
		CompletedAt:    time.Now().Add(-30 * 24 * time.Hour),
		LocalDirectory: true,
	})

	got := d.shouldCleanTaskDir(context.Background(), taskDir)
	if got == gcActionClean {
		t.Fatalf("expected local_directory task to never return gcActionClean, got gcActionClean")
	}
	// Either skip (no patterns configured) or artifact cleanup is OK —
	// what matters is that gcActionClean never fires for local_directory.
	if got != gcActionCleanArtifacts && got != gcActionSkip {
		t.Fatalf("unexpected action for local_directory done issue: %d", got)
	}
}

// TestShouldCleanTaskDir_LocalDirectoryOrphanUsesManagedOnly confirms that
// when the parent issue 404s, a local_directory task preserves its envRoot and
// becomes eligible only for exact daemon-managed artifact cleanup.
func TestShouldCleanTaskDir_LocalDirectoryOrphanUsesManagedOnly(t *testing.T) {
	t.Parallel()
	issueID := "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaa2"

	mux := http.NewServeMux()
	mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {
		http.NotFound(w, r)
	})

	d := newGCTestDaemon(t, mux)
	d.cfg.GCOrphanTTL = 0 // any age is "stale" enough to orphan
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "local-orphan", &execenv.GCMeta{
		Kind:           execenv.GCKindIssue,
		IssueID:        issueID,
		WorkspaceID:    "ws1",
		LocalDirectory: true,
	})

	got := d.shouldCleanTaskDir(context.Background(), taskDir)
	if got != gcActionCleanManagedArtifacts {
		t.Fatalf("expected local_directory orphan to use managed-only cleanup, got %d", got)
	}
}

// TestShouldCleanTaskDir_LocalDirectoryFalsePreservesNormalClean is the
// negative control: a regular (non-local_directory) task whose parent issue
// is done + over TTL must still be reclaimed via gcActionClean.
func TestShouldCleanTaskDir_LocalDirectoryFalsePreservesNormalClean(t *testing.T) {
	t.Parallel()
	issueID := "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaa3"

	mux := http.NewServeMux()
	mux.HandleFunc(fmt.Sprintf("/api/daemon/issues/%s/gc-check", issueID), func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		json.NewEncoder(w).Encode(map[string]any{
			"status":     "done",
			"updated_at": time.Now().Add(-30 * 24 * time.Hour),
		})
	})

	d := newGCTestDaemon(t, mux)
	taskDir := createTaskDir(t, d.cfg.WorkspacesRoot, "ws1", "normal-task", &execenv.GCMeta{
		Kind:        execenv.GCKindIssue,
		IssueID:     issueID,
		WorkspaceID: "ws1",
		CompletedAt: time.Now().Add(-30 * 24 * time.Hour),
		// LocalDirectory unset (false).
	})

	if got := d.shouldCleanTaskDir(context.Background(), taskDir); got != gcActionClean {
		t.Fatalf("expected gcActionClean for normal task, got %d", got)
	}
}