diff --git a/docs/manage-sandboxes/uninstall-nemoclaw.mdx b/docs/manage-sandboxes/uninstall-nemoclaw.mdx index 50d31e7a13c..aa0c4743575 100644 --- a/docs/manage-sandboxes/uninstall-nemoclaw.mdx +++ b/docs/manage-sandboxes/uninstall-nemoclaw.mdx @@ -70,6 +70,13 @@ Resolve the reported error, inspect the remaining gateways with `openshell gatew Default-port uninstall removes NemoClaw-managed entries in `openshell/gateway.env`. For a NemoClaw-managed authority, it also removes only NemoClaw's marked Linux gateway unit. It preserves upstream Linux package units, the macOS Homebrew service, and unrelated environment entries. +Gateway-scoped cleanup removes that gateway's OpenShell resources first, then the marked Linux unit. +The OpenShell gateway service therefore keeps running while uninstall deletes the selected gateway's sandboxes. +If that OpenShell cleanup fails, uninstall exits nonzero and preserves the marked Linux unit and the gateway process. +That cleanup can partially complete before the failure. +Resolve the reported error. +Inspect the remaining gateways with `openshell gateway list`. +Rerun `NEMOCLAW_GATEWAY_PORT= $$nemoclaw uninstall` with the gateway port from the failed uninstall. For an externally supervised authority, uninstall preserves the local gateway state used by the running process in both full and gateway-scoped cleanup. It also preserves the gateway process, supervisor resources, marked Linux unit, Docker resources, OpenShell binaries, and the declared external state directory. A custom-port uninstall does not stop or remove the default gateway service or its environment file. diff --git a/docs/reference/commands.mdx b/docs/reference/commands.mdx index 580f026c341..e1a5aebfb5f 100644 --- a/docs/reference/commands.mdx +++ b/docs/reference/commands.mdx @@ -3654,6 +3654,13 @@ The compatibility `--gateway` flag cannot select another instance: when present, Default-port uninstall removes NemoClaw-managed entries in `openshell/gateway.env`. For a NemoClaw-managed authority, it also removes only NemoClaw's marked Linux gateway unit. It preserves upstream Linux package units, the macOS Homebrew service, and unrelated environment entries. +Gateway-scoped cleanup removes that gateway's OpenShell resources first, then the marked Linux unit. +The OpenShell gateway service therefore keeps running while uninstall deletes the selected gateway's sandboxes. +If that OpenShell cleanup fails, uninstall exits nonzero and preserves the marked Linux unit and the gateway process. +That cleanup can partially complete before the failure. +Resolve the reported error. +Inspect the remaining gateways with `openshell gateway list`. +Rerun `NEMOCLAW_GATEWAY_PORT= $$nemoclaw uninstall` with the gateway port from the failed uninstall. For an externally supervised authority, uninstall preserves the selected local gateway state in both full and gateway-scoped cleanup. It also preserves the gateway process, supervisor resources, marked Linux unit, Docker resources, OpenShell binaries, and the declared external state directory. A custom-port uninstall does not stop or remove the default gateway service or its environment file. diff --git a/src/lib/actions/uninstall/run-plan-gateway-service.test.ts b/src/lib/actions/uninstall/run-plan-gateway-service.test.ts index 43aa24fe325..53b66b9549d 100644 --- a/src/lib/actions/uninstall/run-plan-gateway-service.test.ts +++ b/src/lib/actions/uninstall/run-plan-gateway-service.test.ts @@ -68,6 +68,20 @@ function writeGatewayEnv(test: Fixture, contents = "OPENSHELL_SERVER_PORT=8080\n return envPath; } +function writeSelectedSandboxRegistry(test: Fixture, sandboxName: string): void { + const registryPath = path.join(test.home, ".nemoclaw", "sandboxes.json"); + fs.mkdirSync(path.dirname(registryPath), { recursive: true }); + fs.writeFileSync( + registryPath, + `${JSON.stringify({ + defaultSandbox: sandboxName, + sandboxes: { + [sandboxName]: { name: sandboxName, gatewayName: "nemoclaw", gatewayPort: 8080 }, + }, + })}\n`, + ); +} + function writeGatewayState(test: Fixture): string { const configPath = path.join( test.home, @@ -184,6 +198,109 @@ describe("uninstall OpenShell gateway user service", () => { expect(fs.existsSync(gatewayStatePath)).toBe(true); }); + it("deletes the selected sandbox before it disables the marked Linux unit on scoped uninstall (#8220)", () => { + const test = fixture(true); + const servicePath = writeManagedService(test); + writeSelectedSandboxRegistry(test, "my-assistant"); + const calls: string[][] = []; + const dockerCalls: string[][] = []; + let gatewayStopped = false; + + const result = uninstall( + test, + false, + { + commandExists: (command) => command === "systemctl" || command === "docker", + run: (command, args) => { + calls.push([command, ...args]); + gatewayStopped ||= command === "systemctl" && args.includes("disable"); + // `systemctl disable --now` also stops the OpenShell gateway service, + // so every scoped `openshell` call fails once the unit is disabled. + return command === "openshell" && gatewayStopped + ? { status: 1, stdout: "", stderr: "gateway unreachable" } + : ok(); + }, + runDocker: (args) => { + dockerCalls.push(args); + return args[0] === "ps" + ? ok("sandbox-id openshell/sandbox openshell-cluster-nemoclaw\n") + : ok(); + }, + }, + [{ name: "nemoclaw" }, { name: "nemoclaw-8081" }], + ); + + const deletedAt = calls.findIndex( + (call) => call[0] === "openshell" && call[1] === "sandbox" && call[2] === "delete", + ); + const disabledAt = calls.findIndex( + (call) => call[0] === "systemctl" && call.includes("disable"), + ); + + expect(result.exitCode).toBe(0); + expect(deletedAt).toBeGreaterThanOrEqual(0); + expect(disabledAt).toBeGreaterThan(deletedAt); + expect(dockerCalls).toContainEqual(["rm", "-f", "sandbox-id"]); + expect(fs.existsSync(servicePath)).toBe(false); + }); + + it("preserves the marked Linux unit when scoped sandbox deletion fails (#8220)", () => { + const test = fixture(true); + const servicePath = writeManagedService(test); + writeSelectedSandboxRegistry(test, "my-assistant"); + const calls: string[][] = []; + + const result = uninstall( + test, + false, + { + commandExists: (command) => command === "systemctl", + run: (command, args) => { + calls.push([command, ...args]); + return command === "openshell" && args[0] === "sandbox" + ? { status: 1, stdout: "", stderr: "sandbox unreachable" } + : ok(); + }, + }, + [{ name: "nemoclaw" }, { name: "nemoclaw-8081" }], + ); + + // Sandbox deletion failed, so uninstall returns before it removes the gateway registration. + // It preserves the marked Linux unit and the running OpenShell gateway service for a retry. + expect(result.exitCode).toBe(1); + expect(fs.existsSync(servicePath)).toBe(true); + expect(calls.some((call) => call[0] === "systemctl" && call.includes("disable"))).toBe(false); + }); + + it("preserves the marked Linux unit when scoped gateway registration removal fails (#8220)", () => { + const test = fixture(true); + const servicePath = writeManagedService(test); + writeSelectedSandboxRegistry(test, "my-assistant"); + const calls: string[][] = []; + + const result = uninstall( + test, + false, + { + commandExists: (command) => command === "systemctl", + run: (command, args) => { + calls.push([command, ...args]); + return command === "openshell" && args[0] === "gateway" && args[1] === "remove" + ? { status: 1, stdout: "", stderr: "gateway registration is busy" } + : ok(); + }, + }, + [{ name: "nemoclaw" }, { name: "nemoclaw-8081" }], + ); + + // Sandbox deletion succeeded, so this pins the second cleanup boundary: registration + // removal failed, and uninstall still returns before it removes the gateway service. + expect(calls).toContainEqual(["openshell", "sandbox", "delete", "my-assistant"]); + expect(result.exitCode).toBe(1); + expect(fs.existsSync(servicePath)).toBe(true); + expect(calls.some((call) => call[0] === "systemctl" && call.includes("disable"))).toBe(false); + }); + it("removes only the marked Linux unit and managed env on full uninstall (#6903)", () => { const test = fixture(true); const servicePath = writeManagedService(test); diff --git a/src/lib/actions/uninstall/run-plan.ts b/src/lib/actions/uninstall/run-plan.ts index d5725080f55..eb4f353b90f 100644 --- a/src/lib/actions/uninstall/run-plan.ts +++ b/src/lib/actions/uninstall/run-plan.ts @@ -989,6 +989,20 @@ function removeNemoclawOpenShellGatewayUserService(runtime: UninstallRuntime): b return true; } +// scripts/install.sh stages the NemoClaw-managed gateway user service only for +// the default gateway port, so an uninstall run for any other port leaves it in +// place. `--keep-openshell` and an externally supervised authority leave it in +// place too. +function removeManagedDefaultGatewayUserService( + runtime: UninstallRuntime, + options: UninstallRunOptions, + externallySupervised: boolean, +): boolean { + return options.keepOpenShell || externallySupervised || GATEWAY_PORT !== DEFAULT_GATEWAY_PORT + ? true + : removeNemoclawOpenShellGatewayUserService(runtime); +} + function removeNemoclawOpenShellGatewayEnv( paths: UninstallPaths, runtime: UninstallRuntime, @@ -1799,12 +1813,14 @@ function executePlan( if (!scopedToSelectedGateway && !removeManagedDistributedVllmRuntime(paths, runtime)) { return { ok: false }; } + // #8220: a gateway-scoped uninstall still needs the selected OpenShell + // gateway service running to delete its sandbox, so "OpenShell resources" + // removes that unit after the sandbox delete succeeds. if ( - !options.keepOpenShell && - !externallySupervised && - GATEWAY_PORT === DEFAULT_GATEWAY_PORT + !scopedToSelectedGateway && + !removeManagedDefaultGatewayUserService(runtime, options, externallySupervised) ) { - if (!removeNemoclawOpenShellGatewayUserService(runtime)) ok = false; + ok = false; } if (!scopedToSelectedGateway) { stopHelperServices(paths, runtime); @@ -1874,6 +1890,9 @@ function executePlan( return { ok: false }; } if (scopedToSelectedGateway && !options.keepOpenShell && !externallySupervised) { + if (!removeManagedDefaultGatewayUserService(runtime, options, externallySupervised)) { + ok = false; + } stopHostGatewayProcessesForUninstall(runtime, { gatewayBin: runtime.env.NEMOCLAW_OPENSHELL_GATEWAY_BIN, logNoProcesses: true,