From 7fd98682caf72bb61769dd938ddebb41fdae7a9a Mon Sep 17 00:00:00 2001 From: Dotta <34892728+cryppadotta@users.noreply.github.com> Date: Sat, 7 Nov 2026 18:34:09 -0600 Subject: [PATCH] fix(workspaces): give every subtask its own execution workspace (#19860) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - Agents split work into child tasks, and each task runs in an execution workspace (a checkout, branch, or sandbox folder) > - Since early 2026, a new child task used its parent's realized workspace by default: the same checkout, branch and sandbox > - A parent and its child often run at the same time. Two writers in one folder lose each other's files, the waking parent is cancelled with `workspace_busy`, and sandbox lease ownership crosses from the parent into the child, so retained-file recovery refuses the workspace as ambiguous > - The product rule is simple: a subtask gets its own workspace, independent of the parent's saved files. Paperclip never needs to carry a parent's unmerged changes into a child > - This pull request makes that the only behaviour on every creation path, and moves existing shared children to their own workspace on their next run, with no user or admin action > - The benefit is that parents and children never block or overwrite each other, and recovery ownership stays with one task ## Linked Issues or Issue Description **What happened?** A child task inherited its parent's realized execution workspace by default (`createChild` defaulted to `executionWorkspaceInheritanceMode: "linkage"`, and `issueService.create` fell back from `inheritExecutionWorkspaceFromIssueId` to `parentId`). When the parent woke while the child wrote to the shared workspace, the parent run was cancelled with `workspace_busy`. In sandbox environments, the lease ownership chain crossed from the parent into the child, and retained-file recovery refused the workspace with `allocation_ownership_ambiguous`. **Expected behavior** Every new child task gets its own execution workspace, realized from the project base exactly as a top-level task in that project. It never uses the parent's checkout, branch, saved snapshot or unmerged changes. A parent and its descendant never share a workspace by any route. **Steps to reproduce** 1. Run a parent task in a project with isolated (git worktree) or sandbox workspaces, so the parent has a realized execution workspace. 2. From the parent run, create a child task with only `parentId` (or through the runner `create_task` tool, chat suggestions, decision effects, plugins, or the New sub-task dialog). 3. See that the child's `executionWorkspaceId` is the parent's, with `executionWorkspacePreference: "reuse_existing"`. 4. Wake the parent while the child runs in a sandbox environment. The parent run is cancelled with `workspace_busy`. **Additional context** Refs #9233 (the existing `strategy_only` opt-out). The opt-out `executionWorkspaceInheritanceMode: "strategy_only"` already existed for accepted-plan decomposition. This change makes that behaviour (tightened to drop a pinned existing branch) the only behaviour. ## What Changed - **One rule for every creation path (`server/src/services/issues.ts`).** `issueService.create` still uses the parent (or `inheritExecutionWorkspaceFromIssueId`) as a source for the project, project workspace and workspace *strategy*, but it never copies the source's `executionWorkspaceId`, `reuse_existing` preference, or pinned existing branch. All server paths that create a task go through `create()`/`createChild()`: issue routes (`POST /issues`, `POST /issues/:id/children`, the run-bound default parent and run workspace source), runner `create_task`, chat suggestion acceptance, native status decisions, accepted-plan decomposition, decision effects, plugins (`ctx.issues.create`), external agents, watchdog follow-ups, monitor recovery issues, case handoffs and email channels. - **Explicit ancestor workspaces are dropped.** If a create names an `executionWorkspaceId` (or an `existing` workspace selection) that an ancestor of the new task created or is bound to, `create()` drops it and keeps only the strategy. Older clients and skills that copied the parent's id keep working and get an independent workspace. - **`createChild` no longer has a "linkage" mode.** `executionWorkspaceInheritanceMode` is still accepted from internal callers and is ignored. - **`inheritExecutionWorkspaceFromIssueId` decision.** I kept the field for API compatibility, but it now copies only the source issue's project and workspace strategy. I removed its workspace-sharing behaviour for every pair, not only parent/child: the source task and its follow-up can run at the same time (the runner, watchdog and status-decision callers all create the follow-up while the source run is live), so sharing would again let two tasks write one folder. Explicit sharing between unrelated tasks stays available through an explicit `existing` workspace selection. - **New `server/src/services/task-workspace-lineage.ts`.** `buildIndependentTaskWorkspaceSettings` (strategy only: mode, environment, runtime, network egress, shared-workspace concurrency, base ref, branch template, commands; no `reuse_existing`, no `operator_branch`, no `existingBranch`), `loadTaskLineageIssueIds` (the parent chain plus, for a task that depth placement put beside its requester, the requester's chain via `privacyParentIssueId`) and `isAncestorExecutionWorkspace` (the workspace's `sourceIssueId` is in that lineage, or a lineage issue is bound to it). A child in another project does not take the parent's strategy. - **Upgrade-safe rebind at admission.** `executionWorkspaceService.releaseAncestorWorkspaceBindingAtAdmission` runs at the same admission point as the pending workspace selection (`heartbeat.ts`). For a child (or a flattened task beside its requester) that an older release bound to an ancestor's workspace, it clears the child's binding (keeps the project workspace and strategy, bumps the binding revision) so the run realizes the child's own workspace. It does nothing while another run of the child is running, while the child's earlier run still owns workspace finalization or recovery, for native restart/lease-owner recovery, for a process-loss retry, or for a Retry that keeps the current workspace. The parent and its workspace are never changed. - **Projects without a project workspace (per-task repository acquisition).** A parent with no project workspace worked in its own task files, so its settings (for example `isolated_workspace`) do not describe the project base. A child of such a parent, and an older child released from one, takes the project's defaults exactly like a top-level task, instead of an isolated mode that would resolve to a git worktree with no git base ("needs a project / project workspace"). When the child's new workspace is created at admission, `executionWorkspaceRepositoryService.prepareForAdmission` repeats the parent's (or requester's) repository requests as the child's own pending requests (identity, URL, catalog id and requested ref only; no pinned commit, branch or files). The normal acquisition path then clones a fresh copy into the child's task files with the child's own access checks. This runs only for a workspace created by that admission, so existing tasks never gain repositories. - **UI.** The New sub-task dialog no longer pre-selects the parent's workspace. `buildSubIssueDefaults` drops `executionWorkspaceId` and `reuse_existing`, and derives the parent's independent mode (`shared_workspace`, `isolated_workspace` or `agent_default`) from its preference, saved settings or current workspace. The "This sub-task will no longer use the parent task workspace" warning is removed. - **Contracts and docs.** Shared validator and plugin SDK comments describe the new meaning of `inheritExecutionWorkspaceFromIssueId`. Updated `doc/execution-semantics.md` (new rule in ยง6 and a wording fix in the retained-sandbox paragraph), `doc/SPEC-implementation.md` (child workspace rule, requester keeps *strategy* inheritance, watchdog follow-ups), `doc/SPEC.md` (task workspace ownership), `docs/specs/external-task-protocol.md`, `skills/paperclip/SKILL.md` (step 9 and the workspace bullet), `skills/paperclip/references/issue-workspaces.md`, the CEO onboarding `HEARTBEAT.md`, and the plugin SDK README. ### Previous-release story (`rules/upgrade-safe-run-state.mdc`) - **Migrated on next admission.** A child that the previous release bound to its parent's (or any ancestor's) execution workspace keeps that binding until its next normal admission. Then the server clears the binding and the run realizes the child's own workspace from the project base. No schema migration is needed. No user or admin action is needed. - **In-flight writers finish first.** A running child is not interrupted. Its current run, its native restart recovery (admitted native input or lease owner), a process-loss retry, and a Retry that keeps the current workspace all finish in the shared workspace. While a child's earlier run is still running or its workspace finalization or recovery is unsettled, the binding stays and a later admission releases it. - **No files are dropped.** The release changes only the child's binding row. The parent keeps its binding, and the shared workspace row, folder, branch and saved snapshots are not touched. Anything the child left there stays in the parent's workspace. - **No new refusal codes.** Saved run identity (native execution input, leases, checkpoints) is not changed or refused; the rebind only affects new admissions. ## Verification - New `server/src/__tests__/subtask-workspace-independence.test.ts` (embedded Postgres): `create` with `parentId`, `createChild` with no mode / `"linkage"` / `"strategy_only"`, `inheritExecutionWorkspaceFromIssueId` pointing at the parent, explicit parent and grandparent `executionWorkspaceId`, a non-child follow-up, and unchanged top-level defaults and explicit top-level reuse. 7 creation cases fail on `master` and pass with this change. Admission cases: an old-release child is rebound while its parent is running and the parent keeps its workspace; a running child is not rebound (deploy lands mid-run); an unrelated explicit share is kept; the parent is never rebound. - New heartbeat case in `server/src/__tests__/heartbeat-workspace-busy.test.ts`: an old-release child bound to its running parent's workspace in a sandbox environment runs to `succeeded` with no `workspace_busy`, gets a new workspace, and the parent run and binding stay unchanged. It fails on `master` (the child waits with `workspace_busy`). - Updated tests that asserted the old inheritance in `issues-service.test.ts`, `task-workspace-binding.test.ts`, `chat-project-tools.test.ts`, `task-workspace-child-trust.test.ts`, `heartbeat-git-restore-point-resume.test.ts` (a child bound to a closed parent's expired workspace now starts from the project base and leaves the parent's restore point alone), and `ui/src/lib/subIssueDefaults.test.ts`. Remote-workspace reservation, continuation and writer-wait suites now seed the older-release shared shape directly (or an unrelated explicit share), so those safety paths stay covered. - `npx vitest run` on the changed suites and about 45 related server suites (heartbeat workspace, recovery admission, issue routes, routines, plugins), `ui` `subIssueDefaults` and `NewIssueDialog` tests, `pnpm check:token-gates`, `pnpm -r typecheck`. - New `server/src/__tests__/heartbeat-subtask-acquired-repository.test.ts`: a project with no project workspace, a parent whose task files hold an acquired repository with an unmerged change, and a child (new, and the older-release shape bound to the parent's workspace) that runs to `succeeded` in its own task files with a fresh clone at the project base and its own ready repository receipt. The parent's checkout keeps its change. Both cases fail on the previous head with "needs a project / project workspace". ## Risks - **Project-level configuration still applies to children exactly as to top-level tasks.** In a project configured for `shared_workspace`, every task (top-level or child) uses the project's one folder under the existing shared-workspace concurrency policy. A branch that the project itself pins applies to every task in the project. This PR removes only the parent-to-child carry-over of the realized workspace; it does not change those project defaults. - **Standalone follow-ups that an older release bound through `inheritExecutionWorkspaceFromIssueId`** are migrated only when the source is in their recorded lineage (parent or privacy parent). Others keep their explicit share and the existing writer-wait serialization. - **Behaviour change for agents that relied on a shared checkout.** A child or follow-up no longer sees the parent's unpushed or uncommitted changes. Agents must push or merge code they want a subtask to build on. The skill and onboarding text now say this. - **Explicit ancestor workspace ids are silently dropped** instead of rejected, so older clients do not break. The task gets an independent workspace. - **First run of a migrated child starts from the project base.** If an older child had uncommitted work in the parent's workspace, that work stays in the parent's workspace and is not copied into the child's new workspace. - **Admission adds one indexed read** for tasks with a parent and a bound workspace; the locking transaction and ancestor query run only when the bound workspace was created by another issue. ## Model Used - Claude Opus 5.5 (Anthropic), model ID `claude-opus-5-5`, used through Claude Code with tool use (file edits, shell, test execution) and extended reasoning. ## 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 - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge ๐Ÿค– Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 --- doc/SPEC-implementation.md | 5 +- doc/SPEC.md | 2 +- doc/execution-semantics.md | 46 +- docs/specs/external-task-protocol.md | 7 +- packages/plugins/sdk/README.md | 3 +- packages/plugins/sdk/src/types.ts | 1 + packages/shared/src/validators/issue.ts | 4 + .../src/__tests__/chat-project-tools.test.ts | 10 +- .../git-restore-point-expiration.test.ts | 8 +- ...heartbeat-git-restore-point-resume.test.ts | 34 +- .../heartbeat-remote-workspace-busy.test.ts | 6 +- ...rtbeat-subtask-acquired-repository.test.ts | 166 +++++++ .../heartbeat-workspace-busy.test.ts | 38 ++ server/src/__tests__/issues-service.test.ts | 36 +- .../subtask-workspace-independence.test.ts | 418 ++++++++++++++++++ .../src/__tests__/task-workspace-api.test.ts | 4 +- .../__tests__/task-workspace-binding.test.ts | 16 +- .../task-workspace-child-trust.test.ts | 7 +- server/src/onboarding-assets/ceo/HEARTBEAT.md | 2 +- .../execution-workspace-repositories.ts | 52 ++- server/src/services/execution-workspaces.ts | 52 ++- server/src/services/heartbeat.ts | 9 +- .../heartbeat/workspace-preparation.ts | 10 +- server/src/services/issues.ts | 169 ++++--- .../remote-workspace-reservation.test.ts | 18 +- .../remote-workspaces.integration.test.ts | 7 +- server/src/services/task-workspace-lineage.ts | 188 ++++++++ skills/paperclip/SKILL.md | 4 +- .../paperclip/references/issue-workspaces.md | 2 +- ui/src/components/NewIssueDialog.tsx | 21 +- ui/src/context/DialogContext.tsx | 1 - ui/src/lib/subIssueDefaults.test.ts | 8 +- ui/src/lib/subIssueDefaults.ts | 37 +- 33 files changed, 1185 insertions(+), 206 deletions(-) create mode 100644 server/src/__tests__/heartbeat-subtask-acquired-repository.test.ts create mode 100644 server/src/__tests__/subtask-workspace-independence.test.ts create mode 100644 server/src/services/task-workspace-lineage.ts diff --git a/doc/SPEC-implementation.md b/doc/SPEC-implementation.md index 803ebd93e39..637f509072b 100644 --- a/doc/SPEC-implementation.md +++ b/doc/SPEC-implementation.md @@ -444,8 +444,9 @@ retain their titles and default to no generation request. - identifier fields: `issue_number`, `identifier` - origin fields: `origin_kind`, `origin_id`, `origin_run_id`, `origin_fingerprint` - Creation stores the actor run in `origin_run_id` unless an explicit origin run is supplied. `GET /api/companies/:companyId/issues?createdFromIssueId=` selects tasks whose `origin_issue_id` is that source task. Only for rows without a recorded `origin_issue_id`, it falls back to tasks created by runs bound to the source task, using native run issue identity or persisted legacy task context; a recorded origin suppresses that fallback, so the list agrees with `createdFrom`. Historical rows without an origin run may use their recorded creation activity; comments and shared creators do not establish provenance. Source, run, activity and result are company-scoped. +- A child task never shares its parent's (or any ancestor's) execution workspace. It inherits company, project, goal, privacy and workspace strategy, and its first run realizes its own workspace from the project base. `inheritExecutionWorkspaceFromIssueId` copies only the source issue's project and strategy; an explicit `executionWorkspaceId` that an ancestor created or is bound to is dropped. A child that an older release bound to its parent's workspace is moved to its own on its next admission once no earlier run of it is still running or settling (see `doc/execution-semantics.md`). - Delegated follow-ups keep their structural parent. When an agent run bound to an ordinary execution task creates an issue and omits `parentId`, the server defaults `parentId` to that run's task and records `parentDefaultedFromRunIssue: true` on the `issue.created` activity. An explicit `parentId: null` keeps the task standalone. The default never applies to conversation runs (chat handoffs stay top-level), to watchdog follow-ups, to board/user creates, to a run task the agent may not mutate, when the default parent would create a delegation cycle with the requested assignee, or when the agent may not create a child under the default parent (for example a protected assignment policy without a grant); those requests stay standalone instead of failing. An explicitly requested parent is still subject to the ordinary assignment check. Structural parentage, creation provenance and blockers stay distinct: the default adds no blocker. -- Agent-created task trees are at most two levels (`MAX_AGENT_TASK_HIERARCHY_DEPTH`). When an agent, or automation acting for one (runner and project tools, chat suggestions, native delegation, accepted-plan decomposition, decision effects, plugins, external agents), would create or move a task to a third level, the server does not reject it: the task is placed under the requested parent's ancestor at the allowed depth, a sibling of the requester, and the create/update response and activity carry `parentAdjusted`. An agent move of a task that has its own children skips only the parent change (`reason: "has_children"`). The requester keeps blocking (`blockParentUntilDone` blocks the requester), workspace inheritance and privacy inheritance; provenance follows the `origin_issue_id` rule below. Placement never moves a task above a low-trust boundary root that contains the requester. The ancestor is written only when the caller authorized it and confirms it with `expectedParentId` (the issue routes, the runner `create_task` tool, and nested chat suggestions do); other callers get a standalone task instead (an unconfirmed move skips only the parent change), so no edge lands under an unchecked ancestor. Board users, watchdog and recovery machinery, channel-boundary placement and human-configured routines may nest to any depth; existing deeper trees stay valid and fully readable. +- Agent-created task trees are at most two levels (`MAX_AGENT_TASK_HIERARCHY_DEPTH`). When an agent, or automation acting for one (runner and project tools, chat suggestions, native delegation, accepted-plan decomposition, decision effects, plugins, external agents), would create or move a task to a third level, the server does not reject it: the task is placed under the requested parent's ancestor at the allowed depth, a sibling of the requester, and the create/update response and activity carry `parentAdjusted`. An agent move of a task that has its own children skips only the parent change (`reason: "has_children"`). The requester keeps blocking (`blockParentUntilDone` blocks the requester), workspace strategy inheritance and privacy inheritance; provenance follows the `origin_issue_id` rule below. Placement never moves a task above a low-trust boundary root that contains the requester. The ancestor is written only when the caller authorized it and confirms it with `expectedParentId` (the issue routes, the runner `create_task` tool, and nested chat suggestions do); other callers get a standalone task instead (an unconfirmed move skips only the parent change), so no edge lands under an unchecked ancestor. Board users, watchdog and recovery machinery, channel-boundary placement and human-configured routines may nest to any depth; existing deeper trees stay valid and fully readable. - `origin_issue_id` and `origin_run_id` are server-set only: the task the creating run was bound to (a chat included), otherwise the chat that handed the task off, otherwise the requested parent (before placement); and that run. Privacy still follows the requested parent. Because `origin_issue_id` is returned with the task, it is recorded only when it reveals nothing new: the task's own parent or privacy parent (already exposed on the task), or a fully public origin (open, outside private projects and subtrees, not hidden); otherwise it stays null and `createdFrom` resolves the source through the run with its access checks. They never change after create. - `GET /api/issues/:id` returns `createdFrom` (`{ issue: { id, identifier, title, status }, run: { id, agentId } | null, agent: { id, name } | null } | null`): the task that created this issue, resolved from `origin_issue_id`, otherwise from the task the origin run was executing (`origin_run_id`, or for historical rows the run on the recorded creation activity). It is present whether or not `parentId` is set and requires that the viewer may read the source task. A recorded `origin_issue_id` is returned with `run` and `agent` null when no run created the task or the viewer may not see that run, so a null `run` is not missing provenance; a source resolved only through a run additionally requires that run to be visible. An unreadable, hidden-from-viewer or cross-company source yields `null`, indistinguishable from missing provenance. - Relation lists can use `sortField=id&sortDir=asc&afterId=` for stable pagination. The cursor excludes earlier IDs and cannot be combined with an offset or activity-based order. @@ -1103,7 +1104,7 @@ Task-watchdog wakes must include server-derived capability metadata that names t Within the watched subtree, a watchdog run may perform only mutations that restore or clarify the next live/waiting path: - add comments that explain findings, evidence, and next action -- create descendant follow-up issues under an included subtree issue, inheriting company, project, goal, and workspace context from that subtree +- create descendant follow-up issues under an included subtree issue, inheriting company, project, goal, and workspace strategy from that subtree; each follow-up gets its own execution workspace - assign or reassign included issues to active, invokable, same-company agents when normal assignment checks and scoped assignment grants allow it - move included issues among `todo`, `in_progress`, `in_review`, and `blocked` when the transition is needed to restore a valid action path - reopen `done` or `cancelled` included issues only with explicit resume metadata and an audit comment when evidence shows the stopped disposition is wrong or incomplete diff --git a/doc/SPEC.md b/doc/SPEC.md index 49f2afafecc..e7c5c2b044b 100644 --- a/doc/SPEC.md +++ b/doc/SPEC.md @@ -846,7 +846,7 @@ A company may configure a shared decision model for optional Paperclip features. Organizational projects, task files, personal agent home, and provider home are separate concepts. App and chat tasks share one workspace policy: preserve a binding or explicit configured folder, otherwise allocate a stable task directory. A repository is optional and can be prepared without creating a project. Optional connection and destination defaults feed the same task policy; they do not grant permissions or relocate existing conversations. -Agents select authorized workspace/repository intent while the harness owns paths, preparation, persistence and safe transitions. Intentional simultaneous sharing of one physical local/SSH folder remains supported. Provider sessions keep their admitted root through completion and recovery. See [SPEC-implementation.md](SPEC-implementation.md) and [the layer architecture](plans/2026-10-09-task-workspaces.md) for the versioned contracts and rollout constraints. +Agents select authorized workspace/repository intent while the harness owns paths, preparation, persistence and safe transitions. Intentional simultaneous sharing of one physical local/SSH folder remains supported. A subtask never shares its parent's realized workspace: it keeps the parent's project and workspace strategy and gets its own workspace from the project base, exactly as a top-level task in that project would (a project configured for one shared folder still shares that folder across all its tasks). Provider sessions keep their admitted root through completion and recovery. See [SPEC-implementation.md](SPEC-implementation.md) and [the layer architecture](plans/2026-10-09-task-workspaces.md) for the versioned contracts and rollout constraints. ### Fast responses diff --git a/doc/execution-semantics.md b/doc/execution-semantics.md index d2ea03d6884..462802d2cf9 100644 --- a/doc/execution-semantics.md +++ b/doc/execution-semantics.md @@ -642,6 +642,48 @@ Use it for: Do not treat `parentId` as execution dependency by itself. +A child never shares its parent's execution workspace. Every new child task, +from every creation path, gets its own workspace on its first run, realized from +the project base (the project workspace or base branch) exactly as a top-level +task in that project would be. It never starts from the parent's checkout, +branch, saved snapshot, or unmerged changes. The child keeps the parent's +company, project, goal, privacy, and workspace strategy (mode, environment, +runtime, network egress, shared-workspace concurrency, branch template, base +ref). It does not keep a pinned existing branch, a `reuse_existing` preference, +or the workspace id. A child in another project takes that project's defaults +instead of the parent's strategy, and so does a child of a parent that has no +project workspace (its strategy described its own task files). In a project +without a project workspace, where tasks acquire repositories into their own +task files, the child's new workspace repeats the parent's repository requests +as its own: admission clones a fresh copy at the requested ref through the +normal acquisition path and the child's own access checks, never the parent's +checkout, branch, or pinned commit. No route can bind a task to +a workspace that an ancestor created or is bound to: an explicit +`executionWorkspaceId` naming one is dropped at create, and +`inheritExecutionWorkspaceFromIssueId` copies only the source issue's project and +strategy. The deprecated internal `executionWorkspaceInheritanceMode` is ignored. +This keeps a parent and its child from writing into one realized workspace at +the same time, which made the waking parent wait with `workspace_busy` and +crossed sandbox lease ownership from the parent into the child. Project-level +configuration still applies to the child exactly as to a top-level task: in a +project configured for `shared_workspace`, every task uses the project's one +folder under the shared-workspace concurrency policy, and a branch the project +itself pins applies to every task in the project. + +Older releases bound children to the parent's realized workspace by default. +Such a child, including one that depth placement put beside its requester +(recorded as its privacy parent), moves on its next normal admission: when it starts a new run and no +earlier run of the child is still running, finalizing, or under workspace +recovery, the server clears the child's binding (keeping its project workspace +and strategy) and the run realizes the child's own workspace. A running child +finishes its current run in the shared workspace first, and a process-loss retry +or a Retry that keeps the current workspace also finishes there. The parent keeps +its workspace, and no files in it are moved or removed. Nobody is asked to act. +The same admission rule covers any task bound to a workspace that a relative in +its task tree created: the task that created the workspace keeps it, and the +relative bound to it (for example a parent that selected its child's workspace) +moves to its own on its next admission. + ### Blockers (`blockedByIssueIds`) This is dependency semantics. @@ -1250,8 +1292,8 @@ recovery names this failure for the statuses it scans; for other statuses, such a `blocked` task woken by a reply, the failure path records the action directly. A retained sandbox can still hold a workspace's earlier root after the workspace -moved to another allocation, for example when child tasks inherit a parent's -workspace and save newer work on their own sandboxes. When the parent wakes on its +moved to another allocation, for example when child tasks that an older release +bound to a parent's workspace saved newer work on their own sandboxes. When the parent wakes on its retained sandbox, the restore may replace that occupied root only when the server proves it is the workspace's own earlier allocation on this sandbox: the last recorded use of that root on this sandbox is an earlier epoch that ended in a diff --git a/docs/specs/external-task-protocol.md b/docs/specs/external-task-protocol.md index 78bdf7d1d23..84af58fac8f 100644 --- a/docs/specs/external-task-protocol.md +++ b/docs/specs/external-task-protocol.md @@ -732,9 +732,10 @@ Profile requirements: ### 9.4 Workspaces -Per-issue isolation is provided by host execution workspaces/worktrees keyed by issue. Links and -follow-up issues that must share a checkout use host workspace-inheritance -(`inheritExecutionWorkspaceFromIssueId`); the connector never manages filesystem workspaces. +Per-issue isolation is provided by host execution workspaces/worktrees keyed by issue. Follow-up +issues can inherit the source issue's project and workspace strategy +(`inheritExecutionWorkspaceFromIssueId`), but each gets its own workspace; code carries over through +pushed branches, not a shared checkout. The connector never manages filesystem workspaces. ### 9.5 Workflow Policy diff --git a/packages/plugins/sdk/README.md b/packages/plugins/sdk/README.md index c72a1f49798..29da17228ed 100644 --- a/packages/plugins/sdk/README.md +++ b/packages/plugins/sdk/README.md @@ -595,13 +595,14 @@ headers?, body? }`. Workflow plugins can use `ctx.issues` for orchestration-grade issue operations without importing host server internals. +A child issue always gets its own execution workspace, realized from the project base. It keeps the parent's project and workspace strategy, never the parent's checkout, branch or unmerged changes. `inheritExecutionWorkspaceFromIssueId` likewise copies only the source issue's project and workspace strategy. + Expanded create/update fields include blockers, billing code, board or agent assignees, labels, namespaced plugin origins, request depth, and safe execution workspace fields: ```ts const child = await ctx.issues.create({ companyId, parentId: missionIssueId, - inheritExecutionWorkspaceFromIssueId: missionIssueId, title: "Implement feature slice", status: "todo", assigneeAgentId: workerAgentId, diff --git a/packages/plugins/sdk/src/types.ts b/packages/plugins/sdk/src/types.ts index e6be5116e5d..9555522abfd 100644 --- a/packages/plugins/sdk/src/types.ts +++ b/packages/plugins/sdk/src/types.ts @@ -1490,6 +1490,7 @@ export interface PluginIssuesClient { projectId?: string; goalId?: string; parentId?: string; + /** Inherit this issue's project and workspace strategy. The new issue always gets its own workspace. */ inheritExecutionWorkspaceFromIssueId?: string; title: string; description?: string; diff --git a/packages/shared/src/validators/issue.ts b/packages/shared/src/validators/issue.ts index 1a3db303fec..c0664bbc83c 100644 --- a/packages/shared/src/validators/issue.ts +++ b/packages/shared/src/validators/issue.ts @@ -779,6 +779,10 @@ const createIssueBaseSchema = z.object({ .strict() .optional() .nullable(), + /** + * Inherit the project and workspace strategy of this issue. The new task never + * shares the source issue's realized workspace; it gets its own from the project base. + */ inheritExecutionWorkspaceFromIssueId: z.string().guid().optional().nullable(), title: z.string().min(1), description: multilineTextSchema.optional().nullable(), diff --git a/server/src/__tests__/chat-project-tools.test.ts b/server/src/__tests__/chat-project-tools.test.ts index ec9d6dd0e1c..7def311320d 100644 --- a/server/src/__tests__/chat-project-tools.test.ts +++ b/server/src/__tests__/chat-project-tools.test.ts @@ -4,7 +4,7 @@ import { callProjectTool } from "../services/project-tools.js"; import { createLocalAgentJwt } from "../agent-auth-jwt.js"; import { randomUUID } from "node:crypto"; import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; -import { eq } from "drizzle-orm"; +import { and, eq } from "drizzle-orm"; import { agents, issues, heartbeatRuns, projects, companyMemberships, executionWorkspaces } from "@paperclipai/db"; import { startRunnerApiTestServer } from "./helpers/runner-api-server.js"; import { issueService } from "../services/issues.js"; @@ -337,7 +337,13 @@ import { recoveryPostgresSupport as support } from "./helpers/recovery-postgres. expect(await server.db.select({ id: issues.id }).from(issues).where(eq(issues.companyId, f.companyId))).toEqual(before); await server.db.update(issues).set({ executionWorkspaceId: executionWorkspace.id }).where(eq(issues.id, f.issueId)); - await expect(call(f, "create_task", { title: "Inherited source cannot escape", idempotencyKey: "inherited-source-denied", status: "backlog" })).rejects.toThrow(/outside.*boundary/i); + // A child never inherits its parent's workspace, so the parent's + // out-of-boundary files cannot leak into the child either. + await call(f, "create_task", { title: "Inherited source cannot escape", idempotencyKey: "inherited-source-denied", status: "backlog" }); + const [inheritedChild] = await server.db.select({ executionWorkspaceId: issues.executionWorkspaceId }).from(issues) + .where(and(eq(issues.companyId, f.companyId), eq(issues.title, "Inherited source cannot escape"))); + expect(inheritedChild?.executionWorkspaceId).toBeNull(); + await server.db.delete(issues).where(and(eq(issues.companyId, f.companyId), eq(issues.title, "Inherited source cannot escape"))); await server.db.update(agents).set({ permissions: { trustPreset: "low_trust_review", authorizationPolicy: { trustBoundary: { ...boundary, projectIds: [f.projectId] }, } } }).where(eq(agents.id, f.agentId)); diff --git a/server/src/__tests__/git-restore-point-expiration.test.ts b/server/src/__tests__/git-restore-point-expiration.test.ts index 49a05169abc..7df1ddc24f6 100644 --- a/server/src/__tests__/git-restore-point-expiration.test.ts +++ b/server/src/__tests__/git-restore-point-expiration.test.ts @@ -15,7 +15,6 @@ import { expireReconstructableGitWorkspace, gitWorkspaceLastActivityAt } from ". import { getStorageService } from "../storage/index.js"; import { markGitRestorePointExpired } from "../services/git-restore-point-storage.js"; import { GIT_RESTORE_POINT_EXPIRED_WARNING, setMetadataReopenPendingConsumption } from "../services/execution-workspaces.js"; -import { issueService } from "../services/issues.js"; const gitEnv = { ...process.env, GIT_AUTHOR_NAME: "Fixture", GIT_AUTHOR_EMAIL: "fixture@example.test", GIT_COMMITTER_NAME: "Fixture", GIT_COMMITTER_EMAIL: "fixture@example.test", GIT_TERMINAL_PROMPT: "0" }; @@ -472,10 +471,11 @@ describeEmbeddedPostgres("git restore point expiration", () => { expect(await exists(f.cwd)).toBe(true); }); - it("keeps the worktree for a child created later that inherits it with reuse_existing", async () => { + it("keeps the worktree for an older-release child still bound to it with reuse_existing", async () => { const f = await fixture(); - const created = await issueService(db).create(f.companyId, { parentId: f.issueId, title: "Inherited follow-up", status: "todo" }); - expect(created).toMatchObject({ executionWorkspaceId: f.workspaceId, executionWorkspacePreference: "reuse_existing" }); + // New children get their own workspace; this is the binding an older release wrote. + await child(f, { title: "Inherited follow-up", status: "todo", executionWorkspaceId: f.workspaceId, + executionWorkspacePreference: "reuse_existing" }); expect(await f.expire()).toMatchObject({ expired: false, skipReason: "live_or_unresolved_consumer" }); expect(await worker().sweepTerminalWorkspaces()).toMatchObject({ archived: 0, skippedNonTerminalTree: 1 }); expect(await exists(f.cwd)).toBe(true); diff --git a/server/src/__tests__/heartbeat-git-restore-point-resume.test.ts b/server/src/__tests__/heartbeat-git-restore-point-resume.test.ts index 6ae1694a4f7..1b14c7ae89d 100644 --- a/server/src/__tests__/heartbeat-git-restore-point-resume.test.ts +++ b/server/src/__tests__/heartbeat-git-restore-point-resume.test.ts @@ -18,6 +18,7 @@ import { executionWorkspaces, heartbeatRuns, issues, + projectWorkspaces, projects, } from "@paperclipai/db"; import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js"; @@ -136,7 +137,7 @@ describe.skipIf(!support.supported)("heartbeat resume of an idle-expired workspa await db.update(issues).set({ checkoutRunId: runId, executionRunId: runId, executionAgentNameKey: "resumer", executionLockedAt: new Date() }) .where(eq(issues.id, target)); }; - return { issueId, childId, workspaceId, runId, cwd: realized.cwd, unpushed, expiration, workspace, queueRun }; + return { issueId, childId, companyId, projectId, author, workspaceId, runId, cwd: realized.cwd, unpushed, expiration, workspace, queueRun }; } async function waitForRun(heartbeat: ReturnType, runId: string) { @@ -193,22 +194,37 @@ describe.skipIf(!support.supported)("heartbeat resume of an idle-expired workspa expect(issue.executionWorkspaceId).toBe(seeded.workspaceId); }); - it("restores a closed parent's expired workspace when an open child binds to it", async () => { + it("gives an open child bound to a closed parent's expired workspace its own workspace and keeps the parent's restore point", async () => { const seeded = await seed({ closedParent: true }); expect(seeded.expiration).toMatchObject({ expired: true, restorePoint: true }); await expect(fs.access(seeded.cwd)).rejects.toMatchObject({ code: "ENOENT" }); - await db.update(issues).set({ executionWorkspaceId: seeded.workspaceId, executionWorkspacePreference: "reuse_existing" }) - .where(eq(issues.id, seeded.childId)); + // The project base a fresh task in this project starts from. + const baseCwd = path.join(path.dirname(seeded.author), "project-checkout"); + git(path.dirname(seeded.author), "clone", "-q", path.join(path.dirname(seeded.author), "remote.git"), baseCwd); + const [base] = await db.insert(projectWorkspaces).values({ companyId: seeded.companyId, projectId: seeded.projectId, + name: "Project checkout", sourceType: "local_path", cwd: baseCwd, isPrimary: true }).returning(); + // The binding an older release wrote for every child. + await db.update(issues).set({ projectWorkspaceId: base!.id, executionWorkspaceId: seeded.workspaceId, + executionWorkspacePreference: "reuse_existing" }).where(eq(issues.id, seeded.childId)); + const parentWorkspaceBefore = await seeded.workspace(); await seeded.queueRun(seeded.childId); const heartbeat = heartbeatService(db); await heartbeat.resumeQueuedRuns(); const run = await waitForRun(heartbeat, seeded.runId); await heartbeat.waitForRunExecutionDrain(seeded.runId); expect(run).toMatchObject({ status: "succeeded" }); - expect(adapterExecute.mock.calls[0]?.[0]).toMatchObject({ context: { executionWorkspaceId: seeded.workspaceId, - paperclipWorkspace: { cwd: seeded.cwd } } }); - expect(git(seeded.cwd, "rev-parse", "HEAD")).toBe(seeded.unpushed); - expect(await fs.readFile(path.join(seeded.cwd, "notes.md"), "utf8")).toBe("uncommitted notes\n"); - expect((await seeded.workspace()).status).not.toBe("archived"); + const context = (adapterExecute.mock.calls[0]?.[0] as { context?: Record } | undefined)?.context; + expect(context?.executionWorkspaceId).not.toBe(seeded.workspaceId); + // The child starts from the project base, not the parent's unmerged changes, + // and the parent's saved restore point was left as it was. + const childCwd = (context?.paperclipWorkspace as { cwd?: string } | undefined)?.cwd; + expect(childCwd).toBeTruthy(); + expect(childCwd).not.toBe(seeded.cwd); + expect(git(childCwd!, "rev-parse", "HEAD")).not.toBe(seeded.unpushed); + await expect(fs.access(path.join(childCwd!, "notes.md"))).rejects.toMatchObject({ code: "ENOENT" }); + await expect(fs.access(seeded.cwd)).rejects.toMatchObject({ code: "ENOENT" }); + const [child] = await db.select({ executionWorkspaceId: issues.executionWorkspaceId }).from(issues).where(eq(issues.id, seeded.childId)); + expect(child?.executionWorkspaceId).not.toBe(seeded.workspaceId); + expect(await seeded.workspace()).toEqual(parentWorkspaceBefore); }); }); diff --git a/server/src/__tests__/heartbeat-remote-workspace-busy.test.ts b/server/src/__tests__/heartbeat-remote-workspace-busy.test.ts index 7a404ad6e36..78f82a92e81 100644 --- a/server/src/__tests__/heartbeat-remote-workspace-busy.test.ts +++ b/server/src/__tests__/heartbeat-remote-workspace-busy.test.ts @@ -68,8 +68,10 @@ describe.skipIf(!support.supported)("inherited remote workspace heartbeat wait", name: "Inherited remote workspace", mode, strategyType: "project_primary", providerType: "cloud_sandbox", cwd: `/workspaces/${randomUUID()}`, metadata: { remoteWorkspace: authority } }).returning(); await db.update(issues).set({ executionWorkspaceId: workspace.id }).where(eq(issues.id, parent.id)); - const [child] = await db.insert(issues).values({ companyId: company.id, parentId: parent.id, - title: "Use the inherited files", status: "todo", assigneeAgentId: agent.id, responsibleUserId: userId, + // An unrelated task that explicitly shares the running task's workspace. A + // child bound this way would get its own workspace at admission instead. + const [child] = await db.insert(issues).values({ companyId: company.id, + title: "Use the shared files", status: "todo", assigneeAgentId: agent.id, responsibleUserId: userId, executionWorkspaceId: workspace.id, executionWorkspacePreference: "reuse_existing", executionWorkspaceSettings: { mode } }).returning(); const originalTransaction = db.transaction.bind(db); let reassignmentInjected = false; diff --git a/server/src/__tests__/heartbeat-subtask-acquired-repository.test.ts b/server/src/__tests__/heartbeat-subtask-acquired-repository.test.ts new file mode 100644 index 00000000000..0eeaf2df34d --- /dev/null +++ b/server/src/__tests__/heartbeat-subtask-acquired-repository.test.ts @@ -0,0 +1,166 @@ +import { randomUUID } from "node:crypto"; +import { execFile as execFileCallback } from "node:child_process"; +import { mkdtemp, mkdir, readFile, realpath, rm, writeFile, lstat } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { promisify } from "node:util"; +import { and, eq } from "drizzle-orm"; +import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest"; +import { + agents, authUsers, companies, companyMemberships, createDb, executionWorkspaceRepositories, executionWorkspaces, + heartbeatRuns, issues, projectWorkspaces, projects, +} from "@paperclipai/db"; +import { heartbeatService } from "../services/heartbeat.js"; +import { issueService } from "../services/issues.js"; +import { taskRepositoryRelativePath } from "../services/execution-workspace-repositories.js"; +import { instanceSettingsService } from "../services/instance-settings.js"; +import { recoveryPostgresSupport, startRecoveryPostgres } from "./helpers/recovery-postgres.js"; +import { drainHeartbeatRunsToQuiescence } from "./helpers/drain-heartbeat-runs.js"; + +const execute = vi.hoisted(() => vi.fn()); +vi.mock("../adapters/index.js", () => ({ + getServerAdapter: () => ({ type: "codex_local", execute, supportsLocalAgentJwt: false }), + findActiveServerAdapter: () => ({ type: "codex_local", execute, supportsLocalAgentJwt: false }), + runningProcesses: new Map(), +})); + +const exec = promisify(execFileCallback); +const REPO_URL = "https://github.com/team/source"; + +describe.skipIf(!recoveryPostgresSupport.supported)("subtask in a project that acquires task repositories", () => { + let temporary: Awaited>; + let db: ReturnType; + let heartbeat: ReturnType; + let root: string; + let previousPath: string | undefined; + + beforeAll(async () => { + root = await realpath(await mkdtemp(path.join(tmpdir(), "subtask-acquired-repository-"))); + vi.stubEnv("PAPERCLIP_HOME", path.join(root, "home")); + vi.stubEnv("PAPERCLIP_SECRETS_MASTER_KEY", "01234567890123456789012345678901"); + temporary = await startRecoveryPostgres("subtask-acquired-repository-"); + db = createDb(temporary.connectionString); + await instanceSettingsService(db).updateExperimental({ enableIsolatedWorkspaces: true }); + heartbeat = heartbeatService(db); + // Clone the public GitHub URL from a local fixture repository instead of the network. + const realGit = (await exec("which", ["git"])).stdout.trim(); + const source = path.join(root, "source"), bin = path.join(root, "bin"); + await Promise.all([mkdir(source), mkdir(bin)]); + const git = (...args: string[]) => exec(realGit, ["-C", source, ...args]); + await git("init", "--initial-branch=main"); + await writeFile(path.join(source, "source.txt"), "project base\n"); + await git("add", "."); + await git("-c", "user.name=Fixture", "-c", "user.email=fixture@example.test", "commit", "-m", "base"); + await writeFile(path.join(bin, "git"), `#!${process.execPath}\n` + + `const { spawnSync } = require('node:child_process'); const args = process.argv.slice(2);\n` + + `const remote = args.includes('clone') ? args.at(-2) : null;\n` + + `if (remote && remote !== ${JSON.stringify(REPO_URL)}) process.exit(97);\n` + + `if (remote) args[args.length - 2] = ${JSON.stringify(source)};\n` + + `const result = spawnSync(${JSON.stringify(realGit)}, args, { stdio: 'inherit' });\n` + + `if (result.status) process.exit(result.status);\n` + + `if (remote) process.exit(spawnSync(${JSON.stringify(realGit)}, ['-C', args.at(-1), 'remote', 'set-url', 'origin', ${JSON.stringify(REPO_URL)}], { stdio: 'inherit' }).status ?? 98);\n` + + `process.exit(result.status ?? 99);\n`, { mode: 0o755 }); + previousPath = process.env.PATH; + process.env.PATH = `${bin}${path.delimiter}${previousPath ?? ""}`; + }, 60_000); + + afterEach(async () => { + await drainHeartbeatRunsToQuiescence(db, heartbeat); + execute.mockReset(); + }); + + afterAll(async () => { + if (previousPath === undefined) delete process.env.PATH; else process.env.PATH = previousPath; + await db?.$client.end(); + await temporary?.cleanup(); + vi.unstubAllEnvs(); + if (root) await rm(root, { recursive: true, force: true }); + }); + + it.each(["new", "older-release", "cross-project"] as const)("gives a %s child its own workspace and never the parent's checkout", async (shape) => { + const companyId = randomUUID(), userId = randomUUID(), agentId = randomUUID(), projectId = randomUUID(); + const parentId = randomUUID(), parentWorkspaceId = randomUUID(); + await db.insert(authUsers).values({ id: userId, name: "Owner", email: `${userId}@example.test`, createdAt: new Date(), updatedAt: new Date() }); + await db.insert(companies).values({ id: companyId, name: "Acquired repositories", issuePrefix: `A${companyId.slice(0, 6)}`, + defaultResponsibleUserId: userId, requireBoardApprovalForNewAgents: false }); + await db.insert(companyMemberships).values({ companyId, principalId: userId, principalType: "user", membershipRole: "owner", status: "active" }); + // A project with no project workspace: tasks acquire repositories into their own task files. + await db.insert(projects).values({ id: projectId, companyId, name: "Repository-free project", status: "in_progress" }); + expect(await db.select().from(projectWorkspaces).where(eq(projectWorkspaces.projectId, projectId))).toEqual([]); + await db.insert(agents).values({ id: agentId, companyId, name: "Worker", status: "idle", adapterType: "codex_local", + adapterConfig: {}, runtimeConfig: { heartbeat: { wakeOnDemand: true, maxConcurrentRuns: 2 } }, permissions: {} }); + + // The parent's realized task files, with an acquired repository holding unmerged work. + const parentCwd = path.join(root, `parent-task-${parentId}`); + const relativePath = taskRepositoryRelativePath(REPO_URL); + const parentCheckout = path.join(parentCwd, relativePath); + await mkdir(path.dirname(parentCheckout), { recursive: true }); + await exec("git", ["clone", REPO_URL, parentCheckout]); + const pinnedCommit = (await exec("git", ["-C", parentCheckout, "rev-parse", "HEAD"])).stdout.trim(); + await writeFile(path.join(parentCheckout, "source.txt"), "parent unmerged change\n"); + await db.insert(issues).values({ id: parentId, companyId, projectId, title: "Parent", status: "in_progress", + assigneeAgentId: agentId, responsibleUserId: userId }); + await db.insert(executionWorkspaces).values({ id: parentWorkspaceId, companyId, projectId, sourceIssueId: parentId, + name: "Parent task files", cwd: parentCwd, mode: "isolated_workspace", strategyType: "project_primary", + providerType: "local_fs", metadata: { source: "task_session" } }); + await db.update(issues).set({ executionWorkspaceId: parentWorkspaceId, executionWorkspacePreference: "reuse_existing", + executionWorkspaceSettings: { mode: "isolated_workspace" } }).where(eq(issues.id, parentId)); + await db.insert(executionWorkspaceRepositories).values({ companyId, executionWorkspaceId: parentWorkspaceId, + requestedByIssueId: parentId, repoUrl: REPO_URL, repositoryIdentity: REPO_URL, relativePath, requestedRef: "HEAD", + pinnedCommit, branchName: "main", state: "ready", requestKey: "parent-repository", requestKeys: ["parent-repository"] }); + + // A second project without a project workspace, for the cross-project child. + const otherProjectId = randomUUID(); + await db.insert(projects).values({ id: otherProjectId, companyId, name: "Other repository-free project", status: "in_progress" }); + const child = shape !== "older-release" + ? await issueService(db).create(companyId, { parentId, title: "Child", status: "todo", + assigneeAgentId: agentId, actorResponsibleUserId: userId, + ...(shape === "cross-project" ? { projectId: otherProjectId } : {}) }) + // An older release bound every child to its parent's realized workspace. + : (await db.insert(issues).values({ companyId, projectId, parentId, title: "Child", status: "todo", + assigneeAgentId: agentId, responsibleUserId: userId, executionWorkspaceId: parentWorkspaceId, + executionWorkspacePreference: "reuse_existing", executionWorkspaceSettings: { mode: "isolated_workspace" } }).returning())[0]!; + if (shape !== "older-release") expect(child.executionWorkspaceId).toBeNull(); + if (shape === "cross-project") { + // The target project's defaults, exactly as for a top-level task there. + const topLevel = await issueService(db).create(companyId, { projectId: otherProjectId, title: "Top-level", status: "backlog" }); + expect(child).toMatchObject({ projectId: otherProjectId, projectWorkspaceId: topLevel.projectWorkspaceId, + executionWorkspaceSettings: topLevel.executionWorkspaceSettings }); + } + + let childCwd: string | null = null; + execute.mockImplementation(async (input: { context: { paperclipWorkspace?: { cwd?: string } } }) => { + childCwd = input.context.paperclipWorkspace?.cwd ?? null; + return { exitCode: 0, signal: null, timedOut: false }; + }); + const run = await heartbeat.wakeup(agentId, { source: "on_demand", triggerDetail: "manual", contextSnapshot: { issueId: child.id } }); + expect(run).not.toBeNull(); + await vi.waitFor(async () => { + expect((await heartbeat.getRun(run!.id))?.status).toMatch(/^(succeeded|failed|cancelled|timed_out)$/); + }, { timeout: 30_000 }); + const finished = await heartbeat.getRun(run!.id); + expect({ status: finished?.status, error: finished?.error }).toMatchObject({ status: "succeeded" }); + + // The child ran in its own task files with its own fresh copy of the repository. + expect(childCwd).toBeTruthy(); + expect(childCwd).not.toBe(parentCwd); + const [childRow] = await db.select().from(issues).where(eq(issues.id, child.id)); + expect(childRow.executionWorkspaceId).not.toBe(parentWorkspaceId); + const childRepositories = await db.select().from(executionWorkspaceRepositories) + .where(and(eq(executionWorkspaceRepositories.companyId, companyId), eq(executionWorkspaceRepositories.executionWorkspaceId, childRow.executionWorkspaceId!))); + if (shape === "cross-project") { + // The parent's repositories belong to the parent's project. + expect(childRepositories).toEqual([]); + expect(await lstat(path.join(childCwd!, relativePath)).catch(() => null)).toBeNull(); + expect(await readFile(path.join(parentCheckout, "source.txt"), "utf8")).toBe("parent unmerged change\n"); + return; + } + expect(await readFile(path.join(childCwd!, relativePath, "source.txt"), "utf8")).toBe("project base\n"); + // The parent's checkout and its unmerged change are untouched. + expect(await readFile(path.join(parentCheckout, "source.txt"), "utf8")).toBe("parent unmerged change\n"); + expect(childRepositories).toEqual([expect.objectContaining({ repositoryIdentity: REPO_URL, requestedByIssueId: child.id, + state: "ready", relativePath })]); + expect(await lstat(path.join(childCwd!, relativePath, ".git"))).toBeTruthy(); + expect(await db.select({ id: heartbeatRuns.id }).from(heartbeatRuns).where(eq(heartbeatRuns.companyId, companyId))).toHaveLength(1); + }, 60_000); +}); diff --git a/server/src/__tests__/heartbeat-workspace-busy.test.ts b/server/src/__tests__/heartbeat-workspace-busy.test.ts index 47a4da2caf4..f21701617da 100644 --- a/server/src/__tests__/heartbeat-workspace-busy.test.ts +++ b/server/src/__tests__/heartbeat-workspace-busy.test.ts @@ -240,6 +240,9 @@ describeEmbeddedPostgres("shared-workspace run serialization", () => { // Bind both issues to one reused shared execution workspace, as when a child // task inherits the workspace of a parent whose run is still live. sharedExecutionWorkspace?: boolean; + // Make the deferred issue a child of the holder issue, in the shape older + // releases wrote: the child bound to the parent's realized workspace. + childOfHolder?: boolean; holderIssueWorkspaceSettings?: Record | null; holderProjectWorkspaceId?: string; holderActivityAt?: Date; @@ -400,9 +403,15 @@ describeEmbeddedPostgres("shared-workspace run serialization", () => { : {}), }); + if (sharedExecutionWorkspaceId && input?.childOfHolder) { + await db.update(executionWorkspaces).set({ sourceIssueId: holderIssueId }) + .where(eq(executionWorkspaces.id, sharedExecutionWorkspaceId)); + } + await db.insert(issues).values({ id: issueId, companyId, + ...(input?.childOfHolder ? { parentId: holderIssueId } : {}), title: "Deferred issue", status: "in_progress", priority: "medium", @@ -698,6 +707,35 @@ describeEmbeddedPostgres("shared-workspace run serialization", () => { expect(issueRow?.status).not.toBe("blocked"); }); + it("gives an old-release subtask its own workspace instead of waiting on its running parent", async () => { + const fixture = await seedInheritedWorkspace({ childOfHolder: true }); + + const run = await heartbeat.invoke( + fixture.agentId, + "assignment", + { issueId: fixture.issueId, wakeReason: "issue_assigned" }, + "system", + ); + expect(run).not.toBeNull(); + + const finishedRun = await waitForRunToLeaveActiveStates(run!.id); + expect(finishedRun?.errorCode).not.toBe(WORKSPACE_BUSY_ERROR_CODE); + expect(finishedRun?.status).toBe("succeeded"); + expect(executedRunIds).toContain(run!.id); + // The parent was never interrupted and keeps its workspace. + expect((await heartbeat.getRun(fixture.holderRunId))?.status).toBe("running"); + const [parentRow] = await db.select({ executionWorkspaceId: issues.executionWorkspaceId }) + .from(issues).where(eq(issues.id, fixture.holderIssueId)); + expect(parentRow?.executionWorkspaceId).toBe(fixture.sharedExecutionWorkspaceId); + const [parentWorkspace] = await db.select({ status: executionWorkspaces.status }) + .from(executionWorkspaces).where(eq(executionWorkspaces.id, fixture.sharedExecutionWorkspaceId!)); + expect(parentWorkspace?.status).toBe("active"); + // The child now has its own workspace. + const [childRow] = await db.select({ executionWorkspaceId: issues.executionWorkspaceId }) + .from(issues).where(eq(issues.id, fixture.issueId)); + expect(childRow?.executionWorkspaceId).not.toBe(fixture.sharedExecutionWorkspaceId); + }); + it("resumes in the inherited workspace once the holder finishes", async () => { const fixture = await seedInheritedWorkspace(); diff --git a/server/src/__tests__/issues-service.test.ts b/server/src/__tests__/issues-service.test.ts index 577ccdae3e7..0e64da3c8f6 100644 --- a/server/src/__tests__/issues-service.test.ts +++ b/server/src/__tests__/issues-service.test.ts @@ -3105,7 +3105,7 @@ describeEmbeddedPostgres("issueService.create workspace inheritance", () => { await tempDb?.cleanup(); }); - it("inherits the parent issue workspace linkage when child workspace fields are omitted", async () => { + it("gives a child its own workspace and keeps only the parent's strategy when child workspace fields are omitted", async () => { const companyId = randomUUID(); const projectId = randomUUID(); const parentIssueId = randomUUID(); @@ -3163,8 +3163,8 @@ describeEmbeddedPostgres("issueService.create workspace inheritance", () => { expect(child.parentId).toBe(parentIssueId); expect(child.projectWorkspaceId).toBe(projectWorkspaceId); - expect(child.executionWorkspaceId).toBe(executionWorkspaceId); - expect(child.executionWorkspacePreference).toBe("reuse_existing"); + expect(child.executionWorkspaceId).toBeNull(); + expect(child.executionWorkspacePreference).toBeNull(); expect(child.executionWorkspaceSettings).toEqual({ mode: "isolated_workspace", workspaceRuntime: { profile: "agent" }, @@ -3770,7 +3770,7 @@ describeEmbeddedPostgres("issueService.create workspace inheritance", () => { }); }); - it("inherits workspace linkage from an explicit source issue without creating a parent-child relationship", async () => { + it("inherits project and strategy, not the realized workspace, from an explicit source issue without creating a parent-child relationship", async () => { const companyId = randomUUID(); const projectId = randomUUID(); const sourceIssueId = randomUUID(); @@ -3824,14 +3824,12 @@ describeEmbeddedPostgres("issueService.create workspace inheritance", () => { expect(followUp.parentId).toBeNull(); expect(followUp.projectWorkspaceId).toBe(projectWorkspaceId); - expect(followUp.executionWorkspaceId).toBe(executionWorkspaceId); - expect(followUp.executionWorkspacePreference).toBe("reuse_existing"); - expect(followUp.executionWorkspaceSettings).toEqual({ - mode: "operator_branch", - }); + expect(followUp.executionWorkspaceId).toBeNull(); + expect(followUp.executionWorkspacePreference).toBeNull(); + expect(JSON.stringify(followUp.executionWorkspaceSettings ?? {})).not.toContain("operator_branch"); }); - it("createChild applies parent defaults, acceptance criteria, workspace inheritance, and optional parent blocker chaining", async () => { + it("createChild applies parent defaults, acceptance criteria, an independent workspace, and optional parent blocker chaining", async () => { const companyId = randomUUID(); const projectId = randomUUID(); const goalId = randomUUID(); @@ -3912,8 +3910,8 @@ describeEmbeddedPostgres("issueService.create workspace inheritance", () => { expect(child.description).toContain("## Acceptance Criteria"); expect(child.description).toContain("- Uses the parent issue as parentId"); expect(child.projectWorkspaceId).toBe(projectWorkspaceId); - expect(child.executionWorkspaceId).toBe(executionWorkspaceId); - expect(child.executionWorkspacePreference).toBe("reuse_existing"); + expect(child.executionWorkspaceId).toBeNull(); + expect(child.executionWorkspacePreference).toBeNull(); const parentRelations = await svc.getRelationSummaries(parentIssueId); expect(parentRelations.blockedBy).toEqual([ @@ -4968,7 +4966,7 @@ describeEmbeddedPostgres("issueService.create workspace inheritance", () => { await tempDb?.cleanup(); }); - it("preserves the parent project when a generic child create inherits workspace linkage", async () => { + it("preserves the parent project when a generic child create inherits workspace strategy", async () => { const companyId = randomUUID(); const projectId = randomUUID(); const parentIssueId = randomUUID(); @@ -5023,7 +5021,7 @@ describeEmbeddedPostgres("issueService.create workspace inheritance", () => { expect(child.parentId).toBe(parentIssueId); expect(child.projectId).toBe(projectId); expect(child.projectWorkspaceId).toBe(projectWorkspaceId); - expect(child.executionWorkspaceId).toBe(executionWorkspaceId); + expect(child.executionWorkspaceId).toBeNull(); }); it("uses the target project's own workspaces for a cross-project child instead of inheriting the parent's", async () => { @@ -5143,7 +5141,7 @@ describeEmbeddedPostgres("issueService.create workspace inheritance", () => { }); }); - it("inherits workspace linkage from an explicit source issue without creating a parent-child relationship", async () => { + it("inherits project and strategy, not the realized workspace, from an explicit source issue without creating a parent-child relationship", async () => { const companyId = randomUUID(); const projectId = randomUUID(); const sourceIssueId = randomUUID(); @@ -5197,11 +5195,9 @@ describeEmbeddedPostgres("issueService.create workspace inheritance", () => { expect(followUp.parentId).toBeNull(); expect(followUp.projectId).toBe(projectId); expect(followUp.projectWorkspaceId).toBe(projectWorkspaceId); - expect(followUp.executionWorkspaceId).toBe(executionWorkspaceId); - expect(followUp.executionWorkspacePreference).toBe("reuse_existing"); - expect(followUp.executionWorkspaceSettings).toEqual({ - mode: "operator_branch", - }); + expect(followUp.executionWorkspaceId).toBeNull(); + expect(followUp.executionWorkspacePreference).toBeNull(); + expect(JSON.stringify(followUp.executionWorkspaceSettings ?? {})).not.toContain("operator_branch"); }); it("preserves projectless organization when an update adds workspace linkage", async () => { diff --git a/server/src/__tests__/subtask-workspace-independence.test.ts b/server/src/__tests__/subtask-workspace-independence.test.ts new file mode 100644 index 00000000000..0cdd8115edd --- /dev/null +++ b/server/src/__tests__/subtask-workspace-independence.test.ts @@ -0,0 +1,418 @@ +import { randomUUID } from "node:crypto"; +import { eq } from "drizzle-orm"; +import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest"; +import { + activityLog, + agents, + companies, + createDb, + executionWorkspaces, + heartbeatRuns, + instanceSettings, + issueRelations, + issues, + projectWorkspaces, + projects, +} from "@paperclipai/db"; +import { + getEmbeddedPostgresTestSupport, + startEmbeddedPostgresTestDatabase, +} from "./helpers/embedded-postgres.js"; +import { instanceSettingsService } from "../services/instance-settings.ts"; +import { issueService } from "../services/issues.ts"; +import { executionWorkspaceService } from "../services/execution-workspaces.ts"; +import { buildIndependentTaskWorkspaceSettings } from "../services/task-workspace-lineage.ts"; + +const embeddedPostgresSupport = await getEmbeddedPostgresTestSupport(); +const describeEmbeddedPostgres = embeddedPostgresSupport.supported ? describe : describe.skip; + +describe("buildIndependentTaskWorkspaceSettings", () => { + it("keeps the workspace strategy but drops realized identity and pinned branches", () => { + expect(buildIndependentTaskWorkspaceSettings({ + mode: "isolated_workspace", + workspaceRuntime: { profile: "agent" }, + workspaceStrategy: { + type: "git_worktree", + baseRef: "origin/main", + branchTemplate: "{{issue.identifier}}", + existingBranch: "parent-branch", + }, + })).toEqual({ + mode: "isolated_workspace", + workspaceRuntime: { profile: "agent" }, + workspaceStrategy: { type: "git_worktree", baseRef: "origin/main", branchTemplate: "{{issue.identifier}}" }, + }); + expect(buildIndependentTaskWorkspaceSettings({ + mode: "shared_workspace", + sharedWorkspaceConcurrency: "serialize", + networkEgress: { allowFqdns: ["api.example.com"], allowCidrs: [] }, + })).toEqual({ + mode: "shared_workspace", + sharedWorkspaceConcurrency: "serialize", + networkEgress: { allowFqdns: ["api.example.com"], allowCidrs: [] }, + }); + expect(buildIndependentTaskWorkspaceSettings({ mode: "reuse_existing" })).toBeNull(); + expect(buildIndependentTaskWorkspaceSettings({ mode: "operator_branch" })).toBeNull(); + }); +}); + +describeEmbeddedPostgres("subtasks never share an ancestor's execution workspace", () => { + let db!: ReturnType; + let tempDb: Awaited> | null = null; + + beforeAll(async () => { + tempDb = await startEmbeddedPostgresTestDatabase("paperclip-subtask-workspace-"); + db = createDb(tempDb.connectionString); + }, 20_000); + + afterEach(async () => { + await db.delete(activityLog); + await db.delete(issueRelations); + await db.update(executionWorkspaces).set({ sourceIssueId: null }); + await db.update(issues).set({ parentId: null, privacyParentIssueId: null, executionRunId: null }); + await db.delete(issues); + await db.delete(heartbeatRuns); + await db.delete(executionWorkspaces); + await db.delete(projectWorkspaces); + await db.delete(projects); + await db.delete(agents); + await db.delete(instanceSettings); + await db.delete(companies); + }); + + afterAll(async () => { + await tempDb?.cleanup(); + }); + + const parentSettings = { + mode: "isolated_workspace", + workspaceRuntime: { profile: "agent" }, + workspaceStrategy: { type: "git_worktree", baseRef: "origin/main", existingBranch: "pap-1-parent" }, + }; + + async function seedParentWithRealizedWorkspace() { + const companyId = randomUUID(); + const projectId = randomUUID(); + const projectWorkspaceId = randomUUID(); + const parentWorkspaceId = randomUUID(); + const parentIssueId = randomUUID(); + await db.insert(companies).values({ + id: companyId, + name: "Paperclip", + issuePrefix: `T${companyId.replace(/-/g, "").slice(0, 6).toUpperCase()}`, + requireBoardApprovalForNewAgents: false, + }); + await instanceSettingsService(db).updateExperimental({ enableIsolatedWorkspaces: true }); + await db.insert(projects).values({ id: projectId, companyId, name: "Workspace project", status: "in_progress" }); + await db.insert(projectWorkspaces).values({ + id: projectWorkspaceId, companyId, projectId, name: "Primary workspace", isPrimary: true, sharedWorkspaceKey: "workspace-key", + }); + await db.insert(issues).values({ + id: parentIssueId, companyId, projectId, projectWorkspaceId, title: "Parent issue", status: "in_progress", priority: "medium", + }); + await db.insert(executionWorkspaces).values({ + id: parentWorkspaceId, companyId, projectId, projectWorkspaceId, sourceIssueId: parentIssueId, + mode: "isolated_workspace", strategyType: "git_worktree", name: "Parent worktree", status: "active", + branchName: "pap-1-parent", providerType: "git_worktree", providerRef: `/tmp/${parentWorkspaceId}`, + }); + await db.update(issues).set({ + executionWorkspaceId: parentWorkspaceId, + executionWorkspacePreference: "reuse_existing", + executionWorkspaceSettings: parentSettings, + }).where(eq(issues.id, parentIssueId)); + return { companyId, projectId, projectWorkspaceId, parentWorkspaceId, parentIssueId }; + } + + function expectIndependentChild( + child: { executionWorkspaceId: string | null; executionWorkspacePreference: string | null; executionWorkspaceSettings: unknown; projectWorkspaceId: string | null; projectId: string | null }, + fixture: Awaited>, + ) { + expect(child.executionWorkspaceId).toBeNull(); + expect(child.executionWorkspacePreference).not.toBe("reuse_existing"); + expect(child.projectId).toBe(fixture.projectId); + // The project base, not the parent's checkout. + expect(child.projectWorkspaceId).toBe(fixture.projectWorkspaceId); + expect(JSON.stringify(child.executionWorkspaceSettings ?? {})).not.toContain("pap-1-parent"); + expect(JSON.stringify(child.executionWorkspaceSettings ?? {})).not.toContain("reuse_existing"); + } + + it("create() with parentId gives the child its own workspace and keeps the parent's strategy", async () => { + const fixture = await seedParentWithRealizedWorkspace(); + const child = await issueService(db).create(fixture.companyId, { parentId: fixture.parentIssueId, title: "Child issue" }); + expect(child.parentId).toBe(fixture.parentIssueId); + expectIndependentChild(child, fixture); + expect(child.executionWorkspaceSettings).toEqual({ + mode: "isolated_workspace", + workspaceRuntime: { profile: "agent" }, + workspaceStrategy: { type: "git_worktree", baseRef: "origin/main" }, + }); + }); + + it.each([undefined, "linkage", "strategy_only"] as const)( + "createChild never reuses the parent workspace (deprecated inheritance mode %s is ignored)", + async (mode) => { + const fixture = await seedParentWithRealizedWorkspace(); + const { issue: child } = await issueService(db).createChild(fixture.parentIssueId, { + title: "Child issue", + status: "todo", + priority: "medium", + ...(mode ? { executionWorkspaceInheritanceMode: mode } : {}), + } as Parameters["createChild"]>[1]); + expectIndependentChild(child, fixture); + }, + ); + + it("createChild into another project does not carry the parent's strategy across the project boundary", async () => { + const fixture = await seedParentWithRealizedWorkspace(); + const otherProjectId = randomUUID(); + await db.insert(projects).values({ id: otherProjectId, companyId: fixture.companyId, name: "Other project", status: "in_progress" }); + const { issue: child } = await issueService(db).createChild(fixture.parentIssueId, { + title: "Cross-project child", status: "todo", priority: "medium", projectId: otherProjectId, + } as Parameters["createChild"]>[1]); + expect(child.projectId).toBe(otherProjectId); + expect(child.executionWorkspaceId).toBeNull(); + expect(child.projectWorkspaceId).not.toBe(fixture.projectWorkspaceId); + expect(JSON.stringify(child.executionWorkspaceSettings ?? {})).not.toContain("workspaceRuntime"); + }); + + it("keeps the parent's strategy when the New sub-task dialog names only the mode", async () => { + const fixture = await seedParentWithRealizedWorkspace(); + const child = await issueService(db).create(fixture.companyId, { + parentId: fixture.parentIssueId, + projectId: fixture.projectId, + projectWorkspaceId: fixture.projectWorkspaceId, + executionWorkspacePreference: "isolated_workspace", + executionWorkspaceSettings: { mode: "isolated_workspace" }, + title: "Dialog child", + }); + expect(child.executionWorkspaceId).toBeNull(); + expect(child.executionWorkspaceSettings).toEqual({ + mode: "isolated_workspace", + workspaceRuntime: { profile: "agent" }, + workspaceStrategy: { type: "git_worktree", baseRef: "origin/main" }, + }); + }); + + it("does not share when inheritExecutionWorkspaceFromIssueId points at the parent", async () => { + const fixture = await seedParentWithRealizedWorkspace(); + const child = await issueService(db).create(fixture.companyId, { + parentId: fixture.parentIssueId, + inheritExecutionWorkspaceFromIssueId: fixture.parentIssueId, + title: "Child issue", + }); + expectIndependentChild(child, fixture); + }); + + it("drops an explicit executionWorkspaceId that belongs to the parent or a further ancestor", async () => { + const fixture = await seedParentWithRealizedWorkspace(); + const child = await issueService(db).create(fixture.companyId, { + parentId: fixture.parentIssueId, + projectId: fixture.projectId, + executionWorkspaceId: fixture.parentWorkspaceId, + executionWorkspacePreference: "reuse_existing", + executionWorkspaceSettings: { mode: "isolated_workspace" }, + title: "Child issue", + }); + expectIndependentChild(child, fixture); + + const grandchild = await issueService(db).create(fixture.companyId, { + parentId: child.id, + parentDepthPolicy: "allow_nested", + projectId: fixture.projectId, + executionWorkspaceId: fixture.parentWorkspaceId, + executionWorkspacePreference: "reuse_existing", + title: "Grandchild issue", + }); + expect(grandchild.executionWorkspaceId).toBeNull(); + }); + + it("non-child follow-ups keep the source's project and strategy but get their own workspace", async () => { + const fixture = await seedParentWithRealizedWorkspace(); + const followUp = await issueService(db).create(fixture.companyId, { + parentId: null, + inheritExecutionWorkspaceFromIssueId: fixture.parentIssueId, + title: "Follow-up issue", + }); + expect(followUp.parentId).toBeNull(); + expectIndependentChild(followUp, fixture); + }); + + it("leaves top-level tasks and project workspace defaults unchanged", async () => { + const fixture = await seedParentWithRealizedWorkspace(); + const topLevel = await issueService(db).create(fixture.companyId, { projectId: fixture.projectId, title: "Top-level" }); + expect(topLevel.parentId).toBeNull(); + expect(topLevel.executionWorkspaceId).toBeNull(); + expect(topLevel.projectWorkspaceId).toBe(fixture.projectWorkspaceId); + + // An explicit existing workspace for an unrelated top-level task is still honoured. + const explicit = await issueService(db).create(fixture.companyId, { + projectId: fixture.projectId, + executionWorkspaceId: fixture.parentWorkspaceId, + executionWorkspacePreference: "reuse_existing", + title: "Explicit reuse", + }); + expect(explicit.executionWorkspaceId).toBe(fixture.parentWorkspaceId); + }); + + describe("old-release children bound to the parent's workspace", () => { + async function seedOldReleaseChild() { + const fixture = await seedParentWithRealizedWorkspace(); + const childIssueId = randomUUID(); + // The shape older releases wrote by default for every child. + await db.insert(issues).values({ + id: childIssueId, companyId: fixture.companyId, projectId: fixture.projectId, parentId: fixture.parentIssueId, + projectWorkspaceId: fixture.projectWorkspaceId, title: "Old child", status: "todo", priority: "medium", + executionWorkspaceId: fixture.parentWorkspaceId, executionWorkspacePreference: "reuse_existing", + executionWorkspaceSettings: { ...parentSettings, mode: "isolated_workspace" }, + }); + const agentId = randomUUID(); + await db.insert(agents).values({ + id: agentId, companyId: fixture.companyId, name: "Coder", role: "engineer", status: "active", + adapterType: "process", adapterConfig: {}, runtimeConfig: {}, permissions: {}, + }); + return { ...fixture, childIssueId, agentId }; + } + + async function insertRun(input: { companyId: string; agentId: string; issueId: string; status: string }) { + const id = randomUUID(); + await db.insert(heartbeatRuns).values({ + id, companyId: input.companyId, agentId: input.agentId, invocationSource: "assignment", triggerDetail: "system", + status: input.status, startedAt: new Date(), contextSnapshot: { issueId: input.issueId }, + }); + return id; + } + + async function readIssue(id: string) { + const [row] = await db.select().from(issues).where(eq(issues.id, id)); + return row!; + } + + it("rebinds a child that is not running on its next admission and leaves the parent's workspace alone", async () => { + const fixture = await seedOldReleaseChild(); + // The parent is writing in its workspace right now. + const parentRunId = await insertRun({ companyId: fixture.companyId, agentId: fixture.agentId, issueId: fixture.parentIssueId, status: "running" }); + await db.update(issues).set({ executionRunId: parentRunId }).where(eq(issues.id, fixture.parentIssueId)); + const admittedRunId = await insertRun({ companyId: fixture.companyId, agentId: fixture.agentId, issueId: fixture.childIssueId, status: "running" }); + const before = await readIssue(fixture.childIssueId); + + const released = await executionWorkspaceService(db).releaseAncestorWorkspaceBindingAtAdmission({ + companyId: fixture.companyId, issueId: fixture.childIssueId, runId: admittedRunId, + }); + + expect(released).toBe(true); + const child = await readIssue(fixture.childIssueId); + expect(child.executionWorkspaceId).toBeNull(); + expect(child.executionWorkspacePreference).toBeNull(); + expect(child.projectWorkspaceId).toBe(fixture.projectWorkspaceId); + expect(child.executionWorkspaceSettings).toEqual({ + mode: "isolated_workspace", + workspaceRuntime: { profile: "agent" }, + workspaceStrategy: { type: "git_worktree", baseRef: "origin/main" }, + }); + expect(child.workspaceBindingRevision).toBe(before.workspaceBindingRevision + 1); + + // The parent keeps its workspace, and the workspace (with its files) is untouched. + const parent = await readIssue(fixture.parentIssueId); + expect(parent.executionWorkspaceId).toBe(fixture.parentWorkspaceId); + const [workspace] = await db.select().from(executionWorkspaces).where(eq(executionWorkspaces.id, fixture.parentWorkspaceId)); + expect(workspace).toMatchObject({ status: "active", sourceIssueId: fixture.parentIssueId, providerRef: `/tmp/${fixture.parentWorkspaceId}` }); + + // Idempotent: a second admission has nothing left to release. + expect(await executionWorkspaceService(db).releaseAncestorWorkspaceBindingAtAdmission({ + companyId: fixture.companyId, issueId: fixture.childIssueId, runId: admittedRunId, + })).toBe(false); + }); + + it("rebinds a task that depth placement put beside the requester whose workspace it shares", async () => { + const fixture = await seedOldReleaseChild(); + // Older releases inherited from the requested parent even when placement + // flattened the task beside it; the requester is recorded as privacy parent. + const grandparentId = randomUUID(); + await db.insert(issues).values({ id: grandparentId, companyId: fixture.companyId, projectId: fixture.projectId, + title: "Root", status: "in_progress", priority: "medium" }); + await db.update(issues).set({ parentId: grandparentId }).where(eq(issues.id, fixture.parentIssueId)); + await db.update(issues).set({ parentId: grandparentId, privacyParentIssueId: fixture.parentIssueId }) + .where(eq(issues.id, fixture.childIssueId)); + const runId = await insertRun({ companyId: fixture.companyId, agentId: fixture.agentId, issueId: fixture.childIssueId, status: "running" }); + expect(await executionWorkspaceService(db).releaseAncestorWorkspaceBindingAtAdmission({ + companyId: fixture.companyId, issueId: fixture.childIssueId, runId, + })).toBe(true); + expect((await readIssue(fixture.childIssueId)).executionWorkspaceId).toBeNull(); + expect((await readIssue(fixture.parentIssueId)).executionWorkspaceId).toBe(fixture.parentWorkspaceId); + }); + + it("keeps a child's own workspace when its parent selected it, and moves the parent instead", async () => { + const fixture = await seedOldReleaseChild(); + const childWorkspaceId = randomUUID(); + await db.insert(executionWorkspaces).values({ + id: childWorkspaceId, companyId: fixture.companyId, projectId: fixture.projectId, projectWorkspaceId: fixture.projectWorkspaceId, + sourceIssueId: fixture.childIssueId, mode: "isolated_workspace", strategyType: "git_worktree", name: "Child worktree", + status: "active", providerType: "git_worktree", providerRef: `/tmp/${childWorkspaceId}`, + }); + await db.update(issues).set({ executionWorkspaceId: childWorkspaceId }).where(eq(issues.id, fixture.childIssueId)); + await db.update(issues).set({ executionWorkspaceId: childWorkspaceId }).where(eq(issues.id, fixture.parentIssueId)); + // The child created the workspace, so its admission keeps it. A fresh + // workspace for the child would land on the same issue-named branch. + const childRunId = await insertRun({ companyId: fixture.companyId, agentId: fixture.agentId, issueId: fixture.childIssueId, status: "running" }); + expect(await executionWorkspaceService(db).releaseAncestorWorkspaceBindingAtAdmission({ + companyId: fixture.companyId, issueId: fixture.childIssueId, runId: childRunId, + })).toBe(false); + expect((await readIssue(fixture.childIssueId)).executionWorkspaceId).toBe(childWorkspaceId); + // The parent's next admission moves the parent off its descendant's workspace. + await db.update(heartbeatRuns).set({ status: "succeeded", finishedAt: new Date() }).where(eq(heartbeatRuns.id, childRunId)); + const parentRunId = await insertRun({ companyId: fixture.companyId, agentId: fixture.agentId, issueId: fixture.parentIssueId, status: "running" }); + expect(await executionWorkspaceService(db).releaseAncestorWorkspaceBindingAtAdmission({ + companyId: fixture.companyId, issueId: fixture.parentIssueId, runId: parentRunId, + })).toBe(true); + expect((await readIssue(fixture.parentIssueId)).executionWorkspaceId).toBeNull(); + expect((await readIssue(fixture.childIssueId)).executionWorkspaceId).toBe(childWorkspaceId); + }); + + it("keeps a child's own unshared workspace", async () => { + const fixture = await seedOldReleaseChild(); + const childWorkspaceId = randomUUID(); + await db.insert(executionWorkspaces).values({ + id: childWorkspaceId, companyId: fixture.companyId, projectId: fixture.projectId, projectWorkspaceId: fixture.projectWorkspaceId, + sourceIssueId: fixture.childIssueId, mode: "isolated_workspace", strategyType: "git_worktree", name: "Child worktree", + status: "active", providerType: "git_worktree", providerRef: `/tmp/${childWorkspaceId}`, + }); + await db.update(issues).set({ executionWorkspaceId: childWorkspaceId }).where(eq(issues.id, fixture.childIssueId)); + const runId = await insertRun({ companyId: fixture.companyId, agentId: fixture.agentId, issueId: fixture.childIssueId, status: "running" }); + expect(await executionWorkspaceService(db).releaseAncestorWorkspaceBindingAtAdmission({ + companyId: fixture.companyId, issueId: fixture.childIssueId, runId, + })).toBe(false); + expect((await readIssue(fixture.childIssueId)).executionWorkspaceId).toBe(childWorkspaceId); + }); + + it("lets a running child finish in the shared workspace before rebinding", async () => { + const fixture = await seedOldReleaseChild(); + // The deploy lands while the child's earlier run is still running. + await insertRun({ companyId: fixture.companyId, agentId: fixture.agentId, issueId: fixture.childIssueId, status: "running" }); + const nextRunId = await insertRun({ companyId: fixture.companyId, agentId: fixture.agentId, issueId: fixture.childIssueId, status: "queued" }); + + expect(await executionWorkspaceService(db).releaseAncestorWorkspaceBindingAtAdmission({ + companyId: fixture.companyId, issueId: fixture.childIssueId, runId: nextRunId, + })).toBe(false); + expect((await readIssue(fixture.childIssueId)).executionWorkspaceId).toBe(fixture.parentWorkspaceId); + }); + + it("keeps an explicit share between unrelated tasks", async () => { + const fixture = await seedOldReleaseChild(); + await db.update(issues).set({ parentId: null }).where(eq(issues.id, fixture.childIssueId)); + const runId = await insertRun({ companyId: fixture.companyId, agentId: fixture.agentId, issueId: fixture.childIssueId, status: "running" }); + expect(await executionWorkspaceService(db).releaseAncestorWorkspaceBindingAtAdmission({ + companyId: fixture.companyId, issueId: fixture.childIssueId, runId, + })).toBe(false); + expect((await readIssue(fixture.childIssueId)).executionWorkspaceId).toBe(fixture.parentWorkspaceId); + }); + + it("never rebinds the parent, even when it is bound to its own workspace a child shares", async () => { + const fixture = await seedOldReleaseChild(); + const runId = await insertRun({ companyId: fixture.companyId, agentId: fixture.agentId, issueId: fixture.parentIssueId, status: "running" }); + expect(await executionWorkspaceService(db).releaseAncestorWorkspaceBindingAtAdmission({ + companyId: fixture.companyId, issueId: fixture.parentIssueId, runId, + })).toBe(false); + expect((await readIssue(fixture.parentIssueId)).executionWorkspaceId).toBe(fixture.parentWorkspaceId); + }); + }); +}); diff --git a/server/src/__tests__/task-workspace-api.test.ts b/server/src/__tests__/task-workspace-api.test.ts index af1429e353d..cee003d1b66 100644 --- a/server/src/__tests__/task-workspace-api.test.ts +++ b/server/src/__tests__/task-workspace-api.test.ts @@ -54,8 +54,10 @@ const support = await getEmbeddedPostgresTestSupport(); assigneeAgentId: f.agentId, status: "backlog", workspaceSelection: { kind: "existing", workspaceId: workspace.id } }), }); const response = await create(f.issueId), body = await response.json(); + // The selection passes the trust checks, but the workspace is the parent's, + // so the child gets its own workspace instead of sharing it. expect({ status: response.status, body }).toMatchObject({ status: 201, body: { - parentId: f.issueId, executionWorkspaceId: workspace.id, + parentId: f.issueId, executionWorkspaceId: null, executionPolicy: { authorizationPolicy: { trustBoundary: policy.authorizationPolicy.trustBoundary } }, } }); const denied = await create(f.blockerId); diff --git a/server/src/__tests__/task-workspace-binding.test.ts b/server/src/__tests__/task-workspace-binding.test.ts index 33a2d711aa7..54d967c1536 100644 --- a/server/src/__tests__/task-workspace-binding.test.ts +++ b/server/src/__tests__/task-workspace-binding.test.ts @@ -134,7 +134,7 @@ const support = await getEmbeddedPostgresTestSupport(); } }); - it.each(["create", "createChild"] as const)("inherits a parent task folder through %s with isolated workspaces disabled", async (entryPoint) => { + it.each(["create", "createChild"] as const)("gives a child its own task folder instead of the parent's through %s with isolated workspaces disabled", async (entryPoint) => { const f = await fixture(), settings = instanceSettingsService(db), previous = await settings.getExperimental(); await settings.updateExperimental({ enableIsolatedWorkspaces: false }); try { @@ -145,10 +145,13 @@ const support = await getEmbeddedPostgresTestSupport(); const child = entryPoint === "create" ? await tasks.create(f.companyId, { ...input, parentId: f.issueId }) : (await tasks.createChild(f.issueId, input)).issue; - expect(child).toMatchObject({ parentId: f.issueId, projectId: null, executionWorkspaceId: f.workspaceId, - executionWorkspacePreference: "reuse_existing" }); + expect(child).toMatchObject({ parentId: f.issueId, projectId: null, executionWorkspaceId: null }); + expect(child.executionWorkspacePreference).not.toBe("reuse_existing"); const view = await executionWorkspaceService(db).inspectTaskWorkspace(f.companyId, child.id, f.actor); - expect(view.workspace).toMatchObject({ id: f.workspaceId, cwd: `/tmp/task-${f.issueId}` }); + expect(view.workspace).toBeNull(); + // The parent keeps its own task folder. + expect((await executionWorkspaceService(db).inspectTaskWorkspace(f.companyId, f.issueId, f.actor)).workspace) + .toMatchObject({ id: f.workspaceId, cwd: `/tmp/task-${f.issueId}` }); const explicit = await tasks.create(f.companyId, { ...input, title: "Explicit separate folder", parentId: f.issueId, workspaceSelectionSource: "explicit" }); expect(explicit.executionWorkspaceId).toBeNull(); @@ -233,6 +236,11 @@ const support = await getEmbeddedPostgresTestSupport(); : source === "inherited" ? {} : { workspaceSelection: { kind: "existing" as const, workspaceId: f.workspaceId }, workspaceSelectionSource: source }), }); + if (source === "inherited") { + // A child never inherits the files its parent is bound to. + expect(created).toMatchObject({ projectId: project.id, executionWorkspaceId: null }); + return; + } expect(created).toMatchObject({ projectId: project.id, executionWorkspaceId: f.workspaceId, projectWorkspaceId: null }); const [original] = await db.select().from(issues).where(eq(issues.id, f.issueId)); expect(original).toMatchObject({ projectId: null, projectWorkspaceId: null, executionWorkspaceId: f.workspaceId }); diff --git a/server/src/__tests__/task-workspace-child-trust.test.ts b/server/src/__tests__/task-workspace-child-trust.test.ts index 246c2508880..804a82a23b9 100644 --- a/server/src/__tests__/task-workspace-child-trust.test.ts +++ b/server/src/__tests__/task-workspace-child-trust.test.ts @@ -45,13 +45,18 @@ const support = await getEmbeddedPostgresTestSupport(); const baseInput = scenario === "omitted_policy" ? { ...f.input, executionPolicy: undefined } : f.input; // Descendant trust is the subject here, so keep the deeper placement (board-style nesting). const input = scenario === "descendant" ? { ...baseInput, parentDepthPolicy: "allow_nested" as const } : baseInput; + // The selection passes trust checks, but the workspace belongs to an + // ancestor, so the child still gets its own workspace instead of sharing it. await expect(issueService(db).create(f.companyId, input)).resolves.toMatchObject({ - parentId: f.input.parentId, executionWorkspaceId: f.workspace.id, + parentId: f.input.parentId, executionWorkspaceId: null, }); }); it("allows a parent run to patch an authorized child's existing workspace settings", async () => { const f = await fixture(); const child = await issueService(db).create(f.companyId, { ...f.input, status: "backlog", responsibleUserId: f.userId }); + // Bind the child as an older release did, so the patch path still sees a bound workspace. + await db.update(issues).set({ executionWorkspaceId: f.workspace.id, executionWorkspacePreference: "reuse_existing" }) + .where(eq(issues.id, child.id)); const app = express(); app.use(express.json()); app.use((req, _res, next) => { diff --git a/server/src/onboarding-assets/ceo/HEARTBEAT.md b/server/src/onboarding-assets/ceo/HEARTBEAT.md index f80d325505a..1666caae6b3 100644 --- a/server/src/onboarding-assets/ceo/HEARTBEAT.md +++ b/server/src/onboarding-assets/ceo/HEARTBEAT.md @@ -47,7 +47,7 @@ Status quick guide: ## 6. Delegation -- Create subtasks with `POST /api/companies/{companyId}/issues`. Always set `parentId` and `goalId`. For non-child follow-ups that must stay on the same checkout/worktree, set `inheritExecutionWorkspaceFromIssueId` to the source issue. +- Create subtasks with `POST /api/companies/{companyId}/issues`. Always set `parentId` and `goalId`. Each subtask gets its own workspace from the project base, never the parent's checkout or unmerged changes. - When you know the needed work and owner, create those subtasks directly. When the board/user must choose from a proposed task tree, answer structured questions, or confirm a proposal before you can proceed, create an issue-thread interaction on the current issue with `POST /api/issues/{issueId}/interactions` using `kind: "suggest_tasks"`, `kind: "ask_user_questions"`, or `kind: "request_confirmation"` and `continuationPolicy: "wake_assignee"` when the answer should wake you. - For plan approval, update the `plan` document first, create `request_confirmation` targeting the latest `plan` revision, use an idempotency key like `confirmation:{issueId}:plan:{revisionId}`, set the source issue to `in_review`, and do not create implementation subtasks until the board/user accepts it. - `ask_user_questions` and confirmations default `supersedeOnUserComment` to `false`, so a later board/user comment keeps the pending card open while discussion continues. Set it to `true` when a new comment should replace the pending request. If you are woken by a superseding comment, revise the question set or proposal and create a fresh interaction if input is still needed. diff --git a/server/src/services/execution-workspace-repositories.ts b/server/src/services/execution-workspace-repositories.ts index fcd2d73c4c7..92be994cf24 100644 --- a/server/src/services/execution-workspace-repositories.ts +++ b/server/src/services/execution-workspace-repositories.ts @@ -208,6 +208,54 @@ export function executionWorkspaceRepositoryService(db: Db) { }); return { kind: "requires_next_admission" as const, applies: "next_normal_admission" as const, operationId: receipt.id, repository: receipt, reason: "immutable_admitted_workspace" }; } + /** + * A task in a project without a project workspace gets its repositories by + * acquisition into its own task files. A subtask's new workspace repeats its + * parent's (or requester's) repository requests as its own pending requests, + * so admission clones a fresh copy at the requested ref through the normal + * acquisition path, with the subtask's own access checks. Nothing is copied + * from the parent's checkout: no pinned commit, branch, or files. + */ + async function inheritSelections(input: { companyId: string; issueId: string; workspaceId: string }) { + return db.transaction(async tx => { + const [workspace] = await tx.select({ id: executionWorkspaces.id, sourceIssueId: executionWorkspaces.sourceIssueId }) + .from(executionWorkspaces).where(and(eq(executionWorkspaces.id, input.workspaceId), + eq(executionWorkspaces.companyId, input.companyId))).for("update"); + if (!workspace || workspace.sourceIssueId !== input.issueId) return 0; + const [existing] = await tx.select({ id: executionWorkspaceRepositories.id }).from(executionWorkspaceRepositories) + .where(and(eq(executionWorkspaceRepositories.companyId, input.companyId), + eq(executionWorkspaceRepositories.executionWorkspaceId, input.workspaceId))).limit(1); + if (existing) return 0; + const [task] = await tx.select({ parentId: issues.parentId, privacyParentIssueId: issues.privacyParentIssueId, + projectId: issues.projectId }) + .from(issues).where(and(eq(issues.id, input.issueId), eq(issues.companyId, input.companyId))); + const sourceIssueId = task?.privacyParentIssueId ?? task?.parentId; + if (!sourceIssueId) return 0; + const [source] = await tx.select({ executionWorkspaceId: issues.executionWorkspaceId, projectId: issues.projectId }) + .from(issues).where(and(eq(issues.id, sourceIssueId), eq(issues.companyId, input.companyId))); + if (!source?.executionWorkspaceId || source.executionWorkspaceId === input.workspaceId) return 0; + // Repositories belong to the source's project. A task in another project + // takes its own project's defaults instead, like a top-level task there. + if (task?.projectId !== source.projectId) return 0; + const requested = await tx.select().from(executionWorkspaceRepositories).where(and( + eq(executionWorkspaceRepositories.companyId, input.companyId), + eq(executionWorkspaceRepositories.executionWorkspaceId, source.executionWorkspaceId))); + if (!requested.length) return 0; + const rows = await tx.insert(executionWorkspaceRepositories).values(requested.map(row => { + const requestKey = `inherited:${row.id}`; + return { companyId: input.companyId, executionWorkspaceId: input.workspaceId, requestedByIssueId: input.issueId, + repositoryIdentity: row.repositoryIdentity, catalogRepositoryId: row.catalogRepositoryId, repoUrl: row.repoUrl, + relativePath: row.relativePath, requestedRef: row.requestedRef, requestKey, requestKeys: [requestKey] }; + })).onConflictDoNothing().returning({ id: executionWorkspaceRepositories.id }); + if (rows.length) { + await tx.insert(activityLog).values({ companyId: input.companyId, actorType: "system", actorId: "workspace-preparation", + action: "workspace.repository.requested", entityType: "execution_workspace", entityId: input.workspaceId, + details: { issueId: input.issueId, inheritedFromIssueId: sourceIssueId, repositoryIds: rows.map(row => row.id), + applies: "current_admission" } }); + } + return rows.length; + }); + } async function list(companyId: string, workspaceId: string) { return measureRunPhase("workspace.repository.receipts", () => db.select().from(executionWorkspaceRepositories).where(and(eq(executionWorkspaceRepositories.companyId, companyId), eq(executionWorkspaceRepositories.executionWorkspaceId, workspaceId)))); } @@ -352,8 +400,10 @@ export function executionWorkspaceRepositoryService(db: Db) { return candidatePin; } async function prepareForAdmission(input: { companyId: string; issueId: string; workspaceId: string; cwd: string; agentId: string; runId: string; responsibleUserId: string | null; - remoteTarget?: AdapterExecutionTarget | null; remoteLease?: EnvironmentLease; assertOwned?: () => Promise }) { + remoteTarget?: AdapterExecutionTarget | null; remoteLease?: EnvironmentLease; assertOwned?: () => Promise; + inheritRepositorySelections?: boolean }) { const { acquisitionRestricted, workspace } = await publicationBinding(db, input, false); + if (input.inheritRepositorySelections) await inheritSelections(input); const rows = await list(input.companyId, input.workspaceId); if (!rows.length) return []; await publicationBinding(db, input); diff --git a/server/src/services/execution-workspaces.ts b/server/src/services/execution-workspaces.ts index 4ee7b3daa5f..21dbe1a44d3 100644 --- a/server/src/services/execution-workspaces.ts +++ b/server/src/services/execution-workspaces.ts @@ -29,6 +29,7 @@ import { resolveCoreTrustPreset } from "./trust-preset-resolver.js"; import { CONTROLLER_PROVISION_COPY_ONLY, workspaceRequiresCopyOnlyProvisioning } from "./workspace-host-provision.js"; import { instanceSettingsService } from "./instance-settings.js"; import { taskWorkspaceSelectableCondition } from "./task-workspace-selection.js"; +import { bindsRelativesExecutionWorkspace, buildIndependentTaskWorkspaceSettings } from "./task-workspace-lineage.js"; import { executionWorkspaceRepositoryService, canonicalRepositoryRoot } from "./execution-workspace-repositories.js"; import { assertSelectedWorkspaceTrust, type TaskWorkspaceCreationTrustContext } from "./task-repository-acquisition.js"; import { forbidden } from "../errors.js"; @@ -1721,6 +1722,55 @@ export function executionWorkspaceService(db: Db, opts: ExecutionWorkspaceServic }); } + /** + * Releases a subtask from a workspace it shares with an ancestor (the shape + * older releases created by default) so this admission realizes the + * subtask's own workspace from the project base. The task that created the + * workspace keeps it and its files; a relative bound to it (an ancestor that + * selected a descendant's workspace included) is the one released. While an earlier run of this subtask is still + * running, finalizing, or under recovery, the binding is kept so that writer + * can save its files first; a later admission releases it. Never throws for + * that wait: the run proceeds on its current binding and nobody is asked to act. + */ + async function releaseAncestorWorkspaceBindingAtAdmission(input: { companyId: string; issueId: string; runId: string }) { + // Cheap unlocked pre-check: a task bound to a workspace it created keeps it. + // When a relative selected that workspace, the relative is the one that moves. + const [candidate] = await db.select({ sourceIssueId: executionWorkspaces.sourceIssueId }) + .from(issues).innerJoin(executionWorkspaces, eq(executionWorkspaces.id, issues.executionWorkspaceId)) + .where(and(eq(issues.id, input.issueId), eq(issues.companyId, input.companyId))).limit(1); + if (!candidate || candidate.sourceIssueId === input.issueId) return false; + return db.transaction(async tx => { + const [task] = await tx.select().from(issues).where(and(eq(issues.id, input.issueId), eq(issues.companyId, input.companyId))).for("update"); + if (!task?.executionWorkspaceId || task.workspacePendingSelection) return false; + if (!await bindsRelativesExecutionWorkspace(tx, { companyId: input.companyId, executionWorkspaceId: task.executionWorkspaceId, task })) { + return false; + } + const [holder] = await tx.select({ id: heartbeatRuns.id }).from(heartbeatRuns).where(and( + eq(heartbeatRuns.companyId, input.companyId), ne(heartbeatRuns.id, input.runId), + or(eq(heartbeatRuns.nativeIssueId, input.issueId), + sql`coalesce(${heartbeatRuns.contextSnapshot}->>'issueId', ${heartbeatRuns.contextSnapshot}->>'taskId') = ${input.issueId}`), + eq(heartbeatRuns.status, "running"), + )).limit(1); + if (holder) return false; + if (await hasPendingTaskWorkspaceRecovery(tx, { ...input, excludeRunId: input.runId })) return false; + const selection = task.workspaceSelection; + await notifyStatusCardIssueChange(tx, task.companyId); + await tx.update(issues).set({ + executionWorkspaceId: null, + executionWorkspacePreference: null, + // Without a project workspace the old settings described the relative's + // task files; the project's defaults apply instead, as for a top-level task. + executionWorkspaceSettings: task.projectWorkspaceId + ? buildIndependentTaskWorkspaceSettings(task.executionWorkspaceSettings) + : null, + ...(selection?.selection.kind === "existing" ? { workspaceSelection: null } : {}), + workspaceBindingRevision: task.workspaceBindingRevision + 1, + updatedAt: new Date(), + }).where(eq(issues.id, task.id)); + return true; + }); + } + async function bindTaskWorkspace(companyId: string, issueId: string, workspaceId: string, patch: TaskWorkspaceBindingPatch = {}) { return db.transaction(async tx => { const [task] = await measureRunPhase("workspace.row_lock", () => tx.select().from(issues).where(and(eq(issues.id, issueId), eq(issues.companyId, companyId))).for("no key update")); @@ -2739,7 +2789,7 @@ export function executionWorkspaceService(db: Db, opts: ExecutionWorkspaceServic assertTaskRepositoryPublicationAllowed: executionWorkspaceRepositoryService(db).assertPublicationAllowed, prepareConfiguredRepositoryCopy: executionWorkspaceRepositoryService(db).prepareConfiguredRepositoryCopy, assertTaskRepositoryRecoveryBinding: executionWorkspaceRepositoryService(db).assertRecoveryBinding, - resolveSourceWorkspaceStrategy, validateSelection, validateStoredSelectionForRun, inspectTaskWorkspace, selectTaskWorkspace, applyPendingTaskWorkspaceSelection, bindTaskWorkspace, prepareTaskWorkspaceUpdate, assertTaskWorkspaceUpdateAccess, + resolveSourceWorkspaceStrategy, validateSelection, validateStoredSelectionForRun, inspectTaskWorkspace, selectTaskWorkspace, applyPendingTaskWorkspaceSelection, releaseAncestorWorkspaceBindingAtAdmission, bindTaskWorkspace, prepareTaskWorkspaceUpdate, assertTaskWorkspaceUpdateAccess, listOverview: async ( companyId: string, filters: WorkspaceOverviewQuery, diff --git a/server/src/services/heartbeat.ts b/server/src/services/heartbeat.ts index 43496f50eb8..ea26ccf6390 100644 --- a/server/src/services/heartbeat.ts +++ b/server/src/services/heartbeat.ts @@ -2191,10 +2191,16 @@ export function heartbeatService( const taskKey = deriveTaskKeyWithHeartbeatFallback(context, null); const sessionCodec = getAdapterSessionCodec(agent.adapterType); const issueId = readNonEmptyString(context.issueId); + const freshWorkspace = await readFreshWorkspaceReadiness(db, run); if (issueId && canApplyTaskWorkspaceSelectionAtAdmission({ admittedInput: parseObject(run.runnerProfileJson).nativeExecutionInput, restarting: Boolean(runOptions.nativeRestartRecovery), hasLeaseOwner: Boolean(runOptions.nativeLeaseOwner) })) { const changed = await executionWorkspacesSvc.applyPendingTaskWorkspaceSelection({ companyId: agent.companyId, issueId, runId: run.id, actor: { type: "agent", agentId: agent.id, companyId: agent.companyId, source: "agent_key" } }); - if (changed) { + // A subtask an older release bound to its parent's workspace gets its own + // here. A process-loss retry or a continuation that keeps the current + // workspace still finishes the interrupted attempt there first. + const released = run.processLossRetryCount > 0 || (freshWorkspace && freshWorkspace.mode !== "fresh_isolated_workspace") ? false + : await executionWorkspacesSvc.releaseAncestorWorkspaceBindingAtAdmission({ companyId: agent.companyId, issueId, runId: run.id }); + if (changed || released) { delete context.resumeSessionParams; delete context.resumeSessionDisplayId; context.workspaceSelectionChanged = true; @@ -2323,7 +2329,6 @@ export function heartbeatService( const externalProvider = heartbeatExternalProvider({ run, agent }) ?? providerApiPlacement?.provider; const [taskWorkspaceIntentRow] = issueId && !externalProvider ? await db.select({ intent: issues.workspaceSelection }).from(issues) .where(and(eq(issues.id, issueId), eq(issues.companyId, agent.companyId))) : []; - const freshWorkspace = await readFreshWorkspaceReadiness(db, run); const contextProjectId = readNonEmptyString(context.projectId); const boundSourceWorkspace = !externalProvider && issueContext?.executionWorkspaceId ? await executionWorkspacesSvc.getStoredById(agent.companyId, issueContext.executionWorkspaceId) : null; diff --git a/server/src/services/heartbeat/workspace-preparation.ts b/server/src/services/heartbeat/workspace-preparation.ts index 86acdbd3d45..cce5290dce8 100644 --- a/server/src/services/heartbeat/workspace-preparation.ts +++ b/server/src/services/heartbeat/workspace-preparation.ts @@ -1033,6 +1033,9 @@ async function prepareHeartbeatWorkspaceLocked(db: Db, input: HeartbeatWorkspace const branchNameForInitialPersistence = pendingForwardBranchReconcile?.recordedBranchName ?? executionWorkspace.branchName; + // A workspace created by this admission may inherit its parent's repository + // requests (never its files); see executionWorkspaceRepositoryService. + let createdExecutionWorkspaceThisRun = false; try { if (remoteAuthority) { if (!remoteSourceState.value || !issueId) throw new Error("remote_workspace_source_missing"); @@ -1095,7 +1098,7 @@ async function prepareHeartbeatWorkspaceLocked(db: Db, input: HeartbeatWorkspace }, )) : !controllerOnly && !persistedNativeExecutionInput && (resolvedProjectId || issueRef) - ? await timeWorkspace("workspace_record_create", () => executionWorkspacesSvc.create({ + ? await timeWorkspace("workspace_record_create", () => (createdExecutionWorkspaceThisRun = true, executionWorkspacesSvc.create({ companyId: agent.companyId, projectId: resolvedProjectId, projectWorkspaceId: resolvedProjectWorkspaceId, @@ -1129,7 +1132,7 @@ async function prepareHeartbeatWorkspaceLocked(db: Db, input: HeartbeatWorkspace lastUsedAt: new Date(), openedAt: new Date(), metadata: nextExecutionWorkspaceMetadata, - })) + }))) : null; } catch (error) { if (executionWorkspace.created) { @@ -1223,7 +1226,8 @@ async function prepareHeartbeatWorkspaceLocked(db: Db, input: HeartbeatWorkspace const taskRepositories = await timeWorkspace("workspace_repository_admission", () => executionWorkspacesSvc.prepareTaskRepositoriesForAdmission({ companyId: agent.companyId, issueId, workspaceId: persistedExecutionWorkspace!.id, cwd: executionWorkspace.cwd, - agentId: agent.id, runId: run.id, responsibleUserId })); + agentId: agent.id, runId: run.id, responsibleUserId, + inheritRepositorySelections: createdExecutionWorkspaceThisRun && !resolvedProjectWorkspaceId })); projectRepositoryPaths.push(...taskRepositories.map(repository => repository.relativePath)); } if (!pluginPrepared && !pluginTaskWorkspace && !remoteAuthority && executionWorkspace.projectId diff --git a/server/src/services/issues.ts b/server/src/services/issues.ts index d6911172f7b..deef4eb5a50 100644 --- a/server/src/services/issues.ts +++ b/server/src/services/issues.ts @@ -191,6 +191,12 @@ import { type ParsedExecutionWorkspaceMode, } from "./execution-workspace-policy.js"; import { mergeExecutionWorkspaceConfig } from "./execution-workspaces.js"; +import { + buildIndependentTaskWorkspaceSettings, + isAncestorExecutionWorkspace, + loadAncestorIssueIds, + mergeExplicitWorkspaceSettings, +} from "./task-workspace-lineage.js"; import { hasChatRunOwnedProviderInteraction } from "./chat-interaction-arbitration.js"; import { readChatControlChronology } from "./chat-control-chronology.js"; import { authorizeChatConversationForBoundRun } from "./native-runtime/chat-attachment-reuse.js"; @@ -538,59 +544,6 @@ function buildReusedExecutionWorkspaceConfigPatchFromIssueSettings( }; } -// Accepted-plan children are not realized yet, so carry only unresolved -// workspace intent and let the first child run render/persist its own branch. -function buildPreRealizationExecutionWorkspaceSettings( - raw: unknown, -): Record | null { - const settings = parseIssueExecutionWorkspaceSettings(raw, { - includeEnvironmentId: true, - }); - if (!settings) return null; - const mode = - settings.mode && - settings.mode !== "inherit" && - settings.mode !== "reuse_existing" - ? settings.mode - : null; - const next: Record = {}; - if (mode) next.mode = mode; - if (settings.environmentId !== undefined) - next.environmentId = settings.environmentId; - if (settings.workspaceRuntime) - next.workspaceRuntime = settings.workspaceRuntime; - if (settings.workspaceStrategy) { - next.workspaceStrategy = { - type: settings.workspaceStrategy.type, - ...(settings.workspaceStrategy.baseRef - ? { baseRef: settings.workspaceStrategy.baseRef } - : {}), - ...(settings.workspaceStrategy.branchTemplate - ? { branchTemplate: settings.workspaceStrategy.branchTemplate } - : {}), - ...(settings.workspaceStrategy.existingBranch - ? { existingBranch: settings.workspaceStrategy.existingBranch } - : {}), - ...(settings.workspaceStrategy.worktreeParentDir - ? { worktreeParentDir: settings.workspaceStrategy.worktreeParentDir } - : {}), - ...(settings.workspaceStrategy.provisionCommand - ? { provisionCommand: settings.workspaceStrategy.provisionCommand } - : {}), - ...(settings.workspaceStrategy.runtimeProvisionCommand - ? { - runtimeProvisionCommand: - settings.workspaceStrategy.runtimeProvisionCommand, - } - : {}), - ...(settings.workspaceStrategy.teardownCommand - ? { teardownCommand: settings.workspaceStrategy.teardownCommand } - : {}), - }; - } - return Object.keys(next).length > 0 ? next : null; -} - function toTimestampMs(value: Date | string | null | undefined) { if (!value) return null; const date = value instanceof Date ? value : new Date(value); @@ -9760,14 +9713,14 @@ export function issueService(db: Db) { const { acceptanceCriteria, blockParentUntilDone, - executionWorkspaceInheritanceMode = "linkage", + // Deprecated and ignored: a child never reuses its parent's realized + // workspace, so every mode behaves like the old "strategy_only". + executionWorkspaceInheritanceMode: _deprecatedInheritanceMode, actorAgentId, actorUserId, ...issueData } = data; - const inheritStrategyOnly = - executionWorkspaceInheritanceMode === "strategy_only"; // A child may target another project. Parent workspace identity is only // valid inside the parent's project, so do not forward it across that // boundary; create() then resolves the target project's own workspaces. @@ -9778,12 +9731,23 @@ export function issueService(db: Db) { issueData.executionWorkspaceId !== undefined || issueData.executionWorkspacePreference !== undefined || issueData.executionWorkspaceSettings !== undefined; + // The child keeps the parent's workspace strategy, never its files. The + // strategy belongs to the parent's project, so it stays behind when the + // child targets another project. + // Only a parent realized from a project workspace has a strategy that + // applies to the project base; a parent in its own task files does not. + const parentStrategy = childInheritsParentProject && parent.projectWorkspaceId + ? buildIndependentTaskWorkspaceSettings(parent.executionWorkspaceSettings) + : null; const inheritedPreRealizationWorkspaceSettings = - inheritStrategyOnly && !hasExplicitExecutionWorkspaceOverride - ? buildPreRealizationExecutionWorkspaceSettings( - parent.executionWorkspaceSettings, - ) - : null; + !hasExplicitExecutionWorkspaceOverride + ? parentStrategy + : issueData.executionWorkspaceId === undefined && issueData.executionWorkspaceSettings + ? mergeExplicitWorkspaceSettings( + parentStrategy, + issueData.executionWorkspaceSettings as Record, + ) + : null; let child = await issueService(db).create(parent.companyId, { ...issueData, parentId: parent.id, @@ -9791,7 +9755,7 @@ export function issueService(db: Db) { projectWorkspaceId: issueData.projectWorkspaceId !== undefined ? issueData.projectWorkspaceId - : (inheritStrategyOnly && childInheritsParentProject + : (childInheritsParentProject ? parent.projectWorkspaceId : undefined), goalId: issueData.goalId ?? parent.goalId, @@ -9814,9 +9778,7 @@ export function issueService(db: Db) { inheritedPreRealizationWorkspaceSettings, } : {}), - ...(inheritStrategyOnly - ? { skipExecutionWorkspaceInheritance: true } - : { inheritExecutionWorkspaceFromIssueId: parent.id }), + skipExecutionWorkspaceInheritance: true, }); if (blockParentUntilDone) { @@ -10065,7 +10027,6 @@ export function issueService(db: Db) { tx as unknown as Db, ).createChild(sourceIssue.id, { ...nextChildInput, - executionWorkspaceInheritanceMode: "strategy_only", onDeduplicated: reason => { childReused = true; nextChildInput.onDeduplicated?.(reason); @@ -10449,6 +10410,7 @@ export function issueService(db: Db) { issueData.executionWorkspaceId !== undefined || issueData.executionWorkspacePreference !== undefined || issueData.executionWorkspaceSettings !== undefined; + let inheritedIndependentSettings: Record | null = null; if (workspaceInheritanceIssueId) { const workspaceSource = await getWorkspaceInheritanceIssue( tx, @@ -10476,36 +10438,34 @@ export function issueService(db: Db) { projectWorkspaceId = workspaceSource.projectWorkspaceId; inheritedWorkspaceSelection = true; } - if ( - inheritsSourceProject && - !hasExplicitExecutionWorkspaceOverride && - workspaceSource.executionWorkspaceId + // The source's realized workspace (its checkout, branch, saved + // snapshot and unmerged changes) is never shared. A parent and its + // child, or a source task and its follow-up, can run at the same + // time, and two writers in one folder lose each other's files. The + // new task keeps only the source's workspace strategy and realizes + // its own workspace from the project base on its first run. + // The source's strategy describes how to realize work from its + // project workspace. A source without one worked in its own task + // files, so the new task takes the project's defaults, exactly as a + // top-level task in that project would. + if (inheritsSourceProject && workspaceSource.projectWorkspaceId) { + inheritedIndependentSettings = buildIndependentTaskWorkspaceSettings( + workspaceSource.executionWorkspaceSettings, + ); + } + if (!hasExplicitExecutionWorkspaceOverride && executionWorkspaceSettings == null) { + executionWorkspaceSettings = inheritedIndependentSettings; + } else if ( + issueData.executionWorkspaceId === undefined && + issueData.executionWorkspaceSettings && + !(workspaceSelection !== undefined && workspaceSelectionSource === "explicit") ) { - const sourceWorkspace = await tx - .select({ - id: executionWorkspaces.id, - mode: executionWorkspaces.mode, - }) - .from(executionWorkspaces) - .where( - eq( - executionWorkspaces.id, - workspaceSource.executionWorkspaceId, - ), - ) - .then((rows) => rows[0] ?? null); - if (sourceWorkspace) { - executionWorkspaceId = sourceWorkspace.id; - inheritedWorkspaceSelection = true; - executionWorkspacePreference = "reuse_existing"; - executionWorkspaceSettings = { - ...((workspaceSource.executionWorkspaceSettings as - Record | null | undefined) ?? {}), - mode: issueExecutionWorkspaceModeForPersistedWorkspace( - sourceWorkspace.mode, - ), - }; - } + // A caller that names only a mode keeps the inherited base ref, + // branch template, commands and runtime for that mode. + executionWorkspaceSettings = mergeExplicitWorkspaceSettings( + inheritedIndependentSettings, + executionWorkspaceSettings, + ); } } const workspaceActor: AuthorizationActor = workspaceSelectionActor ?? (issueData.createdByUserId @@ -10525,6 +10485,25 @@ export function issueService(db: Db) { executionWorkspaceSettings = projection.executionWorkspaceSettings; workspaceIntent = { version: 1, selection: workspaceSelection, source: workspaceSelectionSource }; } + // No route may bind a new subtask to an ancestor's workspace, including + // an explicit id copied from the parent (older clients and skills did + // this). Drop it and keep only the ancestor's strategy, so the subtask + // realizes its own workspace from the project base. + if (executionWorkspaceId && (requestedParentId || issueData.parentId)) { + const ancestorIssueIds = [...new Set([ + ...(await loadAncestorIssueIds(tx, companyId, requestedParentId)), + ...(await loadAncestorIssueIds(tx, companyId, issueData.parentId)), + ])]; + if (await isAncestorExecutionWorkspace(tx, { companyId, executionWorkspaceId, ancestorIssueIds })) { + executionWorkspaceId = null; + executionWorkspacePreference = null; + executionWorkspaceSettings = mergeExplicitWorkspaceSettings( + inheritedIndependentSettings, + buildIndependentTaskWorkspaceSettings(executionWorkspaceSettings), + ); + if (workspaceIntent?.selection.kind === "existing") workspaceIntent = null; + } + } if (executionWorkspaceId) { const workspace = await assertValidExecutionWorkspace(companyId, null, executionWorkspaceId, tx); projectWorkspaceId = workspace.projectWorkspaceId; diff --git a/server/src/services/remote-workspace-reservation.test.ts b/server/src/services/remote-workspace-reservation.test.ts index b1980b44041..bace7084e1c 100644 --- a/server/src/services/remote-workspace-reservation.test.ts +++ b/server/src/services/remote-workspace-reservation.test.ts @@ -13,7 +13,6 @@ import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } fro import { createLocalDiskStorageProvider } from "../storage/local-disk-provider.js"; import { createStorageService } from "../storage/service.js"; import { instanceSettingsService } from "./instance-settings.js"; -import { issueService } from "./issues.js"; import { reserveRemoteWorkspace } from "./remote-workspaces.js"; import { remoteTerminationReceipt } from "./remote-execution-termination.js"; import { executionWorkspaceService } from "./execution-workspaces.js"; @@ -62,8 +61,9 @@ describe.skipIf(!support.supported)("remote workspace reservation authority", () metadata: { remoteWorkspace: authority } }).returning(); await db.update(issues).set({ executionWorkspaceId: workspace.id, executionWorkspacePreference: "reuse_existing", executionWorkspaceSettings: { mode } }).where(eq(issues.id, parent.id)); - const child = await issueService(db).create(company.id, { parentId: parent.id, title: "Continue the saved work", assigneeAgentId: agent.id }); - expect(child.executionWorkspaceId).toBe(workspace.id); + // New children never share a parent's workspace. This is the shape an older + // release wrote, which the reservation authority must still handle safely. + const child = await insertBoundChild(company.id, parent, workspace.id, mode, "Continue the saved work", agent.id); const [run] = await db.insert(heartbeatRuns).values({ companyId: company.id, agentId: agent.id, issueId: child.id, status: "running", runtimeMode: "legacy" }).returning(); const storage = { ...createStorageService(createLocalDiskStorageProvider(join(home, company.id))), provider: "s3" as const }; @@ -73,6 +73,14 @@ describe.skipIf(!support.supported)("remote workspace reservation authority", () const [boundWorkspace] = await db.select().from(executionWorkspaces).where(eq(executionWorkspaces.id, workspace.id)); return { input, parent, child, workspace: boundWorkspace!, authority, run, agent, company }; } + async function insertBoundChild(companyId: string, parent: typeof issues.$inferSelect, workspaceId: string, + mode: string, title: string, assigneeAgentId: string) { + const [child] = await db.insert(issues).values({ companyId, parentId: parent.id, projectId: parent.projectId, + projectWorkspaceId: parent.projectWorkspaceId, title, status: "todo", assigneeAgentId, + executionWorkspaceId: workspaceId, executionWorkspacePreference: "reuse_existing", + executionWorkspaceSettings: { mode } }).returning(); + return child!; + } const current = async (id: string) => (await db.select().from(executionWorkspaces).where(eq(executionWorkspaces.id, id)))[0]!; it.each(["isolated_workspace", "operator_branch", "shared_workspace"])("reserves an inherited %s using the persisted task binding", async mode => { @@ -180,8 +188,8 @@ describe.skipIf(!support.supported)("remote workspace reservation authority", () it("grants only one writer when two bound children reserve concurrently", async () => { const f = await fixture(); - const sibling = await issueService(db).create(f.company.id, { parentId: f.parent.id, - title: "Another child", assigneeAgentId: f.agent.id }); + const sibling = await insertBoundChild(f.company.id, f.parent, f.workspace.id, "isolated_workspace", + "Another child", f.agent.id); const [run] = await db.insert(heartbeatRuns).values({ companyId: f.company.id, agentId: f.agent.id, issueId: sibling.id, status: "running" }).returning(); const inputs = [f.input, { ...f.input, issueId: sibling.id, runId: run.id }]; diff --git a/server/src/services/remote-workspaces.integration.test.ts b/server/src/services/remote-workspaces.integration.test.ts index 1037afd1bbf..a290ad09b84 100644 --- a/server/src/services/remote-workspaces.integration.test.ts +++ b/server/src/services/remote-workspaces.integration.test.ts @@ -1147,8 +1147,11 @@ suite("remote workspace lifecycle with real Postgres and independent filesystem lease: await lease(), target: first }); await fs.writeFile(path.join(prepared.remoteRoot, "shared-result.txt"), "from the first task\n"); const childInput = { title: "Continue saved work", status: "in_progress" as const, assigneeAgentId: ids.agent }; - const child = inherited ? await issueService(db).create(ids.company, { ...childInput, parentId: ids.issue }) - : (await db.insert(issues).values({ ...childInput, companyId: ids.company, projectId, projectWorkspaceId: sourceId }).returning())[0]!; + // New children never share a parent's workspace; `inherited` is the shape an + // older release wrote, which saved-file continuation must still handle. + const child = (await db.insert(issues).values({ ...childInput, companyId: ids.company, projectId, projectWorkspaceId: sourceId, + ...(inherited ? { parentId: ids.issue, executionWorkspaceId: reserved.id, executionWorkspacePreference: "reuse_existing", + executionWorkspaceSettings: { mode } } : {}) }).returning())[0]!; if (inherited) expect(child.executionWorkspaceId).toBe(reserved.id); const secondIssue = child.id, secondRun = randomUUID(), secondLease = randomUUID(); await db.insert(heartbeatRuns).values({ id: secondRun, companyId: ids.company, agentId: ids.agent, diff --git a/server/src/services/task-workspace-lineage.ts b/server/src/services/task-workspace-lineage.ts new file mode 100644 index 00000000000..72ae0737411 --- /dev/null +++ b/server/src/services/task-workspace-lineage.ts @@ -0,0 +1,188 @@ +import { and, eq, inArray } from "drizzle-orm"; +import type { Db } from "@paperclipai/db"; +import { executionWorkspaces, issues } from "@paperclipai/db"; +import { parseIssueExecutionWorkspaceSettings } from "./execution-workspace-policy.js"; +import { loadParentChain } from "./issue-hierarchy.js"; + +type LineageReader = Pick; + +/** + * A subtask never shares a workspace with its parent or any other ancestor. + * It may keep the ancestor's workspace *strategy* (mode, environment, runtime, + * branch template, base ref), but nothing that names realized files: no + * workspace id, no `reuse_existing` preference, and no pinned existing branch. + * The first run realizes the subtask's own workspace from the project base, + * exactly as it would for a top-level task. + */ +export function buildIndependentTaskWorkspaceSettings( + raw: unknown, +): Record | null { + const settings = parseIssueExecutionWorkspaceSettings(raw, { + includeEnvironmentId: true, + }); + if (!settings) return null; + // `operator_branch` works on a named existing branch, which for a subtask + // would be the ancestor's branch; the project default applies instead. + const mode = + settings.mode && + settings.mode !== "inherit" && + settings.mode !== "reuse_existing" && + settings.mode !== "operator_branch" + ? settings.mode + : null; + const next: Record = {}; + if (mode) next.mode = mode; + // Policy fields that name no files carry over with the strategy. + if (settings.sharedWorkspaceConcurrency) + next.sharedWorkspaceConcurrency = settings.sharedWorkspaceConcurrency; + if (settings.networkEgress) next.networkEgress = settings.networkEgress; + if (settings.environmentId !== undefined) + next.environmentId = settings.environmentId; + if (settings.workspaceRuntime) + next.workspaceRuntime = settings.workspaceRuntime; + const strategy = settings.workspaceStrategy; + if (strategy) { + next.workspaceStrategy = { + type: strategy.type, + ...(strategy.baseRef ? { baseRef: strategy.baseRef } : {}), + ...(strategy.branchTemplate ? { branchTemplate: strategy.branchTemplate } : {}), + ...(strategy.worktreeParentDir ? { worktreeParentDir: strategy.worktreeParentDir } : {}), + ...(strategy.provisionCommand ? { provisionCommand: strategy.provisionCommand } : {}), + ...(strategy.runtimeProvisionCommand + ? { runtimeProvisionCommand: strategy.runtimeProvisionCommand } + : {}), + ...(strategy.teardownCommand ? { teardownCommand: strategy.teardownCommand } : {}), + }; + } + return Object.keys(next).length > 0 ? next : null; +} + +/** + * Lays a task's explicit workspace settings over the strategy it inherited, so + * a caller that names only a mode (the New sub-task dialog does) keeps the + * inherited base ref, branch template, commands and runtime. A different + * explicit mode is a different strategy, so it replaces the inherited one. + */ +export function mergeExplicitWorkspaceSettings( + inherited: Record | null, + explicit: Record | null, +): Record | null { + if (!inherited) return explicit; + if (!explicit) return inherited; + if (explicit.mode && inherited.mode && explicit.mode !== inherited.mode) return explicit; + const inheritedStrategy = inherited.workspaceStrategy as Record | undefined; + const explicitStrategy = explicit.workspaceStrategy as Record | undefined; + return { + ...inherited, + ...explicit, + ...(inheritedStrategy || explicitStrategy + ? { workspaceStrategy: { ...(inheritedStrategy ?? {}), ...(explicitStrategy ?? {}) } } + : {}), + }; +} + +/** + * The issues a task must never share a workspace with: its parent chain, plus + * the requested parent's chain when depth placement put the task beside its + * requester (recorded as `privacyParentIssueId`). + */ +export async function loadTaskLineageIssueIds( + db: Pick, + companyId: string, + task: { id: string; parentId: string | null; privacyParentIssueId?: string | null }, +): Promise { + const ids = new Set([ + ...(await loadAncestorIssueIds(db, companyId, task.parentId)), + ...(task.privacyParentIssueId && task.privacyParentIssueId !== task.parentId + ? await loadAncestorIssueIds(db, companyId, task.privacyParentIssueId) + : []), + ]); + ids.delete(task.id); + return [...ids]; +} + +/** Ids of `parentId` and every ancestor above it (company-scoped). */ +export async function loadAncestorIssueIds( + db: Pick, + companyId: string, + parentId: string | null | undefined, +): Promise { + if (!parentId) return []; + const chain = await loadParentChain(db, companyId, parentId); + return chain.map((row) => row.id); +} + +/** + * True when `task` is bound to a workspace that a relative in its task tree + * owns: an ancestor (or the requester it was placed beside) created it or is + * bound to it, or a descendant created it. The task that created the + * workspace keeps it; the relative bound to it is the one that moves, so its + * fresh workspace never lands on the owner's branch or folder. + */ +export async function bindsRelativesExecutionWorkspace( + db: LineageReader, + input: { + companyId: string; + executionWorkspaceId: string; + task: { id: string; parentId: string | null; privacyParentIssueId?: string | null }; + }, +): Promise { + const [workspace] = await db + .select({ sourceIssueId: executionWorkspaces.sourceIssueId }) + .from(executionWorkspaces) + .where(and( + eq(executionWorkspaces.companyId, input.companyId), + eq(executionWorkspaces.id, input.executionWorkspaceId), + )) + .limit(1); + const ownerIssueId = workspace?.sourceIssueId ?? null; + if (ownerIssueId === input.task.id) return false; + const ancestorIssueIds = await loadTaskLineageIssueIds(db, input.companyId, input.task); + if (await isAncestorExecutionWorkspace(db, { + companyId: input.companyId, + executionWorkspaceId: input.executionWorkspaceId, + ancestorIssueIds, + })) return true; + if (!ownerIssueId) return false; + const [owner] = await db + .select({ parentId: issues.parentId, privacyParentIssueId: issues.privacyParentIssueId }) + .from(issues) + .where(and(eq(issues.companyId, input.companyId), eq(issues.id, ownerIssueId))) + .limit(1); + if (!owner) return false; + const ownerLineage = await loadTaskLineageIssueIds(db, input.companyId, { id: ownerIssueId, ...owner }); + return ownerLineage.includes(input.task.id); +} + +/** + * True when the execution workspace belongs to one of `ancestorIssueIds`: + * an ancestor created it, or an ancestor is currently bound to it. Either way + * a descendant bound to it would write into the ancestor's files. + */ +export async function isAncestorExecutionWorkspace( + db: LineageReader, + input: { companyId: string; executionWorkspaceId: string; ancestorIssueIds: string[] }, +): Promise { + if (input.ancestorIssueIds.length === 0) return false; + const [workspace] = await db + .select({ sourceIssueId: executionWorkspaces.sourceIssueId }) + .from(executionWorkspaces) + .where(and( + eq(executionWorkspaces.companyId, input.companyId), + eq(executionWorkspaces.id, input.executionWorkspaceId), + )) + .limit(1); + if (workspace?.sourceIssueId && input.ancestorIssueIds.includes(workspace.sourceIssueId)) { + return true; + } + const [boundAncestor] = await db + .select({ id: issues.id }) + .from(issues) + .where(and( + eq(issues.companyId, input.companyId), + inArray(issues.id, input.ancestorIssueIds), + eq(issues.executionWorkspaceId, input.executionWorkspaceId), + )) + .limit(1); + return Boolean(boundAncestor); +} diff --git a/skills/paperclip/SKILL.md b/skills/paperclip/SKILL.md index 7b620731318..e47859edee2 100644 --- a/skills/paperclip/SKILL.md +++ b/skills/paperclip/SKILL.md @@ -280,7 +280,7 @@ response. Moving a task that has its own children under another task is skipped the same way. Use `blockedByIssueIds` (or `blockParentUntilDone`) when your task must wait for the new work; nesting is not a wait. Board users are not limited. -**Step 9 โ€” Delegate if needed.** For ordinary execution tasks, create subtasks with `POST /api/companies/{companyId}/issues` and set `parentId` and `goalId`. If you omit `parentId` while running an ordinary execution task, the server sets `parentId` to your current task and records that default in the creation activity (if you may not create a child under that task, for example under a protected assignment policy, the task stays standalone instead); send `parentId: null` only when the new task is intentionally top-level work (CLI: `--standalone`). Either way the follow-up keeps run provenance, and the task page shows "Created from by " without implying a blocker. For conversation tasks, use the project handoff above instead. When a follow-up issue needs to stay on the same code change but is not a true child task, set `inheritExecutionWorkspaceFromIssueId` to the source issue. Set `billingCode` for cross-team work. +**Step 9 โ€” Delegate if needed.** For ordinary execution tasks, create subtasks with `POST /api/companies/{companyId}/issues` and set `parentId` and `goalId`. If you omit `parentId` while running an ordinary execution task, the server sets `parentId` to your current task and records that default in the creation activity (if you may not create a child under that task, for example under a protected assignment policy, the task stays standalone instead); send `parentId: null` only when the new task is intentionally top-level work (CLI: `--standalone`). Either way the follow-up keeps run provenance, and the task page shows "Created from by " without implying a blocker. For conversation tasks, use the project handoff above instead. Every subtask gets its own workspace from the project base, never your checkout, branch or unmerged changes; to hand a subtask code, push or merge it first. Set `billingCode` for cross-team work. ### Delegating review tasks @@ -617,7 +617,7 @@ Exact response fields are documented in `skills/paperclip/references/api-referen - **Start actionable work before planning-only closure.** Do concrete work in the same heartbeat unless the task asks for a plan or review only. - **Leave a next action.** Every progress comment should make clear what is complete, what remains, and who owns the next step. - **Prefer child issues over polling.** Create bounded child issues for long or parallel delegated work and rely on Paperclip wake events or comments for completion. -- **Preserve workspace continuity for follow-ups.** Child issues inherit execution workspace from `parentId` server-side. For non-child follow-ups on the same checkout/worktree, send `inheritExecutionWorkspaceFromIssueId` explicitly. +- **Subtasks get their own workspace.** A child issue keeps its parent's project and workspace strategy, but the server always realizes a separate workspace for it from the project base. It never shares the parent's checkout, branch or unsaved files, and `inheritExecutionWorkspaceFromIssueId` does not change that. Share code between tasks through pushed branches or merged changes. - **Never cancel cross-team tasks.** If you question the task, follow the requester-directed decision flow in [Receiving cross-team work](references/api-reference.md#receiving-cross-team-work). Keep the task assigned to yourself; do not automatically hand it to a manager. - **Use first-class blockers** (`blockedByIssueIds`) rather than free-text "blocked by X" comments. - **Say only what you actually scheduled.** Never tell a user a "watcher"/monitor will wake you unless you scheduled a real issue monitor (non-null `monitorNextCheckAt`), and never imply a live watcher on a task you mark `done` โ€” see **Monitors and Watchers**. diff --git a/skills/paperclip/references/issue-workspaces.md b/skills/paperclip/references/issue-workspaces.md index 6442910537d..63a5f4fb9a1 100644 --- a/skills/paperclip/references/issue-workspaces.md +++ b/skills/paperclip/references/issue-workspaces.md @@ -18,7 +18,7 @@ Read `currentExecutionWorkspace`: - `status` / `closedAt` โ€” whether the workspace is usable - `runtimeServices[]` โ€” current services, including `serviceName`, `status`, `healthStatus`, `url`, `port`, and `runtimeServiceId` -If `currentExecutionWorkspace` is `null`, the issue does not currently have a realized execution workspace. For child/follow-up work, create the child with `parentId` or use `inheritExecutionWorkspaceFromIssueId` so Paperclip preserves workspace continuity. +If `currentExecutionWorkspace` is `null`, the issue does not currently have a realized execution workspace. Child and follow-up tasks always get their own workspace from the project base on their first run; they never reuse the parent's or source task's workspace, and the server refuses to bind a task to an ancestor's workspace. ## Choose Files and Repositories diff --git a/ui/src/components/NewIssueDialog.tsx b/ui/src/components/NewIssueDialog.tsx index 6fcce8d6e8b..94796956e4d 100644 --- a/ui/src/components/NewIssueDialog.tsx +++ b/ui/src/components/NewIssueDialog.tsx @@ -509,8 +509,6 @@ function AccountNewIssueDialog({ ignoreDefaults }: { ignoreDefaults: boolean }) const isSubIssueMode = Boolean(parentId); const parentIssueLabel = (newIssueDefaults.parentId === parentId ? newIssueDefaults.parentIdentifier : undefined) ?? (parentId ? parentId.slice(0, 8) : ""); - const parentExecutionWorkspaceId = newIssueDefaults.executionWorkspaceId ?? ""; - const parentExecutionWorkspaceLabel = newIssueDefaults.parentExecutionWorkspaceLabel ?? parentExecutionWorkspaceId; const { data: agents } = useQuery({ queryKey: queryKeys.agents.list(effectiveCompanyId!), @@ -1236,15 +1234,6 @@ function AccountNewIssueDialog({ ignoreDefaults }: { ignoreDefaults: boolean }) const selectedReusableWorktree = selectableReusableWorkspaces.find((workspace) => workspace.id === selectedExecutionWorkspaceId); const worktreeSelectionIncomplete = canChooseWorktrees && executionWorkspaceMode === "reuse_existing" && (worktreesLoading || worktreesError || !selectedReusableWorktree); - const isUsingParentExecutionWorkspace = - isSubIssueMode && parentExecutionWorkspaceId - ? executionWorkspaceMode === "reuse_existing" && selectedExecutionWorkspaceId === parentExecutionWorkspaceId - : false; - const showParentWorkspaceWarning = - isSubIssueMode && - currentProjectSupportsExecutionWorkspace && - Boolean(parentExecutionWorkspaceId) && - !isUsingParentExecutionWorkspace; const recentAssigneeIds = useMemo(() => getRecentAssigneeIds(), [newIssueOpen]); const recentProjectIds = useMemo(() => getRecentProjectIds(), [newIssueOpen]); const assigneeOptions = useMemo( @@ -1674,14 +1663,6 @@ function AccountNewIssueDialog({ ignoreDefaults }: { ignoreDefaults: boolean }) ) : null} - {showParentWorkspaceWarning ? ( -
- - This sub-task will no longer use the parent task workspace - {parentExecutionWorkspaceLabel ? ` (${parentExecutionWorkspaceLabel})` : ""}. - -
- ) : null} {effectiveCompanyId && neededUserSecretKeys.length > 0 ? ( ) : null} @@ -1739,7 +1720,7 @@ function AccountNewIssueDialog({ ignoreDefaults }: { ignoreDefaults: boolean }) { setExecutionWorkspaceMode(mode); diff --git a/ui/src/context/DialogContext.tsx b/ui/src/context/DialogContext.tsx index a7e4cbfb9a8..0cd608db4ff 100644 --- a/ui/src/context/DialogContext.tsx +++ b/ui/src/context/DialogContext.tsx @@ -13,7 +13,6 @@ interface NewIssueDefaults { parentTitle?: string; executionWorkspaceId?: string; executionWorkspaceMode?: string; - parentExecutionWorkspaceLabel?: string; assigneeAgentId?: string; assigneeUserId?: string; title?: string; diff --git a/ui/src/lib/subIssueDefaults.test.ts b/ui/src/lib/subIssueDefaults.test.ts index a29402d19d8..f11c35ee2b6 100644 --- a/ui/src/lib/subIssueDefaults.test.ts +++ b/ui/src/lib/subIssueDefaults.test.ts @@ -77,11 +77,12 @@ function makeIssue(overrides: Partial = {}): Issue { } describe("buildSubIssueDefaults", () => { - it("inherits the parent agent assignee and workspace context", () => { + it("inherits the parent agent assignee and workspace mode but never the parent's workspace", () => { const defaults = buildSubIssueDefaults( makeIssue({ assigneeAgentId: "agent-1", executionWorkspaceId: "workspace-1", + executionWorkspacePreference: "reuse_existing", currentExecutionWorkspace: makeExecutionWorkspace(), }), ); @@ -93,9 +94,8 @@ describe("buildSubIssueDefaults", () => { projectId: "project-1", projectWorkspaceId: "project-workspace-1", goalId: "goal-1", - executionWorkspaceId: "workspace-1", - executionWorkspaceMode: "reuse_existing", - parentExecutionWorkspaceLabel: "Parent workspace", + // The parent's realized mode carries over; its workspace does not. + executionWorkspaceMode: "isolated_workspace", assigneeAgentId: "agent-1", }); }); diff --git a/ui/src/lib/subIssueDefaults.ts b/ui/src/lib/subIssueDefaults.ts index 489342d7c48..6260393a62c 100644 --- a/ui/src/lib/subIssueDefaults.ts +++ b/ui/src/lib/subIssueDefaults.ts @@ -8,12 +8,26 @@ type SubIssueDefaultSource = Pick< | "projectId" | "projectWorkspaceId" | "goalId" - | "executionWorkspaceId" | "executionWorkspacePreference" - | "currentExecutionWorkspace" + | "executionWorkspaceSettings" | "assigneeAgentId" | "assigneeUserId" ->; +> & Partial>; + +// Modes a sub-task can start with on its own. `reuse_existing` and +// `operator_branch` would point the sub-task at the parent's files or branch. +const INDEPENDENT_SUB_ISSUE_MODES = new Set(["shared_workspace", "isolated_workspace", "agent_default"]); + +function independentSubIssueMode(issue: SubIssueDefaultSource): string | null { + for (const candidate of [ + issue.executionWorkspacePreference, + issue.executionWorkspaceSettings?.mode, + issue.currentExecutionWorkspace?.mode, + ]) { + if (typeof candidate === "string" && INDEPENDENT_SUB_ISSUE_MODES.has(candidate)) return candidate; + } + return null; +} export function buildSubIssueDefaults(issue: SubIssueDefaultSource) { return buildSubIssueDefaultsForViewer(issue); @@ -23,14 +37,11 @@ export function buildSubIssueDefaultsForViewer( issue: SubIssueDefaultSource, currentUserId?: string | null, ) { - const parentExecutionWorkspaceLabel = - issue.currentExecutionWorkspace?.name - ?? issue.currentExecutionWorkspace?.branchName - ?? issue.currentExecutionWorkspace?.cwd - ?? issue.executionWorkspaceId - ?? null; const shouldInheritUserAssignee = Boolean(issue.assigneeUserId && issue.assigneeUserId !== currentUserId); const inheritedAssigneeUserId = shouldInheritUserAssignee ? issue.assigneeUserId ?? undefined : undefined; + // A sub-task always gets its own workspace from the project base. It keeps + // the parent's workspace mode, never the parent's workspace or files. + const executionWorkspaceMode = independentSubIssueMode(issue); return { parentId: issue.id, @@ -39,13 +50,7 @@ export function buildSubIssueDefaultsForViewer( ...(issue.projectId ? { projectId: issue.projectId } : {}), ...(issue.projectWorkspaceId ? { projectWorkspaceId: issue.projectWorkspaceId } : {}), ...(issue.goalId ? { goalId: issue.goalId } : {}), - ...(issue.executionWorkspaceId ? { executionWorkspaceId: issue.executionWorkspaceId } : {}), - ...(issue.executionWorkspaceId - ? { executionWorkspaceMode: "reuse_existing" } - : issue.executionWorkspacePreference - ? { executionWorkspaceMode: issue.executionWorkspacePreference } - : {}), - ...(parentExecutionWorkspaceLabel ? { parentExecutionWorkspaceLabel } : {}), + ...(executionWorkspaceMode ? { executionWorkspaceMode } : {}), ...(issue.assigneeAgentId ? { assigneeAgentId: issue.assigneeAgentId } : {}), ...(inheritedAssigneeUserId ? { assigneeUserId: inheritedAssigneeUserId } : {}), };