NemoClaw icon indicating copy to clipboard operation
NemoClaw copied to clipboard

[All Platforms][Automation] src/lib/sandbox/version.test.ts writes 7 sandboxes into the real ~/.nemoclaw/sandboxes.json, breaking subsequent onboard

Open Hokonoken opened this issue 4 months ago • 0 comments

Description

src/lib/sandbox/version.test.ts intends to isolate itself by pointing HOME at a temp dir, but the redirection is a no-op: registry.ts captures the registry path at module import time, and the test reassigns HOME in beforeEach, which runs after the import. Every registry.registerSandbox() in that file therefore writes to the real ~/.nemoclaw/sandboxes.json of whoever runs the suite.

This is not cosmetic. The seven entries it leaves behind have no provider and no model, so the fail-closed inference-route containment added in #6338 refuses to onboard any new sandbox on that gateway until they are removed by hand.

The repo already documents the constraint, in test/registry.test.ts:11-12:

// Use a temp dir so tests don't touch real ~/.nemoclaw.
// HOME must be set before loading registry (it reads HOME at require time),
// so we use createRequire instead of a static import.

To find how far this spreads, I took every test file that both reassigns process.env.HOME and reaches state/registry — 19 of them — and ran each under an isolated HOME, then checked whether a registry had been written there. Exactly one had:

POLLUTES (7 entries)  src/lib/sandbox/version.test.ts
clean                 test/registry.test.ts
clean                 test/deepagents-mcp-legacy-lifecycle.test.ts
clean                 src/lib/onboard/sandbox-registry-metadata.test.ts
clean                 ... (15 more)

(src/lib/sandbox/version-scheme.test.ts shares the fixture names but does not reach the registry; checked separately, clean.)

The clean ones set HOME before the module loads — either via createRequire (test/registry.test.ts) or via top-level await import() (test/deepagents-mcp-legacy-lifecycle.test.ts, sandbox-registry-metadata.test.ts). version.test.ts is the only one that reaches state/registry through a static import and sets HOME afterwards.

Environment

OS:            Ubuntu 24.04 (WSL2), x86_64
Node.js:       v24.18.0
NemoClaw:      v0.0.78-5-g48e496780 (main)
vitest:        4.1.9

Steps to Reproduce

# 1. Note the current contents of the real registry
node -e 'console.log(Object.keys(require(process.env.HOME+"/.nemoclaw/sandboxes.json").sandboxes))'

# 2. Run this single test file
npx vitest run --project cli src/lib/sandbox/version.test.ts

# 3. Look again
node -e 'console.log(Object.keys(require(process.env.HOME+"/.nemoclaw/sandboxes.json").sandboxes))'

Expected Result

The test suite leaves ~/.nemoclaw/sandboxes.json untouched.

Actual Result

21 tests pass, and the real registry gains seven entries:

BEFORE: repro6454
vitest exit=0
      Tests  21 passed (21)
AFTER:  repro6454,test-sb,hermes-sb,high-major-sb,low-year-sb,openclaw-sb,hermes-warn-sb,my-sb

Attribution (each file run with an isolated HOME, so the write lands in a throwaway dir):

== src/lib/sandbox/version.test.ts      -> WROTE registry: test-sb, hermes-sb, high-major-sb,
                                           low-year-sb, openclaw-sb, hermes-warn-sb, my-sb
== src/lib/sandbox/version-scheme.test.ts -> no registry written (clean)

Downstream user-visible failure — a later nemoclaw onboard on the same gateway:

  [3/8] Configuring inference provider
  Error: OpenShell gateway 'nemoclaw' has one inference route shared by every registered sandbox.
  Cannot set ollama-local / llama3.2:1b for sandbox 'my-assistant' because it conflicts with
  'hermes-sb', 'hermes-warn-sb', 'high-major-sb', 'low-year-sb', 'my-sb', 'openclaw-sb', 'test-sb'.
  At least one registered sandbox lacks durable provider or model metadata, so same-gateway
  compatibility cannot be proven; remove and re-onboard that sandbox with complete route metadata.

Root cause

src/lib/state/registry.ts:158

export const REGISTRY_FILE = path.join(process.env.HOME || "/tmp", ".nemoclaw", "sandboxes.json");

Module-scope, evaluated once at import. src/lib/sandbox/version.test.ts imports it statically:

import * as registry from "../state/registry.js";   // binds REGISTRY_FILE to the real $HOME
...
beforeEach(() => {
  tmpDir = mkdtempSync(join(tmpdir(), "sandbox-ver-test-"));
  process.env.HOME = tmpDir;                        // too late — no effect
  mkdirSync(join(tmpDir, ".nemoclaw"), { recursive: true });
  writeFileSync(join(tmpDir, ".nemoclaw", "sandboxes.json"), ...);   // never read
});

The temp registry the test carefully prepares is never touched; every write goes to the import-time $HOME.

Suggested fixes

Two options, and I'm happy to send a PR for whichever the maintainers prefer:

  1. Test-local (minimal). Make version.test.ts follow the pattern the other files already use — set HOME before the module loads, then await import() (as test/deepagents-mcp-legacy-lifecycle.test.ts does) or createRequire (as test/registry.test.ts does). Contained, no production change.

  2. Remove the footgun (deeper). Resolve the path lazily in registry.ts — e.g. function registryFile(): string instead of a module-scope const — so that redirecting HOME works regardless of import order. Production behavior is unchanged (the value is read at the same instants it is used today), and the whole class of "my test silently wrote to the real registry" disappears. REGISTRY_FILE is exported and consumed by two tests (test/registry.test.ts, test/deepagents-mcp-legacy-lifecycle.test.ts), so this needs a small follow-through.

Option 2 is the one I'd argue for, with option 1 folded in, but it touches a core state module so the call is yours. A regression test can assert that registerSandbox() under a reassigned HOME lands in the temp dir and that the real path is untouched.

Hokonoken avatar Jul 09 '26 08:07 Hokonoken