[GH-ISSUE #1443] [Issue]: Long-Term Memory: retention still keeps dead usage cleanup, 180 days of accepted receipt ids, and invalidated drafts forever #731

Closed
opened 2026-10-20 17:30:10 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @Promansis on GitHub (Oct 17, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Agents/issues/1443

Originally assigned to: @Promansis on GitHub.

Summary

#1404 (1.4.42) stopped writing per-memory usage counters and started deleting finished drafts. It left three things for a follow-up:

  • Dead cleanup code. Retention step 2 and the cleanup on permanent delete still remove usage counters, but nothing writes them any more. Vaults that haven't injected a memory since the update keep their old counters, because only an injection clears them.
  • Accepted receipt ids are kept for 180 days. usage.json gains one id per injected reply, and the whole file is read and rewritten under the vault lock on every one of those replies. The ids only stop the same prepared prompt from being recorded twice. Engine ignores the result: at 2039583ca, long-term-memory-runtime.ts:138-148 only logs a failure.
  • Invalidated drafts are never deleted. A pending draft becomes invalidated when the memory or detail it targets is deleted. It can't be applied any more; Review only offers "Discard". Drafts nobody discards stay forever and are read on every draft listing.

Affected package

long-term-memory 1.4.46 (upstream/staging 60d50d47d).

S = packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory

  • S/retention.ts:
    • Step 2 (:154-189) prunes usage.chats (:158-175) and expires acceptedReceipts after receiptRetentionDays (:176-183).
    • Step 6 (:236-260) deletes finished drafts.
    • The usageChatsPruned result field is at :135, :168 and :267.
  • S/storage.ts:1301-1313: permanent delete removes the memory from usage.chats.
  • S/usage.ts:107-145: recordLongTermMemoryInjection writes chats: {} and records the accepted id.
  • Invalidation: S/storage.ts:584 (a targeted detail removed) and :1236 (a targeted memory deleted).
  • Review: ReviewQueue.tsx:1344-1372 offers "Discard invalidated draft", which sends DELETE /drafts/:id.

Expected behavior

  • Retention doesn't run code for data that is no longer written, and it clears old counters in every vault.
  • Accepted receipt ids are kept only as long as a repeat of the same dispatch can arrive.
  • Invalidated drafts follow the same window as finished drafts.

Actual behavior

As in the summary.

Steps to reproduce

Code reading at 60d50d47d. From the 2026-10-08 vault copy: acceptedReceipts held 810 ids (about 55 KB) after about 2.5 weeks of use, with a 180-day window.

Minimal fix

One PR, patch bump.

  1. Replace the counter pruning.
    • In step 2, replace the usage.chats loop (:158-175) with: if usage.chats has any entries, set it to {} and mark usage changed.
    • Remove usageChatsPruned.
    • Remove the permanent-delete usage cleanup (S/storage.ts:1301-1313).
    • Keep usageRetentionDays in the settings schema (shared/.../schema.ts:1360, S/default-config.ts:6) so older settings files and backups still parse. Add a comment that nothing reads it.
  2. Keep accepted receipt ids for one day.
    • In step 2, expire acceptedReceipts entries older than one day, as a named constant beside RETENTION_INTERVAL_MS, instead of receiptRetentionDays.
    • Retention runs at most once a day, so an id lives one to two days. That is far longer than a repeat of the same dispatch takes.
    • The duplicate check in usage.ts doesn't change.
    • Per-chat receipt files (step 3) keep receiptRetentionDays, because they hold each chat's last recall.
  3. Delete old invalidated drafts.
    • Add invalidated to the statuses step 6 deletes, with the same auditWindowDays cutoff (30 days by default). Invalidation sets updatedAt, so the window counts from the invalidation.
    • Update the comment at S/retention.ts:22.
    • Pending drafts are still kept.
  4. CHANGELOG under ## Unreleased:
    • invalidated drafts that nobody discards are removed after the audit window;
    • usage.json stays small;
    • leftover usage counters are cleared.

Not included: the per-chat receipt files and their 180-day window. There is one small file per chat, and it holds that chat's last recall.

Regression proof

tests/long-term-memory-storage.regression.ts, beside the #1404 retention cases:

  • Old usage counters: a vault whose usage.chats still has old counters ends with chats: {} after retention.
  • Accepted receipt ids: an id older than one day is removed, and an id from today is kept.
  • Invalidated drafts: one older than auditWindowDays is removed, a recent one is kept, and an old pending draft is kept.
  • Permanent delete: deleting a memory permanently doesn't touch usage.json.

Also run:

  • index-keys (the reserved-key check at :711);
  • runtime (the duplicate-receipt case near :1700);
  • routes and routes-drafts.

Environment

  • Marinara Engine version: 2.5.0 line (staging 2039583ca)
  • Package version: long-term-memory 1.4.46 (upstream/staging 60d50d47d)
  • Install type: source (code audit)
  • OS + version: not platform-specific
  • Browser or app shell: any

Logs, screenshots, or video

None. Follow-ups recorded in the #1404 run, confirmed by reading the code at 60d50d47d. The Engine's handling of the dispatch result was read in the shared host checkout at 2039583ca.

Additional context

Follow-up to #1404. It doesn't share files with the picker and branch recall follow-up #1442, so both can run in parallel.

Template check

Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @Promansis on GitHub (Oct 17, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Agents/issues/1443 Originally assigned to: @Promansis on GitHub. ## Summary #1404 (1.4.42) stopped writing per-memory usage counters and started deleting finished drafts. It left three things for a follow-up: - **Dead cleanup code.** Retention step 2 and the cleanup on permanent delete still remove usage counters, but nothing writes them any more. Vaults that haven't injected a memory since the update keep their old counters, because only an injection clears them. - **Accepted receipt ids are kept for 180 days.** `usage.json` gains one id per injected reply, and the whole file is read and rewritten under the vault lock on every one of those replies. The ids only stop the same prepared prompt from being recorded twice. Engine ignores the result: at 2039583ca, `long-term-memory-runtime.ts:138-148` only logs a failure. - **Invalidated drafts are never deleted.** A pending draft becomes invalidated when the memory or detail it targets is deleted. It can't be applied any more; Review only offers "Discard". Drafts nobody discards stay forever and are read on every draft listing. ## Affected package `long-term-memory` 1.4.46 (`upstream/staging` 60d50d47d). `S` = `packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory` - `S/retention.ts`: - Step 2 (`:154-189`) prunes `usage.chats` (`:158-175`) and expires `acceptedReceipts` after `receiptRetentionDays` (`:176-183`). - Step 6 (`:236-260`) deletes finished drafts. - The `usageChatsPruned` result field is at `:135`, `:168` and `:267`. - `S/storage.ts:1301-1313`: permanent delete removes the memory from `usage.chats`. - `S/usage.ts:107-145`: `recordLongTermMemoryInjection` writes `chats: {}` and records the accepted id. - Invalidation: `S/storage.ts:584` (a targeted detail removed) and `:1236` (a targeted memory deleted). - Review: `ReviewQueue.tsx:1344-1372` offers "Discard invalidated draft", which sends `DELETE /drafts/:id`. ## Expected behavior - Retention doesn't run code for data that is no longer written, and it clears old counters in every vault. - Accepted receipt ids are kept only as long as a repeat of the same dispatch can arrive. - Invalidated drafts follow the same window as finished drafts. ## Actual behavior As in the summary. ## Steps to reproduce Code reading at 60d50d47d. From the 2026-10-08 vault copy: `acceptedReceipts` held 810 ids (about 55 KB) after about 2.5 weeks of use, with a 180-day window. ## Minimal fix One PR, patch bump. 1. **Replace the counter pruning.** - In step 2, replace the `usage.chats` loop (`:158-175`) with: if `usage.chats` has any entries, set it to `{}` and mark usage changed. - Remove `usageChatsPruned`. - Remove the permanent-delete usage cleanup (`S/storage.ts:1301-1313`). - Keep `usageRetentionDays` in the settings schema (`shared/.../schema.ts:1360`, `S/default-config.ts:6`) so older settings files and backups still parse. Add a comment that nothing reads it. 2. **Keep accepted receipt ids for one day.** - In step 2, expire `acceptedReceipts` entries older than one day, as a named constant beside `RETENTION_INTERVAL_MS`, instead of `receiptRetentionDays`. - Retention runs at most once a day, so an id lives one to two days. That is far longer than a repeat of the same dispatch takes. - The duplicate check in `usage.ts` doesn't change. - Per-chat receipt files (step 3) keep `receiptRetentionDays`, because they hold each chat's last recall. 3. **Delete old invalidated drafts.** - Add `invalidated` to the statuses step 6 deletes, with the same `auditWindowDays` cutoff (30 days by default). Invalidation sets `updatedAt`, so the window counts from the invalidation. - Update the comment at `S/retention.ts:22`. - Pending drafts are still kept. 4. **CHANGELOG under `## Unreleased`:** - invalidated drafts that nobody discards are removed after the audit window; - `usage.json` stays small; - leftover usage counters are cleared. **Not included:** the per-chat receipt files and their 180-day window. There is one small file per chat, and it holds that chat's last recall. ## Regression proof `tests/long-term-memory-storage.regression.ts`, beside the #1404 retention cases: - **Old usage counters:** a vault whose `usage.chats` still has old counters ends with `chats: {}` after retention. - **Accepted receipt ids:** an id older than one day is removed, and an id from today is kept. - **Invalidated drafts:** one older than `auditWindowDays` is removed, a recent one is kept, and an old pending draft is kept. - **Permanent delete:** deleting a memory permanently doesn't touch `usage.json`. Also run: - `index-keys` (the reserved-key check at `:711`); - `runtime` (the duplicate-receipt case near `:1700`); - `routes` and `routes-drafts`. ## Environment - Marinara Engine version: 2.5.0 line (`staging` 2039583ca) - Package version: long-term-memory 1.4.46 (`upstream/staging` 60d50d47d) - Install type: source (code audit) - OS + version: not platform-specific - Browser or app shell: any ## Logs, screenshots, or video None. Follow-ups recorded in the #1404 run, confirmed by reading the code at 60d50d47d. The Engine's handling of the dispatch result was read in the shared host checkout at 2039583ca. ## Additional context Follow-up to #1404. It doesn't share files with the picker and branch recall follow-up #1442, so both can run in parallel. ## Template check Please **uncheck (untick)** the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-20 17:30:10 +07:00
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#731
No description provided.