diff --git a/README.md b/README.md index 9f60aab..38783c7 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,7 @@ OpenShell must already be installed and able to reach its gateway. On this host The installer does not create an OpenShell gateway for you. Start or connect OpenShell first, then install this dashboard. -Controller commands follow OpenShell's active gateway by default. Set `OPENSHELL_GATEWAY` only for an intentional override; leaving it unset also allows current NemoClaw to restore a port-qualified gateway such as `nemoclaw-8990` when its installer selected an alternate gateway port. +Controller commands follow OpenShell's active gateway by default. Set `OPENSHELL_GATEWAY` only for an intentional override; leaving it unset also allows current NemoClaw to restore a port-qualified gateway such as `nemoclaw-8990` when its installer selected an alternate gateway port. Quick Deploy image discovery checks both the default NemoClaw registry and its numeric alternate-port registry roots, and fails closed if a sandbox name is registered under multiple gateways. ## Installer diff --git a/app/api/sandbox/create/route.ts b/app/api/sandbox/create/route.ts index 94501c9..2a51a9b 100644 --- a/app/api/sandbox/create/route.ts +++ b/app/api/sandbox/create/route.ts @@ -1,11 +1,15 @@ import { NextResponse } from "next/server" import { execFile, spawn } from "node:child_process" -import { existsSync, readFileSync } from "node:fs" +import { existsSync } from "node:fs" import path from "node:path" import { promisify } from "node:util" import { inspectSandbox, resolveSandboxRef } from "@/app/lib/openshellHost" import { recordActivity } from "@/app/lib/activityLog" import { repairOpenClawExecApprovalsFile } from "@/app/lib/sandboxPrivilegedFiles" +import { + findNemoClawRegistryEntry, + listNemoClawRegistryCandidates, +} from "@/app/lib/nemoclawRegistry.mjs" import { commandExists, HOST_PATH, @@ -20,7 +24,7 @@ import { } from "@/app/lib/hostCommands" const execFileAsync = promisify(execFile) -const NEMOCLAW_REGISTRY_FILE = path.join(process.env.HOME || "/tmp", ".nemoclaw", "sandboxes.json") +const NEMOCLAW_STATE_ROOT = path.join(process.env.HOME || "/tmp", ".nemoclaw") function validateSandboxName(name: string) { if (!name || typeof name !== "string") throw new Error("sandbox name is required") @@ -84,10 +88,6 @@ type CreateInferenceSettings = { envSummary: string[] } -type NemoClawRegistryData = { - sandboxes?: Record - defaultSandbox?: string | null -} function parseCreateGpuMode(body: any): CreateGpuMode { const raw = typeof body?.gpuMode === "string" @@ -270,16 +270,6 @@ function parseOpenShellSandboxNames(output: string) { .filter((entry): entry is string => Boolean(entry)) } -function readNemoClawRegistry(): NemoClawRegistryData { - try { - return existsSync(NEMOCLAW_REGISTRY_FILE) - ? JSON.parse(readFileSync(NEMOCLAW_REGISTRY_FILE, "utf8")) - : {} - } catch { - return {} - } -} - async function listOpenShellSandboxNames() { try { const { stdout } = await execFileAsync(OPENSHELL_BIN, ["sandbox", "list"], { @@ -299,7 +289,7 @@ async function resolveSourcePodImageFromRef(sourceSandboxRef: string) { const requested = sourceSandboxRef.trim() const source = await resolveSandboxRef(requested) const sourceName = validateSandboxName(source.name) - const registryEntry = readNemoClawRegistry().sandboxes?.[sourceName] + const registryEntry = findNemoClawRegistryEntry(sourceName)?.entry const sourceImage = registryEntry?.imageTag?.trim() || null if (!sourceImage) { @@ -319,15 +309,10 @@ async function resolveSourcePodImage(sourceSandboxRef: string | null | undefined return await resolveSourcePodImageFromRef(sourceSandboxRef) } - const registry = readNemoClawRegistry() - const registeredEntries = Object.entries(registry.sandboxes ?? {}) - const newestRegisteredNames = registeredEntries - .sort(([, a], [, b]) => String(b?.createdAt ?? "").localeCompare(String(a?.createdAt ?? ""))) - .map(([key, value]) => value?.name || key) + const registeredNames = listNemoClawRegistryCandidates() const liveNames = await listOpenShellSandboxNames() const candidates = Array.from(new Set([ - registry.defaultSandbox || undefined, - ...newestRegisteredNames, + ...registeredNames, ...liveNames, ].filter((candidate): candidate is string => Boolean(candidate && candidate !== targetSandboxName)))) @@ -346,7 +331,7 @@ function skippedNemoClawImageRedeployRegistration() { return { registered: false as const, skipped: true as const, - registryFile: NEMOCLAW_REGISTRY_FILE, + registryFile: NEMOCLAW_STATE_ROOT, note: "The quick-deployed sandbox remains an OpenShell-managed clone and was not written into NemoClaw's private registry. Current NemoClaw registry entries carry lifecycle identity, workload provenance, and agent-specific state that only NemoClaw can issue safely.", } } diff --git a/app/lib/nemoclawRegistry.mjs b/app/lib/nemoclawRegistry.mjs new file mode 100644 index 0000000..5523544 --- /dev/null +++ b/app/lib/nemoclawRegistry.mjs @@ -0,0 +1,70 @@ +import { existsSync, readdirSync, readFileSync } from 'node:fs' +import path from 'node:path' + +export function discoverNemoClawRegistryFiles(home = process.env.HOME || '/tmp') { + const stateRoot = path.join(home, '.nemoclaw') + const files = [path.join(stateRoot, 'sandboxes.json')] + const gatewaysRoot = path.join(stateRoot, 'gateways') + + if (existsSync(gatewaysRoot)) { + try { + const ports = readdirSync(gatewaysRoot, { withFileTypes: true }) + .filter((entry) => entry.isDirectory() && /^(?:[1-9][0-9]{0,4})$/.test(entry.name)) + .map((entry) => Number(entry.name)) + .filter((port) => port <= 65535) + .sort((a, b) => a - b) + for (const port of ports) files.push(path.join(gatewaysRoot, String(port), 'sandboxes.json')) + } catch { + // Individual readable registry files remain usable when directory discovery is unavailable. + } + } + + return files.filter((file) => existsSync(file)) +} + +export function readNemoClawRegistries(home = process.env.HOME || '/tmp') { + return discoverNemoClawRegistryFiles(home).flatMap((registryFile) => { + try { + const data = JSON.parse(readFileSync(registryFile, 'utf8')) + return data && typeof data === 'object' && !Array.isArray(data) + ? [{ registryFile, data }] + : [] + } catch { + return [] + } + }) +} + +export function findNemoClawRegistryEntry(sandboxName, home = process.env.HOME || '/tmp') { + const matches = [] + for (const registry of readNemoClawRegistries(home)) { + const entries = registry.data.sandboxes && typeof registry.data.sandboxes === 'object' + ? registry.data.sandboxes + : {} + const keyed = entries[sandboxName] + const entry = keyed || Object.values(entries).find((candidate) => candidate?.name === sandboxName) + if (entry) matches.push({ entry, registryFile: registry.registryFile }) + } + + if (matches.length > 1) { + throw new Error(`Sandbox '${sandboxName}' appears in multiple NemoClaw gateway registries; resolve the duplicate registration before quick deploy.`) + } + return matches[0] || null +} + +export function listNemoClawRegistryCandidates(home = process.env.HOME || '/tmp') { + const defaults = [] + const entries = [] + for (const { data } of readNemoClawRegistries(home)) { + if (typeof data.defaultSandbox === 'string' && data.defaultSandbox.trim()) { + defaults.push(data.defaultSandbox.trim()) + } + for (const [key, value] of Object.entries(data.sandboxes || {})) { + const name = typeof value?.name === 'string' && value.name.trim() ? value.name.trim() : key + entries.push({ name, createdAt: String(value?.createdAt || '') }) + } + } + + entries.sort((a, b) => b.createdAt.localeCompare(a.createdAt)) + return [...new Set([...defaults, ...entries.map(({ name }) => name)])] +} diff --git a/tests/nemoclaw-registry-roots-check.mjs b/tests/nemoclaw-registry-roots-check.mjs new file mode 100644 index 0000000..1f23c16 --- /dev/null +++ b/tests/nemoclaw-registry-roots-check.mjs @@ -0,0 +1,60 @@ +import assert from 'node:assert/strict' +import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises' +import os from 'node:os' +import path from 'node:path' + +import { + discoverNemoClawRegistryFiles, + findNemoClawRegistryEntry, + listNemoClawRegistryCandidates, +} from '../app/lib/nemoclawRegistry.mjs' + +const home = await mkdtemp(path.join(os.tmpdir(), 'nemoclaw-registry-roots-')) +try { + const root = path.join(home, '.nemoclaw') + await mkdir(path.join(root, 'gateways', '8990'), { recursive: true }) + await mkdir(path.join(root, 'gateways', 'not-a-port'), { recursive: true }) + await writeFile(path.join(root, 'sandboxes.json'), JSON.stringify({ + defaultSandbox: 'base-box', + sandboxes: { + 'base-box': { name: 'base-box', imageTag: 'base:image', createdAt: '2026-09-01' }, + }, + })) + await writeFile(path.join(root, 'gateways', '8990', 'sandboxes.json'), JSON.stringify({ + defaultSandbox: 'port-box', + sandboxes: { + 'port-box': { name: 'port-box', imageTag: 'port:image', createdAt: '2026-09-02' }, + }, + })) + await writeFile(path.join(root, 'gateways', 'not-a-port', 'sandboxes.json'), '{}') + + const files = discoverNemoClawRegistryFiles(home) + assert.deepEqual(files, [ + path.join(root, 'sandboxes.json'), + path.join(root, 'gateways', '8990', 'sandboxes.json'), + ], 'registry discovery must include the default root and numeric alternate-port roots only') + + assert.equal( + findNemoClawRegistryEntry('port-box', home)?.entry.imageTag, + 'port:image', + 'quick deploy must resolve image metadata from an alternate-port registry', + ) + assert.deepEqual( + listNemoClawRegistryCandidates(home), + ['base-box', 'port-box'], + 'defaults remain first and registered candidates are deduplicated across roots', + ) + + await writeFile(path.join(root, 'sandboxes.json'), JSON.stringify({ + sandboxes: { duplicate: { name: 'port-box', imageTag: 'wrong:image' } }, + })) + assert.throws( + () => findNemoClawRegistryEntry('port-box', home), + /multiple NemoClaw gateway registries/, + 'ambiguous cross-port registrations must fail closed', + ) + + console.log('nemoclaw-registry-roots-check: PASS cross-port registry resolution assertions') +} finally { + await rm(home, { recursive: true, force: true }) +} diff --git a/tests/sandbox-lifecycle-check.mjs b/tests/sandbox-lifecycle-check.mjs index 36234f6..5bfed0a 100644 --- a/tests/sandbox-lifecycle-check.mjs +++ b/tests/sandbox-lifecycle-check.mjs @@ -102,7 +102,7 @@ assert.match(createRouteSource, /label: "Fresh Hermes Sandbox"/, 'Hermes sandbox assert.match(createRouteSource, /source: "~\/NemoClaw\/agents\/hermes\/Dockerfile"/, 'Hermes sandbox creation must point at the upstream Hermes agent source') assert.match(createRouteSource, /id: "nemoclaw-deepagents-code"/, 'create API must expose Deep Agents Code sandbox creation') assert.match(createRouteSource, /source: "~\/NemoClaw\/agents\/langchain-deepagents-code\/manifest\.yaml"/, 'Deep Agents Code sandbox creation must point at the upstream agent manifest') -assert.match(createRouteSource, /readNemoClawRegistry/, 'image redeploy must discover the default NemoClaw sandbox image automatically') +assert.match(createRouteSource, /findNemoClawRegistryEntry/, 'image redeploy must discover NemoClaw sandbox images across default and alternate-port registries') assert.match(createRouteSource, /listOpenShellSandboxNames/, 'image redeploy must fall back to live OpenShell inventory when registry entries are stale') assert.match(createRouteSource, /resolveSourcePodImage\(sourceSandboxName, sandboxName\)/, 'image redeploy must allow optional explicit source while defaulting automatically') assert.match(createRouteSource, /"--from",\s*sourceImage/, 'image redeploy must pass the resolved image to openshell sandbox create')