fix: resolve NemoClaw registries across gateway ports (#48)

This commit is contained in:
mmckeen-nv
2026-09-12 23:09:27 -07:00
committed by GitHub
parent 6154088197
commit 85c6777c55
5 changed files with 142 additions and 27 deletions
+1 -1
View File
@@ -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. 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 ## Installer
+10 -25
View File
@@ -1,11 +1,15 @@
import { NextResponse } from "next/server" import { NextResponse } from "next/server"
import { execFile, spawn } from "node:child_process" import { execFile, spawn } from "node:child_process"
import { existsSync, readFileSync } from "node:fs" import { existsSync } from "node:fs"
import path from "node:path" import path from "node:path"
import { promisify } from "node:util" import { promisify } from "node:util"
import { inspectSandbox, resolveSandboxRef } from "@/app/lib/openshellHost" import { inspectSandbox, resolveSandboxRef } from "@/app/lib/openshellHost"
import { recordActivity } from "@/app/lib/activityLog" import { recordActivity } from "@/app/lib/activityLog"
import { repairOpenClawExecApprovalsFile } from "@/app/lib/sandboxPrivilegedFiles" import { repairOpenClawExecApprovalsFile } from "@/app/lib/sandboxPrivilegedFiles"
import {
findNemoClawRegistryEntry,
listNemoClawRegistryCandidates,
} from "@/app/lib/nemoclawRegistry.mjs"
import { import {
commandExists, commandExists,
HOST_PATH, HOST_PATH,
@@ -20,7 +24,7 @@ import {
} from "@/app/lib/hostCommands" } from "@/app/lib/hostCommands"
const execFileAsync = promisify(execFile) 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) { function validateSandboxName(name: string) {
if (!name || typeof name !== "string") throw new Error("sandbox name is required") if (!name || typeof name !== "string") throw new Error("sandbox name is required")
@@ -84,10 +88,6 @@ type CreateInferenceSettings = {
envSummary: string[] envSummary: string[]
} }
type NemoClawRegistryData = {
sandboxes?: Record<string, { name?: string; createdAt?: string; imageTag?: string }>
defaultSandbox?: string | null
}
function parseCreateGpuMode(body: any): CreateGpuMode { function parseCreateGpuMode(body: any): CreateGpuMode {
const raw = typeof body?.gpuMode === "string" const raw = typeof body?.gpuMode === "string"
@@ -270,16 +270,6 @@ function parseOpenShellSandboxNames(output: string) {
.filter((entry): entry is string => Boolean(entry)) .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() { async function listOpenShellSandboxNames() {
try { try {
const { stdout } = await execFileAsync(OPENSHELL_BIN, ["sandbox", "list"], { const { stdout } = await execFileAsync(OPENSHELL_BIN, ["sandbox", "list"], {
@@ -299,7 +289,7 @@ async function resolveSourcePodImageFromRef(sourceSandboxRef: string) {
const requested = sourceSandboxRef.trim() const requested = sourceSandboxRef.trim()
const source = await resolveSandboxRef(requested) const source = await resolveSandboxRef(requested)
const sourceName = validateSandboxName(source.name) const sourceName = validateSandboxName(source.name)
const registryEntry = readNemoClawRegistry().sandboxes?.[sourceName] const registryEntry = findNemoClawRegistryEntry(sourceName)?.entry
const sourceImage = registryEntry?.imageTag?.trim() || null const sourceImage = registryEntry?.imageTag?.trim() || null
if (!sourceImage) { if (!sourceImage) {
@@ -319,15 +309,10 @@ async function resolveSourcePodImage(sourceSandboxRef: string | null | undefined
return await resolveSourcePodImageFromRef(sourceSandboxRef) return await resolveSourcePodImageFromRef(sourceSandboxRef)
} }
const registry = readNemoClawRegistry() const registeredNames = listNemoClawRegistryCandidates()
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 liveNames = await listOpenShellSandboxNames() const liveNames = await listOpenShellSandboxNames()
const candidates = Array.from(new Set([ const candidates = Array.from(new Set([
registry.defaultSandbox || undefined, ...registeredNames,
...newestRegisteredNames,
...liveNames, ...liveNames,
].filter((candidate): candidate is string => Boolean(candidate && candidate !== targetSandboxName)))) ].filter((candidate): candidate is string => Boolean(candidate && candidate !== targetSandboxName))))
@@ -346,7 +331,7 @@ function skippedNemoClawImageRedeployRegistration() {
return { return {
registered: false as const, registered: false as const,
skipped: true 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.", 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.",
} }
} }
+70
View File
@@ -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)])]
}
+60
View File
@@ -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 })
}
+1 -1
View File
@@ -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, /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, /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, /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, /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, /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') assert.match(createRouteSource, /"--from",\s*sourceImage/, 'image redeploy must pass the resolved image to openshell sandbox create')