Skip to content

Commit eed6416

Browse files
refactor(cli): make multi-agent operations agent-owned (#123871)
* refactor(cli): make multi-agent operations agent-owned * fix(cli): keep provider setup on agent target * fix(cli): preserve legacy operation ownership * fix(cli): align capability target calls with current resolver * fix(cli): preserve operation-specific agent errors * fix(cli): keep allowlist selection agent-scoped * fix(cli): preserve agent-owned model operations * fix(cli): preserve list-form agent rosters --------- Co-authored-by: Josh Lehman <[email protected]>
1 parent 29cfd19 commit eed6416

14 files changed

Lines changed: 523 additions & 50 deletions

‎src/agents/agent-scope-config.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
listAgentEntriesWithSource,
88
listAgentIds,
99
resolveAgentConfig,
10+
resolveAgentOperationAgentId,
1011
resolveAgentWorkspaceDir,
1112
resolveAmbientOwnerAgentId,
1213
resolveDefaultAgentDir,
@@ -154,6 +155,37 @@ describe("agent roster resolution", () => {
154155
expect(resolveDefaultAgentDir(config)).toBe("/tmp/openclaw-beta-agent");
155156
});
156157

158+
it("preserves legacy default ownership for non-explicit CLI operations", () => {
159+
const config = {
160+
agents: {
161+
entries: { main: {}, ops: { default: true } },
162+
},
163+
};
164+
165+
expect(resolveAgentOperationAgentId(config)).toBe("ops");
166+
expect(
167+
resolveAgentOperationAgentId({
168+
...config,
169+
agents: {
170+
...config.agents,
171+
ownership: "explicit" as const,
172+
defaults: { systemAgent: { agentId: "main" } },
173+
},
174+
}),
175+
).toBe("main");
176+
});
177+
178+
it("preserves retained legacy ownership for migrated CLI operations", () => {
179+
const cfg = migratePersistedImplicitMainRoster({
180+
agents: {
181+
entries: { ops: { default: true }, research: {} },
182+
},
183+
}).config as OpenClawConfig;
184+
185+
expect(cfg.agents?.entries?.ops?.default).toBeUndefined();
186+
expect(resolveAgentOperationAgentId(cfg)).toBe("ops");
187+
});
188+
157189
it("resolves defaults only for the rosterless implicit main agent", () => {
158190
const defaults = { fastModeDefault: "auto" as const };
159191

‎src/agents/agent-scope-config.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,18 @@ export function resolveAmbientOwnerAgentId(
242242
return tryResolveAmbientOwnerAgentId(cfg, requestedAgentId) ?? resolveSoleAgentId(cfg, context);
243243
}
244244

245+
/** Resolves a CLI operation owner while preserving legacy default markers outside explicit fleets. */
246+
export function resolveAgentOperationAgentId(
247+
cfg: OpenClawConfig,
248+
requestedAgentId?: string,
249+
context?: AgentSelectionContext,
250+
): string {
251+
if (requestedAgentId !== undefined || cfg.agents?.ownership === "explicit") {
252+
return resolveAmbientOwnerAgentId(cfg, requestedAgentId, context);
253+
}
254+
return tryResolveLegacyCompatibilityAgentId(cfg) ?? resolveDefaultAgentId(cfg, context);
255+
}
256+
245257
/**
246258
* @deprecated Ambient system work uses resolveAmbientOwnerAgentId so the configured
247259
* system agent is honored; explicit-selection surfaces use resolveSoleAgentId. This

‎src/cli/capability-cli/shared.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
parseStrictFiniteNumber,
33
parseStrictPositiveInteger,
44
} from "@openclaw/normalization-core/number-coercion";
5-
import { listAgentIds, resolveAmbientOwnerAgentId } from "../../agents/agent-scope-config.js";
5+
import { listAgentIds, resolveAgentOperationAgentId } from "../../agents/agent-scope-config.js";
66
import { resolveAgentDir } from "../../agents/agent-scope.js";
77
import {
88
listProfilesForProvider,
@@ -111,7 +111,7 @@ export function resolveCapabilityProviderAgentId(
111111
if (rawAgentId !== undefined && !requestedAgentId) {
112112
throw new Error("--agent must not be blank");
113113
}
114-
const agentId = resolveAmbientOwnerAgentId(cfg, requestedAgentId, {
114+
const agentId = resolveAgentOperationAgentId(cfg, requestedAgentId, {
115115
surface,
116116
hint: "Pass --agent <id> or set agents.defaults.systemAgent.agentId.",
117117
});

‎src/commands/channels/add-wizard.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
// prompter) and the gateway `wizard.start {flow:"channels"}` RPC (session
33
// prompter driving the Control UI / native clients).
44
import { normalizeOptionalLowercaseString } from "@openclaw/normalization-core/string-coerce";
5-
import { resolveAgentWorkspaceDir, resolveDefaultAgentId } from "../../agents/agent-scope.js";
5+
import { resolveAgentOperationAgentId } from "../../agents/agent-scope-config.js";
6+
import { resolveAgentWorkspaceDir } from "../../agents/agent-scope.js";
67
import { getLoadedChannelPlugin } from "../../channels/plugins/index.js";
78
import type { ChannelSetupPlugin } from "../../channels/plugins/setup-wizard-types.js";
89
import { formatUnknownChannelMessage } from "../../cli/error-format.js";
@@ -50,7 +51,7 @@ export async function resolveInitialWizardChannelTarget(
5051
const resolved = resolveChannelSetupEntries({
5152
cfg,
5253
installedPlugins: listActiveChannelSetupPlugins(),
53-
workspaceDir: resolveAgentWorkspaceDir(cfg, resolveDefaultAgentId(cfg)),
54+
workspaceDir: resolveAgentWorkspaceDir(cfg, resolveAgentOperationAgentId(cfg)),
5455
});
5556
const matchedEntry =
5657
resolved.entries.find(
@@ -211,7 +212,7 @@ export async function runChannelsAddWizardFlow(params: ChannelsAddWizardFlowPara
211212
initialValue: true,
212213
});
213214
if (bindNow) {
214-
const defaultAgentId = resolveDefaultAgentId(nextConfig);
215+
const defaultAgentId = resolveAgentOperationAgentId(nextConfig);
215216
for (const target of bindTargets) {
216217
const targetAgentId = await prompter.select({
217218
message: `Send ${target.channel}/${target.accountId} messages to agent`,

‎src/commands/channels/add.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { parseStrictNonNegativeInteger } from "@openclaw/normalization-core/number-coercion";
22
// Implements guided and non-interactive `openclaw channels add` account setup.
33
import { normalizeOptionalLowercaseString } from "@openclaw/normalization-core/string-coerce";
4-
import { resolveAgentWorkspaceDir, resolveDefaultAgentId } from "../../agents/agent-scope.js";
4+
import { resolveAgentOperationAgentId } from "../../agents/agent-scope-config.js";
5+
import { resolveAgentWorkspaceDir } from "../../agents/agent-scope.js";
56
import {
67
applyPreparedChannelAccountConfiguration,
78
type ChannelAccountMutationPlugin,
@@ -64,7 +65,7 @@ async function resolveCatalogChannelEntry(raw: string, cfg: OpenClawConfig | nul
6465
({ listTrustedChannelPluginCatalogEntries }) =>
6566
listTrustedChannelPluginCatalogEntries({
6667
cfg,
67-
workspaceDir: resolveAgentWorkspaceDir(cfg, resolveDefaultAgentId(cfg)),
68+
workspaceDir: resolveAgentWorkspaceDir(cfg, resolveAgentOperationAgentId(cfg)),
6869
}),
6970
)
7071
: await import("../../channels/plugins/catalog.js").then(
@@ -187,7 +188,7 @@ async function channelsAddCommandImpl(
187188
let channel = normalizeChannelId(rawChannel);
188189
let catalogEntry = await resolveCatalogChannelEntry(rawChannel, nextConfig);
189190
const resolveWorkspaceDir = () =>
190-
resolveAgentWorkspaceDir(nextConfig, resolveDefaultAgentId(nextConfig));
191+
resolveAgentWorkspaceDir(nextConfig, resolveAgentOperationAgentId(nextConfig));
191192
// May load a scoped plugin when the channel is not already registered.
192193
const loadScopedPlugin = async (
193194
channelId: ChannelId,

‎src/commands/configure.gateway-auth.prompt-auth-config.test.ts‎

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ function makeRuntime(): RuntimeEnv {
223223
function promptModelAllowlistOptions(index = 0) {
224224
return mocks.promptModelAllowlist.mock.calls[index]?.[0] as
225225
| {
226+
agentDir?: string;
227+
agentId?: string;
226228
allowedKeys?: string[];
227229
initialSelections?: string[];
228230
loadCatalog?: boolean;
@@ -501,7 +503,6 @@ describe("promptAuthConfig", () => {
501503

502504
expect(mocks.promptModelAllowlist).toHaveBeenCalledOnce();
503505
expect(promptModelAllowlistOptions()?.preferredProvider).toBe("openai");
504-
expect(mocks.applyPrimaryModel).toHaveBeenCalledWith(expect.any(Object), "openai/gpt-5.5");
505506
expect(result.agents?.defaults?.model).toEqual({
506507
primary: "openai/gpt-5.5",
507508
fallbacks: ["openai/gpt-5.3-codex"],
@@ -512,6 +513,41 @@ describe("promptAuthConfig", () => {
512513
]);
513514
});
514515

516+
it("canonicalizes a selected agent's legacy Codex primary before updating its allowlist", async () => {
517+
vi.clearAllMocks();
518+
mocks.promptAuthChoiceGrouped.mockResolvedValue("openai-device-code");
519+
mocks.resolvePreferredProviderForAuthChoice.mockResolvedValue("openai");
520+
const config = {
521+
agents: {
522+
ownership: "explicit" as const,
523+
defaults: {
524+
systemAgent: { agentId: "ops" },
525+
model: { primary: "anthropic/claude-sonnet-4-6" },
526+
},
527+
entries: {
528+
main: {},
529+
ops: { model: { primary: "codex/gpt-5.5" } },
530+
},
531+
},
532+
} satisfies OpenClawConfig;
533+
mocks.applyAuthChoice.mockResolvedValue({ config });
534+
mocks.promptModelAllowlist.mockResolvedValue({
535+
models: ["openai/gpt-5.5"],
536+
scopeKeys: ["openai/gpt-5.5"],
537+
});
538+
mocks.resolveProviderPluginChoiceCore.mockReturnValue(null);
539+
540+
const result = await promptAuthConfig(config, makeRuntime(), noopPrompter, {
541+
agentId: "ops",
542+
agentDir: "/tmp/ops-agent",
543+
workspaceDir: "/tmp/ops-workspace",
544+
});
545+
546+
expect(result.agents?.entries?.ops?.model).toEqual({ primary: "openai/gpt-5.5" });
547+
expect(result.agents?.entries?.ops?.modelPolicy?.allow).toEqual(["openai/gpt-5.5"]);
548+
expect(result.agents?.defaults?.model).toEqual({ primary: "anthropic/claude-sonnet-4-6" });
549+
});
550+
515551
it("keeps the selected provider scope when existing config has another provider", async () => {
516552
vi.clearAllMocks();
517553
mocks.promptAuthChoiceGrouped.mockResolvedValue("github-copilot");
@@ -752,4 +788,115 @@ describe("promptAuthConfig", () => {
752788
expect(mocks.applyAuthChoice).toHaveBeenCalledTimes(2);
753789
expect(mocks.promptModelAllowlist).toHaveBeenCalledTimes(1);
754790
});
791+
792+
it("writes model policy to the explicit configure target instead of global defaults", async () => {
793+
vi.clearAllMocks();
794+
mocks.promptAuthChoiceGrouped.mockResolvedValue("skip");
795+
mocks.promptDefaultModel.mockResolvedValue({ model: "openai/gpt-5.5" });
796+
mocks.promptModelAllowlist.mockResolvedValue({ models: ["openai/gpt-5.5"] });
797+
798+
const result = await promptAuthConfig(
799+
{
800+
agents: {
801+
ownership: "explicit",
802+
defaults: { systemAgent: { agentId: "ops" } },
803+
entries: { main: {}, ops: {} },
804+
},
805+
},
806+
makeRuntime(),
807+
noopPrompter,
808+
{ agentId: "ops", agentDir: "/tmp/ops-agent", workspaceDir: "/tmp/ops-workspace" },
809+
);
810+
811+
expect(result.agents?.entries?.ops?.model).toEqual({ primary: "openai/gpt-5.5" });
812+
expect(result.agents?.entries?.ops?.modelPolicy?.allow).toEqual(["openai/gpt-5.5"]);
813+
expect(result.agents?.defaults?.model).toBeUndefined();
814+
expect(result.agents?.defaults?.modelPolicy).toBeUndefined();
815+
expect(promptModelAllowlistOptions()).toMatchObject({
816+
agentId: "ops",
817+
agentDir: "/tmp/ops-agent",
818+
});
819+
});
820+
821+
it("projects provider-auth model defaults onto the explicit target", async () => {
822+
vi.clearAllMocks();
823+
mocks.promptAuthChoiceGrouped.mockResolvedValue("provider-auth");
824+
mocks.applyAuthChoice.mockResolvedValue({
825+
config: {
826+
agents: {
827+
ownership: "explicit" as const,
828+
defaults: { model: { primary: "provider/global" } },
829+
entries: { main: {}, OPS: {} },
830+
},
831+
},
832+
agentModelOverride: "provider/selected",
833+
});
834+
mocks.promptModelAllowlist.mockResolvedValue({ models: undefined });
835+
836+
const config = {
837+
agents: {
838+
ownership: "explicit" as const,
839+
defaults: {
840+
systemAgent: { agentId: "ops" },
841+
model: { primary: "provider/original" },
842+
},
843+
entries: { main: {}, OPS: {} },
844+
},
845+
};
846+
const result = await promptAuthConfig(config, makeRuntime(), noopPrompter, {
847+
agentId: "ops",
848+
agentDir: "/tmp/ops-agent",
849+
workspaceDir: "/tmp/ops-workspace",
850+
});
851+
852+
expect(mocks.applyAuthChoice).toHaveBeenCalledWith(
853+
expect.objectContaining({ setDefaultModel: false }),
854+
);
855+
expect(result.agents?.entries?.OPS?.model).toEqual({ primary: "provider/selected" });
856+
expect(result.agents?.defaults?.model).toEqual({ primary: "provider/original" });
857+
expect(result.agents?.entries?.ops).toBeUndefined();
858+
});
859+
860+
it("projects custom-provider model metadata onto the explicit target", async () => {
861+
vi.clearAllMocks();
862+
mocks.promptAuthChoiceGrouped.mockResolvedValue("custom-api-key");
863+
mocks.promptCustomApiConfig.mockResolvedValue({
864+
config: {
865+
agents: {
866+
ownership: "explicit" as const,
867+
entries: {
868+
main: {},
869+
OPS: {
870+
model: { primary: "custom/model" },
871+
models: { "custom/model": { alias: "Custom" } },
872+
},
873+
},
874+
},
875+
models: { providers: { custom: { models: [{ id: "model" }] } } },
876+
},
877+
providerId: "custom",
878+
modelId: "model",
879+
});
880+
881+
const config = {
882+
agents: {
883+
ownership: "explicit" as const,
884+
defaults: { systemAgent: { agentId: "ops" } },
885+
entries: { main: {}, OPS: {} },
886+
},
887+
};
888+
const result = await promptAuthConfig(config, makeRuntime(), noopPrompter, {
889+
agentId: "ops",
890+
agentDir: "/tmp/ops-agent",
891+
workspaceDir: "/tmp/ops-workspace",
892+
});
893+
894+
expect(mocks.promptCustomApiConfig).toHaveBeenCalledWith(
895+
expect.objectContaining({ target: expect.objectContaining({ agentId: "ops" }) }),
896+
);
897+
expect(result.agents?.entries?.OPS?.model).toEqual({ primary: "custom/model" });
898+
expect(result.agents?.entries?.OPS?.models).toEqual({ "custom/model": { alias: "Custom" } });
899+
expect(result.agents?.defaults?.model).toBeUndefined();
900+
expect(result.models?.providers?.custom?.models).toEqual([{ id: "model" }]);
901+
});
755902
});

0 commit comments

Comments
 (0)