From 8459a7364f0597f52264e2defcc259529ce86472 Mon Sep 17 00:00:00 2001 From: Rhys Sullivan <39114868+RhysSullivan@users.noreply.github.com> Date: Sun, 25 Oct 2026 14:13:10 -0700 Subject: [PATCH] Test a request landing after destroy wiped an MCP session's storage --- .../mcp/agent-session-durable-object.test.ts | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/packages/hosts/cloudflare/src/mcp/agent-session-durable-object.test.ts b/packages/hosts/cloudflare/src/mcp/agent-session-durable-object.test.ts index 813f4d5c40..75fee6cc11 100644 --- a/packages/hosts/cloudflare/src/mcp/agent-session-durable-object.test.ts +++ b/packages/hosts/cloudflare/src/mcp/agent-session-durable-object.test.ts @@ -168,6 +168,7 @@ type HarnessSession = { requestIds: ReadonlyArray, ) => Promise; dbHandle: { readonly end: () => void } | null; + destroy: () => Promise; engine: ExecutionEngine | null; getConnections?: () => Iterable; getSessionId: () => string; @@ -669,6 +670,46 @@ describe("McpAgentSessionDOBase transport restore", () => { ).resolves.toBe("ok"); }); + it("answers not_found, without restoring, for a request that lands after destroy wiped storage", async () => { + const session = await makeHarnessSession(); + let restores = 0; + session.runMcpAgentOnStart = async () => { + restores += 1; + session.initialized = true; + }; + // Pause the SDK's teardown right after its storage wipe: the condemned + // marker and the stored meta are gone, the isolate abort has not run yet, + // and requests still reach this instance. + const sdk = Object.getPrototypeOf(McpAgentSessionDOBase.prototype) as { + destroy: (this: HarnessSession) => Promise; + }; + const sdkDestroy = sdk.destroy; + const wiped = makeDeferred(); + const finishTeardown = makeDeferred(); + sdk.destroy = async function () { + await this.ctx.storage.deleteAll(); + wiped.resolve(); + await finishTeardown.promise; + }; + try { + const destroying = session.destroy(); + await wiped.promise; + + await expect( + session.validateMcpSessionOwner( + { accountId: "user-1", organizationId: "org-1" }, + defaultMcpResource, + ), + ).resolves.toBe("not_found"); + expect(restores).toBe(0); + + finishTeardown.resolve(); + await destroying; + } finally { + sdk.destroy = sdkDestroy; + } + }); + it("rejects the same owner on a different MCP resource", async () => { const session = await makeHarnessSession(); const identity = { accountId: "user-1", organizationId: "org-1" };