π OpenHands: AI-Driven Development
fix(scripts): use 127.0.0.1 for remaining localhost service URLs (#16409)
MarMar Labs2738be202577
diff --git a/__tests__/scripts/dev-static.test.ts b/__tests__/scripts/dev-static.test.tsindex 04f2784e1b..c1000ed404 100644--- a/__tests__/scripts/dev-static.test.ts+++ b/__tests__/scripts/dev-static.test.ts@@ -1,6 +1,9 @@import { describe, expect, it } from "vitest";-import { buildAutomationBackendEnv } from "../../scripts/dev-static.mjs";+import {+ buildAutomationBackendEnv,+ buildLocalServiceRouteArgs,+} from "../../scripts/dev-static.mjs";describe("dev-static", () => {it("uses the same session key for both agent-server and automation backend auth", () => {@@ -16,7 +19,7 @@ describe("dev-static", () => {// Both backends receive the same key valueexpect(env).toMatchObject({- AUTOMATION_AGENT_SERVER_URL: "http://localhost:18000",+ AUTOMATION_AGENT_SERVER_URL: "http://127.0.0.1:18000",AUTOMATION_AGENT_SERVER_API_KEY: "shared-session-key",AUTOMATION_LOCAL_API_KEY: "shared-session-key",AUTOMATION_POSTHOG_API_KEY:@@ -24,4 +27,17 @@ describe("dev-static", () => {AUTOMATION_POSTHOG_HOST: "https://us.i.posthog.com",});});++ it("points every local proxy route at the IPv4 loopback", () => {+ // Both backends bind to `0.0.0.0`, which only accepts IPv4, so a+ // `localhost` target strands the proxy on ::1 on Windows.+ const args = buildLocalServiceRouteArgs({+ agentServerPort: 18000,+ autoBackendPort: 18001,+ });++ expect(args).toContain("/api/automation=http://127.0.0.1:18001");+ expect(args).toContain("/server_info=http://127.0.0.1:18000");+ expect(args.filter((arg: string) => arg.includes("localhost"))).toEqual([]);+ });});diff --git a/__tests__/scripts/dev-with-automation.test.ts b/__tests__/scripts/dev-with-automation.test.tsindex f6e108c161..b2d8754684 100644--- a/__tests__/scripts/dev-with-automation.test.ts+++ b/__tests__/scripts/dev-with-automation.test.ts@@ -21,6 +21,7 @@ import {buildConfig,buildRouteArgs,buildViteBackendEnv,+ getAgentServerBaseUrl,getFrontendBackend,getLocalServiceRoutes,setServiceLogListener,@@ -629,6 +630,18 @@ describe("stack mode routing", () => {expect(routeArgs).not.toContain("--default");});+ it("addresses the agent-server over IPv4 for readiness and secret seeding", async () => {+ const config = await buildConfig({}, envWithIsolatedKeyPath());++ // The launcher starts the agent-server with `--host 127.0.0.1`, so the+ // readiness probe (`/server_info`), the secret-seeding request, and the+ // automation backend's AUTOMATION_AGENT_SERVER_URL must all skip the+ // `localhost` lookup that resolves to ::1 first on Windows.+ expect(getAgentServerBaseUrl(config)).toBe(+ `http://127.0.0.1:${config.agentServerPort}`,+ );+ });+it("rejects mutually exclusive partial-stack modes", async () => {await expect(buildConfig(diff --git a/scripts/dev-static.mjs b/scripts/dev-static.mjsindex 3d10da46aa..53be447e72 100644--- a/scripts/dev-static.mjs+++ b/scripts/dev-static.mjs@@ -285,6 +285,44 @@ async function waitForService(name, url, timeoutMs = 30000) {// dev-with-automation; the only difference is the frontend service.)// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ+// Both backends bind to `0.0.0.0`, which only accepts IPv4, but localhost can+// resolve to ::1 first (notably on Windows). Every proxy target and readiness+// probe pointed at them must therefore address IPv4 explicitly.+function getAgentServerBaseUrl(config) {+ return `http://127.0.0.1:${config.agentServerPort}`;+}++function getAutomationBaseUrl(config) {+ return `http://127.0.0.1:${config.autoBackendPort}`;+}++const AUTOMATION_ROUTE_PREFIX = "/api/automation";+const AGENT_SERVER_ROUTE_PREFIXES = [+ "/api",+ "/sockets",+ "/server_info",+ "/health",+ "/ready",+ "/alive",+ "/docs",+ "/redoc",+ "/openapi.json",+];++// The static server and the ingress proxy front the same two local backends,+// so they share one route table.+function buildLocalServiceRouteArgs(config) {+ const agentServerUrl = getAgentServerBaseUrl(config);+ return [+ "--route",+ `${AUTOMATION_ROUTE_PREFIX}=${getAutomationBaseUrl(config)}`,+ ...AGENT_SERVER_ROUTE_PREFIXES.flatMap((prefix) => [+ "--route",+ `${prefix}=${agentServerUrl}`,+ ]),+ ];+}+function startAgentServer(config) {logService("agent-server",@@ -328,7 +366,7 @@ function startAgentServer(config) {function buildAutomationBackendEnv(config, env = process.env) {// Both backends share the same session API key value.return {- AUTOMATION_AGENT_SERVER_URL: `http://localhost:${config.agentServerPort}`,+ AUTOMATION_AGENT_SERVER_URL: getAgentServerBaseUrl(config),AUTOMATION_AGENT_SERVER_API_KEY: config.sessionApiKey,AUTOMATION_DB_URL: `sqlite+aiosqlite:///${join(config.stateDir, "automations.db")}`,AUTOMATION_BASE_URL: `http://localhost:${config.ingressPort}`,@@ -405,26 +443,7 @@ function startStaticServer(config) {: []),"--runtime-services-info",runtimeServicesInfo,- "--route",- `/api/automation=http://localhost:${config.autoBackendPort}`,- "--route",- `/api=http://localhost:${config.agentServerPort}`,- "--route",- `/sockets=http://localhost:${config.agentServerPort}`,- "--route",- `/server_info=http://localhost:${config.agentServerPort}`,- "--route",- `/health=http://localhost:${config.agentServerPort}`,- "--route",- `/ready=http://localhost:${config.agentServerPort}`,- "--route",- `/alive=http://localhost:${config.agentServerPort}`,- "--route",- `/docs=http://localhost:${config.agentServerPort}`,- "--route",- `/redoc=http://localhost:${config.agentServerPort}`,- "--route",- `/openapi.json=http://localhost:${config.agentServerPort}`,+ ...buildLocalServiceRouteArgs(config),],{cwd: config.canvasPath,@@ -453,26 +472,7 @@ function startIngress(config) {config.ingressPort.toString(),"--runtime-services-info",runtimeServicesInfo,- "--route",- `/api/automation=http://localhost:${config.autoBackendPort}`,- "--route",- `/api=http://localhost:${config.agentServerPort}`,- "--route",- `/sockets=http://localhost:${config.agentServerPort}`,- "--route",- `/server_info=http://localhost:${config.agentServerPort}`,- "--route",- `/health=http://localhost:${config.agentServerPort}`,- "--route",- `/ready=http://localhost:${config.agentServerPort}`,- "--route",- `/alive=http://localhost:${config.agentServerPort}`,- "--route",- `/docs=http://localhost:${config.agentServerPort}`,- "--route",- `/redoc=http://localhost:${config.agentServerPort}`,- "--route",- `/openapi.json=http://localhost:${config.agentServerPort}`,+ ...buildLocalServiceRouteArgs(config),"--default",`http://localhost:${config.vitePort}`,],@@ -621,7 +621,7 @@ async function main() {startAgentServer(config);await waitForService("agent-server",- `http://localhost:${config.agentServerPort}/server_info`,+ `${getAgentServerBaseUrl(config)}/server_info`,);startAutomationBackend(config);@@ -641,7 +641,13 @@ async function main() {// Exports for testing// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ-export { buildAutomationBackendEnv, buildFrontend, startStaticServer };+export {+ buildAutomationBackendEnv,+ buildFrontend,+ buildLocalServiceRouteArgs,+ getAgentServerBaseUrl,+ startStaticServer,+};// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ// Main entry point (only when run directly, not when imported)diff --git a/scripts/dev-with-automation.mjs b/scripts/dev-with-automation.mjsindex a965c07634..1c1b447277 100644--- a/scripts/dev-with-automation.mjs+++ b/scripts/dev-with-automation.mjs@@ -735,6 +735,13 @@ const AGENT_SERVER_ROUTE_PREFIXES = ["/openapi.json",];+// This launcher starts the agent-server with `--host 127.0.0.1`, but localhost+// can resolve to ::1 first (notably on Windows), so every request this process+// or the automation backend makes to it must address IPv4 explicitly.+function getAgentServerBaseUrl(config) {+ return `http://127.0.0.1:${config.agentServerPort}`;+}+function getLocalServiceRoutes(config) {const routes = [];@@ -748,7 +755,7 @@ function getLocalServiceRoutes(config) {if (config.launchAgentServer) {for (const prefix of AGENT_SERVER_ROUTE_PREFIXES) {- routes.push([prefix, `http://127.0.0.1:${config.agentServerPort}`]);+ routes.push([prefix, getAgentServerBaseUrl(config)]);}}@@ -931,10 +938,10 @@ function startAutomationBackend(config) {//// Priority:// 1. AUTOMATION_AGENT_SERVER_URL explicitly set in the user's env- // 2. `localhost:<agentServerPort>`+ // 2. `127.0.0.1:<agentServerPort>`AUTOMATION_AGENT_SERVER_URL:process.env.AUTOMATION_AGENT_SERVER_URL ||- `http://localhost:${config.agentServerPort}`,+ getAgentServerBaseUrl(config),// The URL exported into the in-sandbox bash chain as// `AGENT_SERVER_URL` (read by main.py / setup.sh to call back into// the agent-server).@@ -1141,7 +1148,7 @@ async function seedAutomationSecret(config, options = {}) {logService("secrets", `Seeding ${secretName} into agent-server...`, c.dim);- const url = `http://localhost:${config.agentServerPort}/api/settings/secrets`;+ const url = `${getAgentServerBaseUrl(config)}/api/settings/secrets`;const body = JSON.stringify({name: secretName,value: config.sessionApiKey,@@ -1476,7 +1483,7 @@ async function main(options = {}) {agentServerReady = await waitForService("agent-server",- `http://localhost:${config.agentServerPort}/server_info`,+ `${getAgentServerBaseUrl(config)}/server_info`,agentServerReadyTimeoutMs,);}@@ -1586,6 +1593,7 @@ export {buildConfig,buildRouteArgs,buildViteBackendEnv,+ getAgentServerBaseUrl,getFrontendBackend,getLocalServiceRoutes,main,