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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
/**
* Durable consumer-side adapter for federated video jobs.
*
* Mirrors audioGen/remote.js and imageGen/remote.js: the shared executor
* (services/federatedMedia/remoteExecutor.js) owns retry, idempotent replay,
* cancellation, and hash-verified transfer; this module supplies the video
* specifics — the persisted marker shape, where the verified MP4 lands, and
* the history row + thumbnail a local render would have produced.
*
* The history row matters as much as the bytes: mediaAssetIndex's `completed`
* hook looks the render up by job id in video history, and the Video Gen UI
* lists from that same file — a remote render with no row would be invisible.
*/
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import { copyFile, mkdtemp, rm, unlink } from 'node:fs/promises';
import { withBackupAssetPublication } from '../../lib/backupSnapshotBoundary.js';
import { z } from 'zod';
import { PATHS } from '../../lib/fileUtils.js';
import { generateThumbnail, optimizeForStreaming } from '../../lib/ffmpeg.js';
import { FEDERATED_MEDIA_WIRE_VERSION } from '../../lib/federatedMediaWire.js';
import {
federatedMediaVideoJobSubmissionBaseSchema,
federatedMediaVideoJobSubmissionSchema,
} from '../../lib/validation.js';
import { createRemoteMediaExecutor } from '../federatedMedia/remoteExecutor.js';
import { applyRemoteInputAssets, remoteInputAssetsSchema } from '../federatedMedia/inputAssets.js';
import { videoGenEvents } from './events.js';
import { mutateVideoHistory } from './history.js';
import { renderTimingFields } from '../../lib/renderTiming.js';
import { provenanceForRender } from '../../lib/assetProvenance.js';
import { readLoraLicensesByFilename } from '../loras.js';
import { getVideoModels } from '../../lib/mediaModels.js';
const remoteVideoMarkerSchema = z.object({
wireVersion: z.literal(FEDERATED_MEDIA_WIRE_VERSION),
peerId: z.string().uuid(),
reconcile: z.boolean().optional(),
cancelRequested: z.boolean().optional(),
// Set by the unattended (standing) router, never by an interactive render.
// Optional so a marker already queued by an older build still validates; its
// absence means "interactive", which is the correct reading of history.
standingRoute: z.boolean().optional(),
// The full wire submission MINUS its conditioning refs, re-validated on every
// replay. Persisted queue state is user-editable, so the body that actually
// leaves this machine is the one this schema accepted — not whatever the file
// happens to contain.
request: federatedMediaVideoJobSubmissionBaseSchema,
// LOCAL paths for this render's conditioning images, resolved to provider
// asset ids immediately before each submission. Ids are NOT persisted here:
// they name slots in the peer's TTL-swept staging area, so a reconcile after a
// restart re-stages the same bytes instead of referencing bytes that are gone.
// The paths themselves never cross the wire. See federatedMedia/inputAssets.js.
//
// Optional so a marker queued before this shipped still validates; absent
// means the text-only render that build was the only one able to route.
inputAssets: remoteInputAssetsSchema.optional(),
}).passthrough();
const executor = createRemoteMediaExecutor({
kind: 'video',
label: 'video',
events: videoGenEvents,
markerSchema: remoteVideoMarkerSchema,
buildRequest: (marker, ctx) => applyRemoteInputAssets(
marker.request, marker.inputAssets, ctx, federatedMediaVideoJobSubmissionSchema,
),
// `<jobId>.mp4` is videoGen/local.js's own filename shape, which is what the
// provider-side result guard and the local history row both key on.
resolveDestination: ({ jobId }) => ({ dir: PATHS.videos, filename: `${jobId}.mp4` }),
publishResult: (work, { path, jobId }) => withBackupAssetPublication(async () => {
// Replay can replace an already referenced clip and poster. Keep both old
// files until the history commit succeeds, and restore them on failure.
const scratch = await mkdtemp(join(tmpdir(), 'portos-video-replay-'));
const originals = [];
let retainScratch = false;
try {
for (const [index, target] of [path, join(PATHS.videoThumbnails, `${jobId}.jpg`)].entries()) {
const backup = join(scratch, String(index));
try {
await copyFile(target, backup);
originals.push({ target, backup });
} catch (error) {
if (error.code !== 'ENOENT') throw error;
originals.push({ target, backup: null });
}
}
try { return await work(); } catch (error) {
const restored = await Promise.allSettled(originals.map(async ({ target, backup }) => {
if (backup) await copyFile(backup, target);
else await unlink(target).catch(error => { if (error.code !== 'ENOENT') throw error; });
}));
const failures = restored.filter(result => result.status === 'rejected').map(result => result.reason);
if (failures.length) {
retainScratch = true;
throw Object.assign(new AggregateError([error, ...failures], `Video publication rollback failed; originals retained at ${scratch}`), {
backupPublicationUncertain: true, recoveryPath: scratch,
});
}
throw error;
}
} finally { if (!retainScratch) await rm(scratch, { recursive: true, force: true }); }
}),
async finalize({ jobId, path, filename, request, remoteJob, peerId, renderStartedAtMs }) {
// Both ffmpeg passes are best-effort by construction (they no-op when
// ffmpeg is absent), exactly as the local finalize path treats them — a
// missing thumbnail must not fail a render that already landed verified.
await optimizeForStreaming(path);
const thumbnail = await generateThumbnail(path, jobId);
const createdAt = new Date().toISOString();
const modelId = remoteJob.result.modelId ?? request.modelId;
const model = getVideoModels().find((m) => m.id === modelId);
const loraFilenames = Array.isArray(request.loraFilenames) ? request.loraFilenames : [];
const loraLicenses = await readLoraLicensesByFilename(loraFilenames);
await mutateVideoHistory((history) => {
// A reconciled completion replaces its prior row rather than duplicating it.
history = history.filter(entry => entry.id !== jobId);
history.unshift({
id: jobId,
prompt: request.prompt,
negativePrompt: request.negativePrompt ?? '',
modelId,
seed: request.seed ?? null,
width: request.width ?? null,
height: request.height ?? null,
numFrames: request.numFrames ?? null,
fps: request.fps ?? null,
steps: request.steps ?? null,
guidanceScale: request.guidance ?? null,
filename,
thumbnail,
// Result provenance: instance-level identifiers already shared across
// the federation, never a hostname, address, or credential.
federatedPeerId: peerId,
federatedJobId: remoteJob.id,
...(request.audioConditioning ? { audioConditioning: request.audioConditioning } : {}),
createdAt,
provenance: provenanceForRender({
model: {
id: modelId,
name: model?.name || modelId,
license: model?.license,
repo: model?.repo,
disclosure: model?.disclosure,
},
loras: loraFilenames.map((filename) => ({
filename,
...(loraLicenses[filename] || {}),
})),
capturedAt: createdAt,
}),
...renderTimingFields(renderStartedAtMs),
});
return history;
});
return { filename, path: `/data/videos/${filename}`, thumbnail };
},
});
export const generateVideo = executor.run;
export const cancel = executor.cancel;
/**
* Chained renders condition each chunk on the previous chunk's tail frames,
* which the text-only federated wire cannot carry. The queue only reaches this
* when persisted params claim `chunks > 1` on a remote job, so fail loudly
* rather than silently rendering a single unchained clip.
*/
export function generateChainedVideo({ jobId }) {
videoGenEvents.emit('failed', {
generationId: jobId,
error: 'Chained video renders cannot run on a federated media provider',
});
}
export const __configureRemoteVideoForTests = executor.configureForTests;
export const __resetRemoteVideoForTests = executor.resetForTests;