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 <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-09-24 13:41:45 +09:00
co-authored by Claude Opus 5.5
parent 4c7737361a
commit c7052ef09a
2 changed files with 114 additions and 1 deletions
+34 -1
View File
@@ -504,6 +504,17 @@ export function createContractHttpExecutor(
kind: HttpTransportFailure["kind"],
): HttpExecutionOutcome<WireOutput, Problem> =>
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<WireOutput, Problem>),
outcome.certainty,
);
} finally {
clearTimeout(deadlineTimer);
lifetimeController.signal.removeEventListener("abort", forwardLifetime);
+80
View File
@@ -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<unknown, unknown, unknown> = {
...createInstalled,
contract: {
...createInstalled.contract,
retrySemantics: "IDEMPOTENT" as const,
},
frontend: { ...createInstalled.frontend, retryBudget: 1 as const },
};
async function runThenReject(first: () => Promise<Response>) {
const fetcher = vi
.fn<typeof fetch>()
.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",
},
});
});
});