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

address review (agy): import killWithEscalation in videoDownload

[._.]/ Adam Eivyd4f12465bcaa
diff --git a/server/services/videoDownload.js b/server/services/videoDownload.js
index 6bd13a27c9..528776baa0 100644
--- a/server/services/videoDownload.js
+++ b/server/services/videoDownload.js
@@ -28,6 +28,7 @@ import { ServerError } from '../lib/errorHandler.js';
import { shortId, PATHS } from '../lib/fileUtils.js';
import { findFfmpeg, generateThumbnail, probeVideoDuration } from '../lib/ffmpeg.js';
import { findYtDlp } from '../lib/ytdlp.js';
+import { killWithEscalation } from '../lib/killWithEscalation.js';
import { broadcastSse, attachSseClient as attachSse, closeJobAfterDelay } from '../lib/sseUtils.js';
import { downloadVideoToDir, cleanupProducedFiles } from './ytdlpVideoImport.js';
import { loadHistory, mutateVideoHistory } from './videoGen/history.js';
@@ -64,6 +65,12 @@ const downloadJobs = new Map();
export const attachDownloadSseClient = (jobId, res) => attachSse(downloadJobs, jobId, res);
+// The job map is module-private, which left cancelVideoDownload untestable — and
+// an untested cancel path is exactly how a refactor shipped it referencing an
+// unimported killWithEscalation while the whole suite stayed green. Exposing the
+// map lets a test register a fake job and actually run the cancel.
+export const __testing = { downloadJobs };
+
/** Cancel an in-flight download. Returns false if the job is unknown or already finished. */
export function cancelVideoDownload(jobId) {
const job = downloadJobs.get(jobId);
diff --git a/server/services/videoDownload.test.js b/server/services/videoDownload.test.js
index 198289696c..ba48209642 100644
--- a/server/services/videoDownload.test.js
+++ b/server/services/videoDownload.test.js
@@ -20,6 +20,8 @@ import {
listDownloads,
deleteDownload,
buildDownloadHistoryEntry,
+ cancelVideoDownload,
+ __testing,
} from './videoDownload.js';
describe('videoDownload URL allowlist', () => {
@@ -118,3 +120,44 @@ describe('buildDownloadHistoryEntry (contract shape)', () => {
expect(entry).not.toHaveProperty('durationSec');
});
});
+
+describe('cancelVideoDownload', () => {
+ beforeEach(() => __testing.downloadJobs.clear());
+
+ it('returns false for an unknown or already-finished job', () => {
+ expect(cancelVideoDownload('nope')).toBe(false);
+ // A job past its download has no process to signal.
+ __testing.downloadJobs.set('done', { id: 'done', clients: [], process: null });
+ expect(cancelVideoDownload('done')).toBe(false);
+ });
+
+ // Actually RUNS the cancel body. Without this the path had no coverage at all,
+ // which is how a refactor shipped it calling an unimported killWithEscalation
+ // — a ReferenceError on every cancel, with the whole suite green.
+ it('signals the running child and escalates to SIGKILL if it survives the grace window', () => {
+ vi.useFakeTimers();
+ const proc = { exitCode: null, signalCode: null, kill: vi.fn() };
+ const job = { id: 'live', clients: [], process: proc };
+ __testing.downloadJobs.set('live', job);
+
+ expect(cancelVideoDownload('live')).toBe(true);
+ expect(proc.kill).toHaveBeenCalledWith('SIGTERM');
+
+ vi.advanceTimersByTime(8000);
+ expect(proc.kill).toHaveBeenCalledWith('SIGKILL');
+ vi.useRealTimers();
+ });
+
+ it('does not escalate when the child already exited', () => {
+ vi.useFakeTimers();
+ const proc = { exitCode: null, signalCode: null, kill: vi.fn() };
+ __testing.downloadJobs.set('live', { id: 'live', clients: [], process: proc });
+
+ cancelVideoDownload('live');
+ proc.exitCode = 0; // the child honored SIGTERM
+ vi.advanceTimersByTime(8000);
+ expect(proc.kill).toHaveBeenCalledTimes(1);
+ expect(proc.kill).not.toHaveBeenCalledWith('SIGKILL');
+ vi.useRealTimers();
+ });
+});
diff --git a/server/services/youtubeIngest.js b/server/services/youtubeIngest.js
index 7e8d34ce2c..414b3f2eb9 100644
--- a/server/services/youtubeIngest.js
+++ b/server/services/youtubeIngest.js
@@ -477,6 +477,10 @@ const ingestJobs = new Map();
export const attachIngestSseClient = (jobId, res) => attachSse(ingestJobs, jobId, res);
+// Exposed for the same reason as videoDownload's — so the cancel path is
+// reachable from a test instead of only from a live ingest.
+export const __testing = { ingestJobs };
+
/**
* Cancel an in-flight ingest. Returns false if the job is unknown or finished.
*
diff --git a/server/services/youtubeIngest.test.js b/server/services/youtubeIngest.test.js
index af8226bc99..934fa80772 100644
--- a/server/services/youtubeIngest.test.js
+++ b/server/services/youtubeIngest.test.js
@@ -1,4 +1,4 @@
-import { describe, it, expect, vi } from 'vitest';
+import { describe, it, expect, vi, beforeEach } from 'vitest';
// The ingest module pulls in the brain/cos/media graphs at import time for its
// spawn path; the pure helpers under test need none of it. Stub the heavy edges
@@ -18,6 +18,8 @@ import {
parseVideoMetadata,
buildIngestNote,
buildAgentTaskContext,
+ cancelYoutubeIngest,
+ __testing,
} from './youtubeIngest.js';
const META = {
@@ -179,3 +181,36 @@ describe('buildAgentTaskContext', () => {
expect(context).not.toContain('read this first');
});
});
+
+describe('cancelYoutubeIngest', () => {
+ beforeEach(() => __testing.ingestJobs.clear());
+
+ it('returns false for an unknown job', () => {
+ expect(cancelYoutubeIngest('nope')).toBe(false);
+ });
+
+ it('flags a job with no live child so the between-steps cancel is still honored', () => {
+ // An ingest is a chain of steps; cancelling between two of them (or during
+ // the non-spawn persist phase) has no process to signal, so the flag — not
+ // the kill — is what stops it.
+ const job = { id: 'j', clients: [], process: null, canceled: false };
+ __testing.ingestJobs.set('j', job);
+ expect(cancelYoutubeIngest('j')).toBe(true);
+ expect(job.canceled).toBe(true);
+ });
+
+ it('signals the running yt-dlp child and escalates if it survives the grace window', () => {
+ vi.useFakeTimers();
+ const proc = { exitCode: null, signalCode: null, kill: vi.fn() };
+ const job = { id: 'j', clients: [], process: proc, canceled: false };
+ __testing.ingestJobs.set('j', job);
+
+ expect(cancelYoutubeIngest('j')).toBe(true);
+ expect(job.canceled).toBe(true);
+ expect(proc.kill).toHaveBeenCalledWith('SIGTERM');
+
+ vi.advanceTimersByTime(8000);
+ expect(proc.kill).toHaveBeenCalledWith('SIGKILL');
+ vi.useRealTimers();
+ });
+});