From 90bcef8624d9631ff4e945b07c11b9b063f94e67 Mon Sep 17 00:00:00 2001 From: Hung Le Date: Fri, 22 May 2026 01:23:03 +0530 Subject: [PATCH 1/4] fix(channels): sync session policy preset and registry policy preset --- src/lib/actions/sandbox/policy-channel.ts | 67 +++++++ test/channels-add-preset.test.ts | 214 +++++++++++++++++++++- 2 files changed, 280 insertions(+), 1 deletion(-) diff --git a/src/lib/actions/sandbox/policy-channel.ts b/src/lib/actions/sandbox/policy-channel.ts index 686cd2942a2..1efec38cd88 100644 --- a/src/lib/actions/sandbox/policy-channel.ts +++ b/src/lib/actions/sandbox/policy-channel.ts @@ -721,6 +721,7 @@ function applyChannelPresetIfAvailable(sandboxName: string, channelName: string) ); return false; } + syncSessionPolicyPresetsForChannel(sandboxName, channelName, "add"); return true; } catch (err) { const msg = err instanceof Error ? err.message : String(err); @@ -732,6 +733,70 @@ function applyChannelPresetIfAvailable(sandboxName: string, channelName: string) } } +// Keep the onboard session's recorded preset selection in sync with the +// registry whenever `channels add` / `channels remove` mutate the active +// preset list. Without this, a later `nemoclaw rebuild` re-enters +// onboard in resume mode, reads the stale `session.policyPresets`, and +// the policy-selection step narrows the channel's preset back away. The +// new sandbox then boots with the channel auto-launched but no matching +// network policy active — the bridge's Slack/Telegram/Discord WebClient +// hits 403 "CONNECT … not permitted by policy" during init and its +// internal authorize path stays wedged even after rebuild's Step 5.5 +// reapplies the preset from the backup manifest. See "channels-add bug" +// follow-up to #3437. +// +// Best-effort: any failure to load/save the session is logged and +// swallowed. The registry update has already succeeded, so the gateway +// is consistent; the only loss is that a subsequent rebuild may need +// the operator to re-pick presets interactively. +function syncSessionPolicyPresetsForChannel( + sandboxName: string, + channelName: string, + action: "add" | "remove", +): void { + let session: ReturnType; + try { + session = onboardSession.loadSession(); + } catch { + return; + } + // No session file = user never onboarded this host, or it was cleared. + // There is no "intent" record to keep in sync, so leave well enough alone. + if (!session) return; + // The session file is single-tenant (last onboarded sandbox). If it + // currently tracks a different sandbox, mutating policyPresets here + // would corrupt that other sandbox's resume state. + if (session.sandboxName !== sandboxName) return; + + const current = Array.isArray(session.policyPresets) ? session.policyPresets : []; + const has = current.includes(channelName); + // Avoid an unnecessary file write when the desired state already holds. + if (action === "add" && has) return; + if (action === "remove" && !has) return; + + try { + onboardSession.updateSession((s) => { + const arr = Array.isArray(s.policyPresets) ? [...s.policyPresets] : []; + if (action === "add") { + if (!arr.includes(channelName)) arr.push(channelName); + } else { + const idx = arr.indexOf(channelName); + if (idx >= 0) arr.splice(idx, 1); + } + s.policyPresets = arr; + return s; + }); + } catch (err) { + const msg = err instanceof Error ? err.message : String(err); + console.error( + ` ${YW}⚠${R} Could not record '${channelName}' preset ${action} in onboard session: ${msg}`, + ); + console.error( + ` Registry is consistent; rerun '${CLI_NAME} ${sandboxName} policy-${action === "add" ? "add" : "remove"} ${channelName}' after rebuild if needed.`, + ); + } +} + // Mirror of applyChannelPresetIfAvailable. When the channel-named built-in // preset is currently applied to the sandbox, un-apply it so `policy-list` // no longer reports it active and the L7 proxy stops allow-listing the @@ -756,6 +821,8 @@ function removeChannelPresetIfPresent(sandboxName: string, channelName: string): console.error( ` Run manually after rebuild with: ${CLI_NAME} ${sandboxName} policy-remove ${channelName}`, ); + } else { + syncSessionPolicyPresetsForChannel(sandboxName, channelName, "remove"); } } catch (err) { const msg = err instanceof Error ? err.message : String(err); diff --git a/test/channels-add-preset.test.ts b/test/channels-add-preset.test.ts index 7e213b76e45..5194e9f2963 100644 --- a/test/channels-add-preset.test.ts +++ b/test/channels-add-preset.test.ts @@ -49,10 +49,20 @@ function buildPreamble({ presetNamesAvailable = ["telegram", "slack", "discord", "npm", "github"], applyPresetResult = true, sandboxAgent = "openclaw", + sessionSandboxName = "test-sb", + sessionPolicyPresets = ["npm", "pypi", "huggingface", "brew"] as string[] | null, + sessionLoadThrows = false, + sessionUpdateThrows = false, + sessionMissing = false, }: { presetNamesAvailable?: string[]; applyPresetResult?: boolean; sandboxAgent?: string; + sessionSandboxName?: string | null; + sessionPolicyPresets?: string[] | null; + sessionLoadThrows?: boolean; + sessionUpdateThrows?: boolean; + sessionMissing?: boolean; } = {}): string { const j = (p: string) => JSON.stringify(path.join(repoRoot, "dist", "lib", p)); return String.raw` @@ -102,8 +112,48 @@ policies.applyPreset = (sandboxName, presetName) => { callOrder.push("applyPreset:" + presetName); return ${JSON.stringify(applyPresetResult)}; }; +policies.removePreset = (sandboxName, presetName) => { + callOrder.push("removePreset:" + presetName); + return true; +}; policies.getAppliedPresets = () => []; +// Stub onboardSession so the new policyPresets-sync helper has something +// to read/write. The test asserts on sessionUpdates to verify the +// helper kept session.policyPresets aligned with the registry. +const onboardSession = require(${j("state/onboard-session.js")}); +const sessionUpdates = []; +const sessionLoadConfig = ${JSON.stringify({ + sessionSandboxName, + sessionPolicyPresets, + sessionLoadThrows, + sessionMissing, + })}; +const sessionUpdateThrows = ${JSON.stringify(sessionUpdateThrows)}; +let sessionState = sessionLoadConfig.sessionMissing + ? null + : { + sandboxName: sessionLoadConfig.sessionSandboxName, + policyPresets: Array.isArray(sessionLoadConfig.sessionPolicyPresets) + ? [...sessionLoadConfig.sessionPolicyPresets] + : sessionLoadConfig.sessionPolicyPresets, + }; +onboardSession.loadSession = () => { + if (sessionLoadConfig.sessionLoadThrows) throw new Error("simulated load failure"); + return sessionState; +}; +onboardSession.updateSession = (mutator) => { + if (sessionUpdateThrows) throw new Error("simulated save failure"); + // Mirror the real updateSession contract: load → mutate → save. + if (!sessionState) sessionState = { sandboxName: null, policyPresets: null }; + const next = mutator(sessionState) || sessionState; + sessionState = next; + sessionUpdates.push({ + policyPresets: Array.isArray(next.policyPresets) ? [...next.policyPresets] : next.policyPresets, + }); + return next; +}; + // Tag the rebuild-prompt branch via stdout so we can compare ordering. // In NEMOCLAW_NON_INTERACTIVE mode, promptAndRebuild logs "Change queued." // and returns immediately without invoking rebuildSandbox. @@ -116,7 +166,7 @@ console.log = (...args) => { const channelModule = require(${j("actions/sandbox/policy-channel.js")}); -module.exports = { channelModule, appliedCalls, callOrder, providerCalls, registryUpdates }; +module.exports = { channelModule, appliedCalls, callOrder, providerCalls, registryUpdates, sessionUpdates, getSessionState: () => sessionState }; `; } @@ -316,3 +366,165 @@ const ctx = module.exports; ); }); }); + +// Regression: `channels add` was updating the registry but NOT +// session.policyPresets. A later `rebuild` re-entered onboard in resume +// mode, read the stale session, and the policy-selection step narrowed +// the channel's preset back away. The new sandbox booted with the +// channel auto-launched but no matching network policy active, so the +// bridge's Slack/Telegram/Discord WebClient hit 403s and stayed wedged +// even after Step 5.5 of rebuild reapplied the preset from the backup +// manifest. +// +// These tests pin down the invariant: after a successful preset apply +// via channels-add, session.policyPresets must contain the channel +// name; after a successful preset remove via channels-remove, it must +// not. Edge cases (no session, foreign sandbox, save failure) must not +// abort the operation. +describe("channels add/remove keeps session.policyPresets in sync with registry", () => { + it("appends the channel preset to session.policyPresets after a successful add", () => { + const script = `${buildPreamble({ + sessionSandboxName: "test-sb", + sessionPolicyPresets: ["npm", "pypi", "huggingface", "brew"], + })} +const ctx = module.exports; +(async () => { + try { + await ctx.channelModule.addSandboxChannel("test-sb", { channel: "slack" }); + process.stdout.write("\\n__RESULT__" + JSON.stringify({ + sessionUpdates: ctx.sessionUpdates, + finalSession: ctx.getSessionState(), + }) + "\\n"); + } catch (err) { + process.stdout.write("\\n__RESULT__" + JSON.stringify({ error: err.message, stack: err.stack }) + "\\n"); + } +})(); +`; + const result = runScript(script); + assert.equal(result.status, 0, `script failed: ${result.stderr}\n${result.stdout}`); + const marker = result.stdout.lastIndexOf("__RESULT__"); + const payload = JSON.parse(result.stdout.slice(marker + "__RESULT__".length).trim()); + assert.ok(!payload.error, `unexpected error: ${payload.error}\n${payload.stack || ""}`); + + // Exactly one update — the helper short-circuits when the desired + // membership already holds, so duplicate writes would be a bug. + assert.equal( + payload.sessionUpdates.length, + 1, + `expected exactly one session update; got ${JSON.stringify(payload.sessionUpdates)}`, + ); + assert.deepEqual(payload.sessionUpdates[0].policyPresets, [ + "npm", + "pypi", + "huggingface", + "brew", + "slack", + ]); + assert.deepEqual(payload.finalSession.policyPresets, [ + "npm", + "pypi", + "huggingface", + "brew", + "slack", + ]); + }); + + it("does not touch the session when it tracks a different sandbox", () => { + const script = `${buildPreamble({ + sessionSandboxName: "other-sb", + sessionPolicyPresets: ["npm", "github"], + })} +const ctx = module.exports; +(async () => { + try { + await ctx.channelModule.addSandboxChannel("test-sb", { channel: "slack" }); + process.stdout.write("\\n__RESULT__" + JSON.stringify({ + sessionUpdates: ctx.sessionUpdates, + finalSession: ctx.getSessionState(), + appliedCalls: ctx.appliedCalls, + }) + "\\n"); + } catch (err) { + process.stdout.write("\\n__RESULT__" + JSON.stringify({ error: err.message, stack: err.stack }) + "\\n"); + } +})(); +`; + const result = runScript(script); + assert.equal(result.status, 0, `script failed: ${result.stderr}\n${result.stdout}`); + const marker = result.stdout.lastIndexOf("__RESULT__"); + const payload = JSON.parse(result.stdout.slice(marker + "__RESULT__".length).trim()); + assert.ok(!payload.error, `unexpected error: ${payload.error}\n${payload.stack || ""}`); + + // applyPreset still runs against the registry — the preset is the + // channel's egress contract and lives in registry, not session. + assert.deepEqual(payload.appliedCalls, [{ sandboxName: "test-sb", presetName: "slack" }]); + // But the foreign session's policyPresets must be left untouched — + // otherwise we corrupt the other sandbox's resume state. + assert.deepEqual( + payload.sessionUpdates, + [], + `session belonging to a different sandbox must not be mutated; got ${JSON.stringify(payload.sessionUpdates)}`, + ); + assert.deepEqual(payload.finalSession.policyPresets, ["npm", "github"]); + }); + + it("succeeds even when no onboard session file exists", () => { + const script = `${buildPreamble({ sessionMissing: true })} +const ctx = module.exports; +(async () => { + try { + await ctx.channelModule.addSandboxChannel("test-sb", { channel: "slack" }); + process.stdout.write("\\n__RESULT__" + JSON.stringify({ + sessionUpdates: ctx.sessionUpdates, + appliedCalls: ctx.appliedCalls, + callOrder: ctx.callOrder, + }) + "\\n"); + } catch (err) { + process.stdout.write("\\n__RESULT__" + JSON.stringify({ error: err.message, stack: err.stack }) + "\\n"); + } +})(); +`; + const result = runScript(script); + assert.equal(result.status, 0, `script failed: ${result.stderr}\n${result.stdout}`); + const marker = result.stdout.lastIndexOf("__RESULT__"); + const payload = JSON.parse(result.stdout.slice(marker + "__RESULT__".length).trim()); + assert.ok(!payload.error, `unexpected error: ${payload.error}\n${payload.stack || ""}`); + + // Registry mutation still happens; only the session-sync side-effect + // is skipped (there is no intent record to keep aligned). + assert.deepEqual(payload.appliedCalls, [{ sandboxName: "test-sb", presetName: "slack" }]); + assert.deepEqual(payload.sessionUpdates, []); + assert.ok(payload.callOrder.includes("promptAndRebuild")); + }); + + it("does not abort channels-add when session save fails", () => { + const script = `${buildPreamble({ + sessionSandboxName: "test-sb", + sessionPolicyPresets: ["npm", "pypi", "huggingface", "brew"], + sessionUpdateThrows: true, + })} +const ctx = module.exports; +(async () => { + try { + await ctx.channelModule.addSandboxChannel("test-sb", { channel: "slack" }); + process.stdout.write("\\n__RESULT__" + JSON.stringify({ + appliedCalls: ctx.appliedCalls, + callOrder: ctx.callOrder, + }) + "\\n"); + } catch (err) { + process.stdout.write("\\n__RESULT__" + JSON.stringify({ error: err.message, stack: err.stack }) + "\\n"); + } +})(); +`; + const result = runScript(script); + assert.equal(result.status, 0, `script failed: ${result.stderr}\n${result.stdout}`); + const marker = result.stdout.lastIndexOf("__RESULT__"); + const payload = JSON.parse(result.stdout.slice(marker + "__RESULT__".length).trim()); + assert.ok(!payload.error, `unexpected error: ${payload.error}\n${payload.stack || ""}`); + + // Even though session.updateSession threw, the channel add flow + // still completed: preset applied to registry, rebuild prompted. + // Session-sync is best-effort. + assert.deepEqual(payload.appliedCalls, [{ sandboxName: "test-sb", presetName: "slack" }]); + assert.ok(payload.callOrder.includes("promptAndRebuild")); + }); +}); From 20a4a615209adcfa01b0c572b7d3e52f573ca008 Mon Sep 17 00:00:00 2001 From: Hung Le Date: Fri, 22 May 2026 14:25:00 +0530 Subject: [PATCH 2/4] fix(channels): sync session presets on policy-add/remove (built-in + external) --- src/lib/actions/sandbox/policy-channel.ts | 54 ++-- test/policy-add-remove-session-sync.test.ts | 290 ++++++++++++++++++++ 2 files changed, 313 insertions(+), 31 deletions(-) create mode 100644 test/policy-add-remove-session-sync.test.ts diff --git a/src/lib/actions/sandbox/policy-channel.ts b/src/lib/actions/sandbox/policy-channel.ts index 1efec38cd88..4c643f7666e 100644 --- a/src/lib/actions/sandbox/policy-channel.ts +++ b/src/lib/actions/sandbox/policy-channel.ts @@ -161,6 +161,7 @@ export async function addSandboxPolicy( } policies.applyPreset(sandboxName, answer); + syncSessionPolicyPresetsWithRegistry(sandboxName, answer, "add"); } /** @@ -210,6 +211,11 @@ async function applyExternalPreset( const result = policies.applyPresetContent(sandboxName, loaded.presetName, loaded.content, { custom: { sourcePath: path.resolve(filePath) }, }); + if (result !== false) { + // Custom presets share the registry slot with built-ins (customPolicies + // in policy/index.ts:684), so they need the same session-sync. + syncSessionPolicyPresetsWithRegistry(sandboxName, loaded.presetName, "add"); + } return result !== false; } catch (err: unknown) { const message = err instanceof Error ? err.message : String(err); @@ -721,7 +727,7 @@ function applyChannelPresetIfAvailable(sandboxName: string, channelName: string) ); return false; } - syncSessionPolicyPresetsForChannel(sandboxName, channelName, "add"); + syncSessionPolicyPresetsWithRegistry(sandboxName, channelName, "add"); return true; } catch (err) { const msg = err instanceof Error ? err.message : String(err); @@ -733,25 +739,14 @@ function applyChannelPresetIfAvailable(sandboxName: string, channelName: string) } } -// Keep the onboard session's recorded preset selection in sync with the -// registry whenever `channels add` / `channels remove` mutate the active -// preset list. Without this, a later `nemoclaw rebuild` re-enters -// onboard in resume mode, reads the stale `session.policyPresets`, and -// the policy-selection step narrows the channel's preset back away. The -// new sandbox then boots with the channel auto-launched but no matching -// network policy active — the bridge's Slack/Telegram/Discord WebClient -// hits 403 "CONNECT … not permitted by policy" during init and its -// internal authorize path stays wedged even after rebuild's Step 5.5 -// reapplies the preset from the backup manifest. See "channels-add bug" -// follow-up to #3437. -// -// Best-effort: any failure to load/save the session is logged and -// swallowed. The registry update has already succeeded, so the gateway -// is consistent; the only loss is that a subsequent rebuild may need -// the operator to re-pick presets interactively. -function syncSessionPolicyPresetsForChannel( +// Mirror a registry-side preset add/remove into `session.policyPresets`. +// Without this, a later `rebuild` re-enters onboard resume, reads the +// stale session, and narrows the preset back away — see #3437 follow-up. +// Best-effort: registry has already succeeded; failure paths log and +// swallow so the caller's flow is never broken by a session I/O error. +function syncSessionPolicyPresetsWithRegistry( sandboxName: string, - channelName: string, + presetName: string, action: "add" | "remove", ): void { let session: ReturnType; @@ -760,17 +755,13 @@ function syncSessionPolicyPresetsForChannel( } catch { return; } - // No session file = user never onboarded this host, or it was cleared. - // There is no "intent" record to keep in sync, so leave well enough alone. + // No session = nothing to sync. Foreign sandbox = leave its intent alone. if (!session) return; - // The session file is single-tenant (last onboarded sandbox). If it - // currently tracks a different sandbox, mutating policyPresets here - // would corrupt that other sandbox's resume state. if (session.sandboxName !== sandboxName) return; const current = Array.isArray(session.policyPresets) ? session.policyPresets : []; - const has = current.includes(channelName); - // Avoid an unnecessary file write when the desired state already holds. + const has = current.includes(presetName); + // Skip the file write when the desired state already holds. if (action === "add" && has) return; if (action === "remove" && !has) return; @@ -778,9 +769,9 @@ function syncSessionPolicyPresetsForChannel( onboardSession.updateSession((s) => { const arr = Array.isArray(s.policyPresets) ? [...s.policyPresets] : []; if (action === "add") { - if (!arr.includes(channelName)) arr.push(channelName); + if (!arr.includes(presetName)) arr.push(presetName); } else { - const idx = arr.indexOf(channelName); + const idx = arr.indexOf(presetName); if (idx >= 0) arr.splice(idx, 1); } s.policyPresets = arr; @@ -789,10 +780,10 @@ function syncSessionPolicyPresetsForChannel( } catch (err) { const msg = err instanceof Error ? err.message : String(err); console.error( - ` ${YW}⚠${R} Could not record '${channelName}' preset ${action} in onboard session: ${msg}`, + ` ${YW}⚠${R} Could not record '${presetName}' preset ${action} in onboard session: ${msg}`, ); console.error( - ` Registry is consistent; rerun '${CLI_NAME} ${sandboxName} policy-${action === "add" ? "add" : "remove"} ${channelName}' after rebuild if needed.`, + ` Registry is consistent; rerun '${CLI_NAME} ${sandboxName} policy-${action === "add" ? "add" : "remove"} ${presetName}' after rebuild if needed.`, ); } } @@ -822,7 +813,7 @@ function removeChannelPresetIfPresent(sandboxName: string, channelName: string): ` Run manually after rebuild with: ${CLI_NAME} ${sandboxName} policy-remove ${channelName}`, ); } else { - syncSessionPolicyPresetsForChannel(sandboxName, channelName, "remove"); + syncSessionPolicyPresetsWithRegistry(sandboxName, channelName, "remove"); } } catch (err) { const msg = err instanceof Error ? err.message : String(err); @@ -1011,4 +1002,5 @@ export async function removeSandboxPolicy( if (!policies.removePreset(sandboxName, answer)) { process.exit(1); } + syncSessionPolicyPresetsWithRegistry(sandboxName, answer, "remove"); } diff --git a/test/policy-add-remove-session-sync.test.ts b/test/policy-add-remove-session-sync.test.ts new file mode 100644 index 00000000000..e0b68b6e087 --- /dev/null +++ b/test/policy-add-remove-session-sync.test.ts @@ -0,0 +1,290 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// Regression test for the same session/registry divergence that motivated +// the channels-add fix (see test/channels-add-preset.test.ts). The bug +// surfaced first via `nemoclaw channels add slack` → `rebuild` +// (registry got slack, session did not, rebuild's resume step narrowed +// it back away). The exact same divergence applies to the standalone +// preset-mutation CLIs: +// +// - `nemoclaw policy-add ` (built-in preset) +// - `nemoclaw policy-add --from-file …` (custom preset YAML) +// - `nemoclaw policy-remove ` (any preset) +// +// All three call `policies.applyPreset` / `policies.applyPresetContent` / +// `policies.removePreset` to mutate the registry; none of them previously +// touched `session.policyPresets`. These tests pin down the invariant +// that after the channels-add fix was generalised, all three paths now +// keep session in sync with registry, with the same best-effort error +// handling. + +import assert from "node:assert/strict"; +import { spawnSync, type SpawnSyncReturns } from "node:child_process"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { describe, it } from "vitest"; + +const repoRoot = path.join(import.meta.dirname, ".."); + +function runScript(scriptBody: string, extraEnv: Record = {}): SpawnSyncReturns { + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-policy-sync-")); + const scriptPath = path.join(tmpDir, "script.js"); + fs.writeFileSync(scriptPath, scriptBody); + const result = spawnSync(process.execPath, [scriptPath], { + cwd: repoRoot, + encoding: "utf-8", + env: { + ...process.env, + HOME: tmpDir, + NEMOCLAW_NON_INTERACTIVE: "1", + ...extraEnv, + }, + timeout: 15000, + }); + fs.rmSync(tmpDir, { recursive: true, force: true }); + return result; +} + +// Stub every module that addSandboxPolicy / removeSandboxPolicy touches. +// The only side effect we actually want to observe is on the onboardSession +// stub, so everything else is faked to a no-op success. +function buildPreamble({ + presetNamesAvailable = ["github", "npm", "pypi"], + appliedPresets = [] as string[], + sessionSandboxName = "test-sb" as string | null, + sessionPolicyPresets = ["npm"] as string[] | null, + sessionMissing = false, +}: { + presetNamesAvailable?: string[]; + appliedPresets?: string[]; + sessionSandboxName?: string | null; + sessionPolicyPresets?: string[] | null; + sessionMissing?: boolean; +} = {}): string { + const j = (p: string) => JSON.stringify(path.join(repoRoot, "dist", "lib", p)); + return String.raw` +const onboard = require(${j("onboard.js")}); +onboard.isNonInteractive = () => true; + +const credentials = require(${j("credentials/store.js")}); +credentials.prompt = async () => "y"; + +const policies = require(${j("policy/index.js")}); +const calls = { apply: [], applyContent: [], remove: [] }; +policies.listPresets = () => ${JSON.stringify(presetNamesAvailable.map((name) => ({ name })))}; +policies.getAppliedPresets = () => ${JSON.stringify(appliedPresets)}; +policies.loadPreset = (name) => ({ name, network_policies: {} }); +policies.getPresetEndpoints = () => []; +policies.getMessagingPresetWarning = () => null; +policies.selectFromList = async (items) => items[0]?.name || null; +policies.applyPreset = (sandboxName, presetName) => { + calls.apply.push({ sandboxName, presetName }); + return true; +}; +policies.applyPresetContent = (sandboxName, presetName) => { + calls.applyContent.push({ sandboxName, presetName }); + return true; +}; +policies.removePreset = (sandboxName, presetName) => { + calls.remove.push({ sandboxName, presetName }); + return true; +}; +// loadPresetFromFile is used by --from-file path. +policies.loadPresetFromFile = (filePath) => ({ + presetName: "custom-preset-from-file", + content: { network_policies: {} }, +}); + +const onboardSession = require(${j("state/onboard-session.js")}); +const sessionUpdates = []; +let sessionState = ${ + sessionMissing + ? "null" + : `{ + sandboxName: ${JSON.stringify(sessionSandboxName)}, + policyPresets: ${JSON.stringify(sessionPolicyPresets)}, +}` + }; +onboardSession.loadSession = () => sessionState; +onboardSession.updateSession = (mutator) => { + if (!sessionState) sessionState = { sandboxName: null, policyPresets: null }; + const next = mutator(sessionState) || sessionState; + sessionState = next; + sessionUpdates.push({ + policyPresets: Array.isArray(next.policyPresets) ? [...next.policyPresets] : next.policyPresets, + }); + return next; +}; + +const channelModule = require(${j("actions/sandbox/policy-channel.js")}); + +module.exports = { channelModule, calls, sessionUpdates, getSessionState: () => sessionState }; +`; +} + +describe("policy-add / policy-remove keep session.policyPresets in sync with registry", () => { + it("appends the built-in preset to session.policyPresets after policy-add", () => { + const script = `${buildPreamble({ + sessionSandboxName: "test-sb", + sessionPolicyPresets: ["npm"], + })} +const ctx = module.exports; +(async () => { + try { + await ctx.channelModule.addSandboxPolicy("test-sb", { preset: "github", yes: true }); + process.stdout.write("\\n__RESULT__" + JSON.stringify({ + calls: ctx.calls, + sessionUpdates: ctx.sessionUpdates, + finalSession: ctx.getSessionState(), + }) + "\\n"); + } catch (err) { + process.stdout.write("\\n__RESULT__" + JSON.stringify({ error: err.message, stack: err.stack }) + "\\n"); + } +})(); +`; + const result = runScript(script); + assert.equal(result.status, 0, `script failed: ${result.stderr}\n${result.stdout}`); + const marker = result.stdout.lastIndexOf("__RESULT__"); + const payload = JSON.parse(result.stdout.slice(marker + "__RESULT__".length).trim()); + assert.ok(!payload.error, `unexpected error: ${payload.error}\n${payload.stack || ""}`); + + // Contract 1: applyPreset called exactly once with the chosen preset. + assert.deepEqual(payload.calls.apply, [{ sandboxName: "test-sb", presetName: "github" }]); + // Contract 2: session updated exactly once, github appended. + assert.equal(payload.sessionUpdates.length, 1); + assert.deepEqual(payload.sessionUpdates[0].policyPresets, ["npm", "github"]); + assert.deepEqual(payload.finalSession.policyPresets, ["npm", "github"]); + }); + + it("appends the custom preset (--from-file) to session.policyPresets", () => { + // Write a tiny YAML file the stubbed loadPresetFromFile will pretend to parse. + const presetFile = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-preset-")); + const yamlPath = path.join(presetFile, "custom.yaml"); + fs.writeFileSync(yamlPath, "name: custom-preset-from-file\nnetwork_policies: {}\n"); + + const script = `${buildPreamble({ + sessionSandboxName: "test-sb", + sessionPolicyPresets: ["npm"], + })} +const ctx = module.exports; +(async () => { + try { + await ctx.channelModule.addSandboxPolicy("test-sb", { fromFile: ${JSON.stringify(yamlPath)}, yes: true }); + process.stdout.write("\\n__RESULT__" + JSON.stringify({ + calls: ctx.calls, + sessionUpdates: ctx.sessionUpdates, + finalSession: ctx.getSessionState(), + }) + "\\n"); + } catch (err) { + process.stdout.write("\\n__RESULT__" + JSON.stringify({ error: err.message, stack: err.stack }) + "\\n"); + } +})(); +`; + const result = runScript(script); + fs.rmSync(presetFile, { recursive: true, force: true }); + assert.equal(result.status, 0, `script failed: ${result.stderr}\n${result.stdout}`); + const marker = result.stdout.lastIndexOf("__RESULT__"); + const payload = JSON.parse(result.stdout.slice(marker + "__RESULT__".length).trim()); + assert.ok(!payload.error, `unexpected error: ${payload.error}\n${payload.stack || ""}`); + + // The custom-preset path goes through applyPresetContent (NOT applyPreset). + assert.deepEqual(payload.calls.applyContent, [ + { sandboxName: "test-sb", presetName: "custom-preset-from-file" }, + ]); + assert.equal(payload.sessionUpdates.length, 1); + assert.deepEqual(payload.sessionUpdates[0].policyPresets, ["npm", "custom-preset-from-file"]); + }); + + it("removes the preset from session.policyPresets after policy-remove", () => { + const script = `${buildPreamble({ + appliedPresets: ["npm", "github"], + sessionSandboxName: "test-sb", + sessionPolicyPresets: ["npm", "github"], + })} +const ctx = module.exports; +(async () => { + try { + await ctx.channelModule.removeSandboxPolicy("test-sb", { preset: "github", yes: true }); + process.stdout.write("\\n__RESULT__" + JSON.stringify({ + calls: ctx.calls, + sessionUpdates: ctx.sessionUpdates, + finalSession: ctx.getSessionState(), + }) + "\\n"); + } catch (err) { + process.stdout.write("\\n__RESULT__" + JSON.stringify({ error: err.message, stack: err.stack }) + "\\n"); + } +})(); +`; + const result = runScript(script); + assert.equal(result.status, 0, `script failed: ${result.stderr}\n${result.stdout}`); + const marker = result.stdout.lastIndexOf("__RESULT__"); + const payload = JSON.parse(result.stdout.slice(marker + "__RESULT__".length).trim()); + assert.ok(!payload.error, `unexpected error: ${payload.error}\n${payload.stack || ""}`); + + assert.deepEqual(payload.calls.remove, [{ sandboxName: "test-sb", presetName: "github" }]); + assert.equal(payload.sessionUpdates.length, 1); + assert.deepEqual(payload.sessionUpdates[0].policyPresets, ["npm"]); + }); + + it("does not touch a session belonging to a different sandbox", () => { + const script = `${buildPreamble({ + sessionSandboxName: "other-sb", + sessionPolicyPresets: ["pypi"], + })} +const ctx = module.exports; +(async () => { + try { + await ctx.channelModule.addSandboxPolicy("test-sb", { preset: "github", yes: true }); + process.stdout.write("\\n__RESULT__" + JSON.stringify({ + calls: ctx.calls, + sessionUpdates: ctx.sessionUpdates, + finalSession: ctx.getSessionState(), + }) + "\\n"); + } catch (err) { + process.stdout.write("\\n__RESULT__" + JSON.stringify({ error: err.message, stack: err.stack }) + "\\n"); + } +})(); +`; + const result = runScript(script); + assert.equal(result.status, 0, `script failed: ${result.stderr}\n${result.stdout}`); + const marker = result.stdout.lastIndexOf("__RESULT__"); + const payload = JSON.parse(result.stdout.slice(marker + "__RESULT__".length).trim()); + assert.ok(!payload.error, `unexpected error: ${payload.error}\n${payload.stack || ""}`); + + // Registry mutation still happens — that lives per-sandbox in the + // OpenShell policy engine, not in the session file. + assert.deepEqual(payload.calls.apply, [{ sandboxName: "test-sb", presetName: "github" }]); + // But session for "other-sb" must be left alone. + assert.deepEqual(payload.sessionUpdates, []); + assert.deepEqual(payload.finalSession.policyPresets, ["pypi"]); + }); + + it("completes policy-add when no onboard session exists", () => { + const script = `${buildPreamble({ sessionMissing: true })} +const ctx = module.exports; +(async () => { + try { + await ctx.channelModule.addSandboxPolicy("test-sb", { preset: "github", yes: true }); + process.stdout.write("\\n__RESULT__" + JSON.stringify({ + calls: ctx.calls, + sessionUpdates: ctx.sessionUpdates, + }) + "\\n"); + } catch (err) { + process.stdout.write("\\n__RESULT__" + JSON.stringify({ error: err.message, stack: err.stack }) + "\\n"); + } +})(); +`; + const result = runScript(script); + assert.equal(result.status, 0, `script failed: ${result.stderr}\n${result.stdout}`); + const marker = result.stdout.lastIndexOf("__RESULT__"); + const payload = JSON.parse(result.stdout.slice(marker + "__RESULT__".length).trim()); + assert.ok(!payload.error, `unexpected error: ${payload.error}\n${payload.stack || ""}`); + + // Registry mutation succeeded; session-sync was a no-op (no session + // to keep in sync). policy-add must NOT abort the operation in this case. + assert.deepEqual(payload.calls.apply, [{ sandboxName: "test-sb", presetName: "github" }]); + assert.deepEqual(payload.sessionUpdates, []); + }); +}); From 9305ff37bb9ad89dfefa03e3e0fafaedf2a53023 Mon Sep 17 00:00:00 2001 From: Aaron Erickson Date: Fri, 22 May 2026 08:08:45 -0700 Subject: [PATCH 3/4] fix(channels): gate session preset sync on apply success --- src/lib/actions/sandbox/policy-channel.ts | 4 +- test/channels-add-preset.test.ts | 144 +++++++++++++++++++- test/policy-add-remove-session-sync.test.ts | 54 +++++++- 3 files changed, 198 insertions(+), 4 deletions(-) diff --git a/src/lib/actions/sandbox/policy-channel.ts b/src/lib/actions/sandbox/policy-channel.ts index 4c643f7666e..279053dcd58 100644 --- a/src/lib/actions/sandbox/policy-channel.ts +++ b/src/lib/actions/sandbox/policy-channel.ts @@ -160,7 +160,9 @@ export async function addSandboxPolicy( if (confirm.trim().toLowerCase().startsWith("n")) return; } - policies.applyPreset(sandboxName, answer); + if (!policies.applyPreset(sandboxName, answer)) { + process.exit(1); + } syncSessionPolicyPresetsWithRegistry(sandboxName, answer, "add"); } diff --git a/test/channels-add-preset.test.ts b/test/channels-add-preset.test.ts index 5194e9f2963..f7fa78ac37c 100644 --- a/test/channels-add-preset.test.ts +++ b/test/channels-add-preset.test.ts @@ -48,6 +48,7 @@ function runScript(scriptBody: string, extraEnv: Record = {}): S function buildPreamble({ presetNamesAvailable = ["telegram", "slack", "discord", "npm", "github"], applyPresetResult = true, + appliedPresets = [] as string[], sandboxAgent = "openclaw", sessionSandboxName = "test-sb", sessionPolicyPresets = ["npm", "pypi", "huggingface", "brew"] as string[] | null, @@ -57,6 +58,7 @@ function buildPreamble({ }: { presetNamesAvailable?: string[]; applyPresetResult?: boolean; + appliedPresets?: string[]; sandboxAgent?: string; sessionSandboxName?: string | null; sessionPolicyPresets?: string[] | null; @@ -69,6 +71,9 @@ function buildPreamble({ const resolver = require(${j("adapters/openshell/resolve.js")}); resolver.resolveOpenshell = () => "/fake/openshell"; +const openshellRuntime = require(${j("adapters/openshell/runtime.js")}); +openshellRuntime.runOpenshell = () => ({ status: 0, stdout: "", stderr: "" }); + const runner = require(${j("runner.js")}); runner.run = () => ({ status: 0, stdout: "", stderr: "" }); runner.runCapture = () => ""; @@ -105,6 +110,7 @@ registry.updateSandbox = (name, updates) => { const policies = require(${j("policy/index.js")}); const appliedCalls = []; +const removedCalls = []; const callOrder = []; policies.listPresets = () => ${JSON.stringify(presetNamesAvailable.map((name) => ({ name })))}; policies.applyPreset = (sandboxName, presetName) => { @@ -113,10 +119,11 @@ policies.applyPreset = (sandboxName, presetName) => { return ${JSON.stringify(applyPresetResult)}; }; policies.removePreset = (sandboxName, presetName) => { + removedCalls.push({ sandboxName, presetName }); callOrder.push("removePreset:" + presetName); return true; }; -policies.getAppliedPresets = () => []; +policies.getAppliedPresets = () => ${JSON.stringify(appliedPresets)}; // Stub onboardSession so the new policyPresets-sync helper has something // to read/write. The test asserts on sessionUpdates to verify the @@ -166,7 +173,7 @@ console.log = (...args) => { const channelModule = require(${j("actions/sandbox/policy-channel.js")}); -module.exports = { channelModule, appliedCalls, callOrder, providerCalls, registryUpdates, sessionUpdates, getSessionState: () => sessionState }; +module.exports = { channelModule, appliedCalls, removedCalls, callOrder, providerCalls, registryUpdates, sessionUpdates, getSessionState: () => sessionState }; `; } @@ -527,4 +534,137 @@ const ctx = module.exports; assert.deepEqual(payload.appliedCalls, [{ sandboxName: "test-sb", presetName: "slack" }]); assert.ok(payload.callOrder.includes("promptAndRebuild")); }); + + it("removes the channel preset from session.policyPresets after a successful remove", () => { + const script = `${buildPreamble({ + appliedPresets: ["slack"], + sessionSandboxName: "test-sb", + sessionPolicyPresets: ["npm", "slack", "github"], + })} +const ctx = module.exports; +(async () => { + try { + await ctx.channelModule.removeSandboxChannel("test-sb", { channel: "slack" }); + process.stdout.write("\\n__RESULT__" + JSON.stringify({ + removedCalls: ctx.removedCalls, + sessionUpdates: ctx.sessionUpdates, + finalSession: ctx.getSessionState(), + callOrder: ctx.callOrder, + }) + "\\n"); + } catch (err) { + process.stdout.write("\\n__RESULT__" + JSON.stringify({ error: err.message, stack: err.stack }) + "\\n"); + } +})(); +`; + const result = runScript(script); + assert.equal(result.status, 0, `script failed: ${result.stderr}\n${result.stdout}`); + const marker = result.stdout.lastIndexOf("__RESULT__"); + const payload = JSON.parse(result.stdout.slice(marker + "__RESULT__".length).trim()); + assert.ok(!payload.error, `unexpected error: ${payload.error}\n${payload.stack || ""}`); + + assert.deepEqual(payload.removedCalls, [{ sandboxName: "test-sb", presetName: "slack" }]); + assert.equal( + payload.sessionUpdates.length, + 1, + `expected exactly one session update; got ${JSON.stringify(payload.sessionUpdates)}`, + ); + assert.deepEqual(payload.sessionUpdates[0].policyPresets, ["npm", "github"]); + assert.deepEqual(payload.finalSession.policyPresets, ["npm", "github"]); + assert.ok(payload.callOrder.includes("promptAndRebuild")); + }); + + it("does not touch a foreign session during channels-remove", () => { + const script = `${buildPreamble({ + appliedPresets: ["slack"], + sessionSandboxName: "other-sb", + sessionPolicyPresets: ["slack", "npm"], + })} +const ctx = module.exports; +(async () => { + try { + await ctx.channelModule.removeSandboxChannel("test-sb", { channel: "slack" }); + process.stdout.write("\\n__RESULT__" + JSON.stringify({ + removedCalls: ctx.removedCalls, + sessionUpdates: ctx.sessionUpdates, + finalSession: ctx.getSessionState(), + }) + "\\n"); + } catch (err) { + process.stdout.write("\\n__RESULT__" + JSON.stringify({ error: err.message, stack: err.stack }) + "\\n"); + } +})(); +`; + const result = runScript(script); + assert.equal(result.status, 0, `script failed: ${result.stderr}\n${result.stdout}`); + const marker = result.stdout.lastIndexOf("__RESULT__"); + const payload = JSON.parse(result.stdout.slice(marker + "__RESULT__".length).trim()); + assert.ok(!payload.error, `unexpected error: ${payload.error}\n${payload.stack || ""}`); + + assert.deepEqual(payload.removedCalls, [{ sandboxName: "test-sb", presetName: "slack" }]); + assert.deepEqual( + payload.sessionUpdates, + [], + `session belonging to a different sandbox must not be mutated; got ${JSON.stringify(payload.sessionUpdates)}`, + ); + assert.deepEqual(payload.finalSession.policyPresets, ["slack", "npm"]); + }); + + it("succeeds during channels-remove when no onboard session file exists", () => { + const script = `${buildPreamble({ + appliedPresets: ["slack"], + sessionMissing: true, + })} +const ctx = module.exports; +(async () => { + try { + await ctx.channelModule.removeSandboxChannel("test-sb", { channel: "slack" }); + process.stdout.write("\\n__RESULT__" + JSON.stringify({ + removedCalls: ctx.removedCalls, + sessionUpdates: ctx.sessionUpdates, + callOrder: ctx.callOrder, + }) + "\\n"); + } catch (err) { + process.stdout.write("\\n__RESULT__" + JSON.stringify({ error: err.message, stack: err.stack }) + "\\n"); + } +})(); +`; + const result = runScript(script); + assert.equal(result.status, 0, `script failed: ${result.stderr}\n${result.stdout}`); + const marker = result.stdout.lastIndexOf("__RESULT__"); + const payload = JSON.parse(result.stdout.slice(marker + "__RESULT__".length).trim()); + assert.ok(!payload.error, `unexpected error: ${payload.error}\n${payload.stack || ""}`); + + assert.deepEqual(payload.removedCalls, [{ sandboxName: "test-sb", presetName: "slack" }]); + assert.deepEqual(payload.sessionUpdates, []); + assert.ok(payload.callOrder.includes("promptAndRebuild")); + }); + + it("does not abort channels-remove when session save fails", () => { + const script = `${buildPreamble({ + appliedPresets: ["slack"], + sessionSandboxName: "test-sb", + sessionPolicyPresets: ["npm", "slack"], + sessionUpdateThrows: true, + })} +const ctx = module.exports; +(async () => { + try { + await ctx.channelModule.removeSandboxChannel("test-sb", { channel: "slack" }); + process.stdout.write("\\n__RESULT__" + JSON.stringify({ + removedCalls: ctx.removedCalls, + callOrder: ctx.callOrder, + }) + "\\n"); + } catch (err) { + process.stdout.write("\\n__RESULT__" + JSON.stringify({ error: err.message, stack: err.stack }) + "\\n"); + } +})(); +`; + const result = runScript(script); + assert.equal(result.status, 0, `script failed: ${result.stderr}\n${result.stdout}`); + const marker = result.stdout.lastIndexOf("__RESULT__"); + const payload = JSON.parse(result.stdout.slice(marker + "__RESULT__".length).trim()); + assert.ok(!payload.error, `unexpected error: ${payload.error}\n${payload.stack || ""}`); + + assert.deepEqual(payload.removedCalls, [{ sandboxName: "test-sb", presetName: "slack" }]); + assert.ok(payload.callOrder.includes("promptAndRebuild")); + }); }); diff --git a/test/policy-add-remove-session-sync.test.ts b/test/policy-add-remove-session-sync.test.ts index e0b68b6e087..7b1098b674e 100644 --- a/test/policy-add-remove-session-sync.test.ts +++ b/test/policy-add-remove-session-sync.test.ts @@ -53,12 +53,14 @@ function runScript(scriptBody: string, extraEnv: Record = {}): S function buildPreamble({ presetNamesAvailable = ["github", "npm", "pypi"], appliedPresets = [] as string[], + applyPresetResult = true, sessionSandboxName = "test-sb" as string | null, sessionPolicyPresets = ["npm"] as string[] | null, sessionMissing = false, }: { presetNamesAvailable?: string[]; appliedPresets?: string[]; + applyPresetResult?: boolean; sessionSandboxName?: string | null; sessionPolicyPresets?: string[] | null; sessionMissing?: boolean; @@ -81,7 +83,7 @@ policies.getMessagingPresetWarning = () => null; policies.selectFromList = async (items) => items[0]?.name || null; policies.applyPreset = (sandboxName, presetName) => { calls.apply.push({ sandboxName, presetName }); - return true; + return ${JSON.stringify(applyPresetResult)}; }; policies.applyPresetContent = (sandboxName, presetName) => { calls.applyContent.push({ sandboxName, presetName }); @@ -158,6 +160,56 @@ const ctx = module.exports; assert.deepEqual(payload.finalSession.policyPresets, ["npm", "github"]); }); + it("does not sync session.policyPresets when built-in policy-add fails", () => { + const script = `${buildPreamble({ + applyPresetResult: false, + sessionSandboxName: "test-sb", + sessionPolicyPresets: ["npm"], + })} +const ctx = module.exports; +const exitCodes = []; +const originalExit = process.exit; +process.exit = (code) => { + exitCodes.push(code ?? 0); + throw new Error("__EXIT__" + (code ?? 0)); +}; +(async () => { + try { + await ctx.channelModule.addSandboxPolicy("test-sb", { preset: "github", yes: true }); + process.stdout.write("\\n__RESULT__" + JSON.stringify({ + calls: ctx.calls, + sessionUpdates: ctx.sessionUpdates, + finalSession: ctx.getSessionState(), + exitCodes, + }) + "\\n"); + } catch (err) { + if (!String(err && err.message).startsWith("__EXIT__")) { + process.stdout.write("\\n__RESULT__" + JSON.stringify({ error: err.message, stack: err.stack }) + "\\n"); + return; + } + process.stdout.write("\\n__RESULT__" + JSON.stringify({ + calls: ctx.calls, + sessionUpdates: ctx.sessionUpdates, + finalSession: ctx.getSessionState(), + exitCodes, + }) + "\\n"); + } finally { + process.exit = originalExit; + } +})(); +`; + const result = runScript(script); + assert.equal(result.status, 0, `script failed: ${result.stderr}\n${result.stdout}`); + const marker = result.stdout.lastIndexOf("__RESULT__"); + const payload = JSON.parse(result.stdout.slice(marker + "__RESULT__".length).trim()); + assert.ok(!payload.error, `unexpected error: ${payload.error}\n${payload.stack || ""}`); + + assert.deepEqual(payload.calls.apply, [{ sandboxName: "test-sb", presetName: "github" }]); + assert.deepEqual(payload.exitCodes, [1]); + assert.deepEqual(payload.sessionUpdates, []); + assert.deepEqual(payload.finalSession.policyPresets, ["npm"]); + }); + it("appends the custom preset (--from-file) to session.policyPresets", () => { // Write a tiny YAML file the stubbed loadPresetFromFile will pretend to parse. const presetFile = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-preset-")); From 9fcdcc9783934eb58b3883c83f6c3414249b2b6f Mon Sep 17 00:00:00 2001 From: Aaron Erickson Date: Fri, 22 May 2026 08:21:08 -0700 Subject: [PATCH 4/4] test(policy): mirror applyPreset success contract --- test/policies.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/test/policies.test.ts b/test/policies.test.ts index 53c9693e7d9..b1cefaafcee 100644 --- a/test/policies.test.ts +++ b/test/policies.test.ts @@ -74,6 +74,7 @@ policies.listPresets = () => [ policies.getAppliedPresets = () => []; policies.applyPreset = (sandboxName, presetName) => { calls.push({ type: "apply", sandboxName, presetName }); + return true; }; process.argv = ["node", "nemoclaw.js", "test-sandbox", "policy-add", ...${JSON.stringify(extraArgs)}]; Promise.resolve(require(${CLI_PATH}).mainPromise).finally(() => {