惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tailwind CSS Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
雷峰网
雷峰网
量子位
有赞技术团队
有赞技术团队
阮一峰的网络日志
阮一峰的网络日志
The Cloudflare Blog
博客园 - Franky
罗磊的独立博客
宝玉的分享
宝玉的分享
博客园_首页
腾讯CDC
The GitHub Blog
The GitHub Blog
D
DataBreaches.Net
IT之家
IT之家
D
Docker
Microsoft Security Blog
Microsoft Security Blog
博客园 - 司徒正美
V
V2EX
月光博客
月光博客
N
Netflix TechBlog - Medium
爱范儿
爱范儿
I
InfoQ
P
Proofpoint News Feed

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
perf(plugins): memoize metadata snapshots narrowly · open...
Kaspre · 2026-05-14 · via Recent Commits to openclaw:main

@@ -0,0 +1,211 @@

1+

import fs from "node:fs";

2+

import os from "node:os";

3+

import path from "node:path";

4+

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

5+

import type { InstalledPluginIndex } from "./installed-plugin-index.js";

6+

import type { PluginManifestRecord, PluginManifestRegistry } from "./manifest-registry.js";

7+

import {

8+

clearLoadPluginMetadataSnapshotMemo,

9+

loadPluginMetadataSnapshot,

10+

} from "./plugin-metadata-snapshot.js";

11+12+

const loadPluginRegistrySnapshotWithMetadata = vi.hoisted(() => vi.fn());

13+

const loadPluginManifestRegistryForInstalledIndex = vi.hoisted(() => vi.fn());

14+15+

vi.mock("./plugin-registry.js", async (importOriginal) => {

16+

const actual = await importOriginal<typeof import("./plugin-registry.js")>();

17+

return {

18+

...actual,

19+

loadPluginRegistrySnapshotWithMetadata: (params: unknown) =>

20+

loadPluginRegistrySnapshotWithMetadata(params),

21+

};

22+

});

23+24+

vi.mock("./manifest-registry-installed.js", async (importOriginal) => {

25+

const actual = await importOriginal<typeof import("./manifest-registry-installed.js")>();

26+

return {

27+

...actual,

28+

loadPluginManifestRegistryForInstalledIndex: (params: unknown) =>

29+

loadPluginManifestRegistryForInstalledIndex(params),

30+

};

31+

});

32+33+

const tempDirs: string[] = [];

34+35+

function tempStateDir(): string {

36+

const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-metadata-memo-"));

37+

tempDirs.push(dir);

38+

return dir;

39+

}

40+41+

function touchPersistedIndex(stateDir: string, value = 1): void {

42+

const indexPath = path.join(stateDir, "plugins", "installs.json");

43+

fs.mkdirSync(path.dirname(indexPath), { recursive: true });

44+

fs.writeFileSync(indexPath, JSON.stringify({ value }));

45+

}

46+47+

function makeIndex(pluginId = "demo"): InstalledPluginIndex {

48+

return {

49+

version: 1,

50+

hostContractVersion: "test",

51+

compatRegistryVersion: "test",

52+

migrationVersion: 1,

53+

policyHash: "test",

54+

generatedAtMs: 1,

55+

installRecords: {},

56+

diagnostics: [],

57+

plugins: [

58+

{

59+

pluginId,

60+

manifestPath: `/plugins/${pluginId}/openclaw.plugin.json`,

61+

manifestHash: `${pluginId}-manifest`,

62+

rootDir: `/plugins/${pluginId}`,

63+

origin: "global",

64+

enabled: true,

65+

startup: {

66+

sidecar: false,

67+

memory: false,

68+

deferConfiguredChannelFullLoadUntilAfterListen: false,

69+

agentHarnesses: [],

70+

},

71+

compat: [],

72+

},

73+

],

74+

};

75+

}

76+77+

function makeManifestRegistry(pluginId = "demo"): PluginManifestRegistry {

78+

const plugin: PluginManifestRecord = {

79+

id: pluginId,

80+

name: pluginId,

81+

channels: [],

82+

providers: [pluginId],

83+

cliBackends: [],

84+

skills: [],

85+

hooks: [],

86+

commandAliases: [{ name: `${pluginId}-command` }],

87+

rootDir: `/plugins/${pluginId}`,

88+

source: `/plugins/${pluginId}/index.js`,

89+

manifestPath: `/plugins/${pluginId}/openclaw.plugin.json`,

90+

origin: "global",

91+

};

92+

return { plugins: [plugin], diagnostics: [] };

93+

}

94+95+

function firstPlugin(snapshot: ReturnType<typeof loadPluginMetadataSnapshot>): PluginManifestRecord {

96+

const plugin = snapshot.plugins[0];

97+

if (!plugin) {

98+

throw new Error("expected memo test fixture plugin");

99+

}

100+

return plugin;

101+

}

102+103+

function firstCommandAlias(

104+

plugin: PluginManifestRecord,

105+

): NonNullable<PluginManifestRecord["commandAliases"]>[number] {

106+

const commandAlias = plugin.commandAliases?.[0];

107+

if (!commandAlias) {

108+

throw new Error("expected memo test fixture command alias");

109+

}

110+

return commandAlias;

111+

}

112+113+

describe("loadPluginMetadataSnapshot process memo", () => {

114+

beforeEach(() => {

115+

clearLoadPluginMetadataSnapshotMemo();

116+

loadPluginRegistrySnapshotWithMetadata.mockReset();

117+

loadPluginManifestRegistryForInstalledIndex.mockReset();

118+

loadPluginManifestRegistryForInstalledIndex.mockReturnValue(makeManifestRegistry());

119+

});

120+121+

afterEach(() => {

122+

clearLoadPluginMetadataSnapshotMemo();

123+

for (const dir of tempDirs.splice(0)) {

124+

fs.rmSync(dir, { recursive: true, force: true });

125+

}

126+

});

127+128+

it("reuses persisted metadata snapshots for repeated process lookups", () => {

129+

const stateDir = tempStateDir();

130+

touchPersistedIndex(stateDir);

131+

loadPluginRegistrySnapshotWithMetadata.mockReturnValue({

132+

source: "persisted",

133+

snapshot: makeIndex(),

134+

diagnostics: [],

135+

});

136+137+

const first = loadPluginMetadataSnapshot({ config: {}, env: {}, stateDir });

138+

const firstRecord = firstPlugin(first);

139+

firstRecord.providers.push("first-mutated");

140+

firstCommandAlias(firstRecord).name = "first-command-mutated";

141+

const second = loadPluginMetadataSnapshot({ config: {}, env: {}, stateDir });

142+

const secondRecord = firstPlugin(second);

143+

secondRecord.providers.push("second-mutated");

144+

firstCommandAlias(secondRecord).name = "second-command-mutated";

145+

const third = loadPluginMetadataSnapshot({ config: {}, env: {}, stateDir });

146+147+

expect(loadPluginRegistrySnapshotWithMetadata).toHaveBeenCalledOnce();

148+

expect(loadPluginManifestRegistryForInstalledIndex).toHaveBeenCalledOnce();

149+

expect(third.plugins[0]?.providers).toEqual(["demo"]);

150+

expect(third.plugins[0]?.commandAliases?.[0]?.name).toBe("demo-command");

151+

expect(second.manifestRegistry.plugins[0]).toBe(second.plugins[0]);

152+

expect(second.byPluginId.get("demo")).toBe(second.plugins[0]);

153+

});

154+155+

it("memoizes policy-stale derived snapshots used by validation callers", () => {

156+

const stateDir = tempStateDir();

157+

touchPersistedIndex(stateDir);

158+

loadPluginRegistrySnapshotWithMetadata.mockReturnValue({

159+

source: "derived",

160+

snapshot: makeIndex(),

161+

diagnostics: [

162+

{

163+

level: "warn",

164+

code: "persisted-registry-stale-policy",

165+

message: "policy changed",

166+

},

167+

],

168+

});

169+170+

loadPluginMetadataSnapshot({ config: {}, env: {}, stateDir });

171+

loadPluginMetadataSnapshot({ config: {}, env: {}, stateDir });

172+173+

expect(loadPluginRegistrySnapshotWithMetadata).toHaveBeenCalledOnce();

174+

});

175+176+

it.each([

177+

["persisted-registry-missing", undefined],

178+

["persisted-registry-stale-source", undefined],

179+

["persisted-registry-disabled", undefined],

180+

[undefined, { preferPersisted: false }],

181+

])("does not memoize derived snapshots for %s diagnostics", (code, options) => {

182+

const stateDir = tempStateDir();

183+

touchPersistedIndex(stateDir);

184+

loadPluginRegistrySnapshotWithMetadata.mockReturnValue({

185+

source: "derived",

186+

snapshot: makeIndex(),

187+

diagnostics: code ? [{ level: "warn", code, message: "registry not reusable" }] : [],

188+

});

189+190+

loadPluginMetadataSnapshot({ config: {}, env: {}, stateDir, ...options });

191+

loadPluginMetadataSnapshot({ config: {}, env: {}, stateDir, ...options });

192+193+

expect(loadPluginRegistrySnapshotWithMetadata).toHaveBeenCalledTimes(2);

194+

});

195+196+

it("refreshes when the persisted registry file changes", () => {

197+

const stateDir = tempStateDir();

198+

touchPersistedIndex(stateDir, 1);

199+

loadPluginRegistrySnapshotWithMetadata.mockReturnValue({

200+

source: "persisted",

201+

snapshot: makeIndex(),

202+

diagnostics: [],

203+

});

204+205+

loadPluginMetadataSnapshot({ config: {}, env: {}, stateDir });

206+

touchPersistedIndex(stateDir, 22);

207+

loadPluginMetadataSnapshot({ config: {}, env: {}, stateDir });

208+209+

expect(loadPluginRegistrySnapshotWithMetadata).toHaveBeenCalledTimes(2);

210+

});

211+

});