From 3879bd01263ecd1c52c3614d93be81c6442dc3d1 Mon Sep 17 00:00:00 2001 From: San Dang Date: Tue, 30 Jun 2026 14:50:30 +0700 Subject: [PATCH 01/10] feat(cli): add channel config status summary Signed-off-by: San Dang --- docs/reference/commands-nemohermes.mdx | 11 +- docs/reference/commands.mdx | 11 +- src/commands/sandbox/channels/status.ts | 6 +- .../actions/sandbox/channel-status-config.ts | 433 +++++++++ .../actions/sandbox/channel-status.test.ts | 915 +++++++++++++++++- src/lib/actions/sandbox/channel-status.ts | 139 ++- .../actions/sandbox/process-recovery.test.ts | 25 + src/lib/actions/sandbox/process-recovery.ts | 6 +- src/lib/cli/public-display-defaults.ts | 2 +- .../discord/rendered-config-parser.test.ts | 37 + .../discord/rendered-config-parser.ts | 99 ++ src/lib/messaging/channels/index.ts | 1 + src/lib/messaging/channels/manifests.test.ts | 10 + .../channels/rendered-config-parser-utils.ts | 106 ++ .../channels/rendered-config-parser.ts | 48 + .../channels/slack/rendered-config-parser.ts | 60 ++ .../channels/teams/rendered-config-parser.ts | 48 + .../telegram/rendered-config-parser.ts | 50 + .../channels/wechat/rendered-config-parser.ts | 88 ++ .../whatsapp/rendered-config-parser.ts | 19 + test/cli/connect-recovery-settle.test.ts | 10 +- test/cli/connect-recovery.test.ts | 16 + 22 files changed, 2071 insertions(+), 69 deletions(-) create mode 100644 src/lib/actions/sandbox/channel-status-config.ts create mode 100644 src/lib/messaging/channels/discord/rendered-config-parser.test.ts create mode 100644 src/lib/messaging/channels/discord/rendered-config-parser.ts create mode 100644 src/lib/messaging/channels/rendered-config-parser-utils.ts create mode 100644 src/lib/messaging/channels/rendered-config-parser.ts create mode 100644 src/lib/messaging/channels/slack/rendered-config-parser.ts create mode 100644 src/lib/messaging/channels/teams/rendered-config-parser.ts create mode 100644 src/lib/messaging/channels/telegram/rendered-config-parser.ts create mode 100644 src/lib/messaging/channels/wechat/rendered-config-parser.ts create mode 100644 src/lib/messaging/channels/whatsapp/rendered-config-parser.ts diff --git a/docs/reference/commands-nemohermes.mdx b/docs/reference/commands-nemohermes.mdx index 914737590f6..066a8e2c41f 100644 --- a/docs/reference/commands-nemohermes.mdx +++ b/docs/reference/commands-nemohermes.mdx @@ -996,16 +996,21 @@ nemohermes my-assistant channels start telegram ### `nemohermes channels status` -Run channel-specific runtime diagnostics. For WhatsApp the command probes the sandbox to separately report pairing/session state, the Noise WebSocket connection, inbound event delivery, and policy/config coverage; a paired channel with no observed inbound delivery exits non-zero with verdict `idle` so an unhealthy bridge cannot pass as healthy. +Run messaging channel status checks. Without `--channel`, the command prints a compact summary for every configured channel, including registration, policy coverage, and non-secret rendered config comparisons. With `--channel`, it prints the detailed status for that channel. + +For WhatsApp, `--channel whatsapp` also probes the sandbox to separately report pairing/session state, the Noise WebSocket connection, inbound event delivery, and policy/config coverage; a paired channel with no observed inbound delivery exits non-zero with verdict `idle` so an unhealthy bridge cannot pass as healthy. + +For registered channels, the status output also compares non-secret config inputs from the sandbox registry against the values rendered into the agent config, such as Telegram group policy in `openclaw.json` or mention mode in Hermes config. Secret inputs, including tokens, are not printed. ```bash +nemohermes my-assistant channels status nemohermes my-assistant channels status --channel whatsapp ``` | Flag | Description | |------|-------------| -| `--channel ` | Channel to inspect; defaults to `whatsapp` when registered | -| `--json` | Emit the diagnostic report as JSON (exit non-zero when the verdict is not `healthy` or `unknown`) | +| `--channel ` | Channel to inspect in detail | +| `--json` | Emit the status report as JSON (for the detailed WhatsApp probe, exit non-zero when the verdict is not `healthy` or `unknown`) | The probe is bounded by an in-sandbox `openshell sandbox exec` with a hard timeout, captures only short matched bridge log signals (e.g. `connection.open`, `401 unauthorized`, `qr expired`), and never forwards message bodies to the host diagnostic output. diff --git a/docs/reference/commands.mdx b/docs/reference/commands.mdx index ebc9a914932..276c80f703c 100644 --- a/docs/reference/commands.mdx +++ b/docs/reference/commands.mdx @@ -1271,16 +1271,21 @@ $$nemoclaw my-assistant channels start telegram ### `$$nemoclaw channels status` -Run channel-specific runtime diagnostics. For WhatsApp the command probes the sandbox to separately report pairing/session state, the Noise WebSocket connection, inbound event delivery, and policy/config coverage; a paired channel with no observed inbound delivery exits non-zero with verdict `idle` so an unhealthy bridge cannot pass as healthy. +Run messaging channel status checks. Without `--channel`, the command prints a compact summary for every configured channel, including registration, policy coverage, and non-secret rendered config comparisons. With `--channel`, it prints the detailed status for that channel. + +For WhatsApp, `--channel whatsapp` also probes the sandbox to separately report pairing/session state, the Noise WebSocket connection, inbound event delivery, and policy/config coverage; a paired channel with no observed inbound delivery exits non-zero with verdict `idle` so an unhealthy bridge cannot pass as healthy. + +For registered channels, the status output also compares non-secret config inputs from the sandbox registry against the values rendered into the agent config, such as Telegram group policy in `openclaw.json` or mention mode in Hermes config. Secret inputs, including tokens, are not printed. ```bash +$$nemoclaw my-assistant channels status $$nemoclaw my-assistant channels status --channel whatsapp ``` | Flag | Description | |------|-------------| -| `--channel ` | Channel to inspect; defaults to `whatsapp` when registered | -| `--json` | Emit the diagnostic report as JSON (exit non-zero when the verdict is not `healthy` or `unknown`) | +| `--channel ` | Channel to inspect in detail | +| `--json` | Emit the status report as JSON (for the detailed WhatsApp probe, exit non-zero when the verdict is not `healthy` or `unknown`) | The probe is bounded by an in-sandbox `openshell sandbox exec` with a hard timeout, captures only short matched bridge log signals (e.g. `connection.open`, `401 unauthorized`, `qr expired`), and never forwards message bodies to the host diagnostic output. diff --git a/src/commands/sandbox/channels/status.ts b/src/commands/sandbox/channels/status.ts index 731120dbb26..b6ee3ce48ac 100644 --- a/src/commands/sandbox/channels/status.ts +++ b/src/commands/sandbox/channels/status.ts @@ -11,9 +11,9 @@ export default class SandboxChannelsStatusCommand extends NemoClawCommand { static id = "sandbox:channels:status"; static strict = true; static enableJsonFlag = true; - static summary = "Inspect a messaging channel's runtime diagnostics"; + static summary = "Inspect messaging channel status"; static description = - "Report channel-specific runtime diagnostics — for WhatsApp, separately reports QR/session state, Noise WebSocket state, inbound event delivery, and policy coverage so a paired-but-idle channel does not appear healthy."; + "Report configured messaging channels, policy coverage, and non-secret rendered config comparisons. Pass --channel whatsapp for the deeper WhatsApp QR/session and inbound-delivery probe."; static usage = [" [--channel ] [--json]"]; static examples = [ "<%= config.bin %> sandbox channels status alpha --channel whatsapp", @@ -24,7 +24,7 @@ export default class SandboxChannelsStatusCommand extends NemoClawCommand { }; static flags = { channel: Flags.string({ - description: "Messaging channel to inspect (defaults to whatsapp when registered)", + description: "Messaging channel to inspect in detail", required: false, }), }; diff --git a/src/lib/actions/sandbox/channel-status-config.ts b/src/lib/actions/sandbox/channel-status-config.ts new file mode 100644 index 00000000000..3ed50c01461 --- /dev/null +++ b/src/lib/actions/sandbox/channel-status-config.ts @@ -0,0 +1,433 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import YAML from "yaml"; +import type { AgentDefinition } from "../../agent/defs"; +import { CLI_NAME } from "../../cli/branding"; +import type { + RenderedChannelConfigParser, + RenderedConfigSource, + RenderedConfigVisibilityKey, +} from "../../messaging"; +import { + createBuiltInChannelManifestRegistry, + getBuiltInRenderedConfigParser, + tryGetMessagingAgentId, +} from "../../messaging"; +import type { + ChannelConfigInputSpec, + MessagingAgentId, + MessagingSerializableValue, + SandboxMessagingInputReference, +} from "../../messaging/manifest"; +import type { DiagnosticSignal } from "../../sandbox/whatsapp-diagnostics"; +import * as registry from "../../state/registry"; + +const CONFIG_STATUS_TIMEOUT_MS = 5_000; +const channelManifestRegistry = createBuiltInChannelManifestRegistry(); + +type ExecRunner = ( + sandboxName: string, + command: string, + timeoutMs?: number, +) => { + status: number; + stdout: string; + stderr: string; +} | null; + +export type ChannelStatusConfigDeps = { + execSandbox: ExecRunner; +}; + +// Inline single-quote shell quoting — config status probes only quote trusted +// path strings derived from agent/channel manifests. +export function quotePath(value: string): string { + return `'${value.replace(/'/g, "'\\''")}'`; +} + +export function buildConfigStatusSignals( + sandboxName: string, + channelName: string, + entry: ReturnType, + agent: AgentDefinition, + deps: ChannelStatusConfigDeps, +): DiagnosticSignal[] { + const plan = registry.getMessagingPlanFromEntry(entry); + const channelPlan = plan?.channels.find((channel) => channel.channelId === channelName); + if (!channelPlan?.configured) return []; + + const manifest = channelManifestRegistry.get(channelName); + const agentId = tryGetMessagingAgentId( + { name: plan?.agent ?? agent.name }, + channelManifestRegistry.list(), + ); + const parser = manifest ? getBuiltInRenderedConfigParser(manifest.id) : null; + const renderSources = + parser && manifest && agentId + ? resolveRenderedConfigSources( + parser.listConfigVisibilityKeys({ manifest, agentId, inputs: channelPlan.inputs }), + agentId, + agent, + ) + : []; + const sourceReads = parser + ? readConfigSourceValues(sandboxName, renderSources, parser, deps) + : emptyConfigSourceReads(); + const configInputs = new Map( + channelPlan.inputs + .filter((input) => input.kind === "config") + .map((input) => [input.inputId, input] as const), + ); + const signals: DiagnosticSignal[] = configSourceReadSignals(sandboxName, sourceReads.targetReads); + + for (const input of manifest?.inputs ?? []) { + if (input.kind !== "config") continue; + const signal = configInputSignal(input, configInputs.get(input.id), renderSources, sourceReads); + if (signal) signals.push(signal); + } + + return signals; +} + +function configInputSignal( + input: ChannelConfigInputSpec, + planInput: SandboxMessagingInputReference | undefined, + renderSources: readonly ConfigRenderSource[], + sourceReads: ConfigSourceReads, +): DiagnosticSignal | null { + const label = configInputLabel(input, planInput); + const expected = expectedConfigValue(input, planInput); + const sources = renderSources.filter((source) => source.inputId === input.id); + if (sources.length === 0) { + return null; + } + + const comparisons = sources.map((source) => + compareConfigSource(expected, source, sourceReads.sourceValues), + ); + const checkedComparisons = comparisons.filter((comparison) => comparison.checked); + const hasMismatch = checkedComparisons.some((comparison) => !comparison.matches); + if (!expected.hasValue && !hasMismatch) return null; + const allSourcesChecked = + checkedComparisons.length === comparisons.length && checkedComparisons.length > 0; + return { + label, + severity: hasMismatch ? "warn" : allSourcesChecked ? "ok" : "info", + detail: Array.from(new Set(comparisons.map((comparison) => comparison.detail))).join("; "), + }; +} + +type SandboxMessagingInputWithValue = SandboxMessagingInputReference & { + readonly value: Exclude; +}; + +function planInputHasValue( + input: SandboxMessagingInputReference | undefined, +): input is SandboxMessagingInputWithValue { + return input?.value !== undefined && input.value !== null; +} + +function configInputLabel( + input: ChannelConfigInputSpec, + planInput: SandboxMessagingInputReference | undefined, +): string { + const label = input.prompt?.label ?? input.envKey ?? input.id; + const envKey = input.envKey ?? planInput?.sourceEnv; + if (!envKey || label === envKey) return label; + return `${label} (${envKey})`; +} + +function configInputDetail(value: MessagingSerializableValue | undefined): string { + if (value === undefined || value === null) return "not set"; + return formatConfigValue(value); +} + +type ExpectedConfigValue = { + readonly value: MessagingSerializableValue | undefined; + readonly detail: string; + readonly hasValue: boolean; +}; + +function expectedConfigValue( + input: ChannelConfigInputSpec, + planInput: SandboxMessagingInputReference | undefined, +): ExpectedConfigValue { + if (planInputHasValue(planInput)) { + return { + value: planInput.value, + detail: configInputDetail(planInput.value), + hasValue: true, + }; + } + + const defaultValue = input.defaultValue?.trim(); + if (defaultValue) { + return { + value: defaultValue, + detail: `${configInputDetail(defaultValue)} (default)`, + hasValue: true, + }; + } + + return { + value: undefined, + detail: configInputDetail(undefined), + hasValue: false, + }; +} + +function formatConfigValue(value: MessagingSerializableValue): string { + if (typeof value === "string") return value.length === 0 ? '""' : value; + if (typeof value === "number" || typeof value === "boolean") return String(value); + if (Array.isArray(value)) { + if (value.length === 0) return "[]"; + return value.map(formatConfigValue).join(", "); + } + return JSON.stringify(value); +} + +interface ConfigRenderSource extends RenderedConfigVisibilityKey { + readonly resolvedTarget: string; +} + +type ConfigSourceRead = + | { + readonly ok: true; + readonly value: MessagingSerializableValue | undefined; + } + | { + readonly ok: false; + readonly error: string; + }; + +type ConfigTargetRead = + | { + readonly ok: true; + readonly contents: string; + } + | { + readonly ok: false; + readonly error: string; + }; + +type ConfigSourceReads = { + readonly sourceValues: ReadonlyMap; + readonly targetReads: ReadonlyMap; +}; + +type ParsedConfigSourceRead = + | { + readonly ok: true; + readonly source: RenderedConfigSource; + } + | { + readonly ok: false; + readonly error: string; + }; + +function configSourceReadSignals( + sandboxName: string, + targetReads: ReadonlyMap, +): DiagnosticSignal[] { + const signals: DiagnosticSignal[] = []; + for (const [target, read] of targetReads.entries()) { + if (read.ok) continue; + signals.push({ + label: "Rendered config source", + severity: "warn", + detail: `${read.error}; config comparisons not checked`, + hint: `inspect \`${target}\` with \`${CLI_NAME} ${sandboxName} exec -- cat ${target}\`, then re-run \`${CLI_NAME} ${sandboxName} rebuild\` if the channel block needs to be regenerated`, + }); + } + return signals; +} + +function emptyConfigSourceReads(): ConfigSourceReads { + return { sourceValues: new Map(), targetReads: new Map() }; +} + +function resolveRenderedConfigSources( + sources: readonly RenderedConfigVisibilityKey[], + agentId: MessagingAgentId, + agent: AgentDefinition, +): ConfigRenderSource[] { + return sources.flatMap((source) => { + const resolvedTarget = resolveConfigTarget(source.target, agentId, agent); + return resolvedTarget ? [{ ...source, resolvedTarget }] : []; + }); +} + +function resolveConfigTarget( + target: string, + agentId: MessagingAgentId, + agent: AgentDefinition, +): string | null { + if (agentId === "openclaw" && target === "openclaw.json") { + return `${agent.configPaths.dir}/${agent.configPaths.configFile}`; + } + const configDir = agent.configPaths.dir.replace(/\/+$/, ""); + if (agentId === "openclaw" && target.startsWith("~/.openclaw/")) { + return `${configDir}/${target.slice("~/.openclaw/".length)}`; + } + if (agentId === "hermes" && target.startsWith("~/.hermes/")) { + return `${configDir}/${target.slice("~/.hermes/".length)}`; + } + if (target.startsWith("/sandbox/")) return target; + return null; +} + +function readConfigSourceValues( + sandboxName: string, + sources: readonly ConfigRenderSource[], + parser: RenderedChannelConfigParser, + deps: ChannelStatusConfigDeps, +): ConfigSourceReads { + const targetReads = new Map(); + for (const target of new Set(sources.map((source) => source.resolvedTarget))) { + const result = deps.execSandbox( + sandboxName, + `cat ${quotePath(target)}`, + CONFIG_STATUS_TIMEOUT_MS, + ); + targetReads.set( + target, + result && result.status === 0 + ? { ok: true, contents: result.stdout } + : { ok: false, error: `could not read ${target}` }, + ); + } + + const reads = new Map(); + for (const source of sources) { + const targetRead = targetReads.get(source.resolvedTarget); + const key = configSourceKey(source); + if (!targetRead?.ok) { + reads.set(key, { + ok: false, + error: `${source.resolvedTarget} unavailable`, + }); + continue; + } + const parsed = parseRenderedConfigSource( + targetRead.contents, + source.resolvedTarget, + source.kind, + ); + reads.set( + key, + parsed.ok ? { ok: true, value: parser.getValue(source, parsed.source) } : parsed, + ); + } + return { sourceValues: reads, targetReads }; +} + +function parseEnvLines(raw: string): Map { + const entries = new Map(); + for (const line of raw.split(/\r?\n/)) { + const trimmed = line.trim(); + if (!trimmed || trimmed.startsWith("#")) continue; + const eq = trimmed.indexOf("="); + if (eq <= 0) continue; + const key = trimmed.slice(0, eq).trim(); + const value = trimmed.slice(eq + 1).trim(); + entries.set(key, unquoteEnvValue(value)); + } + return entries; +} + +function unquoteEnvValue(value: string): string { + if ( + (value.startsWith('"') && value.endsWith('"')) || + (value.startsWith("'") && value.endsWith("'")) + ) { + return value.slice(1, -1); + } + return value; +} + +function parseRenderedConfigSource( + raw: string, + target: string, + kind: ConfigRenderSource["kind"], +): ParsedConfigSourceRead { + if (kind === "env") return { ok: true, source: { kind: "env", entries: parseEnvLines(raw) } }; + try { + const value = + target.endsWith(".yaml") || target.endsWith(".yml") ? YAML.parse(raw) : JSON.parse(raw); + return { ok: true, source: { kind: "structured", value } }; + } catch { + return { ok: false, error: `could not parse ${target}` }; + } +} + +function compareConfigSource( + expected: ExpectedConfigValue, + source: ConfigRenderSource, + sourceValues: ReadonlyMap, +): { readonly checked: boolean; readonly matches: boolean; readonly detail: string } { + const actual = sourceValues.get(configSourceKey(source)); + if (!actual) { + return { + checked: false, + matches: false, + detail: `${expected.detail} (not checked)`, + }; + } + if (!actual.ok) { + return { + checked: false, + matches: false, + detail: `${expected.detail} (not checked)`, + }; + } + const matches = configValuesEqual(expected.value, actual.value); + return { + checked: true, + matches, + detail: matches + ? expected.detail + : `expected ${expected.detail}; rendered ${configInputDetail(actual.value)}`, + }; +} + +function configSourceKey(source: ConfigRenderSource): string { + return `${source.resolvedTarget}:${source.kind}:${source.key}`; +} + +function configValuesEqual( + expected: MessagingSerializableValue | undefined, + actual: MessagingSerializableValue | undefined, +): boolean { + if (expected === undefined || expected === null) return actual === undefined || actual === null; + if (actual === undefined || actual === null) return false; + if (Array.isArray(expected) || Array.isArray(actual)) { + const expectedList = listConfigValues(expected); + const actualList = listConfigValues(actual); + return ( + expectedList.length === actualList.length && + expectedList.every((value, index) => value === actualList[index]) + ); + } + const expectedBoolean = booleanConfigValue(expected); + const actualBoolean = booleanConfigValue(actual); + if (expectedBoolean !== null && actualBoolean !== null) return expectedBoolean === actualBoolean; + return formatConfigValue(expected) === formatConfigValue(actual); +} + +function listConfigValues(value: MessagingSerializableValue): string[] { + const values = Array.isArray(value) ? value : String(value).split(","); + return values + .map((entry) => String(entry).trim()) + .filter((entry) => entry.length > 0) + .sort(); +} + +function booleanConfigValue(value: MessagingSerializableValue): boolean | null { + if (typeof value === "boolean") return value; + if (typeof value !== "string") return null; + const normalized = value.trim().toLowerCase(); + if (normalized === "1" || normalized === "true") return true; + if (normalized === "0" || normalized === "false") return false; + return null; +} diff --git a/src/lib/actions/sandbox/channel-status.test.ts b/src/lib/actions/sandbox/channel-status.test.ts index 4d9d9005a3c..13a1f76a1d2 100644 --- a/src/lib/actions/sandbox/channel-status.test.ts +++ b/src/lib/actions/sandbox/channel-status.test.ts @@ -15,6 +15,7 @@ vi.mock("../../policy", () => ({ vi.mock("../../state/registry", () => ({ getSandbox: vi.fn(), + getMessagingPlanFromEntry: vi.fn((entry) => entry?.messaging?.plan ?? null), getConfiguredMessagingChannelsFromEntry: vi.fn((entry) => { const channels = entry?.messaging?.plan?.channels; return Array.isArray(channels) @@ -38,6 +39,7 @@ vi.mock("./process-recovery", () => ({ })); import type { AgentDefinition } from "../../agent/defs"; +import type { SandboxMessagingInputReference } from "../../messaging/manifest"; import type { SandboxEntry } from "../../state/registry"; import { showSandboxChannelStatus } from "./channel-status"; @@ -65,7 +67,12 @@ function fakeAgent(name: "openclaw" | "hermes" = "openclaw"): AgentDefinition { return { kind: "ui" as const, label: "UI", path: "/" }; }, get configPaths() { - return { dir: configDir, configFile: "config.json", envFile: null, format: "json" }; + return { + dir: configDir, + configFile: name === "openclaw" ? "openclaw.json" : "config.yaml", + envFile: name === "hermes" ? ".env" : null, + format: name === "openclaw" ? "json" : "yaml", + }; }, get inferenceProviderOptions() { return []; @@ -115,17 +122,19 @@ function fakeAgent(name: "openclaw" | "hermes" = "openclaw"): AgentDefinition { function entry( messagingChannels: string[] = ["whatsapp"], disabledChannels: string[] = [], + channelInputs: Record = {}, + agentName: "openclaw" | "hermes" = "openclaw", ): SandboxEntry { const disabled = new Set(disabledChannels); return { name: "alpha", - agent: "openclaw", + agent: agentName, messaging: { schemaVersion: 1, plan: { schemaVersion: 1, sandboxName: "alpha", - agent: "openclaw", + agent: agentName, workflow: "onboard", channels: messagingChannels.map((channelId) => ({ channelId, @@ -135,7 +144,7 @@ function entry( selected: true, configured: true, disabled: disabled.has(channelId), - inputs: [], + inputs: channelInputs[channelId] ?? [], hooks: [], })), disabledChannels, @@ -203,7 +212,12 @@ describe("showSandboxChannelStatus (whatsapp)", () => { exec: () => ({ status: 0, stdout, stderr: "" }), }); try { - await showSandboxChannelStatus("alpha", { deps, quietJson: true, asJson: true }); + await showSandboxChannelStatus("alpha", { + deps, + channel: "whatsapp", + quietJson: true, + asJson: true, + }); } finally { exitSpy.mockRestore(); } @@ -238,7 +252,7 @@ describe("showSandboxChannelStatus (whatsapp)", () => { }); let threw: Error | null = null; try { - await showSandboxChannelStatus("alpha", { deps }); + await showSandboxChannelStatus("alpha", { deps, channel: "whatsapp" }); } catch (err) { threw = err as Error; } finally { @@ -271,7 +285,7 @@ describe("showSandboxChannelStatus (whatsapp)", () => { const { deps, out_lines } = makeDeps({ exec: () => ({ status: 0, stdout, stderr: "" }), }); - const result = await showSandboxChannelStatus("alpha", { deps }); + const result = await showSandboxChannelStatus("alpha", { deps, channel: "whatsapp" }); expect(result && "report" in result && result.report.verdict).toBe("healthy"); const dump = out_lines.join("\n"); expect(dump).toMatch(/Verdict:.*healthy/); @@ -286,7 +300,7 @@ describe("showSandboxChannelStatus (whatsapp)", () => { }); let threw: Error | null = null; try { - await showSandboxChannelStatus("alpha", { deps }); + await showSandboxChannelStatus("alpha", { deps, channel: "whatsapp" }); } catch (err) { threw = err as Error; } finally { @@ -304,7 +318,7 @@ describe("showSandboxChannelStatus (whatsapp)", () => { }); let threw: Error | null = null; try { - await showSandboxChannelStatus("alpha", { deps, asJson: true }); + await showSandboxChannelStatus("alpha", { deps, channel: "whatsapp", asJson: true }); } catch (err) { threw = err as Error; } finally { @@ -333,7 +347,7 @@ describe("showSandboxChannelStatus (whatsapp)", () => { }); let threw: Error | null = null; try { - await showSandboxChannelStatus("alpha", { deps }); + await showSandboxChannelStatus("alpha", { deps, channel: "whatsapp" }); } catch (err) { threw = err as Error; } finally { @@ -357,7 +371,7 @@ describe("showSandboxChannelStatus (whatsapp)", () => { agentName: "hermes", }); try { - await showSandboxChannelStatus("alpha", { deps }); + await showSandboxChannelStatus("alpha", { deps, channel: "whatsapp" }); } catch { /* expected exit(1) for unpaired */ } finally { @@ -397,7 +411,7 @@ describe("showSandboxChannelStatus (whatsapp)", () => { exec: () => ({ status: 0, stdout: stdoutNoMatch, stderr: "" }), }); try { - await showSandboxChannelStatus("alpha", { deps: depsNoMatch }); + await showSandboxChannelStatus("alpha", { deps: depsNoMatch, channel: "whatsapp" }); } catch { /* expected exit(1) for stale-heartbeat + no bridge */ } @@ -423,7 +437,7 @@ describe("showSandboxChannelStatus (whatsapp)", () => { const { deps: depsTimeout, out_lines: linesTimeout } = makeDeps({ exec: () => ({ status: 0, stdout: stdoutTimeout, stderr: "" }), }); - await showSandboxChannelStatus("alpha", { deps: depsTimeout }); + await showSandboxChannelStatus("alpha", { deps: depsTimeout, channel: "whatsapp" }); const dumpTimeout = linesTimeout.join("\n"); expect(dumpTimeout).toMatch(/Bridge process: could not enumerate sandbox processes/); expect(dumpTimeout).toMatch(/Verdict:.*healthy/); @@ -451,7 +465,7 @@ describe("showSandboxChannelStatus (whatsapp)", () => { }) as never); const { deps } = makeDeps({ exec }); try { - await showSandboxChannelStatus("alpha", { deps }); + await showSandboxChannelStatus("alpha", { deps, channel: "whatsapp" }); } catch { /* unpaired path exits 1 */ } finally { @@ -479,13 +493,80 @@ describe("showSandboxChannelStatus (whatsapp)", () => { sandbox: entry(["whatsapp"], ["whatsapp"]), }); deps.execSandbox = execSpy as unknown as typeof deps.execSandbox; - const result = await showSandboxChannelStatus("alpha", { deps }); + const result = await showSandboxChannelStatus("alpha", { deps, channel: "whatsapp" }); expect(execSpy).not.toHaveBeenCalled(); expect(result && "verdict" in result && result.verdict).toBe("info"); const dump = out_lines.join("\n"); expect(dump).toMatch(/registered but currently paused/); }); + it("emits a compact all-channel report when no channel is selected", async () => { + const commands: string[] = []; + const { deps, out_lines } = makeDeps({ + exec: (_sandbox, command) => { + commands.push(command); + return command.includes("/sandbox/.openclaw/openclaw.json") + ? { + status: 0, + stdout: JSON.stringify({ + channels: { + telegram: { + accounts: { + default: { + groupPolicy: "open", + }, + }, + }, + }, + }), + stderr: "", + } + : { status: 1, stdout: "", stderr: "" }; + }, + sandbox: entry(["telegram", "whatsapp"], [], { + telegram: [ + { + channelId: "telegram", + inputId: "groupPolicy", + kind: "config", + required: false, + sourceEnv: "TELEGRAM_GROUP_POLICY", + statePath: "telegramConfig.groupPolicy", + value: "open", + }, + ], + }), + appliedPresets: ["telegram", "whatsapp"], + }); + const result = await showSandboxChannelStatus("alpha", { deps }); + + expect( + result && "channels" in result && result.channels.map((channel) => channel.channel), + ).toEqual(["telegram", "whatsapp"]); + expect(commands.join("\n")).not.toMatch(/NEMOCLAW_WA_DIAG_OK/); + const dump = out_lines.join("\n"); + expect(dump).toMatch(/NemoClaw channels status:.*alpha/); + expect(dump).toMatch(/\btelegram\b/); + expect(dump).toMatch(/\bwhatsapp\b/); + expect(dump).toMatch(/Telegram group policy \(TELEGRAM_GROUP_POLICY\):\s+open/); + expect(dump).not.toMatch(/Deep diagnostics/); + expect(dump).not.toMatch(/Probed at/); + }); + + it("prints an empty-state hint when no channels are configured", async () => { + const { deps, out_lines } = makeDeps({ + exec: () => ({ status: 0, stdout: "", stderr: "" }), + sandbox: entry([]), + appliedPresets: [], + }); + const result = await showSandboxChannelStatus("alpha", { deps }); + + expect(result && "channels" in result && result.channels).toEqual([]); + const dump = out_lines.join("\n"); + expect(dump).toMatch(/Configured channels: none/); + expect(dump).toMatch(/channels add /); + }); + it("emits a basic per-channel report for non-whatsapp channels", async () => { const { deps, out_lines } = makeDeps({ exec: () => ({ status: 0, stdout: "", stderr: "" }), @@ -501,4 +582,808 @@ describe("showSandboxChannelStatus (whatsapp)", () => { expect(dump).toMatch(/telegram registered/); expect(dump).toMatch(/preset applied/); }); + + it("marks rendered config ok when the sandbox config matches the sandbox entry", async () => { + const { deps, out_lines } = makeDeps({ + exec: (_sandbox, command) => + command.includes("/sandbox/.openclaw/openclaw.json") + ? { + status: 0, + stdout: JSON.stringify({ + channels: { + telegram: { + accounts: { + default: { + groupPolicy: "allowlist", + }, + }, + }, + }, + }), + stderr: "", + } + : { status: 1, stdout: "", stderr: "" }, + sandbox: entry(["telegram"], [], { + telegram: [ + { + channelId: "telegram", + inputId: "botToken", + kind: "secret", + required: true, + sourceEnv: "TELEGRAM_BOT_TOKEN", + credentialAvailable: true, + }, + { + channelId: "telegram", + inputId: "requireMention", + kind: "config", + required: false, + sourceEnv: "TELEGRAM_REQUIRE_MENTION", + statePath: "telegramConfig.requireMention", + value: "1", + }, + { + channelId: "telegram", + inputId: "groupPolicy", + kind: "config", + required: false, + sourceEnv: "TELEGRAM_GROUP_POLICY", + statePath: "telegramConfig.groupPolicy", + value: "allowlist", + }, + ], + }), + appliedPresets: ["telegram"], + }); + const result = await showSandboxChannelStatus("alpha", { + deps, + channel: "telegram", + }); + + expect(result && "verdict" in result && result.verdict).toBe("info"); + const signals = result && "signals" in result ? result.signals : []; + expect( + signals.find((signal) => signal.label === "Telegram group policy (TELEGRAM_GROUP_POLICY)"), + ).toMatchObject({ + severity: "ok", + detail: "allowlist", + }); + expect( + signals.find( + (signal) => signal.label === "Telegram group mention mode (TELEGRAM_REQUIRE_MENTION)", + ), + ).toBeUndefined(); + const dump = out_lines.join("\n"); + expect(dump).toMatch(/Telegram group policy \(TELEGRAM_GROUP_POLICY\):\s+allowlist/); + expect(dump).not.toMatch(/Telegram Bot Token/); + expect(dump).not.toMatch(/TELEGRAM_BOT_TOKEN/); + }); + + it("compares Hermes Telegram group policy from rendered env config", async () => { + const { deps } = makeDeps({ + exec: (_sandbox, command) => + command.includes("/sandbox/.hermes/.env") + ? { + status: 0, + stdout: ["TELEGRAM_ALLOWED_USERS=7895072570", "TELEGRAM_GROUP_POLICY=allowlist"].join( + "\n", + ), + stderr: "", + } + : { status: 1, stdout: "", stderr: "" }, + agentName: "hermes", + sandbox: entry( + ["telegram"], + [], + { + telegram: [ + { + channelId: "telegram", + inputId: "groupPolicy", + kind: "config", + required: false, + sourceEnv: "TELEGRAM_GROUP_POLICY", + statePath: "telegramConfig.groupPolicy", + value: "allowlist", + }, + ], + }, + "hermes", + ), + appliedPresets: ["telegram"], + }); + const result = await showSandboxChannelStatus("alpha", { + deps, + channel: "telegram", + }); + + const signals = result && "signals" in result ? result.signals : []; + expect( + signals.find((signal) => signal.label === "Telegram group policy (TELEGRAM_GROUP_POLICY)"), + ).toMatchObject({ + severity: "ok", + detail: "allowlist", + }); + }); + + it("warns when rendered config differs from the sandbox entry", async () => { + const { deps } = makeDeps({ + exec: () => ({ + status: 0, + stdout: JSON.stringify({ + channels: { + telegram: { + accounts: { + default: { + groupPolicy: "open", + }, + }, + }, + }, + }), + stderr: "", + }), + sandbox: entry(["telegram"], [], { + telegram: [ + { + channelId: "telegram", + inputId: "groupPolicy", + kind: "config", + required: false, + sourceEnv: "TELEGRAM_GROUP_POLICY", + statePath: "telegramConfig.groupPolicy", + value: "allowlist", + }, + ], + }), + appliedPresets: ["telegram"], + }); + const result = await showSandboxChannelStatus("alpha", { + deps, + channel: "telegram", + }); + + const signals = result && "signals" in result ? result.signals : []; + expect( + signals.find((signal) => signal.label === "Telegram group policy (TELEGRAM_GROUP_POLICY)"), + ).toMatchObject({ + severity: "warn", + detail: "expected allowlist; rendered open", + }); + }); + + it("warns once when a shared rendered config source is unreadable", async () => { + const { deps, out_lines } = makeDeps({ + exec: () => ({ + status: 1, + stdout: "", + stderr: "cat: /sandbox/.openclaw/openclaw.json: No such file or directory", + }), + sandbox: entry(["teams"], [], { + teams: [ + { + channelId: "teams", + inputId: "appId", + kind: "config", + required: true, + sourceEnv: "MSTEAMS_APP_ID", + statePath: "teamsConfig.appId", + value: "2542103c-7a1e-408a-b2f3-667e09e86783", + }, + { + channelId: "teams", + inputId: "tenantId", + kind: "config", + required: true, + sourceEnv: "MSTEAMS_TENANT_ID", + statePath: "teamsConfig.tenantId", + value: "43083d15-7273-40c1-b7db-39efd9ccc17a", + }, + { + channelId: "teams", + inputId: "allowedUsers", + kind: "config", + required: false, + sourceEnv: "TEAMS_ALLOWED_USERS", + statePath: "allowedIds.teams", + value: "205f29da-231e-4a0e-a0b2-b398e6302087", + }, + { + channelId: "teams", + inputId: "webhookPort", + kind: "config", + required: false, + sourceEnv: "MSTEAMS_PORT", + statePath: "teamsConfig.webhookPort", + value: "3978", + }, + { + channelId: "teams", + inputId: "requireMention", + kind: "config", + required: false, + sourceEnv: "TEAMS_REQUIRE_MENTION", + statePath: "teamsConfig.requireMention", + value: "1", + }, + ], + }), + appliedPresets: ["teams"], + }); + const result = await showSandboxChannelStatus("alpha", { + deps, + channel: "teams", + }); + + const signals = result && "signals" in result ? result.signals : []; + const sourceWarnings = signals.filter((signal) => signal.label === "Rendered config source"); + expect(sourceWarnings).toHaveLength(1); + expect(sourceWarnings[0]).toMatchObject({ + severity: "warn", + detail: "could not read /sandbox/.openclaw/openclaw.json; config comparisons not checked", + }); + expect( + signals.find((signal) => signal.label === "Microsoft Teams Client ID (MSTEAMS_APP_ID)"), + ).toMatchObject({ + severity: "info", + detail: "2542103c-7a1e-408a-b2f3-667e09e86783 (not checked)", + }); + expect( + signals.find( + (signal) => + signal.label === + "Microsoft Teams AAD Object IDs (comma-separated allowlist) (TEAMS_ALLOWED_USERS)", + ), + ).toMatchObject({ + severity: "info", + detail: "205f29da-231e-4a0e-a0b2-b398e6302087 (not checked)", + }); + expect( + signals.find( + (signal) => signal.label === "Microsoft Teams mention mode (TEAMS_REQUIRE_MENTION)", + ), + ).toMatchObject({ + severity: "info", + detail: "1 (not checked)", + }); + const sourceReadFailures = out_lines + .join("\n") + .match(/could not read \/sandbox\/\.openclaw\/openclaw\.json/g); + expect(sourceReadFailures).toHaveLength(1); + }); + + it("treats 0/1 registry config as matching boolean rendered config", async () => { + const { deps } = makeDeps({ + exec: () => ({ + status: 0, + stdout: JSON.stringify({ + channels: { + msteams: { + requireMention: true, + }, + }, + }), + stderr: "", + }), + sandbox: entry(["teams"], [], { + teams: [ + { + channelId: "teams", + inputId: "requireMention", + kind: "config", + required: false, + sourceEnv: "TEAMS_REQUIRE_MENTION", + statePath: "teamsConfig.requireMention", + value: "1", + }, + ], + }), + appliedPresets: ["teams"], + }); + const result = await showSandboxChannelStatus("alpha", { + deps, + channel: "teams", + }); + + const signals = result && "signals" in result ? result.signals : []; + expect( + signals.find( + (signal) => signal.label === "Microsoft Teams mention mode (TEAMS_REQUIRE_MENTION)", + ), + ).toMatchObject({ + severity: "ok", + detail: "1", + }); + }); + + it("compares manifest-derived allowlist render values", async () => { + const { deps } = makeDeps({ + exec: () => ({ + status: 0, + stdout: JSON.stringify({ + channels: { + msteams: { + allowFrom: ["205f29da-231e-4a0e-a0b2-b398e6302087"], + }, + }, + }), + stderr: "", + }), + sandbox: entry(["teams"], [], { + teams: [ + { + channelId: "teams", + inputId: "allowedUsers", + kind: "config", + required: false, + sourceEnv: "TEAMS_ALLOWED_USERS", + statePath: "allowedIds.teams", + value: "205f29da-231e-4a0e-a0b2-b398e6302087", + }, + ], + }), + appliedPresets: ["teams"], + }); + const result = await showSandboxChannelStatus("alpha", { + deps, + channel: "teams", + }); + + const signals = result && "signals" in result ? result.signals : []; + expect( + signals.find( + (signal) => + signal.label === + "Microsoft Teams AAD Object IDs (comma-separated allowlist) (TEAMS_ALLOWED_USERS)", + ), + ).toMatchObject({ + severity: "ok", + detail: "205f29da-231e-4a0e-a0b2-b398e6302087", + }); + }); + + it("compares Discord guild-derived render values", async () => { + const { deps, out_lines } = makeDeps({ + exec: () => ({ + status: 0, + stdout: JSON.stringify({ + channels: { + discord: { + guilds: { + "1504155275899437177": { + requireMention: true, + }, + }, + }, + }, + }), + stderr: "", + }), + sandbox: entry(["discord"], [], { + discord: [ + { + channelId: "discord", + inputId: "serverId", + kind: "config", + required: false, + sourceEnv: "DISCORD_SERVER_ID", + statePath: "discordGuilds.serverId", + value: "1504155275899437177", + }, + { + channelId: "discord", + inputId: "requireMention", + kind: "config", + required: false, + sourceEnv: "DISCORD_REQUIRE_MENTION", + statePath: "discordGuilds.requireMention", + value: "1", + }, + ], + }), + appliedPresets: ["discord"], + }); + const result = await showSandboxChannelStatus("alpha", { + deps, + channel: "discord", + }); + + const signals = result && "signals" in result ? result.signals : []; + expect( + signals.find( + (signal) => + signal.label === "Discord Server ID (for guild workspace access) (DISCORD_SERVER_ID)", + ), + ).toMatchObject({ + severity: "ok", + detail: "1504155275899437177", + }); + expect( + signals.find((signal) => signal.label === "Discord mention mode (DISCORD_REQUIRE_MENTION)"), + ).toMatchObject({ + severity: "ok", + detail: "1", + }); + expect( + signals.find( + (signal) => signal.label === "Discord User ID (optional guild allowlist) (DISCORD_USER_ID)", + ), + ).toBeUndefined(); + const dump = out_lines.join("\n"); + expect(dump).toMatch( + /Discord Server ID \(for guild workspace access\) \(DISCORD_SERVER_ID\):\s+1504155275899437177/, + ); + expect(dump).toMatch(/Discord mention mode \(DISCORD_REQUIRE_MENTION\):\s+1/); + }); + + it("compares Slack OpenClaw allowlist render values", async () => { + const { deps } = makeDeps({ + exec: () => ({ + status: 0, + stdout: JSON.stringify({ + channels: { + slack: { + accounts: { + default: { + allowFrom: ["U01ABC2DEF3"], + channels: { + C012AB3CD: { + enabled: true, + requireMention: true, + users: ["U01ABC2DEF3"], + }, + }, + }, + }, + }, + }, + }), + stderr: "", + }), + sandbox: entry(["slack"], [], { + slack: [ + { + channelId: "slack", + inputId: "allowedUsers", + kind: "config", + required: false, + sourceEnv: "SLACK_ALLOWED_USERS", + statePath: "allowedIds.slack", + value: "U01ABC2DEF3", + }, + { + channelId: "slack", + inputId: "allowedChannels", + kind: "config", + required: false, + sourceEnv: "SLACK_ALLOWED_CHANNELS", + statePath: "slackConfig.allowedChannels", + value: "C012AB3CD", + }, + ], + }), + appliedPresets: ["slack"], + }); + const result = await showSandboxChannelStatus("alpha", { + deps, + channel: "slack", + }); + + const signals = result && "signals" in result ? result.signals : []; + expect( + signals.find( + (signal) => + signal.label === "Slack Member IDs (comma-separated allowlist) (SLACK_ALLOWED_USERS)", + ), + ).toMatchObject({ + severity: "ok", + detail: "U01ABC2DEF3", + }); + expect( + signals.find( + (signal) => + signal.label === "Slack Channel IDs (comma-separated allowlist) (SLACK_ALLOWED_CHANNELS)", + ), + ).toMatchObject({ + severity: "ok", + detail: "C012AB3CD", + }); + }); + + it("does not treat Slack wildcard channel policy as configured channel IDs", async () => { + const { deps } = makeDeps({ + exec: () => ({ + status: 0, + stdout: JSON.stringify({ + channels: { + slack: { + accounts: { + default: { + allowFrom: ["U0B5BQABTL4"], + channels: { + "*": { + enabled: true, + requireMention: true, + users: ["U0B5BQABTL4"], + }, + }, + }, + }, + }, + }, + }), + stderr: "", + }), + sandbox: entry(["slack"], [], { + slack: [ + { + channelId: "slack", + inputId: "allowedUsers", + kind: "config", + required: false, + sourceEnv: "SLACK_ALLOWED_USERS", + statePath: "allowedIds.slack", + value: "U0B5BQABTL4", + }, + ], + }), + appliedPresets: ["slack"], + }); + const result = await showSandboxChannelStatus("alpha", { + deps, + channel: "slack", + }); + + const signals = result && "signals" in result ? result.signals : []; + expect( + signals.find( + (signal) => + signal.label === "Slack Member IDs (comma-separated allowlist) (SLACK_ALLOWED_USERS)", + ), + ).toMatchObject({ + severity: "ok", + detail: "U0B5BQABTL4", + }); + expect( + signals.find( + (signal) => + signal.label === "Slack Channel IDs (comma-separated allowlist) (SLACK_ALLOWED_CHANNELS)", + ), + ).toBeUndefined(); + }); + + it("compares OpenClaw WeChat account render values", async () => { + const renderedResponses: Array<[string, ExecResult]> = [ + [ + "/sandbox/.openclaw/openclaw.json", + { + status: 0, + stdout: JSON.stringify({ + channels: { + "openclaw-weixin": { + accounts: { + "wechat-account": { + enabled: true, + }, + }, + }, + }, + }), + stderr: "", + }, + ], + [ + "/sandbox/.openclaw/openclaw-weixin/accounts/wechat-account.json", + { + status: 0, + stdout: JSON.stringify({ + baseUrl: "https://ilinkai.wechat.com", + userId: "wechat-user", + }), + stderr: "", + }, + ], + ]; + const { deps } = makeDeps({ + exec: (_sandbox, command) => + renderedResponses.find(([needle]) => command.includes(needle))?.[1] ?? { + status: 1, + stdout: "", + stderr: "", + }, + sandbox: entry(["wechat"], [], { + wechat: [ + { + channelId: "wechat", + inputId: "accountId", + kind: "config", + required: true, + sourceEnv: "WECHAT_ACCOUNT_ID", + statePath: "wechatConfig.accountId", + value: "wechat-account", + }, + { + channelId: "wechat", + inputId: "baseUrl", + kind: "config", + required: false, + sourceEnv: "WECHAT_BASE_URL", + statePath: "wechatConfig.baseUrl", + value: "https://ilinkai.wechat.com", + }, + { + channelId: "wechat", + inputId: "userId", + kind: "config", + required: false, + sourceEnv: "WECHAT_USER_ID", + statePath: "wechatConfig.userId", + value: "wechat-user", + }, + { + channelId: "wechat", + inputId: "allowedIds", + kind: "config", + required: false, + sourceEnv: "WECHAT_ALLOWED_IDS", + statePath: "allowedIds.wechat", + value: "wechat-user", + }, + ], + }), + appliedPresets: ["wechat"], + }); + const result = await showSandboxChannelStatus("alpha", { + deps, + channel: "wechat", + }); + + const signals = result && "signals" in result ? result.signals : []; + expect(signals.find((signal) => signal.label === "WECHAT_ACCOUNT_ID")).toMatchObject({ + severity: "ok", + detail: "wechat-account", + }); + expect(signals.find((signal) => signal.label === "WECHAT_BASE_URL")).toMatchObject({ + severity: "ok", + detail: "https://ilinkai.wechat.com", + }); + expect(signals.find((signal) => signal.label === "WECHAT_USER_ID")).toMatchObject({ + severity: "ok", + detail: "wechat-user", + }); + expect( + signals.find( + (signal) => signal.label === "WeChat User ID(s) (DM allowlist) (WECHAT_ALLOWED_IDS)", + ), + ).toBeUndefined(); + }); + + it("compares Hermes WeChat values through rendered WEIXIN env keys", async () => { + const { deps } = makeDeps({ + exec: (_sandbox, command) => + command.includes("/sandbox/.hermes/.env") + ? { + status: 0, + stdout: [ + "WEIXIN_ACCOUNT_ID=wxid_abc", + "WEIXIN_BASE_URL=https://wechat.example.test", + "WEIXIN_ALLOWED_USERS=wxid_abc,wxid_def", + ].join("\n"), + stderr: "", + } + : { status: 1, stdout: "", stderr: "" }, + agentName: "hermes", + sandbox: entry( + ["wechat"], + [], + { + wechat: [ + { + channelId: "wechat", + inputId: "accountId", + kind: "config", + required: true, + sourceEnv: "WECHAT_ACCOUNT_ID", + statePath: "wechatConfig.accountId", + value: "wxid_abc", + }, + { + channelId: "wechat", + inputId: "baseUrl", + kind: "config", + required: false, + sourceEnv: "WECHAT_BASE_URL", + statePath: "wechatConfig.baseUrl", + value: "https://wechat.example.test", + }, + { + channelId: "wechat", + inputId: "userId", + kind: "config", + required: false, + sourceEnv: "WECHAT_USER_ID", + statePath: "wechatConfig.userId", + value: "wxid_abc", + }, + { + channelId: "wechat", + inputId: "allowedIds", + kind: "config", + required: false, + sourceEnv: "WECHAT_ALLOWED_IDS", + statePath: "allowedIds.wechat", + value: "wxid_abc,wxid_def", + }, + ], + }, + "hermes", + ), + appliedPresets: ["wechat"], + }); + const result = await showSandboxChannelStatus("alpha", { + deps, + channel: "wechat", + }); + + const signals = result && "signals" in result ? result.signals : []; + expect(signals.find((signal) => signal.label === "WECHAT_ACCOUNT_ID")).toMatchObject({ + severity: "ok", + detail: "wxid_abc", + }); + expect(signals.find((signal) => signal.label === "WECHAT_BASE_URL")).toMatchObject({ + severity: "ok", + detail: "https://wechat.example.test", + }); + expect( + signals.find( + (signal) => signal.label === "WeChat User ID(s) (DM allowlist) (WECHAT_ALLOWED_IDS)", + ), + ).toMatchObject({ + severity: "ok", + detail: "wxid_abc,wxid_def", + }); + expect(signals.find((signal) => signal.label === "WECHAT_USER_ID")).toBeUndefined(); + }); + + it("uses manifest defaults when no stored config value exists", async () => { + const { deps, out_lines } = makeDeps({ + exec: () => ({ + status: 0, + stdout: JSON.stringify({ + channels: { + telegram: { + accounts: { + default: { + groupPolicy: "open", + }, + }, + }, + }, + }), + stderr: "", + }), + sandbox: entry(["telegram"]), + appliedPresets: ["telegram"], + }); + const result = await showSandboxChannelStatus("alpha", { + deps, + channel: "telegram", + }); + + const signals = result && "signals" in result ? result.signals : []; + expect( + signals.find((signal) => signal.label === "Telegram group policy (TELEGRAM_GROUP_POLICY)"), + ).toMatchObject({ + severity: "ok", + detail: "open (default)", + }); + expect( + signals.find( + (signal) => signal.label === "Telegram group mention mode (TELEGRAM_REQUIRE_MENTION)", + ), + ).toBeUndefined(); + const dump = out_lines.join("\n"); + expect(dump).not.toMatch(/Telegram User ID \(for DM access\)/); + expect(dump).toMatch(/Telegram group policy \(TELEGRAM_GROUP_POLICY\):\s+open \(default\)/); + }); }); diff --git a/src/lib/actions/sandbox/channel-status.ts b/src/lib/actions/sandbox/channel-status.ts index edf28314b71..e4475cb33d4 100644 --- a/src/lib/actions/sandbox/channel-status.ts +++ b/src/lib/actions/sandbox/channel-status.ts @@ -14,14 +14,14 @@ import { type AgentDefinition, loadAgent } from "../../agent/defs"; import { CLI_DISPLAY_NAME, CLI_NAME } from "../../cli/branding"; import { B, D, G, R, RD, YW } from "../../cli/terminal-style"; -import { - collectBuiltInMessagingChannelDiagnostics, - type MessagingChannelDiagnosticSpec, -} from "../../messaging/diagnostics"; import { createBuiltInChannelManifestRegistry, getMessagingManifestAvailabilityContext, } from "../../messaging"; +import { + collectBuiltInMessagingChannelDiagnostics, + type MessagingChannelDiagnosticSpec, +} from "../../messaging/diagnostics"; import * as policies from "../../policy"; import { type DiagnosticSeverity, @@ -34,6 +34,7 @@ import { type WhatsappProbeInput, } from "../../sandbox/whatsapp-diagnostics"; import * as registry from "../../state/registry"; +import { buildConfigStatusSignals, quotePath } from "./channel-status-config"; // runner.ts (which process-recovery transitively depends on) uses a few CJS // `require()` calls that vitest's CLI-test project cannot resolve at import @@ -44,15 +45,6 @@ function loadProcessRecovery(): typeof import("./process-recovery") { return require("./process-recovery") as typeof import("./process-recovery"); } -// Inline single-quote shell quoting — the probe script only ever quotes -// trusted path strings derived from the agent manifest (`configDir/...`), -// so we don't need the full quoting matrix from `runner.shellQuote`. Keep -// the implementation tiny and avoid the runner import so the orchestrator -// stays loadable from unit tests. -function quotePath(value: string): string { - return `'${value.replace(/'/g, "'\\''")}'`; -} - type ExecRunner = ( sandboxName: string, command: string, @@ -83,7 +75,7 @@ export type ChannelStatusOptions = { deps?: StatusDeps; }; -export type ChannelStatusReport = +type ChannelStatusSingleReport = | { schemaVersion: 1; sandbox: string; channel: string; report: WhatsappDiagnosticReport } | { schemaVersion: 1; @@ -93,6 +85,14 @@ export type ChannelStatusReport = signals: DiagnosticSignal[]; }; +export type ChannelStatusReport = + | ChannelStatusSingleReport + | { + schemaVersion: 1; + sandbox: string; + channels: ChannelStatusSingleReport[]; + }; + // Bound how long we are willing to block inside an `openshell sandbox exec` // for the inline diagnostic snippet. WhatsApp's bridge sometimes goes // unresponsive when the Noise WebSocket is stuck; a fast hard cap keeps @@ -152,19 +152,6 @@ function diagnosticChannelNames(): string[] { return CHANNEL_STATUS_DIAGNOSTICS.map((diagnostic) => diagnostic.channelId); } -function selectDefaultChannel(configuredChannels: readonly string[]): string { - const preferredConfigured = configuredChannels.find( - (channel) => getChannelStatusDiagnostic(channel)?.preferredDefault === true, - ); - if (preferredConfigured) return preferredConfigured; - if (configuredChannels.length > 0) return configuredChannels[0]; - return ( - CHANNEL_STATUS_DIAGNOSTICS.find((diagnostic) => diagnostic.preferredDefault)?.channelId ?? - CHANNEL_STATUS_DIAGNOSTICS[0]?.channelId ?? - "" - ); -} - function resolveStateDirs(agent: AgentDefinition): string[] { const configDir = agent.configPaths?.dir; if (!configDir) return []; @@ -424,8 +411,38 @@ function renderReport( deps.out(JSON.stringify(report, null, 2)); return; } + if ("channels" in report) { + renderAllChannelReport(report, deps); + return; + } deps.out(""); deps.out(` ${B}${CLI_DISPLAY_NAME} channels status:${R} ${report.sandbox} / ${report.channel}`); + renderSingleChannelSignals(report, deps, { includeDeepDiagnostics: true }); +} + +function renderAllChannelReport( + report: Extract, + deps: Required, +): void { + deps.out(""); + deps.out(` ${B}${CLI_DISPLAY_NAME} channels status:${R} ${report.sandbox}`); + if (report.channels.length === 0) { + deps.out(` ${severityLabel("info")} Configured channels: none`); + deps.out(` ${D}hint: run \`${CLI_NAME} ${report.sandbox} channels add \`${R}`); + deps.out(""); + return; + } + for (const channelReport of report.channels) { + deps.out(` ${B}${channelReport.channel}${R}`); + renderSingleChannelSignals(channelReport, deps, { includeDeepDiagnostics: false }); + } +} + +function renderSingleChannelSignals( + report: ChannelStatusSingleReport, + deps: Required, + options: { readonly includeDeepDiagnostics: boolean }, +): void { if ("report" in report) { deps.out(` Probed at ${report.report.probedAt} (agent: ${report.report.agent})`); deps.out(""); @@ -448,6 +465,7 @@ function renderReport( return; } for (const signal of report.signals) { + if (!options.includeDeepDiagnostics && signal.label === "Deep diagnostics") continue; deps.out(` ${severityLabel(signal.severity)} ${signal.label}: ${signal.detail}`); if (signal.hint) deps.out(` ${D}hint: ${signal.hint}${R}`); } @@ -455,6 +473,7 @@ function renderReport( } function exitCodeFor(report: ChannelStatusReport): number { + if ("channels" in report) return 0; if ("report" in report) { switch (report.report.verdict) { case "healthy": @@ -473,7 +492,8 @@ function buildBasicChannelReport( agent: AgentDefinition, deps: Required, diagnostic: MessagingChannelDiagnosticSpec, -): ChannelStatusReport { + options: { readonly includeDeepDiagnostics?: boolean } = {}, +): ChannelStatusSingleReport { const entry = deps.getSandbox(sandboxName); const enabled = registry.getConfiguredMessagingChannelsFromEntry(entry).includes(channelName); const disabled = registry.getDisabledMessagingChannelsFromEntry(entry).includes(channelName); @@ -505,11 +525,16 @@ function buildBasicChannelReport( ? undefined : `run \`${CLI_NAME} ${sandboxName} policy-add ${policyPresets[0]}\``, }); - signals.push({ - label: "Deep diagnostics", - severity: "info", - detail: `not implemented for ${channelName}; see \`${CLI_NAME} ${sandboxName} doctor\` and \`${CLI_NAME} ${sandboxName} logs --follow\``, - }); + if (enabled) { + signals.push(...buildConfigStatusSignals(sandboxName, channelName, entry, agent, deps)); + } + if (options.includeDeepDiagnostics ?? true) { + signals.push({ + label: "Deep diagnostics", + severity: "info", + detail: `not implemented for ${channelName}; see \`${CLI_NAME} ${sandboxName} doctor\` and \`${CLI_NAME} ${sandboxName} logs --follow\``, + }); + } // Reference the agent in a hint so the deep-diagnostic section is // discoverable per agent without needing extra plumbing. if (!channelSupportedByAgent(channelName, agent)) { @@ -528,6 +553,25 @@ function buildBasicChannelReport( }; } +function buildUnknownConfiguredChannelReport( + sandboxName: string, + channelName: string, +): ChannelStatusSingleReport { + return { + schemaVersion: 1, + sandbox: sandboxName, + channel: channelName, + verdict: "info", + signals: [ + { + label: "Channel registration", + severity: "warn", + detail: `${channelName} registered but not recognized by this CLI build`, + }, + ], + }; +} + function channelSupportedByAgent(channelName: string, agent: AgentDefinition): boolean { return channelManifestRegistry .listAvailable(getMessagingManifestAvailabilityContext(agent, channelManifestRegistry.list())) @@ -565,14 +609,31 @@ export async function showSandboxChannelStatus( process.exit(1); } - let channelName = channelArg; - if (!channelName) { + const agent = deps.loadAgent(entry.agent || "openclaw"); + + if (!channelArg) { const configuredChannels = registry.getConfiguredMessagingChannelsFromEntry(entry); - channelName = selectDefaultChannel(configuredChannels); + const report: ChannelStatusReport = { + schemaVersion: 1, + sandbox: sandboxName, + channels: configuredChannels.map((channelName) => { + const diagnostic = getChannelStatusDiagnostic(channelName); + return diagnostic + ? buildBasicChannelReport(sandboxName, channelName, agent, deps, diagnostic, { + includeDeepDiagnostics: false, + }) + : buildUnknownConfiguredChannelReport(sandboxName, channelName); + }), + }; + if (!(asJson && quietJson)) { + renderReport(report, asJson, deps); + } + return report; } - const diagnostic = channelName ? getChannelStatusDiagnostic(channelName) : null; - if (!channelName || !diagnostic) { + const channelName = channelArg; + const diagnostic = getChannelStatusDiagnostic(channelName); + if (!diagnostic) { const known = diagnosticChannelNames().join(", "); if (asJson) { deps.out( @@ -588,8 +649,6 @@ export async function showSandboxChannelStatus( process.exit(1); } - const agent = deps.loadAgent(entry.agent || "openclaw"); - const disabledChannels = new Set(registry.getDisabledMessagingChannelsFromEntry(entry)); const channelIsPaused = disabledChannels.has(channelName); diff --git a/src/lib/actions/sandbox/process-recovery.test.ts b/src/lib/actions/sandbox/process-recovery.test.ts index cbfe901000a..bc46f1cb450 100644 --- a/src/lib/actions/sandbox/process-recovery.test.ts +++ b/src/lib/actions/sandbox/process-recovery.test.ts @@ -5,10 +5,35 @@ import { afterEach, describe, expect, it } from "vitest"; // Import from compiled dist for parity with the other CLI tests in this project. import { + buildSandboxExecMarkedCommand, probeSandboxInferenceGatewayHealth, waitForRecoveredSandboxGateway, } from "./process-recovery"; +describe("sandbox exec command wrapping", () => { + it("keeps single-line payloads readable while avoiding marker newlines", () => { + const wrapped = buildSandboxExecMarkedCommand("echo SECRET_BOUNDARY_OK"); + + expect(wrapped).not.toMatch(/[\r\n]/); + expect(wrapped).toContain("echo SECRET_BOUNDARY_OK"); + expect(wrapped).not.toContain("base64 -d | sh"); + }); + + it("keeps the OpenShell command argument newline-free while preserving multi-line payloads", () => { + const payload = "printf 'hello\\n'\ncat '/sandbox/.openclaw/openclaw.json'"; + const wrapped = buildSandboxExecMarkedCommand(payload); + + expect(wrapped).not.toMatch(/[\r\n]/); + expect(wrapped).toContain("__NEMOCLAW_SANDBOX_EXEC_STARTED__"); + expect(wrapped).toContain("base64 -d | sh"); + expect(wrapped).not.toContain(payload); + + const encoded = wrapped.match(/printf '%s' '([^']+)' \| base64 -d \| sh/)?.[1]; + expect(encoded).toBeTruthy(); + expect(Buffer.from(encoded as string, "base64").toString("utf8")).toBe(payload); + }); +}); + describe("probeSandboxInferenceGatewayHealth gateway-chain subprobe (#3265)", () => { const makeExec = (stdout: string, status = 0) => diff --git a/src/lib/actions/sandbox/process-recovery.ts b/src/lib/actions/sandbox/process-recovery.ts index 47bcfb34a08..6558f670890 100644 --- a/src/lib/actions/sandbox/process-recovery.ts +++ b/src/lib/actions/sandbox/process-recovery.ts @@ -67,9 +67,9 @@ export type SandboxForwardHealth = boolean | "occupied" | null; const SANDBOX_EXEC_STARTED_MARKER = "__NEMOCLAW_SANDBOX_EXEC_STARTED__"; -function buildSandboxExecMarkedCommand(command: string): string { - if (!command.includes("validate-hermes-env-secret-boundary.py")) { - return `printf '%s\n' '${SANDBOX_EXEC_STARTED_MARKER}'; ${command}`; +export function buildSandboxExecMarkedCommand(command: string): string { + if (!/[\r\n]/.test(command)) { + return `printf '%s\\n' '${SANDBOX_EXEC_STARTED_MARKER}'; ${command}`; } const encodedCommand = Buffer.from(command, "utf8").toString("base64"); return [ diff --git a/src/lib/cli/public-display-defaults.ts b/src/lib/cli/public-display-defaults.ts index 131945ccddb..85ec47c4ace 100644 --- a/src/lib/cli/public-display-defaults.ts +++ b/src/lib/cli/public-display-defaults.ts @@ -185,7 +185,7 @@ const PUBLIC_DISPLAY_LAYOUT: Record = { group: "Messaging Channels", order: 25, usage: "nemoclaw channels status", - description: "Channel-specific runtime diagnostics", + description: "Messaging channel status", flags: "[--channel ] [--json]", }, ], diff --git a/src/lib/messaging/channels/discord/rendered-config-parser.test.ts b/src/lib/messaging/channels/discord/rendered-config-parser.test.ts new file mode 100644 index 00000000000..18517f8624b --- /dev/null +++ b/src/lib/messaging/channels/discord/rendered-config-parser.test.ts @@ -0,0 +1,37 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { describe, expect, it } from "vitest"; + +import type { ChannelManifest } from "../../manifest"; +import { discordRenderedConfigParser } from "./rendered-config-parser"; + +describe("discord rendered config parser", () => { + it("treats missing guild mention policy values as unset", () => { + const requireMentionKey = discordRenderedConfigParser + .listConfigVisibilityKeys({ + agentId: "openclaw", + manifest: { id: "discord" } as ChannelManifest, + inputs: [], + }) + .find((key) => key.key === "guildRequireMention"); + + expect(requireMentionKey).toBeDefined(); + expect( + discordRenderedConfigParser.getValue(requireMentionKey!, { + kind: "structured", + value: { + channels: { + discord: { + guilds: { + "1504155275899437177": { + enabled: true, + }, + }, + }, + }, + }, + }), + ).toBeUndefined(); + }); +}); diff --git a/src/lib/messaging/channels/discord/rendered-config-parser.ts b/src/lib/messaging/channels/discord/rendered-config-parser.ts new file mode 100644 index 00000000000..13ec0e4c5f1 --- /dev/null +++ b/src/lib/messaging/channels/discord/rendered-config-parser.ts @@ -0,0 +1,99 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import type { MessagingSerializableValue } from "../../manifest"; +import { + envConfigKey, + getEnvConfigValue, + getStructuredConfigValue, + getStructuredPath, + type RenderedChannelConfigParser, + type RenderedConfigSource, + type RenderedConfigVisibilityKey, + structuredConfigKey, +} from "../rendered-config-parser-utils"; + +const OPENCLAW_GUILDS_PATH = ["channels", "discord", "guilds"] as const; + +export const discordRenderedConfigParser: RenderedChannelConfigParser = { + listConfigVisibilityKeys(context) { + if (context.agentId === "openclaw") { + return [ + structuredConfigKey("serverId", "openclaw.json", OPENCLAW_GUILDS_PATH, "guildIds"), + structuredConfigKey( + "requireMention", + "openclaw.json", + OPENCLAW_GUILDS_PATH, + "guildRequireMention", + ), + structuredConfigKey("userId", "openclaw.json", OPENCLAW_GUILDS_PATH, "guildUsers"), + ]; + } + if (context.agentId === "hermes") { + return [ + envConfigKey("serverId", "~/.hermes/.env", "NEMOCLAW_DISCORD_GUILD_IDS"), + envConfigKey("userId", "~/.hermes/.env", "DISCORD_ALLOWED_USERS"), + structuredConfigKey("requireMention", "~/.hermes/config.yaml", [ + "discord", + "require_mention", + ]), + ]; + } + return []; + }, + + getValue(key, source) { + switch (key.key) { + case "guildIds": + return Object.keys(discordGuilds(source, key)); + case "guildRequireMention": + return discordRequireMention(discordGuilds(source, key)); + case "guildUsers": + return discordGuildUsers(discordGuilds(source, key)); + default: + return key.kind === "env" + ? getEnvConfigValue(source, key.envKey) + : getStructuredConfigValue(source, key.path); + } + }, +}; + +function discordGuilds( + source: RenderedConfigSource, + key: RenderedConfigVisibilityKey, +): Record { + const value = getStructuredConfigValue(source, key.path); + return value && typeof value === "object" && !Array.isArray(value) + ? (value as Record) + : {}; +} + +function discordRequireMention( + guilds: Readonly>, +): MessagingSerializableValue | undefined { + const values = Object.values(guilds) + .map((guild) => + guild && typeof guild === "object" && !Array.isArray(guild) + ? getStructuredPath(guild, ["requireMention"]) + : undefined, + ) + .filter((entry): entry is MessagingSerializableValue => entry !== undefined); + if (values.length === 0) return undefined; + const uniqueValues = Array.from(new Set(values.map(formatProjectionValue))); + return uniqueValues.length === 1 ? values[0] : uniqueValues; +} + +function discordGuildUsers( + guilds: Readonly>, +): MessagingSerializableValue | undefined { + const users = Object.values(guilds).flatMap((guild) => { + if (!guild || typeof guild !== "object" || Array.isArray(guild)) return []; + const rawUsers = getStructuredPath(guild, ["users"]); + return Array.isArray(rawUsers) ? rawUsers.map(String) : []; + }); + return users.length > 0 ? users : undefined; +} + +function formatProjectionValue(value: MessagingSerializableValue): string { + return typeof value === "string" ? value : JSON.stringify(value); +} diff --git a/src/lib/messaging/channels/index.ts b/src/lib/messaging/channels/index.ts index 187cb7e14d5..04823f39f2f 100644 --- a/src/lib/messaging/channels/index.ts +++ b/src/lib/messaging/channels/index.ts @@ -3,4 +3,5 @@ export * from "./built-ins"; export * from "./metadata"; +export * from "./rendered-config-parser"; export { createBuiltInRenderTemplateResolver } from "./template-resolver"; diff --git a/src/lib/messaging/channels/manifests.test.ts b/src/lib/messaging/channels/manifests.test.ts index 8d08c248b18..07f8d799e3f 100644 --- a/src/lib/messaging/channels/manifests.test.ts +++ b/src/lib/messaging/channels/manifests.test.ts @@ -20,6 +20,7 @@ import { BUILT_IN_CHANNEL_MANIFESTS, createBuiltInChannelManifestRegistry, discordManifest, + getBuiltInRenderedConfigParser, slackManifest, teamsManifest, telegramManifest, @@ -221,6 +222,15 @@ describe("built-in channel manifests", () => { ); }); + it("keeps rendered config parsers aligned with built-in manifests", () => { + expect( + BUILT_IN_CHANNEL_MANIFESTS.map((manifest) => [ + manifest.id, + Boolean(getBuiltInRenderedConfigParser(manifest.id)), + ]), + ).toEqual(BUILT_IN_CHANNEL_MANIFESTS.map((manifest) => [manifest.id, true])); + }); + it("keeps phase-1 manifest and hook files free of production side-effect imports", () => { const manifestPaths = [ "src/lib/messaging/channels/telegram/manifest.ts", diff --git a/src/lib/messaging/channels/rendered-config-parser-utils.ts b/src/lib/messaging/channels/rendered-config-parser-utils.ts new file mode 100644 index 00000000000..a572e1f917c --- /dev/null +++ b/src/lib/messaging/channels/rendered-config-parser-utils.ts @@ -0,0 +1,106 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import type { + ChannelManifest, + MessagingAgentId, + MessagingSerializableValue, + SandboxMessagingInputReference, +} from "../manifest"; + +export type RenderedConfigSourceKind = "structured" | "env"; + +export interface RenderedConfigVisibilityKey { + readonly key: string; + readonly inputId: string; + readonly target: string; + readonly kind: RenderedConfigSourceKind; + readonly path?: readonly string[]; + readonly envKey?: string; +} + +export type RenderedConfigSource = + | { + readonly kind: "structured"; + readonly value: unknown; + } + | { + readonly kind: "env"; + readonly entries: ReadonlyMap; + }; + +export interface RenderedChannelConfigParserContext { + readonly manifest: ChannelManifest; + readonly agentId: MessagingAgentId; + readonly inputs: readonly SandboxMessagingInputReference[]; +} + +export interface RenderedChannelConfigParser { + listConfigVisibilityKeys( + context: RenderedChannelConfigParserContext, + ): readonly RenderedConfigVisibilityKey[]; + getValue( + key: RenderedConfigVisibilityKey, + source: RenderedConfigSource, + ): MessagingSerializableValue | undefined; +} + +export function structuredConfigKey( + inputId: string, + target: string, + path: readonly string[], + key = inputId, +): RenderedConfigVisibilityKey { + return { key, inputId, target, kind: "structured", path }; +} + +export function envConfigKey( + inputId: string, + target: string, + envKey: string, + key = inputId, +): RenderedConfigVisibilityKey { + return { key, inputId, target, kind: "env", envKey }; +} + +export function getStructuredConfigValue( + source: RenderedConfigSource, + path: readonly string[] | undefined, +): MessagingSerializableValue | undefined { + if (source.kind !== "structured" || !path) return undefined; + return getStructuredPath(source.value, path); +} + +export function getEnvConfigValue( + source: RenderedConfigSource, + envKey: string | undefined, +): MessagingSerializableValue | undefined { + if (source.kind !== "env" || !envKey) return undefined; + return source.entries.get(envKey); +} + +export function getStructuredPath( + value: unknown, + path: readonly string[], +): MessagingSerializableValue | undefined { + let current = value; + for (const segment of path) { + if (Array.isArray(current)) { + const index = Number(segment); + current = Number.isInteger(index) ? current[index] : undefined; + continue; + } + if (!current || typeof current !== "object") return undefined; + current = (current as Record)[segment]; + } + return isMessagingSerializableValue(current) ? current : undefined; +} + +export function isMessagingSerializableValue(value: unknown): value is MessagingSerializableValue { + if (value === null) return true; + const type = typeof value; + if (type === "string" || type === "number" || type === "boolean") return true; + if (Array.isArray(value)) return value.every(isMessagingSerializableValue); + if (type !== "object") return false; + return Object.values(value as Record).every(isMessagingSerializableValue); +} diff --git a/src/lib/messaging/channels/rendered-config-parser.ts b/src/lib/messaging/channels/rendered-config-parser.ts new file mode 100644 index 00000000000..14bf1b81e44 --- /dev/null +++ b/src/lib/messaging/channels/rendered-config-parser.ts @@ -0,0 +1,48 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import type { ChannelManifest } from "../manifest"; +import { BUILT_IN_CHANNEL_MANIFESTS } from "./built-ins"; +import { discordRenderedConfigParser } from "./discord/rendered-config-parser"; +import type { RenderedChannelConfigParser } from "./rendered-config-parser-utils"; +import { slackRenderedConfigParser } from "./slack/rendered-config-parser"; +import { teamsRenderedConfigParser } from "./teams/rendered-config-parser"; +import { telegramRenderedConfigParser } from "./telegram/rendered-config-parser"; +import { wechatRenderedConfigParser } from "./wechat/rendered-config-parser"; +import { whatsappRenderedConfigParser } from "./whatsapp/rendered-config-parser"; + +export * from "./rendered-config-parser-utils"; + +const BUILT_IN_RENDERED_CONFIG_PARSERS: ReadonlyMap = new Map( + BUILT_IN_CHANNEL_MANIFESTS.map((manifest) => [ + manifest.id, + renderedConfigParserForBuiltInManifest(manifest), + ]), +); + +export function getBuiltInRenderedConfigParser( + channelId: string, +): RenderedChannelConfigParser | null { + return BUILT_IN_RENDERED_CONFIG_PARSERS.get(channelId) ?? null; +} + +function renderedConfigParserForBuiltInManifest( + manifest: ChannelManifest, +): RenderedChannelConfigParser { + switch (manifest.id) { + case "discord": + return discordRenderedConfigParser; + case "slack": + return slackRenderedConfigParser; + case "teams": + return teamsRenderedConfigParser; + case "telegram": + return telegramRenderedConfigParser; + case "wechat": + return wechatRenderedConfigParser; + case "whatsapp": + return whatsappRenderedConfigParser; + default: + throw new Error(`missing rendered config parser for built-in channel '${manifest.id}'`); + } +} diff --git a/src/lib/messaging/channels/slack/rendered-config-parser.ts b/src/lib/messaging/channels/slack/rendered-config-parser.ts new file mode 100644 index 00000000000..c3cedc5c263 --- /dev/null +++ b/src/lib/messaging/channels/slack/rendered-config-parser.ts @@ -0,0 +1,60 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { + envConfigKey, + getEnvConfigValue, + getStructuredConfigValue, + type RenderedChannelConfigParser, + type RenderedConfigSource, + type RenderedConfigVisibilityKey, + structuredConfigKey, +} from "../rendered-config-parser-utils"; + +const OPENCLAW_CHANNELS_KEY = "openclawAllowedChannels"; + +export const slackRenderedConfigParser: RenderedChannelConfigParser = { + listConfigVisibilityKeys(context) { + if (context.agentId === "openclaw") { + return [ + structuredConfigKey("allowedUsers", "openclaw.json", [ + "channels", + "slack", + "accounts", + "default", + "allowFrom", + ]), + structuredConfigKey( + "allowedChannels", + "openclaw.json", + ["channels", "slack", "accounts", "default", "channels"], + OPENCLAW_CHANNELS_KEY, + ), + ]; + } + if (context.agentId === "hermes") { + return [ + envConfigKey("allowedUsers", "~/.hermes/.env", "SLACK_ALLOWED_USERS"), + envConfigKey("allowedChannels", "~/.hermes/.env", "SLACK_ALLOWED_CHANNELS"), + ]; + } + return []; + }, + + getValue(key, source) { + if (key.key === OPENCLAW_CHANNELS_KEY) return slackAllowedChannelIds(source, key); + return key.kind === "env" + ? getEnvConfigValue(source, key.envKey) + : getStructuredConfigValue(source, key.path); + }, +}; + +function slackAllowedChannelIds( + source: RenderedConfigSource, + key: RenderedConfigVisibilityKey, +): string[] | undefined { + const value = getStructuredConfigValue(source, key.path); + if (!value || typeof value !== "object" || Array.isArray(value)) return undefined; + const channelIds = Object.keys(value).filter((channelId) => channelId !== "*"); + return channelIds.length > 0 ? channelIds : undefined; +} diff --git a/src/lib/messaging/channels/teams/rendered-config-parser.ts b/src/lib/messaging/channels/teams/rendered-config-parser.ts new file mode 100644 index 00000000000..cd102cac36d --- /dev/null +++ b/src/lib/messaging/channels/teams/rendered-config-parser.ts @@ -0,0 +1,48 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { + envConfigKey, + getEnvConfigValue, + getStructuredConfigValue, + type RenderedChannelConfigParser, + structuredConfigKey, +} from "../rendered-config-parser-utils"; + +export const teamsRenderedConfigParser: RenderedChannelConfigParser = { + listConfigVisibilityKeys(context) { + if (context.agentId === "openclaw") { + return [ + structuredConfigKey("appId", "openclaw.json", ["channels", "msteams", "appId"]), + structuredConfigKey("tenantId", "openclaw.json", ["channels", "msteams", "tenantId"]), + structuredConfigKey("allowedUsers", "openclaw.json", ["channels", "msteams", "allowFrom"]), + structuredConfigKey("webhookPort", "openclaw.json", [ + "channels", + "msteams", + "webhook", + "port", + ]), + structuredConfigKey("requireMention", "openclaw.json", [ + "channels", + "msteams", + "requireMention", + ]), + ]; + } + if (context.agentId === "hermes") { + return [ + envConfigKey("appId", "~/.hermes/.env", "TEAMS_CLIENT_ID"), + envConfigKey("tenantId", "~/.hermes/.env", "TEAMS_TENANT_ID"), + envConfigKey("allowedUsers", "~/.hermes/.env", "TEAMS_ALLOWED_USERS"), + envConfigKey("webhookPort", "~/.hermes/.env", "TEAMS_PORT"), + ]; + } + return []; + }, + + getValue(key, source) { + return key.kind === "env" + ? getEnvConfigValue(source, key.envKey) + : getStructuredConfigValue(source, key.path); + }, +}; diff --git a/src/lib/messaging/channels/telegram/rendered-config-parser.ts b/src/lib/messaging/channels/telegram/rendered-config-parser.ts new file mode 100644 index 00000000000..b949d6f90cc --- /dev/null +++ b/src/lib/messaging/channels/telegram/rendered-config-parser.ts @@ -0,0 +1,50 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { + envConfigKey, + getEnvConfigValue, + getStructuredConfigValue, + type RenderedChannelConfigParser, + structuredConfigKey, +} from "../rendered-config-parser-utils"; + +export const telegramRenderedConfigParser: RenderedChannelConfigParser = { + listConfigVisibilityKeys(context) { + if (context.agentId === "openclaw") { + return [ + structuredConfigKey("allowedIds", "openclaw.json", [ + "channels", + "telegram", + "accounts", + "default", + "allowFrom", + ]), + structuredConfigKey("groupPolicy", "openclaw.json", [ + "channels", + "telegram", + "accounts", + "default", + "groupPolicy", + ]), + ]; + } + if (context.agentId === "hermes") { + return [ + envConfigKey("allowedIds", "~/.hermes/.env", "TELEGRAM_ALLOWED_USERS"), + envConfigKey("groupPolicy", "~/.hermes/.env", "TELEGRAM_GROUP_POLICY"), + structuredConfigKey("requireMention", "~/.hermes/config.yaml", [ + "telegram", + "require_mention", + ]), + ]; + } + return []; + }, + + getValue(key, source) { + return key.kind === "env" + ? getEnvConfigValue(source, key.envKey) + : getStructuredConfigValue(source, key.path); + }, +}; diff --git a/src/lib/messaging/channels/wechat/rendered-config-parser.ts b/src/lib/messaging/channels/wechat/rendered-config-parser.ts new file mode 100644 index 00000000000..8b410af17cb --- /dev/null +++ b/src/lib/messaging/channels/wechat/rendered-config-parser.ts @@ -0,0 +1,88 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { + envConfigKey, + getEnvConfigValue, + getStructuredConfigValue, + type RenderedChannelConfigParser, + type RenderedChannelConfigParserContext, + type RenderedConfigSource, + type RenderedConfigVisibilityKey, + structuredConfigKey, +} from "../rendered-config-parser-utils"; + +const OPENCLAW_ACCOUNT_IDS_KEY = "openclawWeixinAccountIds"; + +export const wechatRenderedConfigParser: RenderedChannelConfigParser = { + listConfigVisibilityKeys(context) { + if (context.agentId === "openclaw") return openClawConfigVisibilityKeys(context); + if (context.agentId === "hermes") { + return [ + envConfigKey("accountId", "~/.hermes/.env", "WEIXIN_ACCOUNT_ID"), + envConfigKey("baseUrl", "~/.hermes/.env", "WEIXIN_BASE_URL"), + envConfigKey("allowedIds", "~/.hermes/.env", "WEIXIN_ALLOWED_USERS"), + ]; + } + return []; + }, + + getValue(key, source) { + if (key.key === OPENCLAW_ACCOUNT_IDS_KEY) return openClawWeixinAccountIds(source, key); + return key.kind === "env" + ? getEnvConfigValue(source, key.envKey) + : getStructuredConfigValue(source, key.path); + }, +}; + +function openClawConfigVisibilityKeys( + context: RenderedChannelConfigParserContext, +): readonly RenderedConfigVisibilityKey[] { + const keys: RenderedConfigVisibilityKey[] = [ + structuredConfigKey( + "accountId", + "openclaw.json", + ["channels", "openclaw-weixin", "accounts"], + OPENCLAW_ACCOUNT_IDS_KEY, + ), + ]; + const accountId = safeAccountId(inputValue(context, "accountId")); + if (!accountId) return keys; + const accountTarget = `~/.openclaw/openclaw-weixin/accounts/${accountId}.json`; + keys.push( + structuredConfigKey("baseUrl", accountTarget, ["baseUrl"]), + structuredConfigKey("userId", accountTarget, ["userId"]), + ); + return keys; +} + +function openClawWeixinAccountIds( + source: RenderedConfigSource, + key: RenderedConfigVisibilityKey, +): string[] | undefined { + const value = getStructuredConfigValue(source, key.path); + if (!value || typeof value !== "object" || Array.isArray(value)) return undefined; + const accountIds = Object.keys(value); + return accountIds.length > 0 ? accountIds : undefined; +} + +function inputValue( + context: RenderedChannelConfigParserContext, + inputId: string, +): string | undefined { + const value = context.inputs.find((input) => input.inputId === inputId)?.value; + return typeof value === "string" ? value.trim() || undefined : undefined; +} + +function safeAccountId(value: string | undefined): string | undefined { + if ( + !value || + value === "." || + value === ".." || + value.includes("..") || + /[\\/\0-\x1F\x7F]/.test(value) + ) { + return undefined; + } + return value; +} diff --git a/src/lib/messaging/channels/whatsapp/rendered-config-parser.ts b/src/lib/messaging/channels/whatsapp/rendered-config-parser.ts new file mode 100644 index 00000000000..ed122b28cd9 --- /dev/null +++ b/src/lib/messaging/channels/whatsapp/rendered-config-parser.ts @@ -0,0 +1,19 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { + envConfigKey, + getEnvConfigValue, + type RenderedChannelConfigParser, +} from "../rendered-config-parser-utils"; + +export const whatsappRenderedConfigParser: RenderedChannelConfigParser = { + listConfigVisibilityKeys(context) { + if (context.agentId !== "hermes") return []; + return [envConfigKey("allowedIds", "~/.hermes/.env", "WHATSAPP_ALLOWED_USERS")]; + }, + + getValue(key, source) { + return getEnvConfigValue(source, key.envKey); + }, +}; diff --git a/test/cli/connect-recovery-settle.test.ts b/test/cli/connect-recovery-settle.test.ts index bd0f4787464..1307934fc07 100644 --- a/test/cli/connect-recovery-settle.test.ts +++ b/test/cli/connect-recovery-settle.test.ts @@ -7,13 +7,19 @@ // declaring a recovery that is already dying. Split from // connect-recovery.test.ts, which is at the default size budget. -import { describe, it, expect } from "vitest"; import fs from "node:fs"; import os from "node:os"; import path from "node:path"; +import { describe, expect, it } from "vitest"; import { runWithEnv, writeSandboxRegistry } from "./helpers"; +const DECODE_SANDBOX_EXEC_COMMAND_LINES = [ + "decode_sandbox_exec_cmd() {", + ` ${JSON.stringify(process.execPath)} -e "const s=process.argv[1]||'';const m=s.match(/printf '%s' '([A-Za-z0-9+/=]+)' \\| base64 -d \\| sh/);process.stdout.write(m?Buffer.from(m[1],'base64').toString('utf8'):s);" "$1"`, + "}", +]; + describe("CLI dispatch", () => { it("fails probe-only when a wedged gateway serves once and then drops its listener (#4710)", () => { const home = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-cli-connect-probe-wedge-")); @@ -28,6 +34,7 @@ describe("CLI dispatch", () => { path.join(localBin, "openshell"), [ "#!/usr/bin/env bash", + ...DECODE_SANDBOX_EXEC_COMMAND_LINES, `marker_file=${JSON.stringify(markerFile)}`, `state_file=${JSON.stringify(stateFile)}`, `ready_count_file=${JSON.stringify(readyCountFile)}`, @@ -43,6 +50,7 @@ describe("CLI dispatch", () => { "fi", 'if [ "$1" = "sandbox" ] && [ "$2" = "exec" ] && [ "$3" = "--name" ] && [ "$4" = "alpha" ]; then', ' cmd="$8"', + ' cmd="$(decode_sandbox_exec_cmd "$cmd")"', ' case "$cmd" in', ' *"OPENCLAW="*)', ' echo recovered > "$state_file"', diff --git a/test/cli/connect-recovery.test.ts b/test/cli/connect-recovery.test.ts index cd0c8f5b1e0..2066c444cff 100644 --- a/test/cli/connect-recovery.test.ts +++ b/test/cli/connect-recovery.test.ts @@ -16,6 +16,12 @@ import { writeSandboxRegistry, } from "./helpers"; +const DECODE_SANDBOX_EXEC_COMMAND_LINES = [ + "decode_sandbox_exec_cmd() {", + ` ${JSON.stringify(process.execPath)} -e "const s=process.argv[1]||'';const m=s.match(/printf '%s' '([A-Za-z0-9+/=]+)' \\| base64 -d \\| sh/);process.stdout.write(m?Buffer.from(m[1],'base64').toString('utf8'):s);" "$1"`, + "}", +]; + describe("CLI dispatch", () => { it("connect does not pre-start a duplicate port forward", () => { const home = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-cli-connect-forward-")); @@ -159,6 +165,7 @@ describe("CLI dispatch", () => { path.join(localBin, "openshell"), [ "#!/usr/bin/env bash", + ...DECODE_SANDBOX_EXEC_COMMAND_LINES, `marker_file=${JSON.stringify(markerFile)}`, `state_file=${JSON.stringify(stateFile)}`, 'printf \'%s\\n\' "$*" >> "$marker_file"', @@ -173,6 +180,7 @@ describe("CLI dispatch", () => { "fi", 'if [ "$1" = "sandbox" ] && [ "$2" = "exec" ] && [ "$3" = "--name" ] && [ "$4" = "alpha" ]; then', ' cmd="$8"', + ' cmd="$(decode_sandbox_exec_cmd "$cmd")"', ' case "$cmd" in', ' *"OPENCLAW="*)', ' echo recovered > "$state_file"', @@ -221,6 +229,7 @@ describe("CLI dispatch", () => { path.join(localBin, "openshell"), [ "#!/usr/bin/env bash", + ...DECODE_SANDBOX_EXEC_COMMAND_LINES, `marker_file=${JSON.stringify(markerFile)}`, `state_file=${JSON.stringify(stateFile)}`, `ready_count_file=${JSON.stringify(readyCountFile)}`, @@ -236,6 +245,7 @@ describe("CLI dispatch", () => { "fi", 'if [ "$1" = "sandbox" ] && [ "$2" = "exec" ] && [ "$3" = "--name" ] && [ "$4" = "alpha" ]; then', ' cmd="$8"', + ' cmd="$(decode_sandbox_exec_cmd "$cmd")"', ' case "$cmd" in', ' *"OPENCLAW="*)', ' echo recovered > "$state_file"', @@ -285,6 +295,7 @@ describe("CLI dispatch", () => { path.join(localBin, "openshell"), [ "#!/usr/bin/env bash", + ...DECODE_SANDBOX_EXEC_COMMAND_LINES, `marker_file=${JSON.stringify(markerFile)}`, 'printf \'%s\\n\' "$*" >> "$marker_file"', 'if [ "$1" = "sandbox" ] && [ "$2" = "get" ] && [ "$3" = "alpha" ]; then', @@ -298,6 +309,7 @@ describe("CLI dispatch", () => { "fi", 'if [ "$1" = "sandbox" ] && [ "$2" = "exec" ] && [ "$3" = "--name" ] && [ "$4" = "alpha" ]; then', ' cmd="$8"', + ' cmd="$(decode_sandbox_exec_cmd "$cmd")"', ' if [[ "$cmd" == *"curl -so"* ]]; then echo "__NEMOCLAW_SANDBOX_EXEC_STARTED__"; echo RUNNING; exit 0; fi', ' if [[ "$cmd" == *"OPENCLAW="* ]]; then echo "__NEMOCLAW_SANDBOX_EXEC_STARTED__"; echo UNEXPECTED_RECOVERY; exit 1; fi', "fi", @@ -333,6 +345,7 @@ describe("CLI dispatch", () => { path.join(localBin, "openshell"), [ "#!/usr/bin/env bash", + ...DECODE_SANDBOX_EXEC_COMMAND_LINES, `marker_file=${JSON.stringify(markerFile)}`, 'printf \'%s\\n\' "$*" >> "$marker_file"', 'if [ "$1" = "sandbox" ] && [ "$2" = "get" ] && [ "$3" = "alpha" ]; then', @@ -346,6 +359,7 @@ describe("CLI dispatch", () => { "fi", 'if [ "$1" = "sandbox" ] && [ "$2" = "exec" ] && [ "$3" = "--name" ] && [ "$4" = "alpha" ]; then', ' cmd="$8"', + ' cmd="$(decode_sandbox_exec_cmd "$cmd")"', ' if [[ "$cmd" == *"OPENCLAW="* ]]; then echo "__NEMOCLAW_SANDBOX_EXEC_STARTED__"; echo RECOVERY_FAILED >&2; exit 42; fi', ' if [[ "$cmd" == *"curl -so"* ]]; then echo "__NEMOCLAW_SANDBOX_EXEC_STARTED__"; echo STOPPED; exit 0; fi', "fi", @@ -550,6 +564,7 @@ describe("CLI dispatch", () => { path.join(localBin, "openshell"), [ "#!/usr/bin/env bash", + ...DECODE_SANDBOX_EXEC_COMMAND_LINES, `calls=${JSON.stringify(openshellCalls)}`, `state_file=${JSON.stringify(stateFile)}`, 'printf \'%s\\n\' "$*" >> "$calls"', @@ -564,6 +579,7 @@ describe("CLI dispatch", () => { "fi", 'if [ "$1" = "sandbox" ] && [ "$2" = "exec" ] && [ "$3" = "--name" ] && [ "$4" = "alpha" ]; then', ' cmd="$8"', + ' cmd="$(decode_sandbox_exec_cmd "$cmd")"', ' if [[ "$cmd" == *"curl -so"* ]]; then', " echo '__NEMOCLAW_SANDBOX_EXEC_STARTED__'", ' if [ "$(cat "$state_file")" = recovered ]; then echo RUNNING; else echo STOPPED; fi', From 7ab9cf1c3f2ee875d9ceb851e5ed9ed3b8cc3be6 Mon Sep 17 00:00:00 2001 From: San Dang Date: Tue, 30 Jun 2026 15:14:36 +0700 Subject: [PATCH 02/10] fix(cli): harden channel config comparisons Signed-off-by: San Dang --- .../actions/sandbox/channel-status-config.ts | 32 ++++-- .../actions/sandbox/channel-status.test.ts | 97 +++++++++++++++++++ .../discord/rendered-config-parser.test.ts | 31 ++++++ .../discord/rendered-config-parser.ts | 8 +- src/lib/messaging/channels/manifests.test.ts | 18 ++++ 5 files changed, 176 insertions(+), 10 deletions(-) diff --git a/src/lib/actions/sandbox/channel-status-config.ts b/src/lib/actions/sandbox/channel-status-config.ts index 3ed50c01461..55cdc0401d2 100644 --- a/src/lib/actions/sandbox/channel-status-config.ts +++ b/src/lib/actions/sandbox/channel-status-config.ts @@ -63,10 +63,16 @@ export function buildConfigStatusSignals( channelManifestRegistry.list(), ); const parser = manifest ? getBuiltInRenderedConfigParser(manifest.id) : null; + const manifestConfigInputs = (manifest?.inputs ?? []).filter( + (input): input is ChannelConfigInputSpec => input.kind === "config", + ); + const manifestConfigInputIds = new Set(manifestConfigInputs.map((input) => input.id)); const renderSources = parser && manifest && agentId ? resolveRenderedConfigSources( - parser.listConfigVisibilityKeys({ manifest, agentId, inputs: channelPlan.inputs }), + parser + .listConfigVisibilityKeys({ manifest, agentId, inputs: channelPlan.inputs }) + .filter((key) => manifestConfigInputIds.has(key.inputId)), agentId, agent, ) @@ -79,10 +85,9 @@ export function buildConfigStatusSignals( .filter((input) => input.kind === "config") .map((input) => [input.inputId, input] as const), ); - const signals: DiagnosticSignal[] = configSourceReadSignals(sandboxName, sourceReads.targetReads); + const signals: DiagnosticSignal[] = configSourceReadSignals(sandboxName, sourceReads); - for (const input of manifest?.inputs ?? []) { - if (input.kind !== "config") continue; + for (const input of manifestConfigInputs) { const signal = configInputSignal(input, configInputs.get(input.id), renderSources, sourceReads); if (signal) signals.push(signal); } @@ -214,6 +219,7 @@ type ConfigTargetRead = type ConfigSourceReads = { readonly sourceValues: ReadonlyMap; readonly targetReads: ReadonlyMap; + readonly targetParseErrors: ReadonlyMap; }; type ParsedConfigSourceRead = @@ -228,10 +234,10 @@ type ParsedConfigSourceRead = function configSourceReadSignals( sandboxName: string, - targetReads: ReadonlyMap, + sourceReads: ConfigSourceReads, ): DiagnosticSignal[] { const signals: DiagnosticSignal[] = []; - for (const [target, read] of targetReads.entries()) { + for (const [target, read] of sourceReads.targetReads.entries()) { if (read.ok) continue; signals.push({ label: "Rendered config source", @@ -240,11 +246,19 @@ function configSourceReadSignals( hint: `inspect \`${target}\` with \`${CLI_NAME} ${sandboxName} exec -- cat ${target}\`, then re-run \`${CLI_NAME} ${sandboxName} rebuild\` if the channel block needs to be regenerated`, }); } + for (const [target, error] of sourceReads.targetParseErrors.entries()) { + signals.push({ + label: "Rendered config source", + severity: "warn", + detail: `${error}; config comparisons not checked`, + hint: `inspect \`${target}\` with \`${CLI_NAME} ${sandboxName} exec -- cat ${target}\`, then re-run \`${CLI_NAME} ${sandboxName} rebuild\` if the channel block needs to be regenerated`, + }); + } return signals; } function emptyConfigSourceReads(): ConfigSourceReads { - return { sourceValues: new Map(), targetReads: new Map() }; + return { sourceValues: new Map(), targetReads: new Map(), targetParseErrors: new Map() }; } function resolveRenderedConfigSources( @@ -299,6 +313,7 @@ function readConfigSourceValues( } const reads = new Map(); + const targetParseErrors = new Map(); for (const source of sources) { const targetRead = targetReads.get(source.resolvedTarget); const key = configSourceKey(source); @@ -314,12 +329,13 @@ function readConfigSourceValues( source.resolvedTarget, source.kind, ); + if (!parsed.ok) targetParseErrors.set(source.resolvedTarget, parsed.error); reads.set( key, parsed.ok ? { ok: true, value: parser.getValue(source, parsed.source) } : parsed, ); } - return { sourceValues: reads, targetReads }; + return { sourceValues: reads, targetReads, targetParseErrors }; } function parseEnvLines(raw: string): Map { diff --git a/src/lib/actions/sandbox/channel-status.test.ts b/src/lib/actions/sandbox/channel-status.test.ts index 13a1f76a1d2..7876eba9812 100644 --- a/src/lib/actions/sandbox/channel-status.test.ts +++ b/src/lib/actions/sandbox/channel-status.test.ts @@ -852,6 +852,57 @@ describe("showSandboxChannelStatus (whatsapp)", () => { expect(sourceReadFailures).toHaveLength(1); }); + it("warns once when a shared rendered config source is malformed", async () => { + const { deps } = makeDeps({ + exec: () => ({ + status: 0, + stdout: "{not-json", + stderr: "", + }), + sandbox: entry(["teams"], [], { + teams: [ + { + channelId: "teams", + inputId: "appId", + kind: "config", + required: true, + sourceEnv: "MSTEAMS_APP_ID", + statePath: "teamsConfig.appId", + value: "2542103c-7a1e-408a-b2f3-667e09e86783", + }, + { + channelId: "teams", + inputId: "tenantId", + kind: "config", + required: true, + sourceEnv: "MSTEAMS_TENANT_ID", + statePath: "teamsConfig.tenantId", + value: "43083d15-7273-40c1-b7db-39efd9ccc17a", + }, + ], + }), + appliedPresets: ["teams"], + }); + const result = await showSandboxChannelStatus("alpha", { + deps, + channel: "teams", + }); + + const signals = result && "signals" in result ? result.signals : []; + expect(signals.filter((signal) => signal.label === "Rendered config source")).toEqual([ + expect.objectContaining({ + severity: "warn", + detail: "could not parse /sandbox/.openclaw/openclaw.json; config comparisons not checked", + }), + ]); + expect( + signals.find((signal) => signal.label === "Microsoft Teams Client ID (MSTEAMS_APP_ID)"), + ).toMatchObject({ + severity: "info", + detail: "2542103c-7a1e-408a-b2f3-667e09e86783 (not checked)", + }); + }); + it("treats 0/1 registry config as matching boolean rendered config", async () => { const { deps } = makeDeps({ exec: () => ({ @@ -1386,4 +1437,50 @@ describe("showSandboxChannelStatus (whatsapp)", () => { expect(dump).not.toMatch(/Telegram User ID \(for DM access\)/); expect(dump).toMatch(/Telegram group policy \(TELEGRAM_GROUP_POLICY\):\s+open \(default\)/); }); + + it("accepts Telegram disabled group policy from rendered config", async () => { + const { deps } = makeDeps({ + exec: () => ({ + status: 0, + stdout: JSON.stringify({ + channels: { + telegram: { + accounts: { + default: { + groupPolicy: "disabled", + }, + }, + }, + }, + }), + stderr: "", + }), + sandbox: entry(["telegram"], [], { + telegram: [ + { + channelId: "telegram", + inputId: "groupPolicy", + kind: "config", + required: false, + sourceEnv: "TELEGRAM_GROUP_POLICY", + statePath: "telegramConfig.groupPolicy", + value: "disabled", + }, + ], + }), + appliedPresets: ["telegram"], + }); + const result = await showSandboxChannelStatus("alpha", { + deps, + channel: "telegram", + }); + + const signals = result && "signals" in result ? result.signals : []; + expect( + signals.find((signal) => signal.label === "Telegram group policy (TELEGRAM_GROUP_POLICY)"), + ).toMatchObject({ + severity: "ok", + detail: "disabled", + }); + }); }); diff --git a/src/lib/messaging/channels/discord/rendered-config-parser.test.ts b/src/lib/messaging/channels/discord/rendered-config-parser.test.ts index 18517f8624b..d6a7b6d47ca 100644 --- a/src/lib/messaging/channels/discord/rendered-config-parser.test.ts +++ b/src/lib/messaging/channels/discord/rendered-config-parser.test.ts @@ -34,4 +34,35 @@ describe("discord rendered config parser", () => { }), ).toBeUndefined(); }); + + it("preserves requireMention value types when guilds differ", () => { + const requireMentionKey = discordRenderedConfigParser + .listConfigVisibilityKeys({ + agentId: "openclaw", + manifest: { id: "discord" } as ChannelManifest, + inputs: [], + }) + .find((key) => key.key === "guildRequireMention"); + + expect(requireMentionKey).toBeDefined(); + expect( + discordRenderedConfigParser.getValue(requireMentionKey!, { + kind: "structured", + value: { + channels: { + discord: { + guilds: { + "1504155275899437177": { + requireMention: true, + }, + "1504155275899437178": { + requireMention: false, + }, + }, + }, + }, + }, + }), + ).toEqual([true, false]); + }); }); diff --git a/src/lib/messaging/channels/discord/rendered-config-parser.ts b/src/lib/messaging/channels/discord/rendered-config-parser.ts index 13ec0e4c5f1..52f45bc64a3 100644 --- a/src/lib/messaging/channels/discord/rendered-config-parser.ts +++ b/src/lib/messaging/channels/discord/rendered-config-parser.ts @@ -79,8 +79,12 @@ function discordRequireMention( ) .filter((entry): entry is MessagingSerializableValue => entry !== undefined); if (values.length === 0) return undefined; - const uniqueValues = Array.from(new Set(values.map(formatProjectionValue))); - return uniqueValues.length === 1 ? values[0] : uniqueValues; + const uniqueValues = new Map(); + for (const value of values) { + uniqueValues.set(formatProjectionValue(value), value); + } + const dedupedValues = Array.from(uniqueValues.values()); + return dedupedValues.length === 1 ? dedupedValues[0] : dedupedValues; } function discordGuildUsers( diff --git a/src/lib/messaging/channels/manifests.test.ts b/src/lib/messaging/channels/manifests.test.ts index 07f8d799e3f..bc01f2bd03f 100644 --- a/src/lib/messaging/channels/manifests.test.ts +++ b/src/lib/messaging/channels/manifests.test.ts @@ -15,6 +15,7 @@ import type { ChannelInputSpec, ChannelManifest, ChannelRenderSpec, + MessagingAgentId, } from "../manifest"; import { BUILT_IN_CHANNEL_MANIFESTS, @@ -231,6 +232,23 @@ describe("built-in channel manifests", () => { ).toEqual(BUILT_IN_CHANNEL_MANIFESTS.map((manifest) => [manifest.id, true])); }); + it("keeps rendered config parser keys limited to manifest config inputs", () => { + const agentIds: readonly MessagingAgentId[] = ["openclaw", "hermes"]; + expect( + BUILT_IN_CHANNEL_MANIFESTS.flatMap((manifest) => { + const configInputIds: ReadonlySet = new Set( + manifest.inputs.filter((input) => input.kind === "config").map((input) => input.id), + ); + const parser = getBuiltInRenderedConfigParser(manifest.id); + return agentIds.flatMap((agentId) => + (parser?.listConfigVisibilityKeys({ manifest, agentId, inputs: [] }) ?? []) + .filter((key) => !configInputIds.has(key.inputId)) + .map((key) => `${manifest.id}.${agentId}.${key.inputId}`), + ); + }), + ).toEqual([]); + }); + it("keeps phase-1 manifest and hook files free of production side-effect imports", () => { const manifestPaths = [ "src/lib/messaging/channels/telegram/manifest.ts", From 98d47ad23e070256e618eaf922eea7db4aeb9584 Mon Sep 17 00:00:00 2001 From: San Dang Date: Tue, 30 Jun 2026 16:00:18 +0700 Subject: [PATCH 03/10] fix(cli): address channel status review findings Signed-off-by: San Dang --- docs/reference/commands-nemohermes.mdx | 11 +- docs/reference/commands.mdx | 11 +- src/commands/sandbox/channels/status.ts | 2 +- .../channel-status-config-channels.test.ts | 463 +++++++ .../channel-status-config-core.test.ts | 417 ++++++ .../channel-status-config-values.test.ts | 36 + .../sandbox/channel-status-config-values.ts | 56 + .../actions/sandbox/channel-status-config.ts | 62 +- .../sandbox/channel-status-summary.test.ts | 90 ++ .../channel-status-telegram-policy.test.ts | 95 ++ .../sandbox/channel-status.test-helpers.ts | 192 +++ .../actions/sandbox/channel-status.test.ts | 1172 +---------------- src/lib/actions/sandbox/channel-status.ts | 3 +- .../actions/sandbox/process-recovery.test.ts | 10 +- src/lib/actions/sandbox/process-recovery.ts | 3 +- .../discord/rendered-config-parser.test.ts | 97 +- src/lib/messaging/channels/manifests.test.ts | 10 +- .../channels/wechat/rendered-config-parser.ts | 2 +- 18 files changed, 1484 insertions(+), 1248 deletions(-) create mode 100644 src/lib/actions/sandbox/channel-status-config-channels.test.ts create mode 100644 src/lib/actions/sandbox/channel-status-config-core.test.ts create mode 100644 src/lib/actions/sandbox/channel-status-config-values.test.ts create mode 100644 src/lib/actions/sandbox/channel-status-config-values.ts create mode 100644 src/lib/actions/sandbox/channel-status-summary.test.ts create mode 100644 src/lib/actions/sandbox/channel-status-telegram-policy.test.ts create mode 100644 src/lib/actions/sandbox/channel-status.test-helpers.ts diff --git a/docs/reference/commands-nemohermes.mdx b/docs/reference/commands-nemohermes.mdx index 066a8e2c41f..15bac45e1dd 100644 --- a/docs/reference/commands-nemohermes.mdx +++ b/docs/reference/commands-nemohermes.mdx @@ -996,11 +996,16 @@ nemohermes my-assistant channels start telegram ### `nemohermes channels status` -Run messaging channel status checks. Without `--channel`, the command prints a compact summary for every configured channel, including registration, policy coverage, and non-secret rendered config comparisons. With `--channel`, it prints the detailed status for that channel. +Run messaging channel status checks. +Without `--channel`, the command prints a compact summary for every configured channel, including registration, policy coverage, and non-secret rendered config comparisons. +With `--channel`, it prints the detailed status for that channel. -For WhatsApp, `--channel whatsapp` also probes the sandbox to separately report pairing/session state, the Noise WebSocket connection, inbound event delivery, and policy/config coverage; a paired channel with no observed inbound delivery exits non-zero with verdict `idle` so an unhealthy bridge cannot pass as healthy. +For WhatsApp, `--channel whatsapp` also probes the sandbox to separately report pairing/session state, the Noise WebSocket connection, inbound event delivery, and policy coverage. +A paired channel with no observed inbound delivery exits non-zero with verdict `idle` so an unhealthy bridge cannot pass as healthy. +The detailed WhatsApp probe stays focused on QR/session runtime diagnostics and does not include rendered-config comparison lines. -For registered channels, the status output also compares non-secret config inputs from the sandbox registry against the values rendered into the agent config, such as Telegram group policy in `openclaw.json` or mention mode in Hermes config. Secret inputs, including tokens, are not printed. +For registered non-WhatsApp channel details and the compact summary, the status output compares non-secret config inputs from the sandbox registry against the values rendered into the agent config, such as Telegram group policy in `openclaw.json` or mention mode in Hermes config. +Secret inputs, including tokens, are not printed. ```bash nemohermes my-assistant channels status diff --git a/docs/reference/commands.mdx b/docs/reference/commands.mdx index 276c80f703c..2147b0dd826 100644 --- a/docs/reference/commands.mdx +++ b/docs/reference/commands.mdx @@ -1271,11 +1271,16 @@ $$nemoclaw my-assistant channels start telegram ### `$$nemoclaw channels status` -Run messaging channel status checks. Without `--channel`, the command prints a compact summary for every configured channel, including registration, policy coverage, and non-secret rendered config comparisons. With `--channel`, it prints the detailed status for that channel. +Run messaging channel status checks. +Without `--channel`, the command prints a compact summary for every configured channel, including registration, policy coverage, and non-secret rendered config comparisons. +With `--channel`, it prints the detailed status for that channel. -For WhatsApp, `--channel whatsapp` also probes the sandbox to separately report pairing/session state, the Noise WebSocket connection, inbound event delivery, and policy/config coverage; a paired channel with no observed inbound delivery exits non-zero with verdict `idle` so an unhealthy bridge cannot pass as healthy. +For WhatsApp, `--channel whatsapp` also probes the sandbox to separately report pairing/session state, the Noise WebSocket connection, inbound event delivery, and policy coverage. +A paired channel with no observed inbound delivery exits non-zero with verdict `idle` so an unhealthy bridge cannot pass as healthy. +The detailed WhatsApp probe stays focused on QR/session runtime diagnostics and does not include rendered-config comparison lines. -For registered channels, the status output also compares non-secret config inputs from the sandbox registry against the values rendered into the agent config, such as Telegram group policy in `openclaw.json` or mention mode in Hermes config. Secret inputs, including tokens, are not printed. +For registered non-WhatsApp channel details and the compact summary, the status output compares non-secret config inputs from the sandbox registry against the values rendered into the agent config, such as Telegram group policy in `openclaw.json` or mention mode in Hermes config. +Secret inputs, including tokens, are not printed. ```bash $$nemoclaw my-assistant channels status diff --git a/src/commands/sandbox/channels/status.ts b/src/commands/sandbox/channels/status.ts index b6ee3ce48ac..3045cd76ff6 100644 --- a/src/commands/sandbox/channels/status.ts +++ b/src/commands/sandbox/channels/status.ts @@ -13,7 +13,7 @@ export default class SandboxChannelsStatusCommand extends NemoClawCommand { static enableJsonFlag = true; static summary = "Inspect messaging channel status"; static description = - "Report configured messaging channels, policy coverage, and non-secret rendered config comparisons. Pass --channel whatsapp for the deeper WhatsApp QR/session and inbound-delivery probe."; + "Report configured messaging channels, policy coverage, and non-secret rendered config comparisons in the compact summary and non-WhatsApp detail views. Pass --channel whatsapp for the deeper WhatsApp QR/session and inbound-delivery probe."; static usage = [" [--channel ] [--json]"]; static examples = [ "<%= config.bin %> sandbox channels status alpha --channel whatsapp", diff --git a/src/lib/actions/sandbox/channel-status-config-channels.test.ts b/src/lib/actions/sandbox/channel-status-config-channels.test.ts new file mode 100644 index 00000000000..fc66ad85c6a --- /dev/null +++ b/src/lib/actions/sandbox/channel-status-config-channels.test.ts @@ -0,0 +1,463 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { describe, expect, it } from "vitest"; +import { + type ExecResult, + entry, + makeDeps, + showSandboxChannelStatus, +} from "./channel-status.test-helpers"; + +describe("showSandboxChannelStatus channel config parsers", () => { + it("compares Discord guild-derived render values", async () => { + const { deps, out_lines } = makeDeps({ + exec: () => ({ + status: 0, + stdout: JSON.stringify({ + channels: { + discord: { + guilds: { + "1504155275899437177": { + requireMention: true, + }, + }, + }, + }, + }), + stderr: "", + }), + sandbox: entry(["discord"], [], { + discord: [ + { + channelId: "discord", + inputId: "serverId", + kind: "config", + required: false, + sourceEnv: "DISCORD_SERVER_ID", + statePath: "discordGuilds.serverId", + value: "1504155275899437177", + }, + { + channelId: "discord", + inputId: "requireMention", + kind: "config", + required: false, + sourceEnv: "DISCORD_REQUIRE_MENTION", + statePath: "discordGuilds.requireMention", + value: "1", + }, + ], + }), + appliedPresets: ["discord"], + }); + const result = await showSandboxChannelStatus("alpha", { + deps, + channel: "discord", + }); + + const signals = result && "signals" in result ? result.signals : []; + expect( + signals.find( + (signal) => + signal.label === "Discord Server ID (for guild workspace access) (DISCORD_SERVER_ID)", + ), + ).toMatchObject({ + severity: "ok", + detail: "1504155275899437177", + }); + expect( + signals.find((signal) => signal.label === "Discord mention mode (DISCORD_REQUIRE_MENTION)"), + ).toMatchObject({ + severity: "ok", + detail: "1", + }); + expect( + signals.find( + (signal) => signal.label === "Discord User ID (optional guild allowlist) (DISCORD_USER_ID)", + ), + ).toBeUndefined(); + const dump = out_lines.join("\n"); + expect(dump).toMatch( + /Discord Server ID \(for guild workspace access\) \(DISCORD_SERVER_ID\):\s+1504155275899437177/, + ); + expect(dump).toMatch(/Discord mention mode \(DISCORD_REQUIRE_MENTION\):\s+1/); + }); + + it("compares Slack OpenClaw allowlist render values", async () => { + const { deps } = makeDeps({ + exec: () => ({ + status: 0, + stdout: JSON.stringify({ + channels: { + slack: { + accounts: { + default: { + allowFrom: ["U01ABC2DEF3"], + channels: { + C012AB3CD: { + enabled: true, + requireMention: true, + users: ["U01ABC2DEF3"], + }, + }, + }, + }, + }, + }, + }), + stderr: "", + }), + sandbox: entry(["slack"], [], { + slack: [ + { + channelId: "slack", + inputId: "allowedUsers", + kind: "config", + required: false, + sourceEnv: "SLACK_ALLOWED_USERS", + statePath: "allowedIds.slack", + value: "U01ABC2DEF3", + }, + { + channelId: "slack", + inputId: "allowedChannels", + kind: "config", + required: false, + sourceEnv: "SLACK_ALLOWED_CHANNELS", + statePath: "slackConfig.allowedChannels", + value: "C012AB3CD", + }, + ], + }), + appliedPresets: ["slack"], + }); + const result = await showSandboxChannelStatus("alpha", { + deps, + channel: "slack", + }); + + const signals = result && "signals" in result ? result.signals : []; + expect( + signals.find( + (signal) => + signal.label === "Slack Member IDs (comma-separated allowlist) (SLACK_ALLOWED_USERS)", + ), + ).toMatchObject({ + severity: "ok", + detail: "U01ABC2DEF3", + }); + expect( + signals.find( + (signal) => + signal.label === "Slack Channel IDs (comma-separated allowlist) (SLACK_ALLOWED_CHANNELS)", + ), + ).toMatchObject({ + severity: "ok", + detail: "C012AB3CD", + }); + }); + + it("does not treat Slack wildcard channel policy as configured channel IDs", async () => { + const { deps } = makeDeps({ + exec: () => ({ + status: 0, + stdout: JSON.stringify({ + channels: { + slack: { + accounts: { + default: { + allowFrom: ["U0B5BQABTL4"], + channels: { + "*": { + enabled: true, + requireMention: true, + users: ["U0B5BQABTL4"], + }, + }, + }, + }, + }, + }, + }), + stderr: "", + }), + sandbox: entry(["slack"], [], { + slack: [ + { + channelId: "slack", + inputId: "allowedUsers", + kind: "config", + required: false, + sourceEnv: "SLACK_ALLOWED_USERS", + statePath: "allowedIds.slack", + value: "U0B5BQABTL4", + }, + ], + }), + appliedPresets: ["slack"], + }); + const result = await showSandboxChannelStatus("alpha", { + deps, + channel: "slack", + }); + + const signals = result && "signals" in result ? result.signals : []; + expect( + signals.find( + (signal) => + signal.label === "Slack Member IDs (comma-separated allowlist) (SLACK_ALLOWED_USERS)", + ), + ).toMatchObject({ + severity: "ok", + detail: "U0B5BQABTL4", + }); + expect( + signals.find( + (signal) => + signal.label === "Slack Channel IDs (comma-separated allowlist) (SLACK_ALLOWED_CHANNELS)", + ), + ).toBeUndefined(); + }); + + it("compares OpenClaw WeChat account render values", async () => { + const renderedResponses: Array<[string, ExecResult]> = [ + [ + "/sandbox/.openclaw/openclaw.json", + { + status: 0, + stdout: JSON.stringify({ + channels: { + "openclaw-weixin": { + accounts: { + "wechat-account": { + enabled: true, + }, + }, + }, + }, + }), + stderr: "", + }, + ], + [ + "/sandbox/.openclaw/openclaw-weixin/accounts/wechat-account.json", + { + status: 0, + stdout: JSON.stringify({ + baseUrl: "https://ilinkai.wechat.com", + userId: "wechat-user", + }), + stderr: "", + }, + ], + ]; + const { deps } = makeDeps({ + exec: (_sandbox, command) => + renderedResponses.find(([needle]) => command.includes(needle))?.[1] ?? { + status: 1, + stdout: "", + stderr: "", + }, + sandbox: entry(["wechat"], [], { + wechat: [ + { + channelId: "wechat", + inputId: "accountId", + kind: "config", + required: true, + sourceEnv: "WECHAT_ACCOUNT_ID", + statePath: "wechatConfig.accountId", + value: "wechat-account", + }, + { + channelId: "wechat", + inputId: "baseUrl", + kind: "config", + required: false, + sourceEnv: "WECHAT_BASE_URL", + statePath: "wechatConfig.baseUrl", + value: "https://ilinkai.wechat.com", + }, + { + channelId: "wechat", + inputId: "userId", + kind: "config", + required: false, + sourceEnv: "WECHAT_USER_ID", + statePath: "wechatConfig.userId", + value: "wechat-user", + }, + { + channelId: "wechat", + inputId: "allowedIds", + kind: "config", + required: false, + sourceEnv: "WECHAT_ALLOWED_IDS", + statePath: "allowedIds.wechat", + value: "wechat-user", + }, + ], + }), + appliedPresets: ["wechat"], + }); + const result = await showSandboxChannelStatus("alpha", { + deps, + channel: "wechat", + }); + + const signals = result && "signals" in result ? result.signals : []; + expect(signals.find((signal) => signal.label === "WECHAT_ACCOUNT_ID")).toMatchObject({ + severity: "ok", + detail: "wechat-account", + }); + expect(signals.find((signal) => signal.label === "WECHAT_BASE_URL")).toMatchObject({ + severity: "ok", + detail: "https://ilinkai.wechat.com", + }); + expect(signals.find((signal) => signal.label === "WECHAT_USER_ID")).toMatchObject({ + severity: "ok", + detail: "wechat-user", + }); + expect( + signals.find( + (signal) => signal.label === "WeChat User ID(s) (DM allowlist) (WECHAT_ALLOWED_IDS)", + ), + ).toBeUndefined(); + }); + + it.each([ + "../x", + "a/b", + "bad'quote", + 'bad"quote', + "bad\nline", + ])("does not read derived WeChat account files for unsafe accountId %j", async (accountId) => { + const commands: string[] = []; + const { deps } = makeDeps({ + exec: (_sandbox, command) => { + commands.push(command); + return command.includes("/sandbox/.openclaw/openclaw.json") + ? { + status: 0, + stdout: JSON.stringify({ + channels: { + "openclaw-weixin": { + accounts: {}, + }, + }, + }), + stderr: "", + } + : { status: 1, stdout: "", stderr: "" }; + }, + sandbox: entry(["wechat"], [], { + wechat: [ + { + channelId: "wechat", + inputId: "accountId", + kind: "config", + required: true, + sourceEnv: "WECHAT_ACCOUNT_ID", + statePath: "wechatConfig.accountId", + value: accountId, + }, + ], + }), + appliedPresets: ["wechat"], + }); + await showSandboxChannelStatus("alpha", { + deps, + channel: "wechat", + }); + + expect(commands).toEqual(["cat '/sandbox/.openclaw/openclaw.json'"]); + }); + + it("compares Hermes WeChat values through rendered WEIXIN env keys", async () => { + const { deps } = makeDeps({ + exec: (_sandbox, command) => + command.includes("/sandbox/.hermes/.env") + ? { + status: 0, + stdout: [ + "WEIXIN_ACCOUNT_ID=wxid_abc", + "WEIXIN_BASE_URL=https://wechat.example.test", + "WEIXIN_ALLOWED_USERS=wxid_abc,wxid_def", + ].join("\n"), + stderr: "", + } + : { status: 1, stdout: "", stderr: "" }, + agentName: "hermes", + sandbox: entry( + ["wechat"], + [], + { + wechat: [ + { + channelId: "wechat", + inputId: "accountId", + kind: "config", + required: true, + sourceEnv: "WECHAT_ACCOUNT_ID", + statePath: "wechatConfig.accountId", + value: "wxid_abc", + }, + { + channelId: "wechat", + inputId: "baseUrl", + kind: "config", + required: false, + sourceEnv: "WECHAT_BASE_URL", + statePath: "wechatConfig.baseUrl", + value: "https://wechat.example.test", + }, + { + channelId: "wechat", + inputId: "userId", + kind: "config", + required: false, + sourceEnv: "WECHAT_USER_ID", + statePath: "wechatConfig.userId", + value: "wxid_abc", + }, + { + channelId: "wechat", + inputId: "allowedIds", + kind: "config", + required: false, + sourceEnv: "WECHAT_ALLOWED_IDS", + statePath: "allowedIds.wechat", + value: "wxid_abc,wxid_def", + }, + ], + }, + "hermes", + ), + appliedPresets: ["wechat"], + }); + const result = await showSandboxChannelStatus("alpha", { + deps, + channel: "wechat", + }); + + const signals = result && "signals" in result ? result.signals : []; + expect(signals.find((signal) => signal.label === "WECHAT_ACCOUNT_ID")).toMatchObject({ + severity: "ok", + detail: "wxid_abc", + }); + expect(signals.find((signal) => signal.label === "WECHAT_BASE_URL")).toMatchObject({ + severity: "ok", + detail: "https://wechat.example.test", + }); + expect( + signals.find( + (signal) => signal.label === "WeChat User ID(s) (DM allowlist) (WECHAT_ALLOWED_IDS)", + ), + ).toMatchObject({ + severity: "ok", + detail: "wxid_abc,wxid_def", + }); + expect(signals.find((signal) => signal.label === "WECHAT_USER_ID")).toBeUndefined(); + }); +}); diff --git a/src/lib/actions/sandbox/channel-status-config-core.test.ts b/src/lib/actions/sandbox/channel-status-config-core.test.ts new file mode 100644 index 00000000000..aae61677d53 --- /dev/null +++ b/src/lib/actions/sandbox/channel-status-config-core.test.ts @@ -0,0 +1,417 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { describe, expect, it } from "vitest"; +import { entry, makeDeps, showSandboxChannelStatus } from "./channel-status.test-helpers"; + +describe("showSandboxChannelStatus config comparison", () => { + it("marks rendered config ok when the sandbox config matches the sandbox entry", async () => { + const { deps, out_lines } = makeDeps({ + exec: (_sandbox, command) => + command.includes("/sandbox/.openclaw/openclaw.json") + ? { + status: 0, + stdout: JSON.stringify({ + channels: { + telegram: { + accounts: { + default: { + groupPolicy: "allowlist", + }, + }, + }, + }, + }), + stderr: "", + } + : { status: 1, stdout: "", stderr: "" }, + sandbox: entry(["telegram"], [], { + telegram: [ + { + channelId: "telegram", + inputId: "botToken", + kind: "secret", + required: true, + sourceEnv: "TELEGRAM_BOT_TOKEN", + credentialAvailable: true, + }, + { + channelId: "telegram", + inputId: "requireMention", + kind: "config", + required: false, + sourceEnv: "TELEGRAM_REQUIRE_MENTION", + statePath: "telegramConfig.requireMention", + value: "1", + }, + { + channelId: "telegram", + inputId: "groupPolicy", + kind: "config", + required: false, + sourceEnv: "TELEGRAM_GROUP_POLICY", + statePath: "telegramConfig.groupPolicy", + value: "allowlist", + }, + ], + }), + appliedPresets: ["telegram"], + }); + const result = await showSandboxChannelStatus("alpha", { + deps, + channel: "telegram", + }); + + expect(result && "verdict" in result && result.verdict).toBe("info"); + const signals = result && "signals" in result ? result.signals : []; + expect( + signals.find((signal) => signal.label === "Telegram group policy (TELEGRAM_GROUP_POLICY)"), + ).toMatchObject({ + severity: "ok", + detail: "allowlist", + }); + expect( + signals.find( + (signal) => signal.label === "Telegram group mention mode (TELEGRAM_REQUIRE_MENTION)", + ), + ).toBeUndefined(); + const dump = out_lines.join("\n"); + expect(dump).toMatch(/Telegram group policy \(TELEGRAM_GROUP_POLICY\):\s+allowlist/); + expect(dump).not.toMatch(/Telegram Bot Token/); + expect(dump).not.toMatch(/TELEGRAM_BOT_TOKEN/); + }); + + it("compares Hermes Telegram group policy from rendered env config", async () => { + const { deps } = makeDeps({ + exec: (_sandbox, command) => + command.includes("/sandbox/.hermes/.env") + ? { + status: 0, + stdout: ["TELEGRAM_ALLOWED_USERS=7895072570", "TELEGRAM_GROUP_POLICY=allowlist"].join( + "\n", + ), + stderr: "", + } + : { status: 1, stdout: "", stderr: "" }, + agentName: "hermes", + sandbox: entry( + ["telegram"], + [], + { + telegram: [ + { + channelId: "telegram", + inputId: "groupPolicy", + kind: "config", + required: false, + sourceEnv: "TELEGRAM_GROUP_POLICY", + statePath: "telegramConfig.groupPolicy", + value: "allowlist", + }, + ], + }, + "hermes", + ), + appliedPresets: ["telegram"], + }); + const result = await showSandboxChannelStatus("alpha", { + deps, + channel: "telegram", + }); + + const signals = result && "signals" in result ? result.signals : []; + expect( + signals.find((signal) => signal.label === "Telegram group policy (TELEGRAM_GROUP_POLICY)"), + ).toMatchObject({ + severity: "ok", + detail: "allowlist", + }); + }); + + it("warns when rendered config differs from the sandbox entry", async () => { + const { deps } = makeDeps({ + exec: () => ({ + status: 0, + stdout: JSON.stringify({ + channels: { + telegram: { + accounts: { + default: { + groupPolicy: "open", + }, + }, + }, + }, + }), + stderr: "", + }), + sandbox: entry(["telegram"], [], { + telegram: [ + { + channelId: "telegram", + inputId: "groupPolicy", + kind: "config", + required: false, + sourceEnv: "TELEGRAM_GROUP_POLICY", + statePath: "telegramConfig.groupPolicy", + value: "allowlist", + }, + ], + }), + appliedPresets: ["telegram"], + }); + const result = await showSandboxChannelStatus("alpha", { + deps, + channel: "telegram", + }); + + const signals = result && "signals" in result ? result.signals : []; + expect( + signals.find((signal) => signal.label === "Telegram group policy (TELEGRAM_GROUP_POLICY)"), + ).toMatchObject({ + severity: "warn", + detail: "expected allowlist; rendered open", + }); + }); + + it("warns once when a shared rendered config source is unreadable", async () => { + const { deps, out_lines } = makeDeps({ + exec: () => ({ + status: 1, + stdout: "", + stderr: "cat: /sandbox/.openclaw/openclaw.json: No such file or directory", + }), + sandbox: entry(["teams"], [], { + teams: [ + { + channelId: "teams", + inputId: "appId", + kind: "config", + required: true, + sourceEnv: "MSTEAMS_APP_ID", + statePath: "teamsConfig.appId", + value: "2542103c-7a1e-408a-b2f3-667e09e86783", + }, + { + channelId: "teams", + inputId: "tenantId", + kind: "config", + required: true, + sourceEnv: "MSTEAMS_TENANT_ID", + statePath: "teamsConfig.tenantId", + value: "43083d15-7273-40c1-b7db-39efd9ccc17a", + }, + { + channelId: "teams", + inputId: "allowedUsers", + kind: "config", + required: false, + sourceEnv: "TEAMS_ALLOWED_USERS", + statePath: "allowedIds.teams", + value: "205f29da-231e-4a0e-a0b2-b398e6302087", + }, + { + channelId: "teams", + inputId: "webhookPort", + kind: "config", + required: false, + sourceEnv: "MSTEAMS_PORT", + statePath: "teamsConfig.webhookPort", + value: "3978", + }, + { + channelId: "teams", + inputId: "requireMention", + kind: "config", + required: false, + sourceEnv: "TEAMS_REQUIRE_MENTION", + statePath: "teamsConfig.requireMention", + value: "1", + }, + ], + }), + appliedPresets: ["teams"], + }); + const result = await showSandboxChannelStatus("alpha", { + deps, + channel: "teams", + }); + + const signals = result && "signals" in result ? result.signals : []; + const sourceWarnings = signals.filter((signal) => signal.label === "Rendered config source"); + expect(sourceWarnings).toHaveLength(1); + expect(sourceWarnings[0]).toMatchObject({ + severity: "warn", + detail: "could not read /sandbox/.openclaw/openclaw.json; config comparisons not checked", + }); + expect( + signals.find((signal) => signal.label === "Microsoft Teams Client ID (MSTEAMS_APP_ID)"), + ).toMatchObject({ + severity: "info", + detail: "2542103c-7a1e-408a-b2f3-667e09e86783 (not checked)", + }); + expect( + signals.find( + (signal) => + signal.label === + "Microsoft Teams AAD Object IDs (comma-separated allowlist) (TEAMS_ALLOWED_USERS)", + ), + ).toMatchObject({ + severity: "info", + detail: "205f29da-231e-4a0e-a0b2-b398e6302087 (not checked)", + }); + expect( + signals.find( + (signal) => signal.label === "Microsoft Teams mention mode (TEAMS_REQUIRE_MENTION)", + ), + ).toMatchObject({ + severity: "info", + detail: "1 (not checked)", + }); + const sourceReadFailures = out_lines + .join("\n") + .match(/could not read \/sandbox\/\.openclaw\/openclaw\.json/g); + expect(sourceReadFailures).toHaveLength(1); + }); + + it("warns once when a shared rendered config source is malformed", async () => { + const { deps } = makeDeps({ + exec: () => ({ + status: 0, + stdout: "{not-json", + stderr: "", + }), + sandbox: entry(["teams"], [], { + teams: [ + { + channelId: "teams", + inputId: "appId", + kind: "config", + required: true, + sourceEnv: "MSTEAMS_APP_ID", + statePath: "teamsConfig.appId", + value: "2542103c-7a1e-408a-b2f3-667e09e86783", + }, + { + channelId: "teams", + inputId: "tenantId", + kind: "config", + required: true, + sourceEnv: "MSTEAMS_TENANT_ID", + statePath: "teamsConfig.tenantId", + value: "43083d15-7273-40c1-b7db-39efd9ccc17a", + }, + ], + }), + appliedPresets: ["teams"], + }); + const result = await showSandboxChannelStatus("alpha", { + deps, + channel: "teams", + }); + + const signals = result && "signals" in result ? result.signals : []; + expect(signals.filter((signal) => signal.label === "Rendered config source")).toEqual([ + expect.objectContaining({ + severity: "warn", + detail: "could not parse /sandbox/.openclaw/openclaw.json; config comparisons not checked", + }), + ]); + expect( + signals.find((signal) => signal.label === "Microsoft Teams Client ID (MSTEAMS_APP_ID)"), + ).toMatchObject({ + severity: "info", + detail: "2542103c-7a1e-408a-b2f3-667e09e86783 (not checked)", + }); + }); + + it("treats 0/1 registry config as matching boolean rendered config", async () => { + const { deps } = makeDeps({ + exec: () => ({ + status: 0, + stdout: JSON.stringify({ + channels: { + msteams: { + requireMention: true, + }, + }, + }), + stderr: "", + }), + sandbox: entry(["teams"], [], { + teams: [ + { + channelId: "teams", + inputId: "requireMention", + kind: "config", + required: false, + sourceEnv: "TEAMS_REQUIRE_MENTION", + statePath: "teamsConfig.requireMention", + value: "1", + }, + ], + }), + appliedPresets: ["teams"], + }); + const result = await showSandboxChannelStatus("alpha", { + deps, + channel: "teams", + }); + + const signals = result && "signals" in result ? result.signals : []; + expect( + signals.find( + (signal) => signal.label === "Microsoft Teams mention mode (TEAMS_REQUIRE_MENTION)", + ), + ).toMatchObject({ + severity: "ok", + detail: "1", + }); + }); + + it("compares manifest-derived allowlist render values", async () => { + const { deps } = makeDeps({ + exec: () => ({ + status: 0, + stdout: JSON.stringify({ + channels: { + msteams: { + allowFrom: ["205f29da-231e-4a0e-a0b2-b398e6302087"], + }, + }, + }), + stderr: "", + }), + sandbox: entry(["teams"], [], { + teams: [ + { + channelId: "teams", + inputId: "allowedUsers", + kind: "config", + required: false, + sourceEnv: "TEAMS_ALLOWED_USERS", + statePath: "allowedIds.teams", + value: "205f29da-231e-4a0e-a0b2-b398e6302087", + }, + ], + }), + appliedPresets: ["teams"], + }); + const result = await showSandboxChannelStatus("alpha", { + deps, + channel: "teams", + }); + + const signals = result && "signals" in result ? result.signals : []; + expect( + signals.find( + (signal) => + signal.label === + "Microsoft Teams AAD Object IDs (comma-separated allowlist) (TEAMS_ALLOWED_USERS)", + ), + ).toMatchObject({ + severity: "ok", + detail: "205f29da-231e-4a0e-a0b2-b398e6302087", + }); + }); +}); diff --git a/src/lib/actions/sandbox/channel-status-config-values.test.ts b/src/lib/actions/sandbox/channel-status-config-values.test.ts new file mode 100644 index 00000000000..5dbd97ab2f8 --- /dev/null +++ b/src/lib/actions/sandbox/channel-status-config-values.test.ts @@ -0,0 +1,36 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { describe, expect, it } from "vitest"; +import { + booleanConfigValue, + configInputDetail, + configValuesEqual, + formatConfigValue, + listConfigValues, +} from "./channel-status-config-values"; + +describe("channel status config value helpers", () => { + it("formats scalar and list values for status output", () => { + expect(configInputDetail(undefined)).toBe("not set"); + expect(formatConfigValue("")).toBe('""'); + expect(formatConfigValue(["b", "a"])).toBe("b, a"); + expect(formatConfigValue([])).toBe("[]"); + expect(formatConfigValue({ enabled: true })).toBe('{"enabled":true}'); + }); + + it("normalizes comma-separated and array values before comparing", () => { + expect(listConfigValues("b, a,")).toEqual(["a", "b"]); + expect(configValuesEqual("b,a", ["a", "b"])).toBe(true); + expect(configValuesEqual("a,b", ["a", "c"])).toBe(false); + }); + + it("normalizes boolean-like config values before comparing", () => { + expect(booleanConfigValue("1")).toBe(true); + expect(booleanConfigValue("false")).toBe(false); + expect(booleanConfigValue("enabled")).toBeNull(); + expect(configValuesEqual("1", true)).toBe(true); + expect(configValuesEqual("0", false)).toBe(true); + expect(configValuesEqual("true", false)).toBe(false); + }); +}); diff --git a/src/lib/actions/sandbox/channel-status-config-values.ts b/src/lib/actions/sandbox/channel-status-config-values.ts new file mode 100644 index 00000000000..66b03c4ca27 --- /dev/null +++ b/src/lib/actions/sandbox/channel-status-config-values.ts @@ -0,0 +1,56 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import type { MessagingSerializableValue } from "../../messaging/manifest"; + +export function configInputDetail(value: MessagingSerializableValue | undefined): string { + if (value === undefined || value === null) return "not set"; + return formatConfigValue(value); +} + +export function formatConfigValue(value: MessagingSerializableValue): string { + if (typeof value === "string") return value.length === 0 ? '""' : value; + if (typeof value === "number" || typeof value === "boolean") return String(value); + if (Array.isArray(value)) { + if (value.length === 0) return "[]"; + return value.map(formatConfigValue).join(", "); + } + return JSON.stringify(value); +} + +export function configValuesEqual( + expected: MessagingSerializableValue | undefined, + actual: MessagingSerializableValue | undefined, +): boolean { + if (expected === undefined || expected === null) return actual === undefined || actual === null; + if (actual === undefined || actual === null) return false; + if (Array.isArray(expected) || Array.isArray(actual)) { + const expectedList = listConfigValues(expected); + const actualList = listConfigValues(actual); + return ( + expectedList.length === actualList.length && + expectedList.every((value, index) => value === actualList[index]) + ); + } + const expectedBoolean = booleanConfigValue(expected); + const actualBoolean = booleanConfigValue(actual); + if (expectedBoolean !== null && actualBoolean !== null) return expectedBoolean === actualBoolean; + return formatConfigValue(expected) === formatConfigValue(actual); +} + +export function listConfigValues(value: MessagingSerializableValue): string[] { + const values = Array.isArray(value) ? value : String(value).split(","); + return values + .map((entry) => String(entry).trim()) + .filter((entry) => entry.length > 0) + .sort(); +} + +export function booleanConfigValue(value: MessagingSerializableValue): boolean | null { + if (typeof value === "boolean") return value; + if (typeof value !== "string") return null; + const normalized = value.trim().toLowerCase(); + if (normalized === "1" || normalized === "true") return true; + if (normalized === "0" || normalized === "false") return false; + return null; +} diff --git a/src/lib/actions/sandbox/channel-status-config.ts b/src/lib/actions/sandbox/channel-status-config.ts index 55cdc0401d2..cee5f0747db 100644 --- a/src/lib/actions/sandbox/channel-status-config.ts +++ b/src/lib/actions/sandbox/channel-status-config.ts @@ -4,6 +4,7 @@ import YAML from "yaml"; import type { AgentDefinition } from "../../agent/defs"; import { CLI_NAME } from "../../cli/branding"; +import { shellQuote } from "../../core/shell-quote"; import type { RenderedChannelConfigParser, RenderedConfigSource, @@ -22,6 +23,7 @@ import type { } from "../../messaging/manifest"; import type { DiagnosticSignal } from "../../sandbox/whatsapp-diagnostics"; import * as registry from "../../state/registry"; +import { configInputDetail, configValuesEqual } from "./channel-status-config-values"; const CONFIG_STATUS_TIMEOUT_MS = 5_000; const channelManifestRegistry = createBuiltInChannelManifestRegistry(); @@ -40,12 +42,6 @@ export type ChannelStatusConfigDeps = { execSandbox: ExecRunner; }; -// Inline single-quote shell quoting — config status probes only quote trusted -// path strings derived from agent/channel manifests. -export function quotePath(value: string): string { - return `'${value.replace(/'/g, "'\\''")}'`; -} - export function buildConfigStatusSignals( sandboxName: string, channelName: string, @@ -143,11 +139,6 @@ function configInputLabel( return `${label} (${envKey})`; } -function configInputDetail(value: MessagingSerializableValue | undefined): string { - if (value === undefined || value === null) return "not set"; - return formatConfigValue(value); -} - type ExpectedConfigValue = { readonly value: MessagingSerializableValue | undefined; readonly detail: string; @@ -182,16 +173,6 @@ function expectedConfigValue( }; } -function formatConfigValue(value: MessagingSerializableValue): string { - if (typeof value === "string") return value.length === 0 ? '""' : value; - if (typeof value === "number" || typeof value === "boolean") return String(value); - if (Array.isArray(value)) { - if (value.length === 0) return "[]"; - return value.map(formatConfigValue).join(", "); - } - return JSON.stringify(value); -} - interface ConfigRenderSource extends RenderedConfigVisibilityKey { readonly resolvedTarget: string; } @@ -301,7 +282,7 @@ function readConfigSourceValues( for (const target of new Set(sources.map((source) => source.resolvedTarget))) { const result = deps.execSandbox( sandboxName, - `cat ${quotePath(target)}`, + `cat ${shellQuote(target)}`, CONFIG_STATUS_TIMEOUT_MS, ); targetReads.set( @@ -410,40 +391,3 @@ function compareConfigSource( function configSourceKey(source: ConfigRenderSource): string { return `${source.resolvedTarget}:${source.kind}:${source.key}`; } - -function configValuesEqual( - expected: MessagingSerializableValue | undefined, - actual: MessagingSerializableValue | undefined, -): boolean { - if (expected === undefined || expected === null) return actual === undefined || actual === null; - if (actual === undefined || actual === null) return false; - if (Array.isArray(expected) || Array.isArray(actual)) { - const expectedList = listConfigValues(expected); - const actualList = listConfigValues(actual); - return ( - expectedList.length === actualList.length && - expectedList.every((value, index) => value === actualList[index]) - ); - } - const expectedBoolean = booleanConfigValue(expected); - const actualBoolean = booleanConfigValue(actual); - if (expectedBoolean !== null && actualBoolean !== null) return expectedBoolean === actualBoolean; - return formatConfigValue(expected) === formatConfigValue(actual); -} - -function listConfigValues(value: MessagingSerializableValue): string[] { - const values = Array.isArray(value) ? value : String(value).split(","); - return values - .map((entry) => String(entry).trim()) - .filter((entry) => entry.length > 0) - .sort(); -} - -function booleanConfigValue(value: MessagingSerializableValue): boolean | null { - if (typeof value === "boolean") return value; - if (typeof value !== "string") return null; - const normalized = value.trim().toLowerCase(); - if (normalized === "1" || normalized === "true") return true; - if (normalized === "0" || normalized === "false") return false; - return null; -} diff --git a/src/lib/actions/sandbox/channel-status-summary.test.ts b/src/lib/actions/sandbox/channel-status-summary.test.ts new file mode 100644 index 00000000000..39c85f52372 --- /dev/null +++ b/src/lib/actions/sandbox/channel-status-summary.test.ts @@ -0,0 +1,90 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { describe, expect, it } from "vitest"; +import { entry, makeDeps, showSandboxChannelStatus } from "./channel-status.test-helpers"; + +describe("showSandboxChannelStatus summary", () => { + it("emits a compact all-channel report when no channel is selected", async () => { + const commands: string[] = []; + const { deps, out_lines } = makeDeps({ + exec: (_sandbox, command) => { + commands.push(command); + return command.includes("/sandbox/.openclaw/openclaw.json") + ? { + status: 0, + stdout: JSON.stringify({ + channels: { + telegram: { + accounts: { + default: { + groupPolicy: "open", + }, + }, + }, + }, + }), + stderr: "", + } + : { status: 1, stdout: "", stderr: "" }; + }, + sandbox: entry(["telegram", "whatsapp"], [], { + telegram: [ + { + channelId: "telegram", + inputId: "groupPolicy", + kind: "config", + required: false, + sourceEnv: "TELEGRAM_GROUP_POLICY", + statePath: "telegramConfig.groupPolicy", + value: "open", + }, + ], + }), + appliedPresets: ["telegram", "whatsapp"], + }); + const result = await showSandboxChannelStatus("alpha", { deps }); + + expect( + result && "channels" in result && result.channels.map((channel) => channel.channel), + ).toEqual(["telegram", "whatsapp"]); + expect(commands.join("\n")).not.toMatch(/NEMOCLAW_WA_DIAG_OK/); + const dump = out_lines.join("\n"); + expect(dump).toMatch(/NemoClaw channels status:.*alpha/); + expect(dump).toMatch(/\btelegram\b/); + expect(dump).toMatch(/\bwhatsapp\b/); + expect(dump).toMatch(/Telegram group policy \(TELEGRAM_GROUP_POLICY\):\s+open/); + expect(dump).not.toMatch(/Deep diagnostics/); + expect(dump).not.toMatch(/Probed at/); + }); + + it("prints an empty-state hint when no channels are configured", async () => { + const { deps, out_lines } = makeDeps({ + exec: () => ({ status: 0, stdout: "", stderr: "" }), + sandbox: entry([]), + appliedPresets: [], + }); + const result = await showSandboxChannelStatus("alpha", { deps }); + + expect(result && "channels" in result && result.channels).toEqual([]); + const dump = out_lines.join("\n"); + expect(dump).toMatch(/Configured channels: none/); + expect(dump).toMatch(/channels add /); + }); + + it("emits a basic per-channel report for non-whatsapp channels", async () => { + const { deps, out_lines } = makeDeps({ + exec: () => ({ status: 0, stdout: "", stderr: "" }), + sandbox: entry(["telegram"]), + appliedPresets: ["telegram"], + }); + const result = await showSandboxChannelStatus("alpha", { + deps, + channel: "telegram", + }); + expect(result && "verdict" in result && result.verdict).toBe("info"); + const dump = out_lines.join("\n"); + expect(dump).toMatch(/telegram registered/); + expect(dump).toMatch(/preset applied/); + }); +}); diff --git a/src/lib/actions/sandbox/channel-status-telegram-policy.test.ts b/src/lib/actions/sandbox/channel-status-telegram-policy.test.ts new file mode 100644 index 00000000000..b4a47cc1184 --- /dev/null +++ b/src/lib/actions/sandbox/channel-status-telegram-policy.test.ts @@ -0,0 +1,95 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { describe, expect, it } from "vitest"; +import { entry, makeDeps, showSandboxChannelStatus } from "./channel-status.test-helpers"; + +describe("showSandboxChannelStatus Telegram group policy", () => { + it("uses manifest defaults when no stored config value exists", async () => { + const { deps, out_lines } = makeDeps({ + exec: () => ({ + status: 0, + stdout: JSON.stringify({ + channels: { + telegram: { + accounts: { + default: { + groupPolicy: "open", + }, + }, + }, + }, + }), + stderr: "", + }), + sandbox: entry(["telegram"]), + appliedPresets: ["telegram"], + }); + const result = await showSandboxChannelStatus("alpha", { + deps, + channel: "telegram", + }); + + const signals = result && "signals" in result ? result.signals : []; + expect( + signals.find((signal) => signal.label === "Telegram group policy (TELEGRAM_GROUP_POLICY)"), + ).toMatchObject({ + severity: "ok", + detail: "open (default)", + }); + expect( + signals.find( + (signal) => signal.label === "Telegram group mention mode (TELEGRAM_REQUIRE_MENTION)", + ), + ).toBeUndefined(); + const dump = out_lines.join("\n"); + expect(dump).not.toMatch(/Telegram User ID \(for DM access\)/); + expect(dump).toMatch(/Telegram group policy \(TELEGRAM_GROUP_POLICY\):\s+open \(default\)/); + }); + + it("accepts Telegram disabled group policy from rendered config", async () => { + const { deps } = makeDeps({ + exec: () => ({ + status: 0, + stdout: JSON.stringify({ + channels: { + telegram: { + accounts: { + default: { + groupPolicy: "disabled", + }, + }, + }, + }, + }), + stderr: "", + }), + sandbox: entry(["telegram"], [], { + telegram: [ + { + channelId: "telegram", + inputId: "groupPolicy", + kind: "config", + required: false, + sourceEnv: "TELEGRAM_GROUP_POLICY", + statePath: "telegramConfig.groupPolicy", + value: "disabled", + }, + ], + }), + appliedPresets: ["telegram"], + }); + const result = await showSandboxChannelStatus("alpha", { + deps, + channel: "telegram", + }); + + const signals = result && "signals" in result ? result.signals : []; + expect( + signals.find((signal) => signal.label === "Telegram group policy (TELEGRAM_GROUP_POLICY)"), + ).toMatchObject({ + severity: "ok", + detail: "disabled", + }); + }); +}); diff --git a/src/lib/actions/sandbox/channel-status.test-helpers.ts b/src/lib/actions/sandbox/channel-status.test-helpers.ts new file mode 100644 index 00000000000..330c8e23d66 --- /dev/null +++ b/src/lib/actions/sandbox/channel-status.test-helpers.ts @@ -0,0 +1,192 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { vi } from "vitest"; + +// The orchestrator transitively pulls in policy/index.ts and agent/defs.ts, +// both of which require runner.ts via CJS; runner.ts uses `require()` calls +// vitest cannot resolve from a TS source file. Stub the heavy modules so the +// tests stay focused on the orchestrator's diagnostic glue. See +// src/lib/shields/index.test.ts for the same workaround pattern. +vi.mock("../../policy", () => ({ + getAppliedPresets: vi.fn(() => []), + getGatewayPresets: vi.fn(() => null), +})); + +vi.mock("../../state/registry", () => ({ + getSandbox: vi.fn(), + getMessagingPlanFromEntry: vi.fn((entry) => entry?.messaging?.plan ?? null), + getConfiguredMessagingChannelsFromEntry: vi.fn((entry) => { + const channels = entry?.messaging?.plan?.channels; + return Array.isArray(channels) + ? channels + .filter((channel) => channel?.configured === true) + .map((channel) => channel.channelId) + : []; + }), + getDisabledMessagingChannelsFromEntry: vi.fn((entry) => { + const disabled = entry?.messaging?.plan?.disabledChannels; + return Array.isArray(disabled) ? [...disabled] : []; + }), +})); + +vi.mock("../../agent/defs", () => ({ + loadAgent: vi.fn(), +})); + +vi.mock("./process-recovery", () => ({ + executeSandboxExecCommand: vi.fn(), +})); + +import type { AgentDefinition } from "../../agent/defs"; +import type { SandboxMessagingInputReference } from "../../messaging/manifest"; +import type { SandboxEntry } from "../../state/registry"; + +type ShowSandboxChannelStatus = typeof import("./channel-status").showSandboxChannelStatus; + +export const showSandboxChannelStatus = (async (...args: Parameters) => { + const mod = await import("./channel-status"); + return mod.showSandboxChannelStatus(...args); +}) as ShowSandboxChannelStatus; + +export type ExecResult = { status: number; stdout: string; stderr: string }; + +const PROBED_AT = new Date("2026-05-28T04:00:00.000Z"); + +function fakeAgent(name: "openclaw" | "hermes" = "openclaw"): AgentDefinition { + const configDir = name === "openclaw" ? "/sandbox/.openclaw" : "/sandbox/.hermes"; + const stateDirs = name === "openclaw" ? ["whatsapp"] : ["platforms"]; + return { + name, + agentDir: `/fake/${name}`, + manifestPath: `/fake/${name}/manifest.yaml`, + get displayName() { + return name; + }, + get healthProbe() { + return { url: "http://localhost:0/", port: 0, timeout_seconds: 5 }; + }, + get forwardPort() { + return 0; + }, + get dashboard() { + return { kind: "ui" as const, label: "UI", path: "/" }; + }, + get configPaths() { + return { + dir: configDir, + configFile: name === "openclaw" ? "openclaw.json" : "config.yaml", + envFile: name === "hermes" ? ".env" : null, + format: name === "openclaw" ? "json" : "yaml", + }; + }, + get inferenceProviderOptions() { + return []; + }, + get stateDirs() { + return stateDirs; + }, + get stateFiles() { + return []; + }, + get versionCommand() { + return `${name} --version`; + }, + get expectedVersion() { + return null; + }, + get hasDevicePairing() { + return false; + }, + get phoneHomeHosts() { + return []; + }, + get dockerfileBasePath() { + return null; + }, + get dockerfilePath() { + return null; + }, + get startScriptPath() { + return null; + }, + get policyAdditionsPath() { + return null; + }, + get policyPermissivePath() { + return null; + }, + get pluginDir() { + return null; + }, + get legacyPaths() { + return null; + }, + } as unknown as AgentDefinition; +} + +export function entry( + messagingChannels: string[] = ["whatsapp"], + disabledChannels: string[] = [], + channelInputs: Record = {}, + agentName: "openclaw" | "hermes" = "openclaw", +): SandboxEntry { + const disabled = new Set(disabledChannels); + return { + name: "alpha", + agent: agentName, + messaging: { + schemaVersion: 1, + plan: { + schemaVersion: 1, + sandboxName: "alpha", + agent: agentName, + workflow: "onboard", + channels: messagingChannels.map((channelId) => ({ + channelId, + displayName: channelId, + authMode: channelId === "whatsapp" ? "in-sandbox-qr" : "token-paste", + active: !disabled.has(channelId), + selected: true, + configured: true, + disabled: disabled.has(channelId), + inputs: channelInputs[channelId] ?? [], + hooks: [], + })), + disabledChannels, + credentialBindings: [], + networkPolicy: { presets: [], entries: [] }, + agentRender: [], + buildSteps: [], + stateUpdates: [], + healthChecks: [], + }, + }, + } as SandboxEntry; +} + +export function makeDeps(opts: { + exec: (sandboxName: string, command: string, timeoutMs?: number) => ExecResult | null; + appliedPresets?: string[]; + gatewayPresets?: string[] | null; + agentName?: "openclaw" | "hermes"; + sandbox?: SandboxEntry | undefined; + out?: (line: string) => void; +}) { + const calls: string[] = []; + const out = opts.out ?? ((line: string) => calls.push(line)); + return { + out, + deps: { + loadAgent: () => fakeAgent(opts.agentName), + getSandbox: () => opts.sandbox ?? entry(), + getAppliedPresets: () => opts.appliedPresets ?? ["whatsapp"], + getGatewayPresets: () => + opts.gatewayPresets === undefined ? ["whatsapp"] : opts.gatewayPresets, + execSandbox: vi.fn(opts.exec), + now: () => PROBED_AT, + out, + }, + out_lines: calls, + }; +} diff --git a/src/lib/actions/sandbox/channel-status.test.ts b/src/lib/actions/sandbox/channel-status.test.ts index 7876eba9812..1f045cd87e3 100644 --- a/src/lib/actions/sandbox/channel-status.test.ts +++ b/src/lib/actions/sandbox/channel-status.test.ts @@ -2,188 +2,12 @@ // SPDX-License-Identifier: Apache-2.0 import { describe, expect, it, vi } from "vitest"; - -// The orchestrator transitively pulls in policy/index.ts and agent/defs.ts, -// both of which require runner.ts via CJS; runner.ts uses `require()` calls -// vitest cannot resolve from a TS source file. Stub the heavy modules so the -// test stays focused on the orchestrator's diagnostic glue. See -// src/lib/shields/index.test.ts for the same workaround pattern. -vi.mock("../../policy", () => ({ - getAppliedPresets: vi.fn(() => []), - getGatewayPresets: vi.fn(() => null), -})); - -vi.mock("../../state/registry", () => ({ - getSandbox: vi.fn(), - getMessagingPlanFromEntry: vi.fn((entry) => entry?.messaging?.plan ?? null), - getConfiguredMessagingChannelsFromEntry: vi.fn((entry) => { - const channels = entry?.messaging?.plan?.channels; - return Array.isArray(channels) - ? channels - .filter((channel) => channel?.configured === true) - .map((channel) => channel.channelId) - : []; - }), - getDisabledMessagingChannelsFromEntry: vi.fn((entry) => { - const disabled = entry?.messaging?.plan?.disabledChannels; - return Array.isArray(disabled) ? [...disabled] : []; - }), -})); - -vi.mock("../../agent/defs", () => ({ - loadAgent: vi.fn(), -})); - -vi.mock("./process-recovery", () => ({ - executeSandboxExecCommand: vi.fn(), -})); - -import type { AgentDefinition } from "../../agent/defs"; -import type { SandboxMessagingInputReference } from "../../messaging/manifest"; -import type { SandboxEntry } from "../../state/registry"; -import { showSandboxChannelStatus } from "./channel-status"; - -type ExecResult = { status: number; stdout: string; stderr: string }; - -const PROBED_AT = new Date("2026-05-28T04:00:00.000Z"); - -function fakeAgent(name: "openclaw" | "hermes" = "openclaw"): AgentDefinition { - const configDir = name === "openclaw" ? "/sandbox/.openclaw" : "/sandbox/.hermes"; - const stateDirs = name === "openclaw" ? ["whatsapp"] : ["platforms"]; - return { - name, - agentDir: `/fake/${name}`, - manifestPath: `/fake/${name}/manifest.yaml`, - get displayName() { - return name; - }, - get healthProbe() { - return { url: "http://localhost:0/", port: 0, timeout_seconds: 5 }; - }, - get forwardPort() { - return 0; - }, - get dashboard() { - return { kind: "ui" as const, label: "UI", path: "/" }; - }, - get configPaths() { - return { - dir: configDir, - configFile: name === "openclaw" ? "openclaw.json" : "config.yaml", - envFile: name === "hermes" ? ".env" : null, - format: name === "openclaw" ? "json" : "yaml", - }; - }, - get inferenceProviderOptions() { - return []; - }, - get stateDirs() { - return stateDirs; - }, - get stateFiles() { - return []; - }, - get versionCommand() { - return `${name} --version`; - }, - get expectedVersion() { - return null; - }, - get hasDevicePairing() { - return false; - }, - get phoneHomeHosts() { - return []; - }, - get dockerfileBasePath() { - return null; - }, - get dockerfilePath() { - return null; - }, - get startScriptPath() { - return null; - }, - get policyAdditionsPath() { - return null; - }, - get policyPermissivePath() { - return null; - }, - get pluginDir() { - return null; - }, - get legacyPaths() { - return null; - }, - } as unknown as AgentDefinition; -} - -function entry( - messagingChannels: string[] = ["whatsapp"], - disabledChannels: string[] = [], - channelInputs: Record = {}, - agentName: "openclaw" | "hermes" = "openclaw", -): SandboxEntry { - const disabled = new Set(disabledChannels); - return { - name: "alpha", - agent: agentName, - messaging: { - schemaVersion: 1, - plan: { - schemaVersion: 1, - sandboxName: "alpha", - agent: agentName, - workflow: "onboard", - channels: messagingChannels.map((channelId) => ({ - channelId, - displayName: channelId, - authMode: channelId === "whatsapp" ? "in-sandbox-qr" : "token-paste", - active: !disabled.has(channelId), - selected: true, - configured: true, - disabled: disabled.has(channelId), - inputs: channelInputs[channelId] ?? [], - hooks: [], - })), - disabledChannels, - credentialBindings: [], - networkPolicy: { presets: [], entries: [] }, - agentRender: [], - buildSteps: [], - stateUpdates: [], - healthChecks: [], - }, - }, - } as SandboxEntry; -} - -function makeDeps(opts: { - exec: (sandboxName: string, command: string, timeoutMs?: number) => ExecResult | null; - appliedPresets?: string[]; - gatewayPresets?: string[] | null; - agentName?: "openclaw" | "hermes"; - sandbox?: SandboxEntry | undefined; - out?: (line: string) => void; -}) { - const calls: string[] = []; - const out = opts.out ?? ((line: string) => calls.push(line)); - return { - out, - deps: { - loadAgent: () => fakeAgent(opts.agentName), - getSandbox: () => opts.sandbox ?? entry(), - getAppliedPresets: () => opts.appliedPresets ?? ["whatsapp"], - getGatewayPresets: () => - opts.gatewayPresets === undefined ? ["whatsapp"] : opts.gatewayPresets, - execSandbox: vi.fn(opts.exec), - now: () => PROBED_AT, - out, - }, - out_lines: calls, - }; -} +import { + type ExecResult, + entry, + makeDeps, + showSandboxChannelStatus, +} from "./channel-status.test-helpers"; describe("showSandboxChannelStatus (whatsapp)", () => { it("returns idle verdict and exit code 1 when paired but no inbound observed", async () => { @@ -499,988 +323,4 @@ describe("showSandboxChannelStatus (whatsapp)", () => { const dump = out_lines.join("\n"); expect(dump).toMatch(/registered but currently paused/); }); - - it("emits a compact all-channel report when no channel is selected", async () => { - const commands: string[] = []; - const { deps, out_lines } = makeDeps({ - exec: (_sandbox, command) => { - commands.push(command); - return command.includes("/sandbox/.openclaw/openclaw.json") - ? { - status: 0, - stdout: JSON.stringify({ - channels: { - telegram: { - accounts: { - default: { - groupPolicy: "open", - }, - }, - }, - }, - }), - stderr: "", - } - : { status: 1, stdout: "", stderr: "" }; - }, - sandbox: entry(["telegram", "whatsapp"], [], { - telegram: [ - { - channelId: "telegram", - inputId: "groupPolicy", - kind: "config", - required: false, - sourceEnv: "TELEGRAM_GROUP_POLICY", - statePath: "telegramConfig.groupPolicy", - value: "open", - }, - ], - }), - appliedPresets: ["telegram", "whatsapp"], - }); - const result = await showSandboxChannelStatus("alpha", { deps }); - - expect( - result && "channels" in result && result.channels.map((channel) => channel.channel), - ).toEqual(["telegram", "whatsapp"]); - expect(commands.join("\n")).not.toMatch(/NEMOCLAW_WA_DIAG_OK/); - const dump = out_lines.join("\n"); - expect(dump).toMatch(/NemoClaw channels status:.*alpha/); - expect(dump).toMatch(/\btelegram\b/); - expect(dump).toMatch(/\bwhatsapp\b/); - expect(dump).toMatch(/Telegram group policy \(TELEGRAM_GROUP_POLICY\):\s+open/); - expect(dump).not.toMatch(/Deep diagnostics/); - expect(dump).not.toMatch(/Probed at/); - }); - - it("prints an empty-state hint when no channels are configured", async () => { - const { deps, out_lines } = makeDeps({ - exec: () => ({ status: 0, stdout: "", stderr: "" }), - sandbox: entry([]), - appliedPresets: [], - }); - const result = await showSandboxChannelStatus("alpha", { deps }); - - expect(result && "channels" in result && result.channels).toEqual([]); - const dump = out_lines.join("\n"); - expect(dump).toMatch(/Configured channels: none/); - expect(dump).toMatch(/channels add /); - }); - - it("emits a basic per-channel report for non-whatsapp channels", async () => { - const { deps, out_lines } = makeDeps({ - exec: () => ({ status: 0, stdout: "", stderr: "" }), - sandbox: entry(["telegram"]), - appliedPresets: ["telegram"], - }); - const result = await showSandboxChannelStatus("alpha", { - deps, - channel: "telegram", - }); - expect(result && "verdict" in result && result.verdict).toBe("info"); - const dump = out_lines.join("\n"); - expect(dump).toMatch(/telegram registered/); - expect(dump).toMatch(/preset applied/); - }); - - it("marks rendered config ok when the sandbox config matches the sandbox entry", async () => { - const { deps, out_lines } = makeDeps({ - exec: (_sandbox, command) => - command.includes("/sandbox/.openclaw/openclaw.json") - ? { - status: 0, - stdout: JSON.stringify({ - channels: { - telegram: { - accounts: { - default: { - groupPolicy: "allowlist", - }, - }, - }, - }, - }), - stderr: "", - } - : { status: 1, stdout: "", stderr: "" }, - sandbox: entry(["telegram"], [], { - telegram: [ - { - channelId: "telegram", - inputId: "botToken", - kind: "secret", - required: true, - sourceEnv: "TELEGRAM_BOT_TOKEN", - credentialAvailable: true, - }, - { - channelId: "telegram", - inputId: "requireMention", - kind: "config", - required: false, - sourceEnv: "TELEGRAM_REQUIRE_MENTION", - statePath: "telegramConfig.requireMention", - value: "1", - }, - { - channelId: "telegram", - inputId: "groupPolicy", - kind: "config", - required: false, - sourceEnv: "TELEGRAM_GROUP_POLICY", - statePath: "telegramConfig.groupPolicy", - value: "allowlist", - }, - ], - }), - appliedPresets: ["telegram"], - }); - const result = await showSandboxChannelStatus("alpha", { - deps, - channel: "telegram", - }); - - expect(result && "verdict" in result && result.verdict).toBe("info"); - const signals = result && "signals" in result ? result.signals : []; - expect( - signals.find((signal) => signal.label === "Telegram group policy (TELEGRAM_GROUP_POLICY)"), - ).toMatchObject({ - severity: "ok", - detail: "allowlist", - }); - expect( - signals.find( - (signal) => signal.label === "Telegram group mention mode (TELEGRAM_REQUIRE_MENTION)", - ), - ).toBeUndefined(); - const dump = out_lines.join("\n"); - expect(dump).toMatch(/Telegram group policy \(TELEGRAM_GROUP_POLICY\):\s+allowlist/); - expect(dump).not.toMatch(/Telegram Bot Token/); - expect(dump).not.toMatch(/TELEGRAM_BOT_TOKEN/); - }); - - it("compares Hermes Telegram group policy from rendered env config", async () => { - const { deps } = makeDeps({ - exec: (_sandbox, command) => - command.includes("/sandbox/.hermes/.env") - ? { - status: 0, - stdout: ["TELEGRAM_ALLOWED_USERS=7895072570", "TELEGRAM_GROUP_POLICY=allowlist"].join( - "\n", - ), - stderr: "", - } - : { status: 1, stdout: "", stderr: "" }, - agentName: "hermes", - sandbox: entry( - ["telegram"], - [], - { - telegram: [ - { - channelId: "telegram", - inputId: "groupPolicy", - kind: "config", - required: false, - sourceEnv: "TELEGRAM_GROUP_POLICY", - statePath: "telegramConfig.groupPolicy", - value: "allowlist", - }, - ], - }, - "hermes", - ), - appliedPresets: ["telegram"], - }); - const result = await showSandboxChannelStatus("alpha", { - deps, - channel: "telegram", - }); - - const signals = result && "signals" in result ? result.signals : []; - expect( - signals.find((signal) => signal.label === "Telegram group policy (TELEGRAM_GROUP_POLICY)"), - ).toMatchObject({ - severity: "ok", - detail: "allowlist", - }); - }); - - it("warns when rendered config differs from the sandbox entry", async () => { - const { deps } = makeDeps({ - exec: () => ({ - status: 0, - stdout: JSON.stringify({ - channels: { - telegram: { - accounts: { - default: { - groupPolicy: "open", - }, - }, - }, - }, - }), - stderr: "", - }), - sandbox: entry(["telegram"], [], { - telegram: [ - { - channelId: "telegram", - inputId: "groupPolicy", - kind: "config", - required: false, - sourceEnv: "TELEGRAM_GROUP_POLICY", - statePath: "telegramConfig.groupPolicy", - value: "allowlist", - }, - ], - }), - appliedPresets: ["telegram"], - }); - const result = await showSandboxChannelStatus("alpha", { - deps, - channel: "telegram", - }); - - const signals = result && "signals" in result ? result.signals : []; - expect( - signals.find((signal) => signal.label === "Telegram group policy (TELEGRAM_GROUP_POLICY)"), - ).toMatchObject({ - severity: "warn", - detail: "expected allowlist; rendered open", - }); - }); - - it("warns once when a shared rendered config source is unreadable", async () => { - const { deps, out_lines } = makeDeps({ - exec: () => ({ - status: 1, - stdout: "", - stderr: "cat: /sandbox/.openclaw/openclaw.json: No such file or directory", - }), - sandbox: entry(["teams"], [], { - teams: [ - { - channelId: "teams", - inputId: "appId", - kind: "config", - required: true, - sourceEnv: "MSTEAMS_APP_ID", - statePath: "teamsConfig.appId", - value: "2542103c-7a1e-408a-b2f3-667e09e86783", - }, - { - channelId: "teams", - inputId: "tenantId", - kind: "config", - required: true, - sourceEnv: "MSTEAMS_TENANT_ID", - statePath: "teamsConfig.tenantId", - value: "43083d15-7273-40c1-b7db-39efd9ccc17a", - }, - { - channelId: "teams", - inputId: "allowedUsers", - kind: "config", - required: false, - sourceEnv: "TEAMS_ALLOWED_USERS", - statePath: "allowedIds.teams", - value: "205f29da-231e-4a0e-a0b2-b398e6302087", - }, - { - channelId: "teams", - inputId: "webhookPort", - kind: "config", - required: false, - sourceEnv: "MSTEAMS_PORT", - statePath: "teamsConfig.webhookPort", - value: "3978", - }, - { - channelId: "teams", - inputId: "requireMention", - kind: "config", - required: false, - sourceEnv: "TEAMS_REQUIRE_MENTION", - statePath: "teamsConfig.requireMention", - value: "1", - }, - ], - }), - appliedPresets: ["teams"], - }); - const result = await showSandboxChannelStatus("alpha", { - deps, - channel: "teams", - }); - - const signals = result && "signals" in result ? result.signals : []; - const sourceWarnings = signals.filter((signal) => signal.label === "Rendered config source"); - expect(sourceWarnings).toHaveLength(1); - expect(sourceWarnings[0]).toMatchObject({ - severity: "warn", - detail: "could not read /sandbox/.openclaw/openclaw.json; config comparisons not checked", - }); - expect( - signals.find((signal) => signal.label === "Microsoft Teams Client ID (MSTEAMS_APP_ID)"), - ).toMatchObject({ - severity: "info", - detail: "2542103c-7a1e-408a-b2f3-667e09e86783 (not checked)", - }); - expect( - signals.find( - (signal) => - signal.label === - "Microsoft Teams AAD Object IDs (comma-separated allowlist) (TEAMS_ALLOWED_USERS)", - ), - ).toMatchObject({ - severity: "info", - detail: "205f29da-231e-4a0e-a0b2-b398e6302087 (not checked)", - }); - expect( - signals.find( - (signal) => signal.label === "Microsoft Teams mention mode (TEAMS_REQUIRE_MENTION)", - ), - ).toMatchObject({ - severity: "info", - detail: "1 (not checked)", - }); - const sourceReadFailures = out_lines - .join("\n") - .match(/could not read \/sandbox\/\.openclaw\/openclaw\.json/g); - expect(sourceReadFailures).toHaveLength(1); - }); - - it("warns once when a shared rendered config source is malformed", async () => { - const { deps } = makeDeps({ - exec: () => ({ - status: 0, - stdout: "{not-json", - stderr: "", - }), - sandbox: entry(["teams"], [], { - teams: [ - { - channelId: "teams", - inputId: "appId", - kind: "config", - required: true, - sourceEnv: "MSTEAMS_APP_ID", - statePath: "teamsConfig.appId", - value: "2542103c-7a1e-408a-b2f3-667e09e86783", - }, - { - channelId: "teams", - inputId: "tenantId", - kind: "config", - required: true, - sourceEnv: "MSTEAMS_TENANT_ID", - statePath: "teamsConfig.tenantId", - value: "43083d15-7273-40c1-b7db-39efd9ccc17a", - }, - ], - }), - appliedPresets: ["teams"], - }); - const result = await showSandboxChannelStatus("alpha", { - deps, - channel: "teams", - }); - - const signals = result && "signals" in result ? result.signals : []; - expect(signals.filter((signal) => signal.label === "Rendered config source")).toEqual([ - expect.objectContaining({ - severity: "warn", - detail: "could not parse /sandbox/.openclaw/openclaw.json; config comparisons not checked", - }), - ]); - expect( - signals.find((signal) => signal.label === "Microsoft Teams Client ID (MSTEAMS_APP_ID)"), - ).toMatchObject({ - severity: "info", - detail: "2542103c-7a1e-408a-b2f3-667e09e86783 (not checked)", - }); - }); - - it("treats 0/1 registry config as matching boolean rendered config", async () => { - const { deps } = makeDeps({ - exec: () => ({ - status: 0, - stdout: JSON.stringify({ - channels: { - msteams: { - requireMention: true, - }, - }, - }), - stderr: "", - }), - sandbox: entry(["teams"], [], { - teams: [ - { - channelId: "teams", - inputId: "requireMention", - kind: "config", - required: false, - sourceEnv: "TEAMS_REQUIRE_MENTION", - statePath: "teamsConfig.requireMention", - value: "1", - }, - ], - }), - appliedPresets: ["teams"], - }); - const result = await showSandboxChannelStatus("alpha", { - deps, - channel: "teams", - }); - - const signals = result && "signals" in result ? result.signals : []; - expect( - signals.find( - (signal) => signal.label === "Microsoft Teams mention mode (TEAMS_REQUIRE_MENTION)", - ), - ).toMatchObject({ - severity: "ok", - detail: "1", - }); - }); - - it("compares manifest-derived allowlist render values", async () => { - const { deps } = makeDeps({ - exec: () => ({ - status: 0, - stdout: JSON.stringify({ - channels: { - msteams: { - allowFrom: ["205f29da-231e-4a0e-a0b2-b398e6302087"], - }, - }, - }), - stderr: "", - }), - sandbox: entry(["teams"], [], { - teams: [ - { - channelId: "teams", - inputId: "allowedUsers", - kind: "config", - required: false, - sourceEnv: "TEAMS_ALLOWED_USERS", - statePath: "allowedIds.teams", - value: "205f29da-231e-4a0e-a0b2-b398e6302087", - }, - ], - }), - appliedPresets: ["teams"], - }); - const result = await showSandboxChannelStatus("alpha", { - deps, - channel: "teams", - }); - - const signals = result && "signals" in result ? result.signals : []; - expect( - signals.find( - (signal) => - signal.label === - "Microsoft Teams AAD Object IDs (comma-separated allowlist) (TEAMS_ALLOWED_USERS)", - ), - ).toMatchObject({ - severity: "ok", - detail: "205f29da-231e-4a0e-a0b2-b398e6302087", - }); - }); - - it("compares Discord guild-derived render values", async () => { - const { deps, out_lines } = makeDeps({ - exec: () => ({ - status: 0, - stdout: JSON.stringify({ - channels: { - discord: { - guilds: { - "1504155275899437177": { - requireMention: true, - }, - }, - }, - }, - }), - stderr: "", - }), - sandbox: entry(["discord"], [], { - discord: [ - { - channelId: "discord", - inputId: "serverId", - kind: "config", - required: false, - sourceEnv: "DISCORD_SERVER_ID", - statePath: "discordGuilds.serverId", - value: "1504155275899437177", - }, - { - channelId: "discord", - inputId: "requireMention", - kind: "config", - required: false, - sourceEnv: "DISCORD_REQUIRE_MENTION", - statePath: "discordGuilds.requireMention", - value: "1", - }, - ], - }), - appliedPresets: ["discord"], - }); - const result = await showSandboxChannelStatus("alpha", { - deps, - channel: "discord", - }); - - const signals = result && "signals" in result ? result.signals : []; - expect( - signals.find( - (signal) => - signal.label === "Discord Server ID (for guild workspace access) (DISCORD_SERVER_ID)", - ), - ).toMatchObject({ - severity: "ok", - detail: "1504155275899437177", - }); - expect( - signals.find((signal) => signal.label === "Discord mention mode (DISCORD_REQUIRE_MENTION)"), - ).toMatchObject({ - severity: "ok", - detail: "1", - }); - expect( - signals.find( - (signal) => signal.label === "Discord User ID (optional guild allowlist) (DISCORD_USER_ID)", - ), - ).toBeUndefined(); - const dump = out_lines.join("\n"); - expect(dump).toMatch( - /Discord Server ID \(for guild workspace access\) \(DISCORD_SERVER_ID\):\s+1504155275899437177/, - ); - expect(dump).toMatch(/Discord mention mode \(DISCORD_REQUIRE_MENTION\):\s+1/); - }); - - it("compares Slack OpenClaw allowlist render values", async () => { - const { deps } = makeDeps({ - exec: () => ({ - status: 0, - stdout: JSON.stringify({ - channels: { - slack: { - accounts: { - default: { - allowFrom: ["U01ABC2DEF3"], - channels: { - C012AB3CD: { - enabled: true, - requireMention: true, - users: ["U01ABC2DEF3"], - }, - }, - }, - }, - }, - }, - }), - stderr: "", - }), - sandbox: entry(["slack"], [], { - slack: [ - { - channelId: "slack", - inputId: "allowedUsers", - kind: "config", - required: false, - sourceEnv: "SLACK_ALLOWED_USERS", - statePath: "allowedIds.slack", - value: "U01ABC2DEF3", - }, - { - channelId: "slack", - inputId: "allowedChannels", - kind: "config", - required: false, - sourceEnv: "SLACK_ALLOWED_CHANNELS", - statePath: "slackConfig.allowedChannels", - value: "C012AB3CD", - }, - ], - }), - appliedPresets: ["slack"], - }); - const result = await showSandboxChannelStatus("alpha", { - deps, - channel: "slack", - }); - - const signals = result && "signals" in result ? result.signals : []; - expect( - signals.find( - (signal) => - signal.label === "Slack Member IDs (comma-separated allowlist) (SLACK_ALLOWED_USERS)", - ), - ).toMatchObject({ - severity: "ok", - detail: "U01ABC2DEF3", - }); - expect( - signals.find( - (signal) => - signal.label === "Slack Channel IDs (comma-separated allowlist) (SLACK_ALLOWED_CHANNELS)", - ), - ).toMatchObject({ - severity: "ok", - detail: "C012AB3CD", - }); - }); - - it("does not treat Slack wildcard channel policy as configured channel IDs", async () => { - const { deps } = makeDeps({ - exec: () => ({ - status: 0, - stdout: JSON.stringify({ - channels: { - slack: { - accounts: { - default: { - allowFrom: ["U0B5BQABTL4"], - channels: { - "*": { - enabled: true, - requireMention: true, - users: ["U0B5BQABTL4"], - }, - }, - }, - }, - }, - }, - }), - stderr: "", - }), - sandbox: entry(["slack"], [], { - slack: [ - { - channelId: "slack", - inputId: "allowedUsers", - kind: "config", - required: false, - sourceEnv: "SLACK_ALLOWED_USERS", - statePath: "allowedIds.slack", - value: "U0B5BQABTL4", - }, - ], - }), - appliedPresets: ["slack"], - }); - const result = await showSandboxChannelStatus("alpha", { - deps, - channel: "slack", - }); - - const signals = result && "signals" in result ? result.signals : []; - expect( - signals.find( - (signal) => - signal.label === "Slack Member IDs (comma-separated allowlist) (SLACK_ALLOWED_USERS)", - ), - ).toMatchObject({ - severity: "ok", - detail: "U0B5BQABTL4", - }); - expect( - signals.find( - (signal) => - signal.label === "Slack Channel IDs (comma-separated allowlist) (SLACK_ALLOWED_CHANNELS)", - ), - ).toBeUndefined(); - }); - - it("compares OpenClaw WeChat account render values", async () => { - const renderedResponses: Array<[string, ExecResult]> = [ - [ - "/sandbox/.openclaw/openclaw.json", - { - status: 0, - stdout: JSON.stringify({ - channels: { - "openclaw-weixin": { - accounts: { - "wechat-account": { - enabled: true, - }, - }, - }, - }, - }), - stderr: "", - }, - ], - [ - "/sandbox/.openclaw/openclaw-weixin/accounts/wechat-account.json", - { - status: 0, - stdout: JSON.stringify({ - baseUrl: "https://ilinkai.wechat.com", - userId: "wechat-user", - }), - stderr: "", - }, - ], - ]; - const { deps } = makeDeps({ - exec: (_sandbox, command) => - renderedResponses.find(([needle]) => command.includes(needle))?.[1] ?? { - status: 1, - stdout: "", - stderr: "", - }, - sandbox: entry(["wechat"], [], { - wechat: [ - { - channelId: "wechat", - inputId: "accountId", - kind: "config", - required: true, - sourceEnv: "WECHAT_ACCOUNT_ID", - statePath: "wechatConfig.accountId", - value: "wechat-account", - }, - { - channelId: "wechat", - inputId: "baseUrl", - kind: "config", - required: false, - sourceEnv: "WECHAT_BASE_URL", - statePath: "wechatConfig.baseUrl", - value: "https://ilinkai.wechat.com", - }, - { - channelId: "wechat", - inputId: "userId", - kind: "config", - required: false, - sourceEnv: "WECHAT_USER_ID", - statePath: "wechatConfig.userId", - value: "wechat-user", - }, - { - channelId: "wechat", - inputId: "allowedIds", - kind: "config", - required: false, - sourceEnv: "WECHAT_ALLOWED_IDS", - statePath: "allowedIds.wechat", - value: "wechat-user", - }, - ], - }), - appliedPresets: ["wechat"], - }); - const result = await showSandboxChannelStatus("alpha", { - deps, - channel: "wechat", - }); - - const signals = result && "signals" in result ? result.signals : []; - expect(signals.find((signal) => signal.label === "WECHAT_ACCOUNT_ID")).toMatchObject({ - severity: "ok", - detail: "wechat-account", - }); - expect(signals.find((signal) => signal.label === "WECHAT_BASE_URL")).toMatchObject({ - severity: "ok", - detail: "https://ilinkai.wechat.com", - }); - expect(signals.find((signal) => signal.label === "WECHAT_USER_ID")).toMatchObject({ - severity: "ok", - detail: "wechat-user", - }); - expect( - signals.find( - (signal) => signal.label === "WeChat User ID(s) (DM allowlist) (WECHAT_ALLOWED_IDS)", - ), - ).toBeUndefined(); - }); - - it("compares Hermes WeChat values through rendered WEIXIN env keys", async () => { - const { deps } = makeDeps({ - exec: (_sandbox, command) => - command.includes("/sandbox/.hermes/.env") - ? { - status: 0, - stdout: [ - "WEIXIN_ACCOUNT_ID=wxid_abc", - "WEIXIN_BASE_URL=https://wechat.example.test", - "WEIXIN_ALLOWED_USERS=wxid_abc,wxid_def", - ].join("\n"), - stderr: "", - } - : { status: 1, stdout: "", stderr: "" }, - agentName: "hermes", - sandbox: entry( - ["wechat"], - [], - { - wechat: [ - { - channelId: "wechat", - inputId: "accountId", - kind: "config", - required: true, - sourceEnv: "WECHAT_ACCOUNT_ID", - statePath: "wechatConfig.accountId", - value: "wxid_abc", - }, - { - channelId: "wechat", - inputId: "baseUrl", - kind: "config", - required: false, - sourceEnv: "WECHAT_BASE_URL", - statePath: "wechatConfig.baseUrl", - value: "https://wechat.example.test", - }, - { - channelId: "wechat", - inputId: "userId", - kind: "config", - required: false, - sourceEnv: "WECHAT_USER_ID", - statePath: "wechatConfig.userId", - value: "wxid_abc", - }, - { - channelId: "wechat", - inputId: "allowedIds", - kind: "config", - required: false, - sourceEnv: "WECHAT_ALLOWED_IDS", - statePath: "allowedIds.wechat", - value: "wxid_abc,wxid_def", - }, - ], - }, - "hermes", - ), - appliedPresets: ["wechat"], - }); - const result = await showSandboxChannelStatus("alpha", { - deps, - channel: "wechat", - }); - - const signals = result && "signals" in result ? result.signals : []; - expect(signals.find((signal) => signal.label === "WECHAT_ACCOUNT_ID")).toMatchObject({ - severity: "ok", - detail: "wxid_abc", - }); - expect(signals.find((signal) => signal.label === "WECHAT_BASE_URL")).toMatchObject({ - severity: "ok", - detail: "https://wechat.example.test", - }); - expect( - signals.find( - (signal) => signal.label === "WeChat User ID(s) (DM allowlist) (WECHAT_ALLOWED_IDS)", - ), - ).toMatchObject({ - severity: "ok", - detail: "wxid_abc,wxid_def", - }); - expect(signals.find((signal) => signal.label === "WECHAT_USER_ID")).toBeUndefined(); - }); - - it("uses manifest defaults when no stored config value exists", async () => { - const { deps, out_lines } = makeDeps({ - exec: () => ({ - status: 0, - stdout: JSON.stringify({ - channels: { - telegram: { - accounts: { - default: { - groupPolicy: "open", - }, - }, - }, - }, - }), - stderr: "", - }), - sandbox: entry(["telegram"]), - appliedPresets: ["telegram"], - }); - const result = await showSandboxChannelStatus("alpha", { - deps, - channel: "telegram", - }); - - const signals = result && "signals" in result ? result.signals : []; - expect( - signals.find((signal) => signal.label === "Telegram group policy (TELEGRAM_GROUP_POLICY)"), - ).toMatchObject({ - severity: "ok", - detail: "open (default)", - }); - expect( - signals.find( - (signal) => signal.label === "Telegram group mention mode (TELEGRAM_REQUIRE_MENTION)", - ), - ).toBeUndefined(); - const dump = out_lines.join("\n"); - expect(dump).not.toMatch(/Telegram User ID \(for DM access\)/); - expect(dump).toMatch(/Telegram group policy \(TELEGRAM_GROUP_POLICY\):\s+open \(default\)/); - }); - - it("accepts Telegram disabled group policy from rendered config", async () => { - const { deps } = makeDeps({ - exec: () => ({ - status: 0, - stdout: JSON.stringify({ - channels: { - telegram: { - accounts: { - default: { - groupPolicy: "disabled", - }, - }, - }, - }, - }), - stderr: "", - }), - sandbox: entry(["telegram"], [], { - telegram: [ - { - channelId: "telegram", - inputId: "groupPolicy", - kind: "config", - required: false, - sourceEnv: "TELEGRAM_GROUP_POLICY", - statePath: "telegramConfig.groupPolicy", - value: "disabled", - }, - ], - }), - appliedPresets: ["telegram"], - }); - const result = await showSandboxChannelStatus("alpha", { - deps, - channel: "telegram", - }); - - const signals = result && "signals" in result ? result.signals : []; - expect( - signals.find((signal) => signal.label === "Telegram group policy (TELEGRAM_GROUP_POLICY)"), - ).toMatchObject({ - severity: "ok", - detail: "disabled", - }); - }); }); diff --git a/src/lib/actions/sandbox/channel-status.ts b/src/lib/actions/sandbox/channel-status.ts index e4475cb33d4..853f8d65d79 100644 --- a/src/lib/actions/sandbox/channel-status.ts +++ b/src/lib/actions/sandbox/channel-status.ts @@ -14,6 +14,7 @@ import { type AgentDefinition, loadAgent } from "../../agent/defs"; import { CLI_DISPLAY_NAME, CLI_NAME } from "../../cli/branding"; import { B, D, G, R, RD, YW } from "../../cli/terminal-style"; +import { shellQuote as quotePath } from "../../core/shell-quote"; import { createBuiltInChannelManifestRegistry, getMessagingManifestAvailabilityContext, @@ -34,7 +35,7 @@ import { type WhatsappProbeInput, } from "../../sandbox/whatsapp-diagnostics"; import * as registry from "../../state/registry"; -import { buildConfigStatusSignals, quotePath } from "./channel-status-config"; +import { buildConfigStatusSignals } from "./channel-status-config"; // runner.ts (which process-recovery transitively depends on) uses a few CJS // `require()` calls that vitest's CLI-test project cannot resolve at import diff --git a/src/lib/actions/sandbox/process-recovery.test.ts b/src/lib/actions/sandbox/process-recovery.test.ts index bc46f1cb450..325a501d291 100644 --- a/src/lib/actions/sandbox/process-recovery.test.ts +++ b/src/lib/actions/sandbox/process-recovery.test.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import { afterEach, describe, expect, it } from "vitest"; - +import { buildHermesEnvFileBoundaryStandaloneCheck } from "../../agent/hermes-recovery-boundary"; // Import from compiled dist for parity with the other CLI tests in this project. import { buildSandboxExecMarkedCommand, @@ -32,6 +32,14 @@ describe("sandbox exec command wrapping", () => { expect(encoded).toBeTruthy(); expect(Buffer.from(encoded as string, "base64").toString("utf8")).toBe(payload); }); + + it("keeps the Hermes secret-boundary validator on the raw exec path", () => { + const payload = buildHermesEnvFileBoundaryStandaloneCheck(); + const wrapped = buildSandboxExecMarkedCommand(payload); + + expect(wrapped).toContain("validate-hermes-env-secret-boundary.py"); + expect(wrapped).not.toContain("base64 -d | sh"); + }); }); describe("probeSandboxInferenceGatewayHealth gateway-chain subprobe (#3265)", () => { diff --git a/src/lib/actions/sandbox/process-recovery.ts b/src/lib/actions/sandbox/process-recovery.ts index 6558f670890..f853b99ab02 100644 --- a/src/lib/actions/sandbox/process-recovery.ts +++ b/src/lib/actions/sandbox/process-recovery.ts @@ -66,9 +66,10 @@ export type SandboxForwardListEntry = { export type SandboxForwardHealth = boolean | "occupied" | null; const SANDBOX_EXEC_STARTED_MARKER = "__NEMOCLAW_SANDBOX_EXEC_STARTED__"; +const HERMES_SECRET_BOUNDARY_VALIDATOR_SCRIPT = "validate-hermes-env-secret-boundary.py"; export function buildSandboxExecMarkedCommand(command: string): string { - if (!/[\r\n]/.test(command)) { + if (!/[\r\n]/.test(command) || command.includes(HERMES_SECRET_BOUNDARY_VALIDATOR_SCRIPT)) { return `printf '%s\\n' '${SANDBOX_EXEC_STARTED_MARKER}'; ${command}`; } const encodedCommand = Buffer.from(command, "utf8").toString("base64"); diff --git a/src/lib/messaging/channels/discord/rendered-config-parser.test.ts b/src/lib/messaging/channels/discord/rendered-config-parser.test.ts index d6a7b6d47ca..04f93049e8b 100644 --- a/src/lib/messaging/channels/discord/rendered-config-parser.test.ts +++ b/src/lib/messaging/channels/discord/rendered-config-parser.test.ts @@ -7,13 +7,20 @@ import type { ChannelManifest } from "../../manifest"; import { discordRenderedConfigParser } from "./rendered-config-parser"; describe("discord rendered config parser", () => { + const openClawContext = { + agentId: "openclaw" as const, + manifest: { id: "discord" } as ChannelManifest, + inputs: [], + }; + const hermesContext = { + agentId: "hermes" as const, + manifest: { id: "discord" } as ChannelManifest, + inputs: [], + }; + it("treats missing guild mention policy values as unset", () => { const requireMentionKey = discordRenderedConfigParser - .listConfigVisibilityKeys({ - agentId: "openclaw", - manifest: { id: "discord" } as ChannelManifest, - inputs: [], - }) + .listConfigVisibilityKeys(openClawContext) .find((key) => key.key === "guildRequireMention"); expect(requireMentionKey).toBeDefined(); @@ -37,11 +44,7 @@ describe("discord rendered config parser", () => { it("preserves requireMention value types when guilds differ", () => { const requireMentionKey = discordRenderedConfigParser - .listConfigVisibilityKeys({ - agentId: "openclaw", - manifest: { id: "discord" } as ChannelManifest, - inputs: [], - }) + .listConfigVisibilityKeys(openClawContext) .find((key) => key.key === "guildRequireMention"); expect(requireMentionKey).toBeDefined(); @@ -65,4 +68,78 @@ describe("discord rendered config parser", () => { }), ).toEqual([true, false]); }); + + it("extracts OpenClaw guild ids and user allowlists", () => { + const keys = discordRenderedConfigParser.listConfigVisibilityKeys(openClawContext); + const source = { + kind: "structured" as const, + value: { + channels: { + discord: { + guilds: { + "1504155275899437177": { + requireMention: true, + users: ["U01ABC2DEF3"], + }, + "1504155275899437178": { + requireMention: true, + users: ["U04XYZ5RST6"], + }, + }, + }, + }, + }, + }; + + expect( + discordRenderedConfigParser.getValue(keys.find((key) => key.key === "guildIds")!, source), + ).toEqual(["1504155275899437177", "1504155275899437178"]); + expect( + discordRenderedConfigParser.getValue( + keys.find((key) => key.key === "guildRequireMention")!, + source, + ), + ).toBe(true); + expect( + discordRenderedConfigParser.getValue(keys.find((key) => key.key === "guildUsers")!, source), + ).toEqual(["U01ABC2DEF3", "U04XYZ5RST6"]); + }); + + it("extracts Hermes env and config values", () => { + const keys = discordRenderedConfigParser.listConfigVisibilityKeys(hermesContext); + const envSource = { + kind: "env" as const, + entries: new Map([ + ["NEMOCLAW_DISCORD_GUILD_IDS", "1504155275899437177"], + ["DISCORD_ALLOWED_USERS", "U01ABC2DEF3"], + ]), + }; + const configSource = { + kind: "structured" as const, + value: { + discord: { + require_mention: true, + }, + }, + }; + + expect( + discordRenderedConfigParser.getValue( + keys.find((key) => key.inputId === "serverId")!, + envSource, + ), + ).toBe("1504155275899437177"); + expect( + discordRenderedConfigParser.getValue( + keys.find((key) => key.inputId === "userId")!, + envSource, + ), + ).toBe("U01ABC2DEF3"); + expect( + discordRenderedConfigParser.getValue( + keys.find((key) => key.inputId === "requireMention")!, + configSource, + ), + ).toBe(true); + }); }); diff --git a/src/lib/messaging/channels/manifests.test.ts b/src/lib/messaging/channels/manifests.test.ts index bc01f2bd03f..606497cc3c0 100644 --- a/src/lib/messaging/channels/manifests.test.ts +++ b/src/lib/messaging/channels/manifests.test.ts @@ -234,6 +234,7 @@ describe("built-in channel manifests", () => { it("keeps rendered config parser keys limited to manifest config inputs", () => { const agentIds: readonly MessagingAgentId[] = ["openclaw", "hermes"]; + const secretLikePattern = /(?:token|secret|password|client_secret|client-secret)/i; expect( BUILT_IN_CHANNEL_MANIFESTS.flatMap((manifest) => { const configInputIds: ReadonlySet = new Set( @@ -242,8 +243,13 @@ describe("built-in channel manifests", () => { const parser = getBuiltInRenderedConfigParser(manifest.id); return agentIds.flatMap((agentId) => (parser?.listConfigVisibilityKeys({ manifest, agentId, inputs: [] }) ?? []) - .filter((key) => !configInputIds.has(key.inputId)) - .map((key) => `${manifest.id}.${agentId}.${key.inputId}`), + .filter( + (key) => + !configInputIds.has(key.inputId) || + secretLikePattern.test(key.envKey ?? "") || + secretLikePattern.test(key.target), + ) + .map((key) => `${manifest.id}.${agentId}.${key.inputId}:${key.envKey ?? key.target}`), ); }), ).toEqual([]); diff --git a/src/lib/messaging/channels/wechat/rendered-config-parser.ts b/src/lib/messaging/channels/wechat/rendered-config-parser.ts index 8b410af17cb..b9f2f3bf4af 100644 --- a/src/lib/messaging/channels/wechat/rendered-config-parser.ts +++ b/src/lib/messaging/channels/wechat/rendered-config-parser.ts @@ -80,7 +80,7 @@ function safeAccountId(value: string | undefined): string | undefined { value === "." || value === ".." || value.includes("..") || - /[\\/\0-\x1F\x7F]/.test(value) + !/^[A-Za-z0-9._-]+$/.test(value) ) { return undefined; } From d507a255aaa68d92873e77743367b2f0c3c94715 Mon Sep 17 00:00:00 2001 From: San Dang Date: Tue, 30 Jun 2026 16:25:32 +0700 Subject: [PATCH 04/10] test(cli): align process recovery validator coverage Signed-off-by: San Dang --- test/process-recovery.test.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/process-recovery.test.ts b/test/process-recovery.test.ts index a3c3ad3e9dc..48e34b6188e 100644 --- a/test/process-recovery.test.ts +++ b/test/process-recovery.test.ts @@ -272,7 +272,7 @@ describe("executeSandboxExecCommand", () => { expect(result).toBeNull(); }); - it("passes a newline-free Hermes validator payload to OpenShell", () => { + it("passes the Hermes validator payload to OpenShell without base64 wrapping", () => { const childProcess = requireSource("node:child_process"); const spawn = vi.spyOn(childProcess, "spawnSync").mockReturnValue({ status: 0, @@ -290,10 +290,11 @@ describe("executeSandboxExecCommand", () => { const args = spawn.mock.calls[0]?.[1] as string[]; const shellPayload = args.at(-1) ?? ""; expect(result).toEqual({ status: 0, stdout: "SECRET_BOUNDARY_OK", stderr: "" }); - expect(shellPayload.includes("\n")).toBe(false); + expect(shellPayload).toContain("validate-hermes-env-secret-boundary.py"); + expect(shellPayload.includes("\n")).toBe(true); expect(shellPayload.includes("\r")).toBe(false); expect(shellPayload).toContain("printf '%s\\n' '__NEMOCLAW_SANDBOX_EXEC_STARTED__'"); - expect(shellPayload).toContain("base64 -d | sh"); + expect(shellPayload).not.toContain("base64 -d | sh"); }); it("falls back to local Docker root exec when OpenShell exec output has no marker", () => { From 806f3dac89fea3df182743d26b511e7ff49d49e0 Mon Sep 17 00:00:00 2001 From: San Dang Date: Tue, 30 Jun 2026 16:44:29 +0700 Subject: [PATCH 05/10] fix(cli): address channel status review follow-ups Signed-off-by: San Dang --- .../channel-status-config-channels.test.ts | 12 ++- .../channel-status-config-core.test.ts | 96 +++++++++++++++++-- .../channel-status-config-values.test.ts | 5 + .../sandbox/channel-status-config-values.ts | 5 + .../actions/sandbox/channel-status-config.ts | 17 +++- .../sandbox/channel-status-summary.test.ts | 32 ++++++- .../channel-status-telegram-policy.test.ts | 2 +- .../telegram/rendered-config-parser.ts | 1 - test/process-recovery.test.ts | 4 +- 9 files changed, 153 insertions(+), 21 deletions(-) diff --git a/src/lib/actions/sandbox/channel-status-config-channels.test.ts b/src/lib/actions/sandbox/channel-status-config-channels.test.ts index fc66ad85c6a..51eb4495f61 100644 --- a/src/lib/actions/sandbox/channel-status-config-channels.test.ts +++ b/src/lib/actions/sandbox/channel-status-config-channels.test.ts @@ -76,7 +76,10 @@ describe("showSandboxChannelStatus channel config parsers", () => { signals.find( (signal) => signal.label === "Discord User ID (optional guild allowlist) (DISCORD_USER_ID)", ), - ).toBeUndefined(); + ).toMatchObject({ + severity: "info", + detail: "not set", + }); const dump = out_lines.join("\n"); expect(dump).toMatch( /Discord Server ID \(for guild workspace access\) \(DISCORD_SERVER_ID\):\s+1504155275899437177/, @@ -217,7 +220,10 @@ describe("showSandboxChannelStatus channel config parsers", () => { (signal) => signal.label === "Slack Channel IDs (comma-separated allowlist) (SLACK_ALLOWED_CHANNELS)", ), - ).toBeUndefined(); + ).toMatchObject({ + severity: "info", + detail: "not set", + }); }); it("compares OpenClaw WeChat account render values", async () => { @@ -371,7 +377,7 @@ describe("showSandboxChannelStatus channel config parsers", () => { channel: "wechat", }); - expect(commands).toEqual(["cat '/sandbox/.openclaw/openclaw.json'"]); + expect(commands).toEqual(["head -c 65537 '/sandbox/.openclaw/openclaw.json'"]); }); it("compares Hermes WeChat values through rendered WEIXIN env keys", async () => { diff --git a/src/lib/actions/sandbox/channel-status-config-core.test.ts b/src/lib/actions/sandbox/channel-status-config-core.test.ts index aae61677d53..85accac5b00 100644 --- a/src/lib/actions/sandbox/channel-status-config-core.test.ts +++ b/src/lib/actions/sandbox/channel-status-config-core.test.ts @@ -81,24 +81,50 @@ describe("showSandboxChannelStatus config comparison", () => { expect(dump).not.toMatch(/TELEGRAM_BOT_TOKEN/); }); - it("compares Hermes Telegram group policy from rendered env config", async () => { + it("does not compare Hermes Telegram group policy when the manifest does not render it", async () => { const { deps } = makeDeps({ exec: (_sandbox, command) => command.includes("/sandbox/.hermes/.env") ? { status: 0, - stdout: ["TELEGRAM_ALLOWED_USERS=7895072570", "TELEGRAM_GROUP_POLICY=allowlist"].join( - "\n", - ), + stdout: "TELEGRAM_ALLOWED_USERS=7895072570", stderr: "", } - : { status: 1, stdout: "", stderr: "" }, + : command.includes("/sandbox/.hermes/config.yaml") + ? { + status: 0, + stdout: "telegram:\n require_mention: true\n", + stderr: "", + } + : { + status: 1, + stdout: "", + stderr: "", + }, agentName: "hermes", sandbox: entry( ["telegram"], [], { telegram: [ + { + channelId: "telegram", + inputId: "allowedIds", + kind: "config", + required: false, + sourceEnv: "TELEGRAM_ALLOWED_IDS", + statePath: "allowedIds.telegram", + value: "7895072570", + }, + { + channelId: "telegram", + inputId: "requireMention", + kind: "config", + required: false, + sourceEnv: "TELEGRAM_REQUIRE_MENTION", + statePath: "telegramConfig.requireMention", + value: "1", + }, { channelId: "telegram", inputId: "groupPolicy", @@ -121,10 +147,66 @@ describe("showSandboxChannelStatus config comparison", () => { const signals = result && "signals" in result ? result.signals : []; expect( - signals.find((signal) => signal.label === "Telegram group policy (TELEGRAM_GROUP_POLICY)"), + signals.find( + (signal) => signal.label === "Telegram User ID (for DM access) (TELEGRAM_ALLOWED_IDS)", + ), ).toMatchObject({ severity: "ok", - detail: "allowlist", + detail: "7895072570", + }); + expect( + signals.find( + (signal) => signal.label === "Telegram group mention mode (TELEGRAM_REQUIRE_MENTION)", + ), + ).toMatchObject({ + severity: "ok", + detail: "1", + }); + expect( + signals.find((signal) => signal.label === "Telegram group policy (TELEGRAM_GROUP_POLICY)"), + ).toBeUndefined(); + }); + + it("warns when rendered config source is oversized", async () => { + const { deps } = makeDeps({ + exec: () => ({ + status: 0, + stdout: "x".repeat(64 * 1024 + 1), + stderr: "", + }), + sandbox: entry(["teams"], [], { + teams: [ + { + channelId: "teams", + inputId: "appId", + kind: "config", + required: true, + sourceEnv: "MSTEAMS_APP_ID", + statePath: "teamsConfig.appId", + value: "2542103c-7a1e-408a-b2f3-667e09e86783", + }, + ], + }), + appliedPresets: ["teams"], + }); + const result = await showSandboxChannelStatus("alpha", { + deps, + channel: "teams", + }); + + const signals = result && "signals" in result ? result.signals : []; + expect(signals.filter((signal) => signal.label === "Rendered config source")).toEqual([ + expect.objectContaining({ + severity: "warn", + detail: + "rendered config source too large: /sandbox/.openclaw/openclaw.json; config comparisons not checked", + }), + ]); + expect( + signals.find((signal) => signal.label === "Microsoft Teams Client ID (MSTEAMS_APP_ID)"), + ).toMatchObject({ + severity: "info", + detail: "2542103c-7a1e-408a-b2f3-667e09e86783 (not checked)", }); }); diff --git a/src/lib/actions/sandbox/channel-status-config-values.test.ts b/src/lib/actions/sandbox/channel-status-config-values.test.ts index 5dbd97ab2f8..d001d3baa80 100644 --- a/src/lib/actions/sandbox/channel-status-config-values.test.ts +++ b/src/lib/actions/sandbox/channel-status-config-values.test.ts @@ -28,9 +28,14 @@ describe("channel status config value helpers", () => { it("normalizes boolean-like config values before comparing", () => { expect(booleanConfigValue("1")).toBe(true); expect(booleanConfigValue("false")).toBe(false); + expect(booleanConfigValue(1)).toBe(true); + expect(booleanConfigValue(0)).toBe(false); + expect(booleanConfigValue(2)).toBeNull(); expect(booleanConfigValue("enabled")).toBeNull(); expect(configValuesEqual("1", true)).toBe(true); expect(configValuesEqual("0", false)).toBe(true); + expect(configValuesEqual(1, true)).toBe(true); + expect(configValuesEqual(0, false)).toBe(true); expect(configValuesEqual("true", false)).toBe(false); }); }); diff --git a/src/lib/actions/sandbox/channel-status-config-values.ts b/src/lib/actions/sandbox/channel-status-config-values.ts index 66b03c4ca27..57e2f7932bf 100644 --- a/src/lib/actions/sandbox/channel-status-config-values.ts +++ b/src/lib/actions/sandbox/channel-status-config-values.ts @@ -48,6 +48,11 @@ export function listConfigValues(value: MessagingSerializableValue): string[] { export function booleanConfigValue(value: MessagingSerializableValue): boolean | null { if (typeof value === "boolean") return value; + if (typeof value === "number") { + if (value === 1) return true; + if (value === 0) return false; + return null; + } if (typeof value !== "string") return null; const normalized = value.trim().toLowerCase(); if (normalized === "1" || normalized === "true") return true; diff --git a/src/lib/actions/sandbox/channel-status-config.ts b/src/lib/actions/sandbox/channel-status-config.ts index cee5f0747db..c6b03953ebf 100644 --- a/src/lib/actions/sandbox/channel-status-config.ts +++ b/src/lib/actions/sandbox/channel-status-config.ts @@ -1,6 +1,7 @@ // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 +import { Buffer } from "node:buffer"; import YAML from "yaml"; import type { AgentDefinition } from "../../agent/defs"; import { CLI_NAME } from "../../cli/branding"; @@ -26,6 +27,7 @@ import * as registry from "../../state/registry"; import { configInputDetail, configValuesEqual } from "./channel-status-config-values"; const CONFIG_STATUS_TIMEOUT_MS = 5_000; +const CONFIG_STATUS_MAX_SOURCE_BYTES = 64 * 1024; const channelManifestRegistry = createBuiltInChannelManifestRegistry(); type ExecRunner = ( @@ -109,12 +111,11 @@ function configInputSignal( ); const checkedComparisons = comparisons.filter((comparison) => comparison.checked); const hasMismatch = checkedComparisons.some((comparison) => !comparison.matches); - if (!expected.hasValue && !hasMismatch) return null; const allSourcesChecked = checkedComparisons.length === comparisons.length && checkedComparisons.length > 0; return { label, - severity: hasMismatch ? "warn" : allSourcesChecked ? "ok" : "info", + severity: hasMismatch ? "warn" : expected.hasValue && allSourcesChecked ? "ok" : "info", detail: Array.from(new Set(comparisons.map((comparison) => comparison.detail))).join("; "), }; } @@ -280,16 +281,22 @@ function readConfigSourceValues( ): ConfigSourceReads { const targetReads = new Map(); for (const target of new Set(sources.map((source) => source.resolvedTarget))) { + // Targets are resolved only from built-in channel manifests via resolveConfigTarget. + // Keep this command path closed to user-provided targets before broadening shellQuote use. const result = deps.execSandbox( sandboxName, - `cat ${shellQuote(target)}`, + `head -c ${CONFIG_STATUS_MAX_SOURCE_BYTES + 1} ${shellQuote(target)}`, CONFIG_STATUS_TIMEOUT_MS, ); targetReads.set( target, - result && result.status === 0 + result && + result.status === 0 && + Buffer.byteLength(result.stdout, "utf8") <= CONFIG_STATUS_MAX_SOURCE_BYTES ? { ok: true, contents: result.stdout } - : { ok: false, error: `could not read ${target}` }, + : result && result.status === 0 + ? { ok: false, error: `rendered config source too large: ${target}` } + : { ok: false, error: `could not read ${target}` }, ); } diff --git a/src/lib/actions/sandbox/channel-status-summary.test.ts b/src/lib/actions/sandbox/channel-status-summary.test.ts index 39c85f52372..867addc2dc4 100644 --- a/src/lib/actions/sandbox/channel-status-summary.test.ts +++ b/src/lib/actions/sandbox/channel-status-summary.test.ts @@ -1,7 +1,7 @@ // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 -import { describe, expect, it } from "vitest"; +import { describe, expect, it, vi } from "vitest"; import { entry, makeDeps, showSandboxChannelStatus } from "./channel-status.test-helpers"; describe("showSandboxChannelStatus summary", () => { @@ -72,6 +72,36 @@ describe("showSandboxChannelStatus summary", () => { expect(dump).toMatch(/channels add /); }); + it("rejects an explicit unknown channel without falling back to summary probing", async () => { + const commands: string[] = []; + const { deps, out_lines } = makeDeps({ + exec: (_sandbox, command) => { + commands.push(command); + return { status: 0, stdout: "", stderr: "" }; + }, + sandbox: entry(["telegram", "whatsapp"]), + appliedPresets: ["telegram", "whatsapp"], + }); + const exitSpy = vi.spyOn(process, "exit").mockImplementation(((code?: number) => { + throw new Error(`process.exit(${code})`); + }) as never); + + let threw: Error | undefined; + try { + await showSandboxChannelStatus("alpha", { deps, channel: "bogus" }); + } catch (error) { + threw = error as Error; + } finally { + exitSpy.mockRestore(); + } + + expect(threw?.message).toBe("process.exit(1)"); + expect(commands).toEqual([]); + const dump = out_lines.join("\n"); + expect(dump).toMatch(/Unknown channel 'bogus'/); + expect(dump).not.toMatch(/NemoClaw channels status:/); + }); + it("emits a basic per-channel report for non-whatsapp channels", async () => { const { deps, out_lines } = makeDeps({ exec: () => ({ status: 0, stdout: "", stderr: "" }), diff --git a/src/lib/actions/sandbox/channel-status-telegram-policy.test.ts b/src/lib/actions/sandbox/channel-status-telegram-policy.test.ts index b4a47cc1184..1fad223e077 100644 --- a/src/lib/actions/sandbox/channel-status-telegram-policy.test.ts +++ b/src/lib/actions/sandbox/channel-status-telegram-policy.test.ts @@ -43,7 +43,7 @@ describe("showSandboxChannelStatus Telegram group policy", () => { ), ).toBeUndefined(); const dump = out_lines.join("\n"); - expect(dump).not.toMatch(/Telegram User ID \(for DM access\)/); + expect(dump).toMatch(/Telegram User ID \(for DM access\) \(TELEGRAM_ALLOWED_IDS\):\s+not set/); expect(dump).toMatch(/Telegram group policy \(TELEGRAM_GROUP_POLICY\):\s+open \(default\)/); }); diff --git a/src/lib/messaging/channels/telegram/rendered-config-parser.ts b/src/lib/messaging/channels/telegram/rendered-config-parser.ts index b949d6f90cc..3013aa1313a 100644 --- a/src/lib/messaging/channels/telegram/rendered-config-parser.ts +++ b/src/lib/messaging/channels/telegram/rendered-config-parser.ts @@ -32,7 +32,6 @@ export const telegramRenderedConfigParser: RenderedChannelConfigParser = { if (context.agentId === "hermes") { return [ envConfigKey("allowedIds", "~/.hermes/.env", "TELEGRAM_ALLOWED_USERS"), - envConfigKey("groupPolicy", "~/.hermes/.env", "TELEGRAM_GROUP_POLICY"), structuredConfigKey("requireMention", "~/.hermes/config.yaml", [ "telegram", "require_mention", diff --git a/test/process-recovery.test.ts b/test/process-recovery.test.ts index 48e34b6188e..ab4d3e50767 100644 --- a/test/process-recovery.test.ts +++ b/test/process-recovery.test.ts @@ -290,10 +290,8 @@ describe("executeSandboxExecCommand", () => { const args = spawn.mock.calls[0]?.[1] as string[]; const shellPayload = args.at(-1) ?? ""; expect(result).toEqual({ status: 0, stdout: "SECRET_BOUNDARY_OK", stderr: "" }); + expect(args.slice(0, 7)).toEqual(["sandbox", "exec", "--name", "hermes-box", "--", "sh", "-c"]); expect(shellPayload).toContain("validate-hermes-env-secret-boundary.py"); - expect(shellPayload.includes("\n")).toBe(true); - expect(shellPayload.includes("\r")).toBe(false); - expect(shellPayload).toContain("printf '%s\\n' '__NEMOCLAW_SANDBOX_EXEC_STARTED__'"); expect(shellPayload).not.toContain("base64 -d | sh"); }); From 57a1ba40cc2e2c39d0ca4190f68b7fdb0cfb45c0 Mon Sep 17 00:00:00 2001 From: San Dang Date: Tue, 30 Jun 2026 16:58:26 +0700 Subject: [PATCH 06/10] fix(cli): address CodeRabbit status feedback Signed-off-by: San Dang --- .../actions/sandbox/channel-status-config-core.test.ts | 10 +++++----- src/lib/actions/sandbox/channel-status-config.ts | 8 +++++++- src/lib/actions/sandbox/process-recovery.test.ts | 4 +++- src/lib/actions/sandbox/process-recovery.ts | 3 +-- src/lib/agent/hermes-recovery-boundary.ts | 2 +- test/process-recovery.test.ts | 10 +++++----- 6 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/lib/actions/sandbox/channel-status-config-core.test.ts b/src/lib/actions/sandbox/channel-status-config-core.test.ts index 85accac5b00..d98ec5ddbd6 100644 --- a/src/lib/actions/sandbox/channel-status-config-core.test.ts +++ b/src/lib/actions/sandbox/channel-status-config-core.test.ts @@ -205,7 +205,7 @@ describe("showSandboxChannelStatus config comparison", () => { expect( signals.find((signal) => signal.label === "Microsoft Teams Client ID (MSTEAMS_APP_ID)"), ).toMatchObject({ - severity: "info", + severity: "warn", detail: "2542103c-7a1e-408a-b2f3-667e09e86783 (not checked)", }); }); @@ -329,7 +329,7 @@ describe("showSandboxChannelStatus config comparison", () => { expect( signals.find((signal) => signal.label === "Microsoft Teams Client ID (MSTEAMS_APP_ID)"), ).toMatchObject({ - severity: "info", + severity: "warn", detail: "2542103c-7a1e-408a-b2f3-667e09e86783 (not checked)", }); expect( @@ -339,7 +339,7 @@ describe("showSandboxChannelStatus config comparison", () => { "Microsoft Teams AAD Object IDs (comma-separated allowlist) (TEAMS_ALLOWED_USERS)", ), ).toMatchObject({ - severity: "info", + severity: "warn", detail: "205f29da-231e-4a0e-a0b2-b398e6302087 (not checked)", }); expect( @@ -347,7 +347,7 @@ describe("showSandboxChannelStatus config comparison", () => { (signal) => signal.label === "Microsoft Teams mention mode (TEAMS_REQUIRE_MENTION)", ), ).toMatchObject({ - severity: "info", + severity: "warn", detail: "1 (not checked)", }); const sourceReadFailures = out_lines @@ -402,7 +402,7 @@ describe("showSandboxChannelStatus config comparison", () => { expect( signals.find((signal) => signal.label === "Microsoft Teams Client ID (MSTEAMS_APP_ID)"), ).toMatchObject({ - severity: "info", + severity: "warn", detail: "2542103c-7a1e-408a-b2f3-667e09e86783 (not checked)", }); }); diff --git a/src/lib/actions/sandbox/channel-status-config.ts b/src/lib/actions/sandbox/channel-status-config.ts index c6b03953ebf..15b129ba1e1 100644 --- a/src/lib/actions/sandbox/channel-status-config.ts +++ b/src/lib/actions/sandbox/channel-status-config.ts @@ -113,9 +113,15 @@ function configInputSignal( const hasMismatch = checkedComparisons.some((comparison) => !comparison.matches); const allSourcesChecked = checkedComparisons.length === comparisons.length && checkedComparisons.length > 0; + const hasUncheckedExpectedValue = expected.hasValue && !allSourcesChecked; return { label, - severity: hasMismatch ? "warn" : expected.hasValue && allSourcesChecked ? "ok" : "info", + severity: + hasMismatch || hasUncheckedExpectedValue + ? "warn" + : expected.hasValue && allSourcesChecked + ? "ok" + : "info", detail: Array.from(new Set(comparisons.map((comparison) => comparison.detail))).join("; "), }; } diff --git a/src/lib/actions/sandbox/process-recovery.test.ts b/src/lib/actions/sandbox/process-recovery.test.ts index 325a501d291..47849320854 100644 --- a/src/lib/actions/sandbox/process-recovery.test.ts +++ b/src/lib/actions/sandbox/process-recovery.test.ts @@ -33,10 +33,12 @@ describe("sandbox exec command wrapping", () => { expect(Buffer.from(encoded as string, "base64").toString("utf8")).toBe(payload); }); - it("keeps the Hermes secret-boundary validator on the raw exec path", () => { + it("keeps the Hermes secret-boundary validator visible on the raw exec path", () => { const payload = buildHermesEnvFileBoundaryStandaloneCheck(); const wrapped = buildSandboxExecMarkedCommand(payload); + expect(payload).not.toMatch(/[\r\n]/); + expect(wrapped).not.toMatch(/[\r\n]/); expect(wrapped).toContain("validate-hermes-env-secret-boundary.py"); expect(wrapped).not.toContain("base64 -d | sh"); }); diff --git a/src/lib/actions/sandbox/process-recovery.ts b/src/lib/actions/sandbox/process-recovery.ts index f853b99ab02..6558f670890 100644 --- a/src/lib/actions/sandbox/process-recovery.ts +++ b/src/lib/actions/sandbox/process-recovery.ts @@ -66,10 +66,9 @@ export type SandboxForwardListEntry = { export type SandboxForwardHealth = boolean | "occupied" | null; const SANDBOX_EXEC_STARTED_MARKER = "__NEMOCLAW_SANDBOX_EXEC_STARTED__"; -const HERMES_SECRET_BOUNDARY_VALIDATOR_SCRIPT = "validate-hermes-env-secret-boundary.py"; export function buildSandboxExecMarkedCommand(command: string): string { - if (!/[\r\n]/.test(command) || command.includes(HERMES_SECRET_BOUNDARY_VALIDATOR_SCRIPT)) { + if (!/[\r\n]/.test(command)) { return `printf '%s\\n' '${SANDBOX_EXEC_STARTED_MARKER}'; ${command}`; } const encodedCommand = Buffer.from(command, "utf8").toString("base64"); diff --git a/src/lib/agent/hermes-recovery-boundary.ts b/src/lib/agent/hermes-recovery-boundary.ts index a80008f44f0..c8880f17331 100644 --- a/src/lib/agent/hermes-recovery-boundary.ts +++ b/src/lib/agent/hermes-recovery-boundary.ts @@ -165,7 +165,7 @@ export function buildHermesEnvFileBoundaryStandaloneCheck(): string { ` echo ${SECRET_BOUNDARY_REFUSED_MARKER};`, ` exit 1;`, `fi;`, - ].join("\n"); + ].join(" "); } export const __testing = { diff --git a/test/process-recovery.test.ts b/test/process-recovery.test.ts index ab4d3e50767..6bad44549d3 100644 --- a/test/process-recovery.test.ts +++ b/test/process-recovery.test.ts @@ -20,6 +20,9 @@ const { } = requireSource( "../src/lib/actions/sandbox/process-recovery.ts", ) as typeof import("../src/lib/actions/sandbox/process-recovery.js"); +const { buildHermesEnvFileBoundaryStandaloneCheck } = requireSource( + "../src/lib/agent/hermes-recovery-boundary.ts", +) as typeof import("../src/lib/agent/hermes-recovery-boundary.js"); afterEach(() => { vi.restoreAllMocks(); @@ -281,16 +284,13 @@ describe("executeSandboxExecCommand", () => { } as never); const result = withFakeOpenshellBinary(() => - executeSandboxExecCommand( - "hermes-box", - "python3 /usr/local/lib/nemoclaw/validate-hermes-env-secret-boundary.py env-file /sandbox/.hermes/.env\necho SECRET_BOUNDARY_OK", - ), + executeSandboxExecCommand("hermes-box", buildHermesEnvFileBoundaryStandaloneCheck()), ); const args = spawn.mock.calls[0]?.[1] as string[]; const shellPayload = args.at(-1) ?? ""; expect(result).toEqual({ status: 0, stdout: "SECRET_BOUNDARY_OK", stderr: "" }); - expect(args.slice(0, 7)).toEqual(["sandbox", "exec", "--name", "hermes-box", "--", "sh", "-c"]); + expect(shellPayload).not.toMatch(/[\r\n]/); expect(shellPayload).toContain("validate-hermes-env-secret-boundary.py"); expect(shellPayload).not.toContain("base64 -d | sh"); }); From 2b5225d253b2c1aaef0e0be4c603cf7328dcd275 Mon Sep 17 00:00:00 2001 From: San Dang Date: Tue, 30 Jun 2026 17:01:41 +0700 Subject: [PATCH 07/10] docs(cli): clarify unchecked channel status comparisons Signed-off-by: San Dang --- docs/reference/commands-nemohermes.mdx | 2 ++ docs/reference/commands.mdx | 2 ++ 2 files changed, 4 insertions(+) diff --git a/docs/reference/commands-nemohermes.mdx b/docs/reference/commands-nemohermes.mdx index 15bac45e1dd..9de53005cc9 100644 --- a/docs/reference/commands-nemohermes.mdx +++ b/docs/reference/commands-nemohermes.mdx @@ -1006,6 +1006,8 @@ The detailed WhatsApp probe stays focused on QR/session runtime diagnostics and For registered non-WhatsApp channel details and the compact summary, the status output compares non-secret config inputs from the sandbox registry against the values rendered into the agent config, such as Telegram group policy in `openclaw.json` or mention mode in Hermes config. Secret inputs, including tokens, are not printed. +If the registry contains a non-secret expected value but NemoClaw cannot read or check the rendered source, the comparison is a warning and the detail includes `(not checked)`. +Optional unset inputs remain informational. ```bash nemohermes my-assistant channels status diff --git a/docs/reference/commands.mdx b/docs/reference/commands.mdx index 2147b0dd826..35794db81b7 100644 --- a/docs/reference/commands.mdx +++ b/docs/reference/commands.mdx @@ -1281,6 +1281,8 @@ The detailed WhatsApp probe stays focused on QR/session runtime diagnostics and For registered non-WhatsApp channel details and the compact summary, the status output compares non-secret config inputs from the sandbox registry against the values rendered into the agent config, such as Telegram group policy in `openclaw.json` or mention mode in Hermes config. Secret inputs, including tokens, are not printed. +If the registry contains a non-secret expected value but NemoClaw cannot read or check the rendered source, the comparison is a warning and the detail includes `(not checked)`. +Optional unset inputs remain informational. ```bash $$nemoclaw my-assistant channels status From 2fd48f3b57800f3b60d48bcf03a6330aa37916fb Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Tue, 30 Jun 2026 11:30:51 -0700 Subject: [PATCH 08/10] fix(channels-status-config-summary): fix sandbox-exec marker newline and test budget Co-Authored-By: Claude Sonnet 4.6 Signed-off-by: Prek Shiv --- ci/test-file-size-budget.json | 1 + src/lib/actions/sandbox/sandbox-exec-output.ts | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/ci/test-file-size-budget.json b/ci/test-file-size-budget.json index ee67851ca93..a37455da0e2 100644 --- a/ci/test-file-size-budget.json +++ b/ci/test-file-size-budget.json @@ -6,6 +6,7 @@ "src/lib/inference/nim.test.ts": 2068, "src/lib/onboard/preflight.test.ts": 1904, "test/channels-add-preset.test.ts": 1871, + "test/cli/connect-recovery.test.ts": 1509, "test/generate-openclaw-config.test.ts": 1982, "test/install-preflight.test.ts": 4006, "test/nemoclaw-start.test.ts": 5043, diff --git a/src/lib/actions/sandbox/sandbox-exec-output.ts b/src/lib/actions/sandbox/sandbox-exec-output.ts index 752d885c08d..5d5e75b0eca 100644 --- a/src/lib/actions/sandbox/sandbox-exec-output.ts +++ b/src/lib/actions/sandbox/sandbox-exec-output.ts @@ -6,8 +6,8 @@ import { Buffer } from "node:buffer"; export const SANDBOX_EXEC_STARTED_MARKER = "__NEMOCLAW_SANDBOX_EXEC_STARTED__"; export function buildSandboxExecMarkedCommand(command: string): string { - if (!command.includes("validate-hermes-env-secret-boundary.py")) { - return `printf '%s\n' '${SANDBOX_EXEC_STARTED_MARKER}'; ${command}`; + if (!/[\r\n]/.test(command)) { + return `printf '%s\\n' '${SANDBOX_EXEC_STARTED_MARKER}'; ${command}`; } const encodedCommand = Buffer.from(command, "utf8").toString("base64"); return [ From b7d095fe79a9c440d2f8723e8de1bf444ad44ed4 Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Tue, 30 Jun 2026 11:41:08 -0700 Subject: [PATCH 09/10] fix(channels-status-config-summary): split connect-recovery test to satisfy file budget Co-Authored-By: Claude Sonnet 4.6 Signed-off-by: Prek Shiv --- ci/test-file-size-budget.json | 1 - ...connect-recovery-sandbox-not-found.test.ts | 104 ++++++++++++++++++ test/cli/connect-recovery.test.ts | 93 ---------------- 3 files changed, 104 insertions(+), 94 deletions(-) create mode 100644 test/cli/connect-recovery-sandbox-not-found.test.ts diff --git a/ci/test-file-size-budget.json b/ci/test-file-size-budget.json index a37455da0e2..ee67851ca93 100644 --- a/ci/test-file-size-budget.json +++ b/ci/test-file-size-budget.json @@ -6,7 +6,6 @@ "src/lib/inference/nim.test.ts": 2068, "src/lib/onboard/preflight.test.ts": 1904, "test/channels-add-preset.test.ts": 1871, - "test/cli/connect-recovery.test.ts": 1509, "test/generate-openclaw-config.test.ts": 1982, "test/install-preflight.test.ts": 4006, "test/nemoclaw-start.test.ts": 5043, diff --git a/test/cli/connect-recovery-sandbox-not-found.test.ts b/test/cli/connect-recovery-sandbox-not-found.test.ts new file mode 100644 index 00000000000..792cf110b91 --- /dev/null +++ b/test/cli/connect-recovery-sandbox-not-found.test.ts @@ -0,0 +1,104 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { describe, expect, it } from "vitest"; + +import { runWithEnv } from "./helpers"; + +describe("CLI dispatch", () => { + it("connect surfaces sandbox-not-found when recovery cannot find the requested sandbox (#2164)", () => { + const home = fs.mkdtempSync( + path.join(os.tmpdir(), "nemoclaw-cli-connect-unknown-after-recovery-"), + ); + const localBin = path.join(home, "bin"); + const nemoclawDir = path.join(home, ".nemoclaw"); + fs.mkdirSync(localBin, { recursive: true }); + fs.mkdirSync(nemoclawDir, { recursive: true }); + fs.writeFileSync( + path.join(nemoclawDir, "onboard-session.json"), + JSON.stringify( + { + version: 1, + sessionId: "session-1", + resumable: true, + status: "complete", + mode: "interactive", + startedAt: "2026-03-31T00:00:00.000Z", + updatedAt: "2026-03-31T00:00:00.000Z", + lastStepStarted: "policies", + lastCompletedStep: "policies", + failure: null, + sandboxName: "alpha", + provider: "nvidia-prod", + model: "nvidia/nemotron-3-super-120b-a12b", + endpointUrl: null, + credentialEnv: null, + preferredInferenceApi: null, + nimContainer: null, + policyPresets: null, + metadata: { gatewayName: "nemoclaw" }, + steps: { + preflight: { status: "complete", startedAt: null, completedAt: null, error: null }, + gateway: { status: "complete", startedAt: null, completedAt: null, error: null }, + sandbox: { status: "complete", startedAt: null, completedAt: null, error: null }, + provider_selection: { + status: "complete", + startedAt: null, + completedAt: null, + error: null, + }, + inference: { status: "complete", startedAt: null, completedAt: null, error: null }, + openclaw: { status: "complete", startedAt: null, completedAt: null, error: null }, + policies: { status: "complete", startedAt: null, completedAt: null, error: null }, + }, + }, + null, + 2, + ), + { mode: 0o600 }, + ); + fs.writeFileSync( + path.join(localBin, "openshell"), + [ + "#!/usr/bin/env bash", + 'if [ "$1" = "status" ]; then', + " echo 'Server Status'", + " echo", + " echo ' Gateway: nemoclaw'", + " echo ' Status: Connected'", + " exit 0", + "fi", + 'if [ "$1" = "gateway" ] && [ "$2" = "info" ]; then', + " echo 'Gateway Info'", + " echo", + " echo ' Gateway: nemoclaw'", + " exit 0", + "fi", + 'if [ "$1" = "sandbox" ] && [ "$2" = "list" ]; then', + " echo 'No sandboxes found.'", + " exit 0", + "fi", + 'if [ "$1" = "--version" ]; then', + " echo 'openshell 0.0.16'", + " exit 0", + "fi", + "exit 0", + ].join("\n"), + { mode: 0o755 }, + ); + + const r = runWithEnv("beta connect", { + HOME: home, + PATH: `${localBin}:${process.env.PATH || ""}`, + }); + + expect(r.code).toBe(1); + expect(r.out.includes("Sandbox 'beta' does not exist")).toBeTruthy(); + // Recovery from onboard-session.json restores "alpha" into the local registry, + // so the helper lists it rather than the empty-registry onboard hint. + expect(r.out.includes("Registered sandboxes: alpha")).toBeTruthy(); + }); +}); diff --git a/test/cli/connect-recovery.test.ts b/test/cli/connect-recovery.test.ts index 3aa4bffa550..d1ed9e22dee 100644 --- a/test/cli/connect-recovery.test.ts +++ b/test/cli/connect-recovery.test.ts @@ -1413,97 +1413,4 @@ describe("CLI dispatch", () => { expect(log.includes("sandbox get alpha")).toBeTruthy(); expect(log.includes("sandbox connect alpha")).toBeTruthy(); }); - - it("connect surfaces sandbox-not-found when recovery cannot find the requested sandbox (#2164)", () => { - const home = fs.mkdtempSync( - path.join(os.tmpdir(), "nemoclaw-cli-connect-unknown-after-recovery-"), - ); - const localBin = path.join(home, "bin"); - const nemoclawDir = path.join(home, ".nemoclaw"); - fs.mkdirSync(localBin, { recursive: true }); - fs.mkdirSync(nemoclawDir, { recursive: true }); - fs.writeFileSync( - path.join(nemoclawDir, "onboard-session.json"), - JSON.stringify( - { - version: 1, - sessionId: "session-1", - resumable: true, - status: "complete", - mode: "interactive", - startedAt: "2026-03-31T00:00:00.000Z", - updatedAt: "2026-03-31T00:00:00.000Z", - lastStepStarted: "policies", - lastCompletedStep: "policies", - failure: null, - sandboxName: "alpha", - provider: "nvidia-prod", - model: "nvidia/nemotron-3-super-120b-a12b", - endpointUrl: null, - credentialEnv: null, - preferredInferenceApi: null, - nimContainer: null, - policyPresets: null, - metadata: { gatewayName: "nemoclaw" }, - steps: { - preflight: { status: "complete", startedAt: null, completedAt: null, error: null }, - gateway: { status: "complete", startedAt: null, completedAt: null, error: null }, - sandbox: { status: "complete", startedAt: null, completedAt: null, error: null }, - provider_selection: { - status: "complete", - startedAt: null, - completedAt: null, - error: null, - }, - inference: { status: "complete", startedAt: null, completedAt: null, error: null }, - openclaw: { status: "complete", startedAt: null, completedAt: null, error: null }, - policies: { status: "complete", startedAt: null, completedAt: null, error: null }, - }, - }, - null, - 2, - ), - { mode: 0o600 }, - ); - fs.writeFileSync( - path.join(localBin, "openshell"), - [ - "#!/usr/bin/env bash", - 'if [ "$1" = "status" ]; then', - " echo 'Server Status'", - " echo", - " echo ' Gateway: nemoclaw'", - " echo ' Status: Connected'", - " exit 0", - "fi", - 'if [ "$1" = "gateway" ] && [ "$2" = "info" ]; then', - " echo 'Gateway Info'", - " echo", - " echo ' Gateway: nemoclaw'", - " exit 0", - "fi", - 'if [ "$1" = "sandbox" ] && [ "$2" = "list" ]; then', - " echo 'No sandboxes found.'", - " exit 0", - "fi", - 'if [ "$1" = "--version" ]; then', - " echo 'openshell 0.0.16'", - " exit 0", - "fi", - "exit 0", - ].join("\n"), - { mode: 0o755 }, - ); - - const r = runWithEnv("beta connect", { - HOME: home, - PATH: `${localBin}:${process.env.PATH || ""}`, - }); - - expect(r.code).toBe(1); - expect(r.out.includes("Sandbox 'beta' does not exist")).toBeTruthy(); - // Recovery from onboard-session.json restores "alpha" into the local registry, - // so the helper lists it rather than the empty-registry onboard hint. - expect(r.out.includes("Registered sandboxes: alpha")).toBeTruthy(); - }); }); From f99f8af711b17c764c0f3c14912a0608332653e7 Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Tue, 30 Jun 2026 11:56:21 -0700 Subject: [PATCH 10/10] fix(channels-status-config-summary): align exec marker with main design, update tests Co-Authored-By: Claude Sonnet 4.6 Signed-off-by: Prek Shiv --- .../actions/sandbox/process-recovery.test.ts | 17 ++++++----------- src/lib/actions/sandbox/sandbox-exec-output.ts | 2 +- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/lib/actions/sandbox/process-recovery.test.ts b/src/lib/actions/sandbox/process-recovery.test.ts index 47849320854..5450f77c300 100644 --- a/src/lib/actions/sandbox/process-recovery.test.ts +++ b/src/lib/actions/sandbox/process-recovery.test.ts @@ -19,28 +19,23 @@ describe("sandbox exec command wrapping", () => { expect(wrapped).not.toContain("base64 -d | sh"); }); - it("keeps the OpenShell command argument newline-free while preserving multi-line payloads", () => { + it("keeps non-Hermes payloads inline even when multi-line", () => { const payload = "printf 'hello\\n'\ncat '/sandbox/.openclaw/openclaw.json'"; const wrapped = buildSandboxExecMarkedCommand(payload); - expect(wrapped).not.toMatch(/[\r\n]/); expect(wrapped).toContain("__NEMOCLAW_SANDBOX_EXEC_STARTED__"); - expect(wrapped).toContain("base64 -d | sh"); - expect(wrapped).not.toContain(payload); - - const encoded = wrapped.match(/printf '%s' '([^']+)' \| base64 -d \| sh/)?.[1]; - expect(encoded).toBeTruthy(); - expect(Buffer.from(encoded as string, "base64").toString("utf8")).toBe(payload); + expect(wrapped).toContain(payload); + expect(wrapped).not.toContain("base64 -d | sh"); }); - it("keeps the Hermes secret-boundary validator visible on the raw exec path", () => { + it("base64-encodes the Hermes secret-boundary validator to hide it from shell history", () => { const payload = buildHermesEnvFileBoundaryStandaloneCheck(); const wrapped = buildSandboxExecMarkedCommand(payload); expect(payload).not.toMatch(/[\r\n]/); expect(wrapped).not.toMatch(/[\r\n]/); - expect(wrapped).toContain("validate-hermes-env-secret-boundary.py"); - expect(wrapped).not.toContain("base64 -d | sh"); + expect(wrapped).toContain("base64 -d | sh"); + expect(wrapped).not.toContain("validate-hermes-env-secret-boundary.py"); }); }); diff --git a/src/lib/actions/sandbox/sandbox-exec-output.ts b/src/lib/actions/sandbox/sandbox-exec-output.ts index 5d5e75b0eca..9e5b54ede42 100644 --- a/src/lib/actions/sandbox/sandbox-exec-output.ts +++ b/src/lib/actions/sandbox/sandbox-exec-output.ts @@ -6,7 +6,7 @@ import { Buffer } from "node:buffer"; export const SANDBOX_EXEC_STARTED_MARKER = "__NEMOCLAW_SANDBOX_EXEC_STARTED__"; export function buildSandboxExecMarkedCommand(command: string): string { - if (!/[\r\n]/.test(command)) { + if (!command.includes("validate-hermes-env-secret-boundary.py")) { return `printf '%s\\n' '${SANDBOX_EXEC_STARTED_MARKER}'; ${command}`; } const encodedCommand = Buffer.from(command, "utf8").toString("base64");