Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion scripts/backup-workspace.sh
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,37 @@ do_backup() {
info "Backup saved to ${dest}/ (${count} items)"
}

restore_directory() {
local sandbox="$1"
local src_dir="$2"
local dest_dir="$3"

openshell sandbox exec --name "$sandbox" -- mkdir -p "$dest_dir" >/dev/null \
|| return 1

local restored=0
local rel src_file target_dir
while IFS= read -r -d '' src_file; do
rel="${src_file#"${src_dir}/"}"
target_dir="${dest_dir}/$(dirname "$rel")"
openshell sandbox exec --name "$sandbox" -- mkdir -p "$target_dir" >/dev/null \
|| return 1
openshell sandbox upload "$sandbox" "$src_file" "$target_dir/" \
|| return 1
restored=1
done < <(find "$src_dir" -type f -print0)

# Preserve empty directories too. Directory uploads are unreliable in some
# OpenShell versions, so create the tree explicitly and upload files one by one.
while IFS= read -r -d '' src_file; do
rel="${src_file#"${src_dir}/"}"
openshell sandbox exec --name "$sandbox" -- mkdir -p "${dest_dir}/${rel}" >/dev/null \
|| return 1
done < <(find "$src_dir" -mindepth 1 -type d -print0)

return "$((1 - restored))"
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

do_restore() {
local sandbox="$1"
local ts="${2:-}"
Expand Down Expand Up @@ -108,7 +139,7 @@ do_restore() {

for d in "${DIRS[@]}"; do
if [ -d "${src}/${d}" ]; then
if openshell sandbox upload "$sandbox" "${src}/${d}/" "${WORKSPACE_PATH}/${d}/"; then
if restore_directory "$sandbox" "${src}/${d}" "${WORKSPACE_PATH}/${d}"; then
count=$((count + 1))
else
warn "Failed to restore ${d}/"
Expand Down
6 changes: 3 additions & 3 deletions src/lib/actions/sandbox/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function runSandboxConnectProbe(sandboxName: string): void {
process.exit(1);
}
if (processCheck.wasRunning) {
ensureSandboxInferenceRouteOrExit(sandboxName, { quiet: false });
ensureSandboxInferenceRoute(sandboxName, { quiet: true });
if (processCheck.forwardRecovered) {
console.log(
` Probe complete: ${agentName} gateway is running in '${sandboxName}'; restored dashboard port forward.`,
Expand All @@ -139,11 +139,11 @@ function runSandboxConnectProbe(sandboxName: string): void {
return;
}
if (processCheck.recovered) {
ensureSandboxInferenceRouteOrExit(sandboxName, { quiet: false });
ensureSandboxInferenceRoute(sandboxName, { quiet: true });
console.log(` Probe complete: recovered ${agentName} gateway in '${sandboxName}'.`);
return;
}
ensureSandboxInferenceRouteOrExit(sandboxName, { quiet: false });
ensureSandboxInferenceRoute(sandboxName, { quiet: true });
console.error(
` Probe failed: ${agentName} gateway is not running in '${sandboxName}' and automatic recovery failed.`,
);
Expand Down
15 changes: 6 additions & 9 deletions src/lib/onboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ const {
}: typeof import("./onboard/branding") = require("./onboard/branding");
const { cleanupTempDir }: typeof import("./onboard/temp-files") = require("./onboard/temp-files");
const { stopStaleDashboardListenersForSandbox } = require("./onboard/stale-gateway-cleanup");
const {
runBackgroundForwardStartWithDiagnostics,
}: typeof import("./onboard/forward-start") = require("./onboard/forward-start");
const { looksLikeForwardPortConflict, runBackgroundForwardStartWithPortReleaseRetries }: typeof import("./onboard/forward-start") = require("./onboard/forward-start");
const {
ensureOllamaLoopbackSystemdOverride,
}: typeof import("./onboard/ollama-systemd") = require("./onboard/ollama-systemd");
Expand Down Expand Up @@ -9303,17 +9301,16 @@ function ensureDashboardForward(
parsedUrl.port = String(actualPort);
const actualTarget = getDashboardForwardTarget(parsedUrl.toString());
runOpenshell(["forward", "stop", String(actualPort)], { ignoreError: true });
const { result: fwdResult, diagnostic: fwdDiagnostic } =
runBackgroundForwardStartWithDiagnostics((stdio, timeout) =>
const { result: fwdResult, diagnostic: fwdDiagnostic } = runBackgroundForwardStartWithPortReleaseRetries(
(stdio, timeout) =>
runOpenshell(
["forward", "start", "--background", actualTarget, sandboxName],
{ ignoreError: true, suppressOutput: true, stdio, timeout },
),
);
() => { sleep(1); runOpenshell(["forward", "stop", String(actualPort)], { ignoreError: true }); },
);
if (fwdResult && fwdResult.status !== 0) {
const looksLikePortConflict =
fwdDiagnostic === "" ||
/eaddrinuse|address already in use|port .* in use|bind: .*in use/i.test(fwdDiagnostic);
const looksLikePortConflict = looksLikeForwardPortConflict(fwdDiagnostic);
if (rollbackSandboxOnFailure) {
// The sandbox was just created, committed to actualPort via its
// baked-in CHAT_UI_URL and NEMOCLAW_DASHBOARD_PORT env. Silently
Expand Down
21 changes: 21 additions & 0 deletions src/lib/onboard/forward-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ function readDiagnosticFile(filePath: string): string {
}
}

export function looksLikeForwardPortConflict(diagnostic: string): boolean {
return diagnostic === "" || /eaddrinuse|address already in use|port .* in use|bind: .*in use/i.test(diagnostic);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

export function runBackgroundForwardStartWithDiagnostics(
runForwardStart: BackgroundForwardStartRunner,
timeoutMs = 30_000,
Expand Down Expand Up @@ -82,3 +86,20 @@ export function runBackgroundForwardStartWithDiagnostics(
cleanupTempDir(forwardDiagPath, "nemoclaw-forward-start");
}
}

export function runBackgroundForwardStartWithPortReleaseRetries(
runForwardStart: BackgroundForwardStartRunner,
beforeRetry: () => void,
maxRetries = 3,
): { result: BackgroundForwardStartResult; diagnostic: string } {
let attempt = runBackgroundForwardStartWithDiagnostics(runForwardStart);
for (
let retries = 0;
attempt.result.status !== 0 && looksLikeForwardPortConflict(attempt.diagnostic) && retries < maxRetries;
retries++
) {
beforeRetry();
attempt = runBackgroundForwardStartWithDiagnostics(runForwardStart);
}
return attempt;
}
48 changes: 24 additions & 24 deletions test/e2e/docs/parity-inventory.generated.json
Original file line number Diff line number Diff line change
Expand Up @@ -12121,23 +12121,23 @@
},
{
"script": "test/e2e/test-rebuild-hermes.sh",
"line": 383,
"line": 384,
"text": "No credentials in backup",
"polarity": "pass",
"normalized_id": "no.credentials.in.backup",
"mapping_status": "mapped"
},
{
"script": "test/e2e/test-rebuild-hermes.sh",
"line": 385,
"line": 386,
"text": "Credentials found: $CRED_LEAKS",
"polarity": "fail",
"normalized_id": "credentials.found.cred.leaks",
"mapping_status": "mapped"
},
{
"script": "test/e2e/test-rebuild-hermes.sh",
"line": 388,
"line": 389,
"text": "Backup directory missing: $BACKUP_DIR",
"polarity": "fail",
"normalized_id": "backup.directory.missing.backup.dir",
Expand Down Expand Up @@ -12398,103 +12398,103 @@
},
{
"script": "test/e2e/test-rebuild-openclaw.sh",
"line": 380,
"line": 381,
"text": "No credentials in backup",
"polarity": "pass",
"normalized_id": "no.credentials.in.backup",
"mapping_status": "mapped"
},
{
"script": "test/e2e/test-rebuild-openclaw.sh",
"line": 382,
"line": 383,
"text": "Credentials found: $CRED_LEAKS",
"polarity": "fail",
"normalized_id": "credentials.found.cred.leaks",
"mapping_status": "mapped"
},
{
"script": "test/e2e/test-rebuild-openclaw.sh",
"line": 385,
"line": 386,
"text": "Backup directory missing: $BACKUP_DIR",
"polarity": "fail",
"normalized_id": "backup.directory.missing.backup.dir",
"mapping_status": "deferred"
},
{
"script": "test/e2e/test-rebuild-openclaw.sh",
"line": 402,
"line": 403,
"text": "npm preset survived rebuild (in registry)",
"polarity": "pass",
"normalized_id": "npm.preset.survived.rebuild.in.registry",
"mapping_status": "deferred"
},
{
"script": "test/e2e/test-rebuild-openclaw.sh",
"line": 404,
"line": 405,
"text": "npm preset LOST after rebuild — issue #1952",
"polarity": "fail",
"normalized_id": "npm.preset.lost.after.rebuild.issue.1952",
"mapping_status": "deferred"
},
{
"script": "test/e2e/test-rebuild-openclaw.sh",
"line": 407,
"line": 408,
"text": "pypi preset survived rebuild (in registry)",
"polarity": "pass",
"normalized_id": "pypi.preset.survived.rebuild.in.registry",
"mapping_status": "deferred"
},
{
"script": "test/e2e/test-rebuild-openclaw.sh",
"line": 409,
"line": 410,
"text": "pypi preset LOST after rebuild — issue #1952",
"polarity": "fail",
"normalized_id": "pypi.preset.lost.after.rebuild.issue.1952",
"mapping_status": "deferred"
},
{
"script": "test/e2e/test-rebuild-openclaw.sh",
"line": 415,
"line": 416,
"text": "npm preset active in gateway policy after rebuild",
"polarity": "pass",
"normalized_id": "npm.preset.active.in.gateway.policy.after.rebuild",
"mapping_status": "deferred"
},
{
"script": "test/e2e/test-rebuild-openclaw.sh",
"line": 417,
"line": 418,
"text": "npm preset not in live gateway policy after rebuild — issue #1952",
"polarity": "fail",
"normalized_id": "npm.preset.not.in.live.gateway.policy.after.rebuild.issue.1952",
"mapping_status": "deferred"
},
{
"script": "test/e2e/test-rebuild-openclaw.sh",
"line": 420,
"line": 421,
"text": "pypi preset active in gateway policy after rebuild",
"polarity": "pass",
"normalized_id": "pypi.preset.active.in.gateway.policy.after.rebuild",
"mapping_status": "deferred"
},
{
"script": "test/e2e/test-rebuild-openclaw.sh",
"line": 422,
"line": 423,
"text": "pypi preset not in live gateway policy after rebuild — issue #1952",
"polarity": "fail",
"normalized_id": "pypi.preset.not.in.live.gateway.policy.after.rebuild.issue.1952",
"mapping_status": "deferred"
},
{
"script": "test/e2e/test-rebuild-openclaw.sh",
"line": 441,
"line": 442,
"text": "Backup manifest contains policyPresets: ${MANIFEST_PRESETS}",
"polarity": "pass",
"normalized_id": "backup.manifest.contains.policypresets.manifest.presets",
"mapping_status": "deferred"
},
{
"script": "test/e2e/test-rebuild-openclaw.sh",
"line": 443,
"line": 444,
"text": "Backup manifest missing expected policyPresets (npm,pypi): got '${MANIFEST_PRESETS}' — issue #1952",
"polarity": "fail",
"normalized_id": "backup.manifest.missing.expected.policypresets.npm.pypi.got.manifest.presets.issue.1952",
Expand Down Expand Up @@ -13221,15 +13221,15 @@
},
{
"script": "test/e2e/test-sandbox-rebuild.sh",
"line": 184,
"line": 185,
"text": "No credentials found in backup directory",
"polarity": "pass",
"normalized_id": "no.credentials.found.in.backup.directory",
"mapping_status": "mapped"
},
{
"script": "test/e2e/test-sandbox-rebuild.sh",
"line": 186,
"line": 187,
"text": "Credentials found in backup files: $CRED_LEAKS",
"polarity": "fail",
"normalized_id": "credentials.found.in.backup.files.cred.leaks",
Expand Down Expand Up @@ -14905,47 +14905,47 @@
},
{
"script": "test/e2e/test-snapshot-commands.sh",
"line": 253,
"line": 254,
"text": "No credentials in snapshot directories",
"polarity": "pass",
"normalized_id": "no.credentials.in.snapshot.directories",
"mapping_status": "mapped"
},
{
"script": "test/e2e/test-snapshot-commands.sh",
"line": 255,
"line": 256,
"text": "Credentials found: $CRED_LEAKS",
"polarity": "fail",
"normalized_id": "credentials.found.cred.leaks",
"mapping_status": "mapped"
},
{
"script": "test/e2e/test-snapshot-commands.sh",
"line": 258,
"line": 259,
"text": "Backup directory missing: $BACKUP_DIR",
"polarity": "fail",
"normalized_id": "backup.directory.missing.backup.dir",
"mapping_status": "deferred"
},
{
"script": "test/e2e/test-snapshot-commands.sh",
"line": 266,
"line": 267,
"text": "snapshot help exited with code $_CAPTURE_RC: ${HELP_OUTPUT}",
"polarity": "fail",
"normalized_id": "snapshot.help.exited.with.code.capture.rc.help.output",
"mapping_status": "deferred"
},
{
"script": "test/e2e/test-snapshot-commands.sh",
"line": 271,
"line": 272,
"text": "snapshot help shows create/list/restore",
"polarity": "pass",
"normalized_id": "snapshot.help.shows.create.list.restore",
"mapping_status": "deferred"
},
{
"script": "test/e2e/test-snapshot-commands.sh",
"line": 273,
"line": 274,
"text": "snapshot help incomplete: ${HELP_OUTPUT}",
"polarity": "fail",
"normalized_id": "snapshot.help.incomplete.help.output",
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/test-rebuild-hermes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,8 @@ fi
# No credentials in backup
BACKUP_DIR="$HOME/.nemoclaw/rebuild-backups/${SANDBOX_NAME}"
if [ -d "$BACKUP_DIR" ]; then
CRED_LEAKS=$(find "$BACKUP_DIR" \( -name "*.json" -o -name "*.yaml" -o -name "*.env" -o -name ".env" \) -exec grep -l "nvapi-\|sk-\|Bearer " {} \; 2>/dev/null || true)
CRED_PATTERN="nvapi-[[:alnum:]_-]{16,}|sk-[[:alnum:]_-]{16,}|Bearer[[:space:]]+[[:alnum:]._%+/=-]{16,}"
CRED_LEAKS=$(find "$BACKUP_DIR" \( -name "*.json" -o -name "*.yaml" -o -name "*.env" -o -name ".env" \) -exec grep -El "$CRED_PATTERN" {} \; 2>/dev/null || true)
if [ -z "$CRED_LEAKS" ]; then
pass "No credentials in backup"
else
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/test-rebuild-openclaw.sh
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,8 @@ fi
# No credentials in backup
BACKUP_DIR="$HOME/.nemoclaw/rebuild-backups/${SANDBOX_NAME}"
if [ -d "$BACKUP_DIR" ]; then
CRED_LEAKS=$(find "$BACKUP_DIR" \( -name "*.json" -o -name "*.env" -o -name ".env" \) -exec grep -l "nvapi-\|sk-\|Bearer " {} \; 2>/dev/null || true)
CRED_PATTERN="nvapi-[[:alnum:]_-]{16,}|sk-[[:alnum:]_-]{16,}|Bearer[[:space:]]+[[:alnum:]._%+/=-]{16,}"
CRED_LEAKS=$(find "$BACKUP_DIR" \( -name "*.json" -o -name "*.env" -o -name ".env" \) -exec grep -El "$CRED_PATTERN" {} \; 2>/dev/null || true)
if [ -z "$CRED_LEAKS" ]; then
pass "No credentials in backup"
else
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/test-sandbox-rebuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ info "Step 8: Checking backup directory for leaked credentials..."
BACKUP_DIR="$HOME/.nemoclaw/rebuild-backups/$SANDBOX_NAME"
if [ -d "$BACKUP_DIR" ]; then
# Search for common credential patterns in JSON files
CRED_LEAKS=$(find "$BACKUP_DIR" -name "*.json" -exec grep -l "nvapi-\|sk-\|Bearer " {} \; 2>/dev/null || true)
CRED_PATTERN="nvapi-[[:alnum:]_-]{16,}|sk-[[:alnum:]_-]{16,}|Bearer[[:space:]]+[[:alnum:]._%+/=-]{16,}"
CRED_LEAKS=$(find "$BACKUP_DIR" -name "*.json" -exec grep -El "$CRED_PATTERN" {} \; 2>/dev/null || true)
if [ -z "$CRED_LEAKS" ]; then
pass "No credentials found in backup directory"
else
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/test-snapshot-commands.sh
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ info "Phase 8: Checking snapshots for leaked credentials..."

BACKUP_DIR="$HOME/.nemoclaw/rebuild-backups/${SANDBOX_NAME}"
if [ -d "$BACKUP_DIR" ]; then
CRED_LEAKS=$(find "$BACKUP_DIR" \( -name "*.json" -o -name "*.env" -o -name ".env" \) -exec grep -l "nvapi-\|sk-\|Bearer " {} \; 2>/dev/null || true)
CRED_PATTERN="nvapi-[[:alnum:]_-]{16,}|sk-[[:alnum:]_-]{16,}|Bearer[[:space:]]+[[:alnum:]._%+/=-]{16,}"
CRED_LEAKS=$(find "$BACKUP_DIR" \( -name "*.json" -o -name "*.env" -o -name ".env" \) -exec grep -El "$CRED_PATTERN" {} \; 2>/dev/null || true)
if [ -z "$CRED_LEAKS" ]; then
pass "No credentials in snapshot directories"
else
Expand Down
Loading
Loading