Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (codex): reject disabled-provider and over-cap uploads before staging

[._.]/ Adam Eivy2bee4cb2ff6a
diff --git a/server/routes/imageGen.multipart.test.js b/server/routes/imageGen.multipart.test.js
index 60d9ca9863..7083d9470c 100644
--- a/server/routes/imageGen.multipart.test.js
+++ b/server/routes/imageGen.multipart.test.js
@@ -333,6 +333,60 @@ describe('POST /api/image-gen/generate — multipart reference-image packing', (
for (const p of params.referenceImagePaths) expect(p.startsWith(refsSandbox)).toBe(true);
});
+ it('rejects an upload for a DISABLED cloud backend before staging it', async () => {
+ // The route throws the same disabledError a few steps later, but by then
+ // the copy is in PATHS.imageRefs and the route's res.on('close') sweep
+ // covers only multer temps — so a late rejection strands the staged file.
+ mockedSettings = { imageGen: { mode: 'agy', agy: { enabled: false } } };
+ const res = await postMultipart(app, '/api/image-gen/generate', [
+ { name: 'prompt', value: 'a fox' },
+ { name: 'referenceImage1', filename: 'a.png', contentType: 'image/png', value: PNG_FIXTURE },
+ ]);
+
+ expect(res.status).toBe(400);
+ expect(res.body.code || res.body.error).toMatch(/AGY_IMAGEGEN_DISABLED|disabled/i);
+ expect(enqueueJob).not.toHaveBeenCalled();
+ await new Promise((r) => setTimeout(r, 50));
+ const refDirContents = await readdir(refsSandbox).catch(() => []);
+ expect(refDirContents.filter((f) => f.startsWith('ref-'))).toHaveLength(0);
+ });
+
+ it('rejects more input images than the backend accepts instead of silently dropping them', async () => {
+ // Agy's generate_image takes at most 3 images total. A direct API caller
+ // sending 4 used to 200 while the provider quietly kept the first 3 — and
+ // the dropped copies stayed on disk. The form caps its own slots, so this
+ // guards the non-form callers.
+ mockedSettings = { imageGen: { mode: 'agy', agy: { enabled: true } } };
+ const res = await postMultipart(app, '/api/image-gen/generate', [
+ { name: 'prompt', value: 'a fox' },
+ { name: 'referenceImage1', filename: 'a.png', contentType: 'image/png', value: PNG_FIXTURE },
+ { name: 'referenceImage2', filename: 'b.png', contentType: 'image/png', value: PNG_FIXTURE },
+ { name: 'referenceImage3', filename: 'c.png', contentType: 'image/png', value: PNG_FIXTURE },
+ { name: 'referenceImage4', filename: 'd.png', contentType: 'image/png', value: PNG_FIXTURE },
+ ]);
+
+ expect(res.status).toBe(400);
+ expect(res.body.code || res.body.error).toMatch(/TOO_MANY_INPUT_IMAGES|at most 3/i);
+ expect(enqueueJob).not.toHaveBeenCalled();
+ await new Promise((r) => setTimeout(r, 50));
+ const refDirContents = await readdir(refsSandbox).catch(() => []);
+ expect(refDirContents.filter((f) => f.startsWith('ref-'))).toHaveLength(0);
+ });
+
+ it('accepts exactly the backend cap — 3 references on agy with no init image', async () => {
+ // The boundary the gate above must NOT reject.
+ mockedSettings = { imageGen: { mode: 'agy', agy: { enabled: true } } };
+ const res = await postMultipart(app, '/api/image-gen/generate', [
+ { name: 'prompt', value: 'a fox' },
+ { name: 'referenceImage1', filename: 'a.png', contentType: 'image/png', value: PNG_FIXTURE },
+ { name: 'referenceImage2', filename: 'b.png', contentType: 'image/png', value: PNG_FIXTURE },
+ { name: 'referenceImage3', filename: 'c.png', contentType: 'image/png', value: PNG_FIXTURE },
+ ]);
+
+ expect(res.status).toBe(200);
+ expect(enqueueJob.mock.calls.at(-1)[0].params.referenceImagePaths).toHaveLength(3);
+ });
+
it('unlinks an already-staged ref when the prompt gate rejects a prompt-less agy render', async () => {
// The one throw that can fire with an upload ALREADY copied into
// PATHS.imageRefs: agy's tool lists `Prompt` as required, so a prompt-less
diff --git a/server/services/imageGen/prepareParams.js b/server/services/imageGen/prepareParams.js
index df37b41558..45ee686787 100644
--- a/server/services/imageGen/prepareParams.js
+++ b/server/services/imageGen/prepareParams.js
@@ -3,7 +3,8 @@
*
* Handles everything between Zod validation and the final dispatch branch:
* - resolve effective backend mode + per-render cleaners
- * - gate reference-image uploads to backends that can consume them
+ * - gate input-image uploads to backends that can consume them, are enabled,
+ * and can take that many — all BEFORE anything is staged to disk
* - stage multer temp uploads into PATHS.images / PATHS.imageRefs
* - resolve initImagePath from upload or gallery filename
* - enforce each cloud CLI's prompt requirement
@@ -29,7 +30,9 @@ import { PATHS, ensureDir, resolveGalleryImage } from '../../lib/fileUtils.js';
import { getSettings } from '../settings.js';
import { IMAGE_GEN_MODE, resolveImageCleaners } from './index.js';
import { editIncapableModeError, isEditCapableMode, modeLabel } from './modes.js';
-import { cloudPromptRequired, resolveRenderTargetConfig } from './cloudProviderConfig.js';
+import {
+ cloudPromptRequired, maxInputImages, resolveCloudProviderConfig, resolveRenderTargetConfig,
+} from './cloudProviderConfig.js';
import { RENDER_TARGET, recordRenderPin } from '../../lib/renderTargets.js';
import { getProject as getMusicVideoProject } from '../musicVideo/projects.js';
import { getImageModels, isFlux2 } from '../../lib/mediaModels.js';
@@ -121,6 +124,18 @@ export async function prepareGenerateParams({ data, files, referenceImageFields
throw editIncapableModeError(mode);
}
+ // A disabled cloud CLI is rejected BEFORE anything is staged. The route
+ // throws the same `disabledError` a few steps later, but by then the uploads
+ // have been copied into PATHS.imageRefs and the route's `res.on('close')`
+ // sweep only covers multer temps — so the staged copies would survive the
+ // 400 forever. Same "reject up-front rather than copy-then-fail" rule the
+ // reference gates below follow.
+ const cloudConfig = resolveCloudProviderConfig(settings, mode);
+ if (cloudConfig && !cloudConfig.enabled && (initUpload || data.initImageFile || referenceUploads.length)) {
+ cleanupReqFilesTemp();
+ throw cloudConfig.disabledError;
+ }
+
// Resolve cleaners ONCE at the route layer so all three dispatch paths
// (synchronous external, codex queue, local queue) see the same values.
// Stamp onto `data` so they flow through the spread-into-params calls
@@ -157,6 +172,24 @@ export async function prepareGenerateParams({ data, files, referenceImageFields
}
}
+ // Reject an over-cap request instead of staging every upload and letting
+ // `resolveInputImages` silently keep the first N. Same reasoning as the
+ // gates above: the extra copies would be orphaned in PATHS.imageRefs, and
+ // the render would 200 while quietly ignoring images the caller submitted.
+ // The Image Gen form already caps its slots per backend (referenceSlotsFor),
+ // so this only fires for direct API callers — but a silent drop is exactly
+ // the failure the sidecar-honesty rule exists to prevent. `resolveInputImages`
+ // keeps its own cap as the backstop for in-process callers that skip the route.
+ const inputImageCount = (initUpload || data.initImageFile ? 1 : 0) + referenceUploads.length;
+ const inputImageCap = maxInputImages(mode);
+ if (inputImageCap != null && inputImageCount > inputImageCap) {
+ cleanupReqFilesTemp();
+ throw new ServerError(
+ `${modeLabel(mode)} accepts at most ${inputImageCap} input images (init image + references); received ${inputImageCount}`,
+ { status: 400, code: 'TOO_MANY_INPUT_IMAGES' },
+ );
+ }
+
if (initUpload || referenceUploads.length) await ensureDir(PATHS.imageRefs);
if (initUpload) {
// Trust the validated mimetype from the fileFilter — picking the ext
diff --git a/server/services/imageGen/renderTargets.guard.test.js b/server/services/imageGen/renderTargets.guard.test.js
index 4cf0ea09f4..8ce882c7af 100644
--- a/server/services/imageGen/renderTargets.guard.test.js
+++ b/server/services/imageGen/renderTargets.guard.test.js
@@ -24,10 +24,18 @@ const SERVER_ROOT = join(dirname(fileURLToPath(import.meta.url)), '..', '..');
// dispatcher pair: the request carries an explicit mode + cloudModel, so
// there is no render target to consult (the Image Gen page IS the manual
// surface).
+// - services/imageGen/prepareParams.js — the extracted pre-dispatch half of
+// routes/imageGen.js, on the same request. It does NOT resolve a backend
+// (`mode` is already resolved above it, through resolveRenderTargetConfig on
+// the music-video path); it only reads `enabled` so a disabled provider is
+// rejected BEFORE uploads are staged to PATHS.imageRefs. Rejecting later, in
+// the route, strands those staged copies — the route's res.on('close') sweep
+// covers only multer temps.
const ALLOWED = new Set([
'services/imageGen/cloudProviderConfig.js',
'routes/imageGen.js',
'services/imageGen/index.js',
+ 'services/imageGen/prepareParams.js',
]);
describe('render-target resolver guard (#3231)', () => {