Skip to content

AI media harness for planning, generating, and editing locally

TypeScript

Drop completed generation snapshots and encoder buffers

Bitti90b3bc011c04
diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md
index 7a72a84..4eb9d75 100644
--- a/RELEASE_NOTES.md
+++ b/RELEASE_NOTES.md
@@ -36,6 +36,7 @@ This alpha update adds LAN generation workers, Linux support, expanded color gra
## Fixes and improvements
+- Reduced memory retained between generations: release Slopfab samples and handles after consumption, discard completed queue snapshots, close encoders on errors, and clean up abandoned LAN generation results.
- Inspector instructions now appear as tooltips on field labels, keeping the right-side panels compact.
- Scene generation prompts now mark shot times as `[Shot 2] At 00:03.500,`.
- Still-image resolution and aspect ratio are saved separately from video project settings. Editing or selecting an image no longer changes the canvas used for video generation and continuation.
diff --git a/src-tauri/core/src/rendered.rs b/src-tauri/core/src/rendered.rs
index 45d592c..3794891 100644
--- a/src-tauri/core/src/rendered.rs
+++ b/src-tauri/core/src/rendered.rs
@@ -8,8 +8,9 @@
//! CLAUDE.md on the licensing reason).
//!
//! So this module is the hand-off. It keeps the finished generation handle
-//! alive, asks slopfab to convert only the frame the webview currently needs,
-//! and releases the handle after WebCodecs has encoded and muxed the file.
+//! alive and asks slopfab to convert only the frame the consumer currently
+//! needs. Still images release the handle after copying RGBA; videos release
+//! it after WebCodecs consumes the samples, before muxing and writing the file.
//!
//! ## What this costs
//!
@@ -38,7 +39,7 @@ pub trait FrameSource: Send {
fn frame_rgba(&self, index: u32, width: u32, height: u32) -> Result<Vec<u8>, String>;
}
-/// A finished render whose slopfab source is kept alive until encoding ends.
+/// A finished render whose slopfab source lives until its samples are consumed.
pub struct RenderedVideo {
pub job_id: String,
pub width: u32,
diff --git a/src-tauri/core/src/slopfab/ffi/handles.rs b/src-tauri/core/src/slopfab/ffi/handles.rs
index edfa1d6..5b56e85 100644
--- a/src-tauri/core/src/slopfab/ffi/handles.rs
+++ b/src-tauri/core/src/slopfab/ffi/handles.rs
@@ -91,7 +91,8 @@ impl Drop for GenerationHandle {
fn drop(&mut self) {
if !self.terminal {
self.cancel();
- // Joining precedes destruction of callback memory and the library.
+ // Wait for completion before releasing samples. Native destroy
+ // then joins before callback memory and the library are dropped.
while self.api.inner.wait(self.pointer.as_ptr(), -1) == super::super::NOT_READY {}
}
self.api.inner.destroy_generation(self.pointer.as_ptr());
@@ -99,7 +100,7 @@ impl Drop for GenerationHandle {
}
pub struct FinishedGeneration(GenerationHandle);
-// Only successful, joined generations cross threads. The render store serializes
+// Only successfully completed generations cross threads. The render store serializes
// access and the native API exposes immutable output after wait completes.
unsafe impl Send for FinishedGeneration {}
diff --git a/src/lib/generatedVideo.test.ts b/src/lib/generatedVideo.test.ts
index 733a034..ef15c20 100644
--- a/src/lib/generatedVideo.test.ts
+++ b/src/lib/generatedVideo.test.ts
@@ -10,6 +10,7 @@ const muxed = vi.hoisted(() => ({
configs: [] as Array<Record<string, unknown>>,
audioChunks: 0,
videoChunks: 0,
+ finalized: false,
}));
vi.mock("mp4-muxer", () => ({
ArrayBufferTarget: class { buffer = new Uint8Array([1, 2, 3, 4]).buffer; },
@@ -17,7 +18,7 @@ vi.mock("mp4-muxer", () => ({
constructor(config: Record<string, unknown>) { muxed.configs.push(config); }
addVideoChunk() { muxed.videoChunks += 1; }
addAudioChunk() { muxed.audioChunks += 1; }
- finalize() { /* the fake target already owns its test bytes */ }
+ finalize() { muxed.finalized = true; }
},
}));
@@ -33,10 +34,11 @@ const summary = (over: Record<string, unknown> = {}) => ({
const desktop = () => { scope.__TAURI_INTERNALS__ = {}; };
beforeEach(() => {
- invoked.mockReset();
+ invoked.mockReset().mockResolvedValue(true);
muxed.configs.length = 0;
muxed.audioChunks = 0;
muxed.videoChunks = 0;
+ muxed.finalized = false;
delete scope.__TAURI_INTERNALS__;
delete scope.VideoEncoder;
delete scope.VideoFrame;
@@ -59,6 +61,68 @@ afterEach(() => {
the wrong side of the app, keeping hold of half a gigabyte of frames after a
failure, or reporting a file that was never written. */
describe("saving a rendered scene", () => {
+ it.each(["configure", "encode"])("closes video resources when %s throws", async (operation) => {
+ desktop();
+ const closeFrame = vi.fn();
+ const closeEncoder = vi.fn();
+ scope.VideoFrame = class { close = closeFrame; };
+ scope.VideoEncoder = class {
+ static isConfigSupported = async () => ({ supported: true });
+ state = "configured";
+ configure() { if (operation === "configure") throw new Error("Encoder failed"); }
+ encode() { throw new Error("Encoder failed"); }
+ close = closeEncoder;
+ };
+ invoked.mockImplementation(async (command: string) => {
+ if (command === "generated_summary") return summary({ width: 1, height: 1, frameCount: 1, audioSamples: 0 });
+ if (command === "generated_frame") return [0, 0, 0, 255];
+ return true;
+ });
+ await expect(saveGeneratedScene({ folderPath: "/p", jobId: "job-01" })).rejects.toThrow("Encoder failed");
+ expect(closeEncoder).toHaveBeenCalledOnce();
+ expect(closeFrame).toHaveBeenCalledTimes(operation === "encode" ? 1 : 0);
+ expect(invoked).toHaveBeenCalledWith("release_generated_frames", { jobId: "job-01" });
+ });
+
+ it.each(["configure", "encode"])("closes audio resources when %s throws and saves the video", async (operation) => {
+ desktop();
+ const closeData = vi.fn();
+ const closeEncoder = vi.fn();
+ scope.AudioData = class { close = closeData; };
+ scope.AudioEncoder = class {
+ static isConfigSupported = async () => ({ supported: true });
+ state = "configured";
+ configure() { if (operation === "configure") throw new Error("Audio failed"); }
+ encode() { throw new Error("Audio failed"); }
+ close = closeEncoder;
+ };
+ scope.VideoFrame = class { close() {} };
+ scope.VideoEncoder = class {
+ static isConfigSupported = async () => ({ supported: true });
+ state = "configured";
+ encodeQueueSize = 0;
+ constructor(private init: VideoEncoderInit) {}
+ configure() {}
+ encode() { this.init.output({} as EncodedVideoChunk); }
+ async flush() {}
+ close() {}
+ };
+ invoked.mockImplementation(async (command: string) => {
+ if (command === "generated_summary") return summary({ width: 1, height: 1, frameCount: 1, audioSamples: 2 });
+ if (command === "generated_frame") return [0, 0, 0, 255];
+ if (command === "generated_audio") return new Float32Array([0, 0]).buffer;
+ if (command === "release_generated_frames") expect(muxed.finalized).toBe(false);
+ if (command === "write_generated_video") return { relativePath: "media/result.mp4", bytes: 4 };
+ return true;
+ });
+ await expect(saveGeneratedScene({ folderPath: "/p", jobId: "job-01" })).resolves.toMatchObject({ note: "Saved without sound: Audio failed" });
+ expect(closeEncoder).toHaveBeenCalledOnce();
+ expect(closeData).toHaveBeenCalledTimes(operation === "encode" ? 1 : 0);
+ const commands = invoked.mock.calls.map(([command]) => command);
+ expect(commands.filter((command) => command === "release_generated_frames")).toHaveLength(1);
+ expect(commands.indexOf("release_generated_frames")).toBeLessThan(commands.indexOf("write_generated_video"));
+ });
+
it("cannot save from the browser preview, and says which half is missing", async () => {
await expect(saveGeneratedScene({ folderPath: "/p", jobId: "job-01" })).rejects.toThrow(/desktop app/);
expect(invoked).not.toHaveBeenCalled();
@@ -67,9 +131,7 @@ describe("saving a rendered scene", () => {
it("says a webview with no encoder cannot make a video file", async () => {
desktop();
await expect(saveGeneratedScene({ folderPath: "/p", jobId: "job-01" })).rejects.toThrow(/no WebCodecs VideoEncoder/);
- // Nothing was asked of Rust: there is no point pulling frames that cannot
- // be encoded.
- expect(invoked).not.toHaveBeenCalled();
+ expect(invoked).toHaveBeenCalledExactlyOnceWith("release_generated_frames", { jobId: "job-01" });
});
it("says when the frames are gone rather than writing an empty file", async () => {
@@ -99,6 +161,7 @@ describe("saving a rendered scene", () => {
scope.VideoFrame = class {};
invoked.mockResolvedValueOnce(summary({ frameCount: 0 }));
await expect(saveGeneratedScene({ folderPath: "/p", jobId: "job-01" })).rejects.toThrow(/no pictures/);
+ expect(invoked).toHaveBeenCalledWith("release_generated_frames", { jobId: "job-01" });
});
});
diff --git a/src/lib/generatedVideo.ts b/src/lib/generatedVideo.ts
index bf90878..9ae3a05 100644
--- a/src/lib/generatedVideo.ts
+++ b/src/lib/generatedVideo.ts
@@ -290,8 +290,8 @@ async function encodeAudio(
output: (chunk, meta) => { chunks += 1; add(chunk, meta); },
error: (reason) => { failure.reason = reason instanceof Error ? reason : new Error(String(reason)); },
});
- encoder.configure(config);
try {
+ encoder.configure(config);
for (let offset = 0; offset < total; offset += AUDIO_BLOCK) {
if (failure.reason) throw failure.reason;
const count = Math.min(AUDIO_BLOCK, total - offset);
@@ -306,9 +306,12 @@ async function encodeAudio(
timestamp: Math.round((offset * 1_000_000) / sampleRate),
data: interleaved,
});
- encoder.encode(data);
- data.close();
- while (encoder.encodeQueueSize > 16) await tick();
+ try { encoder.encode(data); }
+ finally { data.close(); }
+ while (encoder.encodeQueueSize > 16) {
+ if (failure.reason) throw failure.reason;
+ await tick();
+ }
}
await encoder.flush();
if (failure.reason) throw failure.reason;
@@ -338,22 +341,23 @@ export async function saveGeneratedScene(options: {
}): Promise<SavedGeneration> {
const { folderPath, jobId } = options;
if (!isTauri()) throw new Error("Saving a rendered scene needs the desktop app.");
- if (typeof VideoEncoder === "undefined" || typeof VideoFrame === "undefined") {
- throw new Error("This webview has no WebCodecs VideoEncoder, so a rendered scene cannot be turned into a video file here.");
- }
- const summary = await renderedSummary(jobId);
- if (!summary) {
- throw new Error("The rendered frames are no longer in memory, so there is nothing to save. Run the scene again.");
- }
- if (summary.frameCount <= 0 || summary.width <= 0 || summary.height <= 0) {
- throw new Error("The render came back with no pictures in it.");
- }
- writeDiagnostic("info", "generated-video", "render.loaded", "Rendered buffers are available for encoding.", {
- jobId, width: summary.width, height: summary.height, frames: summary.frameCount, fps: summary.fps,
- audioChannels: summary.audioChannels, audioSampleRate: summary.audioSampleRate, audioSamples: summary.audioSamples,
- });
-
+ let released = false;
try {
+ if (typeof VideoEncoder === "undefined" || typeof VideoFrame === "undefined") {
+ throw new Error("This webview has no WebCodecs VideoEncoder, so a rendered scene cannot be turned into a video file here.");
+ }
+ const summary = await renderedSummary(jobId);
+ if (!summary) {
+ throw new Error("The rendered frames are no longer in memory, so there is nothing to save. Run the scene again.");
+ }
+ if (summary.frameCount <= 0 || summary.width <= 0 || summary.height <= 0) {
+ throw new Error("The render came back with no pictures in it.");
+ }
+ writeDiagnostic("info", "generated-video", "render.loaded", "Rendered buffers are available for encoding.", {
+ jobId, width: summary.width, height: summary.height, frames: summary.frameCount, fps: summary.fps,
+ audioChannels: summary.audioChannels, audioSampleRate: summary.audioSampleRate, audioSamples: summary.audioSamples,
+ });
+
const { width, height } = options.outputSize ?? summary;
if (!Number.isSafeInteger(width) || !Number.isSafeInteger(height) || width <= 0 || height <= 0) {
throw new Error("The selected video output dimensions are invalid.");
@@ -395,12 +399,6 @@ export async function saveGeneratedScene(options: {
/* On an object because the encoder writes it from its own callback, where
TypeScript's flow analysis cannot follow the assignment. */
const failure: { reason: Error | null } = { reason: null };
- const encoder = new VideoEncoder({
- output: (chunk, meta) => muxer.addVideoChunk(chunk, meta),
- error: (reason) => { failure.reason = reason instanceof Error ? reason : new Error(String(reason)); },
- });
- encoder.configure({ codec, width, height, bitrate, framerate: fps, avc: { format: "avc" } });
-
const makeFrame = frameFactory(summary.width, summary.height);
/* Computed from the index every time rather than accumulated, so a rate
that does not divide a microsecond evenly cannot drift the last frame
@@ -409,8 +407,12 @@ export async function saveGeneratedScene(options: {
const keyframeEvery = Math.max(1, Math.round(fps * KEYFRAME_SECONDS));
const thumbnailEvery = Math.max(1, Math.round(fps * TIMELINE_THUMBNAIL_INTERVAL_MS / 1_000));
const timelineThumbnails: Array<{ timeMs: number; bytes: Uint8Array }> = [];
-
+ const encoder = new VideoEncoder({
+ output: (chunk, meta) => muxer.addVideoChunk(chunk, meta),
+ error: (reason) => { failure.reason = reason instanceof Error ? reason : new Error(String(reason)); },
+ });
try {
+ encoder.configure({ codec, width, height, bitrate, framerate: fps, avc: { format: "avc" } });
for (let index = 0; index < summary.frameCount; index += 1) {
if (failure.reason) throw failure.reason;
const pixels = await renderedFrame(jobId, index);
@@ -419,12 +421,15 @@ export async function saveGeneratedScene(options: {
if (thumbnail) timelineThumbnails.push({ timeMs: Math.round(index * 1_000 / fps), bytes: thumbnail });
}
const frame = makeFrame(pixels, timestampUs(index), timestampUs(index + 1) - timestampUs(index));
- encoder.encode(frame, { keyFrame: index % keyframeEvery === 0 });
- frame.close();
+ try { encoder.encode(frame, { keyFrame: index % keyframeEvery === 0 }); }
+ finally { frame.close(); }
options.onProgress?.(index + 1, summary.frameCount);
// Let the encoder drain, and let the window paint: this loop is the
// whole of a five-second render's encode and it must not freeze the UI.
- while (encoder.encodeQueueSize > ENCODE_QUEUE_LIMIT) await tick();
+ while (encoder.encodeQueueSize > ENCODE_QUEUE_LIMIT) {
+ if (failure.reason) throw failure.reason;
+ await tick();
+ }
if (index % 8 === 7) await tick();
}
await encoder.flush();
@@ -433,6 +438,9 @@ export async function saveGeneratedScene(options: {
if (encoder.state !== "closed") encoder.close();
}
+ // Audio and frames are now encoded. Free the native render before muxing
+ // and sending the MP4, which both allocate their own output buffers.
+ released = await releaseRendered(jobId);
audioChunks.forEach(({ chunk, meta }) => muxer.addAudioChunk(chunk, meta));
muxer.finalize();
@@ -442,7 +450,7 @@ export async function saveGeneratedScene(options: {
return { relativePath: written.relativePath, bytes: written.bytes, note, hasAudio: withAudio, width, height, durationMs: Math.round(summary.frameCount / summary.fps * 1000) };
} finally {
// Whatever happened, the render stops occupying memory here.
- await releaseRendered(jobId);
+ if (!released) await releaseRendered(jobId);
}
}
diff --git a/src/lib/workQueue.test.ts b/src/lib/workQueue.test.ts
index 8aefd16..3b7b2b7 100644
--- a/src/lib/workQueue.test.ts
+++ b/src/lib/workQueue.test.ts
@@ -60,6 +60,37 @@ const finish = async (queue: WorkQueue, id: string) => {
await waitFor(() => expect(queue.getSnapshot().find((item) => item.id === id)?.status).toBe("completed"));
};
+it("releases submitted snapshots after each generation while keeping completed results available", async () => {
+ const { queue, first } = setup();
+ const pending = (queue as unknown as { work: Map<string, unknown> }).work;
+ for (let index = 0; index < 3; index++) {
+ const [id] = queue.enqueue(first, [submission(first)]);
+ await waitFor(() => expect(enqueueSlopfabGeneration).toHaveBeenCalledTimes(index + 1));
+ await finish(queue, id);
+ await waitFor(() => expect(pending.size).toBe(0));
+ const progress = vi.fn();
+ await expect(queue.waitFor(id, new AbortController().signal, progress)).resolves.toMatchObject({ id, status: "completed" });
+ expect(progress).toHaveBeenCalledOnce();
+ }
+ expect(queue.getSnapshot()).toHaveLength(3);
+ const id = queue.getSnapshot()[0].id;
+ vi.mocked(releaseRendered).mockClear();
+ emit("slopfab-job", { jobId: id, state: "framesReady", detail: "Late result" });
+ expect(releaseRendered).toHaveBeenCalledWith(id);
+ expect(saveGeneratedScene).toHaveBeenCalledTimes(3);
+});
+
+it("releases a cancelled queued snapshot before the running job finishes", async () => {
+ const { queue, first, second } = setup();
+ const [running] = queue.enqueue(first, [submission(first)]);
+ const [cancelled] = queue.enqueue(second, [submission(second)]);
+ await waitFor(() => expect(enqueueSlopfabGeneration).toHaveBeenCalledOnce());
+ await queue.cancel(cancelled);
+ expect((queue as unknown as { work: Map<string, unknown> }).work.has(cancelled)).toBe(false);
+ await expect(queue.waitFor(cancelled, new AbortController().signal)).resolves.toMatchObject({ status: "cancelled" });
+ await finish(queue, running);
+});
+
it.each([undefined, 17])("freezes audio steps %s independently of video steps through the queue", async (audioSteps) => {
const { queue, first } = setup();
const item = submission(first);
diff --git a/src/lib/workQueue.ts b/src/lib/workQueue.ts
index 95c397f..7ec76ef 100644
--- a/src/lib/workQueue.ts
+++ b/src/lib/workQueue.ts
@@ -7,7 +7,7 @@ import { compileImagePrompt } from "./imagePrompt";
import { compileExtendImage, type ExtendOptions } from "./extendImage";
import { CHARACTER_SHEET_ORDER, CHARACTER_SHEET_VIEWS, characterSheetDimensions, characterSheetViewIndices, characterSheetViews, compileCharacterSheet, type CharacterSheetOptions } from "./characterSheet";
import { completeImageDraft, createTemplateImageDraft, imageFamilyRoot, imageGenerationSnapshot, saveImageDraft } from "./imageHistory";
-import type { ImageGenerationSnapshot } from "./project";
+import type { ImageGenerationSnapshot, ProjectAsset } from "./project";
import { outputDimensions } from "./export";
import { projectItemPath, referenceDefinition, referenceImages, referenceRefmodInputs } from "./project";
import { engineProviderSetting, type GeneratorTemplate } from "./settings";
@@ -244,7 +244,10 @@ export class WorkQueue {
waitFor(id: string, signal: AbortSignal, onProgress: (item: WorkItem) => void = () => {}): Promise<WorkItem> {
const work = this.work.get(id);
let latest = this.items.find((item) => item.id === id);
- if (!work || !latest) return Promise.reject(new Error("Generation is no longer in the work queue."));
+ if (!latest) return Promise.reject(new Error("Generation is no longer in the work queue."));
+ if (signal.aborted) return Promise.reject(signal.reason ?? new Error("Agent request cancelled."));
+ if (!work && !isWorkActive(latest)) { onProgress(latest); return Promise.resolve(latest); }
+ if (!work) return Promise.reject(new Error("Generation is no longer in the work queue."));
return new Promise((resolve, reject) => {
const stop = this.subscribe(() => {
const item = this.items.find((candidate) => candidate.id === id);
@@ -407,9 +410,9 @@ export class WorkQueue {
}
private updateScene(work: PendingWork, patch: Partial<GenerationJob>, dirty = true) {
if (work.image || work.refmod) return;
- work.session.update((current) => ({
- ...current, generationJobs: current.generationJobs.map((job) => job.id === work.sceneId ? { ...job, ...patch, updatedAt: new Date().toISOString() } : job),
- }), dirty);
+ // The undo replay log retains this callback. Do not retain the submitted
+ // project snapshot with every progress update.
+ work.session.update(sceneUpdate(work.sceneId, patch), dirty);
}
private async pump() {
if (this.pumping) return;
@@ -473,6 +476,9 @@ export class WorkQueue {
await releaseReferenceAudios(work.request.referenceAudioIds ?? []).catch((reason) =>
writeDiagnostic("error", "work-queue", "references.release_failed", describeDiagnosticError(reason), { workId: work.id }));
delete work.request.referenceAudioIds;
+ // Queue history only needs WorkItem; finished runs must not keep a
+ // deep copy of the entire project and its image history alive.
+ this.work.delete(work.id);
}
}
} finally { this.pumping = false; }
@@ -503,6 +509,7 @@ export class WorkQueue {
work.cancelled = true;
if (item.status === "queued" || item.status === "preparing") {
this.cancelled(work);
+ if (item.status === "queued") this.work.delete(id);
// Stops files that are still being sent to a LAN worker for this job.
if (item.status === "preparing" && remoteWorkerSelected()) void cancelSlopfabGeneration(work.request.jobId).catch(() => undefined);
return;
@@ -568,7 +575,10 @@ export class WorkQueue {
if (this.icons.event(event)) return;
const work = this.nativeWork(event.jobId);
const item = this.items.find((candidate) => candidate.id === work?.id);
- if (!work || !item) return;
+ if (!work || !item) {
+ if (event.state === "framesReady" && this.items.some((candidate) => candidate.id === event.jobId && !isWorkActive(candidate))) void releaseRendered(event.jobId);
+ return;
+ }
if (!isWorkActive(item)) { if (event.state === "framesReady") void releaseRendered(event.jobId); return; }
if (item.status === "encoding") return;
if (work.characterSheet) {
@@ -629,10 +639,8 @@ export class WorkQueue {
const scene = createImageEditScene({ ...saved, name: sheet.name }, work.config.imageScene ?? undefined);
scene.referenceIds = [];
const generation = { ...work.imageGeneration!, scene };
- work.session.update((current) => completeImageDraft(current, work.id, {
- id: work.id, kind: "image", name: sheet.name, ...saved, mimeType: "image/png",
- parentAssetId: imageFamilyRoot(current.assets, sheet.sourceId), imageGeneration: generation, createdAt: new Date().toISOString(),
- }));
+ work.session.update(derivedImageUpdate(work.id, sheet.sourceId, { id: work.id, kind: "image", name: sheet.name,
+ ...saved, mimeType: "image/png", imageGeneration: generation, createdAt: new Date().toISOString() }));
try { await work.session.save(); this.patch(work.id, { status: "completed", progress: 1, detail: "Character sheet saved" }); }
catch (reason) { this.patch(work.id, { status: "failed", progress: 1, needsSave: true, detail: "Character sheet created; project save failed", error: describeDiagnosticError(reason) }); }
} finally {
@@ -667,16 +675,8 @@ export class WorkQueue {
segments: work.continuationSegments, from: work.request.continuationFrom,
} : undefined);
const media = { kind: "generated" as const, relativePath: saved.relativePath, sourcePath: null, mimeType: "video/mp4", hasAudio: saved.hasAudio ?? null, width: saved.width ?? work.request.canvasWidth, height: saved.height ?? work.request.canvasHeight, durationMs: saved.durationMs ?? Math.round(totalFrames / GENERATION_FRAME_RATE * 1000), sceneSegments };
- work.session.update((current) => ({
- ...current,
- generationJobs: current.generationJobs.map((job) => job.id === work.sceneId ? { ...job, status: "completed", stage: "completed", progress: 1, generationSnapshot: work.snapshot, outputRelativePath: saved.relativePath, latentRelativePath: generationLatentPath(work.id), error: saved.note, updatedAt: new Date().toISOString() } : job),
- assets: current.assets.some((asset) => asset.id === assetId)
- ? current.assets.map((asset) => asset.id === assetId ? { ...asset, ...media } : asset)
- : current.generationJobs.some((job) => job.id === work.sceneId)
- ? [...current.assets, { id: assetId, name: current.generationJobs.find((job) => job.id === work.sceneId)!.title, createdAt: new Date().toISOString(), ...media }]
- : current.assets,
- timeline: { tracks: current.timeline.tracks.map((track) => ({ ...track, clips: track.clips.map((clip) => clip.assetId === assetId ? { ...clip, status: "generated" } : clip) })) },
- }));
+ work.session.update(videoResultUpdate(work.sceneId, assetId, media, { status: "completed", stage: "completed", progress: 1,
+ generationSnapshot: work.snapshot, outputRelativePath: saved.relativePath, latentRelativePath: generationLatentPath(work.id), error: saved.note }));
try {
await work.session.save();
this.patch(work.id, { status: "completed", progress: 1, detail: "Video saved", error: saved.note });
@@ -688,9 +688,10 @@ export class WorkQueue {
}
async retrySave(id: string) {
if (this.icons.owns(id)) { await this.icons.retrySave(); return; }
- const work = this.work.get(id);
- if (!work) return;
- try { await work.session.save(); this.patch(id, { status: "completed", needsSave: false, error: null, detail: work.image ? "Image saved" : "Video saved" }); }
+ const item = this.items.find((candidate) => candidate.id === id);
+ const session = item && this.projects.get(item.projectKey);
+ if (!item?.needsSave || !session) return;
+ try { await session.save(); this.patch(id, { status: "completed", needsSave: false, error: null, detail: item.kind === "image" ? "Image saved" : "Video saved" }); }
catch (reason) { this.patch(id, { error: describeDiagnosticError(reason) }); }
}
private async saveImage(work: PendingWork) {
@@ -700,21 +701,12 @@ export class WorkQueue {
if (work.extend) {
const scene = createImageEditScene({ ...saved, name: work.extend.name }, { ...work.config.imageScene!, steps: work.request.steps, seed: work.request.seed });
work.imageGeneration = { ...work.imageGeneration!, scene };
- work.session.update((current) => completeImageDraft(current, work.id, {
- id: work.id, kind: "image", name: work.extend!.name, ...saved, mimeType: "image/png",
- parentAssetId: imageFamilyRoot(current.assets, work.extend!.sourceId), imageGeneration: work.imageGeneration, createdAt: new Date().toISOString(),
- }));
+ work.session.update(derivedImageUpdate(work.id, work.extend.sourceId, { id: work.id, kind: "image", name: work.extend.name,
+ ...saved, mimeType: "image/png", imageGeneration: work.imageGeneration, createdAt: new Date().toISOString() }));
} else {
- work.session.update((current) => work.imageDraftId ? completeImageDraft(current, work.imageDraftId, {
- id: work.imageDraftId, kind: "image", name: `Image ${current.assets.filter((asset) => asset.kind === "image" && !asset.imageDraft).length + 1}`,
- ...saved, imageGeneration: work.imageGeneration, mimeType: work.request.imageEdit ? "image/png" : "image/jpeg", createdAt: new Date().toISOString(),
- }) : ({ ...current,
- ...(current.imageScene?.outputAssetId === work.config.imageScene?.outputAssetId ? {
- thumbnail: saved.relativePath,
- imageScene: { ...(work.request.imageEdit && JSON.stringify(current.imageScene) === work.snapshot ? createImageEditScene({ ...saved, name: "Edited image" }, current.imageScene ?? undefined) : current.imageScene ?? createImageScene()), outputAssetId: work.id },
- } : {}),
- assets: [...current.assets, { id: work.id, kind: "image", name: `Image ${current.assets.filter((asset) => asset.kind === "image").length + 1}`, ...saved, imageGeneration: work.imageGeneration, mimeType: work.request.imageEdit ? "image/png" : "image/jpeg", ...(work.imageParentId ? { parentAssetId: imageFamilyRoot(current.assets, work.imageParentId) } : {}), createdAt: new Date().toISOString() }],
- }));
+ work.session.update(imageResultUpdate({ id: work.id, imageDraftId: work.imageDraftId, imageParentId: work.imageParentId,
+ imageGeneration: work.imageGeneration, snapshot: work.snapshot, outputAssetId: work.config.imageScene?.outputAssetId,
+ imageEdit: Boolean(work.request.imageEdit), saved }));
}
try { await work.session.save(); this.patch(work.id, { status: "completed", progress: 1, detail: "Image saved" }); }
catch (reason) { this.patch(work.id, { status: "failed", progress: 1, needsSave: true, detail: "Image created; project save failed", error: describeDiagnosticError(reason) }); }
@@ -726,6 +718,50 @@ export class WorkQueue {
}
}
+// These factories live outside the running job's closure scope: undo replay
+// retains only the result fields, never its full submitted project snapshot.
+function sceneUpdate(sceneId: string, patch: Partial<GenerationJob>) {
+ return (current: ProjectConfig): ProjectConfig => ({ ...current,
+ generationJobs: current.generationJobs.map((job) => job.id === sceneId ? { ...job, ...patch, updatedAt: new Date().toISOString() } : job),
+ });
+}
+
+function derivedImageUpdate(id: string, sourceId: string, asset: ProjectAsset) {
+ return (current: ProjectConfig) => completeImageDraft(current, id, {
+ ...asset, parentAssetId: imageFamilyRoot(current.assets, sourceId),
+ });
+}
+
+function videoResultUpdate(sceneId: string, assetId: string, media: Omit<ProjectAsset, "id" | "name" | "createdAt">, patch: Partial<GenerationJob>) {
+ const update = sceneUpdate(sceneId, patch);
+ return (current: ProjectConfig): ProjectConfig => ({
+ ...update(current),
+ assets: current.assets.some((asset) => asset.id === assetId)
+ ? current.assets.map((asset) => asset.id === assetId ? { ...asset, ...media } : asset)
+ : current.generationJobs.some((job) => job.id === sceneId)
+ ? [...current.assets, { id: assetId, name: current.generationJobs.find((job) => job.id === sceneId)!.title, createdAt: new Date().toISOString(), ...media }]
+ : current.assets,
+ timeline: { tracks: current.timeline.tracks.map((track) => ({ ...track, clips: track.clips.map((clip) => clip.assetId === assetId ? { ...clip, status: "generated" } : clip) })) },
+ });
+}
+
+function imageResultUpdate({ id, imageDraftId, imageParentId, imageGeneration, snapshot, outputAssetId, imageEdit, saved }: {
+ id: string; imageDraftId?: string; imageParentId?: string; imageGeneration?: ImageGenerationSnapshot; snapshot: string;
+ outputAssetId?: string | null; imageEdit: boolean; saved: { relativePath: string; width: number; height: number };
+}) {
+ const mimeType = imageEdit ? "image/png" : "image/jpeg";
+ return (current: ProjectConfig): ProjectConfig => imageDraftId ? completeImageDraft(current, imageDraftId, {
+ id: imageDraftId, kind: "image", name: `Image ${current.assets.filter((asset) => asset.kind === "image" && !asset.imageDraft).length + 1}`,
+ ...saved, imageGeneration, mimeType, createdAt: new Date().toISOString(),
+ }) : ({ ...current,
+ ...(current.imageScene?.outputAssetId === outputAssetId ? {
+ thumbnail: saved.relativePath,
+ imageScene: { ...(imageEdit && JSON.stringify(current.imageScene) === snapshot ? createImageEditScene({ ...saved, name: "Edited image" }, current.imageScene ?? undefined) : current.imageScene ?? createImageScene()), outputAssetId: id },
+ } : {}),
+ assets: [...current.assets, { id, kind: "image", name: `Image ${current.assets.filter((asset) => asset.kind === "image").length + 1}`, ...saved, imageGeneration, mimeType, ...(imageParentId ? { parentAssetId: imageFamilyRoot(current.assets, imageParentId) } : {}), createdAt: new Date().toISOString() }],
+ });
+}
+
type WorkerTransfer = GenerationTimingProgress & { detail?: string; transferred?: number; transferTotal?: number | null };
function isWorkerTransfer(event: GenerationTimingProgress): event is WorkerTransfer {