Skip to content

Commit 2f5af3e

Browse files
committed
fix(cli): resolve alias targets from write snapshot
1 parent 93995a0 commit 2f5af3e

3 files changed

Lines changed: 36 additions & 64 deletions

File tree

‎src/commands/models/aliases.test.ts‎

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -380,15 +380,19 @@ describe("modelsAliasesAddCommand", () => {
380380
expect(written.agents?.defaults?.models?.["openai/gpt-5.4-mini"]?.alias).toBe("fast");
381381
});
382382

383-
it("attaches the alias to the target named by the CAS-fenced snapshot, not a pre-command read", async () => {
384-
// The config loaded at command start maps `target` to one model; by the time
385-
// the write snapshot is taken another writer has remapped it. The write is
386-
// fenced by the snapshot hash, so the alias must follow the snapshot's target.
383+
it("resolves alias targets from the CAS-fenced snapshot", async () => {
387384
const staleCfg = {
388-
agents: { defaults: { models: { "openai/gpt-5.6-sol": { alias: "target" } } } },
385+
agents: { defaults: { models: { "openai/gpt-5.6-sol": { alias: "old-alias" } } } },
389386
} as unknown as OpenClawConfig;
390387
const currentCfg = {
391-
agents: { defaults: { models: { "anthropic/claude-opus-5": { alias: "target" } } } },
388+
agents: {
389+
defaults: {
390+
models: {
391+
"openai/gpt-5.6-sol": { params: { temperature: 0.2 } },
392+
"anthropic/claude-opus-5": { alias: "old-alias" },
393+
},
394+
},
395+
},
392396
} as unknown as OpenClawConfig;
393397
mocks.loadModelsConfig.mockResolvedValue(staleCfg);
394398
mocks.readConfigFileSnapshot.mockResolvedValue({
@@ -398,47 +402,24 @@ describe("modelsAliasesAddCommand", () => {
398402
mocks.replaceConfigFile.mockResolvedValue(undefined);
399403
const runtime = makeRuntime();
400404

401-
await modelsAliasesAddCommand("new-name", "target", runtime);
405+
await modelsAliasesAddCommand("new-alias", "old-alias", runtime);
402406

403-
expect(mocks.replaceConfigFile).toHaveBeenCalledOnce();
404407
const [replaceParams] = mocks.replaceConfigFile.mock.calls[0] ?? [];
405408
expect(replaceParams?.baseHash).toBe("current-hash");
406409
expect(replaceParams?.nextConfig.agents?.defaults?.models).toEqual({
407-
"anthropic/claude-opus-5": { alias: "new-name" },
410+
"openai/gpt-5.6-sol": { params: { temperature: 0.2 } },
411+
"anthropic/claude-opus-5": { alias: "new-alias" },
408412
});
409-
expect(runtime.logs).toContain("Alias new-name -> anthropic/claude-opus-5");
413+
expect(runtime.logs).toContain("Alias new-alias -> anthropic/claude-opus-5");
410414
});
411415

412-
it.each([
413-
{
414-
label: "resolves a runtime-only alias while writing only source config",
415-
sourceConfig: {
416-
agents: { defaults: { models: { "anthropic/claude-sonnet-4-6": {} } } },
417-
},
418-
runtimeConfig: {
419-
agents: { defaults: { models: { "anthropic/claude-sonnet-4-6": { alias: "sonnet" } } } },
420-
},
421-
expectedModels: { "anthropic/claude-sonnet-4-6": { alias: "fast" } },
422-
},
423-
{
424-
label: "keeps authored aliases ahead of runtime-only aliases",
425-
sourceConfig: {
426-
agents: { defaults: { models: { "openai/gpt-5.5": { alias: "sonnet" } } } },
427-
},
428-
runtimeConfig: {
429-
agents: {
430-
defaults: {
431-
models: {
432-
"openai/gpt-5.5": { alias: "sonnet" },
433-
"anthropic/claude-sonnet-4-6": { alias: "sonnet" },
434-
},
435-
},
436-
},
437-
},
438-
expectedModels: { "openai/gpt-5.5": { alias: "fast" } },
439-
},
440-
])("$label (parity with models set)", async ({ sourceConfig, runtimeConfig, expectedModels }) => {
441-
mocks.loadModelsConfig.mockResolvedValue(runtimeConfig);
416+
it("resolves a runtime-only alias while persisting only source config", async () => {
417+
const sourceConfig = {
418+
agents: { defaults: { models: { "anthropic/claude-sonnet-4-6": {} } } },
419+
};
420+
const runtimeConfig = {
421+
agents: { defaults: { models: { "anthropic/claude-sonnet-4-6": { alias: "sonnet" } } } },
422+
};
442423
mocks.readConfigFileSnapshot.mockResolvedValue({
443424
...snapshot(sourceConfig as unknown as OpenClawConfig),
444425
runtimeConfig,
@@ -448,7 +429,9 @@ describe("modelsAliasesAddCommand", () => {
448429
await modelsAliasesAddCommand("fast", "sonnet", makeRuntime());
449430

450431
const [replaceParams] = mocks.replaceConfigFile.mock.calls[0] ?? [];
451-
expect(replaceParams?.nextConfig.agents?.defaults?.models).toEqual(expectedModels);
432+
expect(replaceParams?.nextConfig.agents?.defaults?.models).toEqual({
433+
"anthropic/claude-sonnet-4-6": { alias: "fast" },
434+
});
452435
});
453436
});
454437

‎src/commands/models/aliases.ts‎

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { normalizeAgentModelMapForConfig } from "../../config/model-input.js";
66
import { type RuntimeEnv, writeRuntimeJson, writeRuntimeStdout } from "../../runtime.js";
77
import { normalizeAlias } from "./alias-name.js";
88
import { loadModelsConfig } from "./load-config.js";
9-
import { ensureFlagCompatibility, resolveModelTargetForWrite, updateConfig } from "./shared.js";
9+
import { ensureFlagCompatibility, resolveModelTarget, updateConfig } from "./shared.js";
1010

1111
/** Lists configured model aliases as JSON, plain pairs, or human-readable rows. */
1212
export async function modelsAliasesListCommand(
@@ -55,18 +55,13 @@ export async function modelsAliasesAddCommand(
5555
) {
5656
const alias = normalizeAlias(aliasRaw);
5757
const normalizedAlias = alias.toLowerCase();
58-
let target: string | undefined;
59-
await updateConfig((cfg, context) => {
60-
// `modelRaw` may itself be an alias; resolve it from the fenced snapshot so a
61-
// concurrent remap cannot attach the alias to a stale target under a green CAS.
62-
const resolved = resolveModelTargetForWrite({
63-
cfg,
64-
resolveCfg: context.runtimeConfig,
65-
modelRaw,
66-
});
58+
let target = modelRaw;
59+
await updateConfig((cfgLocal, context) => {
60+
// Alias resolution must share the snapshot whose hash fences this write.
61+
const resolved = resolveModelTarget({ raw: modelRaw, cfg: context.runtimeConfig });
6762
const modelKey = `${resolved.provider}/${resolved.model}`;
6863
target = modelKey;
69-
const nextModels = { ...cfg.agents?.defaults?.models };
64+
const nextModels = { ...cfgLocal.agents?.defaults?.models };
7065
// Model selection folds alias case, so case variants must not collide.
7166
for (const [key, entry] of Object.entries(nextModels)) {
7267
const existing = entry?.alias?.trim();
@@ -77,11 +72,11 @@ export async function modelsAliasesAddCommand(
7772
const existing = nextModels[modelKey] ?? {};
7873
nextModels[modelKey] = { ...existing, alias };
7974
return {
80-
...cfg,
75+
...cfgLocal,
8176
agents: {
82-
...cfg.agents,
77+
...cfgLocal.agents,
8378
defaults: {
84-
...cfg.agents?.defaults,
79+
...cfgLocal.agents?.defaults,
8580
models: nextModels,
8681
},
8782
},

‎src/commands/models/shared.ts‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ export function applyDefaultModelPrimaryUpdate(params: {
247247
field: "model" | "imageModel";
248248
resolvedTarget?: { provider: string; model: string };
249249
}): OpenClawConfig {
250-
const resolved = params.resolvedTarget ?? resolveModelTargetForWrite(params);
250+
const resolved = params.resolvedTarget ?? resolveDefaultModelPrimaryTarget(params);
251251
const nextModels = {
252252
...params.cfg.agents?.defaults?.models,
253253
} as Record<string, AgentModelEntryConfig>;
@@ -271,13 +271,7 @@ export function applyDefaultModelPrimaryUpdate(params: {
271271
};
272272
}
273273

274-
/**
275-
* Resolves a CLI model ref for a config write. Authored aliases in the source
276-
* config win; otherwise the runtime config resolves built-in aliases that only
277-
* exist after defaults are applied. Call from inside `updateConfig` so the
278-
* target comes from the same snapshot whose hash fences the write.
279-
*/
280-
export function resolveModelTargetForWrite(params: {
274+
function resolveDefaultModelPrimaryTarget(params: {
281275
cfg: OpenClawConfig;
282276
resolveCfg?: OpenClawConfig;
283277
modelRaw: string;
@@ -295,7 +289,7 @@ export async function updateDefaultModelPrimaryConfig(params: {
295289
}): Promise<{ updated: OpenClawConfig; warning?: string }> {
296290
let warning: string | undefined;
297291
const updated = await updateConfig((cfg, context) => {
298-
const resolvedTarget = resolveModelTargetForWrite({
292+
const resolvedTarget = resolveDefaultModelPrimaryTarget({
299293
cfg,
300294
resolveCfg: context.runtimeConfig,
301295
modelRaw: params.modelRaw,

0 commit comments

Comments
 (0)