Skip to content

Commit 93573e8

Browse files
sashankhsashankh
andauthored
fix(models): --agent is silently ignored by models aliases and models scan (#126864)
* fix(models): reject --agent on aliases and scan instead of ignoring it models aliases list/add/remove and models scan never read the parent --agent option, so an operator scoping one of them to an agent got no feedback that the flag did nothing, and a typo'd or nonexistent agent id was never validated. They only read or write agents.defaults.*, so there is no agent-scoped path for the flag to feed. Reuse the existing set/set-image guard rather than inventing a new mechanism: rename it to rejectAgentScopedModelCommand, give each global-only command an accurate scope phrase, and call it before the command runs. fallbacks/image-fallbacks are deliberately untouched; they are tracked separately in #106346. Fixes #126597 * fix(models): drop release-owned changelog entry * fix(models): simplify global-only scope message Review flagged the production LOC delta. Collapse the runtime scope map into a type-only union and one accurate message covering both the read (aliases list) and write commands; net production delta is now +20. * fix(models): state the guard scope accurately for scan Review finding: the shared message claimed every guarded command 'only reads or writes global model config', but models scan --no-probe prints the catalog and returns before any config update (scan.ts:283). Say what is true of all six instead: they are global and never agent-scoped. Docs line updated to match. --------- Co-authored-by: sashankh <[email protected]>
1 parent 637da87 commit 93573e8

5 files changed

Lines changed: 96 additions & 25 deletions

File tree

‎docs/cli/models.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ openclaw models set-image <model-or-alias>
2828
openclaw models scan
2929
```
3030

31-
`status`, `list`, and `auth` subcommands accept `--agent <id>` to target a configured agent; `scan`, `aliases`, and `fallbacks`/`image-fallbacks` always use the configured default agent, and `set`/`set-image` reject `--agent` outright. When omitted, `--agent`-aware commands use `OPENCLAW_AGENT_DIR` if set, otherwise the configured default agent.
31+
`status`, `list`, and `auth` subcommands accept `--agent <id>` to target a configured agent; `fallbacks`/`image-fallbacks` always use the configured default agent, and `set`, `set-image`, `scan`, and `aliases` reject `--agent` outright because they are global and never agent-scoped. When omitted, `--agent`-aware commands use `OPENCLAW_AGENT_DIR` if set, otherwise the configured default agent.
3232

3333
`models set` and `models set-image` require the provider to be declared by an installed plugin or configured under `models.providers`. An unknown provider exits nonzero without changing config. If the provider is known but the model is absent from the local catalog, the command saves the selection and prints a warning because newly released and self-hosted models may not be cataloged yet. `openclaw doctor --json` reports configured unknown providers; add `--severity-min info` to also see active models that the local catalog cannot confirm.
3434

‎src/cli/models-cli.lazy.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe("models cli lazy runtime boundary", () => {
1919
runtimeLoaded();
2020
return {
2121
defaultRuntime: {},
22-
rejectAgentScopedModelWrite: vi.fn(),
22+
rejectAgentScopedModelCommand: vi.fn(),
2323
resolveModelAgentOption: vi.fn(),
2424
runModelsCommand: vi.fn(),
2525
};
@@ -53,7 +53,7 @@ describe("models cli lazy runtime boundary", () => {
5353
runtimeLoaded();
5454
return {
5555
defaultRuntime,
56-
rejectAgentScopedModelWrite: vi.fn(),
56+
rejectAgentScopedModelCommand: vi.fn(),
5757
resolveModelAgentOption,
5858
runModelsCommand,
5959
};

‎src/cli/models-cli.runtime.ts‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,27 @@ export function resolveModelAgentOption(
2020
);
2121
}
2222

23-
export function rejectAgentScopedModelWrite(
23+
/** `models` subcommands that operate on global state only, never per-agent. */
24+
export type GlobalOnlyModelCommandName =
25+
| "set"
26+
| "set-image"
27+
| "scan"
28+
| "aliases list"
29+
| "aliases add"
30+
| "aliases remove";
31+
32+
export function rejectAgentScopedModelCommand(
2433
command: Command,
25-
commandName: "set" | "set-image",
34+
commandName: GlobalOnlyModelCommandName,
2635
): void {
27-
// Write commands update global defaults; accepting --agent here would imply per-agent mutation.
36+
// None of these resolve an agent, so accepting --agent would imply a scope that
37+
// does not exist. Kept scope-neutral: `scan --no-probe` returns after printing
38+
// the catalog without writing config at all.
2839
const agent = resolveOptionFromCommand<string>(command, "agent");
2940
if (!agent) {
3041
return;
3142
}
3243
throw new Error(
33-
`openclaw models ${commandName} does not support --agent; it only updates global model defaults. Remove --agent, or run ${formatCliCommand("openclaw agents list")} and set the per-agent model in agent config.`,
44+
`openclaw models ${commandName} does not support --agent; it is global and never agent-scoped. Remove --agent, or run ${formatCliCommand("openclaw agents list")} and set the per-agent model in agent config.`,
3445
);
3546
}

‎src/cli/models-cli.test.ts‎

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ const mocks = vi.hoisted(() => ({
1212
modelsSetCommand: vi.fn().mockResolvedValue(undefined),
1313
modelsSetImageCommand: vi.fn().mockResolvedValue(undefined),
1414
noopAsync: vi.fn(async () => undefined),
15+
modelsAliasesAddCommand: vi.fn().mockResolvedValue(undefined),
16+
modelsAliasesListCommand: vi.fn().mockResolvedValue(undefined),
17+
modelsAliasesRemoveCommand: vi.fn().mockResolvedValue(undefined),
18+
modelsScanCommand: vi.fn().mockResolvedValue(undefined),
1519
modelsAuthAddCommand: vi.fn().mockResolvedValue(undefined),
1620
modelsAuthListCommand: vi.fn().mockResolvedValue(undefined),
1721
modelsAuthLoginCommand: vi.fn().mockResolvedValue(undefined),
@@ -25,6 +29,9 @@ const mocks = vi.hoisted(() => ({
2529
}));
2630

2731
const {
32+
modelsAliasesAddCommand,
33+
modelsAliasesListCommand,
34+
modelsAliasesRemoveCommand,
2835
modelsAuthAddCommand,
2936
modelsAuthListCommand,
3037
modelsAuthLoginCommand,
@@ -35,6 +42,7 @@ const {
3542
modelsAuthPasteApiKeyCommand,
3643
modelsAuthPasteTokenCommand,
3744
modelsAuthSetupTokenCommand,
45+
modelsScanCommand,
3846
modelsSetCommand,
3947
modelsSetImageCommand,
4048
modelsStatusCommand,
@@ -65,9 +73,9 @@ vi.mock("../commands/models/auth-order.js", () => ({
6573
modelsAuthOrderSetCommand: mocks.modelsAuthOrderSetCommand,
6674
}));
6775
vi.mock("../commands/models/aliases.js", () => ({
68-
modelsAliasesAddCommand: mocks.noopAsync,
69-
modelsAliasesListCommand: mocks.noopAsync,
70-
modelsAliasesRemoveCommand: mocks.noopAsync,
76+
modelsAliasesAddCommand: mocks.modelsAliasesAddCommand,
77+
modelsAliasesListCommand: mocks.modelsAliasesListCommand,
78+
modelsAliasesRemoveCommand: mocks.modelsAliasesRemoveCommand,
7179
}));
7280
vi.mock("../commands/models/fallbacks.js", () => ({
7381
modelsFallbacksAddCommand: mocks.noopAsync,
@@ -82,7 +90,7 @@ vi.mock("../commands/models/image-fallbacks.js", () => ({
8290
modelsImageFallbacksRemoveCommand: mocks.noopAsync,
8391
}));
8492
vi.mock("../commands/models/scan.js", () => ({
85-
modelsScanCommand: mocks.noopAsync,
93+
modelsScanCommand: mocks.modelsScanCommand,
8694
}));
8795
vi.mock("../commands/models/set.js", () => ({
8896
modelsSetCommand: mocks.modelsSetCommand,
@@ -94,6 +102,10 @@ vi.mock("../commands/models/set-image.js", () => ({
94102
describe("models cli", () => {
95103
beforeEach(() => {
96104
mocks.modelsListCommand.mockClear();
105+
modelsAliasesAddCommand.mockClear();
106+
modelsAliasesListCommand.mockClear();
107+
modelsAliasesRemoveCommand.mockClear();
108+
modelsScanCommand.mockClear();
97109
modelsAuthAddCommand.mockClear();
98110
modelsAuthListCommand.mockClear();
99111
modelsAuthLoginCommand.mockClear();
@@ -488,12 +500,49 @@ describe("models cli", () => {
488500
args: ["models", "--agent", "poe", "set-image", "openai/gpt-image-1"],
489501
command: modelsSetImageCommand,
490502
},
503+
{
504+
label: "aliases list",
505+
args: ["models", "--agent", "poe", "aliases", "list"],
506+
command: modelsAliasesListCommand,
507+
},
508+
{
509+
label: "aliases add",
510+
args: ["models", "--agent", "poe", "aliases", "add", "zzz", "soraka/grok-4.6"],
511+
command: modelsAliasesAddCommand,
512+
},
513+
{
514+
label: "aliases remove",
515+
args: ["models", "--agent", "poe", "aliases", "remove", "zzz"],
516+
command: modelsAliasesRemoveCommand,
517+
},
518+
{
519+
label: "scan",
520+
args: ["models", "--agent", "poe", "scan", "--no-probe", "--no-input"],
521+
command: modelsScanCommand,
522+
},
491523
])("rejects parent --agent for models $label", async ({ args, command }) => {
492524
await expect(runModelsCommand(args)).rejects.toThrow("does not support --agent");
493525

494526
expect(command).not.toHaveBeenCalled();
495527
});
496528

529+
it.each([
530+
{
531+
label: "aliases list",
532+
args: ["models", "aliases", "list"],
533+
command: modelsAliasesListCommand,
534+
},
535+
{
536+
label: "scan",
537+
args: ["models", "scan", "--no-probe", "--no-input"],
538+
command: modelsScanCommand,
539+
},
540+
])("still runs models $label without --agent", async ({ args, command }) => {
541+
await runModelsCommand(args);
542+
543+
expect(command).toHaveBeenCalled();
544+
});
545+
497546
it("shows help for models auth without error exit", async () => {
498547
const program = new Command();
499548
program.exitOverride();

‎src/cli/models-cli.ts‎

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export function registerModelsCli(program: Command) {
142142
.argument("<model>", "Model id or alias")
143143
.action(async (model: string, _opts: unknown, command: Command) => {
144144
const runtime = await loadModelsRuntime();
145-
runtime.rejectAgentScopedModelWrite(command, "set");
145+
runtime.rejectAgentScopedModelCommand(command, "set");
146146
await runtime.runModelsCommand(async () => {
147147
const { modelsSetCommand } = await import("../commands/models/set.js");
148148
await modelsSetCommand(model, runtime.defaultRuntime);
@@ -155,7 +155,7 @@ export function registerModelsCli(program: Command) {
155155
.argument("<model>", "Model id or alias")
156156
.action(async (model: string, _opts: unknown, command: Command) => {
157157
const runtime = await loadModelsRuntime();
158-
runtime.rejectAgentScopedModelWrite(command, "set-image");
158+
runtime.rejectAgentScopedModelCommand(command, "set-image");
159159
await runtime.runModelsCommand(async () => {
160160
const { modelsSetImageCommand } = await import("../commands/models/set-image.js");
161161
await modelsSetImageCommand(model, runtime.defaultRuntime);
@@ -169,10 +169,15 @@ export function registerModelsCli(program: Command) {
169169
.description("List model aliases")
170170
.option("--json", "Output JSON", false)
171171
.option("--plain", "Plain output", false)
172-
.action(async (opts) => {
173-
await withModelsRuntime(async ({ defaultRuntime }) => {
172+
.action(async (opts, command: Command) => {
173+
const runtime = await loadModelsRuntime();
174+
runtime.rejectAgentScopedModelCommand(command, "aliases list");
175+
await runtime.runModelsCommand(async () => {
174176
const { modelsAliasesListCommand } = await loadModelsAliasesCommands();
175-
await modelsAliasesListCommand({ ...opts, json: hasJsonOutput(opts) }, defaultRuntime);
177+
await modelsAliasesListCommand(
178+
{ ...opts, json: hasJsonOutput(opts) },
179+
runtime.defaultRuntime,
180+
);
176181
});
177182
});
178183

@@ -181,21 +186,25 @@ export function registerModelsCli(program: Command) {
181186
.description("Add or update a model alias")
182187
.argument("<alias>", "Alias name")
183188
.argument("<model>", "Model id or alias")
184-
.action(async (alias: string, model: string) => {
185-
await withModelsRuntime(async ({ defaultRuntime }) => {
189+
.action(async (alias: string, model: string, _opts: unknown, command: Command) => {
190+
const runtime = await loadModelsRuntime();
191+
runtime.rejectAgentScopedModelCommand(command, "aliases add");
192+
await runtime.runModelsCommand(async () => {
186193
const { modelsAliasesAddCommand } = await loadModelsAliasesCommands();
187-
await modelsAliasesAddCommand(alias, model, defaultRuntime);
194+
await modelsAliasesAddCommand(alias, model, runtime.defaultRuntime);
188195
});
189196
});
190197

191198
aliases
192199
.command("remove")
193200
.description("Remove a model alias")
194201
.argument("<alias>", "Alias name")
195-
.action(async (alias: string) => {
196-
await withModelsRuntime(async ({ defaultRuntime }) => {
202+
.action(async (alias: string, _opts: unknown, command: Command) => {
203+
const runtime = await loadModelsRuntime();
204+
runtime.rejectAgentScopedModelCommand(command, "aliases remove");
205+
await runtime.runModelsCommand(async () => {
197206
const { modelsAliasesRemoveCommand } = await loadModelsAliasesCommands();
198-
await modelsAliasesRemoveCommand(alias, defaultRuntime);
207+
await modelsAliasesRemoveCommand(alias, runtime.defaultRuntime);
199208
});
200209
});
201210

@@ -286,10 +295,12 @@ export function registerModelsCli(program: Command) {
286295
.option("--set-default", "Set agents.defaults.model to the first selection", false)
287296
.option("--set-image", "Set agents.defaults.imageModel to the first image selection", false)
288297
.option("--json", "Output JSON", false)
289-
.action(async (opts) => {
290-
await withModelsRuntime(async ({ defaultRuntime }) => {
298+
.action(async (opts, command: Command) => {
299+
const runtime = await loadModelsRuntime();
300+
runtime.rejectAgentScopedModelCommand(command, "scan");
301+
await runtime.runModelsCommand(async () => {
291302
const { modelsScanCommand } = await import("../commands/models/scan.js");
292-
await modelsScanCommand({ ...opts, json: hasJsonOutput(opts) }, defaultRuntime);
303+
await modelsScanCommand({ ...opts, json: hasJsonOutput(opts) }, runtime.defaultRuntime);
293304
});
294305
});
295306

0 commit comments

Comments
 (0)