The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
fix(brain): ignore results from superseded graph loads
M3gA-Mind35f37756056d
diff --git a/app/src/pages/Brain.tsx b/app/src/pages/Brain.tsxindex ae4eea8442..0b7a14766d 100644--- a/app/src/pages/Brain.tsx+++ b/app/src/pages/Brain.tsx@@ -110,8 +110,19 @@ export default function Brain() {// manual Refresh (a `refreshKey` change re-runs this effect) start the// ladder over rather than inheriting a spent one.let attempt = 0;+ // Monotonic request generation. Two `load()` calls can be in flight at once+ // (the initial one and a `memory-tree-completed` event, or an automatic+ // retry overtaken by an event), and they share `graph`, `error` and the+ // retry ladder. Without this, a SUPERSEDED call's late result still writes:+ // an obsolete rejection sets an error and schedules another retry even+ // though a newer call has already succeeded, and an obsolete success can+ // overwrite a newer graph. `cancelled` does not cover this — it only+ // distinguishes this effect run from the next one, never two loads within+ // the same run.+ let generation = 0;const load = async () => {+ 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.if (retryTimer !== undefined) {@@ -122,7 +133,7 @@ export default function Brain() {setError(null);try {const resp = await memoryTreeGraphExport(mode);- if (cancelled) return;+ if (cancelled || myGeneration !== generation) return;console.debug('[brain] graph fetch: exit n=%d edges=%d',resp.nodes.length,@@ -142,7 +153,13 @@ export default function Brain() {// set of retries rather than resuming where an old failure left off.attempt = 0;} catch (err) {- if (cancelled) return;+ if (cancelled || myGeneration !== generation) {+ // A newer load has taken over. Dropping this rejection is what stops+ // an obsolete failure from scheduling a retry against a graph that+ // has already refreshed successfully.+ console.debug('[brain] graph fetch: dropping superseded failure');+ return;+ }console.error('[brain] graph fetch failed', err);setError(err instanceof Error ? err.message : String(err));diff --git a/app/src/pages/__tests__/Brain.autoRetry.test.tsx b/app/src/pages/__tests__/Brain.autoRetry.test.tsxindex 610ed7bce9..1e4c6ddb8b 100644--- a/app/src/pages/__tests__/Brain.autoRetry.test.tsx+++ b/app/src/pages/__tests__/Brain.autoRetry.test.tsx@@ -215,4 +215,61 @@ describe('Brain graph — automatic retry', () => {});expect(graphExportMock).toHaveBeenCalledTimes(1);});++ it('ignores a superseded FAILURE instead of retrying from it', async () => {+ // Two loads overlap: the initial one is still in flight when a+ // `memory-tree-completed` event starts a newer one that SUCCEEDS. When the+ // older call then rejects, that obsolete failure must not set an error or+ // schedule a retry against a graph that has already refreshed.+ let rejectFirst!: (reason: unknown) => void;+ const firstCall = new Promise((_resolve, reject) => {+ rejectFirst = reject;+ });+ graphExportMock+ .mockImplementationOnce(() => firstCall)+ .mockImplementationOnce(() => Promise.resolve(makeGraph(7)));++ await renderBrain();+ await act(async () => {+ window.dispatchEvent(new Event('openhuman:memory-tree-completed'));+ });+ expect(screen.getByTestId('memory-graph')).toHaveTextContent('nodes:7');+ expect(graphExportMock).toHaveBeenCalledTimes(2);++ // The superseded call finally fails.+ await act(async () => {+ rejectFirst(new Error('older load failed'));+ });++ // No retry may come from it, however long we wait.+ await act(async () => {+ await vi.advanceTimersByTimeAsync(TOTAL_LADDER_MS * 2);+ });+ expect(graphExportMock).toHaveBeenCalledTimes(2);+ expect(screen.getByTestId('memory-graph')).toHaveTextContent('nodes:7');+ });++ it('ignores a superseded SUCCESS instead of overwriting a newer graph', async () => {+ // The other direction of the same race: a slow older call resolving after a+ // newer one must not roll the graph back to its stale payload.+ let resolveFirst!: (value: unknown) => void;+ const firstCall = new Promise(resolve => {+ resolveFirst = resolve;+ });+ graphExportMock+ .mockImplementationOnce(() => firstCall)+ .mockImplementationOnce(() => Promise.resolve(makeGraph(9)));++ await renderBrain();+ await act(async () => {+ window.dispatchEvent(new Event('openhuman:memory-tree-completed'));+ });+ expect(screen.getByTestId('memory-graph')).toHaveTextContent('nodes:9');++ await act(async () => {+ resolveFirst(makeGraph(1));+ });++ expect(screen.getByTestId('memory-graph')).toHaveTextContent('nodes:9');+ });});