fix(browser-transfer): keep the resourceId of a completed or lost-complete upload (01-02)

A failed checkpoint cleanup after a committed complete returned a failure and
dropped the resourceId, and abort() on ALREADY_COMPLETED returned no id. The
upload now succeeds, abort() reads the id from status first, and the port and
design doc state the in-progress complete contract (429 + Retry-After).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-09-25 22:11:04 +09:00
co-authored by Claude Opus 5.5
parent 5cc1d002d2
commit 7b2cefbd32
4 changed files with 308 additions and 7 deletions
@@ -398,3 +398,204 @@ describe("resumable upload idempotency keys (01-04)", () => {
);
});
});
describe("resumable upload completed-result reporting (01-02)", () => {
/** A server whose complete commits the resource, reported by getStatus. */
function committingHarness(
completeResult: "SUCCEEDS" | "LOST",
) {
const harness = createControlHarness();
const original = harness.control.complete.bind(harness.control);
let committed = false;
harness.control.complete = async (input) => {
committed = true;
return completeResult === "SUCCEEDS"
? await original(input)
: browserDataFailure("UNAVAILABLE", "UPLOAD_COMPLETE", {
retryable: true,
recovery: "RESUME",
});
};
harness.control.getStatus = async () => {
const session = harness.getSession();
if (!session) return browserDataFailure("NOT_FOUND", "UPLOAD_RECONCILE");
return committed
? browserDataSuccess({
state: "QUARANTINED",
session,
resourceId: "resource_01",
})
: browserDataSuccess({
state: "ACTIVE",
session,
acceptedParts: [...harness.accepted.values()],
});
};
harness.control.abort = async () =>
browserDataSuccess({
state: committed ? "ALREADY_COMPLETED" : "ABORTED",
});
return harness;
}
it("reports a completed upload even when the local checkpoint cleanup fails", async () => {
const harness = committingHarness("SUCCEEDS");
const checkpoints = new MemoryCheckpointStore();
const remove = checkpoints.remove.bind(checkpoints);
let failRemove = true;
checkpoints.remove = async (input) =>
failRemove
? browserDataFailure("UNAVAILABLE", "UPLOAD_RECONCILE", {
retryable: true,
recovery: "RESUME",
})
: await remove(input);
const runtime = createResumableUploadRuntime({
controlPlane: harness.control,
partExecutor: executorFor(harness),
checkpoints,
mutationLock: noContentionLock,
crypto,
policy: runtimePolicy({ maxRetries: 0 }),
now: () => 1_000,
sleep: async () => {},
});
const key = "upload_key_cleanup_fails";
const completed = await runtime.upload(request(key));
expect(completed).toEqual({
ok: true,
value: {
state: "QUARANTINED",
resourceId: "resource_01",
byteLength: 4,
replayed: false,
},
});
// The stale row is the next call's to reconcile, not a reason to fail.
expect(checkpoints.rows.has(key)).toBe(true);
failRemove = false;
const replayed = await runtime.upload(request(key));
expect(replayed).toMatchObject({
ok: true,
value: { resourceId: "resource_01", replayed: true },
});
expect(checkpoints.rows.has(key)).toBe(false);
});
it("returns the resourceId when abort() finds the lost complete already applied", async () => {
const harness = committingHarness("LOST");
const checkpoints = new MemoryCheckpointStore();
const runtime = createResumableUploadRuntime({
controlPlane: harness.control,
partExecutor: executorFor(harness),
checkpoints,
mutationLock: noContentionLock,
crypto,
policy: runtimePolicy({ maxRetries: 3 }),
now: () => 1_000,
sleep: async () => {},
});
const key = "upload_key_x2_lost_complete";
const lost = await runtime.upload(request(key));
expect(lost).toMatchObject({
ok: false,
error: { code: "UNAVAILABLE", operation: "UPLOAD_COMPLETE" },
});
const aborted = await runtime.abort({ uploadKey: key, signal: activeSignal });
expect(aborted).toEqual({
ok: true,
value: { state: "ALREADY_COMPLETED", resourceId: "resource_01" },
});
expect(checkpoints.rows.size).toBe(0);
});
it("waits out an in-progress complete answered with 429 Retry-After", async () => {
// The port's in-progress contract, as the fetch transport decodes it.
const harness = createControlHarness();
const original = harness.control.complete.bind(harness.control);
const complete = vi.fn(
async (input: Parameters<typeof original>[0]): ReturnType<typeof original> =>
complete.mock.calls.length <= 2
? Object.freeze({
ok: false as const,
error: Object.freeze({
code: "UNAVAILABLE" as const,
operation: "UPLOAD_COMPLETE" as const,
retryable: true,
recovery: "RESUME" as const,
retryAfterMs: 40,
}),
})
: await original(input),
);
harness.control.complete = complete;
const delays: number[] = [];
const runtime = createResumableUploadRuntime({
controlPlane: harness.control,
partExecutor: executorFor(harness),
checkpoints: new MemoryCheckpointStore(),
mutationLock: noContentionLock,
crypto,
policy: runtimePolicy({ maxRetries: 3 }),
now: () => 1_000,
random: () => 0,
sleep: async (delay) => {
delays.push(delay);
},
});
const result = await runtime.upload(request("upload_key_complete_in_progress"));
expect(result).toMatchObject({ ok: true, value: { replayed: false } });
expect(delays).toEqual([40, 40]);
expect(
new Set(complete.mock.calls.map(([input]) => input.idempotencyKey)).size,
).toBe(1);
});
it("keeps the checkpoint when the completed resource cannot be read yet", async () => {
const harness = committingHarness("LOST");
const status = harness.control.getStatus.bind(harness.control);
let statusDown = false;
harness.control.getStatus = async (input) =>
statusDown
? browserDataFailure("UNAVAILABLE", "UPLOAD_RECONCILE", {
retryable: true,
recovery: "RESUME",
})
: await status(input);
const checkpoints = new MemoryCheckpointStore();
const runtime = createResumableUploadRuntime({
controlPlane: harness.control,
partExecutor: executorFor(harness),
checkpoints,
mutationLock: noContentionLock,
crypto,
policy: runtimePolicy({ maxRetries: 0 }),
now: () => 1_000,
sleep: async () => {},
});
const key = "upload_key_status_unavailable";
await runtime.upload(request(key));
statusDown = true;
const failed = await runtime.abort({ uploadKey: key, signal: activeSignal });
expect(failed).toMatchObject({
ok: false,
error: { code: "UNAVAILABLE", operation: "UPLOAD_ABORT", retryable: true },
});
expect(checkpoints.rows.get(key)?.state).toBe("ABORT_PENDING");
statusDown = false;
const retried = await runtime.abort({ uploadKey: key, signal: activeSignal });
expect(retried).toEqual({
ok: true,
value: { state: "ALREADY_COMPLETED", resourceId: "resource_01" },
});
expect(checkpoints.rows.size).toBe(0);
});
});