[GH-ISSUE #837] A Roleplay reply during a manual range scan makes Memory Nag pay for the same messages twice #441

Closed
opened 2026-10-20 17:27:35 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @kolacheee on GitHub (Sep 13, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Agents/issues/837

Originally assigned to: @SpicyMarinara on GitHub.

When you run a manual "Message range" scan in Memory Nag and an ordinary Roleplay reply completes in the same chat while the scan is running, the background scan grabs a block of messages the manual scan is about to read anyway, and those messages get sent to the scan model twice. No memories are duplicated and the range still gets covered correctly, but you pay for part of the range twice on a feature whose whole point is to keep scanning cheap.

How to reproduce (try it yourself)

  1. Open a Roleplay chat with a long history (around 1000 messages) and add Memory Nag as a Tracker Agent with a fresh vault, so its scan position is still far behind the chat.
  2. Turn on Debug Agents logging so the [memory-nag] Vault scan prompt for chat ... lines are written out.
  3. Open Agents, Memory Nag, Settings. Leave "Messages per batch" at its default of 20, set the scope to "Message range", enter 900 and 1000 as the range, and start the scan. It runs six batches back to back, each one its own request to the model, so it takes a while.
  4. While it is still running, send an ordinary message in that same chat, for example Let's keep going., and let the reply finish.
  5. Read the [memory-nag] Vault scan prompt lines in the log.

What you'll see: seven scan prompts where six cover the range. One block of 20 messages inside the range (messages 920 to 939 in my run) shows up twice, once in the automatic scan's prompt and once in the next manual batch's prompt. That is 121 messages sent for a 101 message range, about 20 percent extra. If a reply lands between every batch it becomes 181 messages for the same range, about 79 percent extra.

What should happen: a range scan sends each message in the range to the model once. A reply arriving mid-scan should not buy a batch the manual scan is about to send again.

Two useful controls: if the vault's scan position is already at the newest message, or is already past the end of the range, there is no duplication at all. The problem needs the scan position to be sitting behind the start of the range.

Where it is

Agents 60c8ef0e, under packages/memory-nag/src/engine/packages/server/src/services/memory-nag/:

  • scanner.ts:260-267 in scanMemoryNagBatchUnlocked: a windowed (range) batch writes its own cursor into the shared vault checkpoint whenever that moves it forward (checkpointAdvanced ? current.checkpointMessageId : checkpointMessageId).
  • scanner.ts:293-299, scanMemoryNagIfDue: the due check at :297 passes, then it calls scanMemoryNagBatch(chatId) with no range, so it starts from the checkpoint the range batch just wrote.
  • agent-runtime.ts:142: void scanMemoryNagIfDue(context.chatId) fires on every prepared Roleplay turn.
  • scanner.ts:17-30, withScanLock: line 28 frees the queue entry as each request finishes, so no lock is held between manual batches.
  • Client side: packages/memory-nag/src/engine/packages/client/src/features/memory-nag/MemoryNagSettings.tsx:223-246 drives the manual loop, one request per batch.

What's happening

A range scan moves the one shared scan position into the middle of the selected range as it works. Each batch is a separate request, and the scan lock is released between them, so a Roleplay turn that lands in that gap sees a scan position that is behind the chat, decides a scan is due, and reads the next 20 messages starting from exactly where the manual loop stopped. The manual loop then reads that same block again on its next batch, because it walks the range from its own cursor. Duplicate memories are filtered out by text, so only the model spend is wasted. The manual scan's own coverage and progress stay correct in every case that was run.

Suggested fix

While a manual range scan is in progress for a chat, the automatic per turn scan for that chat should not consume a batch inside the range the manual loop is about to cover. Because each batch is a separate request and the scan lock is free in between, the guard has to be a session level "range scan in progress" marker for the chat, set when the client starts the scan and cleared when it finishes, stalls or is abandoned. A narrower version is to let the automatic scan run but skip any batch whose window overlaps an active manual range.

There is a deeper option: stop windowed batches from writing the shared scan position at all (scanner.ts:262-267). That removes the collision outright, but it is a design trade rather than an obvious fix, because the automatic scanner would then fall back to the very start of the chat and grind through all the skipped history at 20 messages per turn, which is the cost problem #774 asked to remove, and it is pinned by the assertions in tests/memory-nag-range.regression.ts:74,92.

Context

Introduced by merged PR #776 (the range scanning feature requested in #774). Engine PR #6137 did not touch this: it is an Engine repo change and does not touch the Agents packages/memory-nag/ tree. None of the three Agents commits since (#810, #814, #819, all Long Term Memory work) touch it either, and every cited line is unchanged at Agents staging 60c8ef0e. Note that the package here is memory-nag, which is a different package from long-term-memory despite the similar names.

Found by an AI-assisted sweep and re-verified against Agents 60c8ef0e on 2026-09-13. Please verify before fixing.

Originally created by @kolacheee on GitHub (Sep 13, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Agents/issues/837 Originally assigned to: @SpicyMarinara on GitHub. When you run a manual "Message range" scan in Memory Nag and an ordinary Roleplay reply completes in the same chat while the scan is running, the background scan grabs a block of messages the manual scan is about to read anyway, and those messages get sent to the scan model twice. No memories are duplicated and the range still gets covered correctly, but you pay for part of the range twice on a feature whose whole point is to keep scanning cheap. ## How to reproduce (try it yourself) 1. Open a Roleplay chat with a long history (around 1000 messages) and add Memory Nag as a Tracker Agent with a fresh vault, so its scan position is still far behind the chat. 2. Turn on Debug Agents logging so the `[memory-nag] Vault scan prompt for chat ...` lines are written out. 3. Open Agents, Memory Nag, Settings. Leave "Messages per batch" at its default of 20, set the scope to "Message range", enter 900 and 1000 as the range, and start the scan. It runs six batches back to back, each one its own request to the model, so it takes a while. 4. While it is still running, send an ordinary message in that same chat, for example `Let's keep going.`, and let the reply finish. 5. Read the `[memory-nag] Vault scan prompt` lines in the log. **What you'll see:** seven scan prompts where six cover the range. One block of 20 messages inside the range (messages 920 to 939 in my run) shows up twice, once in the automatic scan's prompt and once in the next manual batch's prompt. That is 121 messages sent for a 101 message range, about 20 percent extra. If a reply lands between every batch it becomes 181 messages for the same range, about 79 percent extra. **What should happen:** a range scan sends each message in the range to the model once. A reply arriving mid-scan should not buy a batch the manual scan is about to send again. Two useful controls: if the vault's scan position is already at the newest message, or is already past the end of the range, there is no duplication at all. The problem needs the scan position to be sitting behind the start of the range. ## Where it is Agents 60c8ef0e, under `packages/memory-nag/src/engine/packages/server/src/services/memory-nag/`: - `scanner.ts:260-267` in `scanMemoryNagBatchUnlocked`: a windowed (range) batch writes its own cursor into the shared vault checkpoint whenever that moves it forward (`checkpointAdvanced ? current.checkpointMessageId : checkpointMessageId`). - `scanner.ts:293-299`, `scanMemoryNagIfDue`: the due check at :297 passes, then it calls `scanMemoryNagBatch(chatId)` with no range, so it starts from the checkpoint the range batch just wrote. - `agent-runtime.ts:142`: `void scanMemoryNagIfDue(context.chatId)` fires on every prepared Roleplay turn. - `scanner.ts:17-30`, `withScanLock`: line 28 frees the queue entry as each request finishes, so no lock is held between manual batches. - Client side: `packages/memory-nag/src/engine/packages/client/src/features/memory-nag/MemoryNagSettings.tsx:223-246` drives the manual loop, one request per batch. ## What's happening A range scan moves the one shared scan position into the middle of the selected range as it works. Each batch is a separate request, and the scan lock is released between them, so a Roleplay turn that lands in that gap sees a scan position that is behind the chat, decides a scan is due, and reads the next 20 messages starting from exactly where the manual loop stopped. The manual loop then reads that same block again on its next batch, because it walks the range from its own cursor. Duplicate memories are filtered out by text, so only the model spend is wasted. The manual scan's own coverage and progress stay correct in every case that was run. ## Suggested fix While a manual range scan is in progress for a chat, the automatic per turn scan for that chat should not consume a batch inside the range the manual loop is about to cover. Because each batch is a separate request and the scan lock is free in between, the guard has to be a session level "range scan in progress" marker for the chat, set when the client starts the scan and cleared when it finishes, stalls or is abandoned. A narrower version is to let the automatic scan run but skip any batch whose window overlaps an active manual range. There is a deeper option: stop windowed batches from writing the shared scan position at all (`scanner.ts:262-267`). That removes the collision outright, but it is a design trade rather than an obvious fix, because the automatic scanner would then fall back to the very start of the chat and grind through all the skipped history at 20 messages per turn, which is the cost problem #774 asked to remove, and it is pinned by the assertions in `tests/memory-nag-range.regression.ts:74,92`. ## Context Introduced by merged PR #776 (the range scanning feature requested in #774). Engine PR #6137 did not touch this: it is an Engine repo change and does not touch the Agents `packages/memory-nag/` tree. None of the three Agents commits since (#810, #814, #819, all Long Term Memory work) touch it either, and every cited line is unchanged at Agents staging 60c8ef0e. Note that the package here is `memory-nag`, which is a different package from `long-term-memory` despite the similar names. Found by an AI-assisted sweep and re-verified against Agents 60c8ef0e on 2026-09-13. Please verify before fixing.
Author
Owner

@SpicyMarinara commented on GitHub (Sep 13, 2026):

SpicyMarinara/Codex is verifying and addressing this in the active release sweep #836. We will preserve the range-scan checkpoint behavior while preventing overlapping automatic scans from charging for the same work.

<!-- gh-comment-id:5653283263 --> @SpicyMarinara commented on GitHub (Sep 13, 2026): SpicyMarinara/Codex is verifying and addressing this in the active release sweep #836. We will preserve the range-scan checkpoint behavior while preventing overlapping automatic scans from charging for the same work.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Agents#441
No description provided.