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
address review (claude): count stall ticks, guard out-of-order polls, carry intake fields
[._.]/ Adam Eivy747b662b4912
diff --git a/client/src/components/brain/tabs/LinksTab.jsx b/client/src/components/brain/tabs/LinksTab.jsxindex 5df64e2233..9517d33072 100644--- a/client/src/components/brain/tabs/LinksTab.jsx+++ b/client/src/components/brain/tabs/LinksTab.jsx@@ -77,24 +77,38 @@ const isCloneInFlight = (link) => link.cloneStatus === 'cloning' || link.cloneStconst 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.+// Give up polling a clone that has shown no progress for roughly 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 count; once it expires the badge says so+// (#5463 tracks giving the stranded record itself a way back).const CLONE_POLL_STALL_MS = 10 * 60 * 1000;-// The fields `cloneRepoInBackground` writes as a clone progresses. The poll+// Counted in TICKS, not wall-clock: `useAutoRefetch` pauses while the tab is+// hidden, so a wall-clock deadline would be tripped by the resume tick alone+// after the user left the tab backgrounded — abandoning a clone that may well+// have finished. Ticks only accrue while we are actually looking.+const CLONE_POLL_STALL_TICKS = Math.ceil(CLONE_POLL_STALL_MS / CLONE_POLL_INTERVAL_MS);++// The fields the background clone and its post-clone intake write. The poll// merges ONLY these onto the record it already has, so a response that was// already in flight when the user renamed a link or dragged its chip cannot-// revert that edit with a pre-edit snapshot of the whole record.-const CLONE_PROGRESS_FIELDS = ['cloneStatus', 'cloneError', 'localPath'];+// revert that edit with a pre-edit snapshot of the whole record. All five are+// server-owned; nothing in this tab edits them locally.+const CLONE_PROGRESS_FIELDS = ['cloneStatus', 'cloneError', 'localPath', 'malwareScan', 'repoStudy'];++// `malwareScan` / `repoStudy` arrive as fresh objects on every fetch, so they+// need a value comparison — otherwise every tick would look like a change and+// re-render the whole list. A false "changed" costs one render; there is no+// false "same" for equal content the server serializes consistently.+const sameFieldValue = (a, b) => a === b || JSON.stringify(a) === JSON.stringify(b);/** The clone-progress fields that actually moved, or null when none did. */function cloneProgressPatch(fresh, current) {const patch = {};for (const field of CLONE_PROGRESS_FIELDS) {- if (fresh[field] !== current[field]) patch[field] = fresh[field];+ if (!sameFieldValue(fresh[field], current[field])) patch[field] = fresh[field];}return Object.keys(patch).length > 0 ? patch : null;}@@ -142,29 +156,37 @@ export default function LinksTab({ onRefresh }) {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.+ // so it restarts the no-progress tick count; an unrelated `links` update (an+ // edit, a bucket drag) leaves both the count and the poll callback alone.const inFlightKey = inFlight.map(l => `${l.id}:${l.cloneStatus}`).join('|');const [stalledPollKey, setStalledPollKey] = useState(null);- const pollWindowStartRef = useRef(0);+ // Ticks since the in-flight set last moved, and a monotonic id so a slow+ // response can't land on top of a newer one.+ const noProgressTicksRef = useRef(0);+ const pollGenerationRef = useRef(0);useEffect(() => {- pollWindowStartRef.current = Date.now();+ noProgressTicksRef.current = 0;}, [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) {+ if (noProgressTicksRef.current >= CLONE_POLL_STALL_TICKS) {setStalledPollKey(inFlightKey);return;}+ noProgressTicksRef.current += 1;+ const generation = ++pollGenerationRef.current;// A 404 means the user deleted the bookmark mid-clone: drop it, or its id// stays in the in-flight set and is polled until the stall bound. Any other// failure is transient and leaves that link exactly as it is.const settled = await Promise.all(inFlightIds.map(id => api.getBrainLink(id, { silent: true }).then(fresh => ({ id, fresh })).catch(err => ({ id, gone: err?.status === 404 }))));+ // A response slower than the interval would otherwise patch a finished+ // clone back to `cloning`, restarting the poll and the stall count.+ if (generation !== pollGenerationRef.current) return;const byId = new Map(settled.map(r => [r.id, r]));setLinks(prev => {let changed = false;diff --git a/client/src/components/brain/tabs/LinksTab.test.jsx b/client/src/components/brain/tabs/LinksTab.test.jsxindex 77476e44ce..87bbad6a74 100644--- a/client/src/components/brain/tabs/LinksTab.test.jsx+++ b/client/src/components/brain/tabs/LinksTab.test.jsx@@ -100,7 +100,7 @@ describe('LinksTab clone-status polling', () => {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' }));+ getBrainLink.mockResolvedValue(link('a', 'cloned'));await renderTab();expect(screen.getByText('Cloning...')).toBeTruthy();@@ -193,19 +193,76 @@ describe('LinksTab clone-status polling', () => {expect(screen.getByText('(stalled)')).toBeTruthy();});+ // The stall bound counts ticks, not wall-clock: useAutoRefetch pauses while+ // the tab is hidden, so a wall-clock deadline would be tripped by the resume+ // tick alone and abandon a clone that may well have finished.+ it('does not count time the tab spent hidden against the stall bound', async () => {+ getBrainLinks.mockResolvedValue({ links: [link('a', 'cloning')] });+ getBrainLink.mockResolvedValue(link('a', 'cloning'));+ await renderTab();++ const hidden = vi.spyOn(document, 'visibilityState', 'get').mockReturnValue('hidden');+ await tick(20 * 60 * 1000);+ expect(getBrainLink).not.toHaveBeenCalled();++ hidden.mockReturnValue('visible');+ await act(async () => { document.dispatchEvent(new Event('visibilitychange')); });+ expect(getBrainLink).toHaveBeenCalledTimes(1);+ expect(screen.queryByText('(stalled)')).toBeNull();+ hidden.mockRestore();+ });++ it('ignores a poll response overtaken by a newer one', async () => {+ getBrainLinks.mockResolvedValue({ links: [link('a', 'cloning')] });+ // The first tick's response resolves only after the second tick's has been+ // applied — it must not patch the finished clone back to `cloning`.+ let release;+ const slow = new Promise(resolve => { release = resolve; });+ getBrainLink+ .mockImplementationOnce(async () => { await slow; return link('a', 'cloning'); })+ .mockResolvedValue(link('a', 'cloned', { localPath: '/repos/a' }));+ await renderTab();++ await tick();+ await tick();+ expect(screen.getByText('Cloned')).toBeTruthy();++ await act(async () => { release(); await Promise.resolve(); });+ expect(screen.getByText('Cloned')).toBeTruthy();+ expect(screen.queryByText('Cloning...')).toBeNull();+ });++ // The post-clone intake writes these in a second update, right after the+ // status flips — the poll has to carry them or the chips never appear.+ it('carries the post-clone intake fields through', async () => {+ getBrainLinks.mockResolvedValue({ links: [link('a', 'cloning')] });+ getBrainLink.mockResolvedValue(link('a', 'cloning', {+ malwareScan: { reportId: '11111111-1111-4111-8111-111111111111', status: 'queued' },+ }));+ await renderTab();++ await tick();+ expect(screen.getByTitle(/Malware scan queued/)).toBeTruthy();++ // …and an unchanged object on the next tick is not treated as a change.+ const renders = bucketBoardRenders;+ await tick();+ expect(bucketBoardRenders).toBe(renders);+ });+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' }));+ getBrainLink.mockResolvedValue(link('a', 'cloning'));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' }));+ getBrainLink.mockResolvedValue(link('a', 'cloning'));const beforeExtra = getBrainLink.mock.calls.length;await tick(3 * 60 * 1000);expect(getBrainLink.mock.calls.length).toBeGreaterThan(beforeExtra);