From 05db76d930dd95f42d34813a94b6532128015956 Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 24 Apr 2026 13:47:10 +0000 Subject: [PATCH] Stabilize dashboard runtime proxy --- .gitignore | 5 +- WEBSERVICE_BIBLE.md | 575 ++++++++++++++++++ app/api/openshell/dashboard/open/route.ts | 108 +++- .../dashboard/proxy/[...path]/route.ts | 9 + app/api/openshell/dashboard/proxy/route.ts | 32 +- app/api/openshell/dashboard/proxy/shared.ts | 200 ++++++ .../dashboard/proxy/[...path]/route.ts | 9 + .../[instanceId]/dashboard/proxy/route.ts | 9 + .../openshell/terminal/introspect/route.ts | 38 -- app/api/openshell/terminal/live/route.ts | 141 ++++- app/api/openshell/terminal/open/route.ts | 13 +- app/api/openshell/terminal/readiness/route.ts | 223 +++---- app/api/openshell/terminal/route.ts | 206 ------- app/api/sandbox/create/route.ts | 407 +++++++++++++ app/api/telemetry/real/route.ts | 470 +++++++++++--- app/components/CompactTelemetryDisplay.tsx | 45 +- app/components/ConfigurationPanel.tsx | 353 +++-------- app/components/SandboxDetails.tsx | 23 +- app/components/SandboxList.tsx | 266 ++++---- app/components/Sidebar.tsx | 125 ++-- app/components/TelemetryDisplay.tsx | 34 +- app/globals.css | 30 +- app/hooks/useSandboxInventory.ts | 151 +++++ app/layout.tsx | 8 +- app/lib/dashboardSession.ts | 89 +++ app/lib/openclawInstances.ts | 131 ++++ app/lib/openshellHost.ts | 525 ++++++++++++---- app/lib/runtimeAuthority.ts | 74 +++ app/operator-terminal/page.tsx | 362 +++-------- app/page.tsx | 117 +++- package.json | 5 +- public/favicon.ico | Bin 0 -> 67 bytes server.mjs | 418 +++++++++++++ terminal-server.mjs | 344 +++++++++-- tests/dashboard-session-check.mjs | 63 ++ tests/initializing-hang-check.mjs | 26 + ...aw-dashboard-instance-resolution-check.mjs | 81 +++ ...post-authority-terminal-contract-check.mjs | 48 ++ tests/runtime-authority-resolution-check.mjs | 58 ++ 39 files changed, 4349 insertions(+), 1472 deletions(-) create mode 100644 WEBSERVICE_BIBLE.md create mode 100644 app/api/openshell/dashboard/proxy/[...path]/route.ts create mode 100644 app/api/openshell/dashboard/proxy/shared.ts create mode 100644 app/api/openshell/instances/[instanceId]/dashboard/proxy/[...path]/route.ts create mode 100644 app/api/openshell/instances/[instanceId]/dashboard/proxy/route.ts delete mode 100644 app/api/openshell/terminal/introspect/route.ts delete mode 100644 app/api/openshell/terminal/route.ts create mode 100644 app/api/sandbox/create/route.ts create mode 100644 app/hooks/useSandboxInventory.ts create mode 100644 app/lib/dashboardSession.ts create mode 100644 app/lib/openclawInstances.ts create mode 100644 app/lib/runtimeAuthority.ts create mode 100644 public/favicon.ico create mode 100644 server.mjs create mode 100644 tests/dashboard-session-check.mjs create mode 100644 tests/initializing-hang-check.mjs create mode 100644 tests/openclaw-dashboard-instance-resolution-check.mjs create mode 100644 tests/post-authority-terminal-contract-check.mjs create mode 100644 tests/runtime-authority-resolution-check.mjs diff --git a/.gitignore b/.gitignore index d572f5f..37d36fa 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,9 @@ npm-debug.log* yarn-debug.log* yarn-error.log* +# local runtime state +/.runtime/ + # local env files .env*.local @@ -30,4 +33,4 @@ yarn-error.log* # typescript *.tsbuildinfo -next-env.d.ts \ No newline at end of file +next-env.d.ts diff --git a/WEBSERVICE_BIBLE.md b/WEBSERVICE_BIBLE.md new file mode 100644 index 0000000..7e7c2dd --- /dev/null +++ b/WEBSERVICE_BIBLE.md @@ -0,0 +1,575 @@ +# NemoClaw Web Service Coding BIBLE + +## Purpose + +This document is the operating doctrine for the NemoClaw web service. + +Its job is to prevent three failure classes that already caused drift: + +1. **authority drift** — multiple competing sources of truth +2. **ownership drift** — multiple processes/services thinking they own the same runtime surface +3. **runtime-truth confusion** — dashboards or probes looking green while describing the wrong process, wrong route owner, or wrong machine state + +This is not philosophy. These are build and run rules. + +--- + +## 0. Canonical names and lanes + +### 0.1 Canonical code lane +The canonical editable codebase is: + +- `/home/nvidia/.openclaw/workspace/projects/nemoclaw-dashboard` + +All feature work, bug fixes, route changes, and tests must start here. + +### 0.2 Canonical live runtime lane +The canonical live runtime tree is: + +- `/home/nvidia/nemoclaw-dashboard` + +This path is a deployment target, not the primary authoring lane. + +### 0.3 Runtime owner +The intended runtime owner is: + +- `nemoclaw-dashboard.service` + +Anything else listening on the production ports is drift unless explicitly documented as a temporary diagnostic with start/stop bounds. + +### 0.4 Intended listeners +The intended listeners are: + +- `:3000` → dashboard HTTP/WebSocket entrypoint +- `:3011` → loopback terminal server + +Per current runtime design: + +- `:3000` must be owned by the dashboard service path under `/home/nvidia/nemoclaw-dashboard` +- `:3011` must be owned by the terminal server started under dashboard supervisor control + +If these ports are owned by a workspace `next dev`, a random shell, or a second service, that is **not success**. It is drift. + +--- + +## 1. Source-of-truth doctrine + +## 1.1 One truth per concern +Every concern must have exactly one canonical authority. + +Use this mapping: + +- **Editable application code truth** → `projects/nemoclaw-dashboard` +- **Live executable runtime truth** → `/home/nvidia/nemoclaw-dashboard` + `nemoclaw-dashboard.service` +- **Port ownership truth** → PID/socket inspection, not browser success alone +- **Terminal session transport truth** → `terminal-server.mjs` on `127.0.0.1:3011` +- **Dashboard-to-terminal bridge truth** → `server.mjs` proxy path `/api/openshell/terminal/live/ws` +- **OpenClaw instance registry truth** → `app/lib/openclawInstances.ts` environment-driven registry +- **Dashboard browser session identity truth** → `app/lib/dashboardSession.ts` + +No feature may silently invent a second authority for the same concern. + +## 1.2 Browser-visible truth is not runtime truth +A page returning HTTP 200 from `:3000` is insufficient evidence. + +A claim like “dashboard is up” is valid only if all are true: + +- the expected port is listening +- the owning PID belongs to the intended runtime path/service +- the service state matches the port state +- the deployed code path matches the claimed release lane + +If one of those is missing, report degraded or unknown, not healthy. + +## 1.3 Environment defaults may bootstrap, not silently redefine truth +Defaults such as: + +- `OPENCLAW_DASHBOARD_URL` +- `TERMINAL_SERVER_URL` +- `OPENCLAW_INSTANCE_REGISTRY_JSON` +- `OPENCLAW_SANDBOX_INSTANCE_MAP_JSON` + +may provide bootstrap behavior, but they must not create silent semantic rewrites. + +If an env var changes authority, routing, loopback policy, or instance selection, the UI and logs must expose that fact. + +--- + +## 2. Route ownership doctrine + +## 2.1 Each route has one owner module +Each API route or transport path must have one clearly named owner. + +Current ownership model: + +- Next.js app routes under `app/api/**` own user-facing HTTP API semantics +- `server.mjs` owns the outer HTTP/WebSocket server and terminal bridge upgrade path +- `terminal-server.mjs` owns terminal session creation, buffering, resize/input handling, and process lifecycle +- `app/lib/*` owns shared authority/identity resolution logic, not ad hoc route-local copies + +Do not duplicate resolution logic inside multiple route handlers when a shared module already exists. + +## 2.2 No split-brain route semantics +If the same conceptual operation is available in multiple places, one path must be canonical and the others must either: + +- delegate directly to it, or +- be removed + +Example: + +- terminal live WebSocket entrypoint must be treated as owned by `server.mjs` proxy path `/api/openshell/terminal/live/ws` +- direct client assumptions that bypass this owner path are not allowed unless the architecture is intentionally changed and this BIBLE is updated + +## 2.3 UI state is not authority +UI-selected sandbox, browser sessionStorage, or client memory can express **intent**, but not authoritative backend fact. + +Specifically: + +- `dashboardSessionId` is correlation state, not backend truth +- `selectedSandboxId` is operator intent, not proof that the backend session is attached to the correct target + +Backends must validate and log the actual resolved identity. + +## 2.4 Shared resolution logic must live in library code +The following classes of logic must live in shared modules, not repeated inline: + +- instance resolution +- sandbox-to-instance mapping +- dashboard session identity construction +- host/runtime probing +- normalized phase/status naming + +If a contributor copies logic from `app/lib/openclawInstances.ts`, `app/lib/dashboardSession.ts`, or `app/lib/openshellHost.ts` into route files, that is a doctrine violation. + +--- + +## 3. Runtime ownership doctrine + +## 3.1 Single supervised owner for live runtime +Live runtime on Spark must be single-owner and supervised. + +That means: + +- `nemoclaw-dashboard.service` is the owner +- its supervisor script controls process startup/cleanup +- `terminal-server.mjs` is not independently service-owned in normal operation + +The disabled oneshot `tmp_nemoclaw_terminal.service` pattern reflects the correct doctrine: terminal runtime is subordinate to the dashboard supervisor, not a co-equal long-running owner. + +## 3.2 Manual `next dev` on production ports is drift +A manually launched workspace process on `:3000` or `:3011` is drift when the service is supposed to own those ports. + +This includes: + +- `npm run dev` +- `next dev` +- `node server.mjs` from the wrong tree +- shell-started `terminal-server.mjs` + +Manual processes are allowed only for bounded diagnostics when all are true: + +- the service is stopped or intentionally isolated +- the operator explicitly says diagnostic mode is active +- the ports and owning path are checked before and after +- the process is removed after diagnosis + +## 3.3 Supervisor owns child lifecycle +The dashboard supervisor must remain responsible for: + +- preflight port guarding +- starting terminal server +- waiting for terminal `/healthz` +- starting dashboard HTTP owner +- cleanup of child terminal PID +- cleanup of both ports on exit + +Do not move those responsibilities piecemeal into random shell scripts or route handlers. + +## 3.4 Loopback-only terminal server is intentional +`terminal-server.mjs` defaulting to `127.0.0.1:3011` is a security and ownership feature. + +Do not expose `:3011` directly on public interfaces as a convenience shortcut. + +External clients must go through the dashboard-owned bridge unless the architecture is explicitly redesigned. + +## 3.5 Preserve runtime helpers across deploys +The live tree’s `.runtime/*` contents are part of runtime ownership. + +Per local notes, do not delete or clobber runtime helper scripts during sync/deploy. + +At minimum preserve: + +- supervisor scripts +- port guards +- PID/log/runtime state files needed by the service contract + +A deploy that replaces app code but destroys ownership tooling is a broken deploy. + +--- + +## 4. Deploy and release discipline + +## 4.1 Build in projects, run in live +Required flow: + +1. change code in `/home/nvidia/.openclaw/workspace/projects/nemoclaw-dashboard` +2. validate there +3. sync/promote intentionally into `/home/nvidia/nemoclaw-dashboard` +4. restart or reload via the owning service +5. verify runtime ownership after deploy + +Do not “hotfix the live tree and maybe backport later.” That creates untraceable truth forks. + +## 4.2 Every deployment must answer four questions +Before claiming deploy success, record: + +- what commit/content was deployed +- from which source path it came +- which service/process now owns `:3000` and `:3011` +- which verification checks passed + +If these are not known, the deploy is not complete. + +## 4.3 Production mode must be explicit +The supervisor currently selects: + +- production mode if `.next/BUILD_ID` exists +- development mode otherwise + +This fallback is operationally useful, but dangerous because it can make drift look normal. + +Doctrine: + +- intended service mode must be explicit in deploy notes/logs +- production claims require proof that production artifacts existed and were used +- if the service falls back to `next dev`, that state must be treated as degraded, not equivalent to production + +## 4.4 No invisible lane crossing +If code is copied/synced from workspace to live tree, that action must be deliberate and logged. + +Allowed: + +- controlled sync scripts +- documented rsync/copy steps +- release commits/tags + +Not allowed: + +- ad hoc manual edits in both trees +- partial file copying without manifest or explanation +- “I think live matches workspace” with no evidence + +--- + +## 5. Fallback and degraded-mode honesty + +## 5.1 Fallbacks must announce themselves +A fallback path must never present itself as canonical success. + +Examples: + +- default instance chosen because requested instance was missing +- default dashboard session created because client session was absent +- terminal transport falling back from PTY to stream +- service using dev mode because build artifacts were absent + +All of these are legal only if they are observable. + +## 5.2 Honesty rule +When the system is using fallback or degraded behavior, it must say one of: + +- `fallback-active` +- `degraded` +- `defaulted` +- `unknown-owner` +- `unverified` + +It must not say only `ok`, `healthy`, or `connected` if the canonical target was not proven. + +## 5.3 Transport downgrade is not neutral +If terminal runtime drops from PTY to stream mode, treat it as a material runtime fact. + +Expose it in: + +- `/healthz` payloads where relevant +- session init response +- logs +- test expectations + +Do not hide transport downgrades; they affect operator behavior and bug interpretation. + +## 5.4 Missing target must fail closed where authority matters +If a requested instance, sandbox, or runtime owner cannot be proven, then: + +- read paths may return explicit unknown/degraded state +- mutating/interactive paths must prefer explicit failure over silent retargeting + +Silent retargeting is how authority drift becomes data loss or operator deception. + +--- + +## 6. Logging and provenance doctrine + +## 6.1 Logs must prove who did what, where +Every meaningful runtime action should be attributable across these axes: + +- timestamp +- owning component +- route/path/event +- dashboard session id if present +- sandbox id or instance id if relevant +- resolved target actually used +- transport/runtime mode if relevant + +## 6.2 Distinguish requested identity from resolved identity +Whenever user input can be remapped, logs must preserve both: + +- requested sandbox/instance/session +- resolved sandbox/instance/session + +Never overwrite requested identity and keep only the resolved one. That destroys forensic value. + +## 6.3 Ownership logs beat cosmetic logs +Prefer logs that answer: + +- which PID owned the port +- which path launched the process +- which service unit was active +- which upstream terminal URL was bridged + +over decorative logs that only say “connected” or “success”. + +## 6.4 Bridge/proxy logs are mandatory for multiplexed terminal behavior +Because `server.mjs` proxies terminal WebSockets, bridge logs are part of runtime truth. + +Required for connection lifecycle events: + +- bridge id +- client path +- upstream URL +- close source +- close code/reason +- frame counters or equivalent activity signal + +That pattern already exists and must remain. + +--- + +## 7. Testing and acceptance gates + +## 7.1 A web-service change is not done until authority is tested +Tests must validate not only feature output, but authority correctness. + +Minimum acceptance for runtime-affecting changes: + +1. route/feature behaves as intended +2. resolved authority matches expected authority +3. fallback/degraded states are explicit +4. logs contain provenance fields +5. runtime owner on ports remains correct after deploy + +## 7.2 Required verification classes +Any non-trivial change touching routing, instance selection, terminal bridging, or deploy logic must include checks from these classes: + +### A. Static ownership checks +- confirm owner module was changed in one place, not duplicated in several +- confirm shared resolution logic still comes from `app/lib/*` + +### B. Behavior checks +- valid request to intended sandbox/instance +- invalid/missing target request +- fallback activation path +- degraded transport path if applicable + +### C. Runtime checks +- service status +- listener presence on `:3000` and `:3011` +- PID/path ownership for both ports +- dashboard bridge path still functional +- terminal server `/healthz` still functional + +### D. Honesty checks +- UI/API response does not call fallback state healthy without qualification +- logs reveal requested vs resolved target + +## 7.3 No green-by-HTTP-only acceptance +A test bundle that checks only: + +- page loads +- API returns 200 +- WebSocket connects + +is insufficient for this service. + +At least one acceptance step must verify owner identity beyond network reachability. + +## 7.4 Promotion gate +Do not promote workspace changes into live runtime unless all apply: + +- build or intentional dev-mode rationale is documented +- tests passed in workspace lane +- sync target is identified +- ownership verification plan exists +- post-deploy owner verification completed + +--- + +## 8. No-go anti-patterns + +These are banned unless this BIBLE is explicitly amended. + +## 8.1 Split truth planes +- showing dashboard state from one source while actions hit another +- inventory from one registry and terminal attach from a hidden different registry +- route owner changed in one layer but old path still appears canonical elsewhere + +## 8.2 Silent default authority +- missing requested instance silently replaced with default for a mutating action +- absent sandbox silently converted to host shell without explicit operator visibility +- production claim made while actually running fallback dev mode + +## 8.3 Cosmetic health +- declaring service healthy from HTTP 200 alone +- using successful UI render as proof of correct owner +- treating any listener on `:3000` as acceptable + +## 8.4 Live-tree hand editing +- editing `/home/nvidia/nemoclaw-dashboard` directly as the normal development path +- patching runtime helper scripts ad hoc without backporting and documenting + +## 8.5 Ownership multiplication +- separate long-lived service for terminal server without deliberate architecture decision +- parallel dashboard launchers fighting over `:3000` +- supervisor plus manual shell process both trying to own the same port + +## 8.6 Logic cloning +- duplicating instance resolution logic in route handlers +- duplicating dashboard session semantics in UI pages and API routes independently +- separate, inconsistent normalization of sandbox phase names + +## 8.7 Lying fallbacks +- fallback used but not logged +- degraded state shown as normal +- unknown owner reported as healthy + +--- + +## 9. Change protocol for contributors and agents + +## 9.1 Before changing anything, classify the change +Every contributor must first state which doctrine area is affected: + +- source of truth +- route ownership +- runtime ownership +- deploy/release +- fallback/degraded behavior +- logging/provenance +- tests/acceptance + +If the change crosses more than one, say so up front. + +## 9.2 For any authority-affecting change, write the invariant first +Before editing code, write the invariant in one sentence. + +Examples: + +- “dashboard WebSocket clients must only reach terminal sessions through `server.mjs` proxy ownership” +- “requested sandbox identity must be preserved in logs even when remapped” +- “service-owned runtime on `:3000` must not be replaceable by stray workspace dev server without detection” + +If you cannot state the invariant, you are not ready to edit. + +## 9.3 Touch the owner, not a symptom surface +Fix problems in the owning layer. + +Examples: + +- instance-selection bug → fix `app/lib/openclawInstances.ts`, not several callers +- terminal bridge bug → fix `server.mjs` or `terminal-server.mjs`, not UI hacks pretending the bridge is fine +- runtime drift → fix deploy/supervisor/service ownership, not status wording alone + +## 9.4 Any new fallback requires four things +If you add a fallback path, you must also add: + +1. explicit trigger condition +2. explicit log field/message +3. explicit degraded/defaulted outward state where relevant +4. test coverage proving fallback honesty + +No silent convenience fallbacks. + +## 9.5 Any new route must declare ownership and authority inputs +For every new route or socket path, document: + +- owner file +- inputs that affect target resolution +- canonical backend authority it depends on +- fallback behavior +- logging fields +- acceptance checks + +## 9.6 Any runtime/deploy change must verify ownership before and after +Before changing service/deploy/runtime scripts: + +- capture current owner of `:3000` and `:3011` +- capture current service state + +After change: + +- recapture owner of `:3000` and `:3011` +- verify expected service and path ownership +- verify HTTP plus ownership, not HTTP alone + +## 9.7 If architecture changes, update this document in the same work packet +This BIBLE is part of the codebase contract. + +If you change: + +- who owns ports +- how terminal transport is exposed +- what the canonical code/live lanes are +- what module owns instance resolution +- whether `server.mjs` remains the sole bridge owner + +then update this file in the same change. + +Code and doctrine must ship together. + +--- + +## 10. Operational truth checklist + +Use this checklist before saying “NemoClaw web service is healthy.” + +- [ ] code change originated from `projects/nemoclaw-dashboard` +- [ ] live tree sync target is known +- [ ] `nemoclaw-dashboard.service` state is known +- [ ] `:3000` listener exists and owner PID/path is correct +- [ ] `:3011` listener exists and owner PID/path is correct +- [ ] terminal `/healthz` passes +- [ ] dashboard root responds +- [ ] terminal bridge path works through the dashboard owner +- [ ] any fallback/degraded mode is explicitly visible +- [ ] logs show requested and resolved target identity where applicable + +If any box is unchecked, the correct status is not healthy. It is partial, degraded, unknown, or failed. + +--- + +## 11. Non-negotiable service invariants + +These are the core invariants future work must preserve unless deliberately replaced. + +1. **One editable code authority**: `projects/nemoclaw-dashboard` +2. **One live runtime authority**: `/home/nvidia/nemoclaw-dashboard` under `nemoclaw-dashboard.service` +3. **One owner for terminal exposure**: dashboard bridge in `server.mjs` +4. **One owner for terminal session lifecycle**: `terminal-server.mjs` +5. **One owner for instance registry logic**: `app/lib/openclawInstances.ts` +6. **One owner for dashboard session identity logic**: `app/lib/dashboardSession.ts` +7. **HTTP success alone never proves runtime truth** +8. **Fallbacks must announce themselves** +9. **Port ownership outranks appearance** +10. **Doctrine must be updated when architecture changes** + +If a proposed change violates one of these, stop and redesign instead of papering over it. diff --git a/app/api/openshell/dashboard/open/route.ts b/app/api/openshell/dashboard/open/route.ts index afb478f..4a26c99 100644 --- a/app/api/openshell/dashboard/open/route.ts +++ b/app/api/openshell/dashboard/open/route.ts @@ -1,14 +1,108 @@ import { NextResponse } from 'next/server' -import { getOpenClawDashboardUrl } from '../../../../lib/openshellHost' +import { getDefaultOpenClawDashboardInstanceId, getOpenClawDashboardUrl, probeOpenClawDashboard } from '../../../../lib/openshellHost' +import { getOpenClawInstanceIdForSandbox, resolveOpenClawInstance, resolveOpenClawInstanceForSandbox } from '../../../../lib/openclawInstances' +import { resolveRuntimeAuthority } from '../../../../lib/runtimeAuthority' + +function parseInventoryCount(requestUrl: URL) { + const raw = requestUrl.searchParams.get('inventoryCount') + if (raw == null) return null + const count = Number.parseInt(raw, 10) + return Number.isFinite(count) && count >= 0 ? count : null +} + +function buildLaunchUrl(proxiedUrl: string, bootstrapUrl: string | null) { + if (!bootstrapUrl) return proxiedUrl + + const bootstrap = new URL(bootstrapUrl) + const browserHash = bootstrap.hash + bootstrap.hash = '' + + const params = new URLSearchParams({ + path: '/', + bootstrapUrl: bootstrap.toString(), + }) + + return `${proxiedUrl}?${params.toString()}${browserHash}` +} + +export async function GET(request: Request) { + const requestUrl = new URL(request.url) + const sandboxId = requestUrl.searchParams.get('sandboxId') + const requestedInstanceId = requestUrl.searchParams.get('instanceId') + const inventoryCount = parseInventoryCount(requestUrl) + const mappedSandboxInstanceId = getOpenClawInstanceIdForSandbox(sandboxId) + const instance = requestedInstanceId + ? resolveOpenClawInstance(requestedInstanceId) + : resolveOpenClawInstanceForSandbox(sandboxId) + const authority = resolveRuntimeAuthority({ + sandboxId, + instanceId: requestedInstanceId, + }) + const probe = await probeOpenClawDashboard(authority.openclaw.id) + const instanceQualifiedProxyBase = `/api/openshell/instances/${encodeURIComponent(instance.id)}/dashboard/proxy` + const zeroInventoryMappedDashboard = Boolean( + inventoryCount === 0 && + sandboxId && + authority.bridgeActive && + probe.reachable + ) + const truthState = zeroInventoryMappedDashboard + ? 'degraded' + : probe.bootstrapAuthority === 'tokenized-cli' + ? 'tokenized' + : authority.bridgeActive + ? 'bridged' + : 'verified' + + const proxiedUrl = authority.explicitInstanceOverride ? instanceQualifiedProxyBase : '/api/openshell/dashboard/proxy' + const launchUrl = buildLaunchUrl(proxiedUrl, probe.bootstrapUrl) -export async function GET() { - const dashboardUrl = getOpenClawDashboardUrl() return NextResponse.json({ ok: true, - dashboardUrl, - proxiedUrl: '/api/openshell/dashboard/proxy', + sandboxId, + sandboxInstanceId: mappedSandboxInstanceId, + instanceId: instance.id, + inventoryCount, + truthState, + degraded: zeroInventoryMappedDashboard, + authority: { + requestedSandboxId: authority.requestedSandboxId, + resolvedSandboxId: authority.resolvedSandboxId, + sandboxAuthority: authority.sandboxAuthority, + authorityMode: authority.authorityMode, + provenance: authority.provenance, + bridgeActive: authority.bridgeActive, + bridgeReason: authority.bridgeReason, + requestedInstanceId: authority.requestedInstanceId, + instanceId: authority.openclaw.id, + sandboxInstanceId: mappedSandboxInstanceId, + explicitInstanceOverride: authority.explicitInstanceOverride, + usedMappedSandboxInstance: authority.usedMappedSandboxInstance, + }, + defaultInstanceId: getDefaultOpenClawDashboardInstanceId(), + dashboardUrl: getOpenClawDashboardUrl(authority.openclaw.id), + bootstrapUrl: probe.bootstrapUrl, + bootstrapTokenPresent: probe.bootstrapTokenPresent, + bootstrapSource: probe.bootstrapSource, + bootstrapAuthority: probe.bootstrapAuthority, + proxiedUrl, + launchUrl, + instanceQualifiedProxiedUrl: instanceQualifiedProxyBase, openInNewTab: true, - loopbackOnly: true, - note: 'OpenClaw Dashboard is loopback-only on the host, so the web UI uses a local proxy route to expose it.' + loopbackOnly: authority.openclaw.loopbackOnly, + reachable: probe.reachable, + upstreamStatus: probe.status, + upstreamStatusText: probe.statusText, + listenerPresent: probe.listenerPresent, + listenerSummary: probe.listenerSummary, + note: zeroInventoryMappedDashboard + ? 'Degraded truth: live OpenShell inventory is zero, but a bridged OpenClaw dashboard upstream is still reachable. Treat this dashboard as a fallback operator surface, not proof that the sandbox currently exists.' + : probe.bootstrapAuthority === 'tokenized-cli' + ? 'Tokenized bootstrap available: the bridge resolved a dashboard URL from `openclaw dashboard --no-open`, so the proxy can carry the real OpenClaw session contract instead of treating raw loopback fetch as sufficient truth.' + : authority.bridgeActive + ? authority.bridgeReason || 'Bridge-active: this dashboard action is using an explicit upstream OpenClaw bridge instead of assuming same-lane loopback ownership.' + : authority.openclaw.loopbackOnly + ? 'OpenClaw Dashboard is loopback-only on the host, so the web UI uses a local proxy route to expose it.' + : 'OpenClaw Dashboard is reachable directly for this instance.' }) } diff --git a/app/api/openshell/dashboard/proxy/[...path]/route.ts b/app/api/openshell/dashboard/proxy/[...path]/route.ts new file mode 100644 index 0000000..28613e9 --- /dev/null +++ b/app/api/openshell/dashboard/proxy/[...path]/route.ts @@ -0,0 +1,9 @@ +import { proxyErrorResponse, proxyOpenClawDashboard } from '../shared' + +export async function GET(request: Request) { + try { + return await proxyOpenClawDashboard(request) + } catch (error) { + return proxyErrorResponse(error) + } +} diff --git a/app/api/openshell/dashboard/proxy/route.ts b/app/api/openshell/dashboard/proxy/route.ts index 8a18b95..4490583 100644 --- a/app/api/openshell/dashboard/proxy/route.ts +++ b/app/api/openshell/dashboard/proxy/route.ts @@ -1,35 +1,9 @@ -import { NextResponse } from 'next/server' - -const OPENCLAW_LOCAL_DASHBOARD = 'http://127.0.0.1:18789/' +import { proxyErrorResponse, proxyOpenClawDashboard } from './shared' export async function GET(request: Request) { try { - const url = new URL(request.url) - const targetPath = url.searchParams.get('path') || '' - const target = new URL(targetPath, OPENCLAW_LOCAL_DASHBOARD) - - const upstream = await fetch(target.toString(), { - method: 'GET', - headers: { - accept: 'text/html,application/json;q=0.9,*/*;q=0.8' - }, - cache: 'no-store' - }) - - const contentType = upstream.headers.get('content-type') || 'text/html; charset=utf-8' - const body = await upstream.text() - - return new NextResponse(body, { - status: upstream.status, - headers: { - 'content-type': contentType, - 'cache-control': 'no-store' - } - }) + return await proxyOpenClawDashboard(request) } catch (error) { - return NextResponse.json({ - ok: false, - error: error instanceof Error ? error.message : 'Failed to proxy OpenClaw dashboard' - }, { status: 500 }) + return proxyErrorResponse(error) } } diff --git a/app/api/openshell/dashboard/proxy/shared.ts b/app/api/openshell/dashboard/proxy/shared.ts new file mode 100644 index 0000000..b49f588 --- /dev/null +++ b/app/api/openshell/dashboard/proxy/shared.ts @@ -0,0 +1,200 @@ +import { NextResponse } from 'next/server' +import { getDefaultOpenClawDashboardInstanceId } from '@/app/lib/openshellHost' +import { resolveRuntimeAuthority } from '@/app/lib/runtimeAuthority' + +const LEGACY_PROXY_PREFIX = '/api/openshell/dashboard/proxy' +const INSTANCES_PREFIX = '/api/openshell/instances/' +const DASHBOARD_PROXY_SUFFIX = '/dashboard/proxy' +const HOP_BY_HOP_HEADERS = new Set([ + 'accept-encoding', + 'connection', + 'keep-alive', + 'proxy-authenticate', + 'proxy-authorization', + 'te', + 'trailer', + 'transfer-encoding', + 'upgrade', + 'content-length', +]) + +type ProxyTargetResolution = { + instanceId: string | null + proxyPrefix: string + targetBaseUrl: string + authorityMode: 'direct-host' | 'bridged-sandbox-map' | 'bridged-instance-override' + bridgeActive: boolean +} + +function normalizeDashboardProxyInstanceId(instanceId: string | null) { + if (!instanceId) return null + return instanceId === getDefaultOpenClawDashboardInstanceId() ? null : instanceId +} + +function resolveProxyTarget(requestUrl: URL): ProxyTargetResolution { + const pathname = requestUrl.pathname + + if (pathname.startsWith(INSTANCES_PREFIX)) { + const suffixIndex = pathname.indexOf(DASHBOARD_PROXY_SUFFIX, INSTANCES_PREFIX.length) + if (suffixIndex !== -1) { + const rawInstanceId = pathname.slice(INSTANCES_PREFIX.length, suffixIndex) + const decodedInstanceId = decodeURIComponent(rawInstanceId) + const normalizedInstanceId = normalizeDashboardProxyInstanceId(decodedInstanceId) + const authority = resolveRuntimeAuthority({ + sandboxId: requestUrl.searchParams.get('sandboxId'), + instanceId: normalizedInstanceId, + }) + return { + instanceId: authority.openclaw.id, + proxyPrefix: `/api/openshell/instances/${encodeURIComponent(decodedInstanceId)}/dashboard/proxy`, + targetBaseUrl: authority.openclaw.dashboardUrl, + authorityMode: authority.authorityMode, + bridgeActive: authority.bridgeActive, + } + } + } + + const authority = resolveRuntimeAuthority({ + sandboxId: requestUrl.searchParams.get('sandboxId'), + }) + + return { + instanceId: authority.bridgeActive ? authority.openclaw.id : null, + proxyPrefix: LEGACY_PROXY_PREFIX, + targetBaseUrl: authority.openclaw.dashboardUrl, + authorityMode: authority.authorityMode, + bridgeActive: authority.bridgeActive, + } +} + +function buildTargetUrl(requestUrl: URL) { + const { proxyPrefix, targetBaseUrl, authorityMode, bridgeActive } = resolveProxyTarget(requestUrl) + const pathParam = requestUrl.searchParams.get('path') + const bootstrapUrl = requestUrl.searchParams.get('bootstrapUrl') + const pathname = requestUrl.pathname.startsWith(proxyPrefix) + ? requestUrl.pathname.slice(proxyPrefix.length) + : '/' + const upstreamPath = pathParam ?? pathname ?? '/' + const normalizedPath = upstreamPath.startsWith('/') ? upstreamPath : `/${upstreamPath}` + const target = bootstrapUrl + ? new URL(bootstrapUrl) + : new URL(normalizedPath, targetBaseUrl) + + if (!bootstrapUrl) { + requestUrl.searchParams.forEach((value, key) => { + if (key !== 'path') target.searchParams.append(key, value) + }) + } + + return { target, proxyPrefix, authorityMode, bridgeActive } +} + +function copyRequestHeaders(request: Request, target: URL) { + const headers = new Headers() + + request.headers.forEach((value, key) => { + if (!HOP_BY_HOP_HEADERS.has(key.toLowerCase()) && key.toLowerCase() !== 'host') { + headers.set(key, value) + } + }) + + headers.set('host', target.host) + return headers +} + +function copyResponseHeaders(upstream: Response) { + const headers = new Headers() + + upstream.headers.forEach((value, key) => { + if (!HOP_BY_HOP_HEADERS.has(key.toLowerCase())) { + headers.set(key, value) + } + }) + + headers.set('cache-control', 'no-store') + return headers +} + +function proxiedAssetPath(value: string, proxyPrefix: string) { + if ( + !value || + value.startsWith('#') || + value.startsWith('//') || + /^[a-z][a-z\d+.-]*:/i.test(value) + ) { + return value + } + + const normalized = value.startsWith('./') + ? value.slice(2) + : value.startsWith('/') + ? value.slice(1) + : value + + return `${proxyPrefix}/${normalized}` +} + +function rewriteHtml(body: string, proxyPrefix: string) { + return body.replace( + /(<(?:script|img|link)\b[^>]*\s(?:src|href)=)(["'])([^"']+)(["'])/gi, + (_match, prefix: string, quote: string, value: string, suffix: string) => + `${prefix}${quote}${proxiedAssetPath(value, proxyPrefix)}${suffix}` + ) +} + +export async function proxyOpenClawDashboard(request: Request) { + const requestUrl = new URL(request.url) + const { target, proxyPrefix, authorityMode, bridgeActive } = buildTargetUrl(requestUrl) + const method = request.method.toUpperCase() + const headers = copyRequestHeaders(request, target) + const shouldSendBody = !['GET', 'HEAD'].includes(method) + const upstreamInit: RequestInit & { duplex?: 'half' } = { + method, + headers, + body: shouldSendBody ? request.body : undefined, + redirect: 'manual', + cache: 'no-store', + } + + if (shouldSendBody) { + upstreamInit.duplex = 'half' + } + + const upstream = await fetch(target.toString(), { + ...upstreamInit, + }) + + const responseHeaders = copyResponseHeaders(upstream) + responseHeaders.set('x-openclaw-authority-mode', authorityMode) + responseHeaders.set('x-openclaw-bridge-active', bridgeActive ? 'true' : 'false') + responseHeaders.set('x-openclaw-dashboard-bootstrap-contract', 'tokenized-openclaw-dashboard') + const location = upstream.headers.get('location') + if (location) { + responseHeaders.set('location', location.startsWith('/') ? `${proxyPrefix}${location}` : location) + } + const contentType = upstream.headers.get('content-type') || 'application/octet-stream' + + if (contentType.includes('text/html')) { + const body = rewriteHtml(await upstream.text(), proxyPrefix) + responseHeaders.set('content-type', contentType) + return new NextResponse(body, { + status: upstream.status, + headers: responseHeaders, + }) + } + + return new NextResponse(upstream.body, { + status: upstream.status, + headers: responseHeaders, + }) +} + +export function proxyErrorResponse(error: unknown) { + return NextResponse.json( + { + ok: false, + error: error instanceof Error ? error.message : 'Failed to proxy OpenClaw dashboard', + }, + { status: 500 } + ) +} diff --git a/app/api/openshell/instances/[instanceId]/dashboard/proxy/[...path]/route.ts b/app/api/openshell/instances/[instanceId]/dashboard/proxy/[...path]/route.ts new file mode 100644 index 0000000..43f9a56 --- /dev/null +++ b/app/api/openshell/instances/[instanceId]/dashboard/proxy/[...path]/route.ts @@ -0,0 +1,9 @@ +import { proxyErrorResponse, proxyOpenClawDashboard } from '../../../../../dashboard/proxy/shared' + +export async function GET(request: Request) { + try { + return await proxyOpenClawDashboard(request) + } catch (error) { + return proxyErrorResponse(error) + } +} diff --git a/app/api/openshell/instances/[instanceId]/dashboard/proxy/route.ts b/app/api/openshell/instances/[instanceId]/dashboard/proxy/route.ts new file mode 100644 index 0000000..439b41d --- /dev/null +++ b/app/api/openshell/instances/[instanceId]/dashboard/proxy/route.ts @@ -0,0 +1,9 @@ +import { proxyErrorResponse, proxyOpenClawDashboard } from '../../../../dashboard/proxy/shared' + +export async function GET(request: Request) { + try { + return await proxyOpenClawDashboard(request) + } catch (error) { + return proxyErrorResponse(error) + } +} diff --git a/app/api/openshell/terminal/introspect/route.ts b/app/api/openshell/terminal/introspect/route.ts deleted file mode 100644 index 7665eb8..0000000 --- a/app/api/openshell/terminal/introspect/route.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { NextResponse } from 'next/server' -import { dockerExecInOpenShell, OPENSHELL_NAMESPACE } from '../../../../lib/openshellHost' - -export async function GET(request: Request) { - try { - const { searchParams } = new URL(request.url) - const sandboxId = searchParams.get('sandboxId') - - if (!sandboxId) { - return NextResponse.json({ error: 'sandboxId is required' }, { status: 400 }) - } - - const result = await dockerExecInOpenShell( - `kubectl -n ${OPENSHELL_NAMESPACE} get pod ${sandboxId} -o json` - ) - - const pod = JSON.parse(result.stdout) - const containerNames = Array.isArray(pod?.spec?.containers) ? pod.spec.containers.map((c: any) => c.name) : [] - const containerImages = Array.isArray(pod?.spec?.containers) ? pod.spec.containers.map((c: any) => c.image) : [] - const statusNames = Array.isArray(pod?.status?.containerStatuses) ? pod.status.containerStatuses.map((c: any) => c.name) : [] - - return NextResponse.json({ - ok: true, - sandboxId, - containers: containerNames, - images: containerImages, - runningStatuses: statusNames, - raw: result.stdout.trim(), - stderr: result.stderr.trim(), - note: 'Pod/container introspection succeeded. Use this to select the right exec target for terminal attach.' - }) - } catch (error) { - return NextResponse.json({ - ok: false, - error: error instanceof Error ? error.message : 'Failed to introspect sandbox pod' - }, { status: 500 }) - } -} diff --git a/app/api/openshell/terminal/live/route.ts b/app/api/openshell/terminal/live/route.ts index 9e25bd5..24d523d 100644 --- a/app/api/openshell/terminal/live/route.ts +++ b/app/api/openshell/terminal/live/route.ts @@ -1,18 +1,131 @@ import { NextResponse } from 'next/server' +import { inspectSandbox } from '@/app/lib/openshellHost' +import { resolveRuntimeAuthority } from '@/app/lib/runtimeAuthority' const TERMINAL_SERVER_URL = process.env.TERMINAL_SERVER_URL || 'http://127.0.0.1:3011' +const TERMINAL_WS_PROXY_PATH = '/api/openshell/terminal/live/ws' +const PUBLIC_BROWSER_HOST = process.env.PUBLIC_BROWSER_HOST || process.env.PUBLIC_WS_HOST || null +const PUBLIC_BASE_URL = process.env.PUBLIC_BASE_URL || null + +function isNonRoutableHost(candidate: string, protocol = 'http:') { + const normalized = candidate.trim() + if (!normalized) return true + + try { + const parsed = new URL(`${protocol}//${normalized}`) + return ( + parsed.hostname === '0.0.0.0' || + parsed.hostname === '::' || + parsed.hostname === '127.0.0.1' || + parsed.hostname === 'localhost' || + parsed.hostname === '[::1]' || + parsed.hostname === '::1' + ) + } catch { + return ( + normalized.startsWith('0.0.0.0') || + normalized.startsWith('[::]') || + normalized.startsWith('127.0.0.1') || + normalized.startsWith('localhost') || + normalized.startsWith('[::1]') || + normalized.startsWith('::1') + ) + } +} + +function resolveBrowserHost(requestUrl: URL, forwardedHost: string | null) { + const forwardedCandidate = (forwardedHost && forwardedHost.trim()) || '' + if (forwardedCandidate && !isNonRoutableHost(forwardedCandidate, requestUrl.protocol)) { + return forwardedCandidate + } + + if (PUBLIC_BROWSER_HOST && !isNonRoutableHost(PUBLIC_BROWSER_HOST, requestUrl.protocol)) { + return PUBLIC_BROWSER_HOST.trim() + } + + if (PUBLIC_BASE_URL) { + try { + const parsed = new URL(PUBLIC_BASE_URL) + if (!isNonRoutableHost(parsed.host, parsed.protocol)) { + return parsed.host + } + } catch { + // ignore malformed PUBLIC_BASE_URL + } + } + + const requestHostHeader = requestUrl.host + if (!isNonRoutableHost(requestHostHeader, requestUrl.protocol)) { + return requestHostHeader + } + + const hostHeader = requestUrl.hostname + const requestPort = requestUrl.port + if (hostHeader && !isNonRoutableHost(hostHeader, requestUrl.protocol)) { + return requestPort ? `${hostHeader}:${requestPort}` : hostHeader + } + + return null +} + +function getBrowserWebSocketUrl(request: Request, params: { + sessionId: string + sandboxId: string + dashboardSessionId: string +}) { + const requestUrl = new URL(request.url) + const forwardedProto = request.headers.get('x-forwarded-proto') + const forwardedHost = request.headers.get('x-forwarded-host') + const host = resolveBrowserHost(requestUrl, forwardedHost) + if (!host) { + throw new Error('No routable browser host available for terminal websocket URL. Set PUBLIC_BROWSER_HOST or PUBLIC_BASE_URL.') + } + const baseProtocol = (forwardedProto && forwardedProto.trim()) || requestUrl.protocol.replace(/:$/, '') + const protocol = baseProtocol === 'https' ? 'wss:' : 'ws:' + const websocketUrl = new URL(TERMINAL_WS_PROXY_PATH, `${protocol}//${host}`) + websocketUrl.searchParams.set('sessionId', params.sessionId) + websocketUrl.searchParams.set('sandboxId', params.sandboxId) + websocketUrl.searchParams.set('dashboardSessionId', params.dashboardSessionId) + return websocketUrl.toString() +} export async function POST(request: Request) { try { const body = await request.json() - const response = await fetch(`${TERMINAL_SERVER_URL}/session`, { + const sandboxId = typeof body?.sandboxId === 'string' ? body.sandboxId.trim() : '' + const sessionId = typeof body?.sessionId === 'string' ? body.sessionId.trim() : '' + const dashboardSessionId = typeof body?.dashboardSessionId === 'string' && body.dashboardSessionId.trim() + ? body.dashboardSessionId.trim() + : 'dashboard-host' + + const requestedSandboxId = sandboxId || 'host' + + let inspection = null + if (sandboxId) { + try { + inspection = await inspectSandbox(sandboxId) + } catch { + inspection = null + } + } + + const authority = resolveRuntimeAuthority({ + sandboxId, + resolvedSandboxId: inspection?.name || null, + instanceId: typeof body?.instanceId === 'string' ? body.instanceId.trim() : '', + }) + const terminalServerUrl = authority.terminalServerUrl || TERMINAL_SERVER_URL + const resolvedSandboxId = authority.resolvedSandboxId || requestedSandboxId + + const response = await fetch(`${terminalServerUrl}/session`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ - sandboxId: body?.sandboxId, - sessionId: body?.sessionId, + sandboxId: resolvedSandboxId, + sessionId, + dashboardSessionId, }), cache: 'no-store', }) @@ -22,13 +135,31 @@ export async function POST(request: Request) { return NextResponse.json({ ok: false, error: result.error || 'Failed to initialize live terminal session.' }, { status: 502 }) } - const wsBase = TERMINAL_SERVER_URL.replace(/^http/, 'ws') return NextResponse.json({ ok: true, sessionId: result.sessionId, sandboxId: result.sandboxId, + dashboardSessionId: result.dashboardSessionId || dashboardSessionId, replay: result.replay, - websocketUrl: `${wsBase}/ws?sessionId=${encodeURIComponent(result.sessionId)}&sandboxId=${encodeURIComponent(result.sandboxId)}`, + transport: result.transport, + instanceId: authority.openclaw.id, + 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, + }, + sshHostAlias: inspection?.sshHostAlias || null, + websocketUrl: getBrowserWebSocketUrl(request, { + sessionId: result.sessionId, + sandboxId: result.sandboxId, + dashboardSessionId: result.dashboardSessionId || dashboardSessionId, + }), + degraded: !inspection, }) } catch (error) { return NextResponse.json({ diff --git a/app/api/openshell/terminal/open/route.ts b/app/api/openshell/terminal/open/route.ts index 280d18c..58aa771 100644 --- a/app/api/openshell/terminal/open/route.ts +++ b/app/api/openshell/terminal/open/route.ts @@ -16,14 +16,19 @@ export async function GET(request: Request) { ok: true, sandboxId, attached: true, - output: result.stdout.trim(), - stderr: result.stderr.trim(), - note: 'Backend-local terminal probe succeeded. Next step is persistent interactive terminal transport.' + name: result.name, + resolvedId: result.id, + namespace: result.namespace, + phase: result.phase, + sshHostAlias: result.sshHostAlias, + sshConfig: result.sshConfig, + output: result.rawDetails, + note: 'Sandbox inspection succeeded. Next step is persistent interactive terminal transport.', }) } catch (error) { return NextResponse.json({ ok: false, - error: error instanceof Error ? error.message : 'Failed to probe terminal attach' + error: error instanceof Error ? error.message : 'Failed to probe terminal attach', }, { status: 500 }) } } diff --git a/app/api/openshell/terminal/readiness/route.ts b/app/api/openshell/terminal/readiness/route.ts index bfbea37..53be12b 100644 --- a/app/api/openshell/terminal/readiness/route.ts +++ b/app/api/openshell/terminal/readiness/route.ts @@ -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 { - 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 }) } } diff --git a/app/api/openshell/terminal/route.ts b/app/api/openshell/terminal/route.ts deleted file mode 100644 index 186d510..0000000 --- a/app/api/openshell/terminal/route.ts +++ /dev/null @@ -1,206 +0,0 @@ -import { NextResponse } from 'next/server' -import { randomUUID } from 'node:crypto' -import { DEFAULT_EXEC_TIMEOUT_MS, MAX_COMMAND_LENGTH, runSandboxCommand, validateSandboxId, validateTerminalCommand } from '../../../lib/openshellHost' - -type TranscriptEntry = { - id: string - command: string - stdout: string - stderr: string - exitCode: number - executionMode?: string - ranAt: string -} - -type TerminalSession = { - sessionId: string - sandboxId: string - createdAt: string - updatedAt: string - cwd?: string - transcript: TranscriptEntry[] -} - -const sessions = new Map() -const MAX_TRANSCRIPT = 100 - -function ensureSession(sessionId: string, sandboxId: string) { - const existing = sessions.get(sessionId) - if (existing) { - if (existing.sandboxId !== sandboxId) { - throw new Error('sessionId does not match sandboxId') - } - return existing - } - - const now = new Date().toISOString() - const created: TerminalSession = { - sessionId, - sandboxId, - createdAt: now, - updatedAt: now, - cwd: undefined, - transcript: [], - } - sessions.set(sessionId, created) - return created -} - -function sanitizePromptPath(cwd?: string) { - if (!cwd) return '~' - return cwd -} - -function buildWrappedCommand(command: string, cwd?: string) { - const trimmed = validateTerminalCommand(command) - const cdPrefix = cwd ? `cd ${quoteForSingleQuotedShell(cwd)} >/dev/null 2>&1 || exit 98; ` : '' - const shellBody = `${cdPrefix}${trimmed}; __cmd_exit=$?; printf '\n__OPENCLAW_PWD__=%s\n' "$PWD"; exit $__cmd_exit` - return shellBody -} - -function quoteForSingleQuotedShell(value: string) { - return `'${value.replace(/'/g, `'"'"'`)}'` -} - -function extractPwd(stdout: string) { - const match = stdout.match(/\n__OPENCLAW_PWD__=(.*)\n?$/) - if (!match) { - return { cleanedStdout: stdout.trim(), cwd: undefined } - } - - const cwd = match[1]?.trim() || undefined - const cleanedStdout = stdout.slice(0, match.index).trimEnd() - return { cleanedStdout, cwd } -} - -function serializeSession(session: TerminalSession) { - return { - ok: true, - interactive: false, - terminalLike: true, - mode: 'session-backed-command-terminal', - session: { - sessionId: session.sessionId, - sandboxId: session.sandboxId, - cwd: session.cwd, - createdAt: session.createdAt, - updatedAt: session.updatedAt, - prompt: `${session.sandboxId}:${sanitizePromptPath(session.cwd)}$`, - transcript: session.transcript, - }, - limits: { - timeoutMs: DEFAULT_EXEC_TIMEOUT_MS, - maxCommandLength: MAX_COMMAND_LENGTH, - }, - note: 'Dashboard terminal is session-backed and command-scoped. It preserves transcript and working directory across commands, but it is not a raw PTY stream yet.', - } -} - -export async function GET(request: Request) { - const { searchParams } = new URL(request.url) - const sandboxId = searchParams.get('sandboxId') || '' - const sessionId = searchParams.get('sessionId') || '' - - if (!sandboxId) { - return NextResponse.json({ - ok: false, - interactive: false, - mode: 'session-backed-command-terminal', - error: 'sandboxId is required', - }, { status: 400 }) - } - - try { - const validatedSandboxId = validateSandboxId(sandboxId) - - if (!sessionId) { - const createdSession = ensureSession(randomUUID(), validatedSandboxId) - return NextResponse.json(serializeSession(createdSession)) - } - - const session = sessions.get(sessionId) - if (!session || session.sandboxId !== validatedSandboxId) { - return NextResponse.json({ - ok: false, - interactive: false, - mode: 'session-backed-command-terminal', - error: 'session not found', - }, { status: 404 }) - } - - return NextResponse.json(serializeSession(session)) - } catch (error) { - const message = error instanceof Error ? error.message : 'Failed to inspect terminal session' - const status = message.includes('sandboxId') ? 400 : 500 - return NextResponse.json({ - ok: false, - interactive: false, - mode: 'session-backed-command-terminal', - error: message, - }, { status }) - } -} - -export async function POST(request: Request) { - try { - const body = await request.json() - const sandboxInput = typeof body?.sandboxId === 'string' ? body.sandboxId : '' - const sessionInput = typeof body?.sessionId === 'string' ? body.sessionId : '' - const commandInput = typeof body?.command === 'string' ? body.command : '' - - const sandboxId = validateSandboxId(sandboxInput) - const command = validateTerminalCommand(commandInput) - const sessionId = sessionInput || randomUUID() - const session = ensureSession(sessionId, sandboxId) - - const wrappedCommand = buildWrappedCommand(command, session.cwd) - const result = await runSandboxCommand(sandboxId, wrappedCommand) - const { cleanedStdout, cwd } = extractPwd(result.stdout) - - session.cwd = cwd || session.cwd - session.updatedAt = new Date().toISOString() - session.transcript.push({ - id: randomUUID(), - command, - stdout: cleanedStdout, - stderr: result.stderr.trim(), - exitCode: result.exitCode, - executionMode: result.mode, - ranAt: session.updatedAt, - }) - if (session.transcript.length > MAX_TRANSCRIPT) { - session.transcript.splice(0, session.transcript.length - MAX_TRANSCRIPT) - } - - const ok = result.exitCode === 0 - - return NextResponse.json({ - ...serializeSession(session), - ok, - command, - exitCode: result.exitCode, - executionMode: result.mode, - stdout: cleanedStdout, - stderr: result.stderr.trim(), - note: ok - ? 'Command completed and terminal session state was updated.' - : `Command exited non-zero (${result.exitCode}); transcript preserved for inspection.`, - }, { status: ok ? 200 : 502 }) - } catch (error) { - const message = error instanceof Error ? error.message : 'Failed to run sandbox command' - const status = message === 'sandboxId is required' - || message === 'command is required' - || message.includes('sandboxId must be a valid lowercase DNS-style identifier') - || message.includes('command exceeds') - || message.includes('sessionId does not match sandboxId') - ? 400 - : 500 - - return NextResponse.json({ - ok: false, - interactive: false, - mode: 'session-backed-command-terminal', - error: message, - }, { status }) - } -} diff --git a/app/api/sandbox/create/route.ts b/app/api/sandbox/create/route.ts new file mode 100644 index 0000000..e195b8f --- /dev/null +++ b/app/api/sandbox/create/route.ts @@ -0,0 +1,407 @@ +import { NextResponse } from "next/server" +import { execFile, spawn } from "node:child_process" +import { promisify } from "node:util" +import { inspectSandbox } from "@/app/lib/openshellHost" + +const execFileAsync = promisify(execFile) +const HOME = process.env.HOME || "" +const NEMOCLAW_SETUP = process.env.NEMOCLAW_SETUP || `${HOME}/NemoClaw/scripts/setup.sh` +const OPENSHELL_BIN = process.env.OPENSHELL_BIN || `${HOME}/.local/bin/openshell` +const HOST_PATH = [ + `${HOME}/.local/bin`, + `${HOME}/.nvm/versions/node/v22.22.2/bin`, + `${HOME}/.nvm/versions/node/v22.22.1/bin`, + "/opt/homebrew/bin", + "/usr/local/bin", + "/usr/bin", + "/bin", + "/usr/sbin", + "/sbin", + process.env.PATH || "", +].filter(Boolean).join(":") + +const GATEWAY_REUSE_POLICY = [ + "Gateway reuse policy:", + "- reuse the existing OpenShell gateway when `openshell status` reports `Connected`", + "- only start a fresh gateway when no healthy gateway is already connected", + `- force non-interactive PATH to include ${HOME}/.local/bin so openshell resolves during setup`, +].join("\n") + +function validateSandboxName(name: string) { + if (!name || typeof name !== "string") throw new Error("sandbox name is required") + if (name.length > 63) throw new Error("sandbox name too long (max 63 chars)") + if (!/^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/.test(name)) { + throw new Error("sandbox name must be lowercase alphanumeric with optional internal hyphens") + } + return name +} + +type SandboxVerification = { + verified: boolean + summary: string + details?: { + name: string + id: string | null + phase: string | null + namespace: string | null + sshHostAlias: string + rawPhase: string | null + rawDetails: string + } + error?: string +} + +const AUTHORITATIVE_SUCCESS_PHASE = "Ready" + +function elapsedMs(start: number) { + return Date.now() - start +} + +async function verifySandboxCreation(sandboxName: string): Promise { + const startedAt = Date.now() + console.log(`[sandbox/create] verify:start sandbox=${sandboxName}`) + try { + const sandbox = await inspectSandbox(sandboxName) + const phase = sandbox.phase ?? "Unknown" + const rawPhase = sandbox.rawPhase ?? "Unknown" + const verified = rawPhase === AUTHORITATIVE_SUCCESS_PHASE + console.log( + `[sandbox/create] verify:done sandbox=${sandboxName} rawPhase=${rawPhase} normalizedPhase=${phase} verified=${verified} elapsedMs=${elapsedMs(startedAt)}`, + ) + + return { + verified, + summary: verified + ? `Verified via openshell sandbox get with authoritative phase (${rawPhase}).` + : `Sandbox lookup succeeded, but authoritative phase was not ready (${rawPhase}); normalized phase=${phase}.`, + details: { + name: sandbox.name, + id: sandbox.id, + phase: sandbox.phase, + namespace: sandbox.namespace, + sshHostAlias: sandbox.sshHostAlias, + rawPhase: sandbox.rawPhase, + rawDetails: sandbox.rawDetails, + }, + ...(verified ? {} : { error: `Sandbox authoritative phase ${rawPhase} is not the required success state ${AUTHORITATIVE_SUCCESS_PHASE}.` }), + } + } catch (error) { + const message = error instanceof Error ? error.message : String(error ?? "Sandbox verification failed") + console.log(`[sandbox/create] verify:error sandbox=${sandboxName} elapsedMs=${elapsedMs(startedAt)} message=${message}`) + return { + verified: false, + summary: "Create command returned success, but post-create authoritative verification failed.", + error: message, + } + } +} + +async function runCommand(file: string, args: string[], env: NodeJS.ProcessEnv) { + const startedAt = Date.now() + console.log(`[sandbox/create] command:start file=${file} args=${JSON.stringify(args)}`) + console.log(`[sandbox/create] gateway-policy ${GATEWAY_REUSE_POLICY.replace(/\n/g, " | ")}`) + try { + const { stdout, stderr } = await execFileAsync(file, args, { + env, + maxBuffer: 20 * 1024 * 1024, + }) + console.log(`[sandbox/create] command:done file=${file} elapsedMs=${elapsedMs(startedAt)} stdoutBytes=${String(stdout).length} stderrBytes=${String(stderr).length}`) + return { ok: true as const, stdout: String(stdout).trim(), stderr: String(stderr).trim() } + } catch (error: any) { + const message = error instanceof Error ? error.message : String(error ?? "Command failed") + console.log( + `[sandbox/create] command:error file=${file} elapsedMs=${elapsedMs(startedAt)} message=${message} stdoutBytes=${String(error?.stdout || "").length} stderrBytes=${String(error?.stderr || "").length}`, + ) + return { + ok: false as const, + stdout: String(error?.stdout || "").trim(), + stderr: String(error?.stderr || "").trim(), + error: message, + } + } +} + +async function runCreateCommandBounded(file: string, args: string[], env: NodeJS.ProcessEnv, timeoutMs: number) { + const startedAt = Date.now() + console.log(`[sandbox/create] bounded-command:start file=${file} args=${JSON.stringify(args)} timeoutMs=${timeoutMs}`) + + return await new Promise<{ + completed: boolean + timedOut: boolean + exitCode: number | null + signal: NodeJS.Signals | null + stdout: string + stderr: string + error?: string + }>((resolve) => { + const child = spawn(file, args, { + env, + stdio: ["ignore", "pipe", "pipe"], + }) + + let stdout = "" + let stderr = "" + let settled = false + + const finish = (result: { + completed: boolean + timedOut: boolean + exitCode: number | null + signal: NodeJS.Signals | null + stdout: string + stderr: string + error?: string + }) => { + if (settled) return + settled = true + clearTimeout(timer) + resolve(result) + } + + child.stdout?.on("data", (chunk) => { + stdout += String(chunk) + }) + + child.stderr?.on("data", (chunk) => { + stderr += String(chunk) + }) + + child.on("error", (error) => { + const message = error instanceof Error ? error.message : String(error ?? "Command failed") + console.log(`[sandbox/create] bounded-command:error file=${file} elapsedMs=${elapsedMs(startedAt)} message=${message}`) + finish({ + completed: false, + timedOut: false, + exitCode: null, + signal: null, + stdout: stdout.trim(), + stderr: stderr.trim(), + error: message, + }) + }) + + child.on("close", (code, signal) => { + console.log(`[sandbox/create] bounded-command:close file=${file} elapsedMs=${elapsedMs(startedAt)} code=${code} signal=${signal} stdoutBytes=${stdout.length} stderrBytes=${stderr.length}`) + finish({ + completed: true, + timedOut: false, + exitCode: code, + signal, + stdout: stdout.trim(), + stderr: stderr.trim(), + ...(code === 0 ? {} : { error: `Command exited with code ${code}${signal ? ` signal ${signal}` : ""}` }), + }) + }) + + const timer = setTimeout(() => { + console.log(`[sandbox/create] bounded-command:timeout file=${file} elapsedMs=${elapsedMs(startedAt)} sending=SIGTERM`) + child.kill("SIGTERM") + setTimeout(() => { + if (!settled) { + console.log(`[sandbox/create] bounded-command:timeout-escalate file=${file} elapsedMs=${elapsedMs(startedAt)} sending=SIGKILL`) + child.kill("SIGKILL") + } + }, 2000) + finish({ + completed: false, + timedOut: true, + exitCode: null, + signal: "SIGTERM", + stdout: stdout.trim(), + stderr: stderr.trim(), + error: `Command exceeded timeout of ${timeoutMs}ms`, + }) + }, timeoutMs) + }) +} + +async function waitForSandboxReady(sandboxName: string, timeoutMs: number, intervalMs: number) { + const startedAt = Date.now() + let lastVerification: SandboxVerification | null = null + let attempts = 0 + + while (elapsedMs(startedAt) < timeoutMs) { + attempts += 1 + lastVerification = await verifySandboxCreation(sandboxName) + console.log(`[sandbox/create] readiness:attempt sandbox=${sandboxName} attempt=${attempts} verified=${lastVerification.verified} elapsedMs=${elapsedMs(startedAt)}`) + if (lastVerification.verified) { + return { + verified: true as const, + verification: lastVerification, + attempts, + elapsedMs: elapsedMs(startedAt), + } + } + await new Promise((resolve) => setTimeout(resolve, intervalMs)) + } + + return { + verified: false as const, + verification: lastVerification, + attempts, + elapsedMs: elapsedMs(startedAt), + } +} + +export async function GET() { + return NextResponse.json({ + ok: true, + blueprints: [ + { + id: "nemoclaw-blueprint", + label: "New NemoClaw Sandbox", + description: "Bootstraps a full NemoClaw sandbox using the nemoclaw-blueprint workflow.", + type: "blueprint", + source: "~/NemoClaw/nemoclaw-blueprint/blueprint.yaml", + supportsTailscale: true, + }, + { + id: "custom-sandbox", + label: "New Custom Sandbox", + description: "Create a generic OpenShell sandbox with a custom policy path.", + type: "custom", + source: "dashboard-custom", + supportsTailscale: false, + }, + ], + }) +} + +export async function POST(request: Request) { + const requestStartedAt = Date.now() + console.log(`[sandbox/create] request:start elapsedMs=0`) + try { + const body = await request.json() + const blueprint = typeof body?.blueprint === "string" ? body.blueprint : "" + const sandboxName = validateSandboxName(typeof body?.sandboxName === "string" ? body.sandboxName.trim() : "") + const policy = body?.policy && typeof body.policy === "object" ? body.policy : null + const enableTailscale = Boolean(body?.enableTailscale) + console.log(`[sandbox/create] request:parsed sandbox=${sandboxName} blueprint=${blueprint} enableTailscale=${enableTailscale} elapsedMs=${elapsedMs(requestStartedAt)}`) + + if (!blueprint) { + return NextResponse.json({ ok: false, error: "blueprint is required" }, { status: 400 }) + } + + if (blueprint === "nemoclaw-blueprint") { + const env: NodeJS.ProcessEnv = { + ...process.env, + PATH: HOST_PATH, + NEMOCLAW_SANDBOX_NAME: sandboxName, + } + + if (!enableTailscale) { + env.NVIDIA_API_KEY = env.NVIDIA_API_KEY || "optional-local-mode" + } + + const result = await runCommand("/bin/bash", [NEMOCLAW_SETUP, sandboxName], env) + + if (!result.ok) { + return NextResponse.json({ + ok: false, + error: result.error, + stdout: result.stdout, + stderr: result.stderr, + blueprint, + sandboxName, + enableTailscale, + }, { status: 500 }) + } + + const verification = await verifySandboxCreation(sandboxName) + const created = verification.verified + console.log(`[sandbox/create] request:complete sandbox=${sandboxName} created=${created} elapsedMs=${elapsedMs(requestStartedAt)}`) + + return NextResponse.json({ + ok: created, + blueprint, + sandboxName, + created, + verified: verification.verified, + verification, + mode: "nemoclaw-blueprint", + enableTailscale, + gatewayReusePolicy: GATEWAY_REUSE_POLICY, + setupPath: NEMOCLAW_SETUP, + hostPath: HOST_PATH, + stdout: result.stdout, + stderr: result.stderr, + note: created + ? (enableTailscale + ? "NemoClaw blueprint workflow completed with Tailscale-enabled prerequisites. Existing healthy OpenShell gateways are reused before any new gateway start is attempted." + : "NemoClaw blueprint workflow completed in local/default mode. Existing healthy OpenShell gateways are reused before any new gateway start is attempted.") + : "Blueprint command reported success, but the sandbox never reached a ready verification state afterward.", + }, { status: created ? 200 : 502 }) + } + + if (blueprint === "custom-sandbox") { + const env: NodeJS.ProcessEnv = { + ...process.env, + PATH: HOST_PATH, + NO_COLOR: "1", + CLICOLOR: "0", + CLICOLOR_FORCE: "0", + } + const createAttempt = await runCreateCommandBounded(OPENSHELL_BIN, ["sandbox", "create", "--name", sandboxName], env, 15000) + + if (createAttempt.completed && createAttempt.exitCode !== 0) { + return NextResponse.json({ + ok: false, + error: createAttempt.error ?? `create command failed with exit code ${createAttempt.exitCode}`, + stdout: createAttempt.stdout, + stderr: createAttempt.stderr, + blueprint, + sandboxName, + }, { status: 500 }) + } + + const readiness = await waitForSandboxReady(sandboxName, 90000, 2000) + const verification = readiness.verification ?? { + verified: false, + summary: "Sandbox readiness polling produced no verification result.", + error: "Sandbox readiness polling produced no verification result.", + } + const created = readiness.verified + const policyPrepared = Boolean(policy) + console.log( + `[sandbox/create] request:complete sandbox=${sandboxName} created=${created} createTimedOut=${createAttempt.timedOut} readinessAttempts=${readiness.attempts} elapsedMs=${elapsedMs(requestStartedAt)}`, + ) + return NextResponse.json({ + ok: created, + blueprint, + sandboxName, + created, + verified: verification.verified, + verification, + mode: "custom-sandbox", + stdout: createAttempt.stdout, + stderr: createAttempt.stderr, + createCommand: { + completed: createAttempt.completed, + timedOut: createAttempt.timedOut, + exitCode: createAttempt.exitCode, + signal: createAttempt.signal, + error: createAttempt.error, + }, + readiness: { + attempts: readiness.attempts, + elapsedMs: readiness.elapsedMs, + }, + policyPrepared, + note: created + ? (createAttempt.timedOut + ? "Custom sandbox reached Ready even though the create CLI did not exit promptly." + : (policyPrepared + ? "Custom sandbox created. Policy draft is prepared, but applying it live should be a follow-up action." + : "Custom sandbox created.")) + : "Create command started, but the sandbox never reached a ready verification state afterward.", + }, { status: created ? 200 : 502 }) + } + + return NextResponse.json({ ok: false, error: `unknown blueprint: ${blueprint}` }, { status: 400 }) + } catch (error) { + const message = error instanceof Error ? error.message : "Sandbox creation failed" + const status = /required|must be|too long|unknown blueprint/.test(message) ? 400 : 500 + console.log(`[sandbox/create] request:error elapsedMs=${elapsedMs(requestStartedAt)} message=${message}`) + return NextResponse.json({ ok: false, error: message }, { status }) + } +} diff --git a/app/api/telemetry/real/route.ts b/app/api/telemetry/real/route.ts index f8b88e9..c6eb9f8 100644 --- a/app/api/telemetry/real/route.ts +++ b/app/api/telemetry/real/route.ts @@ -1,94 +1,400 @@ import { NextResponse } from "next/server" +import { execFile } from "node:child_process" +import { existsSync } from "node:fs" +import { hostname, networkInterfaces } from "node:os" +import { promisify } from "node:util" +import { resolveRuntimeAuthority } from "@/app/lib/runtimeAuthority" + +const execFileAsync = promisify(execFile) + +const HOME = process.env.HOME || "" + +const NODE_BIN_CANDIDATES = [ + process.env.NODE_BIN, + HOME ? `${HOME}/.nvm/versions/node/v22.22.2/bin/node` : undefined, + HOME ? `${HOME}/.nvm/versions/node/v22.22.1/bin/node` : undefined, + "/Users/markmckeen/.nvm/versions/node/v22.22.2/bin/node", + "/Users/markmckeen/.nvm/versions/node/v22.22.1/bin/node", + "/opt/homebrew/bin/node", + "/usr/local/bin/node", + "/usr/bin/node", +].filter((value): value is string => Boolean(value)) +const NODE_BIN = NODE_BIN_CANDIDATES.find((candidate) => existsSync(candidate)) ?? NODE_BIN_CANDIDATES[0] +const NEMOCLAW_BIN_CANDIDATES = [ + process.env.NEMOCLAW_BIN, + HOME ? `${HOME}/.local/bin/nemoclaw` : undefined, + HOME ? `${HOME}/.nemoclaw/source/bin/nemoclaw.js` : undefined, + HOME ? `${HOME}/NemoClaw/bin/nemoclaw.js` : undefined, + HOME ? `${HOME}/.nvm/versions/node/v22.22.2/bin/nemoclaw` : undefined, + "/Users/markmckeen/.local/bin/nemoclaw", + "/Users/markmckeen/.nemoclaw/source/bin/nemoclaw.js", + "/Users/markmckeen/NemoClaw/bin/nemoclaw.js", + "/Users/markmckeen/NemoClaw-mmckeen/bin/nemoclaw.js", + "/Users/markmckeen/.nvm/versions/node/v22.22.2/bin/nemoclaw", + "/usr/local/bin/nemoclaw", + "/opt/homebrew/bin/nemoclaw", +].filter((value): value is string => Boolean(value)) +const NEMOCLAW_BIN = + NEMOCLAW_BIN_CANDIDATES.find((candidate) => existsSync(candidate)) ?? NEMOCLAW_BIN_CANDIDATES[0] + +const OPENSHELL_BIN_CANDIDATES = [ + process.env.OPENSHELL_BIN, + HOME ? `${HOME}/.local/bin/openshell` : undefined, + "/Users/markmckeen/.local/bin/openshell", + "/Users/markmckeen/OpenShell/target/release/openshell", + "/Users/markmckeen/openshell/target/release/openshell", + "/Users/markmckeen/openshell/scripts/bin/openshell", + "/usr/local/bin/openshell", + "/opt/homebrew/bin/openshell", +].filter((value): value is string => Boolean(value)) +const OPENSHELL_BIN = + OPENSHELL_BIN_CANDIDATES.find((candidate) => existsSync(candidate)) ?? OPENSHELL_BIN_CANDIDATES[0] + +type SandboxSummary = { + id: string + name: string + namespace: string + status: string + sshHostAlias: string + hasSshConfig: boolean + source: "openshell" + isDefault: boolean +} + +type NemoClawSummary = { + available: boolean + defaultSandboxNames: string[] + serviceLines: string[] + summaryLines: string[] + source: "nemoclaw-cli" | "none" +} + +type SandboxItem = { + metadata: { + name: string + namespace: string + labels: Record + } + spec: { + template: { + image: string | null + } + } + status: { + phase: string + podIP: string | null + conditions: Array<{ + type: string + status: string + }> + } +} + +function stripAnsi(value: string) { + return value.replace(/\u001b\[[0-9;]*m/g, "") +} + +function parseField(output: string, label: string) { + const normalizedLabel = label.toLowerCase() + const line = output + .split(/\r?\n/) + .map((entry) => stripAnsi(entry).trim()) + .find((entry) => entry.toLowerCase().startsWith(`${normalizedLabel}:`)) + + return line ? line.slice(label.length + 1).trim() : null +} + +function parseSshHostAlias(sshConfig: string, fallbackName: string) { + const hostLine = sshConfig + .split(/\r?\n/) + .map((entry) => entry.trim()) + .find((entry) => entry.toLowerCase().startsWith("host ")) + + const alias = hostLine + ?.split(/\s+/) + .slice(1) + .find((entry) => entry !== "*") + + return alias || `openshell-${fallbackName}` +} + +function normalizePhase(phase: string | null) { + const value = (phase ?? "Unknown").toLowerCase() + + switch (value) { + case "ready": + return "Running" + case "provisioning": + return "Pending" + case "deleting": + return "Stopping" + case "error": + return "Error" + default: + return phase ?? "Unknown" + } +} + +function parseOpenShellSandboxNames(output: string) { + return output + .split(/\r?\n/) + .map((entry) => stripAnsi(entry).trim()) + .filter( + (entry) => + entry && + !/^name\s+/i.test(entry) && + !/^[\s\-=]+$/.test(entry) && + !/^no sandboxes found\.?$/i.test(entry) + ) + .map((entry) => entry.split(/\s{2,}/)[0]?.trim()) + .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): NemoClawSummary { + if (!output) { + return { + available: false, + defaultSandboxNames: [], + serviceLines: [], + summaryLines: [], + source: "none", + } + } + + const lines = output + .split(/\r?\n/) + .map((entry) => stripAnsi(entry).trim()) + .filter(Boolean) + + const serviceLines = lines.filter((entry) => /\((running|stopped|failed|error|unknown)\)/i.test(entry)) + + return { + available: true, + defaultSandboxNames: Array.from(defaultSandboxNames), + serviceLines, + summaryLines: lines.slice(0, 20), + source: "nemoclaw-cli", + } +} + +async function execNemoclaw(args: string[]) { + const env = { + ...process.env, + PATH: `${process.env.PATH ?? ""}:${HOME ? `${HOME}/.local/bin:${HOME}/.nvm/versions/node/v22.22.2/bin:${HOME}/.nvm/versions/node/v22.22.1/bin:` : ""}/opt/homebrew/bin:/usr/local/bin:/usr/bin`, + NO_COLOR: "1", + CLICOLOR: "0", + CLICOLOR_FORCE: "0", + } + + const command = /\.(?:c?m?js|ts)$/i.test(NEMOCLAW_BIN) + ? execFileAsync(NODE_BIN, [NEMOCLAW_BIN, ...args], { env }) + : execFileAsync(NEMOCLAW_BIN, args, { env }) + + const { stdout, stderr } = await command + return { stdout, stderr } +} + +async function execOpenShell(args: string[]) { + const { stdout, stderr } = await execFileAsync(OPENSHELL_BIN, args, { + env: { + ...process.env, + NO_COLOR: "1", + CLICOLOR: "0", + CLICOLOR_FORCE: "0", + }, + }) + + return { stdout, stderr } +} + + +function readHostIdentity() { + const interfaces = networkInterfaces() + const preferred = ["en0", "en1", "bridge0", "lo0"] + + for (const name of preferred) { + const entries = interfaces[name] ?? [] + const match = entries.find((entry) => entry.family === "IPv4" && !entry.internal) + if (match?.address) { + return { hostname: hostname(), address: match.address, interface: name } + } + } + + for (const [name, entries] of Object.entries(interfaces)) { + const match = (entries ?? []).find((entry) => entry.family === "IPv4" && !entry.internal) + if (match?.address) { + return { hostname: hostname(), address: match.address, interface: name } + } + } + + return { hostname: hostname(), address: "127.0.0.1", interface: "lo0" } +} + +async function readSandbox(name: string, defaultSandboxNames: Set): Promise<{ summary: SandboxSummary; pod: SandboxItem }> { + try { + const [{ stdout: detailsStdout }, { stdout: sshStdout }] = await Promise.all([ + execOpenShell(["sandbox", "get", name]), + execOpenShell(["sandbox", "ssh-config", name]), + ]) + + const sandboxName = parseField(detailsStdout, "Name") ?? name + const namespace = parseField(detailsStdout, "Namespace") ?? "openshell" + const phase = normalizePhase(parseField(detailsStdout, "Phase")) + const sandboxId = parseField(detailsStdout, "Id") ?? sandboxName + const sshConfig = sshStdout.trim() + const sshHostAlias = parseSshHostAlias(sshConfig, sandboxName) + const isDefault = defaultSandboxNames.has(sandboxName) + + return { + summary: { + id: sandboxId, + name: sandboxName, + namespace, + status: phase, + sshHostAlias, + hasSshConfig: Boolean(sshConfig), + source: "openshell", + isDefault, + }, + pod: { + metadata: { + name: sandboxName, + namespace, + labels: { + "nemoclaw.ai/sandbox-name": sandboxName, + ...(sandboxId ? { "nemoclaw.ai/sandbox-id": sandboxId } : {}), + ...(isDefault ? { "nemoclaw.ai/default": "true" } : {}), + }, + }, + spec: { + template: { + image: null, + }, + }, + status: { + phase, + podIP: sshHostAlias, + conditions: [ + { + type: "Ready", + status: phase === "Running" ? "True" : "False", + }, + ], + }, + }, + } + } catch (error) { + const sandboxName = name + const isDefault = defaultSandboxNames.has(sandboxName) + return { + summary: { + id: sandboxName, + name: sandboxName, + namespace: "openshell", + status: "Unknown", + sshHostAlias: `openshell-${sandboxName}`, + hasSshConfig: false, + source: "openshell", + isDefault, + }, + pod: { + metadata: { + name: sandboxName, + namespace: "openshell", + labels: { + "nemoclaw.ai/sandbox-name": sandboxName, + "nemoclaw.ai/sandbox-id": sandboxName, + ...(isDefault ? { "nemoclaw.ai/default": "true" } : {}), + }, + }, + spec: { + template: { + image: null, + }, + }, + status: { + phase: "Unknown", + podIP: `openshell-${sandboxName}`, + conditions: [ + { + type: "Ready", + status: "False", + }, + ], + }, + }, + } + } +} export async function GET() { try { - // Use docker exec with a timeout and direct kubectl inside container - const { exec } = await import('child_process') - const util = await import('util') - const execPromise = util.promisify(exec) + const [{ stdout: sandboxListStdout }, nemoclawListResult, nemoclawStatusResult] = await Promise.all([ + execOpenShell(["sandbox", "list"]), + execNemoclaw(["list"]).catch(() => null), + execNemoclaw(["status"]).catch(() => null), + ]) - const dockerPath = '/Applications/Docker.app/Contents/Resources/bin/docker' - - // Run kubectl inside container with explicit kubeconfig - const command = `${dockerPath} exec openshell-cluster-openshell /bin/sh -c 'KUBECONFIG=/etc/rancher/k3s/k3s.yaml kubectl get pods -o json -n agent-sandbox-system'` - - const execWithTimeout = (cmd: string, timeoutMs: number): Promise<{ stdout: string; stderr: string }> => { - return new Promise((resolve, reject) => { - const child = exec(cmd, (error, stdout, stderr) => { - if (error) { - reject(error) - } else { - resolve({ stdout, stderr }) - } - }) - - setTimeout(() => { - child.kill('SIGKILL') - reject(new Error('Command timed out')) - }, timeoutMs) - }) - } - - const { stdout, stderr } = await execWithTimeout(command, 10000) + const defaultSandboxNames = parseDefaultSandboxNames(nemoclawListResult?.stdout ?? "") + const names = parseOpenShellSandboxNames(sandboxListStdout) + const results = await Promise.all(names.map((name) => readSandbox(name, defaultSandboxNames))) + const sandboxes = results.map((result) => result.summary) + const items = results.map((result) => result.pod) + const nemoclaw = buildNemoClawSummary(nemoclawStatusResult?.stdout ?? null, defaultSandboxNames) - if (stderr && !stderr.includes('Waiting')) { - console.error('kubectl stderr:', stderr) - } + const authorities = sandboxes.map((sandbox) => ({ + sandboxId: sandbox.id, + ...resolveRuntimeAuthority({ sandboxId: sandbox.id }), + })) - if (!stdout.trim()) { - throw new Error('No output from kubectl') - } + const inventoryCount = sandboxes.length + const hasMappedFallbackWithoutInventory = inventoryCount === 0 - const pods = JSON.parse(stdout) - - // Filter to only sandbox-related pods - const sandboxPods = pods.items - .filter((pod: any) => - pod.metadata.namespace === 'agent-sandbox-system' || - pod.metadata.name.includes('sandbox') - ) - .map((pod: any) => ({ - metadata: { - name: pod.metadata.name, - namespace: pod.metadata.namespace - }, - status: { - phase: pod.status.phase, - podIP: pod.status.podIP, - conditions: pod.status.conditions || [] - } - })) - - return NextResponse.json({ - pods: { items: sandboxPods }, - message: 'Real telemetry fetched from container' - }) - } catch (error: any) { - console.error('Error fetching real telemetry:', error.message) - - // Fallback to mock data return NextResponse.json({ - pods: { - items: [ - { - metadata: { name: 'agent-sandbox-1', namespace: 'agent-sandbox-system' }, - status: { - phase: 'Running', - podIP: '10.42.0.2', - conditions: [{ type: 'Ready', status: 'True' }] - } - }, - { - metadata: { name: 'agent-sandbox-2', namespace: 'agent-sandbox-system' }, - status: { - phase: 'Pending', - podIP: null, - conditions: [{ type: 'Ready', status: 'False' }] - } - } - ] - }, - message: `Using mock data (${error.message})` + sandboxes, + pods: { items }, + nemoclaw, + host: readHostIdentity(), + source: "openshell-cli", + authoritySource: "runtimeAuthority", + truthState: hasMappedFallbackWithoutInventory ? "unverified" : "verified", + degraded: hasMappedFallbackWithoutInventory, + inventoryCount, + authorities: authorities.map((authority) => ({ + sandboxId: authority.sandboxId, + requestedSandboxId: authority.requestedSandboxId, + resolvedSandboxId: authority.resolvedSandboxId, + sandboxAuthority: authority.sandboxAuthority, + instanceId: authority.openclaw.id, + sandboxInstanceId: authority.mappedSandboxInstanceId, + explicitInstanceOverride: authority.explicitInstanceOverride, + usedMappedSandboxInstance: authority.usedMappedSandboxInstance, + })), + defaultSource: 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." + : "Fetched live sandbox inventory from the clean OpenShell runtime", }) + } catch (error) { + console.error("Error fetching real telemetry:", error) + + return NextResponse.json( + { + error: error instanceof Error ? error.message : "Failed to fetch live OpenShell sandbox inventory", + }, + { status: 500 } + ) } } diff --git a/app/components/CompactTelemetryDisplay.tsx b/app/components/CompactTelemetryDisplay.tsx index d3480f3..1be6754 100644 --- a/app/components/CompactTelemetryDisplay.tsx +++ b/app/components/CompactTelemetryDisplay.tsx @@ -1,5 +1,5 @@ "use client" -import { useState, useEffect } from 'react' +import { useState, useEffect, useCallback } from 'react' import CompactGauge from './CompactGauge' interface TelemetryData { @@ -9,6 +9,7 @@ interface TelemetryData { gpuMemoryUsed?: number gpuMemoryTotal?: number gpuTemperature?: number + timestamp?: string } interface Sandbox { @@ -30,25 +31,6 @@ export default function CompactTelemetryDisplay() { fetchSandboxes() }, []) - useEffect(() => { - if (selectedSandbox) { - fetchTelemetry() - } else { - fetchCombinedTelemetry() - } - }, [selectedSandbox]) - - useEffect(() => { - const interval = setInterval(() => { - if (selectedSandbox) { - fetchTelemetry() - } else { - fetchCombinedTelemetry() - } - }, 5000) - return () => clearInterval(interval) - }, [selectedSandbox]) - const fetchSandboxes = async () => { try { const response = await fetch('/api/telemetry/real') @@ -67,7 +49,7 @@ export default function CompactTelemetryDisplay() { } } - const fetchTelemetry = async () => { + const fetchTelemetry = useCallback(async () => { try { const response = await fetch('/api/telemetry/combined') const data = await response.json() @@ -75,10 +57,29 @@ export default function CompactTelemetryDisplay() { } catch (error) { console.error('Error fetching telemetry:', error) } - } + }, []) const fetchCombinedTelemetry = fetchTelemetry + useEffect(() => { + if (selectedSandbox) { + fetchTelemetry() + } else { + fetchCombinedTelemetry() + } + }, [selectedSandbox, fetchTelemetry, fetchCombinedTelemetry]) + + useEffect(() => { + const interval = setInterval(() => { + if (selectedSandbox) { + fetchTelemetry() + } else { + fetchCombinedTelemetry() + } + }, 5000) + return () => clearInterval(interval) + }, [selectedSandbox, fetchTelemetry, fetchCombinedTelemetry]) + if (loading) { return (
diff --git a/app/components/ConfigurationPanel.tsx b/app/components/ConfigurationPanel.tsx index 52a3e63..bd376b2 100644 --- a/app/components/ConfigurationPanel.tsx +++ b/app/components/ConfigurationPanel.tsx @@ -8,97 +8,44 @@ type EndpointTls = "" | "terminate" | "passthrough" type EndpointEnforcement = "" | "enforce" | "audit" type EndpointAccess = "" | "read-only" | "read-write" | "full" -interface NetworkRule { - method: string - path: string -} - -interface NetworkEndpoint { - host: string - port: string - protocol: EndpointProtocol - tls: EndpointTls - enforcement: EndpointEnforcement - access: EndpointAccess - rules: NetworkRule[] -} - -interface NetworkBinary { - path: string -} - -interface NetworkPolicyBlock { - key: string - name: string - endpoints: NetworkEndpoint[] - binaries: NetworkBinary[] -} - +interface NetworkRule { method: string; path: string } +interface NetworkEndpoint { host: string; port: string; protocol: EndpointProtocol; tls: EndpointTls; enforcement: EndpointEnforcement; access: EndpointAccess; rules: NetworkRule[] } +interface NetworkBinary { path: string } +interface NetworkPolicyBlock { key: string; name: string; endpoints: NetworkEndpoint[]; binaries: NetworkBinary[] } type OpenShellPolicy = OpenShellPolicyShape +type BlueprintOption = { + id: string + label: string + description: string + type: "blueprint" | "custom" + source: string + supportsTailscale?: boolean +} + interface ConfigurationPanelProps { sandboxId: string mode?: 'existing' | 'create' + onCreateSuccess?: (sandboxId: string) => void | Promise + onInventoryRefresh?: () => Promise } function FieldHelp({ text }: { text: string }) { - return ( - - ? - - {text} - - - ) + return ?{text} } - function Badge({ children, tone }: { children: React.ReactNode; tone: "dynamic" | "static" | "danger" }) { - const cls = tone === "dynamic" - ? "bg-[rgba(118,185,0,0.12)] text-[var(--nvidia-green)] border-[rgba(118,185,0,0.35)]" - : tone === "danger" - ? "bg-[rgba(220,38,38,0.12)] text-red-400 border-[rgba(220,38,38,0.35)]" - : "bg-[rgba(245,158,11,0.10)] text-amber-400 border-[rgba(245,158,11,0.35)]" + const cls = tone === "dynamic" ? "bg-[rgba(118,185,0,0.12)] text-[var(--nvidia-green)] border-[rgba(118,185,0,0.35)]" : tone === "danger" ? "bg-[rgba(220,38,38,0.12)] text-red-400 border-[rgba(220,38,38,0.35)]" : "bg-[rgba(245,158,11,0.10)] text-amber-400 border-[rgba(245,158,11,0.35)]" return {children} } - -function TextListEditor({ label, tooltipText, value, onChange, placeholder }: { - label: string - tooltipText: string - value: string[] - onChange: (v: string[]) => void - placeholder: string -}) { - return ( -
- -