Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix(web): select only entitled AI recovery candidates in SQL
Richie McIlroy34ad868497ef
diff --git a/apps/web/__tests__/unit/video-pipeline-recovery.test.ts b/apps/web/__tests__/unit/video-pipeline-recovery.test.tsindex 2cb2c5d95..cb4a331af 100644--- a/apps/web/__tests__/unit/video-pipeline-recovery.test.ts+++ b/apps/web/__tests__/unit/video-pipeline-recovery.test.ts@@ -1,3 +1,4 @@+import { inArray, sql } from "drizzle-orm";import { beforeEach, describe, expect, it, vi } from "vitest";const mockDb = vi.fn();@@ -394,4 +395,80 @@ describe("recoverStalledVideoPipeline", () => {expect(mockStartAiGeneration).toHaveBeenCalledWith("video-ai", "user-1");expect(result.ai.statuses).toEqual({ started: 1 });});++ it("selects only entitled AI candidates in SQL so ineligible videos cannot fill the batch", async () => {+ const chains = [+ makeSelectChain([]),+ makeSelectChain([]),+ makeSelectChain([]),+ ];+ let dbCall = 0;+ mockDb.mockImplementation(() => chains[dbCall++]);++ const { recoverStalledVideoPipeline, STALLED_AI_RECOVERY_BATCH_SIZE } =+ await import("@/lib/video-pipeline-recovery");+ await recoverStalledVideoPipeline({ now, concurrency: 1 });++ expect(chains[2]?.limit).toHaveBeenCalledWith(+ STALLED_AI_RECOVERY_BATCH_SIZE,+ );+ expect(inArray).toHaveBeenCalledWith("users.stripeSubscriptionStatus", [+ "active",+ "trialing",+ "complete",+ "paid",+ "past_due",+ ]);+ const sqlText = vi+ .mocked(sql)+ .mock.calls.map(([strings]) =>+ (strings as TemplateStringsArray).join("?"),+ );+ expect(sqlText).toContain("COALESCE(?, '') <> ''");+ });++ it("never starts AI for transcribe-on-view videos but still resumes AI a view started", async () => {+ const chains = [+ makeSelectChain([]),+ makeSelectChain([]),+ makeSelectChain([]),+ ];+ let dbCall = 0;+ mockDb.mockImplementation(() => chains[dbCall++]);++ const { recoverStalledVideoPipeline } = await import(+ "@/lib/video-pipeline-recovery"+ );+ await recoverStalledVideoPipeline({ now, concurrency: 1 });++ const aiWhere = JSON.stringify(chains[2]?.where.mock.calls[0]?.[0]);+ expect(aiWhere).toContain("$.transcribeOnView') IS NULL");+ expect(aiWhere).toContain('"QUEUED","PROCESSING"');+ });++ it("still drops ineligible rows if the database returns them", async () => {+ const freeCandidate = {+ videoId: "video-free",+ userId: "user-free",+ metadata: null,+ updatedAt: staleAt,+ stripeSubscriptionStatus: "canceled",+ thirdPartyStripeSubscriptionId: "",+ };++ let dbCall = 0;+ mockDb.mockImplementation(() => {+ dbCall++;+ if (dbCall < 3) return makeSelectChain([]);+ return makeSelectChain([freeCandidate]);+ });++ const { recoverStalledVideoPipeline } = await import(+ "@/lib/video-pipeline-recovery"+ );+ const result = await recoverStalledVideoPipeline({ now, concurrency: 1 });++ expect(mockStartAiGeneration).not.toHaveBeenCalled();+ expect(result.ai.statuses).toEqual({});+ });});diff --git a/apps/web/lib/video-pipeline-recovery.ts b/apps/web/lib/video-pipeline-recovery.tsindex e828cd801..217e995dc 100644--- a/apps/web/lib/video-pipeline-recovery.ts+++ b/apps/web/lib/video-pipeline-recovery.ts@@ -21,7 +21,10 @@ import {sql,} from "drizzle-orm";import { start } from "workflow/api";-import { isAiGenerationEnabledForUser } from "@/lib/ai-generation-entitlement";+import {+ AI_ENTITLED_SUBSCRIPTION_STATUSES,+ isAiGenerationEnabledForUser,+} from "@/lib/ai-generation-entitlement";import { startAiGeneration } from "@/lib/generate-ai";import {getLoomImportRunState,@@ -406,7 +409,6 @@ export async function recoverStalledVideoPipeline({.orderBy(asc(videos.updatedAt)).limit(transcriptionLimit);- const aiScanLimit = buildEnv.NEXT_PUBLIC_IS_CAP ? aiLimit * 4 : aiLimit;const aiCandidates = (await db().select({@@ -428,8 +430,13 @@ export async function recoverStalledVideoPipeline({gte(videos.createdAt, recentBefore),lte(videos.updatedAt, staleBefore),or(- isNull(- sql`JSON_EXTRACT(${videos.metadata}, '$.aiGenerationStatus')`,+ // Transcribe-on-view videos (Loom imports) only start AI from a+ // view; recovery resumes AI a view started but never starts it.+ and(+ isNull(+ sql`JSON_EXTRACT(${videos.metadata}, '$.aiGenerationStatus')`,+ ),+ sql`JSON_EXTRACT(${videos.metadata}, '$.transcribeOnView') IS NULL`,),inArray(sql<string>`JSON_UNQUOTE(JSON_EXTRACT(${videos.metadata}, '$.aiGenerationStatus'))`,@@ -437,13 +444,22 @@ export async function recoverStalledVideoPipeline({),),sql`(${videos.metadata} IS NULL OR JSON_EXTRACT(${videos.metadata}, '$.summary') IS NULL OR JSON_EXTRACT(${videos.metadata}, '$.chapters') IS NULL)`,+ // Filtering entitlement after LIMIT let older ineligible videos+ // fill every batch, so eligible ones were never reached.+ buildEnv.NEXT_PUBLIC_IS_CAP+ ? or(+ sql`COALESCE(${users.thirdPartyStripeSubscriptionId}, '') <> ''`,+ inArray(+ users.stripeSubscriptionStatus,+ AI_ENTITLED_SUBSCRIPTION_STATUSES,+ ),+ )+ : undefined,),).orderBy(asc(videos.updatedAt))- .limit(aiScanLimit)- )- .filter(isAiGenerationEnabledForUser)- .slice(0, aiLimit) as AiCandidate[];+ .limit(aiLimit)+ ).filter(isAiGenerationEnabledForUser) as AiCandidate[];const [media, transcription, ai] = await Promise.all([recoverConcurrently(