// The ply Worker: the /v1 git API and the public /view repository viewer, served at ply.ivar.workers.dev. // `cf build`, `cf dev`, `cf deploy` and `cf workers types` read this file. vite.config.ts holds build settings only. import { bindings, defineConfig, exports, triggers, type ConfigContext } from "cf/config"; import { FLAG_OPERATOR_COMPATIBILITY_DATE, FLAG_OPERATOR_MODE, FLAG_OPERATOR_WORKER, OPERATOR_CONTROL_BINDING, OPERATOR_TOKEN_BINDING, } from "./src/flags/operator-names.ts"; import { FLAGS_MAX_AGE_S, FLAGS_SWR_S } from "./src/flags/ttl.ts"; import { PUBLIC_NAMESPACE } from "./src/public-namespace.ts"; import { PROBE_NAMESPACE } from "./src/bench/namespace.ts"; const MILLISECONDS_PER_SECOND = 1_000; const SECONDS_PER_MINUTE = 60; // Merge and rebase line merges are CPU-bound. The paid plan's ceiling for one HTTP request is 5 minutes // (developers.cloudflare.com/workers/platform/limits, checked 2026-10-23). const PAID_PLAN_HTTP_CPU_CEILING_MINUTES = 5; const CPU_LIMIT_MS = PAID_PLAN_HTTP_CPU_CEILING_MINUTES * SECONDS_PER_MINUTE * MILLISECONDS_PER_SECOND; // Head sampling is a fraction of requests in [0, 1]. Every request is logged and traced. const SAMPLE_EVERY_REQUEST = 1; const WORKER_NAME = "ply"; // Live Durable Object namespaces. This replaces the ordered `migrations` list of the old config format. // ViewerRateLimit was created by migration tag "v1-viewer-rate" (new_sqlite_classes) as namespace // 656fdd50f0d6428393eb2d9250ceee67. Declaring it here as a live SQLite class, with no lifecycle tombstones, // keeps that namespace and its data. // Never change `storage` on a live class. Retire or rename a class only with a `state` tombstone. // RateCounter (2026-11-06) is new and additive: the one counter behind the batched limiter (docs/rate-limit.md). const durableObjects = { ViewerRateLimit: exports.durableObject({ storage: "sqlite" }), RateCounter: exports.durableObject({ storage: "sqlite" }), // RefsWatch (2026-11-07) is new and additive: grants and purges the public namespace's long-lived refs cache // (src/refs-watch.ts). Reached through ctx.exports.RefsWatch, never an env binding (same reason as RateCounter). RefsWatch: exports.durableObject({ storage: "sqlite" }), // ProbeBench (2026-11-14) is new and additive: the probe workbench's runs, repositories and admissions // (src/bench/entry.ts). Reached through ctx.exports.ProbeBench, never an env binding (same reason as RateCounter). ProbeBench: exports.durableObject({ storage: "sqlite" }), } as const; // Workers Caching (never the Cache API / caches.default). The default entrypoint is uncached so it always runs (rate limit, // logs, scrub, traces). It sends public upload-pack to GitCache and authenticated inflated blobs to BlobCache over // ctx.exports. crossVersionCache keeps immutable content across deploys; CACHE_VERSION (src/cache-version.ts) is in every key. // Named entrypoints omit `cache` or set `enabled: true`. `enabled: false` does not compile: // the versions API rejects that block on a named entrypoint with error 10375. The default // entrypoint is the one place the block is accepted. Caching is off when the block is omitted. export type NamedWorkerOptions = { cache?: { enabled: true } }; export function namedEntrypoint(options: T): T { return options; } export const entrypoints = { default: exports.worker({ cache: { enabled: false } }), GitCache: exports.worker(namedEntrypoint({ cache: { enabled: true } })), // CAS graph (src/cas-cache.ts / cas-entries.ts): content-addressed layers via ctx.exports; props.scope partitions. ObjectCache: exports.worker(namedEntrypoint({ cache: { enabled: true } })), TreeCache: exports.worker(namedEntrypoint({ cache: { enabled: true } })), CommitCache: exports.worker(namedEntrypoint({ cache: { enabled: true } })), ResolvePathCache: exports.worker(namedEntrypoint({ cache: { enabled: true } })), // Mutable tip: short TTL + purge-on-write (tags + /vN/ref/ pathPrefixes). RefCache: exports.worker(namedEntrypoint({ cache: { enabled: true } })), // Whole responses pinned to a commit oid (src/pinned.ts): /v1 tree/read/commit and viewer raw files. Pinned: exports.worker(namedEntrypoint({ cache: { enabled: true } })), // The one flag ruleset (src/flags/cache.ts). Caching stays off on the default entrypoint, which always runs. FlagsCache: exports.worker(namedEntrypoint({ cache: { enabled: true } })), // The rate limiter's one blocked-clients document (src/rate/doc.ts): max-age 1, stale-while-revalidate 30. RateCache: exports.worker(namedEntrypoint({ cache: { enabled: true } })), // Caching is off by default. An explicit `enabled: false` on a named entrypoint is rejected by the API with 10375. // RPC writes (src/flags/entry.ts): setRollout and setTargeting are not idempotent GETs. FlagControl: exports.worker({} satisfies NamedWorkerOptions), } as const; // A binding can only name a class declared live above. src/index.js re-exports it from src/viewer/rate.ts. const VIEWER_RATE_CLASS: keyof typeof durableObjects = "ViewerRateLimit"; const plyWorker = { worker: { // The Worker already exists on the account. Renaming it here creates a second Worker. name: WORKER_NAME, // The date the live Worker already runs on. No nodejs_compat: ply is Web APIs only. compatibilityDate: "2026-10-19", // nodejs_als: only AsyncLocalStorage (node:async_hooks), which src/edge-trace.ts uses to nest ply's spans under // their ply parent in Workers tracing (#67). Declared rather than relying on the runtime allowing the import. compatibilityFlags: ["nodejs_als"], entrypoint: "src/index.js", workersDev: true, previewUrls: false, limits: { cpuMs: CPU_LIMIT_MS }, cache: { enabled: false, crossVersionCache: true }, // Every minute: bring the public-v0 mirrors (src/mirror.ts MIRRORS) to their GitHub branch. MIRROR_SYNC="off" stops it. triggers: [triggers.scheduled({ schedule: "* * * * *" })], observability: { enabled: true, headSamplingRate: SAMPLE_EVERY_REQUEST, // Remote URLs carry the repo name and path in the query string. Keep them out of the platform's request logs. redactQueryString: true, logs: { enabled: true, invocationLogs: true }, traces: { enabled: true, headSamplingRate: SAMPLE_EVERY_REQUEST }, }, env: { // Bearer token for /v1. A Worker secret, never in this file: set it with // `cf workers secrets update PLY_TOKEN --worker ply --type secret_text` (cf prompts for the value). // cf uploads it as an `inherit` binding with bindings_inherit=strict, so a deploy keeps the stored value // and the API refuses an upload when the Worker has no PLY_TOKEN. PLY_TOKEN: bindings.secret(), // The load-test credential (docs/rate-limit.md "Load-test controls"): unlocks x-ply-rate-client / -mode / -fault and // nothing else. ply-load holds this one, never PLY_TOKEN. Set with // `cf workers secrets update PLY_LOAD_TOKEN --worker ply --type secret_text`; local copy ~/.config/ply/load-token (0600). PLY_LOAD_TOKEN: bindings.secret(), // "on" enables the POST/DELETE endpoints. Anything else answers 501. WRITES: bindings.text("on"), // "off" sends every upstream exchange direct, past GitCache (Workers Caching). PACK_CACHE: bindings.text("on"), // "watched": public-namespace ls-refs answers are cached for minutes and purged by RefsWatch when a ref moves // (src/refs-watch.ts). Anything else keeps every refs answer at max-age 5 + stale-while-revalidate 10. REFS_POLICY: bindings.text("watched"), MIRROR_SYNC: bindings.text("on"), // /view rate limits. One SQLite Durable Object class, defined by this Worker. VIEWER_RATE: bindings.durableObject({ worker: WORKER_NAME, exportName: VIEWER_RATE_CLASS }), // The limiter (docs/rate-limit.md). "memo" (default since 2026-11-07) decides from the isolate's copy of the cached // blocked document and refreshes it in waitUntil (no I/O on the request path); "batched" fetches that document on // every request (25 ms budget); both flush counts in batches to RateCounter. "legacy" calls ViewerRateLimit on every // request. Roll the limiter back alone by setting "batched" (or "legacy") here AND rate.expectMode in // scripts/cd/cd.policy.json, then ship through the pipeline (a `cf` var edit would deploy straight to 100%). // RateCounter is reached through ctx.exports.RateCounter, not a binding: the versions API refuses a binding to a class // that no deploy has provisioned yet (checked 2026-11-06 on the first upload of RateCounter). RATE_MODE: bindings.text("memo"), // The public namespace, served read-only with no token (src/public.ts). The binding is scoped to this one // namespace; the Worker calls only get().createToken("read", 3600) and list(). No stored secret. PUBLIC_REPOS: bindings.artifacts({ namespace: PUBLIC_NAMESPACE }), // The probe workbench's namespace (src/bench): ProbeBench creates one repository per run here, revokes every token // it has and deletes it when the run ends. Scoped to the load-and-probe namespace, never the public one. PROBE_REPOS: bindings.artifacts({ namespace: PROBE_NAMESPACE }), // Exchange bodies of workbench runs (runs//., at most 1 MiB each). Create the // bucket before the first deploy that uploads this binding: `cf r2 bucket create ply-bench`. BENCH_BLOBS: bindings.r2({ name: "ply-bench" }), // The running version's id, sent as x-ply-version on every front-door response so a deploy can prove which // version answered a request pinned with Cloudflare-Workers-Version-Overrides (`npm run cd`). VERSION: bindings.versionMetadata(), // R2 is the flag source of truth: one object, flags/config.json, ETag = revision. // The bucket is not created here. It must already exist before a deploy that uploads this binding. // `cf deploy` can provision a missing R2 bucket; create `ply-flags` first so that path is not taken. FLAGS: bindings.r2({ name: "ply-flags" }), FLAGS_MAX_AGE_S: bindings.text(String(FLAGS_MAX_AGE_S)), FLAGS_SWR_S: bindings.text(String(FLAGS_SWR_S)), // ply's own service binding to FlagControl. Nothing outside this Worker can call it. // The operator Worker (`cf deploy --mode flag-operator`) binds the same export and is the // sanctioned write path. Reads of the ruleset go through ctx.exports.FlagsCache. FLAG_CONTROL: bindings.worker({ worker: WORKER_NAME, exportName: "FlagControl" }), }, exports: { ...durableObjects, ...entrypoints }, }, }; // A second Worker, selected only when `--mode` is exactly `flag-operator`. // `cf build`, `cf deploy`, and `cf workers types` pass production, development, or undefined, and stay on ply. // This Worker has no R2 binding, so a deploy does not provision ply-flags. It does not add a secret to ply. function flagOperatorWorker() { return { name: FLAG_OPERATOR_WORKER, compatibilityDate: FLAG_OPERATOR_COMPATIBILITY_DATE, entrypoint: "src/flags/operator-entry.ts", workersDev: true, previewUrls: false, cache: { enabled: false as const }, observability: { enabled: true, headSamplingRate: SAMPLE_EVERY_REQUEST, redactQueryString: true, logs: { enabled: true, invocationLogs: true }, traces: { enabled: true, headSamplingRate: SAMPLE_EVERY_REQUEST }, }, env: { // Bearer for this Worker only. Set it with // `cf workers secrets update FLAG_OPERATOR_TOKEN --worker ply-flag-operator --type secret_text` // (cf prompts for the value). ply does not gain this secret. [OPERATOR_TOKEN_BINDING]: bindings.secret(), [OPERATOR_CONTROL_BINDING]: bindings.worker({ worker: WORKER_NAME, exportName: "FlagControl" }), }, }; } export default defineConfig((ctx: ConfigContext) => { if (ctx.mode === FLAG_OPERATOR_MODE) return { worker: flagOperatorWorker() }; return plyWorker; });