Skip to content

Commit 6ac11fb

Browse files
authored
fix(plugins): prevent bundled discovery loss and out-of-scope activation (#114547)
* fix(plugins): prevent missing and out-of-scope plugin activation * fix(plugins): clear complete metadata on lifecycle invalidation * fix(plugins): clear metadata without lifecycle import cycles * test(agents): preserve plugin metadata lifecycle in model mock * test(agents): align Ollama auth with the installed plugin index * test(auto-reply): preserve plugin metadata lifecycle in model mock * test(auto-reply): keep plugin metadata mock within lint limit * test(auto-reply): align plugin metadata fixture with lint and format
1 parent 17ed69e commit 6ac11fb

13 files changed

Lines changed: 307 additions & 115 deletions

‎src/agents/model-auth.test.ts‎

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Verifies provider auth resolution, synthetic auth, and auth header behavior.
2+
import { fileURLToPath } from "node:url";
23
import type { Model } from "openclaw/plugin-sdk/llm";
34
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
45
import type { ModelProviderConfig } from "../config/config.js";
@@ -17,11 +18,34 @@ import {
1718
} from "./provider-request-config.js";
1819

1920
vi.mock("../plugins/plugin-registry.js", () => ({
20-
loadPluginRegistrySnapshotWithMetadata: () => ({
21-
source: "derived",
22-
snapshot: { plugins: [] },
23-
diagnostics: [],
24-
}),
21+
loadPluginRegistrySnapshotWithMetadata: () => {
22+
const rootDir = fileURLToPath(new URL("../../extensions/ollama/", import.meta.url));
23+
return {
24+
source: "derived",
25+
snapshot: {
26+
plugins: [
27+
{
28+
pluginId: "ollama",
29+
manifestPath: fileURLToPath(
30+
new URL("../../extensions/ollama/openclaw.plugin.json", import.meta.url),
31+
),
32+
manifestHash: "ollama-model-auth-fixture",
33+
rootDir,
34+
origin: "bundled",
35+
enabled: true,
36+
startup: {
37+
sidecar: false,
38+
memory: false,
39+
deferConfiguredChannelFullLoadUntilAfterListen: false,
40+
agentHarnesses: [],
41+
},
42+
compat: [],
43+
},
44+
],
45+
},
46+
diagnostics: [],
47+
};
48+
},
2549
loadPluginManifestRegistryForPluginRegistry: () => ({
2650
diagnostics: [],
2751
plugins: [

‎src/agents/model-selection.plugin-runtime.test.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@ vi.mock("./provider-model-normalization.runtime.js", () => ({
2626
normalizeProviderModelIdWithPluginMock(params),
2727
}));
2828

29-
vi.mock("../plugins/current-plugin-metadata-snapshot.js", () => ({
30-
getCurrentPluginMetadataSnapshot: getCurrentPluginMetadataSnapshotMock,
31-
}));
29+
vi.mock("../plugins/current-plugin-metadata-snapshot.js", async () => {
30+
const { clearCurrentPluginMetadataSnapshot } =
31+
await import("../plugins/current-plugin-metadata-state.js");
32+
return {
33+
clearCurrentPluginMetadataSnapshot,
34+
getCurrentPluginMetadataSnapshot: getCurrentPluginMetadataSnapshotMock,
35+
};
36+
});
3237

3338
vi.mock("./model-catalog.runtime.js", () => ({
3439
loadManifestModelCatalog: () => [],

‎src/auto-reply/reply/commands-models.test.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ import {
1212
import { buildModelsProviderData, handleModelsCommand } from "./commands-models.js";
1313
import type { HandleCommandsParams } from "./commands-types.js";
1414

15-
const modelCatalogMocks = vi.hoisted(() => ({
16-
loadModelCatalog: vi.fn(),
17-
}));
15+
const modelCatalogMocks = vi.hoisted(() => ({ loadModelCatalog: vi.fn() }));
1816

1917
const modelAuthLabelMocks = vi.hoisted(() => ({
2018
resolveModelAuthLabel: vi.fn<(params: unknown) => string | undefined>(() => undefined),
@@ -138,7 +136,10 @@ vi.mock("../../agents/provider-model-normalization.runtime.js", () => ({
138136
normalizeProviderModelIdWithRuntimeMock(params),
139137
}));
140138

141-
vi.mock("../../plugins/current-plugin-metadata-snapshot.js", () => ({
139+
vi.mock("../../plugins/current-plugin-metadata-snapshot.js", async () => ({
140+
clearCurrentPluginMetadataSnapshot: (
141+
await import("../../plugins/current-plugin-metadata-state.js")
142+
).clearCurrentPluginMetadataSnapshot,
142143
getCurrentPluginMetadataSnapshot: () => pluginMetadataMocks.snapshot,
143144
}));
144145

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from "./current-plugin-metadata-snapshot.js";
1313
import { resolveInstalledPluginIndexPolicyHash } from "./installed-plugin-index-policy.js";
1414
import { writePersistedInstalledPluginIndexSync } from "./installed-plugin-index-store.js";
15+
import { clearPluginMetadataLifecycleCaches } from "./plugin-metadata-lifecycle.js";
1516
import type { PluginMetadataSnapshot } from "./plugin-metadata-snapshot.js";
1617

1718
function createSnapshot(
@@ -307,6 +308,15 @@ describe("current plugin metadata snapshot", () => {
307308
expect(getCurrentPluginMetadataSnapshot()).toBeUndefined();
308309
});
309310

311+
it("clears the complete current snapshot when its metadata lifecycle is invalidated", () => {
312+
const config = { plugins: { allow: ["demo"] } };
313+
setCurrentPluginMetadataSnapshot(createSnapshot({ config }), { config });
314+
315+
clearPluginMetadataLifecycleCaches();
316+
317+
expect(getCurrentPluginMetadataSnapshot({ config })).toBeUndefined();
318+
});
319+
310320
it("keeps derived registry snapshots as the current process snapshot", () => {
311321
const persisted = createSnapshot({ registrySource: "persisted" });
312322
const derived = createSnapshot({ registrySource: "derived" });

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

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { setCurrentManifestModelIdNormalizationRecords } from "@openclaw/model-catalog-core/provider-model-id-normalization";
33
import type { OpenClawConfig } from "../config/types.openclaw.js";
44
import {
5-
clearCurrentPluginMetadataSnapshotState,
5+
currentPluginMetadataConfigIdentityCache,
66
getCurrentPluginMetadataSnapshotState,
77
setCurrentPluginMetadataSnapshotState,
88
} from "./current-plugin-metadata-state.js";
@@ -11,19 +11,14 @@ import {
1111
resolvePluginControlPlaneFingerprint,
1212
type ResolvePluginControlPlaneContextParams,
1313
} from "./plugin-control-plane-context.js";
14-
import { registerPluginMetadataProcessMemoLifecycleClear } from "./plugin-metadata-lifecycle.js";
1514
import type {
1615
PluginMetadataSnapshot,
1716
PluginMetadataSnapshotPluginIdScope,
1817
} from "./plugin-metadata-snapshot.types.js";
1918
import { normalizePluginIdScope, serializePluginIdScope } from "./plugin-scope.js";
2019

2120
type CurrentPluginMetadataSnapshotState = ReturnType<typeof getCurrentPluginMetadataSnapshotState>;
22-
let currentPluginMetadataConfigIdentityCache = new WeakSet<OpenClawConfig>();
23-
24-
registerPluginMetadataProcessMemoLifecycleClear(() => {
25-
setCurrentManifestModelIdNormalizationRecords(undefined);
26-
});
21+
export { clearCurrentPluginMetadataSnapshot } from "./current-plugin-metadata-state.js";
2722

2823
function resolvePluginMetadataControlPlaneFingerprint(
2924
config?: OpenClawConfig,
@@ -35,12 +30,6 @@ function resolvePluginMetadataControlPlaneFingerprint(
3530
});
3631
}
3732

38-
export function isReusableCurrentPluginMetadataSnapshot(
39-
_snapshot: PluginMetadataSnapshot,
40-
): boolean {
41-
return true;
42-
}
43-
4433
// Single-slot Gateway-owned handoff. Replace or clear it at lifecycle boundaries;
4534
// never accumulate historical metadata snapshots here.
4635
export function setCurrentPluginMetadataSnapshot(
@@ -52,7 +41,7 @@ export function setCurrentPluginMetadataSnapshot(
5241
workspaceDir?: string;
5342
} = {},
5443
): void {
55-
currentPluginMetadataConfigIdentityCache = new WeakSet();
44+
currentPluginMetadataConfigIdentityCache.clear();
5645
const compatiblePolicyHashes = snapshot
5746
? options.compatibleConfigs?.map((config) => resolveInstalledPluginIndexPolicyHash(config))
5847
: undefined;
@@ -117,20 +106,14 @@ export function setCurrentPluginMetadataSnapshot(
117106
}
118107
}
119108

120-
export function clearCurrentPluginMetadataSnapshot(): void {
121-
currentPluginMetadataConfigIdentityCache = new WeakSet();
122-
setCurrentManifestModelIdNormalizationRecords(undefined);
123-
clearCurrentPluginMetadataSnapshotState();
124-
}
125-
126109
export function captureCurrentPluginMetadataSnapshotState(): CurrentPluginMetadataSnapshotState {
127110
return getCurrentPluginMetadataSnapshotState();
128111
}
129112

130113
export function restoreCurrentPluginMetadataSnapshotState(
131114
state: CurrentPluginMetadataSnapshotState,
132115
): void {
133-
currentPluginMetadataConfigIdentityCache = new WeakSet();
116+
currentPluginMetadataConfigIdentityCache.clear();
134117
const snapshot = state.snapshot as PluginMetadataSnapshot | undefined;
135118
const defaultDiscoveryConfigFingerprint = snapshot
136119
? resolvePluginMetadataControlPlaneFingerprint(

‎src/plugins/current-plugin-metadata-state.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
11
// Holds current plugin metadata snapshots for process-scoped consumers.
2+
import { setCurrentManifestModelIdNormalizationRecords } from "@openclaw/model-catalog-core/provider-model-id-normalization";
3+
import type { OpenClawConfig } from "../config/types.openclaw.js";
4+
25
let currentPluginMetadataSnapshot: unknown;
36
let currentPluginMetadataSnapshotConfigFingerprint: string | undefined;
47
let currentPluginMetadataSnapshotCompatiblePolicyHashes: readonly string[] | undefined;
58
let currentPluginMetadataSnapshotCompatibleConfigFingerprints: readonly string[] | undefined;
9+
let currentPluginMetadataConfigIdentities = new WeakSet<OpenClawConfig>();
10+
11+
/** Owns config identity reuse for the current immutable metadata snapshot. */
12+
export const currentPluginMetadataConfigIdentityCache = {
13+
add(config: OpenClawConfig): void {
14+
currentPluginMetadataConfigIdentities.add(config);
15+
},
16+
clear(): void {
17+
currentPluginMetadataConfigIdentities = new WeakSet();
18+
},
19+
has(config: OpenClawConfig): boolean {
20+
return currentPluginMetadataConfigIdentities.has(config);
21+
},
22+
};
623

724
/** Stores the process-current plugin metadata snapshot and compatible config fingerprints. */
825
export function setCurrentPluginMetadataSnapshotState(
@@ -22,13 +39,20 @@ export function setCurrentPluginMetadataSnapshotState(
2239
}
2340

2441
/** Clears the process-current plugin metadata snapshot. */
25-
export function clearCurrentPluginMetadataSnapshotState(): void {
42+
function clearCurrentPluginMetadataSnapshotState(): void {
2643
currentPluginMetadataSnapshot = undefined;
2744
currentPluginMetadataSnapshotConfigFingerprint = undefined;
2845
currentPluginMetadataSnapshotCompatiblePolicyHashes = undefined;
2946
currentPluginMetadataSnapshotCompatibleConfigFingerprints = undefined;
3047
}
3148

49+
/** Clears the snapshot, its identity cache, and process-wide model normalization. */
50+
export function clearCurrentPluginMetadataSnapshot(): void {
51+
currentPluginMetadataConfigIdentityCache.clear();
52+
setCurrentManifestModelIdNormalizationRecords(undefined);
53+
clearCurrentPluginMetadataSnapshotState();
54+
}
55+
3256
/** Returns the process-current plugin metadata snapshot state. */
3357
export function getCurrentPluginMetadataSnapshotState(): {
3458
snapshot: unknown;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/** Coordinates plugin metadata snapshot and process memo cache lifecycle resets. */
2-
import { clearCurrentPluginMetadataSnapshotState } from "./current-plugin-metadata-state.js";
2+
import { clearCurrentPluginMetadataSnapshot } from "./current-plugin-metadata-state.js";
33

44
const pluginMetadataProcessMemoClears = new Set<() => void>();
55

@@ -12,7 +12,7 @@ export function registerPluginMetadataProcessMemoLifecycleClear(
1212

1313
/** Clears plugin metadata snapshots and registered process memo caches. */
1414
export function clearPluginMetadataLifecycleCaches(): void {
15-
clearCurrentPluginMetadataSnapshotState();
15+
clearCurrentPluginMetadataSnapshot();
1616
for (const clearProcessMemo of pluginMetadataProcessMemoClears) {
1717
clearProcessMemo();
1818
}

0 commit comments

Comments
 (0)