From 3f9285355d738ae12e2c58bee3a646463068db3f Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Sun, 6 Sep 2026 17:24:21 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=8F=A0=20fix:=20Route=20Standalone=20File?= =?UTF-8?q?=20Writes=20to=20BYOM=20Workers=20(#15675)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/server/services/ToolService.js | 4 +- .../services/__tests__/ToolService.spec.js | 97 ++++++++++++++----- 2 files changed, 78 insertions(+), 23 deletions(-) diff --git a/api/server/services/ToolService.js b/api/server/services/ToolService.js index 2e4f770b7a..6b9fa06e5b 100644 --- a/api/server/services/ToolService.js +++ b/api/server/services/ToolService.js @@ -49,6 +49,8 @@ const { createGitIdentityProgrammaticBashTool, resolveCodeExecutionContext, resolveCallerCapabilityProjectionSnapshot, + CREATE_FILE_TOOL_NAME, + EDIT_FILE_TOOL_NAME, LIST_WORKSPACE_FILES_TOOL_NAME, SEARCH_WORKSPACE_TOOL_NAME, getTransactionsConfig, @@ -2013,7 +2015,7 @@ async function loadToolsForExecution({ isWorkspaceListRequested; const isSkillToolRequested = toolNames.includes(AgentConstants.SKILL_TOOL); const isSandboxFileToolRequested = toolNames.some((name) => - [AgentConstants.READ_FILE, AgentConstants.CREATE_FILE, AgentConstants.EDIT_FILE].includes(name), + [AgentConstants.READ_FILE, CREATE_FILE_TOOL_NAME, EDIT_FILE_TOOL_NAME].includes(name), ); let enabledCapabilities; diff --git a/api/server/services/__tests__/ToolService.spec.js b/api/server/services/__tests__/ToolService.spec.js index 098bdcff1a..d0fdf0edf4 100644 --- a/api/server/services/__tests__/ToolService.spec.js +++ b/api/server/services/__tests__/ToolService.spec.js @@ -2529,43 +2529,96 @@ describe('ToolService - Action Capability Gating', () => { } }); - it('resolves stateful routing for host file tools with the controller conversation ID', async () => { - const capabilities = [ - AgentCapabilities.tools, - AgentCapabilities.execute_code, - AgentCapabilities.stateful_code_sessions, - ]; - const req = createMockReq(capabilities); - req.body = {}; - mockGetEndpointsConfig.mockResolvedValue(createEndpointsConfig(capabilities)); - process.env.LIBRECHAT_CODE_BASEURL_STATEFUL = 'http://code-stateful.test/v1'; + it.each(['read_file', 'create_file', 'edit_file'])( + 'resolves stateful routing for standalone %s with the controller conversation ID', + async (toolName) => { + const capabilities = [ + AgentCapabilities.tools, + AgentCapabilities.execute_code, + AgentCapabilities.stateful_code_sessions, + ]; + const req = createMockReq(capabilities); + req.body = {}; + mockGetEndpointsConfig.mockResolvedValue(createEndpointsConfig(capabilities)); + process.env.LIBRECHAT_CODE_BASEURL_STATEFUL = 'http://code-stateful.test/v1'; + + try { + const result = await loadToolsForExecution({ + req, + res: {}, + conversationId: 'resolved-api-conversation', + agent: { + id: 'stateful-agent', + tools: [Tools.execute_code], + stateful_code_sessions: true, + stateful_code_environment: 'conversation', + }, + toolNames: [toolName], + actionsEnabled: false, + }); + + expect(result.configurable.codeExecutionContext.executionProfile).toBe('stateful'); + expect(mockResolveCodeExecutionContext).toHaveBeenLastCalledWith( + expect.objectContaining({ + statefulSessions: true, + conversationId: 'resolved-api-conversation', + }), + ); + } finally { + delete process.env.LIBRECHAT_CODE_BASEURL_STATEFUL; + } + }, + ); + + it.each(['read_file', 'create_file', 'edit_file'])( + 'keeps standalone %s on the selected attached worker', + async (toolName) => { + const capabilities = [ + AgentCapabilities.tools, + AgentCapabilities.execute_code, + AgentCapabilities.stateful_code_sessions, + ]; + const req = createMockReq(capabilities); + req.config.endpoints.agents.statefulCodeSessions = { + environments: [ + { + id: 'personal-machine', + type: 'attached', + baseURL: 'https://attached-code.test/v1', + workerId: 'worker-personal', + }, + ], + }; + mockGetEndpointsConfig.mockResolvedValue(createEndpointsConfig(capabilities)); + mockResolveCodeExecutionContext.mockImplementationOnce( + jest.requireActual('@librechat/api').resolveCodeExecutionContext, + ); - try { const result = await loadToolsForExecution({ req, res: {}, conversationId: 'resolved-api-conversation', agent: { - id: 'stateful-agent', + id: 'attached-agent', tools: [Tools.execute_code], stateful_code_sessions: true, - stateful_code_environment: 'conversation', + code_environment_id: 'personal-machine', }, - toolNames: [AgentConstants.READ_FILE], + toolNames: [toolName], actionsEnabled: false, }); - expect(result.configurable.codeExecutionContext.executionProfile).toBe('stateful'); - expect(mockResolveCodeExecutionContext).toHaveBeenLastCalledWith( + expect(result.configurable.codeExecutionContext).toEqual( expect.objectContaining({ - statefulSessions: true, - conversationId: 'resolved-api-conversation', + baseUrl: 'https://attached-code.test/v1', + executionProfile: 'stateful', + environmentType: 'attached', + environmentId: 'personal-machine', + bridgeWorkerId: 'worker-personal', }), ); - } finally { - delete process.env.LIBRECHAT_CODE_BASEURL_STATEFUL; - } - }); + }, + ); it('preserves attached routing when workspace search is the only requested tool', async () => { const capabilities = [