fix(server): keep scheduled worktree runs in their project (#10378)

This commit is contained in:
kotusenka
2026-12-01 01:12:44 +00:00
committed by GitHub
parent 6e1dbe31af
commit c00398cd10
4 changed files with 106 additions and 18 deletions
+2
View File
@@ -1470,6 +1470,8 @@ async function assemblePaseoDaemon({
const workspace = await workspaceProvisioning.createWorkspaceForDirectory(
input.cwd,
resolveFirstAgentPromptTitle(input.firstAgentContext),
undefined,
{ inheritWorktreeProject: true },
);
workspaceAutoName.scheduleForDirectory({
workspaceId: workspace.workspaceId,
@@ -275,6 +275,47 @@ test("archiveOnFinish=true scheduled run emits a workspace remove", async () =>
events.stop();
});
test("a local schedule run in an existing Paseo worktree stays in its project", async () => {
const repoDir = createGitRepo();
const source = await ctx.client.createWorkspace({
source: { kind: "worktree", cwd: repoDir, action: "branch-off" },
});
if (source.error || !source.workspace?.workspaceDirectory) {
throw new Error(source.error ?? "workspace/create returned no workspace directory");
}
const { id: sourceWorkspaceId, projectId, workspaceDirectory: cwd } = source.workspace;
const schedule = await createNewAgentSchedule({
prompt: "Say done.",
cadence: { type: "every", everyMs: 60_000 },
target: {
type: "new-agent",
config: { ...getFullAccessConfig("codex"), cwd, isolation: "local" },
},
runOnCreate: false,
});
await ctx.client.fetchWorkspaces({ subscribe: {} });
const events = collectLifecycleUpdates();
try {
const ran = await runScheduleOnce(schedule.id);
const agent = await archivedAgent(requireCompletedAgentId(ran));
const runWorkspaceId = agent.workspaceId!;
expect(runWorkspaceId).not.toBe(sourceWorkspaceId);
expect(agent.cwd).toBe(cwd);
await waitForWorkspaceUpsert(events.workspaceUpdates, runWorkspaceId);
expect(requireWorkspaceUpsert(events.workspaceUpdates, runWorkspaceId).projectId).toBe(
projectId,
);
await waitForWorkspaceRemove(events.workspaceUpdates, runWorkspaceId);
expect((await ctx.client.fetchWorkspaces()).entries.map(({ id }) => id)).toContain(
sourceWorkspaceId,
);
expect(existsSync(cwd)).toBe(true);
} finally {
events.stop();
}
});
test("worktree isolation creates a run worktree and archiveOnFinish removes it", async () => {
const repoDir = createGitRepo();
const expectedRoot = await getPaseoWorktreesRoot(
@@ -210,6 +210,38 @@ test("fresh non-git directory creates a directory workspace at the exact path",
expect(workspace.cwd).toBe(dir);
});
test("a managed worktree without a source workspace can inherit its repository project", async () => {
const repo = path.join(tmpDir, "repo");
const cwd = path.join(tmpDir, "worktree");
mkdirSync(path.join(cwd, ".git"), { recursive: true });
gitRoots.add(repo);
gitRoots.add(cwd);
const git = gitService();
const service = createWorkspaceProvisioningService({
unarchiveWorkspaceAgents: async () => {},
workspaceRegistry,
projectRegistry,
workspaceGitService: {
...git,
getCheckout: async (directory) => ({
...(await git.getCheckout(directory)),
isPaseoOwnedWorktree: directory === cwd,
mainRepoRoot: directory === cwd ? repo : null,
}),
},
isDirectory,
logger,
});
const project = await service.findOrCreateProjectForDirectory(repo);
const workspace = await service.createWorkspaceForDirectory(cwd, null, undefined, {
inheritWorktreeProject: true,
});
expect(workspace).toMatchObject({ cwd, projectId: project.projectId });
expect(await projectRegistry.list()).toEqual([project]);
});
test("workspace parentage survives reload and parent archive without changing placement", async () => {
const cwd = path.join(tmpDir, "plain");
const parent = await provisioning.createWorkspaceForDirectory(cwd);
@@ -43,6 +43,15 @@ export interface ImportWorkspaceResult<T> {
createdWorkspace: PersistedWorkspaceRecord | null;
}
interface CreateDirectoryWorkspaceContext {
expectsInitialAgent?: boolean;
workspaceId?: string;
metadata?: Record<string, JsonValue>;
background?: boolean;
callerWorkspaceId?: string;
inheritWorktreeProject?: boolean;
}
export interface CreateWorktreeWorkspaceInput {
sourceCwd: string;
projectId?: string;
@@ -72,13 +81,7 @@ export interface WorkspaceProvisioningService {
cwd: string,
title?: string | null,
projectId?: string,
context?: {
expectsInitialAgent?: boolean;
workspaceId?: string;
metadata?: Record<string, JsonValue>;
background?: boolean;
callerWorkspaceId?: string;
},
context?: CreateDirectoryWorkspaceContext,
): Promise<PersistedWorkspaceRecord>;
createWorkspaceForWorktree(
input: CreateWorktreeWorkspaceInput,
@@ -255,20 +258,30 @@ export function createWorkspaceProvisioningService(deps: {
cwd: string,
title?: string | null,
projectId?: string,
context?: {
expectsInitialAgent?: boolean;
workspaceId?: string;
metadata?: Record<string, JsonValue>;
background?: boolean;
callerWorkspaceId?: string;
},
context?: CreateDirectoryWorkspaceContext,
): Promise<PersistedWorkspaceRecord> {
const normalizedCwd = resolve(cwd);
const checkout = await workspaceGitService.getCheckout(normalizedCwd);
const project = projectId
? await refreshProjectKind(await requireActiveProject(projectId), normalizedCwd, checkout)
: // COMPAT(workspaceCreateMissingProjectId): added in v0.1.107, remove after 2027-01-15.
await findOrCreateProjectForDirectory(normalizedCwd);
let project: PersistedProjectRecord;
if (projectId) {
project = await refreshProjectKind(
await requireActiveProject(projectId),
normalizedCwd,
checkout,
);
} else if (
context?.inheritWorktreeProject &&
checkout.isPaseoOwnedWorktree &&
checkout.mainRepoRoot
) {
project = await resolveSourceProjectForWorktree({
sourceCwd: normalizedCwd,
repoRoot: checkout.mainRepoRoot,
});
} else {
// COMPAT(workspaceCreateMissingProjectId): added in v0.1.107, remove after 2027-01-15.
project = await findOrCreateProjectForDirectory(normalizedCwd);
}
const create = async () => {
const baseMetadata =
checkout.isGit && checkout.isPaseoOwnedWorktree