Skip to content

Commit 40b10eb

Browse files
authored
perf: avoid plugin cold loads in request hot paths (#109348)
* perf: avoid cold plugin discovery in hot paths * chore: remove stale harness policy re-export * fix: preserve active thinking clamps in status
1 parent bd9a996 commit 40b10eb

18 files changed

Lines changed: 424 additions & 117 deletions

‎extensions/codex/harness.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ describe("Codex agent harness supports()", () => {
1212
expect(harness.authBootstrap).toBe("harness");
1313
});
1414

15+
it("publishes provider ids for lightweight auto selection", () => {
16+
expect(harness.autoSelection?.providerIds).toEqual(["codex", "openai"]);
17+
});
18+
1519
const harness = createCodexAppServerAgentHarness({
1620
bindingStore: testCodexAppServerBindingStore,
1721
});
@@ -216,6 +220,7 @@ describe("Codex agent harness supports()", () => {
216220
});
217221
const result = narrowHarness.supports({ provider: "openai", requestedRuntime: "codex" });
218222
expect(result.supported).toBe(false);
223+
expect(narrowHarness.autoSelection?.providerIds).toEqual(["codex"]);
219224
});
220225

221226
it("exposes the fail-closed exact runtime artifact validator", async () => {

‎extensions/codex/harness.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export function createCodexAppServerAgentHarness(options: {
6262
const harness: CodexAppServerAgentHarness = {
6363
id: harnessRuntimeId,
6464
label: options?.label ?? "Codex agent harness",
65+
autoSelection: { providerIds: [...providerIds] },
6566
delegatedExecutionPluginIds: ["voice-call"],
6667
contextEngineHostCapabilities: CODEX_APP_SERVER_CONTEXT_ENGINE_HOST_CAPABILITIES,
6768
deliveryDefaults: {

‎extensions/copilot/harness.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ describe("createCopilotAgentHarness", () => {
161161
it("supports returns false in auto runtime even for github provider", () => {
162162
const harness = createCopilotAgentHarness();
163163

164+
expect(harness.autoSelection?.providerIds).toEqual([]);
165+
164166
expect(
165167
harness.supports({
166168
provider: "github-copilot",

‎extensions/copilot/harness.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@ export function createCopilotAgentHarness(
657657
return {
658658
id: options?.id ?? "copilot",
659659
label: options?.label ?? "GitHub Copilot agent runtime",
660+
autoSelection: { providerIds: [] },
660661

661662
supports(ctx) {
662663
const requestedRuntime = String(ctx.requestedRuntime ?? "")
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { normalizeProviderId } from "@openclaw/model-catalog-core/provider-id";
2+
import type { AgentHarness, AgentHarnessSupport } from "./types.js";
3+
4+
/** Returns a prepared negative auto-selection fact, or undefined when full support needs probing. */
5+
export function resolveAgentHarnessAutoSelectionHint(params: {
6+
harness: AgentHarness;
7+
provider: string;
8+
}): AgentHarnessSupport | undefined {
9+
const providerIds = params.harness.autoSelection?.providerIds;
10+
if (providerIds === undefined) {
11+
return undefined;
12+
}
13+
const provider = normalizeProviderId(params.provider);
14+
if (providerIds.some((id) => normalizeProviderId(id) === provider)) {
15+
return undefined;
16+
}
17+
return {
18+
supported: false,
19+
reason:
20+
providerIds.length === 0 ? "harness is explicit-only" : "provider is not auto-selectable",
21+
};
22+
}

‎src/agents/harness/selection.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,23 @@ describe("selectAgentHarness", () => {
10071007
expect(supports).toHaveBeenCalledTimes(1);
10081008
});
10091009

1010+
it("rejects statically unrelated auto harnesses before provider discovery", () => {
1011+
const supports = vi.fn(() => ({ supported: true as const, priority: 100 }));
1012+
registerAgentHarness({
1013+
id: "codex",
1014+
label: "Codex",
1015+
autoSelection: { providerIds: ["openai", "codex"] },
1016+
supports,
1017+
runAttempt: vi.fn(async () => createAttemptResult("codex")),
1018+
});
1019+
1020+
expect(selectAgentHarness({ provider: "deepseek", modelId: "deepseek-v4-pro" }).id).toBe(
1021+
"openclaw",
1022+
);
1023+
expect(supports).not.toHaveBeenCalled();
1024+
expect(providerOwnerMocks.resolveProviderRefOwnership).not.toHaveBeenCalled();
1025+
});
1026+
10101027
it("auto-selects the highest-priority plugin harness without duplicate support probes", () => {
10111028
const lowPrioritySupports = vi.fn(() => ({
10121029
supported: true as const,

‎src/agents/harness/selection.ts‎

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
import { resolveSandboxRuntimeStatus } from "../sandbox/runtime-status.js";
3232
import { expandToolGroups, mergeAlsoAllowPolicy, normalizeToolName } from "../tool-policy.js";
3333
import type { SystemAgentToolOptions } from "../tools/system-agent-tool.js";
34+
import { resolveAgentHarnessAutoSelectionHint } from "./auto-selection.js";
3435
import { createOpenClawAgentHarness } from "./builtin-openclaw.js";
3536
import { MissingAgentHarnessError } from "./errors.js";
3637
import { runAgentHarnessLifecycleAttempt } from "./lifecycle.js";
@@ -49,7 +50,6 @@ import type { AgentHarness, AgentHarnessSupport, AgentHarnessSupportContext } fr
4950

5051
const log = createSubsystemLogger("agents/harness");
5152
export { resolveAgentHarnessPolicy } from "./policy.js";
52-
export type { AgentHarnessPolicy };
5353

5454
type AgentHarnessAvailabilityParams = {
5555
provider?: string;
@@ -389,29 +389,35 @@ function selectAgentHarnessDecision(
389389
throw new MissingAgentHarnessError(runtime);
390390
}
391391

392-
const candidates =
393-
pluginHarnesses.length > 0
394-
? (() => {
395-
const supportContext = buildAgentHarnessSupportContext({
392+
const hintedCandidates = pluginHarnesses.map((harness) => ({
393+
harness,
394+
support: resolveAgentHarnessAutoSelectionHint({ harness, provider: params.provider }),
395+
}));
396+
const candidates = hintedCandidates.some((entry) => entry.support === undefined)
397+
? (() => {
398+
const supportContext = buildAgentHarnessSupportContext({
399+
provider: params.provider,
400+
modelId: params.modelId,
401+
modelProvider: params.modelProvider,
402+
requestedRuntime: runtime,
403+
config: params.config,
404+
agentId: params.agentId,
405+
sessionKey: params.sessionKey,
406+
preparedModelProvider: params.preparedModelProvider,
407+
providerOwnership: resolveProviderRefOwnership({
396408
provider: params.provider,
397-
modelId: params.modelId,
398-
modelProvider: params.modelProvider,
399-
requestedRuntime: runtime,
400409
config: params.config,
401-
agentId: params.agentId,
402-
sessionKey: params.sessionKey,
403-
preparedModelProvider: params.preparedModelProvider,
404-
providerOwnership: resolveProviderRefOwnership({
405-
provider: params.provider,
406-
config: params.config,
407-
}),
408-
});
409-
return pluginHarnesses.map((harness) => ({
410-
harness,
411-
support: harness.supports(supportContext),
412-
}));
413-
})()
414-
: [];
410+
}),
411+
});
412+
return hintedCandidates.map(({ harness, support }) => ({
413+
harness,
414+
support: support ?? harness.supports(supportContext),
415+
}));
416+
})()
417+
: hintedCandidates.map(({ harness, support }) => ({
418+
harness,
419+
support: support as AgentHarnessSupport,
420+
}));
415421
const supported = candidates
416422
.filter(
417423
(

‎src/agents/harness/support.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { resolveAgentIdFromSessionKey } from "../../routing/session-key.js";
1515
import { hasModelExtraParams } from "../model-extra-params.js";
1616
import { canonicalizeProviderModelId } from "../provider-model-route.js";
1717
import type { AgentRuntimeAuthPlan } from "../runtime-plan/types.js";
18+
import { resolveAgentHarnessAutoSelectionHint } from "./auto-selection.js";
1819
import { listRegisteredAgentHarnesses } from "./registry.js";
1920
import type {
2021
AgentHarness,
@@ -215,12 +216,26 @@ export function resolveAutoAgentHarnessId(params: {
215216
agentId?: string;
216217
sessionKey?: string;
217218
}): string | undefined {
219+
const registeredHarnesses = listRegisteredAgentHarnesses();
220+
if (registeredHarnesses.length === 0) {
221+
return undefined;
222+
}
223+
const candidates = registeredHarnesses.map(({ harness }) => ({
224+
harness,
225+
support: resolveAgentHarnessAutoSelectionHint({ harness, provider: params.provider }),
226+
}));
227+
if (candidates.every((entry) => entry.support !== undefined)) {
228+
return undefined;
229+
}
218230
const supportContext = buildAgentHarnessSupportContext({
219231
...params,
220232
requestedRuntime: "auto",
221233
});
222-
return listRegisteredAgentHarnesses()
223-
.map(({ harness }) => ({ harness, support: harness.supports(supportContext) }))
234+
return candidates
235+
.map(({ harness, support }) => ({
236+
harness,
237+
support: support ?? harness.supports(supportContext),
238+
}))
224239
.filter(isSupportedHarness)
225240
.toSorted(compareHarnessSupport)[0]?.harness.id;
226241
}

‎src/agents/harness/types.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ type AgentHarnessRunCapability = {
144144
id: string;
145145
label: string;
146146
pluginId?: string;
147+
/**
148+
* Exhaustive provider ids eligible for automatic selection. Omitting this hint preserves
149+
* dynamic probing; an empty list marks an explicit-only harness.
150+
*/
151+
autoSelection?: { providerIds: readonly string[] };
147152
/**
148153
* Plugin ids this harness owner permits to execute its locked sessions.
149154
* Delegates receive work admission and execution only; session mutation stays owner-only.

‎src/agents/thinking-runtime.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,28 @@ describe("resolveEffectiveAgentRuntime", () => {
6565
).toBe("openclaw");
6666
});
6767

68+
it("uses static auto-selection facts before resolving provider routes", () => {
69+
const supports = vi.fn<AgentHarness["supports"]>(() => ({ supported: true, priority: 100 }));
70+
registerAgentHarness({
71+
id: "codex",
72+
label: "Codex",
73+
autoSelection: { providerIds: ["openai", "codex"] },
74+
supports,
75+
runAttempt: async () => {
76+
throw new Error("not exercised");
77+
},
78+
});
79+
80+
expect(
81+
resolveEffectiveAgentRuntime({
82+
cfg: {},
83+
provider: "deepseek",
84+
modelId: "deepseek-v4-pro",
85+
}),
86+
).toBe("openclaw");
87+
expect(supports).not.toHaveBeenCalled();
88+
});
89+
6890
it("keeps an authored custom route on OpenClaw before registered harness selection", () => {
6991
const supports = vi.fn<AgentHarness["supports"]>(({ provider }) =>
7092
provider === "openai" ? { supported: true, priority: 100 } : { supported: false },

0 commit comments

Comments
 (0)