fix: release QA hardening across processing, media, security, and CI gates (#649)

A release-readiness QA pass over the whole product. The commits split into
defects a user would hit and gates that were reporting green while measuring
nothing.

## Fixes that change behaviour

Rate limiting was bypassable on every install: TRUST_PROXY defaulted to true, so
request.ip came from a client-set header and a forged X-Forwarded-For got past
the login limiter. The default is now a private-network trust list.

A transient Postgres outage stranded in-flight jobs, leaving finished output on
disk with no row pointing at it. A reconciler now resolves those rows and adopts
the bytes rather than dropping the work.

A Redis connection that moved to a new address wedged every read-blocked
consumer, so completions stopped signalling while health still answered 200.
Socket timeouts plus subscriber pings recover it.

Installing more than one AI bundle left the shared venv multi-versioned and
silently broke three tools. The installer now reconciles distributions to one
version each.

Converting an image to JXL at quality 1 through 4 returned a 500, because
libjxl 0.7 rejects the distance those values compute. The quality is floored at
what the encoder honours. A missing ffmpeg was also reported to the user as a
corrupt upload; it now says the engine is unavailable.

RAW uploads reached an unpatched LibRaw on arm64, so it is built from source at
0.22.2, and the release scan was split so it can fail on an unfixed critical
instead of hiding it behind ignore-unfixed.

## Gates that could not fail

Two mutation lanes ran zero mutants because Stryker crawled the gitignored docs
build; coverage discarded its whole report on any failing test; the lint gate
skipped root tests, scripts, and two workspaces; and several generated matrices
counted a host missing ffmpeg as a passing tool. Each now measures what it
claims.

Full evidence and the outstanding release items are tracked locally and are not
part of this branch.
This commit is contained in:
SnapOtter
2026-07-27 15:37:30 +08:00
committed by GitHub
parent bc32f86a07
commit d10d0f544f
855 changed files with 54564 additions and 13092 deletions
+111
View File
@@ -1,15 +1,24 @@
import { setTimeout as delay } from "node:timers/promises";
import { eq } from "drizzle-orm";
import type { FastifyInstance } from "fastify";
import { expect } from "vitest";
import { db, schema } from "../../apps/api/src/db/index.js";
import { requestCancel } from "../../apps/api/src/jobs/cancel.js";
import { waitForJob } from "../../apps/api/src/jobs/enqueue.js";
import { getQueue } from "../../apps/api/src/jobs/queues.js";
import type { Pool, ToolJobResult } from "../../apps/api/src/jobs/types.js";
import { resolveToolPool } from "../../apps/api/src/lib/pool.js";
const LIVE_DATABASE_STATUSES = new Set(["queued", "processing"]);
const TERMINAL_QUEUE_STATES = new Set(["completed", "failed"]);
export interface DownloadedJobArtifact {
buffer: Buffer;
contentType: string;
filename: string;
result: ToolJobResult;
}
export class AcceptedJobTimeoutError extends Error {
constructor(jobId: string, timeoutMs: number) {
super(`Job ${jobId} did not finish within ${timeoutMs}ms and was canceled`);
@@ -141,3 +150,105 @@ export async function waitForAcceptedJobOrCancel(
await cancelAcceptedJobAndWait(jobId, pool);
throw new AcceptedJobTimeoutError(jobId, timeoutMs);
}
async function downloadCompletedJobArtifact(
app: FastifyInstance,
token: string,
toolId: string,
jobId: string,
result: ToolJobResult,
): Promise<DownloadedJobArtifact> {
if (result.outputRefs.length === 0) {
throw new Error(`${toolId}: completed job ${jobId} produced no downloadable artifact`);
}
if (!result.filename) throw new Error(`${toolId}: completed job ${jobId} has no filename`);
const download = await app.inject({
method: "GET",
url: `/api/v1/download/${encodeURIComponent(jobId)}/${encodeURIComponent(result.filename)}`,
headers: { authorization: `Bearer ${token}` },
});
if (download.statusCode !== 200) {
throw new Error(
`${toolId}: completed job ${jobId} artifact download returned ${download.statusCode}`,
);
}
if (download.rawPayload.length === 0) {
throw new Error(`${toolId}: completed job ${jobId} produced an empty artifact`);
}
if (result.processedSize <= 0 || download.rawPayload.length !== result.processedSize) {
throw new Error(
`${toolId}: completed job ${jobId} artifact size mismatch ` +
`(worker=${result.processedSize}, downloaded=${download.rawPayload.length})`,
);
}
const downloadedType = String(download.headers["content-type"] ?? "")
.split(";", 1)[0]
.toLowerCase();
const workerType = result.contentType.split(";", 1)[0].toLowerCase();
if (!downloadedType || downloadedType !== workerType) {
throw new Error(
`${toolId}: completed job ${jobId} artifact MIME mismatch ` +
`(worker=${workerType}, downloaded=${downloadedType || "missing"})`,
);
}
return {
buffer: Buffer.from(download.rawPayload),
contentType: downloadedType,
filename: result.filename,
result,
};
}
/**
* Wait for terminal success and return verified downloadable bytes. Installed
* capability tests use this when output semantics, not queue admission, are the
* release contract.
*/
export async function waitForDownloadedJobArtifact(
app: FastifyInstance,
token: string,
toolId: string,
jobId: string,
timeoutMs = 120_000,
): Promise<DownloadedJobArtifact> {
const result = await waitForAcceptedJobOrCancel(jobId, resolveToolPool(toolId), timeoutMs);
return downloadCompletedJobArtifact(app, token, toolId, jobId, result);
}
/**
* Resolve a generated-matrix 202 through terminal success and prove that its
* worker result is observable as either a downloadable artifact or a
* non-empty structured payload. Admission alone is never coverage.
*/
export async function waitForGeneratedJobArtifact(
app: FastifyInstance,
token: string,
toolId: string,
jobId: string,
timeoutMs = 120_000,
): Promise<ToolJobResult> {
const result = await waitForAcceptedJobOrCancel(jobId, resolveToolPool(toolId), timeoutMs);
if (result.outputRefs.length > 0) {
await downloadCompletedJobArtifact(app, token, toolId, jobId, result);
return result;
}
const payload = result.resultPayload;
if (!payload || Object.keys(payload).length === 0) {
throw new Error(`${toolId}: completed job ${jobId} produced no artifact or result payload`);
}
if (payload.success === false || payload.error !== undefined) {
// Carry the worker's own message. Without it every failure reads alike, and
// a caller cannot tell a product defect from a host with no ffmpeg.
const detail =
typeof payload.error === "string"
? payload.error
: ((payload.error as { message?: unknown } | undefined)?.message ?? "");
const suffix = detail ? `: ${String(detail)}` : "";
throw new Error(`${toolId}: completed job ${jobId} returned a failure payload${suffix}`);
}
return result;
}