Skip to content

Commit f1dfa97

Browse files
baxenmurderbotclaude
committed
fix(read-state): bound the follow refresh interval by the dormancy window
Ingest refreshed a live follow's active_at at most daily, so with a dormancy window shorter than a day a mention could not revive a follow that had gone dormant. The refresh interval is now min(1 day, window/4), from the window the relay sets at startup, so it never exceeds the window. Adds codex00's repro: a 1-hour window, a follow last active 2 hours ago, and a fresh mention revives it in the sidebar and the threads view; it fails against the fixed one-day throttle. Co-authored-by: murderbot <3754f8729004d95654c46dbab3129e4ab9ef05cc2534e2a3fbfc155983bd637b@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Bradley Axen <[email protected]>
1 parent 9127a1b commit f1dfa97

5 files changed

Lines changed: 101 additions & 6 deletions

File tree

‎crates/buzz-db/src/store/personal_read/follows.rs‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
11
//! Read state that ingest writes. A thread frontier row exists for a thread
22
//! its actor follows or has unfollowed; ingest and follow intents create one.
3-
use super::model::ELIGIBLE_KINDS;
3+
use super::model::{DEFAULT_RETENTION_SECONDS, ELIGIBLE_KINDS};
44
use crate::{AdmittedTx, Result};
55
use chrono::{DateTime, Utc};
66
use nostr::Event;
7+
use std::sync::atomic::{AtomicU32, Ordering};
78
use uuid::Uuid;
89

10+
/// The relay's follow dormancy window, in seconds (`BUZZ_V1_RETENTION_SECONDS`).
11+
static FOLLOW_WINDOW_SECONDS: AtomicU32 = AtomicU32::new(DEFAULT_RETENTION_SECONDS);
12+
13+
/// Set the follow dormancy window ingest refreshes against: the same window
14+
/// the read projections are given. Process-wide; the relay sets it once at
15+
/// startup.
16+
pub fn set_follow_window_seconds(seconds: u32) {
17+
FOLLOW_WINDOW_SECONDS.store(seconds, Ordering::Relaxed);
18+
}
19+
20+
/// How stale a live follow's involvement may grow before an involvement that
21+
/// does not otherwise write its row refreshes it: a day, or a quarter of the
22+
/// window when that is shorter. Never longer than the window, so such an
23+
/// involvement always revives a dormant follow.
24+
fn refresh_after_seconds() -> f64 {
25+
f64::from((FOLLOW_WINDOW_SECONDS.load(Ordering::Relaxed) / 4).min(24 * 60 * 60))
26+
}
27+
928
/// Where a newly stored channel message sits.
1029
#[derive(Clone, Copy)]
1130
pub(crate) enum Place<'a> {
@@ -133,7 +152,8 @@ pub(crate) async fn record_message(
133152
AND c.channel_type='dm' AND cm.pubkey<>$4
134153
), rows AS (
135154
-- An existing follower keeps their position, so is only touched
136-
-- to keep the follow live, at most daily rather than per message;
155+
-- to keep the follow live, at most once per refresh interval ($12)
156+
-- rather than per message;
137157
-- an unfollow is not touched unless this message re-follows (the
138158
-- author posting, or a mention). Existence probes are LIMIT 1
139159
-- laterals: always one index lookup.
@@ -148,7 +168,7 @@ pub(crate) async fn record_message(
148168
AND (i.message IS NOT NULL OR f.unfollowed IS NULL
149169
OR (i.refollow AND f.unfollowed)
150170
OR (i.root_id<>''::bytea AND NOT f.unfollowed
151-
AND f.active_at < now()-interval '1 day'))
171+
AND f.active_at < now()-make_interval(secs=>$12)))
152172
ORDER BY i.actor, i.root_id, i.message IS NULL, NOT i.refollow
153173
), accounts AS (
154174
INSERT INTO personal_read_accounts (community_id, actor)
@@ -184,7 +204,7 @@ pub(crate) async fn record_message(
184204
WHERE excluded.through_message_id IS NOT NULL OR (personal_read_frontiers.unfollowed
185205
AND excluded.through_timestamp > personal_read_frontiers.through_timestamp)
186206
OR (excluded.root_id<>''::bytea AND NOT personal_read_frontiers.unfollowed
187-
AND personal_read_frontiers.active_at < now()-interval '1 day')",
207+
AND personal_read_frontiers.active_at < now()-make_interval(secs=>$12))",
188208
)
189209
.bind(community.as_uuid())
190210
.bind(channel)
@@ -197,6 +217,7 @@ pub(crate) async fn record_message(
197217
.bind(reply)
198218
.bind(root_created_at)
199219
.bind(broadcast)
220+
.bind(refresh_after_seconds())
200221
.execute(tx.conn())
201222
.await?;
202223
Ok(())

‎crates/buzz-db/src/store/personal_read/follows_postgres_tests.rs‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,3 +866,75 @@ async fn an_agent_keeps_no_read_state() {
866866
.await
867867
.is_some());
868868
}
869+
870+
/// How many threads the sidebar and the threads view list under `window`.
871+
async fn listed_within(
872+
db: &Db,
873+
community: CommunityId,
874+
channel: Uuid,
875+
actor: &Keys,
876+
window: u32,
877+
) -> (usize, usize) {
878+
let sidebar = db
879+
.personal_read_sidebar_channels(community, &actor.public_key(), window, &[channel])
880+
.await
881+
.unwrap()
882+
.channels
883+
.remove(0)
884+
.threads
885+
.len();
886+
let view = db
887+
.personal_read_threads(community, &actor.public_key(), window, MAX_THREADS, None)
888+
.await
889+
.unwrap()
890+
.threads
891+
.len();
892+
(sidebar, view)
893+
}
894+
895+
/// A window shorter than a day still revives on a mention: ingest refreshes
896+
/// against the same window the projections read.
897+
#[tokio::test]
898+
#[ignore = "requires Postgres"]
899+
async fn a_mention_revives_a_follow_under_a_sub_day_window() {
900+
const HOUR: u32 = 60 * 60;
901+
set_follow_window_seconds(HOUR);
902+
let (db, pool, community, channel, actor, root) = fixture().await;
903+
reply(
904+
&db,
905+
community,
906+
channel,
907+
&root,
908+
&Keys::generate(),
909+
mention(&actor),
910+
)
911+
.await;
912+
sqlx::query(
913+
"UPDATE personal_read_frontiers SET active_at=now()-interval '2 hours'
914+
WHERE community_id=$1 AND actor=$2",
915+
)
916+
.bind(community.as_uuid())
917+
.bind(actor.public_key().to_bytes().as_slice())
918+
.execute(&pool)
919+
.await
920+
.unwrap();
921+
assert_eq!(
922+
listed_within(&db, community, channel, &actor, HOUR).await,
923+
(0, 0),
924+
"dormant under the window"
925+
);
926+
reply(
927+
&db,
928+
community,
929+
channel,
930+
&root,
931+
&Keys::generate(),
932+
mention(&actor),
933+
)
934+
.await;
935+
assert_eq!(
936+
listed_within(&db, community, channel, &actor, HOUR).await,
937+
(1, 1),
938+
"a fresh mention revives it"
939+
);
940+
}

‎crates/buzz-db/src/store/personal_read/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ mod model;
1212
mod projection;
1313
mod writes;
1414

15+
pub use follows::set_follow_window_seconds;
1516
pub(crate) use follows::{place, record_message};
1617
pub use model::*;
1718

‎crates/buzz-relay/src/main.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ async fn run_relay_main(boot: BootTracker) -> anyhow::Result<()> {
388388
anyhow::anyhow!("Configuration error: {error}")
389389
})?;
390390
boot = next_boot;
391+
buzz_db::personal_read::set_follow_window_seconds(config.buzz_v1_retention_seconds);
391392

392393
let key_failure = if config.relay_private_key.is_some() {
393394
LifecycleReason::RequiredInvalid

‎docs/buzz-v1-read-state.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ thread is neither counted nor listed, but keeps its row and position, and any
150150
of those involvements makes it live again at that position. Replies that do not
151151
involve you never revive it. A follow made by the `follow` intent never goes
152152
dormant; an unfollow ends that. Being mentioned, a reply to your root and DM
153-
replies refresh a live follow at most daily, so dormancy is measured to the
154-
day.
153+
replies refresh a live follow at most once a day, or once a quarter of the
154+
window when that is shorter, so dormancy is measured to that interval.
155155

156156
## Threads view
157157

0 commit comments

Comments
 (0)