Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
103 changes: 84 additions & 19 deletions src/lib/state/legacy-port-migration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ import path from "node:path";

import { afterEach, describe, expect, it, vi } from "vitest";

import {
type OnboardEntryOptionsDeps,
resolveOnboardEntryOptions,
} from "../onboard/entry-options";
const { observeOnboardLock } = vi.hoisted(() => ({
observeOnboardLock: vi.fn<(lockPath: string) => OnboardLockObservation>(() => ({
kind: "absent",
})),
}));
vi.mock("./onboard-session/lock-observation", () => ({ observeOnboardLock }));

import { type OnboardEntryOptionsDeps, resolveOnboardEntryOptions } from "../onboard/entry-options";
import { migrateLegacyPortState } from "./legacy-port-migration";
import type { OnboardLockObservation } from "./onboard-session/lock-observation";
import {
listRetainedSandboxRecoveryRecords,
recordRetainedSandboxRecovery,
Expand Down Expand Up @@ -87,6 +92,8 @@ function expectRetainedNameBlocked(
}

afterEach(() => {
observeOnboardLock.mockReset();
observeOnboardLock.mockReturnValue({ kind: "absent" });
vi.restoreAllMocks();
for (const home of homes.splice(0)) fs.rmSync(home, { recursive: true, force: true });
});
Expand Down Expand Up @@ -428,41 +435,99 @@ describe("legacy non-default gateway state migration", () => {
fs.mkdirSync(root(shared, selected), { recursive: true });
fs.writeFileSync(path.join(root(shared, selected), "onboard.lock"), "active writer");

observeOnboardLock.mockImplementation((lockPath: string): OnboardLockObservation =>
fs.existsSync(lockPath) ? { kind: "busy", reason: "unverified" } : { kind: "absent" },
);
expect(() => migrateLegacyPortState({ home, gatewayPort: 9123 })).toThrow(
/onboarding lock .* is present/u,
new RegExp(
`onboarding lock .* is unverified; confirm no NemoClaw onboarding process .* then remove only ${path.join(root(shared, selected), "onboard.lock")} and retry; migration will not remove it automatically`,
"u",
),
);
expect(fs.readFileSync(recoveryFile, "utf8")).toBe(before);
expect(fs.existsSync(path.join(selected, "retained-sandbox-recovery.json"))).toBe(false);
});

it.each(
["ollama-proxy-token", "ollama-proxy-port", "ollama-auth-proxy.pid"],
)("keeps host-shared Ollama proxy state out of a non-default gateway migration [%s]", (entry) => {
it("migrates past a proven-stale onboarding lock without removing it", () => {
const home = makeHome();
const shared = path.join(home, ".nemoclaw");
const selected = path.join(shared, "gateways", "9123");
const lock = path.join(shared, "onboard.lock");
writeJson(path.join(shared, "sandboxes.json"), {
defaultSandbox: "port-box",
sandboxes: {
"port-box": { name: "port-box", gatewayName: "nemoclaw-9123", gatewayPort: 9123 },
},
});
writeJson(path.join(shared, "credentials.json"), { NVIDIA_API_KEY: "selected-secret" });
fs.writeFileSync(path.join(shared, "ollama-proxy-token"), "host-token\n");
fs.writeFileSync(path.join(shared, "ollama-proxy-port"), "11435\n");
fs.writeFileSync(path.join(shared, "ollama-auth-proxy.pid"), "4242\n");

const result = migrateLegacyPortState({ home, gatewayPort: 9123 });
writeJson(path.join(shared, "onboard-session.json"), {
sandboxName: "port-box",
metadata: { gatewayName: "nemoclaw-9123" },
});
fs.writeFileSync(lock, "retained stale lock");
observeOnboardLock.mockReturnValue({
kind: "stale",
reason: "departed",
owner: {
pid: 123,
startedAt: null,
command: null,
processGeneration: "boot:10",
hostIdentity: "host-a",
pidNamespaceIdentity: "pid:[1]",
},
});

expect(result.warnings).toEqual([]);
expect(migrateLegacyPortState({ home, gatewayPort: 9123 }).migratedSession).toBe(true);
expect(fs.readFileSync(lock, "utf8")).toBe("retained stale lock");
expect(fs.existsSync(path.join(selected, "onboard-session.json"))).toBe(true);
});

expect(fs.existsSync(path.join(shared, entry))).toBe(true);
expect(fs.existsSync(path.join(selected, entry))).toBe(false);
it("refuses an onboarding lock below a symbolic-link state root", () => {
const home = makeHome();
const shared = path.join(home, ".nemoclaw");
const selected = path.join(shared, "gateways", "9123");
const outside = path.join(home, "outside");
recordRecovery(path.join(shared, "retained-sandbox-recovery.json"), "port-box", 9123, "d");
fs.mkdirSync(path.dirname(selected), { recursive: true });
fs.mkdirSync(outside);
fs.writeFileSync(path.join(outside, "onboard.lock"), "outside lock");
fs.symlinkSync(outside, selected, "dir");

expect(fs.existsSync(path.join(shared, "credentials.json"))).toBe(false);
expect(fs.existsSync(path.join(selected, "credentials.json"))).toBe(true);
expect(() => migrateLegacyPortState({ home, gatewayPort: 9123 })).toThrow(/symbolic link/);
expect(observeOnboardLock).toHaveBeenCalledOnce();
expect(observeOnboardLock).toHaveBeenCalledWith(path.join(shared, "onboard.lock"));
expect(fs.readFileSync(path.join(outside, "onboard.lock"), "utf8")).toBe("outside lock");
});

it.each(["ollama-proxy-token", "ollama-proxy-port", "ollama-auth-proxy.pid"])(
"keeps host-shared Ollama proxy state out of a non-default gateway migration [%s]",
(entry) => {
const home = makeHome();
const shared = path.join(home, ".nemoclaw");
const selected = path.join(shared, "gateways", "9123");
writeJson(path.join(shared, "sandboxes.json"), {
defaultSandbox: "port-box",
sandboxes: {
"port-box": { name: "port-box", gatewayName: "nemoclaw-9123", gatewayPort: 9123 },
},
});
writeJson(path.join(shared, "credentials.json"), { NVIDIA_API_KEY: "selected-secret" });
fs.writeFileSync(path.join(shared, "ollama-proxy-token"), "host-token\n");
fs.writeFileSync(path.join(shared, "ollama-proxy-port"), "11435\n");
fs.writeFileSync(path.join(shared, "ollama-auth-proxy.pid"), "4242\n");

const result = migrateLegacyPortState({ home, gatewayPort: 9123 });

expect(result.warnings).toEqual([]);

expect(fs.existsSync(path.join(shared, entry))).toBe(true);
expect(fs.existsSync(path.join(selected, entry))).toBe(false);

expect(fs.existsSync(path.join(shared, "credentials.json"))).toBe(false);
expect(fs.existsSync(path.join(selected, "credentials.json"))).toBe(true);
},
);

it.each([8080, 9123])(
"removes only generated stale migration-intent directories for gateway port %i",
(gatewayPort) => {
Expand Down
12 changes: 10 additions & 2 deletions src/lib/state/legacy-port-migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
readGatewayRegistryFile,
registryEntryGatewayPort,
} from "./gateway-registry";
import { observeOnboardLock } from "./onboard-session/lock-observation";
import {
listRetainedSandboxRecoveryRecords,
retainedSandboxRecoveryFile,
Expand Down Expand Up @@ -745,9 +746,16 @@ function acquireDirectoryLock(home: string, lock: string): string {
function assertOnboardStateUnlocked(home: string, stateRoots: readonly string[]): void {
for (const stateRoot of stateRoots) {
const activeLock = path.join(stateRoot, "onboard.lock");
if (lstatNoFollow(home, activeLock)) {
assertGatewayStatePathSafe(home, activeLock);
const lock = observeOnboardLock(activeLock);
Comment thread
prekshivyas marked this conversation as resolved.
Resolved
if (lock.kind === "busy") {
const owner = lock.owner ? ` recorded for PID ${String(lock.owner.pid)}` : "";
const recovery =
lock.reason === "unverified"
? `confirm no NemoClaw onboarding process in any environment sharing this state root is active, then remove only ${activeLock} and retry; migration will not remove it automatically`
: "finish that run or verify the lock owner before retrying";
throw migrationError(
`onboarding lock ${activeLock} is present; finish or stop that run before migrating state`,
`onboarding lock ${activeLock}${owner} is ${lock.reason}; ${recovery}`,
);
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/lib/state/onboard-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import {
} from "./onboard-session-tool-disclosure";
import { nextMachineStateAfterCompletedStep } from "./onboard-step-state";
import {
createOnboardLockOwner,
listRetainedSandboxRecoveryRecords as readRetainedSandboxRecoveryRecords,
recordRetainedSandboxRecovery as writeRetainedSandboxRecovery,
retainedSandboxRecoveryAuthorityIsCurrent,
Expand All @@ -69,7 +70,7 @@ import {
type RecordRetainedSandboxRecoveryInput,
type RetainedSandboxRecoveryRecord,
type RetainedSandboxRecoveryReason,
} from "./onboard-session/retained-sandbox-recovery";
} from "./onboard-session/index";
import type { SandboxHostMount } from "./registry/types";
import { hasUnsafeHostMountTerminalText } from "./registry/host-mount";
import { nemoclawStateRoot } from "./state-root";
Expand Down Expand Up @@ -1489,7 +1490,7 @@ export function isOnboardLockHeldByCurrentProcess(): boolean {
export function acquireOnboardLock(command: string | null = null): LockResult {
ensureSessionDir();
const payload = JSON.stringify(
{
createOnboardLockOwner(typeof command === "string" ? command : null) ?? {
pid: process.pid,
startedAt: new Date().toISOString(),
command: typeof command === "string" ? command : null,
Expand Down
16 changes: 16 additions & 0 deletions src/lib/state/onboard-session/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

export { createOnboardLockOwner } from "./lock-observation";
export {
listRetainedSandboxRecoveryRecords,
recordRetainedSandboxRecovery,
retainedSandboxRecoveryAuthorityIsCurrent,
retainedSandboxRecoveryFile,
resolveRetainedSandboxRecovery,
} from "./retained-sandbox-recovery";
export type {
RecordRetainedSandboxRecoveryInput,
RetainedSandboxRecoveryRecord,
RetainedSandboxRecoveryReason,
} from "./retained-sandbox-recovery";
Loading
Loading