Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 7 additions & 17 deletions apps/api/src/routes/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,24 +105,14 @@ video.openapi(getVideoStatus, async (c) => {
let content:
| { type: "video"; url: string; mime_type?: string | null }[]
| undefined;
if (job.status === "completed") {
const log = await db.query.log.findFirst({
where: {
requestId: { eq: job.requestId },
if (job.status === "completed" && job.logId) {
content = [
{
type: "video" as const,
url: buildSignedGatewayVideoLogContentUrl(job.logId),
mime_type: job.contentType ?? null,
},
columns: {
id: true,
},
});
if (log) {
content = [
{
type: "video" as const,
url: buildSignedGatewayVideoLogContentUrl(log.id),
mime_type: job.contentType ?? null,
},
];
}
];
}

return c.json(
Expand Down
29 changes: 4 additions & 25 deletions apps/gateway/src/videos/videos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2227,21 +2227,6 @@ function getInlineGoogleVertexVideoFromBodies(
return null;
}

async function getVideoLogIdByRequestId(
requestId: string,
): Promise<string | null> {
const existingLog = await db
.select({
id: tables.log.id,
})
.from(tables.log)
.where(eq(tables.log.requestId, requestId))
.limit(1)
.then((rows) => rows[0]);

return existingLog?.id ?? null;
}

async function getPublicVideoContentUrl(
job: VideoJobRecord,
logId?: string | null,
Expand All @@ -2250,8 +2235,7 @@ async function getPublicVideoContentUrl(
return null;
}

const resolvedLogId =
logId ?? (await getVideoLogIdByRequestId(job.requestId));
const resolvedLogId = logId ?? job.logId;
if (
resolvedLogId &&
(job.contentUrl ||
Expand Down Expand Up @@ -4498,12 +4482,7 @@ videos.openapi(getVideoLogContent, async (c) => {
const videoJob = await db
.select()
.from(tables.videoJob)
.where(
and(
eq(tables.videoJob.projectId, log.projectId),
eq(tables.videoJob.requestId, log.requestId),
),
)
.where(eq(tables.videoJob.logId, log.id))
.limit(1)
.then((rows) => rows[0]);
if (!videoJob) {
Expand Down Expand Up @@ -4571,7 +4550,7 @@ videos.openapi(getVideoContent, async (c) => {

if (!job.contentUrl && !job.storageUri) {
if (shouldProxyDirectUpstreamVideoContent(job)) {
const logId = await getVideoLogIdByRequestId(job.requestId);
const logId = job.logId;
const response = await streamDirectUpstreamVideoContent(job);
if (logId) {
await markVideoDownloaded(logId);
Expand Down Expand Up @@ -4617,7 +4596,7 @@ videos.openapi(getVideoContent, async (c) => {
});
}

const logId = await getVideoLogIdByRequestId(job.requestId);
const logId = job.logId;
const response = await streamVideoFromUrl(contentUrl, job.contentType);
if (logId) {
await markVideoDownloaded(logId);
Expand Down
25 changes: 7 additions & 18 deletions apps/worker/src/services/video-jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -662,21 +662,6 @@ function getInlineGoogleVertexVideo(
return null;
}

async function getVideoLogIdByRequestId(
requestId: string,
): Promise<string | null> {
const existingLog = await db
.select({
id: tables.log.id,
})
.from(tables.log)
.where(eq(tables.log.requestId, requestId))
.limit(1)
.then((rows) => rows[0]);

return existingLog?.id ?? null;
}

async function getPublicVideoContentUrl(
job: VideoJobRecord,
logId?: string | null,
Expand All @@ -685,8 +670,7 @@ async function getPublicVideoContentUrl(
return null;
}

const resolvedLogId =
logId ?? (await getVideoLogIdByRequestId(job.requestId));
const resolvedLogId = logId ?? job.logId;
if (
resolvedLogId &&
(job.contentUrl || job.storageUri || getInlineGoogleVertexVideo(job))
Expand Down Expand Up @@ -1769,7 +1753,12 @@ async function finalizeVideoJob(job: VideoJobRecord): Promise<void> {
dataStorageCost: "0",
});

return jobToLog;
await tx
.update(tables.videoJob)
.set({ logId })
.where(eq(tables.videoJob.id, jobToLog.id));

return { ...jobToLog, logId };
});

if (claimedJob?.contentUrl) {
Expand Down
4 changes: 4 additions & 0 deletions packages/db/migrations/1781647635_striped_sunspot.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE "video_job" ADD COLUMN "log_id" text;--> statement-breakpoint
UPDATE "video_job" SET "log_id" = "log"."id" FROM "log" WHERE "log"."request_id" = "video_job"."request_id" AND "log"."project_id" = "video_job"."project_id" AND "log"."organization_id" = "video_job"."organization_id";--> statement-breakpoint

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Make the backfill ignore ambiguous request IDs

Fresh evidence: this revised backfill now scopes by project and organization, but it still updates from every log row sharing the client-supplied request_id. Since x-request-id is accepted from callers and log.request_id is only indexed, a single project can have multiple matching logs for the same value (for example a retry, or a chat request and a video request reusing the header); PostgreSQL UPDATE ... FROM can then choose an arbitrary log.id. After this change the gateway resolves video content solely through video_job.log_id, so an ambiguous historical backfill can attach the job to the wrong log and make the real video log URL 404 or mark downloads on an unrelated log.

Useful? React with 👍 / 👎.

CREATE INDEX "video_job_log_id_idx" ON "video_job" ("log_id");--> statement-breakpoint
ALTER TABLE "video_job" ADD CONSTRAINT "video_job_log_id_log_id_fkey" FOREIGN KEY ("log_id") REFERENCES "log"("id") ON DELETE SET NULL;
Loading
Loading