From 7dbb17a4cb240cb06f654f777ae94d672402d64f Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Wed, 22 Jul 2026 04:28:30 +1000 Subject: [PATCH] feat: pluggable storage backends with Docker volumes and /data/ layout MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Introduces the storage.Backend interface for pluggable container mount management. The Docker implementation (DockerBackend) is the MVP — no NFS/SMB/S3, just Docker volumes + bind mounts. /data/ volume layout: /data/worktree/ — bind mount of host worktree (always) /data/repo/ — per-repo Docker volume (when persistent_storage = true) /data/cache/go/ — shared GOMODCACHE volume /data/cache/node/— shared npm cache volume /data/cache/pnpm/— shared pnpm store volume /data/cache/pip/ — shared pip cache volume /data/cache/cargo/— shared Rust cargo cache volume /data/task.json — boot-time brief (existing WriteTaskJSON) Changes: - internal/storage/: Backend interface, DockerBackend, Mount types, ecosystem-to-cache-volume mapping, ConvertMounts helper - container.Pool.Acquire: now accepts []storage.Mount instead of hardcoding a single bind mount — mount list comes from the storage backend - daemon.process(): calls d.Storage.Setup() to build the mount list, passes Ecosystem + PersistentStorage from repo config - config.Repo.PersistentStorage: when true, a named Docker volume (archie-repo--) is mounted at /data/repo and survives task completion - main.go: creates DockerBackend when containers are enabled 21 tests: interface contract, ecosystem cache mounts (go/node/ python/rust/custom), worktree bind mount ordering, persistent storage on/off/empty-owner guard, mount list sort+dedup, adversarial (nonexistent worktree, empty ecosystem, unknown ecosystem). Why: daemon-managed cache volumes mean warm GOMODCACHE/npm cache across tasks. Per-repo persistent volumes preserve expensive build artifacts. The interface leaves room for future backends without rewiring container orchestration. --- cmd/archied/main.go | 13 ++ internal/config/config.go | 5 + internal/container/pool.go | 23 +- internal/daemon/daemon.go | 20 +- internal/storage/storage.go | 216 +++++++++++++++++ internal/storage/storage_test.go | 389 +++++++++++++++++++++++++++++++ 6 files changed, 651 insertions(+), 15 deletions(-) create mode 100644 internal/storage/storage.go create mode 100644 internal/storage/storage_test.go diff --git a/cmd/archied/main.go b/cmd/archied/main.go index c77b68d31..621472e3a 100644 --- a/cmd/archied/main.go +++ b/cmd/archied/main.go @@ -21,8 +21,11 @@ import ( "github.com/samcharles93/archie-core/internal/events" "github.com/samcharles93/archie-core/internal/forge" "github.com/samcharles93/archie-core/internal/nats" + "github.com/moby/moby/client" + "github.com/samcharles93/archie-core/internal/plugin" "github.com/samcharles93/archie-core/internal/plugin/pluginextract" + "github.com/samcharles93/archie-core/internal/storage" "github.com/samcharles93/archie-core/internal/store" "github.com/samcharles93/archie-core/internal/webui" "github.com/samcharles93/archie-core/internal/workflow/skillbuild" @@ -124,6 +127,7 @@ func run() int { // Container pool (optional — no containers when [containers] is absent). var containerPool *container.Pool + var storeBackend storage.Backend if cfg.Containers.Enabled { containerPool, err = container.NewPool(ctx, container.Config{ Image: cfg.Containers.Image, @@ -136,6 +140,14 @@ func run() int { return 1 } defer containerPool.Close() + + // Storage backend — Docker volume management for cache mounts. + dockerCli, err := client.New(client.FromEnv) + if err != nil { + log.Error("docker client for storage failed", "err", err) + return 1 + } + storeBackend = storage.NewDockerBackend(dockerCli) } providers := executionProviders(cfg) @@ -210,6 +222,7 @@ func run() int { Agent: agentRunner, Workflows: registry, PluginRegistry: pluginReg, + Storage: storeBackend, Log: log, CustomStages: wfeval.Discover, Nats: natsClient, diff --git a/internal/config/config.go b/internal/config/config.go index 491811445..f5f833d2b 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -57,6 +57,11 @@ type Repo struct { // test-file pattern (e.g. "*_test.go"). Empty inherits the // ecosystem default. TestGlob string `toml:"test_glob"` + // PersistentStorage creates a named Docker volume for this repo + // (archie-repo--) mounted at /data/repo. The volume + // survives task completion and retains data across tasks. Use for + // repos with expensive build artifacts or large dependency trees. + PersistentStorage bool `toml:"persistent_storage"` } // Protected reports whether path matches a protected suffix. diff --git a/internal/container/pool.go b/internal/container/pool.go index d3a22dfb0..81b1c34bd 100644 --- a/internal/container/pool.go +++ b/internal/container/pool.go @@ -16,8 +16,9 @@ import ( "time" "github.com/moby/moby/api/types/container" - "github.com/moby/moby/api/types/mount" "github.com/moby/moby/client" + + "github.com/samcharles93/archie-core/internal/storage" ) // Container wraps a running Docker container. @@ -106,11 +107,11 @@ func NewPool(ctx context.Context, cfg Config, natsURL string, log *slog.Logger) return p, nil } -// Acquire creates and starts a container, mounting the worktree at -// /data/worktree, passing provider API keys and NATS URL as environment -// variables. If MaxUptime is set, the container is created with a -// deadline — Docker kills it when the time elapses. -func (p *Pool) Acquire(ctx context.Context, workspace string, env []string) (*Container, error) { +// Acquire creates and starts a container with the given mounts and +// environment variables. Mounts are provided by the caller (typically +// from a storage.Backend). If MaxUptime is set, the container is +// created with a deadline — Docker kills it when the time elapses. +func (p *Pool) Acquire(ctx context.Context, mounts []storage.Mount, env []string) (*Container, error) { p.mu.Lock() if p.cfg.MaxConcurrency > 0 && p.active >= p.cfg.MaxConcurrency { p.mu.Unlock() @@ -138,13 +139,7 @@ func (p *Pool) Acquire(ctx context.Context, workspace string, env []string) (*Co }, }, HostConfig: &container.HostConfig{ - Mounts: []mount.Mount{ - { - Type: mount.TypeBind, - Source: workspace, - Target: "/data/worktree", - }, - }, + Mounts: storage.ConvertMounts(mounts), AutoRemove: true, }, }) @@ -164,7 +159,7 @@ func (p *Pool) Acquire(ctx context.Context, workspace string, env []string) (*Co return nil, fmt.Errorf("container start: %w", err) } - p.log.Info("container started", "id", resp.ID[:12], "name", name, "workspace", workspace) + p.log.Info("container started", "id", resp.ID[:12], "name", name) return &Container{ID: resp.ID}, nil } diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index 15aebfc00..3f2e000a2 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -25,6 +25,7 @@ import ( "github.com/samcharles93/archie-core/internal/forge" arnats "github.com/samcharles93/archie-core/internal/nats" "github.com/samcharles93/archie-core/internal/plugin" + "github.com/samcharles93/archie-core/internal/storage" "github.com/samcharles93/archie-core/internal/store" "github.com/samcharles93/archie-core/internal/workflow" "github.com/samcharles93/archie-core/internal/workflow/skillbuild" @@ -47,6 +48,9 @@ type Daemon struct { // ContainerPool manages Docker container lifecycle. Nil when [containers] // is not configured. When non-nil, every task gets a fresh container. ContainerPool *container.Pool + // Storage is the pluggable storage backend for container mounts. + // When nil (no containers configured), mount setup is skipped. + Storage storage.Backend // PluginRegistry holds core daemon plugins loaded from the configured // plugin_dir at startup (Layer 2). Nil when no plugin_dir is configured. PluginRegistry *plugin.Registry @@ -512,7 +516,21 @@ func (d *Daemon) process(ctx context.Context, task *store.Task) { Labels: strings.Split(task.Labels, ","), Workflow: task.Workflow, Branch: task.Branch, Plan: task.Plan, }) - ctr, err := d.ContainerPool.Acquire(ctx, workDir, d.containerEnv()) + + // Build the mount list from the storage backend. + mounts, err := d.Storage.Setup(ctx, storage.TaskRef{ + WorktreeDir: workDir, + Ecosystem: repo.Ecosystem, + PersistentStorage: repo.PersistentStorage, + Owner: task.Owner, + Repo: task.Repo, + }) + if err != nil { + d.Log.Error("storage setup failed", "err", err) + _ = d.Store.Transition(ctx, task.ID, store.StatusRunning, store.StatusParked, "storage setup failed: "+err.Error()) + return + } + ctr, err := d.ContainerPool.Acquire(ctx, mounts, d.containerEnv()) if err != nil { d.Log.Error("container acquire failed", "err", err) _ = d.Store.Transition(ctx, task.ID, store.StatusRunning, store.StatusParked, "container acquire failed: "+err.Error()) diff --git a/internal/storage/storage.go b/internal/storage/storage.go new file mode 100644 index 000000000..9de7fd3e8 --- /dev/null +++ b/internal/storage/storage.go @@ -0,0 +1,216 @@ +// Package storage defines the pluggable storage backend interface for agent +// container execution. The Docker backend (DockerBackend) is the MVP +// implementation — no NFS/SMB/S3 yet, just Docker volumes + bind mounts. +// +// /data/ volume layout: +// +// /data/ +// task.json — boot-time brief (written by container.WriteTaskJSON) +// worktree/ — bind mount of host worktree +// cache/ +// go/ — GOMODCACHE (shared across all tasks) +// node/ — npm cache (shared) +// pnpm/ — pnpm store (shared) +// pip/ — pip cache (shared) +// cargo/ — Rust cargo cache (shared) +// plugins/ — skill plugins staged from worktree +// +// Cache volumes are named Docker volumes created once and shared across all +// tasks. They are never deleted — the daemon operator manages them. +package storage + +import ( + "context" + "fmt" + "sort" + + "github.com/moby/moby/api/types/mount" + "github.com/moby/moby/client" +) + +// ── types ────────────────────────────────────────────────────────────── + +// TaskRef identifies a task for storage setup. +type TaskRef struct { + Owner, Repo string + IssueNumber int + Ecosystem string // "go", "node", "python", "rust", "custom" + WorktreeDir string // host path to the git worktree + PersistentStorage bool // if true, attach a per-repo Docker volume at /data/repo +} + +// Mount describes a container mount point. +type Mount struct { + Type string // "bind" or "volume" + Source string // host path (bind) or volume name (volume) + Destination string // container path +} + +// Mount type constants. +const ( + MountTypeBind = "bind" + MountTypeVolume = "volume" +) + +// Backend prepares container storage. Each container runtime backend +// (Docker, future containerd, etc.) implements this interface. +type Backend interface { + // Setup returns the mount specs for a task container. Must include + // at minimum a bind mount for the worktree at /data/worktree. + Setup(ctx context.Context, task TaskRef) ([]Mount, error) + // Teardown cleans up ephemeral storage. Cache volumes survive. + Teardown(ctx context.Context, task TaskRef) error +} + +// ── cache mounts per ecosystem ───────────────────────────────────────── + +// cacheVolume describes a named Docker volume that is shared across tasks. +type cacheVolume struct { + Name string // Docker volume name + MountPath string // container destination path +} + +// cacheVolumesByEcosystem maps ecosystem names to their shared cache volumes. +// Volumes are created once by the pool and never deleted. +var cacheVolumesByEcosystem = map[string][]cacheVolume{ + "go": { + {Name: "archie-cache-go", MountPath: "/data/cache/go"}, + }, + "node": { + {Name: "archie-cache-node", MountPath: "/data/cache/node"}, + {Name: "archie-cache-pnpm", MountPath: "/data/cache/pnpm"}, + }, + "python": { + {Name: "archie-cache-pip", MountPath: "/data/cache/pip"}, + }, + "rust": { + {Name: "archie-cache-cargo", MountPath: "/data/cache/cargo"}, + }, +} + +// cacheMounts returns the cache mount specs for an ecosystem, sorted by +// destination path for deterministic container configuration. +func cacheMounts(ecosystem string) []Mount { + vols, ok := cacheVolumesByEcosystem[ecosystem] + if !ok { + return nil + } + mounts := make([]Mount, len(vols)) + for i, v := range vols { + mounts[i] = Mount{ + Type: MountTypeVolume, + Source: v.Name, + Destination: v.MountPath, + } + } + sort.Slice(mounts, func(i, j int) bool { + return mounts[i].Destination < mounts[j].Destination + }) + return mounts +} + +// ── error types ──────────────────────────────────────────────────────── + +// ErrVolumeCreate is returned when a Docker volume cannot be created. +type ErrVolumeCreate struct { + Volume string + Cause error +} + +func (e *ErrVolumeCreate) Error() string { + return "create volume " + e.Volume + ": " + e.Cause.Error() +} + +func (e *ErrVolumeCreate) Unwrap() error { return e.Cause } + +// ── Docker backend ───────────────────────────────────────────────────── + +// DockerBackend implements Backend for Docker via the moby client. +type DockerBackend struct { + cli *client.Client +} + +// NewDockerBackend creates a Docker storage backend. +func NewDockerBackend(cli *client.Client) *DockerBackend { + return &DockerBackend{cli: cli} +} + +// ensureVolume creates a Docker volume if it doesn't already exist. +func (d *DockerBackend) ensureVolume(ctx context.Context, name string) error { + if d.cli == nil { + return nil // nil client for testing + } + _, err := d.cli.VolumeInspect(ctx, name, client.VolumeInspectOptions{}) + if err == nil { + return nil // already exists + } + _, err = d.cli.VolumeCreate(ctx, client.VolumeCreateOptions{ + Name: name, + Driver: "local", + }) + if err != nil { + return &ErrVolumeCreate{Volume: name, Cause: err} + } + return nil +} + +// repoVolumeName returns the Docker volume name for a repo's persistent storage. +func repoVolumeName(owner, repo string) string { + return fmt.Sprintf("archie-repo-%s-%s", owner, repo) +} + +// Setup returns the mount list for a task container. Mounts are ordered: +// 1. /data/worktree bind mount (always first) +// 2. /data/repo volume mount (only when PersistentStorage is true) +// 3. /data/cache/ volume mounts (sorted by destination) +func (d *DockerBackend) Setup(ctx context.Context, task TaskRef) ([]Mount, error) { + mounts := []Mount{ + { + Type: MountTypeBind, + Source: task.WorktreeDir, + Destination: "/data/worktree", + }, + } + + // Per-repo persistent volume. + if task.PersistentStorage && task.Owner != "" && task.Repo != "" { + volName := repoVolumeName(task.Owner, task.Repo) + if err := d.ensureVolume(ctx, volName); err != nil { + return nil, err + } + mounts = append(mounts, Mount{ + Type: MountTypeVolume, + Source: volName, + Destination: "/data/repo", + }) + } + + caches := cacheMounts(task.Ecosystem) + for _, m := range caches { + if err := d.ensureVolume(ctx, m.Source); err != nil { + return nil, err + } + } + mounts = append(mounts, caches...) + + return mounts, nil +} + +// Teardown is a no-op for Docker backend. Cache volumes survive task +// completion so subsequent tasks benefit from warmed caches. +func (d *DockerBackend) Teardown(ctx context.Context, task TaskRef) error { + return nil +} + +// ConvertMounts converts []Mount to moby mount.Mount for container creation. +func ConvertMounts(mounts []Mount) []mount.Mount { + out := make([]mount.Mount, len(mounts)) + for i, m := range mounts { + out[i] = mount.Mount{ + Type: mount.Type(m.Type), + Source: m.Source, + Target: m.Destination, + } + } + return out +} diff --git a/internal/storage/storage_test.go b/internal/storage/storage_test.go new file mode 100644 index 000000000..78207a27f --- /dev/null +++ b/internal/storage/storage_test.go @@ -0,0 +1,389 @@ +package storage + +import ( + "context" + "os" + "sort" + "testing" +) + +// ── interface contract ─────────────────────────────────────────────── + +func TestBackendInterfaceIsSatisfied(t *testing.T) { + // Compile-time check that DockerBackend satisfies Backend. + var _ Backend = (*DockerBackend)(nil) +} + +func TestTaskRefFields(t *testing.T) { + ref := TaskRef{ + Owner: "alice", + Repo: "demo", + IssueNumber: 42, + Ecosystem: "go", + WorktreeDir: "/tmp/worktree", + } + if ref.Owner != "alice" || ref.Repo != "demo" || ref.IssueNumber != 42 { + t.Fatal("TaskRef fields not assignable") + } +} + +func TestMountStruct(t *testing.T) { + m := Mount{ + Type: MountTypeVolume, + Source: "archie-cache-go", + Destination: "/data/cache/go", + } + if m.Type != MountTypeVolume || m.Source != "archie-cache-go" { + t.Fatal("Mount fields not assignable") + } +} + +// ── mount list helpers ─────────────────────────────────────────────── + +func TestMountListForEcosystemGo(t *testing.T) { + mounts := cacheMounts("go") + names := mountDestinations(mounts) + if !contains(names, "/data/cache/go") { + t.Errorf("go ecosystem missing /data/cache/go in %v", names) + } +} + +func TestMountListForEcosystemNode(t *testing.T) { + mounts := cacheMounts("node") + names := mountDestinations(mounts) + if !contains(names, "/data/cache/node") { + t.Errorf("node ecosystem missing /data/cache/node in %v", names) + } + if !contains(names, "/data/cache/pnpm") { + t.Errorf("node ecosystem missing /data/cache/pnpm in %v", names) + } +} + +func TestMountListForEcosystemPython(t *testing.T) { + mounts := cacheMounts("python") + names := mountDestinations(mounts) + if !contains(names, "/data/cache/pip") { + t.Errorf("python ecosystem missing /data/cache/pip in %v", names) + } +} + +func TestMountListForEcosystemRust(t *testing.T) { + mounts := cacheMounts("rust") + names := mountDestinations(mounts) + if !contains(names, "/data/cache/cargo") { + t.Errorf("rust ecosystem missing /data/cache/cargo in %v", names) + } +} + +func TestMountListUnknownEcosystemReturnsBaseOnly(t *testing.T) { + mounts := cacheMounts("custom") + if len(mounts) != 0 { + t.Errorf("custom ecosystem returned %d mounts, want 0", len(mounts)) + } +} + +func TestMountListIsSorted(t *testing.T) { + // Mounts must be sorted by Destination for deterministic container config. + mounts := cacheMounts("node") + if !sort.SliceIsSorted(mounts, func(i, j int) bool { + return mounts[i].Destination < mounts[j].Destination + }) { + t.Error("cacheMounts not sorted by Destination") + } +} + +func TestMountListNoDuplicates(t *testing.T) { + // Each destination must appear at most once. + mounts := cacheMounts("node") + seen := map[string]bool{} + for _, m := range mounts { + if seen[m.Destination] { + t.Errorf("duplicate mount destination %q", m.Destination) + } + seen[m.Destination] = true + } +} + +// ── Docker backend (unit tests, no Docker daemon required) ────────── + +func TestDockerBackendSetupWorktreeBindMount(t *testing.T) { + // Setup must always include the worktree bind mount at /data/worktree. + dir := t.TempDir() + if err := os.MkdirAll(dir, 0o755); err != nil { + t.Fatal(err) + } + + backend := &DockerBackend{} // nil client — Setup doesn't need it for bind mounts + mounts, err := backend.Setup(context.Background(), TaskRef{ + WorktreeDir: dir, + Ecosystem: "go", + }) + if err != nil { + t.Fatal(err) + } + + found := false + for _, m := range mounts { + if m.Destination == "/data/worktree" { + found = true + if m.Type != MountTypeBind { + t.Errorf("worktree mount type = %q, want bind", m.Type) + } + if m.Source != dir { + t.Errorf("worktree mount source = %q, want %q", m.Source, dir) + } + } + } + if !found { + t.Error("Setup did not include /data/worktree bind mount") + } +} + +func TestDockerBackendSetupIncludesCacheMounts(t *testing.T) { + dir := t.TempDir() + if err := os.MkdirAll(dir, 0o755); err != nil { + t.Fatal(err) + } + + backend := &DockerBackend{} + mounts, err := backend.Setup(context.Background(), TaskRef{ + WorktreeDir: dir, + Ecosystem: "node", + }) + if err != nil { + t.Fatal(err) + } + + for _, want := range []string{"/data/cache/node", "/data/cache/pnpm"} { + found := false + for _, m := range mounts { + if m.Destination == want { + found = true + if m.Type != MountTypeVolume { + t.Errorf("%s mount type = %q, want volume", want, m.Type) + } + } + } + if !found { + t.Errorf("Setup missing cache mount %s for node ecosystem", want) + } + } +} + +func TestDockerBackendSetupWorktreeMountIsFirst(t *testing.T) { + // Worktree bind mount must be the first entry — container create + // depends on this order for overlay semantics. + dir := t.TempDir() + if err := os.MkdirAll(dir, 0o755); err != nil { + t.Fatal(err) + } + + backend := &DockerBackend{} + mounts, err := backend.Setup(context.Background(), TaskRef{ + WorktreeDir: dir, + Ecosystem: "python", + }) + if err != nil { + t.Fatal(err) + } + + if len(mounts) == 0 { + t.Fatal("Setup returned no mounts") + } + if mounts[0].Destination != "/data/worktree" { + t.Errorf("first mount = %q, want /data/worktree", mounts[0].Destination) + } +} + +func TestDockerBackendTeardownSucceeds(t *testing.T) { + // Teardown is a no-op for Docker backend — cache volumes survive. + // Must not error. + backend := &DockerBackend{} + err := backend.Teardown(context.Background(), TaskRef{ + WorktreeDir: t.TempDir(), + }) + if err != nil { + t.Fatal("Teardown should not error for Docker backend:", err) + } +} + +// ── adversarial ────────────────────────────────────────────────────── + +func TestDockerBackendSetupEmptyWorktreeDir(t *testing.T) { + // An empty (non-existent) worktree dir must still produce valid mounts. + // The backend doesn't validate host paths — that's the pool's job. + backend := &DockerBackend{} + mounts, err := backend.Setup(context.Background(), TaskRef{ + WorktreeDir: "/nonexistent/worktree", + Ecosystem: "go", + }) + if err != nil { + t.Fatal("Setup errored on nonexistent worktree:", err) + } + found := false + for _, m := range mounts { + if m.Destination == "/data/worktree" { + found = true + } + } + if !found { + t.Error("worktree mount missing for nonexistent path") + } +} + +func TestDockerBackendSetupEmptyEcosystem(t *testing.T) { + // Empty ecosystem must not cause nil pointer or crash. + dir := t.TempDir() + if err := os.MkdirAll(dir, 0o755); err != nil { + t.Fatal(err) + } + + backend := &DockerBackend{} + mounts, err := backend.Setup(context.Background(), TaskRef{ + WorktreeDir: dir, + Ecosystem: "", + }) + if err != nil { + t.Fatal("Setup errored on empty ecosystem:", err) + } + // Must at minimum have the worktree bind mount. + if len(mounts) == 0 { + t.Error("Setup returned no mounts for empty ecosystem") + } +} + +func TestDockerBackendSetupUnknownEcosystemSkipped(t *testing.T) { + // Unknown ecosystem must not add cache mounts, but must not error. + dir := t.TempDir() + if err := os.MkdirAll(dir, 0o755); err != nil { + t.Fatal(err) + } + + backend := &DockerBackend{} + mounts, err := backend.Setup(context.Background(), TaskRef{ + WorktreeDir: dir, + Ecosystem: "haskell", // not in the cache map + }) + if err != nil { + t.Fatal("Setup errored on unknown ecosystem:", err) + } + for _, m := range mounts { + if m.Destination != "/data/worktree" { + t.Errorf("unexpected mount %s for unknown ecosystem", m.Destination) + } + } +} + +// ── persistent storage ──────────────────────────────────────────────── + +func TestDockerBackendSetupWithPersistentStorage(t *testing.T) { + dir := t.TempDir() + if err := os.MkdirAll(dir, 0o755); err != nil { + t.Fatal(err) + } + + backend := &DockerBackend{} + mounts, err := backend.Setup(context.Background(), TaskRef{ + WorktreeDir: dir, + Ecosystem: "go", + PersistentStorage: true, + Owner: "alice", + Repo: "demo", + }) + if err != nil { + t.Fatal(err) + } + + // Must include /data/repo volume mount. + found := false + for _, m := range mounts { + if m.Destination == "/data/repo" { + found = true + if m.Type != MountTypeVolume { + t.Errorf("/data/repo mount type = %q, want volume", m.Type) + } + if m.Source != "archie-repo-alice-demo" { + t.Errorf("/data/repo volume name = %q, want archie-repo-alice-demo", m.Source) + } + } + } + if !found { + t.Error("persistent storage enabled but /data/repo mount missing") + } +} + +func TestDockerBackendSetupWithoutPersistentStorage(t *testing.T) { + dir := t.TempDir() + if err := os.MkdirAll(dir, 0o755); err != nil { + t.Fatal(err) + } + + backend := &DockerBackend{} + mounts, err := backend.Setup(context.Background(), TaskRef{ + WorktreeDir: dir, + Ecosystem: "go", + PersistentStorage: false, + }) + if err != nil { + t.Fatal(err) + } + + for _, m := range mounts { + if m.Destination == "/data/repo" { + t.Error("/data/repo mount present but PersistentStorage is false") + } + } +} + +func TestDockerBackendSetupPersistentStorageEmptyOwner(t *testing.T) { + // With empty Owner/Repo, no per-repo volume should be created even + // when PersistentStorage is true — we can't name the volume. + dir := t.TempDir() + if err := os.MkdirAll(dir, 0o755); err != nil { + t.Fatal(err) + } + + backend := &DockerBackend{} + mounts, err := backend.Setup(context.Background(), TaskRef{ + WorktreeDir: dir, + Ecosystem: "go", + PersistentStorage: true, + Owner: "", + Repo: "", + }) + if err != nil { + t.Fatal(err) + } + + for _, m := range mounts { + if m.Destination == "/data/repo" { + t.Error("/data/repo mount present but Owner is empty") + } + } +} + +func TestRepoVolumeName(t *testing.T) { + name := repoVolumeName("myorg", "myrepo") + if name != "archie-repo-myorg-myrepo" { + t.Errorf("repoVolumeName = %q, want archie-repo-myorg-myrepo", name) + } +} + +// ── helpers ────────────────────────────────────────────────────────── + +func mountDestinations(mounts []Mount) []string { + var out []string + for _, m := range mounts { + out = append(out, m.Destination) + } + return out +} + +func contains(slice []string, s string) bool { + for _, v := range slice { + if v == s { + return true + } + } + return false +}