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\n\nThe rebase onto #5942 surfaced a real disagreement between the two PRs, not a\nmerge artefact. #5942's test `does not warn when an older load succeeds after a\nnewer one failed` fails against this branch, and it is right to.\n\nThe guard was asking the wrong question. `generation` says which REQUEST is\nnewest; what a success needs protecting from is newer DATA. Those are the same\nthing right up until the newer request FAILS, and then they are not: with\n`myGeneration !== generation` the older success was dropped even though nothing\nnewer had rendered, leaving the user with an error and no graph. That is worse\nthan either PR intends — #5945 wanted to protect a newer graph, and there was\nno newer graph to protect.\n\nSo the success path now tracks `renderedGeneration`, the generation of the\nresponse actually on screen, and discards a success only when NEWER DATA has\nalready rendered. The failure path is unchanged and still strict: a superseded\nrejection must never set an error or arm a retry, because a newer request has\nalready superseded whatever it would say.\n\nThe retry armed by the newer failure is deliberately left armed. The newest\nthing known about the backend is that it failed, so continuing to retry while\nshowing the older data is the correct combination, not a leftover — the new\ntest asserts both halves so a fix that got only one would not pass.\n\nRevert-proofed, and the two halves fall out independently:\n - P1, the strict `myGeneration !== generation` restored -> ONLY the two\n collision tests fail (#5942's, now on main, and the new one).\n - P2, `renderedGeneration` never advanced -> ONLY the superseded-SUCCESS test\n fails, the stale payload overwriting the newer graph.\n\n14/14 across both Brain suites, sources restored, typecheck clean.\n

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