Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix(dev:docker): container-safe automation paths, hostnames, and AGENT_SERVER_URL alias (#603)

Tim O'Farrell2ac97d1bd8f3
diff --git a/scripts/dev-docker.mjs b/scripts/dev-docker.mjs
index 306c18c67c..416f45da2f 100644
--- a/scripts/dev-docker.mjs
+++ b/scripts/dev-docker.mjs
@@ -354,6 +354,21 @@ function startAgentServerDocker(config) {
// Required so the secret-seeding PUT /api/settings/secrets call from
// the host can authenticate against the agent-server in the container.
OH_SESSION_API_KEYS_0: config.sessionApiKey,
+ // Alias for the agent-server's own URL. The agent-server itself sets
+ // OH_INTERNAL_SERVER_URL=http://127.0.0.1:8000 at startup (host
+ // 0.0.0.0 is rewritten to loopback) and the value is inherited by
+ // every bash subprocess it spawns β€” including the automation run's
+ // main.py / setup.sh. Those scripts read AGENT_SERVER_URL (the
+ // canonical SDK name) instead of the OH_-prefixed variant, so we
+ // mirror it here. 127.0.0.1:8000 is the agent-server's own URL from
+ // *inside* its container (it always listens on port 8000 in
+ // dev:docker).
+ //
+ // We deliberately do NOT set a SESSION_API_KEY alias: the SDK's
+ // sanitized_env() would strip it from bash subprocesses anyway, and
+ // a follow-up change to the automation preset reads
+ // OH_SESSION_API_KEYS_0 directly (which is already in env).
+ AGENT_SERVER_URL: "http://127.0.0.1:8000",
// Make the mounted canvas-tools directory importable so the agent-server
// can resolve modules listed in tool_module_qualnames (e.g. canvas_ui_tool).
OH_EXTRA_PYTHON_PATH: CONTAINER_CANVAS_TOOLS_DIR,
@@ -402,6 +417,21 @@ if (isMainModule) {
extraPrereqs: checkDockerPrereqs,
startAgentServer: startAgentServerDocker,
viteWorkingDir: CONTAINER_WORKSPACES_DIR,
+ // The automation backend runs on the host but its dispatched
+ // `mkdir -p ...` commands execute inside the agent-server container.
+ // Force `AUTOMATION_WORKSPACE_BASE` to a path that exists in that
+ // container so runs don't fail trying to create host paths like
+ // /Users/<you>/... inside Linux. Users can still override by setting
+ // AUTOMATION_WORKSPACE_BASE in their environment.
+ automationWorkspaceBase: CONTAINER_WORKSPACES_DIR,
+ // The automation backend tells each automation sandbox where to
+ // reach it back via `AUTOMATION_API_URL` (used by setup.sh to fetch
+ // /sdk-version and by the SDK to POST run completion). The sandbox
+ // is a separate Docker container, so `localhost` inside it is the
+ // sandbox itself β€” it cannot resolve `http://localhost:<ingress>`
+ // to the host ingress. Use the cross-container hostname instead.
+ // Users can still override by setting AUTOMATION_BASE_URL.
+ automationApiHost: "host.docker.internal",
defaultStaticMode: true,
buildStaticFrontend: buildFrontend,
// The agent-server runs inside a Docker container in this mode, so
diff --git a/scripts/dev-safe.mjs b/scripts/dev-safe.mjs
index 3e423b7c82..caf558bc49 100644
--- a/scripts/dev-safe.mjs
+++ b/scripts/dev-safe.mjs
@@ -605,6 +605,18 @@ export function buildAgentServerEnv(config) {
OH_SECRET_KEY: config.secretKey,
// Use OH_SESSION_API_KEYS_0 for agent-server V1 config format
OH_SESSION_API_KEYS_0: config.sessionApiKey,
+ // Alias for the agent-server's own URL. The agent-server itself sets
+ // OH_INTERNAL_SERVER_URL at startup, but downstream consumers (the
+ // OpenHands SDK boilerplate emitted by automation prompt/plugin
+ // presets) read AGENT_SERVER_URL β€” the canonical SDK name. Mirror it
+ // here so automation runs work without each tarball having to know
+ // about the OH_-prefixed variant.
+ //
+ // We deliberately do NOT set a SESSION_API_KEY alias: the SDK's
+ // sanitized_env() would strip it from bash subprocesses anyway, and
+ // a follow-up change to the automation preset reads
+ // OH_SESSION_API_KEYS_0 directly (which is already in env).
+ AGENT_SERVER_URL: config.backendBaseUrl,
// Make the host tools/ directory importable so the agent-server can
// resolve modules listed in tool_module_qualnames (e.g. canvas_ui_tool).
OH_EXTRA_PYTHON_PATH: config.canvasToolsDir,
diff --git a/scripts/dev-with-automation.mjs b/scripts/dev-with-automation.mjs
index 46b30a26e9..89190f4f90 100644
--- a/scripts/dev-with-automation.mjs
+++ b/scripts/dev-with-automation.mjs
@@ -579,11 +579,60 @@ function startAutomationBackend(config) {
{
cwd: config.stateDir,
env: {
- AUTOMATION_AGENT_SERVER_URL: `http://localhost:${config.agentServerPort}`,
+ // The automation backend uses this to call the agent-server's REST
+ // API for uploads and bash dispatch (host-side) AND it propagates
+ // the same value into the in-sandbox bash command as the
+ // `AGENT_SERVER_URL` env var that main.py reads to connect back.
+ //
+ // In dev:docker that script runs inside the agent-server container,
+ // so the URL has to be reachable from *both* the host and the
+ // container. `host.docker.internal:${agentServerPort}` satisfies
+ // both: from the host it's a loopback alias, and from inside the
+ // container Docker's host-gateway routes back through the published
+ // port. A little wasteful (the in-container script bounces through
+ // the host port-forward) but it's the only single value that works
+ // both ways without changes to the automation backend.
+ //
+ // Priority:
+ // 1. AUTOMATION_AGENT_SERVER_URL explicitly set in the user's env
+ // 2. launcher-provided host (dev-docker.mjs sets
+ // `automationApiHost: "host.docker.internal"`)
+ // 3. `localhost` (correct for dockerless mode where backend and
+ // agent-server both run on the host)
+ AUTOMATION_AGENT_SERVER_URL:
+ process.env.AUTOMATION_AGENT_SERVER_URL ||
+ `http://${config.automationApiHost ?? "localhost"}:${config.agentServerPort}`,
AUTOMATION_AGENT_SERVER_API_KEY: config.sessionApiKey,
AUTOMATION_DB_URL: `sqlite+aiosqlite:///${join(config.stateDir, "automations.db")}`,
- AUTOMATION_BASE_URL: `http://localhost:${config.ingressPort}`,
- AUTOMATION_WORKSPACE_BASE: join(config.stateDir, "workspaces"),
+ // The automation backend uses this as its publicly-reachable base
+ // URL: it's appended to callback URLs and injected into each
+ // sandbox as `AUTOMATION_API_URL` (consumed by setup.sh for
+ // /sdk-version and by the SDK for run completion). In dev:docker
+ // the sandbox is a separate container, so `localhost` won't reach
+ // the host ingress β€” the launcher therefore passes
+ // `automationApiHost: "host.docker.internal"` to override the host.
+ // Priority:
+ // 1. AUTOMATION_BASE_URL explicitly set in the user's env
+ // 2. launcher-provided host (dev-docker.mjs)
+ // 3. `localhost` (correct for dockerless mode)
+ AUTOMATION_BASE_URL:
+ process.env.AUTOMATION_BASE_URL ||
+ `http://${config.automationApiHost ?? "localhost"}:${config.ingressPort}`,
+ // The dispatcher (running on the host) resolves this path and
+ // embeds it into a `mkdir -p ...` shell command that is then
+ // executed *inside* the agent-server container. So the value must
+ // be valid in the container's filesystem, not just on the host.
+ // Priority:
+ // 1. AUTOMATION_WORKSPACE_BASE explicitly set in the user's env
+ // 2. `automationWorkspaceBase` option passed by the launcher
+ // (dev-docker.mjs sets this to a container-safe path)
+ // 3. host-side default that lives under config.stateDir β€” fine
+ // for dockerless mode where the dispatcher and agent-server
+ // share the host filesystem.
+ AUTOMATION_WORKSPACE_BASE:
+ process.env.AUTOMATION_WORKSPACE_BASE ||
+ config.automationWorkspaceBase ||
+ join(config.stateDir, "workspaces"),
// Local API key for self-hosted auth (no cloud API needed)
AUTOMATION_LOCAL_API_KEY: config.localApiKey,
// CORS: allow localhost origins for dev
@@ -862,6 +911,17 @@ async function main(options = {}) {
startAgentServer: startAgentServerOverride,
extraPrereqs,
viteWorkingDir,
+ // Path (in whatever filesystem the agent-server can mkdir into) used
+ // as `AUTOMATION_WORKSPACE_BASE` by the automation backend. dev-docker
+ // sets this to a path that exists inside the agent-server container;
+ // dockerless mode leaves it undefined so the host-side default applies.
+ automationWorkspaceBase,
+ // Host used in `AUTOMATION_BASE_URL` (the URL the automation sandbox
+ // uses to call back into the automation backend). dev-docker sets
+ // this to `host.docker.internal` so the sandbox container can reach
+ // the host ingress; dockerless mode leaves it undefined and the
+ // default `localhost` applies.
+ automationApiHost,
staticMode: staticModeOverride,
defaultStaticMode = false,
buildStaticFrontend,
@@ -903,6 +963,12 @@ async function main(options = {}) {
// Build config with dynamic port allocation
const config = await buildConfig(args);
if (viteWorkingDir) config.viteWorkingDir = viteWorkingDir;
+ if (automationWorkspaceBase) {
+ config.automationWorkspaceBase = automationWorkspaceBase;
+ }
+ if (automationApiHost) {
+ config.automationApiHost = automationApiHost;
+ }
// Stamp the dev-mode label, host alias, and frontend kind on the config
// so downstream helpers (Vite spawn, static build) can produce a
// runtime-services info object describing what the agent can reach.