diff --git a/src/adapters/http/http-execution-v3.ts b/src/adapters/http/http-execution-v3.ts index 9bfaf7ec..47138898 100644 --- a/src/adapters/http/http-execution-v3.ts +++ b/src/adapters/http/http-execution-v3.ts @@ -504,6 +504,17 @@ export function createContractHttpExecutor( kind: HttpTransportFailure["kind"], ): HttpExecutionOutcome => transportFailure(kind, false, abandonedCertainty()); + /** + * Q-6.3-7. What the attempts that were retried settled on. The logical + * accumulator above is raised at dispatch, before the attempt's own answer + * is known, so it cannot tell a rejected attempt from a lost one. + */ + let retriedCertainty: MutationEffectCertainty = "NOT_STARTED"; + const rememberRetried = (settled: MutationEffectCertainty) => { + if (isCommand) { + retriedCertainty = joinMutationEffectCertainty(retriedCertainty, settled); + } + }; let terminalCancellation: CancellationOwner | null = null; const lifetimeController = new AbortController(); const forwardCallerToLifetime = () => { @@ -938,6 +949,7 @@ export function createContractHttpExecutor( lifetimeController.signal, ); if (slept === ABORTED) return terminalOutcomeFor("WAIT"); + rememberRetried(certaintyForAbandonedAttempt(attemptState, isCommand)); continue; } } @@ -982,6 +994,10 @@ export function createContractHttpExecutor( } if ( outcome.retryHint && + // Q-6.3-7. An answer that says the command may already be applied + // is not replayed; reconciliation owns it from here. + outcome.certainty !== "MAYBE_APPLIED" && + outcome.certainty !== "APPLIED_CONFIRMED" && retryIndex < retryCeiling && isRetryableSemantics(contract.retrySemantics) ) { @@ -998,10 +1014,27 @@ export function createContractHttpExecutor( lifetimeController.signal, ); if (slept === ABORTED) return terminalOutcomeFor("WAIT"); + rememberRetried(outcome.result.effect as MutationEffectCertainty); continue; } } - return finish(outcome.result, outcome.certainty); + // Q-6.3-7. A retry never reports less than an earlier attempt of the + // same command already established. + const effect = isCommand + ? joinMutationEffectCertainty( + retriedCertainty, + outcome.result.effect as MutationEffectCertainty, + ) + : outcome.result.effect; + return finish( + effect === outcome.result.effect + ? outcome.result + : (Object.freeze({ + ...outcome.result, + effect, + }) as HttpExecutionOutcome), + outcome.certainty, + ); } finally { clearTimeout(deadlineTimer); lifetimeController.signal.removeEventListener("abort", forwardLifetime); diff --git a/tests/unit/http-execution-v3.test.ts b/tests/unit/http-execution-v3.test.ts index 0072910b..660e68c0 100644 --- a/tests/unit/http-execution-v3.test.ts +++ b/tests/unit/http-execution-v3.test.ts @@ -991,3 +991,83 @@ describe("terminal owner classification at every wait", () => { }, ); }); + +/** + * Q-6.3-7. A retried command's final effect joins what earlier attempts already + * established, and an answer that says the command may already be applied is + * not replayed. + */ +describe("command effect across retries", () => { + const idempotentCommand: InstalledHttpContract = { + ...createInstalled, + contract: { + ...createInstalled.contract, + retrySemantics: "IDEMPOTENT" as const, + }, + frontend: { ...createInstalled.frontend, retryBudget: 1 as const }, + }; + + async function runThenReject(first: () => Promise) { + const fetcher = vi + .fn() + .mockImplementationOnce(first) + .mockImplementation(async () => + // The fixture classifier reads a 400 problem as NOT_APPLIED. + Response.json( + { type: "about:blank", title: "rejected", status: 400 }, + { status: 400 }, + ), + ); + const executor = createContractHttpExecutor({ + baseUrl: "https://api.example/", + maxRetryAttempts: 1, + attachCredentials: () => ({ kind: "READY", headers: {} }), + fetcher, + sleep: async () => {}, + random: () => 0, + }); + const outcome = await executor.execute( + idempotentCommand, + { name: "created" }, + { + routeId: ROUTE_ID, + scope, + intent: mutationIntent({ idempotencyKey: null }), + }, + ); + return { outcome, attempts: fetcher.mock.calls.length }; + } + + it("keeps MAYBE_APPLIED when the retry after a lost response is rejected", async () => { + await expect( + runThenReject(async () => { + throw new TypeError("connection reset"); + }), + ).resolves.toMatchObject({ + attempts: 2, + outcome: { kind: "PROBLEM", effect: "MAYBE_APPLIED" }, + }); + }); + + it("keeps MAYBE_APPLIED when the retry after a rate limit is rejected", async () => { + await expect( + runThenReject(async () => new Response(null, { status: 429 })), + ).resolves.toMatchObject({ + attempts: 2, + outcome: { kind: "PROBLEM", effect: "MAYBE_APPLIED" }, + }); + }); + + it("does not replay a command whose gateway failure may already be applied", async () => { + await expect( + runThenReject(async () => new Response("", { status: 504 })), + ).resolves.toMatchObject({ + attempts: 1, + outcome: { + kind: "CONTRACT_VIOLATION", + metadata: { status: 504 }, + effect: "MAYBE_APPLIED", + }, + }); + }); +});