π OpenHands: AI-Driven Development
fix(launcher): create the resolved terminal socket directory (#18368)
Engel Nyst1f91e1bcc0d0
diff --git a/__tests__/scripts/dev-with-automation.test.ts b/__tests__/scripts/dev-with-automation.test.tsindex 8f89284e68..2262db0300 100644--- a/__tests__/scripts/dev-with-automation.test.ts+++ b/__tests__/scripts/dev-with-automation.test.ts@@ -6,9 +6,15 @@// http://localhost:3000/), breaking that resolution; the Node environment// has the standard WHATWG URL behavior that honors the file:// base.import net from "node:net";-import { spawn } from "node:child_process";+import { spawn, spawnSync } from "node:child_process";import { once } from "node:events";-import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";+import {+ existsSync,+ mkdirSync,+ mkdtempSync,+ rmSync,+ writeFileSync,+} from "node:fs";import { homedir, tmpdir } from "node:os";import path from "node:path";import { setTimeout as delay } from "node:timers/promises";@@ -327,6 +333,61 @@ describe("buildConfig", () => {};}+ // Exercise the public launcher entry point through its existing embedder+ // prerequisite callback. Stop before services start; no test-only exports.+ function launchDirectoryProbe(env: Record<string, string>, args: string[]) {+ const probeHome = path.dirname(env.OH_SESSION_API_KEY_PATH);+ const script = `+ import { main } from ${JSON.stringify(pathToFileURL(path.join(repoRoot, "scripts/dev-with-automation.mjs")).href)};+ process.argv = [process.execPath, "directory-probe", ...${JSON.stringify(args)}];+ try {+ await main({+ staticMode: true,+ skipNpmCheck: true,+ extraPrereqs(config) {+ console.log("QA_DIRECTORY_PROBE=" + JSON.stringify({ stateDir: config.stateDir, tmuxTmpDir: config.tmuxTmpDir }));+ throw new Error("qa-directory-probe-complete");+ },+ });+ } catch (error) {+ if (error.message !== "qa-directory-probe-complete") throw error;+ }+ `;+ // Only command discovery is stubbed. No backend executes in this probe.+ if (process.platform === "win32") {+ writeFileSync(path.join(probeHome, "uvx.cmd"), "@exit /b 0\r\n");+ } else {+ writeFileSync(path.join(probeHome, "uvx"), "#!/bin/sh\nexit 0\n", {+ mode: 0o700,+ });+ }+ // An ambient TMUX_TMPDIR (e.g. npm test run inside tmux) would mask the+ // default; only the case's own env may set it.+ const { TMUX_TMPDIR: _ambientTmuxTmpDir, ...parentEnv } = process.env;+ const result = spawnSync(+ process.execPath,+ ["--input-type=module", "-e", script],+ {+ env: {+ ...parentEnv,+ ...env,+ HOME: probeHome,+ PATH: `${probeHome}${path.delimiter}${process.env.PATH ?? ""}`,+ OH_AGENT_SERVER_LOCAL_PATH: "",+ OH_AUTOMATION_LOCAL_PATH: "",+ },+ encoding: "utf8",+ timeout: 15_000,+ },+ );+ expect(result.status, result.stderr).toBe(0);+ const line = result.stdout+ .split("\n")+ .find((item) => item.startsWith("QA_DIRECTORY_PROBE="));+ expect(line, result.stdout).toBeDefined();+ return JSON.parse(line!.slice("QA_DIRECTORY_PROBE=".length));+ }+it("builds default config with correct ports", async () => {const config = await buildConfig({}, envWithIsolatedKeyPath());@@ -351,6 +412,49 @@ describe("buildConfig", () => {expect(ports.size).toBe(4);});+ it("creates separate default tmux directories before local backends start", async () => {+ const configs = [];+ for (let n = 0; n < 2; n += 1) {+ const env = envWithIsolatedKeyPath();+ env.OH_CANVAS_SAFE_STATE_DIR = path.join(+ path.dirname(env.OH_SESSION_API_KEY_PATH),+ "state",+ );+ const config = launchDirectoryProbe(env, ["--backend-only"]);+ const expected = path.join(config.stateDir, "tmux");+ expect(config.tmuxTmpDir).toBe(expected);+ expect(existsSync(expected)).toBe(true);+ configs.push(config);+ }+ expect(configs[0].tmuxTmpDir).not.toBe(configs[1].tmuxTmpDir);+ });++ it("creates and preserves an explicit socket-capable TMUX_TMPDIR", async () => {+ const env = envWithIsolatedKeyPath();+ env.OH_CANVAS_SAFE_STATE_DIR = path.join(+ path.dirname(env.OH_SESSION_API_KEY_PATH),+ "state",+ );+ env.TMUX_TMPDIR = path.join(+ path.dirname(env.OH_SESSION_API_KEY_PATH),+ "sockets",+ );+ const config = launchDirectoryProbe(env, ["--backend-only"]);+ expect(config.tmuxTmpDir).toBe(env.TMUX_TMPDIR);+ expect(existsSync(env.TMUX_TMPDIR)).toBe(true);+ expect(existsSync(path.join(config.stateDir, "tmux"))).toBe(false);+ });++ it("does not create a local terminal directory in frontend-only mode", async () => {+ const env = envWithIsolatedKeyPath();+ env.OH_CANVAS_SAFE_STATE_DIR = path.join(+ path.dirname(env.OH_SESSION_API_KEY_PATH),+ "state",+ );+ const config = launchDirectoryProbe(env, ["--frontend-only"]);+ expect(existsSync(path.join(config.stateDir, "tmux"))).toBe(false);+ });+it("lets --automation-git-ref win over an exported OH_AUTOMATION_LOCAL_PATH", async () => {// Otherwise someone with the checkout exported in their shell profile// reproduces a bug against their own working tree while believing theydiff --git a/docs/DEVELOPMENT.md b/docs/DEVELOPMENT.mdindex e864c4d42d..656c247c30 100644--- a/docs/DEVELOPMENT.md+++ b/docs/DEVELOPMENT.md@@ -60,9 +60,11 @@ conversation persistence by setting separate `OH_CONVERSATIONS_PATH`,keeps its tmux sockets under `~/.openhands/agent-canvas/tmux` (via`TMUX_TMPDIR`), so it does not collide with other local or cloud-backedOpenHands sessions. If `$HOME` is on a filesystem that does not support Unix-domain sockets (some devcontainers, NFS/CIFS homes), set the standard-`TMUX_TMPDIR` env var to a local path such as `/tmp` and the dev stack will use-it instead.+domain sockets (some devcontainers, NFS/CIFS homes), or if the complete tmux+socket path exceeds the host's Unix socket path limit, set the standard+`TMUX_TMPDIR` env var to a short local directory such as+`/tmp/openhands-tmux-instance-a`. The npm and dev launchers create the resolved+directory before starting the Agent Server and preserve an explicit override.### Environment Variablesdiff --git a/scripts/dev-static.mjs b/scripts/dev-static.mjsindex 4294572ebf..26628c3d81 100644--- a/scripts/dev-static.mjs+++ b/scripts/dev-static.mjs@@ -616,6 +616,7 @@ async function main() {join(config.stateDir, "workspaces"),join(config.stateDir, "bash_events"),join(config.stateDir, "storage"),+ config.tmuxTmpDir,]) {mkdirSync(dir, { recursive: true });}diff --git a/scripts/dev-with-automation.mjs b/scripts/dev-with-automation.mjsindex 96dfb10298..30d804aa8c 100644--- a/scripts/dev-with-automation.mjs+++ b/scripts/dev-with-automation.mjs@@ -524,6 +524,7 @@ async function buildConfig(args, env = process.env) {// Data directories (same as dev-safe.mjs)stateDir,+ tmuxTmpDir: safeConfig.tmuxTmpDir,// Only bake the host-side workspace path when this launcher also starts// the agent-server that can read it. In frontend-only mode the backend may// be a tunnel/remote service, so leave VITE_WORKING_DIR unset unless the@@ -608,6 +609,7 @@ function ensureDirectories(config) {if (config.launchAgentServer) {dirs.push(+ config.tmuxTmpDir,join(config.stateDir, "dev_conversations"),join(config.stateDir, "workspaces"),join(config.stateDir, "bash_events"),@@ -952,6 +954,7 @@ function startAgentServer(config) {OH_CANVAS_SAFE_STATE_DIR: config.stateDir,OH_CANVAS_SAFE_BACKEND_PORT: config.agentServerPort.toString(),OH_CANVAS_SAFE_VSCODE_PORT: config.vscodePort.toString(),+ TMUX_TMPDIR: config.tmuxTmpDir,});const agentServerEnv = {