From dfbba9aa403e7d474d6bd014fe08b59d48ef2c6a Mon Sep 17 00:00:00 2001 From: Vincent Koc <25068+vincentkoc@users.noreply.github.com> Date: Tue, 16 Jun 2026 14:25:54 +0800 Subject: [PATCH] fix(cron): clean up aborted isolated sessions --- .../run.meta-error-status.test.ts | 19 +++++++++++++++++-- src/cron/isolated-agent/run.ts | 7 +++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/cron/isolated-agent/run.meta-error-status.test.ts b/src/cron/isolated-agent/run.meta-error-status.test.ts index 50738a22345..a6b6fb8b980 100644 --- a/src/cron/isolated-agent/run.meta-error-status.test.ts +++ b/src/cron/isolated-agent/run.meta-error-status.test.ts @@ -2,10 +2,15 @@ import { describe, expect, it } from "vitest"; import { CommandLaneTaskTimeoutError } from "../../process/command-queue.js"; import { + makeIsolatedAgentTurnJob, makeIsolatedAgentTurnParams, setupRunCronIsolatedAgentTurnSuite, } from "./run.suite-helpers.js"; -import { loadRunCronIsolatedAgentTurn, runWithModelFallbackMock } from "./run.test-harness.js"; +import { + cleanupDirectCronSessionMock, + loadRunCronIsolatedAgentTurn, + runWithModelFallbackMock, +} from "./run.test-harness.js"; const runCronIsolatedAgentTurn = await loadRunCronIsolatedAgentTurn(); @@ -68,10 +73,20 @@ describe("runCronIsolatedAgentTurn - meta.error status propagation", () => { attempts: [], }); - const result = await runCronIsolatedAgentTurn(makeIsolatedAgentTurnParams()); + const result = await runCronIsolatedAgentTurn( + makeIsolatedAgentTurnParams({ + job: makeIsolatedAgentTurnJob({ deleteAfterRun: true }), + }), + ); expect(result.status).toBe("error"); expect(result.error).toBe("cron isolated agent run aborted"); + expect(cleanupDirectCronSessionMock).toHaveBeenCalledWith({ + job: expect.objectContaining({ deleteAfterRun: true }), + agentSessionKey: "agent:default:cron:test", + sessionId: "test-session-id", + retireReason: "cron-delete-after-run-aborted", + }); }); it("surfaces cron timeout result when the cron-nested lane watchdog fires", async () => { diff --git a/src/cron/isolated-agent/run.ts b/src/cron/isolated-agent/run.ts index 5ee43ba28b8..9e45e4046d8 100644 --- a/src/cron/isolated-agent/run.ts +++ b/src/cron/isolated-agent/run.ts @@ -1081,6 +1081,13 @@ async function finalizeCronRun(params: { if (finalRunResult.meta?.aborted === true && !cronPayloadOutcome.hasFatalErrorPayload) { const metaErrorMessage = normalizeOptionalString(finalRunResult.meta.error?.message); const error = metaErrorMessage ?? "cron isolated agent run aborted"; + const { cleanupDirectCronSession } = await loadCronDeliveryRuntime(); + await cleanupDirectCronSession({ + job: prepared.input.job, + agentSessionKey: prepared.agentSessionKey, + sessionId: prepared.currentRunSessionId(), + retireReason: "cron-delete-after-run-aborted", + }); return prepared.withRunSession({ status: "error", error,