@devries-cameron20 / t3code-1 / commits / a43b0032cf
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

feat(infra): share Vite task cache with Axiom savings telemetry (#24693)

Julius Marminge committed Oct 25, 2026 at 00:06 UTC a43b0032cf3cee8e16e407c7056f180fb1652a8a
67 files changed +5560 -1
.github/actions/setup-ci-deps/action.yml
+22
@@ -1,5 +1,10 @@
1 name: Setup CI dependencies
2 description: Install Vite+ and the workspace dependencies from cache.
3 +inputs:
4 + task-cache-read-token:
5 + description: Read-only credential for the private maintainer task cache
6 + required: false
7 + default: ""
8 runs:
9 using: composite
10 steps:
@@ -65,3 +70,20 @@ runs:
70 - name: Install dependencies
71 shell: bash
72 run: vp install
73 +
74 + - name: Enable shared task cache
75 + if: >-
76 + github.repository == 'pingdotgg/t3code' &&
77 + (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository) &&
78 + hashFiles('.task-cache.json') != '' &&
79 + inputs.task-cache-read-token != ''
80 + shell: bash
81 + env:
82 + T3_TASK_CACHE_READ_TOKEN: ${{ inputs.task-cache-read-token }}
83 + run: |
84 + node infra/task-cache/scripts/install-ci.ts
85 + if [[ ( "$GITHUB_EVENT_NAME" == pull_request && "$GITHUB_BASE_REF" == main ) || ( "$GITHUB_EVENT_NAME" == push && "$GITHUB_REF" == refs/heads/main ) ]]; then
86 + echo 'VP_REMOTE_CACHE=read-write' >> "$GITHUB_ENV"
87 + else
88 + echo 'VP_REMOTE_CACHE=read' >> "$GITHUB_ENV"
89 + fi
.github/workflows/ci.yml
+30
@@ -59,6 +59,9 @@ jobs:
59 # file, docs included. The `Check` job at the end of this file passes only
60 # when every other job passes or was skipped by `changes`.
61 lint:
62 + permissions:
63 + contents: read
64 + id-token: write
65 name: Lint
66 runs-on: blacksmith-8vcpu-ubuntu-2404
67 timeout-minutes: 10
@@ -82,6 +85,8 @@ jobs:
85
86 - name: Setup Vite+
87 uses: ./.github/actions/setup-ci-deps
88 + with:
89 + task-cache-read-token: ${{ secrets.T3_TASK_CACHE_READ_TOKEN }}
90
91 # electron's install.js (run by ensure:electron) reads this cache before it
92 # downloads from GitHub. The key changes only with the Electron version.
@@ -108,6 +113,9 @@ jobs:
113 run: vp check
114
115 build:
116 + permissions:
117 + contents: read
118 + id-token: write
119 name: Build and Typecheck
120 needs: changes
121 if: ${{ !cancelled() && (needs.changes.outputs.build != 'false' || needs.changes.outputs.typecheck != 'false') }}
@@ -139,6 +147,8 @@ jobs:
147
148 - name: Setup Vite+
149 uses: ./.github/actions/setup-ci-deps
150 + with:
151 + task-cache-read-token: ${{ secrets.T3_TASK_CACHE_READ_TOKEN }}
152
153 # electron's install.js (run by ensure:electron) reads this cache before it
154 # downloads from GitHub. The key changes only with the Electron version.
@@ -197,6 +207,9 @@ jobs:
207 # dependency layers. The concurrency limit stays at the default 4 so peak load
208 # per runner is unchanged.
209 test:
210 + permissions:
211 + contents: read
212 + id-token: write
213 name: Test
214 needs: changes
215 if: ${{ !cancelled() && needs.changes.outputs.test != 'false' }}
@@ -237,6 +250,8 @@ jobs:
250
251 - name: Setup Vite+
252 uses: ./.github/actions/setup-ci-deps
253 + with:
254 + task-cache-read-token: ${{ secrets.T3_TASK_CACHE_READ_TOKEN }}
255
256 # electron's install.js (run by ensure:electron) reads this cache before it
257 # downloads from GitHub. The key changes only with the Electron version.
@@ -278,6 +293,9 @@ jobs:
293 # started last and ran alone for about 40s after everything else finished.
294 # release.yml mirrors this test, test_web, and test_server split.
295 test_web:
296 + permissions:
297 + contents: read
298 + id-token: write
299 name: Test Web
300 needs: changes
301 if: ${{ !cancelled() && needs.changes.outputs.test_web != 'false' }}
@@ -295,6 +313,8 @@ jobs:
313
314 - name: Setup Vite+
315 uses: ./.github/actions/setup-ci-deps
316 + with:
317 + task-cache-read-token: ${{ secrets.T3_TASK_CACHE_READ_TOKEN }}
318
319 # The browser project runs React tests in headless Chromium. Same cache key as
320 # test_server, so both jobs share one downloaded browser.
@@ -333,6 +353,9 @@ jobs:
353 # The shards are the longest jobs, so they gate themselves instead of waiting
354 # ~15s for `changes`: a shard the change cannot affect stops after checkout.
355 test_server:
356 + permissions:
357 + contents: read
358 + id-token: write
359 name: Test Server ${{ matrix.shard }}
360 runs-on: blacksmith-4vcpu-ubuntu-2404
361 timeout-minutes: 10
@@ -359,6 +382,8 @@ jobs:
382 - name: Setup Vite+
383 if: steps.changes.outputs.test_server != 'false'
384 uses: ./.github/actions/setup-ci-deps
385 + with:
386 + task-cache-read-token: ${{ secrets.T3_TASK_CACHE_READ_TOKEN }}
387
388 # No Electron setup here: `t3` (apps/server) has no Electron dependency
389 # and none of its tests touch the runtime. Only the non-server `test`
@@ -495,6 +520,9 @@ jobs:
520 run: vp run lint:mobile
521
522 release_smoke:
523 + permissions:
524 + contents: read
525 + id-token: write
526 name: Release Smoke
527 needs: changes
528 if: ${{ !cancelled() && needs.changes.outputs.release_smoke != 'false' }}
@@ -512,6 +540,8 @@ jobs:
540
541 - name: Setup Vite+
542 uses: ./.github/actions/setup-ci-deps
543 + with:
544 + task-cache-read-token: ${{ secrets.T3_TASK_CACHE_READ_TOKEN }}
545
546 - name: Exercise release-only workflow steps
547 run: node scripts/release-smoke.ts
.github/workflows/release.yml
+10
@@ -131,6 +131,8 @@ jobs:
131
132 - name: Setup dependencies
133 uses: ./.github/actions/setup-ci-deps
134 + with:
135 + task-cache-read-token: ${{ secrets.T3_TASK_CACHE_READ_TOKEN }}
136 env:
137 pnpm_config_cache_dir: ${{ runner.temp }}/pnpm-metadata
138
@@ -245,6 +247,8 @@ jobs:
247
248 - name: Setup dependencies
249 uses: ./.github/actions/setup-ci-deps
250 + with:
251 + task-cache-read-token: ${{ secrets.T3_TASK_CACHE_READ_TOKEN }}
252
253 - name: Ensure Electron runtime is installed
254 run: vp run --filter @t3tools/desktop ensure:electron
@@ -275,6 +279,8 @@ jobs:
279
280 - name: Setup dependencies
281 uses: ./.github/actions/setup-ci-deps
282 + with:
283 + task-cache-read-token: ${{ secrets.T3_TASK_CACHE_READ_TOKEN }}
284
285 - name: Ensure Electron runtime is installed
286 run: vp run --filter @t3tools/desktop ensure:electron
@@ -309,6 +315,8 @@ jobs:
315
316 - name: Setup dependencies
317 uses: ./.github/actions/setup-ci-deps
318 + with:
319 + task-cache-read-token: ${{ secrets.T3_TASK_CACHE_READ_TOKEN }}
320
321 - name: Resolve Playwright version
322 id: playwright_version
@@ -359,6 +367,8 @@ jobs:
367
368 - name: Setup dependencies
369 uses: ./.github/actions/setup-ci-deps
370 + with:
371 + task-cache-read-token: ${{ secrets.T3_TASK_CACHE_READ_TOKEN }}
372
373 # No Electron setup here: `t3` (apps/server) has no Electron dependency
374 # and none of its tests touch the runtime. Only the non-server `test`
.task-cache.json new
+3
@@ -0,0 +1,3 @@
1 +{
2 + "endpoint": "https://t3-task-cache.pinglabs.workers.dev/projects/t3code"
3 +}
apps/server/vite.config.ts
+3 -1
@@ -62,9 +62,11 @@ const packExecutableTargets = packExecutableTarget
62 // The root config's `projects` route repo-root runs to package configs,
63 // including this one. Inheriting them would make this config a container.
64 const { projects: _rootTestProjects, ...baseTestConfig } = baseConfig.test ?? {};
65 +// Workspace cache configuration is invalid on a package config.
66 +const { run: _rootRunConfig, ...basePackageConfig } = baseConfig;
67
68 export default mergeConfig(
67 - { ...baseConfig, test: baseTestConfig },
69 + { ...basePackageConfig, test: baseTestConfig },
70 defineConfig({
71 run: {
72 tasks: {
infra/task-cache/README.md new
+77
@@ -0,0 +1,77 @@
1 +# Shared Vite Task cache
2 +
3 +[Alchemy](https://alchemy.run) manages the `T3TaskCache` stack: the `t3-task-cache` Worker, its D1 index, private R2 bucket, rate limits, and cleanup schedule. The production stack adopts the existing resources and preserves their data and repository authorization policy. D1 and R2 have a retain removal policy.
4 +
5 +The Worker uses Effect services for request handling, developer authentication, bounded concurrency, deadlines, and cleanup. The pinned [upstream protocol package](https://github.com/voidzero-dev/vite-plus-remote-cache-cloudflare/tree/5b06c2c10d84ea441735ba07c46f6d1b7cb2ff3b) supplies the experimental wire format, streaming upload, GitHub OIDC validation, and atomic D1 publication. Keeping that adapter preserves compatibility with existing cache entries.
6 +
7 +Vite+ 1.3 includes an experimental remote client even though its TypeScript configuration and CLI help omit it. The native client reads `run.cache.remote.url`, `VP_REMOTE_CACHE_URL`, and `VP_REMOTE_CACHE`. An opt-in Node preload patches the Vite+ JavaScript entrypoint in memory and invokes an optional repository hook before and after the native task runner. The global executable and PATH are unchanged. Vite config loads saved credentials into its in-memory remote URL, and the hook selects the native cache mode and exports task summaries. Saved credentials are never exported into task environments. Normal dependency installs do not patch Vite+. The hook is installed only by explicit maintainer setup or our own CI; without credentials it does not collect or queue telemetry.
8 +
9 +## Deploy
10 +
11 +Install dependencies with `vp install`, then authenticate with Alchemy's Cloudflare profile. Alternatively, provide `CLOUDFLARE_ACCOUNT_ID` and `CLOUDFLARE_API_TOKEN` through your environment. Alchemy stores deployment state remotely with `Cloudflare.state()`.
12 +
13 +```sh
14 +cd infra/task-cache
15 +alchemy deploy --stage prod
16 +```
17 +
18 +The stack reads read-only credential digests from `~/.config/t3-task-cache/readers.json` (or `READER_KEYS`) and writer digests from `~/.config/t3-task-cache/writers.json` and supplies them as a redacted Worker secret. Preserve that file to administer existing writers. Deployment uses the `prod` stage and the existing endpoint in `.task-cache.json`. The upstream protocol is loaded only inside the Worker runtime and bundled by Alchemy, so deployment runs on Node without an extra TypeScript loader. An explicit `DEVELOPER_KEYS` environment variable overrides the local registry, including on deployment hosts without that file. Deployment fails when neither source exists, so a missing local file cannot silently revoke existing writers.
19 +
20 +The production database’s `scopes` and `deployment` records control repository identity, push branch, quotas, and retention. `pull_requests` explicitly enables same-repository PR uploads targeting the configured `branch`; `writes_enabled` disables all artifact uploads, including those from maintainers. Telemetry accepts reports from other PR bases without granting artifact writes. Provisioning a separate cache requires its own repository policy; a new empty database does not grant uploads.
21 +
22 +The CI read identity is named `ci-reader`. Its raw token belongs in the GitHub repository secret `T3_TASK_CACHE_READ_TOKEN`; `readers.json` maps that name to the token’s SHA-256 hex digest. To rotate it, replace that digest, deploy, then replace the GitHub secret. Keep the optional private `ci-reader.json` file as `{ "name": "ci-reader", "token": "…" }` for the smoke test to verify read-only restoration; without it, smoke uses the operator’s saved credential. Never commit these files. The read credential cannot upload artifacts or telemetry. Release jobs lack OIDC and their telemetry is not recorded.
23 +
24 +## Add a contributor or machine
25 +
26 +```sh
27 +node infra/task-cache/scripts/manage.ts add-writer maria
28 +```
29 +
30 +This updates the local writer registry and writes `~/.config/t3-task-cache/maria.json`. Run `alchemy deploy --stage prod` from `infra/task-cache` to apply the registry, then transfer that file through a private channel. The recipient runs:
31 +
32 +```sh
33 +bash infra/task-cache/install.sh
34 +```
35 +
36 +The installer prompts for the endpoint, developer name, and token (hidden), verifies authentication with an empty telemetry report, and saves credentials with mode `0600`. It installs the hook and credentials once in the home directory and enables the preload through `NODE_OPTIONS` in shell startup files. Open a fresh shell after setup. New worktrees and dependency reinstalls automatically use the same hook; no checkout or executable is modified. Existing long-running agents need to restart or inherit the updated environment. Rerunning setup updates the shared hook and restores matching backups from the old on-disk patch.
37 +
38 +Uninstall with `bash infra/task-cache/install.sh --uninstall`, then restart existing shells and agents. This removes shell integration, saved client credentials, and queued telemetry. A no-op preload remains for already-running shells that still inherit its path; no cache code runs through it. Administrative writer digests and issued credential files are preserved.
39 +
40 +Explicit environment settings take precedence: `VP_REMOTE_CACHE_URL` may contain the URL-encoded developer name and token as HTTP Basic credentials, and `VP_REMOTE_CACHE=read-write` enables uploads. These can be supplied by a secret manager instead of saved credentials. Do not put credentials in shell command history or commit them. Use separate writer names for separate people. Repeating `add-writer` rotates the token; `revoke-writer <name>` removes it. Deploy again to apply either change.
41 +
42 +Our repository’s CI explicitly installs the Vite+ hook. Same-repository PRs targeting `main` and default-branch push jobs read and write. PRs targeting other branches read only; fork PRs receive no cache credentials and run normally without remote caching. All artifact reads require authentication, including downloads by blob ID. Trusted maintainers automatically upload artifacts that other authenticated maintainers and CI consume. Deployment credentials are never needed on contributor machines or CI runners.
43 +
44 +## Verify
45 +
46 +```sh
47 +node infra/task-cache/scripts/smoke.ts
48 +vp run --filter @t3tools/task-cache test
49 +vp run --filter @t3tools/task-cache typecheck
50 +```
51 +
52 +The smoke check uploads a unique fixture, restores its output into a cold second directory, changes its input, and confirms recomputation. It also checks that anonymous reads and uploads are rejected. Cacheable commands must run through `vp run`; direct `vp test` or `vp check` invocations do not use this cache.
53 +
54 +## Current limits
55 +
56 +- Vite Task separates operating systems and CPU architectures. macOS results do not satisfy Linux CI; Linux developer machines can populate compatible CI results.
57 +- The current protocol retains one result for each execution key. Competing source versions can overwrite each other's entry. Inputs are validated before reuse, so this causes misses rather than stale hits. Merging to main does not promote or reset the cache.
58 +- A local hit makes no remote request and is not uploaded retroactively. Clear only an isolated worktree's task cache when testing uploads of an already-cached task.
59 +- Production retains results for seven days within an 8 GB budget. Remote errors fall back to execution. `VP_REMOTE_CACHE=off vp run ...` disables the remote tier for one invocation.
60 +
61 +## Savings and tracing
62 +
63 +Alchemy also owns the `t3-task-cache-traces` Axiom dataset, its ingest-only Worker token, a savings dashboard, and a separate retained D1 database for lifetime counters. Deployment needs an Axiom Alchemy profile or `AXIOM_TOKEN`/`AXIOM_ORG_ID` as well as Cloudflare credentials. Contributor machines never receive the Axiom token.
64 +
65 +Both `vp run` and its `vpr` shortcut invoke the hook. The hook observes Vite+'s atomic `last-summary.json` files, including nested runs. It reports package/task names, confirmed local and remote hits, avoided task runtime, developer identity, host, OS/architecture, and CI run ID. It does not send commands, arguments, source paths, environment variables, or task output. Developer identity comes from the authenticated cache writer; CI uses GitHub's verified OIDC actor. Same-repository PRs may upload artifacts as well as telemetry. Fork jobs do not install the hook or report telemetry.
66 +
67 +Reports spool locally and retry on subsequent commands. The completion hook queues the final summary, including failed runs. Local runs start a detached, short-lived Node process; CI awaits its bounded flush before the runner can terminate it. Collection or export failures never change the task exit status. No background service is installed. Queues retain at most 2,000 reports, each sent with a three-second timeout and a 15-second budget per flush. Stable report IDs and transactional D1 receipts prevent duplicate lifetime increments. Receipts expire after 31 days; accepted reports must be less than 30 days old. Lifetime totals survive raw trace expiration and are re-exported hourly, including inactive developers and hosts. Collection starts when the hook is installed; existing cache history is not backfilled.
68 +
69 +**Saved time means historical task execution time avoided.** It is useful as a compute proxy, but it is not measured CPU time, money saved, or elapsed developer waiting time. Parallel tasks can overlap, and cache lookup/restore overhead is not subtracted. Failed restores contribute no savings. Older Vite+ versions omit the hit source; their confirmed savings are counted under an explicit unspecified tier. Native summary collection is best-effort: interrupted runs or summaries overwritten before observation can be missing. The experimental Vite+ summary schema is tested here and must be checked when upgrading Vite+.
70 +
71 +Host labels use `~/.agents/MACHINE` when present, otherwise the hostname. GitHub-hosted runners use their runner OS/architecture class to avoid a new lifetime series for every ephemeral VM. Set `T3_TASK_CACHE_HOST` to override the label, or `T3_TASK_CACHE_TELEMETRY=off` to opt out while retaining remote caching. Re-run the installer to update an existing machine's hook.
72 +
73 +Writer administration requires the complete private registry on the administering machine. Copy that registry securely from its administrator before adding a writer on another machine. `add-writer --new-registry` explicitly initializes a new cache; never use it to recover an existing registry.
74 +
75 +Node processes running with filesystem permissions must allow the installed preload and hook directory. Remove the task-cache import from `NODE_OPTIONS` for a restricted process that cannot allow those files.
76 +
77 +Local telemetry is queued to disk before command exit and uploaded by a short-lived detached Node process. Slow or offline endpoints do not delay `vp run`; unsent reports retry on a later run.
infra/task-cache/alchemy.run.ts new
+28
@@ -0,0 +1,28 @@
1 +import { adopt } from "alchemy/AdoptPolicy";
2 +import * as Alchemy from "alchemy";
3 +import * as Axiom from "alchemy/Axiom";
4 +import * as Layer from "effect/Layer";
5 +import { Observability } from "./src/observability.ts";
6 +import * as Cloudflare from "alchemy/Cloudflare";
7 +import * as Effect from "effect/Effect";
8 +import * as Secrets from "alchemy/Secrets";
9 +import { WriterSecrets } from "./src/deploymentConfig.ts";
10 +import * as Worker from "./src/worker.ts";
11 +
12 +export default Alchemy.Stack(
13 + "T3TaskCache",
14 + {
15 + providers: Axiom.providers().pipe(Layer.provideMerge(Cloudflare.providers())),
16 + state: Cloudflare.state(),
17 + secrets: [Secrets.DotEnv(), Secrets.ProcessEnv(), WriterSecrets],
18 + },
19 + Effect.gen(function* () {
20 + const worker = yield* Worker.Worker;
21 + const observability = yield* Observability;
22 + return {
23 + url: worker.url,
24 + workerName: worker.workerName,
25 + dashboard: observability.dashboard.uid,
26 + };
27 + }).pipe(Effect.provide(Worker.layer), adopt(true)),
28 +);
infra/task-cache/install.sh new
+3
@@ -0,0 +1,3 @@
1 +#!/usr/bin/env bash
2 +set -euo pipefail
3 +exec node "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/scripts/install.ts" "$@"
infra/task-cache/migrations/savings/0001_savings.sql new
+16
@@ -0,0 +1,16 @@
1 +CREATE TABLE receipts (id TEXT PRIMARY KEY, recorded_at INTEGER NOT NULL) WITHOUT ROWID;
2 +CREATE TABLE savings (
3 + developer TEXT NOT NULL,
4 + host TEXT NOT NULL,
5 + platform TEXT NOT NULL,
6 + arch TEXT NOT NULL,
7 + ci INTEGER NOT NULL,
8 + local_ms INTEGER NOT NULL,
9 + remote_ms INTEGER NOT NULL,
10 + local_hits INTEGER NOT NULL,
11 + remote_hits INTEGER NOT NULL,
12 + executed INTEGER NOT NULL,
13 + restore_failures INTEGER NOT NULL,
14 + PRIMARY KEY (developer, host, platform, arch, ci)
15 +) WITHOUT ROWID;
16 +CREATE INDEX receipts_age ON receipts(recorded_at);
infra/task-cache/migrations/savings/0002_unspecified_tier.sql new
+2
@@ -0,0 +1,2 @@
1 +ALTER TABLE savings ADD COLUMN unspecified_ms INTEGER NOT NULL DEFAULT 0;
2 +ALTER TABLE savings ADD COLUMN unspecified_hits INTEGER NOT NULL DEFAULT 0;
infra/task-cache/migrations/savings/0003_remove_setup_probes.sql new
+4
@@ -0,0 +1,4 @@
1 +-- Empty installer authentication probes are not task execution history.
2 +DELETE FROM savings
3 +WHERE local_hits = 0 AND remote_hits = 0 AND unspecified_hits = 0
4 + AND executed = 0 AND restore_failures = 0;
infra/task-cache/package.json new
+25
@@ -0,0 +1,25 @@
1 +{
2 + "name": "@t3tools/task-cache",
3 + "private": true,
4 + "type": "module",
5 + "scripts": {
6 + "test": "vp test run --config ../../vite.config.ts --dir .",
7 + "typecheck": "tsc --noEmit",
8 + "smoke": "node scripts/smoke.ts",
9 + "deploy": "alchemy deploy --stage prod",
10 + "writers": "node scripts/manage.ts"
11 + },
12 + "dependencies": {
13 + "alchemy": "2.0.0-beta.80",
14 + "effect": "catalog:",
15 + "jose": "catalog:"
16 + },
17 + "devDependencies": {
18 + "@cloudflare/workers-types": "^4.20260601.1",
19 + "@effect/platform-node": "catalog:",
20 + "@effect/vitest": "catalog:",
21 + "@types/node": "catalog:",
22 + "typescript": "catalog:",
23 + "vite-plus": "catalog:"
24 + }
25 +}
infra/task-cache/scripts/install-ci.ts new
+49
@@ -0,0 +1,49 @@
1 +// @effect-diagnostics nodeBuiltinImport:off - installs the hook before running project tasks.
2 +import * as NodeFSP from "node:fs/promises";
3 +import * as NodeOS from "node:os";
4 +import * as NodePath from "node:path";
5 +import * as NodeURL from "node:url";
6 +
7 +if (process.env.GITHUB_REPOSITORY !== "pingdotgg/t3code" || process.env.GITHUB_ACTIONS !== "true")
8 + throw new Error("This setup is only for the maintainer repository's GitHub Actions.");
9 +const root = NodePath.resolve(import.meta.dirname, "../../..");
10 +const hookConfig = NodePath.join(NodeOS.homedir(), ".config/vite-plus/run-hook.json");
11 +await NodeFSP.mkdir(NodePath.dirname(hookConfig), { recursive: true });
12 +await NodeFSP.writeFile(
13 + hookConfig,
14 + JSON.stringify({
15 + rootMarker: ".task-cache.json",
16 + module: NodePath.join(root, "infra/task-cache/src/runHook.ts"),
17 + }),
18 +);
19 +const { endpoint } = JSON.parse(
20 + await NodeFSP.readFile(NodePath.join(root, ".task-cache.json"), "utf8"),
21 +);
22 +const token = process.env.T3_TASK_CACHE_READ_TOKEN;
23 +if (!token || token.length < 32) throw new Error("Missing task-cache read credential");
24 +const credentialFile = NodePath.join(NodeOS.homedir(), ".config/t3-task-cache/credentials.json");
25 +await NodeFSP.mkdir(NodePath.dirname(credentialFile), { recursive: true, mode: 0o700 });
26 +await NodeFSP.writeFile(
27 + credentialFile,
28 + JSON.stringify({ [endpoint]: { name: "ci-reader", token } }),
29 + { mode: 0o600 },
30 +);
31 +const githubEnv = process.env.GITHUB_ENV;
32 +if (!githubEnv) throw new Error("Missing GitHub Actions environment file");
33 +const directory = process.env.RUNNER_TEMP;
34 +if (!directory) throw new Error("Missing GitHub Actions temporary directory");
35 +const shellSetup = NodePath.join(directory, "t3-task-cache.sh");
36 +const quote = (value: string) => "'" + value.replaceAll("'", "'\"'\"'") + "'";
37 +const preload = NodeURL.pathToFileURL(NodePath.join(root, "infra/task-cache/src/preload.mjs")).href;
38 +// GitHub prohibits NODE_OPTIONS in GITHUB_ENV; bash exports it to task subprocesses instead.
39 +await NodeFSP.writeFile(
40 + shellSetup,
41 + [
42 + process.env.BASH_ENV && process.env.BASH_ENV !== shellSetup
43 + ? `. ${quote(process.env.BASH_ENV)}`
44 + : "",
45 + `export NODE_OPTIONS="\${NODE_OPTIONS:+$NODE_OPTIONS }"${quote(`--import=${preload}`)}`,
46 + "",
47 + ].join("\n"),
48 +);
49 +await NodeFSP.appendFile(githubEnv, `BASH_ENV=${shellSetup}\n`);
infra/task-cache/scripts/install.ts new
+307
@@ -0,0 +1,307 @@
1 +/* oxlint-disable t3code/no-global-process-runtime -- Standalone setup must run before workspace dependencies are installed. */
2 +// @effect-diagnostics nodeBuiltinImport:off - standalone bootstrap also runs before dependencies are installed.
3 +// @effect-diagnostics globalConsole:off globalFetch:off globalDate:off - interactive setup and endpoint verification.
4 +import * as NodeCrypto from "node:crypto";
5 +import * as NodeFSP from "node:fs/promises";
6 +import * as NodeOS from "node:os";
7 +import * as NodePath from "node:path";
8 +import * as NodeURL from "node:url";
9 +import * as NodeReadlinePromises from "node:readline/promises";
10 +import { patchNativeCli } from "../src/nativeHook.js";
11 +
12 +const home = NodeOS.homedir();
13 +const root = NodePath.resolve(import.meta.dirname, "../../..");
14 +const state = NodePath.join(home, ".config/t3-task-cache");
15 +const nonInteractive = process.argv.includes("--non-interactive");
16 +const installed = NodePath.join(home, ".local/share/t3-task-cache");
17 +const hookConfig = NodePath.join(home, ".config/vite-plus/run-hook.json");
18 +const legacySourceLine = '. "$HOME/.config/t3-task-cache/env"';
19 +const sourceLine =
20 + '[ ! -r "$HOME/.config/t3-task-cache/env" ] || . "$HOME/.config/t3-task-cache/env"';
21 +const profiles = [".profile", ".bashrc", ".bash_profile", ".bash_login", ".zshenv", ".zshrc"];
22 +if (process.argv.includes("--uninstall")) {
23 + for (const file of profiles) {
24 + const path = NodePath.join(home, file);
25 + try {
26 + const original = await NodeFSP.readFile(path, "utf8");
27 + const next = original
28 + .split("\n")
29 + .filter((line) => line !== sourceLine && line !== legacySourceLine)
30 + .join("\n");
31 + if (next !== original) {
32 + if (!next.trim() && (file === ".bash_profile" || file === ".bash_login"))
33 + await NodeFSP.unlink(path);
34 + else await NodeFSP.writeFile(path, next);
35 + }
36 + } catch (error) {
37 + if (!(error instanceof Error && "code" in error && error.code === "ENOENT")) throw error;
38 + }
39 + }
40 + try {
41 + const config = JSON.parse(await NodeFSP.readFile(hookConfig, "utf8"));
42 + if (config.module === NodePath.join(installed, "runHook.ts")) await NodeFSP.unlink(hookConfig);
43 + } catch (error) {
44 + if (!(error instanceof Error && "code" in error && error.code === "ENOENT")) throw error;
45 + }
46 + await NodeFSP.rm(NodePath.join(state, "credentials.json"), { force: true });
47 + await NodeFSP.rm(NodePath.join(state, "env"), { force: true });
48 + await NodeFSP.rm(NodePath.join(home, ".cache/t3-task-cache/telemetry"), {
49 + recursive: true,
50 + force: true,
51 + });
52 + // Existing shells still import this path until restarted; leave a harmless loader for them.
53 + await NodeFSP.mkdir(installed, { recursive: true });
54 + await NodeFSP.writeFile(
55 + NodePath.join(installed, "preload.mjs"),
56 + "// Task cache uninstalled. Safe for shells opened before uninstall.\n",
57 + );
58 + for (const file of await NodeFSP.readdir(installed)) {
59 + if (file !== "preload.mjs")
60 + await NodeFSP.rm(NodePath.join(installed, file), { recursive: true, force: true });
61 + }
62 + console.log(
63 + "Task cache uninstalled. Restart existing shells and agents to clear their inherited preload.",
64 + );
65 + process.exit(0);
66 +}
67 +
68 +const { endpoint: defaultEndpoint } = JSON.parse(
69 + await NodeFSP.readFile(NodePath.join(root, ".task-cache.json"), "utf8"),
70 +);
71 +let credentials: Record<string, { name: string; token: string }> = {};
72 +try {
73 + credentials = JSON.parse(
74 + await NodeFSP.readFile(NodePath.join(state, "credentials.json"), "utf8"),
75 + );
76 +} catch (error) {
77 + if (
78 + !(error instanceof SyntaxError) &&
79 + !(error instanceof Error && "code" in error && error.code === "ENOENT")
80 + )
81 + throw error;
82 +}
83 +const prompt = NodeReadlinePromises.createInterface({
84 + input: process.stdin,
85 + output: process.stdout,
86 +});
87 +let endpoint = process.env.T3_CACHE_SETUP_URL || defaultEndpoint;
88 +let name = process.env.T3_CACHE_SETUP_NAME || credentials[endpoint]?.name;
89 +let token = process.env.T3_CACHE_SETUP_TOKEN || credentials[endpoint]?.token;
90 +const initialEndpoint = endpoint;
91 +const initialName = name;
92 +try {
93 + if (!nonInteractive) {
94 + endpoint = (await prompt.question(`Cache URL [${endpoint}]: `)).trim() || endpoint;
95 + name = (await prompt.question(`Developer name${name ? ` [${name}]` : ""}: `)).trim() || name;
96 + prompt.close();
97 + if (process.stdin.isTTY || !token || endpoint !== initialEndpoint || name !== initialName) {
98 + const savedToken = endpoint === initialEndpoint && name === initialName ? token : undefined;
99 + if (!process.stdin.isTTY)
100 + throw new Error("Use T3_CACHE_SETUP_TOKEN for non-interactive setup.");
101 + process.stdout.write(
102 + `Credential token (hidden)${savedToken ? " [Enter keeps saved token]" : ""}: `,
103 + );
104 + process.stdin.setRawMode(true);
105 + process.stdin.resume();
106 + token =
107 + (await new Promise<string>((resolve, reject) => {
108 + let value = "";
109 + const data = (chunk: Buffer) => {
110 + for (const character of chunk.toString()) {
111 + if (character === "\u0003") {
112 + cleanup();
113 + reject(new Error("Cancelled"));
114 + return;
115 + }
116 + if (character === "\r" || character === "\n") {
117 + cleanup();
118 + resolve(value);
119 + return;
120 + }
121 + if (character === "\u007f") value = value.slice(0, -1);
122 + else value += character;
123 + }
124 + };
125 + const cleanup = () => {
126 + process.stdin.off("data", data);
127 + process.stdin.setRawMode(false);
128 + process.stdin.pause();
129 + process.stdout.write("\n");
130 + };
131 + process.stdin.on("data", data);
132 + })) || savedToken;
133 + }
134 + }
135 +} finally {
136 + prompt.close();
137 +}
138 +if (!name || !token || !/^[a-z0-9-]{1,64}$/.test(name) || token.length < 32)
139 + throw new Error("A valid developer name and credential token are required.");
140 +const url = new URL(endpoint);
141 +if (
142 + (url.protocol !== "https:" &&
143 + !(
144 + url.protocol === "http:" &&
145 + (url.hostname === "localhost" ||
146 + /^127(?:\.\d{1,3}){3}$/.test(url.hostname) ||
147 + url.hostname === "[::1]")
148 + )) ||
149 + url.username ||
150 + url.password ||
151 + url.search ||
152 + url.hash
153 +)
154 + throw new Error(
155 + "Use an HTTPS cache endpoint without embedded credentials, query, or fragment (HTTP is only allowed on loopback).",
156 + );
157 +endpoint = url.href.replace(/\/$/, "");
158 +const response = await fetch(`${endpoint}/telemetry`, {
159 + method: "POST",
160 + headers: {
161 + Authorization: `Basic ${Buffer.from(`${name}:${token}`).toString("base64")}`,
162 + "Content-Type": "application/json",
163 + },
164 + body: JSON.stringify({
165 + id: NodeCrypto.randomBytes(32).toString("hex"),
166 + recordedAt: Date.now(),
167 + host: NodeOS.hostname(),
168 + platform: NodeOS.platform(),
169 + arch: NodeOS.arch(),
170 + ci: false,
171 + runId: "setup",
172 + tasks: [],
173 + }),
174 + signal: AbortSignal.timeout(10_000),
175 +});
176 +await response.body?.cancel();
177 +if (!response.ok)
178 + throw new Error(`Credential verification failed (${response.status}); nothing was installed.`);
179 +
180 +const candidates = [NodePath.join(root, "node_modules/vite-plus/dist/bin.js")];
181 +for (const directory of [
182 + process.env.VITE_PLUS_HOME,
183 + NodePath.join(home, ".vite-plus/current"),
184 + NodePath.join(home, ".local/share/vite-plus/current"),
185 +]) {
186 + if (directory) candidates.push(NodePath.join(directory, "node_modules/vite-plus/dist/bin.js"));
187 +}
188 +for (let index = 0; index < process.argv.length; index++) {
189 + if (process.argv[index] === "--entrypoint") {
190 + const entrypoint = process.argv[++index];
191 + if (!entrypoint) throw new Error("Missing --entrypoint path");
192 + candidates.push(entrypoint);
193 + }
194 +}
195 +const targets = new Map<string, { original: string; patched: string }>();
196 +for (const candidate of candidates) {
197 + try {
198 + const path = await NodeFSP.realpath(candidate);
199 + const original = await NodeFSP.readFile(path, "utf8");
200 + const backup = await NodeFSP.readFile(`${path}.before-task-cache`, "utf8").catch(
201 + (error: NodeJS.ErrnoException) => {
202 + if (error.code === "ENOENT") return undefined;
203 + throw error;
204 + },
205 + );
206 + if (backup && (original === backup || original === patchNativeCli(backup)))
207 + targets.set(path, { original: backup, patched: original });
208 + } catch (error) {
209 + if (!(error instanceof Error && "code" in error && error.code === "ENOENT")) throw error;
210 + }
211 +}
212 +
213 +await NodeFSP.mkdir(installed, { recursive: true });
214 +await NodeFSP.writeFile(NodePath.join(installed, "package.json"), '{"type":"module"}\n');
215 +for (const file of [
216 + "runHook.ts",
217 + "remoteCache.ts",
218 + "flush.ts",
219 + "telemetry.ts",
220 + "nativeHook.js",
221 + "nativeRunHook.js",
222 + "preload.mjs",
223 +]) {
224 + const destination = NodePath.join(installed, file);
225 + const temporary = `${destination}.${NodeCrypto.randomUUID()}.tmp`;
226 + try {
227 + await NodeFSP.copyFile(NodePath.join(import.meta.dirname, "../src", file), temporary);
228 + await NodeFSP.rename(temporary, destination);
229 + } finally {
230 + await NodeFSP.rm(temporary, { force: true });
231 + }
232 +}
233 +await NodeFSP.mkdir(NodePath.dirname(hookConfig), { recursive: true });
234 +await NodeFSP.writeFile(
235 + hookConfig,
236 + JSON.stringify({
237 + rootMarker: ".task-cache.json",
238 + module: NodePath.join(installed, "runHook.ts"),
239 + }),
240 +);
241 +for (const [path, { original }] of targets) {
242 + await NodeFSP.writeFile(path, original);
243 + await NodeFSP.rm(`${path}.before-task-cache`, { force: true });
244 + await NodeFSP.rm(NodePath.join(NodePath.dirname(path), "run-hook.js"), { force: true });
245 +}
246 +for (const file of [
247 + "nativeHook.ts",
248 + "launcher-telemetry.ts",
249 + "launcherTelemetry.ts",
250 + "vp.ts",
251 + "bin",
252 +])
253 + await NodeFSP.rm(NodePath.join(installed, file), { recursive: true, force: true });
254 +await NodeFSP.mkdir(state, { recursive: true, mode: 0o700 });
255 +credentials[endpoint] = { name, token };
256 +const temporaryCredentials = NodePath.join(state, `.credentials-${NodeCrypto.randomUUID()}.tmp`);
257 +try {
258 + await NodeFSP.writeFile(temporaryCredentials, JSON.stringify(credentials), { mode: 0o600 });
259 + await NodeFSP.rename(temporaryCredentials, NodePath.join(state, "credentials.json"));
260 +} finally {
261 + await NodeFSP.rm(temporaryCredentials, { force: true });
262 +}
263 +
264 +const environmentFile = NodePath.join(state, "env");
265 +const preload = NodeURL.pathToFileURL(NodePath.join(installed, "preload.mjs")).href;
266 +await NodeFSP.writeFile(
267 + environmentFile,
268 + [
269 + `if [ -r "${NodePath.join(installed, "preload.mjs")}" ]; then`,
270 + `case " \${NODE_OPTIONS-} " in`,
271 + ` *" --import=${preload} "*) ;;`,
272 + ` *) export NODE_OPTIONS="\${NODE_OPTIONS:+$NODE_OPTIONS }--import=${preload}" ;;`,
273 + `esac`,
274 + `fi`,
275 + "",
276 + ].join("\n"),
277 + { mode: 0o600 },
278 +);
279 +for (const file of profiles) {
280 + const path = NodePath.join(home, file);
281 + let original = "";
282 + try {
283 + original = await NodeFSP.readFile(path, "utf8");
284 + } catch (error) {
285 + if (!(error instanceof Error && "code" in error && error.code === "ENOENT")) throw error;
286 + }
287 + if (file === ".bash_profile" || file === ".bash_login") {
288 + if (!original) continue;
289 + // Earlier installers created a profile that masks the user’s existing login profile.
290 + if (file === ".bash_profile" && [sourceLine, legacySourceLine].includes(original.trim())) {
291 + await NodeFSP.unlink(path);
292 + continue;
293 + }
294 + }
295 + if (original.split("\n").includes(legacySourceLine)) {
296 + original = original
297 + .split("\n")
298 + .filter((line) => line !== legacySourceLine)
299 + .join("\n");
300 + await NodeFSP.writeFile(path, original);
301 + }
302 + if (!original.split("\n").includes(sourceLine))
303 + await NodeFSP.appendFile(path, `\n${sourceLine}\n`);
304 +}
305 +console.log(
306 + "Verified credentials and installed the machine-wide Vite+ hook. Open a fresh shell to enable it in all worktrees. Executables and PATH are unchanged.",
307 +);
infra/task-cache/scripts/manage.ts new
+72
@@ -0,0 +1,72 @@
1 +import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
2 +import * as NodeServices from "@effect/platform-node/NodeServices";
3 +import * as ChildProcess from "effect/process/ChildProcess";
4 +import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
5 +import * as Schema from "effect/Schema";
6 +import * as Console from "effect/Console";
7 +import * as Layer from "effect/Layer";
8 +import * as Effect from "effect/Effect";
9 +import * as Flag from "effect/cli/Flag";
10 +import * as Argument from "effect/cli/Argument";
11 +import * as Command from "effect/cli/Command";
12 +
13 +import * as WriterRegistry from "../src/WriterRegistry.ts";
14 +
15 +const decodeDeployment = Schema.decodeEffect(
16 + Schema.fromJsonString(Schema.Struct({ url: Schema.String })),
17 +);
18 +
19 +const name = Argument.String("name").pipe(Argument.withDescription("Lowercase writer name"));
20 +const updateWriter = Effect.fn("TaskCache.manageWriter")(function* (
21 + name: string,
22 + revoke: boolean,
23 + initialize = false,
24 +) {
25 + const registry = yield* WriterRegistry.WriterRegistry;
26 + let endpoint: string | undefined;
27 + if (!revoke && WriterRegistry.isWriterName(name)) {
28 + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
29 + const output = yield* spawner.string(
30 + ChildProcess.make(
31 + "node",
32 + [
33 + `${import.meta.dirname}/../node_modules/alchemy/bin/cli.js`,
34 + "state",
35 + "read",
36 + "T3TaskCache/prod/output",
37 + "--backend",
38 + "cloudflare",
39 + "--no-input",
40 + ],
41 + { cwd: `${import.meta.dirname}/..`, stderr: "inherit" },
42 + ),
43 + );
44 + const deployed = yield* decodeDeployment(output);
45 + endpoint = new URL("/projects/t3code", deployed.url).href;
46 + }
47 + const file = yield* registry.update(name, revoke, endpoint, initialize);
48 + yield* Console.log(
49 + file === undefined
50 + ? `Removed ${name} from the local writer registry.`
51 + : `Private credential written to ${file}.`,
52 + );
53 + yield* Console.log(
54 + "Run alchemy deploy --stage prod from infra/task-cache to apply the writer changes.",
55 + );
56 +});
57 +
58 +const command = Command.make("task-cache").pipe(
59 + Command.withSubcommands([
60 + Command.make(
61 + "add-writer",
62 + { name, newRegistry: Flag.Boolean("new-registry").pipe(Flag.withDefault(false)) },
63 + ({ name, newRegistry }) => updateWriter(name, false, newRegistry),
64 + ),
65 + Command.make("revoke-writer", { name }, ({ name }) => updateWriter(name, true)),
66 + ]),
67 +);
68 +
69 +Command.run(command, { version: "1.0.0" }).pipe(
70 + Effect.provide(WriterRegistry.layer.pipe(Layer.provideMerge(NodeServices.layer))),
71 + NodeRuntime.runMain,
72 +);
infra/task-cache/scripts/smoke.ts new
+130
@@ -0,0 +1,130 @@
1 +// @effect-diagnostics nodeBuiltinImport:off - exercises real child processes and isolated workspaces.
2 +// @effect-diagnostics globalConsole:off - emits the deployment acceptance transcript.
3 +// @effect-diagnostics globalFetch:off - probes the deployed Worker directly.
4 +import * as NodeChildProcess from "node:child_process";
5 +import * as NodeCrypto from "node:crypto";
6 +import * as NodeFSP from "node:fs/promises";
7 +import * as NodeOS from "node:os";
8 +import * as NodePath from "node:path";
9 +
10 +const project = NodePath.join(import.meta.dirname, "../../..");
11 +const env = {
12 + ...process.env,
13 + T3_TASK_CACHE_TELEMETRY: "off",
14 + VP_REMOTE_CACHE: "read-write",
15 + VP_REMOTE_CACHE_URL: undefined,
16 +};
17 +const root = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-cache-probe-"));
18 +const name = `t3-cache-probe-${NodeCrypto.randomUUID()}`;
19 +
20 +async function run(directory: string, home = NodeOS.homedir()) {
21 + return new Promise<string>((resolve, reject) => {
22 + const child = NodeChildProcess.spawn(
23 + NodePath.join(project, "node_modules/.bin/vp"),
24 + ["run", "--cache", "build"],
25 + {
26 + cwd: NodePath.join(root, directory),
27 + env: {
28 + ...env,
29 + HOME: home,
30 + PATH: `${NodePath.join(project, "node_modules/.bin")}${NodePath.delimiter}${process.env.PATH}`,
31 + },
32 + stdio: ["ignore", "pipe", "pipe"],
33 + },
34 + );
35 + let output = "";
36 + child.stdout.on("data", (chunk) => {
37 + output += chunk;
38 + });
39 + child.stderr.on("data", (chunk) => {
40 + output += chunk;
41 + });
42 + child.on("error", reject);
43 + child.on("close", (code) => (code === 0 ? resolve(output) : reject(new Error(output))));
44 + });
45 +}
46 +
47 +try {
48 + const { endpoint } = JSON.parse(
49 + await NodeFSP.readFile(NodePath.join(project, ".task-cache.json"), "utf8"),
50 + );
51 + for (const [path, method] of [
52 + ["fetch", "POST"],
53 + ["blob/12345678-1234-1234-1234-123456789abc", "GET"],
54 + ] as const) {
55 + const response = await fetch(`${endpoint}/${path}`, { method });
56 + await response.body?.cancel();
57 + if (response.status !== 401)
58 + throw new Error(`Anonymous ${path} returned ${response.status}, expected 401.`);
59 + }
60 + const unauthorized = await fetch(`${endpoint}/store`, { method: "POST" });
61 + await unauthorized.body?.cancel();
62 + if (unauthorized.status !== 401)
63 + throw new Error(`Anonymous upload returned ${unauthorized.status}, expected 401.`);
64 + for (const directory of ["a", "b"]) {
65 + const cwd = NodePath.join(root, directory);
66 + await NodeFSP.mkdir(cwd);
67 + await NodeFSP.writeFile(NodePath.join(cwd, ".task-cache.json"), JSON.stringify({ endpoint }));
68 + await NodeFSP.writeFile(
69 + NodePath.join(cwd, "vite.config.ts"),
70 + `import { remoteCacheUrl } from ${JSON.stringify(NodePath.join(project, "infra/task-cache/src/remoteCache.ts"))}; export default {run:{cache:{scripts:true,remote:{url:remoteCacheUrl(import.meta.dirname)}}}}`,
71 + );
72 + await NodeFSP.writeFile(
73 + NodePath.join(cwd, "package.json"),
74 + JSON.stringify({
75 + name,
76 + private: true,
77 + scripts: { build: `node build.cjs ${name}` },
78 + }),
79 + );
80 + await NodeFSP.writeFile(
81 + NodePath.join(cwd, "build.cjs"),
82 + 'if(process.env.VP_REMOTE_CACHE_URL) throw new Error("Cache URL leaked into task environment"); const fs=require("node:fs"); fs.writeFileSync("output.txt",fs.readFileSync("input.txt")); console.log("computed");',
83 + );
84 + await NodeFSP.writeFile(NodePath.join(cwd, "input.txt"), "version-a");
85 + }
86 + console.log("First checkout:\n" + (await run("a")));
87 + const readerFile = NodePath.join(NodeOS.homedir(), ".config/t3-task-cache/ci-reader.json");
88 + let reader;
89 + try {
90 + reader = JSON.parse(await NodeFSP.readFile(readerFile, "utf8"));
91 + } catch (error) {
92 + if (!(error instanceof Error) || !("code" in error) || error.code !== "ENOENT") throw error;
93 + const credentials = JSON.parse(
94 + await NodeFSP.readFile(
95 + NodePath.join(NodeOS.homedir(), ".config/t3-task-cache/credentials.json"),
96 + "utf8",
97 + ),
98 + );
99 + reader = credentials[endpoint];
100 + }
101 + const otherHome = NodePath.join(root, "other-developer");
102 + await NodeFSP.mkdir(NodePath.join(otherHome, ".config/t3-task-cache"), { recursive: true });
103 + await NodeFSP.writeFile(
104 + NodePath.join(otherHome, ".config/t3-task-cache/credentials.json"),
105 + JSON.stringify({
106 + [endpoint]: reader,
107 + }),
108 + );
109 + const second = await run("b", otherHome);
110 + console.log("Cold second checkout:\n" + second);
111 + if (!second.includes("remote cache hit"))
112 + throw new Error("Second checkout did not reuse the remote result.");
113 + if ((await NodeFSP.readFile(NodePath.join(root, "b/output.txt"), "utf8")) !== "version-a")
114 + throw new Error("Output was not restored.");
115 + await NodeFSP.writeFile(NodePath.join(root, "b/input.txt"), "version-b");
116 + const changed = await run("b");
117 + console.log("Changed input:\n" + changed);
118 + if (
119 + changed.includes("cache hit") ||
120 + (await NodeFSP.readFile(NodePath.join(root, "b/output.txt"), "utf8")) !== "version-b"
121 + )
122 + throw new Error("Changed input reused stale output.");
123 + await NodeFSP.rm(NodePath.join(root, "a/node_modules"), { recursive: true, force: true });
124 + console.log("Original input after competing writer:\n" + (await run("a")));
125 + console.log(
126 + "PASS: authenticated upload, cold-checkout restoration, input validation, and anonymous-write rejection.",
127 + );
128 +} finally {
129 + await NodeFSP.rm(root, { recursive: true, force: true });
130 +}
infra/task-cache/src/Admission.test.ts new
+49
@@ -0,0 +1,49 @@
1 +import { expect, it } from "@effect/vitest";
2 +import * as Deferred from "effect/Deferred";
3 +import * as Effect from "effect/Effect";
4 +import * as Fiber from "effect/Fiber";
5 +import * as Admission from "./Admission.ts";
6 +
7 +it.effect("queues uploads and releases the permit when an upload is interrupted", () =>
8 + Effect.gen(function* () {
9 + const admission = yield* Admission.Admission;
10 + const entered = yield* Deferred.make<void>();
11 + const secondEntered = yield* Deferred.make<void>();
12 + const first = yield* admission
13 + .withPermit(true, Deferred.succeed(entered, undefined).pipe(Effect.andThen(Effect.never)))
14 + .pipe(Effect.forkChild);
15 + yield* Deferred.await(entered);
16 + const second = yield* admission
17 + .withPermit(
18 + true,
19 + Deferred.succeed(secondEntered, undefined).pipe(Effect.andThen(Effect.never)),
20 + )
21 + .pipe(Effect.forkChild);
22 + yield* Deferred.await(secondEntered);
23 + const third = yield* admission
24 + .withPermit(true, Effect.succeed("restored"))
25 + .pipe(Effect.forkChild);
26 + yield* Fiber.interrupt(first);
27 + expect(yield* Fiber.join(third)).toBe("restored");
28 + yield* Fiber.interrupt(second);
29 + }).pipe(Effect.provide(Admission.layer)),
30 +);
31 +
32 +it.effect("reads continue while all upload slots are occupied", () =>
33 + Effect.gen(function* () {
34 + const admission = yield* Admission.Admission;
35 + const entered = yield* Deferred.make<void>();
36 + const occupied = yield* admission
37 + .withPermit(
38 + true,
39 + admission.withPermit(
40 + true,
41 + Deferred.succeed(entered, undefined).pipe(Effect.andThen(Effect.never)),
42 + ),
43 + )
44 + .pipe(Effect.forkChild);
45 + yield* Deferred.await(entered);
46 + expect(yield* admission.withPermit(false, Effect.succeed("hit"))).toBe("hit");
47 + yield* Fiber.interrupt(occupied);
48 + }).pipe(Effect.provide(Admission.layer)),
49 +);
infra/task-cache/src/Admission.ts new
+28
@@ -0,0 +1,28 @@
1 +import * as Context from "effect/Context";
2 +import * as Effect from "effect/Effect";
3 +import * as Layer from "effect/Layer";
4 +import * as Semaphore from "effect/Semaphore";
5 +
6 +export class Admission extends Context.Service<
7 + Admission,
8 + {
9 + readonly withPermit: <A, E, R>(
10 + store: boolean,
11 + task: Effect.Effect<A, E, R>,
12 + ) => Effect.Effect<A, E, R>;
13 + }
14 +>()("@t3tools/task-cache/Admission") {}
15 +
16 +const make = Effect.gen(function* () {
17 + // Bound simultaneous upload buffers; Effect cancels queued waiters and releases permits.
18 + const stores = yield* Semaphore.make(2);
19 + const reads = yield* Semaphore.make(4);
20 + return Admission.of({
21 + // The caller owns the request span; admission adds no second span per request.
22 + withPermit: Effect.fnUntraced(function* (store, task) {
23 + return yield* (store ? stores : reads).withPermits(1)(task);
24 + }),
25 + });
26 +});
27 +
28 +export const layer = Layer.effect(Admission, make);
infra/task-cache/src/Cache.test.ts new
+324
@@ -0,0 +1,324 @@
1 +import { expect, it } from "@effect/vitest";
2 +import * as Effect from "effect/Effect";
3 +import * as Deferred from "effect/Deferred";
4 +import * as Fiber from "effect/Fiber";
5 +import { Deadline } from "../vendor/vite-plus-remote-cache-cloudflare/src/streams.ts";
6 +import * as Layer from "effect/Layer";
7 +import { vi } from "vite-plus/test";
8 +import { Observations } from "../vendor/vite-plus-remote-cache-cloudflare/src/observations.ts";
9 +import * as Admission from "./Admission.ts";
10 +import * as Cache from "./Cache.ts";
11 +import * as Savings from "./Savings.ts";
12 +
13 +vi.mock("../vendor/vite-plus-remote-cache-cloudflare/src/database.ts", () => ({
14 + getScope: async () => ({}),
15 + selectBlob: async () => ({ blob_object: "artifact", blob_size: 4 }),
16 + cleanup: async () => {},
17 +}));
18 +
19 +it.effect("includes successful blob downloads in exported R2 usage", () =>
20 + Effect.gen(function* () {
21 + const get = vi.fn(async () => ({ size: 4, body: new Blob(["data"]).stream() }));
22 + const bindings = {
23 + developerKeys: JSON.stringify({
24 + julius: "210eb3d2c67a7207923e1999751300a87a2dd22839b258b80c3d805a6fe64984",
25 + }),
26 + ARTIFACTS: { get },
27 + readLimit: { limit: async () => ({ success: true }) },
28 + } as unknown as Cache.Bindings["Service"];
29 + using observations = vi.spyOn(Observations.prototype, "toJSON");
30 + const response = yield* Effect.gen(function* () {
31 + const cache = yield* Cache.Cache;
32 + return yield* cache.handle(
33 + new Request(
34 + "https://cache.example/projects/t3code/blob/12345678-1234-1234-1234-123456789abc",
35 + {
36 + headers: {
37 + authorization: `Basic ${btoa("julius:fixture-token-012345678901234567890123456789")}`,
38 + },
39 + },
40 + ),
41 + { waitUntil: () => {} },
42 + );
43 + }).pipe(
44 + Effect.provide(
45 + Cache.layer.pipe(
46 + Layer.provide([
47 + Layer.succeed(Cache.Bindings, bindings),
48 + Admission.layer,
49 + Layer.succeed(Savings.Savings, {
50 + record: () => Effect.void,
51 + emitTotals: () => Effect.void,
52 + cleanup: Effect.void,
53 + }),
54 + ]),
55 + ),
56 + ),
57 + );
58 + expect(response.status).toBe(200);
59 + expect(yield* Effect.promise(() => response.text())).toBe("data");
60 + expect(get).toHaveBeenCalledWith("artifact");
61 + expect(observations.mock.results.at(-1)?.value.r2_operations).toBe(1);
62 + }),
63 +);
64 +
65 +it.effect("aborts the upstream deadline before disposing it on interruption", () =>
66 + Effect.gen(function* () {
67 + const entered = yield* Deferred.make<void>();
68 + const cancelled: boolean[] = [];
69 + const originalDispose = Deadline.prototype.dispose;
70 + using _dispose = vi.spyOn(Deadline.prototype, "dispose").mockImplementation(function (
71 + this: Deadline,
72 + ) {
73 + try {
74 + this.check();
75 + cancelled.push(false);
76 + } catch {
77 + cancelled.push(true);
78 + }
79 + originalDispose.call(this);
80 + });
81 + const bindings = {
82 + developerKeys: JSON.stringify({
83 + julius: "210eb3d2c67a7207923e1999751300a87a2dd22839b258b80c3d805a6fe64984",
84 + }),
85 + ARTIFACTS: {
86 + get: () => {
87 + Deferred.doneUnsafe(entered, Effect.void);
88 + return new Promise(() => {});
89 + },
90 + },
91 + readLimit: { limit: async () => ({ success: true }) },
92 + } as unknown as Cache.Bindings["Service"];
93 + yield* Effect.gen(function* () {
94 + const cache = yield* Cache.Cache;
95 + const fiber = yield* cache
96 + .handle(
97 + new Request(
98 + "https://cache.example/projects/t3code/blob/12345678-1234-1234-1234-123456789abc",
99 + {
100 + headers: {
101 + authorization: `Basic ${btoa("julius:fixture-token-012345678901234567890123456789")}`,
102 + },
103 + },
104 + ),
105 + { waitUntil: () => {} },
106 + )
107 + .pipe(Effect.forkChild);
108 + yield* Deferred.await(entered);
109 + yield* Fiber.interrupt(fiber);
110 + expect(cancelled).toEqual([true]);
111 + }).pipe(
112 + Effect.provide(
113 + Cache.layer.pipe(
114 + Layer.provide([
115 + Layer.succeed(Cache.Bindings, bindings),
116 + Admission.layer,
117 + Layer.succeed(Savings.Savings, {
118 + record: () => Effect.void,
119 + emitTotals: () => Effect.void,
120 + cleanup: Effect.void,
121 + }),
122 + ]),
123 + ),
124 + ),
125 + );
126 + }),
127 +);
128 +
129 +it.effect("does not charge authenticated budgets for anonymous or malformed requests", () => {
130 + const limit = vi.fn(async (_options: { key: string }) => ({ success: true }));
131 + return Effect.gen(function* () {
132 + const cache = yield* Cache.Cache;
133 + for (const operation of [
134 + "fetch",
135 + "store",
136 + "telemetry",
137 + "blob/12345678-1234-1234-1234-123456789abc",
138 + ]) {
139 + for (const method of ["GET", "POST"]) {
140 + yield* cache.handle(
141 + new Request(`https://cache.example/projects/t3code/${operation}`, { method }),
142 + { waitUntil: () => {} },
143 + );
144 + }
145 + }
146 + expect(limit).not.toHaveBeenCalled();
147 + const headers = {
148 + authorization: `Basic ${btoa("julius:fixture-token-012345678901234567890123456789")}`,
149 + };
150 + yield* cache.handle(
151 + new Request("https://cache.example/projects/t3code/telemetry", {
152 + method: "POST",
153 + headers,
154 + body: "{}",
155 + }),
156 + { waitUntil: () => {} },
157 + );
158 + yield* cache.handle(
159 + new Request(
160 + "https://cache.example/projects/t3code/blob/12345678-1234-1234-1234-123456789abc",
161 + { headers },
162 + ),
163 + { waitUntil: () => {} },
164 + );
165 + expect(limit.mock.calls).toEqual([[{ key: "t3code:telemetry" }], [{ key: "t3code:blob" }]]);
166 + }).pipe(
167 + Effect.provide(
168 + Cache.layer.pipe(
169 + Layer.provide([
170 + Layer.succeed(Cache.Bindings, {
171 + developerKeys: JSON.stringify({
172 + julius: "210eb3d2c67a7207923e1999751300a87a2dd22839b258b80c3d805a6fe64984",
173 + }),
174 + readerKeys: "{}",
175 + readLimit: { limit },
176 + storeLimit: { limit },
177 + ARTIFACTS: { get: async () => ({ size: 4, body: new Blob(["data"]).stream() }) },
178 + } as unknown as Cache.Bindings["Service"]),
179 + Admission.layer,
180 + Layer.succeed(Savings.Savings, {
181 + record: () => Effect.void,
182 + emitTotals: () => Effect.void,
183 + cleanup: Effect.void,
184 + }),
185 + ]),
186 + ),
187 + ),
188 + );
189 +});
190 +
191 +it.effect("authenticates setup probes without recording empty savings", () => {
192 + const record = vi.fn(() => Effect.void);
193 + const emitTotals = vi.fn(() => Effect.void);
194 + return Effect.gen(function* () {
195 + const cache = yield* Cache.Cache;
196 + const response = yield* cache.handle(
197 + new Request("https://cache.example/projects/t3code/telemetry", {
198 + method: "POST",
199 + headers: {
200 + authorization: `Basic ${btoa("julius:fixture-token-012345678901234567890123456789")}`,
201 + },
202 + body: JSON.stringify({
203 + id: "a".repeat(64),
204 + recordedAt: 0,
205 + host: "setup-host",
206 + platform: "linux",
207 + arch: "x64",
208 + ci: false,
209 + runId: "setup",
210 + tasks: [],
211 + }),
212 + }),
213 + { waitUntil: () => {} },
214 + );
215 + expect(response.status).toBe(204);
216 + expect(record).not.toHaveBeenCalled();
217 + expect(emitTotals).not.toHaveBeenCalled();
218 + }).pipe(
219 + Effect.provide(
220 + Cache.layer.pipe(
221 + Layer.provide([
222 + Layer.succeed(Cache.Bindings, {
223 + developerKeys: JSON.stringify({
224 + julius: "210eb3d2c67a7207923e1999751300a87a2dd22839b258b80c3d805a6fe64984",
225 + }),
226 + readLimit: { limit: async () => ({ success: true }) },
227 + } as unknown as Cache.Bindings["Service"]),
228 + Admission.layer,
229 + Layer.succeed(Savings.Savings, { record, emitTotals, cleanup: Effect.void }),
230 + ]),
231 + ),
232 + ),
233 + );
234 +});
235 +
236 +it.effect("keeps artifacts private and limits CI reader credentials to reads", () =>
237 + Effect.gen(function* () {
238 + const cache = yield* Cache.Cache;
239 + const context = { waitUntil: () => {} };
240 + const blob = "https://cache.example/projects/t3code/blob/12345678-1234-1234-1234-123456789abc";
241 + for (const url of [blob, "https://cache.example/projects/t3code/fetch"]) {
242 + const method = url === blob ? "GET" : "POST";
243 + expect((yield* cache.handle(new Request(url, { method }), context)).status).toBe(401);
244 + expect(
245 + (yield* cache.handle(
246 + new Request(url, {
247 + method,
248 + headers: { authorization: `Basic ${btoa("ci-reader:wrong")}` },
249 + }),
250 + context,
251 + )).status,
252 + ).toBe(403);
253 + }
254 + const headers = {
255 + authorization: `Basic ${btoa("ci-reader:fixture-token-012345678901234567890123456789")}`,
256 + };
257 + expect((yield* cache.handle(new Request(blob, { headers }), context)).status).toBe(200);
258 + expect(
259 + (yield* cache.handle(
260 + new Request("https://cache.example/projects/t3code/store", { method: "POST", headers }),
261 + context,
262 + )).status,
263 + ).toBe(403);
264 + }).pipe(
265 + Effect.provide(
266 + Cache.layer.pipe(
267 + Layer.provide([
268 + Layer.succeed(Cache.Bindings, {
269 + developerKeys: "{}",
270 + readerKeys: JSON.stringify({
271 + "ci-reader": "210eb3d2c67a7207923e1999751300a87a2dd22839b258b80c3d805a6fe64984",
272 + }),
273 + ARTIFACTS: { get: vi.fn(async () => ({ size: 4, body: new Blob(["data"]).stream() })) },
274 + readLimit: { limit: async () => ({ success: true }) },
275 + storeLimit: { limit: async () => ({ success: true }) },
276 + } as unknown as Cache.Bindings["Service"]),
277 + Admission.layer,
278 + Layer.succeed(Savings.Savings, {
279 + record: () => Effect.void,
280 + emitTotals: () => Effect.void,
281 + cleanup: Effect.void,
282 + }),
283 + ]),
284 + ),
285 + ),
286 + ),
287 +);
288 +
289 +it.effect("rejects anonymous and invalid Basic requests before admission", () =>
290 + Effect.gen(function* () {
291 + const cache = yield* Cache.Cache;
292 + for (const operation of ["store", "telemetry", "fetch"]) {
293 + for (const authorization of [undefined, `Basic ${btoa("attacker:invalid")}`]) {
294 + const response = yield* cache.handle(
295 + new Request(`https://cache.example/projects/t3code/${operation}`, {
296 + method: "POST",
297 + headers: authorization ? { authorization } : {},
298 + }),
299 + { waitUntil: () => {} },
300 + );
301 + expect(response.status).toBe(authorization ? 403 : 401);
302 + }
303 + }
304 + }).pipe(
305 + Effect.provide(
306 + Cache.layer.pipe(
307 + Layer.provide([
308 + Layer.succeed(Cache.Bindings, {
309 + developerKeys: "{}",
310 + readerKeys: "{}",
311 + } as Cache.Bindings["Service"]),
312 + Layer.succeed(Admission.Admission, {
313 + withPermit: () => Effect.die("Unauthenticated request reached admission"),
314 + }),
315 + Layer.succeed(Savings.Savings, {
316 + record: () => Effect.void,
317 + emitTotals: () => Effect.void,
318 + cleanup: Effect.void,
319 + }),
320 + ]),
321 + ),
322 + ),
323 + ),
324 +);
infra/task-cache/src/Cache.ts new
+240
@@ -0,0 +1,240 @@
1 +import * as Context from "effect/Context";
2 +import * as Effect from "effect/Effect";
3 +import * as Clock from "effect/Clock";
4 +import * as Layer from "effect/Layer";
5 +import * as Schema from "effect/Schema";
6 +import type * as Cloudflare from "@cloudflare/workers-types";
7 +import { Summary, recordSummary } from "./Summary.ts";
8 +import * as Savings from "./Savings.ts";
9 +import type { ProtocolBindings } from "./protocolTypes.ts";
10 +import * as Admission from "./Admission.ts";
11 +import { developerIdentity } from "./developerAuth.ts";
12 +
13 +const decodeSummary = Schema.decodeEffect(Schema.fromJsonString(Summary));
14 +
15 +export class CacheError extends Schema.TaggedError<CacheError>()("CacheError", {
16 + status: Schema.Number,
17 + reason: Schema.String,
18 +}) {}
19 +
20 +export class Bindings extends Context.Service<
21 + Bindings,
22 + ProtocolBindings & {
23 + readonly developerKeys: string;
24 + readonly readerKeys: string;
25 + readonly readLimit: Cloudflare.RateLimit;
26 + readonly storeLimit: Cloudflare.RateLimit;
27 + }
28 +>()("@t3tools/task-cache/Cache/Bindings") {}
29 +
30 +export class Cache extends Context.Service<
31 + Cache,
32 + {
33 + readonly handle: (
34 + request: Request,
35 + context: Pick<Cloudflare.ExecutionContext, "waitUntil">,
36 + ) => Effect.Effect<Response>;
37 + readonly cleanup: Effect.Effect<void, CacheError>;
38 + }
39 +>()("@t3tools/task-cache/Cache") {}
40 +
41 +const make = Effect.gen(function* () {
42 + const bindings = yield* Bindings;
43 + const admission = yield* Admission.Admission;
44 + const savings = yield* Savings.Savings;
45 + // The source-only protocol dependency is bundled for Workers, never evaluated by Node during deployment.
46 + const {
47 + authorizeCacheWrite,
48 + authorizeTelemetry,
49 + keys,
50 + cborResponse,
51 + cleanup,
52 + getScope,
53 + selectBlob,
54 + errorResponse,
55 + HttpError,
56 + fetchMetadata,
57 + defaults,
58 + store,
59 + readBody,
60 + Deadline,
61 + Observations,
62 + } = yield* Effect.promise(() => import("./protocol.ts"));
63 +
64 + // The upstream adapter owns the experimental wire format and atomic D1 publication.
65 + const protocol = <A>(run: (signal: AbortSignal) => Promise<A>) =>
66 + Effect.tryPromise({
67 + try: run,
68 + catch: (cause) =>
69 + new CacheError({
70 + status: cause instanceof HttpError ? cause.status : 503,
71 + reason: cause instanceof HttpError ? cause.code : "unavailable",
72 + }),
73 + });
74 +
75 + const checkLimit = Effect.fn("TaskCache.checkLimit")(function* (operation: string) {
76 + const allowed = yield* protocol(() =>
77 + (operation === "store" ? bindings.storeLimit : bindings.readLimit).limit({
78 + key: `t3code:${operation.startsWith("blob/") ? "blob" : operation}`,
79 + }),
80 + );
81 + if (!allowed.success) return yield* new CacheError({ status: 429, reason: "rate_limit" });
82 + });
83 +
84 + const handle = Effect.fn("TaskCache.handle")(
85 + function* (request: Request, context: Pick<Cloudflare.ExecutionContext, "waitUntil">) {
86 + const url = new URL(request.url);
87 + const route = /^\/projects\/t3code\/(fetch|store|telemetry|blob\/([0-9a-f-]{36}))$/.exec(
88 + url.pathname,
89 + );
90 + const operation = route?.[1] ?? "unknown";
91 + const writing = operation === "store";
92 + yield* Effect.annotateCurrentSpan({
93 + "cache.operation": operation.startsWith("blob/") ? "blob" : operation,
94 + });
95 + if (
96 + !route ||
97 + url.search ||
98 + request.method !== (operation.startsWith("blob/") ? "GET" : "POST")
99 + ) {
100 + return yield* new CacheError({ status: 404, reason: "unknown_route" });
101 + }
102 + if (!request.headers.has("authorization"))
103 + return yield* new CacheError({ status: 401, reason: "missing_identity" });
104 + const developer = yield* developerIdentity(request, bindings.developerKeys);
105 + if (writing || operation === "telemetry") {
106 + if (developer === null)
107 + return yield* new CacheError({ status: 403, reason: "invalid_identity" });
108 + } else {
109 + const reader = developer ?? (yield* developerIdentity(request, bindings.readerKeys));
110 + if (typeof reader !== "string")
111 + return yield* new CacheError({ status: 403, reason: "read_denied" });
112 + }
113 + return yield* admission
114 + .withPermit(
115 + writing,
116 + Effect.scoped(
117 + Effect.gen(function* () {
118 + const controller = yield* Effect.acquireRelease(
119 + Effect.sync(() => new AbortController()),
120 + (value) => Effect.sync(() => value.abort()),
121 + );
122 + const deadline = yield* Effect.acquireRelease(
123 + Effect.sync(
124 + () =>
125 + new Deadline(
126 + writing ? defaults.deadlineMs : 15000,
127 + AbortSignal.any([request.signal, controller.signal]),
128 + ),
129 + ),
130 + (value) =>
131 + Effect.sync(() => {
132 + controller.abort();
133 + value.dispose();
134 + }),
135 + );
136 + const stats = new Observations();
137 + yield* Effect.addFinalizer(() =>
138 + Effect.annotateCurrentSpan(
139 + Object.fromEntries(
140 + Object.entries(stats.toJSON()).map(([key, value]) => [`cache.${key}`, value]),
141 + ),
142 + ),
143 + );
144 + if (!writing && operation !== "telemetry") yield* checkLimit(operation);
145 + const scope = yield* protocol(() => getScope(bindings.INDEX, "t3code", stats));
146 + if (operation === "telemetry") {
147 + let identity = developer ?? undefined;
148 + if (identity === undefined) {
149 + identity = yield* protocol(() => authorizeTelemetry(request, scope, keys));
150 + }
151 + yield* checkLimit(operation);
152 + const bytes = yield* protocol(() => readBody(request.body, 256 * 1024, deadline));
153 + const summary = yield* decodeSummary(new TextDecoder().decode(bytes)).pipe(
154 + Effect.mapError(() => new CacheError({ status: 400, reason: "invalid_summary" })),
155 + );
156 + if (summary.tasks.length === 0) return new Response(null, { status: 204 });
157 + const now = yield* Clock.currentTimeMillis;
158 + if (summary.recordedAt > now + 60_000 || summary.recordedAt < now - 30 * 86400_000)
159 + return yield* new CacheError({ status: 400, reason: "summary_expired" });
160 + yield* savings
161 + .record(summary, identity)
162 + .pipe(
163 + Effect.mapError(
164 + () => new CacheError({ status: 503, reason: "telemetry_unavailable" }),
165 + ),
166 + );
167 + yield* recordSummary(summary, identity);
168 + yield* savings
169 + .emitTotals(identity, summary.host)
170 + .pipe(
171 + Effect.mapError(
172 + () => new CacheError({ status: 503, reason: "telemetry_unavailable" }),
173 + ),
174 + );
175 + return new Response(null, { status: 204 });
176 + }
177 + if (writing) {
178 + if (developer !== undefined && !scope.writes_enabled) {
179 + return yield* new CacheError({ status: 403, reason: "developer_write_denied" });
180 + }
181 + const now = yield* Clock.currentTimeMillis;
182 + const identity =
183 + developer === undefined
184 + ? yield* protocol(() => authorizeCacheWrite(request, scope, keys))
185 + : {
186 + exp: Math.floor(now / 1000) + 900,
187 + repository_id: scope.repository_id,
188 + workflow_ref: `developer/${developer}`,
189 + };
190 + yield* checkLimit(operation);
191 + return yield* protocol(() =>
192 + store(request, bindings, context, scope, identity, defaults, deadline, stats),
193 + );
194 + }
195 + if (operation === "fetch") {
196 + const result = yield* protocol(() =>
197 + fetchMetadata(request, bindings, "t3code", defaults, deadline, stats),
198 + );
199 + return cborResponse(result);
200 + }
201 + const selected = yield* protocol(() =>
202 + selectBlob(bindings.INDEX, "t3code", route[2]!, stats),
203 + );
204 + if (!selected) return yield* new CacheError({ status: 404, reason: "blob_missing" });
205 + stats.r2_operations++;
206 + const object = yield* protocol(() => bindings.ARTIFACTS.get(selected.blob_object));
207 + if (!object) return yield* new CacheError({ status: 404, reason: "blob_missing" });
208 + if (object.size !== selected.blob_size)
209 + return yield* new CacheError({ status: 503, reason: "blob_size" });
210 + return new Response(object.body, {
211 + headers: {
212 + "Content-Type": "application/octet-stream",
213 + "Content-Length": String(object.size),
214 + "Cache-Control": "no-store",
215 + },
216 + });
217 + }),
218 + ),
219 + )
220 + .pipe(
221 + Effect.timeout(writing ? "2 minutes" : "15 seconds"),
222 + Effect.catchTags({
223 + TimeoutError: () => Effect.fail(new CacheError({ status: 503, reason: "deadline" })),
224 + }),
225 + );
226 + },
227 + Effect.catchTags({
228 + CacheError: (error) =>
229 + Effect.gen(function* () {
230 + if (error.status >= 500)
231 + yield* Effect.logWarning("Cache request failed", { reason: error.reason });
232 + return errorResponse(new HttpError(error.status, error.reason));
233 + }),
234 + }),
235 + );
236 +
237 + return Cache.of({ handle, cleanup: protocol(() => cleanup(bindings)) });
238 +});
239 +
240 +export const layer = Layer.effect(Cache, make);
infra/task-cache/src/Savings.test.ts new
+71
@@ -0,0 +1,71 @@
1 +// @effect-diagnostics nodeBuiltinImport:off - real SQLite verifies the D1 transaction's deduplication.
2 +import * as NodeSqlite from "node:sqlite";
3 +import * as NodeFS from "node:fs";
4 +import * as Effect from "effect/Effect";
5 +import type * as Cloudflare from "@cloudflare/workers-types";
6 +import { expect, it } from "@effect/vitest";
7 +import * as Savings from "./Savings.ts";
8 +import * as Layer from "effect/Layer";
9 +
10 +it.effect(
11 + "increments lifetime totals once per receipt, including retries and different hosts",
12 + () =>
13 + Effect.gen(function* () {
14 + const sqlite = new NodeSqlite.DatabaseSync(":memory:");
15 + try {
16 + sqlite.exec(
17 + NodeFS.readFileSync(
18 + new URL("../migrations/savings/0001_savings.sql", import.meta.url),
19 + "utf8",
20 + ),
21 + );
22 + sqlite.exec(
23 + NodeFS.readFileSync(
24 + new URL("../migrations/savings/0002_unspecified_tier.sql", import.meta.url),
25 + "utf8",
26 + ),
27 + );
28 + const db = {
29 + prepare: (sql: string) => ({ bind: (...args: (string | number)[]) => ({ sql, args }) }),
30 + batch: async (statements: { sql: string; args: (string | number)[] }[]) => {
31 + sqlite.exec("BEGIN");
32 + try {
33 + for (const statement of statements)
34 + sqlite.prepare(statement.sql).run(...statement.args);
35 + sqlite.exec("COMMIT");
36 + } catch (error) {
37 + sqlite.exec("ROLLBACK");
38 + throw error;
39 + }
40 + return [];
41 + },
42 + } as unknown as Cloudflare.D1Database;
43 + const savings = yield* Savings.Savings.pipe(
44 + Effect.provide(Savings.layer.pipe(Layer.provide(Layer.succeed(Savings.Database, db)))),
45 + );
46 + const summary = {
47 + id: "a".repeat(64),
48 + recordedAt: 1,
49 + host: "mbp",
50 + platform: "darwin",
51 + arch: "arm64",
52 + ci: false,
53 + runId: "",
54 + tasks: [{ package: "server", task: "check", source: "remote" as const, savedMs: 900 }],
55 + };
56 + yield* savings.record(summary, "alice");
57 + yield* savings.record(summary, "alice");
58 + yield* savings.record({ ...summary, id: "b".repeat(64), host: "linux" }, "bob");
59 + yield* savings.record({ ...summary, id: "c".repeat(64) }, "alice");
60 + const rows = sqlite
61 + .prepare("SELECT developer, host, remote_ms, remote_hits FROM savings ORDER BY developer")
62 + .all();
63 + expect(rows).toEqual([
64 + { developer: "alice", host: "mbp", remote_ms: 1800, remote_hits: 2 },
65 + { developer: "bob", host: "linux", remote_ms: 900, remote_hits: 1 },
66 + ]);
67 + } finally {
68 + sqlite.close();
69 + }
70 + }),
71 +);
infra/task-cache/src/Savings.ts new
+140
@@ -0,0 +1,140 @@
1 +import * as Effect from "effect/Effect";
2 +import * as Context from "effect/Context";
3 +import * as Layer from "effect/Layer";
4 +import * as Schema from "effect/Schema";
5 +import * as Clock from "effect/Clock";
6 +import type * as Cloudflare from "@cloudflare/workers-types";
7 +import type { Summary } from "./Summary.ts";
8 +
9 +interface Totals {
10 + developer: string;
11 + host: string;
12 + platform: string;
13 + arch: string;
14 + ci: number;
15 + local_ms: number;
16 + unspecified_ms: number;
17 + unspecified_hits: number;
18 + remote_ms: number;
19 + local_hits: number;
20 + remote_hits: number;
21 + executed: number;
22 + restore_failures: number;
23 +}
24 +
25 +export class SavingsError extends Schema.TaggedError<SavingsError>()("SavingsError", {
26 + operation: Schema.Literals(["record", "read", "cleanup"]),
27 + cause: Schema.Defect(),
28 +}) {
29 + override get message(): string {
30 + return `Failed to ${this.operation} task-cache savings.`;
31 + }
32 +}
33 +
34 +export class Database extends Context.Service<Database, Cloudflare.D1Database>()(
35 + "@t3tools/task-cache/Savings/Database",
36 +) {}
37 +
38 +export class Savings extends Context.Service<
39 + Savings,
40 + {
41 + readonly record: (
42 + summary: typeof Summary.Type,
43 + developer: string,
44 + ) => Effect.Effect<void, SavingsError>;
45 + readonly emitTotals: (developer?: string, host?: string) => Effect.Effect<void, SavingsError>;
46 + readonly cleanup: Effect.Effect<void, SavingsError>;
47 + }
48 +>()("@t3tools/task-cache/Savings") {}
49 +
50 +const make = Effect.gen(function* () {
51 + const database = yield* Database;
52 + const record = Effect.fn("TaskCache.persistSavings")(function* (
53 + summary: typeof Summary.Type,
54 + developer: string,
55 + ) {
56 + const local = summary.tasks.filter((task) => task.source === "local");
57 + const unspecified = summary.tasks.filter((task) => task.source === "unspecified");
58 + const remote = summary.tasks.filter((task) => task.source === "remote");
59 + // D1 batch is transactional. changes() gates the increment on the preceding receipt insertion.
60 + yield* Effect.tryPromise({
61 + try: () =>
62 + database.batch([
63 + database
64 + .prepare("INSERT INTO receipts(id, recorded_at) VALUES (?, ?) ON CONFLICT DO NOTHING")
65 + .bind(summary.id, summary.recordedAt),
66 + database
67 + .prepare(`INSERT INTO savings(developer, host, platform, arch, ci, local_ms, remote_ms, local_hits, remote_hits, executed, restore_failures, unspecified_ms, unspecified_hits) SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? WHERE changes() = 1
68 + ON CONFLICT DO UPDATE SET
69 + local_ms = local_ms + excluded.local_ms, remote_ms = remote_ms + excluded.remote_ms,
70 + local_hits = local_hits + excluded.local_hits, remote_hits = remote_hits + excluded.remote_hits,
71 + executed = executed + excluded.executed, restore_failures = restore_failures + excluded.restore_failures,
72 + unspecified_ms = unspecified_ms + excluded.unspecified_ms, unspecified_hits = unspecified_hits + excluded.unspecified_hits`)
73 + .bind(
74 + developer,
75 + summary.host,
76 + summary.platform,
77 + summary.arch,
78 + Number(summary.ci),
79 + local.reduce((sum, task) => sum + task.savedMs, 0),
80 + remote.reduce((sum, task) => sum + task.savedMs, 0),
81 + local.length,
82 + remote.length,
83 + summary.tasks.filter((task) => task.source === "executed").length,
84 + summary.tasks.filter((task) => task.source === "failed").length,
85 + unspecified.reduce((sum, task) => sum + task.savedMs, 0),
86 + unspecified.length,
87 + ),
88 + ]),
89 + catch: (cause) => new SavingsError({ operation: "record", cause }),
90 + });
91 + });
92 +
93 + const emitTotals = Effect.fn("TaskCache.emitTotals")(function* (
94 + developer?: string,
95 + host?: string,
96 + ) {
97 + const rows = yield* Effect.tryPromise({
98 + try: () =>
99 + (developer === undefined
100 + ? database.prepare("SELECT * FROM savings")
101 + : database
102 + .prepare("SELECT * FROM savings WHERE developer = ? AND host = ?")
103 + .bind(developer, host)
104 + ).all<Totals>(),
105 + catch: (cause) => new SavingsError({ operation: "read", cause }),
106 + });
107 + yield* Effect.forEach(rows.results, (row) =>
108 + Effect.annotateCurrentSpan({
109 + "developer.id": row.developer,
110 + "host.name": row.host,
111 + "host.os": row.platform,
112 + "host.arch": row.arch,
113 + ci: Boolean(row.ci),
114 + "cache.local_saved_ms": row.local_ms,
115 + "cache.unspecified_saved_ms": row.unspecified_ms,
116 + "cache.unspecified_hits": row.unspecified_hits,
117 + "cache.remote_saved_ms": row.remote_ms,
118 + "cache.local_hits": row.local_hits,
119 + "cache.remote_hits": row.remote_hits,
120 + "cache.executed": row.executed,
121 + "cache.restore_failures": row.restore_failures,
122 + }).pipe(Effect.withSpan("TaskCache.lifetime")),
123 + );
124 + });
125 +
126 + const cleanup = Effect.gen(function* () {
127 + const now = yield* Clock.currentTimeMillis;
128 + yield* Effect.tryPromise({
129 + try: () =>
130 + database
131 + .prepare("DELETE FROM receipts WHERE recorded_at < ?")
132 + .bind(now - 31 * 86400_000)
133 + .run(),
134 + catch: (cause) => new SavingsError({ operation: "cleanup", cause }),
135 + });
136 + });
137 + return Savings.of({ record, emitTotals, cleanup });
138 +});
139 +
140 +export const layer = Layer.effect(Savings, make);
infra/task-cache/src/Summary.ts new
+64
@@ -0,0 +1,64 @@
1 +import * as Effect from "effect/Effect";
2 +import * as Schema from "effect/Schema";
3 +
4 +const Label = Schema.String.check(Schema.isMaxLength(256));
5 +const Count = Schema.Number.check(
6 + Schema.isInt(),
7 + Schema.isBetween({ minimum: 0, maximum: Number.MAX_SAFE_INTEGER }),
8 +);
9 +export const Summary = Schema.Struct({
10 + id: Schema.String.check(Schema.isPattern(/^[a-f0-9]{64}$/)),
11 + recordedAt: Count,
12 + host: Label,
13 + platform: Label,
14 + arch: Label,
15 + ci: Schema.Boolean,
16 + runId: Label,
17 + tasks: Schema.Array(
18 + Schema.Struct({
19 + package: Label,
20 + task: Label,
21 + source: Schema.Literals(["local", "remote", "unspecified", "executed", "failed"]),
22 + savedMs: Count,
23 + }),
24 + ).check(Schema.isMaxLength(1000)),
25 +});
26 +
27 +export const recordSummary = Effect.fn("TaskCache.summary")(function* (
28 + summary: typeof Summary.Type,
29 + developer: string,
30 +) {
31 + const local = summary.tasks.filter((task) => task.source === "local");
32 + const unspecified = summary.tasks.filter((task) => task.source === "unspecified");
33 + const remote = summary.tasks.filter((task) => task.source === "remote");
34 + yield* Effect.annotateCurrentSpan({
35 + "cache.event_id": summary.id,
36 + "cache.recorded_at": summary.recordedAt,
37 + "developer.id": developer,
38 + "host.name": summary.host,
39 + "host.os": summary.platform,
40 + "host.arch": summary.arch,
41 + ci: summary.ci,
42 + "ci.run_id": summary.runId,
43 + "cache.local_hits": local.length,
44 + "cache.unspecified_hits": unspecified.length,
45 + "cache.unspecified_saved_ms": unspecified.reduce((sum, task) => sum + task.savedMs, 0),
46 + "cache.remote_hits": remote.length,
47 + "cache.executed": summary.tasks.filter((task) => task.source === "executed").length,
48 + "cache.restore_failures": summary.tasks.filter((task) => task.source === "failed").length,
49 + "cache.local_saved_ms": local.reduce((sum, task) => sum + task.savedMs, 0),
50 + "cache.remote_saved_ms": remote.reduce((sum, task) => sum + task.savedMs, 0),
51 + });
52 + yield* Effect.forEach(summary.tasks, (task, index) =>
53 + Effect.annotateCurrentSpan({
54 + "cache.event_id": summary.id,
55 + "cache.task_index": index,
56 + "developer.id": developer,
57 + "host.name": summary.host,
58 + "task.package": task.package,
59 + "task.name": task.task,
60 + "cache.source": task.source,
61 + "cache.saved_ms": task.savedMs,
62 + }).pipe(Effect.withSpan("TaskCache.task")),
63 + );
64 +});
infra/task-cache/src/WriterRegistry.test.ts new
+247
@@ -0,0 +1,247 @@
1 +import * as NodeServices from "@effect/platform-node/NodeServices";
2 +import { expect, it } from "@effect/vitest";
3 +import * as Config from "effect/Config";
4 +import * as ConfigProvider from "effect/ConfigProvider";
5 +import * as Effect from "effect/Effect";
6 +import * as Exit from "effect/Exit";
7 +import * as Layer from "effect/Layer";
8 +import * as FileSystem from "effect/FileSystem";
9 +import * as PlatformError from "effect/PlatformError";
10 +import * as Redacted from "effect/Redacted";
11 +import * as ChildProcess from "effect/process/ChildProcess";
12 +import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
13 +import * as WriterRegistry from "./WriterRegistry.ts";
14 +import { WriterSecrets } from "./deploymentConfig.ts";
15 +
16 +const readKeys = Config.Redacted("DEVELOPER_KEYS").pipe(Effect.provide(WriterSecrets));
17 +
18 +it.effect("loads the private writer registry as deployment configuration", () =>
19 + Effect.gen(function* () {
20 + const fs = yield* FileSystem.FileSystem;
21 + const home = yield* fs.makeTempDirectoryScoped();
22 + yield* fs.makeDirectory(`${home}/.config/t3-task-cache`, { recursive: true });
23 + yield* fs.writeFileString(`${home}/.config/t3-task-cache/writers.json`, '{"maria":"digest"}');
24 + yield* fs.writeFileString(
25 + `${home}/.config/t3-task-cache/readers.json`,
26 + '{"ci-reader":"digest"}',
27 + );
28 + const keys = yield* readKeys.pipe(
29 + Effect.provide(ConfigProvider.layer(ConfigProvider.fromUnknown({ HOME: home }))),
30 + );
31 + expect(JSON.parse(Redacted.value(keys))).toEqual({ maria: "digest" });
32 + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
33 +);
34 +
35 +it.effect("uses explicit deployment credentials without requiring a local registry", () =>
36 + Effect.gen(function* () {
37 + const keys = yield* readKeys.pipe(
38 + Effect.provide(
39 + ConfigProvider.layer(
40 + ConfigProvider.fromUnknown({ DEVELOPER_KEYS: '{"theo":"digest"}', READER_KEYS: "{}" }),
41 + ),
42 + ),
43 + );
44 + expect(JSON.parse(Redacted.value(keys))).toEqual({ theo: "digest" });
45 + }).pipe(Effect.provide(NodeServices.layer)),
46 +);
47 +
48 +it.effect("fails instead of deploying an empty registry when credentials are missing", () =>
49 + Effect.gen(function* () {
50 + const fs = yield* FileSystem.FileSystem;
51 + const home = yield* fs.makeTempDirectoryScoped();
52 + const result = yield* readKeys.pipe(
53 + Effect.provide(ConfigProvider.layer(ConfigProvider.fromUnknown({ HOME: home }))),
54 + Effect.exit,
55 + );
56 + expect(Exit.isFailure(result)).toBe(true);
57 + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
58 +);
59 +
60 +it.live("rejects reserved writer names without modifying credential stores", () =>
61 + Effect.gen(function* () {
62 + const fs = yield* FileSystem.FileSystem;
63 + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
64 + const home = yield* fs.makeTempDirectoryScoped();
65 + const state = `${home}/.config/t3-task-cache`;
66 + yield* fs.makeDirectory(state, { recursive: true });
67 + const writers = '{"maria":"digest"}';
68 + const credentials = '{"https://cache.example":{"name":"maria","token":"secret"}}';
69 + yield* fs.writeFileString(`${state}/writers.json`, writers);
70 + yield* fs.writeFileString(`${state}/credentials.json`, credentials);
71 + for (const name of ["writers", "credentials", "readers", "ci-reader"]) {
72 + const code = yield* spawner.exitCode(
73 + ChildProcess.make(
74 + "node",
75 + [`${import.meta.dirname}/../scripts/manage.ts`, "add-writer", name],
76 + {
77 + env: { HOME: home },
78 + extendEnv: true,
79 + stdout: "ignore",
80 + stderr: "ignore",
81 + },
82 + ),
83 + );
84 + expect(code).not.toBe(0);
85 + expect(yield* fs.readFileString(`${state}/writers.json`)).toBe(writers);
86 + expect(yield* fs.readFileString(`${state}/credentials.json`)).toBe(credentials);
87 + }
88 + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
89 +);
90 +
91 +it.effect("writes credentials for the supplied deployed worker endpoint", () =>
92 + Effect.gen(function* () {
93 + const fs = yield* FileSystem.FileSystem;
94 + const home = yield* fs.makeTempDirectoryScoped();
95 + const endpoint = "https://preview-cache.example/projects/t3code";
96 + const file = yield* WriterRegistry.WriterRegistry.pipe(
97 + Effect.flatMap((registry) => registry.update("maria", false, endpoint, true)),
98 + Effect.provide(
99 + WriterRegistry.layer.pipe(
100 + Layer.provide(ConfigProvider.layer(ConfigProvider.fromUnknown({ HOME: home }))),
101 + ),
102 + ),
103 + );
104 + expect(file).toBe(`${home}/.config/t3-task-cache/maria.json`);
105 + if (!file) return yield* Effect.die("Expected a credential file");
106 + const credential = JSON.parse(yield* fs.readFileString(file));
107 + expect(credential.endpoint).toBe(endpoint);
108 + expect(credential.name).toBe("maria");
109 + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
110 +);
111 +
112 +it.effect("refuses a concurrent registry update without changing committed credentials", () =>
113 + Effect.gen(function* () {
114 + const fs = yield* FileSystem.FileSystem;
115 + const home = yield* fs.makeTempDirectoryScoped();
116 + const state = `${home}/.config/t3-task-cache`;
117 + yield* fs.makeDirectory(`${state}/writers.lock`, { recursive: true });
118 + yield* fs.writeFileString(`${state}/writers.json`, '{"maria":"digest"}');
119 + const result = yield* WriterRegistry.WriterRegistry.pipe(
120 + Effect.flatMap((registry) => registry.update("theo", false, "https://cache.example")),
121 + Effect.provide(
122 + WriterRegistry.layer.pipe(
123 + Layer.provide(ConfigProvider.layer(ConfigProvider.fromUnknown({ HOME: home }))),
124 + ),
125 + ),
126 + Effect.exit,
127 + );
128 + expect(Exit.isFailure(result)).toBe(true);
129 + expect(yield* fs.readFileString(`${state}/writers.json`)).toBe('{"maria":"digest"}');
130 + expect(yield* fs.exists(`${state}/theo.json`)).toBe(false);
131 + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
132 +);
133 +
134 +it.effect("keeps the committed registry intact if a replacement write fails", () =>
135 + Effect.gen(function* () {
136 + const fs = yield* FileSystem.FileSystem;
137 + const home = yield* fs.makeTempDirectoryScoped();
138 + const state = `${home}/.config/t3-task-cache`;
139 + yield* fs.makeDirectory(state, { recursive: true });
140 + const original = '{"maria":"digest"}';
141 + yield* fs.writeFileString(`${state}/writers.json`, original);
142 + const result = yield* WriterRegistry.WriterRegistry.pipe(
143 + Effect.flatMap((registry) => registry.update("theo", false, "https://cache.example")),
144 + Effect.provide(
145 + WriterRegistry.layer.pipe(
146 + Layer.provide([
147 + ConfigProvider.layer(ConfigProvider.fromUnknown({ HOME: home })),
148 + Layer.succeed(FileSystem.FileSystem, {
149 + ...fs,
150 + writeFileString: (path) =>
151 + fs
152 + .writeFileString(path, "partial")
153 + .pipe(Effect.andThen(Effect.die("disk write failed"))),
154 + }),
155 + ]),
156 + ),
157 + ),
158 + Effect.exit,
159 + );
160 + expect(Exit.isFailure(result)).toBe(true);
161 + expect(yield* fs.readFileString(`${state}/writers.json`)).toBe(original);
162 + expect(yield* fs.exists(`${state}/writers.lock`)).toBe(false);
163 + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
164 +);
165 +
166 +it.effect("preserves filesystem errors when acquiring the registry lock", () =>
167 + Effect.gen(function* () {
168 + const fs = yield* FileSystem.FileSystem;
169 + const home = yield* fs.makeTempDirectoryScoped();
170 + const denied = PlatformError.systemError({
171 + _tag: "PermissionDenied",
172 + module: "FileSystem",
173 + method: "makeDirectory",
174 + });
175 + const error = yield* WriterRegistry.WriterRegistry.pipe(
176 + Effect.flatMap((registry) => registry.update("theo", false, "https://cache.example")),
177 + Effect.provide(
178 + WriterRegistry.layer.pipe(
179 + Layer.provide([
180 + ConfigProvider.layer(ConfigProvider.fromUnknown({ HOME: home })),
181 + Layer.succeed(FileSystem.FileSystem, {
182 + ...fs,
183 + makeDirectory: (path, options) =>
184 + path.endsWith("writers.lock")
185 + ? Effect.fail(denied)
186 + : fs.makeDirectory(path, options),
187 + }),
188 + ]),
189 + ),
190 + ),
191 + Effect.flip,
192 + );
193 + expect(error).toBe(denied);
194 + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
195 +);
196 +
197 +it.effect("refuses to initialize a missing writer registry implicitly", () =>
198 + Effect.gen(function* () {
199 + const fs = yield* FileSystem.FileSystem;
200 + const home = yield* fs.makeTempDirectoryScoped();
201 + const result = yield* WriterRegistry.WriterRegistry.pipe(
202 + Effect.flatMap((registry) => registry.update("theo", false, "https://cache.example")),
203 + Effect.provide(
204 + WriterRegistry.layer.pipe(
205 + Layer.provide(ConfigProvider.layer(ConfigProvider.fromUnknown({ HOME: home }))),
206 + ),
207 + ),
208 + Effect.exit,
209 + );
210 + expect(Exit.isFailure(result)).toBe(true);
211 + expect(yield* fs.exists(`${home}/.config/t3-task-cache/writers.json`)).toBe(false);
212 + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
213 +);
214 +
215 +it.effect("keeps existing authorization if credential publication fails", () =>
216 + Effect.gen(function* () {
217 + const fs = yield* FileSystem.FileSystem;
218 + const home = yield* fs.makeTempDirectoryScoped();
219 + const state = `${home}/.config/t3-task-cache`;
220 + yield* fs.makeDirectory(state, { recursive: true });
221 + const original = '{"maria":"old-digest"}';
222 + yield* fs.writeFileString(`${state}/writers.json`, original);
223 + const error = PlatformError.systemError({
224 + _tag: "PermissionDenied",
225 + module: "FileSystem",
226 + method: "rename",
227 + });
228 + const result = yield* WriterRegistry.WriterRegistry.pipe(
229 + Effect.flatMap((registry) => registry.update("maria", false, "https://cache.example")),
230 + Effect.provide(
231 + WriterRegistry.layer.pipe(
232 + Layer.provide([
233 + ConfigProvider.layer(ConfigProvider.fromUnknown({ HOME: home })),
234 + Layer.succeed(FileSystem.FileSystem, {
235 + ...fs,
236 + rename: (from, to) =>
237 + to === `${state}/maria.json` ? Effect.fail(error) : fs.rename(from, to),
238 + }),
239 + ]),
240 + ),
241 + ),
242 + Effect.exit,
243 + );
244 + expect(Exit.isFailure(result)).toBe(true);
245 + expect(yield* fs.readFileString(`${state}/writers.json`)).toBe(original);
246 + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
247 +);
infra/task-cache/src/WriterRegistry.ts new
+128
@@ -0,0 +1,128 @@
1 +import * as Config from "effect/Config";
2 +import * as Context from "effect/Context";
3 +import * as Crypto from "effect/Crypto";
4 +import * as Effect from "effect/Effect";
5 +import * as FileSystem from "effect/FileSystem";
6 +import type * as PlatformError from "effect/PlatformError";
7 +import * as Layer from "effect/Layer";
8 +import * as Schema from "effect/Schema";
9 +
10 +const Writers = Schema.fromJsonString(Schema.Record(Schema.String, Schema.String));
11 +const decodeWriters = Schema.decodeEffect(Writers);
12 +const encodeWriters = Schema.encodeEffect(Writers);
13 +
14 +const stateDirectory = Config.String("HOME").pipe(
15 + Effect.map((home) => `${home}/.config/t3-task-cache`),
16 +);
17 +
18 +const Credential = Schema.fromJsonString(
19 + Schema.Struct({ endpoint: Schema.String, name: Schema.String, token: Schema.String }),
20 +);
21 +class ManagementError extends Schema.TaggedError<ManagementError>()("ManagementError", {
22 + message: Schema.String,
23 +}) {}
24 +export const isWriterName = (name: string) =>
25 + /^(?!writers$|credentials$|readers$|ci-reader$)[a-z0-9-]{1,64}$/.test(name);
26 +
27 +const encodeCredential = Schema.encodeEffect(Credential);
28 +
29 +export class WriterRegistry extends Context.Service<
30 + WriterRegistry,
31 + {
32 + readonly read: () => Effect.Effect<
33 + Record<string, string>,
34 + PlatformError.PlatformError | Schema.SchemaError
35 + >;
36 + readonly update: (
37 + name: string,
38 + revoke: boolean,
39 + endpoint?: string,
40 + initialize?: boolean,
41 + ) => Effect.Effect<
42 + string | undefined,
43 + PlatformError.PlatformError | Schema.SchemaError | ManagementError
44 + >;
45 + }
46 +>()("@t3tools/task-cache/WriterRegistry") {}
47 +
48 +const make = Effect.gen(function* () {
49 + const fs = yield* FileSystem.FileSystem;
50 + const crypto = yield* Crypto.Crypto;
51 + const state = yield* stateDirectory;
52 + const readWriters = Effect.fn("TaskCache.readWriters")(function* () {
53 + return yield* decodeWriters(yield* fs.readFileString(`${state}/writers.json`));
54 + });
55 +
56 + const save = Effect.fn("TaskCache.save")(function* (
57 + entries: ReadonlyArray<readonly [string, string]>,
58 + ) {
59 + yield* fs.makeDirectory(state, { recursive: true, mode: 0o700 });
60 + yield* Effect.scoped(
61 + Effect.gen(function* () {
62 + const directory = yield* fs.makeTempDirectoryScoped({
63 + directory: state,
64 + prefix: ".writer-",
65 + });
66 + for (const [name, value] of entries)
67 + yield* fs.writeFileString(`${directory}/${name}.json`, value + "\n", { mode: 0o600 });
68 + // The registry is the authorization commit point and must publish last.
69 + for (const [name] of entries)
70 + yield* fs.rename(`${directory}/${name}.json`, `${state}/${name}.json`);
71 + }),
72 + );
73 + });
74 +
75 + const updateWriter = Effect.fn("TaskCache.updateWriter")(function* (
76 + name: string,
77 + revoke: boolean,
78 + endpoint?: string,
79 + initialize = false,
80 + ) {
81 + if (!isWriterName(name)) return yield* new ManagementError({ message: "Invalid writer name." });
82 + if (!revoke && !endpoint)
83 + return yield* new ManagementError({ message: "A deployed cache endpoint is required." });
84 + return yield* Effect.scoped(
85 + Effect.gen(function* () {
86 + yield* fs.makeDirectory(state, { recursive: true, mode: 0o700 });
87 + // Exclusive creation coordinates separate management processes as well as fibers.
88 + yield* Effect.acquireRelease(
89 + fs.makeDirectory(`${state}/writers.lock`).pipe(
90 + Effect.mapError((error) =>
91 + error.reason._tag === "AlreadyExists"
92 + ? new ManagementError({
93 + message:
94 + "Writer registry is locked by another update. Retry after it completes; remove writers.lock only if that process has stopped.",
95 + })
96 + : error,
97 + ),
98 + ),
99 + () => fs.remove(`${state}/writers.lock`, { recursive: true }).pipe(Effect.orDie),
100 + );
101 + const exists = yield* fs.exists(`${state}/writers.json`);
102 + if (!exists && !initialize)
103 + return yield* new ManagementError({
104 + message:
105 + "Writer registry is missing. Copy the existing private registry from its administrator; use --new-registry only when creating a new cache.",
106 + });
107 + const writers = { ...(exists || revoke ? yield* readWriters() : {}) };
108 + const token = hex(yield* crypto.randomBytes(32));
109 + if (revoke) delete writers[name];
110 + else {
111 + writers[name] = hex(yield* crypto.digest("SHA-256", new TextEncoder().encode(token)));
112 + }
113 + const entries: Array<readonly [string, string]> = [];
114 + if (!revoke && endpoint)
115 + entries.push([name, yield* encodeCredential({ endpoint, name, token })]);
116 + entries.push(["writers", yield* encodeWriters(writers)]);
117 + yield* save(entries);
118 + return revoke ? undefined : `${state}/${name}.json`;
119 + }),
120 + );
121 + });
122 + return WriterRegistry.of({ read: readWriters, update: updateWriter });
123 +});
124 +
125 +export const layer = Layer.effect(WriterRegistry, make);
126 +
127 +const hex = (bytes: Uint8Array) =>
128 + Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
infra/task-cache/src/deploymentConfig.ts new
+39
@@ -0,0 +1,39 @@
1 +import * as Layer from "effect/Layer";
2 +import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
3 +import * as Schema from "effect/Schema";
4 +import * as Config from "effect/Config";
5 +import * as ConfigProvider from "effect/ConfigProvider";
6 +import * as Effect from "effect/Effect";
7 +import * as FileSystem from "effect/FileSystem";
8 +import * as Option from "effect/Option";
9 +import * as WriterRegistry from "./WriterRegistry.ts";
10 +
11 +const encodeWriters = Schema.encodeEffect(
12 + Schema.fromJsonString(Schema.Record(Schema.String, Schema.String)),
13 +);
14 +
15 +// An absent registry must fail deployment rather than revoke every existing writer.
16 +export const WriterSecrets = ConfigProvider.layerAdd(
17 + Effect.gen(function* () {
18 + const configured = yield* Config.Redacted("DEVELOPER_KEYS").pipe(Config.option);
19 + const readers = yield* Config.Redacted("READER_KEYS").pipe(Config.option);
20 + const fs = yield* FileSystem.FileSystem;
21 + const readerKeys = Option.isSome(readers)
22 + ? {}
23 + : {
24 + READER_KEYS: yield* fs.readFileString(
25 + `${yield* Config.String("HOME")}/.config/t3-task-cache/readers.json`,
26 + ),
27 + };
28 + if (Option.isSome(configured)) return ConfigProvider.fromUnknown(readerKeys);
29 + return ConfigProvider.fromUnknown({
30 + ...readerKeys,
31 + DEVELOPER_KEYS: yield* encodeWriters(
32 + yield* WriterRegistry.WriterRegistry.pipe(
33 + Effect.flatMap((registry) => registry.read()),
34 + Effect.provide(WriterRegistry.layer.pipe(Layer.provide(NodeCrypto.layer))),
35 + ),
36 + ),
37 + });
38 + }),
39 +);
infra/task-cache/src/developerAuth.test.ts new
+37
@@ -0,0 +1,37 @@
1 +import * as Effect from "effect/Effect";
2 +import { expect, it } from "@effect/vitest";
3 +
4 +import { developerIdentity } from "./developerAuth.ts";
5 +
6 +const token = "fixture-token-012345678901234567890123456789";
7 +const keys = JSON.stringify({
8 + julius: "210eb3d2c67a7207923e1999751300a87a2dd22839b258b80c3d805a6fe64984",
9 +});
10 +const request = (credentials: string) =>
11 + new Request("https://cache.example/projects/t3code/store", {
12 + headers: { authorization: `Basic ${btoa(credentials)}` },
13 + });
14 +
15 +it.effect(
16 + "accepts the registered developer and rejects wrong, revoked, or malformed credentials",
17 + () =>
18 + Effect.gen(function* () {
19 + expect(yield* developerIdentity(request(`julius:${token}`), keys)).toBe("julius");
20 + expect(yield* developerIdentity(request(`theo:${token}`), keys)).toBeNull();
21 + expect(yield* developerIdentity(request(`julius:${token}wrong`), keys)).toBeNull();
22 + expect(yield* developerIdentity(request(`julius:${token}`), "{}")).toBeNull();
23 + expect(yield* developerIdentity(request(`julius:${token}`), undefined)).toBeNull();
24 + expect(yield* developerIdentity(request("malformed"), keys)).toBeNull();
25 + expect(yield* developerIdentity(request(`constructor:${token}`), keys)).toBeNull();
26 + }),
27 +);
28 +
29 +it.effect("leaves anonymous reads and GitHub OIDC authorization to the upstream Worker", () =>
30 + Effect.gen(function* () {
31 + expect(yield* developerIdentity(new Request("https://cache.example"), keys)).toBeUndefined();
32 + const oidc = new Request("https://cache.example", {
33 + headers: { authorization: "Bearer github-oidc-token" },
34 + });
35 + expect(yield* developerIdentity(oidc, keys)).toBeUndefined();
36 + }),
37 +);
infra/task-cache/src/developerAuth.ts new
+35
@@ -0,0 +1,35 @@
1 +import * as Effect from "effect/Effect";
2 +import * as Schema from "effect/Schema";
3 +
4 +const Keys = Schema.fromJsonString(Schema.Record(Schema.String, Schema.String));
5 +
6 +const decodeKeys = Schema.decodeEffect(Keys);
7 +
8 +export const developerIdentity = Effect.fn("TaskCache.developerIdentity")(function* (
9 + request: Request,
10 + encodedKeys: string | undefined,
11 +) {
12 + const authorization = request.headers.get("authorization");
13 + if (!authorization?.toLowerCase().startsWith("basic ")) return undefined;
14 + if (!encodedKeys || authorization.length > 1024) return null;
15 +
16 + const decoded = yield* Effect.try(() => atob(authorization.slice(6))).pipe(Effect.option);
17 + if (decoded._tag === "None") return null;
18 + const keys = yield* decodeKeys(encodedKeys).pipe(Effect.option);
19 + if (keys._tag === "None") return null;
20 + const separator = decoded.value.indexOf(":");
21 + const name = decoded.value.slice(0, separator);
22 + const token = decoded.value.slice(separator + 1);
23 + if (separator < 1 || !/^[a-z0-9-]{1,64}$/.test(name) || token.length < 32) return null;
24 + const expected = Object.hasOwn(keys.value, name) ? keys.value[name] : undefined;
25 + if (typeof expected !== "string" || !/^[a-f0-9]{64}$/.test(expected)) return null;
26 + const digest = new Uint8Array(
27 + yield* Effect.promise(() => crypto.subtle.digest("SHA-256", new TextEncoder().encode(token))),
28 + );
29 + const actual = Array.from(digest, (byte) => byte.toString(16).padStart(2, "0")).join("");
30 + let difference = 0;
31 + for (let index = 0; index < actual.length; index++) {
32 + difference |= actual.charCodeAt(index) ^ expected.charCodeAt(index);
33 + }
34 + return difference === 0 ? name : null;
35 +});
infra/task-cache/src/flush.ts new
+3
@@ -0,0 +1,3 @@
1 +import { flushTelemetry } from "./telemetry.ts";
2 +
3 +await flushTelemetry();
infra/task-cache/src/nativeHook.js new
+17
@@ -0,0 +1,17 @@
1 +/** @param {string} source */
2 +export function patchNativeCli(source) {
3 + if (source.includes('import { startRunHook } from "./run-hook.js";')) return source;
4 + const start = "\tconst exitCode = await run({";
5 + const end = "\t\targs: rustCliArgs\n\t});";
6 + if (source.split(start).length !== 2 || source.split(end).length !== 2)
7 + throw new Error("Unsupported Vite+ entrypoint; no files were changed.");
8 + return (
9 + 'import { startRunHook } from "./run-hook.js";\n' +
10 + source
11 + .replace(
12 + start,
13 + '\tconst finishRunHook = await startRunHook(command, process.cwd(), async () => (await (await import("../binding/index.js")).detectWorkspace(process.cwd())).root);\n\tif (finishRunHook?.remoteCache && !rustCliArgs.includes("--last-details") && !rustCliArgs.some((arg) => arg === "--remote-cache" || arg.startsWith("--remote-cache="))) { rustCliArgs.splice(1, 0, "--remote-cache", finishRunHook.remoteCache); process.env.VP_T3_TASK_CACHE_MODE = finishRunHook.remoteCache; }\n\tlet exitCode;\n\ttry {\n\texitCode = await run({',
14 + )
15 + .replace(end, end + "\n\t} finally {\n\t\tawait finishRunHook?.();\n\t}")
16 + );
17 +}
infra/task-cache/src/nativeHook.test.ts new
+86
@@ -0,0 +1,86 @@
1 +import { expect, it } from "vite-plus/test";
2 +import { patchNativeCli } from "./nativeHook.js";
3 +
4 +it("wraps the native runner in a finally hook and patches only once", () => {
5 + const original =
6 + "\tconst exitCode = await run({\n\t\targs: rustCliArgs\n\t});\n\tprocess.exit(exitCode);";
7 + const patched = patchNativeCli(original);
8 + expect(patched).toContain("await startRunHook(command,");
9 + expect(patched).toContain("finally {\n\t\tawait finishRunHook?.();");
10 + expect(patched).toContain("process.exit(exitCode)");
11 + expect(patchNativeCli(patched)).toBe(patched);
12 +});
13 +
14 +it("rejects an unknown native entrypoint before changing it", () => {
15 + expect(() => patchNativeCli("changed upstream entrypoint")).toThrow(
16 + "Unsupported Vite+ entrypoint",
17 + );
18 +});
19 +
20 +it("leaves an unsupported CLI runnable when the machine preload is enabled", async () => {
21 + const fs = await import("node:fs/promises");
22 + const os = await import("node:os");
23 + const path = await import("node:path");
24 + const { promisify } = await import("node:util");
25 + const { execFile } = await import("node:child_process");
26 + const root = await fs.mkdtemp(path.join(os.tmpdir(), "cache-preload-"));
27 + try {
28 + const directory = path.join(root, "vite-plus/dist");
29 + await fs.mkdir(directory, { recursive: true });
30 + await fs.writeFile(path.join(root, "package.json"), '{"type":"module"}');
31 + await fs.writeFile(path.join(directory, "bin.js"), 'console.log("unmodified CLI");');
32 + const result = await promisify(execFile)(
33 + process.execPath,
34 + ["--import", new URL("./preload.mjs", import.meta.url).href, path.join(directory, "bin.js")],
35 + { env: { ...process.env, NODE_OPTIONS: "" } },
36 + );
37 + expect(result.stdout.trim()).toBe("unmodified CLI");
38 + expect(result.stderr).toBe("");
39 + await fs.mkdir(path.join(root, "vite-plus/binding"));
40 + await fs.writeFile(path.join(root, "vite-plus/binding/index.js"), "export {};");
41 + await fs.writeFile(
42 + path.join(directory, "bin.js"),
43 + 'const command="--version"; const rustCliArgs=[]; const run=async()=>0;\n\tconst exitCode = await run({\n\t\targs: rustCliArgs\n\t});\nconsole.log("compatible CLI");',
44 + );
45 + const compatible = await promisify(execFile)(
46 + process.execPath,
47 + ["--import", new URL("./preload.mjs", import.meta.url).href, path.join(directory, "bin.js")],
48 + { env: { ...process.env, NODE_OPTIONS: "" } },
49 + );
50 + expect(compatible.stdout.trim()).toBe("compatible CLI");
51 + } finally {
52 + await fs.rm(root, { recursive: true, force: true });
53 + }
54 +});
55 +
56 +it("does not execute hook modules supplied by ancestor package manifests", async () => {
57 + const fs = await import("node:fs/promises");
58 + const os = await import("node:os");
59 + const path = await import("node:path");
60 + const { promisify } = await import("node:util");
61 + const { execFile } = await import("node:child_process");
62 + const root = await fs.mkdtemp(path.join(os.tmpdir(), "cache-ancestor-"));
63 + try {
64 + await fs.mkdir(path.join(root, "project"));
65 + await fs.writeFile(
66 + path.join(root, "package.json"),
67 + JSON.stringify({ vitePlus: { runHook: "./malicious.mjs" } }),
68 + );
69 + await fs.writeFile(
70 + path.join(root, "malicious.mjs"),
71 + 'throw new Error("ancestor module executed")',
72 + );
73 + const result = await promisify(execFile)(
74 + process.execPath,
75 + [
76 + "--input-type=module",
77 + "-e",
78 + `import { startRunHook } from ${JSON.stringify(new URL("./nativeRunHook.js", import.meta.url).href)}; await startRunHook("run", ${JSON.stringify(path.join(root, "project"))});`,
79 + ],
80 + { env: { ...process.env, HOME: root, NODE_OPTIONS: "" } },
81 + );
82 + expect(result.stderr).toBe("");
83 + } finally {
84 + await fs.rm(root, { recursive: true, force: true });
85 + }
86 +});
infra/task-cache/src/nativeRunHook.js new
+65
@@ -0,0 +1,65 @@
1 +import * as NodeFSP from "node:fs/promises";
2 +import * as NodeOS from "node:os";
3 +import * as NodePath from "node:path";
4 +import * as NodeURL from "node:url";
5 +
6 +async function readJson(file) {
7 + try {
8 + return JSON.parse(await NodeFSP.readFile(file, "utf8"));
9 + } catch (error) {
10 + if (error.code !== "ENOENT") throw error;
11 + }
12 +}
13 +
14 +// Optional repository integration; never changes the task runner's exit status.
15 +export async function startRunHook(command, cwd = process.cwd(), resolveWorkspaceRoot) {
16 + if (command !== "run") return;
17 + try {
18 + const configured = await readJson(
19 + NodePath.join(NodeOS.homedir(), ".config/vite-plus/run-hook.json"),
20 + );
21 + let root = cwd;
22 + while (true) {
23 + let hook;
24 + if (configured?.rootMarker && configured?.module) {
25 + try {
26 + await NodeFSP.access(NodePath.join(root, configured.rootMarker));
27 + hook = configured.module;
28 + } catch (error) {
29 + if (error.code !== "ENOENT") throw error;
30 + }
31 + }
32 + if (typeof hook === "string") {
33 + // Use Vite's own workspace resolver so nested standalone projects stay independent.
34 + if (resolveWorkspaceRoot) {
35 + const workspace = await resolveWorkspaceRoot();
36 + if (!workspace || (await NodeFSP.realpath(workspace)) !== (await NodeFSP.realpath(root)))
37 + break;
38 + }
39 + const integration = await import(NodeURL.pathToFileURL(NodePath.resolve(root, hook)).href);
40 + const finish = await integration.default({ root });
41 + return Object.assign(
42 + async () => {
43 + try {
44 + await finish?.();
45 + } catch {
46 + console.warn("[Vite+] Run hook failed to finish.");
47 + }
48 + },
49 + { remoteCache: finish?.remoteCache },
50 + );
51 + }
52 + const parent = NodePath.dirname(root);
53 + if (parent === root) break;
54 + root = parent;
55 + }
56 + // Vite forwards our CLI mode to tasks, including agents working in other projects.
57 + if (
58 + process.env.VP_T3_TASK_CACHE_MODE &&
59 + process.env.VP_REMOTE_CACHE === process.env.VP_T3_TASK_CACHE_MODE
60 + )
61 + delete process.env.VP_REMOTE_CACHE;
62 + } catch {
63 + console.warn("[Vite+] Run hook failed to start.");
64 + }
65 +}
infra/task-cache/src/observability.ts new
+203
@@ -0,0 +1,203 @@
1 +import * as Axiom from "alchemy/Axiom";
2 +import * as Output from "alchemy/Output";
3 +import * as Effect from "effect/Effect";
4 +import { cacheName } from "./resources.ts";
5 +
6 +// Native summaries may be observed by nested launchers and retried after a lost response.
7 +// Deduplicate before summing; task runtime is a compute proxy, not measured CPU or critical-path time.
8 +export const savingsQuery = (
9 + dataset: string,
10 + group?: "developer" | "host" | "ci",
11 + lifetime = false,
12 +) =>
13 + [
14 + `['${dataset}']`,
15 + `| where name == '${lifetime ? "TaskCache.lifetime" : "TaskCache.summary"}'`,
16 + `| extend a = ['attributes.custom']`,
17 + `| extend event_id = tostring(a['cache.event_id']), developer = tostring(a['developer.id']), host = tostring(['attributes.host.name']), ci = tobool(a['ci']), recorded_at = tolong(a['cache.recorded_at']), unspecified_ms = coalesce(tolong(a['cache.unspecified_saved_ms']), 0), unspecified_hits = coalesce(tolong(a['cache.unspecified_hits']), 0), local_ms = tolong(a['cache.local_saved_ms']), remote_ms = tolong(a['cache.remote_saved_ms']), local_hits = tolong(a['cache.local_hits']), remote_hits = tolong(a['cache.remote_hits']), executed = tolong(a['cache.executed']), restore_failures = tolong(a['cache.restore_failures'])`,
18 + `| summarize arg_max(_time, unspecified_ms, unspecified_hits, local_ms, remote_ms, local_hits, remote_hits, executed, restore_failures${lifetime ? "" : ", developer, host, ci, recorded_at"}) by ${lifetime ? "developer, host, ci, platform = tostring(a['host.os']), arch = tostring(['attributes.host.arch'])" : "event_id"}`,
19 + ...(!lifetime ? ["| extend _time = unixtime_milliseconds_todatetime(recorded_at)"] : []),
20 + `| where local_hits + remote_hits + unspecified_hits + executed + restore_failures > 0`,
21 + `| summarize saved_task_hours = sum(local_ms + remote_ms + unspecified_ms) / 3600000.0, remote_saved_hours = sum(remote_ms) / 3600000.0, local_saved_hours = sum(local_ms) / 3600000.0, unspecified_saved_hours = sum(unspecified_ms) / 3600000.0, unspecified_hits = sum(unspecified_hits), local_hits = sum(local_hits), remote_hits = sum(remote_hits), executed = sum(executed), restore_failures = sum(restore_failures)${group ? ` by ${group}` : ""}`,
22 + ].join("\n");
23 +
24 +const summaryRows = (dataset: string) => savingsQuery(dataset).split("\n").slice(0, -1).join("\n");
25 +
26 +export const Observability = Effect.gen(function* () {
27 + const name = yield* cacheName;
28 + const traces = yield* Axiom.Dataset("CacheTraces", {
29 + name: `${name}-traces`,
30 + kind: "otel:traces:v1",
31 + description: "Shared task cache: service traces and unsampled native task savings reports.",
32 + retentionDays: 30,
33 + useRetentionPeriod: true,
34 + });
35 + const token = yield* Axiom.ApiToken("CacheIngestToken", {
36 + name: `${name}-otel-ingest`,
37 + datasetCapabilities: Output.map(traces.name, (dataset) => ({
38 + [dataset]: { ingest: ["create" as const] },
39 + })),
40 + });
41 + const charts = [
42 + {
43 + id: "total",
44 + name: "⏳ Task hours given back · lifetime",
45 + type: "Statistic",
46 + x: 0,
47 + y: 0,
48 + w: 3,
49 + h: 3,
50 + query: (dataset: string) =>
51 + `${savingsQuery(dataset, undefined, true)}\n| project saved_task_hours`,
52 + },
53 + {
54 + id: "remote",
55 + name: "🌍 Remote hours shared · lifetime",
56 + type: "Statistic",
57 + x: 3,
58 + y: 0,
59 + w: 3,
60 + h: 3,
61 + query: (dataset: string) =>
62 + `${savingsQuery(dataset, undefined, true)}\n| project remote_saved_hours`,
63 + },
64 + {
65 + id: "hit-rate",
66 + name: "🎯 Cache hit rate · lifetime (%)",
67 + type: "Statistic",
68 + x: 6,
69 + y: 0,
70 + w: 3,
71 + h: 3,
72 + query: (dataset: string) =>
73 + `${savingsQuery(dataset, undefined, true)}\n| project hit_rate = round(100.0 * (local_hits + remote_hits + unspecified_hits) / (local_hits + remote_hits + unspecified_hits + executed), 1)`,
74 + },
75 + {
76 + id: "skipped",
77 + name: "⚡ Tasks we skipped · lifetime",
78 + type: "Statistic",
79 + x: 9,
80 + y: 0,
81 + w: 3,
82 + h: 3,
83 + query: (dataset: string) =>
84 + `${savingsQuery(dataset, undefined, true)}\n| project cached_tasks = local_hits + remote_hits + unspecified_hits`,
85 + },
86 + {
87 + id: "savings-trend",
88 + name: "📈 Minutes given back over time",
89 + type: "TimeSeries",
90 + x: 0,
91 + y: 3,
92 + w: 8,
93 + h: 5,
94 + query: (dataset: string) =>
95 + `${summaryRows(dataset)}\n| summarize local_minutes = sum(local_ms) / 60000.0, remote_minutes = sum(remote_ms) / 60000.0, unspecified_minutes = sum(unspecified_ms) / 60000.0 by bin_auto(_time)`,
96 + },
97 + {
98 + id: "developer",
99 + name: "🏆 Who is saving the most?",
100 + type: "Pie",
101 + x: 8,
102 + y: 3,
103 + w: 4,
104 + h: 5,
105 + query: (dataset: string) =>
106 + `${savingsQuery(dataset, "developer", true)}\n| project developer, saved_task_hours\n| order by saved_task_hours desc`,
107 + },
108 + {
109 + id: "tasks-trend",
110 + name: "🔥 Cache hits vs actual execution",
111 + type: "TimeSeries",
112 + x: 0,
113 + y: 8,
114 + w: 8,
115 + h: 5,
116 + query: (dataset: string) =>
117 + `${summaryRows(dataset)}\n| summarize local_hits = sum(local_hits), remote_hits = sum(remote_hits), unspecified_hits = sum(unspecified_hits), executed = sum(executed) by bin_auto(_time)`,
118 + },
119 + {
120 + id: "host",
121 + name: "🖥️ The machine leaderboard",
122 + type: "Pie",
123 + x: 8,
124 + y: 8,
125 + w: 4,
126 + h: 5,
127 + query: (dataset: string) =>
128 + `${savingsQuery(dataset, "host", true)}\n| project host, saved_task_hours\n| order by saved_task_hours desc`,
129 + },
130 + {
131 + id: "leaderboard",
132 + name: "🏅 Developer scorecard · lifetime",
133 + type: "Table",
134 + x: 0,
135 + y: 13,
136 + w: 8,
137 + h: 4,
138 + query: (dataset: string) =>
139 + `${savingsQuery(dataset, "developer", true)}\n| project developer, saved_task_hours, remote_saved_hours, cached_tasks = local_hits + remote_hits + unspecified_hits, executed\n| order by saved_task_hours desc`,
140 + },
141 + {
142 + id: "ci",
143 + name: "🤖 CI vs developer machines · hours saved",
144 + type: "Pie",
145 + x: 8,
146 + y: 13,
147 + w: 4,
148 + h: 4,
149 + query: (dataset: string) =>
150 + `${savingsQuery(dataset, "ci", true)}\n| summarize saved_task_hours = sum(saved_task_hours) by environment = iff(ci, "CI", "Developer machines")`,
151 + },
152 + ] as const;
153 + for (const chart of charts) {
154 + yield* Axiom.View(`CacheSavings-${chart.id}`, {
155 + name: `${name}-${chart.id}-savings`,
156 + description: chart.name,
157 + datasets: [traces.name],
158 + aplQuery: Output.map(traces.name, chart.query),
159 + });
160 + }
161 + const dashboard = yield* Axiom.Dashboard("CacheSavingsDashboard", {
162 + dashboard: {
163 + name: "⚡ Task Cache · Time Back",
164 + owner: "",
165 + schemaVersion: 2,
166 + refreshTime: 60,
167 + description:
168 + "Avoided task execution time, not CPU seconds or elapsed developer time. Local and remote hits; deduplicated native Vite+ summaries. Lifetime counters are retained in D1 and refreshed hourly. Raw traces have 30-day retention.",
169 + timeWindowStart: "qr-now-30d",
170 + timeWindowEnd: "qr-now",
171 + charts: charts.map((chart) => {
172 + const common = {
173 + id: chart.id,
174 + name: chart.name,
175 + query: { apl: Output.map(traces.name, chart.query) },
176 + };
177 + switch (chart.type) {
178 + case "Statistic":
179 + return { ...common, type: "Statistic" as const };
180 + case "TimeSeries":
181 + return {
182 + ...common,
183 + type: "TimeSeries" as const,
184 + query: {
185 + ...common.query,
186 + queryOptions: {
187 + timeSeriesVariant: chart.id === "savings-trend" ? "area" : "bars",
188 + displayNull: "zero",
189 + overlayCharts: "true",
190 + },
191 + },
192 + };
193 + case "Pie":
194 + return { ...common, type: "Pie" as const };
195 + case "Table":
196 + return { ...common, type: "Table" as const };
197 + }
198 + }),
199 + layout: charts.map(({ id, x, y, w, h }) => ({ i: id, x, y, w, h })),
200 + },
201 + });
202 + return { traces, token, dashboard };
203 +});
infra/task-cache/src/preload.mjs new
+42
@@ -0,0 +1,42 @@
1 +import * as NodeModule from "node:module";
2 +
3 +// Vite forwards explicit modes to tasks; clear our inherited default even on older Node.
4 +if (
5 + process.env.VP_T3_TASK_CACHE_MODE &&
6 + process.env.VP_REMOTE_CACHE === process.env.VP_T3_TASK_CACHE_MODE
7 +)
8 + delete process.env.VP_REMOTE_CACHE;
9 +
10 +// Older Node processes in unrelated projects must remain unaffected by machine setup.
11 +if (
12 + typeof NodeModule.registerHooks === "function" &&
13 + Number(process.versions.node.split(".")[0]) >= 24
14 +) {
15 + try {
16 + const { patchNativeCli } = await import("./nativeHook.js");
17 + const hookUrl = new URL("./nativeRunHook.js", import.meta.url).href;
18 + NodeModule.registerHooks({
19 + load(url, context, nextLoad) {
20 + const loaded = nextLoad(url, context);
21 + if (!url.endsWith("/vite-plus/dist/bin.js")) return loaded;
22 + try {
23 + const source =
24 + typeof loaded.source === "string"
25 + ? loaded.source
26 + : Buffer.from(loaded.source).toString();
27 + return {
28 + ...loaded,
29 + source: patchNativeCli(source).replace(
30 + 'from "./run-hook.js"',
31 + `from ${JSON.stringify(hookUrl)}`,
32 + ),
33 + };
34 + } catch {
35 + return loaded;
36 + }
37 + },
38 + });
39 + } catch {
40 + // Restricted Node processes must still run when the optional hook cannot load.
41 + }
42 +}
infra/task-cache/src/protocol.ts new
+21
@@ -0,0 +1,21 @@
1 +import { githubKeys } from "../vendor/vite-plus-remote-cache-cloudflare/src/auth.ts";
2 +
3 +export { cborResponse } from "../vendor/vite-plus-remote-cache-cloudflare/src/cbor.ts";
4 +export {
5 + cleanup,
6 + getScope,
7 + selectBlob,
8 +} from "../vendor/vite-plus-remote-cache-cloudflare/src/database.ts";
9 +export {
10 + errorResponse,
11 + HttpError,
12 +} from "../vendor/vite-plus-remote-cache-cloudflare/src/errors.ts";
13 +export { fetchMetadata } from "../vendor/vite-plus-remote-cache-cloudflare/src/fetch.ts";
14 +export { defaults } from "../vendor/vite-plus-remote-cache-cloudflare/src/limits.ts";
15 +export { store } from "../vendor/vite-plus-remote-cache-cloudflare/src/store.ts";
16 +export { readBody, Deadline } from "../vendor/vite-plus-remote-cache-cloudflare/src/streams.ts";
17 +export { Observations } from "../vendor/vite-plus-remote-cache-cloudflare/src/observations.ts";
18 +
19 +export const keys = githubKeys();
20 +
21 +export { authorizeCacheWrite, authorizeTelemetry } from "./telemetryAuth.ts";
infra/task-cache/src/protocolTypes.ts new
+14
@@ -0,0 +1,14 @@
1 +import type * as Cloudflare from "@cloudflare/workers-types";
2 +
3 +export interface ProtocolBindings {
4 + readonly INDEX: Cloudflare.D1Database;
5 + readonly ARTIFACTS: Omit<Cloudflare.R2Bucket, "get"> & {
6 + get(key: string): Promise<
7 + | (Omit<Cloudflare.R2ObjectBody, "body"> & {
8 + readonly body: ReadableStream<Uint8Array>;
9 + })
10 + | null
11 + >;
12 + };
13 + readonly GC_BATCH_SIZE: string;
14 +}
infra/task-cache/src/remoteCache.ts new
+30
@@ -0,0 +1,30 @@
1 +// @effect-diagnostics nodeBuiltinImport:off - Vite configuration also loads before dependency installation.
2 +import * as NodeFS from "node:fs";
3 +import * as NodeOS from "node:os";
4 +import * as NodePath from "node:path";
5 +
6 +export function remoteCacheUrl(root: string, env = process.env, home = NodeOS.homedir()) {
7 + if (env.VP_REMOTE_CACHE_URL) return env.VP_REMOTE_CACHE_URL;
8 + try {
9 + const { endpoint } = JSON.parse(
10 + NodeFS.readFileSync(NodePath.join(root, ".task-cache.json"), "utf8"),
11 + );
12 + const credentials = JSON.parse(
13 + NodeFS.readFileSync(NodePath.join(home, ".config/t3-task-cache/credentials.json"), "utf8"),
14 + );
15 + const credential = credentials[endpoint];
16 + if (
17 + typeof endpoint !== "string" ||
18 + typeof credential?.name !== "string" ||
19 + typeof credential.token !== "string"
20 + )
21 + return;
22 + const url = new URL(endpoint);
23 + url.username = credential.name;
24 + url.password = credential.token;
25 + return url.href;
26 + } catch {
27 + // Missing or damaged optional setup must not prevent ordinary project commands.
28 + return;
29 + }
30 +}
infra/task-cache/src/resources.ts new
+46
@@ -0,0 +1,46 @@
1 +import * as Alchemy from "alchemy";
2 +import { adopt } from "alchemy/AdoptPolicy";
3 +import * as Cloudflare from "alchemy/Cloudflare";
4 +import { retain } from "alchemy/RemovalPolicy";
5 +import * as Effect from "effect/Effect";
6 +import * as NodeURL from "node:url";
7 +
8 +const protocol = new URL("../vendor/vite-plus-remote-cache-cloudflare/", import.meta.url);
9 +
10 +export const cacheName = Effect.gen(function* () {
11 + const { stage } = yield* Alchemy.Stack;
12 + return stage === "prod" ? "t3-task-cache" : `t3-task-cache-${stage}`;
13 +});
14 +
15 +export const Index = cacheName.pipe(
16 + Effect.flatMap((name) =>
17 + Cloudflare.D1.Database("Index", {
18 + name,
19 + migrations: NodeURL.fileURLToPath(new URL("migrations", protocol)),
20 + }).pipe(adopt(true), retain()),
21 + ),
22 +);
23 +
24 +export const Artifacts = cacheName.pipe(
25 + Effect.flatMap((name) =>
26 + Cloudflare.R2.Bucket("Artifacts", {
27 + name,
28 + lifecycleRules: [
29 + {
30 + id: "remote-cache-generations",
31 + deleteObjectsTransition: { condition: { type: "Age", maxAge: 9 * 86400 } },
32 + abortMultipartUploadsTransition: { condition: { type: "Age", maxAge: 86400 } },
33 + },
34 + ],
35 + }).pipe(adopt(true), retain()),
36 + ),
37 +);
38 +
39 +export const Savings = cacheName.pipe(
40 + Effect.flatMap((name) =>
41 + Cloudflare.D1.Database("Savings", {
42 + name: `${name}-savings`,
43 + migrations: NodeURL.fileURLToPath(new URL("../migrations/savings", import.meta.url)),
44 + }).pipe(retain()),
45 + ),
46 +);
infra/task-cache/src/runHook.test.ts new
+496
@@ -0,0 +1,496 @@
1 +// @effect-diagnostics nodeBuiltinImport:off - exercises the real native runner and local HTTP collector.
2 +import * as NodeChildProcess from "node:child_process";
3 +import * as NodeFSP from "node:fs/promises";
4 +import * as NodeHttp from "node:http";
5 +import * as NodeOS from "node:os";
6 +import * as NodePath from "node:path";
7 +import { expect, it } from "vite-plus/test";
8 +
9 +it("uses one machine hook across fresh worktrees without modifying Vite+", async () => {
10 + const root = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "task-cache-hook-"));
11 + const unrelated = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "unrelated-project-"));
12 + const reports: { authorization: string | undefined; body: string }[] = [];
13 + let firstExit!: () => void;
14 + const firstExited = new Promise<void>((resolve) => {
15 + firstExit = resolve;
16 + });
17 + let completedReports = 0;
18 + let notified: (() => void) | undefined;
19 + let firstDisconnected = false;
20 + const report = (count: number) =>
21 + completedReports >= count
22 + ? Promise.resolve()
23 + : new Promise<void>((resolve) => {
24 + notified = resolve;
25 + });
26 + const server = NodeHttp.createServer(async (request, response) => {
27 + if (!request.url?.endsWith("/telemetry")) {
28 + response.writeHead(404).end();
29 + return;
30 + }
31 + let body = "";
32 + for await (const chunk of request) body += chunk;
33 + reports.push({ authorization: request.headers.authorization, body });
34 + if (reports.length === 1) {
35 + await firstExited;
36 + firstDisconnected = response.destroyed;
37 + }
38 + response.writeHead(204).end();
39 + completedReports++;
40 + notified?.();
41 + notified = undefined;
42 + });
43 + try {
44 + await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
45 + const address = server.address();
46 + if (!address || typeof address === "string") throw new Error("Missing HTTP address");
47 + const runner = NodePath.join(root, "vite-plus");
48 + const distribution = await NodeFSP.realpath(
49 + NodePath.resolve(import.meta.dirname, "../../../node_modules/vite-plus/dist"),
50 + );
51 + await NodeFSP.cp(distribution, NodePath.join(runner, "dist"), { recursive: true });
52 + await NodeFSP.copyFile(
53 + NodePath.join(distribution, "../package.json"),
54 + NodePath.join(runner, "package.json"),
55 + );
56 + await NodeFSP.symlink(
57 + NodePath.resolve(distribution, "../binding"),
58 + NodePath.join(runner, "binding"),
59 + );
60 + await NodeFSP.symlink(
61 + NodePath.resolve(distribution, "../.."),
62 + NodePath.join(runner, "node_modules"),
63 + );
64 + const entrypoint = NodePath.join(runner, "dist/bin.js");
65 + await NodeFSP.writeFile(
66 + NodePath.join(unrelated, "package.json"),
67 + JSON.stringify({ name: "unrelated", scripts: { build: 'node -e "console.log(7)"' } }),
68 + );
69 + const nested = NodePath.join(root, "nested");
70 + await NodeFSP.mkdir(nested);
71 + await NodeFSP.writeFile(
72 + NodePath.join(nested, "package.json"),
73 + JSON.stringify({ name: "nested", scripts: { build: 'node -e "console.log(8)"' } }),
74 + );
75 + await NodeFSP.writeFile(NodePath.join(nested, "pnpm-workspace.yaml"), "packages: []\n");
76 + const endpoint = `http://127.0.0.1:${address.port}/projects/test`;
77 + await NodeFSP.writeFile(NodePath.join(root, ".task-cache.json"), JSON.stringify({ endpoint }));
78 + await NodeFSP.writeFile(
79 + NodePath.join(root, "package.json"),
80 + JSON.stringify({
81 + name: "hook-fixture",
82 + private: true,
83 + scripts: {
84 + build: `node build.cjs ${JSON.stringify(entrypoint)} ${JSON.stringify(unrelated)}`,
85 + fail: 'node -e "process.exit(7)"',
86 + nested: `node ${JSON.stringify(entrypoint)} -C ${JSON.stringify(nested)} run --no-cache build`,
87 + },
88 + }),
89 + );
90 + await NodeFSP.writeFile(
91 + NodePath.join(root, "build.cjs"),
92 + 'require("node:fs").writeFileSync("environment.json", JSON.stringify({url: process.env.VP_REMOTE_CACHE_URL ?? null, mode: process.env.VP_REMOTE_CACHE ?? null})); console.log(42); try {for (const cwd of process.argv.slice(3)) require("node:child_process").execFileSync(process.execPath, [process.argv[2], "run", "--no-cache", "build"], {cwd, encoding:"utf8"})} catch (error) { throw new Error(error.stdout + error.stderr) }',
93 + );
94 + await NodeFSP.writeFile(
95 + NodePath.join(root, "vite.config.ts"),
96 + `import { remoteCacheUrl } from ${JSON.stringify(new URL("./remoteCache.ts", import.meta.url).href)}; export default {run:{cache:{scripts:true,remote:{url:remoteCacheUrl(import.meta.dirname)}}}};`,
97 + );
98 + await NodeFSP.mkdir(NodePath.join(root, ".config/t3-task-cache"), { recursive: true });
99 + await NodeFSP.writeFile(
100 + NodePath.join(root, ".config/t3-task-cache/credentials.json"),
101 + JSON.stringify({ [endpoint]: { name: "maria", token: "secret" } }),
102 + );
103 + const configDirectory = NodePath.join(root, ".config/vite-plus");
104 + await NodeFSP.mkdir(configDirectory, { recursive: true });
105 + await NodeFSP.writeFile(
106 + NodePath.join(configDirectory, "run-hook.json"),
107 + JSON.stringify({
108 + rootMarker: ".task-cache.json",
109 + module: NodePath.join(import.meta.dirname, "runHook.ts"),
110 + }),
111 + );
112 + const originalEntrypoint = await NodeFSP.readFile(entrypoint, "utf8");
113 + const run = (task: string, cwd = root, ci = false, cache = "--cache") =>
114 + new Promise<number | null>((resolve, reject) => {
115 + const child = NodeChildProcess.spawn(
116 + process.execPath,
117 + [entrypoint, "run", ...(task === "--last-details" ? [] : [cache]), task],
118 + {
119 + cwd,
120 + env: {
121 + ...process.env,
122 + HOME: root,
123 + GITHUB_ACTIONS: ci ? "true" : undefined,
124 + T3_TASK_CACHE_TELEMETRY: undefined,
125 + T3_TASK_CACHE_HOST: undefined,
126 + VITE_CACHE_PATH: undefined,
127 + ACTIONS_ID_TOKEN_REQUEST_URL: undefined,
128 + ACTIONS_ID_TOKEN_REQUEST_TOKEN: undefined,
129 + NODE_OPTIONS: `--import=${new URL("./preload.mjs", import.meta.url).href}`,
130 + VP_REMOTE_CACHE: undefined,
131 + VP_REMOTE_CACHE_URL: undefined,
132 + },
133 + stdio: ["ignore", "ignore", "pipe"],
134 + },
135 + );
136 + let stderr = "";
137 + child.stderr.on("data", (chunk) => {
138 + stderr += chunk;
139 + });
140 + child.once("error", reject);
141 + child.once("close", (code) => {
142 + firstExit();
143 + if (code !== 0 && task !== "fail") reject(new Error(stderr));
144 + else resolve(code);
145 + });
146 + });
147 + expect(await run("build")).toBe(0);
148 + await report(1);
149 + expect(await run("--last-details")).toBe(0);
150 + expect(
151 + JSON.parse(await NodeFSP.readFile(NodePath.join(root, "environment.json"), "utf8")),
152 + ).toMatchObject({ url: null });
153 + expect(firstDisconnected).toBe(false);
154 + expect(await run("build")).toBe(0);
155 + await report(2);
156 + expect(await run("fail")).toBe(7);
157 + await report(3);
158 + const secondWorktree = NodePath.join(root, "second-worktree");
159 + await NodeFSP.mkdir(secondWorktree);
160 + for (const file of ["package.json", ".task-cache.json", "vite.config.ts", "build.cjs"])
161 + await NodeFSP.copyFile(NodePath.join(root, file), NodePath.join(secondWorktree, file));
162 + expect(await run("build", secondWorktree)).toBe(0);
163 + await report(4);
164 + expect(await run("build", secondWorktree, true)).toBe(0);
165 + expect(completedReports).toBe(5);
166 + expect(await NodeFSP.readFile(entrypoint, "utf8")).toBe(originalEntrypoint);
167 + expect(await run("nested", root, false, "--no-cache")).toBe(0);
168 + expect(await run("build", nested)).toBe(0);
169 + expect(reports).toHaveLength(5);
170 + expect(
171 + reports.every(
172 + (report) =>
173 + report.authorization === `Basic ${Buffer.from("maria:secret").toString("base64")}`,
174 + ),
175 + ).toBe(true);
176 + const events = reports.map((report) => JSON.parse(report.body));
177 + expect(events[0].tasks[0].source).toBe("executed");
178 + expect(events[1].tasks[0].source).toBe("local");
179 + expect(events[3].tasks[0].source).toBe("executed");
180 + expect(new Set(events.map((event) => event.id)).size).toBe(5);
181 + } finally {
182 + await new Promise<void>((resolve, reject) =>
183 + server.close((error) => (error ? reject(error) : resolve())),
184 + );
185 + await NodeFSP.rm(root, { recursive: true, force: true });
186 + await NodeFSP.rm(unrelated, { recursive: true, force: true });
187 + }
188 +});
189 +
190 +it("rejects invalid setup credentials before installing anything and never prints the token", async () => {
191 + const home = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "task-cache-setup-"));
192 + const token = "private-credential-that-must-not-appear-in-output";
193 + const server = NodeHttp.createServer((_request, response) => response.writeHead(401).end());
194 + try {
195 + await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
196 + const address = server.address();
197 + if (!address || typeof address === "string") throw new Error("Missing HTTP address");
198 + const result = await new Promise<{ code: number | null; output: string }>((resolve, reject) => {
199 + const child = NodeChildProcess.spawn(
200 + process.execPath,
201 + [NodePath.join(import.meta.dirname, "../scripts/install.ts"), "--non-interactive"],
202 + {
203 + env: {
204 + ...process.env,
205 + HOME: home,
206 + T3_CACHE_SETUP_URL: `http://127.0.0.1:${address.port}/projects/test`,
207 + T3_CACHE_SETUP_NAME: "maria",
208 + T3_CACHE_SETUP_TOKEN: token,
209 + },
210 + stdio: ["ignore", "pipe", "pipe"],
211 + },
212 + );
213 + let output = "";
214 + child.stdout.on("data", (chunk) => {
215 + output += chunk;
216 + });
217 + child.stderr.on("data", (chunk) => {
218 + output += chunk;
219 + });
220 + child.once("error", reject);
221 + child.once("close", (code) => resolve({ code, output }));
222 + });
223 + expect(result.code).not.toBe(0);
224 + expect(result.output).toContain("Credential verification failed (401)");
225 + expect(result.output).not.toContain(token);
226 + expect(await NodeFSP.readdir(home)).toEqual([]);
227 + } finally {
228 + await new Promise<void>((resolve, reject) =>
229 + server.close((error) => (error ? reject(error) : resolve())),
230 + );
231 + await NodeFSP.rm(home, { recursive: true, force: true });
232 + }
233 +});
234 +
235 +it("does not start telemetry or create local state without cache credentials", async () => {
236 + const root = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "task-cache-unconfigured-"));
237 + try {
238 + await NodeFSP.writeFile(
239 + NodePath.join(root, ".task-cache.json"),
240 + JSON.stringify({ endpoint: "https://cache.invalid/projects/test" }),
241 + );
242 + const hook = new URL("./runHook.ts", import.meta.url).href;
243 + const output = await new Promise<string>((resolve, reject) => {
244 + const child = NodeChildProcess.spawn(
245 + process.execPath,
246 + [
247 + "--input-type=module",
248 + "-e",
249 + `import start from ${JSON.stringify(hook)}; const finish = start({ root: process.cwd() }); if (finish !== undefined) process.exit(1);`,
250 + ],
251 + {
252 + cwd: root,
253 + env: { ...process.env, HOME: root, VP_REMOTE_CACHE_URL: "" },
254 + stdio: ["ignore", "pipe", "pipe"],
255 + },
256 + );
257 + let output = "";
258 + child.stdout.on("data", (chunk) => {
259 + output += chunk;
260 + });
261 + child.stderr.on("data", (chunk) => {
262 + output += chunk;
263 + });
264 + child.once("error", reject);
265 + child.once("close", (code) =>
266 + code === 0 ? resolve(output) : reject(new Error(output || `Exited ${code}`)),
267 + );
268 + });
269 + expect(output).toBe("");
270 + expect(await NodeFSP.readdir(root)).toEqual([".task-cache.json"]);
271 + } finally {
272 + await NodeFSP.rm(root, { recursive: true, force: true });
273 + }
274 +});
275 +
276 +it("installs without masking .profile and configures CI through bash", async () => {
277 + const home = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "task-cache-login-"));
278 + const server = NodeHttp.createServer((_request, response) => response.writeHead(204).end());
279 + const run = (args: string[], env: NodeJS.ProcessEnv) =>
280 + new Promise<string>((resolve, reject) => {
281 + const child = NodeChildProcess.spawn(args[0]!, args.slice(1), {
282 + env: { ...process.env, ...env, HOME: home, NODE_OPTIONS: "" },
283 + stdio: ["ignore", "pipe", "pipe"],
284 + });
285 + let output = "";
286 + child.stdout.on("data", (chunk) => {
287 + output += chunk;
288 + });
289 + child.stderr.on("data", (chunk) => {
290 + output += chunk;
291 + });
292 + child.once("error", reject);
293 + child.once("close", (code) => (code === 0 ? resolve(output) : reject(new Error(output))));
294 + });
295 + try {
296 + await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
297 + const address = server.address();
298 + if (!address || typeof address === "string") throw new Error("Missing HTTP address");
299 + await NodeFSP.writeFile(NodePath.join(home, ".profile"), "export EXISTING_PROFILE=preserved\n");
300 + await NodeFSP.mkdir(NodePath.join(home, ".config/t3-task-cache"), { recursive: true });
301 + await NodeFSP.writeFile(
302 + NodePath.join(home, ".config/t3-task-cache/credentials.json"),
303 + "{partial",
304 + );
305 + await run(
306 + [
307 + process.execPath,
308 + NodePath.join(import.meta.dirname, "../scripts/install.ts"),
309 + "--non-interactive",
310 + ],
311 + {
312 + T3_CACHE_SETUP_URL: `http://127.0.0.1:${address.port}/projects/test`,
313 + T3_CACHE_SETUP_NAME: "maria",
314 + T3_CACHE_SETUP_TOKEN: "test-credential-for-isolated-http-server",
315 + },
316 + );
317 + expect(await NodeFSP.readdir(home)).not.toContain(".bash_profile");
318 + expect(await NodeFSP.readFile(NodePath.join(home, ".profile"), "utf8")).toContain(
319 + "EXISTING_PROFILE=preserved",
320 + );
321 + const githubEnv = NodePath.join(home, "github-env");
322 + await run([process.execPath, NodePath.join(import.meta.dirname, "../scripts/install-ci.ts")], {
323 + T3_TASK_CACHE_READ_TOKEN: "read-only-fixture-token-01234567890123456789",
324 + GITHUB_REPOSITORY: "pingdotgg/t3code",
325 + GITHUB_ACTIONS: "true",
326 + GITHUB_ENV: githubEnv,
327 + RUNNER_TEMP: home,
328 + });
329 + const values = await NodeFSP.readFile(githubEnv, "utf8");
330 + expect(values).not.toContain("\nNODE_OPTIONS=");
331 + const bashEnv = values
332 + .split("\n")
333 + .find((line) => line.startsWith("BASH_ENV="))
334 + ?.slice(9);
335 + const output = await run(["bash", "-c", 'printf "%s" "$NODE_OPTIONS"'], { BASH_ENV: bashEnv });
336 + expect(output).toContain("--import=file:");
337 + expect(output).toContain("/infra/task-cache/src/preload.mjs");
338 + await NodeFSP.rm(NodePath.join(home, ".local/share/t3-task-cache/preload.mjs"));
339 + const missing = await run(["bash", "-c", `node -e 'console.log("missing preload is safe")'`], {
340 + BASH_ENV: NodePath.join(home, ".config/t3-task-cache/env"),
341 + });
342 + expect(missing).toContain("missing preload is safe");
343 +
344 + await NodeFSP.writeFile(
345 + NodePath.join(home, ".bash_profile"),
346 + '. "$HOME/.config/t3-task-cache/env"\n',
347 + );
348 + await run(
349 + [
350 + process.execPath,
351 + NodePath.join(import.meta.dirname, "../scripts/install.ts"),
352 + "--uninstall",
353 + ],
354 + {},
355 + );
356 + expect(await NodeFSP.readdir(home)).not.toContain(".bash_profile");
357 + expect(await NodeFSP.readdir(NodePath.join(home, ".local/share/t3-task-cache"))).toEqual([
358 + "preload.mjs",
359 + ]);
360 + expect(await NodeFSP.readFile(NodePath.join(home, ".profile"), "utf8")).not.toContain(
361 + "t3-task-cache/env",
362 + );
363 + expect(await NodeFSP.readdir(NodePath.join(home, ".config/t3-task-cache"))).not.toContain(
364 + "credentials.json",
365 + );
366 + const remaining = await run(
367 + [
368 + process.execPath,
369 + "--import",
370 + NodePath.join(home, ".local/share/t3-task-cache/preload.mjs"),
371 + "-e",
372 + 'console.log("node survives uninstall")',
373 + ],
374 + {},
375 + );
376 + expect(remaining).toContain("node survives uninstall");
377 + } finally {
378 + await new Promise<void>((resolve, reject) =>
379 + server.close((error) => (error ? reject(error) : resolve())),
380 + );
381 + await NodeFSP.rm(home, { recursive: true, force: true });
382 + }
383 +});
384 +
385 +it("does not send an old endpoint's token after changing the URL interactively", async () => {
386 + const root = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "task-cache-endpoint-"));
387 + const requests: string[] = [];
388 + const server = NodeHttp.createServer((request, response) => {
389 + requests.push(request.headers.authorization || "");
390 + response.writeHead(401).end();
391 + });
392 + try {
393 + await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
394 + const address = server.address();
395 + if (!address || typeof address === "string") throw new Error("Missing HTTP address");
396 + const nested = NodePath.join(root, "nested");
397 + await NodeFSP.mkdir(nested);
398 + await NodeFSP.writeFile(
399 + NodePath.join(nested, "package.json"),
400 + JSON.stringify({ name: "nested", scripts: { build: 'node -e "console.log(8)"' } }),
401 + );
402 + await NodeFSP.writeFile(NodePath.join(nested, "pnpm-workspace.yaml"), "packages: []\n");
403 + const endpoint = `http://127.0.0.1:${address.port}/projects/test`;
404 + const scripts = NodePath.join(root, "infra/task-cache/scripts");
405 + await NodeFSP.mkdir(scripts, { recursive: true });
406 + await NodeFSP.mkdir(NodePath.join(root, "infra/task-cache/src"));
407 + await NodeFSP.writeFile(NodePath.join(root, "package.json"), '{"type":"module"}');
408 + await NodeFSP.writeFile(NodePath.join(root, ".task-cache.json"), JSON.stringify({ endpoint }));
409 + await NodeFSP.copyFile(
410 + NodePath.join(import.meta.dirname, "../scripts/install.ts"),
411 + NodePath.join(scripts, "install.ts"),
412 + );
413 + await NodeFSP.copyFile(
414 + NodePath.join(import.meta.dirname, "nativeHook.js"),
415 + NodePath.join(root, "infra/task-cache/src/nativeHook.js"),
416 + );
417 + await NodeFSP.mkdir(NodePath.join(root, ".config/t3-task-cache"), { recursive: true });
418 + await NodeFSP.writeFile(
419 + NodePath.join(root, ".config/t3-task-cache/credentials.json"),
420 + JSON.stringify({
421 + [endpoint]: { name: "maria", token: "different-saved-endpoint-credential" },
422 + }),
423 + );
424 + const result = await new Promise<{ code: number | null; output: string }>((resolve, reject) => {
425 + const child = NodeChildProcess.spawn(
426 + process.execPath,
427 + [NodePath.join(scripts, "install.ts")],
428 + {
429 + env: {
430 + ...process.env,
431 + HOME: root,
432 + NODE_OPTIONS: "",
433 + T3_CACHE_SETUP_URL: "https://old.example",
434 + T3_CACHE_SETUP_NAME: "maria",
435 + T3_CACHE_SETUP_TOKEN: "old-endpoint-token-must-not-be-forwarded",
436 + },
437 + stdio: ["pipe", "pipe", "pipe"],
438 + },
439 + );
440 + let output = "";
441 + let sentUrl = false;
442 + let sentName = false;
443 + child.stdout.on("data", (chunk) => {
444 + output += chunk;
445 + if (!sentUrl && output.includes("Cache URL")) {
446 + sentUrl = true;
447 + child.stdin.write(endpoint + "\n");
448 + }
449 + if (!sentName && output.includes("Developer name")) {
450 + sentName = true;
451 + child.stdin.write("\n");
452 + }
453 + });
454 + child.stderr.on("data", (chunk) => {
455 + output += chunk;
456 + });
457 + child.once("error", reject);
458 + child.once("close", (code) => resolve({ code, output }));
459 + });
460 + expect(result.code).not.toBe(0);
461 + expect(result.output).toContain("Use T3_CACHE_SETUP_TOKEN");
462 + expect(requests).toEqual([]);
463 + } finally {
464 + await new Promise<void>((resolve, reject) =>
465 + server.close((error) => (error ? reject(error) : resolve())),
466 + );
467 + await NodeFSP.rm(root, { recursive: true, force: true });
468 + }
469 +});
470 +
471 +it("rejects non-loopback HTTP before transmitting a setup credential", async () => {
472 + const home = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "task-cache-http-"));
473 + try {
474 + const output = await new Promise<string>((resolve) => {
475 + NodeChildProcess.execFile(
476 + process.execPath,
477 + [NodePath.join(import.meta.dirname, "../scripts/install.ts"), "--non-interactive"],
478 + {
479 + env: {
480 + ...process.env,
481 + HOME: home,
482 + NODE_OPTIONS: "",
483 + T3_CACHE_SETUP_URL: "http://cache.invalid/projects/test",
484 + T3_CACHE_SETUP_NAME: "maria",
485 + T3_CACHE_SETUP_TOKEN: "test-credential-that-must-not-be-transmitted",
486 + },
487 + },
488 + (_error, stdout, stderr) => resolve(stdout + stderr),
489 + );
490 + });
491 + expect(output).toContain("Use an HTTPS cache endpoint");
492 + expect(await NodeFSP.readdir(home)).toEqual([]);
493 + } finally {
494 + await NodeFSP.rm(home, { recursive: true, force: true });
495 + }
496 +});
infra/task-cache/src/runHook.ts new
+41
@@ -0,0 +1,41 @@
1 +// @effect-diagnostics nodeBuiltinImport:off - runs inside Vite+ before project services start.
2 +import * as NodeChildProcess from "node:child_process";
3 +import * as NodeURL from "node:url";
4 +import { remoteCacheUrl } from "./remoteCache.ts";
5 +import { flushTelemetry, observeSummaries } from "./telemetry.ts";
6 +
7 +export default function start({ root }: { readonly root: string }) {
8 + const url = remoteCacheUrl(root);
9 + if (!url) return;
10 + const endpoint = new URL(url);
11 + endpoint.username = "";
12 + endpoint.password = "";
13 + const stop =
14 + process.env.T3_TASK_CACHE_TELEMETRY === "off"
15 + ? undefined
16 + : observeSummaries(root, endpoint.href.replace(/\/$/, ""), process.env);
17 + const finish = async () => {
18 + if (!stop) return;
19 + stop();
20 + // GitHub can terminate detached processes as soon as the final step exits.
21 + if (process.env.GITHUB_ACTIONS === "true") {
22 + await flushTelemetry();
23 + return;
24 + }
25 + // A transient flusher drains the durable queue without delaying tasks or Ctrl-C.
26 + const child = NodeChildProcess.spawn(
27 + process.execPath,
28 + [NodeURL.fileURLToPath(new URL("./flush.ts", import.meta.url))],
29 + {
30 + detached: true,
31 + stdio: "ignore",
32 + env: { ...process.env, NODE_OPTIONS: "" },
33 + },
34 + );
35 + child.on("error", () => {});
36 + child.unref();
37 + };
38 + return Object.assign(finish, {
39 + remoteCache: process.env.VP_REMOTE_CACHE || process.env.VP_T3_TASK_CACHE_MODE || "read-write",
40 + });
41 +}
infra/task-cache/src/telemetry.test.ts new
+162
@@ -0,0 +1,162 @@
1 +// @effect-diagnostics nodeBuiltinImport:off - launcher integration with filesystem and fetch.
2 +import * as NodeFS from "node:fs";
3 +import * as NodeOS from "node:os";
4 +import * as NodePath from "node:path";
5 +import { expect, it, vi } from "vite-plus/test";
6 +import { flushTelemetry, observeSummaries, summarize } from "./telemetry.ts";
7 +
8 +const task = (result: unknown) => ({
9 + package_name: "@team/server",
10 + task_name: "typecheck",
11 + command: "SECRET",
12 + cwd: "/private",
13 + result,
14 +});
15 +
16 +it("counts only confirmed hits, preserves their tier, and never transmits command text", () => {
17 + expect(
18 + summarize({
19 + tasks: [
20 + task({ CacheHit: { source: "Remote", saved_duration_ms: 1200 } }),
21 + task({ CacheHit: { source: "Local", saved_duration_ms: 500 } }),
22 + task({ RestoreFailed: {} }),
23 + task({ Spawned: {} }),
24 + task({ CacheHit: { saved_duration_ms: 999 } }),
25 + task({ CacheHit: { source: "Remote", saved_duration_ms: -1 } }),
26 + ],
27 + }),
28 + ).toEqual([
29 + { package: "@team/server", task: "typecheck", source: "remote", savedMs: 1200 },
30 + { package: "@team/server", task: "typecheck", source: "local", savedMs: 500 },
31 + { package: "@team/server", task: "typecheck", source: "failed", savedMs: 0 },
32 + { package: "@team/server", task: "typecheck", source: "executed", savedMs: 0 },
33 + { package: "@team/server", task: "typecheck", source: "unspecified", savedMs: 999 },
34 + ]);
35 +});
36 +
37 +it("deduplicates shared summaries and retries delivery without credentials in queued records", async () => {
38 + const root = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "cache-observer-"));
39 + const home = NodePath.join(root, "home");
40 + const endpoint = "https://cache.example/projects/t3code";
41 + try {
42 + const stopA = observeSummaries(root, endpoint, { T3_TASK_CACHE_HOST: "test-host" }, home);
43 + const stopB = observeSummaries(root, endpoint, { T3_TASK_CACHE_HOST: "test-host" }, home);
44 + const version = NodePath.join(root, "node_modules/.vite/task-cache/v18");
45 + NodeFS.mkdirSync(version);
46 + NodeFS.writeFileSync(
47 + NodePath.join(version, "last-summary.json"),
48 + JSON.stringify({
49 + tasks: [task({ CacheHit: { source: "Remote", saved_duration_ms: 1200 } })],
50 + }),
51 + );
52 + stopA();
53 + stopB();
54 + const queue = NodePath.join(home, ".cache/t3-task-cache/telemetry");
55 + const files = NodeFS.readdirSync(queue);
56 + expect(files).toHaveLength(1);
57 + const serialized = NodeFS.readFileSync(NodePath.join(queue, files[0]!), "utf8");
58 + expect(serialized).not.toContain("SECRET");
59 + expect(JSON.parse(serialized).event.host).toBe("test-host");
60 + NodeFS.mkdirSync(NodePath.join(home, ".config/t3-task-cache"), { recursive: true });
61 + NodeFS.writeFileSync(
62 + NodePath.join(home, ".config/t3-task-cache/credentials.json"),
63 + JSON.stringify({ [endpoint]: { name: "alice", token: "secret-token" } }),
64 + );
65 + using request = vi
66 + .spyOn(globalThis, "fetch")
67 + .mockResolvedValueOnce(new Response(null, { status: 503 }))
68 + .mockImplementationOnce(async () => {
69 + const late = JSON.parse(serialized);
70 + late.event.id = "b".repeat(64);
71 + NodeFS.writeFileSync(NodePath.join(queue, "late.json"), JSON.stringify(late));
72 + return new Response(null, { status: 204 });
73 + })
74 + .mockResolvedValue(new Response(null, { status: 204 }));
75 + await flushTelemetry(home, {});
76 + expect(NodeFS.readdirSync(queue)).toEqual(files);
77 + await flushTelemetry(home, {});
78 + expect(NodeFS.readdirSync(queue)).toEqual([]);
79 + expect(request).toHaveBeenCalledTimes(3);
80 + expect(request.mock.calls[0]![0].toString()).toBe(`${endpoint}/telemetry`);
81 + } finally {
82 + NodeFS.rmSync(root, { recursive: true, force: true });
83 + }
84 +});
85 +
86 +it("skips unauthenticated endpoints without consuming the delivery budget", async () => {
87 + const home = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "cache-queue-"));
88 + const endpoint = "https://cache.example/projects/t3code";
89 + try {
90 + const queue = NodePath.join(home, ".cache/t3-task-cache/telemetry");
91 + NodeFS.mkdirSync(queue, { recursive: true });
92 + const skipped = Array.from({ length: 101 }, (_, i) => `a${i}.json`);
93 + for (const file of skipped)
94 + NodeFS.writeFileSync(
95 + NodePath.join(queue, file),
96 + JSON.stringify({ endpoint: "https://old.example/projects/t3code", event: { id: file } }),
97 + );
98 + NodeFS.writeFileSync(
99 + NodePath.join(queue, "b.json"),
100 + JSON.stringify({ endpoint, event: { id: "current" } }),
101 + );
102 + NodeFS.mkdirSync(NodePath.join(home, ".config/t3-task-cache"), { recursive: true });
103 + NodeFS.writeFileSync(
104 + NodePath.join(home, ".config/t3-task-cache/credentials.json"),
105 + JSON.stringify({ [endpoint]: { name: "alice", token: "secret-token" } }),
106 + );
107 + using request = vi
108 + .spyOn(globalThis, "fetch")
109 + .mockResolvedValue(new Response(null, { status: 204 }));
110 + await flushTelemetry(home, {});
111 + expect(NodeFS.readdirSync(queue).sort()).toEqual(skipped.sort());
112 + expect(request).toHaveBeenCalledOnce();
113 + expect(request.mock.calls[0]![0].toString()).toBe(`${endpoint}/telemetry`);
114 + } finally {
115 + NodeFS.rmSync(home, { recursive: true, force: true });
116 + }
117 +});
118 +
119 +it("excludes tasks that explicitly disable caching from the hit-rate denominator", () => {
120 + expect(
121 + summarize({
122 + tasks: [
123 + task({ Spawned: { cache_status: "Disabled" } }),
124 + task({ Spawned: { cache_status: { Miss: "changed" } } }),
125 + ],
126 + }),
127 + ).toEqual([{ package: "@team/server", task: "typecheck", source: "executed", savedMs: 0 }]);
128 +});
129 +
130 +it("drops revoked credentials and continues delivering other queued endpoints", async () => {
131 + const home = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "cache-revoked-"));
132 + try {
133 + const queue = NodePath.join(home, ".cache/t3-task-cache/telemetry");
134 + const state = NodePath.join(home, ".config/t3-task-cache");
135 + NodeFS.mkdirSync(queue, { recursive: true });
136 + NodeFS.mkdirSync(state, { recursive: true });
137 + NodeFS.writeFileSync(
138 + NodePath.join(state, "credentials.json"),
139 + JSON.stringify({
140 + "https://revoked.example": { name: "a", token: "old" },
141 + "https://current.example": { name: "a", token: "new" },
142 + }),
143 + );
144 + for (const [file, endpoint] of [
145 + ["a.json", "https://revoked.example"],
146 + ["b.json", "https://current.example"],
147 + ])
148 + NodeFS.writeFileSync(
149 + NodePath.join(queue, file!),
150 + JSON.stringify({ endpoint, event: { id: file } }),
151 + );
152 + using request = vi
153 + .spyOn(globalThis, "fetch")
154 + .mockResolvedValueOnce(new Response(null, { status: 403 }))
155 + .mockResolvedValueOnce(new Response(null, { status: 204 }));
156 + await flushTelemetry(home, {});
157 + expect(request).toHaveBeenCalledTimes(2);
158 + expect(NodeFS.readdirSync(queue)).toEqual([]);
159 + } finally {
160 + NodeFS.rmSync(home, { recursive: true, force: true });
161 + }
162 +});
infra/task-cache/src/telemetry.ts new
+256
@@ -0,0 +1,256 @@
1 +/* oxlint-disable t3code/no-global-process-runtime -- This bootstrap runs before dependencies are installed. */
2 +// @effect-diagnostics globalDate:off globalFetch:off - bootstrap has no Effect dependencies.
3 +// @effect-diagnostics nodeBuiltinImport:off - standalone collector installed outside the checkout.
4 +import * as NodeCrypto from "node:crypto";
5 +import * as NodeFS from "node:fs";
6 +import * as NodeOS from "node:os";
7 +import * as NodePath from "node:path";
8 +
9 +export interface SummaryEvent {
10 + id: string;
11 + recordedAt: number;
12 + host: string;
13 + platform: string;
14 + arch: string;
15 + ci: boolean;
16 + runId: string;
17 + tasks: {
18 + package: string;
19 + task: string;
20 + source: "local" | "remote" | "unspecified" | "executed" | "failed";
21 + savedMs: number;
22 + }[];
23 +}
24 +
25 +export function summarize(value: unknown): SummaryEvent["tasks"] {
26 + if (!value || typeof value !== "object" || !("tasks" in value) || !Array.isArray(value.tasks))
27 + return [];
28 + return value.tasks.flatMap((task: unknown) => {
29 + if (
30 + !task ||
31 + typeof task !== "object" ||
32 + !("package_name" in task) ||
33 + !("task_name" in task) ||
34 + !("result" in task)
35 + )
36 + return [];
37 + if (
38 + typeof task.package_name !== "string" ||
39 + typeof task.task_name !== "string" ||
40 + !task.result ||
41 + typeof task.result !== "object"
42 + )
43 + return [];
44 + let source: SummaryEvent["tasks"][number]["source"] = "executed";
45 + let savedMs = 0;
46 + if ("CacheHit" in task.result) {
47 + const hit = task.result.CacheHit;
48 + if (
49 + !hit ||
50 + typeof hit !== "object" ||
51 + !("saved_duration_ms" in hit) ||
52 + typeof hit.saved_duration_ms !== "number" ||
53 + !Number.isSafeInteger(hit.saved_duration_ms) ||
54 + hit.saved_duration_ms < 0
55 + )
56 + return [];
57 + if ("source" in hit && hit.source !== "Local" && hit.source !== "Remote") return [];
58 + source = !("source" in hit) ? "unspecified" : hit.source === "Remote" ? "remote" : "local";
59 + savedMs = hit.saved_duration_ms;
60 + } else if ("RestoreFailed" in task.result) source = "failed";
61 + else if (!("Spawned" in task.result)) return [];
62 + else if (
63 + task.result.Spawned &&
64 + typeof task.result.Spawned === "object" &&
65 + "cache_status" in task.result.Spawned &&
66 + task.result.Spawned.cache_status === "Disabled"
67 + )
68 + return [];
69 + return [
70 + {
71 + package: task.package_name.slice(0, 256),
72 + task: task.task_name.slice(0, 256),
73 + source,
74 + savedMs,
75 + },
76 + ];
77 + });
78 +}
79 +
80 +export function observeSummaries(
81 + root: string,
82 + endpoint: string,
83 + env: NodeJS.ProcessEnv,
84 + home = NodeOS.homedir(),
85 +) {
86 + const cache = env.VITE_CACHE_PATH || NodePath.join(root, "node_modules/.vite/task-cache");
87 + const queue = NodePath.join(home, ".cache/t3-task-cache/telemetry");
88 + let host =
89 + env.T3_TASK_CACHE_HOST ||
90 + (env.GITHUB_ACTIONS === "true"
91 + ? `${env.RUNNER_ENVIRONMENT || "github"}/${env.RUNNER_OS || NodeOS.platform()}/${env.RUNNER_ARCH || NodeOS.arch()}`
92 + : NodeOS.hostname());
93 + try {
94 + host =
95 + env.T3_TASK_CACHE_HOST ||
96 + NodeFS.readFileSync(NodePath.join(home, ".agents/MACHINE"), "utf8").trim() ||
97 + host;
98 + } catch {
99 + /* Machine labels are optional outside the fleet. */
100 + }
101 + const seen = new Set<string>();
102 + const snapshot = (file: string, enqueue: boolean) => {
103 + try {
104 + const before = NodeFS.statSync(file, { bigint: true });
105 + const contents = NodeFS.readFileSync(file, "utf8");
106 + const after = NodeFS.statSync(file, { bigint: true });
107 + if (before.ino !== after.ino || before.mtimeNs !== after.mtimeNs) return;
108 + // Nested/concurrent launchers see the same summary; stable ids prevent double counting.
109 + const id = NodeCrypto.createHash("sha256")
110 + .update(`${host}\0${root}\0${before.ino}\0${before.mtimeNs}\0${contents}`)
111 + .digest("hex");
112 + if (seen.has(id)) return;
113 + seen.add(id);
114 + if (!enqueue) return;
115 + const tasks = summarize(JSON.parse(contents));
116 + if (!tasks.length) return;
117 + const event: SummaryEvent = {
118 + id,
119 + recordedAt: Number(before.mtimeNs / 1_000_000n),
120 + host: host.slice(0, 256),
121 + platform: NodeOS.platform(),
122 + arch: NodeOS.arch(),
123 + ci: env.GITHUB_ACTIONS === "true",
124 + runId: env.GITHUB_RUN_ID || "",
125 + tasks,
126 + };
127 + NodeFS.mkdirSync(queue, { recursive: true, mode: 0o700 });
128 + const temporary = NodePath.join(queue, `${id}.${process.pid}.tmp`);
129 + NodeFS.writeFileSync(temporary, JSON.stringify({ endpoint, event }), { mode: 0o600 });
130 + NodeFS.renameSync(temporary, NodePath.join(queue, `${id}.json`));
131 + } catch {
132 + /* Telemetry must never prevent task execution. */
133 + }
134 + };
135 + const scan = (enqueue: boolean) => {
136 + for (const entry of NodeFS.readdirSync(cache, { withFileTypes: true })) {
137 + if (entry.isDirectory())
138 + snapshot(NodePath.join(cache, entry.name, "last-summary.json"), enqueue);
139 + }
140 + };
141 + try {
142 + NodeFS.mkdirSync(cache, { recursive: true });
143 + scan(false);
144 + // Capture nested runs before the parent's atomic summary replaces them.
145 + const watcher = NodeFS.watch(cache, { recursive: true }, (_event, file) => {
146 + if (file?.endsWith("last-summary.json")) snapshot(NodePath.join(cache, file), true);
147 + });
148 + watcher.on("error", () => watcher.close());
149 + return () => {
150 + watcher.close();
151 + try {
152 + scan(true);
153 + } catch {
154 + /* A task may remove its cache directory. */
155 + }
156 + };
157 + } catch {
158 + return () => {};
159 + }
160 +}
161 +
162 +export async function flushTelemetry(home = NodeOS.homedir(), env = process.env) {
163 + const queue = NodePath.join(home, ".cache/t3-task-cache/telemetry");
164 + const lock = NodePath.join(queue, "flush.lock");
165 + try {
166 + NodeFS.mkdirSync(queue, { recursive: true, mode: 0o700 });
167 + try {
168 + if (Date.now() - NodeFS.statSync(lock).mtimeMs > 60_000) NodeFS.rmdirSync(lock);
169 + } catch {
170 + /* No previous flusher. */
171 + }
172 + NodeFS.mkdirSync(lock);
173 + } catch {
174 + return;
175 + }
176 + try {
177 + const files = NodeFS.readdirSync(queue)
178 + .filter((file) => file.endsWith(".json"))
179 + .sort(
180 + (a, b) =>
181 + NodeFS.statSync(NodePath.join(queue, a)).mtimeMs -
182 + NodeFS.statSync(NodePath.join(queue, b)).mtimeMs,
183 + );
184 + // Bound disk use while offline. Unsent events retry on the next invocation.
185 + for (const file of files.slice(0, Math.max(0, files.length - 2000)))
186 + NodeFS.rmSync(NodePath.join(queue, file), { force: true });
187 + const credentialsFile = NodePath.join(home, ".config/t3-task-cache/credentials.json");
188 + const credentials = NodeFS.existsSync(credentialsFile)
189 + ? JSON.parse(NodeFS.readFileSync(credentialsFile, "utf8"))
190 + : {};
191 + const deadline = Date.now() + 15_000;
192 + const skipped = new Set<string>();
193 + // Re-scan between sends so a concurrent command's final summary joins this flush.
194 + for (let sent = 0; sent < 100 && Date.now() < deadline;) {
195 + const file = NodeFS.readdirSync(queue)
196 + .filter((entry) => entry.endsWith(".json") && !skipped.has(entry))
197 + .sort()[0];
198 + if (!file) break;
199 + const path = NodePath.join(queue, file);
200 + const { endpoint, event } = JSON.parse(NodeFS.readFileSync(path, "utf8"));
201 + const url = new URL(endpoint);
202 + const configured = env.VP_REMOTE_CACHE_URL ? new URL(env.VP_REMOTE_CACHE_URL) : undefined;
203 + const configuredEndpoint = configured && new URL(configured.href);
204 + if (configuredEndpoint) {
205 + configuredEndpoint.username = "";
206 + configuredEndpoint.password = "";
207 + }
208 + const credential =
209 + configured?.username && configuredEndpoint?.href.replace(/\/$/, "") === endpoint
210 + ? {
211 + name: decodeURIComponent(configured.username),
212 + token: decodeURIComponent(configured.password),
213 + }
214 + : credentials[endpoint];
215 + let authorization: string;
216 + if (
217 + credential &&
218 + !(
219 + env.GITHUB_ACTIONS === "true" &&
220 + env.ACTIONS_ID_TOKEN_REQUEST_URL &&
221 + env.ACTIONS_ID_TOKEN_REQUEST_TOKEN
222 + )
223 + )
224 + authorization = `Basic ${Buffer.from(`${credential.name}:${credential.token}`).toString("base64")}`;
225 + else if (env.ACTIONS_ID_TOKEN_REQUEST_URL && env.ACTIONS_ID_TOKEN_REQUEST_TOKEN) {
226 + const tokenUrl = new URL(env.ACTIONS_ID_TOKEN_REQUEST_URL);
227 + tokenUrl.searchParams.set("audience", endpoint);
228 + const response = await fetch(tokenUrl, {
229 + headers: { Authorization: `Bearer ${env.ACTIONS_ID_TOKEN_REQUEST_TOKEN}` },
230 + signal: AbortSignal.timeout(3000),
231 + });
232 + if (!response.ok) return;
233 + const token = (await response.json()) as { value: string };
234 + authorization = `Bearer ${token.value}`;
235 + } else {
236 + skipped.add(file);
237 + continue;
238 + }
239 + sent++;
240 + url.pathname = `${url.pathname.replace(/\/$/, "")}/telemetry`;
241 + const response = await fetch(url, {
242 + method: "POST",
243 + headers: { Authorization: authorization, "Content-Type": "application/json" },
244 + body: JSON.stringify(event),
245 + signal: AbortSignal.timeout(3000),
246 + });
247 + await response.body?.cancel();
248 + if (response.ok || [400, 401, 403, 413].includes(response.status)) NodeFS.unlinkSync(path);
249 + else return;
250 + }
251 + } catch {
252 + /* Leave unsent events for a later invocation. */
253 + } finally {
254 + NodeFS.rmdirSync(lock);
255 + }
256 +}
infra/task-cache/src/telemetryAuth.test.ts new
+84
@@ -0,0 +1,84 @@
1 +import { generateKeyPair, SignJWT } from "jose";
2 +import { expect, it, vi } from "vite-plus/test";
3 +import { authorizeCacheWrite, authorizeTelemetry } from "./telemetryAuth.ts";
4 +import { authorize, ISSUER } from "../vendor/vite-plus-remote-cache-cloudflare/src/auth.ts";
5 +
6 +it("accepts signed stacked PR telemetry without granting cache writes or trusting another repository", async () => {
7 + vi.useFakeTimers({ toFake: ["Date"] });
8 + vi.setSystemTime(1_000_000);
9 + try {
10 + const { publicKey, privateKey } = await generateKeyPair("RS256");
11 + const scope = {
12 + scope_id: "t3code",
13 + endpoint: "https://cache.example/projects/t3code",
14 + repository_id: "123",
15 + repository_owner_id: "456",
16 + branch: "refs/heads/main",
17 + policy_version: 1,
18 + writes_enabled: 0,
19 + pull_requests: 0,
20 + };
21 + const token = async (repository_id: string, audience = scope.endpoint) =>
22 + new SignJWT({
23 + repository_id,
24 + repository_owner_id: "456",
25 + repository_visibility: "public",
26 + event_name: "pull_request",
27 + ref: "refs/pull/42/merge",
28 + ref_type: "branch",
29 + base_ref: "feature/parent",
30 + actor: "maria",
31 + })
32 + .setProtectedHeader({ alg: "RS256", kid: "test" })
33 + .setIssuer(ISSUER)
34 + .setAudience(audience)
35 + .setIssuedAt(1000)
36 + .setNotBefore(1000)
37 + .setExpirationTime(1600)
38 + .sign(privateKey);
39 + const request = (token: string) =>
40 + new Request(scope.endpoint, { headers: { Authorization: `Bearer ${token}` } });
41 + const keys = async () => publicKey;
42 + const valid = request(await token("123"));
43 + const allowed = {
44 + ...scope,
45 + writes_enabled: 1,
46 + pull_requests: 1,
47 + branch: "refs/heads/feature/parent",
48 + };
49 + expect(
50 + await authorizeCacheWrite(
51 + request(await token("123", "https://ci-reader:secret@cache.example/projects/t3code")),
52 + allowed,
53 + keys,
54 + ),
55 + ).toMatchObject({ repository_id: "123" });
56 + await expect(
57 + authorizeCacheWrite(
58 + request(await token("123", "https://ci-reader:secret@other.example/projects/t3code")),
59 + allowed,
60 + keys,
61 + ),
62 + ).rejects.toMatchObject({ status: 403 });
63 + expect(await authorizeTelemetry(valid, scope, keys)).toBe("github/maria");
64 + await expect(
65 + authorizeCacheWrite(valid, { ...allowed, pull_requests: 0 }, keys),
66 + ).rejects.toMatchObject({ status: 403 });
67 + await expect(
68 + authorizeCacheWrite(valid, { ...allowed, branch: "refs/heads/main" }, keys),
69 + ).rejects.toMatchObject({ status: 403 });
70 + await expect(authorize(valid, scope, keys)).rejects.toMatchObject({ status: 403 });
71 + await expect(authorizeCacheWrite(valid, scope, keys)).rejects.toMatchObject({ status: 403 });
72 + expect(await authorizeCacheWrite(valid, allowed, keys)).toMatchObject({
73 + repository_id: "123",
74 + });
75 + await expect(
76 + authorizeCacheWrite(request(await token("another-repo")), allowed, keys),
77 + ).rejects.toMatchObject({ status: 403 });
78 + await expect(
79 + authorizeTelemetry(request(await token("another-repo")), scope, keys),
80 + ).rejects.toMatchObject({ status: 403 });
81 + } finally {
82 + vi.useRealTimers();
83 + }
84 +});
infra/task-cache/src/telemetryAuth.ts new
+44
@@ -0,0 +1,44 @@
1 +import { decodeJwt, type JWTVerifyGetKey } from "jose";
2 +import { authorize } from "../vendor/vite-plus-remote-cache-cloudflare/src/auth.ts";
3 +import type { Scope } from "../vendor/vite-plus-remote-cache-cloudflare/src/database.ts";
4 +import { HttpError } from "../vendor/vite-plus-remote-cache-cloudflare/src/errors.ts";
5 +
6 +function audienceScope(request: Request, scope: Scope, telemetry = false) {
7 + const authorization = request.headers.get("authorization");
8 + if (!authorization || authorization.length > 16 * 1024) throw new HttpError(401, "invalid_token");
9 + let payload;
10 + try {
11 + payload = decodeJwt(authorization.slice(7));
12 + } catch {
13 + throw new HttpError(401, "invalid_token");
14 + }
15 + // The decoded base only selects policy; authorize verifies all claims and the signature.
16 + const branch =
17 + telemetry && payload.event_name === "pull_request" && typeof payload.base_ref === "string"
18 + ? `refs/heads/${payload.base_ref}`
19 + : scope.branch;
20 + let endpoint = scope.endpoint;
21 + if (typeof payload.aud === "string") {
22 + try {
23 + const audience = new URL(payload.aud);
24 + audience.username = "";
25 + audience.password = "";
26 + // Vite+ uses the configured URL, including read credentials, as its OIDC audience.
27 + if (audience.href.replace(/\/$/, "") === scope.endpoint.replace(/\/$/, ""))
28 + endpoint = payload.aud;
29 + } catch {
30 + // The verified authorization below rejects malformed and foreign audiences.
31 + }
32 + }
33 + return { ...scope, branch, endpoint, pull_requests: telemetry ? 1 : scope.pull_requests };
34 +}
35 +
36 +export async function authorizeCacheWrite(request: Request, scope: Scope, keys: JWTVerifyGetKey) {
37 + return authorize(request, audienceScope(request, scope), keys);
38 +}
39 +
40 +export async function authorizeTelemetry(request: Request, scope: Scope, keys: JWTVerifyGetKey) {
41 + await authorize(request, audienceScope(request, { ...scope, writes_enabled: 1 }, true), keys);
42 + const payload = decodeJwt(request.headers.get("authorization")!.slice(7));
43 + return `github/${typeof payload.actor === "string" ? payload.actor : "unknown"}`;
44 +}
infra/task-cache/src/worker.ts new
+98
@@ -0,0 +1,98 @@
1 +import * as Axiom from "alchemy/Axiom";
2 +import { Observability } from "./observability.ts";
3 +import * as Cloudflare from "alchemy/Cloudflare";
4 +import * as Config from "effect/Config";
5 +import * as Effect from "effect/Effect";
6 +import * as Layer from "effect/Layer";
7 +import * as Redacted from "effect/Redacted";
8 +import * as HttpServerResponse from "effect/http/HttpServerResponse";
9 +import * as Admission from "./Admission.ts";
10 +import * as Cache from "./Cache.ts";
11 +import * as Savings from "./Savings.ts";
12 +import type { ProtocolBindings } from "./protocolTypes.ts";
13 +import * as Resources from "./resources.ts";
14 +
15 +export class Worker extends Cloudflare.Worker<Worker, {}>()("Cache") {}
16 +
17 +export const layer = Worker.make(
18 + Resources.cacheName.pipe(
19 + Effect.map((name) => ({
20 + name,
21 + main: import.meta.filename,
22 + compatibility: { date: "2026-09-11", flags: ["nodejs_compat"] },
23 + })),
24 + ),
25 + Effect.gen(function* () {
26 + const savings = yield* Cloudflare.D1.QueryDatabase(yield* Resources.Savings);
27 + const database = yield* Cloudflare.D1.QueryDatabase(yield* Resources.Index);
28 + const artifacts = yield* Cloudflare.R2.ReadWriteBucket(yield* Resources.Artifacts);
29 + const developerKeys = yield* Config.Redacted("DEVELOPER_KEYS");
30 + const readerKeys = yield* Config.Redacted("READER_KEYS");
31 + const reads = yield* Cloudflare.RateLimit("READ_LIMITER", {
32 + namespaceId: "1001",
33 + simple: { limit: 600, period: 60 },
34 + });
35 + const writes = yield* Cloudflare.RateLimit("STORE_LIMITER", {
36 + namespaceId: "1002",
37 + simple: { limit: 600, period: 60 },
38 + });
39 + const bindings = Effect.gen(function* () {
40 + return Cache.Bindings.of({
41 + INDEX: yield* database.raw,
42 + ARTIFACTS: (yield* artifacts.raw) as unknown as ProtocolBindings["ARTIFACTS"],
43 + GC_BATCH_SIZE: "16",
44 + developerKeys: Redacted.value(developerKeys),
45 + readerKeys: Redacted.value(readerKeys),
46 + readLimit: yield* reads.raw,
47 + storeLimit: yield* writes.raw,
48 + });
49 + });
50 + const admission = yield* Admission.Admission.pipe(Effect.provide(Admission.layer));
51 + const savingsLayer = Savings.layer.pipe(
52 + Layer.provide(Layer.effect(Savings.Database, savings.raw)),
53 + );
54 + const runtime = Cache.layer.pipe(
55 + Layer.provide([
56 + Layer.succeed(Admission.Admission, admission),
57 + Layer.effect(Cache.Bindings, bindings),
58 + savingsLayer,
59 + ]),
60 + );
61 + yield* Cloudflare.Workers.cron("*/5 * * * *", () =>
62 + Cache.Cache.pipe(
63 + Effect.flatMap((cache) => cache.cleanup),
64 + Effect.provide(runtime),
65 + Effect.orDie,
66 + ),
67 + );
68 + yield* Cloudflare.Workers.cron("0 * * * *", () =>
69 + Savings.Savings.pipe(
70 + Effect.flatMap((savings) => savings.cleanup.pipe(Effect.andThen(savings.emitTotals()))),
71 + Effect.provide(savingsLayer),
72 + Effect.orDie,
73 + ),
74 + );
75 + return {
76 + fetch: Effect.gen(function* () {
77 + const request = yield* Cloudflare.Workers.Request;
78 + const context = yield* Cloudflare.Workers.WorkerExecutionContext;
79 + const cache = yield* Cache.Cache;
80 + return HttpServerResponse.fromWeb(yield* cache.handle(request, context.raw));
81 + }).pipe(Effect.provide(runtime)),
82 + };
83 + }).pipe(
84 + Effect.provide([
85 + Cloudflare.D1.QueryDatabaseBinding,
86 + Cloudflare.R2.ReadWriteBucketBinding,
87 + Cloudflare.Workers.RateLimitBinding,
88 + Cloudflare.Workers.CronEventSourceLive,
89 + Layer.unwrap(
90 + Effect.map(Observability, ({ traces, token }) =>
91 + Axiom.Telemetry({ serviceName: "t3-task-cache", traces, token }),
92 + ),
93 + ),
94 + ]),
95 + ),
96 +);
97 +// Alchemy's generated Worker entry imports the runtime layer as the default export.
98 +export default layer;
infra/task-cache/tsconfig.json new
+10
@@ -0,0 +1,10 @@
1 +{
2 + "extends": "../../tsconfig.base.json",
3 + "compilerOptions": {
4 + "allowJs": true,
5 + "lib": ["ESNext", "DOM"],
6 + "types": ["node"],
7 + "erasableSyntaxOnly": false
8 + },
9 + "include": ["alchemy.run.ts", "src/**/*.ts", "scripts/**/*.ts", "types/**/*.d.ts"]
10 +}
infra/task-cache/types/upstream.d.ts new
+12
@@ -0,0 +1,12 @@
1 +// The source-only upstream package expects Wrangler-generated globals but does not ship them.
2 +import type * as Cloudflare from "@cloudflare/workers-types";
3 +import type { ProtocolBindings } from "../src/protocolTypes.ts";
4 +
5 +declare global {
6 + interface Env extends ProtocolBindings {}
7 + type D1Database = Cloudflare.D1Database;
8 + type D1Result<T = unknown> = Cloudflare.D1Result<T>;
9 + type R2MultipartUpload = Cloudflare.R2MultipartUpload;
10 + type R2UploadedPart = Cloudflare.R2UploadedPart;
11 + type ExecutionContext = Cloudflare.ExecutionContext;
12 +}
infra/task-cache/vendor/vite-plus-remote-cache-cloudflare/LICENSE new
+21
@@ -0,0 +1,21 @@
1 +MIT License
2 +
3 +Copyright (c) 2026-present, VoidZero Inc.
4 +
5 +Permission is hereby granted, free of charge, to any person obtaining a copy
6 +of this software and associated documentation files (the "Software"), to deal
7 +in the Software without restriction, including without limitation the rights
8 +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 +copies of the Software, and to permit persons to whom the Software is
10 +furnished to do so, subject to the following conditions:
11 +
12 +The above copyright notice and this permission notice shall be included in all
13 +copies or substantial portions of the Software.
14 +
15 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 +SOFTWARE.
infra/task-cache/vendor/vite-plus-remote-cache-cloudflare/README.md new
+1
@@ -0,0 +1 @@
1 +Vendored from [vite-plus-remote-cache-cloudflare](https://github.com/voidzero-dev/vite-plus-remote-cache-cloudflare/tree/5b06c2c10d84ea441735ba07c46f6d1b7cb2ff3b) at commit `5b06c2c10d84ea441735ba07c46f6d1b7cb2ff3b`. Source and migrations match upstream except for Effect diagnostic suppression comments; the package manifest omits upstream development tooling. MIT license is included. This avoids requiring GitHub downloads for contributor installs.
infra/task-cache/vendor/vite-plus-remote-cache-cloudflare/migrations/0001_cache.sql new
+160
@@ -0,0 +1,160 @@
1 +-- Primary D1 owns policy and accounting. Back up policies separately from cache data.
2 +CREATE TABLE deployment (
3 + id INTEGER PRIMARY KEY CHECK (id = 1),
4 + enabled INTEGER NOT NULL DEFAULT 1 CHECK (enabled IN (0, 1)),
5 + writes_enabled INTEGER NOT NULL DEFAULT 1 CHECK (writes_enabled IN (0, 1)),
6 + byte_limit INTEGER NOT NULL DEFAULT 8000000000 CHECK (byte_limit > 0),
7 + entry_limit INTEGER NOT NULL DEFAULT 20000 CHECK (entry_limit > 0),
8 + association_limit INTEGER NOT NULL DEFAULT 20000 CHECK (association_limit > 0),
9 + retention_high_water_seconds INTEGER NOT NULL DEFAULT 604800,
10 + charged_bytes INTEGER NOT NULL DEFAULT 0 CHECK (charged_bytes >= 0),
11 + entry_count INTEGER NOT NULL DEFAULT 0 CHECK (entry_count >= 0),
12 + association_count INTEGER NOT NULL DEFAULT 0 CHECK (association_count >= 0)
13 +);
14 +INSERT INTO deployment (id) VALUES (1);
15 +CREATE TABLE scopes (
16 + scope_id TEXT PRIMARY KEY,
17 + endpoint TEXT NOT NULL UNIQUE,
18 + repository TEXT NOT NULL,
19 + repository_id TEXT NOT NULL,
20 + repository_owner_id TEXT NOT NULL,
21 + branch TEXT NOT NULL,
22 + enabled INTEGER NOT NULL DEFAULT 1 CHECK (enabled IN (0, 1)),
23 + writes_enabled INTEGER NOT NULL DEFAULT 1 CHECK (writes_enabled IN (0, 1)),
24 + policy_version INTEGER NOT NULL DEFAULT 1,
25 + retention_seconds INTEGER NOT NULL DEFAULT 604800 CHECK (retention_seconds > 0),
26 + byte_limit INTEGER NOT NULL DEFAULT 8000000000 CHECK (byte_limit > 0),
27 + entry_limit INTEGER NOT NULL DEFAULT 20000 CHECK (entry_limit > 0),
28 + association_limit INTEGER NOT NULL DEFAULT 20000 CHECK (association_limit > 0),
29 + charged_bytes INTEGER NOT NULL DEFAULT 0 CHECK (charged_bytes >= 0),
30 + entry_count INTEGER NOT NULL DEFAULT 0 CHECK (entry_count >= 0),
31 + association_count INTEGER NOT NULL DEFAULT 0 CHECK (association_count >= 0)
32 +);
33 +CREATE TABLE generations (
34 + generation_id TEXT PRIMARY KEY,
35 + scope_id TEXT NOT NULL REFERENCES scopes(scope_id),
36 + state TEXT NOT NULL CHECK (state IN ('uploading', 'ready', 'retired', 'deleting')),
37 + policy_version INTEGER NOT NULL,
38 + token_exp INTEGER NOT NULL,
39 + lease_until INTEGER NOT NULL,
40 + gc_after INTEGER NOT NULL,
41 + gc_claim TEXT,
42 + key BLOB,
43 + secondary_key BLOB,
44 + value_object TEXT NOT NULL UNIQUE,
45 + blob_object TEXT NOT NULL UNIQUE,
46 + blob_id TEXT,
47 + upload_id TEXT,
48 + value_size INTEGER NOT NULL DEFAULT 0 CHECK (value_size >= 0),
49 + blob_size INTEGER NOT NULL DEFAULT 0 CHECK (blob_size >= 0),
50 + charged_bytes INTEGER NOT NULL CHECK (charged_bytes >= 0),
51 + expires_at INTEGER,
52 + retired_at INTEGER,
53 + UNIQUE (scope_id, generation_id)
54 +);
55 +CREATE UNIQUE INDEX generations_blob ON generations(scope_id, blob_id) WHERE blob_id IS NOT NULL;
56 +CREATE INDEX generations_gc ON generations(gc_after);
57 +CREATE TABLE entries (
58 + scope_id TEXT NOT NULL REFERENCES scopes(scope_id),
59 + key BLOB NOT NULL CHECK (typeof(key) = 'blob'),
60 + generation_id TEXT NOT NULL,
61 + PRIMARY KEY (scope_id, key),
62 + FOREIGN KEY (scope_id, generation_id) REFERENCES generations(scope_id, generation_id) ON DELETE CASCADE
63 +) WITHOUT ROWID;
64 +CREATE INDEX entries_generation ON entries(generation_id);
65 +CREATE TABLE associations (
66 + scope_id TEXT NOT NULL REFERENCES scopes(scope_id),
67 + secondary_key BLOB NOT NULL CHECK (typeof(secondary_key) = 'blob'),
68 + target_key BLOB NOT NULL CHECK (typeof(target_key) = 'blob'),
69 + PRIMARY KEY (scope_id, secondary_key)
70 +) WITHOUT ROWID;
71 +CREATE INDEX associations_target ON associations(scope_id, target_key);
72 +CREATE TABLE maintenance (id INTEGER PRIMARY KEY CHECK (id = 1), scope_id TEXT NOT NULL, secondary_key BLOB NOT NULL);
73 +INSERT INTO maintenance VALUES (1, '', X'');
74 +
75 +-- Parenthesize CASE expressions so the D1 REST parser preserves each trigger body.
76 +-- Lifecycle rules must never shorten the life of an already published generation.
77 +CREATE TRIGGER initial_retention AFTER INSERT ON scopes BEGIN
78 + UPDATE deployment SET retention_high_water_seconds = max(retention_high_water_seconds, NEW.retention_seconds);
79 +END;
80 +CREATE TRIGGER increased_retention AFTER UPDATE OF retention_seconds ON scopes BEGIN
81 + UPDATE deployment SET retention_high_water_seconds = max(retention_high_water_seconds, NEW.retention_seconds);
82 +END;
83 +CREATE TRIGGER scope_limit BEFORE INSERT ON scopes
84 +WHEN NOT EXISTS (SELECT 1 FROM scopes WHERE scope_id = NEW.scope_id)
85 +BEGIN
86 + SELECT (CASE WHEN (SELECT count(*) FROM scopes) >= 100 THEN RAISE(ABORT, 'cache_scope_limit') END);
87 +END;
88 +
89 +-- Direct policy edits must invalidate uploads that captured the previous policy.
90 +CREATE TRIGGER changed_policy AFTER UPDATE OF endpoint, repository_id, repository_owner_id, branch,
91 + enabled, writes_enabled, retention_seconds, byte_limit, entry_limit, association_limit ON scopes
92 +WHEN NEW.policy_version = OLD.policy_version
93 +BEGIN
94 + UPDATE scopes SET policy_version = OLD.policy_version + 1 WHERE scope_id = NEW.scope_id;
95 +END;
96 +
97 +CREATE TRIGGER reserve_capacity BEFORE INSERT ON generations BEGIN
98 + SELECT (CASE WHEN NOT EXISTS (
99 + SELECT 1 FROM scopes s, deployment d WHERE s.scope_id = NEW.scope_id
100 + AND s.enabled = 1 AND s.writes_enabled = 1 AND d.enabled = 1 AND d.writes_enabled = 1
101 + AND s.policy_version = NEW.policy_version AND NEW.token_exp > unixepoch()
102 + AND s.charged_bytes + NEW.charged_bytes <= s.byte_limit
103 + AND d.charged_bytes + NEW.charged_bytes <= d.byte_limit
104 + ) THEN RAISE(ABORT, 'cache_admission_denied') END);
105 +END;
106 +CREATE TRIGGER charge_generation AFTER INSERT ON generations BEGIN
107 + UPDATE scopes SET charged_bytes = charged_bytes + NEW.charged_bytes WHERE scope_id = NEW.scope_id;
108 + UPDATE deployment SET charged_bytes = charged_bytes + NEW.charged_bytes;
109 +END;
110 +CREATE TRIGGER adjust_generation AFTER UPDATE OF charged_bytes ON generations BEGIN
111 + UPDATE scopes SET charged_bytes = charged_bytes + NEW.charged_bytes - OLD.charged_bytes WHERE scope_id = NEW.scope_id;
112 + UPDATE deployment SET charged_bytes = charged_bytes + NEW.charged_bytes - OLD.charged_bytes;
113 +END;
114 +CREATE TRIGGER release_generation AFTER DELETE ON generations BEGIN
115 + UPDATE scopes SET charged_bytes = charged_bytes - OLD.charged_bytes WHERE scope_id = OLD.scope_id;
116 + UPDATE deployment SET charged_bytes = charged_bytes - OLD.charged_bytes;
117 +END;
118 +
119 +CREATE TRIGGER count_entry BEFORE INSERT ON entries
120 +WHEN NOT EXISTS (SELECT 1 FROM entries WHERE scope_id = NEW.scope_id AND key = NEW.key)
121 +BEGIN
122 + SELECT (CASE WHEN EXISTS (
123 + SELECT 1 FROM scopes s, deployment d WHERE s.scope_id = NEW.scope_id
124 + AND (s.entry_count >= s.entry_limit OR d.entry_count >= d.entry_limit)
125 + ) THEN RAISE(ABORT, 'cache_entry_limit') END);
126 + UPDATE scopes SET entry_count = entry_count + 1 WHERE scope_id = NEW.scope_id;
127 + UPDATE deployment SET entry_count = entry_count + 1;
128 +END;
129 +CREATE TRIGGER uncount_entry AFTER DELETE ON entries BEGIN
130 + UPDATE scopes SET entry_count = entry_count - 1 WHERE scope_id = OLD.scope_id;
131 + UPDATE deployment SET entry_count = entry_count - 1;
132 +END;
133 +CREATE TRIGGER count_association BEFORE INSERT ON associations
134 +WHEN NOT EXISTS (SELECT 1 FROM associations WHERE scope_id = NEW.scope_id AND secondary_key = NEW.secondary_key)
135 +BEGIN
136 + SELECT (CASE WHEN EXISTS (
137 + SELECT 1 FROM scopes s, deployment d WHERE s.scope_id = NEW.scope_id
138 + AND (s.association_count >= s.association_limit OR d.association_count >= d.association_limit)
139 + ) THEN RAISE(ABORT, 'cache_association_limit') END);
140 + UPDATE scopes SET association_count = association_count + 1 WHERE scope_id = NEW.scope_id;
141 + UPDATE deployment SET association_count = association_count + 1;
142 +END;
143 +CREATE TRIGGER uncount_association AFTER DELETE ON associations BEGIN
144 + UPDATE scopes SET association_count = association_count - 1 WHERE scope_id = OLD.scope_id;
145 + UPDATE deployment SET association_count = association_count - 1;
146 +END;
147 +
148 +-- All publication mutations inherit the guarded state change's transaction.
149 +-- A zero-row guard fires no trigger. A quota failure rolls every mutation back.
150 +CREATE TRIGGER publish_generation AFTER UPDATE OF state ON generations
151 +WHEN OLD.state = 'uploading' AND NEW.state = 'ready'
152 +BEGIN
153 + UPDATE generations SET state = 'retired', retired_at = unixepoch(),
154 + gc_after = (CASE WHEN expires_at > unixepoch() THEN unixepoch() + 600 ELSE unixepoch() END)
155 + WHERE generation_id = (SELECT generation_id FROM entries WHERE scope_id = NEW.scope_id AND key = NEW.key) AND state = 'ready';
156 + INSERT INTO entries (scope_id, key, generation_id) VALUES (NEW.scope_id, NEW.key, NEW.generation_id)
157 + ON CONFLICT (scope_id, key) DO UPDATE SET generation_id = excluded.generation_id;
158 + INSERT INTO associations (scope_id, secondary_key, target_key) VALUES (NEW.scope_id, NEW.secondary_key, NEW.key)
159 + ON CONFLICT (scope_id, secondary_key) DO UPDATE SET target_key = excluded.target_key;
160 +END;
infra/task-cache/vendor/vite-plus-remote-cache-cloudflare/migrations/0002_pull_requests.sql new
+12
@@ -0,0 +1,12 @@
1 +-- Per-namespace opt-in for uploads from pull_request runs whose base is the registered branch.
2 +-- Off by default: such uploads run unreviewed PR code, and default-branch builds restore their entries.
3 +ALTER TABLE scopes ADD COLUMN pull_requests INTEGER NOT NULL DEFAULT 0 CHECK (pull_requests IN (0, 1));
4 +
5 +-- Turning the opt-in off must also invalidate uploads that captured the previous policy.
6 +DROP TRIGGER changed_policy;
7 +CREATE TRIGGER changed_policy AFTER UPDATE OF endpoint, repository_id, repository_owner_id, branch,
8 + enabled, writes_enabled, pull_requests, retention_seconds, byte_limit, entry_limit, association_limit ON scopes
9 +WHEN NEW.policy_version = OLD.policy_version
10 +BEGIN
11 + UPDATE scopes SET policy_version = OLD.policy_version + 1 WHERE scope_id = NEW.scope_id;
12 +END;
infra/task-cache/vendor/vite-plus-remote-cache-cloudflare/package.json new
+10
@@ -0,0 +1,10 @@
1 +{
2 + "name": "@voidzero-dev/vite-plus-remote-cache-cloudflare",
3 + "version": "0.0.0",
4 + "private": true,
5 + "license": "MIT",
6 + "type": "module",
7 + "dependencies": {
8 + "jose": "catalog:"
9 + }
10 +}
infra/task-cache/vendor/vite-plus-remote-cache-cloudflare/src/admission.ts new
+17
@@ -0,0 +1,17 @@
1 +import { HttpError } from './errors.ts';
2 +
3 +// Isolate-wide resource counters contain no request data or I/O promises. Rate
4 +// limiting controls traffic; these counters additionally bound simultaneous buffers.
5 +export class Admission {
6 + private stores = 0;
7 + private reads = 0;
8 + acquire(store: boolean): () => void {
9 + if (store ? this.stores >= 2 : this.reads >= 4) throw new HttpError(503, 'concurrency_limit');
10 + if (store) this.stores++;
11 + else this.reads++;
12 + return () => {
13 + if (store) this.stores--;
14 + else this.reads--;
15 + };
16 + }
17 +}
infra/task-cache/vendor/vite-plus-remote-cache-cloudflare/src/auth.ts new
+166
@@ -0,0 +1,166 @@
1 +// @effect-diagnostics globalDate:off globalFetch:off - vendored upstream protocol, not an Effect service.
2 +import {
3 + createRemoteJWKSet,
4 + customFetch,
5 + decodeProtectedHeader,
6 + errors,
7 + jwtVerify,
8 + type JWTVerifyGetKey,
9 + type JWTPayload,
10 +} from 'jose';
11 +import { HttpError, unavailable } from './errors.ts';
12 +import { Deadline, readBody } from './streams.ts';
13 +import type { Scope } from './database.ts';
14 +
15 +export const ISSUER = 'https://token.actions.githubusercontent.com';
16 +export const JWKS_URL = `${ISSUER}/.well-known/jwks`;
17 +
18 +export function githubKeys(): JWTVerifyGetKey {
19 + // Only reusable public-key cache state lives across requests. Failed refreshes
20 + // also have a cooldown; jose's own cooldown starts only after a successful fetch.
21 + let lastAttempt = -Infinity;
22 + return createRemoteJWKSet(new URL(JWKS_URL), {
23 + timeoutDuration: 5000,
24 + cooldownDuration: 30_000,
25 + cacheMaxAge: 10 * 60_000,
26 + [customFetch]: async (url, options) => {
27 + if (Date.now() - lastAttempt < 30_000) unavailable();
28 + lastAttempt = Date.now();
29 + const deadline = new Deadline(5000, options.signal);
30 + try {
31 + const response = await deadline.run(fetch(url, options));
32 + if (response.status !== 200) {
33 + void response.body?.cancel();
34 + unavailable();
35 + }
36 + const bytes = await readBody(response.body, 64 * 1024, deadline);
37 + return new Response(bytes, { headers: { 'Content-Type': 'application/json' } });
38 + } catch {
39 + unavailable();
40 + } finally {
41 + deadline.dispose();
42 + }
43 + },
44 + });
45 +}
46 +
47 +export interface WriteIdentity {
48 + exp: number;
49 + repository_id: string;
50 + workflow_ref?: string;
51 + run_id?: string;
52 + run_attempt?: string;
53 + sha?: string;
54 + event_name?: string;
55 + head_ref?: string;
56 +}
57 +
58 +export async function authorize(
59 + request: Request,
60 + scope: Scope,
61 + keys: JWTVerifyGetKey,
62 +): Promise<WriteIdentity> {
63 + const authorization = request.headers.get('Authorization');
64 + if (
65 + !authorization ||
66 + authorization.length > 16 * 1024 ||
67 + !/^Bearer [A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/i.test(authorization)
68 + ) {
69 + throw new HttpError(401, 'invalid_token');
70 + }
71 + const token = authorization.slice(7);
72 + let payload: JWTPayload;
73 + try {
74 + const header = decodeProtectedHeader(token);
75 + if (
76 + header.alg !== 'RS256' ||
77 + typeof header.kid !== 'string' ||
78 + header.kid.length > 256 ||
79 + header.jku ||
80 + header.x5u ||
81 + header.jwk
82 + ) {
83 + throw new HttpError(401, 'invalid_token');
84 + }
85 + ({ payload } = await jwtVerify(token, keys, {
86 + algorithms: ['RS256'],
87 + issuer: ISSUER,
88 + requiredClaims: ['exp', 'nbf', 'iat'],
89 + clockTolerance: 30,
90 + maxTokenAge: '15 minutes',
91 + }));
92 + } catch (error) {
93 + if (error instanceof HttpError) throw error;
94 + if (
95 + error instanceof errors.JWTExpired ||
96 + error instanceof errors.JWTClaimValidationFailed ||
97 + error instanceof errors.JWSSignatureVerificationFailed ||
98 + error instanceof errors.JWSInvalid ||
99 + error instanceof errors.JWTInvalid ||
100 + error instanceof errors.JOSENotSupported ||
101 + error instanceof errors.JWKSNoMatchingKey ||
102 + error instanceof errors.JOSEAlgNotAllowed
103 + ) {
104 + throw new HttpError(401, 'invalid_token');
105 + }
106 + unavailable();
107 + }
108 + const now = Date.now() / 1000;
109 + const { exp, nbf, iat } = payload;
110 + if (
111 + typeof exp !== 'number' ||
112 + typeof nbf !== 'number' ||
113 + typeof iat !== 'number' ||
114 + !Number.isSafeInteger(exp) ||
115 + !Number.isSafeInteger(nbf) ||
116 + !Number.isSafeInteger(iat) ||
117 + exp <= now ||
118 + nbf > now + 30 ||
119 + iat > now + 30 ||
120 + iat < now - 930 ||
121 + nbf > exp ||
122 + iat >= exp ||
123 + exp - iat > 900
124 + ) {
125 + throw new HttpError(401, 'invalid_token');
126 + }
127 + // A `push` must update the registered branch. A namespace that opts in also accepts
128 + // `pull_request` runs whose PR targets that branch. For those, GitHub signs `ref`
129 + // `refs/pull/<number>/merge`, `ref_type` `branch`, and the target branch's short name (`main`)
130 + // in `base_ref`. The `closed` run of a merged PR has the base branch in `ref` and is denied.
131 + // Fork PR runs carry the base repository's IDs too; only GitHub withholding `id-token: write`
132 + // from them keeps them out.
133 + const event =
134 + payload.event_name === 'push'
135 + ? payload.ref === scope.branch
136 + : payload.event_name === 'pull_request' &&
137 + scope.pull_requests === 1 &&
138 + typeof payload.ref === 'string' &&
139 + /^refs\/pull\/[1-9][0-9]*\/merge$/.test(payload.ref) &&
140 + typeof payload.base_ref === 'string' &&
141 + `refs/heads/${payload.base_ref}` === scope.branch;
142 + if (
143 + !scope.writes_enabled ||
144 + payload.aud !== scope.endpoint ||
145 + payload.repository_id !== scope.repository_id ||
146 + payload.repository_owner_id !== scope.repository_owner_id ||
147 + payload.repository_visibility !== 'public' ||
148 + payload.ref_type !== 'branch' ||
149 + !event
150 + ) {
151 + throw new HttpError(403, 'write_policy');
152 + }
153 + const identity: WriteIdentity = { exp, repository_id: scope.repository_id };
154 + for (const name of [
155 + 'workflow_ref',
156 + 'run_id',
157 + 'run_attempt',
158 + 'sha',
159 + 'event_name',
160 + 'head_ref',
161 + ] as const) {
162 + const value = payload[name];
163 + if (typeof value === 'string' && value && value.length <= 512) identity[name] = value;
164 + }
165 + return identity;
166 +}
infra/task-cache/vendor/vite-plus-remote-cache-cloudflare/src/cbor.ts new
+151
@@ -0,0 +1,151 @@
1 +import { badRequest, tooLarge } from './errors.ts';
2 +import type { Limits } from './limits.ts';
3 +
4 +export interface FetchRequest {
5 + key: Uint8Array;
6 + secondary_key: Uint8Array;
7 +}
8 +
9 +export interface StoreMetadata extends FetchRequest {
10 + value: Uint8Array;
11 +}
12 +
13 +// This codec only decodes the protocol envelope. Opaque bytes are never decoded.
14 +// The accepted containers are one map and (possibly chunked) strings: depth <= 2.
15 +class Decoder {
16 + offset = 0;
17 + constructor(private data: Uint8Array) {}
18 + byte(): number {
19 + return this.data[this.offset++] ?? badRequest();
20 + }
21 + head(major: number): number | null {
22 + const first = this.byte();
23 + if (first >> 5 !== major) badRequest();
24 + const info = first & 31;
25 + if (info < 24) return info;
26 + if (info === 31) return null;
27 + if (info > 27) badRequest();
28 + let length = 0;
29 + for (let i = 0; i < 2 ** (info - 24); i++) length = length * 256 + this.byte();
30 + if (!Number.isSafeInteger(length)) tooLarge();
31 + return length;
32 + }
33 + string(major: number, limit: number): Uint8Array {
34 + const length = this.head(major);
35 + if (length !== null) return this.take(length, limit);
36 + const chunks: Uint8Array[] = [];
37 + let size = 0;
38 + while (this.data[this.offset] !== 255) {
39 + const chunkLength = this.head(major);
40 + if (chunkLength === null) badRequest();
41 + size += chunkLength;
42 + if (size > limit) tooLarge();
43 + chunks.push(this.take(chunkLength, limit));
44 + // Bound bookkeeping even for a malicious sequence of empty chunks.
45 + if (chunks.length > 4096) tooLarge();
46 + }
47 + this.offset++;
48 + return join(chunks, size);
49 + }
50 + take(size: number, limit: number): Uint8Array {
51 + if (size > limit) tooLarge();
52 + if (size > this.data.length - this.offset) badRequest();
53 + const value = this.data.subarray(this.offset, this.offset + size);
54 + this.offset += size;
55 + return value;
56 + }
57 + finished(): boolean {
58 + return this.offset === this.data.length;
59 + }
60 + break(): boolean {
61 + if (this.data[this.offset] !== 255) return false;
62 + this.offset++;
63 + return true;
64 + }
65 +}
66 +
67 +export function join(chunks: Uint8Array[], size: number): Uint8Array<ArrayBuffer> {
68 + const data = new Uint8Array(size);
69 + let offset = 0;
70 + for (const chunk of chunks) {
71 + data.set(chunk, offset);
72 + offset += chunk.length;
73 + }
74 + return data;
75 +}
76 +
77 +export function decodeEnvelope(data: Uint8Array, store: false, limits: Limits): FetchRequest;
78 +export function decodeEnvelope(data: Uint8Array, store: true, limits: Limits): StoreMetadata;
79 +export function decodeEnvelope(
80 + data: Uint8Array,
81 + store: boolean,
82 + limits: Limits,
83 +): FetchRequest | StoreMetadata {
84 + const decoder = new Decoder(data);
85 + const count = decoder.head(5);
86 + const expected = store ? 3 : 2;
87 + if (count !== null && count !== expected) badRequest();
88 + const fields = new Map<string, Uint8Array>();
89 + for (let i = 0; i < expected; i++) {
90 + let name: string;
91 + try {
92 + name = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(
93 + decoder.string(3, 32),
94 + );
95 + } catch {
96 + badRequest();
97 + }
98 + const allowed = name === 'key' || name === 'secondary_key' || (store && name === 'value');
99 + if (!allowed || fields.has(name)) badRequest();
100 + fields.set(name, decoder.string(2, name === 'value' ? limits.value : limits.key));
101 + }
102 + if ((count === null && !decoder.break()) || !decoder.finished()) badRequest();
103 + return {
104 + key: fields.get('key')!,
105 + secondary_key: fields.get('secondary_key')!,
106 + ...(store ? { value: fields.get('value')! } : {}),
107 + };
108 +}
109 +
110 +function header(major: number, length: number): Uint8Array {
111 + if (length < 24) return Uint8Array.of((major << 5) | length);
112 + if (length <= 255) return Uint8Array.of((major << 5) | 24, length);
113 + if (length <= 65535) return Uint8Array.of((major << 5) | 25, length >> 8, length & 255);
114 + return Uint8Array.of(
115 + (major << 5) | 26,
116 + length >>> 24,
117 + (length >>> 16) & 255,
118 + (length >>> 8) & 255,
119 + length & 255,
120 + );
121 +}
122 +
123 +export function encodeEnvelope(
124 + fields: Record<string, string | Uint8Array | null>,
125 +): Uint8Array<ArrayBuffer> {
126 + const chunks = [header(5, Object.keys(fields).length)];
127 + const string = (value: string | Uint8Array) => {
128 + const bytes = typeof value === 'string' ? new TextEncoder().encode(value) : value;
129 + chunks.push(header(typeof value === 'string' ? 3 : 2, bytes.length), bytes);
130 + };
131 + for (const [key, value] of Object.entries(fields)) {
132 + string(key);
133 + if (value === null) chunks.push(Uint8Array.of(246));
134 + else string(value);
135 + }
136 + return join(
137 + chunks,
138 + chunks.reduce((sum, chunk) => sum + chunk.length, 0),
139 + );
140 +}
141 +
142 +export function cborResponse(fields: Record<string, string | Uint8Array | null>): Response {
143 + const body = encodeEnvelope(fields);
144 + return new Response(body, {
145 + headers: {
146 + 'Content-Type': 'application/cbor',
147 + 'Content-Length': String(body.length),
148 + 'Cache-Control': 'no-store',
149 + },
150 + });
151 +}
infra/task-cache/vendor/vite-plus-remote-cache-cloudflare/src/database.ts new
+291
@@ -0,0 +1,291 @@
1 +// @effect-diagnostics cryptoRandomUUID:off globalConsole:off - vendored upstream protocol, not an Effect service.
2 +import { HttpError, unavailable } from './errors.ts';
3 +import { GRACE_SECONDS, LEASE_SECONDS } from './limits.ts';
4 +import { measured, type Observations } from './observations.ts';
5 +
6 +export interface Scope {
7 + scope_id: string;
8 + endpoint: string;
9 + repository_id: string;
10 + repository_owner_id: string;
11 + branch: string;
12 + policy_version: number;
13 + writes_enabled: number;
14 + // 1 when `pull_request` runs whose PR targets `branch` may also publish.
15 + pull_requests: number;
16 +}
17 +export interface Generation {
18 + generation_id: string;
19 + scope_id: string;
20 + value_object: string;
21 + blob_object: string;
22 + blob_id: string | null;
23 + value_size: number;
24 + blob_size: number;
25 +}
26 +export interface Selection extends Generation {
27 + kind: 'exact' | 'fallback';
28 + key: number[];
29 +}
30 +
31 +// Preserve empty and non-UTF-8 keys as BLOBs, without text or hash conversions.
32 +export function binary(bytes: Uint8Array): ArrayBuffer {
33 + return bytes.slice().buffer;
34 +}
35 +
36 +export async function getScope(db: D1Database, id: string, stats?: Observations): Promise<Scope> {
37 + let scope: Scope | null;
38 + try {
39 + const { results } = await measured(
40 + db
41 + .prepare(`SELECT s.*, (s.writes_enabled AND d.writes_enabled) AS writes_enabled
42 + FROM scopes s, deployment d WHERE s.scope_id = ? AND s.enabled = 1 AND d.enabled = 1`)
43 + .bind(id)
44 + .all<Scope>(),
45 + stats,
46 + );
47 + scope = results[0] ?? null;
48 + } catch {
49 + unavailable();
50 + }
51 + if (!scope) throw new HttpError(404, 'unknown_scope');
52 + return scope;
53 +}
54 +
55 +export async function selectEntry(
56 + db: D1Database,
57 + scope: string,
58 + key: Uint8Array,
59 + secondary: Uint8Array,
60 + stats?: Observations,
61 +): Promise<Selection | null> {
62 + // Both branches use indexed identities in one SQLite snapshot.
63 + try {
64 + const { results } = await measured(
65 + db
66 + .prepare(`
67 + SELECT g.*, e.key, 'exact' AS kind, 0 AS priority
68 + FROM entries e JOIN generations g ON g.generation_id = e.generation_id
69 + JOIN scopes s ON s.scope_id = e.scope_id CROSS JOIN deployment d
70 + WHERE e.scope_id = ?1 AND e.key = ?2 AND g.state = 'ready' AND g.expires_at > unixepoch()
71 + AND s.enabled = 1 AND d.enabled = 1
72 + UNION ALL
73 + SELECT g.*, e.key, 'fallback' AS kind, 1 AS priority
74 + FROM associations a JOIN entries e ON e.scope_id = a.scope_id AND e.key = a.target_key
75 + JOIN generations g ON g.generation_id = e.generation_id
76 + JOIN scopes s ON s.scope_id = e.scope_id CROSS JOIN deployment d
77 + WHERE a.scope_id = ?1 AND a.secondary_key = ?3 AND g.state = 'ready' AND g.expires_at > unixepoch()
78 + AND s.enabled = 1 AND d.enabled = 1
79 + ORDER BY priority LIMIT 1`)
80 + .bind(scope, binary(key), binary(secondary))
81 + .all<Selection>(),
82 + stats,
83 + );
84 + return results[0] ?? null;
85 + } catch {
86 + unavailable();
87 + }
88 +}
89 +
90 +export async function selectBlob(
91 + db: D1Database,
92 + scope: string,
93 + blob: string,
94 + stats?: Observations,
95 +): Promise<Generation | null> {
96 + try {
97 + const { results } = await measured(
98 + db
99 + .prepare(`SELECT g.* FROM generations g JOIN scopes s ON s.scope_id = g.scope_id CROSS JOIN deployment d
100 + WHERE g.scope_id = ? AND g.blob_id = ? AND s.enabled = 1 AND d.enabled = 1
101 + AND ((g.state = 'ready' AND g.expires_at > unixepoch()) OR (g.state = 'retired' AND g.gc_after > unixepoch()))`)
102 + .bind(scope, blob)
103 + .all<Generation>(),
104 + stats,
105 + );
106 + return results[0] ?? null;
107 + } catch {
108 + unavailable();
109 + }
110 +}
111 +
112 +export async function reserve(
113 + db: D1Database,
114 + scope: Scope,
115 + tokenExp: number,
116 + bytes: number,
117 + stats?: Observations,
118 +): Promise<Generation> {
119 + const id = crypto.randomUUID();
120 + const prefix = `${scope.scope_id}/${id}`;
121 + try {
122 + await measured(
123 + db
124 + .prepare(`INSERT INTO generations
125 + (generation_id, scope_id, state, policy_version, token_exp, lease_until, gc_after, value_object, blob_object, charged_bytes)
126 + VALUES (?, ?, 'uploading', ?, ?, unixepoch() + ?, unixepoch() + ?, ?, ?, ?)`)
127 + .bind(
128 + id,
129 + scope.scope_id,
130 + scope.policy_version,
131 + tokenExp,
132 + LEASE_SECONDS,
133 + LEASE_SECONDS + GRACE_SECONDS,
134 + `${prefix}/value`,
135 + `${prefix}/blob`,
136 + bytes,
137 + )
138 + .run(),
139 + stats,
140 + );
141 + } catch {
142 + unavailable();
143 + }
144 + return {
145 + generation_id: id,
146 + scope_id: scope.scope_id,
147 + value_object: `${prefix}/value`,
148 + blob_object: `${prefix}/blob`,
149 + blob_id: null,
150 + value_size: 0,
151 + blob_size: 0,
152 + };
153 +}
154 +
155 +export async function recordMultipart(
156 + db: D1Database,
157 + generation: Generation,
158 + uploadId: string,
159 + stats?: Observations,
160 +): Promise<void> {
161 + const result = await measured(
162 + db
163 + .prepare(
164 + `UPDATE generations SET upload_id = ? WHERE generation_id = ? AND state = 'uploading' AND lease_until > unixepoch()`,
165 + )
166 + .bind(uploadId, generation.generation_id)
167 + .run(),
168 + stats,
169 + );
170 + if (result.meta.changes !== 1) unavailable();
171 +}
172 +
173 +export async function publish(
174 + db: D1Database,
175 + generation: Generation,
176 + key: Uint8Array,
177 + secondary: Uint8Array,
178 + stats?: Observations,
179 +): Promise<void> {
180 + const actual = generation.value_size + generation.blob_size;
181 + let results: D1Result[];
182 + try {
183 + results = await measured(
184 + db.batch([
185 + db
186 + .prepare(`UPDATE generations SET state = 'ready', key = ?, secondary_key = ?, blob_id = ?,
187 + value_size = ?, blob_size = ?, charged_bytes = ?,
188 + expires_at = unixepoch() + (SELECT retention_seconds FROM scopes WHERE scope_id = generations.scope_id),
189 + gc_after = unixepoch() + (SELECT retention_seconds FROM scopes WHERE scope_id = generations.scope_id)
190 + WHERE generation_id = ? AND state = 'uploading' AND lease_until > unixepoch() AND token_exp > unixepoch()
191 + AND charged_bytes >= ? AND EXISTS (
192 + SELECT 1 FROM scopes s, deployment d WHERE s.scope_id = generations.scope_id
193 + AND s.policy_version = generations.policy_version AND s.enabled = 1 AND s.writes_enabled = 1
194 + AND d.enabled = 1 AND d.writes_enabled = 1 AND s.charged_bytes <= s.byte_limit AND d.charged_bytes <= d.byte_limit
195 + ) RETURNING generation_id`)
196 + .bind(
197 + binary(key),
198 + binary(secondary),
199 + generation.blob_id,
200 + generation.value_size,
201 + generation.blob_size,
202 + actual,
203 + generation.generation_id,
204 + actual,
205 + ),
206 + ]),
207 + stats,
208 + );
209 + } catch {
210 + unavailable();
211 + }
212 + if (results[0]?.results.length !== 1) throw new HttpError(503, 'publication_guard');
213 +}
214 +
215 +export async function abandon(db: D1Database, id: string): Promise<void> {
216 + // Keep charges during the grace period for R2 operations that finish late.
217 + await db
218 + .prepare(`UPDATE generations SET lease_until = min(lease_until, unixepoch()), gc_after = min(gc_after, unixepoch() + ?)
219 + WHERE generation_id = ? AND state = 'uploading'`)
220 + .bind(GRACE_SECONDS, id)
221 + .run();
222 +}
223 +
224 +export async function cleanup(env: Env): Promise<void> {
225 + const limit = Number(env.GC_BATCH_SIZE);
226 + if (!Number.isInteger(limit) || limit < 1 || limit > 256)
227 + throw new Error('Invalid GC batch size');
228 + const claim = crypto.randomUUID();
229 + const claimed =
230 + await env.INDEX.prepare(`UPDATE generations SET state = 'deleting', gc_claim = ?, gc_after = unixepoch() + 600
231 + WHERE generation_id IN (SELECT generation_id FROM generations WHERE gc_after <= unixepoch() ORDER BY gc_after LIMIT ?)
232 + RETURNING generation_id, value_object, blob_object, upload_id`)
233 + .bind(claim, limit)
234 + .all<{
235 + generation_id: string;
236 + value_object: string;
237 + blob_object: string;
238 + upload_id: string | null;
239 + }>();
240 + let deleted = 0;
241 + for (const generation of claimed.results) {
242 + try {
243 + if (generation.upload_id)
244 + await env.ARTIFACTS.resumeMultipartUpload(
245 + generation.blob_object,
246 + generation.upload_id,
247 + ).abort();
248 + await env.ARTIFACTS.delete([generation.value_object, generation.blob_object]);
249 + // FK deletion only removes entries still selecting this generation.
250 + await env.INDEX.prepare(
251 + `DELETE FROM generations WHERE generation_id = ? AND state = 'deleting' AND gc_claim = ?`,
252 + )
253 + .bind(generation.generation_id, claim)
254 + .run();
255 + deleted++;
256 + } catch {
257 + console.warn(
258 + JSON.stringify({
259 + operation: 'cleanup',
260 + error: 'deletion_failed',
261 + generation: generation.generation_id,
262 + }),
263 + );
264 + }
265 + }
266 + await cleanAssociations(env.INDEX, limit);
267 + console.log(JSON.stringify({ operation: 'cleanup', claimed: claimed.results.length, deleted }));
268 +}
269 +
270 +async function cleanAssociations(db: D1Database, limit: number): Promise<void> {
271 + // Cursor bounds scanning as well as deletion, even when all associations are live.
272 + const batch = await db
273 + .prepare(`SELECT scope_id, secondary_key FROM associations
274 + WHERE (scope_id, secondary_key) > (SELECT scope_id, secondary_key FROM maintenance WHERE id = 1)
275 + ORDER BY scope_id, secondary_key LIMIT ?`)
276 + .bind(limit)
277 + .all<{ scope_id: string; secondary_key: number[] }>();
278 + const last = batch.results.at(-1);
279 + await db.batch([
280 + db
281 + .prepare(`DELETE FROM associations WHERE (scope_id, secondary_key) IN (
282 + SELECT scope_id, secondary_key FROM associations
283 + WHERE (scope_id, secondary_key) > (SELECT scope_id, secondary_key FROM maintenance WHERE id = 1)
284 + ORDER BY scope_id, secondary_key LIMIT ?)
285 + AND NOT EXISTS (SELECT 1 FROM entries e WHERE e.scope_id = associations.scope_id AND e.key = associations.target_key)`)
286 + .bind(limit),
287 + db
288 + .prepare('UPDATE maintenance SET scope_id = ?, secondary_key = ? WHERE id = 1')
289 + .bind(last?.scope_id ?? '', binary(new Uint8Array(last?.secondary_key ?? []))),
290 + ]);
291 +}
infra/task-cache/vendor/vite-plus-remote-cache-cloudflare/src/errors.ts new
+42
@@ -0,0 +1,42 @@
1 +export class HttpError extends Error {
2 + constructor(
3 + public status: number,
4 + public code: string,
5 + ) {
6 + super(code);
7 + }
8 +}
9 +
10 +export function badRequest(): never {
11 + throw new HttpError(400, 'invalid_request');
12 +}
13 +
14 +export function tooLarge(): never {
15 + throw new HttpError(413, 'size_limit');
16 +}
17 +
18 +export function unavailable(): never {
19 + throw new HttpError(503, 'unavailable');
20 +}
21 +
22 +export function errorResponse(error: unknown): Response {
23 + const status = error instanceof HttpError ? error.status : 500;
24 + const messages: Record<number, string> = {
25 + 400: 'Invalid request',
26 + 401: 'Invalid credentials',
27 + 403: 'Write not permitted',
28 + 404: 'Not found',
29 + 413: 'Request too large',
30 + 429: 'Rate limit exceeded',
31 + 500: 'Operation failed',
32 + 503: 'Service unavailable',
33 + };
34 + return new Response(messages[status] ?? messages[500], {
35 + status,
36 + headers: {
37 + 'Content-Type': 'text/plain; charset=utf-8',
38 + 'Cache-Control': 'no-store',
39 + ...(status === 429 || status === 503 ? { 'Retry-After': '60' } : {}),
40 + },
41 + });
42 +}
infra/task-cache/vendor/vite-plus-remote-cache-cloudflare/src/fetch.ts new
+42
@@ -0,0 +1,42 @@
1 +import { decodeEnvelope } from './cbor.ts';
2 +import { selectEntry } from './database.ts';
3 +import { badRequest, HttpError, unavailable } from './errors.ts';
4 +import type { Limits } from './limits.ts';
5 +import { parameters } from './multipart.ts';
6 +import type { Observations } from './observations.ts';
7 +import { contentLength, Deadline, readBody } from './streams.ts';
8 +
9 +type FetchResult =
10 + | { kind: 'exact'; value: Uint8Array; blob_id: string | null }
11 + | { kind: 'fallback'; key: Uint8Array };
12 +
13 +export async function fetchMetadata(
14 + request: Request,
15 + env: Pick<Env, 'INDEX' | 'ARTIFACTS'>,
16 + scope: string,
17 + limits: Limits,
18 + deadline: Deadline,
19 + stats: Observations,
20 +): Promise<FetchResult> {
21 + if (parameters(request.headers.get('Content-Type') ?? '').type !== 'application/cbor')
22 + badRequest();
23 + contentLength(request, limits.fetch);
24 + const body = await readBody(request.body, limits.fetch, deadline);
25 + stats.request_bytes = body.length;
26 + const data = decodeEnvelope(body, false, limits);
27 + const selected = await deadline.run(
28 + selectEntry(env.INDEX, scope, data.key, data.secondary_key, stats),
29 + );
30 + if (!selected) throw new HttpError(404, 'miss');
31 + // Fallbacks only identify the associated key. They must not depend on R2 availability.
32 + if (selected.kind === 'fallback') return { kind: 'fallback', key: new Uint8Array(selected.key) };
33 +
34 + stats.r2_operations++;
35 + const object = await deadline
36 + .run(env.ARTIFACTS.get(selected.value_object))
37 + .catch(() => unavailable());
38 + if (!object || object.size !== selected.value_size || object.size > limits.value) unavailable();
39 + const value = await readBody(object.body, limits.value, deadline).catch(() => unavailable());
40 + if (value.length !== selected.value_size) unavailable();
41 + return { kind: 'exact', value, blob_id: selected.blob_id };
42 +}
infra/task-cache/vendor/vite-plus-remote-cache-cloudflare/src/index.ts new
+130
@@ -0,0 +1,130 @@
1 +import { authorize, githubKeys, type WriteIdentity } from './auth.ts';
2 +import { cborResponse } from './cbor.ts';
3 +import { cleanup, getScope, selectBlob } from './database.ts';
4 +import { errorResponse, HttpError, unavailable } from './errors.ts';
5 +import { fetchMetadata } from './fetch.ts';
6 +import { limitsFrom } from './limits.ts';
7 +import { store } from './store.ts';
8 +import { Deadline } from './streams.ts';
9 +import { Admission } from './admission.ts';
10 +import { Observations } from './observations.ts';
11 +
12 +const keys = githubKeys();
13 +const admission = new Admission();
14 +
15 +export default {
16 + async fetch(request, env, ctx) {
17 + const started = Date.now();
18 + const id = crypto.randomUUID();
19 + const stats = new Observations();
20 + let scopeId = 'unknown';
21 + let operation = 'unknown';
22 + let outcome = 'error';
23 + let identity: WriteIdentity | undefined;
24 + let deadline: Deadline | undefined;
25 + let release: (() => void) | undefined;
26 + let response: Response;
27 + try {
28 + const limits = limitsFrom(env.LIMITS);
29 + const url = new URL(request.url);
30 + deadline = new Deadline(
31 + url.pathname.endsWith('/store') ? limits.deadlineMs : Math.min(limits.deadlineMs, 15000),
32 + request.signal,
33 + );
34 + const route =
35 + /^\/projects\/([a-z0-9][a-z0-9-]{0,62})\/(fetch|store|blob\/([0-9a-f-]{36}))$/.exec(
36 + url.pathname,
37 + );
38 + const namespaces: unknown = JSON.parse(env.NAMESPACES);
39 + if (
40 + !Array.isArray(namespaces) ||
41 + namespaces.length > 100 ||
42 + namespaces.some((value) => typeof value !== 'string')
43 + )
44 + throw new Error('Invalid namespaces');
45 + const known = route && namespaces.includes(route[1]);
46 + if (known) {
47 + scopeId = route[1]!;
48 + operation = route[2]!.startsWith('blob/') ? 'blob' : route[2]!;
49 + }
50 + const limiter =
51 + operation === 'store' || url.pathname.endsWith('/store')
52 + ? env.STORE_LIMITER
53 + : env.READ_LIMITER;
54 + const rateLimit = await deadline.run(
55 + limiter.limit({ key: known ? `${scopeId}:${operation}` : 'unknown' }),
56 + );
57 + if (!rateLimit.success) throw new HttpError(429, 'rate_limit');
58 + if (!known || url.search || request.method !== (operation === 'blob' ? 'GET' : 'POST'))
59 + throw new HttpError(404, 'unknown_route');
60 + release = admission.acquire(operation === 'store');
61 + const scope = await deadline.run(getScope(env.INDEX, scopeId, stats));
62 + if (operation === 'store') {
63 + identity = await deadline.run(authorize(request, scope, keys));
64 + response = await store(request, env, ctx, scope, identity, limits, deadline, stats);
65 + outcome = 'stored';
66 + } else if (operation === 'fetch') {
67 + const result = await fetchMetadata(request, env, scopeId, limits, deadline, stats);
68 + response = cborResponse(result);
69 + outcome = result.kind;
70 + } else {
71 + const selected = await deadline.run(selectBlob(env.INDEX, scopeId, route[3]!, stats));
72 + if (!selected) throw new HttpError(404, 'blob_missing');
73 + stats.r2_operations++;
74 + const object = await deadline
75 + .run(env.ARTIFACTS.get(selected.blob_object))
76 + .catch(() => unavailable());
77 + if (!object) throw new HttpError(404, 'blob_missing');
78 + if (object.size !== selected.blob_size) unavailable();
79 + response = new Response(object.body, {
80 + headers: {
81 + 'Content-Type': 'application/octet-stream',
82 + 'Content-Length': String(object.size),
83 + 'Cache-Control': 'no-store',
84 + },
85 + });
86 + outcome = 'blob';
87 + }
88 + } catch (error) {
89 + response = errorResponse(error);
90 + if (error instanceof HttpError) outcome = error.code;
91 + } finally {
92 + deadline?.dispose();
93 + release?.();
94 + }
95 + response.headers.set('X-Request-Id', id);
96 + response.headers.set('X-Remote-Cache-Deployment', env.DEPLOYMENT_ID ?? 'local');
97 + const sample = Math.min(1, Math.max(0, Number(env.LOG_SAMPLE_RATE) || 0));
98 + // Fixed sampling bounds error volume as well as successful request volume.
99 + if (Math.random() < sample)
100 + console.log(
101 + JSON.stringify({
102 + request_id: id,
103 + scope: scopeId,
104 + operation,
105 + status: response.status,
106 + outcome,
107 + ...(response.status >= 400 ? { error_class: outcome } : {}),
108 + response_bytes: Number(response.headers.get('Content-Length') ?? 0),
109 + duration_ms: Date.now() - started,
110 + ...stats.toJSON(),
111 + ...(stats.d1_storage_bytes >= 400000000 ? { warning: 'd1_storage_400mb' } : {}),
112 + ...(identity
113 + ? {
114 + repository_id: identity.repository_id,
115 + workflow_ref: identity.workflow_ref,
116 + run_id: identity.run_id,
117 + run_attempt: identity.run_attempt,
118 + sha: identity.sha,
119 + event_name: identity.event_name,
120 + head_ref: identity.head_ref,
121 + }
122 + : {}),
123 + }),
124 + );
125 + return response;
126 + },
127 + async scheduled(_event, env) {
128 + await cleanup(env);
129 + },
130 +} satisfies ExportedHandler<Env>;
infra/task-cache/vendor/vite-plus-remote-cache-cloudflare/src/limits.ts new
+42
@@ -0,0 +1,42 @@
1 +export const MiB = 1024 * 1024;
2 +export const PART_SIZE = 5 * MiB;
3 +export const LEASE_SECONDS = 15 * 60;
4 +export const GRACE_SECONDS = 10 * 60;
5 +
6 +export const defaults = {
7 + key: 16 * 1024,
8 + value: 4 * MiB,
9 + metadata: 5 * MiB,
10 + fetch: 40 * 1024,
11 + blob: 64 * MiB,
12 + store: 72 * MiB,
13 + headers: 8 * 1024,
14 + deadlineMs: 120_000,
15 +};
16 +export type Limits = typeof defaults;
17 +
18 +export function limitsFrom(raw: string): Limits {
19 + const input: unknown = JSON.parse(raw);
20 + if (!input || typeof input !== 'object' || Array.isArray(input))
21 + throw new Error('Invalid limits');
22 + const result = { ...defaults };
23 + for (const [name, value] of Object.entries(input)) {
24 + if (
25 + !(name in defaults) ||
26 + !Number.isSafeInteger(value) ||
27 + value <= 0 ||
28 + value > defaults[name as keyof Limits]
29 + ) {
30 + throw new Error('Limits must be positive integers no larger than the tested defaults');
31 + }
32 + result[name as keyof Limits] = value;
33 + }
34 + if (
35 + result.metadata < 2 * result.key + result.value + 128 ||
36 + result.fetch < 2 * result.key + 128 ||
37 + result.store < result.metadata + result.blob + 1024
38 + ) {
39 + throw new Error('Envelope limits must accommodate field limits and framing');
40 + }
41 + return result;
42 +}
infra/task-cache/vendor/vite-plus-remote-cache-cloudflare/src/multipart.ts new
+120
@@ -0,0 +1,120 @@
1 +import { Buffer } from 'node:buffer';
2 +import { badRequest, tooLarge } from './errors.ts';
3 +import { collect, Input } from './streams.ts';
4 +
5 +export function parameters(raw: string): { type: string; params: Map<string, string> } {
6 + const separator = raw.indexOf(';');
7 + const type = (separator === -1 ? raw : raw.slice(0, separator)).trim().toLowerCase();
8 + let rest = separator === -1 ? '' : raw.slice(separator);
9 + const params = new Map<string, string>();
10 + while (rest.length) {
11 + const match = /^;\s*([\w-]+)\s*=\s*(?:"((?:[^"\\\r\n]|\\[^\r\n])*)"|([^\s;]+))\s*/.exec(rest);
12 + if (!match) badRequest();
13 + const name = match[1]!.toLowerCase();
14 + if (params.has(name)) badRequest();
15 + params.set(name, match[2] !== undefined ? match[2].replace(/\\(.)/g, '$1') : match[3]!);
16 + rest = rest.slice(match[0].length);
17 + }
18 + return { type, params };
19 +}
20 +
21 +export function boundaryFrom(contentType: string | null): string {
22 + const { type, params } = parameters(contentType ?? '');
23 + const boundary = params.get('boundary');
24 + if (
25 + type !== 'multipart/form-data' ||
26 + !boundary ||
27 + boundary.length > 70 ||
28 + !/^[0-9A-Za-z'()+_,./:=? -]+$/.test(boundary) ||
29 + boundary.endsWith(' ')
30 + )
31 + badRequest();
32 + return boundary;
33 +}
34 +
35 +export async function* multipart(
36 + input: Input,
37 + boundary: string,
38 + headerLimit: number,
39 +): AsyncGenerator<{
40 + name: 'metadata' | 'blob';
41 + body: AsyncGenerator<Uint8Array>;
42 +}> {
43 + let preamble = 0;
44 + while (true) {
45 + const line = await input.until(Buffer.from('\r\n'), headerLimit);
46 + preamble += line.length + 2;
47 + if (preamble > headerLimit) tooLarge();
48 + if (line.toString('utf8').replace(/[ \t]+$/, '') === `--${boundary}`) break;
49 + }
50 + const delimiter = Buffer.from(`\r\n--${boundary}`);
51 + const seen = new Set<string>();
52 + let closed = false;
53 + while (!closed) {
54 + if (seen.size >= 2) badRequest();
55 + const raw = await input.until(Buffer.from('\r\n\r\n'), headerLimit);
56 + const headers = new Map<string, string>();
57 + for (const line of raw.toString('utf8').split('\r\n')) {
58 + const match = /^([!#$%&'*+.^_`|~\w-]+):[ \t]*([^\r\n]*)$/.exec(line);
59 + if (!match) badRequest();
60 + const name = match[1]!.toLowerCase();
61 + if (headers.has(name)) badRequest();
62 + headers.set(name, match[2]!.trim());
63 + }
64 + const disposition = parameters(headers.get('content-disposition') ?? '');
65 + const name = disposition.params.get('name');
66 + if (
67 + disposition.type !== 'form-data' ||
68 + (name !== 'metadata' && name !== 'blob') ||
69 + seen.has(name)
70 + )
71 + badRequest();
72 + if (headers.has('content-transfer-encoding')) badRequest();
73 + const expected = name === 'metadata' ? 'application/cbor' : 'application/octet-stream';
74 + if (parameters(headers.get('content-type') ?? '').type !== expected) badRequest();
75 + seen.add(name);
76 + let consumed = false;
77 + async function* body(): AsyncGenerator<Uint8Array> {
78 + while (true) {
79 + let index = input.buffer.indexOf(delimiter);
80 + while (index >= 0) {
81 + const match = await delimiterEnd(input, index + delimiter.length, headerLimit);
82 + if (match) {
83 + if (index) yield input.take(index);
84 + input.take(match.end - index);
85 + closed = match.closed;
86 + consumed = true;
87 + return;
88 + }
89 + index = input.buffer.indexOf(delimiter, index + 1);
90 + }
91 + const safe = input.buffer.length - delimiter.length - 2;
92 + if (safe > 0) yield input.take(safe);
93 + if (!(await input.fill())) badRequest();
94 + }
95 + }
96 + yield { name, body: body() };
97 + if (!consumed) throw new Error('Multipart consumer must drain each part');
98 + }
99 + if (!seen.has('metadata')) badRequest();
100 + await collect(input.rest(), headerLimit);
101 +}
102 +
103 +async function delimiterEnd(
104 + input: Input,
105 + start: number,
106 + limit: number,
107 +): Promise<{ end: number; closed: boolean } | null> {
108 + while (input.buffer.length < start + 2 && (await input.fill())) {}
109 + const closed = input.buffer[start] === 45 && input.buffer[start + 1] === 45;
110 + let end = start + (closed ? 2 : 0);
111 + while (true) {
112 + while (input.buffer.length < end + 2 && (await input.fill())) {}
113 + if (input.buffer[end] !== 32 && input.buffer[end] !== 9) break;
114 + if (++end - start > limit) tooLarge();
115 + }
116 + if (input.buffer[end] === 13 && input.buffer[end + 1] === 10) return { end: end + 2, closed };
117 + if (closed && input.ended && end === input.buffer.length) return { end, closed };
118 + // A boundary prefix followed by arbitrary bytes is still part of the opaque body.
119 + return null;
120 +}
infra/task-cache/vendor/vite-plus-remote-cache-cloudflare/src/observations.ts new
+42
@@ -0,0 +1,42 @@
1 +// @effect-diagnostics globalDate:off - vendored upstream protocol, not an Effect service.
2 +export class Observations {
3 + request_bytes = 0;
4 + d1_rows_read = 0;
5 + d1_rows_written = 0;
6 + d1_ms = 0;
7 + d1_queries = 0;
8 + d1_storage_bytes = 0;
9 + r2_operations = 0;
10 +
11 + toJSON() {
12 + return {
13 + request_bytes: this.request_bytes,
14 + d1_rows_read: this.d1_rows_read,
15 + d1_rows_written: this.d1_rows_written,
16 + d1_ms: this.d1_ms,
17 + d1_queries: this.d1_queries,
18 + d1_storage_bytes: this.d1_storage_bytes,
19 + r2_operations: this.r2_operations,
20 + };
21 + }
22 +}
23 +
24 +export async function measured<T extends D1Result | D1Result[]>(
25 + operation: Promise<T>,
26 + stats?: Observations,
27 +): Promise<T> {
28 + const start = Date.now();
29 + try {
30 + const result = await operation;
31 + if (stats)
32 + for (const item of Array.isArray(result) ? result : [result]) {
33 + stats.d1_rows_read += item.meta.rows_read;
34 + stats.d1_rows_written += item.meta.rows_written;
35 + stats.d1_storage_bytes = item.meta.size_after;
36 + stats.d1_queries++;
37 + }
38 + return result;
39 + } finally {
40 + if (stats) stats.d1_ms += Date.now() - start;
41 + }
42 +}
infra/task-cache/vendor/vite-plus-remote-cache-cloudflare/src/store.ts new
+131
@@ -0,0 +1,131 @@
1 +// @effect-diagnostics cryptoRandomUUID:off globalConsole:off - vendored upstream protocol, not an Effect service.
2 +import { cborResponse, decodeEnvelope, type StoreMetadata } from './cbor.ts';
3 +import {
4 + abandon,
5 + publish,
6 + recordMultipart,
7 + reserve,
8 + type Generation,
9 + type Scope,
10 +} from './database.ts';
11 +import { badRequest, tooLarge, unavailable } from './errors.ts';
12 +import { PART_SIZE, type Limits } from './limits.ts';
13 +import { boundaryFrom, multipart } from './multipart.ts';
14 +import { collect, contentLength, Deadline, Input } from './streams.ts';
15 +import type { WriteIdentity } from './auth.ts';
16 +import type { Observations } from './observations.ts';
17 +
18 +async function uploadBlob(
19 + env: Env,
20 + generation: Generation,
21 + source: AsyncIterable<Uint8Array>,
22 + limit: number,
23 + deadline: Deadline,
24 + stats: Observations,
25 +): Promise<void> {
26 + let buffer = new Uint8Array(PART_SIZE);
27 + let used = 0;
28 + let upload: R2MultipartUpload | undefined;
29 + const parts: R2UploadedPart[] = [];
30 + for await (const chunk of source) {
31 + generation.blob_size += chunk.length;
32 + if (generation.blob_size > limit) tooLarge();
33 + let offset = 0;
34 + while (offset < chunk.length) {
35 + if (used === PART_SIZE) {
36 + if (!upload) {
37 + stats.r2_operations++;
38 + upload = await deadline.run(env.ARTIFACTS.createMultipartUpload(generation.blob_object));
39 + try {
40 + await deadline.run(recordMultipart(env.INDEX, generation, upload.uploadId, stats));
41 + } catch (error) {
42 + // Also cover creation succeeding but D1 failing to record its ID.
43 + try {
44 + await deadline.run(upload.abort());
45 + } catch {
46 + /* Lifecycle is the final backstop. */
47 + }
48 + throw error;
49 + }
50 + }
51 + stats.r2_operations++;
52 + parts.push(await deadline.run(upload.uploadPart(parts.length + 1, buffer)));
53 + buffer = new Uint8Array(PART_SIZE);
54 + used = 0;
55 + }
56 + const size = Math.min(PART_SIZE - used, chunk.length - offset);
57 + buffer.set(chunk.subarray(offset, offset + size), used);
58 + used += size;
59 + offset += size;
60 + }
61 + }
62 + if (upload) {
63 + stats.r2_operations += Number(used > 0) + 1;
64 + if (used)
65 + parts.push(await deadline.run(upload.uploadPart(parts.length + 1, buffer.subarray(0, used))));
66 + await deadline.run(upload.complete(parts));
67 + } else {
68 + stats.r2_operations++;
69 + const result = await deadline.run(
70 + env.ARTIFACTS.put(generation.blob_object, buffer.subarray(0, used)),
71 + );
72 + if (!result) unavailable();
73 + }
74 + generation.blob_id = crypto.randomUUID();
75 +}
76 +
77 +export async function store(
78 + request: Request,
79 + env: Env,
80 + ctx: Pick<ExecutionContext, 'waitUntil'>,
81 + scope: Scope,
82 + identity: WriteIdentity,
83 + limits: Limits,
84 + deadline: Deadline,
85 + stats: Observations,
86 +): Promise<Response> {
87 + const boundary = boundaryFrom(request.headers.get('Content-Type'));
88 + const length = contentLength(request, limits.store);
89 + const generation = await deadline.run(
90 + reserve(env.INDEX, scope, identity.exp, length ?? limits.store, stats),
91 + );
92 + let input: Input | undefined;
93 + try {
94 + input = new Input(request.body, Math.min(length ?? limits.store, limits.store), deadline);
95 + let metadata: StoreMetadata | undefined;
96 + for await (const part of multipart(input, boundary, limits.headers)) {
97 + if (part.name === 'metadata') {
98 + metadata = decodeEnvelope(await collect(part.body, limits.metadata), true, limits);
99 + generation.value_size = metadata.value.length;
100 + stats.r2_operations++;
101 + const result = await deadline.run(
102 + env.ARTIFACTS.put(generation.value_object, metadata.value),
103 + );
104 + if (!result) unavailable();
105 + } else {
106 + await uploadBlob(env, generation, part.body, limits.blob, deadline, stats);
107 + }
108 + }
109 + if (!metadata || (length !== null && input.bytes !== length)) badRequest();
110 + deadline.check();
111 + await deadline.run(publish(env.INDEX, generation, metadata.key, metadata.secondary_key, stats));
112 + return cborResponse({ blob_id: generation.blob_id });
113 + } catch (error) {
114 + // Publication is never deferred. Only cleanup can continue after the response.
115 + ctx.waitUntil(
116 + abandon(env.INDEX, generation.generation_id).catch(() => {
117 + console.warn(
118 + JSON.stringify({
119 + operation: 'cleanup',
120 + error: 'abandon_failed',
121 + generation: generation.generation_id,
122 + }),
123 + );
124 + }),
125 + );
126 + throw error;
127 + } finally {
128 + stats.request_bytes = input?.bytes ?? 0;
129 + input?.close();
130 + }
131 +}
infra/task-cache/vendor/vite-plus-remote-cache-cloudflare/src/streams.ts new
+136
@@ -0,0 +1,136 @@
1 +// @effect-diagnostics globalTimers:off - vendored upstream protocol, not an Effect service.
2 +import { Buffer } from 'node:buffer';
3 +import { badRequest, HttpError, tooLarge } from './errors.ts';
4 +
5 +export class Deadline {
6 + private controller = new AbortController();
7 + private timer: ReturnType<typeof setTimeout>;
8 + private cancelled: Promise<never>;
9 + private onAbort = () => this.controller.abort();
10 + constructor(
11 + ms: number,
12 + private signal?: AbortSignal,
13 + ) {
14 + this.timer = setTimeout(this.onAbort, ms);
15 + signal?.addEventListener('abort', this.onAbort, { once: true });
16 + this.cancelled = new Promise((_, reject) =>
17 + this.controller.signal.addEventListener(
18 + 'abort',
19 + () => reject(new HttpError(503, 'deadline')),
20 + { once: true },
21 + ),
22 + );
23 + // Cancellation can precede the first awaited operation.
24 + void this.cancelled.catch(() => {});
25 + if (signal?.aborted) this.onAbort();
26 + }
27 + check(): void {
28 + if (this.controller.signal.aborted) throw new HttpError(503, 'deadline');
29 + }
30 + async run<T>(operation: Promise<T>): Promise<T> {
31 + this.check();
32 + return Promise.race([operation, this.cancelled]);
33 + }
34 + dispose(): void {
35 + clearTimeout(this.timer);
36 + this.signal?.removeEventListener('abort', this.onAbort);
37 + }
38 +}
39 +
40 +export async function collect(
41 + source: AsyncIterable<Uint8Array>,
42 + limit: number,
43 +): Promise<Uint8Array<ArrayBuffer>> {
44 + let size = 0;
45 + // Fixed-capacity accumulation also bounds bookkeeping for one-byte chunks.
46 + const buffer = new Uint8Array(limit);
47 + for await (const chunk of source) {
48 + if (chunk.length > limit - size) tooLarge();
49 + buffer.set(chunk, size);
50 + size += chunk.length;
51 + }
52 + return buffer.subarray(0, size);
53 +}
54 +
55 +export class Input {
56 + private reader: ReadableStreamDefaultReader<Uint8Array>;
57 + private pending: Uint8Array = new Uint8Array(0);
58 + private offset = 0;
59 + bytes = 0;
60 + ended = false;
61 + buffer: Buffer = Buffer.alloc(0);
62 + constructor(
63 + body: ReadableStream<Uint8Array> | null,
64 + private max: number,
65 + private deadline: Deadline,
66 + ) {
67 + if (!body) badRequest();
68 + this.reader = body.getReader();
69 + }
70 + async fill(): Promise<boolean> {
71 + if (this.ended) return false;
72 + if (this.offset === this.pending.length) {
73 + const result = await this.deadline.run(this.reader.read());
74 + if (result.done) {
75 + this.ended = true;
76 + return false;
77 + }
78 + this.bytes += result.value.length;
79 + if (this.bytes > this.max) tooLarge();
80 + this.pending = result.value;
81 + this.offset = 0;
82 + }
83 + const next = this.pending.subarray(this.offset, this.offset + 64 * 1024);
84 + this.offset += next.length;
85 + this.buffer = Buffer.concat([this.buffer, next]);
86 + return true;
87 + }
88 + take(size: number): Buffer {
89 + const result = this.buffer.subarray(0, size);
90 + this.buffer = this.buffer.subarray(size);
91 + return result;
92 + }
93 + async until(delimiter: Buffer, limit: number): Promise<Buffer> {
94 + while (true) {
95 + const index = this.buffer.indexOf(delimiter);
96 + if (index >= 0) {
97 + if (index > limit) tooLarge();
98 + const value = this.take(index);
99 + this.take(delimiter.length);
100 + return value;
101 + }
102 + if (this.buffer.length > limit + delimiter.length) tooLarge();
103 + if (!(await this.fill())) badRequest();
104 + }
105 + }
106 + async *rest(): AsyncGenerator<Uint8Array> {
107 + do {
108 + if (this.buffer.length) yield this.take(this.buffer.length);
109 + } while (await this.fill());
110 + }
111 + close(): void {
112 + void this.reader.cancel().catch(() => {});
113 + }
114 +}
115 +
116 +export async function readBody(
117 + body: ReadableStream<Uint8Array> | null,
118 + limit: number,
119 + deadline: Deadline,
120 +): Promise<Uint8Array<ArrayBuffer>> {
121 + const input = new Input(body, limit, deadline);
122 + try {
123 + return await collect(input.rest(), limit);
124 + } finally {
125 + input.close();
126 + }
127 +}
128 +
129 +export function contentLength(request: Request, maximum: number): number | null {
130 + const raw = request.headers.get('Content-Length');
131 + if (raw === null) return null;
132 + if (!/^\d+$/.test(raw)) badRequest();
133 + const length = Number(raw);
134 + if (!Number.isSafeInteger(length) || length > maximum) tooLarge();
135 + return length;
136 +}
knip.jsonc
+13
@@ -115,6 +115,19 @@
115 // React Native codegen discovers component specifications by filename.
116 "entry": ["src/*NativeComponent.ts!"],
117 },
118 + "infra/task-cache": {
119 + "ignore": ["vendor/**"],
120 + // The bootstrap and Vite+ hook are loaded by shell/package configuration.
121 + "entry": [
122 + "alchemy.run.ts!",
123 + "scripts/install.ts",
124 + "scripts/install-ci.ts",
125 + "src/runHook.ts",
126 + "src/flush.ts",
127 + "src/nativeRunHook.js",
128 + "src/preload.mjs",
129 + ],
130 + },
131 "infra/relay": {
132 "entry": ["alchemy.run.ts!", "src/persistence/schema.ts!"],
133 },
pnpm-lock.yaml
+105
@@ -1303,6 +1303,37 @@ importers:
1303 specifier: 1.3.0
1304 version: 1.3.0(@astrojs/compiler-rs@0.5.1)(@tsc-rs/typescript@0.8.0)(@types/node@24.12.4)(@vitest/browser-playwright@5.0.5)(@vitest/ui@5.0.5)(@volar/typescript@2.4.28(@tsc-rs/typescript@0.8.0))(bufferutil@4.1.0)(esbuild@0.28.2)(jiti@2.7.0)(jsdom@30.1.0(@noble/hashes@1.8.0))(playwright@1.60.0)(terser@5.48.0)(unrun@0.2.39)(utf-8-validate@6.0.6)(yaml@2.9.0)
1305
1306 + infra/task-cache:
1307 + dependencies:
1308 + alchemy:
1309 + specifier: 2.0.0-beta.80
1310 + version: 2.0.0-beta.80(patch_hash=bea1b6c0c0b23157fd5f439a1d7feff430f28ac1621ba135d8a13eeec60ccacd)(c74a1cfb3df8b67fc508b7f3e6f07baa)
1311 + effect:
1312 + specifier: 4.0.5
1313 + version: 4.0.5
1314 + jose:
1315 + specifier: 'catalog:'
1316 + version: 6.2.2
1317 + devDependencies:
1318 + '@cloudflare/workers-types':
1319 + specifier: ^4.20260601.1
1320 + version: 4.20260604.1
1321 + '@effect/platform-node':
1322 + specifier: 4.0.5
1323 + version: 4.0.5(bufferutil@4.1.0)(effect@4.0.5)(redis@6.2.1)(utf-8-validate@6.0.6)
1324 + '@effect/vitest':
1325 + specifier: 4.0.5
1326 + version: 4.0.5(patch_hash=2731c9c3761c595f25ddc625a7d61ca376b389d5b3049ecb837c8940f21cfa02)(@astrojs/compiler-rs@0.5.1)(@tsc-rs/typescript@0.8.0)(@types/node@24.12.4)(@vitest/browser-playwright@5.0.5)(@vitest/ui@5.0.5)(@volar/typescript@2.4.28(@tsc-rs/typescript@0.8.0))(bufferutil@4.1.0)(effect@4.0.5)(esbuild@0.28.2)(jiti@2.7.0)(jsdom@30.1.0(@noble/hashes@1.8.0))(playwright@1.60.0)(terser@5.48.0)(unrun@0.2.39)(utf-8-validate@6.0.6)(yaml@2.9.0)
1327 + '@types/node':
1328 + specifier: 24.12.4
1329 + version: 24.12.4
1330 + typescript:
1331 + specifier: 'catalog:'
1332 + version: '@tsc-rs/typescript@0.8.0'
1333 + vite-plus:
1334 + specifier: 1.3.0
1335 + version: 1.3.0(@astrojs/compiler-rs@0.5.1)(@tsc-rs/typescript@0.8.0)(@types/node@24.12.4)(@vitest/browser-playwright@5.0.5)(@vitest/ui@5.0.5)(@volar/typescript@2.4.28(@tsc-rs/typescript@0.8.0))(bufferutil@4.1.0)(esbuild@0.28.2)(jiti@2.7.0)(jsdom@30.1.0(@noble/hashes@1.8.0))(playwright@1.60.0)(terser@5.48.0)(unrun@0.2.39)(utf-8-validate@6.0.6)(yaml@2.9.0)
1336 +
1337 oxlint-plugin-t3code:
1338 dependencies:
1339 '@effect/platform-node':
@@ -17047,6 +17078,64 @@ snapshots:
17078 - typescript
17079 - utf-8-validate
17080
17081 + alchemy@2.0.0-beta.80(patch_hash=bea1b6c0c0b23157fd5f439a1d7feff430f28ac1621ba135d8a13eeec60ccacd)(c74a1cfb3df8b67fc508b7f3e6f07baa):
17082 + dependencies:
17083 + '@alchemy.run/cloudflare-runtime': 2.0.0-beta.80(@distilled.cloud/cloudflare@1.0.0-rc.13(effect@4.0.5))(@effect/platform-node@4.0.5(bufferutil@4.1.0)(effect@4.0.5)(redis@6.2.1)(utf-8-validate@6.0.6))(@tsc-rs/typescript@0.8.0)(@types/node@24.12.4)(@voidzero-dev/vite-plus-core@1.3.0(@tsc-rs/typescript@0.8.0)(@types/node@24.12.4)(@volar/typescript@2.4.28(@tsc-rs/typescript@0.8.0))(esbuild@0.28.2)(jiti@2.7.0)(terser@5.48.0)(unrun@0.2.39)(yaml@2.9.0))(effect@4.0.5)(rolldown@1.2.5)
17084 + '@alchemy.run/floci': 2.0.0-beta.80(effect@4.0.5)
17085 + '@alchemy.run/node-utils': 2.0.0-beta.80
17086 + '@alchemy.run/sigil': 0.1.0-alpha.1(@types/react@19.3.0)(react-devtools-core@6.1.5(bufferutil@4.1.0)(utf-8-validate@6.0.6))
17087 + '@distilled.cloud/acme': 1.0.0-rc.13(effect@4.0.5)
17088 + '@distilled.cloud/aws': 1.0.0-rc.13(effect@4.0.5)
17089 + '@distilled.cloud/axiom': 1.0.0-rc.13(effect@4.0.5)
17090 + '@distilled.cloud/cloudflare': 1.0.0-rc.13(effect@4.0.5)
17091 + '@distilled.cloud/core': 1.0.0-rc.13(effect@4.0.5)
17092 + '@distilled.cloud/doppler': 1.0.0-rc.13(effect@4.0.5)
17093 + '@distilled.cloud/fly-io': 1.0.0-rc.13(effect@4.0.5)
17094 + '@distilled.cloud/gcp': 1.0.0-rc.13(effect@4.0.5)
17095 + '@distilled.cloud/hetzner': 1.0.0-rc.13(effect@4.0.5)
17096 + '@distilled.cloud/infisical': 1.0.0-rc.13(effect@4.0.5)
17097 + '@distilled.cloud/neon': 1.0.0-rc.13(effect@4.0.5)
17098 + '@distilled.cloud/planetscale': 1.0.0-rc.13(effect@4.0.5)
17099 + '@distilled.cloud/prisma': 1.0.0-rc.13(effect@4.0.5)
17100 + '@distilled.cloud/railway': 1.0.0-rc.13(effect@4.0.5)
17101 + '@distilled.cloud/stripe': 1.0.0-rc.13(effect@4.0.5)
17102 + '@distilled.cloud/zerossl': 1.0.0-rc.13(effect@4.0.5)
17103 + '@effect/sql-d1': 4.0.0(effect@4.0.5)
17104 + '@effect/sql-sqlite-do': 4.0.5(effect@4.0.5)
17105 + '@libsql/client': 0.17.3(bufferutil@4.1.0)(utf-8-validate@6.0.6)
17106 + '@neon/functions': 0.11.0(hono@4.13.7)
17107 + '@octokit/rest': 22.0.1
17108 + '@octokit/webhooks': 14.2.0
17109 + '@types/aws-lambda': 8.10.161
17110 + capnweb: 0.12.0
17111 + effect: 4.0.5
17112 + fflate: 0.8.3
17113 + libsodium-wrappers: 0.8.4
17114 + pathe: 2.0.3
17115 + picomatch: 4.0.7
17116 + rolldown: 1.2.5
17117 + tinyglobby: 0.2.17
17118 + yaml: 2.9.0
17119 + optionalDependencies:
17120 + '@effect/platform-node': 4.0.5(bufferutil@4.1.0)(effect@4.0.5)(redis@6.2.1)(utf-8-validate@6.0.6)
17121 + '@effect/sql-pg': 4.0.5(effect@4.0.5)
17122 + '@effect/vitest': 4.0.5(patch_hash=2731c9c3761c595f25ddc625a7d61ca376b389d5b3049ecb837c8940f21cfa02)(@astrojs/compiler-rs@0.5.1)(@tsc-rs/typescript@0.8.0)(@types/node@24.12.4)(@vitest/browser-playwright@5.0.5)(@vitest/ui@5.0.5)(@volar/typescript@2.4.28(@tsc-rs/typescript@0.8.0))(bufferutil@4.1.0)(effect@4.0.5)(esbuild@0.28.2)(jiti@2.7.0)(jsdom@30.1.0(@noble/hashes@1.8.0))(playwright@1.60.0)(terser@5.48.0)(unrun@0.2.39)(utf-8-validate@6.0.6)(yaml@2.9.0)
17123 + drizzle-kit: 1.0.0-rc.5-ab785fc
17124 + drizzle-orm: 1.0.0-rc.5-ab785fc(@cloudflare/workers-types@4.20260604.1)(@effect/sql-d1@4.0.0(effect@4.0.5))(@effect/sql-pg@4.0.5(effect@4.0.5))(@effect/sql-sqlite-bun@4.0.0-rc.112(effect@4.0.5))(@effect/sql-sqlite-do@4.0.5(effect@4.0.5))(@electric-sql/pglite@0.3.15)(@libsql/client@0.17.3(bufferutil@4.1.0)(utf-8-validate@6.0.6))(bun-types@1.3.14)(effect@4.0.5)(expo-sqlite@58.0.13(expo@58.0.14)(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0))(pg@8.23.0)(zod@4.6.5)
17125 + mongodb: 6.21.0(@aws-sdk/credential-providers@3.1062.0)(socks@2.8.9)
17126 + pg: 8.23.0
17127 + vite: '@voidzero-dev/vite-plus-core@1.3.0(@tsc-rs/typescript@0.8.0)(@types/node@24.12.4)(@volar/typescript@2.4.28(@tsc-rs/typescript@0.8.0))(esbuild@0.28.2)(jiti@2.7.0)(terser@5.48.0)(unrun@0.2.39)(yaml@2.9.0)'
17128 + ws: 8.22.0(bufferutil@4.1.0)(utf-8-validate@6.0.6)
17129 + transitivePeerDependencies:
17130 + - '@types/node'
17131 + - '@types/react'
17132 + - bufferutil
17133 + - hono
17134 + - proxy-agent
17135 + - react-devtools-core
17136 + - typescript
17137 + - utf-8-validate
17138 +
17139 alien-signals@2.0.6: {}
17140
17141 am-i-vibing@0.4.0:
@@ -18013,6 +18102,22 @@ snapshots:
18102 pg: 8.23.0
18103 zod: 4.4.3
18104
18105 + drizzle-orm@1.0.0-rc.5-ab785fc(@cloudflare/workers-types@4.20260604.1)(@effect/sql-d1@4.0.0(effect@4.0.5))(@effect/sql-pg@4.0.5(effect@4.0.5))(@effect/sql-sqlite-bun@4.0.0-rc.112(effect@4.0.5))(@effect/sql-sqlite-do@4.0.5(effect@4.0.5))(@electric-sql/pglite@0.3.15)(@libsql/client@0.17.3(bufferutil@4.1.0)(utf-8-validate@6.0.6))(bun-types@1.3.14)(effect@4.0.5)(expo-sqlite@58.0.13(expo@58.0.14)(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0))(pg@8.23.0)(zod@4.6.5):
18106 + optionalDependencies:
18107 + '@cloudflare/workers-types': 4.20260604.1
18108 + '@effect/sql-d1': 4.0.0(effect@4.0.5)
18109 + '@effect/sql-pg': 4.0.5(effect@4.0.5)
18110 + '@effect/sql-sqlite-bun': 4.0.0-rc.112(effect@4.0.5)
18111 + '@effect/sql-sqlite-do': 4.0.5(effect@4.0.5)
18112 + '@electric-sql/pglite': 0.3.15
18113 + '@libsql/client': 0.17.3(bufferutil@4.1.0)(utf-8-validate@6.0.6)
18114 + bun-types: 1.3.14
18115 + effect: 4.0.5
18116 + expo-sqlite: 58.0.13(expo@58.0.14)(react-native@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.88.0(@babel/core@7.29.7(supports-color@8.1.1))(bufferutil@4.1.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(supports-color@8.1.1)(utf-8-validate@6.0.6))(react@19.3.0)
18117 + pg: 8.23.0
18118 + zod: 4.6.5
18119 + optional: true
18120 +
18121 dset@3.1.4: {}
18122
18123 dunder-proto@1.0.1:
vite.config.ts
+15
@@ -1,6 +1,7 @@
1 import "vite-plus/test/config";
2 import { defineConfig } from "vite-plus";
3 import * as NodeURL from "node:url";
4 +import { remoteCacheUrl } from "./infra/task-cache/src/remoteCache.ts";
5
6 const DOM_TEST_ENVIRONMENT_ALLOWLIST: Array<string> = [];
7
@@ -78,7 +79,17 @@ const RESTRICTED_PULL_REQUEST_GLYPH_IMPORTS = {
79 "Pick a glyph by meaning from PullRequestGlyph in apps/web/src/components/pullRequest/pullRequestIcons.tsx so every surface draws the same pull request the same way.",
80 };
81
82 +// Vite+ supports remote configuration before its published types include it.
83 +const cacheUrl = remoteCacheUrl(import.meta.dirname);
84 +const taskCacheConfig = {
85 + scripts: false,
86 + ...(cacheUrl ? { remote: { url: cacheUrl } } : {}),
87 +};
88 +
89 export default defineConfig({
90 + run: {
91 + cache: taskCacheConfig,
92 + },
93 resolve: {
94 alias: {
95 "~": NodeURL.fileURLToPath(new URL("./apps/web/src", import.meta.url)),
@@ -86,6 +97,8 @@ export default defineConfig({
97 },
98 test: {
99 environment: "node",
100 + // Fixture subprocesses must not inherit the outer task runner’s cache policy.
101 + env: { VP_REMOTE_CACHE: "off", VP_REMOTE_CACHE_URL: "" },
102 exclude: TEST_EXCLUDE,
103 hookTimeout: 60_000,
104 testTimeout: 60_000,
@@ -141,6 +154,7 @@ export default defineConfig({
154 fmt: {
155 ignorePatterns: [
156 ".repos/**",
157 + "infra/task-cache/vendor/**",
158 // Macroscope requires one glob per line; Markdown formatting corrupts it.
159 ".macroscope/ignore.md",
160 ".alchemy",
@@ -175,6 +189,7 @@ export default defineConfig({
189 ignorePatterns: [
190 ".repos",
191 ".repos/**",
192 + "infra/task-cache/vendor/**",
193 "dist",
194 "dist-electron",
195 "node_modules",