Skip to content

Commit e4ad933

Browse files
vincentkocRomneyDa
andcommitted
fix(agents): preserve gateway built plugin artifacts
Co-authored-by: Dallin Romney <[email protected]> Punchcard-Session: frost-orchard-lantern-ze
1 parent f92e936 commit e4ad933

13 files changed

Lines changed: 91 additions & 24 deletions

‎scripts/test-built-plugin-singleton.mts‎

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ const smokeEntryPath = path.join(repoRoot, "dist", "plugins", "build-smoke-entry
1515
assert.ok(fs.existsSync(smokeEntryPath), `missing build output: ${smokeEntryPath}`);
1616

1717
const {
18+
buildPluginRuntimeLoadOptions,
1819
clearPluginCommands,
1920
getPluginCommandSpecs,
2021
getPluginModuleLoaderStats,
2122
loadOpenClawPlugins,
2223
matchPluginCommand,
24+
resolvePluginRuntimeLoadContext,
2325
} = await import(pathToFileURL(smokeEntryPath).href);
2426

2527
assert.equal(typeof loadOpenClawPlugins, "function", "built loader export missing");
@@ -126,24 +128,28 @@ assert.equal(
126128
clearPluginCommands();
127129

128130
const smsStatsBefore = getPluginModuleLoaderStats();
129-
const smsRegistry = loadOpenClawPlugins({
130-
cache: false,
131-
preferBuiltPluginArtifacts: true,
132-
workspaceDir: tempRoot,
133-
env: {
134-
...process.env,
135-
OPENCLAW_BUNDLED_PLUGINS_DIR: path.join(repoRoot, "dist-runtime", "extensions"),
136-
},
137-
config: {
138-
plugins: {
139-
enabled: true,
140-
allow: ["sms"],
141-
entries: {
142-
sms: { enabled: true },
131+
// Prepared runtimes carry this context into late, plugin-scoped loads. Prove that the load-options
132+
// projection retains the built-artifact choice instead of reopening source transformation.
133+
const smsRegistry = loadOpenClawPlugins(
134+
buildPluginRuntimeLoadOptions(
135+
resolvePluginRuntimeLoadContext({
136+
config: {
137+
plugins: {
138+
enabled: true,
139+
allow: ["sms"],
140+
entries: { sms: { enabled: true } },
141+
},
143142
},
144-
},
145-
},
146-
});
143+
env: {
144+
...process.env,
145+
OPENCLAW_BUNDLED_PLUGINS_DIR: path.join(repoRoot, "extensions"),
146+
},
147+
preferBuiltPluginArtifacts: true,
148+
workspaceDir: tempRoot,
149+
}),
150+
{ cache: false, onlyPluginIds: ["sms"] },
151+
),
152+
);
147153
const smsRecord = smsRegistry.plugins.find((entry: { id: string }) => entry.id === "sms");
148154
assert.ok(smsRecord, "SMS plugin missing from registry");
149155
assert.equal(smsRecord.status, "loaded", smsRecord.error ?? "SMS plugin failed to load");

‎src/agents/prepared-model-runtime.inbound-registry.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
registerPreparedModelRuntimePublicationListener,
1717
refreshPreparedModelRuntimeSnapshots,
1818
} from "./prepared-model-runtime.js";
19+
import { getPreparedPluginRuntimeLoadContext } from "./prepared-model-runtime.plugin-context.js";
1920

2021
const mocks = getPreparedModelRuntimeMocks();
2122

@@ -73,6 +74,12 @@ describe("prepared reply dispatch runtime", () => {
7374
);
7475
expect(firstSnapshot?.metadataSnapshot).toBe(mocks.pluginMetadataSnapshot);
7576
expect(Object.isFrozen(firstRuntime)).toBe(true);
77+
expect(
78+
mocks.loadAgentRuntimePluginRegistryHandle.mock.calls.every(
79+
([params]) =>
80+
(params as { preferBuiltPluginArtifacts?: boolean }).preferBuiltPluginArtifacts,
81+
),
82+
).toBe(true);
7683

7784
const replacementCatalog = createDeferred<{ entries: [] }>();
7885
mocks.prepareStaticCatalog.mockImplementationOnce(async () => await replacementCatalog.promise);
@@ -107,6 +114,12 @@ describe("prepared reply dispatch runtime", () => {
107114
});
108115
expect(replacementRuntime).not.toBe(firstRuntime);
109116
expect(replacementRuntime?.modelCatalog).not.toBe(firstRuntime?.modelCatalog);
117+
expect(
118+
mocks.loadAgentRuntimePluginRegistryHandle.mock.calls.every(
119+
([params]) =>
120+
(params as { preferBuiltPluginArtifacts?: boolean }).preferBuiltPluginArtifacts,
121+
),
122+
).toBe(true);
110123
});
111124

112125
it("resolves the configured inbound registry across a launch-workspace override", async () => {
@@ -200,6 +213,9 @@ describe("prepared reply dispatch runtime", () => {
200213
expect(dynamicPreparationRegistries.every(Boolean)).toBe(true);
201214
expect(catalogGenerationRegistries.every(Boolean)).toBe(true);
202215
expect(dynamicSelectedBefore).toBe(configuredSelectedBefore);
216+
expect(getPreparedPluginRuntimeLoadContext(dynamicSelectedBefore)).toMatchObject({
217+
preferBuiltPluginArtifacts: true,
218+
});
203219
const registryCallsBeforeAuth = mocks.loadAgentRuntimePluginRegistryHandle.mock.calls.length;
204220
const authStorageCallsBeforeAuth = mocks.discoverAuthStorage.mock.calls.length;
205221
const modelCallsBeforeAuth = mocks.discoverModels.mock.calls.length;

‎src/agents/prepared-model-runtime.inbound-registry.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ function inboundRegistryIdentity(input: PreparedModelRuntimeInput): string {
1515
env: hashRuntimeConfigValue(input.env ?? process.env),
1616
workspaceDir: input.workspaceDir,
1717
allowGatewaySubagentBinding: input.allowGatewaySubagentBinding === true,
18+
preferBuiltPluginArtifacts: input.preferBuiltPluginArtifacts === true,
1819
});
1920
}
2021

@@ -27,6 +28,7 @@ export function preparedModelRuntimeWorkspaceFactsKey(input: PreparedModelRuntim
2728
loadRuntimePlugins: input.loadRuntimePlugins === true,
2829
workspaceDir: input.workspaceDir,
2930
allowGatewaySubagentBinding: input.allowGatewaySubagentBinding === true,
31+
preferBuiltPluginArtifacts: input.preferBuiltPluginArtifacts === true,
3032
runtimePluginSelections: input.runtimePluginSelections,
3133
});
3234
}
@@ -45,6 +47,7 @@ export function createPreparedInboundRegistryLoader(): PreparedInboundRegistryLo
4547
env: input.env ?? process.env,
4648
...(input.workspaceDir ? { workspaceDir: input.workspaceDir } : {}),
4749
...(input.allowGatewaySubagentBinding ? { allowGatewaySubagentBinding: true } : {}),
50+
...(input.preferBuiltPluginArtifacts ? { preferBuiltPluginArtifacts: true } : {}),
4851
metadataSnapshot,
4952
});
5053
registries.set(key, registry);
@@ -77,6 +80,7 @@ export function prepareWorkspacePluginRegistries(
7780
env: input.env ?? process.env,
7881
...(input.workspaceDir ? { workspaceDir: input.workspaceDir } : {}),
7982
...(input.allowGatewaySubagentBinding ? { allowGatewaySubagentBinding: true } : {}),
83+
...(input.preferBuiltPluginArtifacts ? { preferBuiltPluginArtifacts: true } : {}),
8084
metadataSnapshot,
8185
selections: input.runtimePluginSelections,
8286
})

‎src/agents/prepared-model-runtime.owner.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ export function rebindInputToCommittedConfiguredOwner(
185185
preserveWorkspaceDirOnRefresh: preserveWorkspaceDir,
186186
allowGatewaySubagentBinding:
187187
input.allowGatewaySubagentBinding ?? owner.input.allowGatewaySubagentBinding,
188+
preferBuiltPluginArtifacts:
189+
input.preferBuiltPluginArtifacts ?? owner.input.preferBuiltPluginArtifacts,
188190
runtimePluginSelections: input.runtimePluginSelections ?? owner.input.runtimePluginSelections,
189191
});
190192
}
@@ -213,6 +215,7 @@ export function normalizePreparedModelRuntimeInput(
213215
): PreparedModelRuntimeInput {
214216
const {
215217
inheritedAuthDir: _inheritedAuthDir,
218+
preferBuiltPluginArtifacts: _preferBuiltPluginArtifacts,
216219
readOnly,
217220
runtimePluginSelections: _runtimePluginSelections,
218221
skipCredentials,
@@ -244,6 +247,7 @@ export function normalizePreparedModelRuntimeInput(
244247
...(workspaceDir ? { workspaceDir } : {}),
245248
...(env ? { env } : {}),
246249
...(input.allowGatewaySubagentBinding === true ? { allowGatewaySubagentBinding: true } : {}),
250+
...(input.preferBuiltPluginArtifacts === true ? { preferBuiltPluginArtifacts: true } : {}),
247251
...(runtimePluginSelections?.length ? { runtimePluginSelections } : {}),
248252
};
249253
}
@@ -267,6 +271,7 @@ export function ownerKey(input: PreparedModelRuntimeInput): string {
267271
workspaceDir: input.workspaceDir,
268272
env: environmentFingerprint(input.env),
269273
allowGatewaySubagentBinding: input.allowGatewaySubagentBinding === true,
274+
preferBuiltPluginArtifacts: input.preferBuiltPluginArtifacts === true,
270275
runtimePluginSelections: input.runtimePluginSelections,
271276
config: input.readOnly ? hashRuntimeConfigValue(input.config) : undefined,
272277
});
@@ -281,7 +286,7 @@ export function resolvePublishedOwner(
281286
if (exact) {
282287
return exact;
283288
}
284-
if (!options.allowConfiguredWorkspaceFallback) {
289+
if (!options.allowConfiguredWorkspaceFallback && "preferBuiltPluginArtifacts" in input) {
285290
return undefined;
286291
}
287292
// Gateway launch may supply an authoritative workspace outside config. Request readers still
@@ -302,6 +307,8 @@ export function resolvePublishedOwner(
302307
// rebuild a live ephemeral catalog per request.
303308
(input.allowGatewaySubagentBinding === undefined ||
304309
owner.input.allowGatewaySubagentBinding === input.allowGatewaySubagentBinding) &&
310+
(input.preferBuiltPluginArtifacts === undefined ||
311+
owner.input.preferBuiltPluginArtifacts === input.preferBuiltPluginArtifacts) &&
305312
(input.runtimePluginSelections === undefined ||
306313
JSON.stringify(owner.input.runtimePluginSelections) ===
307314
JSON.stringify(input.runtimePluginSelections)) &&
@@ -327,6 +334,7 @@ export function hasSameLifecycleInput(
327334
environmentFingerprint(left.env) === environmentFingerprint(right.env) &&
328335
left.preserveWorkspaceDirOnRefresh === right.preserveWorkspaceDirOnRefresh &&
329336
left.allowGatewaySubagentBinding === right.allowGatewaySubagentBinding &&
337+
left.preferBuiltPluginArtifacts === right.preferBuiltPluginArtifacts &&
330338
JSON.stringify(left.runtimePluginSelections) === JSON.stringify(right.runtimePluginSelections)
331339
);
332340
}

‎src/agents/prepared-model-runtime.plugin-context.test.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ describe("prepared model runtime plugin metadata ownership", () => {
2929
const inputs = ["first", "second"].map((name) => ({
3030
agentDir: `/tmp/${name}-agent`,
3131
config,
32+
preferBuiltPluginArtifacts: true as const,
3233
workspaceDir: `/tmp/${name}-workspace`,
3334
}));
3435
const pluginGeneration = {
@@ -45,9 +46,10 @@ describe("prepared model runtime plugin metadata ownership", () => {
4546
expect(prepareOwnedPluginLoadContext(input, process.env, registry, gatewaySnapshot)).toBe(
4647
gatewaySnapshot,
4748
);
48-
expect(getPreparedPluginRuntimeLoadContext(registry)?.metadataSnapshot).toBe(
49-
gatewaySnapshot,
50-
);
49+
expect(getPreparedPluginRuntimeLoadContext(registry)).toMatchObject({
50+
metadataSnapshot: gatewaySnapshot,
51+
preferBuiltPluginArtifacts: true,
52+
});
5153
expect(
5254
withPreparedPluginGenerationScope({ input, pluginGeneration }, (snapshot) => snapshot),
5355
).toBe(gatewaySnapshot);
@@ -73,6 +75,7 @@ describe("prepared model runtime plugin metadata ownership", () => {
7375
.mockReturnValue(directSnapshot);
7476

7577
try {
78+
const registry = createEmptyPluginRegistry();
7679
expect(
7780
prepareOwnedPluginLoadContext(
7881
{
@@ -81,9 +84,12 @@ describe("prepared model runtime plugin metadata ownership", () => {
8184
workspaceDir,
8285
},
8386
process.env,
84-
undefined,
87+
registry,
8588
),
8689
).toBe(directSnapshot);
90+
expect(getPreparedPluginRuntimeLoadContext(registry)).not.toHaveProperty(
91+
"preferBuiltPluginArtifacts",
92+
);
8793
expect(resolveMetadata).toHaveBeenCalledWith({
8894
config,
8995
env: process.env,

‎src/agents/prepared-model-runtime.plugin-context.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,16 @@ function preparePluginLoadContext(
3737
const preparedMetadataSnapshot = metadataSnapshot.discovery
3838
? metadataSnapshot
3939
: { ...metadataSnapshot, discovery: emptyPluginDiscovery };
40+
const preparedContext = getPreparedPluginRuntimeLoadContext(registry);
4041
const context = {
4142
...resolvePluginRuntimeLoadContext({
4243
config,
4344
env,
4445
workspaceDir,
4546
metadataSnapshot: preparedMetadataSnapshot,
4647
manifestRegistry: metadataSnapshot.manifestRegistry,
48+
preferBuiltPluginArtifacts:
49+
input.preferBuiltPluginArtifacts || preparedContext?.preferBuiltPluginArtifacts,
4750
}),
4851
metadataSnapshot,
4952
installRecords: extractPluginInstallRecordsFromInstalledPluginIndex(metadataSnapshot.index),

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,10 @@ async function refreshPreparedModelRuntimeSnapshotsNow(
431431
const entries: Array<{ owner?: PreparedModelRuntimeOwner; input: PreparedModelRuntimeInput }> =
432432
[];
433433
const knownKeys = new Set<string>();
434-
for (const rawInput of listConfiguredOwnerInputs(config, workspace, bindings)) {
434+
for (const configuredInput of listConfiguredOwnerInputs(config, workspace, bindings)) {
435+
const rawInput = gatewayLifecycleActive
436+
? { ...configuredInput, preferBuiltPluginArtifacts: true as const }
437+
: configuredInput;
435438
let input = normalizePreparedModelRuntimeInput(rawInput);
436439
const preservedOwner = [...owners.values()].find(
437440
(owner) =>

‎src/agents/prepared-model-runtime.types.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ export type PreparedModelRuntimeInput = {
9292
skipCredentials?: boolean;
9393
env?: NodeJS.ProcessEnv;
9494
allowGatewaySubagentBinding?: boolean;
95+
/** Gateway lifecycle owners load bundled plugins from packaged build artifacts. */
96+
preferBuiltPluginArtifacts?: true;
9597
runtimePluginSelections?: readonly AgentHarnessPluginSelection[];
9698
config: OpenClawConfig;
9799
};

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,15 @@ describe("agent runtime plugin registries", () => {
148148
});
149149
});
150150

151-
it("preserves the gateway startup scope and ordering", () => {
151+
it("preserves the gateway startup scope, artifact selection, and ordering", () => {
152152
const config = {} as never;
153153
const metadataSnapshot = createMetadataSnapshot();
154154

155155
loadAgentRuntimePluginRegistryHandle({
156156
config,
157157
workspaceDir: "/tmp/workspace",
158158
metadataSnapshot: metadataSnapshot as never,
159+
preferBuiltPluginArtifacts: true,
159160
});
160161

161162
expect(hoisted.resolveAgentRuntimePluginLoadPlan).toHaveBeenCalledWith({
@@ -168,6 +169,7 @@ describe("agent runtime plugin registries", () => {
168169
expect(hoisted.loadPluginRegistryHandle).toHaveBeenCalledWith(
169170
expect.objectContaining({
170171
channelPluginLoadIntent: "full",
172+
preferBuiltPluginArtifacts: true,
171173
}),
172174
);
173175
});
@@ -250,6 +252,9 @@ describe("agent runtime plugin registries", () => {
250252
runtimeOptions: undefined,
251253
workspaceDir: snapshot.workspaceDir,
252254
});
255+
expect(hoisted.loadPluginRegistryHandle.mock.calls[0]?.[0]).not.toHaveProperty(
256+
"preferBuiltPluginArtifacts",
257+
);
253258
});
254259

255260
it("owns a scoped registry for direct hosts", async () => {

‎src/agents/runtime-plugins.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type AgentRuntimePluginRegistryParams = {
2929
selections?: readonly AgentHarnessPluginSelection[];
3030
/** Lifecycle-selected metadata. Omission selects one standalone cold generation. */
3131
metadataSnapshot?: PluginMetadataSnapshot;
32+
preferBuiltPluginArtifacts?: true;
3233
};
3334

3435
function resolveAgentRuntimePluginRegistryLoad(params: AgentRuntimePluginRegistryParams) {
@@ -44,6 +45,7 @@ function resolveAgentRuntimePluginRegistryLoad(params: AgentRuntimePluginRegistr
4445
...(params.env ? { env: params.env } : {}),
4546
workspaceDir: requestedWorkspaceDir,
4647
onlyPluginIds: [],
48+
...(params.preferBuiltPluginArtifacts ? { preferBuiltPluginArtifacts: true } : {}),
4749
runtimeOptions: params.allowGatewaySubagentBinding
4850
? { allowGatewaySubagentBinding: true }
4951
: undefined,
@@ -62,6 +64,7 @@ function resolveAgentRuntimePluginRegistryLoad(params: AgentRuntimePluginRegistr
6264
...(metadataSnapshot.discovery ? { discovery: metadataSnapshot.discovery } : {}),
6365
installRecords: extractPluginInstallRecordsFromInstalledPluginIndex(metadataSnapshot.index),
6466
manifestRegistry: metadataSnapshot.manifestRegistry,
67+
...(params.preferBuiltPluginArtifacts ? { preferBuiltPluginArtifacts: true } : {}),
6568
...(workspaceDir ? { workspaceDir } : {}),
6669
};
6770
const requestPluginRegistry = getPluginRuntimeGatewayRequestScope()?.pluginRegistry;

0 commit comments

Comments
 (0)