Open source CapCut alternative.
TypeScript
feat(site): a render refused for space raises the storage wall [rebuild]
David Le80436dac1fe7
diff --git a/docs/guides/cut/README.md b/docs/guides/cut/README.mdindex 579e670f..33aaf5b2 100644--- a/docs/guides/cut/README.md+++ b/docs/guides/cut/README.md@@ -44,7 +44,7 @@ Project operation + captured documentBrowser previews use the browser compositor and replace one cached proxy in OPFS. A browser without the required encoders borrows the cloud worker and brings the result back to OPFS. Automatic browser renders run while the page is hidden and stop when editing resumes or the editor closes; the shelf uses the last completed proxy or its source thumbnail. Mac previews use the engine, retain a file per live job, and publish the project card atomically. Cloud preview jobs have immutable object keys; only the newest requested job can publish the project's preview pointer. A job ID resolves to a fresh delivery URL. Retired cloud previews expire after a day, while the current project preview stays available. The shared viewer plays MP4 or HLS with its own media element and releases its resources on source changes and unmount.-Cloud source media, generated media, and retained exports count toward storage. Derived previews, share cards, and HLS ladders are quota-exempt; staging inputs are temporary. Garbage collection also corrects accounting for older preview and card objects in bounded batches. Transports expose structured storage, credit, and authentication failures. Each host chooses their presentation; the website mounts its storage dialog separately.+Cloud source media, generated media, and retained exports count toward storage. Derived previews, share cards, and HLS ladders are quota-exempt; staging inputs are temporary. An export is weighed twice — the size it is heading for before a frame is drawn, and what actually landed before the account is charged — so one render cannot carry an account past its cap. Transports expose structured storage, credit, and authentication failures. Each host chooses their presentation; the website mounts its storage dialog separately.Timeline and preview share selection: ⌘/Ctrl-click or Shift-click toggles items, and dragging moves them together with one undo step. Items move independently until grouped, and groups save with the project. On the preview the selection wears one frame whose grips scale the set and whose button turns it, while a drag on any member moves them all. Selecting two or more items opens the group panel: it shows the fields every selected item has, reads a field as Mixed where they differ, and writes each edit to all of them in one undo step. A selection of one kind keeps that kind's own rows and its Color and Animation views, and an effect brings only its amount and Hidden.diff --git a/site/src/cut/components/OfferDialog.tsx b/site/src/cut/components/OfferDialog.tsxindex 98d32e17..0acec7e8 100644--- a/site/src/cut/components/OfferDialog.tsx+++ b/site/src/cut/components/OfferDialog.tsx@@ -20,6 +20,9 @@ export const OFFER_BANNERS = {} as const;export type OfferBanner = keyof typeof OFFER_BANNERS;+const isOfferBanner = (value: unknown): value is OfferBanner =>+ typeof value === "string" && value in OFFER_BANNERS;+/** A day as the card names it: "September 30". */export function offerDay(iso: string): string {return new Date(iso).toLocaleDateString("en-US", { day: "numeric", month: "long" });@@ -103,7 +106,9 @@ export function OfferDialog({}: {open: boolean;onOpenChange: (open: boolean) => void;- banner: OfferBanner;+ // A named picture, or a banner of the caller's own for a card that is not+ // selling credit.+ banner: OfferBanner | React.ReactNode;// The last moment the offer can be taken up; a countdown sits over the// picture while there is one.closesAt?: string | null;@@ -124,14 +129,18 @@ export function OfferDialog({className="top-[18%] translate-y-0 gap-0 overflow-hidden rounded-lg bg-background p-0 sm:max-w-sm"><div className="relative">- <Image- src={OFFER_BANNERS[banner]}- alt=""- className="aspect-[8/3] w-full object-cover"- width={1584}- height={672}- priority- />+ {isOfferBanner(banner) ? (+ <Image+ src={OFFER_BANNERS[banner]}+ alt=""+ className="aspect-[8/3] w-full object-cover"+ width={1584}+ height={672}+ priority+ />+ ) : (+ banner+ )}{closesAt ? <Countdown closesAt={closesAt} /> : null}</div><div className="flex flex-col gap-5 p-6">diff --git a/site/src/cut/components/StorageUpgradeDialog.tsx b/site/src/cut/components/StorageUpgradeDialog.tsxindex f895f370..a7e8ecc1 100644--- a/site/src/cut/components/StorageUpgradeDialog.tsx+++ b/site/src/cut/components/StorageUpgradeDialog.tsx@@ -4,7 +4,7 @@// anywhere in the app, or when the top bar's pill is clicked, and sells the// Pro storage tier — or just explains, when the account already has it.import { useEffect, useState } from "react";-import { Loader2 } from "lucide-react";+import { HardDrive, Loader2 } from "lucide-react";import { Button } from "@/components/ui/button";import {Dialog,@@ -13,6 +13,7 @@ import {DialogHeader,DialogTitle,} from "@/components/ui/dialog";+import { OfferButton, OfferDialog, OfferDismiss } from "./OfferDialog";import { useUpgradeToPro } from "@/cut/lib/proUpgrade";import {clearStorageQuotaWall,@@ -23,6 +24,7 @@ import {import { daysUntil } from "@/cut/lib/time";import { track } from "@/lib/analytics";import { useProSubscription } from "@/queries/billing";+import { useCloudUsage } from "@/cut/lib/backend/hooks";import { onOperationFailure } from "@/cut/lib/operationFailure";import { formatBytes } from "@/lib/bytes";@@ -57,54 +59,72 @@ function OpenDialog({ detail, onClose }: { detail: StorageQuotaDetail; onClose:const pro = useProSubscription();const upgrade = useUpgradeToPro();const isPro = pro.data?.isActive === true;+ // A wall raised by a queued render carries no numbers — it failed in the+ // worker, hours from the request. The account's own meter fills them in, so+ // every wall says how full the account is.+ const usage = useCloudUsage(true);+ const bytes = detail.bytes ?? usage.data?.bytes;+ const quotaBytes = detail.quotaBytes ?? usage.data?.quotaBytes ?? undefined;const used =- detail.bytes !== undefined && detail.quotaBytes !== undefined- ? `You've used ${formatBytes(detail.bytes)} of ${formatBytes(detail.quotaBytes)}.`+ bytes !== undefined && quotaBytes !== undefined+ ? `You've used ${formatBytes(bytes)} of ${formatBytes(quotaBytes)}.`: null;+ const title = detail.source === "pill" ? "Cloud storage" : "Cloud storage is full";const graceDays = detail.grace ? daysUntil(detail.grace.deadline) : null;+ const grace =+ detail.grace && graceDays !== null+ ? `Your Pro plan ended. Your oldest cloud projects will be deleted ${+ graceDays === 0 ? "today" : `in ${graceDays} day${graceDays === 1 ? "" : "s"}`+ } unless you upgrade or free ${formatBytes(detail.grace.overBytes)}.`+ : null;- return (- <Dialog open onOpenChange={(open) => !open && onClose()}>- <DialogContent className="sm:max-w-sm">- <DialogHeader>- <DialogTitle>- {detail.source === "quota-413" ? "Cloud storage is full" : "Cloud storage"}- </DialogTitle>- </DialogHeader>- <div className="space-y-2 text-sm text-muted-foreground">- {used && <p>{used}</p>}- {detail.grace && graceDays !== null && (- <p className="text-destructive">- Your Pro plan ended. Your oldest cloud projects will be deleted{" "}- {graceDays === 0 ? "today" : `in ${graceDays} day${graceDays === 1 ? "" : "s"}`}{" "}- unless you upgrade or free {formatBytes(detail.grace.overBytes)}.- </p>- )}- <p>- {isPro- ? "Free up space by deleting projects or media you no longer need."- : "Pro includes 100 GB of cloud storage, or free up space by deleting media."}- </p>- </div>- <DialogFooter className="mt-2">- {isPro ? (+ // An account that already has Pro is being told something, not sold+ // something: the offer card's picture and its full-width buy button belong+ // to the upgrade, so the plain dialog carries this one.+ if (isPro) {+ return (+ <Dialog open onOpenChange={(open) => !open && onClose()}>+ <DialogContent className="top-[18%] translate-y-0 sm:max-w-sm">+ <DialogHeader>+ <DialogTitle>{title}</DialogTitle>+ </DialogHeader>+ <div className="space-y-2 text-sm text-muted-foreground">+ {used && <p>{used}</p>}+ <p>Free up space by deleting projects or media you no longer need.</p>+ </div>+ <DialogFooter className="mt-2"><Button className="w-full" onClick={onClose}>OK</Button>- ) : (- <div className="flex w-full flex-col gap-2">- <Button className="w-full" disabled={upgrade.isPending} onClick={upgrade.start}>- {upgrade.isPending && <Loader2 className="animate-spin" data-icon="inline-start" />}- Upgrade to Pro- </Button>- <Button variant="ghost" className="w-full" onClick={onClose}>- Not now- </Button>- </div>- )}- </DialogFooter>- </DialogContent>- </Dialog>+ </DialogFooter>+ </DialogContent>+ </Dialog>+ );+ }++ return (+ <OfferDialog+ open+ onOpenChange={(open) => !open && onClose()}+ // Neutral, and half the height of a credit offer's picture: this card is+ // a wall, not a promotion.+ banner={+ <div className="flex aspect-[16/3] w-full items-center bg-neutral-800 px-6 dark:bg-neutral-700">+ <HardDrive className="size-7 text-white" strokeWidth={1.5} />+ </div>+ }+ title={title}+ body={`${used ? `${used} ` : ""}Pro includes 100 GB of cloud storage.`}+ terms="Or free up space by deleting projects and media you no longer need."+ error={grace}+ cta={+ <OfferButton disabled={upgrade.isPending} onClick={upgrade.start}>+ {upgrade.isPending && <Loader2 className="animate-spin" data-icon="inline-start" />}+ Upgrade to Pro+ </OfferButton>+ }+ secondary={<OfferDismiss onClick={onClose}>Not now</OfferDismiss>}+ />);}diff --git a/site/src/cut/lib/exportStore.ts b/site/src/cut/lib/exportStore.tsindex 948909bc..c3463dff 100644--- a/site/src/cut/lib/exportStore.ts+++ b/site/src/cut/lib/exportStore.ts@@ -18,6 +18,8 @@ import {import type { ExportDoc } from "./renderSnapshot";import { canRenderInBrowser } from "./exportRender";import { useGenNotify } from "./genNotify";+import { STORAGE_FULL } from "./operationFailure";+import { emitStorageQuota } from "./storageQuota";// Exports are tracked app-wide, not per-open-project. The engine holds every// export job in one process-global registry, so this store is a thin reflection@@ -318,16 +320,30 @@ export const useExports = create<ExportsState>((set, get) => ({cloudRetryAt = 0;}}+ // A failed or skipped feed keeps its previous rows; only a fresh answer+ // replaces that backend's slice.+ const slice = (kind: CutMode, fresh: ExportJob[] | null) =>+ fresh ?? get().jobs.filter((j) => j.residency === kind);+ const jobs = [+ ...slice("local", localRows),+ ...slice("cloud", cloudRows),+ ...slice("browser", browserRows),+ ];+ // A render the machine refused for space never answered a request with a+ // 413 — it failed hours later, in a queue. The wall it would have raised+ // is raised here instead, once per job: the row stays in the feed for a+ // day, and a user who has closed the dialog is done being told.+ // The set follows the feed: an id whose row has aged out is forgotten.+ for (const id of walledJobs) if (!jobs.some((j) => j.id === id)) walledJobs.delete(id);+ for (const job of jobs) {+ if (job.status !== "error" || job.error !== STORAGE_FULL || walledJobs.has(job.id)) continue;+ // Marked only once it lands. Before the dialog mounts, and while a wall+ // is already standing, the emit tells nobody — and a job marked then+ // would never raise its wall at all.+ if (!emitStorageQuota({ source: "render" })) break;+ walledJobs.add(job.id);+ }set((s) => {- // A failed or skipped feed keeps its previous rows; only a fresh answer- // replaces that backend's slice.- const slice = (kind: CutMode, fresh: ExportJob[] | null) =>- fresh ?? s.jobs.filter((j) => j.residency === kind);- const jobs = [- ...slice("local", localRows),- ...slice("cloud", cloudRows),- ...slice("browser", browserRows),- ];// Polls land in the middle of drags. The same rows again hand back the// state as it was, so nothing that reads the feed re-renders; a fresh// array for an unchanged list re-rendered the dock and the Media tab on@@ -449,6 +465,10 @@ const ACTIVE_MS = 700;const IDLE_MIN_MS = 3000;const IDLE_MAX_MS = 30_000;+// Jobs whose storage failure has already raised the wall, so a day of polling+// the same errored row asks once.+const walledJobs = new Set<string>();+// The cloud feed drops out while the browser is offline and backs off while the// server is unreachable; coming back online resets both and polls right away.let cloudFailures = 0;diff --git a/site/src/cut/lib/operationFailure.ts b/site/src/cut/lib/operationFailure.tsindex 313c2591..e57995f7 100644--- a/site/src/cut/lib/operationFailure.ts+++ b/site/src/cut/lib/operationFailure.ts@@ -6,12 +6,17 @@ export type OperationFailure = {quotaBytes?: number;};+/** What a storage refusal reads as, wherever it is raised: a rejected upload,+ * a render refused its space, a render that outgrew it mid-encode. One string,+ * so every surface matches the dialog it opens. */+export const STORAGE_FULL = "Cloud storage is full.";+export function operationFailure(status: number, body: unknown): OperationFailure | null {const data = body && typeof body === "object" ? body as Record<string, unknown> : {};if (status === 401) return { code: "authentication_required", message: "Sign in to continue." };if (status === 413 && data.error === "storage_quota_exceeded") {return {- code: "storage_quota_exceeded", message: "Cloud storage is full.",+ code: "storage_quota_exceeded", message: STORAGE_FULL,...(typeof data.bytes === "number" ? { bytes: data.bytes } : {}),...(typeof data.quotaBytes === "number" ? { quotaBytes: data.quotaBytes } : {}),};diff --git a/site/src/cut/lib/storageQuota.test.ts b/site/src/cut/lib/storageQuota.test.tsnew file mode 100644index 00000000..7c923c80--- /dev/null+++ b/site/src/cut/lib/storageQuota.test.ts@@ -0,0 +1,41 @@+import { afterEach, expect, test } from "bun:test";+import {+ clearStorageQuotaWall,+ emitStorageQuota,+ onStorageQuota,+ openStorageUpgrade,+ type StorageQuotaDetail,+} from "./storageQuota";++const offs: (() => void)[] = [];+afterEach(() => {+ for (const off of offs.splice(0)) off();+ clearStorageQuotaWall();+});++function listen() {+ const seen: StorageQuotaDetail[] = [];+ offs.push(onStorageQuota((detail) => seen.push(detail)));+ return seen;+}++test("the wall goes up once, and says whether it landed", () => {+ const seen = listen();+ expect(emitStorageQuota({ source: "render" })).toBe(true);+ expect(emitStorageQuota({ source: "quota-413" })).toBe(false);+ expect(seen).toEqual([{ source: "render" }]);+});++test("with nothing mounted to show it, nobody was told", () => {+ expect(emitStorageQuota({ source: "render" })).toBe(false);+ const seen = listen();+ expect(emitStorageQuota({ source: "render" })).toBe(true);+ expect(seen).toHaveLength(1);+});++test("a deliberate click opens past a standing wall", () => {+ const seen = listen();+ emitStorageQuota({ source: "render" });+ openStorageUpgrade({ source: "pill" });+ expect(seen.map((d) => d.source)).toEqual(["render", "pill"]);+});diff --git a/site/src/cut/lib/storageQuota.ts b/site/src/cut/lib/storageQuota.tsindex c8656afd..cb02eeed 100644--- a/site/src/cut/lib/storageQuota.ts+++ b/site/src/cut/lib/storageQuota.ts@@ -1,7 +1,9 @@export type StorageQuotaDetail = {bytes?: number;quotaBytes?: number;- source: "quota-413" | "pill";+ /** What raised the wall: a rejected write, a render refused its space, or+ * the top bar's pill. */+ source: "quota-413" | "render" | "pill";grace?: { deadline: string; overBytes: number };};@@ -12,10 +14,14 @@ const listeners = new Set<(detail: StorageQuotaDetail) => void>();// so they stay quiet until the user has answered it.let walled = false;-export function emitStorageQuota(detail: StorageQuotaDetail): void {- if (walled || listeners.size === 0) return;+/** True when the wall actually went up for someone. A caller that only wants+ * to tell the user once has to know whether this landed: with the dialog not+ * yet mounted, or a wall already standing, nobody was told. */+export function emitStorageQuota(detail: StorageQuotaDetail): boolean {+ if (walled || listeners.size === 0) return false;walled = true;for (const listener of listeners) listener(detail);+ return true;}/** Open the dialog from a deliberate click, past any standing wall. */diff --git a/site/src/cut/server/cloud/jobs.ts b/site/src/cut/server/cloud/jobs.tsindex c29c730c..9b42b8d6 100644--- a/site/src/cut/server/cloud/jobs.ts+++ b/site/src/cut/server/cloud/jobs.ts@@ -3,7 +3,7 @@// shapes byte-match the engine's export routes (http/export.ts + server/jobs.ts).import type { Prisma } from "@/generated/prisma/client";import { prisma } from "@/lib/prisma";-import { cutLimitsFor, EXPORT_QUOTA_MARGIN, renderJobCheck } from "./limits";+import { EXPORT_QUOTA_MARGIN, renderJobCheck } from "./limits";import { wakeRenderWorker } from "./wake";import { queuePreview } from "./previewJobs";import { getProject } from "./projects";@@ -11,7 +11,7 @@ import { MEDIA_REDIRECT_HEADERS, mediaObjectUrl, mediaUrlLifetime } from "./mediimport { del, head, overlayKey, overlayPrefix, presignPut, projectExportKey } from "./r2";import { contentTypeFor } from "../serveFile";import { containerOfName, deliveryContainer, exportBaseName, specMediaFiles, type ExportContainer } from "../../lib/exportDelivery";-import { addUsage, quotaCheck, reservedBytes, usageBytes } from "./usage";+import { addUsage, outputByteCeiling, quotaCheck } from "./usage";import { caught, err, redirect } from "./util";/** How long finished jobs stay in the export-jobs feed — the engine's registry@@ -119,10 +119,15 @@ async function importJobFor(userId: string, key: string | undefined): Promise<{/** The most bytes an import may bring in: what is left of the account's* storage, under the hard ceiling. */async function importByteCeiling(userId: string): Promise<number> {- const limits = await cutLimitsFor(userId);- if (limits.storageBytes === null) return IMPORT_MAX_BYTES;- const [stored, reserved] = await Promise.all([usageBytes(userId), reservedBytes(userId)]);- return Math.max(0, Math.min(IMPORT_MAX_BYTES, limits.storageBytes - stored - reserved));+ const left = await outputByteCeiling(userId);+ return left === null ? IMPORT_MAX_BYTES : Math.min(IMPORT_MAX_BYTES, left);+}++/** The most bytes an export's output may land, for the worker to hold its own+ * render to. A render that outgrows it is refused before it is uploaded, so an+ * account cannot pass the gate small and land large. */+async function exportByteCeiling(userId: string): Promise<number | null> {+ return outputByteCeiling(userId, EXPORT_QUOTA_MARGIN);}/** Drop the file a browser render uploaded for a row that never registered@@ -223,13 +228,19 @@ export async function queueDocExport(if (capped) return capped;const over = await quotaCheck(userId, 0, EXPORT_QUOTA_MARGIN);if (over) return over;+ // What the render may land. The worker holds its own output to it, so a+ // queued export cannot pass this gate and then write past the account's cap.+ const maxBytes = await exportByteCeiling(userId);const outName = await exportName(userId, projectId, project.name, undefined);const row = await prisma.cutRenderJob.create({data: {userId,projectId,kind: "export",- spec: { fromDoc: { preset, snapshot: { doc: project.doc, version: String(project.version) } } } as unknown as Prisma.InputJsonValue,+ spec: {+ fromDoc: { preset, snapshot: { doc: project.doc, version: String(project.version) } },+ ...(maxBytes === null ? {} : { maxBytes }),+ } as unknown as Prisma.InputJsonValue,outName,},});@@ -383,11 +394,13 @@ export const jobsCloud = {if (refused) return err(refused, 400);// Retained exports pass storage and render gates. Derived playback// artifacts use a bounded queue and expire through garbage collection.+ let maxBytes: number | null = null;if (target === "export") {const capped = await renderJobCheck(userId);if (capped) return capped;const over = await quotaCheck(userId, 0, EXPORT_QUOTA_MARGIN);if (over) return over;+ maxBytes = await exportByteCeiling(userId);}const jobSpec = {...(body.revision ? { revision: body.revision } : {}),@@ -395,6 +408,7 @@ export const jobsCloud = {overlays: body.overlays ?? [],...(borrowed ? { mediaFrom: "overlays" } : {}),...(target === "hls" ? { burnedSubtitles: body.burnedSubtitles === true } : {}),+ ...(maxBytes === null ? {} : { maxBytes }),} as unknown as Prisma.InputJsonValue;if (target === "preview") {@@ -526,13 +540,18 @@ export const jobsCloud = {*/async exportClientPresign(userId: string, req: Request) {try {- const body = (await req.json()) as { projectId?: string; outName?: string };+ const body = (await req.json()) as { projectId?: string; outName?: string; bytes?: number };const projectId = body.projectId;if (!projectId) return err("projectId is required.", 400);if (!(await getProject(userId, projectId))) return err("Project not found.", 400);const capped = await renderJobCheck(userId);if (capped) return capped;- const over = await quotaCheck(userId, 0, EXPORT_QUOTA_MARGIN);+ // The size the render is heading for, from the same model the export+ // dialog showed. A render an account has no room for is refused here,+ // before minutes of encoding; what actually lands is weighed again at+ // completion, because an encoder spends what the footage needs.+ const estimate = Number.isFinite(body.bytes) && body.bytes! > 0 ? Math.round(body.bytes!) : 0;+ const over = await quotaCheck(userId, estimate, EXPORT_QUOTA_MARGIN);if (over) return over;// The name is claimed by writing the row, not just by reading the rows// that exist. `exportName` dedupes against stored objects and jobs, so a@@ -598,6 +617,19 @@ export const jobsCloud = {const object = await head(key);if (!object) return err("The export was not uploaded.", 400);const bytes = object.bytes;+ // The account is charged what landed, so the quota is checked against it.+ // A file past the ceiling is dropped rather than charged: the row would+ // otherwise put the account over its cap for good, and the bytes it holds+ // are the ones nothing else can free.+ const over = await quotaCheck(userId, bytes, EXPORT_QUOTA_MARGIN);+ if (over) {+ await dropUnregisteredExport(userId, row);+ await prisma.cutRenderJob.updateMany({+ where: { id: row.id, state: { in: ["running", "queued"] } },+ data: { state: "dismissed" },+ });+ return over;+ }await prisma.$transaction(async (tx) => {await tx.cutMediaObject.create({data: {diff --git a/site/src/cut/server/cloud/limits.ts b/site/src/cut/server/cloud/limits.tsindex 0e341797..57d22c4f 100644--- a/site/src/cut/server/cloud/limits.ts+++ b/site/src/cut/server/cloud/limits.ts@@ -32,6 +32,19 @@ export const FREE_STORAGE_BYTES = 250 * 1024 ** 2;* or for Pro. */export const EXPORT_QUOTA_MARGIN = 1.2;+/** The bytes an account may hold at most — the wall every stored artifact is+ * weighed against at the moment it is charged. Null when the account is+ * unquotaed. `margin` widens it the same way `quotaCheck` does. */+export function storageCeiling(limits: CutLimits, margin = 1): number | null {+ return limits.storageBytes === null ? null : Math.floor(limits.storageBytes * margin);+}++/** The headroom a kind is charged against: an export renders into the margin+ * because it takes work out of the account; everything else meets the quota. */+export function quotaMarginFor(kind: string): number {+ return kind === "export" ? EXPORT_QUOTA_MARGIN : 1;+}+const FREE: CutLimits = {storageBytes: FREE_STORAGE_BYTES,renderJobsPerDay: 10,diff --git a/site/src/cut/server/cloud/usage.ts b/site/src/cut/server/cloud/usage.tsindex 33fca20c..c7dc55d4 100644--- a/site/src/cut/server/cloud/usage.ts+++ b/site/src/cut/server/cloud/usage.ts@@ -36,6 +36,16 @@ export async function addUsage(tx: Prisma.TransactionClient, userId: string, delawait adjustStorageBytes(tx, userId, BigInt(Math.round(delta)));}+/** The most bytes a render's output may land, after what is stored and what+ * in-flight claims hold. Null when the account is unquotaed. `margin` widens+ * the ceiling the same way `quotaCheck` does. */+export async function outputByteCeiling(userId: string, margin = 1): Promise<number | null> {+ const limits = await cutLimitsFor(userId);+ if (limits.storageBytes === null) return null;+ const [stored, reserved] = await Promise.all([usageBytes(userId), reservedBytes(userId)]);+ return Math.max(0, Math.floor(limits.storageBytes * margin) - stored - reserved);+}+/** 413 when `incoming` more bytes would break the account's storage quota,* else null. Stored bytes and in-flight claims both count. Superusers are* unquotaed. `margin` widens the ceiling for a writediff --git a/site/src/cut/worker/db.test.ts b/site/src/cut/worker/db.test.tsindex 27d28f7b..25c13593 100644--- a/site/src/cut/worker/db.test.ts+++ b/site/src/cut/worker/db.test.ts@@ -3,8 +3,11 @@ import { Prisma } from "@/generated/prisma/client";import { prisma as actualPrisma } from "@/lib/prisma";const originalPrisma = actualPrisma;+// The account the charge point weighs every object against: free tier, no Pro.const prisma = {settingOverride: { findUnique: mock(async () => ({ value: { maxAttempts: 3 } })) },+ user: { findUnique: mock(async () => ({ superUser: false })) },+ proSubscription: { findUnique: mock(async () => null) },$transaction: mock(async (run: (tx: Prisma.TransactionClient) => Promise<unknown>,options: { isolationLevel: string },@@ -14,6 +17,8 @@ const prisma = {}) };mock.module("@/lib/prisma", () => ({ prisma }));const { registerObject, unregisterObjects } = await import("./db");+const { FREE_STORAGE_BYTES } = await import("../server/cloud/limits");+const { STORAGE_FULL } = await import("../lib/operationFailure");afterAll(() => { mock.module("@/lib/prisma", () => ({ prisma: originalPrisma })); });const conflict = () => new Prisma.PrismaClientKnownRequestError("Write conflict", {@@ -49,6 +54,7 @@ function harness(failures = 1, error: Error = conflict()) {deleteMany: async () => { pending = null; return { count: 1 }; },},cutStorageUsage: {+ findUnique: async () => ({ userId: "user", bytes: pendingUsage }),upsert: async () => ({ userId: "user", bytes: pendingUsage }),updateMany: async ({ where, data }: {where: { bytes: bigint }; data: { bytes: { increment: bigint } };@@ -100,3 +106,29 @@ test("other failures propagate without retrying", async () => {await expect(registerObject(input)).rejects.toBe(error);expect(h.state().attempts).toBe(1);});++test("an object that would break the account's storage ceiling is refused, uncharged", async () => {+ const h = harness(0);+ await expect(registerObject({ ...input, bytes: FREE_STORAGE_BYTES * 2 })).rejects.toThrow(STORAGE_FULL);+ expect(h.state()).toMatchObject({ usage: BigInt(140), stored: { bytes: BigInt(40) } });+});++test("an export renders into the margin past the account's quota", async () => {+ const h = harness(0);+ expect(await registerObject({ ...input, bytes: Math.floor(FREE_STORAGE_BYTES * 1.1) })).toBe("object");+ expect(h.state().stored).toMatchObject({ bytes: BigInt(Math.floor(FREE_STORAGE_BYTES * 1.1)) });+});++test("the margin is the export's alone: the same bytes coming in meet the quota", async () => {+ const h = harness(0);+ await expect(+ registerObject({ ...input, kind: "media", bytes: Math.floor(FREE_STORAGE_BYTES * 1.1) })+ ).rejects.toThrow(STORAGE_FULL);+ expect(h.state()).toMatchObject({ usage: BigInt(140), stored: { bytes: BigInt(40) } });+});++test("a re-registration that shrinks the object is charged its delta", async () => {+ const h = harness(0);+ expect(await registerObject({ ...input, bytes: 10 })).toBe("object");+ expect(h.state()).toMatchObject({ usage: BigInt(110), stored: { bytes: BigInt(10) } });+});diff --git a/site/src/cut/worker/db.ts b/site/src/cut/worker/db.tsindex 6e0622f6..3a4ed22c 100644--- a/site/src/cut/worker/db.ts+++ b/site/src/cut/worker/db.ts@@ -1,5 +1,7 @@import { adjustStorageBytes } from "../server/cloud/storageCounter";import { artifactLifecycle, artifactUsageBytes } from "../lib/artifactPolicy";+import { cutLimitsFor, quotaMarginFor, storageCeiling } from "../server/cloud/limits";+import { STORAGE_FULL } from "../lib/operationFailure";import { prisma } from "@/lib/prisma";import { Prisma } from "@/generated/prisma/client";import { resolveSettings } from "@/lib/config/resolve";@@ -58,6 +60,19 @@ export async function registerObject(opts: {const quotaExempt = artifactLifecycle(opts.kind) !== "retained";const priorBytes = prior ? artifactUsageBytes(prior.bytes, prior.uploadState === "complete", prior.quotaExempt) : BigInt(0);const delta = artifactUsageBytes(BigInt(opts.bytes), true, quotaExempt) - priorBytes;+ // The wall every stored artifact meets, wherever it came from: a job that+ // grew past what its account can hold is failed here, at the moment the+ // bytes would be charged, instead of landing a row nothing can undo.+ if (delta > BigInt(0)) {+ const ceiling = storageCeiling(await cutLimitsFor(opts.userId), quotaMarginFor(opts.kind));+ if (ceiling !== null) {+ const usage = await tx.cutStorageUsage.findUnique({+ where: { userId: opts.userId },+ select: { bytes: true },+ });+ if ((usage?.bytes ?? BigInt(0)) + delta > BigInt(ceiling)) throw new Error(STORAGE_FULL);+ }+ }const row = await tx.cutMediaObject.upsert({where: { r2Key: opts.r2Key },create: { ...opts, bytes: BigInt(opts.bytes), quotaExempt, uploadState: "complete" },diff --git a/site/src/cut/worker/exportJob.ts b/site/src/cut/worker/exportJob.tsindex 96cd7223..5c88fe8f 100644--- a/site/src/cut/worker/exportJob.ts+++ b/site/src/cut/worker/exportJob.ts@@ -1,4 +1,4 @@-import { mkdir, mkdtemp, rm } from "node:fs/promises";+import { mkdir, mkdtemp, rm, stat } from "node:fs/promises";import os from "node:os";import path from "node:path";import type { CloudDocSnapshot } from "../lib/headless/docSession";@@ -6,8 +6,9 @@ import { buildDocExportSpec, isDocExportPreset } from "../lib/headless/docExportimport { runExport, type ExportSpec, type RenderHandle } from "../server/exportPipeline";import { storeCardArtifacts } from "./cardJob";import { prisma, registerObject, type ClaimedJob } from "./db";-import { downloadToFile, exportKey, mediaKey, mimeFor, uploadFile } from "./r2";+import { deleteObjects, downloadToFile, exportKey, mediaKey, mimeFor, uploadFile } from "./r2";import { overlayKey } from "../server/cloud/r2";+import { STORAGE_FULL } from "../lib/operationFailure";import { specMediaFiles } from "../lib/exportDelivery";import { runnerSession } from "./session";@@ -32,6 +33,10 @@ export interface ExportJobSpec {* granting Subtitles is refused the render rather than served cue text it* cannot strip out of the pixels. */burnedSubtitles?: boolean;+ /** The most bytes this render's output may land, stamped by the queueing+ * route from what the account's storage quota leaves. A render that outgrows+ * it fails here rather than uploading past the cap. */+ maxBytes?: number;}/** The job's overlay-PNG R2 keys, restricted to the job owner's own overlay@@ -182,16 +187,31 @@ export async function runExportJob(const key = preview? `cut/${job.userId}/projects/${projectId}/previews/${job.id}.mp4`: exportKey(job.userId, projectId, outName);+ // A retained output is charged to the account, so it is held to the room+ // the account had when the render was queued. The file is still on the+ // worker's disk here: nothing goes up, and nothing is charged.+ if (!preview && typeof stored.maxBytes === "number") {+ const size = (await stat(handle.outPath)).size;+ if (size > stored.maxBytes) throw new Error(STORAGE_FULL);+ }const bytes = await uploadFile(key, handle.outPath, mime);- await registerObject({- userId: job.userId,- projectId,- r2Key: key,- fileName: outName,- mime,- bytes,- kind: preview ? "preview" : "export",- });+ try {+ await registerObject({+ userId: job.userId,+ projectId,+ r2Key: key,+ fileName: outName,+ mime,+ bytes,+ kind: preview ? "preview" : "export",+ });+ } catch (err) {+ // The file is up and no row owns it. The sweep walks rows, so nothing+ // would ever find these bytes again — and a storage wall that leaves the+ // bytes behind walls the account in tighter with every retry.+ await deleteObjects([key]).catch(() => {});+ throw err;+ }if (preview) {await prisma.$transaction(async (tx) => {const project = await tx.cutProject.findUnique({ where: { id: projectId }, select: { previewKey: true } });diff --git a/site/src/lib/analytics.ts b/site/src/lib/analytics.tsindex f7fd1606..82bd7c86 100644--- a/site/src/lib/analytics.ts+++ b/site/src/lib/analytics.ts@@ -31,7 +31,7 @@ export type AnalyticsEvents = {cut_storage_pill_clicked: void;// The top bar's credits pill.cut_credits_pill_clicked: { dollars: number };- cut_storage_upgrade_shown: { source: "quota-413" | "pill" };+ cut_storage_upgrade_shown: { source: "quota-413" | "render" | "pill" };cut_grace_banner_shown: void;// First exposure to an experiment variant; the same value rides every// later event as a super property ($feature/<experiment>).