Skip to content

Commit 8b5871e

Browse files
steipetePeter Steinberger
andauthored
fix(channels): reset metadata memos with plugin lifecycle (#117005)
Co-authored-by: Peter Steinberger <[email protected]>
1 parent 9e626c3 commit 8b5871e

6 files changed

Lines changed: 154 additions & 1 deletion

‎src/channels/bundled-channel-catalog-read.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ vi.mock("../infra/openclaw-root.js", () => ({
3333
}));
3434

3535
import { resolveBundledPluginsDir } from "../plugins/bundled-dir.js";
36+
import { clearPluginMetadataLifecycleCaches } from "../plugins/plugin-metadata-lifecycle.js";
3637
import { listBundledChannelCatalogEntries } from "./bundled-channel-catalog-read.js";
3738

3839
const tempDirs: string[] = [];
@@ -236,4 +237,43 @@ describe("listBundledChannelCatalogEntries", () => {
236237
const entries = listBundledChannelCatalogEntries();
237238
expect(entries.map((entry) => entry.id)).toContain("fallback-channel");
238239
});
240+
241+
it("reloads installed bundled package metadata after an explicit plugin lifecycle reset", () => {
242+
const root = seedRoot("bcr-package-lifecycle-");
243+
const extensionsRoot = path.join(root, "dist", "extensions");
244+
const packagePath = path.join(extensionsRoot, "alpha", "package.json");
245+
seedChannelPkg(packagePath, { id: "alpha", docsPath: "/channels/alpha", label: "Before" });
246+
useBundledPluginsDir(extensionsRoot);
247+
248+
expect(
249+
listBundledChannelCatalogEntries().find((entry) => entry.id === "alpha")?.channel.label,
250+
).toBe("Before");
251+
seedChannelPkg(packagePath, { id: "alpha", docsPath: "/channels/alpha", label: "After" });
252+
clearPluginMetadataLifecycleCaches();
253+
254+
expect(
255+
listBundledChannelCatalogEntries().find((entry) => entry.id === "alpha")?.channel.label,
256+
).toBe("After");
257+
});
258+
259+
it("discovers a generated catalog created after an explicit plugin lifecycle reset", () => {
260+
const root = seedRoot("bcr-generated-lifecycle-");
261+
useBundledPluginsDir(undefined);
262+
263+
expect(
264+
listBundledChannelCatalogEntries().find((entry) => entry.id === "generated"),
265+
).toBeUndefined();
266+
seedGeneratedChannelCatalog(root, {
267+
packageName: "@openclaw/generated",
268+
id: "generated",
269+
label: "Generated after reset",
270+
docsPath: "/channels/generated",
271+
blurb: "generated channel",
272+
});
273+
clearPluginMetadataLifecycleCaches();
274+
275+
expect(
276+
listBundledChannelCatalogEntries().find((entry) => entry.id === "generated")?.channel.label,
277+
).toBe("Generated after reset");
278+
});
239279
});

‎src/channels/bundled-channel-catalog-read.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { tryReadJsonSync } from "../infra/json-files.js";
1111
import { resolveOpenClawPackageRootSync } from "../infra/openclaw-root.js";
1212
import { resolveBundledPluginsDir } from "../plugins/bundled-dir.js";
1313
import type { PluginPackageChannel } from "../plugins/manifest.js";
14+
import { registerPluginMetadataProcessMemoLifecycleClear } from "../plugins/plugin-metadata-lifecycle.js";
1415

1516
type ChannelCatalogEntryLike = {
1617
openclaw?: {
@@ -29,6 +30,11 @@ const OFFICIAL_CHANNEL_CATALOG_RELATIVE_PATH = path.join("dist", "channel-catalo
2930
const officialCatalogFileCache = new Map<string, ChannelCatalogEntryLike[] | null>();
3031
const bundledPackageCatalogCache = new Map<string, ChannelCatalogEntryLike[] | null>();
3132

33+
registerPluginMetadataProcessMemoLifecycleClear(() => {
34+
officialCatalogFileCache.clear();
35+
bundledPackageCatalogCache.clear();
36+
});
37+
3238
function listPackageRoots(): string[] {
3339
// Source checkouts and packaged installs can resolve OpenClaw from different roots; scan both
3440
// once so channel metadata works in dev, linked packages, and published CLI layouts.

‎src/channels/plugins/catalog.test.ts‎

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Channel plugin catalog tests cover plugin catalog entries and metadata normalization.
2-
import { beforeEach, describe, expect, it, vi } from "vitest";
2+
import fs from "node:fs";
3+
import os from "node:os";
4+
import path from "node:path";
5+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
36
import type { PluginChannelCatalogEntry } from "../../plugins/channel-catalog-registry.js";
7+
import { clearPluginMetadataLifecycleCaches } from "../../plugins/plugin-metadata-lifecycle.js";
48

59
const listChannelCatalogEntriesMock = vi.hoisted(() =>
610
vi.fn<() => PluginChannelCatalogEntry[]>(() => []),
@@ -12,10 +16,37 @@ vi.mock("../../plugins/channel-catalog-registry.js", () => ({
1216

1317
import { getChannelPluginCatalogEntry } from "./catalog.js";
1418

19+
const tempDirs: string[] = [];
20+
1521
beforeEach(() => {
1622
listChannelCatalogEntriesMock.mockReset().mockReturnValue([]);
1723
});
1824

25+
afterEach(() => {
26+
vi.restoreAllMocks();
27+
for (const tempDir of tempDirs.splice(0)) {
28+
fs.rmSync(tempDir, { recursive: true, force: true });
29+
}
30+
});
31+
32+
function writeChannelCatalog(catalogPath: string, id: string, label: string): void {
33+
fs.mkdirSync(path.dirname(catalogPath), { recursive: true });
34+
fs.writeFileSync(
35+
catalogPath,
36+
JSON.stringify({
37+
entries: [
38+
{
39+
name: `@example/${id}`,
40+
openclaw: {
41+
channel: { id, label, selectionLabel: label, docsPath: `/channels/${id}`, blurb: id },
42+
install: { npmSpec: `@example/${id}` },
43+
},
44+
},
45+
],
46+
}),
47+
);
48+
}
49+
1950
describe("channel plugin catalog", () => {
2051
it("keeps third-party channel ids mapped with catalog install trust", () => {
2152
const options = {
@@ -74,4 +105,35 @@ describe("channel plugin catalog", () => {
74105
})?.origin,
75106
).toBe("bundled");
76107
});
108+
it("reloads external catalog entries after the explicit plugin metadata lifecycle reset", () => {
109+
const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-channel-external-catalog-"));
110+
tempDirs.push(root);
111+
const catalogPath = path.join(root, "catalog.json");
112+
const options = { catalogPaths: [catalogPath], workspaceDir: root, env: {} };
113+
114+
expect(getChannelPluginCatalogEntry("lifecycle-external", options)).toBeUndefined();
115+
writeChannelCatalog(catalogPath, "lifecycle-external", "After external install");
116+
clearPluginMetadataLifecycleCaches();
117+
118+
expect(getChannelPluginCatalogEntry("lifecycle-external", options)?.meta.label).toBe(
119+
"After external install",
120+
);
121+
});
122+
123+
it("reloads official generated catalog entries after the explicit plugin metadata lifecycle reset", () => {
124+
const root = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-channel-official-catalog-"));
125+
tempDirs.push(root);
126+
fs.writeFileSync(path.join(root, "package.json"), JSON.stringify({ name: "openclaw" }));
127+
vi.spyOn(process, "cwd").mockReturnValue(root);
128+
const catalogPath = path.join(root, "dist", "channel-catalog.json");
129+
const options = { catalogPaths: [path.join(root, "external.json")], env: {} };
130+
131+
expect(getChannelPluginCatalogEntry("lifecycle-official", options)).toBeUndefined();
132+
writeChannelCatalog(catalogPath, "lifecycle-official", "After official update");
133+
clearPluginMetadataLifecycleCaches();
134+
135+
expect(getChannelPluginCatalogEntry("lifecycle-official", options)?.meta.label).toBe(
136+
"After official update",
137+
);
138+
});
77139
});

‎src/channels/plugins/catalog.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
import type { OpenClawPackageManifest } from "../../plugins/manifest.js";
2424
import type { PluginPackageChannel, PluginPackageInstall } from "../../plugins/manifest.js";
2525
import { listOfficialExternalChannelCatalogEntries } from "../../plugins/official-external-plugin-catalog.js";
26+
import { registerPluginMetadataProcessMemoLifecycleClear } from "../../plugins/plugin-metadata-lifecycle.js";
2627
import type { PluginOrigin } from "../../plugins/plugin-origin.types.js";
2728
import { isRecord, resolveConfigDir, resolveUserPath } from "../../utils.js";
2829
import { buildManifestChannelMeta } from "./channel-meta.js";
@@ -117,6 +118,11 @@ const OFFICIAL_CHANNEL_CATALOG_RELATIVE_PATH = path.join("dist", "channel-catalo
117118
const officialCatalogEntriesByPath = new Map<string, ExternalCatalogEntry[] | null>();
118119
const externalCatalogEntriesByPath = new Map<string, ExternalCatalogEntry[] | null>();
119120

121+
registerPluginMetadataProcessMemoLifecycleClear(() => {
122+
officialCatalogEntriesByPath.clear();
123+
externalCatalogEntriesByPath.clear();
124+
});
125+
120126
type ManifestKey = typeof MANIFEST_KEY;
121127

122128
function parseCatalogEntries(raw: unknown): ExternalCatalogEntry[] {

‎src/media/channel-inbound-roots.fast-path.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { beforeEach, describe, expect, it, vi } from "vitest";
33
import type { MsgContext } from "../auto-reply/templating.js";
44
import type { OpenClawConfig } from "../config/types.js";
5+
import { clearPluginMetadataLifecycleCaches } from "../plugins/plugin-metadata-lifecycle.js";
56

67
const publicSurfaceLoaderMocks = vi.hoisted(() => ({
78
loadBundledPluginPublicArtifactModuleSync: vi.fn(),
@@ -173,4 +174,37 @@ describe("channel inbound roots fast path", () => {
173174
},
174175
);
175176
});
177+
178+
it("reloads a channel media contract installed after the explicit plugin lifecycle reset", () => {
179+
publicSurfaceLoaderMocks.loadBundledPluginPublicArtifactModuleSync.mockImplementation(
180+
({ artifactBasename, dirName }: { artifactBasename: string; dirName: string }) => {
181+
throw unableToResolve(dirName, artifactBasename);
182+
},
183+
);
184+
expect(
185+
resolveChannelInboundAttachmentRootsForChannel({
186+
cfg,
187+
channelId: "installedchat",
188+
accountId: "work",
189+
}),
190+
).toBeUndefined();
191+
192+
publicSurfaceLoaderMocks.loadBundledPluginPublicArtifactModuleSync.mockReturnValue({
193+
resolveInboundAttachmentRoots: ({ accountId }: { accountId?: string }) => [
194+
`/installed/${accountId}`,
195+
],
196+
});
197+
clearPluginMetadataLifecycleCaches();
198+
199+
expect(
200+
resolveChannelInboundAttachmentRootsForChannel({
201+
cfg,
202+
channelId: "installedchat",
203+
accountId: "work",
204+
}),
205+
).toEqual(["/installed/work"]);
206+
expect(
207+
publicSurfaceLoaderMocks.loadBundledPluginPublicArtifactModuleSync,
208+
).toHaveBeenCalledTimes(2);
209+
});
176210
});

‎src/media/channel-inbound-roots.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { normalizeOptionalLowercaseString } from "@openclaw/normalization-core/string-coerce";
33
import type { MsgContext } from "../auto-reply/templating.js";
44
import type { OpenClawConfig } from "../config/types.js";
5+
import { registerPluginMetadataProcessMemoLifecycleClear } from "../plugins/plugin-metadata-lifecycle.js";
56
import { loadBundledPluginPublicArtifactModuleSync } from "../plugins/public-surface-loader.js";
67

78
type ChannelMediaContractApi = {
@@ -18,6 +19,10 @@ type ChannelMediaRootResolver = keyof ChannelMediaContractApi;
1819

1920
const mediaContractApiByChannel = new Map<string, ChannelMediaContractApi | null>();
2021

22+
registerPluginMetadataProcessMemoLifecycleClear(() => {
23+
mediaContractApiByChannel.clear();
24+
});
25+
2126
function loadChannelMediaContractApi(
2227
channelId: string,
2328
resolver: ChannelMediaRootResolver,

0 commit comments

Comments
 (0)