Skip to content

Commit 245dbac

Browse files
authored
fix: agent exec loads installed harnesses locally (#119855)
* fix(agents): bind local plugin harness registry (#119852) * test(agents): isolate local registry in command suites
1 parent ed40d05 commit 245dbac

9 files changed

Lines changed: 190 additions & 68 deletions

‎src/agents/agent-command-local.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/** Owns local agent-command admission scopes outside a running Gateway. */
2+
import type { CliDeps } from "../cli/deps.types.js";
3+
import { getRuntimeConfig } from "../config/io.js";
4+
import { withLocalGatewayRequestScope } from "../gateway/local-request-context.js";
5+
import {
6+
captureAgentRunLifecycleGeneration,
7+
withAgentRunLifecycleGeneration,
8+
} from "../infra/agent-events.js";
9+
import type { RuntimeEnv } from "../runtime.js";
10+
import { runWithAgentCommandRecoveryOwner } from "./agent-command-recovery-owner.js";
11+
import {
12+
prepareAgentCommandExecution,
13+
type PreparedAgentCommandExecution,
14+
} from "./command/prepare.js";
15+
import { resolveAgentCommandDeps } from "./command/runtime-loaders.js";
16+
import type { AgentCommandOpts } from "./command/types.js";
17+
import { withAgentPluginRegistry } from "./runtime-plugins.js";
18+
import { measureAgentStartup } from "./startup-timing.js";
19+
20+
type ResolvedAgentCommandDeps = Awaited<ReturnType<typeof resolveAgentCommandDeps>>;
21+
22+
/** Runs a local command under one request, recovery, and plugin-registry generation. */
23+
export async function runLocalAgentCommand<TResult>(params: {
24+
opts: AgentCommandOpts;
25+
runtime: RuntimeEnv;
26+
deps?: CliDeps;
27+
run: (
28+
prepared: PreparedAgentCommandExecution,
29+
resolvedDeps: ResolvedAgentCommandDeps,
30+
) => Promise<TResult>;
31+
}): Promise<TResult> {
32+
const resolvedDeps = await measureAgentStartup("command-dependencies", () =>
33+
resolveAgentCommandDeps(params.deps),
34+
);
35+
const lifecycleGeneration =
36+
params.opts.lifecycleGeneration ?? captureAgentRunLifecycleGeneration(params.opts.runId ?? "");
37+
return await withAgentRunLifecycleGeneration(lifecycleGeneration, () =>
38+
withLocalGatewayRequestScope(
39+
{ deps: resolvedDeps, getRuntimeConfig },
40+
async () =>
41+
await runWithAgentCommandRecoveryOwner({
42+
lifecycleGeneration,
43+
mode: "reject_uncoordinated",
44+
opts: {
45+
...params.opts,
46+
lifecycleGeneration,
47+
senderIsOwner: params.opts.senderIsOwner ?? true,
48+
allowModelOverride: params.opts.allowModelOverride ?? true,
49+
},
50+
prepare: async (preparedOpts) =>
51+
await measureAgentStartup("command-prepare", () =>
52+
prepareAgentCommandExecution(preparedOpts, params.runtime),
53+
),
54+
run: async (prepared) =>
55+
await withAgentPluginRegistry({
56+
config: prepared.cfg,
57+
workspaceDir: prepared.workspaceDir,
58+
run: () => params.run(prepared, resolvedDeps),
59+
}),
60+
}),
61+
),
62+
);
63+
}

‎src/agents/agent-command.compaction-rotation.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ vi.mock("./harness/runtime-plugin.js", () => ({
104104
ensureSelectedAgentHarnessPlugin: vi.fn(async () => undefined),
105105
}));
106106

107+
vi.mock("./runtime-plugins.js", () => ({
108+
withAgentPluginRegistry: ({ run }: { run: () => unknown }) => run(),
109+
}));
110+
107111
vi.mock("./workspace.js", () => ({
108112
ensureAgentWorkspace: vi.fn(async () => undefined),
109113
}));

‎src/agents/agent-command.live-model-switch.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ vi.mock("./harness/runtime-plugin.js", () => ({
249249
ensureSelectedAgentHarnessPlugin: vi.fn(async () => undefined),
250250
}));
251251

252+
vi.mock("./runtime-plugins.js", () => ({
253+
withAgentPluginRegistry: ({ run }: { run: () => unknown }) => run(),
254+
}));
255+
252256
// Harness selection has dedicated coverage; this command suite registers no auto harnesses.
253257
vi.mock("./harness/support.js", () => ({
254258
resolveAutoAgentHarnessId: () => undefined,

‎src/agents/agent-command.ts‎

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
33
import type { VerboseLevel } from "../auto-reply/thinking.js";
44
import type { CliDeps } from "../cli/deps.types.js";
5-
import { getRuntimeConfig } from "../config/io.js";
65
import { resolveSessionWorkStartError } from "../config/sessions/lifecycle.js";
76
import { buildRestartRecoveryClaimCleanupPatch } from "../config/sessions/restart-recovery-state.js";
87
import type { RestartRecoveryTerminalDeliveryEvidenceResult } from "../config/sessions/restart-recovery-types.js";
98
import type { SessionEntry } from "../config/sessions/types.js";
10-
import { withLocalGatewayRequestScope } from "../gateway/local-request-context.js";
119
import {
1210
assertAgentRunLifecycleGenerationCurrent,
1311
captureAgentRunLifecycleGeneration,
@@ -25,6 +23,7 @@ import { beginSessionWorkAdmission } from "../sessions/session-lifecycle-admissi
2523
import { classifySessionStateActor } from "../sessions/session-state-events.js";
2624
import { sessionDeliveryChannel, type DeliveryContext } from "../utils/delivery-context.shared.js";
2725
import { executionIdentity } from "./agent-command-execution-identity.js";
26+
import { runLocalAgentCommand } from "./agent-command-local.js";
2827
import { runWithAgentCommandRecoveryOwner } from "./agent-command-recovery-owner.js";
2928
import {
3029
buildCurrentRunRestartRecoveryClaim,
@@ -599,43 +598,13 @@ async function agentCommandWithAdmissionIngress(
599598
runtime: RuntimeEnv = defaultRuntime,
600599
deps?: CliDeps,
601600
) {
602-
const resolvedDeps = await measureAgentStartup("command-dependencies", () =>
603-
resolveAgentCommandDeps(deps),
604-
);
605-
const lifecycleGeneration =
606-
opts.lifecycleGeneration ?? captureAgentRunLifecycleGeneration(opts.runId ?? "");
607-
return await withAgentRunLifecycleGeneration(lifecycleGeneration, () =>
608-
withLocalGatewayRequestScope(
609-
{
610-
deps: resolvedDeps,
611-
getRuntimeConfig,
612-
},
613-
async () =>
614-
await runWithAgentCommandRecoveryOwner({
615-
lifecycleGeneration,
616-
mode: "reject_uncoordinated",
617-
opts: {
618-
...opts,
619-
lifecycleGeneration,
620-
// Only the local entrypoint may inherit trusted-operator defaults.
621-
senderIsOwner: opts.senderIsOwner ?? true,
622-
allowModelOverride: opts.allowModelOverride ?? true,
623-
},
624-
prepare: async (preparedOpts) =>
625-
await measureAgentStartup("command-prepare", () =>
626-
prepareAgentCommandExecution(preparedOpts, runtime),
627-
),
628-
run: async (prepared) =>
629-
await agentCommandInternal(
630-
prepared,
631-
prepared.opts,
632-
admissionIngress,
633-
runtime,
634-
resolvedDeps,
635-
),
636-
}),
637-
),
638-
);
601+
return await runLocalAgentCommand({
602+
opts,
603+
runtime,
604+
deps,
605+
run: async (prepared, resolvedDeps) =>
606+
await agentCommandInternal(prepared, prepared.opts, admissionIngress, runtime, resolvedDeps),
607+
});
639608
}
640609

641610
export async function agentCommand(

‎src/agents/command/assistant-transcript-repair.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ vi.mock("../harness/runtime-plugin.js", () => ({
113113
ensureSelectedAgentHarnessPlugin: vi.fn(async () => undefined),
114114
}));
115115

116+
vi.mock("../runtime-plugins.js", () => ({
117+
withAgentPluginRegistry: ({ run }: { run: () => unknown }) => run(),
118+
}));
119+
116120
vi.mock("../workspace.js", () => ({
117121
ensureAgentWorkspace: vi.fn(async () => undefined),
118122
}));

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,22 @@ describe("agent runtime plugin registries", () => {
102102
}),
103103
);
104104
});
105+
106+
it("lets direct local hosts bound the registry to configured runtime owners", () => {
107+
const config = {} as never;
108+
109+
loadAgentRuntimePluginRegistryHandle({
110+
basePluginIds: [],
111+
config,
112+
workspaceDir: "/tmp/workspace",
113+
});
114+
115+
expect(hoisted.getCurrentPluginMetadataSnapshot).not.toHaveBeenCalled();
116+
expect(hoisted.resolveAgentRuntimePluginLoadPlan).toHaveBeenCalledWith({
117+
config,
118+
workspaceDir: "/tmp/workspace",
119+
basePluginIds: [],
120+
selections: [],
121+
});
122+
});
105123
});

‎src/agents/runtime-plugins.ts‎

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { normalizePluginsConfig } from "../plugins/config-state.js";
33
import { getCurrentPluginMetadataSnapshot } from "../plugins/current-plugin-metadata-snapshot.js";
44
import { loadPluginRegistryHandle } from "../plugins/loader.js";
55
import type { PluginRegistry } from "../plugins/registry-types.js";
6+
import {
7+
getPluginRuntimeGatewayRequestScope,
8+
withPluginRuntimeRegistryScope,
9+
} from "../plugins/runtime/gateway-request-scope.js";
610
import { resolveUserPath } from "../utils.js";
711
import { collectConfiguredAgentHarnessRuntimes } from "./harness-runtimes.js";
812
import {
@@ -40,6 +44,8 @@ type AgentRuntimePluginRegistryParams = {
4044
env?: NodeJS.ProcessEnv;
4145
workspaceDir?: string | null;
4246
allowGatewaySubagentBinding?: boolean;
47+
/** Explicit base scope for hosts without a Gateway startup registry. */
48+
basePluginIds?: readonly string[];
4349
selections?: readonly AgentHarnessPluginSelection[];
4450
};
4551

@@ -63,11 +69,14 @@ function resolveAgentRuntimePluginRegistryLoad(params: AgentRuntimePluginRegistr
6369
},
6470
};
6571
}
66-
const startupPluginIds = resolveStartupPluginIdsFromCurrentSnapshot({
67-
config: params.config,
68-
env: params.env,
69-
workspaceDir,
70-
});
72+
const startupPluginIds =
73+
params.basePluginIds !== undefined
74+
? [...params.basePluginIds]
75+
: resolveStartupPluginIdsFromCurrentSnapshot({
76+
config: params.config,
77+
env: params.env,
78+
workspaceDir,
79+
});
7180
const plan = resolveAgentRuntimePluginLoadPlan({
7281
config: params.config,
7382
workspaceDir: workspaceDir ?? process.cwd(),
@@ -106,3 +115,20 @@ export function loadAgentRuntimePluginRegistryHandle(
106115
const load = resolveAgentRuntimePluginRegistryLoad(params);
107116
return loadPluginRegistryHandle({ ...load.loadOptions, activate: false });
108117
}
118+
119+
/** Binds a scoped plugin generation when a direct host has no Gateway owner. */
120+
export async function withAgentPluginRegistry<T>(params: {
121+
config: OpenClawConfig;
122+
workspaceDir: string;
123+
run: () => Promise<T>;
124+
}): Promise<T> {
125+
if (getPluginRuntimeGatewayRequestScope()?.pluginRegistry) {
126+
return await params.run();
127+
}
128+
const pluginRegistry = loadAgentRuntimePluginRegistryHandle({
129+
basePluginIds: [],
130+
config: params.config,
131+
workspaceDir: params.workspaceDir,
132+
});
133+
return await withPluginRuntimeRegistryScope(pluginRegistry, params.run);
134+
}

‎src/commands/agent-command.test-mocks.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ vi.mock("../agents/harness/runtime-plugin.js", () => ({
1010
ensureSelectedAgentHarnessPlugin: agentHarnessPluginMocks.ensureSelectedAgentHarnessPlugin,
1111
}));
1212

13+
vi.mock("../agents/runtime-plugins.js", () => ({
14+
withAgentPluginRegistry: ({ run }: { run: () => unknown }) => run(),
15+
}));
16+
1317
vi.mock("../logging/subsystem.js", () => {
1418
const createMockLogger = () => ({
1519
subsystem: "test",

‎src/commands/agent-exec-plugin.e2e.test.ts‎

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import os from "node:os";
44
import path from "node:path";
55
import { promisify } from "node:util";
66
import { afterEach, describe, expect, it } from "vitest";
7+
import type { OpenClawConfig } from "../config/types.openclaw.js";
8+
import { writePersistedInstalledPluginIndexInstallRecords } from "../plugins/installed-plugin-index-records.js";
79

810
const execFileAsync = promisify(execFile);
911
const tempRoots: string[] = [];
@@ -89,35 +91,63 @@ async function writeHarnessPlugin(stateDir: string): Promise<void> {
8991
};\n`,
9092
"utf8",
9193
);
94+
await writePersistedInstalledPluginIndexInstallRecords(
95+
{
96+
"exec-proof": {
97+
source: "path",
98+
sourcePath: pluginDir,
99+
installPath: pluginDir,
100+
},
101+
},
102+
{
103+
stateDir,
104+
config: buildExecProofConfig(),
105+
candidates: [
106+
{
107+
idHint: "exec-proof",
108+
source: path.join(pluginDir, "index.js"),
109+
rootDir: pluginDir,
110+
origin: "global",
111+
},
112+
],
113+
},
114+
);
115+
}
116+
117+
function buildExecProofConfig(): OpenClawConfig {
118+
return {
119+
plugins: {
120+
allow: ["exec-proof"],
121+
entries: { "exec-proof": { enabled: true } },
122+
},
123+
models: {
124+
providers: {
125+
"exec-proof": {
126+
api: "openai-responses",
127+
baseUrl: "https://example.invalid/v1",
128+
models: [
129+
{
130+
id: "proof-model",
131+
name: "Proof model",
132+
reasoning: false,
133+
input: ["text"],
134+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
135+
contextWindow: 128000,
136+
maxTokens: 4096,
137+
agentRuntime: { id: "exec-proof" },
138+
},
139+
],
140+
},
141+
},
142+
},
143+
agents: { defaults: { model: { primary: "exec-proof/proof-model" } } },
144+
};
92145
}
93146

94147
async function writeConfig(stateDir: string): Promise<void> {
95148
await fs.writeFile(
96149
path.join(stateDir, "openclaw.json"),
97-
JSON.stringify({
98-
plugins: {
99-
allow: ["exec-proof"],
100-
entries: { "exec-proof": { enabled: true } },
101-
},
102-
models: {
103-
providers: {
104-
"exec-proof": {
105-
api: "openai-responses",
106-
baseUrl: "https://example.invalid/v1",
107-
models: [
108-
{
109-
id: "proof-model",
110-
name: "Proof model",
111-
contextWindow: 128000,
112-
maxTokens: 4096,
113-
agentRuntime: { id: "exec-proof" },
114-
},
115-
],
116-
},
117-
},
118-
},
119-
agents: { defaults: { model: { primary: "exec-proof/proof-model" } } },
120-
}),
150+
JSON.stringify(buildExecProofConfig()),
121151
"utf8",
122152
);
123153
}

0 commit comments

Comments
 (0)