Commands: narrow agent snapshot test seam

This commit is contained in:
Peter Steinberger
2026-04-07 01:48:21 +08:00
parent 95fe63e63f
commit 226e1afa4d
2 changed files with 37 additions and 21 deletions

View File

@@ -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<v
});
}
async function resolveAgentRuntimeConfig(runtime: RuntimeEnv): Promise<{
loadedRaw: OpenClawConfig;
sourceConfig: OpenClawConfig;
cfg: OpenClawConfig;
}> {
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 <E.164>, --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,
};

View File

@@ -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);
});
});