From c1451814c155adab79fd0181674a23b0dc2669e4 Mon Sep 17 00:00:00 2001 From: Dotta <34892728+cryppadotta@users.noreply.github.com> Date: Sat, 14 Nov 2026 05:46:48 -0600 Subject: [PATCH 1/2] perf(chat): scope GitHub review check run hints to the issue and throttle company-wide rebuilds (#21610) 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 > - The GitHub chat channel keeps each unresolved "Paperclip Review" check in step with its task and run. A background queue (`chat-github-review-checks`) rebuilds its checks when a hint arrives > - Each run milestone (`notifyChatRunMilestones`) sends this queue a company-wide hint, also when the milestone knows the issue. Each company-wide hint rebuilds every unfinished check of the company and reads its full state again. Passes are limited to one a second > - So in a company with a steady stream of run milestones, each unresolved check reads its whole state about once a second, although nothing about it changed > - On a hosted test database the five state statements of a review check ran 10.3 times a second each. #21608 removes the endless 5-second head poll. This is the second cause > - This pull request sends the issue with the hint, rebuilds only the checks of that issue, and rebuilds for a company-wide hint at most once every 30 seconds > - The benefit is that run activity no longer makes every unresolved check of the company read its state each second. A run change of the check's own task still publishes at once ## Linked Issues or Issue Description Refs #21608 (backs off the head poll of an unresolved check; the other cause of the same hosted load). Refs #21580 (the same 30-second rule for the GitHub response sweeps). Refs #18384 (the same rule for external objects). **What happened?** On a hosted test database with zero active runs, the five statements of `githubReviewCheckState` (`chat_endpoints`, `chat_github_reviews`, `chat_message_links`, the `heartbeat_runs` `wakeCommentId` lookup and `chat_deliveries`) ran 10.3 times a second each. One source is the rebuild of the review check queue for company-wide hints. **Current behavior** `notifyChatRunMilestones` sends `{"companyId": ...}` to `chat-github-review-checks` for every milestone, also when its scope has an issue. The queue scope has no issue field. So each milestone rebuilds every received and retryable check of the company, and calls `githubReviewCheckState` for each received check. **Proposed behavior** A milestone with an issue sends `{"companyId", "issueId"}`. The queue matches a check to an issue through the issue of its latest review for the pull request head. This is the same review that `githubReviewCheckState` reads. A company-wide hint (company ID only) rebuilds at most once every 30 seconds. All narrower hints, the startup scope (`*`) and saved deadlines are not changed. **Steps to reproduce** 1. Admit a GitHub review check that stays unresolved (a review with no run that the check can find). 2. Send a run milestone for the company once a second (for example, status changes of other runs). 3. Count statements. Each hint rebuilds the check and reads its full state. ## What Changed - `review-check-work.ts`: the scope has an optional `issueId`. `page()` reads the issue of the latest review for the check head (`reviewIssueId`, through the existing `chat_github_reviews_pull_idx` index) and filters on it for an issue scope. `matches()` uses the same value. - `review-check-work.ts`: set `reconstructionIntervalMs` to `githubReviewCheckReconstructionIntervalMs`: 30 seconds for a company-only scope, 0 for all other scopes. - `chat-run-milestone-notifications.ts`: send the issue with the review check hint when the milestone has one. - `chat-channels.integration.test.ts`: two regression tests on the real queue, the real `dispatch` and embedded PostgreSQL with a controlled clock. The test helper now counts only the state read of `chat_github_reviews`, not the new subquery in `page()`. ## Verification Red/green regression tests: - `cd server && npx vitest run src/__tests__/chat-channels.integration.test.ts -t "company-wide hints at most once per interval|at once from its issue milestone"` - Red (without the fix): `expected [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ] to deeply equal [ 1, +0, +0, +0, +0, +0, +0, +0, …(2) ]`. Each of 10 company-wide hints read the check state. The milestone hint was `{"companyId"}` only. - Green: 2 of 2 pass. The first company-wide hint reads the state once. The next 9 read nothing. After 30 seconds the waiting hint reads it once. A hint for the check's issue reads it at once each time. A hint for another issue or another company reads nothing. A real `notifyChatRunMilestones` with the check's issue publishes `in_progress` on the next pass and resets the head poll interval. Statement counts for one unresolved check with one company-wide hint each second (120 simulated seconds, real queue and `dispatch`, embedded PostgreSQL): | | Statements a second | Check state reads a second | | --- | --- | --- | | Before | 6.0 | 1.0 | | After | 0.2 | 0.033 | In the real-coordinator reproduction of #21608, one hint each second raised one stuck check from 0.70 to 1.67 state reads a second and from 8.4 to 15.2 statements a second. Other checks: - `cd server && npx vitest run src/__tests__/chat-channels.integration.test.ts -t ithub`: passed, including all review check queue tests. - `cd server && npx vitest run src/__tests__/native-status-reassessment-work.test.ts src/__tests__/chat-run-milestone-work.test.ts src/services/native-runtime/external-chat-wait.integration.test.ts src/services/chat-github-review-repair.test.ts src/__tests__/idle-completed-history.test.ts src/__tests__/chat-admission-condition.test.ts src/__tests__/app-private-hostname-gate.test.ts`: 518 passed. - `cd server && node ../scripts/run-tsc.mjs` and `node scripts/typecheck-layers.mjs` (with test layers): passed. - `pnpm verify --explain` retains 2,718 test files conservatively, so I did not run it locally. Required CI supplies that coverage. ## Risks - A change that only a company-wide hint reports can take up to 30 seconds to reach a check. Company-wide hints now come from milestones without an issue (a run with no task, a comment merged into an active run) and from company authority changes. A run with no task cannot be the run of a review check. For a merged comment, the run's next status change sends an issue hint. The saved head poll deadline still applies. Each dispatch reads the full state again before it publishes. - An issue hint finds a check through the issue of its latest review for the head. If a run of another issue were the check's run, its change would wait for the next company-wide rebuild (30 seconds at most) or the next head poll. Run status and run identity milestones use the run's task issue, which is the review's issue for a GitHub review task. - The deadline reconciliation snapshot reports the queue as not reconciled while a company-wide hint waits (up to 30 seconds). This is the same as the GitHub response sweeps and the external-object refresh sweep. - `page()` reads one more indexed subquery for each row. It uses `chat_github_reviews_pull_idx`. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), Anthropic. Used through Claude Code with tool use (shell, file edits, and local embedded PostgreSQL reproductions). ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 --- .../chat-channels.integration.test.ts | 50 +++++++++++++++++-- .../chat-channels/github/review-check-work.ts | 26 ++++++++-- .../chat-run-milestone-notifications.ts | 2 +- 3 files changed, 69 insertions(+), 9 deletions(-) diff --git a/server/src/__tests__/chat-channels.integration.test.ts b/server/src/__tests__/chat-channels.integration.test.ts index fa0fea2c7f6..ff6ce27cc87 100644 --- a/server/src/__tests__/chat-channels.integration.test.ts +++ b/server/src/__tests__/chat-channels.integration.test.ts @@ -82,6 +82,7 @@ import { githubChatReviewService } from "../services/chat-github-reviews.js"; import { resolveGitHubReviewInstructions } from "../services/chat-channels/github/review-instructions.js"; import { githubResponseCommentService } from "../services/chat-github-response-comments.js"; import { GITHUB_REVIEW_CHECK_MAX_HEAD_POLL_MS, githubReviewCheckService } from "../services/chat-github-checks.js"; +import { GITHUB_REVIEW_CHECK_COMPANY_RECONSTRUCTION_MS } from "../services/chat-channels/github/review-check-work.js"; import { githubAutomaticReviewEvent } from "../services/chat-github-events.js"; import { githubBotToolsForSession, syncGitHubBotTools } from "../services/chat-github-tools.js"; import { buildPaperclipRuntimeMcpServers } from "../services/heartbeat/run-preparation.js"; @@ -7774,12 +7775,13 @@ describeEmbeddedPostgres("chat channel control-plane integration", () => { }; const window = async (seconds: number) => { const before = { statements: queries.length, pulls: pulls(), - states: queries.filter(query => query.includes('from "chat_github_reviews"')).length }; + states: queries.filter(query => query.includes('from "chat_github_reviews" where')).length }; for (let second = 0; second < seconds; second++) await pass(); return { statements: queries.length - before.statements, pulls: pulls() - before.pulls, - states: queries.filter(query => query.includes('from "chat_github_reviews"')).length - before.states }; + states: queries.filter(query => query.includes('from "chat_github_reviews" where')).length - before.states }; }; - return { ...f, pass, window, pulls, restart: () => { task = register(); }, now: () => clock }; + const states = () => queries.filter(query => query.includes('from "chat_github_reviews" where')).length; + return { ...f, pass, window, pulls, states, restart: () => { task = register(); }, now: () => clock }; } it("backs off remote head polls while an unresolved GitHub review check cannot change", async () => { @@ -7815,6 +7817,48 @@ describeEmbeddedPostgres("chat channel control-plane integration", () => { expect((await db.select().from(chatGitHubReviews).where(eq(chatGitHubReviews.id, f.review.id)))[0].state).toBe("superseded"); }, 120_000); + it("rebuilds GitHub review checks for company-wide hints at most once per interval", async () => { + const f = await unresolvedReviewCheckPasses(); + await f.window(400); + const reads = async (hint: string) => { + const before = f.states(); + await f.pass(hint); + return f.states() - before; + }; + const company = JSON.stringify({ companyId: f.companyId }); + const repeated = []; + for (let index = 0; index < 10; index++) repeated.push(await reads(company)); + expect(repeated).toEqual([1, 0, 0, 0, 0, 0, 0, 0, 0, 0]); + for (let index = 0; index < GITHUB_REVIEW_CHECK_COMPANY_RECONSTRUCTION_MS / 1000 - 10; index++) await f.pass(); + expect(await reads(company)).toBe(1); + expect(await reads(JSON.stringify({ companyId: f.companyId, issueId: f.review.issueId }))).toBe(1); + expect(await reads(JSON.stringify({ companyId: f.companyId, issueId: f.review.issueId }))).toBe(1); + expect(await reads(JSON.stringify({ companyId: f.companyId, issueId: randomUUID() }))).toBe(0); + expect(await reads(JSON.stringify({ companyId: randomUUID() }))).toBe(0); + }, 120_000); + + it("publishes a run change for a GitHub review check at once from its issue milestone", async () => { + const f = await unresolvedReviewCheckPasses(); + await f.window(400); + await f.pass(JSON.stringify({ companyId: f.companyId })); + const hints: string[] = []; + const unsubscribe = subscribeDatabaseWork(db, DELIVERY_QUEUES.chatGitHubReviewChecks, (event, settled) => { + if (event === "settled") hints.push(...(settled ?? [])); + }); + onTestFinished(unsubscribe); + await db.transaction(async tx => { + await tx.update(chatGitHubReviews).set({ runId: f.run.id }).where(eq(chatGitHubReviews.id, f.review.id)); + await notifyChatRunMilestones(tx, { companyId: f.companyId, issueId: f.review.issueId, runId: f.run.id }); + }); + await expect.poll(() => hints.length, { timeout: 5000 }).toBeGreaterThan(0); + expect(hints.map(hint => JSON.parse(hint))).toContainEqual({ companyId: f.companyId, issueId: f.review.issueId }); + const writes = f.writes.length; + for (const hint of hints) await f.pass(hint); + expect(f.writes).toHaveLength(writes + 1); + expect(f.writes.at(-1)).toMatchObject({ status: "in_progress" }); + expect(await f.action()).toMatchObject({ status: "received", result: { state: "running", unchangedPolls: 0 } }); + }, 120_000); + async function reviewProjectionFixture(cancelFirst = true) { const f = await reviewBotFixture(); const deliveryId = randomUUID(); diff --git a/server/src/services/chat-channels/github/review-check-work.ts b/server/src/services/chat-channels/github/review-check-work.ts index d3e951d0aa3..49ebb3ab8f5 100644 --- a/server/src/services/chat-channels/github/review-check-work.ts +++ b/server/src/services/chat-channels/github/review-check-work.ts @@ -1,14 +1,21 @@ import { and, asc, eq, gt, or, sql } from "drizzle-orm"; -import { chatActions, chatEndpointLeases, type Db } from "@paperclipai/db"; +import { chatActions, chatEndpointLeases, chatGitHubReviews, type Db } from "@paperclipai/db"; import type { GitHubReviewEventContext } from "@paperclipai/shared"; import type { createDeliveryWorkCoordinator } from "../../delivery-work-coordinator.js"; import { DELIVERY_QUEUES, notifyDeliveryWork } from "../../delivery-work-notifications.js"; import { registerScopedDeadlineWork } from "../../scoped-deadline-work.js"; import { githubReviewCheckState } from "./review-check-state.js"; -type Scope = { companyId: string; endpointId?: string; repositoryId?: string; number?: number }; +type Scope = { companyId: string; endpointId?: string; repositoryId?: string; number?: number; issueId?: string }; type Entry = Scope & { id: string; dueAt: number }; +export const GITHUB_REVIEW_CHECK_COMPANY_RECONSTRUCTION_MS = 30_000; + +export function githubReviewCheckReconstructionIntervalMs(scope: Scope | undefined) { + return scope && !scope.endpointId && !scope.repositoryId && !scope.number && !scope.issueId + ? GITHUB_REVIEW_CHECK_COMPANY_RECONSTRUCTION_MS : 0; +} + export async function notifyGitHubReviewChecks(tx: object, scope: Scope) { await notifyDeliveryWork(tx, DELIVERY_QUEUES.chatGitHubReviewChecks, JSON.stringify(scope)); } @@ -19,8 +26,14 @@ export function registerGitHubReviewCheckWork( dispatch: (id: string, canContinue: () => boolean) => Promise, canRun: () => boolean, ) { + const reviewIssueId = sql`(select r.issue_id from ${chatGitHubReviews} r + where r.endpoint_id = ${chatActions.endpointId} + and r.repository_id = ${chatActions.payload}->'event'->>'repositoryId' + and r.pull_number = (${chatActions.payload}->'event'->>'pullNumber')::integer + and r.head_sha = ${chatActions.payload}->'event'->>'headSha' + order by r.created_at desc limit 1)`; async function page(scope?: Scope, cursor?: string, actionId?: string) { - return db.select({ action: chatActions, + return db.select({ action: chatActions, reviewIssueId, leaseExpiresAt: sql`(select max(l.expires_at) from ${chatEndpointLeases} l where l.company_id = ${chatActions.companyId} and l.endpoint_id = ${chatActions.endpointId} and l.lease_key = 'github-publication:' || (${chatActions.payload}->'event'->>'repositoryId') @@ -31,6 +44,7 @@ export function registerGitHubReviewCheckWork( scope?.endpointId ? eq(chatActions.endpointId, scope.endpointId) : undefined, scope?.repositoryId ? sql`${chatActions.payload}->'event'->>'repositoryId' = ${scope.repositoryId}` : undefined, scope?.number ? sql`${chatActions.payload}->'event'->>'pullNumber' = ${String(scope.number)}` : undefined, + scope?.issueId ? sql`${reviewIssueId} = ${scope.issueId}` : undefined, cursor ? gt(chatActions.id, cursor) : undefined, actionId ? eq(chatActions.id, actionId) : undefined, )).orderBy(asc(chatActions.id)).limit(100); } @@ -45,14 +59,16 @@ export function registerGitHubReviewCheckWork( } if (row.leaseExpiresAt) dueAt = Math.max(dueAt, Date.parse(row.leaseExpiresAt)); return { id: action.id, companyId: action.companyId, endpointId: action.endpointId, - repositoryId: event.repositoryId, number: event.pullNumber, dueAt }; + repositoryId: event.repositoryId, number: event.pullNumber, issueId: row.reviewIssueId ?? undefined, dueAt }; } return registerScopedDeadlineWork(coordinator, { queue: DELIVERY_QUEUES.chatGitHubReviewChecks, decodeScope: (hint: string): Scope | undefined => hint === "*" ? undefined : JSON.parse(hint), matches: (value: Entry, scope?: Scope) => !scope || (value.companyId === scope.companyId && (!scope.endpointId || value.endpointId === scope.endpointId) && - (!scope.repositoryId || value.repositoryId === scope.repositoryId) && (!scope.number || value.number === scope.number)), + (!scope.repositoryId || value.repositoryId === scope.repositoryId) && (!scope.number || value.number === scope.number) && + (!scope.issueId || value.issueId === scope.issueId)), + reconstructionIntervalMs: githubReviewCheckReconstructionIntervalMs, load: async (scope?: Scope, signal?: AbortSignal) => { const entries: Entry[] = []; let cursor: string | undefined; diff --git a/server/src/services/chat-run-milestone-notifications.ts b/server/src/services/chat-run-milestone-notifications.ts index 524936395b5..f391ba9d1ca 100644 --- a/server/src/services/chat-run-milestone-notifications.ts +++ b/server/src/services/chat-run-milestone-notifications.ts @@ -19,7 +19,7 @@ export async function notifyChatRunMilestones(tx: object, scope: ChatRunMileston await notifyNativeResponsePresentation(tx, { companyId: scope.companyId, issueId: scope.issueId, runId: scope.issueId ? undefined : scope.runId }); } - await notifyDeliveryWork(tx, DELIVERY_QUEUES.chatGitHubReviewChecks, JSON.stringify({ companyId: scope.companyId })); + await notifyDeliveryWork(tx, DELIVERY_QUEUES.chatGitHubReviewChecks, JSON.stringify({ companyId: scope.companyId, issueId: scope.issueId })); await notifyDeliveryWork(tx, DELIVERY_QUEUES.chatGitHubTerminalResponses, JSON.stringify({ companyId: scope.companyId, issueId: scope.issueId })); await notifyDeliveryWork(tx, DELIVERY_QUEUES.chatRunMilestones, JSON.stringify({ companyId: scope.companyId, issueId: scope.issueId })); // Lifecycle and coarse native progress share lineage and authority. Extend From 20ccf03676c2b70e3865e632780cc95f9aaaa051 Mon Sep 17 00:00:00 2001 From: Dotta <34892728+cryppadotta@users.noreply.github.com> Date: Sat, 14 Nov 2026 07:23:35 -0600 Subject: [PATCH 2/2] fix(workspaces): settle closed remote workspaces after one confirmed destroy (#21616) 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 > - The server cleans up closed execution workspaces in a background sweep (`terminal-workspaces`). For a remote workspace, cleanup destroys the sandbox lease after it confirms that the saved snapshot is committed. > - A closed remote workspace stays a sweep candidate while `metadata.remoteWorkspace.location` is an object. A confirmed destroy did not clear that location. > - The cleanup write goes through the lifecycle gateway, and the gateway published a `workspace:` hint for the same workspace. That hint started a new inspection, which cleaned the workspace again and published again. > - On a hosted instance with no runs, this loop kept three statements at about 15–24 per second each and a dedicated lock connection per inspection. Every pass also asked the sandbox provider to destroy a sandbox that was already destroyed. > - This pull request clears the location after a confirmed destroy, skips the provider call when the stored lease already has a destroy receipt, and stops the sweep from hinting itself for its own writes. > - The benefit is that an idle instance with closed remote workspaces settles: each workspace is cleaned once, and the provider is called at most once per lease. ## Linked Issues or Issue Description No issue exists. This describes the problem with the fields of `.github/ISSUE_TEMPLATE/bug_report.yml`. **What happened?** With no runs, the terminal workspace sweep never became idle. On the hosted instance these statements ran all the time: `workspaceHasActiveRun` (22.8/s), the task tree read (22.6/s) and the snapshot persistence read in `inspectRemoteWorkspacePersistence` (14.8/s). `SET application_name` for the `paperclip-workspace-finalization-lock` connections ran at 9.6/s. **Steps to reproduce** 1. Create closed (`archived`) remote workspaces that have a committed snapshot, a clean authority and a `location`. 2. Start the terminal workspace worker with no other activity. 3. Watch the sweeps. Each cleanup reports `archived`, the gateway publishes `workspace:`, and the next targeted inspection cleans the same workspace again. Locally, a fixture with 140 workspaces (40 of them in this state) ran 568 sweeps, 136,888 statements and 23,880 lease destroy calls in 30 idle minutes. The destroys always hit the same 40 workspaces. **Expected behavior** A closed remote workspace is cleaned once. After the destroy is confirmed it leaves the candidate set, and an idle instance issues no statements between real deadlines. ## What Changed - `server/src/services/remote-workspace-lifecycle.ts` (`cleanupRemoteWorkspaceAllocation`): - Read the stored lease first. When it already carries a destroy receipt for the same environment, lease and provider lease, do not call `destroyLease` again. - After a confirmed destroy, set `metadata.remoteWorkspace.location` to `null`. One fixed-text `jsonb_set` update does this, and only when the stored location is still the location that was destroyed. - `server/src/services/execution-workspaces.ts`: the gateway takes `sweepOwned`. The sweep's own cleanup, cleanup-failure and reopen-flag writes set it, and then the gateway does not publish a terminal workspace hint for that workspace. All other gateway callers still publish. - `server/src/__tests__/terminal-workspace-idle.test.ts`: two new tests on real PostgreSQL. ## Verification - `settles after one cleanup of each closed remote workspace and inspects nothing on its own writes`: - Fixture: 40 workspaces (open trees, a stranded `running` holder, cooldown, done, closed remote with a committed snapshot, closed remote without a snapshot). - Runs the real coordinator and worker on a fake clock for 2 minutes, then 30 idle minutes. - Asserts: each closed remote workspace is destroyed exactly once; no targeted inspection in the idle window; fewer than 5,000 statements in 30 minutes; every cleaned workspace has `location: null`. - Before the change it fails: 850 destroy calls instead of 10 in the warm-up alone. After the change it passes. - `clears the location of an already destroyed lease without calling the provider again`: the stored lease has a destroy receipt. Before the change, the sweep calls `destroyLease`. After the change the provider mock is never called, the location is cleared, and the next inspection finds no candidate. - `npx vitest run src/__tests__/terminal-workspace-idle.test.ts src/services/remote-workspace-lifecycle.test.ts src/__tests__/terminal-workspace-work.test.ts src/__tests__/execution-workspaces-service.test.ts src/__tests__/workspace-delivery-cleanup.integration.test.ts` (in `server/`). Local fixture, 30 idle minutes, 140 workspaces (40 closed remote with a committed snapshot): | | Before | After | | --- | --- | --- | | Sweeps | 568 (all targeted, continuous) | 20 (one deadline rotation every 5 minutes) | | Statements | 136,888 | 1,371 | | Lease destroy calls | 23,880 | 40 (once per workspace) | | `workspaceHasActiveRun` reads | 45,440 | 260 | | Snapshot persistence reads | 22,720 | 40 | The remaining rotation every 5 minutes comes from deadlines of workspaces that cannot make progress (a stranded active-run holder, cleanup backoff). A separate pull request handles it. ## Risks - **Who reads `remoteWorkspace.location`.** For a closed, clean workspace with a committed snapshot and a destroyed lease, none of these readers needs the location: - the live run binding checks (`remote-workspace-lifecycle`, `fresh-continuation-fallback`, `workspace-recovery-fallback`, `workspace-recovery-source`) need a current writer or a dirty run reference; - the abandonment and hold paths (`remote-workspace-epoch-abandonment`, `remote-workspace-abandoned-files-hold`, `remote-workspace-unchanged-proof`) need a dirty or held allocation; - `reusable-sandbox-resume-replacement` treats `null` as no conflicting sandbox; - `remote-workspace-files` reads the saved snapshot when the location is `null`. With a destroyed location it would fail as unavailable. - The lease-release path (`clearRemoteWorkspaceLocationForReleasedLease`) already writes the same `null` for the same state. This change makes the sweep stop depending on that side effect. - **Provider calls.** Before this change every loop pass re-claimed the lease and called the driver's `destroyRunLease` again, through `destroyClaimedSandboxLease` in `server/src/services/environment-runtime.ts`. After it, a lease with a stored destroy receipt causes no provider call. - **Existing backlog after deploy.** The startup rotation and the next deadline rotation visit each closed remote workspace that still has a location once, 50 per page with 1 second between pages. A lease that already has a destroy receipt costs no provider call, and its location is cleared. Only leases without a receipt get one destroy call. The workspaces then leave the candidate set. - **No self-hint.** The sweep already handles the workspace it writes, and an inspection's deadline bookkeeping is unchanged. Other writers still publish hints. The git restore point and task directory paths depend on task or explicit hints, not on these writes. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, with tool use and code execution in Claude Code. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [ ] 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 --- .../__tests__/terminal-workspace-idle.test.ts | 153 ++++++++++++++++++ server/src/services/execution-workspaces.ts | 14 +- .../services/remote-workspace-lifecycle.ts | 19 ++- 3 files changed, 176 insertions(+), 10 deletions(-) create mode 100644 server/src/__tests__/terminal-workspace-idle.test.ts diff --git a/server/src/__tests__/terminal-workspace-idle.test.ts b/server/src/__tests__/terminal-workspace-idle.test.ts new file mode 100644 index 00000000000..c1bef7fc449 --- /dev/null +++ b/server/src/__tests__/terminal-workspace-idle.test.ts @@ -0,0 +1,153 @@ +import { randomUUID } from "node:crypto"; +import { eq } from "drizzle-orm"; +import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest"; +import { + agents, companies, createDb, environmentLeases, environments, executionWorkspaceSnapshots, executionWorkspaces, + heartbeatRuns, issues, projects, type Db, +} from "@paperclipai/db"; +import { readRemoteWorkspaceAuthority } from "@paperclipai/shared"; +import { createDeliveryWorkCoordinator } from "../services/delivery-work-coordinator.js"; +import { executionWorkspaceService } from "../services/execution-workspaces.js"; +import { remoteTerminationReceipt } from "../services/remote-execution-termination.js"; +import { registerTerminalWorkspaceWork, TERMINAL_WORKSPACE_RETRY_MS } from "../services/terminal-workspace-work.js"; +import { meterDatabaseStatements } from "./helpers/database-statement-meter.js"; +import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js"; +import { resolveExternalTestDatabaseUrl } from "./helpers/external-test-database.js"; + +const external = resolveExternalTestDatabaseUrl("PAPERCLIP_TERMINAL_WORKSPACE_IDLE_TEST_DATABASE_URL"); +const support = external ? { supported: true } : await getEmbeddedPostgresTestSupport(); +const DAY = 86_400_000; +const realSetTimeout = globalThis.setTimeout; +const realSleep = (ms: number) => new Promise(resolve => realSetTimeout(resolve, ms)); + +(support.supported ? describe : describe.skip)("terminal workspace sweep at idle", () => { + let temporary: Awaited> | undefined; + let db: Db; + let meter: ReturnType; + beforeAll(async () => { + if (!external) temporary = await startEmbeddedPostgresTestDatabase("terminal-workspace-idle-"); + const url = external ?? temporary!.connectionString; + db = createDb(url); + db.$client.options.onnotice = () => {}; + meter = meterDatabaseStatements(db, url); + }, 120_000); + afterEach(() => { vi.useRealTimers(); }); + afterAll(async () => { meter?.uninstall(); await db?.$client.end({ timeout: 1 }); await temporary?.cleanup(); }); + + async function seedCompany() { + const companyId = randomUUID(), projectId = randomUUID(), agentId = randomUUID(), environmentId = randomUUID(); + await db.insert(companies).values({ id: companyId, name: "Idle", issuePrefix: `I${companyId.slice(0, 6)}` }); + await db.insert(projects).values({ id: projectId, companyId, name: "Project", status: "in_progress" }); + await db.insert(agents).values({ id: agentId, companyId, name: "Agent", role: "engineer", status: "idle", adapterType: "process" }); + await db.insert(environments).values({ id: environmentId, name: `Sandbox ${environmentId}`, driver: "sandbox" }); + return { companyId, projectId, agentId, environmentId }; + } + + async function seedWorkspace(f: Awaited>, kind: "open" | "held" | "cooldown" | "done" | "remote" | "remote_unsaved") { + const old = new Date(Date.now() - 10 * DAY); + const id = randomUUID(), issueId = randomUUID(), snapshotId = randomUUID(); + const location = { environmentId: f.environmentId, leaseId: randomUUID(), providerLeaseId: `sandbox-${randomUUID()}`, remoteCwd: "/workspace" }; + const remote = kind === "remote" || kind === "remote_unsaved"; + let runId: string | null = null; + if (kind === "held") { + runId = randomUUID(); + await db.insert(heartbeatRuns).values({ id: runId, companyId: f.companyId, agentId: f.agentId, status: "running" }); + } + await db.insert(executionWorkspaces).values({ id, companyId: f.companyId, projectId: f.projectId, + mode: remote ? "isolated_workspace" : "shared_workspace", strategyType: "project_primary", name: `${kind} ${id}`, + status: remote ? "archived" : "active", closedAt: remote ? old : null, providerType: remote ? "remote" : "local_fs", + cwd: `/missing/${id}`, createdAt: old, updatedAt: old, + metadata: remote ? { remoteWorkspace: { schema: "paperclip.remote-workspace.v1", epoch: 1, snapshotId, + source: { kind: "empty" }, location, writerRunId: null, dirty: false } } : null }); + await db.insert(issues).values({ id: issueId, companyId: f.companyId, projectId: f.projectId, title: kind, + status: kind === "open" ? "in_progress" : "done", executionWorkspaceId: id, executionRunId: runId, + completedAt: kind === "open" ? null : kind === "cooldown" ? new Date(Date.now() - 3_600_000) : old, updatedAt: old }); + await db.update(executionWorkspaces).set({ sourceIssueId: issueId, updatedAt: old }).where(eq(executionWorkspaces.id, id)); + if (kind === "remote") await db.insert(executionWorkspaceSnapshots).values({ id: snapshotId, companyId: f.companyId, + executionWorkspaceId: id, sourceOperationId: randomUUID(), generation: 1, epoch: 1, status: "committed" }); + return { id, location }; + } + + function destroyedLease(input: { companyId: string; leaseId: string; providerLeaseId: string; environmentId: string }) { + const lease = { id: input.leaseId, companyId: input.companyId, heartbeatRunId: randomUUID(), provider: "daytona", + providerLeaseId: input.providerLeaseId, environmentId: input.environmentId }; + return { ...lease, status: "released", cleanupStatus: "success", releasedAt: new Date(), + metadata: { remoteExecutionTermination: remoteTerminationReceipt(lease, { providerLeaseId: input.providerLeaseId, state: "destroyed" }) } }; + } + + it("settles after one cleanup of each closed remote workspace and inspects nothing on its own writes", async () => { + const f = await seedCompany(); + const remote: string[] = []; + for (const kind of ["open", "open", "held", "cooldown", "done", "remote", "remote", "remote_unsaved"] as const) { + for (let index = 0; index < 5; index++) { + const workspace = await seedWorkspace(f, kind); + if (kind === "remote") remote.push(workspace.id); + } + } + vi.useFakeTimers({ toFake: ["Date", "setTimeout", "clearTimeout"] }); + const destroyCalls: string[] = []; + const service = executionWorkspaceService(db, { + destroyRemoteWorkspaceLease: async input => { destroyCalls.push(input.workspaceId); return destroyedLease(input) as never; }, + workspaceReaperCooldownDays: 1, workspaceCompactionDelayMs: 3_600_000, + terminalWorkspaceInspectionBackoffMs: TERMINAL_WORKSPACE_RETRY_MS, + }); + const sweeps: Array<{ targeted: boolean; checked: number }> = []; + let inFlight = 0; + const coordinator = createDeliveryWorkCoordinator({ owner: db, canRun: () => true, onError: () => {} }); + const worker = registerTerminalWorkspaceWork(coordinator, { ...service, + sweepTerminalWorkspaces: async (limit, signal, options) => { + inFlight += 1; + try { + const result = await service.sweepTerminalWorkspaces(limit, signal, options); + sweeps.push({ targeted: Boolean(options?.targets), checked: result.checked }); + return result; + } finally { inFlight -= 1; } + } }, () => true); + const idle = async (seconds: number) => { + for (let second = 0; second < seconds; second += 10) { + await vi.advanceTimersByTimeAsync(10_000); + do { await realSleep(10); await vi.advanceTimersByTimeAsync(1); } while (inFlight > 0); + } + }; + try { + await idle(120); + expect(new Set(destroyCalls)).toEqual(new Set(remote)); + expect(destroyCalls.length).toBe(remote.length); + const before = { statements: meter.read().statements, sweeps: sweeps.length, destroys: destroyCalls.length }; + await idle(30 * 60); + vi.useRealTimers(); + const window = sweeps.slice(before.sweeps); + expect(destroyCalls.length).toBe(before.destroys); + expect(window.filter(sweep => sweep.targeted)).toEqual([]); + expect(meter.read().statements - before.statements).toBeLessThan(5_000); + for (const id of remote) { + const [row] = await db.select().from(executionWorkspaces).where(eq(executionWorkspaces.id, id)); + expect(readRemoteWorkspaceAuthority(row!.metadata)?.location).toBeNull(); + } + } finally { + vi.useRealTimers(); + await worker.stop(); await coordinator.stop(); + } + }, 600_000); + + it("clears the location of an already destroyed lease without calling the provider again", async () => { + const f = await seedCompany(); + const workspace = await seedWorkspace(f, "remote"); + const runId = randomUUID(); + await db.insert(heartbeatRuns).values({ id: runId, companyId: f.companyId, agentId: f.agentId, status: "succeeded" }); + const lease = { id: workspace.location.leaseId, companyId: f.companyId, heartbeatRunId: runId, provider: "daytona", + providerLeaseId: workspace.location.providerLeaseId }; + await db.insert(environmentLeases).values({ ...lease, environmentId: f.environmentId, executionWorkspaceId: workspace.id, + status: "released", cleanupStatus: "success", releasedAt: new Date(), + metadata: { remoteExecutionTermination: remoteTerminationReceipt(lease, { providerLeaseId: lease.providerLeaseId, state: "destroyed" }) } }); + const destroy = vi.fn(); + const service = executionWorkspaceService(db, { destroyRemoteWorkspaceLease: destroy as never, workspaceReaperCooldownDays: 1 }); + expect(await service.sweepTerminalWorkspaces(50, undefined, { targets: { workspaceIds: [workspace.id], issueIds: [] } })) + .toMatchObject({ checked: 1, archived: 1 }); + expect(destroy).not.toHaveBeenCalled(); + const [row] = await db.select().from(executionWorkspaces).where(eq(executionWorkspaces.id, workspace.id)); + expect(readRemoteWorkspaceAuthority(row!.metadata)?.location).toBeNull(); + expect(await service.sweepTerminalWorkspaces(50, undefined, { targets: { workspaceIds: [workspace.id], issueIds: [] } })) + .toMatchObject({ checked: 0 }); + }); +}); diff --git a/server/src/services/execution-workspaces.ts b/server/src/services/execution-workspaces.ts index bb411786a65..6ec8deb869e 100644 --- a/server/src/services/execution-workspaces.ts +++ b/server/src/services/execution-workspaces.ts @@ -2285,6 +2285,7 @@ export function executionWorkspaceService(db: Db, opts: ExecutionWorkspaceServic isWriteTarget: (fresh: FenceableWorkspaceRow) => boolean; onSkip: () => T; skipLog?: { event: string; message: string }; + sweepOwned?: boolean; write: (context: { tx: DbTransaction; fresh: FenceableWorkspaceRow }) => Promise; }): Promise { return withExecutionWorkspaceLifecycleLock(db, input.workspaceId, async (tx) => { @@ -2317,7 +2318,7 @@ export function executionWorkspaceService(db: Db, opts: ExecutionWorkspaceServic } return input.onSkip(); } - await measureRunPhase("workspace.notification", () => notifyTerminalWorkspaceWork(tx, `workspace:${input.workspaceId}`)); + if (!input.sweepOwned) await measureRunPhase("workspace.notification", () => notifyTerminalWorkspaceWork(tx, `workspace:${input.workspaceId}`)); return input.write({ tx, fresh }); }); } @@ -2337,11 +2338,12 @@ export function executionWorkspaceService(db: Db, opts: ExecutionWorkspaceServic // snapshot. async function clearReopenPendingConsumptionUnderLock( workspaceId: string, - options: { expectedGeneration: number; requireStaleSinceBefore?: Date }, + options: { expectedGeneration: number; requireStaleSinceBefore?: Date; sweepOwned?: boolean }, ): Promise { return fenceLifecycleGenerationWrite({ workspaceId, expectedGeneration: options.expectedGeneration, + sweepOwned: options.sweepOwned, isWriteTarget: (fresh) => { if (!metadataHasReopenPendingConsumption(fresh.metadata)) return false; if (options.requireStaleSinceBefore) { @@ -2622,6 +2624,7 @@ export function executionWorkspaceService(db: Db, opts: ExecutionWorkspaceServic return fenceLifecycleGenerationWrite<{ cleaned: boolean; warnings: string[]; skippedReopened?: boolean }>({ workspaceId: workspace.id, expectedGeneration: capturedGeneration, + sweepOwned: true, isWriteTarget: (fresh) => isClosedExecutionWorkspaceStatus(fresh.status), skipLog: { event: "execution_workspace.cleanup_skipped", @@ -2753,6 +2756,7 @@ export function executionWorkspaceService(db: Db, opts: ExecutionWorkspaceServic return fenceLifecycleGenerationWrite({ workspaceId: input.workspaceId, expectedGeneration: input.capturedGeneration, + sweepOwned: true, isWriteTarget: (fresh) => isClosedExecutionWorkspaceStatus(fresh.status), skipLog: { event: "execution_workspace.cleanup_failed_write_skipped", @@ -3798,7 +3802,7 @@ export function executionWorkspaceService(db: Db, opts: ExecutionWorkspaceServic const expirationTree = usingIssueIds ? issueTree.filter(issue => usingIssueIds.has(issue.id)) : issueTree; if (!sourceIssue || expirationTree.some(issue => !TERMINAL_ISSUE_STATUSES.has(issue.status))) { if (reopenPending) { - await clearReopenPendingConsumptionUnderLock(workspace.id, { + await clearReopenPendingConsumptionUnderLock(workspace.id, { sweepOwned: true, expectedGeneration: readExecutionWorkspaceLifecycleGeneration( workspace.metadata as Record | null, ), @@ -4055,7 +4059,7 @@ export function executionWorkspaceService(db: Db, opts: ExecutionWorkspaceServic // a later terminal cycle can archive the workspace again. Pass the // generation from this snapshot, so the clear skips a newer reopen that // raised the generation and installed its own fence after this read. - await clearReopenPendingConsumptionUnderLock(workspace.id, { + await clearReopenPendingConsumptionUnderLock(workspace.id, { sweepOwned: true, expectedGeneration: readExecutionWorkspaceLifecycleGeneration( workspace.metadata as Record | null, ), @@ -4127,7 +4131,7 @@ export function executionWorkspaceService(db: Db, opts: ExecutionWorkspaceServic // the rebuilt worktree. Pass this snapshot's generation and re-confirm // staleness against the fresh row under the lock, so a newer reopen that // raised the generation or refreshed the timestamp keeps its live fence. - const cleared = await clearReopenPendingConsumptionUnderLock(workspace.id, { + const cleared = await clearReopenPendingConsumptionUnderLock(workspace.id, { sweepOwned: true, expectedGeneration: readExecutionWorkspaceLifecycleGeneration( workspace.metadata as Record | null, ), diff --git a/server/src/services/remote-workspace-lifecycle.ts b/server/src/services/remote-workspace-lifecycle.ts index b4d24873fe0..6ae4bbe562b 100644 --- a/server/src/services/remote-workspace-lifecycle.ts +++ b/server/src/services/remote-workspace-lifecycle.ts @@ -87,12 +87,21 @@ export async function cleanupRemoteWorkspaceAllocation(input: { if (!persistence.ready || !persistence.authority) throw new Error(persistence.reason ?? "remote_workspace_saved_state_required"); const location = persistence.authority.location; if (location) { - const released = await input.destroyLease({ companyId: workspace.companyId, workspaceId: workspace.id, - leaseId: location.leaseId, environmentId: location.environmentId, providerLeaseId: location.providerLeaseId }); - if (!released || !hasRemoteTerminationReceipt(released) - || (released.metadata?.remoteExecutionTermination as { state?: unknown } | undefined)?.state !== "destroyed") { - throw new Error("remote_workspace_cleanup_unconfirmed"); + const destroyed = (lease: Parameters[0] | null | undefined) => Boolean(lease && hasRemoteTerminationReceipt(lease) + && (lease.metadata?.remoteExecutionTermination as { state?: unknown } | undefined)?.state === "destroyed"); + const [stored] = await input.db.select().from(environmentLeases).where(and( + eq(environmentLeases.companyId, workspace.companyId), eq(environmentLeases.id, location.leaseId), + eq(environmentLeases.environmentId, location.environmentId), eq(environmentLeases.providerLeaseId, location.providerLeaseId), + )); + if (!destroyed(stored)) { + const released = await input.destroyLease({ companyId: workspace.companyId, workspaceId: workspace.id, + leaseId: location.leaseId, environmentId: location.environmentId, providerLeaseId: location.providerLeaseId }); + if (!destroyed(released)) throw new Error("remote_workspace_cleanup_unconfirmed"); } + await input.db.update(executionWorkspaces) + .set({ metadata: sql`jsonb_set(${executionWorkspaces.metadata}, '{remoteWorkspace,location}', 'null'::jsonb)`, updatedAt: new Date() }) + .where(and(eq(executionWorkspaces.companyId, workspace.companyId), eq(executionWorkspaces.id, workspace.id), + sql`${executionWorkspaces.metadata}->'remoteWorkspace'->'location' = ${JSON.stringify(location)}::jsonb`)); } return { cleaned: true, warnings: [] as string[] }; }