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

fix([media-job-sse-transient-reconnect-resilience]): only reject on terminal EventSource close

[._.]/ Adam Eivy75619c71aa10
diff --git a/client/src/hooks/useMediaJobSse.js b/client/src/hooks/useMediaJobSse.js
index d775b0cc2b..0a7d935524 100644
--- a/client/src/hooks/useMediaJobSse.js
+++ b/client/src/hooks/useMediaJobSse.js
@@ -77,6 +77,13 @@ export function useMediaJobSse(kind) {
};
es.onerror = () => {
if (!isCurrent()) { es.close(); return; }
+ // Only a genuine terminal failure (readyState CLOSED — e.g. a non-2xx /
+ // non-event-stream response, which EventSource will NOT auto-retry) tears
+ // down the stream and rejects. A transient blip (readyState CONNECTING)
+ // is left alone so the browser's built-in auto-reconnect can recover; the
+ // server replays the job's last SSE payload on re-attach. Mirrors
+ // useSseProgress.js's CLOSED-only terminal handling.
+ if (es.readyState !== EventSource.CLOSED) return;
es.close();
const err = new Error('Lost connection to server');
onConnectionError?.(err);
diff --git a/client/src/hooks/useMediaJobSse.test.jsx b/client/src/hooks/useMediaJobSse.test.jsx
index bdf426c83f..5743c8f597 100644
--- a/client/src/hooks/useMediaJobSse.test.jsx
+++ b/client/src/hooks/useMediaJobSse.test.jsx
@@ -10,17 +10,24 @@ class MockEventSource {
this.onmessage = null;
this.onerror = null;
this.closed = false;
+ this.readyState = MockEventSource.OPEN;
MockEventSource.instances.push(this);
}
- close() { this.closed = true; }
+ close() { this.closed = true; this.readyState = MockEventSource.CLOSED; }
emit(payload) { this.onmessage?.({ data: JSON.stringify(payload) }); }
emitRaw(data) { this.onmessage?.({ data }); }
- fail() { this.onerror?.(); }
+ // A terminal failure (non-2xx / non-event-stream response): the browser sets
+ // readyState CLOSED and will NOT auto-retry. A transient blip leaves
+ // readyState CONNECTING so the browser's built-in reconnect can recover.
+ fail(readyState = MockEventSource.CLOSED) { this.readyState = readyState; this.onerror?.(); }
}
+MockEventSource.CONNECTING = 0;
+MockEventSource.OPEN = 1;
+MockEventSource.CLOSED = 2;
const last = () => MockEventSource.instances[MockEventSource.instances.length - 1];
@@ -120,6 +127,22 @@ describe('useMediaJobSse', () => {
expect(last().closed).toBe(true);
});
+ it('ignores a transient onerror (readyState CONNECTING) so the browser can auto-reconnect', async () => {
+ const { result } = renderHook(() => useMediaJobSse('video'));
+ const onConnectionError = vi.fn();
+ const p = result.current.attach('j', { onConnectionError });
+
+ // A transient blip — the browser will retry on its own; the stream must
+ // stay open and the attach Promise must stay pending.
+ last().fail(MockEventSource.CONNECTING);
+ expect(onConnectionError).not.toHaveBeenCalled();
+ expect(last().closed).toBe(false);
+
+ // The reconnect eventually delivers the replayed terminal frame.
+ last().emit({ type: 'complete', result: { filename: 'recovered.png' } });
+ await expect(p).resolves.toEqual({ filename: 'recovered.png' });
+ });
+
it('ignores frames and tears down the stream when isCurrent() is false', () => {
const onProgress = vi.fn();
const { result } = renderHook(() => useMediaJobSse('image'));