fix(docker): isolate per-container agent state from the host home
Every ACP agent's session dir was bind-mounted from ~/.<agent> on the host into /root/.<agent> in the container. That exposed host state DBs, session caches, and lock files to the container — codex's rollout DB caches absolute /Users/... paths, which don't resolve inside Linux and the agent silently loses prior thread context on resume. Replace the host bind mount with a kandev-managed per-container session dir under <kandev-home>/agent-sessions/<instance_id>/. Before the container starts, selectively copy the auth-relevant files (the same files Sprites already copies via RemoteAuth.SourceFiles) into that dir; everything else — state DBs, sessions/, lock files — stays on the host and the agent rebuilds them inside the container with container-local Linux paths. The change is agent-agnostic: any agent that has SessionDirTemplate + SessionDirTarget gets the new behavior automatically. Cleanup runs on destructive stops only (task/session deleted/archived), mirroring the Sprites preserve-on-stop rule so plain stops let resume re-attach to the same agent state.
This commit is contained in:
@@ -60,7 +60,7 @@ func provideLifecycleManager(
|
||||
|
||||
// Register Docker runtime if enabled (client is created lazily on first use)
|
||||
if cfg.Docker.Enabled {
|
||||
dockerExec := lifecycle.NewDockerExecutor(cfg.Docker, log)
|
||||
dockerExec := lifecycle.NewDockerExecutor(cfg.Docker, cfg.ResolvedHomeDir(), log)
|
||||
executorRegistry.Register(dockerExec)
|
||||
log.Info("Docker runtime registered (lazy initialization)")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
package lifecycle
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/kandev/kandev/internal/agent/agents"
|
||||
"github.com/kandev/kandev/internal/agent/remoteauth"
|
||||
"github.com/kandev/kandev/internal/common/logger"
|
||||
)
|
||||
|
||||
// agentSessionsRootSubdir is the subdirectory under the kandev home where
|
||||
// per-container agent session dirs live. e.g. ~/.kandev/agent-sessions/.
|
||||
const agentSessionsRootSubdir = "agent-sessions"
|
||||
|
||||
// authMethodTypeFiles selects RemoteAuthMethod entries that copy local files
|
||||
// into the remote environment (vs env-var-based auth).
|
||||
const authMethodTypeFiles = "files"
|
||||
|
||||
// AgentSessionsRoot returns the directory under the kandev home where
|
||||
// per-container session dirs are created (e.g. ~/.kandev/agent-sessions/).
|
||||
func AgentSessionsRoot(kandevHomeDir string) string {
|
||||
if kandevHomeDir == "" {
|
||||
return ""
|
||||
}
|
||||
return filepath.Join(kandevHomeDir, agentSessionsRootSubdir)
|
||||
}
|
||||
|
||||
// InstanceSessionRoot returns the per-container parent dir that mimics the
|
||||
// agent's home (e.g. ~/.kandev/agent-sessions/<instance_id>/). Files seeded
|
||||
// from the host land under this path keyed by their RemoteAuth.TargetRelDir,
|
||||
// matching the tree the bind-mount target expects inside the container.
|
||||
func InstanceSessionRoot(kandevHomeDir, instanceID string) string {
|
||||
if kandevHomeDir == "" || instanceID == "" {
|
||||
return ""
|
||||
}
|
||||
return filepath.Join(AgentSessionsRoot(kandevHomeDir), instanceID)
|
||||
}
|
||||
|
||||
// SessionDirHostPath returns the kandev-managed bind-mount source for one
|
||||
// agent in one container. It mirrors the structure SessionDirTarget expects
|
||||
// inside the container: e.g. SessionDirTemplate "{home}/.codex" + target
|
||||
// "/root/.codex" → host path "<kandev-home>/agent-sessions/<instanceID>/
|
||||
// .codex". The leaf dotdir is preserved so the tree on the host matches the
|
||||
// tree the agent expects, which means UploadCredentialFiles + the
|
||||
// inside-container view of the bind mount are byte-identical.
|
||||
func SessionDirHostPath(kandevHomeDir, instanceID, sessionDirTemplate string) string {
|
||||
if sessionDirTemplate == "" {
|
||||
return ""
|
||||
}
|
||||
root := InstanceSessionRoot(kandevHomeDir, instanceID)
|
||||
if root == "" {
|
||||
return ""
|
||||
}
|
||||
rel := strings.TrimPrefix(sessionDirTemplate, "{home}/")
|
||||
return filepath.Join(root, rel)
|
||||
}
|
||||
|
||||
// localFileUploader implements FileUploader for the host filesystem. Used by
|
||||
// the docker session seeder to copy auth files from ~/<src> into the
|
||||
// kandev-managed per-container session dir.
|
||||
type localFileUploader struct{}
|
||||
|
||||
func (localFileUploader) WriteFile(_ context.Context, path string, data []byte, mode os.FileMode) error {
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||
return fmt.Errorf("mkdir %s: %w", filepath.Dir(path), err)
|
||||
}
|
||||
return os.WriteFile(path, data, mode)
|
||||
}
|
||||
|
||||
func (localFileUploader) RunCommand(ctx context.Context, name string, args ...string) error {
|
||||
return exec.CommandContext(ctx, name, args...).Run()
|
||||
}
|
||||
|
||||
// SeedAgentSessionDir copies the agent's RemoteAuth.SourceFiles from the host
|
||||
// home into a per-container session dir, populating just the auth-relevant
|
||||
// files (typically auth.json + config.toml). Stale per-host state — codex's
|
||||
// state.db with absolute macOS paths, sessions/ subdirectories, lock files —
|
||||
// is intentionally NOT copied; the agent rebuilds them inside the container
|
||||
// with container-local Linux paths.
|
||||
//
|
||||
// instanceSessionRoot is the container's session root (e.g. <kandev-home>/
|
||||
// agent-sessions/<container_id>). The function selects only "files"-type
|
||||
// remote-auth methods that match the host OS.
|
||||
func SeedAgentSessionDir(
|
||||
ctx context.Context,
|
||||
ag agents.Agent,
|
||||
instanceSessionRoot string,
|
||||
log *logger.Logger,
|
||||
) error {
|
||||
if ag == nil || instanceSessionRoot == "" {
|
||||
return nil
|
||||
}
|
||||
// Always materialise the per-instance root so the container manager has a
|
||||
// stable bind-mount source even for agents whose auth is env-only (claude,
|
||||
// gemini, …) and the dotdir is otherwise empty until the in-container
|
||||
// SetupScript fills it.
|
||||
if err := os.MkdirAll(instanceSessionRoot, 0o755); err != nil {
|
||||
return fmt.Errorf("mkdir %s: %w", instanceSessionRoot, err)
|
||||
}
|
||||
|
||||
auth := ag.RemoteAuth()
|
||||
if auth == nil {
|
||||
return nil
|
||||
}
|
||||
hostOS := runtime.GOOS
|
||||
methods := make([]remoteauth.Method, 0, len(auth.Methods))
|
||||
for _, m := range auth.Methods {
|
||||
if m.Type != authMethodTypeFiles || len(m.SourceFiles) == 0 {
|
||||
continue
|
||||
}
|
||||
// Resolve the OS-specific source list into a flat one keyed by host
|
||||
// OS, falling back to "linux" for cross-platform agents.
|
||||
srcs := selectAuthSourceFiles(m, hostOS)
|
||||
if len(srcs) == 0 {
|
||||
continue
|
||||
}
|
||||
methods = append(methods, remoteauth.Method{
|
||||
Type: m.Type,
|
||||
SourceFiles: srcs,
|
||||
TargetRelDir: m.TargetRelDir,
|
||||
})
|
||||
}
|
||||
if len(methods) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return UploadCredentialFiles(ctx, localFileUploader{}, methods, instanceSessionRoot, log)
|
||||
}
|
||||
|
||||
// selectAuthSourceFiles picks the SourceFiles list to use for the host OS,
|
||||
// degrading gracefully to whatever the agent ships for "linux" or generic if
|
||||
// the host-specific entry is absent. The returned slice is the relative paths
|
||||
// (e.g. ".codex/auth.json") expected by UploadCredentialFiles.
|
||||
func selectAuthSourceFiles(m agents.RemoteAuthMethod, hostOS string) []string {
|
||||
if v, ok := m.SourceFiles[hostOS]; ok {
|
||||
return v
|
||||
}
|
||||
if v, ok := m.SourceFiles["linux"]; ok {
|
||||
return v
|
||||
}
|
||||
if v, ok := m.SourceFiles[""]; ok {
|
||||
return v
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CleanupAgentSessionDir removes the per-container session root from disk.
|
||||
// Best-effort: a missing directory or a transient I/O error is logged at
|
||||
// debug level and not propagated, since cleanup runs after teardown. Called
|
||||
// only on destructive stops (task/session deleted/archived).
|
||||
func CleanupAgentSessionDir(instanceSessionRoot string, log *logger.Logger) {
|
||||
if instanceSessionRoot == "" {
|
||||
return
|
||||
}
|
||||
if err := os.RemoveAll(instanceSessionRoot); err != nil {
|
||||
log.Debug("failed to remove per-container agent session dir",
|
||||
zap.String("path", instanceSessionRoot), zap.Error(err))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,229 @@
|
||||
package lifecycle
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/kandev/kandev/internal/agent/agents"
|
||||
"github.com/kandev/kandev/internal/common/logger"
|
||||
)
|
||||
|
||||
func newSeederTestLogger(t *testing.T) *logger.Logger {
|
||||
t.Helper()
|
||||
log, err := logger.NewLogger(logger.LoggingConfig{Level: "error", Format: "json"})
|
||||
if err != nil {
|
||||
t.Fatalf("logger: %v", err)
|
||||
}
|
||||
return log
|
||||
}
|
||||
|
||||
// seedTestHostHome rebinds os.UserHomeDir() to a temp dir so the seeder reads
|
||||
// from a clean fixture instead of the developer's actual ~/. Returns the temp
|
||||
// dir path.
|
||||
func seedTestHostHome(t *testing.T) string {
|
||||
t.Helper()
|
||||
tmp := t.TempDir()
|
||||
t.Setenv("HOME", tmp)
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Setenv("USERPROFILE", tmp)
|
||||
}
|
||||
return tmp
|
||||
}
|
||||
|
||||
func writeFile(t *testing.T, root, rel string, contents []byte) {
|
||||
t.Helper()
|
||||
full := filepath.Join(root, rel)
|
||||
if err := os.MkdirAll(filepath.Dir(full), 0o755); err != nil {
|
||||
t.Fatalf("mkdir: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(full, contents, 0o644); err != nil {
|
||||
t.Fatalf("write %s: %v", rel, err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSeedAgentSessionDir_OnlyCopiesAuthFiles is the load-bearing check for
|
||||
// the codex bug: the seeder must copy auth.json/config.toml from the host
|
||||
// home into the per-container session dir but must NOT bring along state.db,
|
||||
// sessions/, or other host-state caches that contain absolute host paths.
|
||||
func TestSeedAgentSessionDir_OnlyCopiesAuthFiles(t *testing.T) {
|
||||
hostHome := seedTestHostHome(t)
|
||||
writeFile(t, hostHome, ".codex/auth.json", []byte(`{"token":"abc"}`))
|
||||
writeFile(t, hostHome, ".codex/config.toml", []byte(`model = "gpt"`))
|
||||
writeFile(t, hostHome, ".codex/state.db", []byte("STATE_DB_BLOB"))
|
||||
writeFile(t, hostHome, ".codex/state.db-wal", []byte("WAL"))
|
||||
writeFile(t, hostHome, ".codex/sessions/2026-05/rollout-x.jsonl", []byte("ROLLOUT"))
|
||||
writeFile(t, hostHome, ".codex/junk.txt", []byte("junk"))
|
||||
|
||||
kandevHome := t.TempDir()
|
||||
const instanceID = "abcdef0123456789"
|
||||
instanceRoot := InstanceSessionRoot(kandevHome, instanceID)
|
||||
|
||||
if err := SeedAgentSessionDir(context.Background(), agents.NewCodexACP(), instanceRoot, newSeederTestLogger(t)); err != nil {
|
||||
t.Fatalf("SeedAgentSessionDir: %v", err)
|
||||
}
|
||||
|
||||
dotdir := filepath.Join(instanceRoot, ".codex")
|
||||
|
||||
mustExist := []string{"auth.json", "config.toml"}
|
||||
for _, name := range mustExist {
|
||||
if _, err := os.Stat(filepath.Join(dotdir, name)); err != nil {
|
||||
t.Fatalf("expected %s in session dir: %v", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
mustNotExist := []string{"state.db", "state.db-wal", "junk.txt", "sessions"}
|
||||
for _, name := range mustNotExist {
|
||||
if _, err := os.Stat(filepath.Join(dotdir, name)); !os.IsNotExist(err) {
|
||||
t.Fatalf("unexpected %s leaked into session dir (err=%v)", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestSeedAgentSessionDir_PerInstanceUnique ensures two containers running
|
||||
// the same agent get independent session dirs. Without this the codex state
|
||||
// DB from one task would clobber another's, defeating the whole isolation
|
||||
// guarantee.
|
||||
func TestSeedAgentSessionDir_PerInstanceUnique(t *testing.T) {
|
||||
hostHome := seedTestHostHome(t)
|
||||
writeFile(t, hostHome, ".codex/auth.json", []byte(`{"token":"a"}`))
|
||||
|
||||
kandevHome := t.TempDir()
|
||||
rootA := InstanceSessionRoot(kandevHome, "instance-a")
|
||||
rootB := InstanceSessionRoot(kandevHome, "instance-b")
|
||||
|
||||
if rootA == rootB || rootA == "" {
|
||||
t.Fatalf("unexpected dir collision: a=%q b=%q", rootA, rootB)
|
||||
}
|
||||
|
||||
log := newSeederTestLogger(t)
|
||||
if err := SeedAgentSessionDir(context.Background(), agents.NewCodexACP(), rootA, log); err != nil {
|
||||
t.Fatalf("seed A: %v", err)
|
||||
}
|
||||
if err := SeedAgentSessionDir(context.Background(), agents.NewCodexACP(), rootB, log); err != nil {
|
||||
t.Fatalf("seed B: %v", err)
|
||||
}
|
||||
|
||||
for _, root := range []string{rootA, rootB} {
|
||||
if _, err := os.Stat(filepath.Join(root, ".codex/auth.json")); err != nil {
|
||||
t.Fatalf("missing auth.json in %s: %v", root, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestSeedAgentSessionDir_MultipleAgents proves the agent-agnostic guarantee:
|
||||
// the same logic seeds claude / opencode just as it seeds codex, with no
|
||||
// per-agent special cases.
|
||||
func TestSeedAgentSessionDir_MultipleAgents(t *testing.T) {
|
||||
hostHome := seedTestHostHome(t)
|
||||
// Touch every auth file each tested agent might want; the seeder skips
|
||||
// missing ones silently so unrelated agents staying empty is fine.
|
||||
writeFile(t, hostHome, ".codex/auth.json", []byte("codex"))
|
||||
writeFile(t, hostHome, ".codex/config.toml", []byte("codex"))
|
||||
writeFile(t, hostHome, ".claude/.credentials.json", []byte("claude"))
|
||||
writeFile(t, hostHome, ".opencode/auth.json", []byte("opencode"))
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
ag agents.Agent
|
||||
}{
|
||||
{"codex", agents.NewCodexACP()},
|
||||
{"claude", agents.NewClaudeACP()},
|
||||
{"opencode", agents.NewOpenCodeACP()},
|
||||
}
|
||||
|
||||
kandevHome := t.TempDir()
|
||||
log := newSeederTestLogger(t)
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
instanceID := tc.name + "-instance"
|
||||
root := InstanceSessionRoot(kandevHome, instanceID)
|
||||
if err := SeedAgentSessionDir(context.Background(), tc.ag, root, log); err != nil {
|
||||
t.Fatalf("seed: %v", err)
|
||||
}
|
||||
// At least the per-instance root must exist and be a subdir of
|
||||
// the kandev-managed agent-sessions root.
|
||||
if _, err := os.Stat(root); err != nil {
|
||||
t.Fatalf("instance root missing: %v", err)
|
||||
}
|
||||
if !strings.HasPrefix(root, AgentSessionsRoot(kandevHome)) {
|
||||
t.Fatalf("root %q outside kandev agent-sessions %q", root, AgentSessionsRoot(kandevHome))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionDirHostPath_TrimsHomeTemplate(t *testing.T) {
|
||||
got := SessionDirHostPath("/tmp/kandev", "abc123", "{home}/.codex")
|
||||
want := filepath.Join("/tmp/kandev", "agent-sessions", "abc123", ".codex")
|
||||
if got != want {
|
||||
t.Fatalf("got %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCleanupAgentSessionDir_RemovesTree(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
root := filepath.Join(tmp, "instance-x")
|
||||
writeFile(t, tmp, "instance-x/.codex/auth.json", []byte("a"))
|
||||
writeFile(t, tmp, "instance-x/.codex/state.db", []byte("b"))
|
||||
|
||||
CleanupAgentSessionDir(root, newSeederTestLogger(t))
|
||||
|
||||
if _, err := os.Stat(root); !os.IsNotExist(err) {
|
||||
t.Fatalf("expected dir removed, got err=%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCleanupAgentSessionDir_NoopOnEmptyPath(t *testing.T) {
|
||||
// Should not panic; just exercise the guard.
|
||||
CleanupAgentSessionDir("", newSeederTestLogger(t))
|
||||
}
|
||||
|
||||
// TestDockerStopInstance_PreservesSessionDirOnPlainStop mirrors the Sprites
|
||||
// preserve-on-stop rule for Docker: a plain agent stop must leave the
|
||||
// per-container agent session dir alone so resume can re-attach to the same
|
||||
// state. Only destructive stops (task/session deleted/archived) clean up.
|
||||
func TestDockerStopInstance_PreservesSessionDirOnPlainStop(t *testing.T) {
|
||||
hostHome := seedTestHostHome(t)
|
||||
writeFile(t, hostHome, ".codex/auth.json", []byte(`{"token":"a"}`))
|
||||
|
||||
kandevHome := t.TempDir()
|
||||
instanceID := "stop-test-instance"
|
||||
root := InstanceSessionRoot(kandevHome, instanceID)
|
||||
if err := SeedAgentSessionDir(context.Background(), agents.NewCodexACP(), root, newSeederTestLogger(t)); err != nil {
|
||||
t.Fatalf("seed: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(root, ".codex/auth.json")); err != nil {
|
||||
t.Fatalf("seed precondition: %v", err)
|
||||
}
|
||||
|
||||
// Plain stop reasons must NOT remove the dir. Only destructive reasons
|
||||
// trigger CleanupAgentSessionDir from StopInstance, mirroring the
|
||||
// shouldRunExecutorCleanup() rule used by Sprites.
|
||||
preserveReasons := []string{"", "stopped via API", "agent crashed", "user requested"}
|
||||
for _, reason := range preserveReasons {
|
||||
if shouldRunExecutorCleanup(reason) {
|
||||
t.Fatalf("preserve reason %q would unexpectedly trigger destructive cleanup", reason)
|
||||
}
|
||||
}
|
||||
if _, err := os.Stat(root); err != nil {
|
||||
t.Fatalf("session dir disappeared without a destructive reason: %v", err)
|
||||
}
|
||||
|
||||
// Destructive reasons trigger cleanup.
|
||||
destructive := []string{"task archived", "task deleted", "session archived", "session deleted"}
|
||||
for _, reason := range destructive {
|
||||
if !shouldRunExecutorCleanup(reason) {
|
||||
t.Fatalf("destructive reason %q must opt into cleanup", reason)
|
||||
}
|
||||
}
|
||||
|
||||
CleanupAgentSessionDir(root, newSeederTestLogger(t))
|
||||
if _, err := os.Stat(root); !os.IsNotExist(err) {
|
||||
t.Fatalf("expected dir removed after destructive cleanup, got err=%v", err)
|
||||
}
|
||||
}
|
||||
@@ -56,25 +56,35 @@ func (cb *CommandBuilder) BuildContinueCommandString(ag agents.Agent, opts agent
|
||||
return strings.Join(cmd.Args(), " ")
|
||||
}
|
||||
|
||||
// ExpandSessionDir expands the session directory template from SessionConfig.
|
||||
// Replaces {home} with the user's home directory.
|
||||
// ExpandSessionDir resolves the host-side directory that should be bind-
|
||||
// mounted into the container at SessionDirTarget. When kandevHomeDir and
|
||||
// instanceID are non-empty, the path is the kandev-managed per-container
|
||||
// session dir (~/.kandev/agent-sessions/<instance_id>/<dotdir>) — isolated
|
||||
// from the user's actual ~/<dotdir> so the host's stale state DBs and
|
||||
// session caches stay out of the container.
|
||||
//
|
||||
// Falls back to the legacy {home}/<dotdir> expansion only when kandev home
|
||||
// is unavailable (kept narrow on purpose; callers in production always have
|
||||
// a resolved kandev home).
|
||||
//
|
||||
// Returns empty string if no session directory is configured.
|
||||
func (cb *CommandBuilder) ExpandSessionDir(ag agents.Agent) string {
|
||||
func (cb *CommandBuilder) ExpandSessionDir(ag agents.Agent, kandevHomeDir, instanceID string) string {
|
||||
template := ag.Runtime().SessionConfig.SessionDirTemplate
|
||||
if template == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
if path := SessionDirHostPath(kandevHomeDir, instanceID, template); path != "" {
|
||||
_ = os.MkdirAll(path, 0o755)
|
||||
return path
|
||||
}
|
||||
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
homeDir = "/tmp"
|
||||
}
|
||||
|
||||
result := strings.ReplaceAll(template, "{home}", homeDir)
|
||||
|
||||
// Ensure the directory exists
|
||||
_ = os.MkdirAll(result, 0755)
|
||||
|
||||
_ = os.MkdirAll(result, 0o755)
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
@@ -54,6 +54,11 @@ type ContainerManager struct {
|
||||
commandBuilder *CommandBuilder
|
||||
logger *logger.Logger
|
||||
networkName string
|
||||
// kandevHomeDir is the resolved Kandev root dir, used to derive the
|
||||
// per-container agent session dirs that replace host home bind-mounts.
|
||||
// Empty means "fall back to legacy {home}/.<agent>" — production callers
|
||||
// always pass a non-empty value.
|
||||
kandevHomeDir string
|
||||
// resolveAgentctlBinary returns the host path to a linux/amd64 agentctl
|
||||
// binary. Indirected so tests can inject a stub.
|
||||
resolveAgentctlBinary func() (string, error)
|
||||
@@ -63,8 +68,11 @@ type ContainerManager struct {
|
||||
resolveMockAgentBinary func() (string, error)
|
||||
}
|
||||
|
||||
// NewContainerManager creates a new ContainerManager
|
||||
func NewContainerManager(dockerClient *docker.Client, networkName string, log *logger.Logger) *ContainerManager {
|
||||
// NewContainerManager creates a new ContainerManager. kandevHomeDir is the
|
||||
// resolved Kandev root dir used to host per-container agent session dirs;
|
||||
// pass "" only in legacy callers/tests that don't exercise the session-dir
|
||||
// mount path.
|
||||
func NewContainerManager(dockerClient *docker.Client, networkName, kandevHomeDir string, log *logger.Logger) *ContainerManager {
|
||||
resolver := NewAgentctlResolver(log)
|
||||
mockResolver := NewMockAgentResolver(log)
|
||||
return &ContainerManager{
|
||||
@@ -72,6 +80,7 @@ func NewContainerManager(dockerClient *docker.Client, networkName string, log *l
|
||||
commandBuilder: NewCommandBuilder(),
|
||||
logger: log.WithFields(zap.String("component", "container-manager")),
|
||||
networkName: networkName,
|
||||
kandevHomeDir: kandevHomeDir,
|
||||
resolveAgentctlBinary: resolver.ResolveLinuxBinary,
|
||||
resolveMockAgentBinary: mockResolver.ResolveLinuxBinary,
|
||||
}
|
||||
@@ -328,7 +337,7 @@ func (cm *ContainerManager) buildContainerConfig(config ContainerConfig) (docker
|
||||
|
||||
// Expand mounts using the host path so {workspace} substitutions in mount
|
||||
// sources resolve to a real on-disk location.
|
||||
mounts := cm.expandMounts(rt.Mounts, config.WorkspacePath, ag)
|
||||
mounts := cm.expandMounts(rt.Mounts, config.WorkspacePath, ag, config.InstanceID)
|
||||
|
||||
// Add main repo .git directory mount for worktrees
|
||||
if config.MainRepoGitDir != "" {
|
||||
@@ -475,8 +484,15 @@ func newDockerPortBinding(containerPort int) docker.PortBindingConfig {
|
||||
}
|
||||
}
|
||||
|
||||
// expandMounts expands mount templates with actual paths
|
||||
func (cm *ContainerManager) expandMounts(templates []agents.MountTemplate, workspacePath string, ag agents.Agent) []docker.MountConfig {
|
||||
// expandMounts expands mount templates with actual paths.
|
||||
//
|
||||
// Per-agent session dirs (SessionConfig.SessionDirTemplate) are mapped to a
|
||||
// kandev-managed path under <kandev-home>/agent-sessions/<instance_id>/, NOT
|
||||
// the user's host home. The seeder (SeedAgentSessionDir) selectively copies
|
||||
// auth files from the host beforehand, so every agent gets a fresh, isolated
|
||||
// session dir per launch — host state DBs and session caches that contain
|
||||
// absolute host paths (e.g. codex's state.db) stay out of the container.
|
||||
func (cm *ContainerManager) expandMounts(templates []agents.MountTemplate, workspacePath string, ag agents.Agent, instanceID string) []docker.MountConfig {
|
||||
mounts := make([]docker.MountConfig, 0, len(templates)+1) // +1 for potential session dir
|
||||
|
||||
for _, mt := range templates {
|
||||
@@ -496,7 +512,7 @@ func (cm *ContainerManager) expandMounts(templates []agents.MountTemplate, works
|
||||
}
|
||||
|
||||
// Add session directory mount from SessionConfig
|
||||
sessionDirSource := cm.commandBuilder.ExpandSessionDir(ag)
|
||||
sessionDirSource := cm.commandBuilder.ExpandSessionDir(ag, cm.kandevHomeDir, instanceID)
|
||||
sessionDirTarget := cm.commandBuilder.GetSessionDirTarget(ag)
|
||||
if sessionDirSource != "" && sessionDirTarget != "" {
|
||||
mounts = append(mounts, docker.MountConfig{
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package lifecycle
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -179,6 +180,78 @@ func TestBuildContainerConfig_PublishesAgentctlPorts(t *testing.T) {
|
||||
assertEnvContains(t, got.Env, "AGENTCTL_INSTANCE_PORT_MAX=41100")
|
||||
}
|
||||
|
||||
// TestBuildContainerConfig_SessionDirIsKandevManagedForEveryAgent locks in
|
||||
// the agent-agnostic guarantee that bind sources for SessionDirTemplate
|
||||
// resolve to <kandev-home>/agent-sessions/<instance>/<dotdir> and never to
|
||||
// the user's host home — the codex bug was a leak of host state into the
|
||||
// container, and any agent with a SessionDirTemplate is at the same risk.
|
||||
func TestBuildContainerConfig_SessionDirIsKandevManagedForEveryAgent(t *testing.T) {
|
||||
allAgents := []struct {
|
||||
name string
|
||||
ag agents.Agent
|
||||
}{
|
||||
{"codex-acp", agents.NewCodexACP()},
|
||||
{"claude-acp", agents.NewClaudeACP()},
|
||||
{"opencode-acp", agents.NewOpenCodeACP()},
|
||||
{"copilot-acp", agents.NewCopilotACP()},
|
||||
{"amp-acp", agents.NewAmpACP()},
|
||||
{"gemini", agents.NewGemini()},
|
||||
{"auggie", agents.NewAuggie()},
|
||||
}
|
||||
const kandevHome = "/tmp/kandev-test-home"
|
||||
const instanceID = "0123456789abcdef"
|
||||
expectedRoot := filepath.Join(kandevHome, "agent-sessions", instanceID)
|
||||
|
||||
for _, tc := range allAgents {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
rt := tc.ag.Runtime()
|
||||
if rt == nil {
|
||||
t.Skipf("%s has no Runtime", tc.name)
|
||||
}
|
||||
// expandMounts only adds the session-dir bind when BOTH fields
|
||||
// are set; agents that omit one rely on the in-container
|
||||
// SetupScript for auth and never bind-mount the host home in the
|
||||
// first place. Skip those — the test guards the resolution shape
|
||||
// only for agents that DO add the bind mount.
|
||||
if rt.SessionConfig.SessionDirTemplate == "" || rt.SessionConfig.SessionDirTarget == "" {
|
||||
t.Skipf("%s has no full SessionDirTemplate+SessionDirTarget pair (no bind mount today)", tc.name)
|
||||
}
|
||||
|
||||
cm := newCMTest(t)
|
||||
cm.kandevHomeDir = kandevHome
|
||||
cfg := ContainerConfig{
|
||||
AgentConfig: tc.ag,
|
||||
InstanceID: instanceID,
|
||||
TaskID: "task-1",
|
||||
}
|
||||
|
||||
got, err := cm.buildContainerConfig(cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("buildContainerConfig: %v", err)
|
||||
}
|
||||
|
||||
target := rt.SessionConfig.SessionDirTarget
|
||||
var found *docker.MountConfig
|
||||
for i := range got.Mounts {
|
||||
if got.Mounts[i].Target == target {
|
||||
found = &got.Mounts[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
if found == nil {
|
||||
t.Fatalf("expected mount for SessionDirTarget %q, got %+v", target, got.Mounts)
|
||||
}
|
||||
if !strings.HasPrefix(found.Source, expectedRoot) {
|
||||
t.Fatalf("session-dir mount source %q not under %q (host home leaked into container?)",
|
||||
found.Source, expectedRoot)
|
||||
}
|
||||
if strings.Contains(found.Source, "{home}") {
|
||||
t.Fatalf("session-dir mount source %q still references {home} placeholder", found.Source)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildContainerConfig_MountsLocalClonePath(t *testing.T) {
|
||||
cm := newCMTest(t)
|
||||
cfg := ContainerConfig{
|
||||
|
||||
@@ -51,8 +51,9 @@ func getMetadataBool(metadata map[string]interface{}, key string) bool {
|
||||
// DockerExecutor implements Runtime for Docker-based agent execution.
|
||||
// The Docker client is created lazily on first use (not at startup).
|
||||
type DockerExecutor struct {
|
||||
cfg config.DockerConfig
|
||||
logger *logger.Logger
|
||||
cfg config.DockerConfig
|
||||
kandevHomeDir string
|
||||
logger *logger.Logger
|
||||
|
||||
// newClientFunc creates the Docker client. Defaults to docker.NewClient.
|
||||
// Override in tests to simulate failures.
|
||||
@@ -69,10 +70,13 @@ type DockerExecutor struct {
|
||||
|
||||
// NewDockerExecutor creates a new Docker runtime.
|
||||
// The Docker client is NOT created here — it is initialized lazily
|
||||
// when CreateInstance is called.
|
||||
func NewDockerExecutor(cfg config.DockerConfig, log *logger.Logger) *DockerExecutor {
|
||||
// when CreateInstance is called. kandevHomeDir is the resolved kandev root
|
||||
// directory used to host per-container agent session dirs (the replacement
|
||||
// for host home bind mounts that were leaking host state into containers).
|
||||
func NewDockerExecutor(cfg config.DockerConfig, kandevHomeDir string, log *logger.Logger) *DockerExecutor {
|
||||
return &DockerExecutor{
|
||||
cfg: cfg,
|
||||
kandevHomeDir: kandevHomeDir,
|
||||
logger: log.WithFields(zap.String("runtime", "docker")),
|
||||
newClientFunc: docker.NewClient,
|
||||
}
|
||||
@@ -95,7 +99,7 @@ func (r *DockerExecutor) ensureClient() (*docker.Client, *ContainerManager, erro
|
||||
}
|
||||
|
||||
r.docker = cli
|
||||
r.containerMgr = NewContainerManager(cli, "", r.logger)
|
||||
r.containerMgr = NewContainerManager(cli, "", r.kandevHomeDir, r.logger)
|
||||
r.initialized = true
|
||||
|
||||
return r.docker, r.containerMgr, nil
|
||||
@@ -156,6 +160,21 @@ func (r *DockerExecutor) CreateInstance(ctx context.Context, req *ExecutorCreate
|
||||
worktreeID := getMetadataString(req.Metadata, MetadataKeyWorktreeID)
|
||||
worktreeBranch := getMetadataString(req.Metadata, MetadataKeyWorktreeBranch)
|
||||
|
||||
// Seed the per-container agent session dir with the auth files the agent
|
||||
// needs (auth.json / config.toml / etc.). This replaces the historical
|
||||
// pattern of bind-mounting the user's whole ~/.<agent> into the container,
|
||||
// which leaked host-absolute state-DB paths into the container and broke
|
||||
// session resume on agents that cache them (codex's rollout DB).
|
||||
if req.AgentConfig != nil && r.kandevHomeDir != "" {
|
||||
instanceRoot := InstanceSessionRoot(r.kandevHomeDir, req.InstanceID)
|
||||
if err := SeedAgentSessionDir(ctx, req.AgentConfig, instanceRoot, r.logger); err != nil {
|
||||
r.logger.Warn("failed to seed agent session dir (continuing)",
|
||||
zap.String("instance_id", req.InstanceID),
|
||||
zap.String("agent_id", req.AgentConfig.ID()),
|
||||
zap.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
// Resolve prepare script for cloning repo inside container
|
||||
prepareScript := r.resolvePrepareScript(req)
|
||||
|
||||
@@ -465,6 +484,18 @@ func resolveDockerEndpoint(
|
||||
}
|
||||
|
||||
func (r *DockerExecutor) StopInstance(ctx context.Context, instance *ExecutorInstance, force bool) error {
|
||||
if instance == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// On destructive stop reasons (task/session deleted/archived), clean up
|
||||
// the kandev-managed per-container session dir so we don't leak GBs of
|
||||
// agent state on disk. Plain stops preserve the dir so resume re-attaches
|
||||
// to the same agent state, mirroring the Sprites preserve-on-stop rule.
|
||||
if shouldRunExecutorCleanup(instance.StopReason) && r.kandevHomeDir != "" && instance.InstanceID != "" {
|
||||
CleanupAgentSessionDir(InstanceSessionRoot(r.kandevHomeDir, instance.InstanceID), r.logger)
|
||||
}
|
||||
|
||||
if instance.ContainerID == "" {
|
||||
return nil // No container to stop
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ func failingClientFactory(errMsg string) func(config.DockerConfig, *logger.Logge
|
||||
|
||||
func TestNewDockerExecutor(t *testing.T) {
|
||||
log := newTestDockerLogger()
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, log)
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, "", log)
|
||||
|
||||
if exec == nil {
|
||||
t.Fatal("expected non-nil executor")
|
||||
@@ -49,7 +49,7 @@ func TestNewDockerExecutor(t *testing.T) {
|
||||
|
||||
func TestDockerExecutor_Name(t *testing.T) {
|
||||
log := newTestDockerLogger()
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, log)
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, "", log)
|
||||
|
||||
if exec.Name() != executor.NameDocker {
|
||||
t.Errorf("expected name %q, got %q", executor.NameDocker, exec.Name())
|
||||
@@ -58,7 +58,7 @@ func TestDockerExecutor_Name(t *testing.T) {
|
||||
|
||||
func TestDockerExecutor_HealthCheck(t *testing.T) {
|
||||
log := newTestDockerLogger()
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, log)
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, "", log)
|
||||
|
||||
if err := exec.HealthCheck(context.Background()); err != nil {
|
||||
t.Errorf("expected nil error, got: %v", err)
|
||||
@@ -67,7 +67,7 @@ func TestDockerExecutor_HealthCheck(t *testing.T) {
|
||||
|
||||
func TestDockerExecutor_RecoverInstances(t *testing.T) {
|
||||
log := newTestDockerLogger()
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, log)
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, "", log)
|
||||
|
||||
instances, err := exec.RecoverInstances(context.Background())
|
||||
if err != nil {
|
||||
@@ -80,7 +80,7 @@ func TestDockerExecutor_RecoverInstances(t *testing.T) {
|
||||
|
||||
func TestDockerExecutor_GetInteractiveRunner(t *testing.T) {
|
||||
log := newTestDockerLogger()
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, log)
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, "", log)
|
||||
|
||||
if runner := exec.GetInteractiveRunner(); runner != nil {
|
||||
t.Error("expected nil interactive runner for docker executor")
|
||||
@@ -89,7 +89,7 @@ func TestDockerExecutor_GetInteractiveRunner(t *testing.T) {
|
||||
|
||||
func TestDockerExecutor_EnsureClient_Success(t *testing.T) {
|
||||
log := newTestDockerLogger()
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, log)
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, "", log)
|
||||
// Default factory uses docker.NewClient which succeeds even without Docker running
|
||||
|
||||
cli, cm, err := exec.ensureClient()
|
||||
@@ -124,7 +124,7 @@ func TestDockerExecutor_EnsureClient_Success(t *testing.T) {
|
||||
|
||||
func TestDockerExecutor_EnsureClient_RetriesOnFailure(t *testing.T) {
|
||||
log := newTestDockerLogger()
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, log)
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, "", log)
|
||||
|
||||
var callCount atomic.Int32
|
||||
exec.newClientFunc = func(_ config.DockerConfig, _ *logger.Logger) (*docker.Client, error) {
|
||||
@@ -156,7 +156,7 @@ func TestDockerExecutor_EnsureClient_RetriesOnFailure(t *testing.T) {
|
||||
|
||||
func TestDockerExecutor_EnsureClient_RecoversAfterTransientFailure(t *testing.T) {
|
||||
log := newTestDockerLogger()
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, log)
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, "", log)
|
||||
|
||||
var callCount atomic.Int32
|
||||
exec.newClientFunc = func(cfg config.DockerConfig, l *logger.Logger) (*docker.Client, error) {
|
||||
@@ -191,7 +191,7 @@ func TestDockerExecutor_EnsureClient_RecoversAfterTransientFailure(t *testing.T)
|
||||
|
||||
func TestDockerExecutor_Client_ReturnsNilOnFailure(t *testing.T) {
|
||||
log := newTestDockerLogger()
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, log)
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, "", log)
|
||||
exec.newClientFunc = failingClientFactory("docker unavailable")
|
||||
|
||||
if cli := exec.Client(); cli != nil {
|
||||
@@ -201,7 +201,7 @@ func TestDockerExecutor_Client_ReturnsNilOnFailure(t *testing.T) {
|
||||
|
||||
func TestDockerExecutor_ContainerMgr_ReturnsNilOnFailure(t *testing.T) {
|
||||
log := newTestDockerLogger()
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, log)
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, "", log)
|
||||
exec.newClientFunc = failingClientFactory("docker unavailable")
|
||||
|
||||
if cm := exec.ContainerMgr(); cm != nil {
|
||||
@@ -211,7 +211,7 @@ func TestDockerExecutor_ContainerMgr_ReturnsNilOnFailure(t *testing.T) {
|
||||
|
||||
func TestDockerExecutor_Client_ReturnsClientOnSuccess(t *testing.T) {
|
||||
log := newTestDockerLogger()
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, log)
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, "", log)
|
||||
|
||||
cli := exec.Client()
|
||||
if cli == nil {
|
||||
@@ -223,7 +223,7 @@ func TestDockerExecutor_Client_ReturnsClientOnSuccess(t *testing.T) {
|
||||
|
||||
func TestDockerExecutor_Close_BeforeInit(t *testing.T) {
|
||||
log := newTestDockerLogger()
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, log)
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, "", log)
|
||||
|
||||
if err := exec.Close(); err != nil {
|
||||
t.Errorf("expected nil error, got: %v", err)
|
||||
@@ -232,7 +232,7 @@ func TestDockerExecutor_Close_BeforeInit(t *testing.T) {
|
||||
|
||||
func TestDockerExecutor_Close_AfterInit(t *testing.T) {
|
||||
log := newTestDockerLogger()
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, log)
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, "", log)
|
||||
|
||||
// Initialize the client
|
||||
_, _, _ = exec.ensureClient()
|
||||
@@ -257,7 +257,7 @@ func TestDockerExecutor_Close_AfterInit(t *testing.T) {
|
||||
|
||||
func TestDockerExecutor_Close_AfterFailedInit(t *testing.T) {
|
||||
log := newTestDockerLogger()
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, log)
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, "", log)
|
||||
exec.newClientFunc = failingClientFactory("docker unavailable")
|
||||
|
||||
// Trigger failed init
|
||||
@@ -297,7 +297,7 @@ func TestDockerClientProvider_NoDockerExecutor(t *testing.T) {
|
||||
func TestDockerClientProvider_WithDockerExecutor(t *testing.T) {
|
||||
log := newTestDockerLogger()
|
||||
registry := NewExecutorRegistry(log)
|
||||
dockerExec := NewDockerExecutor(config.DockerConfig{}, log)
|
||||
dockerExec := NewDockerExecutor(config.DockerConfig{}, "", log)
|
||||
dockerExec.newClientFunc = failingClientFactory("docker unavailable")
|
||||
registry.Register(dockerExec)
|
||||
mgr := NewManager(nil, nil, registry, nil, nil, nil, ExecutorFallbackWarn, "", log)
|
||||
@@ -311,7 +311,7 @@ func TestDockerClientProvider_WithDockerExecutor(t *testing.T) {
|
||||
func TestDockerClientProvider_WithWorkingDocker(t *testing.T) {
|
||||
log := newTestDockerLogger()
|
||||
registry := NewExecutorRegistry(log)
|
||||
dockerExec := NewDockerExecutor(config.DockerConfig{}, log)
|
||||
dockerExec := NewDockerExecutor(config.DockerConfig{}, "", log)
|
||||
registry.Register(dockerExec)
|
||||
mgr := NewManager(nil, nil, registry, nil, nil, nil, ExecutorFallbackWarn, "", log)
|
||||
|
||||
@@ -329,7 +329,7 @@ func TestExecutorRegistry_CloseAll(t *testing.T) {
|
||||
log := newTestDockerLogger()
|
||||
registry := NewExecutorRegistry(log)
|
||||
|
||||
dockerExec := NewDockerExecutor(config.DockerConfig{}, log)
|
||||
dockerExec := NewDockerExecutor(config.DockerConfig{}, "", log)
|
||||
registry.Register(dockerExec)
|
||||
registry.Register(&MockExecutor{name: executor.NameStandalone})
|
||||
|
||||
@@ -348,7 +348,7 @@ func TestExecutorRegistry_CloseAll(t *testing.T) {
|
||||
|
||||
func TestInjectTokenIntoURL(t *testing.T) {
|
||||
log := newTestDockerLogger()
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, log)
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, "", log)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -418,7 +418,7 @@ func TestInjectTokenIntoURL(t *testing.T) {
|
||||
|
||||
func TestReconnectToContainer_ValidationErrors(t *testing.T) {
|
||||
log := newTestDockerLogger()
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, log)
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, "", log)
|
||||
|
||||
t.Run("short execution ID rejected", func(t *testing.T) {
|
||||
req := &ExecutorCreateRequest{
|
||||
@@ -557,7 +557,7 @@ func TestBuildReconnectCreateInstanceRequest(t *testing.T) {
|
||||
|
||||
func TestResolvePrepareScript(t *testing.T) {
|
||||
log := newTestDockerLogger()
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, log)
|
||||
exec := NewDockerExecutor(config.DockerConfig{}, "", log)
|
||||
|
||||
t.Run("uses default docker script when no metadata", func(t *testing.T) {
|
||||
req := &ExecutorCreateRequest{
|
||||
|
||||
Reference in New Issue
Block a user