From e8c4ae2024980d58887d61d97b539bfb0db1df9c Mon Sep 17 00:00:00 2001 From: Mohamed Boudra Date: Wed, 18 Feb 2026 20:35:49 +0700 Subject: [PATCH] fix(app): prefill new agent cwd with main repo for worktrees --- .../app/src/utils/new-agent-routing.test.ts | 12 +++++++++ packages/app/src/utils/new-agent-routing.ts | 25 ++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/packages/app/src/utils/new-agent-routing.test.ts b/packages/app/src/utils/new-agent-routing.test.ts index 47f0e111c..7bfae9520 100644 --- a/packages/app/src/utils/new-agent-routing.test.ts +++ b/packages/app/src/utils/new-agent-routing.test.ts @@ -26,6 +26,18 @@ describe("resolveNewAgentWorkingDir", () => { expect(resolveNewAgentWorkingDir("/repo/path", null)).toBe("/repo/path"); }); + it("falls back to repo root when checkout metadata is unavailable", () => { + expect(resolveNewAgentWorkingDir("/repo/.paseo/worktrees/feature", null)).toBe( + "/repo" + ); + }); + + it("supports windows-style paseo worktree paths without checkout metadata", () => { + expect( + resolveNewAgentWorkingDir("C:\\Users\\me\\repo\\.paseo\\worktrees\\feature", null) + ).toBe("C:\\Users\\me\\repo"); + }); + it("returns the main repo root for paseo-owned worktrees", () => { const checkout = { isPaseoOwnedWorktree: true, diff --git a/packages/app/src/utils/new-agent-routing.ts b/packages/app/src/utils/new-agent-routing.ts index 8a3fd1d7a..dfe1ff987 100644 --- a/packages/app/src/utils/new-agent-routing.ts +++ b/packages/app/src/utils/new-agent-routing.ts @@ -32,11 +32,34 @@ export function resolveSelectedAgentForNewAgent(input: { ); } +function inferMainRepoRootFromPaseoWorktreePath(cwd: string): string | null { + const normalizedPath = cwd.replace(/\\/g, "/"); + const marker = "/.paseo/worktrees"; + const markerIndex = normalizedPath.indexOf(marker); + if (markerIndex <= 0) { + return null; + } + const markerEnd = markerIndex + marker.length; + const nextChar = normalizedPath[markerEnd]; + if (nextChar && nextChar !== "/") { + return null; + } + const inferred = cwd.slice(0, markerIndex).replace(/[\\/]+$/, ""); + return inferred.trim() ? inferred : null; +} + export function resolveNewAgentWorkingDir( cwd: string, checkout: CheckoutStatusPayload | null ): string { - return (checkout?.isPaseoOwnedWorktree ? checkout.mainRepoRoot : null) ?? cwd; + const explicitMainRepoRoot = checkout?.isPaseoOwnedWorktree + ? checkout.mainRepoRoot?.trim() || null + : null; + if (explicitMainRepoRoot) { + return explicitMainRepoRoot; + } + + return inferMainRepoRootFromPaseoWorktreePath(cwd) ?? cwd; } export function buildNewAgentRoute(