Skip to content

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

fix(brain): ignore results from superseded graph loads\n\n`chatgpt-codex-connector` and `coderabbitai` independently flagged the same\nrace, and it is real. Two `load()` calls can be in flight at once — the initial\none and a `memory-tree-completed` event, or an automatic retry overtaken by an\nevent — and they share `graph`, `error` and the retry ladder with nothing to\ntell them apart.\n\n`cancelled` does not cover this. It distinguishes THIS effect run from the next\none; it says nothing about two loads inside the same run.\n\nThe retry ladder makes the consequence worse than it was before this PR, which\nis why the guard belongs here: an obsolete rejection did not merely set a stale\nerror, it scheduled a whole retry ladder against a graph that had already\nrefreshed successfully. The mutation below measures that as 5 calls where 2 are\ncorrect.\n\nAdds a monotonic `generation`, captured per invocation, and checks it after the\nawait on both the success and failure paths.\n\nRevert-proofed, both directions:\n - M6, failure-side guard removed -> ONLY the superseded-FAILURE test fails,\n "expected 2 calls, got 5" — the obsolete failure running a full ladder.\n - M7, success-side guard removed -> ONLY the superseded-SUCCESS test fails,\n the stale payload having rolled the newer graph back.\n7/7 green, sources restored, typecheck clean.\n\n#5942 carries the narrower half of this fix (clearing the error on an accepted\nsuccess) because it is the PR that makes a leftover error VISIBLE as a false\n"data is stale" warning. The two are complementary, not duplicates: that one\nremoves the false alarm, this one stops the obsolete work.\n

M3gA-Mind4a5f426587f3
diff --git a/app/src/pages/Brain.tsx b/app/src/pages/Brain.tsx
index 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.tsx
index 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');
+ });
});