Skip to content

Commit d94c755

Browse files
authored
fix(skills): invalidate plugin skill memo when ACP availability changes (#121181)
1 parent 7c8f08a commit d94c755

2 files changed

Lines changed: 79 additions & 11 deletions

File tree

‎src/skills/loading/plugin-skills.test.ts‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,20 @@ async function setupAcpxAndHelperRegistry() {
136136
return { workspaceDir, acpxRoot, helperRoot };
137137
}
138138

139+
function useStableMetadataSnapshot(manifestRegistry: PluginManifestRegistry): void {
140+
const snapshot = {
141+
manifestRegistry,
142+
plugins: manifestRegistry.plugins,
143+
normalizePluginId: (pluginId: string) =>
144+
manifestRegistry.plugins.find((plugin) => plugin.legacyPluginIds?.includes(pluginId))?.id ??
145+
pluginId,
146+
};
147+
hoisted.loadPluginMetadataSnapshot
148+
.mockReturnValueOnce(snapshot)
149+
.mockReturnValueOnce(snapshot)
150+
.mockReturnValueOnce(snapshot);
151+
}
152+
139153
async function setupPluginOutsideSkills() {
140154
const workspaceDir = await tempDirs.make("openclaw-");
141155
const pluginRoot = await tempDirs.make("openclaw-plugin-");
@@ -236,6 +250,57 @@ describe("resolvePluginSkillDirs", () => {
236250
expect(dirs).toEqual(expectedDirs({ acpxRoot, helperRoot }));
237251
});
238252

253+
it.each([
254+
{
255+
name: "unavailable to available",
256+
initiallyAvailable: false,
257+
firstIncludesAcpx: false,
258+
secondIncludesAcpx: true,
259+
},
260+
{
261+
name: "available to unavailable",
262+
initiallyAvailable: true,
263+
firstIncludesAcpx: true,
264+
secondIncludesAcpx: false,
265+
},
266+
])(
267+
"invalidates the memo when ACP changes from $name with stable inputs",
268+
async ({ initiallyAvailable, firstIncludesAcpx, secondIncludesAcpx }) => {
269+
const { workspaceDir, acpxRoot, helperRoot } = await setupAcpxAndHelperRegistry();
270+
const manifestRegistry = buildRegistry({ acpxRoot, helperRoot });
271+
useStableMetadataSnapshot(manifestRegistry);
272+
const config = {
273+
acp: { enabled: true },
274+
plugins: {
275+
entries: {
276+
acpx: { enabled: true },
277+
helper: { enabled: true },
278+
},
279+
},
280+
} as OpenClawConfig;
281+
if (initiallyAvailable) {
282+
registerHealthyAcpBackend();
283+
}
284+
285+
const first = resolvePluginSkillDirs({ workspaceDir, config });
286+
287+
if (initiallyAvailable) {
288+
acpRuntimeTesting.resetAcpRuntimeBackendsForTests();
289+
} else {
290+
registerHealthyAcpBackend();
291+
}
292+
const second = resolvePluginSkillDirs({ workspaceDir, config });
293+
294+
const dirsForState = (includeAcpx: boolean) => [
295+
...(includeAcpx ? [path.resolve(acpxRoot, "skills")] : []),
296+
path.resolve(helperRoot, "skills"),
297+
];
298+
expect(first).toEqual(dirsForState(firstIncludesAcpx));
299+
expect(second).toEqual(dirsForState(secondIncludesAcpx));
300+
expect(resolvePluginSkillDirs({ workspaceDir, config })).toBe(second);
301+
},
302+
);
303+
239304
it("rejects plugin skill paths that escape the plugin root", async () => {
240305
const { workspaceDir, pluginRoot, outsideSkills } = await setupPluginOutsideSkills();
241306
await fs.mkdir(path.join(pluginRoot, "skills"), { recursive: true });

‎src/skills/loading/plugin-skills.ts‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ const log = createSubsystemLogger("skills");
2222
type PluginSkillLinkType = "dir" | "junction";
2323

2424
// Plugin metadata is process-stable while the gateway runs, but this resolver sits on the
25-
// per-turn skills-refresh path. The single-slot memo keeps repeat turns from re-walking and
26-
// re-publishing every plugin skill dir; lifecycle clears evict it on plugin reload/install.
25+
// per-turn skills-refresh path. ACP availability changes outside that metadata lifecycle, so it
26+
// stays in the memo identity to prevent stale ACPX skill exposure without repeating directory IO.
2727
let pluginSkillDirsMemo: {
2828
workspaceDir: string;
2929
config: OpenClawConfig | undefined;
3030
snapshot: unknown;
31+
acpRuntimeAvailable: boolean;
3132
dirs: string[];
3233
} | null = null;
3334

@@ -55,28 +56,29 @@ export function resolvePluginSkillDirs(params: {
5556
env: process.env,
5657
allowWorkspaceScopedCurrent: true,
5758
});
59+
const registry = metadataSnapshot.manifestRegistry;
60+
if (registry.plugins.length === 0) {
61+
publishPluginSkills([], {
62+
pluginSkillsDir: params.pluginSkillsDir,
63+
});
64+
return [];
65+
}
66+
const acpRuntimeAvailable = isAcpRuntimeSpawnAvailable({ config });
5867
const canMemoize = params.pluginSkillsDir === undefined;
5968
if (
6069
canMemoize &&
6170
pluginSkillDirsMemo &&
6271
pluginSkillDirsMemo.workspaceDir === workspaceDir &&
6372
pluginSkillDirsMemo.config === params.config &&
64-
pluginSkillDirsMemo.snapshot === metadataSnapshot
73+
pluginSkillDirsMemo.snapshot === metadataSnapshot &&
74+
pluginSkillDirsMemo.acpRuntimeAvailable === acpRuntimeAvailable
6575
) {
6676
return pluginSkillDirsMemo.dirs;
6777
}
68-
const registry = metadataSnapshot.manifestRegistry;
69-
if (registry.plugins.length === 0) {
70-
publishPluginSkills([], {
71-
pluginSkillsDir: params.pluginSkillsDir,
72-
});
73-
return [];
74-
}
7578
const normalizedPlugins = normalizePluginsConfigWithResolver(
7679
config.plugins,
7780
metadataSnapshot.normalizePluginId,
7881
);
79-
const acpRuntimeAvailable = isAcpRuntimeSpawnAvailable({ config });
8082
const memorySlot = normalizedPlugins.slots.memory;
8183
let selectedMemoryPluginId: string | null = null;
8284
const seen = new Set<string>();
@@ -147,6 +149,7 @@ export function resolvePluginSkillDirs(params: {
147149
workspaceDir,
148150
config: params.config,
149151
snapshot: metadataSnapshot,
152+
acpRuntimeAvailable,
150153
dirs: resolved,
151154
};
152155
}

0 commit comments

Comments
 (0)