fix: show deployed sandbox agent identity (#44)
Co-authored-by: mmckeen-nv <mmckeen-nv@users.noreply.github.com>
This commit is contained in:
@@ -5,6 +5,13 @@ import { hostname, networkInterfaces } from "node:os"
|
||||
import path from "node:path"
|
||||
import { promisify } from "node:util"
|
||||
import { NEMOCLAW_BIN, NODE_BIN, OPENSHELL_BIN, hostCommandEnv } from "@/app/lib/hostCommands"
|
||||
import {
|
||||
parseDefaultSandboxNames,
|
||||
parseNemoclawListJson,
|
||||
resolveSandboxAgent,
|
||||
type NemoClawListJsonIdentity,
|
||||
type NemoClawRegistryData,
|
||||
} from "@/app/lib/nemoclawIdentity.mjs"
|
||||
import { resolveRuntimeAuthority } from "@/app/lib/runtimeAuthority"
|
||||
|
||||
const execFileAsync = promisify(execFile)
|
||||
@@ -30,10 +37,6 @@ type NemoClawSummary = {
|
||||
source: "nemoclaw-cli" | "none"
|
||||
}
|
||||
|
||||
type NemoClawRegistryData = {
|
||||
sandboxes?: Record<string, { name?: string; agent?: string | null }>
|
||||
}
|
||||
|
||||
type SandboxItem = {
|
||||
metadata: {
|
||||
name: string
|
||||
@@ -115,17 +118,6 @@ function parseOpenShellSandboxNames(output: string) {
|
||||
.filter((entry): entry is string => Boolean(entry))
|
||||
}
|
||||
|
||||
function parseDefaultSandboxNames(output: string) {
|
||||
return new Set(
|
||||
output
|
||||
.split(/\r?\n/)
|
||||
.map((entry) => stripAnsi(entry))
|
||||
.filter((entry) => /^\s{2,}[\w.-]+(?:\s+\*)?\s*$/.test(entry) && entry.includes("*"))
|
||||
.map((entry) => entry.replace("*", "").trim())
|
||||
.filter(Boolean)
|
||||
)
|
||||
}
|
||||
|
||||
function buildNemoClawSummary(output: string | null, defaultSandboxNames: Set<string>): NemoClawSummary {
|
||||
if (!output) {
|
||||
return {
|
||||
@@ -163,14 +155,6 @@ function readNemoClawRegistry(): NemoClawRegistryData {
|
||||
}
|
||||
}
|
||||
|
||||
function resolveSandboxAgent(name: string, id: string | null, registry: NemoClawRegistryData) {
|
||||
const entries = registry.sandboxes ?? {}
|
||||
const directEntry = entries[name] || (id ? entries[id] : undefined)
|
||||
const namedEntry = Object.values(entries).find((entry) => entry?.name === name || Boolean(id && entry?.name === id))
|
||||
const agent = directEntry?.agent || namedEntry?.agent
|
||||
return typeof agent === "string" && agent.trim() ? agent.trim() : "openclaw"
|
||||
}
|
||||
|
||||
async function execNemoclaw(args: string[]) {
|
||||
const env = hostCommandEnv({
|
||||
OPENSHELL_GATEWAY: process.env.OPENSHELL_GATEWAY || "nemoclaw",
|
||||
@@ -215,7 +199,12 @@ function readHostIdentity() {
|
||||
return { hostname: hostname(), address: "127.0.0.1", interface: "lo0" }
|
||||
}
|
||||
|
||||
async function readSandbox(name: string, defaultSandboxNames: Set<string>, registry: NemoClawRegistryData): Promise<{ summary: SandboxSummary; pod: SandboxItem }> {
|
||||
async function readSandbox(
|
||||
name: string,
|
||||
defaultSandboxNames: Set<string>,
|
||||
listJsonIdentity: NemoClawListJsonIdentity | null,
|
||||
registry: NemoClawRegistryData | null
|
||||
): Promise<{ summary: SandboxSummary; pod: SandboxItem }> {
|
||||
try {
|
||||
const [{ stdout: detailsStdout }, { stdout: sshStdout }] = await Promise.all([
|
||||
execOpenShell(["sandbox", "get", name]),
|
||||
@@ -229,7 +218,7 @@ async function readSandbox(name: string, defaultSandboxNames: Set<string>, regis
|
||||
const sshConfig = sshStdout.trim()
|
||||
const sshHostAlias = parseSshHostAlias(sshConfig, sandboxName)
|
||||
const isDefault = defaultSandboxNames.has(sandboxName)
|
||||
const agent = resolveSandboxAgent(sandboxName, sandboxId, registry)
|
||||
const agent = resolveSandboxAgent(sandboxName, sandboxId, listJsonIdentity, registry)
|
||||
|
||||
return {
|
||||
summary: {
|
||||
@@ -274,7 +263,7 @@ async function readSandbox(name: string, defaultSandboxNames: Set<string>, regis
|
||||
} catch (error) {
|
||||
const sandboxName = name
|
||||
const isDefault = defaultSandboxNames.has(sandboxName)
|
||||
const agent = resolveSandboxAgent(sandboxName, sandboxName, registry)
|
||||
const agent = resolveSandboxAgent(sandboxName, sandboxName, listJsonIdentity, registry)
|
||||
return {
|
||||
summary: {
|
||||
id: sandboxName,
|
||||
@@ -322,15 +311,28 @@ export async function GET() {
|
||||
try {
|
||||
const { stdout: sandboxListStdout } = await execOpenShell(["sandbox", "list"])
|
||||
const names = parseOpenShellSandboxNames(sandboxListStdout)
|
||||
const [nemoclawListResult, nemoclawStatusResult] = names.length > 0
|
||||
? await Promise.all([
|
||||
execNemoclaw(["list"]).catch(() => null),
|
||||
execNemoclaw(["status"]).catch(() => null),
|
||||
])
|
||||
: [null, null]
|
||||
const defaultSandboxNames = parseDefaultSandboxNames(nemoclawListResult?.stdout ?? "")
|
||||
const registry = readNemoClawRegistry()
|
||||
const results = await Promise.all(names.map((name) => readSandbox(name, defaultSandboxNames, registry)))
|
||||
let listJsonIdentity: NemoClawListJsonIdentity | null = null
|
||||
let legacyListResult: { stdout: string; stderr: string } | null = null
|
||||
const nemoclawStatusPromise = names.length > 0
|
||||
? execNemoclaw(["status"]).catch(() => null)
|
||||
: Promise.resolve(null)
|
||||
|
||||
if (names.length > 0) {
|
||||
const nemoclawListJsonResult = await execNemoclaw(["list", "--json"]).catch(() => null)
|
||||
listJsonIdentity = parseNemoclawListJson(nemoclawListJsonResult?.stdout ?? "")
|
||||
if (!listJsonIdentity) {
|
||||
legacyListResult = await execNemoclaw(["list"]).catch(() => null)
|
||||
}
|
||||
}
|
||||
|
||||
const nemoclawStatusResult = await nemoclawStatusPromise
|
||||
const defaultSandboxNames = listJsonIdentity
|
||||
? new Set(listJsonIdentity.defaultSandboxNames)
|
||||
: parseDefaultSandboxNames(legacyListResult?.stdout ?? "")
|
||||
const registry = listJsonIdentity ? null : readNemoClawRegistry()
|
||||
const results = await Promise.all(
|
||||
names.map((name) => readSandbox(name, defaultSandboxNames, listJsonIdentity, registry))
|
||||
)
|
||||
const sandboxes = results.map((result) => result.summary)
|
||||
const items = results.map((result) => result.pod)
|
||||
const nemoclaw = buildNemoClawSummary(nemoclawStatusResult?.stdout ?? null, defaultSandboxNames)
|
||||
@@ -363,7 +365,11 @@ export async function GET() {
|
||||
explicitInstanceOverride: authority.explicitInstanceOverride,
|
||||
usedMappedSandboxInstance: authority.usedMappedSandboxInstance,
|
||||
})),
|
||||
defaultSource: defaultSandboxNames.size > 0 ? "nemoclaw-cli" : "none",
|
||||
defaultSource: listJsonIdentity
|
||||
? "nemoclaw-list-json"
|
||||
: defaultSandboxNames.size > 0
|
||||
? "nemoclaw-cli"
|
||||
: "none",
|
||||
count: inventoryCount,
|
||||
message: hasMappedFallbackWithoutInventory
|
||||
? "Fetched live OpenShell inventory: zero sandboxes reported, so any mapped NemoClaw dashboard should be treated as fallback-only."
|
||||
|
||||
@@ -78,14 +78,19 @@ function openDashboardUrl(url: string, openInNewTab: boolean) {
|
||||
}
|
||||
|
||||
function displaySandboxAgent(agent?: string) {
|
||||
if (agent === 'hermes') return 'Hermes'
|
||||
return 'OpenClaw'
|
||||
const normalized = typeof agent === 'string' ? agent.trim().toLowerCase() : ''
|
||||
if (normalized === 'hermes') return 'Hermes'
|
||||
if (!normalized || normalized === 'openclaw') return 'OpenClaw'
|
||||
if (normalized === 'unknown') return 'Unknown'
|
||||
return agent || 'Unknown'
|
||||
}
|
||||
|
||||
function SandboxTypeLogo({ agent }: { agent?: string }) {
|
||||
const isHermes = agent === 'hermes'
|
||||
const label = isHermes ? 'Hermes sandbox' : 'OpenClaw sandbox'
|
||||
const logoSrc = isHermes ? HERMES_SANDBOX_LOGO : OPENCLAW_SANDBOX_LOGO
|
||||
const normalized = typeof agent === 'string' ? agent.trim().toLowerCase() : ''
|
||||
const isHermes = normalized === 'hermes'
|
||||
const isOpenClaw = !normalized || normalized === 'openclaw'
|
||||
const label = isHermes ? 'Hermes sandbox' : isOpenClaw ? 'OpenClaw sandbox' : 'Non-OpenClaw sandbox'
|
||||
const logoSrc = isHermes ? HERMES_SANDBOX_LOGO : isOpenClaw ? OPENCLAW_SANDBOX_LOGO : null
|
||||
return (
|
||||
<span
|
||||
aria-label={label}
|
||||
@@ -93,10 +98,16 @@ function SandboxTypeLogo({ agent }: { agent?: string }) {
|
||||
className={`flex h-8 w-8 shrink-0 items-center justify-center rounded-md border shadow-inner ${
|
||||
isHermes
|
||||
? 'border-sky-300/60 bg-sky-500/15'
|
||||
: 'border-rose-300/60 bg-rose-500/15'
|
||||
: isOpenClaw
|
||||
? 'border-rose-300/60 bg-rose-500/15'
|
||||
: 'border-slate-300/60 bg-slate-500/15 text-[10px] font-semibold uppercase tracking-wide text-[var(--foreground-dim)]'
|
||||
}`}
|
||||
>
|
||||
<span aria-hidden="true" className="h-6 w-6 bg-contain bg-center bg-no-repeat" style={{ backgroundImage: `url(${logoSrc})` }} />
|
||||
{logoSrc ? (
|
||||
<span aria-hidden="true" className="h-6 w-6 bg-contain bg-center bg-no-repeat" style={{ backgroundImage: `url(${logoSrc})` }} />
|
||||
) : (
|
||||
<span aria-hidden="true">?</span>
|
||||
)}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
export type NemoClawRegistryData = {
|
||||
sandboxes?: Record<string, { name?: string; agent?: string | null }>
|
||||
}
|
||||
|
||||
export type NemoClawListJsonIdentity = {
|
||||
agentsByName: Record<string, string>
|
||||
defaultSandboxNames: string[]
|
||||
}
|
||||
|
||||
export function normalizeAgentIdentifier(agent: unknown): string | null
|
||||
export function parseNemoclawListJson(output: string): NemoClawListJsonIdentity | null
|
||||
export function parseDefaultSandboxNames(output: string): Set<string>
|
||||
export function resolveSandboxAgent(
|
||||
name: string,
|
||||
id: string | null,
|
||||
listJsonIdentity: NemoClawListJsonIdentity | null,
|
||||
registry: NemoClawRegistryData | null
|
||||
): string
|
||||
@@ -0,0 +1,80 @@
|
||||
function canonicalizeAgent(value) {
|
||||
return value
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, '-')
|
||||
.replace(/^-+|-+$/g, '')
|
||||
}
|
||||
|
||||
export function normalizeAgentIdentifier(agent) {
|
||||
if (typeof agent !== 'string') return null
|
||||
const trimmed = agent.trim()
|
||||
if (!trimmed) return null
|
||||
|
||||
const compact = trimmed.toLowerCase().replace(/[^a-z0-9]/g, '')
|
||||
if (compact === 'openclaw') return 'openclaw'
|
||||
if (compact === 'nemohermes' || compact === 'hermes') return 'hermes'
|
||||
if (compact.includes('openclaw')) return 'openclaw'
|
||||
if (compact.includes('hermes')) return 'hermes'
|
||||
|
||||
const canonical = canonicalizeAgent(trimmed)
|
||||
if (!canonical) return null
|
||||
if (canonical === 'unknown') return 'unknown'
|
||||
return canonical
|
||||
}
|
||||
|
||||
export function parseNemoclawListJson(output) {
|
||||
if (typeof output !== 'string' || output.trim().length === 0) return null
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(output)
|
||||
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return null
|
||||
if (parsed.schemaVersion !== 1) return null
|
||||
if (!Array.isArray(parsed.sandboxes)) return null
|
||||
|
||||
const agentsByName = {}
|
||||
const defaultSandboxNames = []
|
||||
|
||||
for (const entry of parsed.sandboxes) {
|
||||
if (!entry || typeof entry !== 'object' || Array.isArray(entry)) continue
|
||||
const name = typeof entry.name === 'string' ? entry.name.trim() : ''
|
||||
if (!name) continue
|
||||
|
||||
const normalized = normalizeAgentIdentifier(entry.agent)
|
||||
agentsByName[name] = normalized || 'unknown'
|
||||
if (entry.isDefault === true) defaultSandboxNames.push(name)
|
||||
}
|
||||
|
||||
return {
|
||||
agentsByName,
|
||||
defaultSandboxNames,
|
||||
}
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export function parseDefaultSandboxNames(output) {
|
||||
return new Set(
|
||||
output
|
||||
.split(/\r?\n/)
|
||||
.map((entry) => entry.replace(/\u001b\[[0-9;]*m/g, ''))
|
||||
.filter((entry) => /^\s{2,}[\w.-]+(?:\s+\*)?\s*$/.test(entry) && entry.includes('*'))
|
||||
.map((entry) => entry.replace('*', '').trim())
|
||||
.filter(Boolean)
|
||||
)
|
||||
}
|
||||
|
||||
export function resolveSandboxAgent(name, id, listJsonIdentity, registry) {
|
||||
if (listJsonIdentity) {
|
||||
const direct = listJsonIdentity.agentsByName[name] || (id ? listJsonIdentity.agentsByName[id] : null)
|
||||
return direct || 'unknown'
|
||||
}
|
||||
|
||||
const entries = registry?.sandboxes || {}
|
||||
const directEntry = entries[name] || (id ? entries[id] : undefined)
|
||||
const namedEntry = Object.values(entries).find((entry) => entry?.name === name || Boolean(id && entry?.name === id))
|
||||
const normalized = normalizeAgentIdentifier(directEntry?.agent ?? namedEntry?.agent)
|
||||
|
||||
if (normalized) return normalized
|
||||
return 'openclaw'
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import {
|
||||
normalizeAgentIdentifier,
|
||||
parseNemoclawListJson,
|
||||
resolveSandboxAgent,
|
||||
} from '../app/lib/nemoclawIdentity.mjs'
|
||||
|
||||
const jsonInventory = JSON.stringify({
|
||||
schemaVersion: 1,
|
||||
sandboxes: [
|
||||
{ name: 'my-hermes', agent: 'Hermes', isDefault: true },
|
||||
{ name: 'gateway-recovered', agent: 'unknown', isDefault: false },
|
||||
{ name: 'custom-agent', agent: 'LangChain DeepAgents Code', isDefault: false },
|
||||
],
|
||||
})
|
||||
|
||||
const parsed = parseNemoclawListJson(jsonInventory)
|
||||
assert.ok(parsed, 'schemaVersion=1 NemoClaw JSON inventory should parse')
|
||||
assert.deepEqual(parsed.defaultSandboxNames, ['my-hermes'])
|
||||
assert.equal(parsed.agentsByName['my-hermes'], 'hermes')
|
||||
assert.equal(parsed.agentsByName['gateway-recovered'], 'unknown')
|
||||
assert.equal(parsed.agentsByName['custom-agent'], 'langchain-deepagents-code')
|
||||
|
||||
assert.equal(resolveSandboxAgent('my-hermes', null, parsed, null), 'hermes')
|
||||
assert.equal(resolveSandboxAgent('gateway-recovered', null, parsed, null), 'unknown')
|
||||
assert.equal(resolveSandboxAgent('missing-from-json', null, parsed, null), 'unknown')
|
||||
|
||||
assert.equal(normalizeAgentIdentifier('OpenClaw'), 'openclaw')
|
||||
assert.equal(normalizeAgentIdentifier('nemohermes'), 'hermes')
|
||||
assert.equal(normalizeAgentIdentifier('unknown'), 'unknown')
|
||||
|
||||
const legacyRegistry = {
|
||||
sandboxes: {
|
||||
'legacy-null-agent': { name: 'legacy-null-agent', agent: null },
|
||||
'explicit-hermes': { name: 'explicit-hermes', agent: 'Hermes' },
|
||||
'explicit-unknown': { name: 'explicit-unknown', agent: 'unknown' },
|
||||
},
|
||||
}
|
||||
|
||||
assert.equal(resolveSandboxAgent('legacy-null-agent', null, null, legacyRegistry), 'openclaw')
|
||||
assert.equal(resolveSandboxAgent('explicit-hermes', null, null, legacyRegistry), 'hermes')
|
||||
assert.equal(resolveSandboxAgent('explicit-unknown', null, null, legacyRegistry), 'unknown')
|
||||
|
||||
assert.equal(parseNemoclawListJson('{'), null, 'invalid JSON must fail closed')
|
||||
assert.equal(parseNemoclawListJson(JSON.stringify({ schemaVersion: 2, sandboxes: [] })), null, 'unsupported schema versions must fail closed')
|
||||
|
||||
console.log('nemoclaw-identity-resolution-check: PASS json identity resolution assertions')
|
||||
@@ -190,7 +190,7 @@ assert.doesNotMatch(deleteRouteSource, /Pruned NemoClaw registry entry without d
|
||||
assert.match(deleteRouteSource, /openShellAlreadyGone/, 'delete route must tolerate sandboxes that OpenShell already deleted while NemoClaw still has registry state')
|
||||
assert.doesNotMatch(deleteRouteSource, /isGatewayRecoverable|recoverableGateway/, 'delete route must not include the later gateway-repair branching')
|
||||
assert.match(telemetryRouteSource, /const names = parseOpenShellSandboxNames\(sandboxListStdout\)/, 'telemetry must derive inventory from OpenShell before consulting NemoClaw')
|
||||
assert.match(telemetryRouteSource, /names\.length > 0[\s\S]*execNemoclaw\(\["list"\]\)[\s\S]*execNemoclaw\(\["status"\]\)/, 'telemetry must skip NemoClaw list/status when OpenShell reports zero live sandboxes')
|
||||
assert.match(telemetryRouteSource, /names\.length > 0[\s\S]*execNemoclaw\(\["status"\]\)[\s\S]*execNemoclaw\(\["list", "--json"\]\)/, 'telemetry must skip NemoClaw list/status when OpenShell reports zero live sandboxes and prefer nemoclaw list --json when sandboxes exist')
|
||||
assert.match(telemetryRouteSource, /readNemoClawRegistry/, 'telemetry must read the NemoClaw registry for agent metadata')
|
||||
assert.match(telemetryRouteSource, /resolveSandboxAgent/, 'telemetry must resolve OpenClaw versus Hermes agent metadata')
|
||||
assert.match(telemetryRouteSource, /"nemoclaw\.ai\/agent": agent/, 'telemetry pod labels must include the selected NemoClaw agent')
|
||||
@@ -200,7 +200,7 @@ assert.match(sandboxListSource, /function displaySandboxAgent\(agent\?: string\)
|
||||
assert.match(sandboxListSource, /function SandboxTypeLogo\(\{ agent \}/, 'sandbox list must render per-agent sandbox logos')
|
||||
assert.match(sandboxListSource, /const OPENCLAW_SANDBOX_LOGO = '\/sandbox-logos\/openclaw\.svg'/, 'OpenClaw sandboxes must use the local OpenClaw favicon asset')
|
||||
assert.match(sandboxListSource, /const HERMES_SANDBOX_LOGO = '\/sandbox-logos\/hermes\.png'/, 'Hermes sandboxes must use the local Hermes favicon asset')
|
||||
assert.match(sandboxListSource, /const logoSrc = isHermes \? HERMES_SANDBOX_LOGO : OPENCLAW_SANDBOX_LOGO/, 'sandbox type logo must switch assets by agent type')
|
||||
assert.match(sandboxListSource, /const logoSrc = isHermes \? HERMES_SANDBOX_LOGO : isOpenClaw \? OPENCLAW_SANDBOX_LOGO : null/, 'sandbox type logo must avoid misclassifying unknown agents as OpenClaw')
|
||||
assert.ok(openClawLogoStat.size > 0, 'OpenClaw favicon asset must be vendored locally')
|
||||
assert.ok(hermesLogoStat.size > 0, 'Hermes favicon asset must be vendored locally')
|
||||
assert.match(sandboxListSource, /selectedSandbox\?\.agent === 'hermes'/, 'sandbox operations must detect Hermes sandboxes')
|
||||
|
||||
Reference in New Issue
Block a user