[PR #1427] [MERGED] fix(long-term-memory): prune finished drafts and stop writing usage counters (1.4.42) #1455

Closed
opened 2026-10-21 05:11:41 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Agents/pull/1427
Author: @Promansis
Created: 10/16/2026
Status: ✅ Merged
Merged: 10/16/2026
Merged by: @Promansis

Base: staging ← Head: fix(long-term-memory)/1404-draft-retention


📝 Commits (4)

  • 4f099a8 fix(long-term-memory): prune finished drafts and stop writing usage counters (#1404)
  • 7b4ff1d chore(long-term-memory): release 1.4.42 (#1404)
  • c0801ce fix(long-term-memory): keep finished drafts with an invalid updatedAt (#1404)
  • 8561504 Merge branch 'staging' into fix(long-term-memory)/1404-draft-retention

📊 Changes

16 files changed (+157 additions, -102 deletions)

View changed files

➕ artifacts/long-term-memory-1.4.42.zip (+0 -0)
📝 catalog/catalog.json (+6 -6)
📝 catalog/notes.json (+6 -6)
📝 catalog/v2/catalog.json (+6 -6)
📝 catalog/v2/notes.json (+6 -6)
📝 catalog/v3/catalog.json (+6 -6)
📝 catalog/v3/notes.json (+6 -6)
📝 packages/long-term-memory/CHANGELOG.md (+5 -0)
📝 packages/long-term-memory/manifest.json (+3 -3)
📝 packages/long-term-memory/server.mjs (+36 -36)
📝 packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory/retention.ts (+35 -1)
📝 packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory/usage.ts (+3 -19)
📝 scripts/build-feature-packages.mjs (+1 -1)
📝 tests/long-term-memory-index-keys.regression.ts (+2 -4)
📝 tests/long-term-memory-runtime.regression.ts (+5 -2)
📝 tests/long-term-memory-storage.regression.ts (+31 -0)

📄 Description

Linked issue

Closes #1404

Why this change

Two Long-Term Memory stores grew with use and were read in full on frequent paths:

  • Finished extraction drafts were never deleted. Every draft listing parses all of them, including the pending-count check the Long-Term Memory screen makes when it opens. On one real vault, 17 finished drafts (1.2 MB) were parsed to find zero pending.
  • Every reply that injected memories rewrote usage.json with per-chat, per-memory counters under the vault lock. Nothing reads those counters except the code that expires and deletes them. On the same vault the file was 918 KB, about 92% counters.

What changed

 runLongTermMemoryRetentionUnsafe          (daily, under the vault lock)
   1. quarantine  2. usage  3. receipts  4. events  5. incomplete generations
+  6. finished drafts
+       status in {accepted, auto_applied, superseded}
+       and updatedAt older than auditWindowDays (30 by default)  -> delete, count draftsRemoved
+       pending, invalidated, malformed or no valid updatedAt     -> keep

 recordLongTermMemoryInjection             (each reply that injects memories)
   read usage.json; return null on a repeated accounting id
-  bump retrievalCount/injectionCount/tokens/timestamps for every injected memory
+  usage.chats = {}                        (drops stored counters on the next reply)
   record acceptedReceipts[accountingId]; write usage.json and the chat receipt
  • retention.ts: step 6 reads drafts/ directly. Going through the draft store would call initializeLtmStore, which runs retention again. Every draft writer holds the vault lock, which retention also holds. It reuses the existing auditWindowDays field, so the retention config schema is unchanged.
  • usage.ts: the per-memory counter update is removed. acceptedReceipts, the duplicate check and the returned receipt are unchanged, so the dispatch result the Engine gets is the same.
  • long-term-memory 1.4.42: CHANGELOG entry, version, manifest, rebuilt server.mjs, artifact and catalog entries.
  • Review keeps working when a pruned draft is referenced. subject-choice-review.ts already returns draft: null for a resolved identity decision whose draft is gone, and SubjectIdentityReview.tsx ignores that field.
  • Left for follow-ups: retention step 2 and the usage cleanup on permanent delete now have nothing to do but remain. acceptedReceipts still keeps one id per reply for receiptRetentionDays. Invalidated drafts are still kept indefinitely.

Package and security impact

  • Affected package IDs: long-term-memory
  • Engine compatibility impact: none
  • New or changed permissions/entrypoints: none
  • Restart, storage, update, or uninstall impact:
    • Storage: the daily retention run now deletes finished drafts older than 30 days, and the next injected reply clears usage.chats. Neither deletion can be undone after update. Nothing in the package or the Engine reads either, and Review and Activity show only pending and invalidated drafts.
    • Downgrade: safe. chats: {} is valid for older versions, and a missing draft file is already handled as null.
    • Rollback: deleting the drafts is permanent, but rolling back the code is safe.
    • Who is affected: users with finished drafts older than 30 days, and anyone looking at usage.json by hand.

Validation

  • node scripts/validate-catalog.mjs passes locally
  • git diff --check passes locally
  • Rebuilt every affected manifest, payload, artifact, and catalog entry
  • Installed or updated the affected package through Marinara Engine
  • Checked supported modes, restart behavior, and uninstall cleanup
  • Read and followed CONTRIBUTING.md

Manual verification notes

Run by the coding agent, not by a person.

  • Engine checkout: 2039583ca, Node v24.15.0.
  • Regressions:
    • Before: at b831ec9f8:
      • The new storage case failed: retention.draftsRemoved was undefined, expected 3.
      • The new index-keys case failed with "usage must not keep per-memory counters".
    • After: both pass. The storage case checks that old accepted, auto_applied and superseded drafts are removed. It also checks that a recent accepted draft and old pending and invalidated drafts are kept. The index-keys case checks that usage.chats is {} after the injections and that a repeated accounting id still returns null.
  • After the CodeRabbit repair, which keeps finished drafts whose updatedAt is null or invalid and adds that case to the storage regression, on HEAD c0801ce52:
    • test-ci.sh (the local Catalog validation mirror: check, typecheck, security, catalog, locale and release-notes validators) passed.
    • All Long-Term Memory regressions passed. That covers 21 Engine-backed suites, including installation and the browser lifecycle on the rebuilt 1.4.42 artifact, plus the UI and helper tests.
  • One existing runtime assertion read usage.chats[...].injectionCount. It now checks that both accepted receipt ids are recorded.
  • Not done: a manual update of a real vault to watch the daily run remove old drafts.

Documentation impact

  • No documentation changes needed

UI evidence (if applicable)

None. The Review and Activity screens show the same drafts as before.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Long-Term Memory now automatically removes accepted, auto-applied, and superseded drafts older than the 30-day audit window. Pending and invalidated drafts are retained.
  • Changes
    • Memory-injecting replies no longer keep per-memory usage counters; duplicate replies continue to be recorded only once.
  • Bug Fixes
    • Updated Long-Term Memory to version 1.4.42.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Agents/pull/1427 **Author:** [@Promansis](https://github.com/Promansis) **Created:** 10/16/2026 **Status:** ✅ Merged **Merged:** 10/16/2026 **Merged by:** [@Promansis](https://github.com/Promansis) **Base:** `staging` ← **Head:** `fix(long-term-memory)/1404-draft-retention` --- ### 📝 Commits (4) - [`4f099a8`](https://github.com/Pasta-Devs/Marinara-Agents/commit/4f099a8cc57f3886d5e8c27e1a22f03b87840b87) fix(long-term-memory): prune finished drafts and stop writing usage counters (#1404) - [`7b4ff1d`](https://github.com/Pasta-Devs/Marinara-Agents/commit/7b4ff1db7d2d7d870cbe605434dea4a1c53191ee) chore(long-term-memory): release 1.4.42 (#1404) - [`c0801ce`](https://github.com/Pasta-Devs/Marinara-Agents/commit/c0801ce520c57a854f5693b50a27856d94e0154d) fix(long-term-memory): keep finished drafts with an invalid updatedAt (#1404) - [`8561504`](https://github.com/Pasta-Devs/Marinara-Agents/commit/8561504b107dac13bb047304772af02248ad683e) Merge branch 'staging' into fix(long-term-memory)/1404-draft-retention ### 📊 Changes **16 files changed** (+157 additions, -102 deletions) <details> <summary>View changed files</summary> ➕ `artifacts/long-term-memory-1.4.42.zip` (+0 -0) 📝 `catalog/catalog.json` (+6 -6) 📝 `catalog/notes.json` (+6 -6) 📝 `catalog/v2/catalog.json` (+6 -6) 📝 `catalog/v2/notes.json` (+6 -6) 📝 `catalog/v3/catalog.json` (+6 -6) 📝 `catalog/v3/notes.json` (+6 -6) 📝 `packages/long-term-memory/CHANGELOG.md` (+5 -0) 📝 `packages/long-term-memory/manifest.json` (+3 -3) 📝 `packages/long-term-memory/server.mjs` (+36 -36) 📝 `packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory/retention.ts` (+35 -1) 📝 `packages/long-term-memory/src/engine/packages/server/src/services/long-term-memory/usage.ts` (+3 -19) 📝 `scripts/build-feature-packages.mjs` (+1 -1) 📝 `tests/long-term-memory-index-keys.regression.ts` (+2 -4) 📝 `tests/long-term-memory-runtime.regression.ts` (+5 -2) 📝 `tests/long-term-memory-storage.regression.ts` (+31 -0) </details> ### 📄 Description ## Linked issue Closes #1404 ## Why this change Two Long-Term Memory stores grew with use and were read in full on frequent paths: - Finished extraction drafts were never deleted. Every draft listing parses all of them, including the pending-count check the Long-Term Memory screen makes when it opens. On one real vault, 17 finished drafts (1.2 MB) were parsed to find zero pending. - Every reply that injected memories rewrote `usage.json` with per-chat, per-memory counters under the vault lock. Nothing reads those counters except the code that expires and deletes them. On the same vault the file was 918 KB, about 92% counters. ## What changed ```diff runLongTermMemoryRetentionUnsafe (daily, under the vault lock) 1. quarantine 2. usage 3. receipts 4. events 5. incomplete generations + 6. finished drafts + status in {accepted, auto_applied, superseded} + and updatedAt older than auditWindowDays (30 by default) -> delete, count draftsRemoved + pending, invalidated, malformed or no valid updatedAt -> keep recordLongTermMemoryInjection (each reply that injects memories) read usage.json; return null on a repeated accounting id - bump retrievalCount/injectionCount/tokens/timestamps for every injected memory + usage.chats = {} (drops stored counters on the next reply) record acceptedReceipts[accountingId]; write usage.json and the chat receipt ``` - `retention.ts`: step 6 reads `drafts/` directly. Going through the draft store would call `initializeLtmStore`, which runs retention again. Every draft writer holds the vault lock, which retention also holds. It reuses the existing `auditWindowDays` field, so the retention config schema is unchanged. - `usage.ts`: the per-memory counter update is removed. `acceptedReceipts`, the duplicate check and the returned receipt are unchanged, so the dispatch result the Engine gets is the same. - `long-term-memory` 1.4.42: CHANGELOG entry, version, manifest, rebuilt `server.mjs`, artifact and catalog entries. - Review keeps working when a pruned draft is referenced. `subject-choice-review.ts` already returns `draft: null` for a resolved identity decision whose draft is gone, and `SubjectIdentityReview.tsx` ignores that field. - Left for follow-ups: retention step 2 and the usage cleanup on permanent delete now have nothing to do but remain. `acceptedReceipts` still keeps one id per reply for `receiptRetentionDays`. Invalidated drafts are still kept indefinitely. ## Package and security impact - Affected package IDs: `long-term-memory` - Engine compatibility impact: none - New or changed permissions/entrypoints: none - Restart, storage, update, or uninstall impact: - Storage: the daily retention run now deletes finished drafts older than 30 days, and the next injected reply clears `usage.chats`. Neither deletion can be undone after update. Nothing in the package or the Engine reads either, and Review and Activity show only pending and invalidated drafts. - Downgrade: safe. `chats: {}` is valid for older versions, and a missing draft file is already handled as `null`. - Rollback: deleting the drafts is permanent, but rolling back the code is safe. - Who is affected: users with finished drafts older than 30 days, and anyone looking at `usage.json` by hand. ## Validation - [ ] `node scripts/validate-catalog.mjs` passes locally - [ ] `git diff --check` passes locally - [ ] Rebuilt every affected manifest, payload, artifact, and catalog entry - [ ] Installed or updated the affected package through Marinara Engine - [ ] Checked supported modes, restart behavior, and uninstall cleanup - [ ] Read and followed `CONTRIBUTING.md` ### Manual verification notes Run by the coding agent, not by a person. - Engine checkout: `2039583ca`, Node `v24.15.0`. - Regressions: - **Before:** at `b831ec9f8`: - The new storage case failed: `retention.draftsRemoved` was `undefined`, expected `3`. - The new index-keys case failed with "usage must not keep per-memory counters". - **After:** both pass. The storage case checks that old accepted, auto_applied and superseded drafts are removed. It also checks that a recent accepted draft and old pending and invalidated drafts are kept. The index-keys case checks that `usage.chats` is `{}` after the injections and that a repeated accounting id still returns `null`. - After the CodeRabbit repair, which keeps finished drafts whose `updatedAt` is null or invalid and adds that case to the storage regression, on HEAD `c0801ce52`: - `test-ci.sh` (the local Catalog validation mirror: check, typecheck, security, catalog, locale and release-notes validators) passed. - All Long-Term Memory regressions passed. That covers 21 Engine-backed suites, including installation and the browser lifecycle on the rebuilt 1.4.42 artifact, plus the UI and helper tests. - One existing runtime assertion read `usage.chats[...].injectionCount`. It now checks that both accepted receipt ids are recorded. - Not done: a manual update of a real vault to watch the daily run remove old drafts. ## Documentation impact - [ ] No documentation changes needed ## UI evidence (if applicable) None. The Review and Activity screens show the same drafts as before. 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Long-Term Memory now automatically removes accepted, auto-applied, and superseded drafts older than the 30-day audit window. Pending and invalidated drafts are retained. * **Changes** * Memory-injecting replies no longer keep per-memory usage counters; duplicate replies continue to be recorded only once. * **Bug Fixes** * Updated Long-Term Memory to version 1.4.42. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-21 05:11:41 +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#1455
No description provided.