From a7b1976ea27ddef5c7a2765aa88911e60ea9f9c6 Mon Sep 17 00:00:00 2001 From: Dotta Date: Thu, 11 Jun 2026 02:15:49 +0000 Subject: [PATCH] Parallelize tool discovery policy checks Co-Authored-By: Paperclip --- server/src/services/tool-gateway.ts | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/server/src/services/tool-gateway.ts b/server/src/services/tool-gateway.ts index 97fc37c7d47..735930c0cae 100644 --- a/server/src/services/tool-gateway.ts +++ b/server/src/services/tool-gateway.ts @@ -847,12 +847,13 @@ export function createToolGatewayService( async function listToolsForContext(session: ToolGatewaySession): Promise { await assertAgentInCompany(session.companyId, session.agentId); - const visible: ToolGatewayDescriptor[] = []; - for (const tool of allTools()) { + const decisions = await Promise.all(allTools().map(async (tool) => { const decision = await policyService.decide(policyInputForTool({ session, tool })); - if (decision.allowed || decision.decision === "require_approval") visible.push(tool); - } - return visible; + return { tool, decision }; + })); + return decisions + .filter(({ decision }) => decision.allowed || decision.decision === "require_approval") + .map(({ tool }) => tool); } async function executeBuiltinTool(session: ToolGatewaySession, tool: ToolGatewayDescriptor, parameters: unknown) { @@ -1103,19 +1104,20 @@ export function createToolGatewayService( async listPluginToolsForAgent(input: { companyId: string; agentId: string }): Promise { await assertAgentInCompany(input.companyId, input.agentId); - const visible: AgentToolDescriptor[] = []; - for (const tool of pluginTools()) { + const decisions = await Promise.all(pluginTools().map(async (tool) => { const decision = await policyService.decide(policyInputForAgentTool({ companyId: input.companyId, agentId: input.agentId, tool, })); - if (decision.allowed || decision.decision === "require_approval") { + return { tool, decision }; + })); + return decisions + .filter(({ decision }) => decision.allowed || decision.decision === "require_approval") + .map(({ tool }) => { const { providerType: _providerType, risk: _risk, ...descriptor } = tool; - visible.push(descriptor); - } - } - return visible; + return descriptor; + }); }, async approveActionRequest(input: {