[PR #6289] [MERGED] fix(lorebook): cascade agent lore when the message it came from is deleted #7125

Closed
opened 2026-10-23 04:16:45 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/6289
Author: @Ejmathewp
Created: 9/17/2026
Status: ✅ Merged
Merged: 9/17/2026
Merged by: @undefined

Base: staging ← Head: fix/lorebook-deleted-message-cascade


📝 Commits (10+)

  • 69f4746 fix(lorebook): cascade agent lore when the message it came from is deleted
  • fd96ed6 Merge branch 'staging' into fix/lorebook-deleted-message-cascade
  • 2dde0f6 fix(lorebook): cascade agent lore when the message it came from is deleted
  • cc93a12 fix(lorebook): address CodeRabbit review findings on the cascade
  • 2909fff fix(lorebook): bind tool writes to saved responses and preserve undo
  • 1a34a65 Merge contributor provenance review fixes without losing staging changes
  • 970f2a2 Merge lorebook provenance and package tools for integration review
  • 858d2ed fix(lorebook): retain turn snapshots before a source message exists
  • 1c18d01 Merge branch 'review/pr-6290' into review/pr-6289
  • 61ff062 fix(lorebook): release transactions before queued metadata cleanup

📊 Changes

22 files changed (+1533 additions, -49 deletions)

View changed files

📝 CHANGELOG.md (+6 -0)
📝 packages/server/src/db/file-backed-store.ts (+8 -1)
📝 packages/server/src/db/schema/lorebooks.ts (+12 -0)
📝 packages/server/src/routes/backup.routes.ts (+7 -1)
📝 packages/server/src/routes/chats.routes.ts (+16 -0)
📝 packages/server/src/routes/generate.routes.ts (+43 -4)
📝 packages/server/src/routes/generate/agent-write-approval.ts (+19 -1)
📝 packages/server/src/routes/generate/lorebook-keeper-utils.ts (+30 -0)
📝 packages/server/src/routes/generate/retry-agents-route.ts (+44 -1)
📝 packages/server/src/routes/lorebooks.routes.ts (+16 -3)
📝 packages/server/src/services/generation/tool-resolution-runtime.ts (+73 -3)
📝 packages/server/src/services/lorebook/index.ts (+40 -0)
📝 packages/server/src/services/mari-db/mari-db.service.ts (+8 -0)
📝 packages/server/src/services/storage/chats.storage.ts (+139 -29)
➕ packages/server/src/services/storage/lorebook-provenance.ts (+39 -0)
📝 packages/server/src/services/storage/lorebooks.storage.ts (+90 -4)
📝 packages/shared/src/types/lorebook.ts (+26 -0)
📝 scripts/regressions/file-backed-shutdown.regression.ts (+45 -1)
➕ scripts/regressions/lorebook-entry-message-provenance.regression.ts (+849 -0)
📝 scripts/regressions/profile-import-data-security.regression.ts (+20 -0)

...and 2 more files

📄 Description

Linked issue

Closes #6298: the final CI run exposed an admitted storage flush racing with shutdown.

Closes #6288. Original contribution by @Ejmathewp; maintainer follow-up by @SpicyMarinara with Codex.

Why this change

Deleting or regenerating a message left agent-written lore in later prompts, including facts written into older entries. Track the source messages and swipes so deleting a turn removes its new lore or restores the previous content, and discarded swipes stop contributing lore.

What changed

  • Let flushes admitted before shutdown join the existing close drain, while retaining rejection of new writes after close. The shutdown drain preserves admitted write errors even when its retry succeeds.

  • Track source messages in keeper writes, approvals, retries and tool calls. Tool writes made before the assistant is saved are bound to the saved reply without overwriting later human edits.

  • Restore the previous content and author on deletion, clear stale vectors, and retain the original snapshot across repeated writes from the same turn. Scope bulk cascades to messages actually deleted.

  • Human edits and folder clones remain manual. Legacy profile imports clear foreign message references. Existing entries without provenance are preserved; their historical sources cannot be reconstructed reliably.

  • Expose source references and the existing entries endpoint's source-message filter. There is no new client purge UI in this PR.

Merged into staging through #6295 at 594e9b631, preserving the exact #6289 commits and contributor history. Both PRs passed their required checks and completed GitHub CodeRabbit reviews before merge; the outside contribution had current owner approval. Package tools from #6290 were merged first.

Validation

Final combined commit d39ca9aad passed pnpm check and 290/290 Node regressions. All six lore suites pass. Local CodeRabbit reviewed the latest nine-file behavioral follow-up with zero findings; version and launcher format-guard checks pass.

The real-store proof covers tool writes before reply persistence, repeated keeper writes across routed batches, human corrections, approval/retry provenance, inactive swipes, wrong-chat deletion, folder clones and stale embeddings. Additional regressions reproduce and then pass metadata queue deadlocks, partial bulk failure, atomic rollback of message/lore deletion, foreign attribution in legacy imports, repeated query keys and admitted flush failures followed by a successful close retry.

Message and lore deletion commit together per 500-message chunk; metadata cleanup runs afterward. One lore scan per nonempty chunk is an intentional ceiling documented with the future indexed lookup option. Pure test-organization and speculative indexing suggestions are deferred. Required GitHub checks and the completed GitHub CodeRabbit review remain merge gates.

Contributor checklist retained from the original submission

These are the contributor's existing checkbox states, retained unchanged. They are not new maintainer attestations. Current maintainer verification is described above; the unchecked maintainer checklist remains below.

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Maintainer checklist

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

Manually verify keeper generation, approval, retry, deletion and switching swipes in a live conversation. The maintainer's new proof is automated; it does not reproduce a reporter's private history or reconstruct provenance for old entries.

Docs and release impact

Behavior is described in CHANGELOG. Storage format 7 protects the new provenance columns from older builds that discard unknown fields. The app version is unchanged. Package-tool documentation belongs to the preceding #6290 and translation PR #6297.

UI evidence (if applicable)

No client UI changed by the lorebook feature.

Post-merge verification: the fresh staging Node run at 594e9b631 passed all 290 regressions, including normal shutdown-test process exit (CI result). An earlier optional combined-PR job completed every shutdown assertion but its process timed out on exit; the other Linux PR run and final combined local run also passed. No assertion or timeout was weakened. The broader post-merge browser matrix is still running.


🔄 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-Engine/pull/6289 **Author:** [@Ejmathewp](https://github.com/Ejmathewp) **Created:** 9/17/2026 **Status:** ✅ Merged **Merged:** 9/17/2026 **Merged by:** [@undefined](undefined) **Base:** `staging` ← **Head:** `fix/lorebook-deleted-message-cascade` --- ### 📝 Commits (10+) - [`69f4746`](https://github.com/Pasta-Devs/Marinara-Engine/commit/69f47462a150c60f1b602f2dab157eacc3e766a7) fix(lorebook): cascade agent lore when the message it came from is deleted - [`fd96ed6`](https://github.com/Pasta-Devs/Marinara-Engine/commit/fd96ed69228570852b9b8950d4d4aaeb109f4558) Merge branch 'staging' into fix/lorebook-deleted-message-cascade - [`2dde0f6`](https://github.com/Pasta-Devs/Marinara-Engine/commit/2dde0f6c0e223a4f329d427e60e71b0a0ee320d8) fix(lorebook): cascade agent lore when the message it came from is deleted - [`cc93a12`](https://github.com/Pasta-Devs/Marinara-Engine/commit/cc93a12e49dd248f8ee7a851791de21e5ef0a4d0) fix(lorebook): address CodeRabbit review findings on the cascade - [`2909fff`](https://github.com/Pasta-Devs/Marinara-Engine/commit/2909fff40f5950e0b1fcc249ac4bd34b7b961e2c) fix(lorebook): bind tool writes to saved responses and preserve undo - [`1a34a65`](https://github.com/Pasta-Devs/Marinara-Engine/commit/1a34a65f141567c25267de0d75515d2e22700d14) Merge contributor provenance review fixes without losing staging changes - [`970f2a2`](https://github.com/Pasta-Devs/Marinara-Engine/commit/970f2a25bc91f1034d214269dddffc67b7d74c51) Merge lorebook provenance and package tools for integration review - [`858d2ed`](https://github.com/Pasta-Devs/Marinara-Engine/commit/858d2ede410135a3dca6e317fa758dee057f8714) fix(lorebook): retain turn snapshots before a source message exists - [`1c18d01`](https://github.com/Pasta-Devs/Marinara-Engine/commit/1c18d012159469f1cce49bf689e80623b067e12c) Merge branch 'review/pr-6290' into review/pr-6289 - [`61ff062`](https://github.com/Pasta-Devs/Marinara-Engine/commit/61ff06231f38ebff5d5e702b0910b9d09c088867) fix(lorebook): release transactions before queued metadata cleanup ### 📊 Changes **22 files changed** (+1533 additions, -49 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+6 -0) 📝 `packages/server/src/db/file-backed-store.ts` (+8 -1) 📝 `packages/server/src/db/schema/lorebooks.ts` (+12 -0) 📝 `packages/server/src/routes/backup.routes.ts` (+7 -1) 📝 `packages/server/src/routes/chats.routes.ts` (+16 -0) 📝 `packages/server/src/routes/generate.routes.ts` (+43 -4) 📝 `packages/server/src/routes/generate/agent-write-approval.ts` (+19 -1) 📝 `packages/server/src/routes/generate/lorebook-keeper-utils.ts` (+30 -0) 📝 `packages/server/src/routes/generate/retry-agents-route.ts` (+44 -1) 📝 `packages/server/src/routes/lorebooks.routes.ts` (+16 -3) 📝 `packages/server/src/services/generation/tool-resolution-runtime.ts` (+73 -3) 📝 `packages/server/src/services/lorebook/index.ts` (+40 -0) 📝 `packages/server/src/services/mari-db/mari-db.service.ts` (+8 -0) 📝 `packages/server/src/services/storage/chats.storage.ts` (+139 -29) ➕ `packages/server/src/services/storage/lorebook-provenance.ts` (+39 -0) 📝 `packages/server/src/services/storage/lorebooks.storage.ts` (+90 -4) 📝 `packages/shared/src/types/lorebook.ts` (+26 -0) 📝 `scripts/regressions/file-backed-shutdown.regression.ts` (+45 -1) ➕ `scripts/regressions/lorebook-entry-message-provenance.regression.ts` (+849 -0) 📝 `scripts/regressions/profile-import-data-security.regression.ts` (+20 -0) _...and 2 more files_ </details> ### 📄 Description ## Linked issue Closes #6298: the final CI run exposed an admitted storage flush racing with shutdown. Closes #6288. Original contribution by @Ejmathewp; maintainer follow-up by @SpicyMarinara with Codex. ## Why this change Deleting or regenerating a message left agent-written lore in later prompts, including facts written into older entries. Track the source messages and swipes so deleting a turn removes its new lore or restores the previous content, and discarded swipes stop contributing lore. ## What changed - Let flushes admitted before shutdown join the existing close drain, while retaining rejection of new writes after close. The shutdown drain preserves admitted write errors even when its retry succeeds. - Track source messages in keeper writes, approvals, retries and tool calls. Tool writes made before the assistant is saved are bound to the saved reply without overwriting later human edits. - Restore the previous content and author on deletion, clear stale vectors, and retain the original snapshot across repeated writes from the same turn. Scope bulk cascades to messages actually deleted. - Human edits and folder clones remain manual. Legacy profile imports clear foreign message references. Existing entries without provenance are preserved; their historical sources cannot be reconstructed reliably. - Expose source references and the existing entries endpoint's source-message filter. There is no new client purge UI in this PR. Merged into `staging` through #6295 at `594e9b631`, preserving the exact #6289 commits and contributor history. Both PRs passed their required checks and completed GitHub CodeRabbit reviews before merge; the outside contribution had current owner approval. Package tools from #6290 were merged first. ## Validation Final combined commit `d39ca9aad` passed `pnpm check` and **290/290 Node regressions**. All six lore suites pass. Local CodeRabbit reviewed the latest nine-file behavioral follow-up with zero findings; version and launcher format-guard checks pass. The real-store proof covers tool writes before reply persistence, repeated keeper writes across routed batches, human corrections, approval/retry provenance, inactive swipes, wrong-chat deletion, folder clones and stale embeddings. Additional regressions reproduce and then pass metadata queue deadlocks, partial bulk failure, atomic rollback of message/lore deletion, foreign attribution in legacy imports, repeated query keys and admitted flush failures followed by a successful close retry. Message and lore deletion commit together per 500-message chunk; metadata cleanup runs afterward. One lore scan per nonempty chunk is an intentional ceiling documented with the future indexed lookup option. Pure test-organization and speculative indexing suggestions are deferred. Required GitHub checks and the completed GitHub CodeRabbit review remain merge gates. ### Contributor checklist retained from the original submission These are the contributor's existing checkbox states, retained unchanged. They are not new maintainer attestations. Current maintainer verification is described above; the unchecked maintainer checklist remains below. - [x] `pnpm check` passes locally - [x] Container (Docker / Podman) built and ran without issue - [x] Ran the app, clicked through the changes manually - [x] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [x] Above manual verification completed (describe below) - [x] Read and followed `CONTRIBUTING.md` ### Maintainer checklist - [ ] `pnpm check` passes locally - [ ] Container (Docker / Podman) built and ran without issue - [ ] Ran the app, clicked through the changes manually - [ ] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [ ] Above manual verification completed (describe below) - [ ] Read and followed `CONTRIBUTING.md` ### Manual verification notes Manually verify keeper generation, approval, retry, deletion and switching swipes in a live conversation. The maintainer's new proof is automated; it does not reproduce a reporter's private history or reconstruct provenance for old entries. ## Docs and release impact Behavior is described in CHANGELOG. Storage format 7 protects the new provenance columns from older builds that discard unknown fields. The app version is unchanged. Package-tool documentation belongs to the preceding #6290 and translation PR #6297. ## UI evidence (if applicable) No client UI changed by the lorebook feature. Post-merge verification: the fresh staging Node run at `594e9b631` passed all 290 regressions, including normal shutdown-test process exit ([CI result](https://github.com/Pasta-Devs/Marinara-Engine/actions/runs/35275434262/job/105384798212)). An earlier optional combined-PR job completed every shutdown assertion but its process timed out on exit; the other Linux PR run and final combined local run also passed. No assertion or timeout was weakened. The broader post-merge browser matrix is still running. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-23 04:16:45 +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-Engine#7125
No description provided.