Skip to content

Commit aa25d4e

Browse files
vincentkocsteipete
andcommitted
perf(agents): reuse active gateway plugin generation
Reuse the active Gateway registry only for the exact current metadata generation and matching runtime authority. Seed selected-runtime loading from imported inbound plugin IDs so deferred records remain eligible for their scoped load, while the generation owner continues to supply built-artifact preference. Co-authored-by: Peter Steinberger <[email protected]>
1 parent f33952e commit aa25d4e

5 files changed

Lines changed: 75 additions & 77 deletions

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

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,19 @@ export function createPreparedInboundRegistryLoader(): PreparedInboundRegistryLo
5656
return existing;
5757
}
5858
const activeRegistry = getActivePluginRegistry();
59-
const activeWorkspaceDir = getActivePluginRegistryWorkspaceDir();
60-
// Reuse is generation-bound: the requesting snapshot must BE the process-current
61-
// published generation, or a leaked/older active registry could satisfy the
62-
// manifest match (bundled records compare by id+origin only) and serve another
63-
// generation's plugins. Identity, not equivalence, is the authority check here.
64-
const currentGenerationSnapshot = getCurrentPluginMetadataSnapshot({
65-
config: input.config,
66-
workspaceDir: metadataSnapshot.workspaceDir,
67-
allowWorkspaceScopedSnapshot: true,
68-
});
59+
// Identity is the generation authority. Manifest equivalence alone could let a
60+
// stale active registry satisfy a newer bundled snapshot.
6961
const reusableGatewayRegistry =
7062
input.allowGatewaySubagentBinding === true &&
7163
input.env === undefined &&
7264
getActivePluginRuntimeSubagentMode() === "gateway-bindable" &&
7365
activeRegistry &&
74-
currentGenerationSnapshot === metadataSnapshot &&
75-
(activeWorkspaceDir === undefined || activeWorkspaceDir === metadataSnapshot.workspaceDir) &&
66+
getActivePluginRegistryWorkspaceDir() === metadataSnapshot.workspaceDir &&
67+
getCurrentPluginMetadataSnapshot({
68+
config: input.config,
69+
workspaceDir: metadataSnapshot.workspaceDir,
70+
allowWorkspaceScopedSnapshot: true,
71+
}) === metadataSnapshot &&
7672
registryMatchesManifestPluginIds(
7773
activeRegistry,
7874
metadataSnapshot.manifestRegistry.plugins,
@@ -113,11 +109,6 @@ export function prepareWorkspacePluginRegistries(
113109
const inboundPluginRegistry = input.readOnly
114110
? undefined
115111
: loadInboundRegistry?.(input, metadataSnapshot);
116-
// Extending the reused Gateway generation inherits its artifact preference: the
117-
// Gateway loader realizes built artifacts, so the delta load must not re-import
118-
// the same plugins through source transforms. Isolated loads keep source truth.
119-
const extendsActiveGatewayGeneration =
120-
inboundPluginRegistry !== undefined && inboundPluginRegistry === getActivePluginRegistry();
121112
const runtimePluginRegistry =
122113
input.runtimePluginSelections || !inboundPluginRegistry
123114
? loadAgentRuntimePluginRegistryHandle({
@@ -131,11 +122,7 @@ export function prepareWorkspacePluginRegistries(
131122
...(input.workspaceDir ? { workspaceDir: input.workspaceDir } : {}),
132123
...(input.allowGatewaySubagentBinding ? { allowGatewaySubagentBinding: true } : {}),
133124
metadataSnapshot,
134-
// Lifecycle-selected callers and delta loads extending the active Gateway
135-
// generation both stay on built artifacts; isolated loads keep source truth.
136-
...(preferBuiltPluginArtifacts || extendsActiveGatewayGeneration
137-
? { preferBuiltPluginArtifacts: true }
138-
: {}),
125+
...(preferBuiltPluginArtifacts ? { preferBuiltPluginArtifacts: true } : {}),
139126
selections: input.runtimePluginSelections,
140127
})
141128
: inboundPluginRegistry;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe("prepared model runtime plugin metadata ownership", () => {
3636
inlineProviderModels: [],
3737
pluginMetadataSnapshot: gatewaySnapshot,
3838
};
39-
const resolveMetadata = vi.spyOn(pluginMetadata, "loadPluginMetadataSnapshot");
39+
const resolveMetadata = vi.spyOn(pluginMetadata, "resolvePluginMetadataSnapshot");
4040
const getCurrentMetadata = vi.spyOn(currentPluginMetadata, "getCurrentPluginMetadataSnapshot");
4141

4242
try {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,8 @@ function resolveColdMetadataSnapshot(
7474
input: PreparedModelRuntimeInput,
7575
env: NodeJS.ProcessEnv,
7676
): PluginMetadataSnapshot {
77-
// Resolve (slot-probing) instead of load: in the gateway the published current
78-
// generation satisfies this read, which keeps snapshot identity stable for the
79-
// generation-bound registry reuse in the inbound loader.
77+
// Slot probing preserves the published Gateway generation identity; cold callers
78+
// still fall through to a fresh metadata load.
8079
const resolvedMetadataSnapshot = resolvePluginMetadataSnapshot({
8180
config: input.config,
8281
env,

‎src/agents/prepared-model-runtime.startup-static.test.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,8 @@ describe("prepared model runtime Gateway catalog mode", () => {
407407
);
408408
expect(mocks.buildPreparedModelCatalogSnapshot).not.toHaveBeenCalled();
409409
expect(mocks.loadStaticCatalog).not.toHaveBeenCalled();
410-
// Two slot-probing resolutions share the published generation: the prepared
411-
// cold plugin context and the model-id normalization lane. Neither builds a
412-
// catalog; the laziness guards above are the eager-work protections.
410+
// The prepared plugin context and model-id normalization probe the same
411+
// published metadata generation without starting catalog discovery.
413412
expect(mocks.resolvePluginMetadataSnapshot).toHaveBeenCalledTimes(2);
414413
expect(configuredRuntimeModelCount).toBe(1);
415414
expect(generatedCatalogReadCount).toBe(0);

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

Lines changed: 61 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -122,34 +122,36 @@ describe("agent runtime plugin registries", () => {
122122
);
123123
});
124124

125-
it("reuses the Gateway inbound registry and loads only its selected provider delta", () => {
125+
it("reuses the current Gateway generation and loads only the imported-plugin delta", () => {
126126
const config = {} as never;
127-
const gatewayPlugin = {
128-
id: "gateway-owned",
129-
origin: "bundled",
130-
rootDir: "/dist/extensions/gateway-owned",
131-
source: "/dist/extensions/gateway-owned/index.js",
132-
status: "loaded",
127+
const workspaceDir = "/tmp/default-workspace";
128+
const activeRegistry = {
129+
plugins: [
130+
{ id: "gateway-owned", origin: "bundled", status: "loaded" },
131+
{
132+
id: "deferred",
133+
origin: "bundled",
134+
status: "loaded",
135+
format: "openclaw",
136+
imported: false,
137+
},
138+
],
133139
};
134-
const activeRegistry = { plugins: [gatewayPlugin] };
135-
const selectedRegistry = { plugins: [gatewayPlugin, { id: "selected-provider" }] };
136140
const metadataSnapshot = {
137-
...createMetadataSnapshot("/tmp/default-workspace", undefined),
141+
...createMetadataSnapshot(workspaceDir, undefined),
138142
manifestRegistry: {
139143
diagnostics: [],
140144
plugins: [
141-
{
142-
id: "gateway-owned",
143-
origin: "bundled",
144-
rootDir: "/extensions/gateway-owned",
145-
source: "/extensions/gateway-owned/index.ts",
146-
},
145+
{ id: "gateway-owned", origin: "bundled" },
146+
{ id: "deferred", origin: "bundled" },
147147
],
148148
},
149149
};
150+
const selectedRegistry = { plugins: [...activeRegistry.plugins, { id: "selected-provider" }] };
150151
hoisted.getActivePluginRegistry.mockReturnValue(activeRegistry);
152+
hoisted.getActivePluginRegistryWorkspaceDir.mockReturnValue(workspaceDir);
151153
hoisted.getActivePluginRuntimeSubagentMode.mockReturnValue("gateway-bindable");
152-
hoisted.getCurrentPluginMetadataSnapshot.mockImplementation(() => metadataSnapshot);
154+
hoisted.getCurrentPluginMetadataSnapshot.mockReturnValue(metadataSnapshot);
153155
hoisted.loadPluginRegistryHandle.mockReturnValue(selectedRegistry);
154156
hoisted.resolveAgentRuntimePluginLoadPlan.mockImplementation(({ basePluginIds }) => ({
155157
config,
@@ -162,71 +164,82 @@ describe("agent runtime plugin registries", () => {
162164
allowGatewaySubagentBinding: true,
163165
config,
164166
runtimePluginSelections: [{ provider: "selected", modelId: "model" }],
165-
workspaceDir: "/tmp/default-workspace",
167+
workspaceDir,
166168
},
167169
metadataSnapshot as never,
168170
createPreparedInboundRegistryLoader(),
171+
true,
169172
);
170173

171174
expect(prepared.inboundPluginRegistry).toBe(activeRegistry);
172175
expect(prepared.runtimePluginRegistry).toBe(selectedRegistry);
176+
expect(hoisted.resolveAgentRuntimePluginLoadPlan).toHaveBeenCalledWith(
177+
expect.objectContaining({ basePluginIds: ["gateway-owned"] }),
178+
);
173179
expect(hoisted.loadPluginRegistryHandle).toHaveBeenCalledOnce();
174180
expect(hoisted.loadPluginRegistryHandle).toHaveBeenCalledWith(
175181
expect.objectContaining({
176182
onlyPluginIds: ["gateway-owned", "selected-provider"],
183+
preferBuiltPluginArtifacts: true,
177184
}),
178185
);
179186
});
180187

181-
it("refuses Gateway registry reuse for a non-current metadata generation", () => {
188+
it.each([
189+
{
190+
name: "custom environment",
191+
input: { env: { OPENCLAW_STATE_DIR: "/tmp/custom-state" } },
192+
},
193+
{
194+
name: "non-bindable mode",
195+
setup: () => hoisted.getActivePluginRuntimeSubagentMode.mockReturnValue("default"),
196+
},
197+
{
198+
name: "different workspace",
199+
setup: () => hoisted.getActivePluginRegistryWorkspaceDir.mockReturnValue("/tmp/other"),
200+
},
201+
{
202+
name: "stale metadata generation",
203+
setup: () => hoisted.getCurrentPluginMetadataSnapshot.mockReturnValue({}),
204+
},
205+
{
206+
name: "manifest mismatch",
207+
setup: (activeRegistry: { plugins: Array<{ origin: string }> }) => {
208+
activeRegistry.plugins[0]!.origin = "external";
209+
},
210+
},
211+
])("refuses Gateway registry reuse for $name", ({ input, setup }) => {
182212
const config = {} as never;
183-
const gatewayPlugin = {
184-
id: "gateway-owned",
185-
origin: "bundled",
186-
rootDir: "/dist/extensions/gateway-owned",
187-
source: "/dist/extensions/gateway-owned/index.js",
188-
status: "loaded",
213+
const workspaceDir = "/tmp/default-workspace";
214+
const activeRegistry = {
215+
plugins: [{ id: "gateway-owned", origin: "bundled", status: "loaded" }],
189216
};
190-
const activeRegistry = { plugins: [gatewayPlugin] };
191217
const metadataSnapshot = {
192-
...createMetadataSnapshot("/tmp/default-workspace", undefined),
218+
...createMetadataSnapshot(workspaceDir, undefined),
193219
manifestRegistry: {
194220
diagnostics: [],
195-
plugins: [
196-
{
197-
id: "gateway-owned",
198-
origin: "bundled",
199-
rootDir: "/extensions/gateway-owned",
200-
source: "/extensions/gateway-owned/index.ts",
201-
},
202-
],
221+
plugins: [{ id: "gateway-owned", origin: "bundled" }],
203222
},
204223
};
205224
hoisted.getActivePluginRegistry.mockReturnValue(activeRegistry);
225+
hoisted.getActivePluginRegistryWorkspaceDir.mockReturnValue(workspaceDir);
206226
hoisted.getActivePluginRuntimeSubagentMode.mockReturnValue("gateway-bindable");
207-
// A different (older/leaked) generation is current: identity must fail closed.
208-
hoisted.getCurrentPluginMetadataSnapshot.mockReturnValue(
209-
createMetadataSnapshot("/tmp/default-workspace", undefined),
210-
);
211-
hoisted.loadPluginRegistryHandle.mockReturnValue({ plugins: [] });
212-
hoisted.resolveAgentRuntimePluginLoadPlan.mockImplementation(({ basePluginIds }) => ({
213-
config,
214-
pluginIds: basePluginIds,
215-
}));
227+
hoisted.getCurrentPluginMetadataSnapshot.mockReturnValue(metadataSnapshot);
228+
setup?.(activeRegistry);
216229

217-
const loadInboundRegistry = createPreparedInboundRegistryLoader();
218-
const inbound = loadInboundRegistry(
230+
const inbound = createPreparedInboundRegistryLoader()(
219231
{
220232
agentDir: "/tmp/agent",
221233
allowGatewaySubagentBinding: true,
222234
config,
223-
workspaceDir: "/tmp/default-workspace",
235+
workspaceDir,
236+
...input,
224237
},
225238
metadataSnapshot as never,
226239
);
227240

228241
expect(inbound).not.toBe(activeRegistry);
229-
expect(hoisted.loadPluginRegistryHandle).toHaveBeenCalled();
242+
expect(hoisted.loadPluginRegistryHandle).toHaveBeenCalledOnce();
230243
});
231244

232245
it("keeps direct no-current loads on the requested workspace", () => {

0 commit comments

Comments
 (0)