Skip to content

Commit 29a92e9

Browse files
authored
fix(plugins): recheck cached tool availability (#116625)
1 parent b2cba40 commit 29a92e9

2 files changed

Lines changed: 68 additions & 9 deletions

File tree

‎src/plugins/tools.optional.test.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,6 +1729,67 @@ describe("resolvePluginTools optional tools", () => {
17291729
expect(loadOpenClawPluginsMock).not.toHaveBeenCalled();
17301730
});
17311731

1732+
it("rechecks cached optional tool availability when provider auth changes", () => {
1733+
const config = createContext().config;
1734+
const env = {};
1735+
installToolManifestSnapshot({
1736+
config,
1737+
env,
1738+
plugin: {
1739+
id: "multi",
1740+
origin: "bundled",
1741+
enabledByDefault: true,
1742+
channels: [],
1743+
providers: [],
1744+
setup: {
1745+
providers: [{ id: "xai", envVars: ["XAI_API_KEY"] }],
1746+
},
1747+
contracts: {
1748+
tools: ["other_tool", "optional_tool"],
1749+
},
1750+
toolMetadata: {
1751+
optional_tool: {
1752+
optional: true,
1753+
authSignals: [{ provider: "xai" }],
1754+
},
1755+
},
1756+
},
1757+
});
1758+
const factory = vi.fn(() => [makeTool("other_tool"), makeTool("optional_tool")]);
1759+
setActivePluginRegistry(
1760+
createToolRegistry([
1761+
{
1762+
pluginId: "multi",
1763+
optional: false,
1764+
source: "/tmp/multi.js",
1765+
names: ["other_tool", "optional_tool"],
1766+
declaredNames: ["other_tool", "optional_tool"],
1767+
factory,
1768+
},
1769+
]) as never,
1770+
"test-tool-registry",
1771+
"gateway-bindable",
1772+
"/tmp",
1773+
);
1774+
const resolveWithAuth = (hasAuth: boolean) =>
1775+
resolvePluginTools({
1776+
...createResolveToolsParams({
1777+
context: {
1778+
...createContext(),
1779+
config,
1780+
},
1781+
env,
1782+
toolAllowlist: [DEFAULT_PLUGIN_TOOLS_ALLOWLIST_ENTRY, "optional_tool"],
1783+
}),
1784+
hasAuthForProvider: (providerId) => providerId === "xai" && hasAuth,
1785+
});
1786+
1787+
expectResolvedToolNames(resolveWithAuth(true), ["other_tool", "optional_tool"]);
1788+
expectResolvedToolNames(resolveWithAuth(false), ["other_tool"]);
1789+
expectResolvedToolNames(resolveWithAuth(true), ["other_tool", "optional_tool"]);
1790+
expect(factory).toHaveBeenCalledTimes(1);
1791+
});
1792+
17321793
it("does not materialize manifest-optional sibling tools from non-optional factories by default", async () => {
17331794
const config = createContext().config;
17341795
installToolManifestSnapshot({

‎src/plugins/tools.ts‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,14 @@ function resolveCachedPluginTools(params: {
958958
const pluginTools: AnyAgentTool[] = [];
959959
let hasNameConflict = false;
960960
const localNormalizedNames = new Set<string>();
961+
const availableNormalizedToolNames = new Set(availableToolNames.map(normalizeToolName));
961962
for (const cachedDescriptor of cached) {
963+
const normalizedDescriptorName = normalizeToolName(cachedDescriptor.descriptor.name);
964+
// Live auth is intentionally absent from the descriptor cache key, so re-project
965+
// every cached name through current manifest availability before optional grants.
966+
if (!availableNormalizedToolNames.has(normalizedDescriptorName)) {
967+
continue;
968+
}
962969
if (!hasRequiredClientCaps(cachedDescriptor.requiredClientCaps, params.clientCaps)) {
963970
continue;
964971
}
@@ -972,14 +979,6 @@ function resolveCachedPluginTools(params: {
972979
) {
973980
continue;
974981
}
975-
if (
976-
!cachedDescriptor.optional &&
977-
!availableToolNames.some(
978-
(name) => normalizeToolName(name) === normalizeToolName(cachedDescriptor.descriptor.name),
979-
)
980-
) {
981-
continue;
982-
}
983982
if (
984983
cachedDescriptor.optional &&
985984
!isOptionalToolAllowed({
@@ -990,7 +989,6 @@ function resolveCachedPluginTools(params: {
990989
) {
991990
continue;
992991
}
993-
const normalizedDescriptorName = normalizeToolName(cachedDescriptor.descriptor.name);
994992
if (
995993
denylistBlocksPluginTool({
996994
pluginId: plugin.id,

0 commit comments

Comments
 (0)