Skip to content

fix(#273): clear trades, messages and sessions on identity deletion - #298

Open
codaMW wants to merge 2 commits into
MostroP2P:mainfrom
codaMW:feat/273-clear-trades-messages-on-regenerate
Open

fix(#273): clear trades, messages and sessions on identity deletion#298
codaMW wants to merge 2 commits into
MostroP2P:mainfrom
codaMW:feat/273-clear-trades-messages-on-regenerate

Conversation

@codaMW

@codaMW codaMW commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator

Problem

"Generate new user" rotated the identity but never deleted the data derived from the old one. delete_identity() cleared the in-memory identity, the persisted identity row, the trade-key mappings and the logs but the trades table (My Trades history), the messages table (chat history), and the in-memory sessions survived. The new identity started with fresh keys yet inherited the previous user's entire trade list and conversations a privacy issue, and dead state (those trade keys were already cleared, so nothing could operate on the orders).

Fix

  • Add clear_trades / clear_messages to the DB trait, mirroring clear_trade_keys. SQLite implements both; IndexedDB stubs them alongside the existing clear_trade_keys stub, pending IndexedDB persistence (Web: IndexedDB storage backend is a stub — nothing persists across a reload #233).
  • Add SessionManager::clear_all() to drop every in-memory session.
  • Call all three from delete_identity() clear_messages before clear_trades for the messages.trade_id -> trades(id) FK and empty the in-memory sessions.
  • Dart: invalidate rawTradesProvider after IdentityService.regenerate() so My Trades reflects the clean slate immediately. The chat rooms list (chatRoomsFromTradesProvider) derives from rawTradesProvider, so it clears in the same pass.

Testing

  • SQLite test: after clear_messages + clear_trades, both tables are empty; clearing again on empty tables is a no-op.
  • cargo test --lib (255) / clippy -D warnings / wasm check all green; flutter analyze clean.
  • Verified on a physical device (Nokia C31): create an order, Generate New User → My Trades and chats are empty.

Closes #273.

Summary by CodeRabbit

  • New Features

    • Added comprehensive identity deletion cleanup, including persisted trades, chat messages, and active sessions.
    • Newly generated identities now immediately clear cached trade data.
    • Repeated cleanup operations are handled safely without errors.
  • Bug Fixes

    • Prevented deleted identity data from remaining accessible through cached or persisted records.

…eletion

Generating a new user rotated the identity but left the previous user's data
behind: delete_identity() cleared the identity row and trade-key mappings but
not the trades table, the messages table, or the in-memory sessions, so the new
identity inherited the old one's My Trades list and chats — a privacy issue, and
dead state (the trade keys were already cleared).

Add clear_trades / clear_messages to the DB trait (SQLite implemented; IndexedDB
stubbed alongside the existing clear_trade_keys pending MostroP2P#233) and a
SessionManager::clear_all(). Call them from delete_identity() — messages before
trades for the FK — and empty the in-memory sessions. On the Dart side,
invalidate rawTradesProvider after regenerate() so My Trades (and the chat rooms
derived from it) reflect the clean slate immediately.

Verified on a physical device (Nokia C31): after Generate New User, My Trades
and chats are empty. Adds a SQLite test that the clears empty both tables.
@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@codaMW, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 27 minutes

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 61df53ad-18db-4ce5-876d-1bfe7e56ce4c

📥 Commits

Reviewing files that changed from the base of the PR and between 9aca693 and 6a8bbbb.

📒 Files selected for processing (3)
  • rust/src/api/identity.rs
  • rust/src/db/indexeddb.rs
  • test/features/trades/filtered_trades_provider_test.dart

Walkthrough

Identity cleanup now removes persisted trades, messages, and in-memory sessions. Identity regeneration also invalidates the cached trades provider. SQLite supports the cleanup operations, while IndexedDB uses successful no-op implementations.

Changes

Identity cleanup

Layer / File(s) Summary
Storage cleanup
rust/src/db/mod.rs, rust/src/db/sqlite.rs, rust/src/db/indexeddb.rs
The storage interface and backends now expose trade and message clearing. SQLite deletes all rows and tests repeated clearing. IndexedDB returns success without changing persistence.
Session clearing
rust/src/mostro/session.rs
SessionManager::clear_all removes every in-memory session and returns the previous count.
Identity and UI cleanup
rust/src/api/identity.rs, lib/features/account/screens/account_screen.dart
Identity deletion clears persisted data and sessions while logging non-fatal cleanup errors. Identity regeneration invalidates rawTradesProvider.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related issues

Possibly related PRs

Suggested reviewers: grunch, catrya

Poem

A rabbit cleared the trades away,
And messages followed without delay.
Old sessions hopped out of sight,
Fresh identity brought cache-light.
SQLite now starts clean and bright.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: clearing trades, messages, and sessions when an identity is deleted.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@lib/features/account/screens/account_screen.dart`:
- Around line 390-395: Add a focused provider test for the identity-regeneration
cache reset around rawTradesProvider: override it with a mutable fetcher, verify
an initial non-empty result, change the fetcher to return an empty list,
invalidate rawTradesProvider, and assert the subsequent read is empty without
invoking native identity APIs.

In `@rust/src/api/identity.rs`:
- Around line 307-317: The delete_identity cleanup path must not report success
when clear_messages or clear_trades fails. Update delete_identity to propagate
either cleanup error and stop before identity replacement, ensuring
importAndStore and regenerate cannot proceed; alternatively, make both cleanup
operations atomic in one transaction.

In `@rust/src/db/indexeddb.rs`:
- Around line 243-249: Implement clear_messages to open a read-write IndexedDB
transaction for MESSAGES_STORE and clear its object store, awaiting the
operation and propagating any errors through Result. Leave clear_trades
unchanged as a no-op.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 4af5cd31-2de4-4bfc-b866-8b97070cbe53

📥 Commits

Reviewing files that changed from the base of the PR and between 7625b24 and 9aca693.

📒 Files selected for processing (6)
  • lib/features/account/screens/account_screen.dart
  • rust/src/api/identity.rs
  • rust/src/db/indexeddb.rs
  • rust/src/db/mod.rs
  • rust/src/db/sqlite.rs
  • rust/src/mostro/session.rs

Comment thread lib/features/account/screens/account_screen.dart
Comment thread rust/src/api/identity.rs Outdated
Comment thread rust/src/db/indexeddb.rs
…web messages, test invalidation

- delete_identity() now propagates clear_messages/clear_trades errors instead of
  logging and returning Ok. These tables are not identity-scoped and have no
  reconcile fallback, so a silent failure would leak the previous identity's
  history; propagating aborts regenerate/importAndStore before the replacement
  identity is created (deleteIdentity runs before the new identity exists).
- IndexedDB clear_messages now clears MESSAGES_STORE in a read-write transaction
  rather than no-op'ing: messages are persisted on web (save_message), so
  identity deletion must actually wipe them. clear_trades stays a no-op (no web
  trades store yet, MostroP2P#233).
- Add a provider test: invalidating rawTradesProvider after the DB is cleared
  yields an empty list (the cache reset the account screen relies on).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(account): "Generate new user" keeps the previous identity's trades and chats

1 participant