Skip to content

Commit 9127a1b

Browse files
baxenmurderbotclaude
committed
fix(read-state): root-first thread mentions, follow dormancy, humans only
Three fixes from the unread load study: - THREAD_MENTIONS starts from the thread's unread replies and probes the mention key for each, instead of walking the actor's mentions across the channel and filtering by root. Same counts (pinned against the old query in a test), no new index; it was ~92% of the heaviest sidebar. - Automatic thread follows go dormant after BUZZ_V1_RETENTION_SECONDS (30 days) without the actor's involvement: posting, being mentioned, a reply to their root or in their DM, marking it read, or following it. A dormant follow is neither counted nor listed but keeps its row and position, and any involvement revives it. Follow intents set `explicit`, which never goes dormant. 0061 adds personal_read_frontiers.explicit, .active_at and idx_personal_read_frontiers_active (actor's threads by involvement, for a later history view). This replaces the author-time unread horizon: author time no longer filters unread or mentions. - Agent accounts (users.agent_owner_pubkey set) keep no read state: ingest writes them no positions or follows, and /buzz/v1 answers 403. Their messages still make people follow. 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 4da2b9f commit 9127a1b

15 files changed

Lines changed: 697 additions & 143 deletions

File tree

‎crates/buzz-db/src/runtime/migration.rs‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2258,16 +2258,25 @@ mod postgres_tests {
22582258
.sql
22592259
.as_str();
22602260
assert!(through_message.contains("DROP COLUMN threads_through_timestamp"));
2261-
// 0061 adds the sticky thread unfollow.
2261+
// 0061 adds the sticky thread unfollow and follow liveness.
22622262
let unfollowed_column = "unfollowed boolean not null default false \
2263-
check (not unfollowed or root_id <> ''::bytea), ";
2264-
assert!(MIGRATOR
2263+
check (not unfollowed or root_id <> ''::bytea), \
2264+
explicit boolean not null default false, \
2265+
active_at timestamptz not null default now(), ";
2266+
let access_paths = MIGRATOR
22652267
.iter()
22662268
.find(|m| m.version == 61)
22672269
.expect("read-state access path migration")
22682270
.sql
2269-
.as_str()
2270-
.contains("$d$BOOLEAN NOT NULL DEFAULT false CHECK (NOT unfollowed OR root_id <> ''::bytea)$d$"));
2271+
.as_str();
2272+
assert!(access_paths.contains(
2273+
"$d$BOOLEAN NOT NULL DEFAULT false CHECK (NOT unfollowed OR root_id <> ''::bytea)$d$"
2274+
));
2275+
assert!(access_paths
2276+
.contains("('personal_read_frontiers', 'explicit', 'BOOLEAN NOT NULL DEFAULT false')"));
2277+
assert!(access_paths.contains(
2278+
"('personal_read_frontiers', 'active_at', 'TIMESTAMPTZ NOT NULL DEFAULT now()')"
2279+
));
22712280
for (table, definition) in personal.tables {
22722281
let in_schema = schema.tables.get(&table).map(|schema_definition| {
22732282
if table == "personal_read_accounts" {

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

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ pub(crate) fn place<'a>(
8080
/// Everyone but the author starts just before the message, so it is unread
8181
/// to them. A follower keeps an existing position. An unfollow sticks until
8282
/// its actor posts in the thread or is mentioned there again.
83+
///
84+
/// Each of these involves its actor in the thread (`active_at`), which keeps
85+
/// an implicit follow live. Agent accounts keep no read state.
8386
pub(crate) async fn record_message(
8487
tx: &mut AdmittedTx,
8588
event: &Event,
@@ -129,16 +132,23 @@ pub(crate) async fn record_message(
129132
WHERE $9 AND cm.community_id=$1 AND cm.channel_id=$2 AND cm.removed_at IS NULL
130133
AND c.channel_type='dm' AND cm.pubkey<>$4
131134
), rows AS (
132-
-- An existing follower keeps their position, so is not touched;
133-
-- nor is an unfollow, unless this message re-follows (the author
134-
-- posting, or a mention). Existence probes are LIMIT 1 laterals:
135-
-- always one index lookup.
135+
-- An existing follower keeps their position, so is only touched
136+
-- to keep the follow live, at most daily rather than per message;
137+
-- an unfollow is not touched unless this message re-follows (the
138+
-- author posting, or a mention). Existence probes are LIMIT 1
139+
-- laterals: always one index lookup.
136140
SELECT DISTINCT ON (i.actor, i.root_id) i.* FROM involved i
137141
LEFT JOIN LATERAL (
138-
SELECT f.unfollowed FROM personal_read_frontiers f WHERE f.community_id=$1
139-
AND f.actor=i.actor AND f.channel_id=$2 AND f.root_id=i.root_id LIMIT 1
142+
SELECT f.unfollowed, f.active_at FROM personal_read_frontiers f
143+
WHERE f.community_id=$1 AND f.actor=i.actor AND f.channel_id=$2
144+
AND f.root_id=i.root_id LIMIT 1
140145
) f ON true
141-
WHERE i.message IS NOT NULL OR f.unfollowed IS NULL OR (i.refollow AND f.unfollowed)
146+
WHERE NOT EXISTS (SELECT 1 FROM users u WHERE u.community_id=$1
147+
AND u.pubkey=i.actor AND u.agent_owner_pubkey IS NOT NULL)
148+
AND (i.message IS NOT NULL OR f.unfollowed IS NULL
149+
OR (i.refollow AND f.unfollowed)
150+
OR (i.root_id<>''::bytea AND NOT f.unfollowed
151+
AND f.active_at < now()-interval '1 day'))
142152
ORDER BY i.actor, i.root_id, i.message IS NULL, NOT i.refollow
143153
), accounts AS (
144154
INSERT INTO personal_read_accounts (community_id, actor)
@@ -152,21 +162,29 @@ pub(crate) async fn record_message(
152162
)
153163
-- A re-follow starts just before this message, like a new follow,
154164
-- unless the unfollow is later. A row that a concurrent message
155-
-- created is only advanced when it carries an anchor.
165+
-- created is only advanced when it carries an anchor; one kept live
166+
-- keeps its position.
156167
INSERT INTO personal_read_frontiers
157168
(community_id, actor, channel_id, root_id, through_timestamp, through_message_id)
158169
SELECT $1, actor, $2, root_id, through, message
159170
FROM rows CROSS JOIN (SELECT count(*) FROM accounts) written
160171
ORDER BY actor, root_id
161172
ON CONFLICT (community_id, actor, channel_id, root_id) DO UPDATE
162-
SET through_timestamp=GREATEST(personal_read_frontiers.through_timestamp,
163-
excluded.through_timestamp),
173+
SET through_timestamp=CASE
174+
WHEN excluded.through_message_id IS NOT NULL OR personal_read_frontiers.unfollowed
175+
THEN GREATEST(personal_read_frontiers.through_timestamp, excluded.through_timestamp)
176+
ELSE personal_read_frontiers.through_timestamp END,
164177
through_message_id=CASE
165-
WHEN excluded.through_timestamp > personal_read_frontiers.through_timestamp
178+
WHEN (excluded.through_message_id IS NOT NULL OR personal_read_frontiers.unfollowed)
179+
AND excluded.through_timestamp > personal_read_frontiers.through_timestamp
166180
THEN excluded.through_message_id ELSE personal_read_frontiers.through_message_id END,
167-
unfollowed=false
181+
unfollowed=false,
182+
active_at=CASE WHEN excluded.root_id<>''::bytea THEN now()
183+
ELSE personal_read_frontiers.active_at END
168184
WHERE excluded.through_message_id IS NOT NULL OR (personal_read_frontiers.unfollowed
169-
AND excluded.through_timestamp > personal_read_frontiers.through_timestamp)",
185+
AND excluded.through_timestamp > personal_read_frontiers.through_timestamp)
186+
OR (excluded.root_id<>''::bytea AND NOT personal_read_frontiers.unfollowed
187+
AND personal_read_frontiers.active_at < now()-interval '1 day')",
170188
)
171189
.bind(community.as_uuid())
172190
.bind(channel)

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

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,3 +645,224 @@ async fn the_threads_view_lists_follows_newest_arrival_first() {
645645
.threads
646646
.is_empty());
647647
}
648+
649+
/// Put every one of the actor's follows past the default window.
650+
async fn go_dormant(pool: &PgPool, community: CommunityId, actor: &Keys) {
651+
sqlx::query(
652+
"UPDATE personal_read_frontiers SET active_at=now()-interval '31 days'
653+
WHERE community_id=$1 AND actor=$2",
654+
)
655+
.bind(community.as_uuid())
656+
.bind(actor.public_key().to_bytes().as_slice())
657+
.execute(pool)
658+
.await
659+
.unwrap();
660+
}
661+
662+
/// Roots of the threads listed for the actor, by the sidebar and by the
663+
/// threads view.
664+
async fn listed(db: &Db, community: CommunityId, channel: Uuid, actor: &Keys) -> [Vec<String>; 2] {
665+
let sidebar = row(db, community, channel, actor)
666+
.await
667+
.threads
668+
.into_iter()
669+
.map(|t| t.root_id)
670+
.collect();
671+
let view = threads_page(db, community, actor, MAX_THREADS, None)
672+
.await
673+
.threads
674+
.into_iter()
675+
.map(|t| t.thread.root_id)
676+
.collect();
677+
[sidebar, view]
678+
}
679+
680+
#[tokio::test]
681+
#[ignore = "requires Postgres"]
682+
async fn a_dormant_follow_is_hidden_until_a_mention_or_post_revives_it() {
683+
let (db, pool, community, channel, actor, root) = fixture().await;
684+
let id = root.id.to_hex();
685+
let own = reply(&db, community, channel, &root, &actor, vec![]).await;
686+
reply(&db, community, channel, &root, &Keys::generate(), vec![]).await;
687+
assert_eq!(
688+
listed(&db, community, channel, &actor).await,
689+
[vec![id.clone()], vec![id.clone()]]
690+
);
691+
692+
go_dormant(&pool, community, &actor).await;
693+
// A reply that does not involve the actor leaves it dormant; the row and
694+
// its position stay.
695+
reply(&db, community, channel, &root, &Keys::generate(), vec![]).await;
696+
assert_eq!(
697+
listed(&db, community, channel, &actor).await,
698+
[Vec::<String>::new(), vec![]]
699+
);
700+
assert_eq!(
701+
follow(&pool, community, &actor, &root).await,
702+
Some(Some(own.id.to_hex()))
703+
);
704+
705+
// A mention revives it at its old position.
706+
reply(
707+
&db,
708+
community,
709+
channel,
710+
&root,
711+
&Keys::generate(),
712+
mention(&actor),
713+
)
714+
.await;
715+
let summary = row(&db, community, channel, &actor).await;
716+
assert_eq!(threads(&summary), [(id.clone(), 1)]);
717+
assert_eq!(summary.threads[0].read_through_id, Some(own.id.to_hex()));
718+
719+
// So does posting, which reads through the post.
720+
go_dormant(&pool, community, &actor).await;
721+
assert!(row(&db, community, channel, &actor)
722+
.await
723+
.threads
724+
.is_empty());
725+
let again = reply(&db, community, channel, &root, &actor, vec![]).await;
726+
reply(&db, community, channel, &root, &Keys::generate(), vec![]).await;
727+
let summary = row(&db, community, channel, &actor).await;
728+
assert_eq!(threads(&summary), [(id.clone(), 0)]);
729+
assert_eq!(summary.threads[0].read_through_id, Some(again.id.to_hex()));
730+
731+
// And reading it.
732+
go_dormant(&pool, community, &actor).await;
733+
let last = reply(&db, community, channel, &root, &Keys::generate(), vec![]).await;
734+
assert!(row(&db, community, channel, &actor)
735+
.await
736+
.threads
737+
.is_empty());
738+
assert_eq!(
739+
intent(
740+
&db,
741+
community,
742+
&actor,
743+
mark_thread(channel, &root, &last.id.to_hex())
744+
)
745+
.await,
746+
IntentOutcome::Applied
747+
);
748+
reply(&db, community, channel, &root, &Keys::generate(), vec![]).await;
749+
assert_eq!(
750+
listed(&db, community, channel, &actor).await,
751+
[vec![id.clone()], vec![id]]
752+
);
753+
}
754+
755+
#[tokio::test]
756+
#[ignore = "requires Postgres"]
757+
async fn an_explicit_follow_never_goes_dormant() {
758+
let (db, pool, community, channel, actor, _) = fixture().await;
759+
// One thread followed only by intent, one followed implicitly first.
760+
let chosen = post(&db, community, channel, &Keys::generate(), vec![]).await;
761+
let joined = post(&db, community, channel, &Keys::generate(), vec![]).await;
762+
reply(&db, community, channel, &joined, &actor, vec![]).await;
763+
for root in [&chosen, &joined] {
764+
assert_eq!(
765+
intent(
766+
&db,
767+
community,
768+
&actor,
769+
follow_intent(channel, Some(root), true)
770+
)
771+
.await,
772+
IntentOutcome::Applied
773+
);
774+
}
775+
let implicit = post(&db, community, channel, &Keys::generate(), vec![]).await;
776+
reply(&db, community, channel, &implicit, &actor, vec![]).await;
777+
for root in [&chosen, &joined, &implicit] {
778+
reply(&db, community, channel, root, &Keys::generate(), vec![]).await;
779+
}
780+
go_dormant(&pool, community, &actor).await;
781+
let mut explicit = vec![chosen.id.to_hex(), joined.id.to_hex()];
782+
explicit.sort();
783+
let [mut sidebar, mut view] = listed(&db, community, channel, &actor).await;
784+
sidebar.sort();
785+
view.sort();
786+
assert_eq!([sidebar, view], [explicit.clone(), explicit]);
787+
788+
// Unfollowing ends it; a later mention re-follows it implicitly, so it
789+
// can go dormant again.
790+
intent(
791+
&db,
792+
community,
793+
&actor,
794+
follow_intent(channel, Some(&chosen), false),
795+
)
796+
.await;
797+
reply(
798+
&db,
799+
community,
800+
channel,
801+
&chosen,
802+
&Keys::generate(),
803+
mention(&actor),
804+
)
805+
.await;
806+
assert_eq!(
807+
threads(&row(&db, community, channel, &actor).await).len(),
808+
2
809+
);
810+
go_dormant(&pool, community, &actor).await;
811+
assert_eq!(
812+
listed(&db, community, channel, &actor).await,
813+
[vec![joined.id.to_hex()], vec![joined.id.to_hex()]]
814+
);
815+
}
816+
817+
#[tokio::test]
818+
#[ignore = "requires Postgres"]
819+
async fn an_agent_keeps_no_read_state() {
820+
let (db, pool, community, channel, actor, _) = fixture().await;
821+
let agent = member(&db, &pool, community, channel).await;
822+
sqlx::query("DELETE FROM personal_read_accounts WHERE community_id=$1 AND actor=$2")
823+
.bind(community.as_uuid())
824+
.bind(agent.public_key().to_bytes().as_slice())
825+
.execute(&pool)
826+
.await
827+
.unwrap();
828+
for (who, owner) in [(&actor, None), (&agent, Some(&actor))] {
829+
sqlx::query(
830+
"INSERT INTO users (community_id, pubkey, agent_owner_pubkey) VALUES ($1,$2,$3)",
831+
)
832+
.bind(community.as_uuid())
833+
.bind(who.public_key().to_bytes().as_slice())
834+
.bind(owner.map(|o| o.public_key().to_bytes().to_vec()))
835+
.execute(&pool)
836+
.await
837+
.unwrap();
838+
}
839+
// The agent posts, replies to its owner's root, is mentioned, and has a
840+
// root replied to: every way a person would gain a position or a follow.
841+
let own_root = post(&db, community, channel, &agent, mention(&actor)).await;
842+
reply(&db, community, channel, &own_root, &actor, mention(&agent)).await;
843+
let owner_root = post(&db, community, channel, &actor, mention(&agent)).await;
844+
reply(
845+
&db,
846+
community,
847+
channel,
848+
&owner_root,
849+
&agent,
850+
mention(&actor),
851+
)
852+
.await;
853+
let rows: i64 = sqlx::query_scalar(
854+
"SELECT (SELECT count(*) FROM personal_read_frontiers WHERE community_id=$1 AND actor=$2)
855+
+ (SELECT count(*) FROM personal_read_accounts WHERE community_id=$1 AND actor=$2)",
856+
)
857+
.bind(community.as_uuid())
858+
.bind(agent.public_key().to_bytes().as_slice())
859+
.fetch_one(&pool)
860+
.await
861+
.unwrap();
862+
assert_eq!(rows, 0, "an agent has no positions, follows or account");
863+
// The agent's messages still involve its owner.
864+
assert!(follow(&pool, community, &actor, &own_root).await.is_some());
865+
assert!(follow(&pool, community, &actor, &owner_root)
866+
.await
867+
.is_some());
868+
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
//! Private, signer-owned accessory read progress, separate from NIP-RS events.
22
//!
33
//! A frontier is the relay arrival time of the message a context was read
4-
//! through; the unread horizon alone uses author time. Read intents and the
5-
//! author's own posts advance frontiers. A thread frontier exists only for a
6-
//! followed thread, and the sidebar counts forward from each frontier.
4+
//! through. Read intents and the author's own posts advance frontiers. A
5+
//! thread frontier exists only for a followed thread, and the sidebar counts
6+
//! forward from each frontier. An implicit follow goes dormant after the
7+
//! relay's window without the actor's involvement; an explicit one never does.
8+
//! Agent accounts keep no read state.
79
810
mod follows;
911
mod model;

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use uuid::Uuid;
44

55
/// Maximum independent operations in one HTTP request.
66
pub const MAX_INTENTS: usize = 100;
7-
/// Default unread-tracking duration, not event or encrypted NIP-RS retention.
7+
/// Default time an implicit thread follow stays live without the actor's
8+
/// involvement; not event or encrypted NIP-RS retention.
89
pub const DEFAULT_RETENTION_SECONDS: u32 = 30 * 24 * 60 * 60;
910

1011
/// A channel or canonical thread; absence of a root denotes only the channel timeline.
@@ -68,7 +69,8 @@ pub enum IntentOutcome {
6869
/// The tracking boundary for the authenticated account. Not on the wire.
6970
#[derive(Clone, Debug)]
7071
pub(super) struct ReadAccount {
71-
/// Read-time author-time cutoff (Unix milliseconds), not a discard boundary.
72+
/// Implicit follows not involved since this instant (Unix milliseconds) are
73+
/// dormant; nothing is discarded.
7274
pub(super) cutoff_ms: i64,
7375
/// Arrival floor of every frontier: the account's first applied read
7476
/// intent. `None` until then, and nothing counts as unread.

0 commit comments

Comments
 (0)