Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web): add stalled video pipeline recovery

Richie McIlroya0a4c985600c
diff --git a/apps/web/__tests__/unit/video-pipeline-recovery.test.ts b/apps/web/__tests__/unit/video-pipeline-recovery.test.ts
new file mode 100644
index 000000000..b1e63315e
--- /dev/null
+++ b/apps/web/__tests__/unit/video-pipeline-recovery.test.ts
@@ -0,0 +1,230 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mockDb = vi.fn();
+const mockStart = vi.fn();
+const mockTranscribeVideo = vi.fn();
+const mockStartAiGeneration = vi.fn();
+
+vi.mock("@cap/database", () => ({
+ db: mockDb,
+}));
+
+vi.mock("@cap/database/schema", () => ({
+ users: {
+ id: "users.id",
+ stripeSubscriptionStatus: "users.stripeSubscriptionStatus",
+ thirdPartyStripeSubscriptionId: "users.thirdPartyStripeSubscriptionId",
+ },
+ videos: {
+ id: "videos.id",
+ ownerId: "videos.ownerId",
+ bucket: "videos.bucket",
+ source: "videos.source",
+ metadata: "videos.metadata",
+ transcriptionStatus: "videos.transcriptionStatus",
+ isScreenshot: "videos.isScreenshot",
+ createdAt: "videos.createdAt",
+ updatedAt: "videos.updatedAt",
+ },
+ videoUploads: {
+ videoId: "videoUploads.videoId",
+ phase: "videoUploads.phase",
+ processingMessage: "videoUploads.processingMessage",
+ processingError: "videoUploads.processingError",
+ processingProgress: "videoUploads.processingProgress",
+ rawFileKey: "videoUploads.rawFileKey",
+ startedAt: "videoUploads.startedAt",
+ updatedAt: "videoUploads.updatedAt",
+ },
+}));
+
+vi.mock("@cap/env", () => ({
+ buildEnv: { NEXT_PUBLIC_IS_CAP: true },
+}));
+
+vi.mock("drizzle-orm", () => ({
+ and: vi.fn((...conditions: unknown[]) => conditions),
+ asc: vi.fn((value: unknown) => value),
+ eq: vi.fn((left: unknown, right: unknown) => ({ left, right })),
+ gte: vi.fn((left: unknown, right: unknown) => ({ left, right })),
+ inArray: vi.fn((left: unknown, right: unknown) => ({ left, right })),
+ isNotNull: vi.fn((value: unknown) => value),
+ isNull: vi.fn((value: unknown) => value),
+ lte: vi.fn((left: unknown, right: unknown) => ({ left, right })),
+ or: vi.fn((...conditions: unknown[]) => conditions),
+ sql: vi.fn((strings: TemplateStringsArray, ...values: unknown[]) => ({
+ strings,
+ values,
+ })),
+}));
+
+vi.mock("workflow/api", () => ({
+ start: mockStart,
+}));
+
+vi.mock("@/lib/transcribe", () => ({
+ transcribeVideo: mockTranscribeVideo,
+}));
+
+vi.mock("@/lib/generate-ai", () => ({
+ startAiGeneration: mockStartAiGeneration,
+}));
+
+vi.mock("@/workflows/process-video", () => ({
+ processVideoWorkflow: vi.fn(),
+}));
+
+vi.mock("@/workflows/finalize-desktop-recording", () => ({
+ finalizeDesktopRecordingWorkflow: vi.fn(),
+}));
+
+function makeSelectChain(candidates: unknown[]) {
+ const chain = {
+ select: vi.fn(),
+ from: vi.fn(),
+ innerJoin: vi.fn(),
+ leftJoin: vi.fn(),
+ where: vi.fn(),
+ orderBy: vi.fn(),
+ limit: vi.fn(),
+ };
+ chain.select.mockReturnValue(chain);
+ chain.from.mockReturnValue(chain);
+ chain.innerJoin.mockReturnValue(chain);
+ chain.leftJoin.mockReturnValue(chain);
+ chain.where.mockReturnValue(chain);
+ chain.orderBy.mockReturnValue(chain);
+ chain.limit.mockResolvedValue(candidates);
+ return chain;
+}
+
+function makeUpdateChain(affectedRows: number) {
+ const chain = {
+ update: vi.fn(),
+ set: vi.fn(),
+ where: vi.fn(),
+ };
+ chain.update.mockReturnValue(chain);
+ chain.set.mockReturnValue(chain);
+ chain.where.mockResolvedValue([{ affectedRows }]);
+ return chain;
+}
+
+const now = new Date("2026-07-20T15:00:00.000Z");
+const staleAt = new Date("2026-07-20T14:00:00.000Z");
+
+beforeEach(() => {
+ vi.clearAllMocks();
+ mockStart.mockResolvedValue({ runId: "run-1" });
+ mockTranscribeVideo.mockResolvedValue({
+ success: true,
+ message: "Transcription workflow started",
+ });
+ mockStartAiGeneration.mockResolvedValue({
+ success: true,
+ message: "AI generation workflow started",
+ });
+});
+
+describe("recoverStalledVideoPipeline", () => {
+ it("atomically restarts recent stale media, transcription, and AI work", async () => {
+ const mediaCandidate = {
+ videoId: "video-media",
+ userId: "user-1",
+ bucketId: null,
+ rawFileKey: "user-1/video-media/raw.mp4",
+ sourceType: "webMP4",
+ updatedAt: staleAt,
+ };
+ const transcriptionCandidate = {
+ videoId: "video-transcription",
+ userId: "user-1",
+ transcriptionStatus: "PROCESSING",
+ updatedAt: staleAt,
+ stripeSubscriptionStatus: "active",
+ thirdPartyStripeSubscriptionId: null,
+ };
+ const aiCandidate = {
+ videoId: "video-ai",
+ userId: "user-1",
+ metadata: { aiGenerationStatus: "QUEUED" },
+ updatedAt: staleAt,
+ stripeSubscriptionStatus: "active",
+ thirdPartyStripeSubscriptionId: null,
+ };
+
+ let dbCall = 0;
+ mockDb.mockImplementation(() => {
+ dbCall++;
+ if (dbCall === 1) return makeSelectChain([mediaCandidate]);
+ if (dbCall === 2) return makeSelectChain([transcriptionCandidate]);
+ if (dbCall === 3) return makeSelectChain([aiCandidate]);
+ return makeUpdateChain(1);
+ });
+
+ const { recoverStalledVideoPipeline } = await import(
+ "@/lib/video-pipeline-recovery"
+ );
+ const result = await recoverStalledVideoPipeline({ now, concurrency: 1 });
+
+ expect(mockStart).toHaveBeenCalledTimes(1);
+ expect(mockTranscribeVideo).toHaveBeenCalledTimes(1);
+ expect(mockStartAiGeneration).toHaveBeenCalledTimes(1);
+ expect(result.media.statuses).toEqual({ started: 1 });
+ expect(result.transcription.statuses).toEqual({ started: 1 });
+ expect(result.ai.statuses).toEqual({ started: 1 });
+ });
+
+ it("does not start a duplicate media workflow after a lost claim", async () => {
+ const mediaCandidate = {
+ videoId: "video-media",
+ userId: "user-1",
+ bucketId: null,
+ rawFileKey: "user-1/video-media/raw.mp4",
+ sourceType: "webMP4",
+ updatedAt: staleAt,
+ };
+
+ let dbCall = 0;
+ mockDb.mockImplementation(() => {
+ dbCall++;
+ if (dbCall === 1) return makeSelectChain([mediaCandidate]);
+ if (dbCall <= 3) return makeSelectChain([]);
+ return makeUpdateChain(0);
+ });
+
+ const { recoverStalledVideoPipeline } = await import(
+ "@/lib/video-pipeline-recovery"
+ );
+ const result = await recoverStalledVideoPipeline({ now, concurrency: 1 });
+
+ expect(mockStart).not.toHaveBeenCalled();
+ expect(result.media.statuses).toEqual({ "already-claimed": 1 });
+ });
+
+ it("starts eligible AI generation when legacy metadata is null", async () => {
+ const aiCandidate = {
+ videoId: "video-ai",
+ userId: "user-1",
+ metadata: null,
+ updatedAt: staleAt,
+ stripeSubscriptionStatus: "active",
+ thirdPartyStripeSubscriptionId: null,
+ };
+
+ let dbCall = 0;
+ mockDb.mockImplementation(() => {
+ dbCall++;
+ if (dbCall < 3) return makeSelectChain([]);
+ return makeSelectChain([aiCandidate]);
+ });
+
+ const { recoverStalledVideoPipeline } = await import(
+ "@/lib/video-pipeline-recovery"
+ );
+ const result = await recoverStalledVideoPipeline({ now, concurrency: 1 });
+
+ expect(mockStartAiGeneration).toHaveBeenCalledWith("video-ai", "user-1");
+ expect(result.ai.statuses).toEqual({ started: 1 });
+ });
+});
diff --git a/apps/web/app/api/cron/recover-failed-video-processing/route.ts b/apps/web/app/api/cron/recover-failed-video-processing/route.ts
index 6574a6e1b..a5b735a42 100644
--- a/apps/web/app/api/cron/recover-failed-video-processing/route.ts
+++ b/apps/web/app/api/cron/recover-failed-video-processing/route.ts
@@ -1,5 +1,6 @@
import { timingSafeEqual } from "node:crypto";
import { NextResponse } from "next/server";
+import { recoverStalledVideoPipeline } from "@/lib/video-pipeline-recovery";
import { recoverFailedVideoProcessing } from "@/lib/video-processing-recovery";
export const dynamic = "force-dynamic";
@@ -23,10 +24,14 @@ export async function GET(request: Request) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
- const summary = await recoverFailedVideoProcessing();
+ const [summary, stalledPipeline] = await Promise.all([
+ recoverFailedVideoProcessing(),
+ recoverStalledVideoPipeline(),
+ ]);
return NextResponse.json({
success: true,
...summary,
+ stalledPipeline,
});
}
diff --git a/apps/web/lib/video-pipeline-recovery.ts b/apps/web/lib/video-pipeline-recovery.ts
new file mode 100644
index 000000000..f4cc8f09f
--- /dev/null
+++ b/apps/web/lib/video-pipeline-recovery.ts
@@ -0,0 +1,375 @@
+import { db } from "@cap/database";
+import { users, videos, videoUploads } from "@cap/database/schema";
+import type { VideoMetadata } from "@cap/database/types";
+import { buildEnv } from "@cap/env";
+import type { User, Video } from "@cap/web-domain";
+import {
+ and,
+ asc,
+ eq,
+ gte,
+ inArray,
+ isNotNull,
+ isNull,
+ lte,
+ or,
+ sql,
+} from "drizzle-orm";
+import { start } from "workflow/api";
+import { isAiGenerationEnabledForUser } from "@/lib/ai-generation-entitlement";
+import { startAiGeneration } from "@/lib/generate-ai";
+import { transcribeVideo } from "@/lib/transcribe";
+import { processVideoWorkflow } from "@/workflows/process-video";
+
+export const STALLED_PIPELINE_MIN_AGE_MS = 60 * 60 * 1000;
+export const STALLED_PIPELINE_MAX_AGE_MS = 48 * 60 * 60 * 1000;
+export const STALLED_MEDIA_RECOVERY_BATCH_SIZE = 50;
+export const STALLED_TRANSCRIPTION_RECOVERY_BATCH_SIZE = 50;
+export const STALLED_AI_RECOVERY_BATCH_SIZE = 25;
+
+type PipelineRecoveryStatus =
+ | "started"
+ | "already-claimed"
+ | "retry-scheduled"
+ | "failed";
+
+type PipelineRecoveryResult = {
+ videoId: Video.VideoId;
+ status: PipelineRecoveryStatus;
+};
+
+type PipelineRecoveryGroup = {
+ checked: number;
+ statuses: Partial<Record<PipelineRecoveryStatus, number>>;
+ results: PipelineRecoveryResult[];
+};
+
+export type StalledVideoPipelineRecoverySummary = {
+ media: PipelineRecoveryGroup;
+ transcription: PipelineRecoveryGroup;
+ ai: PipelineRecoveryGroup;
+};
+
+const getAffectedRows = (result: unknown) => {
+ if (Array.isArray(result)) {
+ return (
+ (result[0] as { affectedRows?: number } | undefined)?.affectedRows ?? 0
+ );
+ }
+
+ return (result as { affectedRows?: number } | undefined)?.affectedRows ?? 0;
+};
+
+const summarize = (
+ results: PipelineRecoveryResult[],
+): PipelineRecoveryGroup => {
+ const statuses: PipelineRecoveryGroup["statuses"] = {};
+ for (const result of results) {
+ statuses[result.status] = (statuses[result.status] ?? 0) + 1;
+ }
+
+ return { checked: results.length, statuses, results };
+};
+
+async function recoverConcurrently<T extends { videoId: Video.VideoId }>(
+ candidates: T[],
+ concurrency: number,
+ recover: (candidate: T) => Promise<PipelineRecoveryStatus>,
+): Promise<PipelineRecoveryResult[]> {
+ const results: PipelineRecoveryResult[] = new Array(candidates.length);
+ let nextIndex = 0;
+
+ const workers = Array.from(
+ { length: Math.min(Math.max(1, concurrency), candidates.length) },
+ async () => {
+ while (nextIndex < candidates.length) {
+ const index = nextIndex++;
+ const candidate = candidates[index];
+ if (!candidate) continue;
+
+ let status: PipelineRecoveryStatus;
+ try {
+ status = await recover(candidate);
+ } catch (error) {
+ status = "failed";
+ console.error(
+ `[video-pipeline-recovery] Failed to recover ${candidate.videoId}`,
+ error,
+ );
+ }
+
+ results[index] = { videoId: candidate.videoId, status };
+ }
+ },
+ );
+
+ await Promise.all(workers);
+ return results;
+}
+
+type MediaCandidate = {
+ videoId: Video.VideoId;
+ userId: User.UserId;
+ bucketId: string | null;
+ rawFileKey: string;
+ updatedAt: Date;
+};
+
+async function recoverMediaCandidate(
+ candidate: MediaCandidate,
+): Promise<PipelineRecoveryStatus> {
+ const claimResult = await db()
+ .update(videoUploads)
+ .set({
+ processingProgress: 0,
+ processingMessage: "Restarting video processing",
+ processingError: null,
+ updatedAt: new Date(),
+ })
+ .where(
+ and(
+ eq(videoUploads.videoId, candidate.videoId),
+ eq(videoUploads.phase, "processing"),
+ eq(videoUploads.processingProgress, 0),
+ eq(videoUploads.processingMessage, "Starting video processing..."),
+ eq(videoUploads.rawFileKey, candidate.rawFileKey),
+ eq(videoUploads.updatedAt, candidate.updatedAt),
+ ),
+ );
+
+ if (getAffectedRows(claimResult) === 0) return "already-claimed";
+
+ try {
+ await start(processVideoWorkflow, [
+ {
+ videoId: candidate.videoId,
+ userId: candidate.userId,
+ rawFileKey: candidate.rawFileKey,
+ bucketId: candidate.bucketId,
+ },
+ ]);
+
+ return "started";
+ } catch {
+ await db()
+ .update(videoUploads)
+ .set({
+ processingMessage: "Processing recovery will retry automatically",
+ updatedAt: new Date(),
+ })
+ .where(
+ and(
+ eq(videoUploads.videoId, candidate.videoId),
+ eq(videoUploads.phase, "processing"),
+ ),
+ );
+ return "retry-scheduled";
+ }
+}
+
+type TranscriptionCandidate = {
+ videoId: Video.VideoId;
+ userId: string;
+ transcriptionStatus: "PROCESSING" | null;
+ updatedAt: Date;
+ stripeSubscriptionStatus: string | null;
+ thirdPartyStripeSubscriptionId: string | null;
+};
+
+async function recoverTranscriptionCandidate(
+ candidate: TranscriptionCandidate,
+): Promise<PipelineRecoveryStatus> {
+ if (candidate.transcriptionStatus === "PROCESSING") {
+ const claimResult = await db()
+ .update(videos)
+ .set({ transcriptionStatus: null })
+ .where(
+ and(
+ eq(videos.id, candidate.videoId),
+ eq(videos.transcriptionStatus, "PROCESSING"),
+ eq(videos.updatedAt, candidate.updatedAt),
+ ),
+ );
+
+ if (getAffectedRows(claimResult) === 0) return "already-claimed";
+ }
+
+ const result = await transcribeVideo(
+ candidate.videoId,
+ candidate.userId,
+ isAiGenerationEnabledForUser(candidate),
+ );
+
+ if (!result.success) return "retry-scheduled";
+ return result.message === "Transcription workflow started"
+ ? "started"
+ : "already-claimed";
+}
+
+type AiCandidate = {
+ videoId: Video.VideoId;
+ userId: string;
+ metadata: VideoMetadata | null;
+ updatedAt: Date;
+ stripeSubscriptionStatus: string | null;
+ thirdPartyStripeSubscriptionId: string | null;
+};
+
+async function recoverAiCandidate(
+ candidate: AiCandidate,
+): Promise<PipelineRecoveryStatus> {
+ const metadata = candidate.metadata ?? {};
+ const status = metadata.aiGenerationStatus;
+ if (status === "QUEUED" || status === "PROCESSING") {
+ const claimResult = await db()
+ .update(videos)
+ .set({
+ metadata: {
+ ...metadata,
+ aiGenerationStatus: "ERROR",
+ },
+ })
+ .where(
+ and(
+ eq(videos.id, candidate.videoId),
+ eq(videos.updatedAt, candidate.updatedAt),
+ sql`JSON_UNQUOTE(JSON_EXTRACT(${videos.metadata}, '$.aiGenerationStatus')) = ${status}`,
+ ),
+ );
+
+ if (getAffectedRows(claimResult) === 0) return "already-claimed";
+ }
+
+ const result = await startAiGeneration(candidate.videoId, candidate.userId);
+ if (!result.success) return "failed";
+ return result.message === "AI generation workflow started"
+ ? "started"
+ : "already-claimed";
+}
+
+export async function recoverStalledVideoPipeline({
+ now = new Date(),
+ mediaLimit = STALLED_MEDIA_RECOVERY_BATCH_SIZE,
+ transcriptionLimit = STALLED_TRANSCRIPTION_RECOVERY_BATCH_SIZE,
+ aiLimit = STALLED_AI_RECOVERY_BATCH_SIZE,
+ concurrency = 5,
+}: {
+ now?: Date;
+ mediaLimit?: number;
+ transcriptionLimit?: number;
+ aiLimit?: number;
+ concurrency?: number;
+} = {}): Promise<StalledVideoPipelineRecoverySummary> {
+ const staleBefore = new Date(now.getTime() - STALLED_PIPELINE_MIN_AGE_MS);
+ const recentBefore = new Date(now.getTime() - STALLED_PIPELINE_MAX_AGE_MS);
+
+ const mediaCandidates = await db()
+ .select({
+ videoId: videos.id,
+ userId: videos.ownerId,
+ bucketId: videos.bucket,
+ rawFileKey: videoUploads.rawFileKey,
+ updatedAt: videoUploads.updatedAt,
+ })
+ .from(videos)
+ .innerJoin(videoUploads, eq(videos.id, videoUploads.videoId))
+ .where(
+ and(
+ eq(videoUploads.phase, "processing"),
+ eq(videoUploads.processingProgress, 0),
+ eq(videoUploads.processingMessage, "Starting video processing..."),
+ isNotNull(videoUploads.rawFileKey),
+ lte(videoUploads.updatedAt, staleBefore),
+ gte(videoUploads.startedAt, recentBefore),
+ sql`JSON_UNQUOTE(JSON_EXTRACT(${videos.source}, '$.type')) = 'webMP4'`,
+ ),
+ )
+ .orderBy(asc(videoUploads.updatedAt))
+ .limit(mediaLimit);
+
+ const transcriptionCandidates = await db()
+ .select({
+ videoId: videos.id,
+ userId: videos.ownerId,
+ transcriptionStatus: videos.transcriptionStatus,
+ updatedAt: videos.updatedAt,
+ stripeSubscriptionStatus: users.stripeSubscriptionStatus,
+ thirdPartyStripeSubscriptionId: users.thirdPartyStripeSubscriptionId,
+ })
+ .from(videos)
+ .innerJoin(users, eq(videos.ownerId, users.id))
+ .leftJoin(videoUploads, eq(videos.id, videoUploads.videoId))
+ .where(
+ and(
+ isNull(videoUploads.videoId),
+ eq(videos.isScreenshot, false),
+ gte(videos.createdAt, recentBefore),
+ lte(videos.updatedAt, staleBefore),
+ or(
+ isNull(videos.transcriptionStatus),
+ eq(videos.transcriptionStatus, "PROCESSING"),
+ ),
+ ),
+ )
+ .orderBy(asc(videos.updatedAt))
+ .limit(transcriptionLimit);
+
+ const aiScanLimit = buildEnv.NEXT_PUBLIC_IS_CAP ? aiLimit * 4 : aiLimit;
+ const aiCandidates = (
+ await db()
+ .select({
+ videoId: videos.id,
+ userId: videos.ownerId,
+ metadata: videos.metadata,
+ updatedAt: videos.updatedAt,
+ stripeSubscriptionStatus: users.stripeSubscriptionStatus,
+ thirdPartyStripeSubscriptionId: users.thirdPartyStripeSubscriptionId,
+ })
+ .from(videos)
+ .innerJoin(users, eq(videos.ownerId, users.id))
+ .leftJoin(videoUploads, eq(videos.id, videoUploads.videoId))
+ .where(
+ and(
+ isNull(videoUploads.videoId),
+ eq(videos.isScreenshot, false),
+ eq(videos.transcriptionStatus, "COMPLETE"),
+ gte(videos.createdAt, recentBefore),
+ lte(videos.updatedAt, staleBefore),
+ or(
+ isNull(
+ sql`JSON_EXTRACT(${videos.metadata}, '$.aiGenerationStatus')`,
+ ),
+ inArray(
+ sql<string>`JSON_UNQUOTE(JSON_EXTRACT(${videos.metadata}, '$.aiGenerationStatus'))`,
+ ["QUEUED", "PROCESSING"],
+ ),
+ ),
+ sql`(${videos.metadata} IS NULL OR JSON_EXTRACT(${videos.metadata}, '$.summary') IS NULL OR JSON_EXTRACT(${videos.metadata}, '$.chapters') IS NULL)`,
+ ),
+ )
+ .orderBy(asc(videos.updatedAt))
+ .limit(aiScanLimit)
+ )
+ .filter(isAiGenerationEnabledForUser)
+ .slice(0, aiLimit) as AiCandidate[];
+
+ const [media, transcription, ai] = await Promise.all([
+ recoverConcurrently(
+ mediaCandidates as MediaCandidate[],
+ concurrency,
+ recoverMediaCandidate,
+ ),
+ recoverConcurrently(
+ transcriptionCandidates as TranscriptionCandidate[],
+ concurrency,
+ recoverTranscriptionCandidate,
+ ),
+ recoverConcurrently(aiCandidates, concurrency, recoverAiCandidate),
+ ]);
+
+ return {
+ media: summarize(media),
+ transcription: summarize(transcription),
+ ai: summarize(ai),
+ };
+}