import { env } from "cloudflare:workers";
import { chunks, first, placeholders } from "../db";
import { repoDb, stmt } from "../scoped-db";
import { randomId } from "../crypto";
import { CAPS, enforceRateLimit, numberVar } from "../limits";
import { HttpError } from "../errors";
import type { JobTokenAccess } from "./job-token";
import { meterCacheFreed, meterCacheStorage } from "./usage-hooks";
const DEFAULT_CACHE_BYTES_PER_REPO = 2 * 1024 * 1024 * 1024;
const MAX_ENTRY_BYTES = 1024 * 1024 * 1024;
const MAX_KEY_LENGTH = 512;
const MAX_RESTORE_KEYS = 10;
const EVICTION_BATCH = 100;
export const IDLE_EVICTION_MS = 7 * 24 * 60 * 60 * 1000;
export const ABANDONED_RESERVATION_MS = 60 * 60 * 1000;
const VERSION = /^[0-9a-f]{1,64}$/;
const SHA256 = /^[0-9a-f]{64}$/;
const SCOPE_HEX_LENGTH = 32;
export const CONTENT_SHA256_HEADER = "x-gitfor-content-sha256";
type CacheRow = {
id: string;
repo_id: string;
ref: string;
key: string;
version: string;
size: number;
r2_key: string;
created_at: number;
last_used_at: number;
};
/** `readRefs`: refs a restore may read from; untrusted (fork) runs only see caches their own ref saved (CI-A10). */
type CacheScope = { repoId: string; ref: string; defaultRef: string; readRefs: string[] };
export function cacheQuotaBytes(): number {
return numberVar(env.CACHE_BYTES_PER_REPO, DEFAULT_CACHE_BYTES_PER_REPO);
}