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 <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-09-25 22:31:41 +09:00
co-authored by Claude Opus 5.5
parent b52ca8d69b
commit 8f65eabadd
3 changed files with 68 additions and 6 deletions
@@ -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하거나
@@ -98,7 +98,7 @@ export function validateStatus<Capability>(
checkpoint.requestBindingSha256,
manifest.fingerprint,
dependencies,
true,
"ACTIVE",
);
if (
!session.ok ||
@@ -140,7 +140,7 @@ export function validateStatus<Capability>(
checkpoint.requestBindingSha256,
manifest.fingerprint,
dependencies,
false,
"COMPLETED",
);
if (
!session.ok ||
@@ -190,12 +190,17 @@ export function validateStatus<Capability>(
);
}
/**
* 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<Capability>(
value: UploadSession,
requestBindingSha256: string,
fingerprint: UploadFileFingerprint,
dependencies: RuntimeDependencies<Capability>,
requireActiveExpiry = true,
stage: "CREATE" | "ACTIVE" | "COMPLETED" = "CREATE",
): BrowserDataResult<UploadSession> {
if (
!exactRecord(value, [
@@ -218,7 +223,8 @@ export function validateSession<Capability>(
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<Capability>(
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",
});
@@ -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);
});
});