[PR #2399] [MERGED] Replace atomic-update deep-equality conflict check with cheap file-stamp guard #5583

Closed
opened 2026-10-22 04:17:34 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/2399
Author: @Romuromylus
Created: 6/6/2026
Status: ✅ Merged
Merged: 6/6/2026
Merged by: @Romuromylus

Base: refactor ← Head: fix/storage-cheap-conflict-guard


📝 Commits (2)

  • 49d9107 Replace atomic-update deep-equality conflict check with cheap file-stamp guard
  • 33363e1 Capture conflict-guard stamp under the load lock to close a TOCTOU window

📊 Changes

1 file changed (+13 additions, -10 deletions)

View changed files

📝 src-tauri/crates/storage/src/lib.rs (+13 -10)

📄 Description

Linked issue

Closes #2301

Why this change

  • update_collections_atomically ran on every message create/edit/swipe. Its concurrent-modification guard cloned the entire global messages + message-swipes collections and deep-compared them by structural serde_json::Value equality (O(n) over the whole corpus) under the storage write lock — pure overhead that scales with chat history size.

What changed

  • src-tauri/crates/storage/src/lib.rs: the guard now captures a cheap CollectionFileStamp (file length + mtime + content hash) per target collection at load and re-checks it at commit, instead of cloning and deep-comparing all rows. The CollectionFileStamp/collection_file_stamp primitive already exists in the file and is used by the projected-list cache.
  • The pre-load flush_dirty_collections_locked() plus the atomic-update mutex mean targets are clean on disk and only an external/concurrent write changes a stamp, so the storage_conflict contract is preserved. Public method signature unchanged — callers (message_swipes.rs / chats.rs / entities.rs) untouched.

Refactor impact

Primary owner: Rust storage

Impact areas reviewed:

  • src-tauri/crates/storage/src/lib.rs (update_collections_atomically)

Boundary notes:

  • None. Private method body in the marinara-storage crate; no mode-specific logic (shared infra). Per-chat sharding (issue's larger alternative) intentionally out of scope.

Pressure points touched:

  • None.

Validation

  • Matching validation command passes locally
  • Full pnpm check passes before PR push/handoff
  • Human/manual validation completed by contributor or reviewer

Manual verification notes

  • cargo test --manifest-path src-tauri/Cargo.toml -p marinara-storage — 45 passed, including all existing conflict/recovery tests (rejects_duplicate, rejects_reentrant_writes, reads_targets_without_recovery, reentrant_reads_do_not_recover, reads_and_replaces_multiple_collections). Local inline tests confirmed the cheap guard still rejects a genuine out-of-band modification (storage_conflict) and does not false-positive on a normal sequential write (kept local per repo convention).
  • cargo clippy --manifest-path src-tauri/Cargo.toml --workspace --all-targets -- -D warnings — clean.

Feature Discoverability

  • Updated src/features/shell/discovery/
  • N/A because this PR is a performance bugfix in storage internals.

Reason:

  • No user-facing surface change.

Docs and release impact

  • No docs changes needed

🔄 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/2399 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 6/6/2026 **Status:** ✅ Merged **Merged:** 6/6/2026 **Merged by:** [@Romuromylus](https://github.com/Romuromylus) **Base:** `refactor` ← **Head:** `fix/storage-cheap-conflict-guard` --- ### 📝 Commits (2) - [`49d9107`](https://github.com/Pasta-Devs/Marinara-Engine/commit/49d91073c0146454db6f3b5271bf5efad1798bc2) Replace atomic-update deep-equality conflict check with cheap file-stamp guard - [`33363e1`](https://github.com/Pasta-Devs/Marinara-Engine/commit/33363e1079d41e4086cfa1202e21bb758c0d1426) Capture conflict-guard stamp under the load lock to close a TOCTOU window ### 📊 Changes **1 file changed** (+13 additions, -10 deletions) <details> <summary>View changed files</summary> 📝 `src-tauri/crates/storage/src/lib.rs` (+13 -10) </details> ### 📄 Description ## Linked issue Closes #2301 ## Why this change - `update_collections_atomically` ran on every message create/edit/swipe. Its concurrent-modification guard cloned the entire global `messages` + `message-swipes` collections and deep-compared them by structural `serde_json::Value` equality (O(n) over the whole corpus) under the storage write lock — pure overhead that scales with chat history size. ## What changed - `src-tauri/crates/storage/src/lib.rs`: the guard now captures a cheap `CollectionFileStamp` (file length + mtime + content hash) per target collection at load and re-checks it at commit, instead of cloning and deep-comparing all rows. The `CollectionFileStamp`/`collection_file_stamp` primitive already exists in the file and is used by the projected-list cache. - The pre-load `flush_dirty_collections_locked()` plus the atomic-update mutex mean targets are clean on disk and only an external/concurrent write changes a stamp, so the `storage_conflict` contract is preserved. Public method signature unchanged — callers (message_swipes.rs / chats.rs / entities.rs) untouched. ## Refactor impact Primary owner: Rust storage Impact areas reviewed: - `src-tauri/crates/storage/src/lib.rs` (`update_collections_atomically`) Boundary notes: - None. Private method body in the `marinara-storage` crate; no mode-specific logic (shared infra). Per-chat sharding (issue's larger alternative) intentionally out of scope. Pressure points touched: - None. ## Validation - [x] Matching validation command passes locally - [ ] Full `pnpm check` passes before PR push/handoff - [ ] Human/manual validation completed by contributor or reviewer ### Manual verification notes - `cargo test --manifest-path src-tauri/Cargo.toml -p marinara-storage` — 45 passed, including all existing conflict/recovery tests (`rejects_duplicate`, `rejects_reentrant_writes`, `reads_targets_without_recovery`, `reentrant_reads_do_not_recover`, `reads_and_replaces_multiple_collections`). Local inline tests confirmed the cheap guard still rejects a genuine out-of-band modification (`storage_conflict`) and does not false-positive on a normal sequential write (kept local per repo convention). - `cargo clippy --manifest-path src-tauri/Cargo.toml --workspace --all-targets -- -D warnings` — clean. ## Feature Discoverability - [ ] Updated `src/features/shell/discovery/` - [x] N/A because this PR is a performance bugfix in storage internals. Reason: - No user-facing surface change. ## Docs and release impact - [x] No docs changes needed --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:17:34 +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#5583
No description provided.