Skip to content

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

fix(brain): null-check the error, and clear it on a superseded success\n\nThree review findings on #5942, all legitimate.\n\n`coderabbitai`: an empty error message defeats the truthiness test. `load()`\nstores `err.message`, so an Error carrying no message lands as `''`, which is\nfalsy — that failure then renders NEITHER alert and is silently swallowed. That\nis the exact defect this PR exists to remove, reached through a different door.\nBoth branches now test `error !== null`, the pre-existing destructive one\nincluded, since it had the same hole.\n\n`chatgpt-codex-connector`: two `load()` calls can overlap (the initial one and a\n`memory-tree-completed` event) and share `graph`/`error` with no request\ngeneration. If the newer FAILS and the older then SUCCEEDS, the success renders\na good graph while the newer error is still set, because errors were cleared\nonly when a load STARTS. Before this PR that leftover error was invisible; with\nthe warning it would be a false "your data is stale" on data that is not — a\nregression my own change would have introduced.\n\nTaken codex's second suggested option, "clear the corresponding error on an\naccepted success", rather than a full request-generation guard: it is one line,\nit removes the user-visible false alarm, and it needs no new state. The wider\nserialisation fix belongs in the PR that restructures `load()` — #5945 carries\nit there, where the same race also schedules a spurious retry.\n\n`tinysweeper`: the alert assertions after the failed refresh ran synchronously\nafter a `waitFor` that only waits for the mock to be CALLED, not for React to\ncommit the state its rejection sets. Moved inside `waitFor`.\n\nTwo regression tests added, and both are revert-proofed:\n - N4, success no longer clears a superseded error -> ONLY the overlapping-load\n test fails, and the failure prints the false warning element itself.\n - N5, truthiness restored in both branches -> ONLY the empty-message test\n fails.\nEvery test in the file now has a killing mutation (N1-N5); the earlier three are\nrecorded in the PR body. Sources restored, 5/5 green, typecheck clean.\n

M3gA-Mind1ccef9f69d7c
diff --git a/app/src/pages/Brain.tsx b/app/src/pages/Brain.tsx
index 5c2c1d8737..8e233e6f35 100644
--- a/app/src/pages/Brain.tsx
+++ b/app/src/pages/Brain.tsx
@@ -105,6 +105,15 @@ export default function Brain() {
resp.edges.length
);
setGraph(resp);
+ // Clear the error on an ACCEPTED SUCCESS, not only when a load starts.
+ // Two `load()` calls can overlap (the initial one and a
+ // `memory-tree-completed` event, or two events in quick succession),
+ // and they share this state with no request-generation guard. If the
+ // newer call fails and the older then succeeds, `error` stays set from
+ // the newer one while a perfectly good graph renders — which before
+ // this PR was invisible, and with the warning below would be a FALSE
+ // "your data is stale" on data that is not.
+ setError(null);
} catch (err) {
if (cancelled) return;
console.error('[brain] graph fetch failed', err);
@@ -258,7 +267,14 @@ export default function Brain() {
this one means "what you see is old", the one below
means "there is nothing to see".
*/}
- {error && graph ? (
+ {/*
+ `error !== null`, not truthiness: `load()`'s catch does
+ `setError(err.message)`, and an Error carrying an empty
+ message yields `''`, which is falsy. Under a truthiness
+ test that failure suppresses BOTH alerts and is silent
+ again — the exact defect this PR exists to remove.
+ */}
+ {error !== null && graph ? (
<Alert variant="warning">
<AlertDescription>{t('brain.refreshError')}</AlertDescription>
</Alert>
@@ -271,7 +287,7 @@ export default function Brain() {
mode={mode}
emptyHint={t('brain.empty')}
/>
- ) : error ? (
+ ) : error !== null ? (
<Alert variant="destructive">
<AlertDescription>{t('brain.error')}</AlertDescription>
</Alert>
diff --git a/app/src/pages/__tests__/Brain.errorRecovery.test.tsx b/app/src/pages/__tests__/Brain.errorRecovery.test.tsx
index cc7c348c8b..88025da23f 100644
--- a/app/src/pages/__tests__/Brain.errorRecovery.test.tsx
+++ b/app/src/pages/__tests__/Brain.errorRecovery.test.tsx
@@ -21,9 +21,17 @@
* 3. A FIRST load that fails still shows the destructive "couldn't load"
* alert and no graph — the branch that must not be swallowed by (2).
*
- * Both drive the retry through the `openhuman:memory-tree-completed` window
- * event, which is the in-product refetch trigger (`Brain.tsx:113-117`) and
- * needs no DOM control.
+ * 4. A failure whose message is EMPTY still surfaces. `load()` stores
+ * `err.message`, so `new Error('')` becomes `''`; a truthiness test would
+ * swallow it silently, which is (2) again through a different door.
+ *
+ * 5. An older load succeeding after a newer one failed must NOT leave a
+ * stale-data warning on data that is not stale. The two overlap and share
+ * state; the warning is what turns that pre-existing race into a visible
+ * false alarm, so the success path clears the error.
+ *
+ * These drive the refetch through the `openhuman:memory-tree-completed` window
+ * event, which is the in-product refetch trigger and needs no DOM control.
*/
import { act, screen, waitFor } from '@testing-library/react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
@@ -192,9 +200,15 @@ describe('Brain graph — transient failure recovery', () => {
// than on `role="alert"`: BOTH the warning and the destructive variant are
// in `ASSERTIVE_VARIANTS` (`Alert.tsx:62`) and so both carry that role, and
// an assertion that cannot tell the two apart would pass on either branch.
- const alert = document.querySelector('[data-slot="alert"]');
- expect(alert).not.toBeNull();
- expect(alert).toHaveAttribute('data-variant', 'warning');
+ // Inside `waitFor`: the `toHaveBeenCalledTimes(2)` above only waits until
+ // the mock is CALLED, not until React has committed the state its rejection
+ // sets. Querying synchronously after it can read the DOM one commit early
+ // and fail intermittently.
+ await waitFor(() => {
+ const alert = document.querySelector('[data-slot="alert"]');
+ expect(alert).not.toBeNull();
+ expect(alert).toHaveAttribute('data-variant', 'warning');
+ });
expect(screen.getByTestId('memory-graph')).toHaveTextContent('nodes:3');
});
@@ -219,4 +233,59 @@ describe('Brain graph — transient failure recovery', () => {
expect(document.querySelector('[data-variant="warning"]')).toBeNull();
expect(screen.queryByTestId('memory-graph')).not.toBeInTheDocument();
});
+
+ it('still surfaces a failure whose message is empty', async () => {
+ // `load()` stores `err.message`, so `new Error('')` lands as `''`. Under a
+ // truthiness test (`error ? ...`) that failure renders NOTHING and is
+ // silently swallowed — the very defect this PR exists to remove, reachable
+ // through a different door. Hence `error !== null` in both branches.
+ graphExportMock.mockRejectedValue(new Error(''));
+
+ await act(async () => {
+ renderWithProviders(<Brain />, { initialEntries: ['/?tab=graph'] });
+ });
+
+ await waitFor(() => {
+ const alert = document.querySelector('[data-slot="alert"]');
+ expect(alert).not.toBeNull();
+ expect(alert).toHaveAttribute('data-variant', 'destructive');
+ });
+ });
+
+ it('does not warn when an older load succeeds after a newer one failed', async () => {
+ // Two `load()` calls overlap and share `graph`/`error`: the initial one and
+ // one started by `memory-tree-completed`. If the NEWER fails while the
+ // OLDER is still in flight, and the older then succeeds, the success
+ // renders a good graph — and must not leave a "your data is stale" warning
+ // attached to data that is not stale.
+ //
+ // Before this PR the leftover error was invisible, so the race was
+ // harmless; the warning is what would have made it a visible false alarm.
+ let resolveFirst!: (value: unknown) => void;
+ const firstCall = new Promise(resolve => {
+ resolveFirst = resolve;
+ });
+ graphExportMock
+ .mockImplementationOnce(() => firstCall)
+ .mockImplementationOnce(() => Promise.reject(new Error('newer load failed')));
+
+ await act(async () => {
+ renderWithProviders(<Brain />, { initialEntries: ['/?tab=graph'] });
+ });
+
+ // The newer load starts and fails while the first is still pending.
+ await act(async () => {
+ window.dispatchEvent(new Event('openhuman:memory-tree-completed'));
+ });
+
+ // Now the older, superseded call finally succeeds.
+ await act(async () => {
+ resolveFirst(makeGraph(4));
+ });
+
+ await waitFor(() => {
+ expect(screen.getByTestId('memory-graph')).toHaveTextContent('nodes:4');
+ });
+ expect(document.querySelector('[data-variant="warning"]')).toBeNull();
+ });
});