From 226e1afa4d82fcd071828635d2e2c82e4f44bac3 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Tue, 7 Apr 2026 01:48:21 +0800 Subject: [PATCH] Commands: narrow agent snapshot test seam --- src/agents/agent-command.ts | 53 ++++++++++++++++++++++++------------- src/commands/agent.test.ts | 5 ++-- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/src/agents/agent-command.ts b/src/agents/agent-command.ts index 71943f4271b..26eeb379e80 100644 --- a/src/agents/agent-command.ts +++ b/src/agents/agent-command.ts @@ -18,6 +18,7 @@ import { loadConfig, readConfigFileSnapshotForWrite, setRuntimeConfigSnapshot, + type OpenClawConfig, } from "../config/config.js"; import { resolveAgentIdFromSessionKey, type SessionEntry } from "../config/sessions.js"; import { resolveSessionTranscriptFile } from "../config/sessions/transcript.js"; @@ -127,6 +128,33 @@ async function persistSessionEntry(params: PersistSessionEntryParams): Promise { + const loadedRaw = loadConfig(); + const sourceConfig = await (async () => { + try { + const { snapshot } = await readConfigFileSnapshotForWrite(); + if (snapshot.valid) { + return snapshot.resolved; + } + } catch { + // Fall back to runtime-loaded config when source snapshot is unavailable. + } + return loadedRaw; + })(); + const { resolvedConfig: cfg } = await resolveCommandConfigWithSecrets({ + config: loadedRaw, + commandName: "agent", + targetIds: getAgentRuntimeCommandSecretTargetIds(), + runtime, + }); + setRuntimeConfigSnapshot(cfg, sourceConfig); + return { loadedRaw, sourceConfig, cfg }; +} + function containsControlCharacters(value: string): boolean { for (const char of value) { const code = char.codePointAt(0); @@ -168,25 +196,7 @@ async function prepareAgentCommandExecution( throw new Error("Pass --to , --session-id, or --agent to choose a session"); } - const loadedRaw = loadConfig(); - const sourceConfig = await (async () => { - try { - const { snapshot } = await readConfigFileSnapshotForWrite(); - if (snapshot.valid) { - return snapshot.resolved; - } - } catch { - // Fall back to runtime-loaded config when source snapshot is unavailable. - } - return loadedRaw; - })(); - const { resolvedConfig: cfg } = await resolveCommandConfigWithSecrets({ - config: loadedRaw, - commandName: "agent", - targetIds: getAgentRuntimeCommandSecretTargetIds(), - runtime, - }); - setRuntimeConfigSnapshot(cfg, sourceConfig); + const { cfg } = await resolveAgentRuntimeConfig(runtime); const normalizedSpawned = normalizeSpawnedRunMetadata({ spawnedBy: opts.spawnedBy, groupId: opts.groupId, @@ -1003,3 +1013,8 @@ export async function agentCommandFromIngress( deps, ); } + +export const __testing = { + resolveAgentRuntimeConfig, + prepareAgentCommandExecution, +}; diff --git a/src/commands/agent.test.ts b/src/commands/agent.test.ts index 2f995f76bb8..c0af41d856f 100644 --- a/src/commands/agent.test.ts +++ b/src/commands/agent.test.ts @@ -4,6 +4,7 @@ import { beforeEach, describe, expect, it, type MockInstance, vi } from "vitest" import { withTempHome as withTempHomeBase } from "../../test/helpers/temp-home.js"; import "../cron/isolated-agent.mocks.js"; import { __testing as acpManagerTesting } from "../acp/control-plane/manager.js"; +import { __testing as agentCommandTesting } from "../agents/agent-command.js"; import { resolveAgentDir, resolveSessionAgentId } from "../agents/agent-scope.js"; import * as authProfilesModule from "../agents/auth-profiles.js"; import * as sessionStoreModule from "../agents/command/session-store.js"; @@ -370,7 +371,7 @@ describe("agentCommand", () => { diagnostics: [], }); - await agentCommand({ message: "hello", to: "+1555" }, runtime); + const prepared = await agentCommandTesting.resolveAgentRuntimeConfig(runtime); expect(resolveConfigWithSecretsSpy).toHaveBeenCalledWith({ config: loadedConfig, @@ -379,7 +380,7 @@ describe("agentCommand", () => { runtime, }); expect(setRuntimeConfigSnapshotSpy).toHaveBeenCalledWith(resolvedConfig, sourceConfig); - expect(vi.mocked(runEmbeddedPiAgent).mock.calls.at(-1)?.[0]?.config).toBe(resolvedConfig); + expect(prepared.cfg).toBe(resolvedConfig); }); });