From 7395c78c3a8f9e7d4ada95fe8dae9e9911cf03a0 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Fri, 6 Nov 2026 05:18:58 +0900 Subject: [PATCH 1/2] =?UTF-8?q?fix(http):=20202=20REJECTED=20=EC=97=85?= =?UTF-8?q?=EB=A1=9C=EB=93=9C=20=EC=8B=A4=ED=8C=A8=EC=97=90=20=EC=9D=91?= =?UTF-8?q?=EB=8B=B5=EC=9D=98=20traceId=EB=A5=BC=20=EC=8B=A3=EB=8A=94?= =?UTF-8?q?=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 문제: 짝 백엔드 Fileserver는 검증기가 거절한 업로드를 202 성공 Envelope(state REJECTED)로 답한다. client는 이 답을 FILE_REJECTED 실패로 바꾸는데, readSuccessEnvelope가 meta.traceId를 버려서 실패에 traceId가 없다. 같은 client의 Envelope 실패는 traceId를 싣는다 (1105/01-03). 서버가 정상으로 답해 trace 값이 손에 있는데도, 지원 담당이 이 거절을 서버 로그나 REJECTED 레코드와 잇지 못한다. 고른 방법: readSuccessEnvelope가 readFailureEnvelope처럼 meta.traceId가 있으면 traceId로 함께 돌려준다. Fileserver client는 202 REJECTED 실패에 그 값을 싣는다. V3 실행기는 value와 page만 읽으므로 동작이 같다. 매뉴얼 7절 결과 읽기 줄에 적었다. 버린 대안: fileserver-upload가 successEnvelopeSchema를 직접 파싱하면 Envelope 읽기가 backend-envelope.ts 밖으로 갈라진다. traceId만 읽는 함수를 따로 두면 같은 답을 두 번 파싱한다. 반환 칸이 늘어 readSuccessEnvelope 결과를 toEqual로 보는 테스트 두 곳(tests/runtime-schema/http-schema.test.ts, tests/unit/a11y-api-answers.test.ts)의 기대값에 traceId를 더했다. 기대를 넓힌 것이 아니라 새 칸의 값까지 고정한 것이다. 위험·한계: 거절 이유는 여전히 없다. 프론트로는 고칠 수 없어 백엔드로 넘긴다. - 사실(백엔드 45d2a478): FinalizeUploadApplicationUseCase #applyNonAcceptVerdict는 REJECT 판정의 code(FILE_TOO_LARGE, DIGEST_MALFORMED, 제품 검증기 code 등)를 FileRecordMutation.failure로 레코드의 lastErrorCode에만 쓴다. FileUploadController#created는 UploadedFileResponse 7칸(fileId, state, filename, mediaType, size, sha256, etag)을 202로 답한다. 이유 칸이 없다. - 요청: REJECT를 실패 Envelope(422, error.code = 판정 code, retryable false)로 답하거나, 202 REJECTED 응답에 판정 code 칸을 더한다. 앞의 것이면 프론트의 기존 422 경로가 code와 traceId를 그대로 싣고 NOT_APPLIED로 읽는다. 그때 FILE_REJECTED 분기는 지운다. - CHANGELOG의 "백엔드로 넘김" 목록에 이 항목을 더하는 일은 CHANGELOG 담당에게 남긴다. 실제 배포 응답의 성공 meta에 traceId가 있는지는 백엔드 문서 (docs/web/http-contract.md §1 meta 키)와 EnvelopeBodyAdvice 코드로만 확인했다. backend-envelope.ts는 초기 번들에 있어 초기 JS가 수십 B 늘 수 있다. 검증: desktop batch에서 확인. node scripts/run-vitest.ts run tests/integration/fileserver-upload.test.ts tests/runtime-schema/http-schema.test.ts tests/unit/a11y-api-answers.test.ts RED(이 커밋의 테스트 + 부모 src): "reads 202 REJECTED as a refused file, not as a broken answer"가 traceId "trace-1"이 없어 실패한다. "reads data, and a dropped data key as null"과 "answers an Envelope list read with an empty list inside the Envelope"는 결과에 traceId 칸이 없어 실패한다. Refs: 1106/01-01 --- docs/manual/02-decision-tables.md | 2 +- src/adapters/http/backend-envelope.ts | 15 +++++++++++++-- src/adapters/http/fileserver-upload.ts | 7 +++++++ tests/integration/fileserver-upload.test.ts | 3 +++ tests/runtime-schema/http-schema.test.ts | 3 ++- tests/unit/a11y-api-answers.test.ts | 7 ++++++- 6 files changed, 32 insertions(+), 5 deletions(-) diff --git a/docs/manual/02-decision-tables.md b/docs/manual/02-decision-tables.md index a63a822e..654f4743 100644 --- a/docs/manual/02-decision-tables.md +++ b/docs/manual/02-decision-tables.md @@ -324,7 +324,7 @@ context는 URL 앞부분 하나(라우트 루트)를 가진다. 그 아래 기 - 멱등성 응답(`IDEMPOTENT_*`, `IDEMPOTENCY_*`)과 V3 표에 있는 web 공통 응답은 V3 실행기와 같은 표(`backend-envelope.ts`, 4절)로 판정한다. 400 `IDEMPOTENCY_KEY_INVALID`·422 `IDEMPOTENT_REQUEST_MISMATCH`는 `NOT_APPLIED`, 409 `IDEMPOTENT_IN_FLIGHT`·503 `IDEMPOTENCY_UNAVAILABLE`·503 `IDEMPOTENCY_COMPLETION_UNKNOWN`·500 `IDEMPOTENCY_RECOVERY_REQUIRED`는 `MAYBE_APPLIED`다. - `APPLIED_CONFIRMED`: 서버는 만들었다고 답했지만 응답을 읽지 못했다. 다시 올리지 않는다. - `code`는 백엔드 code 그대로다(`FILE_TOO_LARGE`, `UNSUPPORTED_MEDIA_TYPE`, `MALWARE_DETECTED` …). 백엔드가 code를 주지 않는 거절은 client가 이름을 붙인다(`FILE_REJECTED`, `FILE_EMPTY`, `FILE_NOT_READABLE`). 화면 문구는 기능이 code로 고른다. -- 실패 Envelope의 `meta.traceId`는 실패의 `traceId`로 온다. V3 실행기와 같다. 지원 담당이 서버 로그에서 그 요청을 찾을 때 쓴다. +- 실패 Envelope의 `meta.traceId`는 실패의 `traceId`로 온다. V3 실행기와 같다. 지원 담당이 서버 로그에서 그 요청을 찾을 때 쓴다. 202 `REJECTED`의 `FILE_REJECTED`도 성공 Envelope의 `meta.traceId`를 싣는다. 이 응답에는 거절 이유가 없다. 짝 백엔드는 검증기 code를 파일 레코드에만 적는다. 그래서 화면은 이유별 문구를 고를 수 없다. - client는 스스로 다시 보내지 않는다. `retryable: true`이고 `retryAfterMs`가 있으면 그만큼 기다린 뒤 기능이 다시 부른다. 서버가 상한(`maxRetryAfterMs`, 기본 30초)보다 길게 기다리라고 하면 `retryable: false`다. - `MAYBE_APPLIED`는 언제나 `retryable: false`다. 백엔드가 Envelope에 `retryable: true`와 `Retry-After`를 붙여도(504 `TRANSFER_TIMEOUT`) 기능이 자동으로 다시 부르지 않는다. 아래 주의대로 먼저 확인한다. diff --git a/src/adapters/http/backend-envelope.ts b/src/adapters/http/backend-envelope.ts index b9e0c971..990277a5 100644 --- a/src/adapters/http/backend-envelope.ts +++ b/src/adapters/http/backend-envelope.ts @@ -93,13 +93,23 @@ const ENVELOPE_INVALID: EnvelopeRead = Object.freeze({ ok: false }); /** * The success `data`. A missing key is the null the backend dropped. * 1103/03-02. A page list's `meta.page` comes with it. + * 1106/01-01. And the backend's `meta.traceId`, when it sent one, as + * `readFailureEnvelope` gives it, for a client that turns a well-formed + * answer into a failure (Fileserver's 202 `REJECTED`). */ export function readSuccessEnvelope( value: unknown, -): Readonly<{ ok: true; value: unknown; page?: EnvelopePage }> | Readonly<{ ok: false }> { +): + | Readonly<{ + ok: true; + value: unknown; + page?: EnvelopePage; + traceId?: string; + }> + | Readonly<{ ok: false }> { const parsed = successEnvelopeSchema.safeParse(value); if (!parsed.success) return ENVELOPE_INVALID; - const page = parsed.data.meta.page; + const { page, traceId } = parsed.data.meta; return Object.freeze({ ok: true as const, value: parsed.data.data, @@ -113,6 +123,7 @@ export function readSuccessEnvelope( ...(typeof page.sort === "string" ? { sort: page.sort } : {}), }), }), + ...(traceId === undefined ? {} : { traceId }), }); } diff --git a/src/adapters/http/fileserver-upload.ts b/src/adapters/http/fileserver-upload.ts index 736392ed..5e21d39d 100644 --- a/src/adapters/http/fileserver-upload.ts +++ b/src/adapters/http/fileserver-upload.ts @@ -682,9 +682,15 @@ async function readAnswer( // 1105/01-01. The file is the user's to replace, as with a 413, 415 or // 422 refusal. The backend keeps the record unpublished and schedules // its bytes for removal, so nothing usable was stored. + // 1106/01-01. The server answered, so the failure carries its + // `meta.traceId` as an Envelope failure does (1105/01-03). The answer + // names no reason: the backend keeps the verifier's code on its record. return failure("VALIDATION_REJECTED", "FILE_REJECTED", { httpStatus: status, effect: "NOT_APPLIED", + ...(envelope.traceId === undefined + ? {} + : { traceId: envelope.traceId }), }); } return Object.freeze({ @@ -790,6 +796,7 @@ function failure( details: Readonly<{ effect: FailureEffectCertainty; httpStatus?: number; + traceId?: string; }>, ): Result { return Object.freeze({ diff --git a/tests/integration/fileserver-upload.test.ts b/tests/integration/fileserver-upload.test.ts index c4981fa0..0c9da0a4 100644 --- a/tests/integration/fileserver-upload.test.ts +++ b/tests/integration/fileserver-upload.test.ts @@ -214,6 +214,8 @@ describe("Fileserver multipart upload — success", () => { it("reads 202 REJECTED as a refused file, not as a broken answer", async () => { // 1105/01-01. A verifier refused the file for a reason other than malware. + // 1106/01-01. The server answered, so its `meta.traceId` comes with the + // failure for support to find the upload in the server logs. answer({ status: 202, body: successEnvelope( @@ -233,6 +235,7 @@ describe("Fileserver multipart upload — success", () => { retryable: false, action: "none", operationId: "FILESERVER_UPLOAD", + traceId: "trace-1", }), }); }); diff --git a/tests/runtime-schema/http-schema.test.ts b/tests/runtime-schema/http-schema.test.ts index b30eeeb0..8757e346 100644 --- a/tests/runtime-schema/http-schema.test.ts +++ b/tests/runtime-schema/http-schema.test.ts @@ -19,13 +19,14 @@ describe("paired backend Envelope boundary", () => { }); it("reads data, and a dropped data key as null", () => { + // 1106/01-01. With `meta.traceId`, as the failure reader gives it. expect( readSuccessEnvelope({ success: true, data: [], meta: { requestId: "request-1", traceId: "trace-1" }, }), - ).toEqual({ ok: true, value: [] }); + ).toEqual({ ok: true, value: [], traceId: "trace-1" }); expect(readSuccessEnvelope({ success: true, meta: {} })).toEqual({ ok: true, value: null, diff --git a/tests/unit/a11y-api-answers.test.ts b/tests/unit/a11y-api-answers.test.ts index 3b1f633b..53cb47f0 100644 --- a/tests/unit/a11y-api-answers.test.ts +++ b/tests/unit/a11y-api-answers.test.ts @@ -50,7 +50,12 @@ describe("accessibility e2e API answers", () => { expect(answer.kind).toBe("empty-list"); if (answer.kind !== "empty-list") return; // What the executor reads out of the frame is what the operation accepts. - expect(readSuccessEnvelope(answer.body)).toEqual({ ok: true, value: [] }); + // 1106/01-01. The reader also gives the scenario meta's `traceId`. + expect(readSuccessEnvelope(answer.body)).toEqual({ + ok: true, + value: [], + traceId: "0af7651916cd43dd8448eb211c80319c", + }); expect(LIST!.contract.outputValidator.safeParse([]).success).toBe(true); }); From 4cef2a27161924529696021301f30f14b5024442 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Fri, 6 Nov 2026 05:21:06 +0900 Subject: [PATCH 2/2] =?UTF-8?q?fix(cache-storage):=20=EC=9D=BD=ED=9E=88?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20active=20pointer=EB=A5=BC=20ac?= =?UTF-8?q?tivation=20=EC=98=B5=EC=85=98=EC=9C=BC=EB=A1=9C=20=EB=8D=AE?= =?UTF-8?q?=EC=96=B4=EC=93=B4=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 문제: control cache의 active pointer entry는 있는데 저장된 본문이 다시 쓰기 전까지 계속 읽히지 않으면(blob 손실 등) Cache Storage 관리가 모두 멈춘다. readActivePointer는 이 실패를 UNAVAILABLE/RETRY(또는 NOT_READABLE/REOPEN)로 돌려주고 CORRUPT_DATA로 보지 않는다. activation은 CORRUPT_DATA 밖의 pointer 실패에서 멈추고, cleanup·inspect·조회도 같은 실패에서 멈춘다. pointer를 쓰는 곳은 activation 하나라서 RETRY를 지켜도 돌아오지 않는다. 돌아오는 길은 adapter 밖에서 control cache를 지우는 것뿐이다. 리뷰 실험 02-R1 X3이 이것을 보였다. 고른 방법: activateRelease에 선택 옵션 replaceUnreadablePointer를 둔다. true면 이전 pointer를 읽지 못한 이유와 상관없이, 손상 pointer와 같은 0929/02-01 경로로 검증된 후보를 rollback 목록 없이 덮어쓴다. 기본값은 지금처럼 멈춤이라 "일시 오류 하나로 rollback 목록을 잃지 않는다"는 보존 조건이 지켜진다. 포트 주석, 설계 문서 8.3절, 운영 문서에 언제 쓰는지와 rollback 목록을 잃는다는 것을 적었다. 버린 대안: (가) 문서에 "control cache를 지우고 다시 activate"만 적기. 제품이 adapter 밖 코드로 소유 cache 이름을 알고 지워야 하고, 그동안 조회가 pointer 없음으로 바뀐다. (다) 연속 N회 실패를 손상으로 보기. 실패 횟수를 저장해야 하고, 긴 일시 오류에서도 rollback 목록을 잃는다. 위험·한계: 옵션을 켜면 일시 오류에서도 rollback 목록을 잃고, 다음 cleanup이 새 활성 release 밖의 owned release cache를 모두 지운다. 그래서 기본값으로 두지 않았다. 일시와 지속을 가르는 판단은 운영자 몫이다. 포트에 선택 칸이 하나 늘었다. 호출부·구현부 모두 호환이라 breaking이 아니다. CHANGELOG [계약] 행은 CHANGELOG 담당에게 남긴다. 1105/02-02의 남은 부분(release marker 본문이 계속 읽히지 않으면 stage가 marker를 다시 쓰지 않는다)은 이 변경으로 풀리지 않는다. marker는 release cache 안의 entry라 stage 경로를 바꿔야 한다. 범위 밖으로 둔다. service-worker-pwa 레시피 번들(여유 306 B)에 수십 B가 더해진다. 실제 브라우저에서 pointer 본문 읽기 실패가 지속되는지는 확인하지 못했다(리뷰 5절 미검증과 같다). 검증: desktop batch에서 확인. node scripts/run-vitest.ts run tests/unit/public-response-cache-repair.test.ts RED(이 커밋의 테스트 + 부모 src): "replaces a pointer whose body stays unreadable when the activation asks to (1106/02-01)"가 옵션을 준 activation에서 실패한다. 부모는 옵션을 모르므로 UNAVAILABLE/RETRY를 돌려주고 pointer를 다시 쓰지 않는다. 그 앞의 멈춤 단언(X3와 같은 결과)은 부모에서도 통과한다. 일시 오류 테스트 "still stops activation when the pointer read fails transiently"는 바뀌지 않았고 통과해야 한다. Refs: 1106/02-01 --- .../browser-file-and-origin-storage.md | 10 +++ .../client-cache-and-storage-recovery.md | 6 ++ .../public-response-cache-adapter.ts | 5 +- .../cache-storage-ports.ts | 19 +++- .../unit/public-response-cache-repair.test.ts | 87 +++++++++++++++++++ 5 files changed, 124 insertions(+), 3 deletions(-) diff --git a/docs/architecture/browser-file-and-origin-storage.md b/docs/architecture/browser-file-and-origin-storage.md index 3ff7c1f3..8a8e7846 100644 --- a/docs/architecture/browser-file-and-origin-storage.md +++ b/docs/architecture/browser-file-and-origin-storage.md @@ -1016,6 +1016,16 @@ rollback 목록 없이 덮어쓴다. pointer 읽기의 일시 오류는 activati cleanup이 무엇을 지울지 볼 수 있다. 일시 오류에서는 inspect도 실패한다 (2026-11-03 02-05). +pointer entry는 있는데 저장된 본문이 다시 쓰기 전까지 계속 읽히지 않으면(blob +손실 등) 일시 오류와 구별되지 않는다. 조회, activation, cleanup, inspect가 모두 +같은 실패(`UNAVAILABLE`/`RETRY` 등)를 계속 돌려준다. pointer를 쓰는 곳은 +activation 하나라서 기다려도 돌아오지 않는다. 이때 운영자는 +`activateRelease(releaseRegistryId, manifestDigestHex, { replaceUnreadablePointer: true })`로 +검증된 후보를 활성화한다. 손상 pointer와 같이 rollback 목록 없이 덮어쓴다. 그 뒤 +cleanup은 새 활성 release 밖의 owned release cache를 모두 지운다. 옵션이 없으면 +지금처럼 멈춘다. 일시 오류 한 번으로 rollback 목록을 잃지 않기 위해서다 +(1106/02-01). + 현재 `cleanupOwned()`와 `inspect()`는 위 ownership/integrity 경계를 지키지만 cache count에 대한 cursor, per-invocation max count와 deadline은 제공하지 않는다. 따라서 cache 수에 비례한 unbounded maintenance를 production boot path에 diff --git a/docs/operations/client-cache-and-storage-recovery.md b/docs/operations/client-cache-and-storage-recovery.md index 5780b9c5..b768b139 100644 --- a/docs/operations/client-cache-and-storage-recovery.md +++ b/docs/operations/client-cache-and-storage-recovery.md @@ -27,6 +27,12 @@ session/account Query lifecycle, strict query policy와 Web Storage v2 lifecycle 않는다. 두 operation은 Cache Storage와 mutation lock만 요구하고 실패 시 recovery는 `RETRY`다. `stageRelease`만 fetcher를 요구하며 recovery는 `ONLINE_ONLY`다. +- active pointer 본문을 계속 읽지 못하면 조회, `activateRelease`, + `cleanupOwned`, `inspect`가 모두 같은 실패를 계속 돌려준다. 다시 시도해도 + 돌아오지 않는다. pointer를 쓰는 곳은 activation 하나다. 이때 + `activateRelease(id, digest, { replaceUnreadablePointer: true })`로 검증된 + release를 활성화한다. rollback 목록은 잃고, 다음 cleanup이 다른 owned release + cache를 지운다. 옵션 없는 activation은 일시 오류에서 멈춘다(1106/02-01). - variant를 사용하는 policy(`allowedVaryHeaderNames` 비어 있지 않음)는 반드시 `allowedResponseHeaderNames`에 `vary`를 포함해야 한다. 아니면 composition이 `TypeError`로 즉시 실패한다. 저장된 variant가 같은 key로 충돌하는 상태를 만들지 diff --git a/src/adapters/cache-storage/public-response-cache-adapter.ts b/src/adapters/cache-storage/public-response-cache-adapter.ts index 41e2ce25..43ed3ee5 100644 --- a/src/adapters/cache-storage/public-response-cache-adapter.ts +++ b/src/adapters/cache-storage/public-response-cache-adapter.ts @@ -719,9 +719,12 @@ export function createPublicResponseCacheAdapter( // under this policy) is replaced by this verified candidate with // an empty rollback list, rather than blocking every later // deployment. A transient read failure still stops activation. + // 1106/02-01. Unless the caller asks to replace it: a body that + // stays unreadable fails the same way, and only this writes it. if ( !previousPointer.ok && - previousPointer.error.code !== "CORRUPT_DATA" + previousPointer.error.code !== "CORRUPT_DATA" && + options.replaceUnreadablePointer !== true ) { return rebaseFailure( previousPointer.error, diff --git a/src/application/ports/browser-file-storage/cache-storage-ports.ts b/src/application/ports/browser-file-storage/cache-storage-ports.ts index ce1e41e3..ffc9658e 100644 --- a/src/application/ports/browser-file-storage/cache-storage-ports.ts +++ b/src/application/ports/browser-file-storage/cache-storage-ports.ts @@ -45,7 +45,9 @@ export type PublicCacheInspection = Readonly<{ * no longer accepts: lookup and cleanup stop on it and the next verified * activation replaces it with an empty rollback list. The candidates are * still listed so the operator can see what that activation and the - * cleanup after it will keep. A transient pointer read is a failure. + * cleanup after it will keep. A transient pointer read is a failure. One + * that keeps failing is the case for `replaceUnreadablePointer` on + * `activateRelease` (1106/02-01). */ activePointer: "NONE" | "READABLE" | "UNREADABLE"; /** `null` unless `activePointer` is `READABLE`. */ @@ -94,10 +96,23 @@ export interface PublicResponseCacheAdminPort { manifest: PublicCacheReleaseManifest, options?: Readonly<{ signal?: AbortSignal }>, ): Promise>; + /** + * 1106/02-01. A pointer read that fails other than as damage stops the + * activation, so one bad read does not lose the rollback list. A pointer + * body that stays unreadable fails the same way every time, and lookup, + * cleanup and inspect stop on it too; only an activation writes the + * pointer. `replaceUnreadablePointer: true` is the caller's way back: this + * activation then replaces a pointer it cannot read, as it always replaces + * a damaged one, with an empty rollback list, and the next cleanup deletes + * every other release cache. + */ activateRelease( releaseRegistryId: string, manifestDigestHex: string, - options?: Readonly<{ signal?: AbortSignal }>, + options?: Readonly<{ + signal?: AbortSignal; + replaceUnreadablePointer?: boolean; + }>, ): Promise>; cleanupOwned( request?: Readonly<{ diff --git a/tests/unit/public-response-cache-repair.test.ts b/tests/unit/public-response-cache-repair.test.ts index d7ff2d8a..ee905ddc 100644 --- a/tests/unit/public-response-cache-repair.test.ts +++ b/tests/unit/public-response-cache-repair.test.ts @@ -970,4 +970,91 @@ describe("public cache pointer after a policy change or damage (0929/02-01)", () await adapter.responses.matchActiveExact({ absoluteUrl: assetUrl("r1") }), ).toMatchObject({ ok: true, value: expect.anything() }); }); + + // 1106/02-01 (review experiment 02-R1 X3). The pointer entry is there, but + // its stored body fails to read until something writes it again (a lost + // blob). Without the option that looks like the transient failure above and + // everything stops; only an activation writes the pointer. + it("replaces a pointer whose body stays unreadable when the activation asks to (1106/02-01)", async () => { + const cacheStorage = new MemoryCacheStorage(); + const policy = policyRetaining(1); + const adapter = adapterFor(cacheStorage, policy); + expect(await deploy(adapter, "r1", policy)).toMatchObject({ ok: true }); + const control = cacheStorage.caches.get(controlCacheName(policy))!; + const pointerUrl = activePointerRequest(policy).url; + const pointer = { unreadable: true, rewrites: 0 }; + const realMatch = control.match.bind(control); + const realPut = control.put.bind(control); + const urlOf = (request: RequestInfo | URL) => + request instanceof Request ? request.url : new URL(String(request)).href; + control.put = async (request, response) => { + if (urlOf(request) === pointerUrl) { + pointer.unreadable = false; + pointer.rewrites += 1; + } + await realPut(request, response); + }; + control.match = async (request) => { + const stored = await realMatch(request); + if (!stored || urlOf(request) !== pointerUrl || !pointer.unreadable) { + return stored; + } + return new Response( + new ReadableStream({ + pull(controller) { + controller.error(new TypeError("blob missing")); + }, + }), + { headers: stored.headers }, + ); + }; + const next = await manifestOf("r2", policy); + const activateNext = ( + options?: Readonly<{ replaceUnreadablePointer?: boolean }>, + ) => + adapter.admin.activateRelease( + next.releaseRegistryId, + next.manifestDigestHex, + options, + ); + const unavailable = (operation: string) => ({ + ok: false, + error: { code: "UNAVAILABLE", operation, recovery: "RETRY" }, + }); + + // Staging does not read the pointer; everything else stops on it. + expect(await adapter.admin.stageRelease(next)).toMatchObject({ ok: true }); + expect( + await adapter.responses.matchActiveExact({ absoluteUrl: assetUrl("r1") }), + ).toMatchObject(unavailable("CACHE_LOOKUP")); + expect(await activateNext()).toMatchObject(unavailable("CACHE_ACTIVATE")); + expect(await adapter.admin.cleanupOwned()).toMatchObject( + unavailable("CACHE_DELETE"), + ); + expect(await adapter.admin.inspect()).toMatchObject( + unavailable("CACHE_LOOKUP"), + ); + expect(await activateNext()).toMatchObject(unavailable("CACHE_ACTIVATE")); + expect(pointer).toEqual({ unreadable: true, rewrites: 0 }); + + // The way back: this activation replaces the pointer it cannot read. + expect( + await activateNext({ replaceUnreadablePointer: true }), + ).toMatchObject({ ok: true, value: { releaseRegistryId: "r2" } }); + expect(pointer).toEqual({ unreadable: false, rewrites: 1 }); + expect( + await adapter.responses.matchActiveExact({ absoluteUrl: assetUrl("r2") }), + ).toMatchObject({ ok: true, value: expect.anything() }); + expect(await adapter.admin.inspect()).toMatchObject({ + ok: true, + value: { activePointer: "READABLE", activeReleaseRegistryId: "r2" }, + }); + // The rollback list went with the old pointer, so cleanup drops r1. + expect(await adapter.admin.cleanupOwned()).toMatchObject({ + ok: true, + value: { deletedOwnedCaches: 1, retainedOwnedCaches: 2 }, + }); + // A later activation without the option works as before. + expect(await deploy(adapter, "r3", policy)).toMatchObject({ ok: true }); + }); });