mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#6842
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/5603
Author: @kolacheee
Created: 8/28/2026
Status: ✅ Merged
Merged: 8/28/2026
Merged by: @kolacheee
Base:
staging← Head:feat/5592-phase2b-eviction📝 Commits (8)
cc1350afeat(storage): LRU eviction of clean chat units behind MARINARA_MAX_RESIDENT_CHATS0e2a238fix(storage): eviction hardening from adversarial review42093b6feat(conversation): storage-free autonomous idle checksf7e1004fix(conversation): CodeRabbit round 1 — seed race, role-ordering guard, invalid-cap warning, harness-only dirty marks231dccbstyle: format checkpoint.service.ts (missed in the PR-B prettier pass)ef98279fix(storage): same-createdAt rows keep insertion order under lazy placement5cf1ba1fix(conversation): CodeRabbit round 2 — unhandled seed-cleanup rejection; createdAt in the DM store contract7ea709bfix(conversation): CodeRabbit round 3 — merge transcript seed into partial presence state; NaN-safe DM timestamps📊 Changes
27 files changed (+1027 additions, -154 deletions)
View changed files
📝
CHANGELOG.md(+1 -0)📝
docs/CONFIGURATION.md(+69 -67)📝
packages/server/src/config/env-watcher.ts(+2 -0)📝
packages/server/src/config/runtime-config.ts(+31 -0)📝
packages/server/src/db/file-backed-store.ts(+228 -7)📝
packages/server/src/routes/agents.routes.ts(+1 -1)📝
packages/server/src/routes/chats.routes.ts(+5 -1)📝
packages/server/src/routes/conversation.routes.ts(+63 -16)📝
packages/server/src/routes/game.routes.ts(+15 -9)📝
packages/server/src/routes/generate.routes.ts(+11 -3)📝
packages/server/src/routes/generate/connected-conversation-injections.ts(+2 -2)📝
packages/server/src/services/conversation/autonomous.service.ts(+38 -3)📝
packages/server/src/services/game/checkpoint.service.ts(+3 -1)📝
packages/server/src/services/generation/roleplay-dm-command-runtime.ts(+15 -3)📝
packages/server/src/services/storage/agents.storage.ts(+5 -4)📝
packages/server/src/services/storage/chats.storage.ts(+28 -14)📝
packages/server/src/services/storage/game-engine-state.storage.ts(+10 -4)📝
packages/server/src/services/storage/game-state.storage.ts(+9 -3)📝
packages/server/src/services/storage/game-storyboards.storage.ts(+19 -6)📝
packages/server/src/services/storage/mari-workspace-context.storage.ts(+4 -2)...and 7 more files
📄 Description
Why
Part of #5592 (Phase 2, PR-B — the second of the two PRs planned there, on top of #5597). PR-A made chats load lazily but kept every loaded chat resident forever, so a long session touching many chats still converges toward the eager memory footprint. This PR bounds it: past a configurable cap, the least-recently-used chat with no unsaved changes is dropped from memory (never from disk) and reloads transparently on next touch.
What
Eviction (
MARINARA_MAX_RESIDENT_CHATS, default off; the Termux launcher — which is also what the Android APK runs — defaults it to 8): an LRU sweep runs at the tail of each successful flush. That trigger point carries the safety argument: no flush is in flight (so no captured mark batch is invisible to the clean-check), and every synchronous mutate-then-markDirty stretch has completed — a sweep triggered inside unit loading could run between a write installing rows and its dirty-marking and evict unflushed data. A unit is evictable only when nothing in the live dirty/stale maps names it; the unassigned pseudo-unit and any unit involved in corruption healing (strays, duplicates, canonical replacements) are pinned; leased (fully-resident) tables are never evicted from. Eviction removes rows by array replacement (captured references escape across awaits), clears exactly the unit's own read-once mark, and leaves the complete message index, discovery index, and all persistence marks untouched — they describe the disk, not residency.Two prerequisite fixes in existing code that only eviction makes reachable (found by the pre-implementation audit):
knownShardFilesbut never in the lazy discovery index (written only at boot). Invisible while units never unloaded — but an evicted fresh chat would have reloaded permanently empty. Writes now register the shard; unlinks deregister it..bak. It now requires the file to be in the read-once set (a stale mark is created by reading) and defers otherwise — unreachable by construction given the eviction gate, but the same defense-in-depth shape the zero-row path already had.Also: the messages stray-location index is no longer consumed destructively (the read-once set already dedups), so a reload can find misfiled rows again.
~20 storage-layer writes scoped by their owner: writes that addressed lazy-table rows by bare primary key (swipe mirrors in eight chat flows, agent memory, game state/engine-state/checkpoint/storyboard updates, workspace context removal, influence consumption, note pruning, run-result edits) now carry the owning chatId/messageId conjunct the caller already had. Without this, losing a unit between a flow's read and its write degrades to a whole-table load — which is irreversible (leased tables are pinned resident), so under memory pressure the store could progressively pin its biggest tables and make the cap unreachable. With it, the same race degrades to a cheap unit reload. These are behavior-preserving today and strictly tighten each WHERE.
Config surface:
getMaxResidentChatUnits()in runtime-config (hot-reloadable, floor of 2, 0 = disabled);MARINARA_MAX_RESIDENT_CHATSadded to the launcher env allowlist and the Termux settings loop with an${VAR:-8}default placed outside the format-guard's probed byte range;MARINARA_EAGER_STORAGEadded to the env-watcher's restart-required keys; docs/CONFIGURATION.md documents both variables (EAGER was previously undocumented). One launcher-regression proximity bound ({0,30000}between the log-tee start and fd restore) is widened to 40000 — the assertion pins ordering, not distance, and the old bound had 55 bytes of headroom left for the whole script.Diagnostics: the store controller exposes
getResidentChatUnits()(residency introspection) andmarkShardDirty()(regression harness).Regressions
Five new blocks in
lazy-chat-units.regression.ts: cap enforcement with LRU order and full reload correctness (messages + swipes), the post-boot-created-chat evict/reload round trip (pins the discovery-registration fix), dirty-unit skip with next-oldest-clean eviction and eventual evictability, flushed-edit survival across eviction, and eviction-disabled-by-default. Full battery run locally and green: lazy-chat-units, message-sharding in both lazy and eager modes, file-backed-shutdown, chat-branch-lineage, memory-recall-revectorize, agent-runtime, checkpoint-retention, and both launcher guards (format-guard, env).Test plan
pnpm checkon a non-Windows checkoutpnpm regression(full node suite)MARINARA_MAX_RESIDENT_CHATS=2in.env, open several chats in sequence, return to the first — history intact, edits stick, swipes work.envoverride of the cap is honored, and long multi-chat sessions no longer grow memory unboundedlyReferences #5592 (do not auto-close — the issue tracks the remaining follow-ups).
Summary by CodeRabbit
New Features
Bug Fixes
Documentation
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.