Skip to content

Commit 3dd18cc

Browse files
authored
fix(models): model browsing fails after automatic plugin activation (#130481)
* fix(models): preserve Gateway metadata in catalog workers * docs: leave catalog release notes to release automation
1 parent cb1d892 commit 3dd18cc

5 files changed

Lines changed: 106 additions & 26 deletions

‎src/agents/prepared-model-catalog-worker.integration.test.ts‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
loadGatewayModelCatalogSnapshot,
1111
loadPreparedGatewayModelCatalogSnapshot,
1212
} from "../gateway/server-model-catalog.js";
13+
import { loadPluginMetadataSnapshot } from "../plugins/plugin-metadata-snapshot.js";
1314
import { closeOpenClawAgentDatabasesForTest } from "../state/openclaw-agent-db.js";
1415
import { OPENAI_CODEX_DEFAULT_PROFILE_ID } from "./auth-profiles/constants.js";
1516
import { getRuntimeExternalCliProfileIds } from "./auth-profiles/runtime-external-profile-references.js";
@@ -201,7 +202,10 @@ module.exports = {
201202
async function createStaticSnapshot(
202203
spinMs: number,
203204
envOverride: NodeJS.ProcessEnv = {},
204-
options?: { hydrateExternalCliProviderIds?: readonly string[] },
205+
options?: {
206+
hydrateExternalCliProviderIds?: readonly string[];
207+
metadataWorkspace?: "gateway" | "none" | "activation";
208+
},
205209
) {
206210
const root = tempDirs.make("openclaw-model-catalog-worker-");
207211
const stateDir = path.join(root, "state");
@@ -293,6 +297,20 @@ async function createStaticSnapshot(
293297
30_000,
294298
"static",
295299
() => current,
300+
false,
301+
undefined,
302+
options?.metadataWorkspace
303+
? loadPluginMetadataSnapshot({
304+
config:
305+
options.metadataWorkspace === "activation"
306+
? { ...config, plugins: { ...config.plugins, entries: {} } }
307+
: config,
308+
env,
309+
...(options.metadataWorkspace === "gateway"
310+
? { workspaceDir: path.join(root, "gateway-workspace") }
311+
: {}),
312+
})
313+
: undefined,
296314
).pending;
297315
return {
298316
agentDir,
@@ -314,6 +332,32 @@ async function waitForMarker(marker: string): Promise<void> {
314332
}
315333

316334
describe("prepared model catalog worker boundary", () => {
335+
it.each([
336+
["gateway", "catalog"],
337+
["gateway", "auth-refresh"],
338+
["none", "catalog"],
339+
["none", "auth-refresh"],
340+
["activation", "catalog"],
341+
["activation", "auth-refresh"],
342+
] as const)(
343+
"keeps %s metadata discovery scope with %s first",
344+
async (metadataWorkspace, first) => {
345+
const fixture = await createStaticSnapshot(0, {}, { metadataWorkspace });
346+
if (first === "auth-refresh") {
347+
const auth = await loadPreparedModelRuntimeAuth(fixture.snapshot, {
348+
providerIds: [PROVIDER_ID],
349+
});
350+
expect(auth?.authStore.profiles[EXTERNAL_AUTH_PROFILE_ID]).toMatchObject({
351+
access: "v1:A",
352+
});
353+
}
354+
const catalog = await fixture.snapshot.loadFullModelCatalog?.();
355+
expect(catalog?.entries).toContainEqual(
356+
expect.objectContaining({ provider: PROVIDER_ID, id: "plugin-generation-v1" }),
357+
);
358+
},
359+
);
360+
317361
it("publishes account-scoped harness models only in the full catalog", async () => {
318362
const fixture = await createStaticSnapshot(0);
319363

‎src/agents/prepared-model-catalog-worker.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export type PreparedModelCatalogWorkerInput = Readonly<{
2525
input: PreparedModelRuntimeInput;
2626
authStore: AuthProfileStore;
2727
providerIds: readonly string[];
28+
pluginMetadataSnapshot: Omit<PluginMetadataSnapshot, "normalizePluginId">;
2829
}>;
2930

3031
export type PreparedModelWorkerRequest =
@@ -115,6 +116,8 @@ export function createPreparedModelCatalogWorkerInput(params: {
115116
};
116117
const authStore = cloneAuthProfileStore(params.agentFacts.authStore);
117118
const providerIds = [...params.agentFacts.providerIds];
119+
const { normalizePluginId: _normalizePluginId, ...pluginMetadataSnapshot } =
120+
params.pluginMetadataSnapshot;
118121
return {
119122
kind: "catalog",
120123
generationFingerprint: fingerprintPreparedModelCatalogGeneration({
@@ -126,6 +129,7 @@ export function createPreparedModelCatalogWorkerInput(params: {
126129
input,
127130
authStore,
128131
providerIds,
132+
pluginMetadataSnapshot,
129133
};
130134
}
131135

‎src/agents/prepared-model-catalog.worker.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/** Worker-thread entrypoint for complete model-catalog discovery. */
22
import { parentPort, workerData } from "node:worker_threads";
3+
import { restorePluginMetadataSnapshot } from "../plugins/plugin-metadata-snapshot.js";
34
import { withPluginRuntimeGenerationScope } from "../plugins/runtime/generation-scope.js";
45
import { resolveRuntimeSyntheticAuthProviderRefs } from "../plugins/synthetic-auth.runtime.js";
56
import {
@@ -77,7 +78,17 @@ function refreshAuthStore(params: {
7778

7879
async function prepareWorkerGeneration(value: PreparedModelCatalogWorkerInput) {
7980
const { prepareWorkspaceBuildGroup } = await import("./prepared-model-runtime.facts.js");
80-
const prepared = await prepareWorkspaceBuildGroup([value.input], "live");
81+
// Rediscovery under agent workspaces or runtime activation overlays loses the owner's
82+
// metadata generation. Transfer its facts and restore only process-local behavior.
83+
const metadata = restorePluginMetadataSnapshot(value.pluginMetadataSnapshot);
84+
const prepared = await prepareWorkspaceBuildGroup(
85+
[value.input],
86+
"live",
87+
{},
88+
undefined,
89+
undefined,
90+
metadata,
91+
);
8192
const agentFacts = prepared.agentFacts[0];
8293
if (!agentFacts) {
8394
throw new Error("prepared model catalog worker produced no agent facts");

‎src/plugins/plugin-metadata-snapshot.test.ts‎

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
completePluginMetadataSnapshot,
1414
loadPluginMetadataSnapshot,
1515
resolvePluginMetadataSnapshot,
16+
restorePluginMetadataSnapshot,
1617
} from "./plugin-metadata-snapshot.js";
1718

1819
const { loadPluginRegistrySnapshotWithMetadata, loadPluginManifestRegistryForInstalledIndex } =
@@ -684,25 +685,35 @@ describe("plugin metadata snapshot", () => {
684685
});
685686
});
686687

687-
it("freezes a cloned index instead of caller-owned records", () => {
688-
const index = makeIndex();
689-
loadPluginRegistrySnapshotWithMetadata.mockReturnValue({
690-
source: "provided",
691-
snapshot: index,
692-
diagnostics: [],
693-
});
694-
695-
const snapshot = loadPluginMetadataSnapshot({ config: {}, env: {}, index });
696-
const callerRecord = index.plugins[0];
697-
const snapshotRecord = snapshot.index.plugins[0];
698-
if (!callerRecord || !snapshotRecord) {
699-
throw new Error("expected metadata records");
700-
}
688+
it.each([false, true])(
689+
"freezes a cloned index instead of caller-owned records (worker: %s)",
690+
(worker) => {
691+
const index = makeIndex();
692+
loadPluginRegistrySnapshotWithMetadata.mockReturnValue({
693+
source: "provided",
694+
snapshot: index,
695+
diagnostics: [],
696+
});
701697

702-
callerRecord.pluginId = "caller-mutated";
703-
expect(snapshotRecord.pluginId).toBe("demo");
704-
expect(() => {
705-
snapshotRecord.pluginId = "snapshot-mutated";
706-
}).toThrow();
707-
});
698+
const loaded = loadPluginMetadataSnapshot({ config: {}, env: {}, index });
699+
const { normalizePluginId: _normalizePluginId, ...transfer } = loaded;
700+
const snapshot = worker ? restorePluginMetadataSnapshot(structuredClone(transfer)) : loaded;
701+
expect(snapshot.normalizePluginId(" DEMO ")).toBe("demo");
702+
expect(snapshot.owners.providers.get("demo")).toEqual(["demo"]);
703+
expect(() => (snapshot.owners.providers as Map<string, string[]>).set("other", [])).toThrow(
704+
"Plugin metadata snapshots are immutable",
705+
);
706+
const callerRecord = index.plugins[0];
707+
const snapshotRecord = snapshot.index.plugins[0];
708+
if (!callerRecord || !snapshotRecord) {
709+
throw new Error("expected metadata records");
710+
}
711+
712+
callerRecord.pluginId = "caller-mutated";
713+
expect(snapshotRecord.pluginId).toBe("demo");
714+
expect(() => {
715+
snapshotRecord.pluginId = "snapshot-mutated";
716+
}).toThrow();
717+
},
718+
);
708719
});

‎src/plugins/plugin-metadata-snapshot.ts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,18 @@ function indexesMatch(
122122
);
123123
}
124124

125+
/** Restores process-local behavior and immutability after a snapshot crosses a worker boundary. */
126+
export function restorePluginMetadataSnapshot(
127+
snapshot: Omit<PluginMetadataSnapshot, "normalizePluginId">,
128+
): PluginMetadataSnapshot {
129+
return freezeSnapshotValue({
130+
...snapshot,
131+
normalizePluginId: createPluginRegistryIdNormalizer(snapshot.index, {
132+
manifestRegistry: snapshot.manifestRegistry,
133+
}),
134+
});
135+
}
136+
125137
function resolvePluginMetadataSnapshotPluginIds(params: {
126138
index: InstalledPluginIndex;
127139
params: LoadPluginMetadataSnapshotParams;
@@ -308,7 +320,7 @@ export function loadPluginMetadataSnapshot(
308320
);
309321
return measureDiagnosticsTimelineSpanSync(
310322
"plugins.metadata.freeze",
311-
() => freezeSnapshotValue(snapshot),
323+
() => restorePluginMetadataSnapshot(snapshot),
312324
{
313325
phase: activeTimelineSpan?.phase ?? "startup",
314326
config: params.config,
@@ -417,7 +429,7 @@ export function resolvePluginMetadataSnapshot(
417429

418430
function loadPluginMetadataSnapshotImpl(
419431
params: LoadPluginMetadataSnapshotParams,
420-
): PluginMetadataSnapshot {
432+
): Omit<PluginMetadataSnapshot, "normalizePluginId"> {
421433
const totalStartedAt = performance.now();
422434
const registryStartedAt = performance.now();
423435
const registryResult = loadPluginRegistrySnapshotWithMetadata({
@@ -448,7 +460,6 @@ function loadPluginMetadataSnapshotImpl(
448460
includeDisabled: true,
449461
});
450462
const manifestRegistryMs = performance.now() - manifestStartedAt;
451-
const normalizePluginId = createPluginRegistryIdNormalizer(index, { manifestRegistry });
452463
const byPluginId = new Map(manifestRegistry.plugins.map((plugin) => [plugin.id, plugin]));
453464
const ownerMapsStartedAt = performance.now();
454465
const owners = buildPluginMetadataOwnerMaps(manifestRegistry.plugins);
@@ -473,7 +484,6 @@ function loadPluginMetadataSnapshotImpl(
473484
plugins: manifestRegistry.plugins,
474485
diagnostics: manifestRegistry.diagnostics,
475486
byPluginId,
476-
normalizePluginId,
477487
owners,
478488
metrics: {
479489
registrySnapshotMs,

0 commit comments

Comments
 (0)