Stabilize dashboard runtime proxy

This commit is contained in:
Codex
2026-04-24 13:47:10 +00:00
parent 4d15ce4388
commit 05db76d930
39 changed files with 4349 additions and 1472 deletions
+121 -102
View File
@@ -1,131 +1,150 @@
import { NextResponse } from 'next/server'
import { dockerExecInOpenShell, OPENSHELL_NAMESPACE, probeSandboxShell, validateSandboxId } from '../../../../lib/openshellHost'
import { inspectSandbox, isOpenShellTransportError } from '@/app/lib/openshellHost'
import { resolveRuntimeAuthority } from '@/app/lib/runtimeAuthority'
interface PodSummary {
containers: string[]
images: string[]
runningStatuses: string[]
phase?: string
readyCondition?: string
function normalizeSandboxId(value: string | null) {
return (value || '').trim()
}
const EMPTY_POD_SUMMARY: PodSummary = {
containers: [],
images: [],
runningStatuses: [],
}
function buildAttach(alias: string, sandboxId: string) {
const loginShell = process.platform === 'darwin' ? 'zsh' : 'bash'
const extraPath = process.platform === 'darwin' ? '/opt/homebrew/bin' : `${process.env.HOME || '~'}/.local/bin`
async function inspectPod(sandboxId: string): Promise<PodSummary> {
const result = await dockerExecInOpenShell(
`kubectl -n ${OPENSHELL_NAMESPACE} get pod ${sandboxId} -o json`
)
if (result.exitCode !== 0) {
const failureText = `${result.stderr}\n${result.stdout}`.toLowerCase()
if (failureText.includes('(notfound)') || failureText.includes('not found')) {
return EMPTY_POD_SUMMARY
}
throw new Error(result.stderr.trim() || result.stdout.trim() || `kubectl get pod failed with exit code ${result.exitCode}`)
}
if (!result.stdout.trim()) {
throw new Error('kubectl get pod returned empty stdout')
}
const pod = JSON.parse(result.stdout)
return {
containers: Array.isArray(pod?.spec?.containers) ? pod.spec.containers.map((c: any) => c.name) : [],
images: Array.isArray(pod?.spec?.containers) ? pod.spec.containers.map((c: any) => c.image) : [],
runningStatuses: Array.isArray(pod?.status?.containerStatuses) ? pod.status.containerStatuses.map((c: any) => c.name) : [],
phase: pod?.status?.phase,
readyCondition: Array.isArray(pod?.status?.conditions)
? pod.status.conditions.find((c: any) => c.type === 'Ready')?.status
: undefined,
}
}
function buildAttachContract(sandboxId: string) {
const alias = `openshell-${sandboxId}`
return {
alias,
aliasCommand: `ssh ${alias}`,
fallbackCommand: `ssh -i ~/.ssh/id_ed25519_openclaw_labmac ${alias}`,
loginShellCommand: `env PATH=/opt/homebrew/bin:$PATH zsh -l -c 'ssh ${alias}'`,
shellHint: 'If a non-interactive shell misses Homebrew tooling, retry from a login shell or prepend PATH=/opt/homebrew/bin:$PATH before ssh.',
fallbackCommand: `ssh ${alias}`,
loginShellCommand: `env PATH=${extraPath}:$PATH ${loginShell} -l -c 'ssh ${alias}'`,
shellHint: process.platform === 'darwin'
? 'If your non-interactive shell misses Homebrew tooling, retry from a login shell or prepend PATH=/opt/homebrew/bin:$PATH before ssh.'
: 'If your non-interactive shell misses local tooling, retry from a login shell or prepend PATH=$HOME/.local/bin:$PATH before ssh.',
}
}
export async function GET(request: Request) {
const url = new URL(request.url)
let sandboxId = 'unknown'
try {
const { searchParams } = new URL(request.url)
const sandboxId = validateSandboxId(searchParams.get('sandboxId') || '')
sandboxId = normalizeSandboxId(url.searchParams.get('sandboxId'))
const attach = buildAttachContract(sandboxId)
const pod = await inspectPod(sandboxId)
let shellProbe: {
ok: boolean
output?: string
stderr?: string
error?: string
} = { ok: false }
const podExists = Boolean(pod.containers.length || pod.images.length || pod.phase)
if (podExists) {
try {
const probe = await probeSandboxShell(sandboxId)
shellProbe = {
if (!sandboxId) {
const authority = resolveRuntimeAuthority({ sandboxId: null })
return NextResponse.json({
ok: true,
sandboxId: 'host',
authority: {
requestedSandboxId: authority.requestedSandboxId,
resolvedSandboxId: authority.resolvedSandboxId,
sandboxAuthority: authority.sandboxAuthority,
requestedInstanceId: authority.requestedInstanceId,
instanceId: authority.openclaw.id,
sandboxInstanceId: authority.mappedSandboxInstanceId,
explicitInstanceOverride: authority.explicitInstanceOverride,
usedMappedSandboxInstance: authority.usedMappedSandboxInstance,
terminalFallback: true,
},
podExists: false,
podReady: true,
sshReachable: true,
degraded: true,
attach: null,
pod: {
phase: 'Host',
readyCondition: 'Host',
containers: [],
images: [],
runningStatuses: [],
},
shellProbe: {
ok: true,
output: probe.stdout.trim(),
stderr: probe.stderr.trim(),
}
} catch (error) {
shellProbe = {
ok: false,
error: error instanceof Error ? error.message : 'Shell probe failed',
}
}
} else {
shellProbe = {
ok: false,
error: `Sandbox pod ${sandboxId} was not found in ${OPENSHELL_NAMESPACE}.`,
}
output: 'Host terminal mode active. No sandbox selected.',
stderr: '',
},
note: 'Host operator terminal is ready. Sandbox context is optional.',
})
}
const podReady = pod.readyCondition === 'True' || pod.phase === 'Running'
const sshReachable = shellProbe.ok
const sandbox = await inspectSandbox(sandboxId)
const authority = resolveRuntimeAuthority({
sandboxId,
resolvedSandboxId: sandbox.name,
})
const podReady = sandbox.phase === 'Running'
const sshReachable = sandbox.phase === 'Running'
return NextResponse.json({
ok: true,
sandboxId,
podExists,
sandboxId: sandbox.name,
authority: {
requestedSandboxId: authority.requestedSandboxId,
resolvedSandboxId: authority.resolvedSandboxId,
sandboxAuthority: authority.sandboxAuthority,
requestedInstanceId: authority.requestedInstanceId,
instanceId: authority.openclaw.id,
sandboxInstanceId: authority.mappedSandboxInstanceId,
explicitInstanceOverride: authority.explicitInstanceOverride,
usedMappedSandboxInstance: authority.usedMappedSandboxInstance,
terminalFallback: false,
},
podExists: true,
podReady,
sshReachable,
degraded: podExists && !sshReachable,
attach,
pod,
shellProbe,
note: !podExists
? `Sandbox pod ${sandboxId} is not present in ${OPENSHELL_NAMESPACE}.`
: sshReachable
? 'Pod introspection and bounded shell probe both succeeded.'
: 'Pod introspection succeeded, but bounded shell probe did not. Treat this as degraded dashboard reachability until direct SSH is confirmed.',
degraded: !sshReachable,
attach: buildAttach(sandbox.sshHostAlias, sandbox.name),
pod: {
phase: sandbox.phase,
readyCondition: podReady ? 'True' : sandbox.rawPhase || 'Unknown',
containers: [],
images: [],
runningStatuses: [],
},
shellProbe: {
ok: sshReachable,
output: sandbox.rawDetails,
stderr: '',
},
note: sshReachable
? 'Sandbox inspection succeeded and the normalized phase is Running.'
: `Sandbox inspection succeeded, but authoritative phase is ${sandbox.rawPhase || 'Unknown'} (normalized ${sandbox.phase || 'Unknown'}).`,
})
} catch (error) {
const message = error instanceof Error ? error.message : 'Failed to build readiness contract'
const status = message === 'sandboxId is required'
|| message.includes('sandboxId must be a valid lowercase DNS-style identifier')
? 400
: 500
const message = error instanceof Error ? error.message : 'Failed to inspect terminal readiness.'
const transportError = isOpenShellTransportError(error)
const authority = resolveRuntimeAuthority({ sandboxId })
return NextResponse.json({
ok: false,
ok: true,
sandboxId: sandboxId || 'host',
authority: {
requestedSandboxId: authority.requestedSandboxId,
resolvedSandboxId: authority.resolvedSandboxId,
sandboxAuthority: authority.sandboxAuthority,
requestedInstanceId: authority.requestedInstanceId,
instanceId: authority.openclaw.id,
sandboxInstanceId: authority.mappedSandboxInstanceId,
explicitInstanceOverride: authority.explicitInstanceOverride,
usedMappedSandboxInstance: authority.usedMappedSandboxInstance,
terminalFallback: true,
},
podExists: false,
podReady: false,
sshReachable: false,
degraded: false,
podReady: true,
sshReachable: true,
degraded: true,
error: message,
}, { status })
attach: null,
pod: {
phase: 'Host',
readyCondition: 'Host',
containers: [],
images: [],
runningStatuses: [],
},
shellProbe: {
ok: true,
output: 'Host terminal mode active. Sandbox inspection failed, but host shell remains available.',
stderr: message,
},
note: transportError ? 'OpenShell transport is unavailable for sandbox inspection; host operator terminal remains available.' : 'Sandbox inspection failed; host operator terminal remains available.',
}, { status: 200 })
}
}