From 890dcbdd66f177dcdd6fc724f7a87b590060589b Mon Sep 17 00:00:00 2001 From: Dotta <34892728+cryppadotta@users.noreply.github.com> Date: Sat, 14 Nov 2026 11:59:17 -0600 Subject: [PATCH 1/2] fix(native): wait for a live warm session owner during a deploy overlap (#21627) 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 > - A deploy swaps servers with an overlap: the outgoing server still serves while the incoming server starts new runs > - A warm agent session stays open in the server process that ran the last turn, after that run finished > - The incoming server took over a warm session while the outgoing server was alive and still held it, because the finished run's finalization lease ends at commit > - The takeover then failed, the task went blocked with a Retry, and in one case a retry loop ran 27 times on the same sandbox > - This pull request makes the incoming server wait while the server that holds the warm session is alive, makes the outgoing server close idle warm sessions as soon as it leaves new runs to the replacement, and stops the retry loop when the promised fresh sandbox does not come > - The benefit is that a follow-up during a deploy finishes without a person, and no Runner ever gets two controllers ## Linked Issues or Issue Description **What happened?** A zero-downtime release-swap test (Gate B, wave B1, master aaec67b891) found two failures with one cause. Task 1: run A finished on the outgoing server at 15:13:56. Its warm session stayed idle in that server. The incoming server claimed the follow-up at 15:13:57. `inspectRemoteWarmSessionRecovery` asked `evaluateNativeControllerTakeover`, which allowed the takeover with `remote_controller_lease_released_or_expired`. That lease covers the finished run, not the warm session. The outgoing server was alive until 15:18:18. `runner.prp.authenticate` failed after 62.3 s, the run failed with `remote_warm_session_unproven`, the failure was saved as ambiguous and owned by the board, and the task stayed blocked with a Retry. Task 2: the same shape on a later swap started 27 automatic runs in 13 minutes. Fourteen of them said "The next attempt starts on a fresh sandbox", but each one resumed the same sandbox and got the same refusal (`journal_unsettled`). **Expected behavior** A follow-up claimed during an overlap waits for the warm session owner to let go, then continues with no Retry banner and no second controller. If the outgoing server dies with the session open, the takeover continues after its ownership ends. A message that promises a fresh sandbox is true, or the retries stop after a bounded number with a true state. **Steps to reproduce** 1. Run the restart-chaos release-swap preset with a warm (opencode or codex) staging mix. 2. Let a run finish on the outgoing server after the traffic switch, with a follow-up comment already queued. 3. See the follow-up fail with `remote_warm_session_unproven` and the task go blocked. **Paperclip version or commit** master aaec67b891 and 45531b719e. **Deployment mode** Hosted deploy overlap (release swap) with Daytona sandboxes. ## What Changed - `remote-warm-session-recovery.ts`: before live adoption, the run proves that the server process that ran the finished turn is gone. It uses the shared owner proof (`proveOwnerGone`): a PID check in the same process space, or the `controller_instances` lease in another process space. While that server is alive, the run defers with the existing `native_controller_recovery_waiting_for_owner` wait and checks again after 5 s. The wait ends when the server exits or is fenced, or when the finished turn's warm window (idle timeout plus two minutes) ends. - `native-session-executor.ts` and `run-start-handover.ts`: a server that leaves new run starts to a replacement closes its idle warm sessions after 1 s, through the existing idle-timeout close. A session that becomes idle after that closes after 1 s too. This is the same close that shutdown already does, done earlier. - `predecessor-retirement-fallback.ts`: when a retry meets an unretired owner on the same sandbox that its source already asked to replace, it does not ask again. The run fails without another automatic retry, and the task shows a Retry. - `predecessor-retirement.ts`: the error no longer says "The next attempt starts on a fresh sandbox", because the replacement is only a request. - `doc/execution-semantics.md`: documents the owner wait, the hand-off at the swap, and the retry stop. ## Verification - `cd server && npx vitest run src/services/native-runtime/native-controller-recovery.integration.test.ts` - New: "waits while the outgoing server that ran the finished turn still holds its warm session, then takes it over once that server exits" (red on master: master returned a takeover while the owner was alive). - New: "waits for a live outgoing server in another process space until its controller lease ends" (red on master). - New: "stops waiting for a live outgoing server once the finished turn's warm window has closed" (bound check). - `cd server && npx vitest run src/__tests__/heartbeat-native-predispatch-retry.test.ts` - New: "stops retrying with a truthful failure when the next attempt met the same unretired sandbox instead of a fresh one" (red on master: master scheduled another retry on the same sandbox). - `cd server && npx vitest run src/services/native-runtime/native-session-executor.test.ts -t "replacement server starts"` - New: "closes a warm session promptly when a replacement server starts this server's runs (before/after the turn ends)" (both red on master). - Focused suites: native-session-executor, remote-warm-session-recovery, native-controller-recovery, native-recovered-owner-followup, native-runner-restart-recovery, heartbeat-native-predispatch-retry, environment-runtime-reusable-resume-replacement, task-interruption-presentation, sandbox-runner-port-holders. - `npx tsc --noEmit -p server/tsconfig.json` passes. `pnpm verify` exits 2 (a doc is a global CI input), so required CI supplies broad coverage. - Not run here: the restart-chaos release-swap wave. The maintainer runs it after merge. ## Risks - A follow-up can now wait up to the outgoing server's remaining overlap when that server cannot close its idle session. Before, it failed and needed a Retry. The wait is bounded by the owner's exit or the warm window. - The outgoing server closes idle warm sessions earlier during an overlap. The next turn then resumes from a suspended Runner instead of a live one, as it already does after a shutdown. - A retry chain that meets the same unretired sandbox twice now stops one attempt earlier than the shared cap. The task shows a Retry instead of looping. - Rows without a recorded process space keep the existing one-release local proof, so they behave as before outside the same host. - Previous-release state: this change adds no persisted field. Finalization rows saved by the previous release already record the controller boot, PID, process space and start time, so they carry forward. A follow-up already deferred by the previous release resumes through the same wait code. - Runtime cost: the owner proof adds one `ps` start-time read (same process space) or one `controller_instances` row read (other process space) per inspection. It runs once per attempt, at most every 5 s while a live owner holds the session. ## Model Used - Anthropic Claude Opus 5.5 (`claude-opus-5-5`), agentic coding with tool use (shell, file edits, test runs). ## 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 --- doc/execution-semantics.md | 19 +++ ...heartbeat-native-predispatch-retry.test.ts | 40 ++++++- .../predecessor-retirement-fallback.ts | 12 +- ...ve-controller-recovery.integration.test.ts | 111 +++++++++++++++++- .../native-session-executor.test.ts | 45 ++++++- .../native-runtime/native-session-executor.ts | 20 +++- .../native-runtime/predecessor-retirement.ts | 2 +- .../remote-warm-session-recovery.ts | 33 ++++++ server/src/services/run-start-handover.ts | 5 + 9 files changed, 277 insertions(+), 10 deletions(-) diff --git a/doc/execution-semantics.md b/doc/execution-semantics.md index c5801b8d8ec..ae94ee4e86f 100644 --- a/doc/execution-semantics.md +++ b/doc/execution-semantics.md @@ -3823,6 +3823,25 @@ in the sandbox, and only the killed server could have adopted it. A refusal because the new run itself is no longer admitted (stopped, reassigned, paused or no longer owned by this controller) stays an ordinary failure. +The finished run's finalization lease ends at commit, but its warm session +stays with the server process that ran it. Before live adoption, the new run +proves that process gone with the shared owner proof: a dead PID in the same +process space, or a stopped or fenced controller instance lease in another one. +While that server is still alive, the run defers with +`native_controller_recovery_waiting_for_owner` and checks again every few +seconds. It never opens a second controller on the same Runner. The wait ends +when that server exits, closes the session, or the finished run's warm window +(its idle timeout plus two minutes) ends. A server that leaves new run starts +to a replacement during a deploy overlap closes its idle warm sessions within +a second, and closes each session that becomes idle after that, so the +replacement usually waits only for that close. + +A fresh sandbox is only requested, not guaranteed: the next acquisition +replaces the marked sandbox only when the saved workspace and harness backup +allow it. When the next attempt meets an unretired owner again on the same +sandbox, that attempt stops the automatic retries. It fails without another +fallback, and the task shows a Retry. + A follow-up on a warm session starts with its predecessor's Runner process identity. That inherited identity alone does not count as a process of the follow-up when it proves that it left the workspace unchanged. The identity diff --git a/server/src/__tests__/heartbeat-native-predispatch-retry.test.ts b/server/src/__tests__/heartbeat-native-predispatch-retry.test.ts index a99d4ca48c1..0a6c76312d5 100644 --- a/server/src/__tests__/heartbeat-native-predispatch-retry.test.ts +++ b/server/src/__tests__/heartbeat-native-predispatch-retry.test.ts @@ -44,7 +44,7 @@ type AdmissionFailure = { remaining: number; turnNeverIssued: boolean; environme sessionRoots: string[] }; function nativeBackend(db: ReturnType, options: { failDescriptorOnce?: { failed: boolean }; - unretiredPredecessorOnce?: { failed: boolean; environmentId: string; taskBlocked?: boolean }; turnMessages?: Map; + unretiredPredecessorOnce?: { failed: boolean; environmentId: string; taskBlocked?: boolean; repeat?: string[] }; turnMessages?: Map; admissionFailure?: AdmissionFailure } = {}): NonNullable { return execution => { @@ -87,7 +87,8 @@ function nativeBackend(db: ReturnType, options: { failDescripto throw Object.assign(new Error("could not serialize access due to concurrent update"), { name: "PostgresError", code: "40001" }); } const unretired = options.unretiredPredecessorOnce; - if (unretired && !unretired.failed) { + if (unretired && (!unretired.failed || unretired.repeat)) { + unretired.repeat?.push(execution.binding.runId); // Staging: this run resumed the task's reused sandbox, where // the previous run's warm Runner could not be retired before any // provider work. The executor reports that after its coordinator claim. @@ -242,6 +243,41 @@ describe.skipIf(!support.supported)("heartbeat native pre-dispatch failure retry } }, 90_000); + it("stops retrying with a truthful failure when the next attempt met the same unretired sandbox instead of a fresh one", async () => { + const { companyId } = fixture.current; + const db = createDb(fixture.current.temporary.connectionString, { maxConnections: 4 }); + const [environment] = await db.insert(environments).values({ name: `reused-${randomUUID()}`, driver: "sandbox" }).returning(); + const attempts: string[] = []; + const heartbeat = heartbeatService(db, { nativeSessionBackendFactory: nativeBackend(db, + { unretiredPredecessorOnce: { failed: false, environmentId: environment!.id, repeat: attempts } }) }); + try { + const { issueId, runId } = await startTask(db, heartbeat); + const [first] = await db.select().from(heartbeatRuns).where(and(eq(heartbeatRuns.companyId, companyId), + eq(heartbeatRuns.retryOfRunId, runId))); + expect(first).toMatchObject({ scheduledRetryReason: "interrupted_preparation", scheduledRetryAttempt: 1 }); + expect(await heartbeat.promoteDueScheduledRetries()).toMatchObject({ promoted: 1 }); + await heartbeat.resumeQueuedRuns(); + await waitForCondition("the second attempt to end", async () => + (await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, first!.id)))[0]?.status === "failed"); + await waitForCondition("the second attempt to settle", () => heartbeat.getTaskDrainStatus().quiescent); + expect(attempts).toEqual([runId, first!.id]); + const [second] = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, first!.id)); + expect(second?.resultJson ?? {}).not.toHaveProperty("predecessorRetirementFallback"); + expect(second?.error ?? "").not.toMatch(/fresh sandbox/i); + expect(await db.select().from(heartbeatRuns).where(and(eq(heartbeatRuns.companyId, companyId), + eq(heartbeatRuns.retryOfRunId, first!.id)))).toHaveLength(0); + expect(await heartbeat.promoteDueScheduledRetries()).toMatchObject({ promoted: 0 }); + const [issue] = await db.select().from(issues).where(eq(issues.id, issueId)); + expect(await taskInterruptionPresentation(db, issue!)).toMatchObject({ canRetry: true }); + } finally { + await heartbeat.drainActiveRunExecutions(); + await closeIdleWarmNativeSessionsForRestart(); + await db.update(heartbeatRuns).set({ status: "cancelled", finishedAt: new Date() }).where(and( + eq(heartbeatRuns.companyId, companyId), inArray(heartbeatRuns.status, ["queued", "scheduled_retry"]))); + await db.$client.end(); + } + }, 90_000); + it.each([ { title: "continues a follow-up on a blocked task whose predecessor could not be retired", blockedAfter: false }, { title: "does not continue a task that was blocked only after its run failed", blockedAfter: true }, diff --git a/server/src/services/heartbeat/predecessor-retirement-fallback.ts b/server/src/services/heartbeat/predecessor-retirement-fallback.ts index 648ce5aee87..354406f9da3 100644 --- a/server/src/services/heartbeat/predecessor-retirement-fallback.ts +++ b/server/src/services/heartbeat/predecessor-retirement-fallback.ts @@ -4,7 +4,7 @@ import { readRemoteWorkspaceAuthority, remoteWorkspaceRunReferenceSchema } from import { persistActivity } from "../activity-log.js"; import { hasNativeExecutionStartEvidence } from "../native-runtime/native-restart-recovery.js"; import { - findNativePredecessorRetirementUnproven, REPLACE_BEFORE_REUSE_KEY, REPLACE_BEFORE_REUSE_SCHEMA, + findNativePredecessorRetirementUnproven, readPredecessorRetirementFallback, REPLACE_BEFORE_REUSE_KEY, REPLACE_BEFORE_REUSE_SCHEMA, type PredecessorRetirementFallback, type ReplaceBeforeReuseMarker, } from "../native-runtime/predecessor-retirement.js"; import { FAILED_BEFORE_PROVIDER_TURN_KEY, FAILED_BEFORE_PROVIDER_TURN_SCHEMA, findNativeFailedBeforeProviderTurn, @@ -47,6 +47,7 @@ export async function preparePredecessorRetirementFallback(db: Db, run: Run, err : eq(environmentLeases.leasePolicy, "reuse_by_environment"), isNotNull(environmentLeases.providerLeaseId), )).for("update")).filter(lease => !failure.providerLeaseId || lease.providerLeaseId === failure.providerLeaseId); if (leases.length === 0 && !beforeProviderTurn) return null; + if (await replacementAlreadyMissed(tx as unknown as Db, current, leases.map(lease => lease.providerLeaseId!))) return null; if (beforeProviderTurn) await tx.update(heartbeatRuns).set({ runnerProfileJson: sql`coalesce(${heartbeatRuns.runnerProfileJson}, '{}'::jsonb) || ${JSON.stringify({ [FAILED_BEFORE_PROVIDER_TURN_KEY]: { schema: FAILED_BEFORE_PROVIDER_TURN_SCHEMA, cause: beforeProviderTurn.code, proof: beforeProviderTurn.proof, technicalCause: beforeProviderTurn.technicalCause } })}::jsonb` }) @@ -68,6 +69,15 @@ export async function preparePredecessorRetirementFallback(db: Db, run: Run, err }); } +async function replacementAlreadyMissed(db: Db, run: Pick, + providerLeaseIds: string[]): Promise { + if (!run.retryOfRunId || providerLeaseIds.length === 0) return false; + const [source] = await db.select({ resultJson: heartbeatRuns.resultJson }).from(heartbeatRuns) + .where(and(eq(heartbeatRuns.companyId, run.companyId), eq(heartbeatRuns.id, run.retryOfRunId))); + const replaced = readPredecessorRetirementFallback(source?.resultJson)?.replacedProviderLeaseIds ?? []; + return providerLeaseIds.some(id => replaced.includes(id)); +} + /** * After the failed run ended: hand its prepared remote workspace on at the * saved version, but only through the shared unchanged-files proof (no runner diff --git a/server/src/services/native-runtime/native-controller-recovery.integration.test.ts b/server/src/services/native-runtime/native-controller-recovery.integration.test.ts index e91afae1b21..739a56e3895 100644 --- a/server/src/services/native-runtime/native-controller-recovery.integration.test.ts +++ b/server/src/services/native-runtime/native-controller-recovery.integration.test.ts @@ -10,7 +10,7 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { eq } from "drizzle-orm"; import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; -import { agentTaskSessions, agents, companies, createDb, environmentLeases, environments, heartbeatRunEvents, heartbeatRuns, issues, nativeRunFinalizations, nativeRunResults, nativeRunnerCheckpoints } from "@paperclipai/db"; +import { agentTaskSessions, agents, companies, controllerInstances, createDb, environmentLeases, environments, heartbeatRunEvents, heartbeatRuns, issues, nativeRunFinalizations, nativeRunResults, nativeRunnerCheckpoints } from "@paperclipai/db"; import { DurablePrpControlPlane, parseNativeExecutionInput, type PersistedNativeSession } from "../../vendor/paperclip-runner/index.js"; import { startRecoveryPostgres, recoveryPostgresSupport } from "../../__tests__/helpers/recovery-postgres.js"; import { createIsolatedTestHome } from "../../__tests__/helpers/isolated-test-home.js"; @@ -24,6 +24,9 @@ import { AgentDirectoryPreparationDeferral, deferRunForBusyAgentDirectory, prepa import { AgentDirectoryOperationBusyError } from "../agent-directory-working-copies.js"; import { readNativeControllerRecoveryInventory } from "./native-controller-recovery-inventory.js"; import { inspectRemoteWarmSessionRecovery } from "./remote-warm-session-recovery.js"; +import { currentProcessSpaceHash } from "../controller-process-identity.js"; +import { registerControllerInstanceDb, resetControllerInstanceLeaseForTests } from "../controller-instance-lease.js"; +import { readProcessStartedAt } from "../hot-restart.js"; describe.skipIf(!recoveryPostgresSupport.supported)("native controller durable discovery and import", () => { let database: Awaited>; @@ -535,6 +538,112 @@ describe.skipIf(!recoveryPostgresSupport.supported)("native controller durable d expect(await claimNativeSessionResumptions({ db, runnerInstanceId: "duplicate", runIds: [runId], now: deadline })).toEqual([]); }); + async function seedWarmFollowUpOnOldServer() { + const fixture = await seed(), runId = randomUUID(), environmentId = randomUUID(); + const previous = parseNativeExecutionInput({ + schema: "paperclip.native-execution-input.v1", provider: { kind: "codex", model: null }, + binding: { companyId, agentId, issueId: fixture.scope.issueId, runId: fixture.scope.runId, executionWorkspaceId: randomUUID() }, + task: { identifier: "NCD-9", title: "Answer the follow-up", description: null, prompt: "Continue.", workMode: "standard" }, + workspace: { cwd: "/workspace", repoUrl: null, repoRef: null, branchName: null }, + session: { normalizedSessionId: fixture.scope.normalizedSessionId, driverKind: "codex_app_server", + protocolVersion: 1, lifecyclePolicy: { mode: "warm", idleTimeoutMs: 600_000 } }, + completionContract: { id: randomUUID(), sha256: "fixture", schemaVersion: "paperclip.completion-contract.v1", + contract: { revision: "1", objective: "Continue.", criteria: [{ id: "objective", requirement: "Save the result." }] } }, + credentialBindings: [], interactionResponses: [], + }); + const current = { ...previous, binding: { ...previous.binding, runId } }; + const checkpoint: PersistedNativeSession = { ...fixture.snapshot, driverKind: "codex_app_server", + providerSessionId: "warm-provider-session", activeTurnId: null, pendingRuntimeRequests: [] }; + await db.update(heartbeatRuns).set({ finishedAt: new Date(), runnerProfileJson: { ...fixture.profile, nativeExecutionInput: previous, + sessionCheckpoint: checkpoint, nativeControllerRecovery: createNativeControllerRecovery(checkpoint, compatibility, + { runId: fixture.scope.runId, runnerInstanceId: fixture.native.runnerInstanceId, normalizedSessionId: fixture.scope.normalizedSessionId }) } }) + .where(eq(heartbeatRuns.id, fixture.scope.runId)); + await db.insert(heartbeatRuns).values({ id: runId, companyId, agentId, nativeIssueId: fixture.scope.issueId, + nativeSessionId: fixture.scope.normalizedSessionId, status: "running", runtimeMode: "native", + runnerProfileJson: { nativeExecutionInput: current, sessionCheckpoint: { ...checkpoint, identity: { ...checkpoint.identity, runId } } } }); + await db.insert(nativeRunFinalizations).values({ companyId, issueId: fixture.scope.issueId, runId, phase: "observed" }); + await db.update(issues).set({ executionRunId: runId }).where(eq(issues.id, fixture.scope.issueId)); + await db.insert(environments).values({ id: environmentId, name: `Warm ${environmentId}`, driver: "sandbox" }); + await db.insert(environmentLeases).values({ companyId, environmentId, issueId: fixture.scope.issueId, + heartbeatRunId: fixture.scope.runId, provider: "daytona", providerLeaseId: "warm-sandbox", status: "released", + metadata: { remoteCwd: "/workspace" } }); + const identity = { runId: fixture.scope.runId, normalizedSessionId: fixture.scope.normalizedSessionId, + runnerInstanceId: fixture.native.runnerInstanceId, environmentLeaseId: fixture.native.environmentLeaseId, + turnId: fixture.native.turnId, itemId: fixture.native.itemId }; + const inspect = () => inspectRemoteWarmSessionRecovery({ db, execution: current, identity, + target: { kind: "remote", transport: "sandbox", environmentId, leaseId: randomUUID(), providerKey: "daytona", + remoteCwd: "/workspace", sandboxLeaseAcquisition: { outcome: "resumed", providerLeaseId: "warm-sandbox" } } as never, + readControlState: () => ({ identity, commands: [{ type: "turn.start", status: "completed" }], + committedEvents: [{ eventType: "run.terminal", envelope: identity }] }), + matchesExecution: saved => saved.binding.runId === fixture.scope.runId, + loadCheckpoint: () => checkpoint, canReuseProcess: () => true, + }); + const recordOwner = (owner: Partial) => db.update(nativeRunFinalizations) + .set(owner).where(eq(nativeRunFinalizations.runId, fixture.scope.runId)); + return { fixture, runId, current, identity, inspect, recordOwner }; + } + + it("waits while the outgoing server that ran the finished turn still holds its warm session, then takes it over once that server exits", async () => { + const oldServer = spawn(process.execPath, ["-e", "setInterval(() => {}, 60_000)"], { stdio: "ignore" }); + try { + await new Promise(resolve => oldServer.once("spawn", resolve)); + const startedAt = await readProcessStartedAt(oldServer.pid!); + expect(startedAt).toBeTruthy(); + const { inspect, recordOwner, runId, current } = await seedWarmFollowUpOnOldServer(); + await recordOwner({ controllerBootId: randomUUID(), controllerPid: oldServer.pid!, controllerHostHash: currentProcessSpaceHash(), + controllerProcessStartedAt: new Date(startedAt!), leaseOwner: null, leaseExpiresAt: null }); + const before = Date.now(); + const waiting = await inspect().catch(error => error); + expect(waiting).toBeInstanceOf(NativeControllerRecoveryWaitingError); + expect(waiting.retryAt.getTime()).toBeGreaterThan(before); + expect(waiting.retryAt.getTime()).toBeLessThanOrEqual(Date.now() + 10_000); + await deferNativeControllerRecovery(db, { ...current.binding, retryAt: waiting.retryAt }); + expect((await db.select().from(nativeRunFinalizations).where(eq(nativeRunFinalizations.runId, runId)))[0]) + .toMatchObject({ phase: "retryable_failure", attempt: 0, failureCode: NATIVE_RETAINED_OWNER_WAIT, resultId: null }); + const [task] = await db.select().from(issues).where(eq(issues.executionRunId, runId)); + expect(task?.status).toBe("in_progress"); + + const exited = new Promise(resolve => oldServer.once("exit", resolve)); + oldServer.kill("SIGKILL"); + await exited; + const recovered = await inspect(); + expect(recovered?.identity.runId).not.toBe(runId); + expect(recovered?.reuseProcess).toBe(true); + } finally { + oldServer.kill("SIGKILL"); + } + }); + + it("waits for a live outgoing server in another process space until its controller lease ends", async () => { + const { inspect, recordOwner } = await seedWarmFollowUpOnOldServer(); + const oldBoot = randomUUID(); + await recordOwner({ controllerBootId: oldBoot, controllerPid: 7, controllerHostHash: "outgoing-container", + controllerProcessStartedAt: new Date(Date.now() - 600_000), leaseOwner: null, leaseExpiresAt: null }); + await db.insert(controllerInstances).values({ bootId: oldBoot, processSpace: "outgoing-container", pid: 7, + leaseExpiresAt: new Date(Date.now() + 90_000) }); + registerControllerInstanceDb(db); + try { + await expect(inspect()).rejects.toBeInstanceOf(NativeControllerRecoveryWaitingError); + await db.update(controllerInstances).set({ stoppedAt: new Date() }).where(eq(controllerInstances.bootId, oldBoot)); + await expect(inspect()).resolves.not.toBeNull(); + } finally { resetControllerInstanceLeaseForTests(); } + }); + + it("stops waiting for a live outgoing server once the finished turn's warm window has closed", async () => { + const oldServer = spawn(process.execPath, ["-e", "setInterval(() => {}, 60_000)"], { stdio: "ignore" }); + try { + await new Promise(resolve => oldServer.once("spawn", resolve)); + const startedAt = await readProcessStartedAt(oldServer.pid!); + const { inspect, recordOwner, fixture } = await seedWarmFollowUpOnOldServer(); + await recordOwner({ controllerBootId: randomUUID(), controllerPid: oldServer.pid!, controllerHostHash: currentProcessSpaceHash(), + controllerProcessStartedAt: new Date(startedAt!), leaseOwner: null, leaseExpiresAt: null }); + await db.update(heartbeatRuns).set({ finishedAt: new Date(Date.now() - 3_600_000) }).where(eq(heartbeatRuns.id, fixture.scope.runId)); + await expect(inspect()).resolves.not.toBeNull(); + } finally { + oldServer.kill("SIGKILL"); + } + }); + it("sends a re-wake after a crash restart to a fresh sandbox when the parked predecessor's warm Runner cannot be adopted", async () => { const fixture = await seed(), runId = randomUUID(), environmentId = randomUUID(), leaseId = randomUUID(); const previous = parseNativeExecutionInput({ diff --git a/server/src/services/native-runtime/native-session-executor.test.ts b/server/src/services/native-runtime/native-session-executor.test.ts index 81aba3bf4c1..cb08e585392 100644 --- a/server/src/services/native-runtime/native-session-executor.test.ts +++ b/server/src/services/native-runtime/native-session-executor.test.ts @@ -22,6 +22,7 @@ import { configuredEnvironment, configuredEnvironmentProjection } from "../../ve import { afterEach, beforeEach, describe, expect, it, onTestFinished, vi } from "vitest"; import { enableReattachPhase, isReattachPhaseActive, joinReattachPhase, onReattachPhaseEnd, resetReattachPhaseForTests, type ReattachPhaseSummary } from "../heartbeat/run-admission.js"; import { createSystemRecovery, failSystemRecovery, reserveSystemRecoveryAttempt, SystemRecoveryFailure } from "../system-recovery-state.js"; +import { registerRunStartHandover } from "../run-start-handover.js"; import { mkdirSync, readFileSync, writeFileSync } from "node:fs"; import { once } from "node:events"; import { PgDialect } from "drizzle-orm/pg-core"; @@ -11465,6 +11466,41 @@ describe("native warm session supervision", () => { } }); + it.each(["before", "after"] as const)("closes a warm session promptly when a replacement server starts this server's runs (%s the turn ends)", async when => { + const identity = `warm-handoff-${when}`; + const warmExecution = { ...execution, binding: { ...execution.binding, runId: identity, executionWorkspaceId: `${identity}-workspace` }, + session: { ...execution.session, normalizedSessionId: identity, lifecyclePolicy: { mode: "warm" as const, idleTimeoutMs: 600_000 } }, + } as NativeExecutionInputV1; + const session = { close: vi.fn(async () => undefined) }; + state.execute.mockReset().mockImplementation(async (options) => { + await options.onSession?.(session); + return { result: { summary: "completed" }, terminal: { runTerminalState: "succeeded" }, + turnId: identity, normalizedSessionId: identity, providerSessionId: identity, + driverKind: "test", driverVersion: "1", nativeEventCount: 1, highestContiguousSourceSeq: 1, usage: null }; + }); + const listeners = new Set<() => void>(); + let leaving = when === "before"; + registerRunStartHandover({ isLeavingRunStarts: () => leaving, subscribe: (listener: () => void) => { + listeners.add(listener); + return () => { listeners.delete(listener); }; + } } as unknown as Parameters[0]); + try { + await executePaperclipNativeSession({ db: leaseDb(warmExecution), execution: warmExecution, runnerInstanceId: identity, + runnerExecutionTarget: { kind: "remote", transport: "sandbox", environmentId: identity, remoteCwd: `/tmp/${identity}` } }); + if (when === "after") { + await new Promise(resolve => setTimeout(resolve, 1_500)); + expect(session.close).not.toHaveBeenCalled(); + leaving = true; + for (const listener of listeners) listener(); + } + await vi.waitFor(() => expect(session.close).toHaveBeenCalledTimes(1), { timeout: 3_000 }); + await vi.waitFor(() => expect(readNativeSessionIdleWork()).toBe("none")); + } finally { + registerRunStartHandover(null); + await closeWarmNativeSessionsForEnvironment({ environmentId: identity, reason: "test cleanup" }); + } + }); + it("a follow-up that arrives while the idle timeout is closing the warm session waits for that close instead of failing busy", async () => { const identity = "warm-idle-close-follow-up"; const warmExecution = { ...execution, binding: { ...execution.binding, runId: identity, executionWorkspaceId: `${identity}-workspace` }, @@ -18955,7 +18991,7 @@ describe("runnerd provider runtime wiring", () => { it.each((["codex", "claude"] as const).flatMap(provider => [ ...[[false, false, false, false], [true, false, false, false], [false, true, false, false], [false, false, true, false], ...(provider === "codex" ? [[false, false, false, true]] : [])] .map(([unknown, shutdown, journalFailure, deadline]) => [provider, unknown, shutdown, journalFailure, deadline, "idle timeout"] as const), - ...(["controller restart", "detach failure", "environment deletion"] as const) + ...(["controller restart", "detach failure", "environment deletion", "replacement hand-off"] as const) .map(retirement => [provider, false, false, false, false, retirement] as const), ]))( "releases settled %s only under its current owner and retires its cache (unknown=%s, shutdown=%s, journalFailure=%s, deadline=%s, retirement=%s)", async (provider, unknown, shutdown, journalFailure, deadline, retirement) => { @@ -18965,6 +19001,8 @@ describe("runnerd provider runtime wiring", () => { process.env.PAPERCLIP_HOME = isolatedStateDirectory; const current = scopedNativeExecution(`released-${provider}-${unknown}`); current.session.lifecyclePolicy = { mode: "warm", idleTimeoutMs: retirement === "idle timeout" ? unknown ? 750 : 250 : 60_000 }; + if (retirement === "replacement hand-off") registerRunStartHandover({ isLeavingRunStarts: () => true, + subscribe: () => () => undefined } as unknown as Parameters[0]); if (provider === "claude") current.provider = { kind: "acpx", agent: "claude", model: "fixture", permissionMode: "deny-all" } as never; const source = { runnerInstanceId: "released-runner", environmentLeaseId: "released-lease", runId: current.binding.runId, normalizedSessionId: current.session.normalizedSessionId!, turnId: "settled-turn", itemId: "settled-item" }; @@ -19119,10 +19157,10 @@ describe("runnerd provider runtime wiring", () => { } detachControllerForRestart.mockResolvedValue(undefined); } - if (retirement !== "idle timeout") { + if (retirement !== "idle timeout" && retirement !== "replacement hand-off") { expect(await closeIdleWarmNativeSessionsForRestart()).toEqual({ closed: 1, busy: 0, failed: 0, failures: [] }); expect(await closeIdleWarmNativeSessionsForRestart()).toEqual({ closed: 0, busy: 0, failed: 0, failures: [] }); - } else await vi.waitFor(() => expect(detachControllerForRestart).toHaveBeenCalledOnce()); + } else await vi.waitFor(() => expect(detachControllerForRestart).toHaveBeenCalledOnce(), { timeout: 3_000 }); expect(readNativeSessionIdleWork()).toBe("none"); expect(close).not.toHaveBeenCalled(); expect(task.mock.calls.map(([operation]) => (operation as { kind: string }).kind)).toEqual(["describe"]); @@ -19157,6 +19195,7 @@ describe("runnerd provider runtime wiring", () => { expect(JSON.parse(await readFile(path, "utf8")).releasedRunner).toEqual(replacement.releasedRunner); } finally { detachControllerForRestart.mockResolvedValue(undefined); + registerRunStartHandover(null); allowRelease(); allowPhysicalStop(); await execute.catch(() => undefined); await closeIdleWarmNativeSessionsForRestart(); owners.mockRestore(); if (previousHome === undefined) delete process.env.PAPERCLIP_HOME; else process.env.PAPERCLIP_HOME = previousHome; } diff --git a/server/src/services/native-runtime/native-session-executor.ts b/server/src/services/native-runtime/native-session-executor.ts index 3f33a94c005..914ceded34a 100644 --- a/server/src/services/native-runtime/native-session-executor.ts +++ b/server/src/services/native-runtime/native-session-executor.ts @@ -41,6 +41,7 @@ import { notifyExternalAgentSettlement } from "../../modules/external-agents/ind import { notifyNativeResultCommit } from "../native-result-commit-notifications.js"; import { releaseRetainedNativeSessionCleanup } from "../retained-native-session-cleanup-notifications.js"; import { registerIdleLocalWorkProbe, type IdleLocalWork } from "../idle-local-work.js"; +import { isLeavingRunStartsToReplacement, subscribeRunStartHandoverChanges } from "../run-start-handover.js"; import type { NativeCleanupOutcome } from "./retained-native-session-cleanup.js"; import { notifyNativeStatusReassessment } from "../native-status-reassessment-notifications.js"; import { notifyNativeFinalizationWork } from "../native-finalization-notifications.js"; @@ -930,6 +931,21 @@ export function readNativeSessionIdleWork(): IdleLocalWork { } registerIdleLocalWorkProbe(readNativeSessionIdleWork); +export const WARM_HANDOFF_CLOSE_DELAY_MS = 1_000; + +function warmIdleCloseDelay(idleTimeoutMs: number): number { + return isLeavingRunStartsToReplacement() ? Math.min(idleTimeoutMs, WARM_HANDOFF_CLOSE_DELAY_MS) : idleTimeoutMs; +} + +subscribeRunStartHandoverChanges(() => { + if (!isLeavingRunStartsToReplacement()) return; + for (const [sessionId, entry] of warmNativeSessions) { + if (entry.busy || entry.idleTimer === null) continue; + clearTimeout(entry.idleTimer); + armWarmNativeSessionIdleTimer(sessionId, entry, WARM_HANDOFF_CLOSE_DELAY_MS); + } +}); + /** * How long a new run's admission waits for the warm session to stop being busy. * A follow-up comment can wake the next run within milliseconds of the previous @@ -8775,7 +8791,7 @@ async function releaseWarmNativeSession( } finally { entry.busy = false; } return; } - armWarmNativeSessionIdleTimer(sessionId, entry, idleTimeoutMs); + armWarmNativeSessionIdleTimer(sessionId, entry, warmIdleCloseDelay(idleTimeoutMs)); } function armWarmNativeSessionIdleTimer( @@ -13774,7 +13790,7 @@ async function executePaperclipNativeSessionAttempt( if (current()) { delete entry.finishRelease; entry.busy = false; - armWarmNativeSessionIdleTimer(scope, entry, lifecyclePolicy.idleTimeoutMs); + armWarmNativeSessionIdleTimer(scope, entry, warmIdleCloseDelay(lifecyclePolicy.idleTimeoutMs)); } } }; diff --git a/server/src/services/native-runtime/predecessor-retirement.ts b/server/src/services/native-runtime/predecessor-retirement.ts index 8882a12f3e1..b3b397be9ec 100644 --- a/server/src/services/native-runtime/predecessor-retirement.ts +++ b/server/src/services/native-runtime/predecessor-retirement.ts @@ -21,7 +21,7 @@ export class NativePredecessorRetirementUnprovenError extends Error { cause: unknown, ) { super(`The previous run's session on this sandbox could not be retired (${ - cause instanceof Error ? cause.message : String(cause)}). The next attempt starts on a fresh sandbox.`, { cause }); + cause instanceof Error ? cause.message : String(cause)}).`, { cause }); this.name = "NativePredecessorRetirementUnprovenError"; } } diff --git a/server/src/services/native-runtime/remote-warm-session-recovery.ts b/server/src/services/native-runtime/remote-warm-session-recovery.ts index 65c4bd151e9..ebb21f0ac33 100644 --- a/server/src/services/native-runtime/remote-warm-session-recovery.ts +++ b/server/src/services/native-runtime/remote-warm-session-recovery.ts @@ -6,6 +6,36 @@ import { currentNativeControllerIdentity, evaluateNativeControllerTakeover, reco import { NativeControllerRecoveryWaitingError, readNativeControllerRecovery } from "./native-controller-recovery.js"; import { NativePredecessorRetirementUnprovenError } from "./predecessor-retirement.js"; import { readyPortableTaskSource } from "../plugin-task-ownership.js"; +import { PAPERCLIP_RUNNER_IDLE_TIMEOUT_MAX_MS } from "@paperclipai/adapter-utils"; +import { currentControllerBootId } from "../controller-process-identity.js"; +import { currentProcessProbe, proveOwnerGone } from "../owner-proof.js"; + +export const WARM_OWNER_RECHECK_MS = 5_000; +const WARM_OWNER_CLOSE_MARGIN_MS = 2 * 60_000; + +export async function liveWarmSessionOwnerRetryAt(input: { + owner: Pick; + finishedAt: Date | null; + idleTimeoutMs: number | null; + now?: Date; +}): Promise { + const { owner } = input; + const now = input.now ?? new Date(); + if (!owner.controllerBootId || owner.controllerBootId === currentControllerBootId()) return null; + const releasedAt = (input.finishedAt ?? owner.updatedAt).getTime(); + const closesBy = releasedAt + (input.idleTimeoutMs ?? PAPERCLIP_RUNNER_IDLE_TIMEOUT_MAX_MS) + WARM_OWNER_CLOSE_MARGIN_MS; + if (closesBy <= now.getTime()) return null; + const proof = await proveOwnerGone({ + kind: "local", scope: "native_warm_session", generation: null, host: null, + processSpace: owner.controllerHostHash, processSpaceUnrecorded: owner.controllerHostHash == null, + bootId: owner.controllerBootId, pid: owner.controllerPid, + processStartedAt: owner.controllerProcessStartedAt?.toISOString() ?? null, + touchedAt: owner.updatedAt.toISOString(), + }, { ...currentProcessProbe(), readOnly: true }); + if (proof.verdict !== "alive") return null; + return new Date(Math.min(now.getTime() + WARM_OWNER_RECHECK_MS, closesBy)); +} export type RemoteWarmSessionRecovery = { identity: Record; @@ -176,6 +206,9 @@ export async function inspectRemoteWarmSessionRecovery(input: { throw new NativeControllerRecoveryWaitingError(prior.owner.leaseExpiresAt); } if (prior.owner.phase !== "committed" || prior.owner.leaseOwner) return deny("owner_unsettled"); + const liveOwnerRetryAt = await liveWarmSessionOwnerRetryAt({ owner: prior.owner, finishedAt: prior.run.finishedAt, + idleTimeoutMs: previous.session?.lifecyclePolicy?.idleTimeoutMs ?? null }); + if (liveOwnerRetryAt) throw new NativeControllerRecoveryWaitingError(liveOwnerRetryAt); if (sourceProviderLeaseId === providerLeaseId) settledPredecessor = { runId: identity.runId, providerLeaseId }; const checkpoint = input.loadCheckpoint(previous); if (!checkpoint || checkpoint.identity.runId !== identity.runId || diff --git a/server/src/services/run-start-handover.ts b/server/src/services/run-start-handover.ts index 1fef8c812ac..4109a9e75d8 100644 --- a/server/src/services/run-start-handover.ts +++ b/server/src/services/run-start-handover.ts @@ -347,6 +347,7 @@ export function createRunStartHandover(input: { refresh, /** This server holds a hand-over an older server may be reading. */ isHandingOver: () => handingOver(), + isLeavingRunStarts: () => yieldingTo !== null, /** A hand-over involves this server (either side). Pure read, no refresh. */ isActive: () => handingOver() || yieldingTo !== null, subscribe(listener: () => void) { @@ -387,6 +388,10 @@ export function refreshRunStartYieldIfStale(): Promise | null { return current?.refreshIfStale() ?? null; } +export function isLeavingRunStartsToReplacement(): boolean { + return current?.isLeavingRunStarts() ?? false; +} + /** Either side of a hand-over is in progress here; a pure read for listeners. */ export function isRunStartHandoverActive(): boolean { return current?.isActive() ?? false; From a18479bbba222e8e95d0cdf1fbd4043328c0d28d Mon Sep 17 00:00:00 2001 From: Dotta <34892728+cryppadotta@users.noreply.github.com> Date: Sat, 14 Nov 2026 12:19:43 -0600 Subject: [PATCH 2/2] perf(workspaces): start a full terminal workspace pass only at startup and on the hourly backstop (#21630) 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 terminal workspace sweep walks every candidate workspace in a full pass. On staging that is about 4,000 finished remote workspaces, and each one costs a persistence probe and a work product read. > - #21617 limited full passes to startup and an hourly backstop that only activity arms. But three other paths in the worker still started a full pass at once: > - a wake whose hints do not parse to workspace or task targets; > - more than 50 pending targets; > - any run that finds no targets. > - Across one staging deploy the probe count rose by about 2.8 full passes in 26 minutes. At 1,000 concurrent agents, more than 50 run-finish hints between passes would be normal, so the overflow path alone would bring back passes proportional to history. > - This pull request works through targets in batches of 50 and turns hintless wakes into the backstop. It gives the one caller that needs a broad look, a workspace provider plugin becoming ready, its own hint that inspects only plugin workspaces. > - The benefit is that a full pass happens only at startup and at most once per backstop hour, whatever the activity. ## Linked Issues or Issue Description No issue exists. This describes the problem with the fields of `.github/ISSUE_TEMPLATE/bug_report.yml`. **What happened?** On staging, the hourly steady state holds (one full pass per hour, no probes between). But across a deploy the probe count rose by 11,351 in under 26 minutes, about 2.8 full passes over the roughly 4,000 finished remote workspaces. **Steps to reproduce** The new test file `server/src/__tests__/terminal-workspace-rotation-limits.test.ts` runs the real coordinator and worker against real PostgreSQL on a fake clock, with finished remote workspaces that cannot be archived (the staging state). These are the counts on master. The numbers are workspaces checked by full passes; a pass over N workspaces checks N: | Scenario | Full-pass checks on master | Expected | | --- | --- | --- | | A burst of 120 run-finish `issue:` hints, 300 workspaces | 600 (2 extra passes) | 0 | | One hintless wake and one unparseable hint, 120 workspaces, 30 minutes | 1,490 (about 12 passes) | 0, then 1 at the backstop | | A workspace provider plugin becomes ready (hintless notify from `plugin-registry`), 123 workspaces | 123 (1 pass) | 0; only the 3 plugin workspaces | | Restart: old instance drains while hints arrive, new instance starts and gets the plugin hint and 120 run-finish hints, 202 workspaces, 30 minutes | 2,424 (12 passes) | 202 (the startup pass) | **Cause of the passes across a deploy:** - The new instance runs its startup pass. - Each workspace provider plugin that turns `ready` sends a hintless notification (`plugin-registry.ts`), which started a full pass. - Run recovery at startup finishes many runs, and their `issue:` hints overflow 50, which started another full pass. - Any later wake without targets also started a full pass, including the coordinator's 5-minute retry timer. In the fixture this produced two back-to-back passes every 5 minutes. - The draining old instance starts nothing, because the worker's `canRun` is false during drain. The test confirms this. **Expected behavior** A full pass runs only at startup and when the activity-armed backstop is due, at most once per hour. Targets are worked through in targeted batches. ## What Changed - `server/src/services/terminal-workspace-work.ts`: - **Pending targets.** Workspace and task targets go into pending sets with no cap. Each run inspects at most 50 of them, and the rest stay queued for the next run, which is scheduled at once. A failed targeted run puts its targets back. - **Hintless or unparseable wakes** only arm the hourly backstop. - **No targets, nothing due:** the run does nothing. - **Due deadlines:** the due workspaces are queued as targets, including isolated re-home and nested clone deadlines keyed by a workspace. A due non-workspace deadline (the isolated source orphan sweep, task directory work) runs a targeted sweep for its prelude and arms the backstop. - **A failed plugin workspace listing** restores the plugin refresh flag, so the coordinator's retry lists again. - **Full passes** happen only at startup, when the backstop is due, and on continuation pages of a pass already under way. - **New hint `plugin-workspaces`** (`TERMINAL_WORKSPACE_PLUGIN_HINT`): it queues the ids of plugin-provider candidate workspaces from `listPluginTerminalWorkspaceIds`. - The worker now always tells the service `targeted: true`, so a hint during a pass no longer requests a rescan lap. - `server/src/services/execution-workspaces.ts`: - New `listPluginTerminalWorkspaceIds` reads the plugin-provider candidates. - A targeted sweep reads all matches of its at most 50 targets, with no page limit and no rescan request. - A failed targeted sweep no longer requests a rescan lap, because the worker re-queues its targets. - `server/src/services/plugin-registry.ts`: the two workspace-provider notifications send `plugin-workspaces` instead of no hint. - `server/src/__tests__/terminal-workspace-work.test.ts`: a new test, `retries the plugin workspace listing after it fails instead of waiting for the backstop`, fails without the retry fix. Three existing tests now describe the new behaviour: - unscoped commits go to the backstop; - more than 50 hinted workspaces are inspected in batches of 50 and 1; - a due deadline on a service without targeting still runs a full pass. - `server/src/__tests__/terminal-workspace-rotation-limits.test.ts`: four new tests, the scenarios above. The startup pass stays. The 1,000-workspace test in `terminal-workspace-deadlines.test.ts` shows that durable deadlines keep an idle instance quiet after startup. But only the startup pass reconciles workspaces whose hints were lost while no instance was running, so removing it would need a stored "last full pass" marker first. ## Verification - The four new tests fail on master with the counts in the table and pass after the change: - 0 full-pass checks for the burst, worked as targeted batches of at most 50 (120 checks in total); - 0 full-pass checks for 30 minutes after hintless and unparseable wakes, then 1 pass at the backstop; - only the 3 plugin workspaces are inspected after the plugin hint; - exactly 1 full pass across the restart: none on the draining instance, only the startup pass on the new one. - Prompt archiving and the missed-hint backstop keep their existing tests in `terminal-workspace-deadlines.test.ts`: - the release of a holding run inspects its workspace at once; - a run made terminal outside `writeRunStatus` is archived within 1 hour 1 minute; - 1,000 finished remote workspaces stay unread at idle. - `npx vitest run` in `server/` on `terminal-workspace-rotation-limits`, `terminal-workspace-work`, `terminal-workspace-deadlines`, `terminal-workspace-idle`, `terminal-workspace-page-batch`, `execution-workspaces-service`, `plugin-workspaces.integration`, `workspace-operations-reconciliation`, `execution-workspace-reopen`, `isolated-task-directory-retention`, `git-workspace-expiration` and `workspace-delivery-cleanup.integration`. - `pnpm check:module-boundaries` passes. ## Risks - **A notification that relied on an immediate broad pass now waits up to an hour.** The only caller I found that sent such a notification on purpose is the workspace provider readiness in `plugin-registry.ts`, and it now has its own targeted hint. Other hintless notifications arm the backstop. - **The isolated source orphan sweep** runs only at the start of a full pass. Its due deadline now arms the backstop instead of starting a pass at once. This only matters when the isolated source re-home feature is enabled. - **A targeted sweep has no page limit.** Its at most 50 targets bound the matched workspaces to those tasks, their ancestors and their execution workspaces. - **A burst of N hints costs N/50 targeted runs, back to back.** The cost scales with activity, not with history. ## 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 --- ...terminal-workspace-rotation-limits.test.ts | 172 ++++++++++++++++++ .../__tests__/terminal-workspace-work.test.ts | 38 ++-- server/src/services/execution-workspaces.ts | 30 ++- server/src/services/plugin-registry.ts | 5 +- .../src/services/terminal-workspace-work.ts | 73 ++++---- 5 files changed, 263 insertions(+), 55 deletions(-) create mode 100644 server/src/__tests__/terminal-workspace-rotation-limits.test.ts diff --git a/server/src/__tests__/terminal-workspace-rotation-limits.test.ts b/server/src/__tests__/terminal-workspace-rotation-limits.test.ts new file mode 100644 index 00000000000..892ce9624f5 --- /dev/null +++ b/server/src/__tests__/terminal-workspace-rotation-limits.test.ts @@ -0,0 +1,172 @@ +import { randomUUID } from "node:crypto"; +import { eq, sql } from "drizzle-orm"; +import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest"; +import { companies, createDb, executionWorkspaceSnapshots, executionWorkspaces, issues, projects, type Db } from "@paperclipai/db"; +import { createDeliveryWorkCoordinator } from "../services/delivery-work-coordinator.js"; +import { DELIVERY_QUEUES, notifyDeliveryWork } from "../services/delivery-work-notifications.js"; +import { executionWorkspaceService } from "../services/execution-workspaces.js"; +import { + notifyTerminalWorkspaceWork, registerTerminalWorkspaceWork, TERMINAL_WORKSPACE_BACKSTOP_MS, TERMINAL_WORKSPACE_PLUGIN_HINT, + TERMINAL_WORKSPACE_RETRY_MS, +} from "../services/terminal-workspace-work.js"; +import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js"; +import { resolveExternalTestDatabaseUrl } from "./helpers/external-test-database.js"; + +const external = resolveExternalTestDatabaseUrl("PAPERCLIP_TERMINAL_WORKSPACE_ROTATION_TEST_DATABASE_URL"); +const support = external ? { supported: true } : await getEmbeddedPostgresTestSupport(); +const HOUR = 3_600_000; +const realSetTimeout = globalThis.setTimeout; +const realSleep = (ms: number) => new Promise(resolve => realSetTimeout(resolve, ms)); + +(support.supported ? describe : describe.skip)("terminal workspace full rotation limits", () => { + let temporary: Awaited> | undefined; + let db: Db; + beforeAll(async () => { + if (!external) temporary = await startEmbeddedPostgresTestDatabase("terminal-workspace-rotation-"); + db = createDb(external ?? temporary!.connectionString); + db.$client.options.onnotice = () => {}; + }, 120_000); + afterEach(async () => { vi.useRealTimers(); await db.execute(sql`truncate companies cascade`); }); + afterAll(async () => { await db?.$client.end({ timeout: 1 }); await temporary?.cleanup(); }); + + async function seed(input: { remote: number; plugin?: number }) { + const companyId = randomUUID(), projectId = randomUUID(); + await db.insert(companies).values({ id: companyId, name: "Rotation", issuePrefix: `R${companyId.slice(0, 6)}` }); + await db.insert(projects).values({ id: projectId, companyId, name: "Project", status: "in_progress" }); + const old = new Date(Date.now() - 5 * 24 * HOUR); + const remote = Array.from({ length: input.remote }, () => ({ workspaceId: randomUUID(), issueId: randomUUID(), snapshotId: randomUUID() })); + const plugin = Array.from({ length: input.plugin ?? 0 }, () => ({ workspaceId: randomUUID(), issueId: randomUUID() })); + for (let index = 0; index < remote.length; index += 250) { + const page = remote.slice(index, index + 250); + await db.insert(executionWorkspaces).values(page.map(row => ({ id: row.workspaceId, companyId, projectId, mode: "isolated_workspace", + strategyType: "git_worktree", name: row.workspaceId, status: "active", providerType: "remote", createdAt: old, updatedAt: old, + metadata: { remoteWorkspace: { schema: "paperclip.remote-workspace.v1", epoch: 1, snapshotId: row.snapshotId, source: { kind: "empty" }, + writerRunId: null, dirty: false, location: { environmentId: randomUUID(), leaseId: randomUUID(), + providerLeaseId: `sandbox-${row.workspaceId}`, remoteCwd: "/workspace" } } } }))); + await db.insert(executionWorkspaceSnapshots).values(page.map(row => ({ id: row.snapshotId, companyId, executionWorkspaceId: row.workspaceId, + sourceOperationId: randomUUID(), generation: 1, epoch: 1, status: "committed" as const }))); + } + if (plugin.length) await db.insert(executionWorkspaces).values(plugin.map(row => ({ id: row.workspaceId, companyId, projectId, + mode: "isolated_workspace", strategyType: "git_worktree", name: row.workspaceId, status: "active", providerType: "plugin", + createdAt: old, updatedAt: old }))); + const all = [...remote, ...plugin]; + for (let index = 0; index < all.length; index += 250) { + const page = all.slice(index, index + 250); + await db.insert(issues).values(page.map(row => ({ id: row.issueId, companyId, projectId, title: "done", status: "done", + executionWorkspaceId: row.workspaceId, completedAt: old, updatedAt: old }))); + } + await db.execute(sql`update execution_workspaces set source_issue_id = issues.id, updated_at = ${old.toISOString()}::timestamptz + from issues where issues.execution_workspace_id = execution_workspaces.id and issues.company_id = ${companyId}::uuid`); + return { companyId, remote, plugin }; + } + + function start(canRun: () => boolean = () => true) { + vi.useFakeTimers({ toFake: ["Date", "setTimeout", "clearTimeout"] }); + const service = executionWorkspaceService(db, { workspaceReaperCooldownDays: 1, terminalWorkspaceInspectionBackoffMs: TERMINAL_WORKSPACE_RETRY_MS }); + const sweeps: Array<{ targeted: boolean; checked: number; workspaceIds: number }> = []; + let inFlight = 0; + const coordinator = createDeliveryWorkCoordinator({ owner: db, canRun, onError: () => {} }); + const worker = registerTerminalWorkspaceWork(coordinator, { ...service, + sweepTerminalWorkspaces: async (limit, signal, options) => { + inFlight += 1; + const result = await service.sweepTerminalWorkspaces(limit, signal, options).finally(() => { inFlight -= 1; }); + sweeps.push({ targeted: Boolean(options?.targets), checked: result.checked, + workspaceIds: (options?.targets?.workspaceIds.length ?? 0) + (options?.targets?.issueIds.length ?? 0) }); + return result; + } }, canRun); + const advance = async (ms: number) => { + for (let elapsed = 0; elapsed < ms; elapsed += 10_000) { + await vi.advanceTimersByTimeAsync(10_000); + do { await realSleep(10); await vi.advanceTimersByTimeAsync(1); } while (inFlight > 0); + } + }; + const settle = async (operation: Promise) => { + let done = false; + const result = operation.finally(() => { done = true; }); + while (!done) { await realSleep(5); await vi.advanceTimersByTimeAsync(1); } + return result; + }; + const fullChecked = (from = 0) => sweeps.slice(from).filter(sweep => !sweep.targeted).reduce((sum, sweep) => sum + sweep.checked, 0); + return { sweeps, advance, settle, fullChecked, stop: async () => { vi.useRealTimers(); await worker.stop(); await coordinator.stop(); } }; + } + + it("works through a burst of run-finish hints in targeted batches without a full pass", async () => { + const f = await seed({ remote: 300 }); + const work = start(); + try { + await work.advance(60_000); + expect(work.fullChecked()).toBe(300); + const mark = work.sweeps.length; + const burst = f.remote.slice(0, 120); + for (let index = 0; index < burst.length; index += 40) { + await work.settle(db.transaction(async tx => { + for (const row of burst.slice(index, index + 40)) await notifyTerminalWorkspaceWork(tx, `issue:${row.issueId}`); + })); + } + await work.advance(5 * 60_000); + const window = work.sweeps.slice(mark); + expect(work.fullChecked(mark)).toBe(0); + expect(window.every(sweep => sweep.targeted && sweep.workspaceIds <= 50)).toBe(true); + expect(window.reduce((sum, sweep) => sum + sweep.checked, 0)).toBe(120); + } finally { await work.stop(); } + }, 300_000); + + it("arms the hourly backstop for a hint without a target instead of starting a pass", async () => { + await seed({ remote: 120 }); + const work = start(); + try { + await work.advance(60_000); + const mark = work.sweeps.length; + await work.settle(db.transaction(tx => notifyDeliveryWork(tx, DELIVERY_QUEUES.terminalWorkspaces))); + await work.settle(db.transaction(tx => notifyDeliveryWork(tx, DELIVERY_QUEUES.terminalWorkspaces, "not-a-target"))); + await work.advance(30 * 60_000); + expect(work.fullChecked(mark)).toBe(0); + await work.advance(TERMINAL_WORKSPACE_BACKSTOP_MS); + expect(work.fullChecked(mark)).toBe(120); + } finally { await work.stop(); } + }, 300_000); + + it("inspects plugin workspaces when a workspace provider becomes ready without a full pass", async () => { + await seed({ remote: 120, plugin: 3 }); + const work = start(); + try { + await work.advance(60_000); + const mark = work.sweeps.length; + await work.settle(db.transaction(tx => notifyTerminalWorkspaceWork(tx, TERMINAL_WORKSPACE_PLUGIN_HINT))); + await work.advance(60_000); + expect(work.fullChecked(mark)).toBe(0); + expect(work.sweeps.slice(mark).reduce((sum, sweep) => sum + sweep.checked, 0)).toBe(3); + } finally { await work.stop(); } + }, 300_000); + + it("runs one full pass across a restart: none while the old instance drains and one at the new start", async () => { + const f = await seed({ remote: 200, plugin: 2 }); + let draining = false; + const old = start(() => !draining); + try { + await old.advance(60_000); + const mark = old.sweeps.length; + draining = true; + for (let index = 0; index < 120; index += 40) { + await old.settle(db.transaction(async tx => { + for (const row of f.remote.slice(index, index + 40)) await notifyTerminalWorkspaceWork(tx, `issue:${row.issueId}`); + })); + } + await old.settle(db.transaction(tx => notifyDeliveryWork(tx, DELIVERY_QUEUES.terminalWorkspaces))); + await old.advance(5 * 60_000); + expect(old.sweeps.slice(mark)).toEqual([]); + } finally { await old.stop(); } + const fresh = start(); + try { + await fresh.advance(10_000); + await fresh.settle(db.transaction(tx => notifyTerminalWorkspaceWork(tx, TERMINAL_WORKSPACE_PLUGIN_HINT))); + for (let index = 0; index < 120; index += 40) { + await fresh.settle(db.transaction(async tx => { + for (const row of f.remote.slice(index, index + 40)) await notifyTerminalWorkspaceWork(tx, `issue:${row.issueId}`); + })); + } + await fresh.advance(30 * 60_000); + expect(fresh.fullChecked()).toBe(202); + } finally { await fresh.stop(); } + }, 300_000); +}); diff --git a/server/src/__tests__/terminal-workspace-work.test.ts b/server/src/__tests__/terminal-workspace-work.test.ts index 4ac80c30906..5189bc03eb0 100644 --- a/server/src/__tests__/terminal-workspace-work.test.ts +++ b/server/src/__tests__/terminal-workspace-work.test.ts @@ -1,7 +1,7 @@ import { afterEach, describe, expect, it, vi } from "vitest"; import { installDatabaseWorkSignals } from "../../../packages/db/src/work-signals.js"; import { createDeliveryWorkCoordinator } from "../services/delivery-work-coordinator.js"; -import { notifyTerminalWorkspaceWork, registerTerminalWorkspaceWork, TERMINAL_WORKSPACE_RETRY_MS, type TerminalWorkspaceTargets } from "../services/terminal-workspace-work.js"; +import { notifyTerminalWorkspaceWork, registerTerminalWorkspaceWork, TERMINAL_WORKSPACE_BACKSTOP_MS, TERMINAL_WORKSPACE_PLUGIN_HINT, TERMINAL_WORKSPACE_RETRY_MS, type TerminalWorkspaceTargets } from "../services/terminal-workspace-work.js"; import { idleWorkSnapshot } from "../services/task-admission.js"; const coordinators: ReturnType[] = []; @@ -88,18 +88,21 @@ describe("terminal workspace work", () => { } finally { release?.(); } }); - it("restores the cooldown deadline, lets commits preempt it, and parks after cleanup", async () => { + it("restores the cooldown deadline, leaves unscoped commits to the hourly backstop, and parks after cleanup", async () => { const s = setup(), due = Date.now() + 86_400_000; s.service.nextTerminalWorkspaceSweepAt.mockResolvedValue(due); const worker = s.start(); await worker.ready; expect(worker.hasUnrecoveredWork()).toBe(false); - await vi.advanceTimersByTimeAsync(3_600_000); + await vi.advanceTimersByTimeAsync(30 * 60_000); expect(s.service.sweepTerminalWorkspaces).toHaveBeenCalledTimes(1); await s.owner.transaction(tx => notifyTerminalWorkspaceWork(tx)); await s.owner.transaction(tx => notifyTerminalWorkspaceWork(tx)); await vi.advanceTimersByTimeAsync(1); + expect(s.service.sweepTerminalWorkspaces).toHaveBeenCalledTimes(1); + await vi.advanceTimersByTimeAsync(TERMINAL_WORKSPACE_BACKSTOP_MS); expect(s.service.sweepTerminalWorkspaces).toHaveBeenCalledTimes(2); + expect(s.service.sweepTerminalWorkspaces.mock.calls[1]?.[2]?.targets).toBeUndefined(); expect(s.coordinator.nextWakeAt()).toBe(due); s.service.nextTerminalWorkspaceSweepAt.mockResolvedValue(null); await vi.advanceTimersByTimeAsync(due - Date.now()); @@ -107,7 +110,7 @@ describe("terminal workspace work", () => { expect(vi.getTimerCount()).toBe(0); }); - it("inspects only hinted workspaces after a commit, and keeps unscoped commits and deadlines on the full rotation", async () => { + it("inspects only hinted workspaces after a commit, and leaves an unscoped commit to the backstop", async () => { const s = setup(), due = Date.now() + 60_000; s.service.nextTerminalWorkspaceSweepAt.mockResolvedValue(due); await s.start().ready; @@ -121,21 +124,34 @@ describe("terminal workspace work", () => { expect(s.service.sweepTerminalWorkspaces.mock.calls[1]?.[2]).toMatchObject({ targets: { workspaceIds: [workspaceId], issueIds: [issueId] } }); await s.owner.transaction(tx => notifyTerminalWorkspaceWork(tx)); await vi.advanceTimersByTimeAsync(1); + expect(s.service.sweepTerminalWorkspaces).toHaveBeenCalledTimes(2); + await vi.advanceTimersByTimeAsync(due - Date.now()); expect(s.service.sweepTerminalWorkspaces).toHaveBeenCalledTimes(3); expect(s.service.sweepTerminalWorkspaces.mock.calls[2]?.[2]?.targets).toBeUndefined(); - await vi.advanceTimersByTimeAsync(due - Date.now()); - expect(s.service.sweepTerminalWorkspaces).toHaveBeenCalledTimes(4); - expect(s.service.sweepTerminalWorkspaces.mock.calls[3]?.[2]?.targets).toBeUndefined(); }); - it("falls back to the full rotation when more workspaces are hinted than one inspection reads", async () => { + it("retries the plugin workspace listing after it fails instead of waiting for the backstop", async () => { + const s = setup(); + const pluginWorkspaceId = "6b1f9c2e-8a4d-4f3b-9c1e-2d7a5b8e0f13"; + const listPluginTerminalWorkspaceIds = vi.fn<() => Promise>() + .mockRejectedValueOnce(new Error("database unavailable")).mockResolvedValue([pluginWorkspaceId]); + await registerTerminalWorkspaceWork(s.coordinator, { ...s.service, listPluginTerminalWorkspaceIds }, s.enabled).ready; + await s.owner.transaction(tx => notifyTerminalWorkspaceWork(tx, TERMINAL_WORKSPACE_PLUGIN_HINT)); + await vi.advanceTimersByTimeAsync(1); + expect(listPluginTerminalWorkspaceIds).toHaveBeenCalledTimes(1); + await vi.advanceTimersByTimeAsync(TERMINAL_WORKSPACE_RETRY_MS); + expect(listPluginTerminalWorkspaceIds).toHaveBeenCalledTimes(2); + expect(s.service.sweepTerminalWorkspaces.mock.calls.at(-1)?.[2]?.targets).toEqual({ workspaceIds: [pluginWorkspaceId], issueIds: [] }); + }); + + it("works through more hinted workspaces than one inspection reads in targeted batches", async () => { const s = setup(); await s.start().ready; const ids = Array.from({ length: 51 }, (_, index) => `00000000-0000-4000-8000-${String(index).padStart(12, "0")}`); await s.owner.transaction(async tx => { for (const id of ids) await notifyTerminalWorkspaceWork(tx, `workspace:${id}`); }); - await vi.advanceTimersByTimeAsync(1); - expect(s.service.sweepTerminalWorkspaces).toHaveBeenCalledTimes(2); - expect(s.service.sweepTerminalWorkspaces.mock.calls[1]?.[2]?.targets).toBeUndefined(); + await vi.advanceTimersByTimeAsync(10); + expect(s.service.sweepTerminalWorkspaces).toHaveBeenCalledTimes(3); + expect(s.service.sweepTerminalWorkspaces.mock.calls.slice(1).map(call => call[2]?.targets?.workspaceIds.length)).toEqual([50, 1]); }); it("keeps the pause between rotation pages while hinted workspaces wait", async () => { diff --git a/server/src/services/execution-workspaces.ts b/server/src/services/execution-workspaces.ts index 999a2424f70..9bb81f59c19 100644 --- a/server/src/services/execution-workspaces.ts +++ b/server/src/services/execution-workspaces.ts @@ -39,7 +39,7 @@ import { authorizationService, executionWorkspaceReadSqlCondition, projectReadSq import type { SQL } from "drizzle-orm"; import { nativeSessionProviderEvidence } from "./native-runtime/native-session-resume.js"; import { hasWorkspaceRestoreFailure } from "@paperclipai/shared"; -import { notifyTerminalWorkspaceWork, terminalWorkspaceTargetsFromHints, type TerminalWorkspaceTargets } from "./terminal-workspace-work.js"; +import { notifyTerminalWorkspaceWork, type TerminalWorkspaceTargets } from "./terminal-workspace-work.js"; import { clearTerminalWorkspaceRetry, readTerminalWorkspaceRetry, recordTerminalWorkspaceFailure, terminalWorkspaceFailure, TERMINAL_WORKSPACE_RETRY_KEY, type TerminalWorkspaceFailure, @@ -3688,16 +3688,15 @@ export function executionWorkspaceService(db: Db, opts: ExecutionWorkspaceServic : undefined; const scanFilter = targets ? and(baseCandidateFilter, terminalWorkspaceTargetFilter(targets)) : and(baseCandidateFilter, boundaryFilter, cursorFilter); - const candidates = await db + const candidateQuery = db .select({ workspace: executionWorkspaces, cursorAt: sql`${executionWorkspaces.updatedAt}::text` }) .from(executionWorkspaces) .where(scanFilter) - .orderBy(asc(executionWorkspaces.updatedAt), asc(executionWorkspaces.id)) - .limit(limit); + .orderBy(asc(executionWorkspaces.updatedAt), asc(executionWorkspaces.id)); + const candidates = await (targets ? candidateQuery : candidateQuery.limit(limit)); // Advance the cursor to this page's last row. A short page means the scan // reached the end of the bounded candidate set, so reset the cursor and // the bound to start a new rotation on the next sweep. - if (targets && candidates.length >= limit) terminalSweepRescanRequested = true; if (!targets && candidates.length < limit) { terminalSweepCursor = null; terminalSweepBoundary = null; @@ -4398,8 +4397,8 @@ export function executionWorkspaceService(db: Db, opts: ExecutionWorkspaceServic if (!options.targets) { terminalSweepCursor = previousCursor; terminalSweepBoundary = previousBoundary; + terminalSweepRescanRequested = true; } - terminalSweepRescanRequested = true; throw error; } finally { terminalSweepInProgress = false; @@ -4409,7 +4408,7 @@ export function executionWorkspaceService(db: Db, opts: ExecutionWorkspaceServic hasTerminalWorkspaceSweepContinuation: () => terminalSweepCursor !== null || terminalSweepRescanRequested, terminalWorkspaceWorkSettled: (hints?: ReadonlySet, options: { targeted?: boolean } = {}) => { - if ((terminalSweepInProgress || terminalSweepCursor !== null) && !(options.targeted && terminalWorkspaceTargetsFromHints(hints))) { + if ((terminalSweepInProgress || terminalSweepCursor !== null) && !options.targeted) { terminalSweepRescanRequested = true; } pullRequestStateCache.clear(); @@ -4432,11 +4431,24 @@ export function executionWorkspaceService(db: Db, opts: ExecutionWorkspaceServic } }, + listPluginTerminalWorkspaceIds: async () => { + const rows = await db.select({ id: executionWorkspaces.id }).from(executionWorkspaces).where(and( + eq(executionWorkspaces.providerType, "plugin"), + sql`${executionWorkspaces.sourceIssueId} IS NOT NULL`, + or(and(inArray(executionWorkspaces.status, ["active", "idle", "in_review"]), isNull(executionWorkspaces.closedAt)), + and(inArray(executionWorkspaces.status, ["archived", "cleanup_failed"]), + sql`(${executionWorkspaces.metadata}->>'workspaceProviderRemoved') IS DISTINCT FROM 'true'`)), + )); + return rows.map(row => row.id); + }, + takeDueTerminalWorkspaceTargets: (): { workspaceIds: string[]; rotation: boolean } => { const at = now().getTime(); const due = (deadlines: Map) => [...deadlines].filter(([, deadline]) => deadline <= at).map(([id]) => id); - const rotation = due(isolatedRehomeDeadlines).length + due(nestedCloneMigrationDeadlines).length + due(taskDirectoryDeadlines).length > 0; - return { workspaceIds: due(terminalWorkspaceDeadlines), rotation }; + const workspaceKey = (key: string) => /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(key); + const migrations = [...due(isolatedRehomeDeadlines), ...due(nestedCloneMigrationDeadlines)]; + const rotation = due(taskDirectoryDeadlines).length > 0 || migrations.some(key => !workspaceKey(key)); + return { workspaceIds: [...new Set([...due(terminalWorkspaceDeadlines), ...migrations.filter(workspaceKey)])], rotation }; }, nextTerminalWorkspaceSweepAt: async () => { diff --git a/server/src/services/plugin-registry.ts b/server/src/services/plugin-registry.ts index ab9eabc658d..30f2259d958 100644 --- a/server/src/services/plugin-registry.ts +++ b/server/src/services/plugin-registry.ts @@ -32,6 +32,7 @@ import type { } from "@paperclipai/shared"; import { conflict, notFound } from "../errors.js"; import { DELIVERY_QUEUES, notifyDeliveryWork } from "./delivery-work-notifications.js"; +import { TERMINAL_WORKSPACE_PLUGIN_HINT } from "./terminal-workspace-work.js"; function sameBundledPluginRegistration(existing: typeof plugins.$inferSelect, packageName: string, manifest: PaperclipPluginManifestV1): boolean { return existing.pluginKey === manifest.id && existing.packageName === packageName && existing.version === manifest.version && isDeepStrictEqual(existing.manifestJson, manifest); @@ -92,7 +93,7 @@ export function pluginRegistryService(db: Db) { async function writeWorkspaceProviderChange(reconsiderWorkspaces: boolean, write: (database: Db) => Promise): Promise { return db.transaction(async tx => { await notifyExternalObjectRefresh(tx); - if (reconsiderWorkspaces) await notifyDeliveryWork(tx, DELIVERY_QUEUES.terminalWorkspaces); + if (reconsiderWorkspaces) await notifyDeliveryWork(tx, DELIVERY_QUEUES.terminalWorkspaces, TERMINAL_WORKSPACE_PLUGIN_HINT); return write(tx as unknown as Db); }); } @@ -517,7 +518,7 @@ export function pluginRegistryService(db: Db) { .then((rows) => rows[0] ?? null); if (plugin.manifestJson.workspaceProvider && existing?.enabled === false && input.enabled === true) { - await notifyDeliveryWork(db, DELIVERY_QUEUES.terminalWorkspaces); + await notifyDeliveryWork(db, DELIVERY_QUEUES.terminalWorkspaces, TERMINAL_WORKSPACE_PLUGIN_HINT); } if (existing) { diff --git a/server/src/services/terminal-workspace-work.ts b/server/src/services/terminal-workspace-work.ts index b9845bdbb5d..6f8220f0854 100644 --- a/server/src/services/terminal-workspace-work.ts +++ b/server/src/services/terminal-workspace-work.ts @@ -4,6 +4,7 @@ import { DELIVERY_QUEUES, notifyDeliveryWork } from "./delivery-work-notificatio export const TERMINAL_WORKSPACE_RETRY_MS = 5 * 60 * 1000; export const MAX_TERMINAL_WORKSPACE_TARGETS = 50; export const TERMINAL_WORKSPACE_BACKSTOP_MS = 60 * 60 * 1000; +export const TERMINAL_WORKSPACE_PLUGIN_HINT = "plugin-workspaces"; export function notifyTerminalWorkspaceWork(transaction: object, hint?: string) { return notifyDeliveryWork(transaction, DELIVERY_QUEUES.terminalWorkspaces, hint); @@ -38,6 +39,7 @@ export function registerTerminalWorkspaceWork( hasTerminalWorkspaceSweepContinuation(): boolean; terminalWorkspaceWorkSettled?(hints?: ReadonlySet, options?: { targeted?: boolean }): void; takeDueTerminalWorkspaceTargets?(): { workspaceIds: string[]; rotation: boolean }; + listPluginTerminalWorkspaceIds?(): Promise; }, canRun: () => boolean, ) { @@ -48,26 +50,31 @@ export function registerTerminalWorkspaceWork( // can take seconds per workspace, so the first sweep skips it and later // sweeps do it. let startupSweepStarted = false; - let rotationRequested = false; let dueAt: number | null = null; let lastRotationPageAt = 0; let backstopArmed = false; let lastFullRotationAt = Date.now(); let continuing = false; - const targetWorkspaceIds = new Set(), targetIssueIds = new Set(); + let pluginRefresh = false; + const pendingWorkspaceIds = new Set(), pendingIssueIds = new Set(); + const pendingCount = () => pendingWorkspaceIds.size + pendingIssueIds.size; + const takeTargets = (): TerminalWorkspaceTargets => { + const workspaceIds = [...pendingWorkspaceIds].slice(0, MAX_TERMINAL_WORKSPACE_TARGETS); + for (const id of workspaceIds) pendingWorkspaceIds.delete(id); + const issueIds = [...pendingIssueIds].slice(0, MAX_TERMINAL_WORKSPACE_TARGETS - workspaceIds.length); + for (const id of issueIds) pendingIssueIds.delete(id); + return { workspaceIds, issueIds }; + }; const worker = coordinator.register(DELIVERY_QUEUES.terminalWorkspaces, { retryMs: TERMINAL_WORKSPACE_RETRY_MS, onSettled: hints => { backstopArmed = true; - const targets = terminalWorkspaceTargetsFromHints(hints); - for (const id of targets?.workspaceIds ?? []) targetWorkspaceIds.add(id); - for (const id of targets?.issueIds ?? []) targetIssueIds.add(id); - const overflow = targetWorkspaceIds.size + targetIssueIds.size > MAX_TERMINAL_WORKSPACE_TARGETS; - service.terminalWorkspaceWorkSettled?.(hints, { targeted: Boolean(targets) && !overflow }); - if (!targets || overflow) { - rotationRequested = true; - targetWorkspaceIds.clear(); targetIssueIds.clear(); - } + if (hints?.has(TERMINAL_WORKSPACE_PLUGIN_HINT)) pluginRefresh = true; + const scoped = hints ? new Set([...hints].filter(hint => hint !== TERMINAL_WORKSPACE_PLUGIN_HINT)) : undefined; + const targets = terminalWorkspaceTargetsFromHints(scoped); + for (const id of targets?.workspaceIds ?? []) pendingWorkspaceIds.add(id); + for (const id of targets?.issueIds ?? []) pendingIssueIds.add(id); + service.terminalWorkspaceWorkSettled?.(hints, { targeted: true }); }, run: async signal => { recovered = false; @@ -76,44 +83,44 @@ export function registerTerminalWorkspaceWork( next = Date.now() + TERMINAL_WORKSPACE_RETRY_MS; const startup = !startupSweepStarted; startupSweepStarted = true; - let dueRotation = due; + const options = { compact: !startup, allowRemoteDeliveryChecks: !startup }; + let prelude = false; + const legacyDue = due && !startup && !service.takeDueTerminalWorkspaceTargets; if (due && !startup && service.takeDueTerminalWorkspaceTargets) { const dueTargets = service.takeDueTerminalWorkspaceTargets(); - dueRotation = dueTargets.rotation; - for (const id of dueTargets.workspaceIds) targetWorkspaceIds.add(id); - if (targetWorkspaceIds.size + targetIssueIds.size > MAX_TERMINAL_WORKSPACE_TARGETS) { - dueRotation = true; - targetWorkspaceIds.clear(); targetIssueIds.clear(); + for (const id of dueTargets.workspaceIds) pendingWorkspaceIds.add(id); + if (dueTargets.rotation) { prelude = true; backstopArmed = true; } + } + if (pluginRefresh && !startup && service.listPluginTerminalWorkspaceIds) { + pluginRefresh = false; + try { + for (const id of await service.listPluginTerminalWorkspaceIds()) pendingWorkspaceIds.add(id); + } catch (error) { + pluginRefresh = true; + throw error; } } - const options = { compact: !startup, allowRemoteDeliveryChecks: !startup }; - const targeted = targetWorkspaceIds.size + targetIssueIds.size > 0; const backstopDue = backstopArmed && Date.now() >= lastFullRotationAt + TERMINAL_WORKSPACE_BACKSTOP_MS; - if (due && service.takeDueTerminalWorkspaceTargets && !startup && !dueRotation && !targeted && !rotationRequested && !backstopDue - && !service.hasTerminalWorkspaceSweepContinuation()) return { checked: 0 }; - if (startup || dueRotation || rotationRequested || backstopDue || !targeted - || (service.hasTerminalWorkspaceSweepContinuation() && Date.now() >= lastRotationPageAt + 1000)) { - rotationRequested = false; + if (startup || backstopDue || legacyDue || (continuing && Date.now() >= lastRotationPageAt + 1000)) { lastRotationPageAt = Date.now(); - if (!continuing) { lastFullRotationAt = Date.now(); backstopArmed = false; } + if (!continuing) { lastFullRotationAt = Date.now(); backstopArmed = false; pluginRefresh = false; } return service.sweepTerminalWorkspaces(50, signal, options); } - const targets = { workspaceIds: [...targetWorkspaceIds], issueIds: [...targetIssueIds] }; - targetWorkspaceIds.clear(); targetIssueIds.clear(); + if (pendingCount() === 0 && !prelude) return { checked: 0 }; + const targets = takeTargets(); try { return await service.sweepTerminalWorkspaces(50, signal, { ...options, targets }); } catch (error) { - rotationRequested = true; + for (const id of targets.workspaceIds) pendingWorkspaceIds.add(id); + for (const id of targets.issueIds) pendingIssueIds.add(id); throw error; } }, hasPending: async () => { - continuing = false; + if (canRun()) continuing = service.hasTerminalWorkspaceSweepContinuation(); if (!canRun()) next = Date.now() + TERMINAL_WORKSPACE_RETRY_MS; - else if (service.hasTerminalWorkspaceSweepContinuation()) { - continuing = true; - next = Math.max(Date.now(), lastRotationPageAt + 1000); - } else if (rotationRequested || targetWorkspaceIds.size + targetIssueIds.size > 0) next = Date.now(); + else if (continuing) next = Math.max(Date.now(), lastRotationPageAt + 1000); + else if (pendingCount() > 0 || pluginRefresh) next = Date.now(); else { const deadline = await service.nextTerminalWorkspaceSweepAt(); next = deadline === null ? null : deadline > Date.now()