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

7.6 KB
Raw Blame
import { effectiveJobPrompt } from '../../lib/federatedMediaWire.js';
import { videoHoldSchema } from './videoHolds.js';
import { isRemoteMediaJob } from './remoteMediaJob.js';
import { mediaJobExecutionLane } from '../../lib/generationModes.js';

// Public projection of a media job. Keep worker-only paths and subprocess
// details out of both the queue API and the processing dashboard.
export const PARAM_ALLOWLIST = new Set([
  'prompt', 'negativePrompt', 'modelId', 'model', 'effort',
  'width', 'height', 'numFrames', 'fps', 'steps', 'guidanceScale',
  'batchSize', 'seed', 'tiling', 'disableAudio', 'mode', 'imageStrength',
  'i2vReferenceMode',
  // Sampler/decoder knobs the retry editor re-offers. 'draftDecode' (#5423) is
  // the preview-fidelity decode REQUEST the job was submitted with — projected
  // so the requeue editor can seed its picker from what the job actually asked
  // for instead of snapping every requeue back to Full.
  'textEncoderId', 'speedProfileId', 'draftDecode', 'streamingMode',
  'chunks', 'chunkPrompts', 'contextFrames', 'loras',
  'cfgScale', 'guidance', 'quantize',
  'runId', 'runtime', 'datasetId', 'characterId', 'characterName',
  'triggerWord', 'rank', 'baseModelId', 'spriteRef', 'spriteWalk', 'deckCard',
]);

const MUSIC_STUDIO_KEYS = new Set([
  'trackId', 'title', 'artistId', 'artist', 'albumId', 'lyricsEnabled', 'lyricsProvided', 'instrumentalOnly',
]);

const EXECUTION_RUNTIME_KEYS = new Set(['torch', 'diffusers', 'transformers', 'accelerate']);
const executionProvenance = (value) => {
  if (!value || typeof value !== 'object') return undefined;
  if (value.state === 'malformed') return { state: 'malformed' };
  if (!['confirmed', 'degraded'].includes(value.state)
    || !['auto', 'mps', 'cuda', 'cpu'].includes(value.requestedDevice)
    || !['mps', 'cuda', 'cpu'].includes(value.effectiveDevice)
    || !['mps', 'cuda', 'cuda+offload', 'cpu'].includes(value.placement)
    || typeof value.cpuFallback !== 'boolean'
    || !['flux2', 'diffusers-image'].includes(value.runtime?.runtime)) return undefined;
  return {
    version: 1,
    state: value.state,
    requestedDevice: value.requestedDevice,
    effectiveDevice: value.effectiveDevice,
    placement: value.placement,
    cpuFallback: value.cpuFallback,
    runtime: {
      runtime: value.runtime.runtime,
      versions: Object.fromEntries(Object.entries(value.runtime.versions || {})
        .filter(([key, version]) => EXECUTION_RUNTIME_KEYS.has(key) && typeof version === 'string' && version.length <= 80)),
    },
  };
};

// A history row must retain usable reference evidence without exposing worker paths.
// Missing old inputs mean unknown provenance, not a confirmed text-only render.
const referenceInputs = (params, routed) => {
  const inputs = routed
    ? (Array.isArray(params?.remoteMedia?.inputAssets) ? params.remoteMedia.inputAssets : [])
    : [
      ...(params?.initImagePath ? [{ role: 'initImage', path: params.initImagePath }] : []),
      ...(Array.isArray(params?.referenceImagePaths) ? params.referenceImagePaths.map(path => ({ role: 'referenceImages', path })) : []),
    ];
  return inputs.flatMap(input => {
    if (!['initImage', 'referenceImages'].includes(input?.role) || typeof input?.path !== 'string') return [];
    const filename = input.path.split(/[\\/]/).pop();
    if (!filename || !/^[^\x00-\x1f?#]+\.(?:png|jpe?g|webp|gif)$/i.test(filename)) return [];
    return [{ role: input.role, filename }];
  });
};

export function sanitizeJob(job) {
  if (!job) return job;
  if (job.durabilityPending) {
    job = { ...job,
      statusMsg: job.persistenceError ? 'Outcome pending: media queue storage unavailable' : 'Saving outcome' };
  }
  const safeParams = job.params
    ? Object.fromEntries(Object.entries(job.params)
      .filter(([key]) => PARAM_ALLOWLIST.has(key) || key === 'musicStudio')
      .map(([key, value]) => [
        key,
        key === 'musicStudio' && value && typeof value === 'object'
          ? Object.fromEntries(Object.entries(value).filter(([nestedKey]) => MUSIC_STUDIO_KEYS.has(nestedKey)))
          : value,
      ]))
    : undefined;
  // A remote job's conditioning text lives inside its versioned marker, never
  // in top-level params — that is what makes an older build (which cannot route
  // the marker) fail closed instead of quietly re-rendering the job locally.
  // Rebuild the prompt for the public projection without exposing private peer
  // routing state.
  const remotePrompt = effectiveJobPrompt(job);
  const safeExecution = executionProvenance(job.result?.executionProvenance);
  const { executionProvenance: _executionProvenance, ...resultWithoutExecution } = job.result && typeof job.result === 'object' ? job.result : {};
  const safeResult = job.result && typeof job.result === 'object'
    ? { ...resultWithoutExecution, ...(safeExecution ? { executionProvenance: safeExecution } : {}) }
    : job.result;
  // The model id is nulled in top-level params for the same reason (#4683), so
  // rebuild it from the marker too — the Render Queue's model badge reads
  // `params.modelId`, and every routed kind (audio included: routes/music.js
  // requires an explicit `modelId` for a peer render) carries it on the wire
  // request. This branch is now the ONLY source of a routed job's model id.
  const routed = isRemoteMediaJob(job);
  const hold = !routed && job.status === 'queued' ? videoHoldSchema.safeParse(job.hold) : null;
  const remoteModelId = job.params?.remoteMedia?.request?.modelId;
  if (safeParams && routed) {
    if (remotePrompt) safeParams.prompt = remotePrompt;
    if (typeof remoteModelId === 'string' && remoteModelId) safeParams.modelId = remoteModelId;
  }
  const requestedInputImages = job.kind === 'image' ? referenceInputs(job.params, routed) : [];
  return {
    ...(requestedInputImages.length ? { requestedInputImages } : {}),
    id: job.id,
    kind: job.kind,
    // Where this job renders. Job metadata, not a render input, so it rides on
    // the envelope rather than inside `params` — the PARAM_ALLOWLIST above stays
    // an exact description of what a projected `params` can contain. The UI
    // needs it because a peer render must not wear the local model badge.
    renderer: routed ? 'remote' : 'local',
    // Which of the scheduler's execution lanes actually runs this job, from the
    // SAME classifier mediaJobQueue schedules with — so the Render Queue can
    // never re-derive a stale backend list of its own. `renderer` above stays
    // exactly what it was (routed vs not); this is the finer local-GPU vs
    // cloud-provider split it cannot express.
    executionLane: mediaJobExecutionLane({ kind: job.kind, mode: job.params?.mode, remote: routed }),
    owner: job.owner,
    status: job.status,
    // This flag describes an in-flight request. Archived terminal jobs can
    // retain the internal marker, and remote recovery may resume as queued.
    cancelRequested: (job.status === 'queued' || job.status === 'running') && job.cancelRequested === true,
    ...(hold?.success ? { hold: { ...hold.data, heldJobCount: job.hold.heldJobCount,
      ...(job.hold.scope === 'local-video' ? { scope: 'local-video' } : {}) } } : {}),
    queuedAt: job.queuedAt,
    startedAt: job.startedAt,
    completedAt: job.completedAt,
    position: job.position,
    progress: job.progress,
    // A queued remote job held behind an unsettled earlier job to its peer.
    ...(job.status === 'queued' && job.peerHold
      ? { peerHold: { reason: job.peerHold.reason, blockingJobId: job.peerHold.blockingJobId } } : {}),
    statusMsg: job.status === 'queued' && job.peerHold ? 'Waiting for peer to settle an earlier job' : job.statusMsg,
    etaMs: job.etaMs,
    error: job.error,
    result: safeResult,
    params: safeParams,
  };
}