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\n\n`coderabbitai` is right, and the defect is one this PR created. `setError(null)`\nat the top of every `load()` was correct while every load was user- or\nevent-driven: the previous failure is no longer what is being reported. A\ntimer-driven retry is different — nothing has changed from the user's point of\nview, so blanking the alert (or #5942's stale-data warning) for the duration of\nthe request makes the failure flicker out and back for no perceivable reason.\nThe window where the user sees nothing is exactly the window the ladder creates.\n\nTook the proposed `load(isAutomaticRetry = false)` shape over deriving it from\n`attempt > 0`: `attempt` is reset by a success, so it answers "how deep is the\nladder", not "who asked for this load", and the two come apart precisely in the\noverlapping-load cases this PR already had to reason about. An explicit\nparameter says the thing being asked.\n\nThe error still clears on an accepted success, which is when it stops being true.\n\nAlso hardened the ladder test `tinysweeper` flagged: it advanced timers straight\nafter dispatching the refetch event, so it could have measured a retry arming\nthat had not happened yet — the timer is armed inside the catch. It now asserts\nthe alert is present first, which proves the catch ran.\n\nThat assertion is deliberately NOT wrapped in `waitFor`. This file runs on fake\ntimers and `waitFor` polls on real ones, so it never retries and hangs to the\n30s test timeout instead of failing — I hit exactly that writing this, twice.\nThe enclosing `act` has already flushed the rejection's microtask, which is what\nmakes the direct read sound.\n\nRevert-proofed:\n - P3, unconditional `setError(null)` restored -> ONLY the new visibility test\n fails.\n - M1', retry never scheduled -> the visibility test AND the collision test\n fail, among others, which is the evidence that both genuinely depend on the\n ladder firing.\n\n22/22 across all three Brain suites (Brain, Brain.errorRecovery,\nBrain.autoRetry), sources restored, typecheck clean.\n

M3gA-Mind7b7838a23a30
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.