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

fix: sanitize provider capabilities, refine canvas tie-breaking, and return negotiated dimensions (#4681)

[._.]/ Adam Eivy4e0021a784c0
diff --git a/server/routes/imageGen.js b/server/routes/imageGen.js
index 598ecd7334..2fb6ec8160 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, remoteMedia } = await prepareRemoteMediaJob({
+ const { peer, request: negotiatedRequest, remoteMedia } = await prepareRemoteMediaJob({
peerId: params.mediaProviderPeerId,
kind: 'image',
request,
@@ -477,10 +477,11 @@ 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: request.modelId,
+ model: effectiveRequest.modelId,
mediaProviderPeerId: peer.id,
}));
}
diff --git a/server/routes/videoGen.js b/server/routes/videoGen.js
index 8d5d54ba41..5c9ecd7055 100644
--- a/server/routes/videoGen.js
+++ b/server/routes/videoGen.js
@@ -1055,7 +1055,7 @@ router.post('/', frameImageUpload, asyncHandler(async (req, res) => {
// schema's overlap with it: this object is persisted and replayed on every
// reconcile, so it must already be a body the provider accepts.
const request = buildFederatedMediaRequest({ kind: 'video', params: body });
- const { peer, remoteMedia } = await prepareRemoteMediaJob({
+ const { peer, request: negotiatedRequest, remoteMedia } = await prepareRemoteMediaJob({
peerId: body.mediaProviderPeerId,
kind: 'video',
request,
@@ -1068,11 +1068,15 @@ router.post('/', frameImageUpload, asyncHandler(async (req, res) => {
kind: 'video',
params: { remoteMedia },
});
+ const effectiveRequest = negotiatedRequest || request;
return res.json({
jobId,
generationId: jobId,
filename: `${jobId}.mp4`,
- model: request.modelId,
+ model: effectiveRequest.modelId,
+ ...(effectiveRequest?.numFrames !== undefined ? { numFrames: effectiveRequest.numFrames } : {}),
+ ...(effectiveRequest?.width !== undefined ? { width: effectiveRequest.width } : {}),
+ ...(effectiveRequest?.height !== undefined ? { height: effectiveRequest.height } : {}),
// 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/defaultRouting.js b/server/services/federatedMedia/defaultRouting.js
index f6d802ffc7..99adb2eb44 100644
--- a/server/services/federatedMedia/defaultRouting.js
+++ b/server/services/federatedMedia/defaultRouting.js
@@ -250,7 +250,7 @@ export async function resolveDefaultMediaRoute({ kind, params }) {
// queued or reconciling job re-resolves its peer from the registry on every
// request, and that record can change (a host edited from a .ts.net name to a
// LAN address) between enqueue and submit. The executor re-checks on this bit.
- return { peer, capability, request: finalRequest, remoteMedia: { ...remoteMedia, standingRoute: true } };
+ return { peer, request: finalRequest, remoteMedia: { ...remoteMedia, standingRoute: true } };
}
/**
@@ -283,7 +283,7 @@ const DROPPED_POST_PROCESSING = Object.freeze(['cleanC2PA', 'denoise']);
* enqueue. The prompt is blanked because it rides ONLY inside the versioned
* marker (see the generate routes for the same reasoning).
*/
-export function routedJobParams(params, { request, remoteMedia } = {}) {
+export function routedJobParams(params, { request, remoteMedia }) {
const effectiveRequest = request || remoteMedia?.request;
const jobParams = { ...(params || {}) };
for (const key of LOCAL_ONLY_ROUTED_PARAMS) delete jobParams[key];
@@ -295,7 +295,7 @@ export function routedJobParams(params, { request, remoteMedia } = {}) {
return {
...jobParams,
prompt: '',
- modelId: effectiveRequest?.modelId || params?.modelId,
+ modelId: effectiveRequest.modelId,
...(effectiveRequest?.numFrames !== undefined ? { numFrames: effectiveRequest.numFrames } : {}),
...(effectiveRequest?.width !== undefined ? { width: effectiveRequest.width } : {}),
...(effectiveRequest?.height !== undefined ? { height: effectiveRequest.height } : {}),
diff --git a/server/services/federatedMedia/remoteSubmission.js b/server/services/federatedMedia/remoteSubmission.js
index eea0ce887f..05c680cfc5 100644
--- a/server/services/federatedMedia/remoteSubmission.js
+++ b/server/services/federatedMedia/remoteSubmission.js
@@ -83,14 +83,21 @@ export function negotiateVideoConstraints(request, capability) {
if (Array.isArray(capability.resolutionOptions) && capability.resolutionOptions.length > 0
&& negotiated.width !== undefined && negotiated.height !== undefined) {
const validOptions = capability.resolutionOptions.filter(
- (opt) => Number.isFinite(opt?.w) && Number.isFinite(opt?.h)
+ (opt) => Number.isInteger(Number(opt?.w)) && Number.isInteger(Number(opt?.h))
&& opt.w >= 64 && opt.w <= 2048 && opt.h >= 64 && opt.h <= 2048,
);
if (validOptions.length > 0) {
- const aspectValue = Number(negotiated.width) / Number(negotiated.height);
+ const requestedAspect = Number(negotiated.width) / Number(negotiated.height);
+ const requestedArea = Number(negotiated.width) * Number(negotiated.height);
const bestOption = validOptions.reduce((best, option) => {
- const distance = Math.abs((Number(option.w) / Number(option.h)) - aspectValue);
- return !best || distance < best.distance ? { option, distance } : best;
+ 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 !== bestOption.w || negotiated.height !== 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}`);
diff --git a/server/services/federatedMedia/remoteSubmission.test.js b/server/services/federatedMedia/remoteSubmission.test.js
index 3894c90ae6..91fd72a2e4 100644
--- a/server/services/federatedMedia/remoteSubmission.test.js
+++ b/server/services/federatedMedia/remoteSubmission.test.js
@@ -81,6 +81,25 @@ describe('negotiateVideoConstraints', () => {
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 },
+ ],
+ };
+
+ 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 frame count cannot be made legal', () => {
const capability = {
modelId: 'wan22',
diff --git a/server/services/federatedMediaProvider.js b/server/services/federatedMediaProvider.js
index 232627943c..59838f8028 100644
--- a/server/services/federatedMediaProvider.js
+++ b/server/services/federatedMediaProvider.js
@@ -267,6 +267,27 @@ async function localGeneratorCapabilities(kind, pythonPath, { models, configured
: !runtimeReady ? 'runtime-unavailable'
: !modelReady ? 'model-unavailable'
: null;
+ const frameStride = Number.isInteger(Number(model?.frameStride)) && Number(model.frameStride) > 0
+ ? Number(model.frameStride)
+ : null;
+ const validFrameOptions = Array.isArray(model?.frameOptions)
+ ? model.frameOptions.filter((f) => Number.isInteger(f) && f > 0).slice(0, 100)
+ : [];
+ const maxNumFrames = Number.isInteger(Number(model?.maxNumFrames)) && Number(model.maxNumFrames) > 0
+ ? Number(model.maxNumFrames)
+ : (validFrameOptions.length > 0 ? Math.max(...validFrameOptions) : null);
+ const validResolutions = Array.isArray(model?.resolutionOptions)
+ ? model.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)
+ .slice(0, 100)
+ .map(({ label, w, h }) => ({
+ w: Number(w),
+ h: Number(h),
+ ...(label ? { label: String(label).slice(0, 120) } : {}),
+ }))
+ : [];
return {
kind,
engine: selected.engine,
@@ -284,22 +305,10 @@ async function localGeneratorCapabilities(kind, pythonPath, { models, configured
defaultDurationSec: null,
lyrics: false,
autoDuration: false,
- frameStride: Number.isFinite(model?.frameStride) && Number(model.frameStride) > 0
- ? Number(model.frameStride)
- : null,
- maxNumFrames: Number.isFinite(model?.maxNumFrames) && Number(model.maxNumFrames) > 0
- ? Number(model.maxNumFrames)
- : (Array.isArray(model?.frameOptions) && model.frameOptions.length ? Math.max(...model.frameOptions) : null),
- frameOptions: Array.isArray(model?.frameOptions) && model.frameOptions.length > 0
- ? model.frameOptions.filter(Number.isFinite)
- : null,
- resolutionOptions: Array.isArray(model?.resolutionOptions) && model.resolutionOptions.length > 0
- ? model.resolutionOptions.map(({ label, w, h }) => ({
- w: Number(w),
- h: Number(h),
- ...(label ? { label: String(label) } : {}),
- }))
- : null,
+ frameStride,
+ maxNumFrames: Number.isFinite(maxNumFrames) && maxNumFrames > 0 ? maxNumFrames : null,
+ frameOptions: validFrameOptions.length > 0 ? validFrameOptions : null,
+ resolutionOptions: validResolutions.length > 0 ? validResolutions : null,
_pythonPath: isLocal ? pythonPath : null,
_model: model,
};
diff --git a/server/services/federatedMediaProvider.test.js b/server/services/federatedMediaProvider.test.js
index 70b9ee680c..e16fbd9d16 100644
--- a/server/services/federatedMediaProvider.test.js
+++ b/server/services/federatedMediaProvider.test.js
@@ -679,4 +679,40 @@ describe('federated media provider — prompt-free status and projection payload
resolutionOptions: [{ label: '1344x768 (16:9)', w: 1344, h: 768 }],
});
});
+
+ it('sanitizes out-of-range, non-integer, and oversized constraint arrays provider-side', async () => {
+ state.videoModels = [{
+ id: 'custom_model',
+ name: 'Custom Model',
+ runtime: 'wan22',
+ repo: 'example/wan22',
+ supportedModes: ['text'],
+ frameStride: -4,
+ maxNumFrames: 'not-a-number',
+ frameOptions: [25, 33.5, -10, 'invalid', 49],
+ resolutionOptions: [
+ { label: 'valid', w: 1344, h: 768 },
+ { label: '4k-too-large', w: 3840, h: 2160 },
+ { label: 'too-small', w: 32, h: 32 },
+ { label: 'invalid-w', w: 'NaN', h: 768 },
+ ],
+ }];
+ state.cachedRepos = new Set(['example/wan22']);
+ const providerConfig = {
+ enabled: true,
+ maxQueuedJobs: 2,
+ audioModels: [],
+ imageModels: [],
+ videoModels: [{ engine: 'local', modelId: 'custom_model' }],
+ };
+ state.settings = { federation: { mediaProvider: providerConfig } };
+
+ const status = await getFederatedMediaProviderStatus(providerConfig, { kinds: ['video'] });
+ const cap = status.capabilities[0];
+
+ expect(cap.frameStride).toBeNull();
+ expect(cap.maxNumFrames).toBe(49);
+ expect(cap.frameOptions).toEqual([25, 49]);
+ expect(cap.resolutionOptions).toEqual([{ label: 'valid', w: 1344, h: 768 }]);
+ });
});