fix(app): prefill new agent cwd with main repo for worktrees

This commit is contained in:
Mohamed Boudra
2026-02-18 20:35:49 +07:00
parent e22f183bb2
commit e8c4ae2024
2 changed files with 36 additions and 1 deletions

View File

@@ -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,

View File

@@ -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(