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([importer-progress-reconnect]): replay importer analyze snapshot to (re)connecting sockets
[._.]/ Adam Eivye78ca3c25d55
diff --git a/client/src/hooks/useImporterProgress.test.jsx b/client/src/hooks/useImporterProgress.test.jsxindex 862d0666be..c27bec60c6 100644--- a/client/src/hooks/useImporterProgress.test.jsx+++ b/client/src/hooks/useImporterProgress.test.jsx@@ -61,6 +61,29 @@ describe('useImporterProgress', () => {expect(result.current.stages).toBeNull();});+ it('rebuilds the checklist from a replayed snapshot (mid-analyze reconnect)', () => {+ // A socket that (re)connects mid-analyze gets the server's snapshot+ // replayed as a `start` frame followed by the stage statuses seen so far+ // (see importerEvents.getImporterProgressFrames + socket.js). The hook+ // seeds runId from the replayed `start`, so the trailing `stage` frames now+ // match and the checklist is fully restored — labels + per-stage status —+ // instead of staying stuck on "Starting…".+ const { result } = renderHook(() => useImporterProgress());+ const stages = [+ { id: 'canon', label: 'Canon' },+ { id: 'arc', label: 'Arc' },+ { id: 'issues', label: 'Issues' },+ ];+ fire({ type: 'start', runId: 'r9', stages });+ fire({ type: 'stage', runId: 'r9', id: 'canon', status: 'done' });+ fire({ type: 'stage', runId: 'r9', id: 'arc', status: 'running' });+ expect(result.current.stages).toEqual([+ { id: 'canon', label: 'Canon', status: 'done' },+ { id: 'arc', label: 'Arc', status: 'running' },+ { id: 'issues', label: 'Issues', status: 'pending' },+ ]);+ });+it('ignores malformed frames', () => {const { result } = renderHook(() => useImporterProgress());fire(null);diff --git a/server/services/importer.js b/server/services/importer.jsindex c4f8b7f148..e39ac218f3 100644--- a/server/services/importer.js+++ b/server/services/importer.js@@ -12,7 +12,7 @@import { randomUUID } from 'crypto';import { runStagedLLM } from '../lib/stageRunner.js';-import { importerEvents } from './importerEvents.js';+import { importerEvents, emitImporterProgress } from './importerEvents.js';// Re-export so consumers reach the analyze-phase progress bus through the// importer's public surface (the socket bridge imports it from the source// module directly; tests + future callers use this).@@ -699,10 +699,13 @@ export async function analyzeImport({// Live stage progress (see ANALYZE_STAGES). `runId` lets the client ignore// stragglers from a prior run. Emitting is best-effort UI sugar — never let// a listener throw abort the extraction, so swallow + log at this boundary.+ // `emitImporterProgress` records each frame into the live snapshot before+ // broadcasting so a socket that (re)connects mid-analyze can be replayed the+ // checklist (see importerEvents.js / socket.js).const runId = randomUUID();const emitProgress = (frame) => {try {- importerEvents.emit('progress', { runId, ...frame });+ emitImporterProgress({ runId, ...frame });} catch (err) {console.error(`❌ importer progress emit failed: ${err.message}`);}@@ -827,6 +830,14 @@ export async function analyzeImport({const arcPreview = buildArcPreview(arcRun.content);+ // Terminal frame: clears the live snapshot so a tab that opens after this+ // run finishes isn't replayed a stale (all-done) checklist. A throw before+ // here (an LLM timeout on canon/arc, a series-create failure) leaves the+ // snapshot in place; that's benign — the client only renders the checklist+ // while its own analyze request is in flight, and the next run's `start`+ // frame overwrites the snapshot.+ emitProgress({ type: 'done' });+return {universe,series,diff --git a/server/services/importerEvents.js b/server/services/importerEvents.jsindex 5bf2af90c7..40a8aa6e38 100644--- a/server/services/importerEvents.js+++ b/server/services/importerEvents.js@@ -10,3 +10,59 @@ import { EventEmitter } from 'events';// Single-user trust model: at most one analyze runs at a time, but each frame// carries a `runId` so the client can ignore stragglers from a prior run.export const importerEvents = new EventEmitter();++// Live snapshot of the in-flight run so a client that (re)connects mid-analyze+// can rebuild the checklist. The original design gated every `stage` frame on+// a runId the client only learned from the `start` frame — so a socket that+// reconnected after `start` (or mounted late) dropped every subsequent frame+// and the checklist stayed stuck on "Starting…" until the blocking analyze+// resolved. `socket.js` replays these frames to each newly-connected socket so+// a late joiner is brought fully up to date, labels and per-stage statuses+// included (a client-only lazy-seed couldn't recover either — `stage` frames+// carry no label and pre-reconnect completions would be lost).+let currentRun = null; // { runId, stages: [{id,label}], statusById: Map<id,status> }++// Record a frame into the live snapshot. Called by `recordAndEmit` below for+// every frame the orchestrator emits. A terminal frame clears the snapshot so+// a finished run isn't replayed to tabs that open later.+export function recordImporterFrame(frame) {+ if (!frame || typeof frame !== 'object') return;+ if (frame.type === 'start') {+ currentRun = {+ runId: frame.runId,+ stages: Array.isArray(frame.stages) ? frame.stages : [],+ statusById: new Map(),+ };+ return;+ }+ // Stale frame from a run other than the one currently tracked — ignore so a+ // late straggler can't mutate a newer run's snapshot.+ if (!currentRun || frame.runId !== currentRun.runId) return;+ if (frame.type === 'stage') {+ currentRun.statusById.set(frame.id, frame.status);+ return;+ }+ if (frame.type === 'done') {+ currentRun = null;+ }+}++// Replayable frames that reconstruct the in-flight run for a fresh subscriber:+// the original `start` (so labels + ordering are restored) followed by one+// `stage` frame per status update already seen. Empty when no run is active.+export function getImporterProgressFrames() {+ if (!currentRun) return [];+ const frames = [{ type: 'start', runId: currentRun.runId, stages: currentRun.stages }];+ for (const [id, status] of currentRun.statusById) {+ frames.push({ type: 'stage', runId: currentRun.runId, id, status });+ }+ return frames;+}++// Single entry point the orchestrator uses: update the snapshot AND broadcast.+// Keeping both behind one call means a frame can never be emitted without also+// being recorded (which would desync late joiners from live subscribers).+export function emitImporterProgress(frame) {+ recordImporterFrame(frame);+ importerEvents.emit('progress', frame);+}diff --git a/server/services/importerEvents.test.js b/server/services/importerEvents.test.jsnew file mode 100644index 0000000000..8e0d3e17ed--- /dev/null+++ b/server/services/importerEvents.test.js@@ -0,0 +1,87 @@+import { describe, it, expect, beforeEach } from 'vitest';+import {+ recordImporterFrame,+ getImporterProgressFrames,+ emitImporterProgress,+ importerEvents,+} from './importerEvents.js';++// Reset the module-level snapshot before each test by recording a terminal+// frame for whatever run might be live (a `done` with a matching runId clears+// it). The cleanest reset that doesn't depend on internals: record a fresh+// start then a done.+beforeEach(() => {+ recordImporterFrame({ type: 'start', runId: '__reset__', stages: [] });+ recordImporterFrame({ type: 'done', runId: '__reset__' });+});++describe('importerEvents snapshot', () => {+ it('returns no frames when no run is active', () => {+ expect(getImporterProgressFrames()).toEqual([]);+ });++ it('replays the start frame (labels + ordering) for a fresh subscriber', () => {+ const stages = [{ id: 'canon', label: 'Canon' }, { id: 'arc', label: 'Arc' }];+ recordImporterFrame({ type: 'start', runId: 'r1', stages });+ expect(getImporterProgressFrames()).toEqual([+ { type: 'start', runId: 'r1', stages },+ ]);+ });++ it('replays start + one stage frame per status update seen so far', () => {+ const stages = [{ id: 'canon', label: 'Canon' }, { id: 'arc', label: 'Arc' }, { id: 'issues', label: 'Issues' }];+ recordImporterFrame({ type: 'start', runId: 'r1', stages });+ recordImporterFrame({ type: 'stage', runId: 'r1', id: 'canon', status: 'running' });+ recordImporterFrame({ type: 'stage', runId: 'r1', id: 'arc', status: 'running' });+ recordImporterFrame({ type: 'stage', runId: 'r1', id: 'canon', status: 'done' });+ expect(getImporterProgressFrames()).toEqual([+ { type: 'start', runId: 'r1', stages },+ // canon collapses to its latest status (done), arc stays running.+ { type: 'stage', runId: 'r1', id: 'canon', status: 'done' },+ { type: 'stage', runId: 'r1', id: 'arc', status: 'running' },+ ]);+ });++ it('ignores stage frames from a run other than the active one', () => {+ recordImporterFrame({ type: 'start', runId: 'r2', stages: [{ id: 'canon', label: 'Canon' }] });+ recordImporterFrame({ type: 'stage', runId: 'r1', id: 'canon', status: 'done' });+ expect(getImporterProgressFrames()).toEqual([+ { type: 'start', runId: 'r2', stages: [{ id: 'canon', label: 'Canon' }] },+ ]);+ });++ it('a new start frame replaces the prior run snapshot', () => {+ recordImporterFrame({ type: 'start', runId: 'r1', stages: [{ id: 'canon', label: 'Canon' }] });+ recordImporterFrame({ type: 'stage', runId: 'r1', id: 'canon', status: 'done' });+ recordImporterFrame({ type: 'start', runId: 'r2', stages: [{ id: 'arc', label: 'Arc' }] });+ expect(getImporterProgressFrames()).toEqual([+ { type: 'start', runId: 'r2', stages: [{ id: 'arc', label: 'Arc' }] },+ ]);+ });++ it('a terminal done frame clears the snapshot (no replay to later tabs)', () => {+ recordImporterFrame({ type: 'start', runId: 'r1', stages: [{ id: 'canon', label: 'Canon' }] });+ recordImporterFrame({ type: 'stage', runId: 'r1', id: 'canon', status: 'done' });+ recordImporterFrame({ type: 'done', runId: 'r1' });+ expect(getImporterProgressFrames()).toEqual([]);+ });++ it('ignores malformed frames', () => {+ expect(() => recordImporterFrame(null)).not.toThrow();+ expect(() => recordImporterFrame('nope')).not.toThrow();+ expect(getImporterProgressFrames()).toEqual([]);+ });++ it('emitImporterProgress both records the snapshot and broadcasts the frame', () => {+ const seen = [];+ const listener = (f) => seen.push(f);+ importerEvents.on('progress', listener);+ const startFrame = { type: 'start', runId: 'r1', stages: [{ id: 'canon', label: 'Canon' }] };+ emitImporterProgress(startFrame);+ importerEvents.off('progress', listener);+ // Broadcast happened…+ expect(seen).toEqual([startFrame]);+ // …and the snapshot was updated in the same call.+ expect(getImporterProgressFrames()).toEqual([startFrame]);+ });+});diff --git a/server/services/socket.js b/server/services/socket.jsindex 9055a33192..494dab7e0b 100644--- a/server/services/socket.js+++ b/server/services/socket.js@@ -19,7 +19,7 @@ import { instanceEvents } from './instanceEvents.js';import { reviewEvents } from './review.js';import { loopEvents } from './loops.js';import { imageGenEvents } from './imageGenEvents.js';-import { importerEvents } from './importerEvents.js';+import { importerEvents, getImporterProgressFrames } from './importerEvents.js';import { catalogEvents } from './catalogEvents.js';import { videoGenEvents } from './videoGen/events.js';import { aiStatusEvents } from './aiStatusEvents.js';@@ -93,6 +93,16 @@ export function initSocket(io) {// and the user is offered a reload.socket.emit('build:id', { buildId: getBuildId() });+ // Replay the in-flight importer analyze snapshot to this socket so a tab+ // that (re)connects mid-analyze rebuilds its stage checklist instead of+ // staying stuck on "Starting…" — the original gate dropped every `stage`+ // frame whose run the client never saw a `start` for. No-op when no+ // analyze is running (empty frame list). `setupImporterEventForwarding`+ // (below) is what keeps the snapshot fed; it's armed at server start.+ for (const frame of getImporterProgressFrames()) {+ socket.emit('importer:progress', frame);+ }+// Handle streaming app detectionsocket.on('detect:start', async (rawData) => {try {