Skip to content

Commit 26cf4e6

Browse files
steipetePeter Steinberger
andauthored
test(gateway): reuse startup plugin metadata snapshot (#117897)
Co-authored-by: Peter Steinberger <[email protected]>
1 parent 86a95b4 commit 26cf4e6

2 files changed

Lines changed: 70 additions & 16 deletions

File tree

‎src/gateway/server-startup-model-runtime.event-loop.test.ts‎

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { mkdtemp, rm } from "node:fs/promises";
22
import http from "node:http";
33
import os from "node:os";
44
import path from "node:path";
5-
import { performance } from "node:perf_hooks";
65
import { afterEach, describe, expect, it, vi } from "vitest";
76
import type { OpenClawConfig } from "../config/types.openclaw.js";
87
import type { ProviderPlugin } from "../plugins/types.js";
@@ -41,8 +40,8 @@ vi.mock("../plugins/provider-discovery.js", async (importOriginal) => {
4140
pluginId: "openai",
4241
label: "OpenAI",
4342
auth: [],
44-
catalog: { order: "simple", run: async () => null },
45-
staticCatalog: { order: "simple", run: async () => null },
43+
catalog: { order: "simple", run: providerMocks.liveCatalog },
44+
staticCatalog: { order: "simple", run: providerMocks.staticCatalog },
4645
};
4746
return {
4847
...actual,
@@ -56,6 +55,11 @@ const { resetPreparedModelRuntimeSnapshotsForTest } =
5655
await import("../agents/prepared-model-runtime.test-support.js");
5756
const { writePersistedAuthProfileStoreRaw } = await import("../agents/auth-profiles/sqlite.js");
5857
const { resolveAgentDir } = await import("../agents/agent-scope.js");
58+
const { createPluginMetadataSnapshot, makeRegistry } =
59+
await import("../config/plugin-auto-enable.test-helpers.js");
60+
const { setCurrentPluginMetadataSnapshot } =
61+
await import("../plugins/current-plugin-metadata-snapshot.js");
62+
const pluginDiscovery = await import("../plugins/discovery.js");
5963
const { startGatewaySidecars } = await import("./server-startup-post-attach.js");
6064

6165
async function listenHealthz(
@@ -108,9 +112,55 @@ describe("Gateway prepared model runtime startup", () => {
108112
},
109113
},
110114
gateway: { mode: "local", bind: "loopback", auth: { mode: "none" } },
111-
plugins: { enabled: false },
112115
} satisfies OpenClawConfig;
113116
const env = { ...process.env, OPENCLAW_STATE_DIR: stateDir };
117+
const manifestRegistry = makeRegistry([
118+
{ id: "openai", channels: [], providers: ["openai"], origin: "bundled" },
119+
]);
120+
const providerManifest = manifestRegistry.plugins[0];
121+
if (!providerManifest) {
122+
throw new Error("expected bundled OpenAI provider manifest");
123+
}
124+
providerManifest.enabledByDefault = true;
125+
providerManifest.modelCatalog = { ...providerConfig, discovery: { openai: "runtime" } };
126+
const metadataSnapshot = createPluginMetadataSnapshot({
127+
config: cfg,
128+
manifestRegistry,
129+
workspaceDir,
130+
});
131+
const startupMetadataSnapshot = {
132+
...metadataSnapshot,
133+
index: {
134+
...metadataSnapshot.index,
135+
plugins: [
136+
{
137+
pluginId: providerManifest.id,
138+
manifestPath: providerManifest.manifestPath,
139+
manifestHash: "openai-test-manifest",
140+
rootDir: providerManifest.rootDir,
141+
origin: providerManifest.origin,
142+
enabled: true,
143+
enabledByDefault: true,
144+
startup: {
145+
sidecar: false,
146+
memory: false,
147+
deferConfiguredChannelFullLoadUntilAfterListen: false,
148+
agentHarnesses: [],
149+
},
150+
compat: [],
151+
},
152+
],
153+
},
154+
owners: {
155+
...metadataSnapshot.owners,
156+
providers: new Map([["openai", ["openai"]]]),
157+
modelCatalogProviders: new Map([["openai", ["openai"]]]),
158+
},
159+
metrics: { ...metadataSnapshot.metrics, indexPluginCount: 1 },
160+
startup: { channelPluginIds: [], configuredDeferredChannelPluginIds: [], pluginIds: [] },
161+
};
162+
setCurrentPluginMetadataSnapshot(startupMetadataSnapshot, { config: cfg, env, workspaceDir });
163+
const discoverPlugins = vi.spyOn(pluginDiscovery, "discoverOpenClawPlugins");
114164
const agentDir = resolveAgentDir(cfg, "main", env);
115165
writePersistedAuthProfileStoreRaw(
116166
{
@@ -126,15 +176,6 @@ describe("Gateway prepared model runtime startup", () => {
126176
},
127177
agentDir,
128178
);
129-
const blockEventLoop = async () => {
130-
const stopAt = performance.now() + 1_500;
131-
while (performance.now() < stopAt) {
132-
// Deliberately model synchronous provider/plugin catalog work that starves timers.
133-
}
134-
return providerConfig;
135-
};
136-
providerMocks.staticCatalog.mockImplementation(blockEventLoop);
137-
providerMocks.liveCatalog.mockImplementation(blockEventLoop);
138179
const startupEvents: string[] = [];
139180
const healthServer = await listenHealthz(() => {
140181
startupEvents.push("health-served");
@@ -198,6 +239,7 @@ describe("Gateway prepared model runtime startup", () => {
198239
"startup-complete",
199240
]);
200241
console.info(`[gateway-startup-proof] ${startupEvents.join(" -> ")}`);
242+
expect(discoverPlugins).not.toHaveBeenCalled();
201243
expect(providerMocks.staticCatalog).not.toHaveBeenCalled();
202244
expect(providerMocks.liveCatalog).not.toHaveBeenCalled();
203245
},

‎src/plugins/provider-runtime.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type { ProviderSystemPromptContribution } from "../agents/system-prompt-c
1717
import type { ModelProviderConfig } from "../config/types.js";
1818
import type { OpenClawConfig } from "../config/types.openclaw.js";
1919
import type { UsageProviderId } from "../infra/provider-usage.types.js";
20+
import { getCurrentPluginMetadataSnapshot } from "./current-plugin-metadata-snapshot.js";
2021
import { normalizeProviderModelIdWithManifest } from "./manifest-model-id-normalization.js";
2122
import type { PluginManifestRecord } from "./manifest-registry.js";
2223
import { resolvePluginMetadataSnapshot } from "./plugin-metadata-snapshot.js";
@@ -998,11 +999,22 @@ export function resolveExternalAuthProfilesWithPlugins(params: {
998999
}): ProviderExternalAuthProfile[] {
9991000
const workspaceDir = params.workspaceDir ?? getActivePluginRegistryWorkspaceDirFromState();
10001001
const env = params.env ?? process.env;
1001-
const { manifestRegistry } = resolvePluginMetadataSnapshot({
1002-
config: params.config ?? {},
1003-
workspaceDir,
1002+
const config = params.config ?? {};
1003+
const currentMetadataSnapshot = getCurrentPluginMetadataSnapshot({
1004+
config,
10041005
env,
1006+
...(workspaceDir === undefined ? { allowWorkspaceScopedSnapshot: true } : { workspaceDir }),
10051007
});
1008+
const { manifestRegistry } =
1009+
currentMetadataSnapshot ?? resolvePluginMetadataSnapshot({ config, workspaceDir, env });
1010+
// A lifecycle-owned manifest is authoritative: no external-auth contracts means
1011+
// no provider registry discovery or runtime activation is needed for this overlay.
1012+
if (
1013+
currentMetadataSnapshot &&
1014+
!manifestRegistry.plugins.some((plugin) => plugin.contracts?.externalAuthProviders?.length)
1015+
) {
1016+
return [];
1017+
}
10061018
const externalAuthPluginIds = resolveExternalAuthProfileProviderPluginIds({
10071019
config: params.config,
10081020
workspaceDir,

0 commit comments

Comments
 (0)