Skip to content

Commit d7ab20a

Browse files
steipetePeter Steinberger
andauthored
perf(system-agent): reuse setup metadata per activation (#118352)
* perf(system-agent): reuse setup metadata per activation * chore: remove release-owned changelog entry --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent e4884d9 commit d7ab20a

8 files changed

Lines changed: 161 additions & 53 deletions

‎src/agents/model-runtime-aliases.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,26 @@ describe("resolveCliRuntimeExecutionProvider", () => {
103103
).toBe("claude-cli");
104104
});
105105

106+
it("uses caller-provided plugin auth aliases without metadata discovery", () => {
107+
expect(
108+
resolveCliRuntimeExecutionProvider({
109+
authProfileId: "anthropic:claude-cli",
110+
cfg: createAnthropicAuthConfig({ order: ["anthropic:api"] }),
111+
provider: "anthropic",
112+
modelId: "opus-4.7",
113+
metadataSnapshot: {
114+
plugins: [
115+
{
116+
id: "anthropic",
117+
origin: "bundled",
118+
providerAuthAliases: { "claude-cli": "anthropic" },
119+
},
120+
],
121+
} as never,
122+
}),
123+
).toBe("claude-cli");
124+
});
125+
106126
it("does not override an explicit OpenClaw model-runtime policy with CLI auth", () => {
107127
// Runtime policy is more explicit than profile order, so CLI auth cannot
108128
// force a model onto the CLI harness when config says OpenClaw.

‎src/agents/model-runtime-aliases.ts‎

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import {
1313
resolveCliRuntimeModelBackendBinding,
1414
} from "./cli-backends.js";
1515
import { resolveModelRuntimePolicy } from "./model-runtime-policy.js";
16-
import { resolveProviderIdForAuth } from "./provider-auth-aliases.js";
16+
import {
17+
resolveProviderIdForAuth,
18+
type ProviderAuthAliasLookupParams,
19+
} from "./provider-auth-aliases.js";
1720

1821
const RETIRED_MODEL_PICKER_PROVIDERS = new Set(["codex", "codex-cli"]);
1922

@@ -169,11 +172,24 @@ function resolveConfiguredRuntime(params: {
169172
};
170173
}
171174

172-
function resolveProfileRuntimeAlias(params: {
175+
type RuntimeAuthAliasParams = {
173176
cfg?: OpenClawConfig;
174-
provider: string;
175-
profileId: string;
176-
}): string | undefined {
177+
metadataSnapshot?: ProviderAuthAliasLookupParams["metadataSnapshot"];
178+
};
179+
180+
function resolveRuntimeAuthProvider(provider: string, params: RuntimeAuthAliasParams): string {
181+
return resolveProviderIdForAuth(provider, {
182+
config: params.cfg,
183+
...(params.metadataSnapshot ? { metadataSnapshot: params.metadataSnapshot } : {}),
184+
});
185+
}
186+
187+
function resolveProfileRuntimeAlias(
188+
params: RuntimeAuthAliasParams & {
189+
provider: string;
190+
profileId: string;
191+
},
192+
): string | undefined {
177193
const profile = params.cfg?.auth?.profiles?.[params.profileId];
178194
if (!profile?.provider) {
179195
return undefined;
@@ -183,8 +199,8 @@ function resolveProfileRuntimeAlias(params: {
183199
if (!provider || !profileProvider) {
184200
return undefined;
185201
}
186-
const providerAuthKey = resolveProviderIdForAuth(provider, { config: params.cfg });
187-
const profileAuthKey = resolveProviderIdForAuth(profileProvider, { config: params.cfg });
202+
const providerAuthKey = resolveRuntimeAuthProvider(provider, params);
203+
const profileAuthKey = resolveRuntimeAuthProvider(profileProvider, params);
188204
if (providerAuthKey !== profileAuthKey) {
189205
return undefined;
190206
}
@@ -198,24 +214,25 @@ function resolveProfileRuntimeAlias(params: {
198214
})?.runtime;
199215
}
200216

201-
function resolveCliRuntimeFromAuthProfile(params: {
202-
cfg?: OpenClawConfig;
203-
provider: string;
204-
authProfileId?: string;
205-
}): string | undefined {
217+
function resolveCliRuntimeFromAuthProfile(
218+
params: RuntimeAuthAliasParams & {
219+
provider: string;
220+
authProfileId?: string;
221+
},
222+
): string | undefined {
206223
if (!params.cfg?.auth?.profiles) {
207224
return undefined;
208225
}
209226
if (params.authProfileId?.trim()) {
210227
return resolveProfileRuntimeAlias({
211-
cfg: params.cfg,
228+
...params,
212229
provider: params.provider,
213230
profileId: params.authProfileId.trim(),
214231
});
215232
}
216233

217234
const provider = normalizeProviderId(params.provider);
218-
const providerAuthKey = resolveProviderIdForAuth(provider, { config: params.cfg });
235+
const providerAuthKey = resolveRuntimeAuthProvider(provider, params);
219236
const orderedProfileIds = [
220237
...(params.cfg.auth.order?.[providerAuthKey] ?? []),
221238
...(providerAuthKey === provider ? [] : (params.cfg.auth.order?.[provider] ?? [])),
@@ -225,37 +242,46 @@ function resolveCliRuntimeFromAuthProfile(params: {
225242
if (!profile?.provider) {
226243
continue;
227244
}
228-
const profileAuthKey = resolveProviderIdForAuth(profile.provider, { config: params.cfg });
245+
const profileAuthKey = resolveRuntimeAuthProvider(profile.provider, params);
229246
if (profileAuthKey !== providerAuthKey) {
230247
continue;
231248
}
232-
return resolveProfileRuntimeAlias({ cfg: params.cfg, provider, profileId });
249+
return resolveProfileRuntimeAlias({
250+
...params,
251+
provider,
252+
profileId,
253+
});
233254
}
234255

235256
const compatibleProfileIds = Object.entries(params.cfg.auth.profiles)
236257
.filter(([, profile]) => {
237258
if (!profile?.provider) {
238259
return false;
239260
}
240-
return resolveProviderIdForAuth(profile.provider, { config: params.cfg }) === providerAuthKey;
261+
return resolveRuntimeAuthProvider(profile.provider, params) === providerAuthKey;
241262
})
242263
.map(([profileId]) => profileId);
243264
if (compatibleProfileIds.length !== 1) {
244265
return undefined;
245266
}
246267
const [profileId] = compatibleProfileIds;
247268
return profileId
248-
? resolveProfileRuntimeAlias({ cfg: params.cfg, provider, profileId })
269+
? resolveProfileRuntimeAlias({
270+
...params,
271+
provider,
272+
profileId,
273+
})
249274
: undefined;
250275
}
251276

252-
export function resolveCliRuntimeExecutionProvider(params: {
253-
provider: string;
254-
cfg?: OpenClawConfig;
255-
agentId?: string;
256-
modelId?: string;
257-
authProfileId?: string;
258-
}): string | undefined {
277+
export function resolveCliRuntimeExecutionProvider(
278+
params: RuntimeAuthAliasParams & {
279+
provider: string;
280+
agentId?: string;
281+
modelId?: string;
282+
authProfileId?: string;
283+
},
284+
): string | undefined {
259285
const provider = normalizeProviderId(params.provider);
260286
const { runtime, matchedProvider } = resolveConfiguredRuntime({ ...params, provider });
261287
if (runtime === "openclaw") {

‎src/agents/simple-completion-runtime.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
buildModelAliasIndex,
4545
resolveDefaultModelForAgent,
4646
resolveModelRefFromString,
47+
type ModelManifestNormalizationContext,
4748
} from "./model-selection.js";
4849
import { resolveOpenAIModelRoutes, selectOpenAIModelRouteAuth } from "./openai-model-routes.js";
4950
import { OPENAI_PROVIDER_ID, isOpenAIProvider } from "./openai-routing.js";
@@ -117,10 +118,12 @@ export function resolveSimpleCompletionSelectionForAgent(params: {
117118
agentDir?: string;
118119
modelRef?: string;
119120
useUtilityModel?: boolean;
121+
manifestPlugins?: ModelManifestNormalizationContext["manifestPlugins"];
120122
}): AgentSimpleCompletionSelection | null {
121123
const fallbackRef = resolveDefaultModelForAgent({
122124
cfg: params.cfg,
123125
agentId: params.agentId,
126+
manifestPlugins: params.manifestPlugins,
124127
});
125128
// Utility routing derives a provider-declared small model when unset and
126129
// treats an explicit empty utilityModel as "use the primary" (disabled).
@@ -138,12 +141,14 @@ export function resolveSimpleCompletionSelectionForAgent(params: {
138141
const aliasIndex = buildModelAliasIndex({
139142
cfg: params.cfg,
140143
defaultProvider: fallbackRef.provider || DEFAULT_PROVIDER,
144+
manifestPlugins: params.manifestPlugins,
141145
});
142146
const resolved = split
143147
? resolveModelRefFromString({
144148
raw: split.model,
145149
defaultProvider: fallbackRef.provider || DEFAULT_PROVIDER,
146150
aliasIndex,
151+
manifestPlugins: params.manifestPlugins,
147152
})
148153
: null;
149154
const provider = resolved?.ref.provider ?? fallbackRef.provider;

‎src/system-agent/inference-route.ts‎

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
resolveCliExecutionAuthProfileId,
1313
} from "../agents/cli-execution-auth.js";
1414
import type { OpenClawConfig } from "../config/types.openclaw.js";
15+
import type { PluginMetadataSnapshot } from "../plugins/plugin-metadata-snapshot.types.js";
1516
import { normalizeAgentId } from "../routing/session-key.js";
1617

1718
export type SystemAgentConfiguredRoute = {
@@ -46,7 +47,12 @@ export function resolveSystemAgentTargetAgentId(
4647
export type SystemAgentConfiguredRouteDeps = {
4748
readConfigFileSnapshot?: typeof import("../config/config.js").readConfigFileSnapshot;
4849
loadAuthProfileStoreForRuntime?: typeof import("../agents/auth-profiles/store.js").loadAuthProfileStoreForRuntime;
50+
pluginMetadataPlugins?: PluginMetadataSnapshot["plugins"];
4951
};
52+
type SystemAgentRouteProjectionDeps = Pick<
53+
SystemAgentConfiguredRouteDeps,
54+
"loadAuthProfileStoreForRuntime" | "pluginMetadataPlugins"
55+
>;
5056

5157
type DistributiveOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never;
5258

@@ -114,7 +120,7 @@ function projectSystemAgentExecutionConfig(
114120
export async function resolveSystemAgentConfiguredRouteFromConfig(
115121
runConfig: OpenClawConfig,
116122
requestedAgentId?: string,
117-
deps: Pick<SystemAgentConfiguredRouteDeps, "loadAuthProfileStoreForRuntime"> = {},
123+
deps: SystemAgentRouteProjectionDeps = {},
118124
): Promise<SystemAgentConfiguredRoute | null> {
119125
const [agentScope, modelSelection, modelRuntimeAliases, simpleCompletion, harnessPolicy] =
120126
await Promise.all([
@@ -131,16 +137,21 @@ export async function resolveSystemAgentConfiguredRouteFromConfig(
131137
const selection = simpleCompletion.resolveSimpleCompletionSelectionForAgent({
132138
cfg: runConfig,
133139
agentId: modelOwnerAgentId,
140+
manifestPlugins: deps.pluginMetadataPlugins,
134141
});
135142
if (!selection) {
136143
return null;
137144
}
145+
const metadataSnapshot = deps.pluginMetadataPlugins
146+
? { plugins: deps.pluginMetadataPlugins }
147+
: undefined;
138148
const cliExecutionProvider = modelRuntimeAliases.resolveCliRuntimeExecutionProvider({
139149
provider: selection.provider,
140150
cfg: runConfig,
141151
agentId: modelOwnerAgentId,
142152
modelId: selection.modelId,
143153
...(selection.profileId ? { authProfileId: selection.profileId } : {}),
154+
...(metadataSnapshot ? { metadataSnapshot } : {}),
144155
});
145156
const executionProvider = cliExecutionProvider ?? selection.runtimeProvider ?? selection.provider;
146157
const isCliRoute = modelSelection.isCliProvider(executionProvider, runConfig);
@@ -220,15 +231,16 @@ function projectRelevantModelMap(params: {
220231
/** Project every config input that can change the configured default-agent route. */
221232
export async function projectDefaultInferenceRoute(
222233
config: OpenClawConfig,
234+
deps: SystemAgentRouteProjectionDeps = {},
223235
): Promise<DefaultInferenceRouteProjection> {
224-
return await projectInferenceRoute(config);
236+
return await projectInferenceRoute(config, undefined, deps);
225237
}
226238

227239
/** Project every config input that can change one configured agent route. */
228240
export async function projectInferenceRoute(
229241
config: OpenClawConfig,
230242
requestedAgentId?: string,
231-
deps: Pick<SystemAgentConfiguredRouteDeps, "loadAuthProfileStoreForRuntime"> = {},
243+
deps: SystemAgentRouteProjectionDeps = {},
232244
): Promise<DefaultInferenceRouteProjection> {
233245
const { resolveProviderIdForAuth } = await import("../agents/provider-auth-aliases.js");
234246
const routeAgentId = resolveSystemAgentTargetAgentId(config, requestedAgentId);
@@ -243,17 +255,24 @@ export async function projectInferenceRoute(
243255
const providerIds = new Set(
244256
[logicalProvider, normalizeProviderId(route?.provider ?? "")].filter(Boolean),
245257
);
258+
const metadataSnapshot = deps.pluginMetadataPlugins
259+
? { plugins: deps.pluginMetadataPlugins }
260+
: undefined;
261+
const authAliasParams = {
262+
config,
263+
...(metadataSnapshot ? { metadataSnapshot } : {}),
264+
};
246265
const authProviderIds = new Set(
247-
[...providerIds].map((provider) => resolveProviderIdForAuth(provider, { config })),
266+
[...providerIds].map((provider) => resolveProviderIdForAuth(provider, authAliasParams)),
248267
);
249268
const authProfiles = Object.fromEntries(
250269
Object.entries(config.auth?.profiles ?? {}).filter(([, profile]) =>
251-
authProviderIds.has(resolveProviderIdForAuth(profile.provider, { config })),
270+
authProviderIds.has(resolveProviderIdForAuth(profile.provider, authAliasParams)),
252271
),
253272
);
254273
const authOrder = Object.fromEntries(
255274
Object.entries(config.auth?.order ?? {}).filter(([provider]) =>
256-
authProviderIds.has(resolveProviderIdForAuth(provider, { config })),
275+
authProviderIds.has(resolveProviderIdForAuth(provider, authAliasParams)),
257276
),
258277
);
259278
const modelProviders = Object.fromEntries(

0 commit comments

Comments
 (0)