mirror of
https://github.com/getpaseo/paseo.git
synced 2026-11-26 02:05:31 +00:00
* 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.
272 lines
9.4 KiB
TypeScript
272 lines
9.4 KiB
TypeScript
import type { AgentToolDefinition } from "./agent-tools.js";
|
|
import type {
|
|
AgentPermissionRequest,
|
|
AgentPermissionResponse,
|
|
AgentSessionConfig,
|
|
JsonValue,
|
|
} from "@getpaseo/protocol/agent-types";
|
|
import type { PaseoApi } from "@getpaseo/client";
|
|
import type { WorkspaceCreateRequest } from "@getpaseo/protocol/messages";
|
|
|
|
export interface PluginHookContext {
|
|
paseo: PaseoApi;
|
|
signal: AbortSignal;
|
|
}
|
|
|
|
export interface PluginHookWorkspace {
|
|
id: string;
|
|
projectId: string;
|
|
cwd: string;
|
|
name: string | null;
|
|
archivedAt: string | null;
|
|
/** Data plugins stored on the workspace. Paseo never reads a key. */
|
|
metadata: Record<string, JsonValue>;
|
|
}
|
|
|
|
export interface PluginHookWorktree {
|
|
path: string;
|
|
branch: string;
|
|
sourceCheckout: string;
|
|
}
|
|
|
|
/** Resolved creation options before Git fetches refs or chooses a unique branch name. */
|
|
export type PluginWorktreeCreateRequest = {
|
|
readonly repoRoot: string;
|
|
readonly sourceCheckout: string;
|
|
branch: string;
|
|
slug: string;
|
|
path: string;
|
|
} & (
|
|
| {
|
|
kind: "branch-off";
|
|
baseRef: string;
|
|
/** Where the new branch starts when that is not `baseRef`, such as a fork's head. */
|
|
readonly startPoint?: string;
|
|
}
|
|
| { kind: "checkout-branch" }
|
|
| {
|
|
kind: "checkout-change-request";
|
|
baseRef: string;
|
|
forge: string;
|
|
changeRequestNumber: number;
|
|
headRef: string;
|
|
headRepositoryOwner?: string;
|
|
headRepository?: string;
|
|
checkoutRefs?: { remoteName?: string; remoteRef: string }[];
|
|
pushRemoteUrl?: string;
|
|
trackOriginHead?: boolean;
|
|
}
|
|
| {
|
|
kind: "checkout-github-pr";
|
|
baseRef: string;
|
|
githubPrNumber: number;
|
|
headRef: string;
|
|
headRepositoryOwner?: string;
|
|
checkoutRefs?: { remoteName?: string; remoteRef: string }[];
|
|
pushRemoteUrl?: string;
|
|
trackOriginHead?: boolean;
|
|
}
|
|
);
|
|
|
|
export interface PluginHookAgent {
|
|
id: string;
|
|
workspaceId: string | null;
|
|
parentAgentId: string | null;
|
|
provider: string;
|
|
cwd: string;
|
|
title: string | null;
|
|
/** A helper hidden from tabs, lists and attention. Fixed at creation. */
|
|
readonly background: boolean;
|
|
/** Data plugins stored on the agent. Paseo never reads a key. */
|
|
metadata: Record<string, JsonValue>;
|
|
}
|
|
|
|
export interface PluginSessionOpenRequest {
|
|
agentId: string;
|
|
workspaceId: string | null;
|
|
provider: string;
|
|
cwd: string;
|
|
reason: "create" | "resume" | "refresh" | "import";
|
|
purpose: "interactive" | "history";
|
|
readonly title: string | null;
|
|
/** A helper hidden from tabs, lists and attention. Fixed at creation. */
|
|
readonly background: boolean;
|
|
readonly labels: Readonly<Record<string, string>>;
|
|
readonly metadata: Readonly<Record<string, JsonValue>>;
|
|
/** Instructions appended for this open; not saved on the agent. */
|
|
appendSystemPrompt?: string[];
|
|
tools: AgentToolDefinition[];
|
|
env: Record<string, string>;
|
|
mcpServers: NonNullable<AgentSessionConfig["mcpServers"]>;
|
|
}
|
|
|
|
export type PluginTurnOutcome =
|
|
| { kind: "completed" }
|
|
| { kind: "failed"; error: { message: string; code?: string } }
|
|
| { kind: "canceled"; reason: string };
|
|
|
|
export interface PluginHookEvents {
|
|
"agent.turn.before": { agent: PluginHookAgent; turnId: string | null };
|
|
"agent.turn.after": {
|
|
agent: PluginHookAgent;
|
|
turnId: string | null;
|
|
/**
|
|
* The payload carries no history. Read the turn with
|
|
* `paseo.agents.ref(agent.id).timeline.refetch({ direction: "tail", limit: 1, wholeTurns: true })`
|
|
* and keep the entries whose `turnId` equals this `turnId`. The result is empty when a newer turn
|
|
* started before the read, or when `turnId` is `null`.
|
|
*/
|
|
outcome: PluginTurnOutcome;
|
|
};
|
|
"agent.permission.before": { agent: PluginHookAgent; request: AgentPermissionRequest };
|
|
"agent.permission.after": {
|
|
agent: PluginHookAgent;
|
|
requestId: string;
|
|
resolution: AgentPermissionResponse;
|
|
};
|
|
"agent.archive.after": { agent: PluginHookAgent; archivedAt: string };
|
|
"agent.create.after": { agent: PluginHookAgent };
|
|
"agent.close.after": { agent: PluginHookAgent };
|
|
"workspace.create.after": { workspace: PluginHookWorkspace };
|
|
"workspace.archive.after": { workspace: PluginHookWorkspace };
|
|
}
|
|
|
|
export interface PluginHookRequests {
|
|
"worktree.create.before": PluginWorktreeCreateRequest;
|
|
"agent.create.before": {
|
|
config: AgentSessionConfig;
|
|
env?: Record<string, string>;
|
|
/** Stored on the new agent. Always present in the payload; return it changed to change it. */
|
|
metadata?: Record<string, JsonValue>;
|
|
};
|
|
"agent.open.before": PluginSessionOpenRequest;
|
|
"workspace.create.before": Omit<WorkspaceCreateRequest, "type" | "requestId">;
|
|
}
|
|
|
|
/** What a hook may return. An omitted `metadata` stays as it was. */
|
|
type PluginHookRequestResults = Omit<PluginHookRequests, "agent.open.before"> & {
|
|
// Read-only fields added after the first SDK stay optional in a result; omitted means unchanged.
|
|
"agent.open.before": Omit<PluginSessionOpenRequest, "metadata" | "background"> & {
|
|
readonly background?: boolean;
|
|
metadata?: Readonly<Record<string, JsonValue>>;
|
|
};
|
|
};
|
|
|
|
export type PluginHooks = {
|
|
[Name in keyof PluginHookRequests]: {
|
|
request: PluginHookRequests[Name];
|
|
result: PluginHookRequestResults[Name];
|
|
};
|
|
} & { [Name in keyof PluginHookEvents]: { payload: PluginHookEvents[Name] } } & {
|
|
"worktree.create.after": { payload: { worktree: PluginHookWorktree } };
|
|
};
|
|
|
|
export type PluginHookPayload<Name extends keyof PluginHooks> = {
|
|
[Key in keyof PluginHooks]: PluginHooks[Key] extends { request: infer Request }
|
|
? { request: Request }
|
|
: PluginHooks[Key] extends { payload: infer Payload }
|
|
? Payload
|
|
: never;
|
|
}[Name];
|
|
export type PluginHookResult<Name extends keyof PluginHooks> = {
|
|
[Key in keyof PluginHooks]: PluginHooks[Key] extends { result: infer Result }
|
|
? Result | void
|
|
: void;
|
|
}[Name];
|
|
|
|
type HookHandlers = {
|
|
[Name in keyof PluginHooks]: (
|
|
payload: PluginHookPayload<Name>,
|
|
context: PluginHookContext,
|
|
) => Promise<PluginHookResult<Name>>;
|
|
};
|
|
|
|
export interface PluginLifecycleRegistration {
|
|
addHook<Name extends keyof PluginHooks>(name: Name, handler: HookHandlers[Name]): () => void;
|
|
/** @deprecated Use addHook with the renamed hook. Removed after 2027-04-30. */
|
|
on<Name extends keyof PluginLifecycleEvents>(
|
|
name: Name,
|
|
handler: (
|
|
event: PluginLifecycleEvents[Name],
|
|
context: PluginHookContext,
|
|
) => void | Promise<void>,
|
|
): () => void;
|
|
/** @deprecated Use addHook with the renamed hook. Removed after 2027-04-30. */
|
|
before<Name extends keyof PluginBeforeRequests>(
|
|
name: Name,
|
|
handler: (
|
|
input: { request: PluginBeforeRequests[Name] },
|
|
context: PluginHookContext,
|
|
) => PluginBeforeResults[Name] | void | Promise<PluginBeforeResults[Name] | void>,
|
|
): () => void;
|
|
}
|
|
|
|
/** @internal Lifecycle policy shared by the daemon and plugin process. */
|
|
export const pluginHookRegistry = {
|
|
"workspace.create.before": { waits: true },
|
|
"workspace.create.after": { waits: false },
|
|
"workspace.archive.after": { waits: false },
|
|
"worktree.create.before": { waits: true },
|
|
"worktree.create.after": { waits: true },
|
|
"agent.create.before": { waits: true },
|
|
"agent.create.after": { waits: false },
|
|
"agent.open.before": { waits: true },
|
|
"agent.close.after": { waits: false },
|
|
"agent.archive.after": { waits: false },
|
|
"agent.turn.before": { waits: false },
|
|
"agent.turn.after": { waits: false },
|
|
"agent.permission.before": { waits: false },
|
|
"agent.permission.after": { waits: false },
|
|
} as const satisfies Record<keyof PluginHooks, { waits: boolean }>;
|
|
|
|
// COMPAT(pluginHookNames): added in v0.14, remove after 2027-04-30
|
|
const legacyEvents = {
|
|
"workspace.created": "workspace.create.after",
|
|
"workspace.archived": "workspace.archive.after",
|
|
"agent.created": "agent.create.after",
|
|
"agent.closed": "agent.close.after",
|
|
"agent.archived": "agent.archive.after",
|
|
"agent.turn_started": "agent.turn.before",
|
|
"agent.turn_ended": "agent.turn.after",
|
|
"agent.permission_requested": "agent.permission.before",
|
|
"agent.permission_resolved": "agent.permission.after",
|
|
} as const;
|
|
const legacyRequests = {
|
|
"workspace.create": "workspace.create.before",
|
|
"agent.create": "agent.create.before",
|
|
"agent.session_open": "agent.open.before",
|
|
} as const;
|
|
export type PluginLifecycleEvents = {
|
|
[Name in keyof typeof legacyEvents]: PluginHookEvents[(typeof legacyEvents)[Name]];
|
|
};
|
|
export type PluginBeforeRequests = {
|
|
[Name in keyof typeof legacyRequests]: PluginHookRequests[(typeof legacyRequests)[Name]];
|
|
};
|
|
type PluginBeforeResults = {
|
|
[Name in keyof typeof legacyRequests]: PluginHookRequestResults[(typeof legacyRequests)[Name]];
|
|
};
|
|
|
|
/** @internal SDK adapter for the daemon's hook registry. */
|
|
export function createLifecycleRegistration(
|
|
register: (
|
|
name: keyof PluginHooks,
|
|
handler: (input: never, context: PluginHookContext) => unknown,
|
|
) => () => void,
|
|
): PluginLifecycleRegistration {
|
|
const names = new Set<string>(Object.keys(pluginHookRegistry));
|
|
return {
|
|
addHook(name, handler) {
|
|
if (!names.has(name)) throw new Error(`Unknown lifecycle hook: ${name}`);
|
|
return register(name, handler);
|
|
},
|
|
on(name, handler) {
|
|
if (!Object.hasOwn(legacyEvents, name)) throw new Error(`Unknown lifecycle event: ${name}`);
|
|
return register(legacyEvents[name], handler);
|
|
},
|
|
before(name, handler) {
|
|
if (!Object.hasOwn(legacyRequests, name)) throw new Error(`Unknown before hook: ${name}`);
|
|
return register(legacyRequests[name], handler);
|
|
},
|
|
};
|
|
}
|