Files
paseo/plugin-examples/worktrunk/server/wt.ts
T
Mohamed Boudra d154df3165 Let plugins manage worktree creation, removal, and restoration (#10278)
* feat(plugins): let plugins manage worktree create, remove, and restore

Adds server.registerWorktreeManager({ create, remove?, restore? }) so external worktree tools such as Worktrunk can own Git worktree creation, removal, and restoration instead of overloading worktree.create.before.

Keeping worktree.create.before purely transformative preserves multi-plugin chaining, rollback, and the 30s hook budget, while giving manager operations a 120s budget matching git worktree commands. Paseo snapshots existing worktrees before create so managers cannot hijack pre-existing user worktrees, writes comparison-base and owning plugin metadata into paseo/worktree.json and the workspace record, saves refs/paseo/kept-commits before removal so restore stays safe, and falls back to built-in Git if a manager declines or is disabled.

* fix(plugins): harden worktree manager lifecycle, rollback, and schema

* fix(plugins): pass untrusted on create, clean up rejected worktrees, and sync docs

* refactor(plugins): unify worktree manager checkout flow and metadata rules

* fix(plugins): verify managed worktree ownership and restore targets

A failed callback does not establish ownership of worktrees created during its call. Require the manager to identify the checkout before Paseo attempts rollback, and verify the requested Git target before reopening a restored workspace.

* fix(plugins): preserve worktree setup on retry and refresh branch bases

A failed setup could leave a new branch behind and send the retry through the built-in checkout. Use the manager's reported branch creation status during rollback. Share base preparation so both checkout paths start from the updated remote-tracking ref.

* fix(plugins): preserve managed checkout targets and rollback

Keep branch creation ownership with the linked checkout so a failed after-hook can be retried with the same branch. Leave same-repository change requests with a local branch collision to the checkout path that fetches the change request head. Use the worktree timeout for compatibility commands.

* fix(worktrees): carry source trust and await managed creation

Preserve fork provenance for manager callbacks and the workspace record. Honor tracking branch creation in the manager example and let the CLI wait on the existing workspace creation lifecycle.

* fix(plugins): verify PR head commits, restore CLI worktree RPC, and fix Windows rollback paths

* fix(plugins): pre-resolve PR head, use NUL worktree snapshots, and wait on CLI worktree create

* fix(plugins): unify worktree list reader with Git 2.35 fallback and fetch PR head once

* fix(plugins): require exact worktree snapshot for managers and configure PR push remotes on adoption

* fix(server): coordinate workspace restores across daemon sessions

* fix(server): coordinate archive, undo, and restore across daemon checkout boundary

* fix: serialize open-project with checkout archive, sync sibling owners, lift client timeouts, bump plugin version

* fix: forbid nested secondary worktrees, preserve nested checkouts on archive, wait on openProject

* fix(server): refuse deleting a worktree containing a live nested Git worktree

* test(server): normalize Windows paths and teardown command in worktree-manager E2E

* fix(server): guard cross-repo nested checkouts on delete and share checkout trust across sibling workspaces

* fix(worktrees): scope nested checkout protection to managed worktrees

The removal scan treated dependency clones and recursive submodules as independent worktrees. Identify linked worktrees through Git metadata and use the scan only for manager-owned removals.

* test(worktrees): accept native line endings in archive fixtures

The nested submodule fixture checks that Git populated the checkout. The README line endings depend on the runner's Git configuration.

* docs(plugins): document the example manager Git requirement

* fix(worktrees): name the manager that returns an invalid path

The destination validator serves both creation hooks and managers. Keep its errors about the path so the caller can identify the plugin and operation.
2026-11-21 13:51:28 +07:00

318 lines
9.7 KiB
TypeScript

import { lstatSync } from "node:fs";
import { readFile, writeFile } from "node:fs/promises";
import { delimiter, join } from "node:path";
import {
execCommand,
type PluginWorktreeCreateInput,
type PluginWorktreeRemoveRequest,
type PluginWorktreeRestoreRequest,
} from "@getpaseo/plugin/server";
const MAX_OUTPUT_BYTES = 16 * 1024 * 1024;
const CREATED_BRANCH_FILE = "paseo-worktrunk-created-branch";
export const WORKING_MARKER = "🤖";
export const WAITING_MARKER = "💬";
interface SwitchJsonOutput {
path: string;
action?: string;
branch?: string;
createdBranch: boolean;
}
/**
* Resolve the Worktrunk CLI executable.
* Honors `WORKTRUNK_BIN` first; on Windows prefers `git-wt.exe` and skips the
* WindowsApps alias that points to Windows Terminal.
*/
export function resolveWorktrunkBin(): string | null {
const configured = process.env.WORKTRUNK_BIN?.trim();
if (configured) return configured;
if (process.platform !== "win32") return "wt";
const dirs = (process.env.PATH ?? "").split(delimiter).filter(Boolean);
const findOnPath = (name: string) =>
dirs
.map((dir) => join(dir, name))
.find((candidate) => {
try {
lstatSync(candidate);
return true;
} catch {
return false;
}
});
if (findOnPath("git-wt.exe")) return "git-wt.exe";
const wt = findOnPath("wt.exe");
return wt && !/[\\/]WindowsApps[\\/]/i.test(wt) ? "wt" : null;
}
async function runWt(
args: string[],
options: { cwd?: string; signal?: AbortSignal } = {},
): Promise<string> {
const bin = resolveWorktrunkBin();
if (!bin) {
throw new Error(
"Worktrunk CLI not found on PATH. Install wt (or git-wt.exe on Windows) or set WORKTRUNK_BIN.",
);
}
try {
const { stdout } = await execCommand(bin, args, {
cwd: options.cwd,
signal: options.signal,
maxBuffer: MAX_OUTPUT_BYTES,
});
return stdout;
} catch (error) {
const stderr = (error as { stderr?: string }).stderr?.trim();
throw new Error(stderr || (error instanceof Error ? error.message : String(error)), {
cause: error,
});
}
}
function tryParseSwitchOutput(stdout: string | undefined): SwitchJsonOutput | null {
if (!stdout?.trim()) return null;
try {
const parsed = JSON.parse(stdout) as {
path?: unknown;
action?: unknown;
branch?: unknown;
created_branch?: unknown;
from_remote?: unknown;
};
if (typeof parsed.path !== "string" || parsed.path.length === 0) return null;
return {
path: parsed.path,
// Worktrunk sets from_remote when switch creates a branch that tracks a remote.
createdBranch:
parsed.created_branch === true ||
(typeof parsed.from_remote === "string" && parsed.from_remote.length > 0),
...(typeof parsed.branch === "string" && parsed.branch ? { branch: parsed.branch } : {}),
...(typeof parsed.action === "string" ? { action: parsed.action } : {}),
};
} catch {
return null;
}
}
function parseSwitchOutput(stdout: string): SwitchJsonOutput {
const parsed = tryParseSwitchOutput(stdout);
if (!parsed) {
throw new Error("wt switch --format=json output did not include a worktree path");
}
return parsed;
}
function hookFlags(untrusted: boolean): string[] {
// Never approve repository-defined hooks on untrusted checkouts such as fork PRs.
return untrusted ? ["--no-hooks"] : ["--yes"];
}
export function buildCreateArgs(request: PluginWorktreeCreateInput): string[] | null {
const common = [
"-C",
request.repoRoot,
"switch",
"--no-cd",
...hookFlags(request.untrusted),
"--format=json",
];
switch (request.kind) {
case "branch-off":
return [
...common,
"--create",
request.branch,
"--base",
request.startPoint ?? request.baseRef,
];
case "checkout-branch":
return [...common, request.branch];
case "checkout-change-request":
case "checkout-github-pr":
if (!request.headSha) return null;
return [...common, "--create", request.branch, "--base", request.headSha];
}
}
function failedSwitchOutput(error: unknown): SwitchJsonOutput | null {
// Worktrunk reports the checkout and whether it created the branch even if a hook fails.
const stdout =
error instanceof Error && error.cause && typeof error.cause === "object"
? (error.cause as { stdout?: string }).stdout
: undefined;
return tryParseSwitchOutput(stdout);
}
async function createdBranchFile(worktreePath: string): Promise<string> {
const { stdout } = await execCommand("git", ["rev-parse", "--absolute-git-dir"], {
cwd: worktreePath,
});
return join(stdout.trim(), CREATED_BRANCH_FILE);
}
async function cleanupPartiallyCreatedWorktree(
repoRoot: string,
output: SwitchJsonOutput | null,
): Promise<void> {
if (output?.action !== "created") return;
await runWt([
"-C",
repoRoot,
"remove",
"--foreground",
"--no-delete-branch",
"--no-hooks",
"--force",
output.path,
]);
if (output.createdBranch && output.branch) {
await execCommand("git", ["branch", "-D", "--", output.branch], { cwd: repoRoot });
}
}
async function recordCreatedBranch(repoRoot: string, output: SwitchJsonOutput): Promise<void> {
if (output.action !== "created" || !output.createdBranch || !output.branch) return;
try {
// The linked worktree owns this record until removal. An archive keeps the branch.
await writeFile(await createdBranchFile(output.path), output.branch, { flag: "wx" });
} catch (error) {
await cleanupPartiallyCreatedWorktree(repoRoot, output).catch(() => {});
throw error;
}
}
async function hasLocalBranch(
repoRoot: string,
branch: string,
signal: AbortSignal,
): Promise<boolean> {
const ref = `refs/heads/${branch}`;
const { stdout } = await execCommand("git", ["for-each-ref", "--format=%(refname)", ref], {
cwd: repoRoot,
signal,
});
return stdout.trim().split(/\r?\n/).includes(ref);
}
export async function createWithWorktrunk(
request: PluginWorktreeCreateInput,
signal: AbortSignal,
): Promise<{ type: "created"; path: string } | undefined> {
const args = buildCreateArgs(request);
if (!args) return undefined;
// With a local branch collision, decline so Paseo's Git checkout allocates a unique branch
// name without disturbing the existing local branch.
const createsLocalBranch =
request.kind === "branch-off" ||
request.kind === "checkout-change-request" ||
request.kind === "checkout-github-pr";
if (createsLocalBranch && (await hasLocalBranch(request.repoRoot, request.branch, signal))) {
return undefined;
}
let stdout: string;
try {
stdout = await runWt(args, { signal });
} catch (error) {
await cleanupPartiallyCreatedWorktree(request.repoRoot, failedSwitchOutput(error)).catch(
() => {},
);
throw error;
}
const output = parseSwitchOutput(stdout);
// If Worktrunk switched to an already-existing checkout instead of creating one,
// decline so Paseo falls back to Git (which creates a unique branch).
if (output.action !== undefined && output.action !== "created") {
return undefined;
}
await recordCreatedBranch(request.repoRoot, output);
return { type: "created", path: output.path };
}
export async function removeWithWorktrunk(
request: PluginWorktreeRemoveRequest,
signal: AbortSignal,
): Promise<{ type: "removed" }> {
// Run from the main repository root when available and skip repository hooks for
// untrusted workspaces (or when repoRoot is unknown) so fork PRs cannot run hooks.
const runDir = request.repoRoot ?? request.path;
const skipHooks = request.untrusted || request.repoRoot === null;
const args = [
"-C",
runDir,
"remove",
"--foreground",
"--no-delete-branch",
...hookFlags(skipHooks),
...(request.force ? ["--force"] : []),
request.path,
];
const createdBranch =
request.reason === "rollback" && request.repoRoot && request.branch
? await createdBranchFile(request.path)
.then((file) => readFile(file, "utf8"))
.catch(() => null)
: null;
await runWt(args, { signal });
if (createdBranch && createdBranch === request.branch && request.repoRoot) {
await execCommand("git", ["branch", "-D", "--", createdBranch], { cwd: request.repoRoot });
}
return { type: "removed" };
}
export async function restoreWithWorktrunk(
request: PluginWorktreeRestoreRequest,
signal: AbortSignal,
): Promise<{ type: "restored"; path: string } | undefined> {
if (request.target.type === "commit") {
// Detached-commit restores fall back to Paseo's built-in Git restore.
return undefined;
}
const common = [
"-C",
request.repoRoot,
"switch",
"--path",
request.path,
"--no-cd",
...hookFlags(request.untrusted),
"--format=json",
];
const args =
request.target.type === "branch"
? [...common, request.target.branch]
: [...common, "--create", request.target.branch, "--base", request.target.baseRef];
let stdout: string;
try {
stdout = await runWt(args, { signal });
} catch (error) {
await cleanupPartiallyCreatedWorktree(request.repoRoot, failedSwitchOutput(error)).catch(
() => {},
);
throw error;
}
const output = parseSwitchOutput(stdout);
await recordCreatedBranch(request.repoRoot, output);
return { type: "restored", path: output.path };
}
export async function setActivityMarker(
cwd: string,
marker: string | null,
signal: AbortSignal,
): Promise<void> {
const args =
marker === null
? ["config", "state", "marker", "clear"]
: ["config", "state", "marker", "set", marker];
try {
await runWt(args, { cwd, signal });
} catch {
// Activity markers are best-effort and must not disrupt agent sessions.
}
}