Skip to content

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

fix(brain): let a superseded success through when the newer load failed

M3gA-Mind2c2aeaba3950
diff --git a/app/src/pages/Brain.tsx b/app/src/pages/Brain.tsx
index 0b7a14766d..76a375f9e0 100644
--- a/app/src/pages/Brain.tsx
+++ b/app/src/pages/Brain.tsx
@@ -120,6 +120,13 @@ export default function Brain() {
// distinguishes this effect run from the next one, never two loads within
// the same run.
let generation = 0;
+ // Generation of the response currently ON SCREEN, which is NOT the same as
+ // the newest request. That difference is the whole point: `generation` says
+ // which request is newest, `renderedGeneration` says which one produced the
+ // data the user is looking at. They diverge exactly when a newer request
+ // FAILED — and that is the case where a superseded success must still be
+ // allowed through, because there is no newer data for it to clobber.
+ let renderedGeneration = 0;
const load = async () => {
const myGeneration = ++generation;
@@ -133,13 +140,22 @@ export default function Brain() {
setError(null);
try {
const resp = await memoryTreeGraphExport(mode);
- if (cancelled || myGeneration !== generation) return;
+ // Discard this success only if NEWER DATA already rendered — not merely
+ // because a newer request exists. Testing `myGeneration !== generation`
+ // here also dropped the older success when the newer request had
+ // failed, which leaves the user with an error and no graph: strictly
+ // worse than either behaviour this guard was meant to produce.
+ if (cancelled || myGeneration < renderedGeneration) {
+ console.debug('[brain] graph fetch: dropping success behind newer data');
+ return;
+ }
console.debug(
'[brain] graph fetch: exit n=%d edges=%d',
resp.nodes.length,
resp.edges.length
);
setGraph(resp);
+ renderedGeneration = myGeneration;
// 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),
diff --git a/app/src/pages/__tests__/Brain.autoRetry.test.tsx b/app/src/pages/__tests__/Brain.autoRetry.test.tsx
index 1e4c6ddb8b..b96005f063 100644
--- a/app/src/pages/__tests__/Brain.autoRetry.test.tsx
+++ b/app/src/pages/__tests__/Brain.autoRetry.test.tsx
@@ -249,6 +249,48 @@ describe('Brain graph — automatic retry', () => {
expect(screen.getByTestId('memory-graph')).toHaveTextContent('nodes:7');
});
+ it('renders an older success when the newer load FAILED, and still retries', async () => {
+ // The collision between this PR's guard and #5942's "clear the error on an
+ // accepted success". The guard's job is to stop a stale response clobbering
+ // NEWER DATA — but when the newer request failed there is no newer data,
+ // and dropping the older success leaves the user with an error and no
+ // graph, which is worse than anything either PR intends.
+ //
+ // Both halves are asserted, because they are in tension and a fix that got
+ // only one would look right:
+ // - the older success renders (the guard let it through), AND
+ // - the ladder is still armed, because the NEWEST thing we know about the
+ // backend is that it failed. Showing data and continuing to retry is
+ // the correct combination, not a leftover.
+ let resolveFirst!: (value: unknown) => void;
+ const firstCall = new Promise(resolve => {
+ resolveFirst = resolve;
+ });
+ graphExportMock
+ .mockImplementationOnce(() => firstCall)
+ .mockImplementationOnce(() => Promise.reject(new Error('newer load failed')))
+ .mockResolvedValue(makeGraph(6));
+
+ await renderBrain();
+ await act(async () => {
+ window.dispatchEvent(new Event('openhuman:memory-tree-completed'));
+ });
+ expect(graphExportMock).toHaveBeenCalledTimes(2);
+
+ // The superseded call succeeds. Nothing newer has rendered, so it must show.
+ await act(async () => {
+ resolveFirst(makeGraph(3));
+ });
+ expect(screen.getByTestId('memory-graph')).toHaveTextContent('nodes:3');
+
+ // And the retry the failure armed still fires.
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(RETRY_DELAYS_MS[0]);
+ });
+ expect(graphExportMock).toHaveBeenCalledTimes(3);
+ expect(screen.getByTestId('memory-graph')).toHaveTextContent('nodes:6');
+ });
+
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.