Skip to content

Commit 69403e0

Browse files
fix(cli): stop in-sandbox messaging channels on nemoclaw stop (#1977)
<!-- markdownlint-disable MD041 --> ## Summary `nemoclaw stop` only killed the host-side `cloudflared` process but left the OpenClaw gateway (and its Telegram/Discord/Slack messaging channels) running inside the sandbox. After PR #1081 moved bridges into the sandbox, no stop mechanism was added. This PR adds `stopSandboxChannels()` which execs into the sandbox via `openshell sandbox exec` to SIGTERM the gateway process, stopping all messaging channel polling. ## Related Issue Fixes #1825 ## Changes - Add `stopSandboxChannels()` to `src/lib/services.ts` — resolves `openshell`, execs `pkill -TERM -f "openclaw gateway"` inside the sandbox, and handles all pkill exit codes (0 = stopped, 1 = already stopped, >1 = unreachable) - Update `stopAll()` to call `stopSandboxChannels()` before stopping host-side services; reads sandbox name from opts, `NEMOCLAW_SANDBOX`, or `SANDBOX_NAME` env vars; warns when no sandbox name is available - Import `spawnSync` from `node:child_process` and `resolveOpenshell` from `./resolve-openshell` - Add `src/lib/services-sandbox.test.ts` with 9 tests covering: successful gateway stop, pkill exit 1 (no match), unreachable sandbox, null status (timeout), openshell not found, stopAll with sandbox name, stopAll without sandbox name, cloudflared cleanup on exec failure, and env var fallback ## Type of Change - [x] Code change (feature, bug fix, or refactor) - [ ] Code change with doc updates - [ ] Doc only (prose changes, no code sample modifications) - [ ] Doc only (includes code sample changes) ## Verification - [ ] `npx prek run --all-files` passes - [x] `npm test` passes - [x] Tests added or updated for new or changed behavior - [x] No secrets, API keys, or credentials committed - [ ] Docs updated for user-facing behavior changes - [ ] `make docs` builds without warnings (doc changes only) - [ ] Doc pages follow the [style guide](https://github.com/NVIDIA/NemoClaw/blob/main/docs/CONTRIBUTING.md) (doc changes only) - [ ] New doc pages include SPDX header and frontmatter (new pages only) ## AI Disclosure - [x] AI-assisted — tool: Claude Code --- Signed-off-by: Brandon Pelfrey <[email protected]> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Introduced sandbox-aware shutdown flow: attempts in-sandbox gateway cleanup when a validated sandbox name is available, prefers validated env-provided sandbox names (with correct precedence), and logs clearer success/warning outcomes for varied exit statuses and missing in-sandbox tooling. * **Tests** * Added tests for sandbox shutdown paths, missing/failed in-sandbox tooling, env-var vs registry name selection and validation (rejecting unsafe names), and ensuring host-side cleanup still runs. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: Brandon Pelfrey <[email protected]> Co-authored-by: Aaron Erickson 🦞 <[email protected]>
1 parent defeffc commit 69403e0

4 files changed

Lines changed: 686 additions & 4 deletions

File tree

‎src/lib/services-command.test.ts‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
import { describe, expect, it, vi } from "vitest";
4+
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
55

66
import {
77
resolveDefaultSandboxName,
@@ -10,6 +10,29 @@ import {
1010
} from "../../dist/lib/services-command";
1111

1212
describe("services command", () => {
13+
let savedEnv: Record<string, string | undefined>;
14+
15+
beforeEach(() => {
16+
savedEnv = {
17+
NEMOCLAW_SANDBOX_NAME: process.env.NEMOCLAW_SANDBOX_NAME,
18+
NEMOCLAW_SANDBOX: process.env.NEMOCLAW_SANDBOX,
19+
SANDBOX_NAME: process.env.SANDBOX_NAME,
20+
};
21+
delete process.env.NEMOCLAW_SANDBOX_NAME;
22+
delete process.env.NEMOCLAW_SANDBOX;
23+
delete process.env.SANDBOX_NAME;
24+
});
25+
26+
afterEach(() => {
27+
for (const [key, val] of Object.entries(savedEnv)) {
28+
if (val !== undefined) {
29+
process.env[key] = val;
30+
} else {
31+
delete process.env[key];
32+
}
33+
}
34+
});
35+
1336
it("returns a safe default sandbox name", () => {
1437
expect(resolveDefaultSandboxName(() => ({ defaultSandbox: "alpha-1" }))).toBe("alpha-1");
1538
});
@@ -21,6 +44,21 @@ describe("services command", () => {
2144
expect(resolveDefaultSandboxName(() => ({ defaultSandbox: "-leading-dash" }))).toBeUndefined();
2245
});
2346

47+
it("prefers NEMOCLAW_SANDBOX_NAME env var over registry default", () => {
48+
process.env.NEMOCLAW_SANDBOX_NAME = "env-sandbox";
49+
expect(resolveDefaultSandboxName(() => ({ defaultSandbox: "registry-sandbox" }))).toBe("env-sandbox");
50+
});
51+
52+
it("prefers NEMOCLAW_SANDBOX env var over registry default", () => {
53+
process.env.NEMOCLAW_SANDBOX = "env-sandbox-2";
54+
expect(resolveDefaultSandboxName(() => ({ defaultSandbox: "registry-sandbox" }))).toBe("env-sandbox-2");
55+
});
56+
57+
it("ignores unsafe env var values and falls back to registry", () => {
58+
process.env.NEMOCLAW_SANDBOX_NAME = "bad name";
59+
expect(resolveDefaultSandboxName(() => ({ defaultSandbox: "registry-sandbox" }))).toBe("registry-sandbox");
60+
});
61+
2462
it("starts services for the default sandbox when present", async () => {
2563
const startAll = vi.fn(async () => {});
2664
await runStartCommand({

‎src/lib/services-command.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ export interface StopCommandDeps {
1818
const SAFE_SANDBOX_RE = /^[a-zA-Z0-9][a-zA-Z0-9._-]*$/;
1919

2020
export function resolveDefaultSandboxName(listSandboxes: () => SandboxSummary): string | undefined {
21+
// Explicit env var overrides take highest priority so that
22+
// `NEMOCLAW_SANDBOX_NAME=foo nemoclaw stop` targets the right sandbox.
23+
const envName =
24+
process.env.NEMOCLAW_SANDBOX_NAME ??
25+
process.env.NEMOCLAW_SANDBOX ??
26+
process.env.SANDBOX_NAME;
27+
if (envName && SAFE_SANDBOX_RE.test(envName)) return envName;
28+
2129
const { defaultSandbox } = listSandboxes();
2230
return defaultSandbox && SAFE_SANDBOX_RE.test(defaultSandbox) ? defaultSandbox : undefined;
2331
}

0 commit comments

Comments
 (0)