Skip to content

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

fix(memory): drop a pending read of the move after a start or retry

M3gA-Mind6f82ab373d30
diff --git a/app/src/components/memory/MemoryImportBanner.test.tsx b/app/src/components/memory/MemoryImportBanner.test.tsx
index d8e1f2a058..bd0c88920a 100644
--- a/app/src/components/memory/MemoryImportBanner.test.tsx
+++ b/app/src/components/memory/MemoryImportBanner.test.tsx
@@ -497,6 +497,36 @@ describe('MemoryImportBanner', () => {
expect(screen.queryByTestId('memory-migration-offer')).not.toBeInTheDocument();
});
+ it('drops a read still pending when the user starts the move', async () => {
+ hoisted.mScan.mockResolvedValue({ needed: true, shared: false });
+ hoisted.mStatus.mockResolvedValue(moving(1));
+ vi.useFakeTimers({ shouldAdvanceTime: true });
+ renderWithProviders(<MemoryImportBanner engineLabel="TinyHumans" />);
+ expect(await screen.findByTestId('memory-migration-running')).toBeInTheDocument();
+
+ // A run ends; the read that follows is slow to answer.
+ hoisted.mStatus.mockResolvedValueOnce(MOVE_IDLE);
+ let answerRead: (value: unknown) => void = () => {};
+ hoisted.mStatus.mockReturnValueOnce(
+ new Promise(resolve => {
+ answerRead = resolve;
+ })
+ );
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(MIGRATION_POLL_MS + 10);
+ });
+ // Meanwhile the user starts the move again.
+ fireEvent.click(await screen.findByTestId('memory-migration-start'));
+ expect(await screen.findByTestId('memory-migration-running')).toHaveTextContent('4');
+
+ // The read answers last, with the state from before the start.
+ await act(async () => {
+ answerRead(MOVE_IDLE);
+ });
+ expect(screen.getByTestId('memory-migration-running')).toBeInTheDocument();
+ expect(screen.queryByTestId('memory-migration-offer')).not.toBeInTheDocument();
+ });
+
it('drops a poll that fails after the user started the move', async () => {
hoisted.mScan.mockResolvedValue({ needed: true, shared: false });
vi.useFakeTimers({ shouldAdvanceTime: true });
diff --git a/app/src/components/memory/MemoryImportBanner.tsx b/app/src/components/memory/MemoryImportBanner.tsx
index 65abfbad1f..99428ae8b6 100644
--- a/app/src/components/memory/MemoryImportBanner.tsx
+++ b/app/src/components/memory/MemoryImportBanner.tsx
@@ -144,14 +144,20 @@ export default function MemoryImportBanner({ engineLabel }: MemoryImportBannerPr
// the one it replaces.
const [mScanAttempt, setMScanAttempt] = useState(0);
const rescanMove = useCallback(() => setMScanAttempt(n => n + 1), []);
+ // Bumped by a start or retry: a read or poll asked before it answers for
+ // an older state, and is dropped.
+ const mStatusGen = useRef(0);
useEffect(() => {
let cancelled = false;
let retry: ReturnType<typeof setTimeout> | undefined;
+ const gen = mStatusGen.current;
// Both answers or neither: an offer without a known status could start
// a move that is already running.
Promise.all([memoryMigrationScan(), memoryMigrationStatus()])
.then(([found, current]) => {
- if (cancelled) return;
+ // A start or retry since this read began owns the status now; the
+ // run's end reads again.
+ if (cancelled || gen !== mStatusGen.current) return;
mlog('scan: needed=%s shared=%s', found?.needed, found?.shared);
setMScan(found ?? null);
setMStatus(current ?? null);
@@ -176,9 +182,6 @@ export default function MemoryImportBanner({ engineLabel }: MemoryImportBannerPr
// One status request at a time: a slow answer must not land after, and
// overwrite, a newer one.
const mPolling = useRef(false);
- // Bumped by a start or retry: a poll asked before it answers for an older
- // state, and is dropped.
- const mStatusGen = useRef(0);
const mPoll = useCallback(async () => {
if (mPolling.current) return;
mPolling.current = true;