@smthrs/artifacts 1.0.0-rc.7 → 1.0.0-rc.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/RemoteArtifacts.ts"],
4
- "sourcesContent": ["/**\n * The shared artifact tier: an {@link ArtifactStore.Service} spoken over HTTP.\n *\n * The wire protocol mirrors Bazel's dumb-HTTP remote cache, class\n * `HttpCacheClient` in `com.google.devtools.build.lib.remote.http`, which\n * documents it as: \"CAS (Content Addressable Storage) blobs are stored under\n * the path `/cas/base16-key`\", uploaded with `PUT`, downloaded with `GET`. We add\n * `HEAD /cas/{digest}` for a single existence probe and\n * `POST /cas/findMissing` for the batched one, because Bazel's HTTP client has\n * no `findMissingDigests` at all \u2014 it answers \"everything is missing\" and\n * re-uploads \u2014 and a batched probe is the whole reason\n * `MissingDigestsFinder` exists in the gRPC client.\n *\n * Transport is Effect's own `HttpClient` tag. There is no lower transport in\n * this repository, and there does not need to be one: the kernel already\n * decorates that tag with `net:get`/`net:post` capability checks, so a remote\n * artifact fetch is permission-checked like every other egress.\n *\n * ## Chunked uploads\n *\n * With `chunkBytes` set, a blob past that size travels as a sequence of\n * `PUT /cas/{digest}` requests carrying `Content-Range: bytes {a}-{b}/{total}`,\n * preceded by a `HEAD /cas/{digest}` existence probe and a\n * `Content-Range: bytes *\\/{total}` probe with an empty body. This is the\n * resumable-upload shape HTTP already has: the range probe asks what prefix\n * the tier holds and `308` means \"keep going\", with a `Range: bytes=0-{last}`\n * header on the probe or on a chunk answer moving the offset.\n *\n * Only a `308` continues the sequence, and the completing chunk's `2xx` is\n * confirmed with `HEAD` before `put` reports the digest as published. A `2xx`\n * anywhere else, to the empty probe or to a chunk the tier is still owed bytes\n * after, reads as a tier that ignored `Content-Range` and stored the body it\n * was handed, which is what plain WebDAV `PUT` does. That tier, the one that\n * answers `400`, `411`, or `416`, and the one whose `HEAD` will not confirm\n * the stored length all get the same treatment: one whole-blob `PUT`, which\n * overwrites the partial body the sequence left behind. So the blob always\n * lands whole, and the cost of turning the dial on against a server that\n * never learned about it is round trips, never a digest published over bytes\n * the server does not hold.\n *\n * This repository's own hosted and self-hosted cache services are such\n * servers: neither implements the resumable sequence, and both refuse a\n * ranged `PUT` with `400`, RFC 9110 section 14.5's answer from a resource\n * that does not support partial `PUT`. Against them `chunkBytes` costs the\n * probe round trip and the blob travels whole, and both cap one request body\n * at 16 MiB, so a blob past that cap is refused with `413` in either mode.\n *\n * The endpoint and its credentials arrive as **layer construction options**.\n * They are capabilities, never step inputs: they are not hashed into a step\n * key, journaled, or returned in a recorded result. Invalid endpoint errors are\n * sanitized before they cross the boundary.\n *\n * @since 1.0.0-rc.0\n */\n\nimport * as Duration from \"effect/Duration\"\nimport * as Effect from \"effect/Effect\"\nimport * as FetchHttpClient from \"effect/http/FetchHttpClient\"\nimport * as Headers from \"effect/http/Headers\"\nimport * as HttpClient from \"effect/http/HttpClient\"\nimport * as HttpClientRequest from \"effect/http/HttpClientRequest\"\nimport type * as HttpClientResponse from \"effect/http/HttpClientResponse\"\nimport * as Layer from \"effect/Layer\"\nimport * as Option from \"effect/Option\"\nimport * as Schema from \"effect/Schema\"\nimport * as Stream from \"effect/Stream\"\nimport * as ArtifactStore from \"./ArtifactStore.ts\"\n\n/**\n * How eagerly a composition materializes shared blobs into its local store.\n *\n * @category models\n * @since 1.0.0-rc.0\n */\nexport const DownloadPolicy = Schema.Literals([\"all\", \"toplevel\", \"minimal\"] as const)\n\n/**\n * How eagerly a composition materializes remote blobs.\n *\n * @category models\n * @since 1.0.0-rc.0\n */\nexport type DownloadPolicy = typeof DownloadPolicy.Type\n\n/**\n * Every download policy, in materialization order.\n *\n * @category models\n * @since 1.0.0-rc.0\n */\nexport const downloadPolicies: ReadonlyArray<DownloadPolicy> = Object.freeze([\"all\", \"toplevel\", \"minimal\"])\n\n/**\n * A remote artifact tier, which is an ordinary store that also states how\n * eagerly a composition reading through it should materialize blobs locally.\n *\n * @category models\n * @since 1.0.0-rc.0\n */\nexport interface Service extends ArtifactStore.Service {\n readonly downloadPolicy: DownloadPolicy\n}\n\n/**\n * Reads the download policy a store declares, or `undefined` for a store that\n * declares none \u2014 every local store, and any foreign implementation.\n *\n * @category getters\n * @since 1.0.0-rc.0\n */\nexport const downloadPolicyOf = (store: ArtifactStore.Service): DownloadPolicy | undefined => {\n const declared = (store as { readonly downloadPolicy?: unknown }).downloadPolicy\n return Schema.is(DownloadPolicy)(declared) ? declared : undefined\n}\n\n/**\n * How to reach the shared artifact tier.\n *\n * @category models\n * @since 1.0.0-rc.0\n * @slop\n */\nexport interface Options {\n /**\n * The cache root, e.g. `https://cache.example.com`. `/cas/{digest}` and\n * `/cas/findMissing` are resolved beneath it. A trailing slash is ignored.\n *\n * Four shapes are refused at construction as `invalid_configuration`, before\n * any request leaves and therefore before `headers` can reach a wire: a value\n * that is not a string, one no `URL` parser accepts, a scheme other than\n * `https:` (plain `http:` is accepted only for the literal loopback hosts\n * `localhost`, `127.0.0.1`, and `[::1]`), and an endpoint carrying\n * userinfo, a query, or a fragment. The\n * last rule keeps a credential out of a place nothing here would redact: an\n * endpoint is interpolated into every request path and into span attributes.\n *\n * The refusal message names only the violated rule and never echoes the\n * endpoint, so a rejected `https://user:secret@host` cannot leak its\n * credential into a log line or a durable error.\n */\n readonly endpoint: string\n /**\n * Headers sent with every request \u2014 an `Authorization` bearer token, a\n * tenant header, whatever the deployment's gateway wants. This is the\n * credential seam, and it is deliberately construction-time: a credential\n * that arrived as a step input would be hashed into a step key and persisted\n * everywhere the journal goes.\n *\n * The protocol wins every collision. These headers are applied to a request\n * first. Configured `content-type`, `content-length`, and `content-range`\n * are removed, then computed only where the protocol needs them. Whole-blob\n * uploads, including fallbacks, never carry `content-range`.\n * All configured header names are redacted in HTTP spans, in addition to\n * the host's existing redaction rules.\n *\n * A name that is not an HTTP token, or a value longer than 16 KiB or\n * carrying a control character (CR and LF included), is refused at\n * construction as `invalid_configuration`.\n */\n readonly headers?: Record<string, string> | undefined\n /**\n * The deadline on a single download, from the request leaving to the last\n * body byte arriving. A shared tier that stops answering must fail the read\n * rather than park it forever. `CombinedArtifacts.get` propagates a remote\n * `ArtifactStoreError`, including a download timeout. Only opportunistic\n * uploads from `CombinedArtifacts.put` ignore remote failures;\n * `ArtifactSync.hydrate` separately turns a failure into replay rejection.\n * Defaults to 60 seconds, Bazel's `--remote_timeout`\n * default for the same REST protocol (its `RemoteOptions`: \"For the REST\n * cache, this is both the connect and the read timeout\").\n */\n readonly downloadTimeout?: Duration.Input | undefined\n /**\n * Deadline for an upload, including every resume probe and chunk. Defaults\n * to 60 seconds.\n */\n readonly uploadTimeout?: Duration.Input | undefined\n /**\n * Deadline for an existence probe or one `findMissing` batch, including its\n * response body. Defaults to 60 seconds.\n */\n readonly requestTimeout?: Duration.Input | undefined\n /**\n * The largest download this client will buffer, in bytes. The body is read\n * incrementally and abandoned the moment it exceeds the bound \u2014 and refused\n * outright when `Content-Length` already declares the excess \u2014 so a\n * mis-serving or hostile cache cannot make this process buffer and hash an\n * arbitrarily large body before the digest check refuses it. Defaults to\n * 256 MiB.\n */\n readonly maxDownloadBytes?: number | undefined\n /**\n * Largest `findMissing` response body accepted. Defaults to the protocol's\n * 256 KiB request/response bound and may only lower that bound.\n */\n readonly maxFindMissingResponseBytes?: number | undefined\n /**\n * Above this many bytes an upload travels as a sequence of `Content-Range`\n * `PUT`s instead of one whole-blob body, and a transfer that died partway\n * resumes from the prefix the tier kept rather than starting over. See\n * {@link module:RemoteArtifacts | the chunked upload protocol} below.\n *\n * Absent by default: every upload is one whole-blob `PUT`, which is what\n * Bazel's dumb-HTTP client does and what every deployment already serving\n * this protocol expects. Set it when a proxy caps request bodies, or when\n * artifacts are large enough that losing a transfer is expensive.\n *\n * The repository's own hosted and self-hosted cache services answer the\n * ranged probe with `400`, so against them this dial degrades to one\n * whole-blob `PUT`, and their 16 MiB body cap refuses a larger blob with\n * `413` in either mode.\n */\n readonly chunkBytes?: number | undefined\n /**\n * How eagerly a composition reading through this tier materializes blobs\n * into its local store. This is Bazel's `--remote_download_{all,toplevel,\n * minimal}` dial (`RemoteOutputChecker`), and it lives here because the\n * shared tier is the thing being conserved.\n *\n * - `all` (the default) prefetches every referenced blob when a replay is\n * admitted, so the replay reads local bytes.\n * - `toplevel` prefetches nothing and materializes a blob into the local\n * store on the first read that actually needs it.\n * - `minimal` prefetches nothing and materializes nothing: a read is served\n * straight from the shared tier and the local store never grows.\n *\n * `CombinedArtifacts.get` honors the last two; `@smthrs/engine-store`'s\n * `ArtifactSync.hydrate` honors the first.\n */\n readonly downloadPolicy?: DownloadPolicy | undefined\n}\n\n/**\n * The default download deadline. 60 seconds is Bazel's `--remote_timeout`\n * default, governing the same dumb-HTTP cache protocol.\n */\nconst defaultDownloadTimeout = Duration.seconds(60)\nconst defaultUploadTimeout = Duration.seconds(60)\nconst defaultRequestTimeout = Duration.seconds(60)\n\n/**\n * The default bound on a downloaded blob: 256 MiB. Artifacts are spilled step\n * values, not media libraries; a body past this size is far more likely a\n * mis-serving cache than a legitimate blob, and the dial is per-store for a\n * deployment that knows better.\n */\nconst defaultMaxDownloadBytes = 256 * 1024 * 1024\nconst maxFindMissingBatchDigests = 1_000\nconst maxFindMissingBodyBytes = 256 * 1024\nconst encoder = new TextEncoder()\nconst decoder = new TextDecoder(\"utf-8\", { fatal: true })\n\n/**\n * The credential-free part of a transport failure: the HTTP client's reason\n * tag and the first operating-system error code (`ECONNREFUSED`, `ENOTFOUND`,\n * `CERT_HAS_EXPIRED`) in its cause chain. Never the request, the URL, the\n * headers, or free-form text, any of which may carry a credential.\n */\nconst transportReason = (cause: unknown): string | undefined => {\n const reason = (cause as { readonly reason?: { readonly _tag?: unknown } } | null)?.reason\n const tag = typeof reason?._tag === \"string\" ? reason._tag : undefined\n let code: string | undefined\n let current: unknown = reason ?? cause\n for (let depth = 0; depth < 8 && typeof current === \"object\" && current !== null; depth++) {\n const candidate = (current as { readonly code?: unknown }).code\n if (typeof candidate === \"string\" && /^[A-Z][A-Z0-9_]{1,63}$/.test(candidate)) {\n code = candidate\n break\n }\n current = (current as { readonly cause?: unknown }).cause\n }\n const parts = [tag, code].filter((part): part is string => part !== undefined)\n return parts.length === 0 ? undefined : parts.join(\" \")\n}\n\nconst transportFailure = (operation: string, cause?: unknown): ArtifactStore.ArtifactStoreError => {\n const reason = transportReason(cause)\n return new ArtifactStore.ArtifactStoreError({\n code: \"transport_failed\",\n message: `the remote artifact tier refused ${operation}${reason === undefined ? \"\" : ` (${reason})`}`\n })\n}\n\nconst unexpectedStatus = (operation: string, status: number): ArtifactStore.ArtifactStoreError =>\n new ArtifactStore.ArtifactStoreError({\n code: \"transport_failed\",\n message: `the remote artifact tier answered ${operation} with HTTP ${status}`\n })\n\nconst invalidOption = (name: string): ArtifactStore.ArtifactStoreError =>\n new ArtifactStore.ArtifactStoreError({\n code: \"invalid_configuration\",\n message: `invalid remote artifact option: ${name}`\n })\n\n/** The batched-probe response body. */\nconst FindMissingResponse = Schema.Struct({ missing: Schema.Array(Schema.String) })\n\n/** An RFC 9110 field-name token. */\nconst headerName = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/\n\n/** Whether a header value carries a C0 control character or DEL, CR and LF included. */\nconst hasControlText = (value: string): boolean => {\n for (let index = 0; index < value.length; index++) {\n const unit = value.charCodeAt(index)\n if (unit <= 0x1f || unit === 0x7f) return true\n }\n return false\n}\n\nconst configurationFailure = (message: string): ArtifactStore.ArtifactStoreError =>\n new ArtifactStore.ArtifactStoreError({ code: \"invalid_configuration\", message })\n\n/**\n * A 2xx is success, a 404 is a miss, and everything else is a transport\n * failure. Bazel's HTTP client takes the same three-way split; it is the only\n * classification a dumb-HTTP cache can support, because there is no richer\n * error envelope on the wire.\n */\nconst isOk = (response: HttpClientResponse.HttpClientResponse): boolean =>\n response.status >= 200 && response.status < 300\n\n/**\n * Builds a remote artifact store over Effect's `HttpClient`.\n *\n * @category constructors\n * @since 1.0.0-rc.0\n * @slop\n */\nexport const make = (\n options: Options\n): Effect.Effect<Service, ArtifactStore.ArtifactStoreError, HttpClient.HttpClient> =>\n Effect.gen(function*() {\n const configured = yield* Effect.try({\n try: () => {\n const headers = options.headers === undefined\n ? undefined\n : Object.freeze(Object.fromEntries(\n Object.entries(options.headers).map(([name, value]) => {\n // Refused here, once, rather than on every request as an opaque\n // transport failure: a CRLF in a value or a space in a name can\n // never become a valid header.\n if (typeof value !== \"string\" || value.length > 16 * 1024 || hasControlText(value)) {\n throw new TypeError(\"header value\")\n }\n if (!headerName.test(name)) throw new TypeError(\"header name\")\n return [name, value]\n })\n ))\n return {\n endpoint: options.endpoint,\n headers,\n downloadTimeout: options.downloadTimeout,\n uploadTimeout: options.uploadTimeout,\n requestTimeout: options.requestTimeout,\n maxDownloadBytes: options.maxDownloadBytes,\n maxFindMissingResponseBytes: options.maxFindMissingResponseBytes,\n chunkBytes: options.chunkBytes,\n downloadPolicy: options.downloadPolicy\n }\n },\n catch: () => invalidOption(\"options\")\n })\n if (typeof configured.endpoint !== \"string\") return yield* Effect.fail(invalidOption(\"endpoint\"))\n const endpoint = yield* Effect.try({\n try: () => new URL(configured.endpoint),\n catch: () => configurationFailure(\"invalid remote artifact endpoint\")\n })\n // A self-hosted cache on this machine is reachable over plain HTTP, and\n // loopback traffic never leaves the host. Only literal loopback names are\n // exempt: a `*.localhost` name reaches loopback only on a resolver that\n // follows RFC 6761, and one that forwards it to DNS would carry the\n // credential headers over plaintext to whatever host answers.\n const loopback = endpoint.hostname === \"localhost\" || endpoint.hostname === \"127.0.0.1\" ||\n endpoint.hostname === \"[::1]\"\n if (endpoint.protocol !== \"https:\" && !(endpoint.protocol === \"http:\" && loopback)) {\n return yield* Effect.fail(configurationFailure(\"remote artifact endpoint must use HTTPS\"))\n }\n if (endpoint.username !== \"\" || endpoint.password !== \"\" || endpoint.search !== \"\" || endpoint.hash !== \"\") {\n return yield* Effect.fail(\n configurationFailure(\"remote artifact endpoint must not contain credentials, a query, or a fragment\")\n )\n }\n endpoint.pathname = endpoint.pathname.replace(/\\/+$/, \"\")\n const base = endpoint.toString().replace(/\\/+$/, \"\")\n const headers = configured.headers\n const client = yield* HttpClient.HttpClient\n const credentialNames = Object.keys(headers ?? {}).map((name) => name.toLowerCase())\n /**\n * Protocol-owned headers are computed from each request's body and range,\n * never inherited from credentials, including on a whole-blob fallback.\n */\n const authorized = (request: HttpClientRequest.HttpClientRequest): HttpClientRequest.HttpClientRequest =>\n (headers === undefined ? request : HttpClientRequest.setHeaders(request, headers)).pipe(\n HttpClientRequest.removeHeader(\"content-type\"),\n HttpClientRequest.removeHeader(\"content-length\"),\n HttpClientRequest.removeHeader(\"content-range\")\n )\n // The address is a URL path segment, so it is refused before it is\n // interpolated and percent-encoded when it is: an address carrying a\n // separator or a `..` would otherwise point this client at a different\n // resource on the configured endpoint. `ArtifactStore.validateDigest` is\n // the same guard the filesystem tier applies to the same untrusted input \u2014\n // a digest read back out of a durable row.\n const casUrl = (digest: ArtifactStore.Digest) => `${base}/cas/${digest}`\n const scopedClient = HttpClient.withScope(client)\n /**\n * Keep body processing inside the exchange; abort unused responses on every exit.\n *\n * Redirects are never followed. A fetch-based client follows them by\n * default and strips only `Authorization` on a cross-origin hop, so a\n * tier answering `3xx` would otherwise receive the credential headers at\n * another origin or over plain HTTP. Manual mode hands the `3xx` back,\n * where it is an unexpected status (or, for `308`, the resumable-upload\n * answer this protocol already speaks).\n */\n const send = <A, E, R>(\n operation: string,\n request: HttpClientRequest.HttpClientRequest,\n use: (response: HttpClientResponse.HttpClientResponse) => Effect.Effect<A, E, R>\n ): Effect.Effect<A, E | ArtifactStore.ArtifactStoreError, R> =>\n Effect.gen(function*() {\n const redactedNames = yield* Headers.CurrentRedactedNames\n const requestInit = yield* Effect.serviceOption(FetchHttpClient.RequestInit)\n return yield* scopedClient.execute(request).pipe(\n Effect.provideService(FetchHttpClient.RequestInit, {\n ...Option.getOrUndefined(requestInit),\n redirect: \"manual\"\n }),\n Effect.mapError((cause) => transportFailure(operation, cause)),\n Effect.flatMap(use),\n Effect.provideService(Headers.CurrentRedactedNames, [...redactedNames, ...credentialNames]),\n Effect.scoped\n )\n })\n const parseDeadline = (name: string, input: Duration.Input | undefined, fallback: Duration.Duration) =>\n Effect.gen(function*() {\n const parsed = Duration.fromInput(input ?? fallback)\n if (\n Option.isNone(parsed) ||\n !Number.isFinite(Duration.toMillis(parsed.value)) ||\n Duration.toMillis(parsed.value) <= 0\n ) {\n return yield* Effect.fail(invalidOption(name))\n }\n return parsed.value\n })\n const downloadDeadline = yield* parseDeadline(\n \"downloadTimeout\",\n configured.downloadTimeout,\n defaultDownloadTimeout\n )\n const uploadDeadline = yield* parseDeadline(\"uploadTimeout\", configured.uploadTimeout, defaultUploadTimeout)\n const requestDeadline = yield* parseDeadline(\"requestTimeout\", configured.requestTimeout, defaultRequestTimeout)\n const maxDownloadBytes = configured.maxDownloadBytes ?? defaultMaxDownloadBytes\n if (!Number.isSafeInteger(maxDownloadBytes) || maxDownloadBytes <= 0) {\n return yield* Effect.fail(invalidOption(\"maxDownloadBytes\"))\n }\n const maxFindMissingResponseBytes = configured.maxFindMissingResponseBytes ?? maxFindMissingBodyBytes\n if (\n !Number.isSafeInteger(maxFindMissingResponseBytes) ||\n maxFindMissingResponseBytes <= 0 ||\n maxFindMissingResponseBytes > maxFindMissingBodyBytes\n ) {\n return yield* Effect.fail(invalidOption(\"maxFindMissingResponseBytes\"))\n }\n const chunkBytes = configured.chunkBytes\n if (chunkBytes !== undefined && (!Number.isSafeInteger(chunkBytes) || chunkBytes <= 0)) {\n return yield* Effect.fail(invalidOption(\"chunkBytes\"))\n }\n const downloadPolicy = configured.downloadPolicy ?? \"all\"\n if (!Schema.is(DownloadPolicy)(downloadPolicy)) {\n return yield* Effect.fail(invalidOption(\"downloadPolicy\"))\n }\n const bodyTooLarge = (operation: string, received: number, bound: number): ArtifactStore.ArtifactStoreError =>\n new ArtifactStore.ArtifactStoreError({\n code: \"transport_failed\",\n message: `the remote artifact tier answered ${operation} with ${received} bytes, past the ${bound}-byte bound`\n })\n const timedOut = (operation: string): ArtifactStore.ArtifactStoreError =>\n new ArtifactStore.ArtifactStoreError({\n code: \"transport_failed\",\n message: `the remote artifact tier did not finish ${operation} within its configured deadline`\n })\n const within = <A, E, R>(\n operation: string,\n deadline: Duration.Duration,\n effect: Effect.Effect<A, E, R>\n ): Effect.Effect<A, E | ArtifactStore.ArtifactStoreError, R> =>\n effect.pipe(\n Effect.timeout(deadline),\n Effect.catchTag(\"TimeoutError\", () => Effect.fail(timedOut(operation)))\n )\n /**\n * Reads a download body incrementally, refusing it the moment it exceeds\n * the size bound; a `Content-Length` that already declares the excess is\n * refused before a single body byte is read. Streaming instead of\n * buffering the whole body means the guard fires at most one chunk past\n * the bound, never after an arbitrarily large response is in memory.\n */\n const readBounded = (\n response: HttpClientResponse.HttpClientResponse,\n operation: string,\n bound: number\n ) =>\n Effect.gen(function*() {\n const declared = response.headers[\"content-length\"]\n if (declared !== undefined) {\n const declaredBytes = Number(declared)\n if (!/^\\d+$/.test(declared) || !Number.isSafeInteger(declaredBytes)) {\n return yield* Effect.fail(transportFailure(`${operation} body`))\n }\n if (declaredBytes > bound) return yield* Effect.fail(bodyTooLarge(operation, declaredBytes, bound))\n }\n const chunks: Array<Uint8Array> = []\n let received = 0\n yield* Stream.runForEach(response.stream, (chunk: Uint8Array) =>\n Effect.suspend(() => {\n received += chunk.byteLength\n if (received > bound) return Effect.fail(bodyTooLarge(operation, received, bound))\n chunks.push(new Uint8Array(chunk))\n return Effect.void\n })).pipe(\n Effect.catchTag(\"HttpClientError\", (cause) =>\n // An absent body is zero bytes, not a transport refusal: the\n // digest check in `get` is the arbiter of whether empty content\n // is the requested artifact.\n cause.reason._tag === \"EmptyBodyError\"\n ? Effect.void\n : Effect.fail(transportFailure(`${operation} body`)))\n )\n const bytes = new Uint8Array(received)\n let offset = 0\n for (const chunk of chunks) {\n bytes.set(chunk, offset)\n offset += chunk.byteLength\n }\n return bytes\n })\n /** The network half of `get`: request, status split, bounded body read. */\n const download = (digest: ArtifactStore.Digest) =>\n send(\"a download\", authorized(HttpClientRequest.get(casUrl(digest))), (response) =>\n Effect.gen(function*() {\n if (response.status === 404) {\n return yield* Effect.fail(new ArtifactStore.ArtifactMissing({ code: \"artifact_missing\", digest }))\n }\n if (!isOk(response)) return yield* Effect.fail(unexpectedStatus(\"a download\", response.status))\n return yield* readBounded(response, \"a download\", maxDownloadBytes)\n }))\n\n /** One whole-blob `PUT`: the protocol every dumb-HTTP CAS already speaks. */\n const uploadWhole = (digest: ArtifactStore.Digest, bytes: Uint8Array) =>\n send(\n \"an upload\",\n authorized(HttpClientRequest.put(casUrl(digest))).pipe(\n HttpClientRequest.bodyUint8Array(bytes, \"application/octet-stream\")\n ),\n (response) => isOk(response) ? Effect.void : Effect.fail(unexpectedStatus(\"an upload\", response.status))\n )\n\n /**\n * The byte after the prefix a `Range: bytes=0-{last}` header reports, or\n * `undefined` when the answer names no prefix. Only a prefix starting at\n * zero is usable: a tier holding a middle span has nothing this client can\n * continue from, so it is treated as holding nothing.\n */\n const resumeOffset = (response: HttpClientResponse.HttpClientResponse): number | undefined => {\n const header = response.headers[\"range\"]\n if (header === undefined) return undefined\n const match = /^bytes=0-(\\d+)$/.exec(header.trim())\n if (match === null) return undefined\n const last = Number(match[1])\n // A prefix past the safe integer range does not survive `Number`: the\n // decimal digits are rounded, so the offset it names is a number this\n // client made up. It proves nothing about what the tier holds, which is\n // the same as naming no prefix at all.\n if (!Number.isSafeInteger(last)) return undefined\n return last + 1\n }\n\n /**\n * A tier that refuses ranged bodies, in the three ways HTTP has to say\n * so. `411` and `416` refuse the body's framing; `400` is RFC 9110\n * section 14.5's answer from a resource that \"does not support partial\n * PUT requests\", and it is what both of this repository's cache services\n * send. Falling back on it can mask no genuine `400`: the whole-blob\n * `PUT` that follows presents the same URL, digest, and credential, so a\n * real refusal comes straight back on that request.\n */\n const rejectsRanges = (status: number): boolean => status === 400 || status === 411 || status === 416\n\n /**\n * Whether the tier holds exactly `total` bytes under `digest`, read from\n * the `Content-Length` of a `HEAD` answer.\n *\n * This is the only claim about a chunked transfer this client will make on\n * its own behalf. A tier that answers `HEAD` without a length is not\n * refused \u2014 it simply proves nothing, so the caller sends the blob whole.\n */\n const holdsWhole = (digest: ArtifactStore.Digest, total: number) =>\n send(\"an existence probe\", authorized(HttpClientRequest.head(casUrl(digest))), (response) =>\n Effect.sync(() => {\n if (!isOk(response)) return false\n const declared = response.headers[\"content-length\"]\n return declared !== undefined && Number(declared) === total\n }))\n\n /**\n * Sends `bytes` as `Content-Range` chunks, resuming from whatever prefix\n * the tier reports. Answers `false` when the tier has not proved it stored\n * the whole blob, which is the caller's signal to send it whole.\n *\n * A `2xx` answer is never by itself proof of that. A tier that ignores\n * `Content-Range`, such as plain WebDAV `PUT`, which Bazel documents as a\n * supported dumb-HTTP cache, answers every request in this sequence `201`\n * while storing only the last body it was handed, which for the probe is\n * zero bytes. So only a `308` continues the sequence, and the completing\n * chunk's `2xx` is confirmed with `HEAD` before the caller is told the\n * digest is published. Everything else falls back, and the whole-blob\n * `PUT` overwrites whatever partial body the sequence left behind.\n */\n const uploadChunked = (digest: ArtifactStore.Digest, bytes: Uint8Array, chunk: number) =>\n Effect.gen(function*() {\n const total = bytes.byteLength\n const ranged = (range: string, body: Uint8Array) =>\n send(\n \"an upload\",\n authorized(HttpClientRequest.put(casUrl(digest))).pipe(\n HttpClientRequest.bodyUint8Array(body, \"application/octet-stream\"),\n HttpClientRequest.setHeader(\"content-range\", range)\n ),\n // Only status and headers are used after the response is released.\n Effect.succeed\n )\n // A blob the tier already holds whole costs one `HEAD` and no body.\n // Asking first is also what makes the probe's answer readable: a `2xx`\n // to it can no longer mean \"already there\", so the one reading left is\n // a tier that ignored the header and stored the empty body.\n if (yield* holdsWhole(digest, total)) return true\n // The probe carries no bytes: it exists to learn where to start, so\n // that an interrupted transfer costs one round trip rather than the\n // whole blob again.\n const probe = yield* ranged(`bytes */${total}`, new Uint8Array(0))\n if (isOk(probe) || rejectsRanges(probe.status)) return false\n if (probe.status !== 308 && probe.status !== 404) {\n return yield* Effect.fail(unexpectedStatus(\"an upload probe\", probe.status))\n }\n let offset = probe.status === 308 ? resumeOffset(probe) ?? 0 : 0\n while (offset < total) {\n const end = Math.min(offset + chunk, total)\n const answer = yield* ranged(`bytes ${offset}-${end - 1}/${total}`, bytes.subarray(offset, end))\n if (answer.status !== 308) {\n // The completing chunk may answer `2xx`; anything before it may\n // not, because a tier that is still owed bytes and reports success\n // is a tier that is not reading the header. Both are checked\n // against what the tier actually holds below.\n if (end === total && isOk(answer)) break\n if (isOk(answer) || rejectsRanges(answer.status)) return false\n return yield* Effect.fail(unexpectedStatus(\"an upload\", answer.status))\n }\n // A tier that reports a longer prefix than this chunk delivered has\n // more than we just sent, so the next chunk starts there. Never\n // backwards: an answer naming a shorter prefix would loop forever.\n offset = Math.max(end, resumeOffset(answer) ?? 0)\n }\n return yield* holdsWhole(digest, total)\n })\n\n const put: ArtifactStore.Service[\"put\"] = Effect.fn(\"RemoteArtifacts.put\")((bytes: Uint8Array) =>\n Effect.flatMap(ArtifactStore.snapshotBytes(bytes), (snapshot) =>\n Effect.gen(function*() {\n const digest = yield* ArtifactStore.measureBytes(snapshot)\n yield* Effect.annotateCurrentSpan({ digest })\n yield* within(\n \"an upload\",\n uploadDeadline,\n Effect.gen(function*() {\n if (chunkBytes === undefined || snapshot.byteLength <= chunkBytes) {\n yield* uploadWhole(digest, snapshot)\n return\n }\n const transferred = yield* uploadChunked(digest, snapshot, chunkBytes)\n if (!transferred) yield* uploadWhole(digest, snapshot)\n })\n )\n return digest\n }))\n )\n\n const get: ArtifactStore.Service[\"get\"] = Effect.fn(\"RemoteArtifacts.get\")((digest: string) =>\n Effect.gen(function*() {\n const validated = yield* ArtifactStore.validateDigest(digest)\n yield* Effect.annotateCurrentSpan({ digest: validated })\n // The deadline covers the whole exchange \u2014 request, headers, body \u2014\n // because a tier that stalls mid-body is exactly as unanswering as\n // one that never accepts the connection.\n const bytes = yield* within(\"a download\", downloadDeadline, download(validated))\n // The shared tier is the least trusted store there is: it is written\n // by machines this one has never met. Verifying the address here means\n // a mis-serving or compromised cache can waste a round trip but can\n // never substitute content.\n const measured = yield* ArtifactStore.measureBytes(bytes)\n if (measured !== validated) {\n return yield* Effect.fail(\n new ArtifactStore.ArtifactCorruption({\n code: \"artifact_corruption\",\n recordedDigest: validated,\n measuredDigest: measured\n })\n )\n }\n return bytes\n })\n )\n\n const has: ArtifactStore.Service[\"has\"] = Effect.fn(\"RemoteArtifacts.has\")((digest: string) =>\n Effect.gen(function*() {\n const validated = yield* ArtifactStore.validateDigest(digest)\n yield* Effect.annotateCurrentSpan({ digest: validated })\n const response = yield* within(\n \"an existence probe\",\n requestDeadline,\n send(\"an existence probe\", authorized(HttpClientRequest.head(casUrl(validated))), Effect.succeed)\n )\n if (response.status === 404) return false\n if (!isOk(response)) return yield* Effect.fail(unexpectedStatus(\"an existence probe\", response.status))\n return true\n })\n )\n\n const findMissing: ArtifactStore.Service[\"findMissing\"] = Effect.fn(\"RemoteArtifacts.findMissing\")(\n (digests: Iterable<string>) =>\n Effect.gen(function*() {\n const requested = [...new Set(digests)]\n if (requested.length === 0) return []\n // The batch travels in a JSON body rather than a path, but an\n // unusable address is still an unusable address, and the local tier\n // refuses the same batch wholesale rather than probing part of it.\n const validated: Array<ArtifactStore.Digest> = []\n for (const digest of requested) validated.push(yield* ArtifactStore.validateDigest(digest))\n yield* Effect.annotateCurrentSpan({ count: validated.length })\n const missing = new Set<string>()\n for (let offset = 0; offset < validated.length; offset += maxFindMissingBatchDigests) {\n const batch = validated.slice(offset, offset + maxFindMissingBatchDigests)\n const requestBody = encoder.encode(JSON.stringify({ digests: batch }))\n /* v8 ignore next 3 -- strict 64-byte digests plus the 1,000-entry batch cap make this\n * at most 67,013 bytes; keep the protocol assertion beside serialization in case either invariant changes. */\n if (requestBody.byteLength > maxFindMissingBodyBytes) {\n return yield* Effect.fail(configurationFailure(\"remote artifact findMissing request exceeds 256 KiB\"))\n }\n const decoded = yield* within(\n \"a batched existence probe\",\n requestDeadline,\n send(\n \"a batched existence probe\",\n authorized(HttpClientRequest.post(`${base}/cas/findMissing`)).pipe(\n HttpClientRequest.bodyUint8Array(requestBody, \"application/json\")\n ),\n (response) =>\n Effect.gen(function*() {\n if (!isOk(response)) {\n return yield* Effect.fail(unexpectedStatus(\"a batched existence probe\", response.status))\n }\n const bodyBytes = yield* readBounded(\n response,\n \"a batched existence probe\",\n maxFindMissingResponseBytes\n )\n const body = yield* Effect.try({\n try: () => JSON.parse(decoder.decode(bodyBytes)) as unknown,\n catch: () => transportFailure(\"a batched existence probe body\")\n })\n return yield* Schema.decodeUnknownEffect(FindMissingResponse)(body).pipe(\n Effect.mapError(() => transportFailure(\"a batched existence probe body\"))\n )\n })\n )\n )\n const asked = new Set<string>(batch)\n for (const digest of decoded.missing) if (asked.has(digest)) missing.add(digest)\n }\n // \"The returned set is guaranteed to be a subset of `digests`\"\n // (`MissingDigestsFinder`). Bazel gets that from the server; we\n // enforce it on this side too, because a server that answered with\n // an unrequested digest would otherwise make the caller upload bytes\n // it never asked about.\n return validated.filter((digest) => missing.has(digest))\n })\n )\n\n return { put, get, has, findMissing, downloadPolicy }\n })\n\n/**\n * Provides a remote artifact store as the `ArtifactStore` tag.\n *\n * Composing this *alone* makes every artifact read and write a network round\n * trip. The intended production shape is\n * {@link CombinedArtifacts.layer | CombinedArtifacts}, with this as its remote\n * tier.\n *\n * @category layers\n * @since 1.0.0-rc.0\n * @slop\n */\nexport const layer = (\n options: Options\n): Layer.Layer<ArtifactStore.ArtifactStore, ArtifactStore.ArtifactStoreError, HttpClient.HttpClient> =>\n Layer.effect(ArtifactStore.ArtifactStore)(make(options))\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAuDA,eAA0B;AAC1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ACvB;AAAA,gBACA;AAAA,gBACA;AAAA,cACF;AACA,oBAAM,OAAO,OAAO,OAAO,IAAI;AAAA,gBAC7B,KAAK,MAAM,KAAK,MAAM,QAAQ,OAAO,SAAS,CAAC;AAAA,gBAC/C,OAAO,MAAM,iBAAiB,gCAAgC;AAAA,cAChE,CAAC;AACD,qBAAO,OAAO,OAAO,oBAAoB,mBAAmB,EAAE,IAAI,EAAE;AAAA,gBAClE,OAAO,SAAS,MAAM,iBAAiB,gCAAgC,CAAC;AAAA,cAC1E;AAAA,YACF,CAAC;AAAA,UACL;AAAA,QACF;AACA,cAAM,QAAQ,IAAI,IAAY,KAAK;AACnC,mBAAW,UAAU,QAAQ,QAAS,KAAI,MAAM,IAAI,MAAM,EAAG,SAAQ,IAAI,MAAM;AAAA,MACjF;AAMA,aAAO,UAAU,OAAO,CAAC,WAAW,QAAQ,IAAI,MAAM,CAAC;AAAA,IACzD,CAAC;AAAA,EACL;AAEA,SAAO,EAAE,KAAK,KAAK,KAAK,aAAa,eAAe;AACtD,CAAC;AAcI,MAAM,QAAQ,CACnB,YAEA,MAAM,OAAO,cAAc,aAAa,EAAE,KAAK,OAAO,CAAC;",
4
+ "sourcesContent": ["/**\n * The shared artifact tier: an {@link ArtifactStore.Service} spoken over HTTP.\n *\n * The wire protocol mirrors Bazel's dumb-HTTP remote cache, class\n * `HttpCacheClient` in `com.google.devtools.build.lib.remote.http`, which\n * documents it as: \"CAS (Content Addressable Storage) blobs are stored under\n * the path `/cas/base16-key`\", uploaded with `PUT`, downloaded with `GET`. We add\n * `HEAD /cas/{digest}` for a single existence probe and\n * `POST /cas/findMissing` for the batched one, because Bazel's HTTP client has\n * no `findMissingDigests` at all \u2014 it answers \"everything is missing\" and\n * re-uploads \u2014 and a batched probe is the whole reason\n * `MissingDigestsFinder` exists in the gRPC client.\n *\n * Transport is Effect's own `HttpClient` tag. There is no lower transport in\n * this repository, and there does not need to be one: the kernel already\n * decorates that tag with `net:get`/`net:post` capability checks, so a remote\n * artifact fetch is permission-checked like every other egress.\n *\n * ## Chunked uploads\n *\n * With `chunkBytes` set, a blob past that size travels as a sequence of\n * `PUT /cas/{digest}` requests carrying `Content-Range: bytes {a}-{b}/{total}`,\n * preceded by a `HEAD /cas/{digest}` existence probe and a\n * `Content-Range: bytes *\\/{total}` probe with an empty body. This is the\n * resumable-upload shape HTTP already has: the range probe asks what prefix\n * the tier holds and `308` means \"keep going\", with a `Range: bytes=0-{last}`\n * header on the probe or on a chunk answer moving the offset.\n *\n * Only a `308` continues the sequence, and the completing chunk's `2xx` is\n * confirmed with `HEAD` before `put` reports the digest as published. A `2xx`\n * anywhere else, to the empty probe or to a chunk the tier is still owed bytes\n * after, reads as a tier that ignored `Content-Range` and stored the body it\n * was handed, which is what plain WebDAV `PUT` does. That tier, the one that\n * answers `400`, `411`, or `416`, and the one whose `HEAD` will not confirm\n * the stored length all get the same treatment: one whole-blob `PUT`, which\n * overwrites the partial body the sequence left behind. So the blob always\n * lands whole, and the cost of turning the dial on against a server that\n * never learned about it is round trips, never a digest published over bytes\n * the server does not hold.\n *\n * This repository's own hosted and self-hosted cache services are such\n * servers: neither implements the resumable sequence, and both refuse a\n * ranged `PUT` with `400`, RFC 9110 section 14.5's answer from a resource\n * that does not support partial `PUT`. Against them `chunkBytes` costs the\n * probe round trip and the blob travels whole, and both cap one request body\n * at 16 MiB, so a blob past that cap is refused with `413` in either mode.\n *\n * The endpoint and its credentials arrive as **layer construction options**.\n * They are capabilities, never step inputs: they are not hashed into a step\n * key, journaled, or returned in a recorded result. Invalid endpoint errors are\n * sanitized before they cross the boundary.\n *\n * @since 1.0.0-rc.0\n */\n\nimport * as Duration from \"effect/Duration\"\nimport * as Effect from \"effect/Effect\"\nimport * as FetchHttpClient from \"effect/http/FetchHttpClient\"\nimport * as Headers from \"effect/http/Headers\"\nimport * as HttpClient from \"effect/http/HttpClient\"\nimport * as HttpClientRequest from \"effect/http/HttpClientRequest\"\nimport type * as HttpClientResponse from \"effect/http/HttpClientResponse\"\nimport * as Layer from \"effect/Layer\"\nimport * as Option from \"effect/Option\"\nimport * as Schema from \"effect/Schema\"\nimport * as Stream from \"effect/Stream\"\nimport * as ArtifactStore from \"./ArtifactStore.ts\"\n\n/**\n * How eagerly a composition materializes shared blobs into its local store.\n *\n * @category models\n * @since 1.0.0-rc.0\n */\nexport const DownloadPolicy = Schema.Literals([\"all\", \"toplevel\", \"minimal\"] as const)\n\n/**\n * How eagerly a composition materializes remote blobs.\n *\n * @category models\n * @since 1.0.0-rc.0\n */\nexport type DownloadPolicy = typeof DownloadPolicy.Type\n\n/**\n * Every download policy, in materialization order.\n *\n * @category models\n * @since 1.0.0-rc.0\n */\nexport const downloadPolicies: ReadonlyArray<DownloadPolicy> = Object.freeze([\"all\", \"toplevel\", \"minimal\"])\n\n/**\n * A remote artifact tier, which is an ordinary store that also states how\n * eagerly a composition reading through it should materialize blobs locally.\n *\n * @category models\n * @since 1.0.0-rc.0\n */\nexport interface Service extends ArtifactStore.Service {\n readonly downloadPolicy: DownloadPolicy\n}\n\n/**\n * Reads the download policy a store declares, or `undefined` for a store that\n * declares none \u2014 every local store, and any foreign implementation.\n *\n * @category getters\n * @since 1.0.0-rc.0\n */\nexport const downloadPolicyOf = (store: ArtifactStore.Service): DownloadPolicy | undefined => {\n const declared = (store as { readonly downloadPolicy?: unknown }).downloadPolicy\n return Schema.is(DownloadPolicy)(declared) ? declared : undefined\n}\n\n/**\n * How to reach the shared artifact tier.\n *\n * @category models\n * @since 1.0.0-rc.0\n * @slop\n */\nexport interface Options {\n /**\n * The cache root, e.g. `https://cache.example.com`. `/cas/{digest}` and\n * `/cas/findMissing` are resolved beneath it. A trailing slash is ignored.\n *\n * Four shapes are refused at construction as `invalid_configuration`, before\n * any request leaves and therefore before `headers` can reach a wire: a value\n * that is not a string, one no `URL` parser accepts, a scheme other than\n * `https:` (plain `http:` is accepted only for the literal loopback hosts\n * `localhost`, `127.0.0.1`, and `[::1]`), and an endpoint carrying\n * userinfo, a query, or a fragment. The\n * last rule keeps a credential out of a place nothing here would redact: an\n * endpoint is interpolated into every request path and into span attributes.\n *\n * The refusal message names only the violated rule and never echoes the\n * endpoint, so a rejected `https://user:secret@host` cannot leak its\n * credential into a log line or a durable error.\n */\n readonly endpoint: string\n /**\n * Headers sent with every request \u2014 an `Authorization` bearer token, a\n * tenant header, whatever the deployment's gateway wants. This is the\n * credential seam, and it is deliberately construction-time: a credential\n * that arrived as a step input would be hashed into a step key and persisted\n * everywhere the journal goes.\n *\n * The protocol wins every collision. These headers are applied to a request\n * first. Configured `content-type`, `content-length`, and `content-range`\n * are removed, then computed only where the protocol needs them. Whole-blob\n * uploads, including fallbacks, never carry `content-range`.\n * All configured header names are redacted in HTTP spans, in addition to\n * the host's existing redaction rules.\n *\n * A name that is not an HTTP token, or a value longer than 16 KiB or\n * carrying a control character (CR and LF included), is refused at\n * construction as `invalid_configuration`.\n */\n readonly headers?: Record<string, string> | undefined\n /**\n * The deadline on a single download, from the request leaving to the last\n * body byte arriving. A shared tier that stops answering must fail the read\n * rather than park it forever. `CombinedArtifacts.get` propagates a remote\n * `ArtifactStoreError`, including a download timeout. Only opportunistic\n * uploads from `CombinedArtifacts.put` ignore remote failures;\n * `ArtifactSync.hydrate` separately turns a failure into replay rejection.\n * Defaults to 60 seconds, Bazel's `--remote_timeout`\n * default for the same REST protocol (its `RemoteOptions`: \"For the REST\n * cache, this is both the connect and the read timeout\").\n */\n readonly downloadTimeout?: Duration.Input | undefined\n /**\n * Deadline for an upload, including every resume probe and chunk. Defaults\n * to 60 seconds.\n */\n readonly uploadTimeout?: Duration.Input | undefined\n /**\n * Deadline for an existence probe or one `findMissing` batch, including its\n * response body. Defaults to 60 seconds.\n */\n readonly requestTimeout?: Duration.Input | undefined\n /**\n * The largest download this client will buffer, in bytes. The body is read\n * incrementally and abandoned the moment it exceeds the bound \u2014 and refused\n * outright when `Content-Length` already declares the excess \u2014 so a\n * mis-serving or hostile cache cannot make this process buffer and hash an\n * arbitrarily large body before the digest check refuses it. Defaults to\n * 256 MiB.\n */\n readonly maxDownloadBytes?: number | undefined\n /**\n * Largest `findMissing` response body accepted. Defaults to the protocol's\n * 256 KiB request/response bound and may only lower that bound.\n */\n readonly maxFindMissingResponseBytes?: number | undefined\n /**\n * Above this many bytes an upload travels as a sequence of `Content-Range`\n * `PUT`s instead of one whole-blob body, and a transfer that died partway\n * resumes from the prefix the tier kept rather than starting over. See\n * {@link module:RemoteArtifacts | the chunked upload protocol} below.\n *\n * Absent by default: every upload is one whole-blob `PUT`, which is what\n * Bazel's dumb-HTTP client does and what every deployment already serving\n * this protocol expects. Set it when a proxy caps request bodies, or when\n * artifacts are large enough that losing a transfer is expensive.\n *\n * The repository's own hosted and self-hosted cache services answer the\n * ranged probe with `400`, so against them this dial degrades to one\n * whole-blob `PUT`, and their 16 MiB body cap refuses a larger blob with\n * `413` in either mode.\n */\n readonly chunkBytes?: number | undefined\n /**\n * How eagerly a composition reading through this tier materializes blobs\n * into its local store. This is Bazel's `--remote_download_{all,toplevel,\n * minimal}` dial (`RemoteOutputChecker`), and it lives here because the\n * shared tier is the thing being conserved.\n *\n * - `all` (the default) prefetches every referenced blob when a replay is\n * admitted, so the replay reads local bytes.\n * - `toplevel` prefetches nothing and materializes a blob into the local\n * store on the first read that actually needs it.\n * - `minimal` prefetches nothing and materializes nothing: a read is served\n * straight from the shared tier and the local store never grows.\n *\n * `CombinedArtifacts.get` honors the last two; `@smthrs/engine-store`'s\n * `ArtifactSync.hydrate` honors the first.\n */\n readonly downloadPolicy?: DownloadPolicy | undefined\n}\n\n/**\n * The default download deadline. 60 seconds is Bazel's `--remote_timeout`\n * default, governing the same dumb-HTTP cache protocol.\n */\nconst defaultDownloadTimeout = Duration.seconds(60)\nconst defaultUploadTimeout = Duration.seconds(60)\nconst defaultRequestTimeout = Duration.seconds(60)\n\n/**\n * The default bound on a downloaded blob: 256 MiB. Artifacts are spilled step\n * values, not media libraries; a body past this size is far more likely a\n * mis-serving cache than a legitimate blob, and the dial is per-store for a\n * deployment that knows better.\n */\nconst defaultMaxDownloadBytes = 256 * 1024 * 1024\nconst maxFindMissingBatchDigests = 1_000\nconst maxFindMissingBodyBytes = 256 * 1024\nconst encoder = new TextEncoder()\nconst decoder = new TextDecoder(\"utf-8\", { fatal: true })\n\n/**\n * The credential-free part of a transport failure: the HTTP client's reason\n * tag and the first operating-system error code (`ECONNREFUSED`, `ENOTFOUND`,\n * `CERT_HAS_EXPIRED`) in its cause chain. Never the request, the URL, the\n * headers, or free-form text, any of which may carry a credential.\n */\nconst transportReason = (cause: unknown): string | undefined => {\n const reason = (cause as { readonly reason?: { readonly _tag?: unknown } } | null)?.reason\n const tag = typeof reason?._tag === \"string\" ? reason._tag : undefined\n let code: string | undefined\n let current: unknown = reason ?? cause\n for (let depth = 0; depth < 8 && typeof current === \"object\" && current !== null; depth++) {\n const candidate = (current as { readonly code?: unknown }).code\n if (typeof candidate === \"string\" && /^[A-Z][A-Z0-9_]{1,63}$/.test(candidate)) {\n code = candidate\n break\n }\n current = (current as { readonly cause?: unknown }).cause\n }\n const parts = [tag, code].filter((part): part is string => part !== undefined)\n return parts.length === 0 ? undefined : parts.join(\" \")\n}\n\nconst transportFailure = (operation: string, cause?: unknown): ArtifactStore.ArtifactStoreError => {\n const reason = transportReason(cause)\n return new ArtifactStore.ArtifactStoreError({\n code: \"transport_failed\",\n message: `the remote artifact tier refused ${operation}${reason === undefined ? \"\" : ` (${reason})`}`\n })\n}\n\nconst unexpectedStatus = (operation: string, status: number): ArtifactStore.ArtifactStoreError =>\n new ArtifactStore.ArtifactStoreError({\n code: \"transport_failed\",\n message: `the remote artifact tier answered ${operation} with HTTP ${status}`\n })\n\nconst invalidOption = (name: string): ArtifactStore.ArtifactStoreError =>\n new ArtifactStore.ArtifactStoreError({\n code: \"invalid_configuration\",\n message: `invalid remote artifact option: ${name}`\n })\n\n/** The batched-probe response body. */\nconst FindMissingResponse = Schema.Struct({ missing: Schema.Array(Schema.String) })\n\n/** An RFC 9110 field-name token. */\nconst headerName = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/\n\n/** Whether a header value carries a C0 control character or DEL, CR and LF included. */\nconst hasControlText = (value: string): boolean => {\n for (let index = 0; index < value.length; index++) {\n const unit = value.charCodeAt(index)\n if (unit <= 0x1f || unit === 0x7f) return true\n }\n return false\n}\n\nconst configurationFailure = (message: string): ArtifactStore.ArtifactStoreError =>\n new ArtifactStore.ArtifactStoreError({ code: \"invalid_configuration\", message })\n\n/**\n * A 2xx is success, a 404 is a miss, and everything else is a transport\n * failure. Bazel's HTTP client takes the same three-way split; it is the only\n * classification a dumb-HTTP cache can support, because there is no richer\n * error envelope on the wire.\n */\nconst isOk = (response: HttpClientResponse.HttpClientResponse): boolean =>\n response.status >= 200 && response.status < 300\n\n/**\n * Builds a remote artifact store over Effect's `HttpClient`.\n *\n * @category constructors\n * @since 1.0.0-rc.0\n * @slop\n */\nexport const make = (\n options: Options\n): Effect.Effect<Service, ArtifactStore.ArtifactStoreError, HttpClient.HttpClient> =>\n Effect.gen(function*() {\n const configured = yield* Effect.try({\n try: () => {\n const headers = options.headers === undefined\n ? undefined\n : Object.freeze(Object.fromEntries(\n Object.entries(options.headers).map(([name, value]) => {\n // Refused here, once, rather than on every request as an opaque\n // transport failure: a CRLF in a value or a space in a name can\n // never become a valid header.\n if (typeof value !== \"string\" || value.length > 16 * 1024 || hasControlText(value)) {\n throw new TypeError(\"header value\")\n }\n if (!headerName.test(name)) throw new TypeError(\"header name\")\n return [name, value]\n })\n ))\n return {\n endpoint: options.endpoint,\n headers,\n downloadTimeout: options.downloadTimeout,\n uploadTimeout: options.uploadTimeout,\n requestTimeout: options.requestTimeout,\n maxDownloadBytes: options.maxDownloadBytes,\n maxFindMissingResponseBytes: options.maxFindMissingResponseBytes,\n chunkBytes: options.chunkBytes,\n downloadPolicy: options.downloadPolicy\n }\n },\n catch: () => invalidOption(\"options\")\n })\n if (typeof configured.endpoint !== \"string\") return yield* Effect.fail(invalidOption(\"endpoint\"))\n const endpoint = yield* Effect.try({\n try: () => new URL(configured.endpoint),\n catch: () => configurationFailure(\"invalid remote artifact endpoint\")\n })\n // A self-hosted cache on this machine is reachable over plain HTTP, and\n // loopback traffic never leaves the host. Only literal loopback names are\n // exempt: a `*.localhost` name reaches loopback only on a resolver that\n // follows RFC 6761, and one that forwards it to DNS would carry the\n // credential headers over plaintext to whatever host answers.\n const loopback = endpoint.hostname === \"localhost\" || endpoint.hostname === \"127.0.0.1\" ||\n endpoint.hostname === \"[::1]\"\n if (endpoint.protocol !== \"https:\" && !(endpoint.protocol === \"http:\" && loopback)) {\n return yield* Effect.fail(configurationFailure(\"remote artifact endpoint must use HTTPS\"))\n }\n // An empty query or fragment (\"https://h/p?\") parses as \"\" yet survives\n // serialization, so the delimiter itself is refused in the href.\n if (\n endpoint.username !== \"\" || endpoint.password !== \"\" || endpoint.search !== \"\" || endpoint.hash !== \"\" ||\n endpoint.href.includes(\"?\") || endpoint.href.includes(\"#\")\n ) {\n return yield* Effect.fail(\n configurationFailure(\"remote artifact endpoint must not contain credentials, a query, or a fragment\")\n )\n }\n endpoint.pathname = endpoint.pathname.replace(/\\/+$/, \"\")\n const base = endpoint.toString().replace(/\\/+$/, \"\")\n const headers = configured.headers\n const client = yield* HttpClient.HttpClient\n const credentialNames = Object.keys(headers ?? {}).map((name) => name.toLowerCase())\n /**\n * Protocol-owned headers are computed from each request's body and range,\n * never inherited from credentials, including on a whole-blob fallback.\n */\n const authorized = (request: HttpClientRequest.HttpClientRequest): HttpClientRequest.HttpClientRequest =>\n (headers === undefined ? request : HttpClientRequest.setHeaders(request, headers)).pipe(\n HttpClientRequest.removeHeader(\"content-type\"),\n HttpClientRequest.removeHeader(\"content-length\"),\n HttpClientRequest.removeHeader(\"content-range\")\n )\n // The address is a URL path segment, so it is refused before it is\n // interpolated and percent-encoded when it is: an address carrying a\n // separator or a `..` would otherwise point this client at a different\n // resource on the configured endpoint. `ArtifactStore.validateDigest` is\n // the same guard the filesystem tier applies to the same untrusted input \u2014\n // a digest read back out of a durable row.\n const casUrl = (digest: ArtifactStore.Digest) => `${base}/cas/${digest}`\n const scopedClient = HttpClient.withScope(client)\n /**\n * Keep body processing inside the exchange; abort unused responses on every exit.\n *\n * Redirects are never followed. A fetch-based client follows them by\n * default and strips only `Authorization` on a cross-origin hop, so a\n * tier answering `3xx` would otherwise receive the credential headers at\n * another origin or over plain HTTP. Manual mode hands the `3xx` back,\n * where it is an unexpected status (or, for `308`, the resumable-upload\n * answer this protocol already speaks).\n */\n const send = <A, E, R>(\n operation: string,\n request: HttpClientRequest.HttpClientRequest,\n use: (response: HttpClientResponse.HttpClientResponse) => Effect.Effect<A, E, R>\n ): Effect.Effect<A, E | ArtifactStore.ArtifactStoreError, R> =>\n Effect.gen(function*() {\n const redactedNames = yield* Headers.CurrentRedactedNames\n const requestInit = yield* Effect.serviceOption(FetchHttpClient.RequestInit)\n return yield* scopedClient.execute(request).pipe(\n Effect.provideService(FetchHttpClient.RequestInit, {\n ...Option.getOrUndefined(requestInit),\n redirect: \"manual\"\n }),\n Effect.mapError((cause) => transportFailure(operation, cause)),\n Effect.flatMap(use),\n Effect.provideService(Headers.CurrentRedactedNames, [...redactedNames, ...credentialNames]),\n Effect.scoped\n )\n })\n const parseDeadline = (name: string, input: Duration.Input | undefined, fallback: Duration.Duration) =>\n Effect.gen(function*() {\n const parsed = Duration.fromInput(input ?? fallback)\n if (\n Option.isNone(parsed) ||\n !Number.isFinite(Duration.toMillis(parsed.value)) ||\n Duration.toMillis(parsed.value) <= 0\n ) {\n return yield* Effect.fail(invalidOption(name))\n }\n return parsed.value\n })\n const downloadDeadline = yield* parseDeadline(\n \"downloadTimeout\",\n configured.downloadTimeout,\n defaultDownloadTimeout\n )\n const uploadDeadline = yield* parseDeadline(\"uploadTimeout\", configured.uploadTimeout, defaultUploadTimeout)\n const requestDeadline = yield* parseDeadline(\"requestTimeout\", configured.requestTimeout, defaultRequestTimeout)\n const maxDownloadBytes = configured.maxDownloadBytes ?? defaultMaxDownloadBytes\n if (!Number.isSafeInteger(maxDownloadBytes) || maxDownloadBytes <= 0) {\n return yield* Effect.fail(invalidOption(\"maxDownloadBytes\"))\n }\n const maxFindMissingResponseBytes = configured.maxFindMissingResponseBytes ?? maxFindMissingBodyBytes\n if (\n !Number.isSafeInteger(maxFindMissingResponseBytes) ||\n maxFindMissingResponseBytes <= 0 ||\n maxFindMissingResponseBytes > maxFindMissingBodyBytes\n ) {\n return yield* Effect.fail(invalidOption(\"maxFindMissingResponseBytes\"))\n }\n const chunkBytes = configured.chunkBytes\n if (chunkBytes !== undefined && (!Number.isSafeInteger(chunkBytes) || chunkBytes <= 0)) {\n return yield* Effect.fail(invalidOption(\"chunkBytes\"))\n }\n const downloadPolicy = configured.downloadPolicy ?? \"all\"\n if (!Schema.is(DownloadPolicy)(downloadPolicy)) {\n return yield* Effect.fail(invalidOption(\"downloadPolicy\"))\n }\n const bodyTooLarge = (operation: string, received: number, bound: number): ArtifactStore.ArtifactStoreError =>\n new ArtifactStore.ArtifactStoreError({\n code: \"transport_failed\",\n message: `the remote artifact tier answered ${operation} with ${received} bytes, past the ${bound}-byte bound`\n })\n const timedOut = (operation: string): ArtifactStore.ArtifactStoreError =>\n new ArtifactStore.ArtifactStoreError({\n code: \"transport_failed\",\n message: `the remote artifact tier did not finish ${operation} within its configured deadline`\n })\n const within = <A, E, R>(\n operation: string,\n deadline: Duration.Duration,\n effect: Effect.Effect<A, E, R>\n ): Effect.Effect<A, E | ArtifactStore.ArtifactStoreError, R> =>\n effect.pipe(\n Effect.timeout(deadline),\n Effect.catchTag(\"TimeoutError\", () => Effect.fail(timedOut(operation)))\n )\n /**\n * Reads a download body incrementally, refusing it the moment it exceeds\n * the size bound; a `Content-Length` that already declares the excess is\n * refused before a single body byte is read. Streaming instead of\n * buffering the whole body means the guard fires at most one chunk past\n * the bound, never after an arbitrarily large response is in memory.\n */\n const readBounded = (\n response: HttpClientResponse.HttpClientResponse,\n operation: string,\n bound: number\n ) =>\n Effect.gen(function*() {\n const declared = response.headers[\"content-length\"]\n if (declared !== undefined) {\n const declaredBytes = Number(declared)\n if (!/^\\d+$/.test(declared) || !Number.isSafeInteger(declaredBytes)) {\n return yield* Effect.fail(transportFailure(`${operation} body`))\n }\n if (declaredBytes > bound) return yield* Effect.fail(bodyTooLarge(operation, declaredBytes, bound))\n }\n const chunks: Array<Uint8Array> = []\n let received = 0\n yield* Stream.runForEach(response.stream, (chunk: Uint8Array) =>\n Effect.suspend(() => {\n received += chunk.byteLength\n if (received > bound) return Effect.fail(bodyTooLarge(operation, received, bound))\n chunks.push(new Uint8Array(chunk))\n return Effect.void\n })).pipe(\n Effect.catchTag(\"HttpClientError\", (cause) =>\n // An absent body is zero bytes, not a transport refusal: the\n // digest check in `get` is the arbiter of whether empty content\n // is the requested artifact.\n cause.reason._tag === \"EmptyBodyError\"\n ? Effect.void\n : Effect.fail(transportFailure(`${operation} body`)))\n )\n const bytes = new Uint8Array(received)\n let offset = 0\n for (const chunk of chunks) {\n bytes.set(chunk, offset)\n offset += chunk.byteLength\n }\n return bytes\n })\n /** The network half of `get`: request, status split, bounded body read. */\n const download = (digest: ArtifactStore.Digest) =>\n send(\"a download\", authorized(HttpClientRequest.get(casUrl(digest))), (response) =>\n Effect.gen(function*() {\n if (response.status === 404) {\n return yield* Effect.fail(new ArtifactStore.ArtifactMissing({ code: \"artifact_missing\", digest }))\n }\n if (!isOk(response)) return yield* Effect.fail(unexpectedStatus(\"a download\", response.status))\n return yield* readBounded(response, \"a download\", maxDownloadBytes)\n }))\n\n /** One whole-blob `PUT`: the protocol every dumb-HTTP CAS already speaks. */\n const uploadWhole = (digest: ArtifactStore.Digest, bytes: Uint8Array) =>\n send(\n \"an upload\",\n authorized(HttpClientRequest.put(casUrl(digest))).pipe(\n HttpClientRequest.bodyUint8Array(bytes, \"application/octet-stream\")\n ),\n (response) => isOk(response) ? Effect.void : Effect.fail(unexpectedStatus(\"an upload\", response.status))\n )\n\n /**\n * The byte after the prefix a `Range: bytes=0-{last}` header reports, or\n * `undefined` when the answer names no prefix. Only a prefix starting at\n * zero is usable: a tier holding a middle span has nothing this client can\n * continue from, so it is treated as holding nothing.\n */\n const resumeOffset = (response: HttpClientResponse.HttpClientResponse): number | undefined => {\n const header = response.headers[\"range\"]\n if (header === undefined) return undefined\n const match = /^bytes=0-(\\d+)$/.exec(header.trim())\n if (match === null) return undefined\n const last = Number(match[1])\n // A prefix past the safe integer range does not survive `Number`: the\n // decimal digits are rounded, so the offset it names is a number this\n // client made up. It proves nothing about what the tier holds, which is\n // the same as naming no prefix at all.\n if (!Number.isSafeInteger(last)) return undefined\n return last + 1\n }\n\n /**\n * A tier that refuses ranged bodies, in the three ways HTTP has to say\n * so. `411` and `416` refuse the body's framing; `400` is RFC 9110\n * section 14.5's answer from a resource that \"does not support partial\n * PUT requests\", and it is what both of this repository's cache services\n * send. Falling back on it can mask no genuine `400`: the whole-blob\n * `PUT` that follows presents the same URL, digest, and credential, so a\n * real refusal comes straight back on that request.\n */\n const rejectsRanges = (status: number): boolean => status === 400 || status === 411 || status === 416\n\n /**\n * Whether the tier holds exactly `total` bytes under `digest`, read from\n * the `Content-Length` of a `HEAD` answer.\n *\n * This is the only claim about a chunked transfer this client will make on\n * its own behalf. A tier that answers `HEAD` without a length is not\n * refused \u2014 it simply proves nothing, so the caller sends the blob whole.\n */\n const holdsWhole = (digest: ArtifactStore.Digest, total: number) =>\n send(\"an existence probe\", authorized(HttpClientRequest.head(casUrl(digest))), (response) =>\n Effect.sync(() => {\n if (!isOk(response)) return false\n const declared = response.headers[\"content-length\"]\n return declared !== undefined && Number(declared) === total\n }))\n\n /**\n * Sends `bytes` as `Content-Range` chunks, resuming from whatever prefix\n * the tier reports. Answers `false` when the tier has not proved it stored\n * the whole blob, which is the caller's signal to send it whole.\n *\n * A `2xx` answer is never by itself proof of that. A tier that ignores\n * `Content-Range`, such as plain WebDAV `PUT`, which Bazel documents as a\n * supported dumb-HTTP cache, answers every request in this sequence `201`\n * while storing only the last body it was handed, which for the probe is\n * zero bytes. So only a `308` continues the sequence, and the completing\n * chunk's `2xx` is confirmed with `HEAD` before the caller is told the\n * digest is published. Everything else falls back, and the whole-blob\n * `PUT` overwrites whatever partial body the sequence left behind.\n */\n const uploadChunked = (digest: ArtifactStore.Digest, bytes: Uint8Array, chunk: number) =>\n Effect.gen(function*() {\n const total = bytes.byteLength\n const ranged = (range: string, body: Uint8Array) =>\n send(\n \"an upload\",\n authorized(HttpClientRequest.put(casUrl(digest))).pipe(\n HttpClientRequest.bodyUint8Array(body, \"application/octet-stream\"),\n HttpClientRequest.setHeader(\"content-range\", range)\n ),\n // Only status and headers are used after the response is released.\n Effect.succeed\n )\n // A blob the tier already holds whole costs one `HEAD` and no body.\n // Asking first is also what makes the probe's answer readable: a `2xx`\n // to it can no longer mean \"already there\", so the one reading left is\n // a tier that ignored the header and stored the empty body.\n if (yield* holdsWhole(digest, total)) return true\n // The probe carries no bytes: it exists to learn where to start, so\n // that an interrupted transfer costs one round trip rather than the\n // whole blob again.\n const probe = yield* ranged(`bytes */${total}`, new Uint8Array(0))\n if (isOk(probe) || rejectsRanges(probe.status)) return false\n if (probe.status !== 308 && probe.status !== 404) {\n return yield* Effect.fail(unexpectedStatus(\"an upload probe\", probe.status))\n }\n let offset = probe.status === 308 ? resumeOffset(probe) ?? 0 : 0\n while (offset < total) {\n const end = Math.min(offset + chunk, total)\n const answer = yield* ranged(`bytes ${offset}-${end - 1}/${total}`, bytes.subarray(offset, end))\n if (answer.status !== 308) {\n // The completing chunk may answer `2xx`; anything before it may\n // not, because a tier that is still owed bytes and reports success\n // is a tier that is not reading the header. Both are checked\n // against what the tier actually holds below.\n if (end === total && isOk(answer)) break\n if (isOk(answer) || rejectsRanges(answer.status)) return false\n return yield* Effect.fail(unexpectedStatus(\"an upload\", answer.status))\n }\n // A tier that reports a longer prefix than this chunk delivered has\n // more than we just sent, so the next chunk starts there. Never\n // backwards: an answer naming a shorter prefix would loop forever.\n offset = Math.max(end, resumeOffset(answer) ?? 0)\n }\n return yield* holdsWhole(digest, total)\n })\n\n const put: ArtifactStore.Service[\"put\"] = Effect.fn(\"RemoteArtifacts.put\")((bytes: Uint8Array) =>\n Effect.flatMap(ArtifactStore.snapshotBytes(bytes), (snapshot) =>\n Effect.gen(function*() {\n const digest = yield* ArtifactStore.measureBytes(snapshot)\n yield* Effect.annotateCurrentSpan({ digest })\n yield* within(\n \"an upload\",\n uploadDeadline,\n Effect.gen(function*() {\n if (chunkBytes === undefined || snapshot.byteLength <= chunkBytes) {\n yield* uploadWhole(digest, snapshot)\n return\n }\n const transferred = yield* uploadChunked(digest, snapshot, chunkBytes)\n if (!transferred) yield* uploadWhole(digest, snapshot)\n })\n )\n return digest\n }))\n )\n\n const get: ArtifactStore.Service[\"get\"] = Effect.fn(\"RemoteArtifacts.get\")((digest: string) =>\n Effect.gen(function*() {\n const validated = yield* ArtifactStore.validateDigest(digest)\n yield* Effect.annotateCurrentSpan({ digest: validated })\n // The deadline covers the whole exchange \u2014 request, headers, body \u2014\n // because a tier that stalls mid-body is exactly as unanswering as\n // one that never accepts the connection.\n const bytes = yield* within(\"a download\", downloadDeadline, download(validated))\n // The shared tier is the least trusted store there is: it is written\n // by machines this one has never met. Verifying the address here means\n // a mis-serving or compromised cache can waste a round trip but can\n // never substitute content.\n const measured = yield* ArtifactStore.measureBytes(bytes)\n if (measured !== validated) {\n return yield* Effect.fail(\n new ArtifactStore.ArtifactCorruption({\n code: \"artifact_corruption\",\n recordedDigest: validated,\n measuredDigest: measured\n })\n )\n }\n return bytes\n })\n )\n\n const has: ArtifactStore.Service[\"has\"] = Effect.fn(\"RemoteArtifacts.has\")((digest: string) =>\n Effect.gen(function*() {\n const validated = yield* ArtifactStore.validateDigest(digest)\n yield* Effect.annotateCurrentSpan({ digest: validated })\n const response = yield* within(\n \"an existence probe\",\n requestDeadline,\n send(\"an existence probe\", authorized(HttpClientRequest.head(casUrl(validated))), Effect.succeed)\n )\n if (response.status === 404) return false\n if (!isOk(response)) return yield* Effect.fail(unexpectedStatus(\"an existence probe\", response.status))\n return true\n })\n )\n\n const findMissing: ArtifactStore.Service[\"findMissing\"] = Effect.fn(\"RemoteArtifacts.findMissing\")(\n (digests: Iterable<string>) =>\n Effect.gen(function*() {\n const requested = [...new Set(digests)]\n if (requested.length === 0) return []\n // The batch travels in a JSON body rather than a path, but an\n // unusable address is still an unusable address, and the local tier\n // refuses the same batch wholesale rather than probing part of it.\n const validated: Array<ArtifactStore.Digest> = []\n for (const digest of requested) validated.push(yield* ArtifactStore.validateDigest(digest))\n yield* Effect.annotateCurrentSpan({ count: validated.length })\n const missing = new Set<string>()\n for (let offset = 0; offset < validated.length; offset += maxFindMissingBatchDigests) {\n const batch = validated.slice(offset, offset + maxFindMissingBatchDigests)\n const requestBody = encoder.encode(JSON.stringify({ digests: batch }))\n /* v8 ignore next 3 -- strict 64-byte digests plus the 1,000-entry batch cap make this\n * at most 67,013 bytes; keep the protocol assertion beside serialization in case either invariant changes. */\n if (requestBody.byteLength > maxFindMissingBodyBytes) {\n return yield* Effect.fail(configurationFailure(\"remote artifact findMissing request exceeds 256 KiB\"))\n }\n const decoded = yield* within(\n \"a batched existence probe\",\n requestDeadline,\n send(\n \"a batched existence probe\",\n authorized(HttpClientRequest.post(`${base}/cas/findMissing`)).pipe(\n HttpClientRequest.bodyUint8Array(requestBody, \"application/json\")\n ),\n (response) =>\n Effect.gen(function*() {\n if (!isOk(response)) {\n return yield* Effect.fail(unexpectedStatus(\"a batched existence probe\", response.status))\n }\n const bodyBytes = yield* readBounded(\n response,\n \"a batched existence probe\",\n maxFindMissingResponseBytes\n )\n const body = yield* Effect.try({\n try: () => JSON.parse(decoder.decode(bodyBytes)) as unknown,\n catch: () => transportFailure(\"a batched existence probe body\")\n })\n return yield* Schema.decodeUnknownEffect(FindMissingResponse)(body).pipe(\n Effect.mapError(() => transportFailure(\"a batched existence probe body\"))\n )\n })\n )\n )\n const asked = new Set<string>(batch)\n for (const digest of decoded.missing) if (asked.has(digest)) missing.add(digest)\n }\n // \"The returned set is guaranteed to be a subset of `digests`\"\n // (`MissingDigestsFinder`). Bazel gets that from the server; we\n // enforce it on this side too, because a server that answered with\n // an unrequested digest would otherwise make the caller upload bytes\n // it never asked about.\n return validated.filter((digest) => missing.has(digest))\n })\n )\n\n return { put, get, has, findMissing, downloadPolicy }\n })\n\n/**\n * Provides a remote artifact store as the `ArtifactStore` tag.\n *\n * Composing this *alone* makes every artifact read and write a network round\n * trip. The intended production shape is\n * {@link CombinedArtifacts.layer | CombinedArtifacts}, with this as its remote\n * tier.\n *\n * @category layers\n * @since 1.0.0-rc.0\n * @slop\n */\nexport const layer = (\n options: Options\n): Layer.Layer<ArtifactStore.ArtifactStore, ArtifactStore.ArtifactStoreError, HttpClient.HttpClient> =>\n Layer.effect(ArtifactStore.ArtifactStore)(make(options))\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAuDA,eAA0B;AAC1B,aAAwB;AACxB,sBAAiC;AACjC,cAAyB;AACzB,iBAA4B;AAC5B,wBAAmC;AAEnC,YAAuB;AACvB,aAAwB;AACxB,aAAwB;AACxB,aAAwB;AACxB,oBAA+B;AAQxB,MAAM,iBAAiB,OAAO,SAAS,CAAC,OAAO,YAAY,SAAS,CAAU;AAgB9E,MAAM,mBAAkD,OAAO,OAAO,CAAC,OAAO,YAAY,SAAS,CAAC;AAoBpG,MAAM,mBAAmB,CAAC,UAA6D;AAC5F,QAAM,WAAY,MAAgD;AAClE,SAAO,OAAO,GAAG,cAAc,EAAE,QAAQ,IAAI,WAAW;AAC1D;AA2HA,MAAM,yBAAyB,SAAS,QAAQ,EAAE;AAClD,MAAM,uBAAuB,SAAS,QAAQ,EAAE;AAChD,MAAM,wBAAwB,SAAS,QAAQ,EAAE;AAQjD,MAAM,0BAA0B,MAAM,OAAO;AAC7C,MAAM,6BAA6B;AACnC,MAAM,0BAA0B,MAA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ArtifactBackupLease.d.ts","sourceRoot":"","sources":["../../src/ArtifactBackupLease.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAGH,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,KAAK,UAAU,MAAM,mBAAmB,CAAA;AACpD,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AAqDvC;;;;;;;GAOG;AACH,eAAO,MAAM,SAAS,GAAI,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,EACnC,IAAI,UAAU,CAAC,UAAU,EACzB,WAAW,MAAM,EACjB,QAAQ,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAC9B,SAAS,CAAC,KAAK,EAAE,OAAO,KAAK,EAAE,KAC9B,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,CAoHzB,CAAA;AAEJ;;;;;;;GAOG;AACH,eAAO,MAAM,YAAY,GAAI,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,EACtC,IAAI,UAAU,CAAC,UAAU,EACzB,WAAW,MAAM,EACjB,QAAQ,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAC9B,SAAS,CAAC,KAAK,EAAE,OAAO,KAAK,EAAE,KAC9B,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,CASzC,CAAA"}
@@ -72,24 +72,33 @@ export const withLease = (fs, directory, effect, failure) => Effect.gen(function
72
72
  // Install marker cleanup before acquisition, including gate release. The
73
73
  // marker write and ownership bookkeeping complete in one interruption mask.
74
74
  return yield* Effect.acquireUseRelease(Effect.void, () => acquire.pipe(Effect.andThen(Effect.scoped(Effect.gen(function* () {
75
- yield* Effect.forkScoped(Effect.forever(Effect.sleep(heartbeatEvery).pipe(Effect.andThen(gate(fs, directory, fs.readFileString(marker).pipe(Effect.map(Option.some), Effect.catch((cause) => isReason(cause, "NotFound") ? Effect.succeed(Option.none()) : Effect.fail(cause)), Effect.flatMap((found) => {
76
- // A missing or foreign marker means a sweep judged this
77
- // lease stale and reaped it: deletion is no longer
78
- // fenced while the backup still copies. The sibling
79
- // `internal/ArtifactLocks.ts` heartbeat reports the same
80
- // loss the same way.
81
- if (Option.getOrUndefined(found) !== owner) {
82
- return Effect.logWarning("Artifact backup lease was reclaimed while its backup was still running");
75
+ yield* Effect.forkScoped(Effect.gen(function* () {
76
+ while (true) {
77
+ yield* Effect.sleep(heartbeatEvery);
78
+ const reclaimed = yield* gate(fs, directory, fs.readFileString(marker).pipe(Effect.map(Option.some), Effect.catch((cause) => isReason(cause, "NotFound") ? Effect.succeed(Option.none()) : Effect.fail(cause)), Effect.flatMap((found) => {
79
+ // A missing or foreign marker means a sweep judged this
80
+ // lease stale and reaped it: deletion is no longer
81
+ // fenced while the backup still copies. The sibling
82
+ // `internal/ArtifactLocks.ts` heartbeat reports the same
83
+ // loss the same way. A reaped marker is never handed
84
+ // back, so the loss is reported once and the heartbeat
85
+ // ends instead of repeating it every tick.
86
+ if (Option.getOrUndefined(found) !== owner) {
87
+ return Effect.as(Effect.logWarning("Artifact backup lease was reclaimed while its backup was still running"), true);
88
+ }
89
+ return Effect.flatMap(Clock.currentTimeMillis, (now) => {
90
+ const timestamp = new Date(now);
91
+ return Effect.as(fs.utimes(marker, timestamp, timestamp), false);
92
+ });
93
+ }), Effect.mapError(failure)), failure).pipe(
94
+ // A failed heartbeat is retried on the next tick, but a
95
+ // minute of them lets the marker go stale, so each one is
96
+ // reported rather than dropped.
97
+ Effect.catch((cause) => Effect.as(Effect.logWarning("Artifact backup lease heartbeat failed", cause), false)));
98
+ if (reclaimed)
99
+ return;
83
100
  }
84
- return Effect.flatMap(Clock.currentTimeMillis, (now) => {
85
- const timestamp = new Date(now);
86
- return fs.utimes(marker, timestamp, timestamp);
87
- });
88
- }), Effect.mapError(failure)), failure)),
89
- // A failed heartbeat is retried on the next tick, but a
90
- // minute of them lets the marker go stale, so each one is
91
- // reported rather than dropped.
92
- Effect.catch((cause) => Effect.logWarning("Artifact backup lease heartbeat failed", cause)))));
101
+ }));
93
102
  return yield* effect;
94
103
  })))), () => ownsMarker
95
104
  ? gate(fs, directory, fs.readFileString(marker).pipe(Effect.flatMap((found) => found === owner ? fs.remove(marker) : Effect.void),
@@ -1 +1 @@
1
- {"version":3,"file":"ArtifactBackupLease.js","sourceRoot":"","sources":["../../src/ArtifactBackupLease.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AACrC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AAEvC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,aAAa,MAAM,sBAAsB,CAAA;AACrD,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,aAAa,MAAM,6BAA6B,CAAA;AAE5D,MAAM,UAAU,GAAG,eAAe,CAAA;AAClC,MAAM,QAAQ,GAAG,oBAAoB,CAAA;AACrC,MAAM,cAAc,GAAG,YAAY,CAAA;AACnC,MAAM,YAAY,GAAG,MAAM,CAAA;AAC3B,MAAM,aAAa,GAAG,WAAW,CAAA;AACjC,MAAM,UAAU,GAAG,WAAW,CAAA;AAE9B,MAAM,QAAQ,GAAG,CAAC,KAAc,EAAE,GAAiC,EAAW,EAAE,CAC9E,KAAK,YAAY,aAAa,CAAC,aAAa,IAAI,KAAK,CAAC,MAAM,CAAC,IAAI,KAAK,GAAG,CAAA;AAE3E,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;IAChC,MAAM,KAAK,GAAG,KAAK,CAAC,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC,EAAE,MAAM,CAAC,gBAAgB,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,C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AC,CAAC,YAAY,CAAC,EAAE,EAAE,SAAS,EAAE,OAAO,CAAC;QAAE,OAAO,MAAM,CAAC,IAAI,EAAK,CAAA;IACxE,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,CAAA;AACnC,CAAC,CAAC,EACF,OAAO,CACR,CAAA","sourcesContent":["/**\n * Cross-process exclusion between a filesystem artifact backup and garbage\n * collection.\n *\n * A backup holds one heartbeat-backed marker for the duration of its database\n * snapshot and blob copy. Sweep deletion checks that marker while holding the\n * same short-lived global gate. Publication remains unconstrained: new blobs\n * may appear during a backup, but a blob already referenced by the frozen\n * database cannot disappear underneath it.\n *\n * @since 1.0.0-rc.0\n */\n\nimport * as Clock from \"effect/Clock\"\nimport * as Effect from \"effect/Effect\"\nimport type * as FileSystem from \"effect/FileSystem\"\nimport * as Option from \"effect/Option\"\nimport * as PlatformError from \"effect/PlatformError\"\nimport * as Random from \"effect/Random\"\nimport * as ArtifactLocks from \"./internal/ArtifactLocks.ts\"\n\nconst markerName = \".backup-lease\"\nconst gateName = \".backup-lease-gate\"\nconst heartbeatEvery = \"10 seconds\"\nconst staleAfterMs = 60_000\nconst acquireWithin = \"2 minutes\"\nconst retryEvery = \"25 millis\"\n\nconst isReason = (cause: unknown, tag: PlatformError.SystemErrorTag): boolean =>\n cause instanceof PlatformError.PlatformError && cause.reason._tag === tag\n\nconst token = Effect.gen(function*() {\n const first = yield* Random.nextIntBetween(0, Number.MAX_SAFE_INTEGER, { halfOpen: true })\n const second = yield* Random.nextIntBetween(0, Number.MAX_SAFE_INTEGER, { halfOpen: true })\n return `${first.toString(36)}-${second.toString(36)}-${yield* Clock.currentTimeMillis}`\n})\n\nconst gate = <A, E, R, E2>(\n fs: FileSystem.FileSystem,\n directory: string,\n effect: Effect.Effect<A, E, R>,\n failure: (cause: unknown) => E2\n): Effect.Effect<A, E | E2, R> => ArtifactLocks.withDigest(fs, directory, gateName, effect, failure)\n\nconst activeMarker = <E>(\n fs: FileSystem.FileSystem,\n directory: string,\n failure: (cause: unknown) => E\n): Effect.Effect<boolean, E> =>\n Effect.gen(function*() {\n const path = `${directory}/${markerName}`\n const info = yield* fs.stat(path).pipe(\n Effect.map(Option.some),\n Effect.catch((cause): Effect.Effect<Option.Option<FileSystem.File.Info>, E> =>\n isReason(cause, \"NotFound\") ? Effect.succeed(Option.none()) : Effect.fail(failure(cause))\n )\n )\n if (Option.isNone(info)) return false\n const modified = Option.getOrUndefined(info.value.mtime)\n const now = yield* Clock.currentTimeMillis\n if (modified === undefined || now - modified.getTime() <= staleAfterMs) return true\n yield* fs.remove(path).pipe(\n Effect.catch((cause): Effect.Effect<void, E> =>\n isReason(cause, \"NotFound\") ? Effect.void : Effect.fail(failure(cause))\n )\n )\n return false\n })\n\n/**\n * Runs `effect` while sweep deletion is fenced across every process using\n * this objects directory. A crashed holder is recovered after its heartbeat\n * becomes stale.\n *\n * @category combinators\n * @since 1.0.0-rc.0\n */\nexport const withLease = <A, E, R, E2>(\n fs: FileSystem.FileSystem,\n directory: string,\n effect: Effect.Effect<A, E, R>,\n failure: (cause: unknown) => E2\n): Effect.Effect<A, E | E2, R> =>\n Effect.gen(function*() {\n const owner = yield* token\n const marker = `${directory}/${markerName}`\n yield* fs.makeDirectory(directory, { recursive: true }).pipe(Effect.mapError(failure))\n\n let ownsMarker = false\n const acquire = Effect.gen(function*() {\n while (true) {\n const acquired = yield* gate(\n fs,\n directory,\n Effect.gen(function*() {\n if (yield* activeMarker(fs, directory, failure)) return false\n return yield* Effect.uninterruptible(\n fs.writeFileString(marker, owner, { flag: \"wx\", mode: 0o600 }).pipe(\n Effect.andThen(Effect.sync(() => {\n ownsMarker = true\n return true\n })),\n Effect.catch((cause): Effect.Effect<boolean, E2> =>\n isReason(cause, \"AlreadyExists\") ? Effect.succeed(false) : Effect.fail(failure(cause))\n )\n )\n )\n }),\n failure\n )\n if (acquired) return\n yield* Effect.sleep(retryEvery)\n }\n }).pipe(\n Effect.timeout(acquireWithin),\n Effect.catchTag(\"TimeoutError\", (cause) => Effect.fail(failure(cause)))\n )\n\n // Install marker cleanup before acquisition, including gate release. The\n // marker write and ownership bookkeeping complete in one interruption mask.\n return yield* Effect.acquireUseRelease(\n Effect.void,\n () =>\n acquire.pipe(Effect.andThen(\n Effect.scoped(Effect.gen(function*() {\n yield* Effect.forkScoped(\n Effect.forever(\n Effect.sleep(heartbeatEvery).pipe(\n Effect.andThen(gate(\n fs,\n directory,\n fs.readFileString(marker).pipe(\n Effect.map(Option.some),\n Effect.catch((cause) =>\n isReason(cause, \"NotFound\") ? Effect.succeed(Option.none<string>()) : Effect.fail(cause)\n ),\n Effect.flatMap((found) => {\n // A missing or foreign marker means a sweep judged this\n // lease stale and reaped it: deletion is no longer\n // fenced while the backup still copies. The sibling\n // `internal/ArtifactLocks.ts` heartbeat reports the same\n // loss the same way.\n if (Option.getOrUndefined(found) !== owner) {\n return Effect.logWarning(\n \"Artifact backup lease was reclaimed while its backup was still running\"\n )\n }\n return Effect.flatMap(Clock.currentTimeMillis, (now) => {\n const timestamp = new Date(now)\n return fs.utimes(marker, timestamp, timestamp)\n })\n }),\n Effect.mapError(failure)\n ),\n failure\n )),\n // A failed heartbeat is retried on the next tick, but a\n // minute of them lets the marker go stale, so each one is\n // reported rather than dropped.\n Effect.catch((cause) => Effect.logWarning(\"Artifact backup lease heartbeat failed\", cause))\n )\n )\n )\n return yield* effect\n }))\n )),\n () =>\n ownsMarker\n ? gate(\n fs,\n directory,\n fs.readFileString(marker).pipe(\n Effect.flatMap((found) => found === owner ? fs.remove(marker) : Effect.void),\n // A backup whose heartbeat lapsed has its marker reaped by whoever\n // noticed, so `NotFound` is the ordinary end of a slow lease, not a\n // fault: there is nothing left to release and nothing left to leak.\n // The sibling release in `internal/ArtifactLocks.ts` classifies the\n // same cause the same way.\n Effect.catch((cause): Effect.Effect<void, PlatformError.PlatformError> =>\n isReason(cause, \"NotFound\") ? Effect.void : Effect.fail(cause)\n )\n ),\n failure\n ).pipe(\n Effect.catch((cause) => Effect.logWarning(\"Artifact backup lease release failed\", cause))\n )\n : Effect.void\n )\n })\n\n/**\n * Runs one sweep deletion only when no live backup lease exists. The marker\n * check and `effect` share the global gate, closing the check/delete race.\n * `None` means a backup deliberately fenced the operation.\n *\n * @category combinators\n * @since 1.0.0-rc.0\n */\nexport const unlessActive = <A, E, R, E2>(\n fs: FileSystem.FileSystem,\n directory: string,\n effect: Effect.Effect<A, E, R>,\n failure: (cause: unknown) => E2\n): Effect.Effect<Option.Option<A>, E | E2, R> =>\n gate(\n fs,\n directory,\n Effect.gen(function*() {\n if (yield* activeMarker(fs, directory, failure)) return Option.none<A>()\n return Option.some(yield* effect)\n }),\n failure\n )\n"]}
1
+ {"version":3,"file":"ArtifactBackupLease.js","sourceRoot":"","sources":["../../src/ArtifactBackupLease.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AACrC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AAEvC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,aAAa,MAAM,sBAAsB,CAAA;AACrD,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,aAAa,MAAM,6BAA6B,CAAA;AAE5D,MAAM,UAAU,GAAG,eAAe,CAAA;AAClC,MAAM,QAAQ,GAAG,oBAAoB,CAAA;AACrC,MAAM,cAAc,GAAG,YAAY,CAAA;AACnC,MAAM,YAAY,GAAG,MAAM,CAAA;AAC3B,MAAM,aAAa,GAAG,WAAW,CAAA;AACjC,MAAM,UAAU,GAAG,WAAW,CAAA;AAE9B,MAAM,QAAQ,GAAG,CAAC,KAAc,EAAE,GAAiC,EAAW,EAAE,CAC9E,KAAK,YAAY,aAAa,CAAC,aAAa,IAAI,KAAK,CAAC,MAAM,CAAC,IAAI,KAAK,GAAG,CAAA;AAE3E,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CA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I,CAAC,KAAK,CAAC,CAC/D,CACF,EACD,OAAO,CACR,CAAC,IAAI,CACJ,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC,UAAU,CAAC,sCAAsC,EAAE,KAAK,CAAC,CAAC,CAC1F;QACD,CAAC,CAAC,MAAM,CAAC,IAAI,CAClB,CAAA;AACH,CAAC,CAAC,CAAA;AAEJ;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAC1B,EAAyB,EACzB,SAAiB,EACjB,MAA8B,EAC9B,OAA+B,EACa,EAAE,CAC9C,IAAI,CACF,EAAE,EACF,SAAS,EACT,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;IAClB,IAAI,KAAK,CAAC,CAAC,YAAY,CAAC,EAAE,EAAE,SAAS,EAAE,OAAO,CAAC;QAAE,OAAO,MAAM,CAAC,IAAI,EAAK,CAAA;IACxE,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,CAAA;AACnC,CAAC,CAAC,EACF,OAAO,CACR,CAAA","sourcesContent":["/**\n * Cross-process exclusion between a filesystem artifact backup and garbage\n * collection.\n *\n * A backup holds one heartbeat-backed marker for the duration of its database\n * snapshot and blob copy. Sweep deletion checks that marker while holding the\n * same short-lived global gate. Publication remains unconstrained: new blobs\n * may appear during a backup, but a blob already referenced by the frozen\n * database cannot disappear underneath it.\n *\n * @since 1.0.0-rc.0\n */\n\nimport * as Clock from \"effect/Clock\"\nimport * as Effect from \"effect/Effect\"\nimport type * as FileSystem from \"effect/FileSystem\"\nimport * as Option from \"effect/Option\"\nimport * as PlatformError from \"effect/PlatformError\"\nimport * as Random from \"effect/Random\"\nimport * as ArtifactLocks from \"./internal/ArtifactLocks.ts\"\n\nconst markerName = \".backup-lease\"\nconst gateName = \".backup-lease-gate\"\nconst heartbeatEvery = \"10 seconds\"\nconst staleAfterMs = 60_000\nconst acquireWithin = \"2 minutes\"\nconst retryEvery = \"25 millis\"\n\nconst isReason = (cause: unknown, tag: PlatformError.SystemErrorTag): boolean =>\n cause instanceof PlatformError.PlatformError && cause.reason._tag === tag\n\nconst token = Effect.gen(function*() {\n const first = yield* Random.nextIntBetween(0, Number.MAX_SAFE_INTEGER, { halfOpen: true })\n const second = yield* Random.nextIntBetween(0, Number.MAX_SAFE_INTEGER, { halfOpen: true })\n return `${first.toString(36)}-${second.toString(36)}-${yield* Clock.currentTimeMillis}`\n})\n\nconst gate = <A, E, R, E2>(\n fs: FileSystem.FileSystem,\n directory: string,\n effect: Effect.Effect<A, E, R>,\n failure: (cause: unknown) => E2\n): Effect.Effect<A, E | E2, R> => ArtifactLocks.withDigest(fs, directory, gateName, effect, failure)\n\nconst activeMarker = <E>(\n fs: FileSystem.FileSystem,\n directory: string,\n failure: (cause: unknown) => E\n): Effect.Effect<boolean, E> =>\n Effect.gen(function*() {\n const path = `${directory}/${markerName}`\n const info = yield* fs.stat(path).pipe(\n Effect.map(Option.some),\n Effect.catch((cause): Effect.Effect<Option.Option<FileSystem.File.Info>, E> =>\n isReason(cause, \"NotFound\") ? Effect.succeed(Option.none()) : Effect.fail(failure(cause))\n )\n )\n if (Option.isNone(info)) return false\n const modified = Option.getOrUndefined(info.value.mtime)\n const now = yield* Clock.currentTimeMillis\n if (modified === undefined || now - modified.getTime() <= staleAfterMs) return true\n yield* fs.remove(path).pipe(\n Effect.catch((cause): Effect.Effect<void, E> =>\n isReason(cause, \"NotFound\") ? Effect.void : Effect.fail(failure(cause))\n )\n )\n return false\n })\n\n/**\n * Runs `effect` while sweep deletion is fenced across every process using\n * this objects directory. A crashed holder is recovered after its heartbeat\n * becomes stale.\n *\n * @category combinators\n * @since 1.0.0-rc.0\n */\nexport const withLease = <A, E, R, E2>(\n fs: FileSystem.FileSystem,\n directory: string,\n effect: Effect.Effect<A, E, R>,\n failure: (cause: unknown) => E2\n): Effect.Effect<A, E | E2, R> =>\n Effect.gen(function*() {\n const owner = yield* token\n const marker = `${directory}/${markerName}`\n yield* fs.makeDirectory(directory, { recursive: true }).pipe(Effect.mapError(failure))\n\n let ownsMarker = false\n const acquire = Effect.gen(function*() {\n while (true) {\n const acquired = yield* gate(\n fs,\n directory,\n Effect.gen(function*() {\n if (yield* activeMarker(fs, directory, failure)) return false\n return yield* Effect.uninterruptible(\n fs.writeFileString(marker, owner, { flag: \"wx\", mode: 0o600 }).pipe(\n Effect.andThen(Effect.sync(() => {\n ownsMarker = true\n return true\n })),\n Effect.catch((cause): Effect.Effect<boolean, E2> =>\n isReason(cause, \"AlreadyExists\") ? Effect.succeed(false) : Effect.fail(failure(cause))\n )\n )\n )\n }),\n failure\n )\n if (acquired) return\n yield* Effect.sleep(retryEvery)\n }\n }).pipe(\n Effect.timeout(acquireWithin),\n Effect.catchTag(\"TimeoutError\", (cause) => Effect.fail(failure(cause)))\n )\n\n // Install marker cleanup before acquisition, including gate release. The\n // marker write and ownership bookkeeping complete in one interruption mask.\n return yield* Effect.acquireUseRelease(\n Effect.void,\n () =>\n acquire.pipe(Effect.andThen(\n Effect.scoped(Effect.gen(function*() {\n yield* Effect.forkScoped(\n Effect.gen(function*() {\n while (true) {\n yield* Effect.sleep(heartbeatEvery)\n const reclaimed = yield* gate(\n fs,\n directory,\n fs.readFileString(marker).pipe(\n Effect.map(Option.some),\n Effect.catch((cause) =>\n isReason(cause, \"NotFound\") ? Effect.succeed(Option.none<string>()) : Effect.fail(cause)\n ),\n Effect.flatMap((found) => {\n // A missing or foreign marker means a sweep judged this\n // lease stale and reaped it: deletion is no longer\n // fenced while the backup still copies. The sibling\n // `internal/ArtifactLocks.ts` heartbeat reports the same\n // loss the same way. A reaped marker is never handed\n // back, so the loss is reported once and the heartbeat\n // ends instead of repeating it every tick.\n if (Option.getOrUndefined(found) !== owner) {\n return Effect.as(\n Effect.logWarning(\n \"Artifact backup lease was reclaimed while its backup was still running\"\n ),\n true\n )\n }\n return Effect.flatMap(Clock.currentTimeMillis, (now) => {\n const timestamp = new Date(now)\n return Effect.as(fs.utimes(marker, timestamp, timestamp), false)\n })\n }),\n Effect.mapError(failure)\n ),\n failure\n ).pipe(\n // A failed heartbeat is retried on the next tick, but a\n // minute of them lets the marker go stale, so each one is\n // reported rather than dropped.\n Effect.catch((cause) =>\n Effect.as(Effect.logWarning(\"Artifact backup lease heartbeat failed\", cause), false)\n )\n )\n if (reclaimed) return\n }\n })\n )\n return yield* effect\n }))\n )),\n () =>\n ownsMarker\n ? gate(\n fs,\n directory,\n fs.readFileString(marker).pipe(\n Effect.flatMap((found) => found === owner ? fs.remove(marker) : Effect.void),\n // A backup whose heartbeat lapsed has its marker reaped by whoever\n // noticed, so `NotFound` is the ordinary end of a slow lease, not a\n // fault: there is nothing left to release and nothing left to leak.\n // The sibling release in `internal/ArtifactLocks.ts` classifies the\n // same cause the same way.\n Effect.catch((cause): Effect.Effect<void, PlatformError.PlatformError> =>\n isReason(cause, \"NotFound\") ? Effect.void : Effect.fail(cause)\n )\n ),\n failure\n ).pipe(\n Effect.catch((cause) => Effect.logWarning(\"Artifact backup lease release failed\", cause))\n )\n : Effect.void\n )\n })\n\n/**\n * Runs one sweep deletion only when no live backup lease exists. The marker\n * check and `effect` share the global gate, closing the check/delete race.\n * `None` means a backup deliberately fenced the operation.\n *\n * @category combinators\n * @since 1.0.0-rc.0\n */\nexport const unlessActive = <A, E, R, E2>(\n fs: FileSystem.FileSystem,\n directory: string,\n effect: Effect.Effect<A, E, R>,\n failure: (cause: unknown) => E2\n): Effect.Effect<Option.Option<A>, E | E2, R> =>\n gate(\n fs,\n directory,\n Effect.gen(function*() {\n if (yield* activeMarker(fs, directory, failure)) return Option.none<A>()\n return Option.some(yield* effect)\n }),\n failure\n )\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"ArtifactSweep.d.ts","sourceRoot":"","sources":["../../src/ArtifactSweep.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAA;AACzC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;;;;;;;;;;;OAYG;IACH,QAAQ,CAAC,MAAM,EAAE,CACf,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,aAAa,KACpB,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,aAAa,CAAC,kBAAkB,CAAC,CAAA;CAC9D;;AAED;;;;;;;;;GASG;AACH,qBAAa,aAAc,SAAQ,kBAA4E;CAAG;AAelH;;;;;;;;;GASG;AACH,MAAM,WAAW,YAAY;IAC3B;;;;OAIG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IACvC;;;;;;OAMG;IACH,QAAQ,CAAC,YAAY,CAAC,EAAE,UAAU,GAAG,SAAS,GAAG,SAAS,CAAA;CAC3D;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,eAAO,MAAM,cAAc,GACzB,IAAI,UAAU,CAAC,UAAU,EACzB,UAAS,YAAiB,KACzB,OA2HF,CAAA;AAED;;;;;;;;;GASG;AACH,eAAO,MAAM,eAAe,GAC1B,UAAS,YAAiB,KACzB,KAAK,CAAC,KAAK,CAAC,aAAa,EAAE,KAAK,EAAE,UAAU,CAAC,UAAU,CAC2C,CAAA;AAErG;;;;;;;GAOG;AACH,eAAO,MAAM,QAAQ,GAAI,YAAW,OAAO,CAAC,OAAO,CAAM,KAAG,OAI1D,CAAA;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,SAAS,GAAI,YAAW,OAAO,CAAC,OAAO,CAAM,KAAG,KAAK,CAAC,KAAK,CAAC,aAAa,CACnC,CAAA"}
@@ -132,6 +132,15 @@ export const makeFileSystem = (fs, options = {}) => {
132
132
  });
133
133
  const remove = Effect.fn("ArtifactSweep.remove")((digest, removeOptions) => Effect.gen(function* () {
134
134
  const validated = yield* ArtifactStore.validateDigest(digest);
135
+ const bound = removeOptions?.ifUnmodifiedSinceMs;
136
+ // NaN fails every comparison, so the fence below would read it as "not
137
+ // modified since" and delete a blob a concurrent put just freshened.
138
+ if (bound !== undefined && (typeof bound !== "number" || Number.isNaN(bound))) {
139
+ return yield* Effect.fail(new ArtifactStore.ArtifactStoreError({
140
+ code: "invalid_configuration",
141
+ message: "invalid sweep option: ifUnmodifiedSinceMs"
142
+ }));
143
+ }
135
144
  yield* Effect.annotateCurrentSpan({ digest: validated });
136
145
  const checkRoot = yield* ArtifactPath.guard(fs, directory).pipe(Effect.mapError(hostFailure));
137
146
  const checkParent = yield* ArtifactPath.guard(fs, `${directory}/${validated.slice(0, 2)}`).pipe(Effect.mapError(hostFailure));
@@ -144,7 +153,6 @@ export const makeFileSystem = (fs, options = {}) => {
144
153
  yield* checkRoot.pipe(Effect.mapError(hostFailure));
145
154
  yield* checkParent.pipe(Effect.mapError(hostFailure));
146
155
  const checkBlob = yield* ArtifactPath.guard(fs, path, "File").pipe(Effect.mapError(hostFailure));
147
- const bound = removeOptions?.ifUnmodifiedSinceMs;
148
156
  if (bound !== undefined) {
149
157
  const info = yield* fs.stat(path).pipe(Effect.option);
150
158
  // Already gone is a completed deletion, and an unmeasurable mtime is
@@ -1 +1 @@
1
- {"version":3,"file":"ArtifactSweep.js","sourceRoot":"","sources":["../../src/ArtifactSweep.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAA;AACzC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,UAAU,MAAM,mBAAmB,CAAA;AAC/C,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AACrC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,aAAa,MAAM,sBAAsB,CAAA;AACrD,OAAO,KAAK,mBAAmB,MAAM,0BAA0B,CAAA;AAC/D,OAAO,KAAK,aAAa,MAAM,oBAAoB,CAAA;AACnD,OAAO,KAAK,aAAa,MAAM,6BAA6B,CAAA;AAC5D,OAAO,KAAK,YAAY,MAAM,4BAA4B,CAAA;AAuE1D;;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I,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAC/F,CACF,CACF,CAAA;YACH,CAAC,CAAC,CAAA;QACJ,CAAC;IACH,CAAC,CAAC,CACH,CAAA;IAED,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,CAAA;AAC9B,CAAC,CAAA;AAED;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAC7B,UAAwB,EAAE,EACgC,EAAE,CAC5D,KAAK,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,UAAU,CAAC,UAAU,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,cAAc,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC,CAAC,CAAA;AAErG;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,YAA8B,EAAE,EAAW,EAAE,CAAC,CAAC;IACtE,SAAS,EAAE,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,0BAA0B,CAAC,CAAC;IACzD,MAAM,EAAE,MAAM,CAAC,EAAE,CAAC,sBAAsB,CAAC,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,uBAAuB,CAAC,CAAC,CAAC;IAC5F,GAAG,SAAS;CACb,CAAC,CAAA;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,YAA8B,EAAE,EAA8B,EAAE,CACxF,KAAK,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAA","sourcesContent":["/**\n * The sweep half of artifact garbage collection: enumerate this host's blobs\n * and delete one, fenced by its age.\n *\n * This surface is deliberately NOT part of `ArtifactStore.Service`. The store\n * contract is shared by memory, filesystem, and network tiers, and a remote\n * tier can neither enumerate its address space nor accept a delete — Bazel\n * draws the same line: its disk-cache collector\n * (class `DiskCacheGarbageCollector` in\n * `com.google.devtools.build.lib.remote.disk`) walks the local directory, while\n * the remote tier owns its own retention. Only the\n * host-local filesystem store implements this.\n *\n * The *policy* — which digests are live, how old a dead blob must be before it\n * goes — belongs to the engine composition, which is the only place the\n * durable roots (attempt rows, cache entries) are visible. This module ships\n * mechanics alone; `@smthrs/engine-store` owns the mark policy and grace\n * period.\n *\n * @since 1.0.0-rc.0\n */\n\nimport * as Context from \"effect/Context\"\nimport * as Effect from \"effect/Effect\"\nimport * as FileSystem from \"effect/FileSystem\"\nimport * as Layer from \"effect/Layer\"\nimport * as Option from \"effect/Option\"\nimport * as PlatformError from \"effect/PlatformError\"\nimport * as ArtifactBackupLease from \"./ArtifactBackupLease.ts\"\nimport * as ArtifactStore from \"./ArtifactStore.ts\"\nimport * as ArtifactLocks from \"./internal/ArtifactLocks.ts\"\nimport * as ArtifactPath from \"./internal/ArtifactPath.ts\"\n\n/**\n * One enumerated blob: its content address, when it was last written or\n * freshened, and its size.\n *\n * `modifiedAtMs` is the age evidence a sweep fences on — the same signal git\n * (`git prune --expire`) and Bazel's disk-cache collector read, because it is\n * the one timestamp a filesystem maintains without any bookkeeping of ours.\n *\n * @category models\n * @since 1.0.0-rc.0\n * @slop\n */\nexport interface BlobStat {\n readonly digest: string\n readonly modifiedAtMs: number\n readonly sizeBytes: number\n}\n\n/**\n * Fencing predicate for a sweep deletion.\n *\n * @category models\n * @since 1.0.0-rc.0\n * @slop\n */\nexport interface RemoveOptions {\n /**\n * Deletes the blob only while its mtime is still at or before this bound.\n * A blob freshened after the caller computed its live set — a concurrent\n * `put` re-referencing the same bytes — fails the fence and survives, the\n * same shape as `CacheStore.evict`'s `ifRecordedBy`: the guard rides in the\n * delete, never in a prior read alone.\n */\n readonly ifUnmodifiedSinceMs?: number | undefined\n}\n\n/**\n * Host-local blob enumeration and fenced deletion.\n *\n * @category models\n * @since 1.0.0-rc.0\n * @slop\n */\nexport interface Service {\n /**\n * Every content-addressed blob this host's store currently holds. In-flight\n * `.tmp-*` scratch files, foreign files, and blobs whose age cannot be\n * measured are excluded — what the sweep cannot judge it must not touch.\n */\n readonly inventory: Effect.Effect<Array<BlobStat>, ArtifactStore.ArtifactStoreError>\n /**\n * Deletes the blob at `digest`, returning whether bytes were removed. A\n * missing blob reports `false` rather than failing, so a crashed and\n * re-run sweep converges instead of erroring on its own progress.\n *\n * `false` covers three outcomes a caller cannot tell apart, all of which mean\n * \"nothing was reclaimed, and retrying later is safe\": the blob was already\n * gone, it failed the `ifUnmodifiedSinceMs` fence, or a live backup lease\n * fenced the deletion (see `ArtifactBackupLease`). The last one is not\n * progress — the blob is still there — so a collector that counts reclaimed\n * bytes must not count it, and the next pass after the backup finishes\n * removes it.\n */\n readonly remove: (\n digest: string,\n options?: RemoveOptions\n ) => Effect.Effect<boolean, ArtifactStore.ArtifactStoreError>\n}\n\n/**\n * Service tag for host-local artifact sweeping.\n *\n * The identity string equals this module's package path, per the house rule\n * that a new identity is the defining module path.\n *\n * @category services\n * @since 1.0.0-rc.0\n * @slop\n */\nexport class ArtifactSweep extends Context.Service<ArtifactSweep, Service>()(\"@smthrs/artifacts/ArtifactSweep\") {}\n\nconst error = (message: string, cause?: unknown): ArtifactStore.ArtifactStoreError =>\n new ArtifactStore.ArtifactStoreError({\n code: \"unavailable\",\n message,\n ...(cause === undefined ? {} : { cause })\n })\n\nconst hostFailure = (cause: unknown): ArtifactStore.ArtifactStoreError =>\n error(`the host filesystem refused a sweep operation: ${String(cause)}`, cause)\n\nconst isNotFound = (cause: unknown): boolean =>\n cause instanceof PlatformError.PlatformError && cause.reason._tag === \"NotFound\"\n\n/**\n * How the sweep reaches the objects directory.\n *\n * Deliberately not `ArtifactStore.FileSystemOptions`: the sweep never writes a\n * blob, so `durability` would be an accepted argument it ignores. Both fields\n * must match the store built over the same directory — see `coordination`.\n *\n * @category models\n * @since 1.0.0-rc.0\n */\nexport interface SweepOptions {\n /**\n * The objects directory to enumerate, the one the paired\n * `ArtifactStore.makeFileSystem` publishes into. Defaults to\n * `ArtifactStore.defaultDirectory`.\n */\n readonly directory?: string | undefined\n /**\n * `required` takes the same per-digest lock file a publishing writer takes,\n * and consults the backup lease, before deleting. It fences nothing unless\n * the paired store was built with `required` too: a store on `process` never\n * takes the lock this sweep waits on, so the fence would read as armed while\n * protecting nothing. `process` is the explicit weaker browser/test mode.\n */\n readonly coordination?: \"required\" | \"process\" | undefined\n}\n\n/**\n * Builds the filesystem-backed sweep surface over the same objects directory\n * an `ArtifactStore.makeFileSystem` publishes into.\n *\n * Enumeration is conservative by construction. Only paths in the store's\n * canonical fanout shape — `xx/<digest>` with `xx` equal to the digest's\n * two-hex prefix — are blobs; anything else in the directory (a temp file, a\n * lock file, a foreign file, a fanout directory itself) is skipped, never\n * deleted. A blob that vanishes between listing and stat was removed by someone\n * else and is likewise skipped.\n *\n * Pair it with the store over the SAME `directory` and the SAME `coordination`.\n * Nothing checks the pairing, and a mismatch is silent: a sweep on `required`\n * beside a store on `process` deletes blobs no writer is fenced against.\n *\n * Under `required`, one deletion costs two lock acquisitions — the per-digest\n * lock, then the workspace-global backup-lease gate — which is roughly ten\n * filesystem operations and two forked heartbeat fibers per blob. The gate is\n * one file for the whole workspace, so concurrent deletions serialize through\n * it. That is the price of never deleting a blob a running backup already\n * recorded; size a collection window with it in mind.\n *\n * @category constructors\n * @since 1.0.0-rc.0\n * @slop\n */\nexport const makeFileSystem = (\n fs: FileSystem.FileSystem,\n options: SweepOptions = {}\n): Service => {\n const directory = (options.directory ?? ArtifactStore.defaultDirectory).replace(/([^/])\\/+$/, \"$1\")\n const coordination = options.coordination ?? \"required\"\n const blobPath = (digest: string): string => `${directory}/${digest.slice(0, 2)}/${digest}`\n\n const inventory: Service[\"inventory\"] = Effect.gen(function*() {\n // A store that never published anything has no directory; that is an\n // empty inventory, not a failure.\n const checkRoot = yield* ArtifactPath.guard(fs, directory).pipe(Effect.mapError(hostFailure))\n const parents = yield* fs.readDirectory(directory).pipe(\n Effect.catch((cause) => isNotFound(cause) ? Effect.succeed([] as Array<string>) : Effect.fail(hostFailure(cause)))\n )\n const entries: Array<string> = []\n for (const parent of parents) {\n if (!/^[0-9a-f]{2}$/.test(parent)) continue\n yield* checkRoot.pipe(Effect.mapError(hostFailure))\n const safe = yield* ArtifactPath.guard(fs, `${directory}/${parent}`).pipe(Effect.option)\n if (Option.isNone(safe)) continue\n const children = yield* fs.readDirectory(`${directory}/${parent}`).pipe(\n Effect.orElseSucceed(() => [] as Array<string>)\n )\n for (const child of children) entries.push(`${parent}/${child}`)\n }\n const candidates = entries.filter((entry) => {\n if (entry.includes(\".tmp-\")) return false\n const separator = entry.indexOf(\"/\")\n const fan = entry.slice(0, separator)\n const digest = entry.slice(separator + 1)\n return /^[0-9a-f]{64}$/.test(digest) && fan === digest.slice(0, 2)\n })\n const blobs = yield* Effect.forEach(candidates, (entry) =>\n Effect.gen(function*() {\n const separator = entry.indexOf(\"/\")\n const fan = entry.slice(0, separator)\n const digest = entry.slice(separator + 1)\n // The stat is per-candidate and tolerant: a blob another process\n // removed between the listing and here simply is not in the\n // inventory, and one whose mtime the host cannot report offers no\n // age evidence, so the sweep must never judge it.\n const info = yield* Effect.gen(function*() {\n yield* checkRoot\n yield* ArtifactPath.guard(fs, `${directory}/${fan}`)\n yield* ArtifactPath.guard(fs, `${directory}/${entry}`, \"File\")\n return yield* fs.stat(`${directory}/${entry}`)\n }).pipe(Effect.option)\n if (Option.isNone(info) || info.value.type !== \"File\") return undefined\n const mtime = Option.getOrUndefined(info.value.mtime)\n if (mtime === undefined) return undefined\n return {\n digest,\n modifiedAtMs: mtime.getTime(),\n sizeBytes: Number(info.value.size)\n }\n }), { concurrency: 16 })\n return blobs.filter((blob) => blob !== undefined)\n })\n\n const remove: Service[\"remove\"] = Effect.fn(\"ArtifactSweep.remove\")((digest, removeOptions) =>\n Effect.gen(function*() {\n const validated = yield* ArtifactStore.validateDigest(digest)\n yield* Effect.annotateCurrentSpan({ digest: validated })\n const checkRoot = yield* ArtifactPath.guard(fs, directory).pipe(Effect.mapError(hostFailure))\n const checkParent = yield* ArtifactPath.guard(fs, `${directory}/${validated.slice(0, 2)}`).pipe(\n Effect.mapError(hostFailure)\n )\n return yield* ArtifactLocks.withDigest(\n fs,\n directory,\n validated,\n coordination === \"process\"\n ? removeBlob()\n : ArtifactBackupLease.unlessActive(fs, directory, removeBlob(), hostFailure).pipe(\n Effect.map(Option.getOrElse(() => false))\n ),\n hostFailure,\n coordination\n )\n\n function removeBlob(): Effect.Effect<boolean, ArtifactStore.ArtifactStoreError> {\n return Effect.gen(function*() {\n const path = blobPath(validated)\n yield* checkRoot.pipe(Effect.mapError(hostFailure))\n yield* checkParent.pipe(Effect.mapError(hostFailure))\n const checkBlob = yield* ArtifactPath.guard(fs, path, \"File\").pipe(Effect.mapError(hostFailure))\n const bound = removeOptions?.ifUnmodifiedSinceMs\n if (bound !== undefined) {\n const info = yield* fs.stat(path).pipe(Effect.option)\n // Already gone is a completed deletion, and an unmeasurable mtime is\n // no proof of age: both refuse rather than delete.\n if (Option.isNone(info)) return false\n const mtime = Option.getOrUndefined(info.value.mtime)\n if (mtime === undefined || mtime.getTime() > bound) return false\n }\n yield* checkRoot.pipe(Effect.mapError(hostFailure))\n yield* checkParent.pipe(Effect.mapError(hostFailure))\n yield* checkBlob.pipe(Effect.mapError(hostFailure))\n return yield* fs.remove(path).pipe(\n Effect.as(true),\n // The blob may have been removed between the fence and here; only a\n // refusal over bytes that still exist is a host failure.\n Effect.catch((cause) =>\n fs.exists(path).pipe(\n Effect.mapError((probeCause) => hostFailure({ remove: cause, existenceProbe: probeCause })),\n Effect.flatMap((present) => present ? Effect.fail(hostFailure(cause)) : Effect.succeed(false))\n )\n )\n )\n })\n }\n })\n )\n\n return { inventory, remove }\n}\n\n/**\n * Provides the filesystem-backed sweep surface.\n *\n * Give it the same `directory` and `coordination` as the `ArtifactStore` layer\n * it sweeps behind; see {@link makeFileSystem}.\n *\n * @category layers\n * @since 1.0.0-rc.0\n * @slop\n */\nexport const layerFileSystem = (\n options: SweepOptions = {}\n): Layer.Layer<ArtifactSweep, never, FileSystem.FileSystem> =>\n Layer.effect(ArtifactSweep)(Effect.map(FileSystem.FileSystem, (fs) => makeFileSystem(fs, options)))\n\n/**\n * Builds a sweep surface whose every operation fails as unavailable, with\n * per-method overrides.\n *\n * @category constructors\n * @since 1.0.0-rc.0\n * @slop\n */\nexport const makeNoop = (overrides: Partial<Service> = {}): Service => ({\n inventory: Effect.fail(error(\"inventory is unavailable\")),\n remove: Effect.fn(\"ArtifactSweep.remove\")(() => Effect.fail(error(\"remove is unavailable\"))),\n ...overrides\n})\n\n/**\n * Provides a no-op sweep surface.\n *\n * @category layers\n * @since 1.0.0-rc.0\n * @slop\n */\nexport const layerNoop = (overrides: Partial<Service> = {}): Layer.Layer<ArtifactSweep> =>\n Layer.succeed(ArtifactSweep)(makeNoop(overrides))\n"]}
1
+ {"version":3,"file":"ArtifactSweep.js","sourceRoot":"","sources":["../../src/ArtifactSweep.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAA;AACzC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,UAAU,MAAM,mBAAmB,CAAA;AAC/C,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AACrC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,aAAa,MAAM,sBAAsB,CAAA;AACrD,OAAO,KAAK,mBAAmB,MAAM,0BAA0B,CAAA;AAC/D,OAAO,KAAK,aAAa,MAAM,oBAAoB,CAAA;AACnD,OAAO,KAAK,aAAa,MAAM,6BAA6B,CAAA;AAC5D,OAAO,KAAK,YAAY,MAAM,4BAA4B,CAAA;AAuE1D;;;;;;;;;GASG;AACH,MAAM,OAAO,aAAc,SAAQ,OAAO,CAAC,OAAO,EAA0B,CAAC,iCAAiC,CAAC;CAAG;AAElH,MAA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QAAQ,CAAC,SAAS,CAAC,CAAC,CAAA","sourcesContent":["/**\n * The sweep half of artifact garbage collection: enumerate this host's blobs\n * and delete one, fenced by its age.\n *\n * This surface is deliberately NOT part of `ArtifactStore.Service`. The store\n * contract is shared by memory, filesystem, and network tiers, and a remote\n * tier can neither enumerate its address space nor accept a delete — Bazel\n * draws the same line: its disk-cache collector\n * (class `DiskCacheGarbageCollector` in\n * `com.google.devtools.build.lib.remote.disk`) walks the local directory, while\n * the remote tier owns its own retention. Only the\n * host-local filesystem store implements this.\n *\n * The *policy* — which digests are live, how old a dead blob must be before it\n * goes — belongs to the engine composition, which is the only place the\n * durable roots (attempt rows, cache entries) are visible. This module ships\n * mechanics alone; `@smthrs/engine-store` owns the mark policy and grace\n * period.\n *\n * @since 1.0.0-rc.0\n */\n\nimport * as Context from \"effect/Context\"\nimport * as Effect from \"effect/Effect\"\nimport * as FileSystem from \"effect/FileSystem\"\nimport * as Layer from \"effect/Layer\"\nimport * as Option from \"effect/Option\"\nimport * as PlatformError from \"effect/PlatformError\"\nimport * as ArtifactBackupLease from \"./ArtifactBackupLease.ts\"\nimport * as ArtifactStore from \"./ArtifactStore.ts\"\nimport * as ArtifactLocks from \"./internal/ArtifactLocks.ts\"\nimport * as ArtifactPath from \"./internal/ArtifactPath.ts\"\n\n/**\n * One enumerated blob: its content address, when it was last written or\n * freshened, and its size.\n *\n * `modifiedAtMs` is the age evidence a sweep fences on — the same signal git\n * (`git prune --expire`) and Bazel's disk-cache collector read, because it is\n * the one timestamp a filesystem maintains without any bookkeeping of ours.\n *\n * @category models\n * @since 1.0.0-rc.0\n * @slop\n */\nexport interface BlobStat {\n readonly digest: string\n readonly modifiedAtMs: number\n readonly sizeBytes: number\n}\n\n/**\n * Fencing predicate for a sweep deletion.\n *\n * @category models\n * @since 1.0.0-rc.0\n * @slop\n */\nexport interface RemoveOptions {\n /**\n * Deletes the blob only while its mtime is still at or before this bound.\n * A blob freshened after the caller computed its live set — a concurrent\n * `put` re-referencing the same bytes — fails the fence and survives, the\n * same shape as `CacheStore.evict`'s `ifRecordedBy`: the guard rides in the\n * delete, never in a prior read alone.\n */\n readonly ifUnmodifiedSinceMs?: number | undefined\n}\n\n/**\n * Host-local blob enumeration and fenced deletion.\n *\n * @category models\n * @since 1.0.0-rc.0\n * @slop\n */\nexport interface Service {\n /**\n * Every content-addressed blob this host's store currently holds. In-flight\n * `.tmp-*` scratch files, foreign files, and blobs whose age cannot be\n * measured are excluded — what the sweep cannot judge it must not touch.\n */\n readonly inventory: Effect.Effect<Array<BlobStat>, ArtifactStore.ArtifactStoreError>\n /**\n * Deletes the blob at `digest`, returning whether bytes were removed. A\n * missing blob reports `false` rather than failing, so a crashed and\n * re-run sweep converges instead of erroring on its own progress.\n *\n * `false` covers three outcomes a caller cannot tell apart, all of which mean\n * \"nothing was reclaimed, and retrying later is safe\": the blob was already\n * gone, it failed the `ifUnmodifiedSinceMs` fence, or a live backup lease\n * fenced the deletion (see `ArtifactBackupLease`). The last one is not\n * progress — the blob is still there — so a collector that counts reclaimed\n * bytes must not count it, and the next pass after the backup finishes\n * removes it.\n */\n readonly remove: (\n digest: string,\n options?: RemoveOptions\n ) => Effect.Effect<boolean, ArtifactStore.ArtifactStoreError>\n}\n\n/**\n * Service tag for host-local artifact sweeping.\n *\n * The identity string equals this module's package path, per the house rule\n * that a new identity is the defining module path.\n *\n * @category services\n * @since 1.0.0-rc.0\n * @slop\n */\nexport class ArtifactSweep extends Context.Service<ArtifactSweep, Service>()(\"@smthrs/artifacts/ArtifactSweep\") {}\n\nconst error = (message: string, cause?: unknown): ArtifactStore.ArtifactStoreError =>\n new ArtifactStore.ArtifactStoreError({\n code: \"unavailable\",\n message,\n ...(cause === undefined ? {} : { cause })\n })\n\nconst hostFailure = (cause: unknown): ArtifactStore.ArtifactStoreError =>\n error(`the host filesystem refused a sweep operation: ${String(cause)}`, cause)\n\nconst isNotFound = (cause: unknown): boolean =>\n cause instanceof PlatformError.PlatformError && cause.reason._tag === \"NotFound\"\n\n/**\n * How the sweep reaches the objects directory.\n *\n * Deliberately not `ArtifactStore.FileSystemOptions`: the sweep never writes a\n * blob, so `durability` would be an accepted argument it ignores. Both fields\n * must match the store built over the same directory — see `coordination`.\n *\n * @category models\n * @since 1.0.0-rc.0\n */\nexport interface SweepOptions {\n /**\n * The objects directory to enumerate, the one the paired\n * `ArtifactStore.makeFileSystem` publishes into. Defaults to\n * `ArtifactStore.defaultDirectory`.\n */\n readonly directory?: string | undefined\n /**\n * `required` takes the same per-digest lock file a publishing writer takes,\n * and consults the backup lease, before deleting. It fences nothing unless\n * the paired store was built with `required` too: a store on `process` never\n * takes the lock this sweep waits on, so the fence would read as armed while\n * protecting nothing. `process` is the explicit weaker browser/test mode.\n */\n readonly coordination?: \"required\" | \"process\" | undefined\n}\n\n/**\n * Builds the filesystem-backed sweep surface over the same objects directory\n * an `ArtifactStore.makeFileSystem` publishes into.\n *\n * Enumeration is conservative by construction. Only paths in the store's\n * canonical fanout shape — `xx/<digest>` with `xx` equal to the digest's\n * two-hex prefix — are blobs; anything else in the directory (a temp file, a\n * lock file, a foreign file, a fanout directory itself) is skipped, never\n * deleted. A blob that vanishes between listing and stat was removed by someone\n * else and is likewise skipped.\n *\n * Pair it with the store over the SAME `directory` and the SAME `coordination`.\n * Nothing checks the pairing, and a mismatch is silent: a sweep on `required`\n * beside a store on `process` deletes blobs no writer is fenced against.\n *\n * Under `required`, one deletion costs two lock acquisitions — the per-digest\n * lock, then the workspace-global backup-lease gate — which is roughly ten\n * filesystem operations and two forked heartbeat fibers per blob. The gate is\n * one file for the whole workspace, so concurrent deletions serialize through\n * it. That is the price of never deleting a blob a running backup already\n * recorded; size a collection window with it in mind.\n *\n * @category constructors\n * @since 1.0.0-rc.0\n * @slop\n */\nexport const makeFileSystem = (\n fs: FileSystem.FileSystem,\n options: SweepOptions = {}\n): Service => {\n const directory = (options.directory ?? ArtifactStore.defaultDirectory).replace(/([^/])\\/+$/, \"$1\")\n const coordination = options.coordination ?? \"required\"\n const blobPath = (digest: string): string => `${directory}/${digest.slice(0, 2)}/${digest}`\n\n const inventory: Service[\"inventory\"] = Effect.gen(function*() {\n // A store that never published anything has no directory; that is an\n // empty inventory, not a failure.\n const checkRoot = yield* ArtifactPath.guard(fs, directory).pipe(Effect.mapError(hostFailure))\n const parents = yield* fs.readDirectory(directory).pipe(\n Effect.catch((cause) => isNotFound(cause) ? Effect.succeed([] as Array<string>) : Effect.fail(hostFailure(cause)))\n )\n const entries: Array<string> = []\n for (const parent of parents) {\n if (!/^[0-9a-f]{2}$/.test(parent)) continue\n yield* checkRoot.pipe(Effect.mapError(hostFailure))\n const safe = yield* ArtifactPath.guard(fs, `${directory}/${parent}`).pipe(Effect.option)\n if (Option.isNone(safe)) continue\n const children = yield* fs.readDirectory(`${directory}/${parent}`).pipe(\n Effect.orElseSucceed(() => [] as Array<string>)\n )\n for (const child of children) entries.push(`${parent}/${child}`)\n }\n const candidates = entries.filter((entry) => {\n if (entry.includes(\".tmp-\")) return false\n const separator = entry.indexOf(\"/\")\n const fan = entry.slice(0, separator)\n const digest = entry.slice(separator + 1)\n return /^[0-9a-f]{64}$/.test(digest) && fan === digest.slice(0, 2)\n })\n const blobs = yield* Effect.forEach(candidates, (entry) =>\n Effect.gen(function*() {\n const separator = entry.indexOf(\"/\")\n const fan = entry.slice(0, separator)\n const digest = entry.slice(separator + 1)\n // The stat is per-candidate and tolerant: a blob another process\n // removed between the listing and here simply is not in the\n // inventory, and one whose mtime the host cannot report offers no\n // age evidence, so the sweep must never judge it.\n const info = yield* Effect.gen(function*() {\n yield* checkRoot\n yield* ArtifactPath.guard(fs, `${directory}/${fan}`)\n yield* ArtifactPath.guard(fs, `${directory}/${entry}`, \"File\")\n return yield* fs.stat(`${directory}/${entry}`)\n }).pipe(Effect.option)\n if (Option.isNone(info) || info.value.type !== \"File\") return undefined\n const mtime = Option.getOrUndefined(info.value.mtime)\n if (mtime === undefined) return undefined\n return {\n digest,\n modifiedAtMs: mtime.getTime(),\n sizeBytes: Number(info.value.size)\n }\n }), { concurrency: 16 })\n return blobs.filter((blob) => blob !== undefined)\n })\n\n const remove: Service[\"remove\"] = Effect.fn(\"ArtifactSweep.remove\")((digest, removeOptions) =>\n Effect.gen(function*() {\n const validated = yield* ArtifactStore.validateDigest(digest)\n const bound = removeOptions?.ifUnmodifiedSinceMs\n // NaN fails every comparison, so the fence below would read it as \"not\n // modified since\" and delete a blob a concurrent put just freshened.\n if (bound !== undefined && (typeof bound !== \"number\" || Number.isNaN(bound))) {\n return yield* Effect.fail(\n new ArtifactStore.ArtifactStoreError({\n code: \"invalid_configuration\",\n message: \"invalid sweep option: ifUnmodifiedSinceMs\"\n })\n )\n }\n yield* Effect.annotateCurrentSpan({ digest: validated })\n const checkRoot = yield* ArtifactPath.guard(fs, directory).pipe(Effect.mapError(hostFailure))\n const checkParent = yield* ArtifactPath.guard(fs, `${directory}/${validated.slice(0, 2)}`).pipe(\n Effect.mapError(hostFailure)\n )\n return yield* ArtifactLocks.withDigest(\n fs,\n directory,\n validated,\n coordination === \"process\"\n ? removeBlob()\n : ArtifactBackupLease.unlessActive(fs, directory, removeBlob(), hostFailure).pipe(\n Effect.map(Option.getOrElse(() => false))\n ),\n hostFailure,\n coordination\n )\n\n function removeBlob(): Effect.Effect<boolean, ArtifactStore.ArtifactStoreError> {\n return Effect.gen(function*() {\n const path = blobPath(validated)\n yield* checkRoot.pipe(Effect.mapError(hostFailure))\n yield* checkParent.pipe(Effect.mapError(hostFailure))\n const checkBlob = yield* ArtifactPath.guard(fs, path, \"File\").pipe(Effect.mapError(hostFailure))\n if (bound !== undefined) {\n const info = yield* fs.stat(path).pipe(Effect.option)\n // Already gone is a completed deletion, and an unmeasurable mtime is\n // no proof of age: both refuse rather than delete.\n if (Option.isNone(info)) return false\n const mtime = Option.getOrUndefined(info.value.mtime)\n if (mtime === undefined || mtime.getTime() > bound) return false\n }\n yield* checkRoot.pipe(Effect.mapError(hostFailure))\n yield* checkParent.pipe(Effect.mapError(hostFailure))\n yield* checkBlob.pipe(Effect.mapError(hostFailure))\n return yield* fs.remove(path).pipe(\n Effect.as(true),\n // The blob may have been removed between the fence and here; only a\n // refusal over bytes that still exist is a host failure.\n Effect.catch((cause) =>\n fs.exists(path).pipe(\n Effect.mapError((probeCause) => hostFailure({ remove: cause, existenceProbe: probeCause })),\n Effect.flatMap((present) => present ? Effect.fail(hostFailure(cause)) : Effect.succeed(false))\n )\n )\n )\n })\n }\n })\n )\n\n return { inventory, remove }\n}\n\n/**\n * Provides the filesystem-backed sweep surface.\n *\n * Give it the same `directory` and `coordination` as the `ArtifactStore` layer\n * it sweeps behind; see {@link makeFileSystem}.\n *\n * @category layers\n * @since 1.0.0-rc.0\n * @slop\n */\nexport const layerFileSystem = (\n options: SweepOptions = {}\n): Layer.Layer<ArtifactSweep, never, FileSystem.FileSystem> =>\n Layer.effect(ArtifactSweep)(Effect.map(FileSystem.FileSystem, (fs) => makeFileSystem(fs, options)))\n\n/**\n * Builds a sweep surface whose every operation fails as unavailable, with\n * per-method overrides.\n *\n * @category constructors\n * @since 1.0.0-rc.0\n * @slop\n */\nexport const makeNoop = (overrides: Partial<Service> = {}): Service => ({\n inventory: Effect.fail(error(\"inventory is unavailable\")),\n remove: Effect.fn(\"ArtifactSweep.remove\")(() => Effect.fail(error(\"remove is unavailable\"))),\n ...overrides\n})\n\n/**\n * Provides a no-op sweep surface.\n *\n * @category layers\n * @since 1.0.0-rc.0\n * @slop\n */\nexport const layerNoop = (overrides: Partial<Service> = {}): Layer.Layer<ArtifactSweep> =>\n Layer.succeed(ArtifactSweep)(makeNoop(overrides))\n"]}
@@ -34,7 +34,7 @@
34
34
  *
35
35
  * @since 1.0.0-rc.1
36
36
  */
37
- import type * as Duration from "effect/Duration";
37
+ import * as Duration from "effect/Duration";
38
38
  import * as Effect from "effect/Effect";
39
39
  import type * as FileSystem from "effect/FileSystem";
40
40
  /**
@@ -50,7 +50,11 @@ export interface Options {
50
50
  readonly annotations?: Readonly<Record<string, unknown>> | undefined;
51
51
  /** How often the holder refreshes the lock's mtime. Defaults to 10 seconds. */
52
52
  readonly heartbeatEvery?: Duration.Input | undefined;
53
- /** Age past which a lock or claim is stale. Defaults to 60 000 ms. */
53
+ /**
54
+ * Age past which a lock or claim is stale. Defaults to 60 000 ms. Must be a
55
+ * finite positive number longer than `heartbeatEvery`; anything else fails
56
+ * the lease through `failure` before the lock is touched.
57
+ */
54
58
  readonly staleAfterMs?: number | undefined;
55
59
  /** How long acquisition may wait. Defaults to 2 minutes. */
56
60
  readonly acquireWithin?: Duration.Input | undefined;
@@ -1 +1 @@
1
- {"version":3,"file":"FileLease.d.ts","sourceRoot":"","sources":["../../src/FileLease.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAIH,OAAO,KAAK,KAAK,QAAQ,MAAM,iBAAiB,CAAA;AAChD,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,KAAK,UAAU,MAAM,mBAAmB,CAAA;AAKpD;;;;;GAKG;AACH,MAAM,WAAW,OAAO;IACtB,kFAAkF;IAClF,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IACnC,6DAA6D;IAC7D,QAAQ,CAAC,WAAW,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAAG,SAAS,CAAA;IACpE,+EAA+E;IAC/E,QAAQ,CAAC,cAAc,CAAC,EAAE,QAAQ,CAAC,KAAK,GAAG,SAAS,CAAA;IACpD,sEAAsE;IACtE,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC1C,4DAA4D;IAC5D,QAAQ,CAAC,aAAa,CAAC,EAAE,QAAQ,CAAC,KAAK,GAAG,SAAS,CAAA;IACnD,wEAAwE;IACxE,QAAQ,CAAC,UAAU,CAAC,EAAE,QAAQ,CAAC,KAAK,GAAG,SAAS,CAAA;CAC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