diff --git a/packages/core/src/tools/agent/agent.test.ts b/packages/core/src/tools/agent/agent.test.ts index 30e15fc590d..6b076c64fb1 100644 --- a/packages/core/src/tools/agent/agent.test.ts +++ b/packages/core/src/tools/agent/agent.test.ts @@ -670,6 +670,47 @@ describe('AgentTool', () => { ).toMatch(/working_dir/i); }); + it('accepts an empty working_dir with worktree isolation', () => { + expect( + agentTool.validateToolParams({ + ...validParams, + working_dir: '', + isolation: 'worktree', + }), + ).toBeNull(); + }); + + it('accepts a whitespace-only working_dir with worktree isolation', () => { + expect( + agentTool.validateToolParams({ + ...validParams, + working_dir: ' ', + isolation: 'worktree', + }), + ).toBeNull(); + }); + + it('normalizes an empty working_dir before creating an isolated invocation', () => { + const params = { + ...validParams, + working_dir: '', + isolation: 'worktree' as const, + }; + + expect(agentTool.validateToolParams(params)).toBeNull(); + + const invocation = ( + agentTool as AgentTool & { + createInvocation(params: AgentParams): { + params: AgentParams; + }; + } + ).createInvocation(params); + + expect(invocation.params.working_dir).toBeUndefined(); + expect(invocation.params.isolation).toBe('worktree'); + }); + it('accepts redundant worktree isolation when working_dir is set', () => { expect( agentTool.validateToolParams({ diff --git a/packages/core/src/tools/agent/agent.ts b/packages/core/src/tools/agent/agent.ts index 718c40dafce..6a777a284e6 100644 --- a/packages/core/src/tools/agent/agent.ts +++ b/packages/core/src/tools/agent/agent.ts @@ -1045,6 +1045,14 @@ assistant: Uses the ${ToolNames.AGENT} tool to launch the test-runner agent } } + if ( + params.isolation === 'worktree' && + typeof params.working_dir === 'string' && + params.working_dir.trim().length === 0 + ) { + params.working_dir = undefined; + } + if (params.working_dir !== undefined) { if ( typeof params.working_dir !== 'string' ||