Broaden NemoClaw home directory discovery

This commit is contained in:
Codex
2026-04-27 15:42:44 +00:00
parent b82e8fb56e
commit a5ab13f578
4 changed files with 115 additions and 11 deletions
+2 -2
View File
@@ -2,7 +2,7 @@ import { NextResponse } from "next/server"
import { execFile, spawn } from "node:child_process"
import { promisify } from "node:util"
import { inspectSandbox } from "@/app/lib/openshellHost"
import { commandExists, HOST_PATH, NEMOCLAW_SETUP, OPENSHELL_BIN } from "@/app/lib/hostCommands"
import { commandExists, HOST_PATH, NEMOCLAW_SETUP, NEMOCLAW_SETUP_CANDIDATES, OPENSHELL_BIN } from "@/app/lib/hostCommands"
const execFileAsync = promisify(execFile)
@@ -44,7 +44,7 @@ function appendNote(...parts: Array<string | false | null | undefined>) {
function requireNemoClawSetup() {
if (NEMOCLAW_SETUP && commandExists(NEMOCLAW_SETUP)) return NEMOCLAW_SETUP
throw new Error(
"NemoClaw blueprint setup script was not found. Install NemoClaw on this host or set NEMOCLAW_SETUP in .env.local to the absolute path of scripts/setup.sh.",
`NemoClaw blueprint setup script was not found. Install NemoClaw under your Linux home directory, or set NEMOCLAW_SETUP in .env.local to the absolute path of scripts/setup.sh. Searched: ${NEMOCLAW_SETUP_CANDIDATES.join(", ") || "no candidates"}.`,
)
}
+63 -8
View File
@@ -1,4 +1,4 @@
import { existsSync } from "node:fs"
import { existsSync, readdirSync, statSync } from "node:fs"
import path from "node:path"
const HOME = process.env.HOME || ""
@@ -7,6 +7,50 @@ function firstExisting(candidates: Array<string | undefined>, fallback: string)
return candidates.filter((value): value is string => Boolean(value)).find((candidate) => existsSync(candidate)) || fallback
}
function unique(values: Array<string | undefined>) {
return Array.from(new Set(values.filter((value): value is string => Boolean(value))))
}
function safeIsDirectory(candidate: string) {
try {
return statSync(candidate).isDirectory()
} catch {
return false
}
}
function homeChildDirectories() {
if (!HOME || !safeIsDirectory(HOME)) return []
try {
return readdirSync(HOME, { withFileTypes: true })
.filter((entry) => entry.isDirectory())
.map((entry) => path.join(HOME, entry.name))
.filter((directory) => !/(^|\/)(node_modules|\.cache|\.npm|\.local\/share\/Trash)$/.test(directory))
} catch {
return []
}
}
function discoverHomeFiles(relativePath: string) {
const found: string[] = []
for (const directory of homeChildDirectories()) {
const direct = path.join(directory, relativePath)
if (existsSync(direct)) found.push(direct)
try {
for (const entry of readdirSync(directory, { withFileTypes: true })) {
if (!entry.isDirectory()) continue
if (["node_modules", ".git", ".next", "dist", "build"].includes(entry.name)) continue
const nested = path.join(directory, entry.name, relativePath)
if (existsSync(nested)) found.push(nested)
}
} catch {
// Ignore unreadable home subdirectories.
}
}
return found
}
function pathEntries() {
return [
process.env.TERMINAL_EXTRA_PATH,
@@ -25,7 +69,7 @@ function pathEntries() {
].filter((value): value is string => Boolean(value))
}
export const HOST_PATH = Array.from(new Set(pathEntries())).join(":")
export const HOST_PATH = unique(pathEntries()).join(":")
export const NODE_BIN = firstExisting([
process.env.NODE_BIN,
@@ -52,27 +96,38 @@ export const OPENCLAW_BIN = firstExisting([
"/usr/local/bin/openclaw",
], "openclaw")
export const NEMOCLAW_BIN = firstExisting([
export const NEMOCLAW_BIN_CANDIDATES = unique([
process.env.NEMOCLAW_BIN,
HOME ? path.join(HOME, ".local/bin/nemoclaw") : undefined,
HOME ? path.join(HOME, ".nemoclaw/source/bin/nemoclaw.js") : undefined,
HOME ? path.join(HOME, "NemoClaw/bin/nemoclaw.js") : undefined,
HOME ? path.join(HOME, "nemoclaw/bin/nemoclaw.js") : undefined,
HOME ? path.join(HOME, "nemoclaw.js") : undefined,
...discoverHomeFiles("bin/nemoclaw.js"),
HOME ? path.join(HOME, ".nvm/versions/node/v22.22.2/bin/nemoclaw") : undefined,
"/opt/homebrew/bin/nemoclaw",
"/usr/local/bin/nemoclaw",
], "nemoclaw")
])
export const NEMOCLAW_BIN = firstExisting(NEMOCLAW_BIN_CANDIDATES, "nemoclaw")
export const NEMOCLAW_SETUP = firstExisting([
export const NEMOCLAW_SETUP_CANDIDATES = unique([
process.env.NEMOCLAW_SETUP,
HOME ? path.join(HOME, ".nemoclaw/source/scripts/setup.sh") : undefined,
HOME ? path.join(HOME, "NemoClaw/scripts/setup.sh") : undefined,
], "")
HOME ? path.join(HOME, "nemoclaw/scripts/setup.sh") : undefined,
...discoverHomeFiles("scripts/setup.sh"),
])
export const NEMOCLAW_SETUP = firstExisting(NEMOCLAW_SETUP_CANDIDATES, "")
export const NEMOCLAW_CWD = firstExisting([
export const NEMOCLAW_CWD_CANDIDATES = unique([
process.env.NEMOCLAW_CWD,
NEMOCLAW_SETUP ? path.dirname(path.dirname(NEMOCLAW_SETUP)) : undefined,
NEMOCLAW_BIN.includes("/") ? path.dirname(path.dirname(NEMOCLAW_BIN)) : undefined,
HOME ? path.join(HOME, ".nemoclaw/source") : undefined,
HOME ? path.join(HOME, "NemoClaw") : undefined,
], process.cwd())
HOME ? path.join(HOME, "nemoclaw") : undefined,
])
export const NEMOCLAW_CWD = firstExisting(NEMOCLAW_CWD_CANDIDATES, process.cwd())
export function commandExists(command: string) {
return command.includes("/") ? existsSync(command) : true
+43 -1
View File
@@ -140,11 +140,29 @@ find_nemoclaw_bin() {
"$HOME/.local/bin/nemoclaw" \
"$HOME/.nemoclaw/source/bin/nemoclaw.js" \
"$HOME/NemoClaw/bin/nemoclaw.js" \
"$HOME/nemoclaw/bin/nemoclaw.js" \
"/opt/homebrew/bin/nemoclaw" \
"/usr/local/bin/nemoclaw"
do
[[ -x "$candidate" || -f "$candidate" ]] && printf '%s\n' "$candidate" && return
done
local root nested
for root in "$HOME"/* "$HOME"/.*; do
[[ -d "$root" ]] || continue
case "$(basename "$root")" in
.|..|.cache|.npm|node_modules) continue ;;
esac
candidate="$root/bin/nemoclaw.js"
[[ -x "$candidate" || -f "$candidate" ]] && printf '%s\n' "$candidate" && return
for nested in "$root"/*; do
[[ -d "$nested" ]] || continue
case "$(basename "$nested")" in
.git|.next|build|dist|node_modules) continue ;;
esac
candidate="$nested/bin/nemoclaw.js"
[[ -x "$candidate" || -f "$candidate" ]] && printf '%s\n' "$candidate" && return
done
done
command -v nemoclaw 2>/dev/null || true
}
@@ -152,10 +170,28 @@ find_nemoclaw_setup() {
local candidate
for candidate in \
"$HOME/.nemoclaw/source/scripts/setup.sh" \
"$HOME/NemoClaw/scripts/setup.sh"
"$HOME/NemoClaw/scripts/setup.sh" \
"$HOME/nemoclaw/scripts/setup.sh"
do
[[ -f "$candidate" ]] && printf '%s\n' "$candidate" && return
done
local root nested
for root in "$HOME"/* "$HOME"/.*; do
[[ -d "$root" ]] || continue
case "$(basename "$root")" in
.|..|.cache|.npm|node_modules) continue ;;
esac
candidate="$root/scripts/setup.sh"
[[ -f "$candidate" ]] && printf '%s\n' "$candidate" && return
for nested in "$root"/*; do
[[ -d "$nested" ]] || continue
case "$(basename "$nested")" in
.git|.next|build|dist|node_modules) continue ;;
esac
candidate="$nested/scripts/setup.sh"
[[ -f "$candidate" ]] && printf '%s\n' "$candidate" && return
done
done
}
nemoclaw_cwd_for() {
@@ -164,10 +200,16 @@ nemoclaw_cwd_for() {
printf '%s\n' "$HOME/.nemoclaw/source"
elif [[ "$path_value" == "$HOME/NemoClaw/"* ]]; then
printf '%s\n' "$HOME/NemoClaw"
elif [[ "$path_value" == "$HOME/nemoclaw/"* ]]; then
printf '%s\n' "$HOME/nemoclaw"
elif [[ -d "$HOME/.nemoclaw/source" ]]; then
printf '%s\n' "$HOME/.nemoclaw/source"
elif [[ -d "$HOME/NemoClaw" ]]; then
printf '%s\n' "$HOME/NemoClaw"
elif [[ -d "$HOME/nemoclaw" ]]; then
printf '%s\n' "$HOME/nemoclaw"
elif [[ -n "$path_value" ]]; then
dirname "$(dirname "$path_value")"
fi
}
+7
View File
@@ -30,6 +30,9 @@ const [
assert.match(installSource, /find_nemoclaw_bin\(\)/, 'installer must discover NemoClaw CLI')
assert.match(installSource, /find_nemoclaw_setup\(\)/, 'installer must discover NemoClaw setup workflow')
assert.match(installSource, /\$HOME\/nemoclaw\/scripts\/setup\.sh/, 'installer must check common lowercase ~/nemoclaw setup path')
assert.match(installSource, /candidate="\$root\/scripts\/setup\.sh"/, 'installer must scan user home project directories for setup.sh')
assert.match(installSource, /candidate="\$nested\/scripts\/setup\.sh"/, 'installer must scan one nested user-home level for setup.sh')
assert.match(installSource, /set_env "NEMOCLAW_SETUP"/, 'installer must persist discovered NemoClaw setup path')
assert.match(installSource, /set_env "NEMOCLAW_BIN"/, 'installer must persist discovered NemoClaw CLI path')
assert.match(installSource, /set_env "OPENSHELL_BIN"/, 'installer must persist discovered OpenShell path')
@@ -38,10 +41,14 @@ assert.match(installSource, /upsert_env "TERMINAL_SERVER_AUTOSTART" "true"/, 'in
assert.match(hostCommandsSource, /export const HOST_PATH/, 'host command resolution must centralize PATH construction')
assert.match(hostCommandsSource, /\.nemoclaw\/source\/bin\/nemoclaw\.js/, 'host command resolution must support standard ~/.nemoclaw installs')
assert.match(hostCommandsSource, /\.nemoclaw\/source\/scripts\/setup\.sh/, 'host command resolution must support standard ~/.nemoclaw setup workflow')
assert.match(hostCommandsSource, /discoverHomeFiles\("scripts\/setup\.sh"\)/, 'runtime command resolution must scan user home directories for setup.sh')
assert.match(hostCommandsSource, /discoverHomeFiles\("bin\/nemoclaw\.js"\)/, 'runtime command resolution must scan user home directories for nemoclaw.js')
assert.match(hostCommandsSource, /export const NEMOCLAW_SETUP_CANDIDATES/, 'runtime command resolution must expose searched setup candidates')
assert.doesNotMatch(hostCommandsSource, /\/Users\/markmckeen|\/home\/nvidia/, 'host command resolution must not bake in developer machine paths')
assert.match(createRouteSource, /requireNemoClawSetup\(\)/, 'NemoClaw blueprint create must validate setup workflow before running bash')
assert.match(createRouteSource, /NemoClaw blueprint setup script was not found/, 'missing setup workflow must produce an actionable error')
assert.match(createRouteSource, /Searched: \$\{NEMOCLAW_SETUP_CANDIDATES\.join/, 'missing setup workflow error must list searched candidates')
assert.match(createRouteSource, /from "@\/app\/lib\/hostCommands"/, 'create route must use shared command resolution')
assert.match(deleteRouteSource, /NEMOCLAW_CWD/, 'delete route must use resolved NemoClaw working directory')