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
@@ -1107,12 +1107,11 @@ async function completeUpload<Capability>(
{ recovery: "RECONCILE" },
);
}
const removed = await removeCheckpoint(
dependencies,
checkpoint,
request.signal,
);
if (!removed.ok) return removed;
// 01-02. The server has committed the resource. A failed local cleanup (an
// IndexedDB error, or the caller aborting right after complete) must not
// turn that into a failure and drop the resourceId. The leftover row is
// reconciled to this same resource by the next upload() or abort().
await removeCheckpoint(dependencies, checkpoint, request.signal);
return browserDataSuccess(
quarantinedOutcome(value.resourceId, sessionFromCheckpoint(checkpoint), false),
);
@@ -1213,6 +1212,16 @@ async function executeAbort<Capability>(
recovery: "RECONCILE",
});
}
let resourceId: string | undefined;
if (aborted.value.state === "ALREADY_COMPLETED") {
// 01-02. The checkpoint is the last local link to a completed resource
// (typically a complete whose response was lost), so its id is read before
// the row goes. A transient status failure keeps the ABORT_PENDING row for
// the next abort(); a definitive one has nothing left to read.
const completed = await readCompletedResourceId(dependencies, checkpoint, signal);
if (!completed.ok) return completed;
resourceId = completed.value ?? undefined;
}
const removed = await dependencies.checkpoints.remove({
uploadKey,
expectedRevision: checkpoint.revision,
@@ -1225,7 +1234,46 @@ async function executeAbort<Capability>(
: aborted.value.state === "ALREADY_COMPLETED"
? "ALREADY_COMPLETED"
: "ORPHANED";
return browserDataSuccess(Object.freeze({ state }));
return browserDataSuccess(
Object.freeze(resourceId ? { state, resourceId } : { state }),
);
}
async function readCompletedResourceId<Capability>(
dependencies: RuntimeDependencies<Capability>,
checkpoint: ResumableUploadCheckpoint,
signal: AbortSignal,
): Promise<BrowserDataResult<string | null>> {
const status = await callWithRetry(
dependencies,
"UPLOAD_RECONCILE",
signal,
async (attemptSignal) =>
await dependencies.controlPlane.getStatus({
protocol: RESUMABLE_UPLOAD_PROTOCOL,
sessionId: checkpoint.sessionId,
requestBindingSha256: checkpoint.requestBindingSha256,
fingerprint: checkpoint.fingerprint,
signal: attemptSignal,
}),
);
if (!status.ok) {
return status.error.code === "NOT_FOUND" ||
status.error.code === "EXPIRED_RESOURCE"
? browserDataSuccess(null)
: remapResult(stripProviderFailure(status), "UPLOAD_ABORT");
}
const value = status.value;
return browserDataSuccess(
value?.state === "QUARANTINED" &&
value.session?.sessionId === checkpoint.sessionId &&
value.session.requestBindingSha256 ===
checkpoint.requestBindingSha256 &&
typeof value.resourceId === "string" &&
SAFE_OPAQUE_ID.test(value.resourceId)
? value.resourceId
: null,
);
}
/** Drops the durable checkpoint a finished or dead session no longer needs. */
@@ -92,6 +92,12 @@ export type QuarantinedUpload = Readonly<{
export type UploadAbortOutcome = Readonly<{
state: "ABORTED" | "ORPHANED" | "ALREADY_COMPLETED" | "NOT_FOUND";
/**
* 01-02. `ALREADY_COMPLETED` only, when the server status still names the
* quarantined resource the session completed (a complete whose response was
* lost). Absent when the server no longer knows the session.
*/
resourceId?: string;
}>;
export interface ResumableUploadPort {
@@ -194,6 +200,15 @@ export interface ResumableUploadControlPlane<Capability> {
signal: AbortSignal;
}>): Promise<UploadProviderResult<UploadPartCapability<Capability>>>;
/**
* 01-02. In-progress contract. While a complete with the same idempotency
* key is still running, the server answers `429` with a `Retry-After` within
* the client's `maxRetryAfterMs`, which the client retries with the same key
* (`503` is retried too, without reading `Retry-After`). It never answers
* `409`: the client reads that as a final `CONFLICT`. Until the resource is
* committed `getStatus` stays `ACTIVE` with every part accepted; there is no
* "completing" status value.
*/
complete(input: Readonly<{
protocol: ResumableUploadProtocol;
sessionId: string;