Skip to content

Commit 7145e31

Browse files
committed
fix(cli): preserve list-form agent rosters
1 parent 81557e2 commit 7145e31

3 files changed

Lines changed: 134 additions & 36 deletions

File tree

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,66 @@ describe("onboarding agent target", () => {
127127
expect(updated.agents?.entries?.ops).toBeUndefined();
128128
});
129129

130+
it("preserves every list-form agent when applying the primary model", () => {
131+
const config = {
132+
agents: {
133+
ownership: "explicit" as const,
134+
list: [
135+
{ id: "main", name: "Main", model: { primary: "openai/main" } },
136+
{
137+
id: "OPS",
138+
name: "Operations",
139+
model: { primary: "openai/old", fallbacks: ["openai/fallback"] },
140+
models: { "openai/old": { alias: "Old" } },
141+
},
142+
],
143+
},
144+
};
145+
const target = resolveOnboardingAgentTarget(config, "ops");
146+
147+
const updated = applyOnboardingPrimaryModel(config, target, "openai/new");
148+
149+
expect(updated.agents?.list).toBeUndefined();
150+
expect(updated.agents?.entries).toEqual({
151+
main: { name: "Main", model: { primary: "openai/main" } },
152+
OPS: {
153+
name: "Operations",
154+
model: { primary: "openai/new", fallbacks: ["openai/fallback"] },
155+
models: { "openai/old": { alias: "Old" }, "openai/new": {} },
156+
},
157+
});
158+
});
159+
160+
it("preserves every list-form agent when projecting model policy", () => {
161+
const config = {
162+
agents: {
163+
ownership: "explicit" as const,
164+
list: [
165+
{ id: "main", modelPolicy: { allow: ["openai/main"] } },
166+
{ id: "ops", modelPolicy: { allow: ["openai/old"] } },
167+
],
168+
},
169+
};
170+
const target = resolveOnboardingAgentTarget(config, "ops");
171+
172+
const updated = applyAgentModelDefaults(config, target, (projected) => ({
173+
...projected,
174+
agents: {
175+
...projected.agents,
176+
defaults: {
177+
...projected.agents?.defaults,
178+
modelPolicy: { allow: ["openai/new"] },
179+
},
180+
},
181+
}));
182+
183+
expect(updated.agents?.list).toBeUndefined();
184+
expect(updated.agents?.entries).toEqual({
185+
main: { modelPolicy: { allow: ["openai/main"] } },
186+
ops: { modelPolicy: { allow: ["openai/new"] } },
187+
});
188+
});
189+
130190
it("provisions the configured default agent workspace and sessions", async () => {
131191
const stateDir = tempDirs.make("openclaw-onboard-target-");
132192
const globalWorkspace = path.join(stateDir, "global-workspace");

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

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// Resolves one concrete agent owner for onboarding auth, model, workspace, and session effects.
22
import {
3+
listAgentEntries,
34
resolveAgentDir,
45
resolveAgentWorkspaceDir,
56
resolveMutableAgentEntry,
67
resolveSoleAgentId,
8+
toAgentEntriesRecord,
79
} from "../agents/agent-scope-config.js";
810
import { tryResolveLegacyCompatibilityAgentId } from "../config/legacy.default-agent-owner.js";
911
import {
@@ -12,6 +14,7 @@ import {
1214
resolveAgentModelFallbackValues,
1315
} from "../config/model-input.js";
1416
import type { OptionalBootstrapFileName } from "../config/types.agent-defaults.js";
17+
import type { AgentEntryConfig } from "../config/types.agents.js";
1518
import type { OpenClawConfig } from "../config/types.openclaw.js";
1619
import { formatErrorMessage } from "../infra/errors.js";
1720
import { applyPrimaryModel } from "../plugins/provider-model-primary.js";
@@ -66,15 +69,37 @@ export async function ensureOnboardingAgentWorkspace(
6669
}
6770
}
6871

72+
function replaceOnboardingAgentEntry(
73+
config: OpenClawConfig,
74+
updated: OpenClawConfig,
75+
target: OnboardingAgentTarget,
76+
nextEntry: AgentEntryConfig,
77+
): OpenClawConfig {
78+
const entries = listAgentEntries(config);
79+
const index = entries.findIndex((entry) => normalizeAgentId(entry.id) === target.agentId);
80+
const nextEntries = [...entries];
81+
const replacement = { id: index >= 0 ? entries[index]!.id : target.agentId, ...nextEntry };
82+
if (index >= 0) {
83+
nextEntries[index] = replacement;
84+
} else {
85+
nextEntries.push(replacement);
86+
}
87+
const { list: _list, entries: _entries, ...agents } = config.agents ?? {};
88+
return {
89+
...updated,
90+
agents: {
91+
...agents,
92+
entries: toAgentEntriesRecord(nextEntries),
93+
},
94+
};
95+
}
96+
6997
export function applyOnboardingPrimaryModel(
7098
config: OpenClawConfig,
7199
target: OnboardingAgentTarget,
72100
model: string,
73101
): OpenClawConfig {
74-
const authoredEntryKey = Object.keys(config.agents?.entries ?? {}).find(
75-
(key) => normalizeAgentId(key) === target.agentId,
76-
);
77-
const entry = authoredEntryKey ? config.agents?.entries?.[authoredEntryKey] : undefined;
102+
const entry = resolveMutableAgentEntry(config, target.agentId);
78103
if (entry?.model === undefined && config.agents?.ownership !== "explicit") {
79104
return applyPrimaryModel(config, model);
80105
}
@@ -84,26 +109,17 @@ export function applyOnboardingPrimaryModel(
84109
normalizeAgentModelRefForConfig(fallback),
85110
);
86111
const models = normalizeAgentModelMapForConfig(entry?.models ?? {});
87-
return {
88-
...config,
89-
agents: {
90-
...config.agents,
91-
entries: {
92-
...config.agents?.entries,
93-
[authoredEntryKey ?? target.agentId]: {
94-
...entry,
95-
model: {
96-
...(fallbackValues.length > 0 ? { fallbacks: fallbackValues } : {}),
97-
primary,
98-
},
99-
models: {
100-
...models,
101-
[primary]: models[primary] ?? {},
102-
},
103-
},
104-
},
112+
return replaceOnboardingAgentEntry(config, config, target, {
113+
...entry,
114+
model: {
115+
...(fallbackValues.length > 0 ? { fallbacks: fallbackValues } : {}),
116+
primary,
105117
},
106-
};
118+
models: {
119+
...models,
120+
[primary]: models[primary] ?? {},
121+
},
122+
});
107123
}
108124

109125
/** Apply a model-default mutation to one agent without flattening it globally. */
@@ -140,9 +156,6 @@ export function projectAgentModelDefaults(
140156
}
141157
const updatedDefaults = updated.agents?.defaults;
142158
const { model: _model, models: _models, modelPolicy: _modelPolicy, ...entryRest } = entry ?? {};
143-
const authoredEntryKey = Object.keys(config.agents?.entries ?? {}).find(
144-
(key) => normalizeAgentId(key) === target.agentId,
145-
);
146159
const nextEntry = {
147160
...entryRest,
148161
...(updatedDefaults?.model !== undefined ? { model: updatedDefaults.model } : {}),
@@ -151,14 +164,5 @@ export function projectAgentModelDefaults(
151164
? { modelPolicy: updatedDefaults.modelPolicy }
152165
: {}),
153166
};
154-
return {
155-
...updated,
156-
agents: {
157-
...config.agents,
158-
entries: {
159-
...config.agents?.entries,
160-
[authoredEntryKey ?? target.agentId]: nextEntry,
161-
},
162-
},
163-
};
167+
return replaceOnboardingAgentEntry(config, updated, target, nextEntry);
164168
}

‎src/commands/onboard-custom-config.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,40 @@ it("keeps explicit custom-provider model state on the authored agent entry", ()
7676
]);
7777
});
7878

79+
it("preserves a list-form roster when applying custom-provider model state", () => {
80+
const result = applyCustomApiConfig({
81+
config: {
82+
agents: {
83+
ownership: "explicit",
84+
defaults: { systemAgent: { agentId: "ops" } },
85+
list: [
86+
{ id: "main", name: "Main" },
87+
{ id: "ops", name: "Operations" },
88+
],
89+
},
90+
},
91+
baseUrl: "https://llm.example.com/v1",
92+
modelId: "foo-large",
93+
compatibility: "openai",
94+
providerId: "custom",
95+
alias: "Custom",
96+
target: { agentId: "ops", agentDir: "/tmp/ops-agent", workspaceDir: "/tmp/ops-workspace" },
97+
});
98+
99+
expect(result.config.agents?.list).toBeUndefined();
100+
expect(result.config.agents?.entries).toEqual({
101+
main: { name: "Main" },
102+
ops: {
103+
name: "Operations",
104+
model: { primary: "custom/foo-large" },
105+
models: { "custom/foo-large": { alias: "Custom" } },
106+
},
107+
});
108+
expect(result.config.models?.providers?.custom?.models?.map((model) => model.id)).toEqual([
109+
"foo-large",
110+
]);
111+
});
112+
79113
it("uses expanded max_tokens for openai verification probes", () => {
80114
const request = buildOpenAiVerificationProbeRequest({
81115
baseUrl: "https://example.com/v1",

0 commit comments

Comments
 (0)