From 926a9119b063ac804a0e83094598e20b2e046745 Mon Sep 17 00:00:00 2001 From: Dotta Date: Fri, 13 Nov 2026 00:00:29 -0600 Subject: [PATCH] fix(recovery): always record the end of retention for abandoned files Co-Authored-By: Claude Opus 5.5 --- .../abandoned-workspace-files-hold.test.ts | 23 ++++++++++++++++--- .../remote-workspace-abandoned-files-hold.ts | 4 ++-- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/server/src/__tests__/abandoned-workspace-files-hold.test.ts b/server/src/__tests__/abandoned-workspace-files-hold.test.ts index 53ec13cc30b..9ba279adc77 100644 --- a/server/src/__tests__/abandoned-workspace-files-hold.test.ts +++ b/server/src/__tests__/abandoned-workspace-files-hold.test.ts @@ -305,7 +305,7 @@ describe.skipIf(!support.supported)("sandbox that holds files from an abandoned const [released] = await entries("workspace.abandoned_files_released"); expect(released).toMatchObject({ entityType: "issue", entityId: seeded.issueId, actorType: "system", actorId: "workspace-retention" }); expect(released!.details).toMatchObject({ reason: "retention_expired", - summary: "Unsaved files from an interrupted run were kept for 8 days. They are scheduled for removal." }); + summary: "Unsaved files from an interrupted run were kept for 8 days, and their retention period has ended. They are scheduled for removal." }); expect(await entries("workspace.abandoned_files_removed")).toEqual([]); await sweep(runtime); @@ -333,7 +333,7 @@ describe.skipIf(!support.supported)("sandbox that holds files from an abandoned expect(await leaseRow(seeded)).toMatchObject({ status: "pending_cleanup" }); }); - it("says nothing on the task when another receipt still holds the sandbox", async () => { + it("records the end of retention without scheduling removal when another receipt still holds the sandbox", async () => { const seeded = await seed(); await abandon(seeded); const { call, runtime } = worker(seeded); @@ -347,11 +347,28 @@ describe.skipIf(!support.supported)("sandbox that holds files from an abandoned retainUntil: new Date(Date.now() + 86_400_000).toISOString() }, ] } }).where(eq(executionWorkspaces.id, seeded.executionWorkspaceId)); await sweep(runtime); - expect(await entries("workspace.abandoned_files_released")).toEqual([]); + const [ended] = await entries("workspace.abandoned_files_released"); + expect(ended).toMatchObject({ entityType: "issue", entityId: seeded.issueId, actorType: "system", actorId: "workspace-retention" }); + expect(ended!.details).toMatchObject({ reason: "retention_expired", leasesHandedToCleanup: 0, + summary: expect.not.stringContaining("scheduled for removal") }); + expect((ended!.details as { summary: string }).summary).toContain("retention period has ended"); expect(destroyCalls(call)).toEqual([]); expect(await leaseRow(seeded)).toMatchObject({ status: "retained" }); }); + it("records the end of retention when it expires before the lease was ever parked", async () => { + const seeded = await seed(); + await abandon(seeded); + const { runtime } = worker(seeded); + await setReceipts(seeded, receipt => ({ ...receipt, retainUntil: new Date(Date.now() - 1_000).toISOString() })); + await sweep(runtime); + const [ended] = await entries("workspace.abandoned_files_released"); + expect(ended).toMatchObject({ entityType: "issue", entityId: seeded.issueId, actorId: "workspace-retention" }); + expect(ended!.details).toMatchObject({ reason: "retention_expired", leasesHandedToCleanup: 0 }); + expect((ended!.details as { summary: string }).summary).not.toContain("scheduled for removal"); + expect(await entries("workspace.abandoned_files_removed")).toEqual([]); + }); + it("wakes the cleanup worker at the receipt's retention end, also after a restart, and then goes quiet", async () => { const seeded = await seed(); await abandon(seeded); diff --git a/server/src/services/remote-workspace-abandoned-files-hold.ts b/server/src/services/remote-workspace-abandoned-files-hold.ts index 6f65507220e..06078f08745 100644 --- a/server/src/services/remote-workspace-abandoned-files-hold.ts +++ b/server/src/services/remote-workspace-abandoned-files-hold.ts @@ -112,13 +112,13 @@ export async function releaseAbandonedWorkspaceFiles(db: Db, input: { { companyId: input.companyId }); const scheduled = unparked.length > 0; const summary = input.reason === "retention_expired" - ? scheduled ? `Unsaved files from an interrupted run were kept for ${days(keptDays)}. They are scheduled for removal.` : null + ? `Unsaved files from an interrupted run were kept for ${days(keptDays)}, and their retention period has ended.${scheduled ? " They are scheduled for removal." : ""}` : input.reason === "discarded" ? `Unsaved files from an interrupted run were discarded.${scheduled ? " They are scheduled for removal." : ""}` : "Unsaved files from an interrupted run were saved to the task's recovered files."; logger.info({ companyId: input.companyId, workspaceId: workspace.id, abandonedEpoch: input.epoch, reason: input.reason, actorType: input.actorType, actorId: input.actorId, leasesHandedToCleanup: unparked.length }, "abandoned workspace files released"); - if (summary) await logActivity(tx, { companyId: input.companyId, actorType: input.actorType, actorId: input.actorId, + await logActivity(tx, { companyId: input.companyId, actorType: input.actorType, actorId: input.actorId, action: "workspace.abandoned_files_released", entityType: issueId ? "issue" : "execution_workspace", entityId: issueId ?? workspace.id, runId, details: { summary, reason: input.reason, workspaceId: workspace.id, abandonedEpoch: input.epoch,