Skip to content

Open source CapCut alternative.

TypeScript

feat(site): lapsed credit grants settle nightly and read as spent

David Le2e4501aeff4a
diff --git a/site/scripts/backfill-signup-credit-expiry.ts b/site/scripts/backfill-signup-credit-expiry.ts
new file mode 100644
index 00000000..e5865e73
--- /dev/null
+++ b/site/scripts/backfill-signup-credit-expiry.ts
@@ -0,0 +1,56 @@
+#!/usr/bin/env bun
+/**
+ * One-off: stamp an expiry on the signup grants made before signup credits
+ * had one. Only signup grants that are still active, still hold credit, and
+ * carry no expiry are touched; purchased, manual and Pro grants are left
+ * alone. The lifetime comes from the signupCredits setting and counts from
+ * the run, so every account gets the full window from today. The nightly
+ * sweep then expires them when the day comes. Idempotent: a stamped grant no
+ * longer matches.
+ *
+ * Run from site/ with production credentials in the environment:
+ * bun run scripts/backfill-signup-credit-expiry.ts # dry run
+ * bun run scripts/backfill-signup-credit-expiry.ts --apply
+ */
+
+import { getGlobalSetting } from "../src/lib/config/effective";
+import { zeroCreditMicros } from "../src/lib/credits/amounts";
+import { creditGrantExpiry } from "../src/lib/credits/top-up";
+import { prisma } from "../src/lib/prisma";
+
+const apply = process.argv.includes("--apply");
+
+const setting = await getGlobalSetting("signupCredits");
+const expiresAt = creditGrantExpiry(setting.expiresAfterDays);
+if (!expiresAt) {
+ console.error("signupCredits grants never expire; set a lifetime on su first");
+ process.exit(1);
+}
+
+const where = {
+ expiresAt: null,
+ remainingAmountMicros: { gt: zeroCreditMicros },
+ source: "signup",
+ status: "active",
+};
+const grants = await prisma.userCreditGrant.findMany({
+ select: { id: true, remainingAmountMicros: true, userId: true },
+ where,
+});
+const remaining = grants.reduce((sum, g) => sum + g.remainingAmountMicros, zeroCreditMicros);
+console.log(
+ `${grants.length} signup grants without an expiry, ${remaining} micros outstanding; ` +
+ `expiry ${expiresAt.toISOString()} (${setting.expiresAfterDays} days)`,
+);
+
+if (apply) {
+ const result = await prisma.userCreditGrant.updateMany({
+ data: { expiresAt },
+ where: { ...where, id: { in: grants.map((g) => g.id) } },
+ });
+ console.log(`stamped ${result.count} grants`);
+} else {
+ console.log("dry run; pass --apply to stamp them");
+}
+
+await prisma.$disconnect();
diff --git a/site/scripts/backfill-signup-credit.ts b/site/scripts/backfill-signup-credit.ts
index a0791089..5208cb72 100644
--- a/site/scripts/backfill-signup-credit.ts
+++ b/site/scripts/backfill-signup-credit.ts
@@ -11,6 +11,7 @@
*/
import { getGlobalSetting } from "../src/lib/config/effective";
+import { creditGrantExpiry } from "../src/lib/credits/top-up";
import { grantSignupAppCredits } from "../src/lib/onboarding/signup-grants";
import { prisma } from "../src/lib/prisma";
@@ -27,7 +28,11 @@ if (setting.dollars <= 0) {
}
for (const userId of userIds) {
- const grant = await grantSignupAppCredits(userId, String(setting.dollars), setting.expiresAfterDays);
+ const grant = await grantSignupAppCredits(
+ userId,
+ String(setting.dollars),
+ creditGrantExpiry(setting.expiresAfterDays) ?? null,
+ );
console.log(`${userId}: grant ${grant.id} (${grant.originalAmountMicros} micros)`);
}
diff --git a/site/src/app/api/credits/expire/route.ts b/site/src/app/api/credits/expire/route.ts
new file mode 100644
index 00000000..0abb4999
--- /dev/null
+++ b/site/src/app/api/credits/expire/route.ts
@@ -0,0 +1,15 @@
+import { NextRequest, NextResponse } from "next/server";
+
+import { isVercelCron, notFoundResponse } from "@/lib/donkey-api-auth";
+import { enqueueJob } from "@/lib/jobs/queue";
+
+// Without a queue configured (local dev) the job runs inline before the
+// response; give it room.
+export const maxDuration = 300;
+
+// The nightly credit-expiry sweep. Vercel's cron authenticates with the
+// CRON_SECRET bearer token; a manual run goes through POST /api/jobs.
+export const GET = async (request: NextRequest) => {
+ if (!isVercelCron(request)) return notFoundResponse();
+ return NextResponse.json(await enqueueJob("credit-expiry", {}, "vercel-cron"));
+};
diff --git a/site/src/lib/analytics/pipeline.ts b/site/src/lib/analytics/pipeline.ts
index f1d88f43..2b159e9f 100644
--- a/site/src/lib/analytics/pipeline.ts
+++ b/site/src/lib/analytics/pipeline.ts
@@ -29,6 +29,8 @@ import {
} from "@/lib/analytics/schema";
import { cutLimitsForTier } from "@/cut/server/cloud/limits";
import { isActiveProStatus } from "@/lib/billing/pro-subscription";
+import { zeroCreditMicros } from "@/lib/credits/amounts";
+import { lapsedCreditByUser } from "@/lib/credits/inference";
import { REFERRAL_SOURCES } from "@/lib/onboarding/sequence";
import { prisma } from "@/lib/prisma";
@@ -238,6 +240,10 @@ async function writeSnapshot(): Promise<AnalyticsSnapshotFile> {
: {}),
}));
+ // The stored balance still carries a lapsed grant until the account is
+ // read, charged, or reached by the nightly sweep; the snapshot takes the
+ // lapsed amount off so every balance is what the account can spend.
+ const lapsed = await lapsedCreditByUser();
const balances = (
await fetchAllPages((cursorArgs) =>
prisma.userCreditAccount.findMany({
@@ -248,7 +254,9 @@ async function writeSnapshot(): Promise<AnalyticsSnapshotFile> {
}),
)
).map((account) => ({
- balanceMicros: account.balanceMicros.toString(),
+ balanceMicros: (
+ account.balanceMicros - (lapsed.get(account.userId) ?? zeroCreditMicros)
+ ).toString(),
userId: account.userId,
}));
diff --git a/site/src/lib/credits/inference.ts b/site/src/lib/credits/inference.ts
index 53113d34..c82ca7b5 100644
--- a/site/src/lib/credits/inference.ts
+++ b/site/src/lib/credits/inference.ts
@@ -257,6 +257,59 @@ export async function expireCredits(userId: string, now = new Date()) {
));
}
+// Expiry is otherwise applied when an account is read or charged, so an
+// account nobody touched after its grant lapsed still carries the lapsed
+// amount in balanceMicros. The nightly sweep settles those accounts here.
+// Each account is its own serializable transaction, the same path a live read
+// takes; an account whose transaction keeps losing to live charges is logged
+// and skipped, and the sweep stops at its time budget so the job never runs
+// past its worker. Whatever is left waits for the next run, or for the
+// account's next read.
+export async function expireAllCredits(now = new Date(), budgetMs = 240_000) {
+ const startedAt = Date.now();
+ const overdue = await prisma.userCreditGrant.findMany({
+ distinct: ["userId"],
+ orderBy: { expiresAt: "asc" },
+ select: { userId: true },
+ where: {
+ expiresAt: { lte: now },
+ remainingAmountMicros: { gt: zeroCreditMicros },
+ status: "active",
+ },
+ });
+ let settled = 0;
+ let failed = 0;
+ let index = 0;
+ for (; index < overdue.length; index++) {
+ if (Date.now() - startedAt > budgetMs) break;
+ const { userId } = overdue[index];
+ try {
+ await expireCredits(userId, now);
+ settled++;
+ } catch (error) {
+ failed++;
+ console.error("[credit-expiry] failed to settle an account", { error, userId });
+ }
+ }
+ return { failed, remaining: overdue.length - index, settled };
+}
+
+// What each account could spend right now, with grants past their expiry
+// taken off even where the sweep has not reached the account yet. A read,
+// so a snapshot of every balance costs one aggregate on top of the page scan.
+export async function lapsedCreditByUser(now = new Date()) {
+ const rows = await prisma.userCreditGrant.groupBy({
+ _sum: { remainingAmountMicros: true },
+ by: ["userId"],
+ where: {
+ expiresAt: { lte: now },
+ remainingAmountMicros: { gt: zeroCreditMicros },
+ status: "active",
+ },
+ });
+ return new Map(rows.map((row) => [row.userId, row._sum.remainingAmountMicros ?? zeroCreditMicros]));
+}
+
export async function assertCanUseInference(input: CreditPreflightInput) {
await ensureCreditAccount(input.userId);
await expireCredits(input.userId);
diff --git a/site/src/lib/jobs/credit-expiry.ts b/site/src/lib/jobs/credit-expiry.ts
new file mode 100644
index 00000000..69e13ab6
--- /dev/null
+++ b/site/src/lib/jobs/credit-expiry.ts
@@ -0,0 +1,13 @@
+import { z } from "zod";
+
+import { expireAllCredits } from "@/lib/credits/inference";
+import { defineJob } from "@/lib/jobs/registry";
+
+// The nightly credit-expiry sweep. A grant past its expiry is normally settled
+// the next time its account is read or charged; this settles the accounts
+// nobody touched, so the stored balance the outreach scan reads is spendable
+// money. Runs from /api/credits/expire. The result names how many accounts
+// were settled, how many failed, and how many were left for the next run.
+export const creditExpiryJob = defineJob(z.object({}).strict(), async () =>
+ expireAllCredits(),
+);
diff --git a/site/src/lib/jobs/registry.ts b/site/src/lib/jobs/registry.ts
index 52d0b739..9cb48332 100644
--- a/site/src/lib/jobs/registry.ts
+++ b/site/src/lib/jobs/registry.ts
@@ -3,6 +3,7 @@ import type { z } from "zod";
import type { Prisma } from "@/generated/prisma/client";
import { analyticsDailyJob } from "@/lib/jobs/analytics-daily";
import { billingRefreshJob } from "@/lib/jobs/billing-refresh";
+import { creditExpiryJob } from "@/lib/jobs/credit-expiry";
import { deleteUserJob } from "@/lib/jobs/delete-user";
import { experimentResultsJob } from "@/lib/jobs/experiment-results";
import { outreachScanJob } from "@/lib/jobs/outreach-scan";
@@ -29,6 +30,7 @@ export function defineJob<S extends z.ZodType<unknown>>(
export const jobKinds: Record<string, JobKind> = {
"analytics-daily": analyticsDailyJob,
"billing-refresh": billingRefreshJob,
+ "credit-expiry": creditExpiryJob,
"delete-user": deleteUserJob,
"experiment-results": experimentResultsJob,
"outreach-scan": outreachScanJob,
diff --git a/site/src/lib/onboarding/signup-grants.ts b/site/src/lib/onboarding/signup-grants.ts
index 0a2c66f0..f4720401 100644
--- a/site/src/lib/onboarding/signup-grants.ts
+++ b/site/src/lib/onboarding/signup-grants.ts
@@ -26,12 +26,13 @@ export async function provisionSignupGrants(user: EmailUser): Promise<void> {
});
}
const credits = returning || setting.dollars <= 0 ? null : String(setting.dollars);
+ const expiresAt = creditGrantExpiry(setting.expiresAfterDays) ?? null;
// Settle the steps independently: one failing must not block the others,
// and signup itself must never fail because a bonus grant hiccupped.
const results = await Promise.allSettled([
credits === null
? Promise.resolve()
- : grantSignupAppCredits(user.id, credits, setting.expiresAfterDays),
+ : grantSignupAppCredits(user.id, credits, expiresAt),
seedStarterProject(user.id),
seedFontsFolder(user.id),
sendWelcomeEmail(user, credits),
@@ -51,14 +52,14 @@ export async function provisionSignupGrants(user: EmailUser): Promise<void> {
export async function grantSignupAppCredits(
userId: string,
credits: string,
- expiresAfterDays: number | null,
+ expiresAt: Date | null,
) {
// grantCredits dedupes on (source, sourceId, userId), so this is a no-op on
// re-run.
return grantCredits({
amountMicros: creditStringToMicros(credits),
description: "Signup bonus credits",
- expiresAt: creditGrantExpiry(expiresAfterDays),
+ expiresAt: expiresAt ?? undefined,
source: "signup",
sourceId: `signup-app-credit:${userId}`,
userId,
diff --git a/site/vercel.json b/site/vercel.json
index 70982861..26971ead 100644
--- a/site/vercel.json
+++ b/site/vercel.json
@@ -1,5 +1,6 @@
{
"crons": [
+ { "path": "/api/credits/expire", "schedule": "30 0 * * *" },
{ "path": "/api/cut-cloud/gc", "schedule": "0 4 * * *" },
{ "path": "/api/analytics/run", "schedule": "0 1 * * *" },
{ "path": "/api/experiments/results", "schedule": "0 3 * * *" },