fix(workspaces): reconcile project-derived workspace bindings when a task moves projects (#23293)

## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - When a task is created in a project without explicit workspace
overrides, Paperclip derives `projectWorkspaceId`,
`executionWorkspaceSettings`, and a project-derived `workspaceSelection`
(`source: "project"`).
> - Moving that unbound task to a different project via `issues.update`
left the old project's `projectWorkspaceId` and `workspaceSelection`
behind.
> - Subsequent runs kept resolving back to the old project's workspace
and storage owner, causing repeated workspace validation or
storage-incompatibility failures even after the user moved the task to a
compatible project.
> - This pull request re-derives project-derived workspace bindings when
an unbound task moves projects, and reconciles already-moved unbound
tasks with stale `source: "project"` bindings at run admission while
preserving explicit workspace selections and bound execution workspaces.
> - The benefit is that moving an unbound task to a compatible project
immediately lets it run on the new project's workspace without hidden
stale-workspace failures.

## Linked Issues or Issue Description

Related: #17552, #22849.

**What happened?**

When a task was created in a project, `issues.create` automatically set
`projectWorkspaceId` and `workspaceSelection` with `source: "project"`.
If the task failed before binding an execution workspace
(`executionWorkspaceId === null`) and the user moved the task to a
different project (for example from a Core-storage project to a
plugin-storage project), `issues.update` updated `projectId` but left
`projectWorkspaceId` and `workspaceSelection` pointing to the old
project's workspace. Every subsequent run still resolved the old
project's workspace and storage policy and failed.

**Expected behavior**

- Moving an unbound task whose workspace binding is project-derived
(`source: "project"`, or a legacy `projectWorkspaceId` belonging to the
previous project with no `workspaceSelection`) re-derives
`projectWorkspaceId`, `workspaceSelection`, and
`executionWorkspaceSettings` from the new project (or clears them when
moving to no project or a sourceless project).
- Explicit workspace selections (`source !== "project"`), bound
execution workspaces (`executionWorkspaceId != null`), pending workspace
selections, and tasks with an active running run or pending workspace
recovery keep their current workspace binding.
- Tasks already moved on an older release with a stale `source:
"project"` binding pointing to a different project are reconciled at run
admission before workspace preparation.

**Steps to reproduce**

1. Create a Core-storage project A with a Git workspace and a
plugin-storage project B with a Git workspace.
2. Create an isolated task in project A (so `issues.create` sets
`workspaceSelection.source = "project"`).
3. Move the task to project B via `issueService.update(id, { projectId:
projectB.id })`.
4. Before this fix, `projectWorkspaceId` and
`workspaceSelection.selection.projectWorkspaceId` still point to project
A's workspace.

**Paperclip version or commit**

Reproduced against `origin/master` at `2d764bebc6ed`.

**Deployment mode**

Self-hosted / Cloud server. Verified locally on macOS arm64 with
embedded PostgreSQL.

## What Changed

- `server/src/services/project-move-workspace-reconciliation.ts`: add
focused helpers (`isProjectDerivedWorkspaceBindingStale`,
`deriveProjectDefaultWorkspaceBinding`,
`hasActiveTaskWorkspaceRunHolder`) shared between issue updates and run
admission.
- `server/src/services/execution-workspaces.ts`: add
`reconcileProjectWorkspaceForProjectMove` (used by `issues.update`) and
`reconcileStaleProjectWorkspaceAtAdmission` (used at run admission for
already-moved tasks with stale `source: "project"` bindings). Keeping
the shared reconciliation helpers in a sibling module avoids growing
`execution-workspaces.ts` and `issues.ts` unnecessarily.
- `server/src/services/issues.ts`: invoke
`reconcileProjectWorkspaceForProjectMove` inside `issues.update` after
`changesPrivacy` when `projectId` changes and no explicit workspace
fields were supplied.
- `server/src/services/heartbeat.ts`: invoke
`reconcileStaleProjectWorkspaceAtAdmission` alongside
`releaseAncestorWorkspaceBindingAtAdmission` at run admission, marking
`workspaceSelectionChanged = true` when reconciled.
- `server/src/__tests__/issue-project-move-workspace.test.ts`: add
red-green database regressions for moving to another project, moving to
a sourceless/null project, preserving explicit selections, preserving
bound `executionWorkspaceId`, deferring mid-run moves, and reconciling
already-moved tasks at admission.
- `server/src/__tests__/agent-project-access.test.ts`: use `source:
"explicit"` for the explicitly-retained-source denial test and add a
companion test verifying `source: "project"` re-derives on project move.

## Verification

- **Red (before production edits)**:
- `pnpm --filter @paperclipai/server exec vitest run
src/__tests__/issue-project-move-workspace.test.ts` failed on
`re-derives a project-derived workspace when moving an unbound task to
another project` and `clears a project-derived workspace when moving to
no project or a sourceless project` because `projectWorkspaceId` and
`workspaceSelection` stayed pinned to the old project.
- **Green (after production edits)**:
- `pnpm --filter @paperclipai/server exec vitest run
src/__tests__/issue-project-move-workspace.test.ts
src/__tests__/agent-project-access.test.ts` — 20/20 passed.
  - `pnpm --filter @paperclipai/server typecheck` — passed.
  - `pnpm --filter @paperclipai/server build` — passed.
- `pnpm --filter @paperclipai/server exec vitest run
src/services/heartbeat/workspace-preparation.test.ts -t "names
incompatible Core project storage|rejects Core Git worktree
preparation"` — 2/2 passed.
- `pnpm --filter @paperclipai/server exec vitest run
src/services/task-interruption-presentation.test.ts` — 82/82 passed.
- `pnpm verify --explain` selected 1978/2333 files due to touching
central server services; required GitHub Actions CI supplies the broad
suite coverage.
- Live deployment and recovery of previously affected tasks on hosted
instances are outside this PR's scope.

## Risks

- Low risk. Reconciliation is gated to unbound tasks
(`executionWorkspaceId == null`, `workspacePendingSelection == null`)
with no active run holder or pending workspace recovery, and never
alters explicit (`source !== "project"`) workspace selections or bound
files.
- Moving a task to a project whose trust boundary currently denies the
assigned agent will not fail the move; execution access remains enforced
at run admission.

## Model Used

- Google Gemini (Antigravity agentic coding assistant) with tool use,
shell execution, and local test verification. Exact deployment model ID
and context window are not exposed to the session.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge
This commit is contained in:
Dotta authored and GitHub committed 2026-11-30 15:44:28 +00:00
1 parent 2d764bebc6
commit e7ed0c2f93
6 files changed
+865 -45

No files matched your search

+44 -28
View File
@@ -38,6 +38,7 @@ import { assertNoCaseHandoffMutation, cancelCaseHandoffsForIssue, completeTermin
import { measureRunPhase } from "./execution-preparation-timing.js";
import { automaticReadRecoveryCleanupCondition, directHumanCommentCondition, humanCreatedIssueCondition, pendingUserRequestCondition, pendingUserRequestRowCondition, touchedByUserCondition, userParticipationActivityCondition } from "./issue-participation.js";
import { resolveProjectDefaultWorkspaceId } from "./project-default-workspace.js";
import { deriveProjectDefaultWorkspaceBinding } from "./project-move-workspace-reconciliation.js";
import { type AuthorizationActor, canActorReadIssuePrivacy, executionWorkspaceReadSqlCondition, projectReadSqlCondition } from "./authorization.js";
import { assertExecutionWorkspaceRecordAccess, assertTaskWorkspaceSourceProjectAccess } from "./task-workspace-access.js";
import { type TaskWorkspaceSelection, type TaskWorkspaceIntent } from "@paperclipai/shared";
@@ -11013,32 +11014,19 @@ export function issueService(db: Db) {
) as Record<string, unknown> | null;
}
if (!executionWorkspaceId && !projectWorkspaceId && issueData.projectId && workspaceIntent?.selection.kind !== "task_directory") {
const project = await tx
.select({
executionWorkspacePolicy: projects.executionWorkspacePolicy,
})
.from(projects)
.where(
and(
eq(projects.id, issueData.projectId),
eq(projects.companyId, companyId),
),
)
.then((rows) => rows[0] ?? null);
const projectPolicy = parseProjectExecutionWorkspacePolicy(
project?.executionWorkspacePolicy,
);
projectWorkspaceId = await resolveProjectDefaultWorkspaceId(tx, {
companyId, projectId: issueData.projectId, policy: projectPolicy,
const derived = await deriveProjectDefaultWorkspaceBinding(tx, {
companyId,
projectId: issueData.projectId,
assigneeAgentId: issueData.assigneeAgentId,
assigneeAdapterOverrides: issueData.assigneeAdapterOverrides,
executionPolicy: issueData.executionPolicy,
isolatedWorkspacesEnabled,
existingExecutionWorkspaceSettings: executionWorkspaceSettings,
resolveSourceWorkspaceStrategy: (input, reader) =>
executionWorkspaceService(db).resolveSourceWorkspaceStrategy(input, reader),
});
if (projectWorkspaceId && isolatedWorkspacesEnabled) {
const sourceStrategy = await executionWorkspaceService(db).resolveSourceWorkspaceStrategy({ companyId,
assigneeAgentId: issueData.assigneeAgentId, assigneeAdapterOverrides: issueData.assigneeAdapterOverrides,
executionWorkspaceSettings, executionWorkspacePolicy: projectPolicy, sourceWorkspaceId: projectWorkspaceId,
typedSelection: false, projectId: issueData.projectId, executionPolicy: issueData.executionPolicy }, tx);
if (sourceStrategy === "git_worktree") workspaceIntent = { version: 1, source: "project",
selection: { kind: "configured_source", projectWorkspaceId, mode: "managed_isolated" } };
}
projectWorkspaceId = derived.projectWorkspaceId;
if (derived.workspaceSelection) workspaceIntent = derived.workspaceSelection;
}
if (projectWorkspaceId && workspaceSelectionActor) {
const [readableSource] = await tx.select({ id: projectWorkspaces.id, projectId: projectWorkspaces.projectId, executionWorkspacePolicy: projects.executionWorkspacePolicy }).from(projectWorkspaces)
@@ -12144,8 +12132,12 @@ export function issueService(db: Db) {
}
Object.assign(patch, transition.patch);
}
if (issueData.executionWorkspaceId !== undefined || issueData.projectWorkspaceId !== undefined ||
issueData.executionWorkspacePreference !== undefined || issueData.executionWorkspaceSettings !== undefined) {
const explicitWorkspaceFieldsChanged =
issueData.executionWorkspaceId !== undefined ||
issueData.projectWorkspaceId !== undefined ||
issueData.executionWorkspacePreference !== undefined ||
issueData.executionWorkspaceSettings !== undefined;
if (explicitWorkspaceFieldsChanged) {
Object.assign(patch, await executionWorkspaceService(db).prepareTaskWorkspaceUpdate({
companyId: receiptExisting.companyId, issueId: receiptExisting.id, patch,
}, tx));
@@ -12188,6 +12180,30 @@ export function issueService(db: Db) {
patch.privacyRootIssueId = receiptExisting.id;
}
}
const effectiveNextProjectId = patch.projectId !== undefined ? patch.projectId : receiptExisting.projectId;
if (effectiveNextProjectId !== receiptExisting.projectId && !explicitWorkspaceFieldsChanged) {
Object.assign(
patch,
await executionWorkspaceService(db).reconcileProjectWorkspaceForProjectMove(
{
companyId: receiptExisting.companyId,
task: receiptExisting,
nextProjectId: effectiveNextProjectId ?? null,
assigneeAgentId: nextAssigneeAgentId,
assigneeAdapterOverrides:
issueData.assigneeAdapterOverrides !== undefined
? issueData.assigneeAdapterOverrides
: receiptExisting.assigneeAdapterOverrides,
executionPolicy:
issueData.executionPolicy !== undefined
? issueData.executionPolicy
: receiptExisting.executionPolicy,
isolatedWorkspacesEnabled,
},
tx,
),
);
}
if (actorAgentId && actorRunId) {
// Recheck under a run lock: a request admitted before Stop must not
// commit a late Done after cancellation revoked its credentials.
@@ -12368,7 +12384,7 @@ export function issueService(db: Db) {
await completeTerminalCaseHandoffsForIssue(tx, { companyId: updated.companyId, issueId: updated.id,
publications: activityPublications });
}
if (workspaceSelectionActor && updated.workspaceBindingRevision !== receiptExisting.workspaceBindingRevision) {
if (explicitWorkspaceFieldsChanged && workspaceSelectionActor && updated.workspaceBindingRevision !== receiptExisting.workspaceBindingRevision) {
await executionWorkspaceService(db).assertTaskWorkspaceUpdateAccess({
task: updated, actor: workspaceSelectionActor,
}, tx);