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

Code Animation: delete waits for a poster in progress, inside the backup boundary

Claude4e6e1ad3d20f
diff --git a/server/routes/codeAnimation.test.js b/server/routes/codeAnimation.test.js
index 8b1548dd90..45135f51e1 100644
--- a/server/routes/codeAnimation.test.js
+++ b/server/routes/codeAnimation.test.js
@@ -38,7 +38,10 @@ vi.mock('../services/codeAnimation/jobStore.js', () => ({
saveCodeAnimationHtml: vi.fn(async (id, html) => codeAnimationHtml.set(id, html)),
saveCodeAnimationJobRecord: vi.fn(async (job) => codeAnimationRecords.set(job.id, job)),
deleteCodeAnimationJobRecord: vi.fn(async (id) => codeAnimationRecords.delete(id)),
- codeAnimationJobFiles: vi.fn(async (id) => (codeAnimationHtml.has(id) ? [`/animations/${id}.html`] : [])),
+ codeAnimationJobFiles: vi.fn(async (id) => [
+ ...(codeAnimationHtml.has(id) ? [`/animations/${id}.html`] : []),
+ ...(codeAnimationPosters.has(id) ? [`/animations/${id}.jpg`] : []),
+ ]),
codeAnimationPosterPath: (id) => `${process.env.PORTOS_TEST_POSTER_DIR}/${id}.jpg`,
codeAnimationPosterVersions: vi.fn(async (ids) => new Map(ids.filter((id) => codeAnimationPosters.has(id)).map((id) => [id, codeAnimationPosters.get(id)]))),
}));
@@ -49,6 +52,7 @@ vi.mock('../services/codeAnimation/poster.js', () => ({
codeAnimationPosters.set(id, 1700000000000);
return true;
}),
+ settleCodeAnimationPoster: vi.fn(async () => false),
}));
vi.mock('../services/socket.js', () => ({
// Observe the persisted state at emit time: a notification before a save
@@ -96,7 +100,7 @@ import { getTrack } from '../services/tracks/index.js';
import { listCodeAnimationJobPage, listCodeAnimationJobRecords, readCodeAnimationHtml, saveCodeAnimationHtml, saveCodeAnimationJobRecord } from '../services/codeAnimation/jobStore.js';
import { assertProvider, resolveProviderAndModel, runPromptThroughProvider } from '../services/promptRunner.js';
import { enqueueJob } from '../services/mediaJobQueue/index.js';
-import { captureCodeAnimationPoster } from '../services/codeAnimation/poster.js';
+import { captureCodeAnimationPoster, settleCodeAnimationPoster } from '../services/codeAnimation/poster.js';
import routes from './codeAnimation.js';
const makeApp = () => {
@@ -204,6 +208,19 @@ describe('DELETE /api/code-animation/jobs/:id', () => {
expect((await request(app).delete(`/api/code-animation/jobs/${id}`)).status).toBe(404);
});
+ it('waits for a poster still being drawn, so it goes to the Trash too', async () => {
+ codeAnimationRecords.set(id, finished);
+ codeAnimationHtml.set(id, '<!doctype html>');
+ let drawn;
+ settleCodeAnimationPoster.mockReturnValueOnce(new Promise((resolve) => { drawn = resolve; }));
+ const deleting = request(makeApp()).delete(`/api/code-animation/jobs/${id}`).then((res) => res);
+ await vi.waitFor(() => expect(settleCodeAnimationPoster).toHaveBeenCalledWith(id));
+ codeAnimationPosters.set(id, 1700000000000);
+ drawn(true);
+ expect((await deleting).status).toBe(200);
+ expect(moveToNativeTrash).toHaveBeenCalledWith([`/animations/${id}.html`, `/animations/${id}.jpg`]);
+ });
+
it('keeps the item when the Trash refuses its files', async () => {
codeAnimationRecords.set(id, finished);
codeAnimationHtml.set(id, '<!doctype html>');
diff --git a/server/services/codeAnimation/index.js b/server/services/codeAnimation/index.js
index 6682facaa1..733434e599 100644
--- a/server/services/codeAnimation/index.js
+++ b/server/services/codeAnimation/index.js
@@ -392,16 +392,24 @@ export async function deleteCodeAnimationJob(id, { moveToTrash } = {}) {
if (job.status === 'running' && activeJobs.has(id)) {
throw new ServerError('This animation is still generating. Delete it once it finishes.', { status: 409, code: 'CODE_ANIMATION_RUNNING' });
}
- const files = await codeAnimationJobFiles(id);
- if (files.length) {
- const trash = moveToTrash || (await import('../../lib/nativeTrash.js')).moveToNativeTrash;
- await trash(files).catch((error) => {
- throw new ServerError(`Could not move the animation files to the Trash (${error.code || error.message}). Nothing was deleted.`, {
- status: 500, code: 'CODE_ANIMATION_TRASH_FAILED',
+ // A poster drawn just after completion must land before the files are
+ // listed, or it would be written after the row is gone.
+ await (await import('./poster.js')).settleCodeAnimationPoster(id);
+ // Files and row leave together, as they were published (#9982), so a
+ // snapshot never holds a completed row whose HTML is gone.
+ let files = [];
+ await withBackupAssetPublication(async () => {
+ files = await codeAnimationJobFiles(id);
+ if (files.length) {
+ const trash = moveToTrash || (await import('../../lib/nativeTrash.js')).moveToNativeTrash;
+ await trash(files).catch((error) => {
+ throw new ServerError(`Could not move the animation files to the Trash (${error.code || error.message}). Nothing was deleted.`, {
+ status: 500, code: 'CODE_ANIMATION_TRASH_FAILED',
+ });
});
- });
- }
- await deleteCodeAnimationJobRecord(id);
+ }
+ await deleteCodeAnimationJobRecord(id);
+ });
emitCodeAnimationChanged(id, 'deleted');
console.log(`🗑️ Code animation ${id.slice(0, 8)} deleted (${files.length} file(s) to Trash)`);
return { id, deleted: true };
diff --git a/server/services/codeAnimation/poster.js b/server/services/codeAnimation/poster.js
index 111cc10563..1e875b4b94 100644
--- a/server/services/codeAnimation/poster.js
+++ b/server/services/codeAnimation/poster.js
@@ -83,6 +83,11 @@ async function capture(id) {
}
}
+/** Resolves once a capture in flight (or queued) for `id` has finished writing. */
+export function settleCodeAnimationPoster(id) {
+ return inFlight.get(id) || Promise.resolve(false);
+}
+
/**
* Capture a completed job's poster. Resolves true when a poster was written;
* never rejects, because the completion path runs it detached.