Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions scripts/checks/vitest-project-overlap.mts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const INSTALLER_INTEGRATION_TESTS = new Set([
"test/install-clone-ref.test.ts",
"test/install-express-prompt.test.ts",
"test/install-express-wsl-ollama.test.ts",
"test/install-forward-restore-diagnostics.test.ts",
"test/install-managed-cli-reuse.test.ts",
"test/install-openshell-version-pin.test.ts",
"test/install-openshell-version-check.test.ts",
Expand Down
52 changes: 48 additions & 4 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ resolve_onboarded_agent() {
}

restore_onboard_forward_after_post_checks() {
local sandbox_name agent_name agent_display port openshell_bin openshell_dir attempt selected_state_dir state_dir pid_file watcher_script watcher_pid
local sandbox_name agent_name agent_display port openshell_bin openshell_dir attempt selected_state_dir state_dir pid_file watcher_script watcher_pid start_diagnostic diagnostic_file
sandbox_name="$(resolve_default_sandbox_name)"
agent_name="$(resolve_onboarded_agent)"
agent_display="$(agent_display_name "$agent_name")"
Expand Down Expand Up @@ -532,12 +532,33 @@ restore_onboard_forward_after_post_checks() {
fi
}

start_diagnostic=""
diagnostic_file="$(mktemp "${TMPDIR:-/tmp}/nemoclaw-forward-start-XXXXXX" 2>/dev/null)" \
|| diagnostic_file=""

for attempt in 1 2 3; do
stop_agent_forward_if_owned
if [ "$attempt" -gt 1 ]; then
sleep 2
fi
"$openshell_bin" forward start --background "$port" "$sandbox_name" >/dev/null 2>&1 || true
if [[ -n "$diagnostic_file" ]]; then
"$openshell_bin" forward start --background "$port" "$sandbox_name" \
>"$diagnostic_file" 2>&1 || true
start_diagnostic="$(awk '
{
gsub(/[[:space:]]+/, " ")
sub(/^ /, "")
sub(/ $/, "")
if ($0 != "") joined = (joined == "" ? $0 : joined "; " $0)
}
END { printf "%s", joined }
' <"$diagnostic_file" 2>/dev/null || true)"
if [[ "${#start_diagnostic}" -gt 300 ]]; then
start_diagnostic="${start_diagnostic:0:300} [truncated]"
fi
else
"$openshell_bin" forward start --background "$port" "$sandbox_name" >/dev/null 2>&1 || true
fi
watcher_pid=""
if [[ "${NEMOCLAW_SKIP_FORWARD_WATCHER:-}" != "1" ]] && command_exists node; then
watcher_script="${pid_file}.js"
Expand All @@ -552,10 +573,27 @@ function healthy() {
stdio: "ignore",
}).status === 0;
}
function listedStatus() {
const listed = spawnSync(openshellBin, ["forward", "list"], { encoding: "utf-8" });
if (listed.status !== 0 || typeof listed.stdout !== "string") return null;
for (const line of listed.stdout.split("\n")) {
const columns = line.trim().split(/\s+/);
if (columns[0] === sandboxName && columns[2] === port) {
return (columns[4] || "").toLowerCase();
}
}
return "";
}
function tick() {
if (healthy()) return;
run(["forward", "stop", port, sandboxName]);
run(["forward", "start", "--background", port, sandboxName]);
const status = listedStatus();
if (status === null) return;
if (status === "dead") {
run(["forward", "stop", port, sandboxName]);
run(["forward", "start", "--background", port, sandboxName]);
return;
}
if (status === "") run(["forward", "start", "--background", port, sandboxName]);
}
tick();
setInterval(tick, 10_000);
Expand All @@ -576,19 +614,25 @@ NODE
sleep 4
if command_exists curl \
&& curl -sf --max-time 3 "http://127.0.0.1:${port}/health" >/dev/null 2>&1; then
[[ -n "$diagnostic_file" ]] && rm -f "$diagnostic_file"
return 0
fi
watcher_pid="$(cat "$pid_file" 2>/dev/null || true)"
if ! command_exists curl && [[ -n "$watcher_pid" ]] && kill -0 "$watcher_pid" >/dev/null 2>&1; then
[[ -n "$diagnostic_file" ]] && rm -f "$diagnostic_file"
return 0
fi
if [[ -n "$watcher_pid" ]]; then
kill "$watcher_pid" >/dev/null 2>&1 || true
fi
rm -f "$pid_file"
done
[[ -n "$diagnostic_file" ]] && rm -f "$diagnostic_file"

warn "Could not restore ${agent_display} host forward on port ${port}."
if [[ -n "$start_diagnostic" ]]; then
warn "OpenShell reported: ${start_diagnostic:0:300}"
fi
warn "Run: openshell forward start --background ${port} ${sandbox_name}"
return 1
}
Expand Down
215 changes: 215 additions & 0 deletions test/install-forward-restore-diagnostics.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

import { spawnSync } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";

import { describe, expect, it } from "vitest";

const REPOSITORY_ROOT = path.resolve(import.meta.dirname, "..");
const INSTALLER = path.join(REPOSITORY_ROOT, "scripts", "install.sh");
const SANDBOX = "created-by-onboard";
const PORT = "8642";
const LISTENER_FAILURE = [
"ssh process started but local forward listener was not reachable",
`local forward listener did not open on 127.0.0.1:${PORT} within 10000ms`,
"last probe failed with Connection refused (os error 111)",
];

function writeExecutable(target: string, contents: string): void {
fs.writeFileSync(target, contents, { mode: 0o755 });
}

function prepareCheckout(prefix: string): { root: string; binDir: string } {
const root = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
const binDir = path.join(root, "bin");
const stateDir = path.join(root, ".nemoclaw");
fs.mkdirSync(binDir, { recursive: true });
fs.mkdirSync(stateDir, { recursive: true });
fs.writeFileSync(
path.join(stateDir, "onboard-session.json"),
JSON.stringify({ sandboxName: SANDBOX, agent: "hermes" }),
);
for (const command of ["sleep"]) {
writeExecutable(path.join(binDir, command), "#!/usr/bin/env bash\nexit 0\n");
}
return { root, binDir };
}

function runRestore(root: string, binDir: string, env: Record<string, string> = {}) {
return spawnSync(
"bash",
["-c", 'source "$INSTALLER" 2>/dev/null; restore_onboard_forward_after_post_checks'],
{
cwd: REPOSITORY_ROOT,
encoding: "utf-8",
env: {
...process.env,
HOME: root,
INSTALLER,
PATH: `${binDir}:${path.dirname(process.execPath)}:/usr/bin:/bin`,
...env,
},
},
);
}

function findWatcherScript(root: string): string {
const stack = [root];
while (stack.length > 0) {
const current = stack.pop() as string;
for (const entry of fs.readdirSync(current, { withFileTypes: true })) {
const full = path.join(current, entry.name);
if (entry.isDirectory()) stack.push(full);
else if (entry.name.endsWith(".forward.pid.js")) return full;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
}
throw new Error(`no watcher script written under ${root}`);
}

function runWatcherTick(watcherScript: string, binDir: string, openshell: string): string {
const log = `${watcherScript}.calls`;
spawnSync(process.execPath, [watcherScript, openshell, PORT, SANDBOX], {
encoding: "utf-8",
env: { ...process.env, OPENSHELL_LOG: log, PATH: `${binDir}:/usr/bin:/bin` },
killSignal: "SIGKILL",
timeout: 4_000,
});
return fs.existsSync(log) ? fs.readFileSync(log, "utf-8") : "";
}

function watcherScriptForListing(
prefix: string,
listing: string,
): {
binDir: string;
openshell: string;
root: string;
watcherScript: string;
} {
const { root, binDir } = prepareCheckout(prefix);
const openshell = path.join(binDir, "openshell");
writeExecutable(
openshell,
`#!/usr/bin/env bash
if [ -n "\${OPENSHELL_LOG:-}" ]; then printf '%s\\n' "$*" >> "$OPENSHELL_LOG"; fi
if [ "$1" = "forward" ] && [ "$2" = "list" ]; then
echo "SANDBOX BIND PORT PID STATUS"
${listing}
fi
exit 0
`,
);
writeExecutable(path.join(binDir, "curl"), "#!/usr/bin/env bash\nexit 7\n");
writeExecutable(
path.join(binDir, "node"),
`#!/usr/bin/env bash
if [ "\${1:-}" = "-e" ] && [[ "\${2:-}" == *"const { spawn }"* ]]; then exit 0; fi
exec ${JSON.stringify(process.execPath)} "$@"
`,
);
const result = runRestore(root, binDir);
expect(result.status).toBe(1);
return { binDir, openshell, root, watcherScript: findWatcherScript(root) };
}

describe("Hermes host forward restore diagnostics", () => {
it("reports why the OpenShell forward start failed (#8884)", () => {
const { root, binDir } = prepareCheckout("nemohermes-forward-diagnostic-");
try {
writeExecutable(
path.join(binDir, "openshell"),
`#!/usr/bin/env bash
if [ "$1" = "forward" ] && [ "$2" = "list" ]; then
echo "SANDBOX BIND PORT PID STATUS"
exit 0
fi
if [ "$1" = "forward" ] && [ "$2" = "start" ]; then
${LISTENER_FAILURE.map((line) => ` echo ${JSON.stringify(line)} >&2`).join("\n")}
exit 1
fi
exit 0
`,
);
writeExecutable(path.join(binDir, "curl"), "#!/usr/bin/env bash\nexit 7\n");

const result = runRestore(root, binDir, { NEMOCLAW_SKIP_FORWARD_WATCHER: "1" });

expect(result.status).toBe(1);
expect(result.stdout).toContain(`Could not restore Hermes host forward on port ${PORT}.`);
expect(result.stdout).toContain("OpenShell reported:");
for (const line of LISTENER_FAILURE) {
expect(result.stdout).toContain(line);
}
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
});

it("omits the `OpenShell reported:` warning when the failed start printed no output (#8884)", () => {
const { root, binDir } = prepareCheckout("nemohermes-forward-silent-");
try {
writeExecutable(path.join(binDir, "openshell"), "#!/usr/bin/env bash\nexit 1\n");
writeExecutable(path.join(binDir, "curl"), "#!/usr/bin/env bash\nexit 7\n");

const result = runRestore(root, binDir, { NEMOCLAW_SKIP_FORWARD_WATCHER: "1" });

expect(result.status).toBe(1);
expect(result.stdout).toContain(`Could not restore Hermes host forward on port ${PORT}.`);
expect(result.stdout).not.toContain("OpenShell reported:");
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
});
});

describe("Hermes host forward watcher", () => {
it("does not stop a forward that OpenShell lists as running when the health check fails (#8884)", () => {
const { root, binDir, openshell, watcherScript } = watcherScriptForListing(
"nemohermes-watcher-live-",
` echo "${SANDBOX} 127.0.0.1 ${PORT} 123 running"`,
);
try {
const calls = runWatcherTick(watcherScript, binDir, openshell);

expect(calls).toContain("forward list");
expect(calls).not.toContain(`forward stop ${PORT} ${SANDBOX}`);
expect(calls).not.toContain(`forward start --background ${PORT} ${SANDBOX}`);
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
});

it("does not run forward stop before starting a forward that OpenShell does not list (#8884)", () => {
const { root, binDir, openshell, watcherScript } = watcherScriptForListing(
"nemohermes-watcher-absent-",
" :",
);
try {
const calls = runWatcherTick(watcherScript, binDir, openshell);

expect(calls).toContain(`forward start --background ${PORT} ${SANDBOX}`);
expect(calls).not.toContain(`forward stop ${PORT} ${SANDBOX}`);
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
});

it("restarts a forward OpenShell reports as dead (#8884)", () => {
const { root, binDir, openshell, watcherScript } = watcherScriptForListing(
"nemohermes-watcher-dead-",
` echo "${SANDBOX} 127.0.0.1 ${PORT} 123 dead"`,
);
try {
const calls = runWatcherTick(watcherScript, binDir, openshell);

expect(calls).toContain(`forward stop ${PORT} ${SANDBOX}`);
expect(calls).toContain(`forward start --background ${PORT} ${SANDBOX}`);
expect(calls.indexOf("forward stop")).toBeLessThan(calls.indexOf("forward start"));
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
});
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
2 changes: 2 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export default defineConfig({
"test/install-station-vllm-continuation.test.ts",
"test/install-build-dependency-preflight.test.ts",
"test/install-clone-ref.test.ts",
"test/install-forward-restore-diagnostics.test.ts",
"test/install-managed-cli-reuse.test.ts",
"test/install-preflight.test.ts",
"test/install-preflight-docker-bootstrap.test.ts",
Expand Down Expand Up @@ -193,6 +194,7 @@ export default defineConfig({
"test/install-station-vllm-continuation.test.ts",
"test/install-build-dependency-preflight.test.ts",
"test/install-clone-ref.test.ts",
"test/install-forward-restore-diagnostics.test.ts",
"test/install-managed-cli-reuse.test.ts",
"test/install-preflight.test.ts",
"test/install-preflight-docker-bootstrap.test.ts",
Expand Down
Loading