Skip to content

Commit 5dcc91d

Browse files
fix(cli): honor explicit system agent in configure (#125377)
* fix(cli): honor explicit system agent in configure * fix(cli): persist explicit agent workspace * fix(cli): preserve authored agent key casing * fix(cli): preserve configured system agent key * fix(configure): preserve legacy onboarding owner * fix(configure): preserve workspace ownership semantics * fix(configure): retain migrated legacy owner * fix(configure): retain legacy owner marker --------- Co-authored-by: Patrick Erichsen <[email protected]>
1 parent 47af5a0 commit 5dcc91d

4 files changed

Lines changed: 257 additions & 6 deletions

File tree

‎src/commands/configure.wizard.default-agent.test.ts‎

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Configure wizard tests keep workspace-owned effects on the configured default agent.
22
import { beforeEach, describe, expect, it, vi } from "vitest";
3+
import { retainLegacyDefaultAgentId } from "../config/legacy.default-agent-owner.js";
34
import type { OpenClawConfig } from "../config/types.openclaw.js";
45
import type { RuntimeEnv } from "../runtime.js";
56

@@ -159,6 +160,192 @@ describe("runConfigureWizard default-agent ownership", () => {
159160
);
160161
});
161162

163+
it("uses the configured system agent when ownership is explicit", async () => {
164+
const baseConfig = {
165+
agents: {
166+
ownership: "explicit",
167+
defaults: {
168+
workspace: "/tmp/global-workspace",
169+
systemAgent: { agentId: "main" },
170+
},
171+
entries: {
172+
MAIN: {
173+
agentDir: "/tmp/main-agent",
174+
},
175+
ops: {
176+
agentDir: "/tmp/ops-agent",
177+
workspace: "/tmp/ops-workspace",
178+
},
179+
},
180+
},
181+
} satisfies OpenClawConfig;
182+
mocks.state.snapshot = {
183+
exists: true,
184+
valid: true,
185+
hash: "config-hash",
186+
config: baseConfig,
187+
sourceConfig: baseConfig,
188+
issues: [],
189+
};
190+
mocks.text.mockResolvedValue("/tmp/new-main-workspace");
191+
192+
await runConfigureWizard(
193+
{ command: "configure", sections: ["workspace", "plugins", "skills"] },
194+
runtime,
195+
);
196+
197+
expect(mocks.commitConfig).toHaveBeenCalledWith(
198+
expect.objectContaining({
199+
nextConfig: expect.objectContaining({
200+
agents: expect.objectContaining({
201+
defaults: expect.objectContaining({ workspace: "/tmp/global-workspace" }),
202+
entries: {
203+
MAIN: {
204+
agentDir: "/tmp/main-agent",
205+
workspace: "/tmp/new-main-workspace",
206+
},
207+
ops: {
208+
agentDir: "/tmp/ops-agent",
209+
workspace: "/tmp/ops-workspace",
210+
},
211+
},
212+
}),
213+
}),
214+
}),
215+
);
216+
expect(mocks.setupPluginConfig).toHaveBeenCalledWith(
217+
expect.objectContaining({ workspaceDir: "/tmp/new-main-workspace" }),
218+
);
219+
expect(mocks.setupSkills).toHaveBeenCalledWith(
220+
expect.any(Object),
221+
"/tmp/new-main-workspace",
222+
runtime,
223+
expect.any(Object),
224+
);
225+
expect(mocks.ensureWorkspaceAndSessions).toHaveBeenCalledWith(
226+
"/tmp/new-main-workspace",
227+
runtime,
228+
expect.objectContaining({ agentId: "main" }),
229+
);
230+
});
231+
232+
it("preserves the legacy default owner when system-agent ownership is not explicit", async () => {
233+
const baseConfig = {
234+
agents: {
235+
defaults: {
236+
workspace: "/tmp/global-workspace",
237+
systemAgent: { agentId: "main" },
238+
},
239+
entries: {
240+
MAIN: { agentDir: "/tmp/main-agent" },
241+
ops: {
242+
default: true,
243+
agentDir: "/tmp/ops-agent",
244+
workspace: "/tmp/ops-workspace",
245+
},
246+
},
247+
},
248+
} satisfies OpenClawConfig;
249+
mocks.state.snapshot = {
250+
exists: true,
251+
valid: true,
252+
hash: "config-hash",
253+
config: baseConfig,
254+
sourceConfig: baseConfig,
255+
issues: [],
256+
};
257+
mocks.text.mockResolvedValue("/tmp/new-ops-workspace");
258+
259+
await runConfigureWizard(
260+
{ command: "configure", sections: ["workspace", "plugins", "skills"] },
261+
runtime,
262+
);
263+
264+
expect(mocks.setupPluginConfig).toHaveBeenCalledWith(
265+
expect.objectContaining({ workspaceDir: "/tmp/new-ops-workspace" }),
266+
);
267+
expect(mocks.setupSkills).toHaveBeenCalledWith(
268+
expect.any(Object),
269+
"/tmp/new-ops-workspace",
270+
runtime,
271+
expect.any(Object),
272+
);
273+
expect(mocks.ensureWorkspaceAndSessions).toHaveBeenCalledWith(
274+
"/tmp/new-ops-workspace",
275+
runtime,
276+
expect.objectContaining({ agentId: "ops" }),
277+
);
278+
expect(mocks.commitConfig).toHaveBeenCalledWith(
279+
expect.objectContaining({
280+
nextConfig: expect.objectContaining({
281+
agents: expect.objectContaining({
282+
entries: expect.objectContaining({
283+
MAIN: expect.not.objectContaining({ workspace: "/tmp/new-ops-workspace" }),
284+
ops: expect.objectContaining({ workspace: "/tmp/new-ops-workspace" }),
285+
}),
286+
}),
287+
}),
288+
}),
289+
);
290+
});
291+
292+
it("keeps a workspace-less legacy owner on the global workspace", async () => {
293+
const baseConfig = {
294+
agents: {
295+
defaults: {
296+
workspace: "/tmp/global-workspace",
297+
systemAgent: { agentId: "main" },
298+
},
299+
entries: {
300+
main: { agentDir: "/tmp/main-agent" },
301+
ops: {
302+
agentDir: "/tmp/ops-agent",
303+
},
304+
},
305+
},
306+
} satisfies OpenClawConfig;
307+
retainLegacyDefaultAgentId(baseConfig, "ops");
308+
mocks.state.snapshot = {
309+
exists: true,
310+
valid: true,
311+
hash: "config-hash",
312+
config: baseConfig,
313+
sourceConfig: baseConfig,
314+
issues: [],
315+
};
316+
mocks.text.mockResolvedValue("/tmp/new-global-workspace");
317+
318+
await runConfigureWizard(
319+
{ command: "configure", sections: ["workspace", "plugins", "skills"] },
320+
runtime,
321+
);
322+
323+
expect(mocks.commitConfig).toHaveBeenCalledWith(
324+
expect.objectContaining({
325+
nextConfig: expect.objectContaining({
326+
agents: expect.objectContaining({
327+
defaults: expect.objectContaining({ workspace: "/tmp/new-global-workspace" }),
328+
entries: baseConfig.agents.entries,
329+
}),
330+
}),
331+
}),
332+
);
333+
expect(mocks.setupPluginConfig).toHaveBeenCalledWith(
334+
expect.objectContaining({ workspaceDir: "/tmp/new-global-workspace" }),
335+
);
336+
expect(mocks.setupSkills).toHaveBeenCalledWith(
337+
expect.any(Object),
338+
"/tmp/new-global-workspace",
339+
runtime,
340+
expect.any(Object),
341+
);
342+
expect(mocks.ensureWorkspaceAndSessions).toHaveBeenCalledWith(
343+
"/tmp/new-global-workspace",
344+
runtime,
345+
expect.objectContaining({ agentId: "ops" }),
346+
);
347+
});
348+
162349
it("does not persist an unprovisionable workspace", async () => {
163350
mocks.ensureWorkspaceAndSessions.mockRejectedValueOnce(new Error("workspace is unwritable"));
164351

‎src/commands/configure.wizard.ts‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import { formatCliCommand } from "../cli/command-format.js";
88
import { formatPortRangeHint } from "../cli/error-format.js";
99
import { parsePort } from "../cli/shared/parse-port.js";
1010
import { readConfigFileSnapshotForWrite, resolveGatewayPort } from "../config/config.js";
11+
import { inheritLegacyDefaultAgentId } from "../config/legacy.default-agent-owner.js";
1112
import { logConfigUpdated } from "../config/logging.js";
1213
import type { OpenClawConfig } from "../config/types.openclaw.js";
1314
import { resolveGatewayProbeAuthSafeWithSecretInputs } from "../gateway/probe-auth.js";
1415
import { formatWindowsGatewayFirewallGuidance } from "../infra/windows-gateway-firewall-diagnostics.js";
1516
import { commitConfigWithPendingPluginInstalls } from "../plugins/install-record-commit.js";
1617
import { resolvePluginContributionOwners } from "../plugins/plugin-registry.js";
18+
import { normalizeAgentId } from "../routing/session-key.js";
1719
import type { RuntimeEnv } from "../runtime.js";
1820
import { defaultRuntime } from "../runtime.js";
1921
import { createLazyImportLoader } from "../shared/lazy-promise.js";
@@ -44,6 +46,7 @@ import { healthCommand } from "./health.js";
4446
import {
4547
ensureOnboardingAgentWorkspace,
4648
resolveOnboardingAgentTarget,
49+
resolveSystemAgentOnboardingTarget,
4750
} from "./onboard-agent-target.js";
4851
import { setupChannels } from "./onboard-channels.js";
4952
import {
@@ -608,7 +611,12 @@ export async function runConfigureWizard(
608611
};
609612
didSetGatewayMode = true;
610613
}
611-
const resolveSetupTarget = () => resolveOnboardingAgentTarget(nextConfig);
614+
// Configure keeps legacy default-owner semantics; only explicit fleets opt into
615+
// the System Agent target used unconditionally by setup and recovery callers.
616+
const resolveSetupTarget = () =>
617+
nextConfig.agents?.ownership === "explicit"
618+
? resolveSystemAgentOnboardingTarget(nextConfig)
619+
: resolveOnboardingAgentTarget(inheritLegacyDefaultAgentId(baseConfig, nextConfig));
612620
let workspaceDir = resolveSetupTarget().workspaceDir;
613621
let gatewayPort = resolveGatewayPort(baseConfig);
614622

@@ -665,16 +673,24 @@ export async function runConfigureWizard(
665673
}
666674
}
667675
const target = resolveSetupTarget();
668-
const targetEntry = nextConfig.agents?.entries?.[target.agentId];
676+
const authoredEntryKey = Object.keys(nextConfig.agents?.entries ?? {}).find(
677+
(key) => normalizeAgentId(key) === target.agentId,
678+
);
679+
const targetEntry = authoredEntryKey
680+
? nextConfig.agents?.entries?.[authoredEntryKey]
681+
: undefined;
682+
// Explicit fleets own workspace at the selected entry even when it inherited
683+
// the global default; legacy owners stay global until they author an override.
669684
nextConfig =
670-
targetEntry?.workspace !== undefined
685+
targetEntry?.workspace !== undefined ||
686+
(nextConfig.agents?.ownership === "explicit" && targetEntry !== undefined)
671687
? {
672688
...nextConfig,
673689
agents: {
674690
...nextConfig.agents,
675691
entries: {
676692
...nextConfig.agents?.entries,
677-
[target.agentId]: { ...targetEntry, workspace: workspaceDir },
693+
[authoredEntryKey ?? target.agentId]: { ...targetEntry, workspace: workspaceDir },
678694
},
679695
},
680696
}

‎src/commands/onboard-agent-target.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,34 @@ import { retainLegacyDefaultAgentId } from "../config/legacy.default-agent-owner
77
import type { RuntimeEnv } from "../runtime.js";
88
import { withEnvAsync } from "../test-utils/env.js";
99
import {
10+
applyOnboardingPrimaryModel,
1011
ensureOnboardingAgentWorkspace,
1112
resolveOnboardingAgentTarget,
13+
resolveSystemAgentOnboardingTarget,
1214
} from "./onboard-agent-target.js";
1315

1416
const tempDirs = useAutoCleanupTempDirTracker(afterEach);
1517

1618
describe("onboarding agent target", () => {
19+
it("preserves an uppercase authored entry key when applying the primary model", () => {
20+
const config = {
21+
agents: {
22+
ownership: "explicit" as const,
23+
entries: {
24+
MAIN: { model: "openai/old" },
25+
},
26+
},
27+
};
28+
const target = resolveOnboardingAgentTarget(config, "main");
29+
30+
expect(applyOnboardingPrimaryModel(config, target, "openai/new").agents?.entries).toEqual({
31+
MAIN: {
32+
model: { primary: "openai/new" },
33+
models: { "openai/new": {} },
34+
},
35+
});
36+
});
37+
1738
it("uses the retained compatibility owner after the marker is removed", () => {
1839
const config = retainLegacyDefaultAgentId(
1940
{ agents: { entries: { main: {}, ops: { workspace: "/srv/ops" } } } },
@@ -26,6 +47,30 @@ describe("onboarding agent target", () => {
2647
});
2748
});
2849

50+
it("resolves shared system-agent setup to the configured system agent on a legacy roster", () => {
51+
const config = {
52+
agents: {
53+
defaults: {
54+
workspace: "/srv/global",
55+
systemAgent: { agentId: "main" },
56+
},
57+
entries: {
58+
main: { workspace: "/srv/main" },
59+
ops: { default: true, workspace: "/srv/ops" },
60+
},
61+
},
62+
};
63+
64+
expect(resolveOnboardingAgentTarget(config)).toMatchObject({
65+
agentId: "ops",
66+
workspaceDir: "/srv/ops",
67+
});
68+
expect(resolveSystemAgentOnboardingTarget(config)).toMatchObject({
69+
agentId: "main",
70+
workspaceDir: "/srv/main",
71+
});
72+
});
73+
2974
it("provisions the configured default agent workspace and sessions", async () => {
3075
const stateDir = tempDirs.make("openclaw-onboard-target-");
3176
const globalWorkspace = path.join(stateDir, "global-workspace");

‎src/commands/onboard-agent-target.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ export function applyOnboardingPrimaryModel(
6161
target: OnboardingAgentTarget,
6262
model: string,
6363
): OpenClawConfig {
64-
const entry = config.agents?.entries?.[target.agentId];
64+
const authoredEntryKey = Object.keys(config.agents?.entries ?? {}).find(
65+
(key) => normalizeAgentId(key) === target.agentId,
66+
);
67+
const entry = authoredEntryKey ? config.agents?.entries?.[authoredEntryKey] : undefined;
6568
if (entry?.model === undefined) {
6669
return applyPrimaryModel(config, model);
6770
}
@@ -77,7 +80,7 @@ export function applyOnboardingPrimaryModel(
7780
...config.agents,
7881
entries: {
7982
...config.agents?.entries,
80-
[target.agentId]: {
83+
[authoredEntryKey ?? target.agentId]: {
8184
...entry,
8285
model: {
8386
...(fallbackValues.length > 0 ? { fallbacks: fallbackValues } : {}),

0 commit comments

Comments
 (0)