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: use StrictMode-safe useMounted for VideoGen/FeatureAgentDetail guards (#2542)

Adam Eivy8b17fe0e8dc2
diff --git a/client/src/pages/FeatureAgentDetail.jsx b/client/src/pages/FeatureAgentDetail.jsx
index c49400e739..42778a0570 100644
--- a/client/src/pages/FeatureAgentDetail.jsx
+++ b/client/src/pages/FeatureAgentDetail.jsx
@@ -1,4 +1,5 @@
import { useState, useEffect, useCallback, useRef } from 'react';
+import useMounted from '../hooks/useMounted';
import { useParams, useNavigate, Link } from 'react-router-dom';
import { Wand2, ArrowLeft } from 'lucide-react';
import toast from '../components/ui/Toast';
@@ -23,10 +24,9 @@ export default function FeatureAgentDetail() {
const [loading, setLoading] = useState(!isCreate);
const [apps, setApps] = useState([]);
- // Unmount guard — never reset to true so StrictMode's mount→cleanup→remount
- // can't strand it false and swallow the post-unmount stale-result check.
- const mountedRef = useRef(true);
- useEffect(() => () => { mountedRef.current = false; }, []);
+ // Unmount guard (StrictMode-safe: resets to true on mount) so an async agent
+ // load that resolves after teardown doesn't setState on a dead component.
+ const mountedRef = useMounted();
// Monotonic load token: an A→B navigation fires a fresh fetch and bumps this,
// so a slow response for A resolving after we've switched to B is ignored
// instead of overwriting the displayed agent with the wrong record.
diff --git a/client/src/pages/FeatureAgentDetail.test.jsx b/client/src/pages/FeatureAgentDetail.test.jsx
index 83994ed5c4..7eeaeca12a 100644
--- a/client/src/pages/FeatureAgentDetail.test.jsx
+++ b/client/src/pages/FeatureAgentDetail.test.jsx
@@ -1,5 +1,6 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen, waitFor, act } from '@testing-library/react';
+import { StrictMode } from 'react';
import { MemoryRouter, Routes, Route, useNavigate } from 'react-router-dom';
// Deferred-promise registry so a test can resolve A's load *after* navigating
@@ -104,4 +105,22 @@ describe('FeatureAgentDetail load race', () => {
expect(screen.queryByText('Agent Alpha')).not.toBeInTheDocument();
expect(screen.getByText(/Loading agent/)).toBeInTheDocument();
});
+
+ it('still renders the agent under StrictMode (mount→cleanup→remount must not strand the mounted guard)', async () => {
+ render(
+ <StrictMode>
+ <MemoryRouter initialEntries={['/feature-agents/agent-a/overview']}>
+ <Harness />
+ </MemoryRouter>
+ </StrictMode>,
+ );
+ // StrictMode fires setup→cleanup→setup; the last in-flight fetch is the
+ // current one. Resolve every fetch id we saw so the current seq resolves.
+ await act(async () => {
+ for (const d of Object.values(deferreds)) d.resolve({ id: 'agent-a', name: 'Agent Alpha', appId: 'app1' });
+ });
+ // If the mounted guard were stuck false (non-StrictMode-safe pattern) the
+ // result would be ignored and the spinner would hang forever.
+ await waitFor(() => expect(screen.getByText('Agent Alpha')).toBeInTheDocument());
+ });
});
diff --git a/client/src/pages/VideoGen.jsx b/client/src/pages/VideoGen.jsx
index 1d5a775c1f..e063d158e7 100644
--- a/client/src/pages/VideoGen.jsx
+++ b/client/src/pages/VideoGen.jsx
@@ -56,6 +56,7 @@ import { useMediaJobSse } from '../hooks/useMediaJobSse';
import { useMediaCompletionRefresh } from '../hooks/useMediaCompletionRefresh';
import { useMediaAnnotations } from '../hooks/useMediaAnnotations';
import usePreviewRoute from '../hooks/usePreviewRoute';
+import useMounted from '../hooks/useMounted';
import {
getVideoGenStatus, generateVideo, cancelVideoGen,
listVideoHistory, deleteVideoHistoryItem, setVideoHidden, extractLastFrame,
@@ -564,11 +565,13 @@ export default function VideoGen() {
// dispatch or after unmount) sees a moved-on token and bails without touching
// state or re-releasing the running slot.
const queueWorkerGenRef = useRef(0);
- // Unmount guard for the queue worker's deferred callbacks. Never reset to true
- // so StrictMode's mount→cleanup→remount can't strand it false.
- const mountedRef = useRef(true);
+ // Unmount guard for the queue worker's deferred callbacks (StrictMode-safe:
+ // resets to true on mount, so the mount→cleanup→remount cycle can't strand it
+ // false and freeze the queue worker). A dedicated unmount cleanup clears any
+ // pending BUSY-retry timer — this is the authoritative clear (the worker
+ // effect's own cleanup is unreliable, see the queue-worker effect below).
+ const mountedRef = useMounted();
useEffect(() => () => {
- mountedRef.current = false;
if (busyRetryTimerRef.current) {
clearTimeout(busyRetryTimerRef.current);
busyRetryTimerRef.current = null;