[PR #3502] [MERGED] fix(mari-db): import the authoritative cascade graph and make JSON_COLUMNS schema-true (#3486) #5968

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/3502
Author: @kolacheee
Created: 7/11/2026
Status: ✅ Merged
Merged: 7/11/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/3486-mari-db-drift


📝 Commits (2)

  • b955c08 fix(mari-db): import the authoritative cascade graph and make JSON_COLUMNS schema-true
  • ea69e2e fix(mari-db): add the message-scoped game-row edges to the cascade graph

📊 Changes

2 files changed (+38 additions, -52 deletions)

View changed files

📝 packages/server/src/db/file-backed-store.ts (+12 -1)
📝 packages/server/src/services/mari-db/mari-db.service.ts (+26 -51)

📄 Description

Linked issue

Fixes #3486

Why this change

services/mari-db (the Mari CLI's database layer) kept two private copies of schema knowledge, and both had drifted from the real definitions. The stale copies were load-bearing: mari db delete --cascade silently orphaned conversation-call and game-engine rows, the dangling-reference validator could not see them (it uses the same graph), and the JSON column map made Mari read raw strings and skip stringification for real JSON columns while "handling" columns that do not exist.

What changed

Cascade graph — deduplicated at the source. db/file-backed-store.ts now exports its CASCADES relation graph as the single source of truth, and mari-db.service.ts imports it instead of maintaining a duplicate. The duplicate was missing four edges (chats → conversation_call_sessions, chats → conversation_call_messages, chats → game_engine_state, conversation_call_sessions → conversation_call_messages); all mari-db consumers (cascade delete, the standalone validator, and the mutation-plan dangling check) now see the full graph, and any relation added in the store automatically reaches them.

JSON column map — made schema-true, and the drift ran deeper than the issue's two examples. I audited the entire map against the drizzle schema (script cross-checks every entry both directions: phantoms and omissions). Findings beyond the two game tables from #3486:

  • 20 phantom columns removed (they do not exist in the schema, so their entries were silent no-ops): the issue's game_state_snapshots (7) and game_checkpoints (2) items, plus regex_scripts.rules/tags, prompt_presets.tags, prompt_sections.enabledModes, choice_blocks.choices, chat_presets.parameters/tags, and metadata entries for chat_images, character_images, assets, custom_themes, and manifest/settings for installed_extensions (none of those tables have any JSON column).
  • 16 real JSON columns added (these were the live bugs — raw strings on parsed reads, no validity checking on writes): game_state_snapshots.recentEvents/personaStats/manualOverrides/fieldLocks, all six prompt_presets JSON columns (sectionOrder, groupOrder, variableGroups, variableValues, parameters, defaultChoices), regex_scripts.trimStrings/placement/targetCharacterIds, chat_presets.settings, choice_blocks.options, prompt_sections.markerConfig, game_engine_state.state, and api_connections.comfyuiWorkflow.

Two judgment calls, documented in code comments: comfyuiWorkflow is included because the app itself throws "Invalid ComfyUI workflow JSON" on parse failure (placeholders live inside string values), so JSON validity is already its contract; treatAsLocalEndpoint is excluded because it is a boolean-as-text whose doc comment merely mentions JSON tool behavior.

Known pre-existing limitation (unchanged, noting for completeness): mari-db does not model the store's SET_NULL_RELATIONS, so CLI cascade deletes do not null out storyboard keyframe references the way app-level deletes do.

Validation

  • 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

Runtime-validated end to end against a scratch server (fresh DATA_DIR), driving the real mari CLI over HTTP:

  1. Validator sees the new edges: inserting a conversation_call_messages row with an unknown callId is now BLOCKED at write time with Dangling reference callId=... -> conversation_call_sessions.id (pre-fix, the validator was blind to this relation). A clean dataset still reports status: "passed".
  2. Cascade covers the new edges: after mari db delete chats <id> --cascade --apply, the chat's conversation_call_sessions, conversation_call_messages, and game_engine_state rows are all gone (pre-fix they survived as orphans).
  3. JSON round-trip on corrected columns: inserting objects/arrays for game_engine_state.state, game_state_snapshots.recentEvents/personaStats, and regex_scripts.trimStrings/placement stores proper JSON text on disk (not [object Object]) and mari db get --parsed / db select return real parsed values.
  4. Whole-map audit: the schema cross-check script reports zero phantoms and zero unexplained omissions after the change.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Version/release files updated (only if this PR includes a version bump)

UI evidence (if applicable)

N/A — Mari CLI / server internals; no UI surface.

Summary by CodeRabbit

  • Bug Fixes

    • Improved database cleanup to consistently remove related records and reduce dangling references.
    • Updated data handling for saved prompts, API connections, agent configurations, workflows, game snapshots, and scripts.
    • Removed outdated field mappings to improve database import and export reliability.
  • Refactor

    • Centralized cascade-delete rules to keep database maintenance behavior consistent across tools.

🔄 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/3502 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 7/11/2026 **Status:** ✅ Merged **Merged:** 7/11/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/3486-mari-db-drift` --- ### 📝 Commits (2) - [`b955c08`](https://github.com/Pasta-Devs/Marinara-Engine/commit/b955c088e7df68a57ee8ba1787dd688f2c2c2d2b) fix(mari-db): import the authoritative cascade graph and make JSON_COLUMNS schema-true - [`ea69e2e`](https://github.com/Pasta-Devs/Marinara-Engine/commit/ea69e2ea63bda426f4c21549bcc575d0206e5031) fix(mari-db): add the message-scoped game-row edges to the cascade graph ### 📊 Changes **2 files changed** (+38 additions, -52 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/db/file-backed-store.ts` (+12 -1) 📝 `packages/server/src/services/mari-db/mari-db.service.ts` (+26 -51) </details> ### 📄 Description ## Linked issue Fixes #3486 ## Why this change `services/mari-db` (the Mari CLI's database layer) kept two private copies of schema knowledge, and both had drifted from the real definitions. The stale copies were load-bearing: `mari db delete --cascade` silently orphaned conversation-call and game-engine rows, the dangling-reference validator could not see them (it uses the same graph), and the JSON column map made Mari read raw strings and skip stringification for real JSON columns while "handling" columns that do not exist. ## What changed **Cascade graph — deduplicated at the source.** `db/file-backed-store.ts` now exports its `CASCADES` relation graph as the single source of truth, and `mari-db.service.ts` imports it instead of maintaining a duplicate. The duplicate was missing four edges (`chats → conversation_call_sessions`, `chats → conversation_call_messages`, `chats → game_engine_state`, `conversation_call_sessions → conversation_call_messages`); all mari-db consumers (cascade delete, the standalone validator, and the mutation-plan dangling check) now see the full graph, and any relation added in the store automatically reaches them. **JSON column map — made schema-true, and the drift ran deeper than the issue's two examples.** I audited the entire map against the drizzle schema (script cross-checks every entry both directions: phantoms and omissions). Findings beyond the two game tables from #3486: - 20 phantom columns removed (they do not exist in the schema, so their entries were silent no-ops): the issue's `game_state_snapshots` (7) and `game_checkpoints` (2) items, plus `regex_scripts.rules/tags`, `prompt_presets.tags`, `prompt_sections.enabledModes`, `choice_blocks.choices`, `chat_presets.parameters/tags`, and `metadata` entries for `chat_images`, `character_images`, `assets`, `custom_themes`, and `manifest`/`settings` for `installed_extensions` (none of those tables have any JSON column). - 16 real JSON columns added (these were the live bugs — raw strings on parsed reads, no validity checking on writes): `game_state_snapshots.recentEvents/personaStats/manualOverrides/fieldLocks`, all six `prompt_presets` JSON columns (`sectionOrder`, `groupOrder`, `variableGroups`, `variableValues`, `parameters`, `defaultChoices`), `regex_scripts.trimStrings/placement/targetCharacterIds`, `chat_presets.settings`, `choice_blocks.options`, `prompt_sections.markerConfig`, `game_engine_state.state`, and `api_connections.comfyuiWorkflow`. Two judgment calls, documented in code comments: `comfyuiWorkflow` is included because the app itself throws "Invalid ComfyUI workflow JSON" on parse failure (placeholders live inside string values), so JSON validity is already its contract; `treatAsLocalEndpoint` is excluded because it is a boolean-as-text whose doc comment merely mentions JSON tool behavior. Known pre-existing limitation (unchanged, noting for completeness): mari-db does not model the store's `SET_NULL_RELATIONS`, so CLI cascade deletes do not null out storyboard keyframe references the way app-level deletes do. ## Validation - [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` ### Manual verification notes Runtime-validated end to end against a scratch server (fresh `DATA_DIR`), driving the real `mari` CLI over HTTP: 1. **Validator sees the new edges:** inserting a `conversation_call_messages` row with an unknown `callId` is now BLOCKED at write time with `Dangling reference callId=... -> conversation_call_sessions.id` (pre-fix, the validator was blind to this relation). A clean dataset still reports `status: "passed"`. 2. **Cascade covers the new edges:** after `mari db delete chats <id> --cascade --apply`, the chat's `conversation_call_sessions`, `conversation_call_messages`, and `game_engine_state` rows are all gone (pre-fix they survived as orphans). 3. **JSON round-trip on corrected columns:** inserting objects/arrays for `game_engine_state.state`, `game_state_snapshots.recentEvents`/`personaStats`, and `regex_scripts.trimStrings`/`placement` stores proper JSON text on disk (not `[object Object]`) and `mari db get --parsed` / `db select` return real parsed values. 4. **Whole-map audit:** the schema cross-check script reports zero phantoms and zero unexplained omissions after the change. ## Docs and release impact - [x] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Version/release files updated (only if this PR includes a version bump) ## UI evidence (if applicable) N/A — Mari CLI / server internals; no UI surface. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved database cleanup to consistently remove related records and reduce dangling references. * Updated data handling for saved prompts, API connections, agent configurations, workflows, game snapshots, and scripts. * Removed outdated field mappings to improve database import and export reliability. * **Refactor** * Centralized cascade-delete rules to keep database maintenance behavior consistent across tools. <!-- 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-23 04:05:43 +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#5968
No description provided.