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
+14 -4
View File
@@ -15,6 +15,7 @@ export interface RemoveBackgroundOptions {
backgroundColor?: string;
edgeRefine?: number;
decontaminate?: boolean;
signal?: AbortSignal;
}
const MAX_REMBG_PX = Number(process.env.MAX_REMBG_PX) || 2048;
@@ -43,6 +44,7 @@ export async function removeBackground(
options: RemoveBackgroundOptions = {},
onProgress?: ProgressCallback,
): Promise<Buffer> {
const { signal, ...sidecarOptions } = options;
const id = randomUUID();
const inputPath = join(tmpdir(), `rembg_in_${id}.png`);
const outputPath = join(outputDir, `rembg_out_${id}.png`);
@@ -67,10 +69,17 @@ export async function removeBackground(
try {
const megapixels = (origW * origH) / 1_000_000;
const baseTimeout = options.model?.startsWith("birefnet") ? 600000 : 300000;
const baseTimeout = sidecarOptions.model?.startsWith("birefnet") ? 600000 : 300000;
const timeout = Math.max(baseTimeout, megapixels * 30 * 1000);
const rawMask = await runAndParse(inputPath, outputPath, options, onProgress, timeout);
const rawMask = await runAndParse(
inputPath,
outputPath,
sidecarOptions,
onProgress,
timeout,
signal,
);
if (needsDownscale) {
return sharp(rawMask).resize({ width: origW, height: origH, fit: "fill" }).png().toBuffer();
@@ -89,12 +98,13 @@ async function runAndParse(
options: RemoveBackgroundOptions,
onProgress: ProgressCallback | undefined,
timeout: number,
signal: AbortSignal | undefined,
): Promise<Buffer> {
try {
const { stdout } = await runPythonWithProgress(
"remove_bg.py",
[inputPath, outputPath, JSON.stringify(options)],
{ onProgress, timeout },
{ onProgress, timeout, signal },
);
const result = parseStdoutJson(stdout);
if (!result.success) {
@@ -112,7 +122,7 @@ async function runAndParse(
const { stdout } = await runPythonWithProgress(
"remove_bg.py",
[inputPath, outputPath, JSON.stringify(fallbackOpts)],
{ onProgress, timeout: 300000 },
{ onProgress, timeout: 300000, signal },
);
const result = parseStdoutJson(stdout);
if (!result.success) {
+132 -54
View File
@@ -1,6 +1,7 @@
import { type ChildProcess, spawn } from "node:child_process";
import { randomUUID } from "node:crypto";
import { dirname, resolve } from "node:path";
import { existsSync } from "node:fs";
import { dirname, posix, resolve, win32 } from "node:path";
import { fileURLToPath } from "node:url";
import { context, propagation, SpanStatusCode, trace } from "@opentelemetry/api";
import { isSafeMessageError, SafeError } from "@snapotter/shared";
@@ -34,7 +35,7 @@ function resolvePythonTimeout(explicitTimeout?: number): number | undefined {
* package resolution, and locale -- avoids leaking secrets or
* application config from the parent process.
*/
function buildMinimalEnv(): Record<string, string> {
export function buildMinimalEnv(): Record<string, string> {
const env: Record<string, string> = {
PYTHONUNBUFFERED: "1",
LANG: process.env.LANG || "C.UTF-8",
@@ -67,17 +68,13 @@ function buildMinimalEnv(): Record<string, string> {
env.DATA_DIR ??= "./data";
env.MODELS_PATH ??= appendEnvPath(env.DATA_DIR, "ai/models");
// Runtime model downloads are allowed by default (public model weights
// only, never user data). SNAPOTTER_ALLOW_MODEL_DOWNLOAD=0 enables strict
// offline mode for airgapped deployments: the sidecar then gets the
// Hugging Face offline flags and every download fallback raises an
// actionable error instead of fetching. Bundle installs stay exempt
// because install_feature.py lifts the flags in its own process.
const allowModelDownload = process.env.SNAPOTTER_ALLOW_MODEL_DOWNLOAD;
if (allowModelDownload !== undefined) {
env.SNAPOTTER_ALLOW_MODEL_DOWNLOAD = allowModelDownload;
}
if (allowModelDownload === "0" || allowModelDownload?.toLowerCase() === "false") {
// Runtime downloads require an explicit opt-in. Feature bundle installs are
// unaffected: install_feature.py intentionally lifts Hugging Face's offline
// flags in its own process while it installs the signed bundle.
const allowModelDownload = process.env.SNAPOTTER_ALLOW_MODEL_DOWNLOAD?.toLowerCase();
const modelDownloadsEnabled = allowModelDownload === "1" || allowModelDownload === "true";
env.SNAPOTTER_ALLOW_MODEL_DOWNLOAD = modelDownloadsEnabled ? "1" : "0";
if (!modelDownloadsEnabled) {
env.HF_HUB_OFFLINE = "1";
env.TRANSFORMERS_OFFLINE = "1";
}
@@ -87,7 +84,12 @@ function buildMinimalEnv(): Record<string, string> {
/** Try venv first, then system python. */
function getPythonPath(): string {
const venvPath = process.env.PYTHON_VENV_PATH || resolve(__dirname, "../../../.venv");
return `${venvPath}/bin/python3`;
const path = process.platform === "win32" ? win32 : posix;
const venvPython = path.join(
venvPath,
process.platform === "win32" ? "Scripts/python.exe" : "bin/python3",
);
return existsSync(venvPython) ? venvPython : process.platform === "win32" ? "python" : "python3";
}
/**
@@ -186,6 +188,21 @@ function extractPythonError(error: unknown): string {
export type ProgressCallback = (percent: number, stage: string) => void;
export interface PythonRunOptions {
onProgress?: ProgressCallback;
signal?: AbortSignal;
timeout?: number;
}
function pythonAbortError(): Error {
const error = new SafeError("Python script canceled", {
kind: "operational",
code: "canceled",
});
error.name = "AbortError";
return error;
}
// ── PythonDispatcher class ─────────────────────────────────────────
interface PendingRequest {
@@ -483,43 +500,52 @@ export class PythonDispatcher {
private dispatcherRun(
scriptName: string,
args: string[],
options: { onProgress?: ProgressCallback; timeout?: number } = {},
options: PythonRunOptions = {},
): Promise<{ stdout: string; stderr: string }> | null {
if (options.signal?.aborted) return Promise.reject(pythonAbortError());
const proc = this.getChild();
if (!proc || !proc.stdin || !this.childReady) return null;
const stdin = proc?.stdin;
if (!proc || !stdin || !this.childReady) return null;
const id = randomUUID();
const timeout = resolvePythonTimeout(options.timeout);
return new Promise((resolvePromise, rejectPromise) => {
const timer =
timeout === undefined
? undefined
: setTimeout(() => {
this.pending.delete(id);
// Kill the stuck dispatcher so it restarts on the next request instead of
// blocking all subsequent operations behind the timed-out script.
if (this.child && !this.child.killed) {
this.child.kill("SIGTERM");
}
rejectPromise(
new SafeError("Python script timed out", {
kind: "operational",
code: "timeout",
}),
);
}, timeout);
let timer: ReturnType<typeof setTimeout> | undefined;
let onAbort: (() => void) | undefined;
const cleanup = () => {
if (timer) clearTimeout(timer);
if (onAbort) options.signal?.removeEventListener("abort", onAbort);
};
const wrappedResolve = (result: { stdout: string; stderr: string }) => {
if (timer) clearTimeout(timer);
cleanup();
resolvePromise(result);
};
const wrappedReject = (err: Error) => {
if (timer) clearTimeout(timer);
cleanup();
rejectPromise(err);
};
if (timeout !== undefined) {
timer = setTimeout(() => {
this.pending.delete(id);
// Kill the stuck dispatcher so it restarts on the next request instead of
// blocking all subsequent operations behind the timed-out script.
if (this.child && !this.child.killed) {
this.child.kill("SIGTERM");
}
wrappedReject(
new SafeError("Python script timed out", {
kind: "operational",
code: "timeout",
}),
);
}, timeout);
}
this.pending.set(id, {
resolve: wrappedResolve,
reject: wrappedReject,
@@ -530,6 +556,25 @@ export class PythonDispatcher {
generation: this.generation,
});
onAbort = () => {
this.pending.delete(id);
// The dispatcher executes synchronously and has no request-level cancel
// protocol, so stopping the process is the only way to stop Python work.
// Treat this as intentional teardown, not a dispatcher crash.
this.stoppedChildren.add(proc);
if (this.child === proc) {
this.child = null;
this.childReady = false;
}
if (!proc.killed) proc.kill("SIGTERM");
wrappedReject(pythonAbortError());
};
options.signal?.addEventListener("abort", onAbort, { once: true });
if (options.signal?.aborted) {
onAbort();
return;
}
const msg: Record<string, unknown> = { id, script: scriptName.replace(".py", ""), args };
const otelCarrier: Record<string, string> = {};
propagation.inject(context.active(), otelCarrier);
@@ -541,11 +586,10 @@ export class PythonDispatcher {
}
const request = JSON.stringify(msg);
try {
proc.stdin!.write(request + "\n");
stdin.write(`${request}\n`);
} catch {
this.pending.delete(id);
if (timer) clearTimeout(timer);
rejectPromise(
wrappedReject(
new SafeError("Python dispatcher stdin closed unexpectedly", {
kind: "operational",
code: "dispatcher-stdin-closed",
@@ -560,11 +604,9 @@ export class PythonDispatcher {
private runPerRequest(
scriptName: string,
args: string[],
options: {
onProgress?: ProgressCallback;
timeout?: number;
} = {},
options: PythonRunOptions = {},
): Promise<{ stdout: string; stderr: string }> {
if (options.signal?.aborted) return Promise.reject(pythonAbortError());
// Mirror the dispatcher's feature gate. The persistent dispatcher rejects
// scripts whose bundle is not installed (in Python); this fallback spawns
// scripts directly, so without the same check it would run them ungated
@@ -582,11 +624,44 @@ export class PythonDispatcher {
const timeout = resolvePythonTimeout(options.timeout);
return new Promise((resolvePromise, rejectPromise) => {
let activeAttempt = 0;
let activeProcess: ChildProcess | null = null;
let activeTimer: ReturnType<typeof setTimeout> | undefined;
let settled = false;
const cleanup = () => {
if (activeTimer) clearTimeout(activeTimer);
options.signal?.removeEventListener("abort", onAbort);
};
const rejectOnce = (error: Error) => {
if (settled) return;
settled = true;
cleanup();
rejectPromise(error);
};
const resolveOnce = (result: { stdout: string; stderr: string }) => {
if (settled) return;
settled = true;
cleanup();
resolvePromise(result);
};
const onAbort = () => {
if (settled) return;
const processToStop = activeProcess;
// Invalidate callbacks from the killed attempt before signaling it.
activeAttempt++;
if (processToStop && !processToStop.killed) processToStop.kill("SIGTERM");
rejectOnce(pythonAbortError());
};
const trySpawn = (pythonBin: string, isFallback: boolean) => {
if (settled) return;
const attempt = ++activeAttempt;
const proc = spawn(pythonBin, [scriptPath, ...args], {
stdio: ["ignore", "pipe", "pipe"],
env: this.buildEnv(),
});
activeProcess = proc;
let stdout = "";
const stderrLines: string[] = [];
@@ -597,9 +672,11 @@ export class PythonDispatcher {
timeout === undefined
? undefined
: setTimeout(() => {
if (attempt !== activeAttempt) return;
timedOut = true;
proc.kill("SIGTERM");
}, timeout);
activeTimer = timer;
proc.stdout.on("data", (chunk: Buffer) => {
stdout += chunk.toString();
@@ -629,10 +706,11 @@ export class PythonDispatcher {
proc.on("error", (err: NodeJS.ErrnoException) => {
if (timer) clearTimeout(timer);
if (attempt !== activeAttempt) return;
if (err.code === "ENOENT" && !isFallback) {
trySpawn("python3", true);
} else {
rejectPromise(
rejectOnce(
new SafeError(extractPythonError(err) || "Failed to start Python process", {
kind: "operational",
code: err.code ?? "spawn-error",
@@ -643,13 +721,14 @@ export class PythonDispatcher {
proc.on("close", (code, signal) => {
if (timer) clearTimeout(timer);
if (attempt !== activeAttempt) return;
if (stderrBuffer.trim()) {
stderrLines.push(stderrBuffer.trim());
}
if (timedOut) {
rejectPromise(
rejectOnce(
new SafeError("Python script timed out", { kind: "operational", code: "timeout" }),
);
return;
@@ -658,16 +737,21 @@ export class PythonDispatcher {
const stderr = stderrLines.join("\n");
if (code !== 0) {
rejectPromise(
rejectOnce(
pythonExitError(code, signal, extractPythonError({ stdout: stdout.trim(), stderr })),
);
return;
}
resolvePromise({ stdout: stdout.trim(), stderr });
resolveOnce({ stdout: stdout.trim(), stderr });
});
};
options.signal?.addEventListener("abort", onAbort, { once: true });
if (options.signal?.aborted) {
onAbort();
return;
}
trySpawn(getPythonPath(), false);
});
}
@@ -752,10 +836,7 @@ export class PythonDispatcher {
run(
scriptName: string,
args: string[],
options: {
onProgress?: ProgressCallback;
timeout?: number;
} = {},
options: PythonRunOptions = {},
): Promise<{ stdout: string; stderr: string }> {
const tracer = trace.getTracer("snapotter-sidecar");
const span = trace.getActiveSpan()
@@ -867,10 +948,7 @@ export function initDispatcher(timeoutMs = 30_000): Promise<{ ready: boolean; gp
export function runPythonWithProgress(
scriptName: string,
args: string[],
options: {
onProgress?: ProgressCallback;
timeout?: number;
} = {},
options: PythonRunOptions = {},
): Promise<{ stdout: string; stderr: string }> {
return getAiDispatcher().run(scriptName, args, options);
}
+2
View File
@@ -24,6 +24,7 @@ export interface TranscriptionResult {
export interface TranscribeOptions {
language: string;
signal?: AbortSignal;
}
export async function transcribeAudio(
@@ -39,6 +40,7 @@ export async function transcribeAudio(
const { stdout } = await runPythonWithProgress("transcribe.py", [inputPath, optionsJson], {
timeout: 30 * 60_000,
onProgress,
signal: opts.signal,
});
const result = parseStdoutJson(stdout);