From ce637139e09354fe7e8671558ca7dcb8e728ee4a Mon Sep 17 00:00:00 2001 From: Mohamed Boudra Date: Tue, 17 Nov 2026 13:53:56 +0100 Subject: [PATCH] fix(archive): key a saved HEAD by the folder's real path Archive and recovery key the saved HEAD by the recorded worktree root, which is already a real path. Worktree placement checked the path as written, so with a symlink in the Paseo home or worktrees root a new worktree missed the saved HEAD and took the archived folder's path. archived-head.ts now resolves symlinks itself when it builds the key, through the nearest existing ancestor when the folder is gone, so every spelling of one folder reaches the same ref. Co-authored-by: Okan YILDIRIM <63511519+ragokan@users.noreply.github.com> --- docs/agent-lifecycle.md | 2 +- .../workspace-recovery/archived-head.test.ts | 27 ++++++++++++-- .../workspace-recovery/archived-head.ts | 36 ++++++++++++++----- 3 files changed, 53 insertions(+), 12 deletions(-) diff --git a/docs/agent-lifecycle.md b/docs/agent-lifecycle.md index fefb2d961b..6c3e45ba58 100644 --- a/docs/agent-lifecycle.md +++ b/docs/agent-lifecycle.md @@ -282,7 +282,7 @@ does not flip it back. Tabs and layout are purged only once the undo window clos archive committed. Before deleting the directory, the archive saves HEAD as one ref, -`refs/paseo/archived//`, whose value is HEAD's commit and +`refs/paseo/archived//`, whose value is HEAD's commit and whose `` is `git rev-parse --abbrev-ref HEAD` (`feature`, or `HEAD` when detached). The ref keeps git's cleanup from deleting commits only that HEAD held. Recovery checks out the saved name when it still resolves to the saved commit and the saved commit otherwise, so git decides diff --git a/packages/server/src/server/session/workspace-recovery/archived-head.test.ts b/packages/server/src/server/session/workspace-recovery/archived-head.test.ts index fc92adcf74..15a140226e 100644 --- a/packages/server/src/server/session/workspace-recovery/archived-head.test.ts +++ b/packages/server/src/server/session/workspace-recovery/archived-head.test.ts @@ -1,11 +1,16 @@ import { execFileSync } from "node:child_process"; -import { mkdtempSync, rmSync } from "node:fs"; +import { mkdtempSync, rmSync, symlinkSync } from "node:fs"; import { tmpdir } from "node:os"; -import { join } from "node:path"; +import { basename, dirname, join } from "node:path"; import { afterEach, expect, test } from "vitest"; -import { readArchivedHead, releaseArchivedHead, saveArchivedHead } from "./archived-head.js"; +import { + isHeldByArchivedWorktree, + readArchivedHead, + releaseArchivedHead, + saveArchivedHead, +} from "./archived-head.js"; const tempDirectories: string[] = []; @@ -57,6 +62,22 @@ test("restore checks out the saved branch while it still points at the saved com }); }); +test("a removed folder reached through a symlinked parent finds the HEAD saved under its real path", async () => { + const { repoRoot, worktreePath } = createRepositoryWithWorktree(); + const linkedParent = `${dirname(worktreePath)}-link`; + symlinkSync(dirname(worktreePath), linkedParent); + tempDirectories.push(linkedParent); + const linkedWorktreePath = join(linkedParent, basename(worktreePath)); + const commit = git(worktreePath, ["rev-parse", "HEAD"]); + await saveArchivedHead({ worktreePath, repoRoot, hadActiveWorkspace: true }); + removeWorktree(repoRoot, worktreePath); + + expect({ + held: await isHeldByArchivedWorktree({ repoRoot, worktreePath: linkedWorktreePath }), + head: await readArchivedHead({ repoRoot, worktreePath: linkedWorktreePath }), + }).toEqual({ held: true, head: { checkout: "feature", commit } }); +}); + test("a HEAD left behind by a failed release is not read as current after the folder disappears", async () => { const { repoRoot, worktreePath } = createRepositoryWithWorktree(); // An earlier archive saved HEAD, and the unarchive that followed could not delete it. diff --git a/packages/server/src/server/session/workspace-recovery/archived-head.ts b/packages/server/src/server/session/workspace-recovery/archived-head.ts index fc493065f6..bfd333efd2 100644 --- a/packages/server/src/server/session/workspace-recovery/archived-head.ts +++ b/packages/server/src/server/session/workspace-recovery/archived-head.ts @@ -1,13 +1,15 @@ import { createHash } from "node:crypto"; +import { realpath } from "node:fs"; import { stat } from "node:fs/promises"; -import { join, resolve } from "node:path"; +import { basename, dirname, join, resolve } from "node:path"; +import { promisify } from "node:util"; import { runGitCommand } from "../../../utils/run-git-command.js"; // Removing a worktree folder at archive deletes its HEAD, and with it the only hold on commits // that no branch reaches. Before the folder goes, archive saves HEAD as one ref: // -// refs/paseo/archived// -> HEAD's commit +// refs/paseo/archived// -> HEAD's commit // // The name is `git rev-parse --abbrev-ref HEAD`, stored as git prints it and never parsed here. // The ref keeps the commit alive, and recovery hands the name back to git when it still means @@ -25,8 +27,26 @@ async function exists(path: string): Promise { ); } -function savedHeadPrefix(worktreePath: string): string { - const folder = createHash("sha256").update(resolve(worktreePath)).digest("hex"); +// fs.realpath rather than the native resolver, which can return a Windows 8.3 alias. +const resolveRealPath = promisify(realpath); + +// The folder's path with symlinks resolved, even after the folder is gone: archive, recovery and +// worktree placement can each spell one folder differently, and all must reach the same key. +async function canonicalFolderPath(path: string): Promise { + const absolute = resolve(path); + const parent = dirname(absolute); + if (parent === absolute) return absolute; + try { + return await resolveRealPath(absolute); + } catch { + return join(await canonicalFolderPath(parent), basename(absolute)); + } +} + +async function savedHeadPrefix(worktreePath: string): Promise { + const folder = createHash("sha256") + .update(await canonicalFolderPath(worktreePath)) + .digest("hex"); return `refs/paseo/archived/${folder}/`; } @@ -74,7 +94,7 @@ export async function saveArchivedHead(input: { repoRoot: string | null; hadActiveWorkspace: boolean; }): Promise { - const prefix = savedHeadPrefix(input.worktreePath); + const prefix = await savedHeadPrefix(input.worktreePath); const name = await readHeadName(input.worktreePath); if (name) { const cwd = input.worktreePath; @@ -98,7 +118,7 @@ export async function readArchivedHead(input: { worktreePath: string; }): Promise { if (!(await isRepository(input.repoRoot))) return null; - const prefix = savedHeadPrefix(input.worktreePath); + const prefix = await savedHeadPrefix(input.worktreePath); const [saved] = await listSavedHeads(input.repoRoot, prefix); if (!saved) return null; const separator = saved.indexOf(" "); @@ -118,7 +138,7 @@ export async function releaseArchivedHead(input: { }): Promise { // Without a repository there is no ref to delete. if (!(await isRepository(input.repoRoot))) return; - await discardSavedHeads(input.repoRoot, savedHeadPrefix(input.worktreePath)); + await discardSavedHeads(input.repoRoot, await savedHeadPrefix(input.worktreePath)); } /** Whether an archived worktree still holds this path, so a new worktree must not take it. */ @@ -126,6 +146,6 @@ export async function isHeldByArchivedWorktree(input: { repoRoot: string; worktreePath: string; }): Promise { - const saved = await listSavedHeads(input.repoRoot, savedHeadPrefix(input.worktreePath)); + const saved = await listSavedHeads(input.repoRoot, await savedHeadPrefix(input.worktreePath)); return saved.length > 0; }