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
refactor([issue-1343]): use the factory-provided broadcast, drop the duplicated per-runner helper
[._.]/ Adam Eivyc81d841eb2de
diff --git a/server/services/pipeline/autoRunner.js b/server/services/pipeline/autoRunner.jsindex 54fad16941..62cee178c4 100644--- a/server/services/pipeline/autoRunner.js+++ b/server/services/pipeline/autoRunner.js@@ -15,7 +15,7 @@* grace window in lib/sseUtils.js.*/-import { broadcastSse, createSseRunner } from '../../lib/sseUtils.js';+import { createSseRunner } from '../../lib/sseUtils.js';import { generateStage } from './textStages.js';import { getIssue, updateIssue, isStageReady } from './issues.js';import { startEpisodeVideoForIssue, ERR_NO_STORYBOARDS } from './episodeVideo.js';@@ -39,21 +39,13 @@ export function cancelAutoRun(issueId) {return runner.cancel(issueId);}-function broadcast(issueId, payload) {- const run = runner.runs.get(issueId);- if (!run) return;- broadcastSse(run, payload);-}-/*** Kick off the text-stage chain for an issue. Returns the runId immediately;* progress lands via SSE. Idempotent in the sense that re-calling while a- * run is in flight resolves to the existing runId.+ * run is in flight resolves to the existing runId (the factory's start()+ * coalesces an in-flight run on its own — no pre-check needed here).*/export async function startAutoRunTextStages(issueId, options = {}) {- if (runner.isActive(issueId)) {- return { runId: runner.runs.get(issueId).runId, alreadyRunning: true };- }// Which script stages to adapt from prose. Defaults to both (manual auto-run// from the UI); callers targeting a single format (e.g. Series Autopilot on a// comic-only series) pass `scripts: ['comicScript']` to skip the off-target@@ -63,9 +55,9 @@ export async function startAutoRunTextStages(issueId, options = {}) {? VALID_SCRIPTS.filter((s) => options.scripts.includes(s)): VALID_SCRIPTS;- return runner.start(issueId, async ({ runId, record }) => {+ return runner.start(issueId, async ({ runId, record, broadcast }) => {await updateIssue(issueId, { status: 'running' }).catch(() => null);- broadcast(issueId, { type: 'start', runId, stages: ['idea', 'prose', ...scripts] });+ broadcast({ type: 'start', runId, stages: ['idea', 'prose', ...scripts] });// Own the error frame + status flip here (and swallow) so the factory's// generic catch doesn't double-emit; without it an LLM rejection would@@ -73,7 +65,7 @@ export async function startAutoRunTextStages(issueId, options = {}) {try {// Stage 1: idea — skip if already ready/edited and not force-rerunif (!record.cancelRequested) {- await runStageIfNeeded(issueId, 'idea', options);+ await runStageIfNeeded(issueId, 'idea', options, broadcast);}// Stage 2: prose — depends on idea. SKIP when the issue arrived// script-first (an imported comic seeds stages.comicScript ready with@@ -91,9 +83,9 @@ export async function startAutoRunTextStages(issueId, options = {}) {|| isStageReady(proseIssue.stages?.teleplay);if (proseEmpty && scriptAuthored && !options.force) {scriptFirstSkip = true;- broadcast(issueId, { type: 'skip', stage: 'prose', reason: 'script already authored (imported script-first) — not back-filling prose' });+ broadcast({ type: 'skip', stage: 'prose', reason: 'script already authored (imported script-first) — not back-filling prose' });} else {- await runStageIfNeeded(issueId, 'prose', options);+ await runStageIfNeeded(issueId, 'prose', options, broadcast);}}// Stage 3: comicScript + teleplay in parallel — both derive from prose.@@ -107,11 +99,11 @@ export async function startAutoRunTextStages(issueId, options = {}) {if (scriptFirstSkip && !options.force) {const iss = await getIssue(issueId);if (!isStageReady(iss.stages?.[stageId])) {- broadcast(issueId, { type: 'skip', stage: stageId, reason: 'no prose to adapt (imported script-first)' });+ broadcast({ type: 'skip', stage: stageId, reason: 'no prose to adapt (imported script-first)' });return;}}- return runStageIfNeeded(issueId, stageId, options);+ return runStageIfNeeded(issueId, stageId, options, broadcast);}));}@@ -121,21 +113,21 @@ export async function startAutoRunTextStages(issueId, options = {}) {// fire-and-forget the kickoff and surface the cdProjectId via SSE so// the UI can switch to polling the CD project for progress.if (options.includeVideo && !record.cancelRequested) {- broadcast(issueId, { type: 'stage:start', stage: 'episodeVideo' });+ broadcast({ type: 'stage:start', stage: 'episodeVideo' });const kickoff = await startEpisodeVideoForIssue(issueId, {aspectRatio: options.aspectRatio,quality: options.quality,modelId: options.modelId,}).catch((err) => {if (err?.code === ERR_NO_STORYBOARDS) {- broadcast(issueId, { type: 'skip', stage: 'episodeVideo', reason: 'storyboards stage has no scenes — fill it in first' });+ broadcast({ type: 'skip', stage: 'episodeVideo', reason: 'storyboards stage has no scenes — fill it in first' });return null;}- broadcast(issueId, { type: 'stage:error', stage: 'episodeVideo', error: (err?.message || String(err)).slice(0, 500) });+ broadcast({ type: 'stage:error', stage: 'episodeVideo', error: (err?.message || String(err)).slice(0, 500) });return null;});if (kickoff) {- broadcast(issueId, {+ broadcast({type: 'stage:complete',stage: 'episodeVideo',cdProjectId: kickoff.cdProjectId,@@ -146,7 +138,7 @@ export async function startAutoRunTextStages(issueId, options = {}) {}await updateIssue(issueId, { status: 'needs-review' }).catch(() => null);- broadcast(issueId, {+ broadcast({type: record.cancelRequested ? 'canceled' : 'complete',runId,completedAt: new Date().toISOString(),@@ -156,7 +148,7 @@ export async function startAutoRunTextStages(issueId, options = {}) {const message = (err?.message || String(err)).slice(0, 1000);console.error(`❌ Pipeline auto-run failed — issue=${issueId.slice(0, 8)} ${message}`);await updateIssue(issueId, { status: 'needs-review' }).catch(() => null);- broadcast(issueId, { type: 'error', runId, error: message, failedAt: new Date().toISOString() });+ broadcast({ type: 'error', runId, error: message, failedAt: new Date().toISOString() });// Swallow: the error frame + status flip above are the terminal handling.// The factory's finally marks the run finished and schedules the replay// grace-window cleanup.@@ -164,15 +156,15 @@ export async function startAutoRunTextStages(issueId, options = {}) {});}-async function runStageIfNeeded(issueId, stageId, options) {+async function runStageIfNeeded(issueId, stageId, options, broadcast) {const issue = await getIssue(issueId);if (!options.force && isStageReady(issue.stages?.[stageId])) {- broadcast(issueId, { type: 'skip', stage: stageId, reason: 'already populated' });+ broadcast({ type: 'skip', stage: stageId, reason: 'already populated' });return;}- broadcast(issueId, { type: 'stage:start', stage: stageId });+ broadcast({ type: 'stage:start', stage: stageId });const { stage } = await generateStage(issueId, stageId, options);- broadcast(issueId, {+ broadcast({type: 'stage:complete',stage: stageId,status: stage.status,diff --git a/server/services/pipeline/volumeBeatsRunner.js b/server/services/pipeline/volumeBeatsRunner.jsindex b9bcf7a6df..23174f7f6a 100644--- a/server/services/pipeline/volumeBeatsRunner.js+++ b/server/services/pipeline/volumeBeatsRunner.js@@ -26,7 +26,7 @@* { type: 'error', runId, error, failedAt }*/-import { broadcastSse, createSseRunner } from '../../lib/sseUtils.js';+import { createSseRunner } from '../../lib/sseUtils.js';import { generateStage } from './textStages.js';import { listIssues, isStageReady } from './issues.js';import { getSeries } from './series.js';@@ -52,12 +52,6 @@ export function cancelVolumeBeatsRun(seasonId) {return runner.cancel(seasonId);}-function broadcast(seasonId, payload) {- const run = runner.runs.get(seasonId);- if (!run) return;- broadcastSse(run, payload);-}-/*** Kick off the volume beat-sheet chain. Returns the runId immediately;* progress lands via SSE. Idempotent when a run is in flight for this volume.@@ -73,7 +67,7 @@ export async function startVolumeBeatsRun(seriesId, seasonId, options = {}) {const mode = VOLUME_BEATS_MODES.includes(options.mode) ? options.mode : 'skip-existing';- return runner.start(seasonId, async ({ runId, record }) => {+ return runner.start(seasonId, async ({ runId, record, broadcast }) => {// Own the error frame here (and swallow) so the factory's generic catch// doesn't double-emit; without it an LLM rejection would surface as an// unhandledRejection and kill the process.@@ -83,7 +77,7 @@ export async function startVolumeBeatsRun(seriesId, seasonId, options = {}) {.filter((i) => i.seasonId === seasonId).sort(compareIssuesByPosition);- broadcast(seasonId, {+ broadcast({type: 'start',runId,seasonId,@@ -104,7 +98,7 @@ export async function startVolumeBeatsRun(seriesId, seasonId, options = {}) {if (mode === 'skip-existing' && isStageReady(issue.stages?.idea)) {skipped += 1;- broadcast(seasonId, {+ broadcast({type: 'issue:skip',issueId: issue.id,issueNumber: issue.number,@@ -116,7 +110,7 @@ export async function startVolumeBeatsRun(seriesId, seasonId, options = {}) {continue;}- broadcast(seasonId, {+ broadcast({type: 'issue:start',issueId: issue.id,issueNumber: issue.number,@@ -134,7 +128,7 @@ export async function startVolumeBeatsRun(seriesId, seasonId, options = {}) {model: options.model,});generated += 1;- broadcast(seasonId, {+ broadcast({type: 'issue:complete',issueId: issue.id,ordinal,@@ -145,7 +139,7 @@ export async function startVolumeBeatsRun(seriesId, seasonId, options = {}) {});} catch (err) {errored += 1;- broadcast(seasonId, {+ broadcast({type: 'issue:error',issueId: issue.id,ordinal,@@ -155,7 +149,7 @@ export async function startVolumeBeatsRun(seriesId, seasonId, options = {}) {}}- broadcast(seasonId, {+ broadcast({type: record.cancelRequested ? 'canceled' : 'complete',runId,generated,@@ -167,7 +161,7 @@ export async function startVolumeBeatsRun(seriesId, seasonId, options = {}) {} catch (err) {const message = (err?.message || String(err)).slice(0, 1000);console.error(`❌ Pipeline volume-beats failed — season=${seasonId.slice(0, 8)} ${message}`);- broadcast(seasonId, { type: 'error', runId, error: message, failedAt: new Date().toISOString() });+ broadcast({ type: 'error', runId, error: message, failedAt: new Date().toISOString() });// Swallow: the error frame above is the terminal handling. The factory's// finally marks the run finished and schedules the replay-window cleanup.}diff --git a/server/services/storyBuilderRunner.js b/server/services/storyBuilderRunner.jsindex b764f0fab5..4c1f6578ef 100644--- a/server/services/storyBuilderRunner.js+++ b/server/services/storyBuilderRunner.js@@ -23,7 +23,7 @@* the frame (matches the pipeline auto-run's refetch-on-terminal pattern).*/-import { broadcastSse, createSseRunner } from '../lib/sseUtils.js';+import { createSseRunner } from '../lib/sseUtils.js';import { generateStep, refineStep } from './storyBuilder.js';// Shared SSE run-lifecycle keyed by `${sessionId}::${stepId}`. This runner uses@@ -43,12 +43,6 @@ export function attachClient(sessionId, stepId, res) {return runner.attachClient(runKey(sessionId, stepId), res);}-function broadcast(key, payload) {- const run = runner.runs.get(key);- if (!run) return;- broadcastSse(run, payload);-}-/*** Kick off a step generate (op: 'generate') or refine (op: 'refine') for a* session. Returns the runId immediately; progress lands via SSE. Re-calling@@ -77,13 +71,13 @@ export function startStepRun(sessionId, stepId, { op = 'generate', ...options }// `meta`) instead — the client then refuses to bind its success handler to the// running request's frame. A finished run lingering in the replay grace window// does NOT block: the factory starts a fresh run that replaces it.- return runner.start(key, async ({ runId }) => {- broadcast(key, { type: 'start', runId, stepId, op });+ return runner.start(key, async ({ runId, broadcast }) => {+ broadcast({ type: 'start', runId, stepId, op });// Best-effort phase emitter handed to the conductor; an onProgress throw// must never break the run.const onProgress = (frame) => {try {- broadcast(key, { type: 'progress', runId, ...frame });+ broadcast({ type: 'progress', runId, ...frame });} catch (err) {console.error(`❌ story-builder progress emit failed: ${err?.message || err}`);}@@ -95,7 +89,7 @@ export function startStepRun(sessionId, stepId, { op = 'generate', ...options }const result = op === 'refine'? await refineStep(sessionId, stepId, { ...options, onProgress }): await generateStep(sessionId, stepId, { ...options, onProgress });- broadcast(key, {+ broadcast({type: 'complete',runId,stepId,@@ -112,7 +106,7 @@ export function startStepRun(sessionId, stepId, { op = 'generate', ...options }} catch (err) {const message = (err?.message || String(err)).slice(0, 1000);console.error(`❌ story-builder ${op} failed — session=${sessionId.slice(0, 8)} step=${stepId} ${message}`);- broadcast(key, { type: 'error', runId, stepId, op, error: message, failedAt: new Date().toISOString() });+ broadcast({ type: 'error', runId, stepId, op, error: message, failedAt: new Date().toISOString() });}}, { sig, meta: { op } });}