Skip to content

Commit 9500f7f

Browse files
committed
fix(cli): align agent exec with system owner
1 parent d91b290 commit 9500f7f

2 files changed

Lines changed: 95 additions & 2 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import fs from "node:fs/promises";
2+
import path from "node:path";
3+
import { afterEach, describe, expect, it, vi } from "vitest";
4+
import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js";
5+
import { resolveSessionStorePathCore } from "../config/sessions/paths.js";
6+
import { resolveSqliteScope } from "../config/sessions/session-accessor.sqlite-scope.js";
7+
import type { RuntimeEnv } from "../runtime.js";
8+
import { agentExecCommand } from "./agent-exec.js";
9+
10+
const tempDirs = useAutoCleanupTempDirTracker(afterEach);
11+
12+
const runtime: RuntimeEnv = {
13+
log: vi.fn(),
14+
error: vi.fn(),
15+
exit: vi.fn(),
16+
};
17+
18+
afterEach(() => {
19+
vi.restoreAllMocks();
20+
});
21+
22+
describe("agent exec owner selection", () => {
23+
it.each([
24+
{
25+
name: "a migrated legacy default",
26+
config: {
27+
agents: {
28+
entries: {
29+
alpha: { default: true },
30+
beta: {},
31+
},
32+
},
33+
},
34+
},
35+
{
36+
name: "the canonical system-agent owner",
37+
config: {
38+
agents: {
39+
ownership: "explicit",
40+
defaults: { systemAgent: { agentId: "alpha" } },
41+
entries: { alpha: {}, beta: {} },
42+
},
43+
},
44+
},
45+
])("uses $name for the run and its SQLite store scope", async ({ config }) => {
46+
const configRoot = tempDirs.make("openclaw-agent-exec-default-agent-");
47+
const configPath = path.join(configRoot, "openclaw.json");
48+
await fs.writeFile(configPath, JSON.stringify(config), "utf8");
49+
const runAgent = vi.fn(async (options: Record<string, unknown>) => {
50+
const requestedAgentId = typeof options.agentId === "string" ? options.agentId : "main";
51+
const sessionId = String(options.sessionId);
52+
const storePath = resolveSessionStorePathCore(undefined, {
53+
agentId: "alpha",
54+
env: process.env,
55+
});
56+
57+
// Exercise the production guard that rejects keys owned by another agent.
58+
expect(() =>
59+
resolveSqliteScope({
60+
agentId: requestedAgentId,
61+
defaultAgentId: "alpha",
62+
env: process.env,
63+
sessionKey: `agent:${requestedAgentId}:explicit:${sessionId}`,
64+
storePath,
65+
}),
66+
).not.toThrow();
67+
expect(requestedAgentId).toBe("alpha");
68+
return {
69+
payloads: [{ text: "done" }],
70+
meta: { durationMs: 1 },
71+
};
72+
});
73+
74+
const result = await agentExecCommand("inspect", { config: configPath }, runtime, { runAgent });
75+
76+
expect(result.envelope.error).toBeUndefined();
77+
expect(result.exitCode).toBe(0);
78+
expect(runAgent).toHaveBeenCalledOnce();
79+
});
80+
});

‎src/commands/agent-exec.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,15 +629,27 @@ export async function agentExecCommand(
629629
const pluginInstallRoots = pluginInstallContext?.resolvePluginInstallRoots();
630630
const timeout = normalizeTimeoutSeconds(opts.timeout);
631631
const fallbacks = normalizeFallbacks(opts.model, opts.fallback);
632-
const { resolveDefaultAgentDir } = await import("../agents/agent-scope-config.js");
632+
const {
633+
resolveAgentDir,
634+
resolveSystemAgentTargetAgentId,
635+
tryResolveLegacyCompatibilityAgentId,
636+
} = await import("../agents/agent-scope-config.js");
633637
// Resolve from the inherited config, not `{}`: the default agent may declare
634638
// its own `agentDir`, and that is where its stored auth profiles live. This
635639
// reads `baseConfig` rather than `runConfig` because the run config
636640
// deliberately strips agent directories to keep run state ephemeral, while
637641
// credential ownership must still follow the operator's configuration.
638642
// Computed before the environment repoints the state dir so the unconfigured
639643
// case still resolves against the real one.
640-
const storedAuthAgentDir = resolveDefaultAgentDir(baseConfig);
644+
const execAgentId =
645+
tryResolveLegacyCompatibilityAgentId(baseConfig) ??
646+
resolveSystemAgentTargetAgentId(baseConfig, undefined, {
647+
surface: "agent exec",
648+
hint: "Set agents.defaults.systemAgent.agentId.",
649+
});
650+
// Auth, session keys, and SQLite ownership must share one resolved owner.
651+
// Splitting these paths can select an agent's store but emit a `main` key.
652+
const storedAuthAgentDir = resolveAgentDir(baseConfig, execAgentId);
641653
restoreEnvironment = setAgentExecEnvironment({ stateDir, cwd });
642654
runtimePaths = await import("../config/paths.js");
643655
runtimePaths.pinRuntimePaths();
@@ -692,6 +704,7 @@ export async function agentExecCommand(
692704
{
693705
message: prompt,
694706
sessionId,
707+
agentId: execAgentId,
695708
workspaceDir: cwd,
696709
cwd,
697710
model: opts.model,

0 commit comments

Comments
 (0)