Skip to content

The open-source app everyone uses to manage agents at work

fix: retain project defaults in partial workspace overrides (#14502)

Devin Foleyea371b9684ee
diff --git a/doc/DEVELOPING.md b/doc/DEVELOPING.md
index 317bdbbefe..0bca5513e5 100644
--- a/doc/DEVELOPING.md
+++ b/doc/DEVELOPING.md
@@ -992,6 +992,8 @@ eval "$(npx paperclipai worktree env)"
For project execution worktrees, Paperclip can also run a project-defined provision command after it creates or reuses an isolated git worktree. Configure this on the project's execution workspace policy (`workspaceStrategy.provisionCommand`). The command runs inside the derived worktree and receives `PAPERCLIP_WORKSPACE_*`, `PAPERCLIP_PROJECT_ID`, `PAPERCLIP_AGENT_ID`, and `PAPERCLIP_ISSUE_*` environment variables so each repo can bootstrap itself however it wants.
+An issue's partial `workspaceStrategy` inherits omitted fields from the enabled project's strategy when both use the same type. For example, an issue can override `baseRef` without losing the project's provision, runtime provision, or teardown commands. An explicit value, including `null` or an empty string, replaces the project value. Clearing a command restores the runtime's usual default behavior; use `provisionCommand: "true"` for an explicit no-op. A different strategy type or a disabled project policy does not supply these defaults. An issue's `existingBranch` pin also excludes the project's `branchTemplate`.
+
An issue can pin its isolated worktree to an exact pre-existing branch instead of a template-derived one — the contract PR-preparation tasks use. Set the issue's `executionWorkspaceSettings` to `{ "mode": "isolated_workspace", "workspaceStrategy": { "type": "git_worktree", "existingBranch": "<branch>" } }`. The validator requires isolated mode plus a `git_worktree` strategy and rejects `branchTemplate` alongside `existingBranch`. At dispatch the runtime attaches (never creates, renames, fast-forwards, or resets) that branch: it reuses a registered worktree that already has the branch checked out (including legacy `.worktrees/` paths), otherwise it attaches the branch under the managed worktree parent. A missing branch, an occupied worktree path on another branch, or a non-worktree strategy fails closed with a `workspace_validation_failed` error instead of falling back to the shared checkout or a derived branch, and an inherited `reuse_existing` workspace binding on a different branch is ignored in favor of realizing the pinned branch.
Heavier setup that is only needed by a managed runtime service can use `workspaceStrategy.runtimeProvisionCommand`. Paperclip runs this command lazily before spawning the first service in a start batch, serializes concurrent provisioning for the same workspace, and records the attempt as `workspace_runtime_provision`. The command receives the same workspace environment as `provisionCommand` and should be idempotent because later service-start batches invoke it again.
diff --git a/server/src/__tests__/execution-workspace-policy.test.ts b/server/src/__tests__/execution-workspace-policy.test.ts
index 07f6179731..e5cf70cf01 100644
--- a/server/src/__tests__/execution-workspace-policy.test.ts
+++ b/server/src/__tests__/execution-workspace-policy.test.ts
@@ -300,6 +300,107 @@ describe("execution workspace policy helpers", () => {
});
});
+ describe("partial issue workspace strategies", () => {
+ const projectStrategy = {
+ type: "git_worktree" as const,
+ baseRef: "origin/main",
+ branchTemplate: "{{issue.identifier}}-{{slug}}",
+ worktreeParentDir: ".paperclip/worktrees",
+ provisionCommand: "true",
+ runtimeProvisionCommand: "npm run setup:runtime",
+ teardownCommand: "npm run teardown",
+ };
+
+ function resolveStrategy(
+ strategy: Record<string, unknown>,
+ enabled = true,
+ ) {
+ return buildExecutionWorkspaceAdapterConfig({
+ agentConfig: { workspaceStrategy: { type: "git_worktree", provisionCommand: "agent-setup" } },
+ projectPolicy: parseProjectExecutionWorkspacePolicy({
+ enabled,
+ defaultMode: "isolated_workspace",
+ workspaceStrategy: projectStrategy,
+ }),
+ issueSettings: parseIssueExecutionWorkspaceSettings({
+ mode: "isolated_workspace",
+ workspaceStrategy: strategy,
+ }),
+ mode: "isolated_workspace",
+ legacyUseProjectWorkspace: null,
+ }).workspaceStrategy;
+ }
+
+ it("retains project hooks when an issue changes only its base branch", () => {
+ expect(resolveStrategy({ type: "git_worktree", baseRef: "origin/release" })).toEqual({
+ ...projectStrategy,
+ baseRef: "origin/release",
+ });
+ });
+
+ it.each(["npm run issue-setup", "", null])("honors an explicit provisioning override of %j", (provisionCommand) => {
+ expect(resolveStrategy({ type: "git_worktree", provisionCommand })).toEqual({
+ ...projectStrategy,
+ provisionCommand,
+ });
+ });
+
+ it("preserves explicit null clears through persisted JSON parsing", () => {
+ const strategy = {
+ type: "git_worktree",
+ baseRef: null,
+ branchTemplate: null,
+ worktreeParentDir: null,
+ provisionCommand: null,
+ runtimeProvisionCommand: null,
+ teardownCommand: null,
+ };
+ expect(resolveStrategy(strategy)).toEqual(strategy);
+ });
+
+ it.each(["cloud_sandbox", "adapter_managed", "project_primary"])("does not carry project hooks into %s", (type) => {
+ expect(resolveStrategy({ type })).toEqual({ type });
+ });
+
+ it("does not inherit a disabled project strategy", () => {
+ expect(resolveStrategy({ type: "git_worktree", baseRef: "origin/release" }, false)).toEqual({
+ type: "git_worktree",
+ baseRef: "origin/release",
+ });
+ expect(resolveStrategy({}, false)).toEqual({
+ type: "git_worktree",
+ provisionCommand: "agent-setup",
+ });
+ });
+
+ it("keeps project hooks for an exact branch pin without inheriting a branch template", () => {
+ const resolved = resolveStrategy({ type: "git_worktree", existingBranch: "fix/existing" });
+ expect(resolved).toEqual({
+ ...projectStrategy,
+ branchTemplate: undefined,
+ existingBranch: "fix/existing",
+ });
+ expect(issueExecutionWorkspaceSettingsSchema.safeParse({
+ mode: "isolated_workspace",
+ workspaceStrategy: resolved,
+ }).success).toBe(true);
+ });
+
+ it("does not mutate the project or issue strategy", () => {
+ const issueStrategy = { type: "git_worktree" as const, baseRef: "origin/release" };
+ const result = buildExecutionWorkspaceAdapterConfig({
+ agentConfig: {},
+ projectPolicy: { enabled: true, workspaceStrategy: Object.freeze({ ...projectStrategy }) },
+ issueSettings: { workspaceStrategy: Object.freeze(issueStrategy) },
+ mode: "isolated_workspace",
+ legacyUseProjectWorkspace: null,
+ });
+ expect(result.workspaceStrategy).not.toBe(issueStrategy);
+ expect(issueStrategy).toEqual({ type: "git_worktree", baseRef: "origin/release" });
+ expect(projectStrategy.baseRef).toBe("origin/main");
+ });
+ });
+
it("preserves project authorization policy for trust-preset resolution", () => {
expect(parseProjectExecutionWorkspacePolicy({
enabled: true,
diff --git a/server/src/__tests__/workspace-runtime.test.ts b/server/src/__tests__/workspace-runtime.test.ts
index ececaebf9e..fd0caefa8e 100644
--- a/server/src/__tests__/workspace-runtime.test.ts
+++ b/server/src/__tests__/workspace-runtime.test.ts
@@ -24,6 +24,11 @@ import {
workspaceRuntimeServices,
} from "@paperclipai/db";
import { eq } from "drizzle-orm";
+import {
+ buildExecutionWorkspaceAdapterConfig,
+ parseIssueExecutionWorkspaceSettings,
+ parseProjectExecutionWorkspacePolicy,
+} from "../services/execution-workspace-policy.ts";
import {
buildWorkspaceRuntimeDesiredStatePatch,
cleanupExecutionWorkspaceArtifacts,
@@ -869,6 +874,52 @@ describe("realizeExecutionWorkspace", () => {
expect(second.branchName).toBe(first.branchName);
});
+ it("retains the project provision command when an issue overrides its base branch", async () => {
+ const repoRoot = await createTempRepo();
+ await fs.mkdir(path.join(repoRoot, "scripts"));
+ await fs.writeFile(
+ path.join(repoRoot, "scripts", "provision-worktree.sh"),
+ "#!/usr/bin/env bash\necho 'Unexpected repository provision fallback' >&2\nexit 1\n",
+ );
+ await runGit(repoRoot, ["add", "scripts/provision-worktree.sh"]);
+ await runGit(repoRoot, ["commit", "-m", "Add fallback provisioner"]);
+ await runGit(repoRoot, ["branch", "release"]);
+ const config = buildExecutionWorkspaceAdapterConfig({
+ agentConfig: {},
+ projectPolicy: parseProjectExecutionWorkspacePolicy({
+ enabled: true,
+ defaultMode: "isolated_workspace",
+ workspaceStrategy: { type: "git_worktree", baseRef: "main", provisionCommand: "true" },
+ }),
+ issueSettings: parseIssueExecutionWorkspaceSettings({
+ mode: "isolated_workspace",
+ workspaceStrategy: { type: "git_worktree", baseRef: "release" },
+ }),
+ mode: "isolated_workspace",
+ legacyUseProjectWorkspace: null,
+ });
+ try {
+ const workspace = await realizeExecutionWorkspace({
+ base: {
+ baseCwd: repoRoot,
+ source: "project_primary",
+ projectId: "project-1",
+ workspaceId: "workspace-1",
+ repoUrl: null,
+ repoRef: "HEAD",
+ },
+ config,
+ issue: { id: "issue-1", identifier: "TEST-1", title: "Keep project setup" },
+ agent: { id: "agent-1", name: "Test agent", companyId: "company-1" },
+ });
+ expect(workspace.created).toBe(true);
+ expect(workspace.baseRefSha).toBe(await readGit(repoRoot, ["rev-parse", "release"]));
+ await expect(fs.stat(path.join(workspace.cwd, ".git"))).resolves.toBeTruthy();
+ } finally {
+ await fs.rm(repoRoot, { recursive: true, force: true });
+ }
+ });
+
it("defaults the repo-provided worktree provisioner for git worktree strategies", async () => {
const repoRoot = await createTempRepo();
await fs.mkdir(path.join(repoRoot, "scripts"), { recursive: true });
diff --git a/server/src/services/execution-workspace-policy.ts b/server/src/services/execution-workspace-policy.ts
index b430de18cc..dd36825a2a 100644
--- a/server/src/services/execution-workspace-policy.ts
+++ b/server/src/services/execution-workspace-policy.ts
@@ -38,17 +38,17 @@ function parseExecutionWorkspaceStrategy(raw: unknown): ExecutionWorkspaceStrate
}
return {
type,
- ...(typeof parsed.baseRef === "string" ? { baseRef: parsed.baseRef } : {}),
- ...(typeof parsed.branchTemplate === "string" ? { branchTemplate: parsed.branchTemplate } : {}),
+ ...(typeof parsed.baseRef === "string" || parsed.baseRef === null ? { baseRef: parsed.baseRef } : {}),
+ ...(typeof parsed.branchTemplate === "string" || parsed.branchTemplate === null ? { branchTemplate: parsed.branchTemplate } : {}),
...(typeof parsed.existingBranch === "string" && parsed.existingBranch.trim().length > 0
? { existingBranch: parsed.existingBranch.trim() }
: {}),
- ...(typeof parsed.worktreeParentDir === "string" ? { worktreeParentDir: parsed.worktreeParentDir } : {}),
- ...(typeof parsed.provisionCommand === "string" ? { provisionCommand: parsed.provisionCommand } : {}),
- ...(typeof parsed.runtimeProvisionCommand === "string"
+ ...(typeof parsed.worktreeParentDir === "string" || parsed.worktreeParentDir === null ? { worktreeParentDir: parsed.worktreeParentDir } : {}),
+ ...(typeof parsed.provisionCommand === "string" || parsed.provisionCommand === null ? { provisionCommand: parsed.provisionCommand } : {}),
+ ...(typeof parsed.runtimeProvisionCommand === "string" || parsed.runtimeProvisionCommand === null
? { runtimeProvisionCommand: parsed.runtimeProvisionCommand }
: {}),
- ...(typeof parsed.teardownCommand === "string" ? { teardownCommand: parsed.teardownCommand } : {}),
+ ...(typeof parsed.teardownCommand === "string" || parsed.teardownCommand === null ? { teardownCommand: parsed.teardownCommand } : {}),
};
}
@@ -418,11 +418,18 @@ export function buildExecutionWorkspaceAdapterConfig(input: {
if (hasWorkspaceControl) {
if (input.mode === "isolated_workspace") {
- const strategy =
- input.issueSettings?.workspaceStrategy ??
- input.projectPolicy?.workspaceStrategy ??
+ const projectStrategy = projectHasPolicy ? input.projectPolicy?.workspaceStrategy : undefined;
+ const issueStrategy = input.issueSettings?.workspaceStrategy;
+ // An issue that changes its branch still needs the project's setup hooks.
+ // Do not carry those defaults into a different execution strategy.
+ const strategy = issueStrategy && projectStrategy?.type === issueStrategy.type
+ ? { ...projectStrategy, ...issueStrategy }
+ : issueStrategy ?? projectStrategy ??
parseExecutionWorkspaceStrategy(nextConfig.workspaceStrategy) ??
({ type: "git_worktree" } satisfies ExecutionWorkspaceStrategy);
+ if (issueStrategy?.existingBranch && issueStrategy.branchTemplate === undefined && strategy !== issueStrategy) {
+ delete strategy.branchTemplate;
+ }
nextConfig.workspaceStrategy = strategy as unknown as Record<string, unknown>;
} else {
delete nextConfig.workspaceStrategy;