[PR #4738] [MERGED] feat(storage): downgrade guard + unshard escape hatch (#4708, part 2) #6484

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

📋 Pull Request Information

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

Base: staging ← Head: feat/4708-downgrade-guard


📝 Commits (10+)

  • 6c98122 wip(4708): downgrade guard - format file, launcher check-target, server 409 gate
  • e6f225d Merge feat/4708-message-shards (review fixes) into feat/4708-downgrade-guard
  • 3141411 feat(launcher): unshard escape hatch, docs, and format-guard regression
  • 7728f7f Merge feat/4708-message-shards (manifest-heal fix) into feat/4708-downgrade-guard
  • ad891b6 fix(launcher): harden the downgrade guard and unshard after adversarial review
  • 2e2ec13 Merge feat/4708-message-shards (stale-physical-shard fix) into feat/4708-downgrade-guard
  • d141c9f Merge branch 'staging' into feat/4708-downgrade-guard
  • 269b8be Merge feat/4708-message-shards (review fixes + staging) into feat/4708-downgrade-guard
  • c10af1e fix(launcher): honor the check-target exit-code contract everywhere
  • 137ede3 Merge remote feat/4708-downgrade-guard (maintainer branch update)

📊 Changes

14 files changed (+1161 additions, -16 deletions)

View changed files

📝 CLAUDE.md (+4 -0)
📝 Dockerfile (+3 -0)
📝 Dockerfile.lite (+3 -0)
📝 docs/TROUBLESHOOTING.md (+21 -0)
📝 docs/UPGRADING.md (+6 -0)
📝 package.json (+1 -1)
📝 packages/server/src/routes/updates.routes.ts (+103 -1)
📝 scripts/protect-launcher-data.mjs (+417 -8)
➕ scripts/regressions/launcher-format-guard.regression.mjs (+513 -0)
📝 start-termux.sh (+29 -3)
📝 start.bat (+15 -0)
📝 start.sh (+29 -3)
➕ storage-format.json (+3 -0)
📝 win/installer/install.bat (+14 -0)

📄 Description

Note

Stacked on #4735 (message sharding, PR 1 of #4708). This diff includes #4735's commits until it merges; the PR 2-only changes are the commits touching storage-format.json, scripts/protect-launcher-data.mjs, packages/server/src/routes/updates.routes.ts, the three launchers, win/installer/install.bat, both Dockerfiles, docs, and the new regression lane. Per the approved plan on #4708, this must ship in the same release as #4735 — PR 1 without this guard means the first downgrade a user attempts silently empties their chat history.

Linked issue

Part 2 of #4708 (intentionally not Closes — the issue stays open for the remaining chat-keyed tables; see the checklist there).

Why this change

  • #4735 moves messages and swipes to a per-chat sharded layout (storage format 3). Older builds only understand the monolith layout (format 2): if a user downgrades — auto-update onto an older channel, in-app update to an older tag, or a manual checkout — their chats look empty and the old build can write a forked monolith next to the shards.
  • Maintainer-approved plan on #4708: the migration is automatic with no opt-out, so the protection lives at the downgrade boundary — refuse moves onto builds that cannot read the data, and give the user a real escape hatch instead of "you cannot downgrade".

What changed

  • storage-format.json (repo root, tracked): declares the storage format a checkout understands. The guard reads it off the target ref with git show; absent (any ref predating this PR) means format 2. A regression pins it to STORAGE_VERSION in file-backed-store.ts so a missed bump cannot silently disable the guard.
  • Launcher guard — scripts/protect-launcher-data.mjs check-target <ref> compares the on-disk storage/manifest.json version against the target ref's declared format; exits 2 with a [BLOCK] message on incompatible targets. start.sh, start-termux.sh, and start.bat run it before the update snapshot (a blocked target stays blocked on every launch — snapshotting first would re-copy the whole data directory each time) and skip the auto-update instead of downgrading (fail-safe: any guard failure skips the update, never applies it). The Windows installer's update-over-existing-install path carries the same guard, blocking only on exit code 2 so an older checkout whose script predates the subcommand cannot break a legitimate upgrade. The storage-dir mirror honors MARINARA_FILE_STORAGE_DIR and MARINARA_ENV_FILE with the server's exact dotenv merge semantics, so the guard reads the same manifest the server would.
  • In-app updater guard — the apply-update route refuses an incompatible target with a 409 before creating the update stash or touching the working tree. The server cannot import the launcher script, so updates.routes.ts carries a twin of checkTargetStorageFormat with a kept-in-sync comment; the regression pins the pairing.
  • Escape hatch — node scripts/protect-launcher-data.mjs unshard (run with the server stopped) rebuilds the monolith layout from the shard files: reads everything before writing anything (fsync-before-rename writes), keeps every shard file in <table>.post-unshard-<timestamp>/, rewrites the manifest as format 2 so the guard unblocks, keeps a crashed migration's monolith authoritative, and refuses the ambiguous "monolith and shards both present, neither in progress" forked state rather than guessing a merge. A health-endpoint probe refuses to run while a server is answering (a live Marinara re-shards on its next save), and an interrupted run resumes under a shards-stay-authoritative rule — the same rule the store's own classifier applies — with any displaced monolith set aside as .pre-unshard-<timestamp>, never deleted. Both Docker images ship the script so the documented one-off-container recovery actually works.
    • The #4708 plan sketched this as a mari db unshard command. The mari CLI turned out to be an HTTP forwarder to a running server — unusable here (a live sharded server re-shards on its next flush, and Docker/manual users may have no server at all), so the command lives in the offline launcher-data script instead.
  • Docs — new docs/TROUBLESHOOTING.md section ("Chats show no messages after switching to an older version" — the exact title every guard message points at) and a "Downgrading to an older version" section in docs/UPGRADING.md. CLAUDE.md documents the storage-format.json ↔ STORAGE_VERSION pairing rule (it is not a version:sync file).
  • Regression — new launcher-format-guard lane in regression:issues: format-file pairing, guard presence and guard-before-snapshot ordering in every entry point (source pins, including the installer and both Dockerfiles), SHARDED_TABLES/.migrating pairing pins between the script and the store source, checkTargetStorageFormat against a real git fixture repo (blocked / allowed / fresh-install cases), the check-target CLI exit contract, env-mirror parity (alias + custom env file), and ten behavioral unshard cases (rebuild + ordering, dedup + bak-only recovery, crashed-migration, forked-state abort incl. .bak-only monoliths, unreadable-shard abort, resumed-run shards-win semantics, live-server refusal). Plus, on the #4735 side: the store heals a lagging manifest version on startup, so a crash between the migration and its first flush cannot leave the guard trusting a stale format 2.

The change went through an adversarial review workflow (3 specialist lenses, every finding independently re-verified against the code); 16 confirmed findings were fixed and regression-pinned before this PR was opened.

Coverage is honest, not universal: manual git checkout bypasses every guard, and a Docker tag switch is only protected by the store's own forward version gate (PR 1) — for both, the TROUBLESHOOTING steps are the recovery. The PR does not pretend otherwise.

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

  • Manually verify the in-app updater against an older tag: with format-3 data on disk, pick a pre-sharding release in Settings → Advanced → Check for Updates and confirm Apply Update surfaces the 409 message instead of applying.
  • Manually verify the launcher path: with format-3 data, point the release channel at an older target and confirm start.bat / start.sh print the [WARN] Skipping auto-update… line and boot the current version.
  • Manually verify the escape hatch end-to-end: stop the server, run node scripts/protect-launcher-data.mjs unshard, boot an older build, and confirm chats render; then boot the current build again and confirm the automatic re-migration.
  • Commands run: pnpm check (green, before and after the review-fix round), node scripts/regressions/launcher-format-guard.regression.mjs (green, including the resumed-run, live-server-refusal, and env-parity cases), launcher-env + launcher-update lanes (green), message-sharding lane including the new manifest-heal pin (green), bash -n on both shell launchers (clean).

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Translated docs on the docs-i18n branch updated to match, or a [docs-i18n] follow-up issue opened (see CONTRIBUTING.md § Translated documentation)
  • Version/release files updated (only if this PR includes a version bump)

docs/TROUBLESHOOTING.md and docs/UPGRADING.md changed → needs a [docs-i18n] docs/TROUBLESHOOTING.md docs/UPGRADING.md follow-up issue (will open one when this PR is submitted).

Summary by CodeRabbit

  • New Features

    • Organized chat messages and swipe data into independently managed files.
    • Added storage recovery, validation, backups, and repair capabilities.
    • Added tools to check compatibility and convert data for older releases.
    • Added compatibility checks to launchers, installers, and updates.
  • Bug Fixes

    • Prevented incompatible downgrades from risking chat history.
    • Safeguarded malformed, duplicate, missing, or partially written chat data.
  • Documentation

    • Added upgrade, downgrade, troubleshooting, and storage-format guidance, including Docker and Podman instructions.

🔄 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/4738 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 8/7/2026 **Status:** ✅ Merged **Merged:** 8/8/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `feat/4708-downgrade-guard` --- ### 📝 Commits (10+) - [`6c98122`](https://github.com/Pasta-Devs/Marinara-Engine/commit/6c981221aae39add652d98cc37b1b29524a75293) wip(4708): downgrade guard - format file, launcher check-target, server 409 gate - [`e6f225d`](https://github.com/Pasta-Devs/Marinara-Engine/commit/e6f225db00ca8d5c3040608fb7653c9b3baa4000) Merge feat/4708-message-shards (review fixes) into feat/4708-downgrade-guard - [`3141411`](https://github.com/Pasta-Devs/Marinara-Engine/commit/314141120d91195398072289462a1316b361f684) feat(launcher): unshard escape hatch, docs, and format-guard regression - [`7728f7f`](https://github.com/Pasta-Devs/Marinara-Engine/commit/7728f7f735fdfca644a51f205f545f9c7bedc42f) Merge feat/4708-message-shards (manifest-heal fix) into feat/4708-downgrade-guard - [`ad891b6`](https://github.com/Pasta-Devs/Marinara-Engine/commit/ad891b6976988a72c6c155bc415b160e9bf82358) fix(launcher): harden the downgrade guard and unshard after adversarial review - [`2e2ec13`](https://github.com/Pasta-Devs/Marinara-Engine/commit/2e2ec1324dd15f5bfd8caf8fd77bbf533b916e0c) Merge feat/4708-message-shards (stale-physical-shard fix) into feat/4708-downgrade-guard - [`d141c9f`](https://github.com/Pasta-Devs/Marinara-Engine/commit/d141c9ff6547985ec64de89464cf7074e7e34869) Merge branch 'staging' into feat/4708-downgrade-guard - [`269b8be`](https://github.com/Pasta-Devs/Marinara-Engine/commit/269b8befb72478e302752a755ec826909898d429) Merge feat/4708-message-shards (review fixes + staging) into feat/4708-downgrade-guard - [`c10af1e`](https://github.com/Pasta-Devs/Marinara-Engine/commit/c10af1e0a32bd936c73bca9e2145c1fc98ca8590) fix(launcher): honor the check-target exit-code contract everywhere - [`137ede3`](https://github.com/Pasta-Devs/Marinara-Engine/commit/137ede3018fd3d3f8b8d6ad4f623f549da3c3765) Merge remote feat/4708-downgrade-guard (maintainer branch update) ### 📊 Changes **14 files changed** (+1161 additions, -16 deletions) <details> <summary>View changed files</summary> 📝 `CLAUDE.md` (+4 -0) 📝 `Dockerfile` (+3 -0) 📝 `Dockerfile.lite` (+3 -0) 📝 `docs/TROUBLESHOOTING.md` (+21 -0) 📝 `docs/UPGRADING.md` (+6 -0) 📝 `package.json` (+1 -1) 📝 `packages/server/src/routes/updates.routes.ts` (+103 -1) 📝 `scripts/protect-launcher-data.mjs` (+417 -8) ➕ `scripts/regressions/launcher-format-guard.regression.mjs` (+513 -0) 📝 `start-termux.sh` (+29 -3) 📝 `start.bat` (+15 -0) 📝 `start.sh` (+29 -3) ➕ `storage-format.json` (+3 -0) 📝 `win/installer/install.bat` (+14 -0) </details> ### 📄 Description > [!NOTE] > **Stacked on #4735** (message sharding, PR 1 of #4708). This diff includes #4735's commits until it merges; the PR 2-only changes are the commits touching `storage-format.json`, `scripts/protect-launcher-data.mjs`, `packages/server/src/routes/updates.routes.ts`, the three launchers, `win/installer/install.bat`, both Dockerfiles, docs, and the new regression lane. Per the approved plan on #4708, **this must ship in the same release as #4735** — PR 1 without this guard means the first downgrade a user attempts silently empties their chat history. ## Linked issue Part 2 of #4708 (intentionally not `Closes` — the issue stays open for the remaining chat-keyed tables; see the checklist there). ## Why this change - #4735 moves messages and swipes to a per-chat sharded layout (storage format 3). Older builds only understand the monolith layout (format 2): if a user downgrades — auto-update onto an older channel, in-app update to an older tag, or a manual checkout — their chats look empty and the old build can write a forked monolith next to the shards. - Maintainer-approved plan on #4708: the migration is automatic with no opt-out, so the protection lives at the *downgrade* boundary — refuse moves onto builds that cannot read the data, and give the user a real escape hatch instead of "you cannot downgrade". ## What changed - **`storage-format.json`** (repo root, tracked): declares the storage format a checkout understands. The guard reads it off the *target ref* with `git show`; absent (any ref predating this PR) means format 2. A regression pins it to `STORAGE_VERSION` in `file-backed-store.ts` so a missed bump cannot silently disable the guard. - **Launcher guard** — `scripts/protect-launcher-data.mjs check-target <ref>` compares the on-disk `storage/manifest.json` version against the target ref's declared format; exits 2 with a `[BLOCK]` message on incompatible targets. `start.sh`, `start-termux.sh`, and `start.bat` run it **before** the update snapshot (a blocked target stays blocked on every launch — snapshotting first would re-copy the whole data directory each time) and skip the auto-update instead of downgrading (fail-safe: any guard failure skips the update, never applies it). The Windows installer's update-over-existing-install path carries the same guard, blocking only on exit code 2 so an older checkout whose script predates the subcommand cannot break a legitimate upgrade. The storage-dir mirror honors `MARINARA_FILE_STORAGE_DIR` and `MARINARA_ENV_FILE` with the server's exact dotenv merge semantics, so the guard reads the same manifest the server would. - **In-app updater guard** — the apply-update route refuses an incompatible target with a 409 before creating the update stash or touching the working tree. The server cannot import the launcher script, so `updates.routes.ts` carries a twin of `checkTargetStorageFormat` with a kept-in-sync comment; the regression pins the pairing. - **Escape hatch** — `node scripts/protect-launcher-data.mjs unshard` (run with the server stopped) rebuilds the monolith layout from the shard files: reads everything before writing anything (fsync-before-rename writes), keeps every shard file in `<table>.post-unshard-<timestamp>/`, rewrites the manifest as format 2 so the guard unblocks, keeps a crashed migration's monolith authoritative, and refuses the ambiguous "monolith and shards both present, neither in progress" forked state rather than guessing a merge. A health-endpoint probe refuses to run while a server is answering (a live Marinara re-shards on its next save), and an interrupted run resumes under a shards-stay-authoritative rule — the same rule the store's own classifier applies — with any displaced monolith set aside as `.pre-unshard-<timestamp>`, never deleted. Both Docker images ship the script so the documented one-off-container recovery actually works. - The #4708 plan sketched this as a `mari db unshard` command. The `mari` CLI turned out to be an HTTP forwarder to a *running* server — unusable here (a live sharded server re-shards on its next flush, and Docker/manual users may have no server at all), so the command lives in the offline launcher-data script instead. - **Docs** — new `docs/TROUBLESHOOTING.md` section ("Chats show no messages after switching to an older version" — the exact title every guard message points at) and a "Downgrading to an older version" section in `docs/UPGRADING.md`. `CLAUDE.md` documents the `storage-format.json` ↔ `STORAGE_VERSION` pairing rule (it is *not* a `version:sync` file). - **Regression** — new `launcher-format-guard` lane in `regression:issues`: format-file pairing, guard presence and guard-before-snapshot ordering in every entry point (source pins, including the installer and both Dockerfiles), `SHARDED_TABLES`/`.migrating` pairing pins between the script and the store source, `checkTargetStorageFormat` against a real git fixture repo (blocked / allowed / fresh-install cases), the `check-target` CLI exit contract, env-mirror parity (alias + custom env file), and ten behavioral unshard cases (rebuild + ordering, dedup + bak-only recovery, crashed-migration, forked-state abort incl. `.bak`-only monoliths, unreadable-shard abort, resumed-run shards-win semantics, live-server refusal). Plus, on the #4735 side: the store heals a lagging manifest version on startup, so a crash between the migration and its first flush cannot leave the guard trusting a stale format 2. The change went through an adversarial review workflow (3 specialist lenses, every finding independently re-verified against the code); 16 confirmed findings were fixed and regression-pinned before this PR was opened. Coverage is honest, not universal: manual `git checkout` bypasses every guard, and a Docker tag switch is only protected by the store's own forward version gate (PR 1) — for both, the TROUBLESHOOTING steps are the recovery. The PR does not pretend otherwise. ## 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 - Manually verify the in-app updater against an older tag: with format-3 data on disk, pick a pre-sharding release in **Settings → Advanced → Check for Updates** and confirm Apply Update surfaces the 409 message instead of applying. - Manually verify the launcher path: with format-3 data, point the release channel at an older target and confirm `start.bat` / `start.sh` print the `[WARN] Skipping auto-update…` line and boot the current version. - Manually verify the escape hatch end-to-end: stop the server, run `node scripts/protect-launcher-data.mjs unshard`, boot an older build, and confirm chats render; then boot the current build again and confirm the automatic re-migration. - Commands run: `pnpm check` (green, before and after the review-fix round), `node scripts/regressions/launcher-format-guard.regression.mjs` (green, including the resumed-run, live-server-refusal, and env-parity cases), `launcher-env` + `launcher-update` lanes (green), message-sharding lane including the new manifest-heal pin (green), `bash -n` on both shell launchers (clean). ## Docs and release impact - [ ] No docs changes needed - [x] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Translated docs on the `docs-i18n` branch updated to match, or a `[docs-i18n]` follow-up issue opened (see CONTRIBUTING.md § Translated documentation) - [ ] Version/release files updated (only if this PR includes a version bump) `docs/TROUBLESHOOTING.md` and `docs/UPGRADING.md` changed → needs a `[docs-i18n] docs/TROUBLESHOOTING.md docs/UPGRADING.md` follow-up issue (will open one when this PR is submitted). <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Organized chat messages and swipe data into independently managed files. * Added storage recovery, validation, backups, and repair capabilities. * Added tools to check compatibility and convert data for older releases. * Added compatibility checks to launchers, installers, and updates. * **Bug Fixes** * Prevented incompatible downgrades from risking chat history. * Safeguarded malformed, duplicate, missing, or partially written chat data. * **Documentation** * Added upgrade, downgrade, troubleshooting, and storage-format guidance, including Docker and Podman instructions. <!-- 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:10:51 +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#6484
No description provided.