Skip to content

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

Go

fix(daemon): isolate agent temp dirs (#5005)

YOMXXXe002ee5a6bb3
diff --git a/server/internal/daemon/daemon.go b/server/internal/daemon/daemon.go
index 8fbafac536..9e48e87abf 100644
--- a/server/internal/daemon/daemon.go
+++ b/server/internal/daemon/daemon.go
@@ -3841,6 +3841,10 @@ func (d *Daemon) runTask(ctx context.Context, task Task, provider string, slot i
d.markActiveEnvRoot(env.RootDir)
defer d.unmarkActiveEnvRoot(env.RootDir)
}
+ taskTempDir, err := ensureTaskTempDir(env.RootDir, task.ID)
+ if err != nil {
+ return TaskResult{}, fmt.Errorf("prepare task temp dir: %w", err)
+ }
// Issue #3999 race A: now that env.WorkDir is on disk, transition the
// server-side state machine dispatched (or waiting_local_directory) →
@@ -3922,6 +3926,9 @@ func (d *Daemon) runTask(ctx context.Context, task Task, provider string, slot i
"MULTICA_AGENT_ID": task.AgentID,
"MULTICA_TASK_ID": task.ID,
"MULTICA_TASK_SLOT": strconv.Itoa(slot),
+ "TMPDIR": taskTempDir,
+ "TMP": taskTempDir,
+ "TEMP": taskTempDir,
}
if task.AutopilotRunID != "" {
agentEnv["MULTICA_AUTOPILOT_RUN_ID"] = task.AutopilotRunID
@@ -4848,6 +4855,30 @@ func composeOpenclawIncludeRoots(addRoot, userValue string) (string, bool) {
return strings.Join(parts, string(os.PathListSeparator)), true
}
+func ensureTaskTempDir(envRoot string, taskID string) (string, error) {
+ envRoot = strings.TrimSpace(envRoot)
+ if envRoot == "" {
+ return "", errors.New("env root is empty")
+ }
+ dir := filepath.Join(envRoot, "tmp", safeTempPathComponent(taskID))
+ if err := os.MkdirAll(dir, 0o700); err != nil {
+ return "", err
+ }
+ if err := os.Chmod(dir, 0o700); err != nil {
+ return "", err
+ }
+ return dir, nil
+}
+
+func safeTempPathComponent(value string) string {
+ value = strings.TrimSpace(value)
+ if value == "" {
+ return "task"
+ }
+ replacer := strings.NewReplacer("/", "_", "\\", "_", ":", "_")
+ return replacer.Replace(value)
+}
+
// isBlockedEnvKey returns true if the key must not be overridden by user-
// configured custom_env. This prevents accidental or malicious override of
// daemon-internal variables and critical system paths.
@@ -4857,7 +4888,7 @@ func isBlockedEnvKey(key string) bool {
return true
}
switch upper {
- case "HOME", "PATH", "USER", "SHELL", "TERM", "CODEX_HOME", "CURSOR_DATA_DIR", "OPENCLAW_CONFIG_PATH", "OPENCLAW_INCLUDE_ROOTS":
+ case "HOME", "PATH", "USER", "SHELL", "TERM", "TMPDIR", "TMP", "TEMP", "CODEX_HOME", "CURSOR_DATA_DIR", "OPENCLAW_CONFIG_PATH", "OPENCLAW_INCLUDE_ROOTS":
return true
}
return false
diff --git a/server/internal/daemon/daemon_test.go b/server/internal/daemon/daemon_test.go
index 325d85c0d4..55124329b5 100644
--- a/server/internal/daemon/daemon_test.go
+++ b/server/internal/daemon/daemon_test.go
@@ -93,6 +93,9 @@ func TestIsBlockedEnvKey(t *testing.T) {
{key: "multica_runtime_id", want: true},
{key: "HOME", want: true},
{key: "PATH", want: true},
+ {key: "TMPDIR", want: true},
+ {key: "tmp", want: true},
+ {key: "TEMP", want: true},
{key: "CODEX_HOME", want: true},
{key: "CURSOR_DATA_DIR", want: true},
{key: "cursor_data_dir", want: true},
diff --git a/server/internal/daemon/workdir_race_test.go b/server/internal/daemon/workdir_race_test.go
index d76c310906..96b2e830fd 100644
--- a/server/internal/daemon/workdir_race_test.go
+++ b/server/internal/daemon/workdir_race_test.go
@@ -8,6 +8,7 @@ import (
"net/http/httptest"
"os"
"path/filepath"
+ "runtime"
"strings"
"sync"
"sync/atomic"
@@ -157,6 +158,106 @@ func TestRunTask_StartTaskCalledAfterWorkdirOnDisk(t *testing.T) {
}
}
+func TestRunTask_InjectsPrivateTaskTempDir(t *testing.T) {
+ if runtime.GOOS == "windows" {
+ t.Skip("shell-script agent fixture is POSIX-only")
+ }
+
+ workspacesRoot := t.TempDir()
+ workspaceID := "ws-private-temp"
+ taskID := "task-private-temp"
+ expectedTempDir := filepath.Join(execenv.PredictRootDir(workspacesRoot, workspaceID, taskID), "tmp", taskID)
+
+ captureFile := filepath.Join(t.TempDir(), "agent-env.txt")
+ fakeBin := filepath.Join(t.TempDir(), "claude")
+ script := `#!/bin/sh
+printf 'TMPDIR=%s\nTMP=%s\nTEMP=%s\n' "$TMPDIR" "$TMP" "$TEMP" > "$CAPTURE_FILE"
+IFS= read -r _
+printf '%s\n' '{"type":"system","session_id":"sess-private-temp"}'
+printf '%s\n' '{"type":"result","subtype":"success","is_error":false,"session_id":"sess-private-temp","result":"done"}'
+`
+ if err := os.WriteFile(fakeBin, []byte(script), 0o755); err != nil {
+ t.Fatalf("write fake agent: %v", err)
+ }
+ if err := os.Chmod(fakeBin, 0o755); err != nil {
+ t.Fatalf("chmod fake agent: %v", err)
+ }
+
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusOK)
+ }))
+ t.Cleanup(srv.Close)
+
+ d := &Daemon{
+ client: NewClient(srv.URL),
+ logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
+ workspaces: make(map[string]*workspaceState),
+ runtimeIndex: map[string]Runtime{"rt-1": {ID: "rt-1", Provider: "claude"}},
+ activeEnvRoots: make(map[string]int),
+ cfg: Config{
+ WorkspacesRoot: workspacesRoot,
+ AgentTimeout: 5 * time.Second,
+ ServerBaseURL: srv.URL,
+ Agents: map[string]AgentEntry{
+ "claude": {Path: fakeBin, Model: ""},
+ },
+ },
+ }
+
+ task := Task{
+ ID: taskID,
+ WorkspaceID: workspaceID,
+ RuntimeID: "rt-1",
+ IssueID: "issue-private-temp",
+ AuthToken: "mat_private_temp",
+ Agent: &AgentData{
+ ID: "agent-private-temp",
+ Name: "test-agent",
+ CustomEnv: map[string]string{
+ "CAPTURE_FILE": captureFile,
+ "TMPDIR": "/shared/tmp",
+ "TMP": "/shared/tmp",
+ "TEMP": "/shared/tmp",
+ },
+ },
+ }
+
+ taskLog := slog.New(slog.NewTextHandler(io.Discard, nil))
+ result, err := d.runTask(context.Background(), task, "claude", 0, taskLog)
+ if err != nil {
+ t.Fatalf("runTask(): %v", err)
+ }
+ if result.Status != "completed" {
+ t.Fatalf("runTask status = %q, want completed (comment=%q)", result.Status, result.Comment)
+ }
+
+ info, err := os.Stat(expectedTempDir)
+ if err != nil {
+ t.Fatalf("expected task temp dir %q to exist: %v", expectedTempDir, err)
+ }
+ if !info.IsDir() {
+ t.Fatalf("expected task temp path %q to be a directory", expectedTempDir)
+ }
+
+ raw, err := os.ReadFile(captureFile)
+ if err != nil {
+ t.Fatalf("read captured agent env: %v", err)
+ }
+ got := make(map[string]string)
+ for _, line := range strings.Split(strings.TrimSpace(string(raw)), "\n") {
+ key, value, ok := strings.Cut(line, "=")
+ if !ok {
+ t.Fatalf("malformed captured env line %q", line)
+ }
+ got[key] = value
+ }
+ for _, key := range []string{"TMPDIR", "TMP", "TEMP"} {
+ if got[key] != expectedTempDir {
+ t.Fatalf("%s = %q, want private task temp dir %q", key, got[key], expectedTempDir)
+ }
+ }
+}
+
func TestRunTask_ExtendsPrepareLeaseDuringStartTask(t *testing.T) {
oldRefresh := taskPrepareLeaseRefresh
oldTimeout := taskPrepareLeaseTimeout