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

feat: negotiate video frame and canvas constraints over federated media wire (#4681)

[._.]/ Adam Eivyf3e9974ba899
diff --git a/server/routes/imageGen.js b/server/routes/imageGen.js
index 2fb6ec8160..598ecd7334 100644
--- a/server/routes/imageGen.js
+++ b/server/routes/imageGen.js
@@ -455,7 +455,7 @@ router.post('/generate', imageGenUploads, asyncHandler(async (req, res) => {
// schema's overlap with it: this object is what gets persisted and replayed
// on every reconcile, so it must already be a body the provider accepts.
const request = buildFederatedMediaRequest({ kind: 'image', params });
- const { peer, request: negotiatedRequest, remoteMedia } = await prepareRemoteMediaJob({
+ const { peer, remoteMedia } = await prepareRemoteMediaJob({
peerId: params.mediaProviderPeerId,
kind: 'image',
request,
@@ -477,11 +477,10 @@ router.post('/generate', imageGenUploads, asyncHandler(async (req, res) => {
kind: 'image',
params: { ...jobParams, remoteMedia },
});
- const effectiveRequest = negotiatedRequest || request;
return res.json(queuedImageResponse({
...queued,
mode: null,
- model: effectiveRequest.modelId,
+ model: request.modelId,
mediaProviderPeerId: peer.id,
}));
}
diff --git a/server/routes/videoGen.js b/server/routes/videoGen.js
index ee0a1906b3..079da20385 100644
--- a/server/routes/videoGen.js
+++ b/server/routes/videoGen.js
@@ -1060,33 +1060,19 @@ router.post('/', frameImageUpload, asyncHandler(async (req, res) => {
kind: 'video',
request,
});
- const {
- mediaProviderPeerId: _peerId, mediaProviderEngine: _engine,
- mode: _mode, cloudModel: _cloudModel, ...jobParams
- } = body;
- const effectiveRequest = negotiatedRequest || request;
// Prompt and dials ride only inside the versioned marker: enqueueJob
// normalizes any job carrying one into the downgrade-safe shape, so a build
// rolled back past `remoteMedia` cannot re-render this locally. Contract:
// services/federatedMedia/routedJobParams.js.
const { jobId, position, status } = enqueueJob({
kind: 'video',
- params: {
- ...jobParams,
- ...(effectiveRequest?.numFrames !== undefined ? { numFrames: effectiveRequest.numFrames } : {}),
- ...(effectiveRequest?.width !== undefined ? { width: effectiveRequest.width } : {}),
- ...(effectiveRequest?.height !== undefined ? { height: effectiveRequest.height } : {}),
- remoteMedia,
- },
+ params: { remoteMedia },
});
return res.json({
jobId,
generationId: jobId,
filename: `${jobId}.mp4`,
- model: effectiveRequest.modelId,
- ...(effectiveRequest?.numFrames !== undefined ? { numFrames: effectiveRequest.numFrames } : {}),
- ...(effectiveRequest?.width !== undefined ? { width: effectiveRequest.width } : {}),
- ...(effectiveRequest?.height !== undefined ? { height: effectiveRequest.height } : {}),
+ model: request.modelId,
// No local backend is running this, so `mode` is null rather than a
// backend name the render never used.
mode: null,
diff --git a/server/services/federatedMedia/remoteSubmission.js b/server/services/federatedMedia/remoteSubmission.js
index 2e0950ab6f..7dc668269c 100644
--- a/server/services/federatedMedia/remoteSubmission.js
+++ b/server/services/federatedMedia/remoteSubmission.js
@@ -16,9 +16,9 @@ import { ServerError } from '../../lib/errorHandler.js';
import { FEDERATED_MEDIA_WIRE_VERSION } from '../../lib/federatedMediaWire.js';
/**
- * Negotiate frame count, frame rate, and model constraints against the provider capability.
- * Snaps numFrames against discrete frameOptions or nearest legal n*stride + 1, bounds against maxNumFrames,
- * and snaps fps against supported fpsOptions.
+ * Negotiate frame and canvas constraints against the provider capability.
+ * Snaps numFrames down to the nearest legal discrete option or n*stride + 1 (never up),
+ * bounds against maxNumFrames, and matches resolution against closest aspect/area preset.
*/
export function negotiateVideoConstraints(request, capability) {
if (!request || !capability) return request;
@@ -38,16 +38,19 @@ export function negotiateVideoConstraints(request, capability) {
const maxNumFrames = capability.maxNumFrames != null ? Number(capability.maxNumFrames) : null;
const validOptions = capability.frameOptions
.map(Number)
- .filter((opt) => Number.isInteger(opt) && opt > 0 && (maxNumFrames === null || opt <= maxNumFrames));
- if (validOptions.length > 0) {
- // Nearest legal discrete option (matches local reconciler)
- const best = validOptions.reduce((closest, opt) =>
- Math.abs(opt - requestedFrames) < Math.abs(closest - requestedFrames) ? opt : closest,
- validOptions[0]);
- if (best !== requestedFrames) {
- console.log(`🌐 Federated render: adjusted numFrames from ${requestedFrames} to ${best} for ${capability.modelName || capability.modelId}`);
- negotiated = { ...negotiated, numFrames: best };
- }
+ .filter((opt) => Number.isInteger(opt) && opt > 0 && (maxNumFrames === null || opt <= maxNumFrames))
+ .filter((opt) => opt <= requestedFrames)
+ .sort((a, b) => b - a);
+ const best = validOptions[0];
+ if (!best) {
+ throw new ServerError(
+ `Requested frame count (${requestedFrames}) cannot be satisfied by ${capability.modelName || capability.modelId}`,
+ { status: 400, code: 'MEDIA_PROVIDER_INPUT_UNSUPPORTED' },
+ );
+ }
+ if (best !== requestedFrames) {
+ console.log(`🌐 Federated render: adjusted numFrames from ${requestedFrames} to ${best} for ${capability.modelName || capability.modelId}`);
+ negotiated = { ...negotiated, numFrames: best };
}
} else {
const frameStride = capability.frameStride != null ? Number(capability.frameStride) : null;
@@ -58,9 +61,7 @@ export function negotiateVideoConstraints(request, capability) {
if (hasStride || hasMax) {
let legalFrames = requestedFrames;
if (hasStride) {
- const minLegal = frameStride + 1;
legalFrames = Math.floor((legalFrames - 1) / frameStride) * frameStride + 1;
- if (legalFrames < minLegal) legalFrames = minLegal;
}
if (hasMax && legalFrames > maxNumFrames) {
legalFrames = hasStride
@@ -81,17 +82,29 @@ export function negotiateVideoConstraints(request, capability) {
}
}
- // FPS constraint negotiation
- if (Array.isArray(capability.fpsOptions) && capability.fpsOptions.length > 0 && negotiated.fps !== undefined) {
- const requestedFps = Number(negotiated.fps);
- const validFps = capability.fpsOptions.map(Number).filter((f) => Number.isInteger(f) && f > 0);
- if (validFps.length > 0 && Number.isFinite(requestedFps)) {
- const bestFps = validFps.reduce((closest, opt) =>
- Math.abs(opt - requestedFps) < Math.abs(closest - requestedFps) ? opt : closest,
- validFps[0]);
- if (bestFps !== requestedFps) {
- console.log(`🌐 Federated render: adjusted fps from ${requestedFps} to ${bestFps} for ${capability.modelName || capability.modelId}`);
- negotiated = { ...negotiated, fps: bestFps };
+ // Canvas constraint negotiation (issue #4681)
+ if (Array.isArray(capability.resolutionOptions) && capability.resolutionOptions.length > 0
+ && negotiated.width !== undefined && negotiated.height !== undefined) {
+ const validOptions = capability.resolutionOptions.filter(
+ (opt) => Number.isInteger(Number(opt?.w)) && Number.isInteger(Number(opt?.h))
+ && Number(opt.w) >= 64 && Number(opt.w) <= 2048 && Number(opt.h) >= 64 && Number(opt.h) <= 2048,
+ );
+ if (validOptions.length > 0) {
+ const requestedAspect = Number(negotiated.width) / Number(negotiated.height);
+ const requestedArea = Number(negotiated.width) * Number(negotiated.height);
+ const bestOption = validOptions.reduce((best, option) => {
+ const aspectDiff = Math.abs((Number(option.w) / Number(option.h)) - requestedAspect);
+ const areaDiff = Math.abs((Number(option.w) * Number(option.h)) - requestedArea);
+ if (!best) return { option, aspectDiff, areaDiff };
+ if (aspectDiff < best.aspectDiff - 0.001) return { option, aspectDiff, areaDiff };
+ if (Math.abs(aspectDiff - best.aspectDiff) <= 0.001 && areaDiff < best.areaDiff) {
+ return { option, aspectDiff, areaDiff };
+ }
+ return best;
+ }, null)?.option;
+ if (bestOption && (negotiated.width !== Number(bestOption.w) || negotiated.height !== Number(bestOption.h))) {
+ console.log(`🌐 Federated render: adjusted resolution from ${negotiated.width}x${negotiated.height} to ${bestOption.w}x${bestOption.h} for ${capability.modelName || capability.modelId}`);
+ negotiated = { ...negotiated, width: Number(bestOption.w), height: Number(bestOption.h) };
}
}
}
diff --git a/server/services/federatedMedia/remoteSubmission.test.js b/server/services/federatedMedia/remoteSubmission.test.js
index 94b9333c98..c09e92a8c3 100644
--- a/server/services/federatedMedia/remoteSubmission.test.js
+++ b/server/services/federatedMedia/remoteSubmission.test.js
@@ -45,36 +45,59 @@ describe('negotiateVideoConstraints', () => {
expect(negotiateVideoConstraints({ numFrames: 40 }, {}).numFrames).toBe(40);
});
- it('snaps numFrames to the nearest discrete option when frameOptions is present', () => {
+ it('snaps numFrames down to the nearest discrete option when frameOptions is present', () => {
const capability = {
modelId: 'minimax_h3',
frameOptions: [107, 124, 141, 158],
};
- expect(negotiateVideoConstraints({ numFrames: 50 }, capability).numFrames).toBe(107);
- expect(negotiateVideoConstraints({ numFrames: 121 }, capability).numFrames).toBe(124);
+ expect(negotiateVideoConstraints({ numFrames: 121 }, capability).numFrames).toBe(107);
expect(negotiateVideoConstraints({ numFrames: 124 }, capability).numFrames).toBe(124);
expect(negotiateVideoConstraints({ numFrames: 130 }, capability).numFrames).toBe(124);
expect(negotiateVideoConstraints({ numFrames: 200 }, capability).numFrames).toBe(158);
});
- it('snaps fps to the nearest option when fpsOptions is present', () => {
+ it('rejects when requested numFrames is smaller than the minimum frameOption', () => {
const capability = {
modelId: 'minimax_h3',
- fpsOptions: [24, 25],
+ frameOptions: [107, 124, 141, 158],
};
- expect(negotiateVideoConstraints({ fps: 16 }, capability).fps).toBe(24);
- expect(negotiateVideoConstraints({ fps: 30 }, capability).fps).toBe(25);
+ expect(() => negotiateVideoConstraints({ numFrames: 50 }, capability))
+ .toThrow(/cannot be satisfied/);
});
- it('floors frameStride at stride + 1 rather than emitting 1-frame video', () => {
+ it('snaps width and height to closest aspect ratio when resolutionOptions is present', () => {
const capability = {
- modelId: 'wan22_t2v_a14b',
- frameStride: 4,
+ modelId: 'minimax_h3',
+ resolutionOptions: [
+ { label: '16:9', w: 1344, h: 768 },
+ { label: '9:16', w: 768, h: 1344 },
+ ],
+ };
+
+ const snapped = negotiateVideoConstraints({ width: 1280, height: 720 }, capability);
+ expect(snapped.width).toBe(1344);
+ expect(snapped.height).toBe(768);
+ });
+
+ it('tie-breaks equal aspect ratios by closest area and ignores out-of-bounds options', () => {
+ const capability = {
+ modelId: 'custom',
+ resolutionOptions: [
+ { label: '4k-out-of-bounds', w: 3840, h: 2160 },
+ { label: '1080p', w: 1920, h: 1080 },
+ { label: '720p', w: 1280, h: 720 },
+ ],
};
- expect(negotiateVideoConstraints({ numFrames: 3 }, capability).numFrames).toBe(5);
+ const snapped720 = negotiateVideoConstraints({ width: 1024, height: 576 }, capability);
+ expect(snapped720.width).toBe(1280);
+ expect(snapped720.height).toBe(720);
+
+ const snapped1080 = negotiateVideoConstraints({ width: 1800, height: 1012 }, capability);
+ expect(snapped1080.width).toBe(1920);
+ expect(snapped1080.height).toBe(1080);
});
it('rejects when requested numFrames is invalid (< 1)', () => {