[GH-ISSUE #5302] [Feature]: Shard every remaining file-storage table instead of rewriting global JSON monoliths #2932

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

Originally created by @SpicyMarinara on GitHub (Aug 20, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/5302

Originally assigned to: @SpicyMarinara on GitHub.

What problem does this solve?

Marinara's file-native store still persists most logical tables as one global JSON array per table under storage/tables/<table>.json. Only the chat-keyed high-write tables listed in SHARDED_TABLES were split by #4708.

That leaves characters and their version history, personas and their version history, lorebooks and entries, presets and sections, connections, agent configs, images, Noodle/Slurp data, folders, themes, settings, extensions, and many other collections as monoliths. A mutation to one row marks the whole table dirty, serializes every row, and atomically replaces the entire table snapshot.

The frequency is lower than messages, but the same structural problems remain:

  • Editing one entity rewrites unrelated entities in the same table.
  • Large cards, alternate greetings, embeddings, version histories, lorebook entries, image metadata, and social data make write cost grow with the whole install.
  • Table-level corruption and recovery have a wider blast radius than the entity that changed.
  • Startup must parse every row from each global table snapshot.
  • Phone-hosted installs incur avoidable serialization work and flash writes.
  • Leaving most tables monolithic gives the storage layer two inconsistent durability and scaling models.

This becomes more expensive to migrate later as user libraries and embedding data grow.

Proposed solution

Extend the proven storage-format migration from #4708 so every persistent table has an explicit shard strategy, and no multi-row collection remains a global JSON-array monolith.

Use the stable ownership/access key for each table rather than assuming every shard is chat-keyed. Examples:

  • characters/<characterId>.json
  • character_card_versions/<characterId>.json
  • personas/<personaId>.json
  • persona_card_versions/<personaId>.json
  • lorebooks/<lorebookId>.json
  • lorebook_entries/<lorebookId>.json
  • prompt_presets/<presetId>.json with groups, sections, and choice blocks grouped by their owning preset
  • api_connections/<connectionId>.json
  • agent_configs/<agentId>.json
  • entity-owned gallery rows grouped by character/persona/owner
  • naturally keyed settings and singleton-style records split by their stable key rather than exempting an entire growing table

The implementation should generalize the current chat-specific shard metadata into declarative per-table shard ownership while preserving the existing query/storage API. Parent-owned child tables may use a maintained parent index, as message_swipes already does.

Carry forward all safety properties established by #4708:

  • Bump and validate the storage format.
  • Use collision-safe shard filenames for imported IDs on case-insensitive filesystems.
  • Migrate both primary and .bak monolith snapshots, with a crash-recoverable migration sentinel.
  • Preserve .pre-shard recovery artifacts.
  • Prefer shards and quarantine a conflicting monolith after downgrade; never silently merge divergent copies.
  • Preserve malformed/orphaned rows in an explicit orphan shard rather than dropping them.
  • Track dirty state, transaction rollback, durable rollback, cascades, and set-null mutations at shard granularity.
  • Keep manifest row counts, backup/restore, profile import/export, clear-data flows, and writer-lease behavior correct.
  • Recover or quarantine one damaged shard without discarding unrelated entities.

This can land in reviewable phases, but it should remain one coordinated storage-format feature with a complete table-to-shard ownership map and a final regression proving that every registered table is sharded or has an explicit one-record-per-key strategy.

Acceptance criteria

  • Every entry in FILE_BACKED_TABLES has a declared shard-key/ownership strategy.
  • No multi-row table is persisted as storage/tables/<table>.json after migration.
  • Mutating one character, persona, lorebook, preset, connection, agent, or other entity rewrites only the affected shard(s), plus required manifest metadata.
  • Child-row moves dirty both the old and new owner shards.
  • Current-format, legacy-monolith, interrupted-migration, backup-only, downgrade-conflict, malformed-row, and orphan-row paths have runnable regression coverage.
  • Profile export/import and destructive clear-data operations preserve their current behavior.
  • User-facing storage documentation and release notes describe the migration and recovery artifacts.

Alternatives considered

  • Shard only the currently largest tables: postpones the same failure mode and leaves storage behavior inconsistent. The requirement is an explicit strategy for every table.
  • Increase the save debounce: reduces write frequency but not serialization size, recovery blast radius, or startup parsing.
  • Replace file-native storage with SQLite or another database: substantially larger platform and migration work, especially on Android/Termux, when the existing sharded snapshot model already provides the intended portability.
  • One file per raw row for every child table: simple but can create excessive tiny-file counts; grouping dependent rows by their stable owning entity gives bounded rewrites without abandoning the snapshot model.

Additional context

Related completed work: #4708 introduced storage formats 3/4, safe shard filenames, shard-granular dirty tracking, migration sentinels, downgrade conflict handling, backups, and orphan preservation for chat-keyed tables. This issue extends that architecture across the rest of FILE_BACKED_TABLES rather than creating a second persistence design.

Canonical architecture reference: docs/development/file-storage.md. Current shard registry and migration machinery: packages/server/src/db/file-backed-store.ts.

Duplicate check: no open issue currently covers sharding the remaining entity/configuration tables.

Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @SpicyMarinara on GitHub (Aug 20, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/5302 Originally assigned to: @SpicyMarinara on GitHub. ## What problem does this solve? Marinara's file-native store still persists most logical tables as one global JSON array per table under `storage/tables/<table>.json`. Only the chat-keyed high-write tables listed in `SHARDED_TABLES` were split by #4708. That leaves characters and their version history, personas and their version history, lorebooks and entries, presets and sections, connections, agent configs, images, Noodle/Slurp data, folders, themes, settings, extensions, and many other collections as monoliths. A mutation to one row marks the whole table dirty, serializes every row, and atomically replaces the entire table snapshot. The frequency is lower than messages, but the same structural problems remain: - Editing one entity rewrites unrelated entities in the same table. - Large cards, alternate greetings, embeddings, version histories, lorebook entries, image metadata, and social data make write cost grow with the whole install. - Table-level corruption and recovery have a wider blast radius than the entity that changed. - Startup must parse every row from each global table snapshot. - Phone-hosted installs incur avoidable serialization work and flash writes. - Leaving most tables monolithic gives the storage layer two inconsistent durability and scaling models. This becomes more expensive to migrate later as user libraries and embedding data grow. ## Proposed solution Extend the proven storage-format migration from #4708 so **every persistent table has an explicit shard strategy**, and no multi-row collection remains a global JSON-array monolith. Use the stable ownership/access key for each table rather than assuming every shard is chat-keyed. Examples: - `characters/<characterId>.json` - `character_card_versions/<characterId>.json` - `personas/<personaId>.json` - `persona_card_versions/<personaId>.json` - `lorebooks/<lorebookId>.json` - `lorebook_entries/<lorebookId>.json` - `prompt_presets/<presetId>.json` with groups, sections, and choice blocks grouped by their owning preset - `api_connections/<connectionId>.json` - `agent_configs/<agentId>.json` - entity-owned gallery rows grouped by character/persona/owner - naturally keyed settings and singleton-style records split by their stable key rather than exempting an entire growing table The implementation should generalize the current chat-specific shard metadata into declarative per-table shard ownership while preserving the existing query/storage API. Parent-owned child tables may use a maintained parent index, as `message_swipes` already does. Carry forward all safety properties established by #4708: - Bump and validate the storage format. - Use collision-safe shard filenames for imported IDs on case-insensitive filesystems. - Migrate both primary and `.bak` monolith snapshots, with a crash-recoverable migration sentinel. - Preserve `.pre-shard` recovery artifacts. - Prefer shards and quarantine a conflicting monolith after downgrade; never silently merge divergent copies. - Preserve malformed/orphaned rows in an explicit orphan shard rather than dropping them. - Track dirty state, transaction rollback, durable rollback, cascades, and set-null mutations at shard granularity. - Keep manifest row counts, backup/restore, profile import/export, clear-data flows, and writer-lease behavior correct. - Recover or quarantine one damaged shard without discarding unrelated entities. This can land in reviewable phases, but it should remain one coordinated storage-format feature with a complete table-to-shard ownership map and a final regression proving that every registered table is sharded or has an explicit one-record-per-key strategy. ### Acceptance criteria - Every entry in `FILE_BACKED_TABLES` has a declared shard-key/ownership strategy. - No multi-row table is persisted as `storage/tables/<table>.json` after migration. - Mutating one character, persona, lorebook, preset, connection, agent, or other entity rewrites only the affected shard(s), plus required manifest metadata. - Child-row moves dirty both the old and new owner shards. - Current-format, legacy-monolith, interrupted-migration, backup-only, downgrade-conflict, malformed-row, and orphan-row paths have runnable regression coverage. - Profile export/import and destructive clear-data operations preserve their current behavior. - User-facing storage documentation and release notes describe the migration and recovery artifacts. ## Alternatives considered - **Shard only the currently largest tables:** postpones the same failure mode and leaves storage behavior inconsistent. The requirement is an explicit strategy for every table. - **Increase the save debounce:** reduces write frequency but not serialization size, recovery blast radius, or startup parsing. - **Replace file-native storage with SQLite or another database:** substantially larger platform and migration work, especially on Android/Termux, when the existing sharded snapshot model already provides the intended portability. - **One file per raw row for every child table:** simple but can create excessive tiny-file counts; grouping dependent rows by their stable owning entity gives bounded rewrites without abandoning the snapshot model. ## Additional context Related completed work: #4708 introduced storage formats 3/4, safe shard filenames, shard-granular dirty tracking, migration sentinels, downgrade conflict handling, backups, and orphan preservation for chat-keyed tables. This issue extends that architecture across the rest of `FILE_BACKED_TABLES` rather than creating a second persistence design. Canonical architecture reference: `docs/development/file-storage.md`. Current shard registry and migration machinery: `packages/server/src/db/file-backed-store.ts`. Duplicate check: no open issue currently covers sharding the remaining entity/configuration tables. ## Template check - [ ] I DID NOT read this template and provide the requested details.
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#2932
No description provided.