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
fix: honor cancellation while cloud renders prepare (#12052)
Adam Eivy819b4f0f85f5
diff --git a/server/lib/README.md b/server/lib/README.mdindex d7d7a5d355..e0fb3f7777 100644--- a/server/lib/README.md+++ b/server/lib/README.md@@ -44,6 +44,7 @@ The barrel `server/lib/index.js` is a machine-checkable enumeration of every pub| `taskDataInputCatalog.js` | Pure catalog and persisted ids for deterministic context sources that scheduled agent tasks can preload before dispatch. || `sharedSchemas.js` | Cross-domain Zod fragments that `validation.js` and the per-domain `*Validation.js` files both need, kept in a leaf module so the domain files never import back through `validation.js` (its hoisted `export * from` lines would TDZ). `grokVideoDurationSchema` (grok clip-length union), `cloudModelIdString(message)` (cloud-CLI model-id charset/bounds), `recordRenderPinFields` (the `imageMode`/`imageModelId` per-record render pin pair), and `isSafeSubdirFilter(v)` (relative path with no wildcard, `..` segment, or leading `/`), and `csvIdsParam({ max, maxIdLength, truncate })` (the `?ids=a,b,c` batch-by-id query param: trims, drops empties, reads all-blank as absent, and either 400s or silently slices an over-cap batch). Also the backup-exclude anchoring rule (#7241): `anchorUserExclude` / `anchorUserExcludes` prepend the rsync root anchor (`/`) to any user pattern that is not already anchored and is not deliberately `*`/`**`-led, and `isSafeExcludePattern` (defined as `anchorUserExclude(x) !== null`, so the two can never disagree) rejects a `..` segment, a NUL, or a pattern longer than `EXCLUDE_PATTERN_MAX_LENGTH` once anchored — one rule shared by `backupConfigSchema` and `services/backup.js#computeEffectiveExcludes`, since an unanchored `cache/` is an rsync filter that matches at EVERY depth. Mirrored to `client/src/lib/backupExcludes.js`. Also `resolveAutoUpdateConfig(settings.autoUpdate)` + the channel/interval constants — same sparse-slice rule for the automatic self-update: omitted `enabled` means OFF, omitted `channel` means `release`, `minIntervalHours` clamps into `[1, 720]`, `resolveBlockersWithAgent` defaults on — shared by `autoUpdateSettingsSchema`, `services/autoUpdateScheduler.js`, and the settings GET projection. || `generationModes.js` | Shared immutable image/video render-backend alphabets (`IMAGE_GEN_MODE[S]`, `QUEUEABLE_IMAGE_MODES`, `VIDEO_GEN_MODE[S]`, and cloud subsets) plus `mediaJobExecutionLane({ kind, mode, remote })` / `MEDIA_JOB_EXECUTION_LANES` — THE media-queue lane rule the scheduler and the public job projection share, kept below both validation and generation services so schemas never import orchestration modules. |+| `mediaJobPreparation.js` | `createMediaJobPreparation()` tracks per-job cancellation before cloud providers install a render handle; `run`, `cancel`, `take`, and `ids` cover preparation, handoff, and bulk cancellation. || `imageGenCapabilities.js` | Per-backend image-gen capability literals, a dependency-free leaf beside `generationModes.js`: `EDIT_INCAPABLE_IMAGE_MODES`/`isEditCapableMode` and the derived `I2I_CAPABLE_MODES`, the shipped defaults (`CODEX_IMAGEGEN_DEFAULT_MODEL`/`_EFFORT`, `AGY_IMAGEGEN_DEFAULT_MODEL`, `AGY_IMAGEGEN_IMAGE_MODEL`), the `AGY_`/`GROK_ASPECT_RATIOS` alphabets, and `IMAGE_GEN_PROVIDER_CAPABILITIES` (`supportsModelOverride`, `maxInputImages`, `promptRequiredWithInputImage`) with its `supportsCloudModelOverride`/`maxInputImages`/`cloudPromptRequired` readers. The `services/imageGen/*` modules re-export these, and `client/src/lib/imageGenModes.js` imports them instead of copying them (#6590). || `falImageModels.js` | Curated fal.ai image model catalog, a dependency-free leaf the client imports: `FAL_IMAGE_FAMILIES` (text + `/edit` endpoint per family, per-model reference cap, sizing alphabet, pricing), `FAL_IMAGE_DEFAULT_MODEL`, `FAL_IMAGE_MODEL_IDS` (the only ids a fal `cloudModel` may name), `falImageFamily`/`isFalImageModel`, `falImagePriceLabel`, `buildFalImageRequest` (routes to `/edit` when input images are present; maps width/height to aspect-ratio + resolution tier or pixel `image_size`) and `estimateFalImageCostUsd` (with `FAL_MEGAPIXEL`, fal.ai's binary megapixel). || `imageRuntimeRemedies.js` | Local image-runtime vocabulary, a dependency-free leaf beside `imageGenCapabilities.js`: `IMAGE_RUNTIME_READINESS` (ready/unavailable/unknown), `IMAGE_RUNTIME` + `IMAGE_RUNTIME_LABELS` (the shared torch venv vs. the configurable mflux interpreter), `IMAGE_RUNTIME_REMEDY` (the one action that fixes each unavailable state), and `IMAGE_RUNTIME_UNAVAILABLE_CODE`. `services/imageGen/localRuntime.js` builds the diagnosis from these, `services/imageGen/local.js` tags its pre-flight render refusal with the same remedy, and `client/src/lib/imageGenModes.js` re-exports them so no component hand-copies a remedy kind. |diff --git a/server/lib/index.js b/server/lib/index.jsindex 7c1a3bc8b6..7f48508365 100644--- a/server/lib/index.js+++ b/server/lib/index.js@@ -897,3 +897,5 @@ export * from './musicVideoImageEffects.js';export * from './securityScanEvidence.js';export * from './musicDesignerValidation.js';++export * from './mediaJobPreparation.js';diff --git a/server/lib/mediaJobPreparation.js b/server/lib/mediaJobPreparation.jsnew file mode 100644index 0000000000..f3cd12933f--- /dev/null+++ b/server/lib/mediaJobPreparation.js@@ -0,0 +1,32 @@+import { randomUUID } from 'node:crypto';++/** Track per-job cancellation until a provider installs its render handle. */+export function createMediaJobPreparation() {+ const preparing = new Map();+ return {+ async run(params, generate) {+ const jobId = params.jobId || randomUUID();+ const entry = { aborted: false };+ preparing.set(jobId, entry);+ try {+ return await generate({ ...params, jobId });+ } finally {+ if (preparing.get(jobId) === entry) preparing.delete(jobId);+ }+ },+ cancel(jobId) {+ const entry = preparing.get(jobId);+ if (!entry) return false;+ entry.aborted = true;+ return 'pending';+ },+ // Called synchronously at the spawn/submit handoff; no await may separate+ // this gate from installing the provider's active handle.+ take(jobId) {+ const entry = preparing.get(jobId);+ preparing.delete(jobId);+ return entry?.aborted === true;+ },+ ids: () => [...preparing.keys()],+ };+}diff --git a/server/services/imageGen/agy.js b/server/services/imageGen/agy.jsindex 44a2c78c91..508b5509af 100644--- a/server/services/imageGen/agy.js+++ b/server/services/imageGen/agy.js@@ -1,3 +1,4 @@+import { createMediaJobPreparation } from '../../lib/mediaJobPreparation.js';import { withBackupAssetPublication } from '../../lib/backupSnapshotBoundary.js';import { maintenance } from '../../lib/maintenanceAdmission.js';/**@@ -82,8 +83,12 @@ const terminate = (jobId, proc) => {});};+const preparations = createMediaJobPreparation();+export const cancel = (jobId) => {if (!jobId) throw new Error('agy.cancel requires a jobId — use agy.cancelAll() to terminate every in-flight render');+ const pending = preparations.cancel(jobId);+ if (pending) return pending;const proc = activeProcs.get(jobId);if (!proc) return false;terminate(jobId, proc);@@ -91,9 +96,9 @@ export const cancel = (jobId) => {};export const cancelAll = () => {- const entries = [...activeProcs.entries()];- if (!entries.length) return false;- for (const [jobId, proc] of entries) terminate(jobId, proc);+ const ids = [...preparations.ids(), ...activeProcs.keys()];+ if (ids.length === 0) return false;+ for (const id of ids) cancel(id);return true;};@@ -276,7 +281,9 @@ export function agySentPrompt({ prompt = '', negativePrompt, width, height, stag};}-export async function generateImage({+export const generateImage = (params) => preparations.run(params, generateImagePrepared);++async function generateImagePrepared({promptAdditions = [],agyPath,model,@@ -408,6 +415,10 @@ function runAgy(job, jobId, bin, args, {maintenance.markCurrentUnsettled();console.error(`❌ agy scratch cleanup failed: ${err.message}`);});+ if (preparations.take(jobId)) {+ void removeScratch();+ return finalizeJobFailure.canceled(job, jobId);+ }let proc;try {const resolvedBin = (!isAbsolute(bin) && (bin.includes('/') || bin.includes(sep))) ? pathResolve(bin) : bin;diff --git a/server/services/imageGen/agy.test.js b/server/services/imageGen/agy.test.jsindex c46dbedc9d..76f61a2b04 100644--- a/server/services/imageGen/agy.test.js+++ b/server/services/imageGen/agy.test.js@@ -519,3 +519,19 @@ describe('agy image provider — launch refusal settlement', () => {expect(agy.cancel(job.jobId)).toBe(false);});});++it('cancels during preparation before starting a render', async () => {+ const { imageGenEvents: events } = await import('../imageGenEvents.js');+ const failed = vi.fn();+ const completed = vi.fn();+ events.on('failed', failed);+ events.on('completed', completed);+ const jobId = 'preamble-cancel-imageGen-agy';+ const pending = agy.generateImage({ prompt: 'a quiet gate', jobId });+ expect(agy.cancel(jobId)).toBe('pending');+ await pending;+ await vi.waitFor(() => expect(failed).toHaveBeenCalledWith(expect.objectContaining({ generationId: jobId, error: 'Canceled' })));+ expect(completed).not.toHaveBeenCalled();+ expect(spawnCalls).toHaveLength(0);+ expect(agy.cancel(jobId)).toBe(false);+});diff --git a/server/services/imageGen/codex.js b/server/services/imageGen/codex.jsindex 49637d06e4..98d709e071 100644--- a/server/services/imageGen/codex.js+++ b/server/services/imageGen/codex.js@@ -1,3 +1,4 @@+import { createMediaJobPreparation } from '../../lib/mediaJobPreparation.js';import { withBackupAssetPublication } from '../../lib/backupSnapshotBoundary.js';/*** Image Gen — Codex CLI provider.@@ -130,10 +131,14 @@ const sigtermWithEscalation = (id, proc) => {// in-flight render), so callers have to be explicit. Use `cancelAll()` for// the legacy "stop everything" path that the imageGen.cancel() dispatcher// wires up.+const preparations = createMediaJobPreparation();+export const cancel = (jobId) => {if (!jobId) {throw new Error("codex.cancel requires a jobId — use codex.cancelAll() to terminate every in-flight render");}+ const pending = preparations.cancel(jobId);+ if (pending) return pending;const proc = activeProcs.get(jobId);if (!proc) return false;sigtermWithEscalation(jobId, proc);@@ -144,9 +149,9 @@ export const cancel = (jobId) => {// dispatcher's "cancel everything" route — the per-job mediaJobQueue path// always passes a specific jobId to `cancel()`.export const cancelAll = () => {- const entries = [...activeProcs.entries()];- if (entries.length === 0) return false;- for (const [id, proc] of entries) sigtermWithEscalation(id, proc);+ const ids = [...preparations.ids(), ...activeProcs.keys()];+ if (ids.length === 0) return false;+ for (const id of ids) cancel(id);return true;};@@ -237,7 +242,9 @@ export function codexSentPrompt({ prompt = '', negativePrompt, width, height, in// Input images are attached via codex CLI's `-i <FILE>` flag (variadic, so all// of them ride one flag) and the prompt is reshaped so the `$imagegen` skill// feeds the attachments to `image_gen` as references.-export async function generateImage({+export const generateImage = (params) => preparations.run(params, generateImagePrepared);++async function generateImagePrepared({promptAdditions = [],codexPath, model, modelIsShippedDefault = false, effort, prompt = '', width, height, negativePrompt,initImagePath, initImageStrength, referenceImagePaths = [],@@ -401,6 +408,10 @@ function removeScratchDirectory(scratchDir) {// close handler to settle once it has physically exited.function runCodex(job, jobId, bin, args, outputPath, filename, meta, { cleanC2PA = false, denoise = false, scratchDir } = {}) {const removeScratch = () => removeScratchDirectory(scratchDir);+ if (preparations.take(jobId)) {+ void removeScratch();+ return finalizeJobFailure.canceled(job, jobId);+ }let proc;try {const resolvedBin = !isAbsolute(bin) && (bin.includes('/') || bin.includes(sep)) ? resolve(bin) : bin;diff --git a/server/services/imageGen/codex.test.js b/server/services/imageGen/codex.test.jsindex 892c642567..4b7a7da58d 100644--- a/server/services/imageGen/codex.test.js+++ b/server/services/imageGen/codex.test.js@@ -1099,3 +1099,19 @@ describe('Codex cached defaults and isolated workspace', () => {expect(JSON.stringify(failed.mock.calls)).toContain('does not establish whether the tool is available');});});++it('cancels during preparation before starting a render', async () => {+ const { imageGenEvents: events } = await import('../imageGenEvents.js');+ const failed = vi.fn();+ const completed = vi.fn();+ events.on('failed', failed);+ events.on('completed', completed);+ const jobId = 'preamble-cancel-imageGen-codex';+ const pending = codex.generateImage({ prompt: 'a quiet gate', jobId });+ expect(codex.cancel(jobId)).toBe('pending');+ await pending;+ await vi.waitFor(() => expect(failed).toHaveBeenCalledWith(expect.objectContaining({ generationId: jobId, error: 'Canceled' })));+ expect(completed).not.toHaveBeenCalled();+ expect(spawnCalls).toHaveLength(0);+ expect(codex.cancel(jobId)).toBe(false);+});diff --git a/server/services/imageGen/fal.js b/server/services/imageGen/fal.jsindex 2c59a07e23..e54bea9226 100644--- a/server/services/imageGen/fal.js+++ b/server/services/imageGen/fal.js@@ -1,3 +1,4 @@+import { createMediaJobPreparation } from '../../lib/mediaJobPreparation.js';import { withBackupAssetPublication } from '../../lib/backupSnapshotBoundary.js';/*** Image Gen — fal.ai queue REST API provider.@@ -96,10 +97,14 @@ export const attachSseClient = (jobId, res) => attachSse(jobs, jobId, res);// Cancel one specific render. jobId is required — with parallel renders an// anonymous cancel would stop every in-flight one; use cancelAll() for that.+const preparations = createMediaJobPreparation();+export const cancel = (jobId) => {if (!jobId) {throw new Error('imageGen/fal.cancel requires a jobId — use cancelAll() to terminate every in-flight render');}+ const pending = preparations.cancel(jobId);+ if (pending) return pending;const entry = activeRequests.get(jobId);if (!entry) return false;entry.aborted = true;@@ -109,7 +114,7 @@ export const cancel = (jobId) => {};export const cancelAll = () => {- const ids = [...activeRequests.keys()];+ const ids = [...preparations.ids(), ...activeRequests.keys()];if (ids.length === 0) return false;for (const id of ids) cancel(id);return true;@@ -218,7 +223,9 @@ export function falImageSentPrompt({ family, prompt = '', negativePrompt, inputI};}-export async function generateImage({+export const generateImage = (params) => preparations.run(params, generateImagePrepared);++async function generateImagePrepared({promptAdditions = [],model, prompt = '', negativePrompt, width, height, seed,initImagePath, initImageStrength, referenceImagePaths = [],@@ -312,6 +319,7 @@ async function runFal(job, jobId, {inputPaths, outputPath, filename, meta, cleanC2PA = false, denoise = false,}) {const entry = createFalRequestEntry(apiKey);+ entry.aborted = preparations.take(jobId);activeRequests.set(jobId, entry);const deadline = Date.now() + FAL_IMAGE_TIMEOUT_MS;let wroteOutput = false;diff --git a/server/services/imageGen/fal.test.js b/server/services/imageGen/fal.test.jsindex 4583cbf23e..2a1f4d0b13 100644--- a/server/services/imageGen/fal.test.js+++ b/server/services/imageGen/fal.test.js@@ -249,3 +249,20 @@ describe('imageGen/fal — checkConnection', () => {expect(await fal.checkConnection({})).toMatchObject({ connected: false, reason: expect.stringContaining('HTTP 401') });});});++it('cancels during preparation before starting a render', async () => {+ const { imageGenEvents: events } = await import('../imageGenEvents.js');+ const failed = vi.fn();+ const completed = vi.fn();+ events.on('failed', failed);+ events.on('completed', completed);+ vi.stubGlobal('fetch', vi.fn());+ const jobId = 'preamble-cancel-imageGen-fal';+ const pending = fal.generateImage({ prompt: 'a quiet gate', jobId });+ expect(fal.cancel(jobId)).toBe('pending');+ await pending;+ await vi.waitFor(() => expect(failed).toHaveBeenCalledWith(expect.objectContaining({ generationId: jobId, error: 'Canceled' })));+ expect(completed).not.toHaveBeenCalled();+ expect(fetch).not.toHaveBeenCalled();+ expect(fal.cancel(jobId)).toBe(false);+});diff --git a/server/services/imageGen/grok.js b/server/services/imageGen/grok.jsindex b75f2973c8..db0faa5726 100644--- a/server/services/imageGen/grok.js+++ b/server/services/imageGen/grok.js@@ -1,3 +1,4 @@+import { createMediaJobPreparation } from '../../lib/mediaJobPreparation.js';import { withBackupAssetPublication } from '../../lib/backupSnapshotBoundary.js';import { maintenance } from '../../lib/maintenanceAdmission.js';/**@@ -115,10 +116,14 @@ const sigtermWithEscalation = (id, proc) => {// Cancel one specific grok render. jobId is required — with parallel renders// an "anonymous cancel" would nuke every in-flight render. Use `cancelAll()`// for the dispatcher's "stop everything" path.+const preparations = createMediaJobPreparation();+export const cancel = (jobId) => {if (!jobId) {throw new Error("grok.cancel requires a jobId — use grok.cancelAll() to terminate every in-flight render");}+ const pending = preparations.cancel(jobId);+ if (pending) return pending;const proc = activeProcs.get(jobId);if (!proc) return false;sigtermWithEscalation(jobId, proc);@@ -128,9 +133,9 @@ export const cancel = (jobId) => {// Bulk terminate every in-flight grok render. Only used by the imageGen// dispatcher's "cancel everything" route.export const cancelAll = () => {- const entries = [...activeProcs.entries()];- if (entries.length === 0) return false;- for (const [id, proc] of entries) sigtermWithEscalation(id, proc);+ const ids = [...preparations.ids(), ...activeProcs.keys()];+ if (ids.length === 0) return false;+ for (const id of ids) cancel(id);return true;};@@ -238,7 +243,9 @@ export function grokSentPrompt({ prompt = '', negativePrompt, width, height, asp// `initImageStrength` maps to a fidelity phrase (grok's image_edit has no// numeric denoise knob, same constraint as codex).-export async function generateImage({+export const generateImage = (params) => preparations.run(params, generateImagePrepared);++async function generateImagePrepared({promptAdditions = [],grokPath, aspectRatio, prompt = '', width, height, negativePrompt,initImagePath, initImageStrength, referenceImagePaths = [],@@ -351,6 +358,10 @@ function runGrok(job, jobId, bin, baseArgs, {console.error(`❌ grok scratch cleanup failed: ${err.message}`);});+ if (preparations.take(jobId)) {+ void removeScratch();+ return finalizeJobFailure.canceled(job, jobId);+ }let proc;let useStdin;try {diff --git a/server/services/imageGen/grok.test.js b/server/services/imageGen/grok.test.jsindex 0d3c42a67f..4497b58ec5 100644--- a/server/services/imageGen/grok.test.js+++ b/server/services/imageGen/grok.test.js@@ -652,3 +652,19 @@ describe('grok provider — launch refusal settlement', () => {expect(grok.cancel(job.jobId)).toBe(false);});});++it('cancels during preparation before starting a render', async () => {+ const { imageGenEvents: events } = await import('../imageGenEvents.js');+ const failed = vi.fn();+ const completed = vi.fn();+ events.on('failed', failed);+ events.on('completed', completed);+ const jobId = 'preamble-cancel-imageGen-grok';+ const pending = grok.generateImage({ prompt: 'a quiet gate', jobId });+ expect(grok.cancel(jobId)).toBe('pending');+ await pending;+ await vi.waitFor(() => expect(failed).toHaveBeenCalledWith(expect.objectContaining({ generationId: jobId, error: 'Canceled' })));+ expect(completed).not.toHaveBeenCalled();+ expect(spawnCalls).toHaveLength(0);+ expect(grok.cancel(jobId)).toBe(false);+});diff --git a/server/services/mediaJobQueue/index.test.js b/server/services/mediaJobQueue/index.test.jsindex 20b5d52319..bbabde58cb 100644--- a/server/services/mediaJobQueue/index.test.js+++ b/server/services/mediaJobQueue/index.test.js@@ -1060,6 +1060,25 @@ describe('mediaJobQueue', () => {for (const path of [uploadedTempPath, audioFilePath, extraPath]) expect(existsSync(path)).toBe(false);});+ it('accepts cancellation while a cloud provider is preparing before started', async () => {+ let finishPreparation;+ stubs.generateVideoGrok.mockImplementationOnce(({ jobId }) => new Promise(resolve => {+ finishPreparation = () => {+ videoGenEvents.emit('failed', { generationId: jobId, error: 'Canceled' });+ resolve({ jobId });+ };+ }));+ const job = await mediaJobQueue.enqueueJob({ kind: 'video', params: { mode: 'grok', prompt: 'preparing render' } });+ await waitFor(() => typeof finishPreparation === 'function');+ stubs.cancelVideo.mockReturnValueOnce('pending');+ try {+ await expect(mediaJobQueue.cancelJob(job.jobId)).resolves.toEqual({ ok: true, status: 'canceling' });+ } finally {+ finishPreparation();+ }+ await waitFor(() => mediaJobQueue.getJob(job.jobId).status === 'canceled');+ });+it('restores cancellation bookkeeping when a provider refuses during finalization', async () => {const job = await mediaJobQueue.enqueueJob({ kind: 'video', params: {mode: 'grok', prompt: 'finishing render', videoProduction: { submissionUncertain: false },diff --git a/server/services/videoGen/fal.js b/server/services/videoGen/fal.jsindex ec8499a927..ed0d171122 100644--- a/server/services/videoGen/fal.js+++ b/server/services/videoGen/fal.js@@ -23,6 +23,7 @@* cost is recorded as unknown.*/+import { createMediaJobPreparation } from '../../lib/mediaJobPreparation.js';import { randomUUID } from 'crypto';import { readFile, writeFile, unlink } from 'fs/promises';import { join, relative, resolve, isAbsolute } from 'path';@@ -98,10 +99,14 @@ export const getActiveJob = () => {export const attachSseClient = (jobId, res) => attachSse(jobs, jobId, res);+const preparations = createMediaJobPreparation();+export const cancel = (jobId) => {if (!jobId) {throw new Error("videoGen/fal.cancel requires a jobId — use cancelAll() to terminate every in-flight render");}+ const pending = preparations.cancel(jobId);+ if (pending) return pending;const entry = activeRequests.get(jobId);if (!entry) return false;entry.aborted = true;@@ -111,7 +116,7 @@ export const cancel = (jobId) => {};export const cancelAll = () => {- const ids = [...activeRequests.keys()];+ const ids = [...preparations.ids(), ...activeRequests.keys()];if (ids.length === 0) return false;for (const id of ids) cancel(id);return true;@@ -181,7 +186,9 @@ export function falVideoSentPrompt({ hasImage = false, hasEndImage = false, hasAreturn { planned, record };}-export async function generateVideo({+export const generateVideo = (params) => preparations.run(params, generateVideoPrepared);++async function generateVideoPrepared({apiKey: providedApiKey, settings, modelId: requestedModelId,prompt = '', negativePrompt, duration, aspectRatio, width, height,sourceImagePath = null, lastImagePath = null, jobId: providedJobId = null,@@ -314,6 +321,7 @@ async function runFalVideo(job, jobId, {frameAspect = null,}) {const entry = createFalRequestEntry(apiKey);+ entry.aborted = preparations.take(jobId);const publication = {};// Every terminal path shares the same cleanup attempt. A publication error// can enter the catch after the success path has already settled its inputs.diff --git a/server/services/videoGen/fal.test.js b/server/services/videoGen/fal.test.jsindex 02e826a0fe..e934637792 100644--- a/server/services/videoGen/fal.test.js+++ b/server/services/videoGen/fal.test.js@@ -934,3 +934,20 @@ describe('hosted inputs', () => {expect(JSON.stringify(posted)).not.toContain('data:');});});++it('cancels during preparation before starting a render', async () => {+ const { videoGenEvents: events } = await import('./events.js');+ const failed = vi.fn();+ const completed = vi.fn();+ events.on('failed', failed);+ events.on('completed', completed);+ vi.stubGlobal('fetch', vi.fn());+ const jobId = 'preamble-cancel-videoGen-fal';+ const pending = fal.generateVideo({ prompt: 'a quiet gate', jobId, apiKey: 'test-key', modelId: 'fal-ai/veo3.1/fast' });+ expect(fal.cancel(jobId)).toBe('pending');+ await pending;+ await vi.waitFor(() => expect(failed).toHaveBeenCalledWith(expect.objectContaining({ generationId: jobId, error: 'Canceled' })));+ expect(completed).not.toHaveBeenCalled();+ expect(fetch).not.toHaveBeenCalled();+ expect(fal.cancel(jobId)).toBe(false);+});diff --git a/server/services/videoGen/grok.js b/server/services/videoGen/grok.jsindex a5914eca46..640556a5bb 100644--- a/server/services/videoGen/grok.js+++ b/server/services/videoGen/grok.js@@ -21,6 +21,7 @@* disabled/missing-binary up front; this module assumes it's enabled.*/+import { createMediaJobPreparation } from '../../lib/mediaJobPreparation.js';import { spawn } from '../../lib/childProcess.js';import { mkdir, open, stat } from 'fs/promises';import { isAbsolute, join, resolve as pathResolve, sep } from 'path';@@ -83,10 +84,14 @@ const sigtermWithEscalation = (id, proc) => {killWithEscalation(proc, { label: 'grok video child', delayMs: 5000, stillRunning: () => activeProcs.get(id) === proc });};+const preparations = createMediaJobPreparation();+export const cancel = (jobId) => {if (!jobId) {throw new Error("videoGen/grok.cancel requires a jobId — use cancelAll() to terminate every in-flight render");}+ const pending = preparations.cancel(jobId);+ if (pending) return pending;const proc = activeProcs.get(jobId);if (!proc) return false;sigtermWithEscalation(jobId, proc);@@ -94,9 +99,9 @@ export const cancel = (jobId) => {};export const cancelAll = () => {- const entries = [...activeProcs.entries()];- if (entries.length === 0) return false;- for (const [id, proc] of entries) sigtermWithEscalation(id, proc);+ const ids = [...preparations.ids(), ...activeProcs.keys()];+ if (ids.length === 0) return false;+ for (const id of ids) cancel(id);return true;};@@ -141,7 +146,9 @@ export function grokVideoSentPrompt({};}-export async function generateVideo({+export const generateVideo = (params) => preparations.run(params, generateVideoPrepared);++async function generateVideoPrepared({grokPath, aspectRatio, prompt = '', negativePrompt, width, height,sourceImagePath = null, duration,uploadedTempPath = null,@@ -220,15 +227,6 @@ export async function generateVideo({async function runGrokVideo(job, jobId, bin, args, {useStdin, fullPrompt, cleanupPromptFile, scratchDir, stagingPath, outputPath, filename, meta, uploadedTempPath,}) {- // Resolve a path-shaped grokPath against the PortOS cwd before the child's- // cwd moves to the scratch dir; bare names stay bare for PATH lookup.- const resolvedBin = (!isAbsolute(bin) && (bin.includes('/') || bin.includes(sep))) ? pathResolve(bin) : bin;- const { command: spawnBin, args: spawnArgs } = prepareCliSpawn(resolvedBin, args);- // Pin PWD to the spawn cwd — see withSpawnCwdEnv (#3193). grok reads- // process.cwd(), so this is defensive rather than a live fix; it keeps every- // scratch-dir spawn telling the child one consistent story about where it is.- const proc = spawn(spawnBin, spawnArgs, { cwd: scratchDir, env: withSpawnCwdEnv(process.env, scratchDir), shell: false, stdio: [useStdin ? 'pipe' : 'ignore', 'pipe', 'pipe'] });- activeProcs.set(jobId, proc);const retainOwnership = (error) => {maintenance.markCurrentUnsettled();maintenance.markResourceUnsettled('media', jobId);@@ -249,6 +247,20 @@ async function runGrokVideo(job, jobId, bin, args, {}});+ if (preparations.take(jobId)) {+ await cleanup();+ return finalizeJobFailure.canceled(job, jobId);+ }+ // Resolve a path-shaped grokPath against the PortOS cwd before the child's+ // cwd moves to the scratch dir; bare names stay bare for PATH lookup.+ const resolvedBin = (!isAbsolute(bin) && (bin.includes('/') || bin.includes(sep))) ? pathResolve(bin) : bin;+ const { command: spawnBin, args: spawnArgs } = prepareCliSpawn(resolvedBin, args);+ // Pin PWD to the spawn cwd — see withSpawnCwdEnv (#3193). grok reads+ // process.cwd(), so this is defensive rather than a live fix; it keeps every+ // scratch-dir spawn telling the child one consistent story about where it is.+ const proc = spawn(spawnBin, spawnArgs, { cwd: scratchDir, env: withSpawnCwdEnv(process.env, scratchDir), shell: false, stdio: [useStdin ? 'pipe' : 'ignore', 'pipe', 'pipe'] });+ activeProcs.set(jobId, proc);+if (useStdin) {proc.stdin.on('error', () => {});proc.stdin.write(fullPrompt);diff --git a/server/services/videoGen/grok.test.js b/server/services/videoGen/grok.test.jsindex eb457e6068..6f12e1adfd 100644--- a/server/services/videoGen/grok.test.js+++ b/server/services/videoGen/grok.test.js@@ -415,3 +415,19 @@ describe('videoGen/grok — cancel', () => {expect(grok.cancelAll()).toBe(false);});});++it('cancels during preparation before starting a render', async () => {+ const { videoGenEvents: events } = await import('./events.js');+ const failed = vi.fn();+ const completed = vi.fn();+ events.on('failed', failed);+ events.on('completed', completed);+ const jobId = 'preamble-cancel-videoGen-grok';+ const pending = grok.generateVideo({ prompt: 'a quiet gate', jobId });+ expect(grok.cancel(jobId)).toBe('pending');+ await pending;+ await vi.waitFor(() => expect(failed).toHaveBeenCalledWith(expect.objectContaining({ generationId: jobId, error: 'Canceled' })));+ expect(completed).not.toHaveBeenCalled();+ expect(spawnCalls).toHaveLength(0);+ expect(grok.cancel(jobId)).toBe(false);+});diff --git a/server/services/videoGen/reactor.js b/server/services/videoGen/reactor.jsindex d7e039aa26..d3f6b3e4dd 100644--- a/server/services/videoGen/reactor.js+++ b/server/services/videoGen/reactor.js@@ -1,4 +1,5 @@/** Reactor FastH3 renders one bounded SDK/WebRTC session per job. */+import { createMediaJobPreparation } from '../../lib/mediaJobPreparation.js';import { randomUUID } from 'crypto';import { stat, rm } from 'fs/promises';import { spawn } from '../../lib/childProcess.js';@@ -79,10 +80,14 @@ export async function mintReactorToken(apiKey) {return { jwt: payload.jwt, expiresAt: payload.expires_at || null };}+const preparations = createMediaJobPreparation();+export const cancel = (jobId) => {if (!jobId) {throw new Error("videoGen/reactor.cancel requires a jobId — use cancelAll() to terminate every in-flight render");}+ const pending = preparations.cancel(jobId);+ if (pending) return pending;const entry = activeRequests.get(jobId);if (!entry || entry.finalizing) return false;entry.aborted = true;@@ -91,7 +96,7 @@ export const cancel = (jobId) => {};export const cancelAll = () => {- const ids = [...activeRequests.keys()];+ const ids = [...preparations.ids(), ...activeRequests.keys()];if (ids.length === 0) return false;for (const id of ids) cancel(id);return true;@@ -211,7 +216,9 @@ function captureClip(entry, input, pythonPath, job, jobId) {});}-export async function generateVideo({+export const generateVideo = (params) => preparations.run(params, generateVideoPrepared);++async function generateVideoPrepared({settings, prompt = '', negativePrompt,continueFromClipId, seconds, seed, aspect,sourceImagePath = null, jobId: providedJobId = null,@@ -272,6 +279,7 @@ async function runReactorVideo(job, jobId, {apiKey, prompt, seconds, seed, aspect, continueFromClipId, sourceImagePath, outputPath, filename, meta,}) {const entry = { aborted: false, finalizing: false, stop: null };+ entry.aborted = preparations.take(jobId);activeRequests.set(jobId, entry);// Declared outside the try so the finally can still delete a fitted frame// whose render failed after it was written.diff --git a/server/services/videoGen/reactor.test.js b/server/services/videoGen/reactor.test.jsindex 8a82702b0c..f6b6577dae 100644--- a/server/services/videoGen/reactor.test.js+++ b/server/services/videoGen/reactor.test.js@@ -405,3 +405,20 @@ describe('Reactor SDK adapter', () => {});});++it('cancels during preparation before starting a render', async () => {+ const { videoGenEvents: events } = await import('./events.js');+ const failed = vi.fn();+ const completed = vi.fn();+ events.on('failed', failed);+ events.on('completed', completed);+ const jobId = 'preamble-cancel-videoGen-reactor';+ const pending = reactor.generateVideo({ prompt: 'a quiet gate', jobId });+ expect(reactor.cancel(jobId)).toBe('pending');+ await pending;+ await vi.waitFor(() => expect(failed).toHaveBeenCalledWith(expect.objectContaining({ generationId: jobId, error: 'Canceled' })));+ expect(completed).not.toHaveBeenCalled();+ expect(mocks.spawn).not.toHaveBeenCalled();+ expect(fetch).not.toHaveBeenCalled();+ expect(reactor.cancel(jobId)).toBe(false);+});