Skip to content

Commit b185ab9

Browse files
committed
fix(plugins): surface empty availability groups at plugin tool registration
1 parent 341ae21 commit b185ab9

5 files changed

Lines changed: 227 additions & 1 deletion

File tree

‎src/plugins/tool-descriptor-cache.test.ts‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ vi.mock("../config/runtime-snapshot.js", () => ({
1717

1818
import {
1919
buildPluginToolDescriptorCacheKey,
20+
capturePluginToolDescriptor,
2021
createPluginToolDescriptorConfigCacheKeyMemo,
2122
resetPluginToolDescriptorCache,
2223
} from "./tool-descriptor-cache.js";
24+
import type { AnyAgentTool } from "../agents/tools/common.js";
2325

2426
describe("plugin tool descriptor cache keys", () => {
2527
afterEach(() => {
@@ -170,3 +172,110 @@ describe("plugin tool descriptor cache keys", () => {
170172
expect(firstKey).toBe(secondKey);
171173
});
172174
});
175+
176+
describe("capturePluginToolDescriptor", () => {
177+
it("preserves tool availability on captured plugin descriptors", () => {
178+
const tool = {
179+
name: "needs_secret",
180+
label: "Needs secret",
181+
description: "Needs secret description",
182+
parameters: { type: "object" },
183+
availability: { kind: "env", name: "OPENCLAW_PLUGIN_SECRET" },
184+
async execute() {
185+
return { content: [], details: undefined };
186+
},
187+
} satisfies AnyAgentTool & {
188+
availability: { kind: "env"; name: string };
189+
};
190+
191+
const captured = capturePluginToolDescriptor({
192+
pluginId: "demo",
193+
tool,
194+
optional: false,
195+
});
196+
197+
expect(captured.descriptor.availability).toStrictEqual({
198+
kind: "env",
199+
name: "OPENCLAW_PLUGIN_SECRET",
200+
});
201+
});
202+
203+
it("warns during capture when a plugin tool declares an empty availability group", () => {
204+
const logger = { warn: vi.fn() };
205+
const tool = {
206+
name: "broken_tool",
207+
label: "Broken tool",
208+
description: "Broken tool description",
209+
parameters: { type: "object" },
210+
availability: { anyOf: [] },
211+
async execute() {
212+
return { content: [], details: undefined };
213+
},
214+
} satisfies AnyAgentTool & {
215+
availability: { anyOf: [] };
216+
};
217+
218+
capturePluginToolDescriptor({
219+
pluginId: "demo",
220+
tool,
221+
optional: false,
222+
logger,
223+
});
224+
225+
expect(logger.warn).toHaveBeenCalledWith(
226+
"plugin tool availability diagnostic (demo/broken_tool): Empty availability anyOf group",
227+
);
228+
});
229+
230+
it("ignores malformed non-contract availability group fields during capture", () => {
231+
const logger = { warn: vi.fn() };
232+
const tool = {
233+
name: "incidental_tool",
234+
label: "Incidental tool",
235+
description: "Incidental tool description",
236+
parameters: { type: "object" },
237+
availability: { anyOf: null },
238+
async execute() {
239+
return { content: [], details: undefined };
240+
},
241+
} satisfies AnyAgentTool & {
242+
availability: { anyOf: null };
243+
};
244+
245+
const captured = capturePluginToolDescriptor({
246+
pluginId: "demo",
247+
tool,
248+
optional: false,
249+
logger,
250+
});
251+
252+
expect(captured.descriptor.availability).toBeUndefined();
253+
expect(logger.warn).not.toHaveBeenCalled();
254+
});
255+
256+
it("ignores malformed non-contract availability signal fields during capture", () => {
257+
const logger = { warn: vi.fn() };
258+
const tool = {
259+
name: "incidental_signal_tool",
260+
label: "Incidental signal tool",
261+
description: "Incidental signal tool description",
262+
parameters: { type: "object" },
263+
availability: { kind: "config" },
264+
async execute() {
265+
return { content: [], details: undefined };
266+
},
267+
} satisfies AnyAgentTool & {
268+
availability: { kind: "config" };
269+
};
270+
271+
const captured = capturePluginToolDescriptor({
272+
pluginId: "demo",
273+
tool,
274+
optional: false,
275+
logger,
276+
});
277+
278+
expect(captured.descriptor.availability).toBeUndefined();
279+
expect(logger.warn).not.toHaveBeenCalled();
280+
});
281+
});

‎src/plugins/tool-descriptor-cache.ts‎

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
import fs from "node:fs";
33
import type { AnyAgentTool } from "../agents/tools/common.js";
44
import { resolveRuntimeConfigCacheKey } from "../config/runtime-snapshot.js";
5-
import type { JsonObject, ToolDescriptor } from "../tools/types.js";
5+
import { evaluateEmptyAvailabilityGroupDiagnostics } from "../tools/availability.js";
6+
import type {
7+
JsonObject,
8+
ToolAvailabilityExpression,
9+
ToolDescriptor,
10+
} from "../tools/types.js";
611
import type { PluginLoadOptions } from "./loader.js";
712
import type { OpenClawPluginToolContext } from "./types.js";
813

@@ -144,13 +149,97 @@ function asJsonObject(value: unknown): JsonObject {
144149
return value as JsonObject;
145150
}
146151

152+
function isStringArray(value: unknown): value is readonly string[] {
153+
return Array.isArray(value) && value.every((entry) => typeof entry === "string");
154+
}
155+
156+
function isJsonPrimitive(value: unknown): boolean {
157+
return value === null || ["boolean", "number", "string"].includes(typeof value);
158+
}
159+
160+
function isToolAvailabilitySignal(value: { readonly kind?: unknown }): boolean {
161+
switch (value.kind) {
162+
case "always":
163+
return true;
164+
case "auth":
165+
return typeof (value as { providerId?: unknown }).providerId === "string";
166+
case "config": {
167+
const check = (value as { check?: unknown }).check;
168+
return (
169+
isStringArray((value as { path?: unknown }).path) &&
170+
(check === undefined || check === "exists" || check === "non-empty" || check === "available")
171+
);
172+
}
173+
case "env":
174+
return typeof (value as { name?: unknown }).name === "string";
175+
case "plugin-enabled":
176+
return typeof (value as { pluginId?: unknown }).pluginId === "string";
177+
case "context": {
178+
const equals = (value as { equals?: unknown }).equals;
179+
return (
180+
typeof (value as { key?: unknown }).key === "string" &&
181+
(equals === undefined || isJsonPrimitive(equals))
182+
);
183+
}
184+
default:
185+
return false;
186+
}
187+
}
188+
189+
function isToolAvailabilityExpression(value: unknown): value is ToolAvailabilityExpression {
190+
if (!value || typeof value !== "object" || Array.isArray(value)) {
191+
return false;
192+
}
193+
if ("kind" in value) {
194+
return isToolAvailabilitySignal(value);
195+
}
196+
if ("allOf" in value) {
197+
const entries = (value as { allOf?: unknown }).allOf;
198+
return Array.isArray(entries) && entries.every(isToolAvailabilityExpression);
199+
}
200+
if ("anyOf" in value) {
201+
const entries = (value as { anyOf?: unknown }).anyOf;
202+
return Array.isArray(entries) && entries.every(isToolAvailabilityExpression);
203+
}
204+
return false;
205+
}
206+
207+
type PluginToolDescriptorLogger = {
208+
warn(message: string): void;
209+
};
210+
211+
function warnEmptyAvailabilityGroups(params: {
212+
availability: ToolAvailabilityExpression | undefined;
213+
logger: PluginToolDescriptorLogger | undefined;
214+
pluginId: string;
215+
toolName: string;
216+
}): void {
217+
if (!params.availability || !params.logger) {
218+
return;
219+
}
220+
for (const diagnostic of evaluateEmptyAvailabilityGroupDiagnostics(params.availability)) {
221+
params.logger.warn(
222+
`plugin tool availability diagnostic (${params.pluginId}/${params.toolName}): ${diagnostic.message}`,
223+
);
224+
}
225+
}
226+
147227
export function capturePluginToolDescriptor(params: {
148228
pluginId: string;
149229
tool: AnyAgentTool;
150230
optional: boolean;
231+
logger?: PluginToolDescriptorLogger;
151232
}): CachedPluginToolDescriptor {
152233
const label = (params.tool as { label?: unknown }).label;
153234
const title = typeof label === "string" && label.trim() ? label.trim() : undefined;
235+
const availabilityRaw = (params.tool as { availability?: unknown }).availability;
236+
const availability = isToolAvailabilityExpression(availabilityRaw) ? availabilityRaw : undefined;
237+
warnEmptyAvailabilityGroups({
238+
availability,
239+
logger: params.logger,
240+
pluginId: params.pluginId,
241+
toolName: params.tool.name,
242+
});
154243
return {
155244
...(params.tool.displaySummary ? { displaySummary: params.tool.displaySummary } : {}),
156245
optional: params.optional,
@@ -161,6 +250,7 @@ export function capturePluginToolDescriptor(params: {
161250
inputSchema: asJsonObject(params.tool.parameters),
162251
owner: { kind: "plugin", pluginId: params.pluginId },
163252
executor: { kind: "plugin", pluginId: params.pluginId, toolName: params.tool.name },
253+
...(availability ? { availability } : {}),
164254
},
165255
};
166256
}

‎src/plugins/tools.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,7 @@ export function resolvePluginTools(params: {
13731373
pluginId: entry.pluginId,
13741374
tool,
13751375
optional,
1376+
logger: context.logger,
13761377
}),
13771378
);
13781379
capturedDescriptorsByPluginId.set(entry.pluginId, capturedDescriptors);

‎src/tools/availability.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,16 @@ function evaluateExpression(
166166
];
167167
}
168168

169+
/** Collect authoring diagnostics for empty boolean groups without reporting runtime-missing signals. */
170+
export function evaluateEmptyAvailabilityGroupDiagnostics(
171+
availability: ToolAvailabilityExpression,
172+
): readonly ToolAvailabilityDiagnostic[] {
173+
return evaluateExpression(availability, {}).filter(
174+
(entry) =>
175+
entry.reason === "unsupported-signal" && entry.message.startsWith("Empty availability "),
176+
);
177+
}
178+
169179
/** Evaluate one descriptor against runtime context and return hidden-tool diagnostics. */
170180
export function evaluateToolAvailability(params: {
171181
descriptor: ToolDescriptor;

‎src/tools/planner.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,22 @@ describe("buildToolPlan", () => {
113113
]);
114114
});
115115

116+
it("hides descriptors with malformed empty anyOf availability", () => {
117+
const plan = buildToolPlan({
118+
descriptors: [descriptor("malformed", { availability: { anyOf: [] } })],
119+
});
120+
121+
expect(plan.visible).toStrictEqual([]);
122+
const hiddenTool = expectHiddenTool(plan, 0);
123+
expect(hiddenTool.descriptor.name).toBe("malformed");
124+
expect(hiddenTool.diagnostics).toEqual([
125+
{
126+
reason: "unsupported-signal",
127+
message: "Empty availability anyOf group",
128+
},
129+
]);
130+
});
131+
116132
it("keeps protocol conversion separate from executor refs and model normalization", () => {
117133
const plan = buildToolPlan({
118134
descriptors: [

0 commit comments

Comments
 (0)