Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(brain): keep a failure visible while an automatic retry is in flight

M3gA-Mindbc6db27ba6ec
diff --git a/app/src/pages/Brain.tsx b/app/src/pages/Brain.tsx
index 76a375f9e0..f580eeacfc 100644
--- a/app/src/pages/Brain.tsx
+++ b/app/src/pages/Brain.tsx
@@ -128,7 +128,7 @@ export default function Brain() {
// allowed through, because there is no newer data for it to clobber.
let renderedGeneration = 0;
- const load = async () => {
+ const load = async (isAutomaticRetry = false) => {
const myGeneration = ++generation;
// Any load supersedes a retry still waiting, so a manual Refresh or a
// `memory-tree-completed` event cannot race a timer into a double fetch.
@@ -136,8 +136,22 @@ export default function Brain() {
clearTimeout(retryTimer);
retryTimer = undefined;
}
- console.debug('[brain] graph fetch: entry mode=%s attempt=%d', mode, attempt);
- setError(null);
+ console.debug(
+ '[brain] graph fetch: entry mode=%s attempt=%d retry=%s',
+ mode,
+ attempt,
+ isAutomaticRetry
+ );
+ // An AUTOMATIC retry must not clear the error while it is in flight.
+ // Clearing it here is right for a load the user or the app asked for —
+ // mount, Refresh, `memory-tree-completed` — because the previous failure
+ // is no longer what is being reported. A timer-driven retry is different:
+ // nothing has changed from the user's point of view, so blanking the
+ // alert (or the stale-data warning) for the duration of the request makes
+ // the failure flicker out and back for no reason they can perceive. The
+ // success path clears it on an accepted success, which is when the error
+ // has actually stopped being true.
+ if (!isAutomaticRetry) setError(null);
try {
const resp = await memoryTreeGraphExport(mode);
// Discard this success only if NEWER DATA already rendered — not merely
@@ -191,7 +205,7 @@ export default function Brain() {
console.debug('[brain] graph fetch: scheduling retry %d in %dms', attempt + 1, delay);
attempt += 1;
retryTimer = setTimeout(() => {
- void load();
+ void load(true);
}, delay);
}
};
diff --git a/app/src/pages/__tests__/Brain.autoRetry.test.tsx b/app/src/pages/__tests__/Brain.autoRetry.test.tsx
index b96005f063..b9e16b4614 100644
--- a/app/src/pages/__tests__/Brain.autoRetry.test.tsx
+++ b/app/src/pages/__tests__/Brain.autoRetry.test.tsx
@@ -189,6 +189,17 @@ describe('Brain graph — automatic retry', () => {
window.dispatchEvent(new Event('openhuman:memory-tree-completed'));
});
expect(graphExportMock).toHaveBeenCalledTimes(3);
+ // Prove the CATCH ran before advancing timers, not merely that the call was
+ // made. The retry timer is armed inside the catch, so without this the
+ // advance below could race the rejection's microtask and the ladder
+ // assertion would be measuring an arming that had not happened yet.
+ //
+ // Asserted directly rather than through `waitFor`: this file runs on fake
+ // timers, and `waitFor` polls on real timers that never advance, so it hangs
+ // to the 30s test timeout instead of retrying. The enclosing `act` above has
+ // already flushed the rejection's microtask, which is what makes the direct
+ // read sound here.
+ expect(document.querySelector('[data-slot="alert"]')).not.toBeNull();
// The full ladder is available again: 3 more retries on top of that call.
await act(async () => {
@@ -197,6 +208,42 @@ describe('Brain graph — automatic retry', () => {
expect(graphExportMock).toHaveBeenCalledTimes(3 + RETRY_DELAYS_MS.length);
});
+ it('keeps the failure visible while an automatic retry is in flight', async () => {
+ // A timer-driven retry must not blank the alert for the duration of its
+ // request. Nothing has changed from the user's point of view, so the
+ // failure flickering out and back is noise they cannot account for. The
+ // error is cleared on an accepted SUCCESS, which is when it stops being
+ // true, rather than at the top of every retry.
+ let resolveRetry!: (value: unknown) => void;
+ const retryCall = new Promise(resolve => {
+ resolveRetry = resolve;
+ });
+ graphExportMock
+ .mockImplementationOnce(() => Promise.reject(new Error('down')))
+ .mockImplementationOnce(() => retryCall);
+
+ await renderBrain();
+ // Direct, not `waitFor` — see the note in the ladder test: `waitFor` polls
+ // on real timers and this file fakes them.
+ expect(document.querySelector('[data-slot="alert"]')).not.toBeNull();
+
+ // The automatic retry starts and stays pending.
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(RETRY_DELAYS_MS[0]);
+ });
+ expect(graphExportMock).toHaveBeenCalledTimes(2);
+
+ // Still on screen while that request is outstanding.
+ expect(document.querySelector('[data-slot="alert"]')).not.toBeNull();
+
+ // And it goes away only once the retry actually succeeds.
+ await act(async () => {
+ resolveRetry(makeGraph(2));
+ });
+ expect(document.querySelector('[data-slot="alert"]')).toBeNull();
+ expect(screen.getByTestId('memory-graph')).toHaveTextContent('nodes:2');
+ });
+
it('cancels a pending retry when the panel unmounts', async () => {
// A timer that outlives the component would fetch (and setState) against an
// unmounted tree.