From c7052ef09aa06113f1d7b2805a5bd428e52a63f7 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 24 Sep 2026 13:14:25 +0900 Subject: [PATCH] fix(http): join a retried command's effect and never replay MAYBE_APPLIED (Q-6.3-7) A retried command reported only its last attempt's effect. An IDEMPOTENT command whose first attempt was lost (network error or 429) and whose retry got a NOT_APPLIED problem ended as "not applied, retry", although the first attempt may have applied. A 5xx with an HTML or empty body is MAYBE_APPLIED for a command, yet the executor still replayed it. The executor now records what each retried attempt settled on and joins it into the final outcome's effect. An admission whose own certainty is MAYBE_APPLIED (or APPLIED_CONFIRMED) is not retried; a 429 still is, because its admission reason is RATE_LIMITED rather than an effect verdict. The logical accumulator is not reused because it is raised at dispatch and cannot tell a rejected attempt from a lost one. Verified: three new tests (lost response then 400, 429 then 400, 504 empty body) failed RED with NOT_APPLIED / a second attempt and now pass. All lane gates pass except the known removal-fixture test; two component tests timed out once under load and passed on rerun. Co-Authored-By: Claude Opus 5.5 --- src/adapters/http/http-execution-v3.ts | 35 ++++++++++- tests/unit/http-execution-v3.test.ts | 80 ++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 1 deletion(-) 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", + }, + }); + }); +});