Skip to content

Bots: Channel::history reads the store after a restart - #116

Open
TheSeydiCharyyev wants to merge 1 commit into
VectorPrivacy:masterfrom
TheSeydiCharyyev:fix/sdk-history-after-restart
Open

TheSeydiCharyyev wants to merge 1 commit into
VectorPrivacy:masterfrom
TheSeydiCharyyev:fix/sdk-history-after-restart

Conversation

@TheSeydiCharyyev

Copy link
Copy Markdown

A bot's Channel::history and Channel::history_before now return the messages it stored before a restart. Closes #115.

Change

  • db::events::get_messages_before (new): up to limit stored messages strictly before an (at_ms, id) cursor, in the same order as STATE's paging. SQL works in whole seconds, so the query reads every row of each second it reaches, and the exact (at, id) order and the cursor check are done in Rust. The cursor is still compared by value, so it works after its message is deleted.
  • VectorCore::get_messages_before: reads STATE as before, then the store, merges by id and keeps the newest limit. STATE's copy of a message wins, because it can hold a message that is applied but not saved yet.

Test

history_reads_the_store_after_a_restart in vector-core/src/community/v2/service.rs:

  • ingests four messages with v2_ingest_chat_page: two in the same millisecond, one later in that second, one 5 s before;
  • restarts: swap_to the same actor again, so a new session with an empty STATE opens the same database;
  • reads one page of 10, a walk with limit 1 by cursor, and a walk after the same page is ingested again. All three must give the four messages in (at, id) order.

Without the fix, all three are [], while get_message still finds the rows. Three broken versions of the new query also fail the test: reading only the newest second, skipping the cursor's own second, and dropping the cursor check.

Verified at 4c865db + this commit, on Windows:

  • cd crates && cargo test -p vector-core: 2934 passed, 4 failed; integration tests 5 passed, doc tests 5 passed. The 4 are relay and SOCKS tests (a_relay_never_reached_leaves_the_read_of_the_others_complete, heal_waits_out_the_backoff_of_a_recent_close, a_live_subscription_a_relay_closes_is_sent_again, bridge_socks_protocol). They also fail on this machine at 4c865db without this change.
  • cargo test -p vector-core --lib -- --ignored guarded_key: 6 passed, 0 failed.

Not covered

  • VectorCore::get_messages (offset paging) still reads only STATE, and the SDK README sends bots to it for older history ("use bot.core().get_messages(...)"). I can change it the same way, or point that README line to channel.history(), if you want.
  • DMs use the same get_messages_before, so they read the store too now. The test covers a Community channel only.
  • The store's rows are not put into STATE, so a long walk does not grow memory.

STATE holds only a window, and a new process starts with none of it, so
history returned nothing a bot had stored before a restart, and a sync
page could not bring it back. get_messages_before now reads the store
too, with a cursor query in db/events.rs.

Closes VectorPrivacy#115
@TheSeydiCharyyev

Copy link
Copy Markdown
Author

The red vector-core job is one test: control_sync::a_relay_that_caps_open_sessions_is_never_written_off_and_every_plane_still_compares (24 parallel NIP-77 sessions to a local relay). This PR does not touch that code. It passed 15 of 15 runs here at 4794efa, and Tests on master at 4c865db is green, so it looks flaky. A re-run should show it.

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.

vector-sdk: Channel::history is empty after a bot restart

1 participant