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: poll only in-flight clone ids in the Links tab instead of the whole collection (#5442)

[._.]/ Adam Eivy6644702ad90d
diff --git a/client/src/components/brain/tabs/LinksTab.jsx b/client/src/components/brain/tabs/LinksTab.jsx
index a62d80f274..842df1051f 100644
--- a/client/src/components/brain/tabs/LinksTab.jsx
+++ b/client/src/components/brain/tabs/LinksTab.jsx
@@ -72,6 +72,18 @@ const CLONE_STATUS_STYLES = {
failed: 'text-port-error'
};
+/** A clone the server is still working on — the only reason this tab polls. */
+const isCloneInFlight = (link) => link.cloneStatus === 'cloning' || link.cloneStatus === 'pending';
+
+const CLONE_POLL_INTERVAL_MS = 3000;
+
+// Give up polling a clone that has shown no progress for this long. A server
+// restart mid-clone strands the record at `cloning` forever (only the promise
+// callbacks in `cloneRepoInBackground` reset it), and without this bound the
+// poll below becomes the permanent steady state of the Links tab. Any status
+// change restarts the window; after it expires the user re-triggers the clone.
+const CLONE_POLL_STALL_MS = 10 * 60 * 1000;
+
export default function LinksTab({ onRefresh }) {
const [inputUrl, setInputUrl] = useState('');
const [inputTitle, setInputTitle] = useState('');
@@ -107,9 +119,60 @@ export default function LinksTab({ onRefresh }) {
.catch(() => setBuckets([]));
}, [fetchLinks]);
- // Poll for clone status updates while at least one link is in flight.
- const hasInFlightClone = links.some(l => l.cloneStatus === 'cloning' || l.cloneStatus === 'pending');
- useAutoRefetch(fetchLinks, 3000, { enabled: hasInFlightClone, pollOnly: true });
+ // Poll ONLY the links whose clone is in flight, patching each fresh record
+ // into local state. Re-running `fetchLinks` here re-read and re-parsed every
+ // link record server-side (one file read per link, up to 5000) and replaced
+ // the whole array — re-rendering every bucket board, chip, and search result
+ // — every 3s just to move one status badge (#5442).
+ const inFlight = links.filter(isCloneInFlight);
+ const inFlightIds = inFlight.map(l => l.id);
+ // Identity of the in-flight SET and its statuses. A change here is progress,
+ // so it restarts the stall window; an unrelated `links` update (an edit, a
+ // bucket drag) leaves both the window and the poll callback alone.
+ const inFlightKey = inFlight.map(l => `${l.id}:${l.cloneStatus}`).join('|');
+
+ const [stalledPollKey, setStalledPollKey] = useState(null);
+ const pollWindowStartRef = useRef(0);
+ useEffect(() => {
+ pollWindowStartRef.current = Date.now();
+ }, [inFlightKey]);
+
+ // Depends on `inFlightKey` alone: the same key means the same ids, so the
+ // closure over `inFlightIds` can never go stale.
+ const pollInFlightClones = useCallback(async () => {
+ if (Date.now() - pollWindowStartRef.current > CLONE_POLL_STALL_MS) {
+ setStalledPollKey(inFlightKey);
+ return;
+ }
+ // A 404 (link deleted mid-clone) or a transient failure drops that id and
+ // leaves every other link untouched.
+ const fresh = await Promise.all(
+ inFlightIds.map(id => api.getBrainLink(id, { silent: true }).catch(() => null))
+ );
+ const byId = new Map(fresh.filter(Boolean).map(l => [l.id, l]));
+ if (byId.size === 0) return;
+ setLinks(prev => {
+ let changed = false;
+ const next = prev.map(l => {
+ const updated = byId.get(l.id);
+ // `updatedAt` is the storage layer's write clock; `cloneStatus` is a
+ // belt-and-braces second signal. Unchanged records keep their identity
+ // so the boards, chips, and search results don't re-render on a tick.
+ if (!updated || (updated.updatedAt === l.updatedAt && updated.cloneStatus === l.cloneStatus)) return l;
+ changed = true;
+ return updated;
+ });
+ return changed ? next : prev;
+ });
+ }, [inFlightKey]);
+
+ useAutoRefetch(pollInFlightClones, CLONE_POLL_INTERVAL_MS, {
+ enabled: inFlightIds.length > 0 && stalledPollKey !== inFlightKey,
+ // The initial `fetchLinks` already delivered current statuses, so the first
+ // tick belongs one interval out, not immediately on enable.
+ immediate: false,
+ pollOnly: true
+ });
// Client-side filter (type / bucket membership) then keyword search.
const matchesFilter = (link) => {
diff --git a/client/src/components/brain/tabs/LinksTab.test.jsx b/client/src/components/brain/tabs/LinksTab.test.jsx
new file mode 100644
index 0000000000..207eb897d3
--- /dev/null
+++ b/client/src/components/brain/tabs/LinksTab.test.jsx
@@ -0,0 +1,177 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { act, render, screen } from '@testing-library/react';
+import { MemoryRouter } from 'react-router';
+
+vi.mock('../../../services/api', () => ({
+ getBrainLinks: vi.fn(),
+ getBrainLink: vi.fn(),
+ getBrainBuckets: vi.fn(),
+ createBrainLink: vi.fn(),
+ updateBrainLink: vi.fn(),
+ deleteBrainLink: vi.fn(),
+ reorderBrainLinks: vi.fn(),
+ cloneBrainLink: vi.fn(),
+ pullBrainLink: vi.fn(),
+ scanBrainLink: vi.fn(),
+ openBrainLinkFolder: vi.fn(),
+ brainScanReportPath: vi.fn(() => '/report'),
+}));
+
+vi.mock('../../ui/Toast', () => ({ default: { success: vi.fn(), error: vi.fn() } }));
+
+// Counting stub: the board is the most expensive consumer of the `links`
+// array, so its render count is the direct measure of "the poll does not
+// replace the array on every tick".
+let bucketBoardRenders = 0;
+vi.mock('../links/BucketBoard', () => ({
+ default: () => {
+ bucketBoardRenders += 1;
+ return <div data-testid="bucket-board" />;
+ },
+}));
+
+import { getBrainLink, getBrainLinks, getBrainBuckets } from '../../../services/api';
+import LinksTab from './LinksTab';
+
+const link = (id, cloneStatus, overrides = {}) => ({
+ id,
+ url: `https://github.com/example/${id}`,
+ title: `repo-${id}`,
+ linkType: 'github',
+ tags: [],
+ isGitHubRepo: true,
+ cloneStatus,
+ bucketId: null,
+ createdAt: '2026-01-01T00:00:00.000Z',
+ updatedAt: '2026-01-01T00:00:00.000Z',
+ ...overrides,
+});
+
+// Mount and settle the initial `getBrainLinks` + `getBrainBuckets` round-trip.
+async function renderTab() {
+ const result = render(<MemoryRouter><LinksTab /></MemoryRouter>);
+ await act(async () => { await Promise.resolve(); await Promise.resolve(); });
+ return result;
+}
+
+const tick = (ms = 3000) => act(async () => { await vi.advanceTimersByTimeAsync(ms); });
+
+beforeEach(() => {
+ vi.clearAllMocks();
+ bucketBoardRenders = 0;
+ vi.useFakeTimers();
+ getBrainBuckets.mockResolvedValue({ buckets: [] });
+});
+
+afterEach(() => {
+ vi.useRealTimers();
+});
+
+describe('LinksTab clone-status polling', () => {
+ it('polls only the in-flight ids, never the whole collection again', async () => {
+ getBrainLinks.mockResolvedValue({ links: [link('a', 'cloning'), link('b', 'cloned')] });
+ getBrainLink.mockResolvedValue(link('a', 'cloning'));
+ await renderTab();
+
+ expect(getBrainLinks).toHaveBeenCalledTimes(1);
+ expect(getBrainLink).not.toHaveBeenCalled();
+
+ await tick();
+ expect(getBrainLink).toHaveBeenCalledTimes(1);
+ expect(getBrainLink).toHaveBeenCalledWith('a', { silent: true });
+
+ await tick();
+ expect(getBrainLink).toHaveBeenCalledTimes(2);
+ expect(getBrainLink.mock.calls.every(([id]) => id === 'a')).toBe(true);
+ // The whole-collection fetch happened once, at mount, and never again.
+ expect(getBrainLinks).toHaveBeenCalledTimes(1);
+ });
+
+ it('polls a pending clone too, and every in-flight id in one tick', async () => {
+ getBrainLinks.mockResolvedValue({
+ links: [link('a', 'cloning'), link('b', 'pending'), link('c', 'none')],
+ });
+ getBrainLink.mockImplementation(async (id) => link(id, 'cloning'));
+ await renderTab();
+
+ await tick();
+ expect(getBrainLink.mock.calls.map(([id]) => id).sort()).toEqual(['a', 'b']);
+ });
+
+ it('patches the fresh status in and stops polling once nothing is in flight', async () => {
+ getBrainLinks.mockResolvedValue({ links: [link('a', 'cloning'), link('b', 'cloned')] });
+ getBrainLink.mockResolvedValue(link('a', 'cloned', { updatedAt: '2026-01-01T00:05:00.000Z' }));
+ await renderTab();
+
+ expect(screen.getByText('Cloning...')).toBeTruthy();
+
+ await tick();
+ expect(screen.queryByText('Cloning...')).toBeNull();
+ expect(screen.getAllByText('Cloned')).toHaveLength(2);
+
+ const callsAfterCompletion = getBrainLink.mock.calls.length;
+ await tick(9000);
+ expect(getBrainLink).toHaveBeenCalledTimes(callsAfterCompletion);
+ });
+
+ it('leaves the other links intact when one in-flight id 404s', async () => {
+ getBrainLinks.mockResolvedValue({ links: [link('a', 'cloning'), link('c', 'cloning')] });
+ getBrainLink.mockImplementation(async (id) => {
+ if (id === 'a') throw new Error('Link not found');
+ return link('c', 'cloned', { updatedAt: '2026-01-01T00:05:00.000Z' });
+ });
+ await renderTab();
+
+ await tick();
+ expect(screen.getByText('Cloning...')).toBeTruthy();
+ expect(screen.getByText('Cloned')).toBeTruthy();
+ expect(screen.getByText('repo-a')).toBeTruthy();
+ expect(screen.getByText('repo-c')).toBeTruthy();
+ });
+
+ it('does not re-render the list from a replaced array when a tick brings no change', async () => {
+ getBrainLinks.mockResolvedValue({ links: [link('a', 'cloning')] });
+ getBrainLink.mockResolvedValue(link('a', 'cloning'));
+ await renderTab();
+
+ const before = bucketBoardRenders;
+ await tick(9000);
+ expect(getBrainLink).toHaveBeenCalledTimes(3);
+ expect(bucketBoardRenders).toBe(before);
+ });
+
+ // A server restart mid-clone strands the record at `cloning` forever, so an
+ // unbounded poll would become the tab's permanent steady state.
+ it('gives up on a clone stuck with no status change past the stall window', async () => {
+ getBrainLinks.mockResolvedValue({ links: [link('a', 'cloning')] });
+ getBrainLink.mockResolvedValue(link('a', 'cloning'));
+ await renderTab();
+
+ await tick(10 * 60 * 1000);
+ const stalledAt = getBrainLink.mock.calls.length;
+ expect(stalledAt).toBeGreaterThan(0);
+
+ await tick(60 * 1000);
+ expect(getBrainLink).toHaveBeenCalledTimes(stalledAt);
+ // The badge still reports the last known state — the user retriggers it.
+ expect(screen.getByText('Cloning...')).toBeTruthy();
+ });
+
+ it('restarts the stall window when a clone actually progresses', async () => {
+ getBrainLinks.mockResolvedValue({ links: [link('a', 'pending')] });
+ getBrainLink.mockResolvedValue(link('a', 'pending'));
+ await renderTab();
+
+ await tick(9 * 60 * 1000);
+ getBrainLink.mockResolvedValue(link('a', 'cloning', { updatedAt: '2026-01-01T00:09:00.000Z' }));
+ await tick();
+ expect(screen.getByText('Cloning...')).toBeTruthy();
+
+ // Without the reset the window would have expired 2 minutes in; the status
+ // change bought a fresh 10 minutes.
+ getBrainLink.mockResolvedValue(link('a', 'cloning', { updatedAt: '2026-01-01T00:09:00.000Z' }));
+ const beforeExtra = getBrainLink.mock.calls.length;
+ await tick(3 * 60 * 1000);
+ expect(getBrainLink.mock.calls.length).toBeGreaterThan(beforeExtra);
+ });
+});