From 8f65eabadd81ed32eda963d0118849633ace60e8 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Fri, 25 Sep 2026 22:31:41 +0900 Subject: [PATCH] fix(browser-transfer): check policy ceilings only when a session is created (01-08) Resume re-ran the current policy against the stored session, so a build that lowered maxConcurrency or maxSessionLifetimeMs failed every upload an older build had started with INTEGRITY_FAILED. Resume now checks the session only against its checkpoint. Co-Authored-By: Claude Opus 5.5 --- .../presigned-transfer-and-image-cdn.md | 6 +++ .../upload-response-validation.ts | 18 ++++--- .../resumable-upload-runtime-recovery.test.ts | 50 +++++++++++++++++++ 3 files changed, 68 insertions(+), 6 deletions(-) diff --git a/docs/architecture/presigned-transfer-and-image-cdn.md b/docs/architecture/presigned-transfer-and-image-cdn.md index cb69078e..014d9154 100644 --- a/docs/architecture/presigned-transfer-and-image-cdn.md +++ b/docs/architecture/presigned-transfer-and-image-cdn.md @@ -323,6 +323,12 @@ resume 시 checkpoint만 신뢰하지 않는다. 전환한다. 5. 새 part만 업로드한 뒤 전체 ordered set을 다시 reconcile한다. +재개 때 서버 session은 저장된 checkpoint 값(session ID, 동시성, 만료 시각)과 +일치하는지만 본다. 정책 상한(`maxConcurrency`, `maxSessionLifetimeMs`)은 새 +session을 만들 때만 검사한다. 그래서 정책을 낮춘 빌드도 이전 빌드가 시작한 +업로드를 이어 간다. worker 수는 `min(현재 정책, session 동시성)`이다 +(`upload-response-validation.ts` `validateSession`의 `stage`). + `GET_STATUS`가 HTTP `404` 또는 `410`을 반환하거나 decoded status가 `NOT_FOUND`/`EXPIRED`이면 해당 session은 terminal이다. runtime은 CAS revision을 확인해 checkpoint를 제거하고 새 session으로 restart하거나 diff --git a/src/adapters/browser-transfer/resumable-upload/upload-response-validation.ts b/src/adapters/browser-transfer/resumable-upload/upload-response-validation.ts index 50651092..5e880712 100644 --- a/src/adapters/browser-transfer/resumable-upload/upload-response-validation.ts +++ b/src/adapters/browser-transfer/resumable-upload/upload-response-validation.ts @@ -98,7 +98,7 @@ export function validateStatus( checkpoint.requestBindingSha256, manifest.fingerprint, dependencies, - true, + "ACTIVE", ); if ( !session.ok || @@ -140,7 +140,7 @@ export function validateStatus( checkpoint.requestBindingSha256, manifest.fingerprint, dependencies, - false, + "COMPLETED", ); if ( !session.ok || @@ -190,12 +190,17 @@ export function validateStatus( ); } +/** + * 01-08. Policy ceilings (concurrency, lifetime) apply only to a session being + * created. A stored session is checked against its checkpoint by the caller, + * so a build that lowers the policy still resumes uploads an older build began. + */ export function validateSession( value: UploadSession, requestBindingSha256: string, fingerprint: UploadFileFingerprint, dependencies: RuntimeDependencies, - requireActiveExpiry = true, + stage: "CREATE" | "ACTIVE" | "COMPLETED" = "CREATE", ): BrowserDataResult { if ( !exactRecord(value, [ @@ -218,7 +223,8 @@ export function validateSession( value.partCount !== fingerprint.partCount || !Number.isSafeInteger(value.maxConcurrency) || value.maxConcurrency < 1 || - value.maxConcurrency > dependencies.policy.maxConcurrency || + (stage === "CREATE" && + value.maxConcurrency > dependencies.policy.maxConcurrency) || !Number.isSafeInteger(value.expiresAtEpochMs) ) { // 01-05. A create is replayed under the same key, so neither calling @@ -230,14 +236,14 @@ export function validateSession( const now = safeNow(dependencies, "UPLOAD_SESSION"); if (!now.ok) return now; if ( - requireActiveExpiry && + stage === "CREATE" && value.expiresAtEpochMs - now.value > dependencies.policy.maxSessionLifetimeMs + dependencies.policy.maxClockSkewMs ) { return browserDataFailure("POLICY_REJECTED", "UPLOAD_SESSION"); } - if (requireActiveExpiry && value.expiresAtEpochMs <= now.value) { + if (stage !== "COMPLETED" && value.expiresAtEpochMs <= now.value) { return browserDataFailure("EXPIRED_RESOURCE", "UPLOAD_SESSION", { recovery: "RESTART", }); diff --git a/tests/unit/resumable-upload-runtime-recovery.test.ts b/tests/unit/resumable-upload-runtime-recovery.test.ts index 22e7a8e3..effb5181 100644 --- a/tests/unit/resumable-upload-runtime-recovery.test.ts +++ b/tests/unit/resumable-upload-runtime-recovery.test.ts @@ -924,3 +924,53 @@ describe("device clock skew on server lifetimes (01-06)", () => { }, ); }); + +describe("resume after a policy change (01-08)", () => { + it("resumes a concurrency-3 checkpoint with a runtime lowered to 1", async () => { + const harness = createControlHarness({ serverMaxConcurrency: 3 }); + const checkpoints = new MemoryCheckpointStore(); + const source = () => rangeSource(new Uint8Array(12)); + const key = "upload_key_policy_lowered"; + const partial = await createResumableUploadRuntime({ + controlPlane: harness.control, + partExecutor: { + async uploadPart(input) { + if (input.part.partNumber === 2) { + return browserDataFailure("UNAVAILABLE", "UPLOAD_PART", { + recovery: "RESUME", + }); + } + return await executorFor(harness).uploadPart(input); + }, + }, + checkpoints, + mutationLock: noContentionLock, + crypto, + policy: runtimePolicy({ maxRetries: 0, maxConcurrency: 3 }), + now: () => 1_000, + sleep: async () => {}, + }).upload({ ...request(key), source: source() }); + expect(partial.ok).toBe(false); + expect(checkpoints.rows.get(key)?.sessionMaxConcurrency).toBe(3); + + const lowered = createResumableUploadRuntime({ + controlPlane: harness.control, + partExecutor: executorFor(harness), + checkpoints, + mutationLock: noContentionLock, + crypto, + // A later build lowers both limits the stored session was made under. + policy: runtimePolicy({ + maxRetries: 0, + maxConcurrency: 1, + maxSessionLifetimeMs: 1_000, + }), + now: () => 1_000, + sleep: async () => {}, + }); + const resumed = await lowered.upload({ ...request(key), source: source() }); + + expect(resumed).toMatchObject({ ok: true, value: { replayed: false } }); + expect(checkpoints.rows.size).toBe(0); + }); +});