Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix(agent-canvas): scope baked working_dir to the default-local backend (#16759)

Engel Nyst861e9ef50173
diff --git a/__tests__/api/agent-server-conversation-service.test.ts b/__tests__/api/agent-server-conversation-service.test.ts
index b75a6b5e84..c97cd07325 100644
--- a/__tests__/api/agent-server-conversation-service.test.ts
+++ b/__tests__/api/agent-server-conversation-service.test.ts
@@ -86,7 +86,7 @@ vi.mock("#/api/agent-server-config", () => ({
getAgentServerBaseUrl: vi.fn(() => "http://localhost:54928"),
getAgentServerSessionApiKey: vi.fn(() => "test-api-key"),
getAgentServerWorkingDir: vi.fn(() => "/workspace/project/agent-canvas"),
- buildConversationWorkingDir: vi.fn(
+ buildConversationWorkingDirForBackend: vi.fn(
(id: string) => `/state/workspaces/${id.replace(/-/g, "")}`,
),
getAgentServerHeaders: vi.fn(() => ({ "X-Session-API-Key": "test-api-key" })),
@@ -410,7 +410,7 @@ describe("AgentServerConversationService", () => {
// home dir so the worktree and later file uploads agree on a writable
// absolute path.
it("resolves relative default working dirs against /api/file/home", async () => {
- const { buildConversationWorkingDir: mockedBuilder } =
+ const { buildConversationWorkingDirForBackend: mockedBuilder } =
await import("#/api/agent-server-config");
vi.mocked(mockedBuilder).mockImplementationOnce(
(id: string) => `workspace/project/${id.replace(/-/g, "")}`,
diff --git a/__tests__/api/use-create-conversation-metadata.test.ts b/__tests__/api/use-create-conversation-metadata.test.ts
index 8251c278fb..5f18c2858c 100644
--- a/__tests__/api/use-create-conversation-metadata.test.ts
+++ b/__tests__/api/use-create-conversation-metadata.test.ts
@@ -53,7 +53,7 @@ vi.mock("#/api/agent-server-config", () => ({
getBakedSessionApiKey: vi.fn(() => "test-session-key"),
getAgentServerSessionApiKey: vi.fn(() => "test-session-key"),
getAgentServerWorkingDir: vi.fn(() => "/workspace/project/agent-canvas"),
- buildConversationWorkingDir: vi.fn(
+ buildConversationWorkingDirForBackend: vi.fn(
(id: string) => `/state/workspaces/${id.replace(/-/g, "")}`,
),
shouldLoadPublicSkills: vi.fn(() => true),
diff --git a/src/api/agent-server-config.test.ts b/src/api/agent-server-config.test.ts
new file mode 100644
index 0000000000..739dbeb454
--- /dev/null
+++ b/src/api/agent-server-config.test.ts
@@ -0,0 +1,117 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+import {
+ DEFAULT_WORKING_DIR,
+ buildConversationWorkingDir,
+ buildConversationWorkingDirForBackend,
+ buildRelativeConversationWorkingDir,
+ isServedOriginHost,
+} from "./agent-server-config";
+
+describe("buildRelativeConversationWorkingDir", () => {
+ afterEach(() => vi.unstubAllEnvs());
+
+ it("ignores a baked absolute VITE_WORKING_DIR and stays relative", () => {
+ vi.stubEnv("VITE_WORKING_DIR", "/Users/someone/.openhands/workspaces");
+ expect(buildRelativeConversationWorkingDir("abc-123")).toBe(
+ `${DEFAULT_WORKING_DIR}/abc123`,
+ );
+ });
+
+ it("strips dashes from the conversation id", () => {
+ expect(buildRelativeConversationWorkingDir("a-b-c")).toBe(
+ `${DEFAULT_WORKING_DIR}/abc`,
+ );
+ });
+});
+
+describe("buildConversationWorkingDir (baked default)", () => {
+ afterEach(() => vi.unstubAllEnvs());
+
+ it("uses a baked absolute dir as the base", () => {
+ vi.stubEnv("VITE_WORKING_DIR", "/Users/someone/workspaces");
+ expect(buildConversationWorkingDir("abc-123")).toBe(
+ "/Users/someone/workspaces/abc123",
+ );
+ });
+
+ it("falls back to the relative default when nothing is baked", () => {
+ vi.stubEnv("VITE_WORKING_DIR", "");
+ expect(buildConversationWorkingDir("abc-123")).toBe(
+ `${DEFAULT_WORKING_DIR}/abc123`,
+ );
+ });
+});
+
+describe("isServedOriginHost", () => {
+ afterEach(() => vi.unstubAllEnvs());
+
+ it("matches the host that served this frontend", () => {
+ vi.stubEnv("VITE_BACKEND_BASE_URL", "http://127.0.0.1:8000");
+ expect(isServedOriginHost("http://127.0.0.1:8000")).toBe(true);
+ });
+
+ it("normalizes a bare host against the served origin", () => {
+ vi.stubEnv("VITE_BACKEND_BASE_URL", "http://127.0.0.1:8000");
+ // getAgentServerBaseUrl returns the already-normalized configured URL;
+ // a bare candidate host is normalized to http:// off-window.
+ expect(isServedOriginHost("127.0.0.1:8000")).toBe(true);
+ });
+
+ it("does not match a different host", () => {
+ vi.stubEnv("VITE_BACKEND_BASE_URL", "http://127.0.0.1:8000");
+ expect(isServedOriginHost("https://cabin.liberty-labs.org")).toBe(false);
+ });
+
+ it("does not match when the backend host is empty", () => {
+ vi.stubEnv("VITE_BACKEND_BASE_URL", "http://127.0.0.1:8000");
+ expect(isServedOriginHost("")).toBe(false);
+ expect(isServedOriginHost(null)).toBe(false);
+ });
+});
+
+describe("buildConversationWorkingDirForBackend", () => {
+ afterEach(() => vi.unstubAllEnvs());
+
+ it("uses the baked absolute default for the served-origin backend", () => {
+ vi.stubEnv("VITE_BACKEND_BASE_URL", "http://127.0.0.1:8000");
+ vi.stubEnv(
+ "VITE_WORKING_DIR",
+ "/Users/me/.openhands/agent-canvas/workspaces",
+ );
+ expect(
+ buildConversationWorkingDirForBackend("abc-123", "http://127.0.0.1:8000"),
+ ).toBe("/Users/me/.openhands/agent-canvas/workspaces/abc123");
+ });
+
+ // Regression for the reviewed bug: the seeded `default-local` backend is
+ // mutable. Editing its host to a remote VM (while its id stays
+ // `default-local`) must NOT send the baked macOS path to that VM β€” this is
+ // the exact case that failed on our remote Linux backend (Cabin) with
+ // `mkdir: cannot create directory '/Users': Permission denied`.
+ it("uses the relative default for an edited default-local pointing at a remote host", () => {
+ vi.stubEnv("VITE_BACKEND_BASE_URL", "http://127.0.0.1:8000");
+ vi.stubEnv(
+ "VITE_WORKING_DIR",
+ "/Users/me/.openhands/agent-canvas/workspaces",
+ );
+ // The user edited the seeded `default-local` entry to a remote Linux VM
+ // (home /home/daytona); its host no longer matches the served origin.
+ expect(
+ buildConversationWorkingDirForBackend(
+ "abc-123",
+ "https://cabin.liberty-labs.org",
+ ),
+ ).toBe(`${DEFAULT_WORKING_DIR}/abc123`);
+ });
+
+ it("uses the relative default for any separately-registered remote backend", () => {
+ vi.stubEnv("VITE_BACKEND_BASE_URL", "http://127.0.0.1:8000");
+ vi.stubEnv("VITE_WORKING_DIR", "/Users/me/workspaces");
+ expect(
+ buildConversationWorkingDirForBackend(
+ "abc-123",
+ "https://sandbox.example.dev",
+ ),
+ ).toBe(`${DEFAULT_WORKING_DIR}/abc123`);
+ });
+});
diff --git a/src/api/agent-server-config.ts b/src/api/agent-server-config.ts
index 923c5b352d..739b7c2379 100644
--- a/src/api/agent-server-config.ts
+++ b/src/api/agent-server-config.ts
@@ -200,10 +200,61 @@ export function getAgentServerWorkingDir(): string {
return DEFAULT_WORKING_DIR;
}
-export function buildConversationWorkingDir(conversationId: string): string {
- const base = getAgentServerWorkingDir().replace(/\/+$/, "");
+function buildWorkingDir(base: string, conversationId: string): string {
+ const trimmed = base.replace(/\/+$/, "");
const hex = conversationId.replace(/-/g, "");
- return `${base}/${hex}`;
+ return `${trimmed}/${hex}`;
+}
+
+export function buildConversationWorkingDir(conversationId: string): string {
+ return buildWorkingDir(getAgentServerWorkingDir(), conversationId);
+}
+
+/**
+ * Conversation working dir under the backend-relative default
+ * (`workspace/project/<hex>`), deliberately ignoring any baked absolute
+ * `VITE_WORKING_DIR`. `resolveAbsoluteAgentServerPath()` anchors this to the
+ * active backend's own home via `GET /api/file/home`, so it resolves to a
+ * writable path on whichever backend actually runs the conversation.
+ */
+export function buildRelativeConversationWorkingDir(
+ conversationId: string,
+): string {
+ return buildWorkingDir(DEFAULT_WORKING_DIR, conversationId);
+}
+
+/**
+ * Whether `backendHost` is the same host that served this frontend.
+ *
+ * A launcher-baked `VITE_WORKING_DIR` is an absolute path on the serving
+ * host's filesystem, so it is only valid on that exact backend. The seeded
+ * `default-local` entry starts life pointing at the served origin, but its
+ * `host` is mutable β€” the user can edit it to a remote backend while its id
+ * stays `default-local`. Matching on the host (not the stable id) keeps the
+ * real invariant: the baked path is only safe for the host it was baked for.
+ */
+export function isServedOriginHost(
+ backendHost: string | null | undefined,
+): boolean {
+ const served = normalizeBaseUrl(getAgentServerBaseUrl());
+ const candidate = normalizeBaseUrl(backendHost);
+ if (!served || !candidate) return false;
+ return served === candidate;
+}
+
+/**
+ * Base working dir for a new conversation on the backend at `backendHost`.
+ * The baked (possibly absolute) default is used only for the served-origin
+ * backend; every other backend gets the relative default, anchored to its
+ * own home by `resolveAbsoluteAgentServerPath()`.
+ */
+export function buildConversationWorkingDirForBackend(
+ conversationId: string,
+ backendHost: string | null | undefined,
+): string {
+ return isServedOriginHost(backendHost)
+ ? buildConversationWorkingDir(conversationId)
+ : buildRelativeConversationWorkingDir(conversationId);
}
export function getAgentServerHeaders(): Record<string, string> {
diff --git a/src/api/conversation-service/agent-server-conversation-service.api.ts b/src/api/conversation-service/agent-server-conversation-service.api.ts
index bb08d26fb4..42b1c875fd 100644
--- a/src/api/conversation-service/agent-server-conversation-service.api.ts
+++ b/src/api/conversation-service/agent-server-conversation-service.api.ts
@@ -15,7 +15,7 @@ import { AgentKind, Provider } from "#/types/settings";
import type { ConversationRuntimeContext } from "#/api/conversation-file-upload.api";
import { buildHttpBaseUrl } from "#/utils/websocket-url";
import {
- buildConversationWorkingDir,
+ buildConversationWorkingDirForBackend,
getAgentServerWorkingDir,
} from "../agent-server-config";
import { resolveAbsoluteAgentServerPath } from "../agent-server-home";
@@ -462,9 +462,28 @@ class AgentServerConversationService {
// to `/workspace/...` (read-only on macOS and fresh containers). When
// the user picks an explicit workspace, `workingDirOverride` is
// already absolute (it comes from `search_subdirs`).
- const workingDir = await resolveAbsoluteAgentServerPath(
- workingDirOverride ?? buildConversationWorkingDir(conversationId),
- );
+ //
+ // Pick the base working dir per-backend:
+ // 1. explicit user workspace pick β†’ use it as-is;
+ // 2. no pick, backend that served this frontend β†’ the baked default
+ // (honors a launcher-baked absolute `VITE_WORKING_DIR`);
+ // 3. no pick, any other backend β†’ the backend-relative default.
+ // A baked absolute dir is a path on the host that served this frontend,
+ // so it is only valid on that backend. Using it for a different backend
+ // (e.g. a remote sandbox) makes the agent-server mkdir an unwritable path
+ // and the conversation fails at the first prompt (e.g. `Permission
+ // denied: '/Users'`). The relative default is anchored per-backend by
+ // `resolveAbsoluteAgentServerPath()` via `/api/file/home`. The gate keys
+ // on the active backend's host (not its id): the seeded `default-local`
+ // entry is mutable, so a user can edit it to point at a remote host while
+ // its id stays `default-local`.
+ const baseWorkingDir =
+ workingDirOverride ??
+ buildConversationWorkingDirForBackend(
+ conversationId,
+ getActiveBackend().backend.host,
+ );
+ const workingDir = await resolveAbsoluteAgentServerPath(baseWorkingDir);
const resolvedWorkspaceMode =
workspaceMode ?? (workingDirOverride ? "local_repo" : "new_worktree");