Skip to content

Commit 9663cc7

Browse files
authored
fix: keep status read-only while gateway owns state (#124142)
1 parent c00dd28 commit 9663cc7

18 files changed

Lines changed: 259 additions & 58 deletions

‎src/agents/exec-defaults.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,18 @@ describe("resolveExecDefaults", () => {
338338
).toEqual({ canExec: false, node: "build-mac" });
339339
});
340340

341+
it("uses an explicitly loaded approval snapshot for read-only callers", () => {
342+
const load = vi.mocked(execApprovals.loadExecApprovals);
343+
344+
expect(
345+
resolveNodeExecEligibility({
346+
cfg: withDefaultAgent({ tools: { exec: { host: "node", mode: "full" } } }),
347+
execApprovals: { version: 1, defaults: { security: "deny" }, agents: {} },
348+
}),
349+
).toEqual({ canExec: false });
350+
expect(load).not.toHaveBeenCalled();
351+
});
352+
341353
it("blocks node skill eligibility when the gateway denies system.run", () => {
342354
expect(
343355
resolveNodeExecEligibility({

‎src/agents/exec-defaults.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { OpenClawConfig } from "../config/types.openclaw.js";
66
import {
77
loadExecApprovals,
88
type ExecAsk,
9+
type ExecApprovalsFile,
910
type ExecHost,
1011
type ExecMode,
1112
type ExecSecurity,
@@ -112,6 +113,7 @@ function resolveExecConfigState(params: {
112113
/** Resolves whether node exec is usable and any effective node binding. */
113114
export function resolveNodeExecEligibility(params: {
114115
cfg?: OpenClawConfig;
116+
execApprovals?: ExecApprovalsFile;
115117
sessionEntry?: ExecSessionDefaults;
116118
execOverrides?: ExecPolicyOverrides;
117119
agentId?: string;
@@ -131,6 +133,7 @@ export function resolveNodeExecEligibility(params: {
131133
/** Resolves effective exec host, mode, approval policy, and node availability. */
132134
export function resolveExecDefaults(params: {
133135
cfg?: OpenClawConfig;
136+
execApprovals?: ExecApprovalsFile;
134137
sessionEntry?: ExecSessionDefaults;
135138
execOverrides?: ExecPolicyOverrides;
136139
agentId?: string;
@@ -173,7 +176,7 @@ export function resolveExecDefaults(params: {
173176
resolved.effectiveHost === "sandbox"
174177
? undefined
175178
: resolveExecApprovalsFromFile({
176-
file: loadExecApprovals(),
179+
file: params.execApprovals ?? loadExecApprovals(),
177180
agentId: resolvedAgentId,
178181
overrides: {
179182
security: defaultSecurity,

‎src/commands/channels/status.runtime.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ export async function renderChannelsStatusFallback(params: {
231231
runtime.error(
232232
`${gatewayAuthUnavailable ? "Gateway auth unavailable" : "Gateway not reachable"}: ${safeError}`,
233233
);
234-
const cfg = await requireValidConfig(runtime);
234+
const cfg = await requireValidConfig(runtime, { observe: false });
235235
if (!cfg) {
236236
return;
237237
}
@@ -242,7 +242,7 @@ export async function renderChannelsStatusFallback(params: {
242242
mode: "read_only_status",
243243
runtime,
244244
});
245-
const snapshot = await readConfigFileSnapshot();
245+
const snapshot = await readConfigFileSnapshot({ observe: false });
246246
const mode = cfg.gateway?.mode === "remote" ? "remote" : "local";
247247
const requestedChannel = opts.channel
248248
? (normalizeChannelId(opts.channel) ?? normalizeOptionalLowercaseString(opts.channel))

‎src/commands/config-validation.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ describe("requireValidConfig", () => {
8282
expect(readConfigFileSnapshot).toHaveBeenCalledWith({ skipPluginValidation: true });
8383
});
8484

85+
it("can validate config without observing persistent health state", async () => {
86+
createValidSnapshot();
87+
const runtime = createRuntime();
88+
89+
await expect(requireValidConfig(runtime, { observe: false })).resolves.toEqual({
90+
plugins: {},
91+
});
92+
93+
expect(readConfigFileSnapshot).toHaveBeenCalledWith({ observe: false });
94+
});
95+
8596
it("emits a non-blocking compatibility advisory when explicitly requested", async () => {
8697
createValidSnapshot();
8798
const runtime = createRuntime();

‎src/commands/config-validation.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,18 @@ import type { RuntimeEnv } from "../runtime.js";
1717
/** Read the config file and exit through the runtime when validation fails. */
1818
export async function requireValidConfigFileSnapshot(
1919
runtime: RuntimeEnv,
20-
opts?: { includeCompatibilityAdvisory?: boolean; skipPluginValidation?: boolean },
20+
opts?: {
21+
includeCompatibilityAdvisory?: boolean;
22+
observe?: boolean;
23+
skipPluginValidation?: boolean;
24+
},
2125
): Promise<ConfigFileSnapshot | null> {
26+
const readOptions = {
27+
...(opts?.observe === false ? { observe: false } : {}),
28+
...(opts?.skipPluginValidation ? { skipPluginValidation: true } : {}),
29+
};
2230
const snapshot = await readConfigFileSnapshot(
23-
opts?.skipPluginValidation ? { skipPluginValidation: true } : undefined,
31+
Object.keys(readOptions).length > 0 ? readOptions : undefined,
2432
);
2533
if (snapshot.exists && !snapshot.valid) {
2634
const issues =
@@ -59,7 +67,11 @@ export async function requireValidConfigFileSnapshot(
5967
/** Read and return a valid OpenClaw config, or null after reporting validation errors. */
6068
export async function requireValidConfig(
6169
runtime: RuntimeEnv,
62-
opts?: { includeCompatibilityAdvisory?: boolean; skipPluginValidation?: boolean },
70+
opts?: {
71+
includeCompatibilityAdvisory?: boolean;
72+
observe?: boolean;
73+
skipPluginValidation?: boolean;
74+
},
6375
): Promise<OpenClawConfig | null> {
6476
return (await requireValidConfigFileSnapshot(runtime, opts))?.config ?? null;
6577
}

‎src/commands/status-all/report-data.test.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const mocks = vi.hoisted(() => ({
88
resolveStatusGatewayDiagnosticsSafe: vi.fn(async () => null),
99
resolveStatusGatewayHealthSafe: vi.fn(async () => undefined),
1010
resolveNodeExecEligibility: vi.fn(() => ({ canExec: false })),
11+
loadExecApprovalsReadOnly: vi.fn(() => ({ version: 1, agents: {} })),
1112
buildWorkspaceSkillStatus: vi.fn(() => null),
1213
}));
1314

@@ -27,7 +28,12 @@ vi.mock("../../gateway/net.js", () => ({
2728
bindHost === "100.64.0.40" ? [bindHost, "127.0.0.1"] : [bindHost],
2829
}));
2930
vi.mock("../../infra/ports-inspect.js", () => ({ inspectPortUsage: mocks.inspectPortUsage }));
30-
vi.mock("../../infra/restart-sentinel.js", () => ({ readRestartSentinel: async () => null }));
31+
vi.mock("../../infra/exec-approvals.js", () => ({
32+
loadExecApprovalsReadOnly: mocks.loadExecApprovalsReadOnly,
33+
}));
34+
vi.mock("../../infra/restart-sentinel.js", () => ({
35+
readRestartSentinelReadOnly: async () => null,
36+
}));
3137
vi.mock("../../plugins/status.js", () => ({ buildPluginCompatibilityNotices: () => [] }));
3238
vi.mock("../../skills/discovery/status.js", () => ({
3339
buildWorkspaceSkillStatus: mocks.buildWorkspaceSkillStatus,
@@ -172,6 +178,7 @@ describe("buildStatusAllReportData", () => {
172178

173179
expect(mocks.resolveNodeExecEligibility).toHaveBeenCalledWith({
174180
cfg: expect.any(Object),
181+
execApprovals: { version: 1, agents: {} },
175182
agentId: "beta",
176183
});
177184
expect(mocks.buildWorkspaceSkillStatus).toHaveBeenCalledWith("/tmp/beta", expect.any(Object));

‎src/commands/status-all/report-data.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import { resolveNodeExecEligibility } from "../../agents/exec-defaults.js";
55
import { readConfigFileSnapshot, resolveGatewayPort } from "../../config/config.js";
66
import { readLastGatewayErrorLine } from "../../daemon/diagnostics.js";
77
import { resolveGatewayBindHost, resolveGatewayRequiredListenHosts } from "../../gateway/net.js";
8+
import { loadExecApprovalsReadOnly } from "../../infra/exec-approvals.js";
89
import { inspectPortUsage } from "../../infra/ports-inspect.js";
9-
import { readRestartSentinel } from "../../infra/restart-sentinel.js";
10+
import { readRestartSentinelReadOnly } from "../../infra/restart-sentinel.js";
1011
import { resolvePluginControlPlaneWorkspace } from "../../plugins/control-plane-workspace.js";
1112
import { buildPluginCompatibilityNotices } from "../../plugins/status.js";
1213
import { buildWorkspaceSkillStatus } from "../../skills/discovery/status.js";
@@ -58,7 +59,7 @@ async function resolveStatusAllLocalDiagnosis(params: {
5859
snap: ConfigFileSnapshot | null;
5960
remoteUrlMissing: boolean;
6061
secretDiagnostics: StatusScanOverviewResult["secretDiagnostics"];
61-
sentinel: Awaited<ReturnType<typeof readRestartSentinel>> | null;
62+
sentinel: Awaited<ReturnType<typeof readRestartSentinelReadOnly>> | null;
6263
lastErr: string | null;
6364
port: number;
6465
portUsage: Awaited<ReturnType<typeof inspectPortUsage>> | null;
@@ -111,7 +112,7 @@ async function resolveStatusAllLocalDiagnosis(params: {
111112

112113
params.progress.setLabel("Checking local state…");
113114
// These probes are intentionally best-effort so status-all can still print a partial report.
114-
const sentinel = await readRestartSentinel().catch(() => null);
115+
const sentinel = await readRestartSentinelReadOnly().catch(() => null);
115116
const lastErr = await readLastGatewayErrorLine(process.env).catch(() => null);
116117
const port = resolveGatewayPort(overview.cfg);
117118
const bindHost = await resolveGatewayBindHost(
@@ -135,6 +136,7 @@ async function resolveStatusAllLocalDiagnosis(params: {
135136
// Skill eligibility depends on whether the default agent may request node exec.
136137
const nodeSkills = resolveNodeExecEligibility({
137138
cfg: overview.cfg,
139+
execApprovals: loadExecApprovalsReadOnly(),
138140
agentId: controlPlaneWorkspace.agentId,
139141
});
140142
return buildWorkspaceSkillStatus(defaultWorkspace, {

‎src/commands/status.command.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
import { sanitizeTerminalText } from "../../packages/terminal-core/src/safe-text.js";
1111
import { withProgress } from "../cli/progress.js";
1212
import { OPENCLAW_WRAPPER_ENV_KEY } from "../daemon/program-args.js";
13-
import { readRestartSentinel } from "../infra/restart-sentinel.js";
13+
import { readRestartSentinelReadOnly } from "../infra/restart-sentinel.js";
1414
import type { RuntimeEnv } from "../runtime.js";
1515
import { createLazyImportLoader } from "../shared/lazy-promise.js";
1616
import { assertStatusUsageAgentScope, runStatusJsonCommand } from "./status-json-command.ts";
@@ -319,7 +319,7 @@ export async function statusCommand(
319319
nodeOnlyGateway,
320320
});
321321
const updateRestartValue = formatUpdateRestartStatusValue(
322-
(await readRestartSentinel().catch(() => null))?.payload,
322+
(await readRestartSentinelReadOnly().catch(() => null))?.payload,
323323
{
324324
ok,
325325
warn,

‎src/commands/status.node-mode.test.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22
import { beforeEach, describe, expect, it, vi } from "vitest";
33

44
const mocks = vi.hoisted(() => ({
5-
loadNodeHostConfig: vi.fn(),
5+
loadNodeHostConfigReadOnly: vi.fn(),
66
}));
77

88
vi.mock("../node-host/config.js", () => ({
9-
loadNodeHostConfig: mocks.loadNodeHostConfig,
9+
loadNodeHostConfigReadOnly: mocks.loadNodeHostConfigReadOnly,
1010
}));
1111

1212
import { resolveNodeOnlyGatewayInfo } from "./status.node-mode.js";
1313

1414
describe("resolveNodeOnlyGatewayInfo", () => {
1515
beforeEach(() => {
16-
mocks.loadNodeHostConfig.mockReset();
16+
mocks.loadNodeHostConfigReadOnly.mockReset();
1717
});
1818

1919
it("returns node-only gateway details when no local gateway is installed", async () => {
20-
mocks.loadNodeHostConfig.mockResolvedValueOnce({
20+
mocks.loadNodeHostConfigReadOnly.mockResolvedValueOnce({
2121
version: 1,
2222
nodeId: "node-1",
2323
gateway: { host: "gateway.example.com", port: 19000 },
@@ -46,7 +46,7 @@ describe("resolveNodeOnlyGatewayInfo", () => {
4646
});
4747

4848
it("does not claim node-only mode when the node service is installed but inactive", async () => {
49-
mocks.loadNodeHostConfig.mockResolvedValueOnce({
49+
mocks.loadNodeHostConfigReadOnly.mockResolvedValueOnce({
5050
version: 1,
5151
nodeId: "node-1",
5252
gateway: { host: "gateway.example.com", port: 19000 },
@@ -67,7 +67,7 @@ describe("resolveNodeOnlyGatewayInfo", () => {
6767
});
6868

6969
it("falls back to an unknown gateway target when node-only config is missing", async () => {
70-
mocks.loadNodeHostConfig.mockResolvedValueOnce(null);
70+
mocks.loadNodeHostConfigReadOnly.mockResolvedValueOnce(null);
7171

7272
await expect(
7373
resolveNodeOnlyGatewayInfo({

‎src/commands/status.node-mode.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// On these machines the local gateway daemon is absent by design, but the node service may point at a remote gateway.
33

44
import { DEFAULT_GATEWAY_PORT } from "../config/paths.js";
5-
import { loadNodeHostConfig } from "../node-host/config.js";
5+
import { loadNodeHostConfigReadOnly } from "../node-host/config.js";
66

77
type NodeOnlyServiceLike = {
88
installed: boolean | null;
@@ -66,7 +66,7 @@ export async function resolveNodeOnlyGatewayInfo(params: {
6666
return null;
6767
}
6868

69-
const gatewayTarget = resolveNodeGatewayTarget((await loadNodeHostConfig())?.gateway);
69+
const gatewayTarget = resolveNodeGatewayTarget((await loadNodeHostConfigReadOnly())?.gateway);
7070
return {
7171
gatewayTarget,
7272
gatewayValue: `node → ${gatewayTarget} · no local gateway`,

0 commit comments

Comments
 (0)