|
| 1 | +// --------------------------------------------------------------------------- |
| 2 | +// Regression coverage for the transient DO-relocation retry in the worker-side |
| 3 | +// session-store dispatcher. |
| 4 | +// |
| 5 | +// Cloudflare may relocate a live Durable Object between machines; an in-flight |
| 6 | +// `init` then throws "cannot access storage because object has moved to a |
| 7 | +// different machine". Before the retry, that rejection became an unrecoverable |
| 8 | +// defect the envelope rendered as a -32603 — exactly the prod incident on |
| 9 | +// 2026-06-15 (one `mcp.do.init` failure a reconnect cleared). These tests inject |
| 10 | +// that rejection at the `McpSessionDOStub` seam and pin the recovery behavior. |
| 11 | +// --------------------------------------------------------------------------- |
| 12 | + |
| 13 | +import { describe, expect, it } from "@effect/vitest"; |
| 14 | +import { Cause, Effect, Exit } from "effect"; |
| 15 | + |
| 16 | +import { McpSessionStore, type McpDispatchResult, type Principal } from "@executor-js/host-mcp"; |
| 17 | + |
| 18 | +import { |
| 19 | + DO_RELOCATION_MAX_RETRIES, |
| 20 | + makeDurableObjectMcpSessionStore, |
| 21 | + type McpSessionDOStub, |
| 22 | +} from "./session-store"; |
| 23 | + |
| 24 | +const RELOCATION_ERROR = "cannot access storage because object has moved to a different machine"; |
| 25 | + |
| 26 | +const TEST_PRINCIPAL: Principal = { |
| 27 | + accountId: "user_test", |
| 28 | + organizationId: "org_test", |
| 29 | + organizationName: "Test Org", |
| 30 | + email: "test@example.com", |
| 31 | + name: "Test", |
| 32 | + avatarUrl: null, |
| 33 | + roles: ["user"], |
| 34 | +}; |
| 35 | + |
| 36 | +/** A POST with no session id — routes dispatch through the create/init path. */ |
| 37 | +const initializeRequest = (): Request => |
| 38 | + new Request("https://mcp.test/mcp", { |
| 39 | + method: "POST", |
| 40 | + headers: { authorization: "Bearer x", "content-type": "application/json" }, |
| 41 | + body: JSON.stringify({ jsonrpc: "2.0", id: 0, method: "initialize" }), |
| 42 | + }); |
| 43 | + |
| 44 | +/** The JSON-RPC body the faked DO returns once `init` succeeds. */ |
| 45 | +const okResponse = (): Response => |
| 46 | + new Response(JSON.stringify({ jsonrpc: "2.0", id: 0, result: {} }), { |
| 47 | + status: 200, |
| 48 | + headers: { "content-type": "application/json" }, |
| 49 | + }); |
| 50 | + |
| 51 | +/** Drive `dispatch` for a create over a store built from one fake stub. */ |
| 52 | +const dispatchCreate = (stub: McpSessionDOStub): Effect.Effect<McpDispatchResult> => |
| 53 | + Effect.gen(function* () { |
| 54 | + const store = yield* McpSessionStore; |
| 55 | + return yield* store.dispatch({ |
| 56 | + request: initializeRequest(), |
| 57 | + principal: TEST_PRINCIPAL, |
| 58 | + sessionId: null, |
| 59 | + method: "POST", |
| 60 | + }); |
| 61 | + }).pipe( |
| 62 | + Effect.provide(makeDurableObjectMcpSessionStore({ newStub: () => stub, getStub: () => stub })), |
| 63 | + ); |
| 64 | + |
| 65 | +/** Rendered cause of a failed dispatch (empty for a success) — keeps the |
| 66 | + * message assertion unconditional, off the `Exit.isFailure` branch. */ |
| 67 | +const failureText = (exit: Exit.Exit<McpDispatchResult>): string => |
| 68 | + Exit.isFailure(exit) ? Cause.pretty(exit.cause) : ""; |
| 69 | + |
| 70 | +describe("makeDurableObjectMcpSessionStore — DO-relocation retry", () => { |
| 71 | + it.live("retries mcp.do.init past a relocation, then returns the DO response", () => |
| 72 | + Effect.gen(function* () { |
| 73 | + let initCalls = 0; |
| 74 | + let handleCalls = 0; |
| 75 | + const stub: McpSessionDOStub = { |
| 76 | + init: () => { |
| 77 | + initCalls += 1; |
| 78 | + if (initCalls === 1) { |
| 79 | + // oxlint-disable-next-line executor/no-promise-reject, executor/no-error-constructor -- boundary: fake DO stub rejects to simulate a Cloudflare relocation throw |
| 80 | + return Promise.reject(new Error(RELOCATION_ERROR)); |
| 81 | + } |
| 82 | + return Promise.resolve(); |
| 83 | + }, |
| 84 | + handleRequest: () => { |
| 85 | + handleCalls += 1; |
| 86 | + return Promise.resolve(okResponse()); |
| 87 | + }, |
| 88 | + clearSession: () => Promise.resolve(), |
| 89 | + }; |
| 90 | + |
| 91 | + const result = yield* dispatchCreate(stub); |
| 92 | + |
| 93 | + expect(initCalls, "init retried once after the relocation").toBe(2); |
| 94 | + expect(handleCalls, "handleRequest runs once, after init recovers").toBe(1); |
| 95 | + expect(result).toBeInstanceOf(Response); |
| 96 | + expect((result as Response).status).toBe(200); |
| 97 | + }), |
| 98 | + ); |
| 99 | + |
| 100 | + it.live("gives up after the retry budget when relocation never clears", () => |
| 101 | + Effect.gen(function* () { |
| 102 | + let initCalls = 0; |
| 103 | + let handleCalls = 0; |
| 104 | + const stub: McpSessionDOStub = { |
| 105 | + init: () => { |
| 106 | + initCalls += 1; |
| 107 | + // oxlint-disable-next-line executor/no-promise-reject, executor/no-error-constructor -- boundary: fake DO stub rejects to simulate a Cloudflare relocation throw |
| 108 | + return Promise.reject(new Error(RELOCATION_ERROR)); |
| 109 | + }, |
| 110 | + handleRequest: () => { |
| 111 | + handleCalls += 1; |
| 112 | + return Promise.resolve(okResponse()); |
| 113 | + }, |
| 114 | + clearSession: () => Promise.resolve(), |
| 115 | + }; |
| 116 | + |
| 117 | + const exit = yield* Effect.exit(dispatchCreate(stub)); |
| 118 | + |
| 119 | + expect(Exit.isFailure(exit), "exhausted retries surface as a defect").toBe(true); |
| 120 | + expect(initCalls, "one initial attempt plus the full retry budget").toBe( |
| 121 | + 1 + DO_RELOCATION_MAX_RETRIES, |
| 122 | + ); |
| 123 | + expect(handleCalls, "handleRequest is never reached when init never succeeds").toBe(0); |
| 124 | + expect(failureText(exit), "the relocation cause is preserved").toContain(RELOCATION_ERROR); |
| 125 | + }), |
| 126 | + ); |
| 127 | + |
| 128 | + it.live("does not retry a non-relocation failure — surfaces it immediately", () => |
| 129 | + Effect.gen(function* () { |
| 130 | + let initCalls = 0; |
| 131 | + const stub: McpSessionDOStub = { |
| 132 | + init: () => { |
| 133 | + initCalls += 1; |
| 134 | + // oxlint-disable-next-line executor/no-promise-reject, executor/no-error-constructor -- boundary: fake DO stub rejects to simulate a non-transient init failure |
| 135 | + return Promise.reject(new Error("schema spread bug — not transient")); |
| 136 | + }, |
| 137 | + handleRequest: () => Promise.resolve(okResponse()), |
| 138 | + clearSession: () => Promise.resolve(), |
| 139 | + }; |
| 140 | + |
| 141 | + const exit = yield* Effect.exit(dispatchCreate(stub)); |
| 142 | + |
| 143 | + expect(Exit.isFailure(exit), "a non-transient rejection still dies").toBe(true); |
| 144 | + expect(initCalls, "non-relocation errors are not retried").toBe(1); |
| 145 | + expect(failureText(exit), "the original failure is preserved").toContain("schema spread bug"); |
| 146 | + }), |
| 147 | + ); |
| 148 | +}); |
0 commit comments