Changes from fix/dev-server-hang

This commit is contained in:
gsxdsm
2026-03-02 23:03:36 -08:00
parent b2915f4de1
commit fc6c69f03d
8 changed files with 108 additions and 42 deletions

View File

@@ -32,11 +32,7 @@ export function useBoardFeatures({ currentProject }: UseBoardFeaturesProps) {
const isRestoring = useIsRestoring();
// Use React Query for features
const {
data: features = [],
isLoading: isQueryLoading,
refetch: loadFeatures,
} = useFeatures(currentProject?.path);
const { data: features = [], isLoading: isQueryLoading } = useFeatures(currentProject?.path);
// Don't report loading while IDB cache restore is in progress —
// features will appear momentarily once the restore completes.
@@ -159,7 +155,6 @@ export function useBoardFeatures({ currentProject }: UseBoardFeaturesProps) {
});
return unsubscribe;
// eslint-disable-next-line react-hooks/exhaustive-deps -- loadFeatures is a stable ref from React Query
}, [currentProject]);
// Check for interrupted features on mount

View File

@@ -146,11 +146,18 @@ export function useDevServerLogs({ worktreePath, autoSubscribe = true }: UseDevS
hasFetchedInitialLogs.current = false;
}, []);
/**
* Append content to logs, enforcing a maximum buffer size to prevent
* unbounded memory growth and progressive UI lag.
*/
const appendLogs = useCallback((content: string) => {
// Buffer for batching rapid output events into fewer setState calls.
// Content accumulates here and is flushed via requestAnimationFrame,
// ensuring at most one React re-render per animation frame (~60fps max).
const pendingOutputRef = useRef('');
const rafIdRef = useRef<number | null>(null);
const flushPendingOutput = useCallback(() => {
rafIdRef.current = null;
const content = pendingOutputRef.current;
if (!content) return;
pendingOutputRef.current = '';
setState((prev) => {
const combined = prev.logs + content;
const didTrim = combined.length > MAX_LOG_BUFFER_SIZE;
@@ -170,6 +177,33 @@ export function useDevServerLogs({ worktreePath, autoSubscribe = true }: UseDevS
});
}, []);
/**
* Append content to logs, enforcing a maximum buffer size to prevent
* unbounded memory growth and progressive UI lag.
*
* Uses requestAnimationFrame to batch rapid output events into at most
* one React state update per frame, preventing excessive re-renders.
*/
const appendLogs = useCallback(
(content: string) => {
pendingOutputRef.current += content;
if (rafIdRef.current === null) {
rafIdRef.current = requestAnimationFrame(flushPendingOutput);
}
},
[flushPendingOutput]
);
// Clean up pending RAF on unmount to prevent state updates after unmount
useEffect(() => {
return () => {
if (rafIdRef.current !== null) {
cancelAnimationFrame(rafIdRef.current);
rafIdRef.current = null;
}
};
}, []);
// Fetch initial logs when worktreePath changes
useEffect(() => {
if (worktreePath && autoSubscribe) {

View File

@@ -4,13 +4,17 @@ import { getElectronAPI } from '@/lib/electron';
import { normalizePath } from '@/lib/utils';
import { toast } from 'sonner';
import type { DevServerInfo, WorktreeInfo } from '../types';
import { useEventRecencyStore } from '@/hooks/use-event-recency';
const logger = createLogger('DevServers');
// Timeout (ms) for port detection before showing a warning to the user
const PORT_DETECTION_TIMEOUT_MS = 30_000;
// Interval (ms) for periodic state reconciliation with the backend
const STATE_RECONCILE_INTERVAL_MS = 5_000;
// Interval (ms) for periodic state reconciliation with the backend.
// 30 seconds is sufficient since WebSocket events handle real-time updates;
// reconciliation is only a fallback for missed events (PWA restart, WS gaps).
// The previous 5-second interval added unnecessary HTTP pressure.
const STATE_RECONCILE_INTERVAL_MS = 30_000;
interface UseDevServersOptions {
projectPath: string;
@@ -322,12 +326,24 @@ export function useDevServers({ projectPath }: UseDevServersOptions) {
return () => clearInterval(intervalId);
}, [clearPortDetectionTimer, startPortDetectionTimer]);
// Record global events so smart polling knows WebSocket is healthy.
// Without this, dev-server events don't suppress polling intervals,
// causing all queries (features, worktrees, running-agents) to poll
// at their default rates even though the WebSocket is actively connected.
const recordGlobalEvent = useEventRecencyStore((state) => state.recordGlobalEvent);
// Subscribe to all dev server lifecycle events for reactive state updates
useEffect(() => {
const api = getElectronAPI();
if (!api?.worktree?.onDevServerLogEvent) return;
const unsubscribe = api.worktree.onDevServerLogEvent((event) => {
// Record that WS is alive (but only for lifecycle events, not output -
// output fires too frequently and would trigger unnecessary store updates)
if (event.type !== 'dev-server:output') {
recordGlobalEvent();
}
if (event.type === 'dev-server:starting') {
const { worktreePath } = event.payload;
const key = normalizePath(worktreePath);
@@ -424,7 +440,7 @@ export function useDevServers({ projectPath }: UseDevServersOptions) {
});
return unsubscribe;
}, [clearPortDetectionTimer, startPortDetectionTimer]);
}, [clearPortDetectionTimer, startPortDetectionTimer, recordGlobalEvent]);
// Cleanup all port detection timers on unmount
useEffect(() => {