Files
PaperClipAI/server/src/__tests__/issue-closed-workspace-routes.test.ts
T
Nicky LeachandPaperclip d0d242e843 feat(server): reopen an archived isolated execution workspace in place (#11322)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Issue execution uses isolated workspaces that hold the issue
worktree
> - A terminal issue can leave its isolated workspace archived and
unable to resume
> - The existing closed-workspace guards returned a conflict and gave
the user no self-serve recovery
> - This pull request reopens the same isolated workspace row and
rebuilds its worktree
> - The benefit is that resume, checkout, and comment actions can
continue without a new workspace row

## Linked Issues or Issue Description

**Problem or motivation**

A terminal issue can point to an archived isolated execution workspace.
Resume, checkout, and comment actions then stop with a conflict.

**What happened?**

A terminal issue kept its issue-to-workspace link after the isolated
workspace reached a closed status. The guarded actions returned HTTP 409
instead of restoring access.

**Expected behavior**

The next authorized resume, checkout, or comment action reopens the same
isolated workspace row. The action rebuilds the worktree and then
continues.

**Steps to reproduce**

1. Create an issue that uses an isolated execution workspace.
2. Move the issue to a terminal state and let the workspace archive.
3. Try to resume the issue or add a comment.
4. Observe the closed-workspace conflict.

**Paperclip version or commit**

e6e79f458e

**Deployment mode**

Built from source with pnpm.

**Proposed solution**

Reopen the closed isolated workspace in place. Rebuild the worktree
before the route reports success.

**Alternatives considered**

Create a new workspace row. This would require repointing the issue link
and would not preserve access for issues that share the original row.

**Roadmap alignment**

ROADMAP.md has no matching reopen item.

## What Changed

- Reopen closed isolated workspace rows in place and rebuild their
worktrees.
- Use the reopen path from resume, checkout, and comment guards.
- Return a clear error when the rebuild fails and keep the workspace
closed.
- Fence terminal reaping and archive cleanup while a reopen is in
flight.
- Update the comment composer to allow the next action to reopen the
workspace.
- Add service and route tests for reopen, scope, failure, and lifecycle
races.

## Verification

- Server TypeScript check passes with the tsc --noEmit command.
- UI TypeScript check passes with the tsc --noEmit command.
- Seventy-two server tests pass across the affected test files.
- The isolated worktree UI test suite has a dependency mismatch and
fails before tests run with the error TypeError: act is not a function.
- CI confirms the UI test job after a clean dependency install.

## Risks

The reopen path changes behavior for closed isolated workspaces. A
rebuild failure returns an error and keeps the row closed. Lifecycle
locks and generation checks protect the worktree from stale cleanup. The
UI test mismatch needs CI confirmation.

## Model Used

OpenAI Codex, GPT-5, with tool use and code review assistance. The
runtime did not provide the context window size.

## 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 references)
- [x] My branch name describes the change 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

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-08-13 07:44:19 -07:00

453 lines
16 KiB
TypeScript

import express from "express";
import request from "supertest";
import { beforeEach, describe, expect, it, vi } from "vitest";
const issueId = "11111111-1111-4111-8111-111111111111";
const closedWorkspaceId = "33333333-3333-4333-8333-333333333333";
const nextWorkspaceId = "44444444-4444-4444-8444-444444444444";
const agentId = "22222222-2222-4222-8222-222222222222";
const mockIssueService = vi.hoisted(() => ({
getById: vi.fn(),
update: vi.fn(),
checkout: vi.fn(),
addComment: vi.fn(),
}));
const mockExecutionWorkspaceService = vi.hoisted(() => ({
getById: vi.fn(),
reopenClosedIsolatedExecutionWorkspaceForIssue: vi.fn(),
clearReopenPendingConsumptionForUnconsumedReopen: vi.fn(async () => ({ cleared: true })),
}));
const mockAccessService = vi.hoisted(() => ({
canUser: vi.fn(),
hasPermission: vi.fn(),
}));
const mockHeartbeatService = vi.hoisted(() => ({
wakeup: vi.fn(async () => undefined),
reportRunActivity: vi.fn(async () => undefined),
getRun: vi.fn(async () => null),
getActiveRunForAgent: vi.fn(async () => null),
cancelRun: vi.fn(async () => null),
}));
const mockProjectService = vi.hoisted(() => ({
getById: vi.fn(async () => null),
}));
const mockLogActivity = vi.hoisted(() => vi.fn(async () => undefined));
function registerServiceMocks() {
vi.doMock("../routes/authz.js", async () => vi.importActual("../routes/authz.js"));
vi.doMock("@paperclipai/shared/telemetry", () => ({
trackAgentTaskCompleted: vi.fn(),
trackErrorHandlerCrash: vi.fn(),
}));
vi.doMock("../telemetry.js", () => ({
getTelemetryClient: vi.fn(() => ({ track: vi.fn() })),
}));
vi.doMock("../services/access.js", () => ({
accessService: () => mockAccessService,
}));
vi.doMock("../services/activity-log.js", () => ({
logActivity: mockLogActivity,
}));
vi.doMock("../services/execution-workspaces.js", () => ({
executionWorkspaceService: () => mockExecutionWorkspaceService,
STALE_REOPEN_PENDING_CONSUMPTION_GRACE_MS: 5 * 60 * 1000,
}));
vi.doMock("../services/heartbeat.js", () => ({
heartbeatService: () => mockHeartbeatService,
}));
vi.doMock("../services/issues.js", () => ({
issueService: () => mockIssueService,
}));
vi.doMock("../services/projects.js", () => ({
projectService: () => mockProjectService,
}));
vi.doMock("../services/index.js", () => ({
companyService: () => ({
getById: vi.fn(async () => ({ id: "company-1", attachmentMaxBytes: 10 * 1024 * 1024 })),
}),
accessService: () => mockAccessService,
agentService: () => ({
getById: vi.fn(async () => null),
}),
companySkillService: () => ({
completeTestRunForIssue: vi.fn(async () => null),
}),
documentAnnotationService: () => ({ remapOpenThreadsForDocument: async () => [] }),
documentService: () => ({}),
executionWorkspaceService: () => mockExecutionWorkspaceService,
feedbackService: () => ({
listIssueVotesForUser: vi.fn(async () => []),
saveIssueVote: vi.fn(async () => ({ vote: null, consentEnabledNow: false, sharingEnabled: false })),
}),
goalService: () => ({
getDefaultCompanyGoal: vi.fn(async () => null),
getById: vi.fn(async () => null),
}),
heartbeatService: () => mockHeartbeatService,
instanceSettingsService: () => ({
get: vi.fn(async () => ({
id: "instance-settings-1",
general: {
censorUsernameInLogs: false,
feedbackDataSharingPreference: "prompt",
},
})),
listCompanyIds: vi.fn(async () => ["company-1"]),
}),
issueApprovalService: () => ({}),
issueReferenceService: () => ({
deleteDocumentSource: async () => undefined,
diffIssueReferenceSummary: () => ({
addedReferencedIssues: [],
removedReferencedIssues: [],
currentReferencedIssues: [],
}),
emptySummary: () => ({ outbound: [], inbound: [] }),
listIssueReferenceSummary: async () => ({ outbound: [], inbound: [] }),
syncComment: async () => undefined,
syncDocument: async () => undefined,
syncIssue: async () => undefined,
}),
issueThreadInteractionService: () => ({
listForIssue: vi.fn(async () => []),
expireRequestConfirmationsSupersededByComment: vi.fn(async () => []),
expireStaleRequestConfirmationsForIssueDocument: vi.fn(async () => []),
}),
issueRecoveryActionService: () => ({
getActiveForIssue: vi.fn(async () => null),
listActiveForIssues: vi.fn(async () => new Map()),
}),
issueService: () => mockIssueService,
logActivity: mockLogActivity,
projectService: () => mockProjectService,
routineService: () => ({
syncRunStatusForIssue: vi.fn(async () => undefined),
}),
workProductService: () => ({}),
}));
}
async function createApp(actor?: Record<string, unknown>) {
const [{ issueRoutes }, { errorHandler }] = await Promise.all([
import("../routes/issues.js"),
import("../middleware/index.js"),
]);
const app = express();
app.use(express.json());
app.use((req, _res, next) => {
(req as any).actor = actor ?? {
type: "board",
userId: "local-board",
companyIds: ["company-1"],
source: "local_implicit",
isInstanceAdmin: false,
};
next();
});
app.use("/api", issueRoutes({} as any, {} as any));
app.use(errorHandler);
return app;
}
function makeIssue() {
return {
id: issueId,
companyId: "company-1",
status: "todo",
priority: "medium",
assigneeAgentId: agentId,
assigneeUserId: null,
createdByUserId: "local-board",
identifier: "PAP-1085",
title: "Closed worktree issue",
projectId: null,
executionRunId: null,
checkoutRunId: null,
executionWorkspaceId: closedWorkspaceId,
};
}
function makeClosedWorkspace() {
return {
id: closedWorkspaceId,
name: "PAP-1085-fix-worktree-guard",
mode: "isolated_workspace",
status: "archived",
closedAt: new Date("2026-04-04T17:00:00.000Z"),
};
}
describe.sequential("closed isolated workspace issue routes", () => {
beforeEach(() => {
vi.resetModules();
vi.doUnmock("@paperclipai/shared/telemetry");
vi.doUnmock("../telemetry.js");
vi.doUnmock("../services/access.js");
vi.doUnmock("../services/activity-log.js");
vi.doUnmock("../services/execution-workspaces.js");
vi.doUnmock("../services/heartbeat.js");
vi.doUnmock("../services/index.js");
vi.doUnmock("../services/issues.js");
vi.doUnmock("../services/projects.js");
vi.doUnmock("../routes/issues.js");
vi.doUnmock("../routes/authz.js");
vi.doUnmock("../middleware/index.js");
registerServiceMocks();
vi.clearAllMocks();
mockIssueService.getById.mockResolvedValue(makeIssue());
mockExecutionWorkspaceService.getById.mockResolvedValue(makeClosedWorkspace());
// The guard reopens a closed isolated workspace and lets the request
// continue. The default is a successful reopen.
mockExecutionWorkspaceService.reopenClosedIsolatedExecutionWorkspaceForIssue.mockResolvedValue({
ok: true,
reopened: true,
workspace: { ...makeClosedWorkspace(), status: "active", closedAt: null },
generation: 4,
});
});
it("reopens the closed isolated workspace and accepts a new comment", async () => {
const res = await request(await createApp())
.post(`/api/issues/${issueId}/comments`)
.send({ body: "hello" });
expect(mockExecutionWorkspaceService.reopenClosedIsolatedExecutionWorkspaceForIssue).toHaveBeenCalledWith({
workspaceId: closedWorkspaceId,
issue: { id: issueId, companyId: "company-1", projectId: null },
actor: expect.objectContaining({ actorType: "user" }),
});
// The closed-workspace dead end is gone.
expect(res.status).not.toBe(409);
});
it("reopens the closed isolated workspace and accepts a comment update", async () => {
const res = await request(await createApp())
.patch(`/api/issues/${issueId}`)
.send({ comment: "hello" });
expect(mockExecutionWorkspaceService.reopenClosedIsolatedExecutionWorkspaceForIssue).toHaveBeenCalledTimes(1);
expect(res.status).not.toBe(409);
});
it("reopens the closed isolated workspace and accepts a checkout", async () => {
const res = await request(await createApp())
.post(`/api/issues/${issueId}/checkout`)
.send({
agentId,
expectedStatuses: ["todo", "backlog", "blocked"],
});
expect(mockExecutionWorkspaceService.reopenClosedIsolatedExecutionWorkspaceForIssue).toHaveBeenCalledTimes(1);
expect(res.status).not.toBe(409);
});
it("returns 409 and blocks the comment when the workspace cannot be reopened", async () => {
mockExecutionWorkspaceService.reopenClosedIsolatedExecutionWorkspaceForIssue.mockResolvedValue({
ok: false,
code: "not_reopenable",
message: "Execution workspace is not reopenable",
});
const res = await request(await createApp())
.post(`/api/issues/${issueId}/comments`)
.send({ body: "hello" });
expect(res.status).toBe(409);
expect(mockIssueService.addComment).not.toHaveBeenCalled();
});
it("returns 503 and blocks the checkout when the rebuild fails", async () => {
mockExecutionWorkspaceService.reopenClosedIsolatedExecutionWorkspaceForIssue.mockResolvedValue({
ok: false,
code: "rebuild_failed",
message: "Failed to rebuild the execution workspace",
});
const res = await request(await createApp())
.post(`/api/issues/${issueId}/checkout`)
.send({
agentId,
expectedStatuses: ["todo", "backlog", "blocked"],
});
expect(res.status).toBe(503);
expect(mockIssueService.checkout).not.toHaveBeenCalled();
});
it("does not reopen the workspace when a checkout fails the run-id gate", async () => {
// An agent checkout without a run id is rejected before the reopen runs. The
// reopen must not rebuild and republish the workspace, or the still-terminal
// issue keeps a leaked active workspace that the reaper skips.
const agentActorWithoutRunId = {
type: "agent",
agentId,
companyId: "company-1",
runId: null,
source: "agent_key",
};
const res = await request(await createApp(agentActorWithoutRunId))
.post(`/api/issues/${issueId}/checkout`)
.send({
agentId,
expectedStatuses: ["todo", "backlog", "blocked"],
});
expect(res.status).toBe(401);
expect(
mockExecutionWorkspaceService.reopenClosedIsolatedExecutionWorkspaceForIssue,
).not.toHaveBeenCalled();
expect(mockIssueService.checkout).not.toHaveBeenCalled();
});
it("clears the reopen-pending flag when the comment update returns null after a reopen", async () => {
// The workspace reopens, but the issue update then returns null. The issue
// stays terminal, so the guard must clear the reopen-pending flag. Otherwise
// the terminal reaper and the archive route skip the rebuilt worktree forever.
mockIssueService.getById.mockResolvedValue({ ...makeIssue(), status: "done" });
mockIssueService.update.mockResolvedValue(null);
const res = await request(await createApp())
.patch(`/api/issues/${issueId}`)
.send({ comment: "hello" });
expect(res.status).toBe(404);
await vi.waitFor(() => {
expect(
mockExecutionWorkspaceService.clearReopenPendingConsumptionForUnconsumedReopen,
).toHaveBeenCalledWith(
expect.objectContaining({
workspaceId: closedWorkspaceId,
issue: expect.objectContaining({ id: issueId }),
expectedGeneration: 4,
}),
);
});
});
it("clears the reopen-pending flag when the checkout throws after a reopen", async () => {
mockIssueService.getById.mockResolvedValue({ ...makeIssue(), status: "done" });
mockIssueService.checkout.mockRejectedValue(new Error("checkout failed"));
const res = await request(await createApp())
.post(`/api/issues/${issueId}/checkout`)
.send({
agentId,
expectedStatuses: ["todo", "backlog", "blocked"],
});
expect(res.status).toBe(500);
await vi.waitFor(() => {
expect(
mockExecutionWorkspaceService.clearReopenPendingConsumptionForUnconsumedReopen,
).toHaveBeenCalledWith(
expect.objectContaining({
workspaceId: closedWorkspaceId,
issue: expect.objectContaining({ id: issueId }),
expectedGeneration: 4,
}),
);
});
});
it("does not clear the reopen-pending flag when the checkout resumes the issue", async () => {
// The checkout moves the issue out of the terminal state, so the reaper clears
// the flag on its next cycle. The route must not clear it here.
mockIssueService.getById.mockResolvedValue({ ...makeIssue(), status: "done" });
mockIssueService.checkout.mockResolvedValue({ ...makeIssue(), status: "in_progress" });
const res = await request(await createApp())
.post(`/api/issues/${issueId}/checkout`)
.send({
agentId,
expectedStatuses: ["todo", "backlog", "blocked"],
});
expect(res.status).toBe(200);
await new Promise((resolve) => setImmediate(resolve));
expect(
mockExecutionWorkspaceService.clearReopenPendingConsumptionForUnconsumedReopen,
).not.toHaveBeenCalled();
});
it("does not clear the reopen-pending flag when a concurrent request already reopened the workspace", async () => {
// A concurrent request reopened the workspace first, so this request receives
// reopened: false and never set the flag. Even though the checkout leaves the
// issue terminal, this request must not clear the flag that the other request
// owns. Otherwise the reaper or the archive route can destroy the rebuilt
// worktree while the other request still uses it.
mockIssueService.getById.mockResolvedValue({ ...makeIssue(), status: "done" });
mockExecutionWorkspaceService.reopenClosedIsolatedExecutionWorkspaceForIssue.mockResolvedValue({
ok: true,
reopened: false,
workspace: { ...makeClosedWorkspace(), status: "active", closedAt: null },
generation: 4,
});
mockIssueService.checkout.mockResolvedValue(null);
const res = await request(await createApp())
.post(`/api/issues/${issueId}/checkout`)
.send({
agentId,
expectedStatuses: ["todo", "backlog", "blocked"],
});
expect(res.status).toBe(200);
await new Promise((resolve) => setImmediate(resolve));
expect(
mockExecutionWorkspaceService.clearReopenPendingConsumptionForUnconsumedReopen,
).not.toHaveBeenCalled();
});
it("retries the reopen-pending clear when the first attempt fails transiently", async () => {
// The workspace reopens, but the comment update returns null, so the issue
// stays terminal and the guard must clear the flag. A transient failure of the
// first clear must not strand the flag; the guard retries until it succeeds.
mockIssueService.getById.mockResolvedValue({ ...makeIssue(), status: "done" });
mockIssueService.update.mockResolvedValue(null);
mockExecutionWorkspaceService.clearReopenPendingConsumptionForUnconsumedReopen
.mockRejectedValueOnce(new Error("transient database error"))
.mockResolvedValue({ cleared: true });
const res = await request(await createApp())
.patch(`/api/issues/${issueId}`)
.send({ comment: "hello" });
expect(res.status).toBe(404);
await vi.waitFor(() => {
expect(
mockExecutionWorkspaceService.clearReopenPendingConsumptionForUnconsumedReopen.mock.calls.length,
).toBeGreaterThanOrEqual(2);
});
});
it("still allows non-comment board updates so the issue can be moved to a new workspace", async () => {
mockIssueService.update.mockResolvedValue({
...makeIssue(),
executionWorkspaceId: nextWorkspaceId,
});
const res = await request(await createApp())
.patch(`/api/issues/${issueId}`)
.send({ executionWorkspaceId: nextWorkspaceId });
expect(res.status).toBe(200);
expect(res.body.executionWorkspaceId).toBe(nextWorkspaceId);
});
});