@@ -1616,265 +1616,6 @@ impl Db {
16161616 dm:: list_hidden_dms ( & self . pool , community_id, pubkey) . await
16171617 }
16181618
1619- /// Insert thread metadata.
1620- #[ allow( clippy:: too_many_arguments) ]
1621- #[ datastore_span( name = "insert_thread_metadata" , system = "postgresql" ) ]
1622- pub async fn insert_thread_metadata (
1623- & self ,
1624- community_id : CommunityId ,
1625- event_id : & [ u8 ] ,
1626- event_created_at : DateTime < Utc > ,
1627- channel_id : Uuid ,
1628- parent_event_id : Option < & [ u8 ] > ,
1629- parent_event_created_at : Option < DateTime < Utc > > ,
1630- root_event_id : Option < & [ u8 ] > ,
1631- root_event_created_at : Option < DateTime < Utc > > ,
1632- depth : i32 ,
1633- broadcast : bool ,
1634- ) -> Result < ( ) > {
1635- thread:: insert_thread_metadata (
1636- & self . pool ,
1637- community_id,
1638- event_id,
1639- event_created_at,
1640- channel_id,
1641- parent_event_id,
1642- parent_event_created_at,
1643- root_event_id,
1644- root_event_created_at,
1645- depth,
1646- broadcast,
1647- )
1648- . await
1649- }
1650-
1651- /// Fetch replies under a root event.
1652- ///
1653- /// Routing mirrors [`Db::get_channel_window_with_session`]: a head
1654- /// fetch (`cursor: None`) is Predicate A (bounded staleness, gated by
1655- /// the default-off head budget); cursor pages are Predicate B
1656- /// (completeness). Thread pagination walks **forward** from oldest to
1657- /// newest, so a cursor carries no upper bound — instead the served page
1658- /// is post-verified against the wall the serving session proved:
1659- ///
1660- /// - an under-`limit` page is a candidate terminal page — the client
1661- /// treats it as EOF, so it is re-run on the writer to keep the EOF
1662- /// decision authoritative (a lagged replica could truncate the tail);
1663- /// - a full page whose newest row exceeds the proved fence wall could
1664- /// straddle a row the session has not replayed (commit order is not
1665- /// `created_at` order), so it is also re-run on the writer. Only a
1666- /// full page that sits entirely at or below the proved wall is served
1667- /// from the replica.
1668- ///
1669- /// A head fetch routed under Predicate A skips the re-run: bounded
1670- /// staleness (missing at most the freshest budget-window of replies) is
1671- /// exactly the semantic the head gate accepts.
1672- #[ datastore_span( name = "get_thread_replies" , system = "postgresql" ) ]
1673- pub async fn get_thread_replies (
1674- & self ,
1675- community_id : CommunityId ,
1676- root_event_id : & [ u8 ] ,
1677- depth_limit : Option < u32 > ,
1678- limit : u32 ,
1679- cursor : Option < & [ u8 ] > ,
1680- ) -> Result < Vec < thread:: ThreadReply > > {
1681- let ( path, predicate) : ( & ' static str , RoutePredicate ) = match cursor {
1682- Some ( _) => (
1683- "thread_cursor" ,
1684- RoutePredicate :: CoveredPostVerified {
1685- proof : ChannelScoped :: from_thread_metadata_join ( ) ,
1686- } ,
1687- ) ,
1688- None => ( "thread_head" , RoutePredicate :: Bounded ) ,
1689- } ;
1690- if let RouteDecision :: Replica ( mut tx, entry, reason) =
1691- self . route_read ( path, predicate) . await
1692- {
1693- match thread:: get_thread_replies_on (
1694- & mut tx,
1695- community_id,
1696- root_event_id,
1697- depth_limit,
1698- limit,
1699- cursor,
1700- )
1701- . await
1702- {
1703- Ok ( replies) => {
1704- if cursor. is_none ( ) {
1705- // Predicate A: bounded-stale head page, served as proved.
1706- Self :: record_route ( path, "replica" , reason) ;
1707- return Ok ( replies) ;
1708- }
1709- let full = replies. len ( ) >= limit as usize ;
1710- let below_fence = replies
1711- . last ( )
1712- . is_some_and ( |tail| tail. created_at <= entry. fence_wall ) ;
1713- if full && below_fence {
1714- Self :: record_route ( path, "replica" , reason) ;
1715- return Ok ( replies) ;
1716- }
1717- // Candidate terminal page, or page reaching above the
1718- // proved wall — verify against the writer. Recorded as
1719- // the request's ONLY route event: the replica leg was
1720- // discarded, so counting it would overstate offload.
1721- Self :: record_route ( "thread_eof" , "writer" , "stale" ) ;
1722- }
1723- Err ( e) => {
1724- // Mid-request replica failure (e.g. a hot-standby
1725- // recovery conflict) fails closed to the writer.
1726- tracing:: warn!(
1727- error = %e,
1728- path,
1729- "replica thread query failed; re-running on writer"
1730- ) ;
1731- Self :: record_route ( path, "writer" , "replica_error" ) ;
1732- }
1733- }
1734- }
1735- thread:: get_thread_replies (
1736- & self . pool ,
1737- community_id,
1738- root_event_id,
1739- depth_limit,
1740- limit,
1741- cursor,
1742- )
1743- . await
1744- }
1745-
1746- /// Fetch aggregated thread stats.
1747- #[ datastore_span( name = "get_thread_summary" , system = "postgresql" ) ]
1748- pub async fn get_thread_summary (
1749- & self ,
1750- community_id : CommunityId ,
1751- event_id : & [ u8 ] ,
1752- ) -> Result < Option < thread:: ThreadSummary > > {
1753- thread:: get_thread_summary ( & self . pool , community_id, event_id) . await
1754- }
1755-
1756- /// One channel window: top-level rows + summaries + server `has_more`.
1757- ///
1758- /// Convenience wrapper over [`Db::get_channel_window_with_session`] for
1759- /// callers with no follow-up queries; the serving session is released.
1760- pub async fn get_channel_window (
1761- & self ,
1762- community_id : CommunityId ,
1763- channel_id : Uuid ,
1764- limit : u32 ,
1765- cursor : Option < ( DateTime < Utc > , Vec < u8 > ) > ,
1766- kind_filter : Option < & [ u32 ] > ,
1767- ) -> Result < thread:: ChannelWindow > {
1768- self . get_channel_window_with_session ( community_id, channel_id, limit, cursor, kind_filter)
1769- . await
1770- . map ( |( window, _session) | window)
1771- }
1772-
1773- /// [`Db::get_channel_window`], additionally returning the session that
1774- /// served the page so request-scoped follow-ups (the aux closure) run on
1775- /// the same proved connection.
1776- ///
1777- /// Routing:
1778- ///
1779- /// - **Cursor page** (Predicate B — completeness): scrolls *backward*
1780- /// into history bounded above by the cursor timestamp (`created_at <
1781- /// ts`, or `= ts` with the id tiebreak), so it may be served by a
1782- /// replica session when one is configured AND that session **proves**
1783- /// coverage of the cursor timestamp: the heartbeat token/epoch is
1784- /// observed on the exact connection that will serve the page and
1785- /// resolved against the fence's retained ring ([`replica_fence`]).
1786- /// - **Head fetch** (Predicate A — bounded staleness): served by a
1787- /// proved replica session only when the head gate is configured
1788- /// ([`DbConfig::replica_read_max_age_ms`], default off) and the
1789- /// proved entry is within the budget. This trades a bounded staleness
1790- /// window (budget plus probe cadence) on the GET leg for writer
1791- /// offload. NOTE: enabling the budget also breaks read-your-own-writes
1792- /// on the GET leg; the client-side WS `since`-overlap union intended
1793- /// to cover fresh events has NOT shipped yet — do not enable
1794- /// `BUZZ_REPLICA_HEAD_MAX_AGE_SECS` until it has, proven by a
1795- /// post-then-immediately-refetch test.
1796- ///
1797- /// Every failure fails closed to the writer and is recorded in
1798- /// `buzz_db_route_decision`.
1799- #[ datastore_span( name = "get_channel_window" , system = "postgresql" ) ]
1800- pub async fn get_channel_window_with_session (
1801- & self ,
1802- community_id : CommunityId ,
1803- channel_id : Uuid ,
1804- limit : u32 ,
1805- cursor : Option < ( DateTime < Utc > , Vec < u8 > ) > ,
1806- kind_filter : Option < & [ u32 ] > ,
1807- ) -> Result < ( thread:: ChannelWindow , ReadSession ) > {
1808- let path: & ' static str = if cursor. is_some ( ) {
1809- "channel_cursor"
1810- } else {
1811- "channel_head"
1812- } ;
1813- match self
1814- . route_read (
1815- path,
1816- RoutePredicate :: from_channel_cursor ( channel_id, & cursor) ,
1817- )
1818- . await
1819- {
1820- RouteDecision :: Replica ( mut tx, _entry, reason) => {
1821- match thread:: get_channel_window_on (
1822- & mut tx,
1823- community_id,
1824- channel_id,
1825- limit,
1826- cursor. clone ( ) ,
1827- kind_filter,
1828- )
1829- . await
1830- {
1831- Ok ( window) => {
1832- Self :: record_route ( path, "replica" , reason) ;
1833- return Ok ( (
1834- window,
1835- ReadSession {
1836- inner : ReadSessionInner :: Replica {
1837- tx,
1838- writer : self . pool . clone ( ) ,
1839- } ,
1840- } ,
1841- ) ) ;
1842- }
1843- Err ( e) => {
1844- // A mid-request replica failure (e.g. a hot-standby
1845- // recovery conflict cancelling the held snapshot)
1846- // fails closed to the writer: a stale-but-served
1847- // page, never an error the writer could have
1848- // answered. Dropping `tx` rolls the reader
1849- // transaction back.
1850- tracing:: warn!(
1851- error = %e,
1852- path,
1853- "replica window query failed; re-running on writer"
1854- ) ;
1855- Self :: record_route ( path, "writer" , "replica_error" ) ;
1856- }
1857- }
1858- }
1859- RouteDecision :: Writer => { }
1860- }
1861- let window = thread:: get_channel_window (
1862- & self . pool ,
1863- community_id,
1864- channel_id,
1865- limit,
1866- cursor,
1867- kind_filter,
1868- )
1869- . await ?;
1870- Ok ( (
1871- window,
1872- ReadSession {
1873- inner : ReadSessionInner :: Writer ( self . pool . clone ( ) ) ,
1874- } ,
1875- ) )
1876- }
1877-
18781619 /// Shared route decision for one read: evaluate the predicate against a
18791620 /// proved reader session and record the decision. Fail closed to the
18801621 /// writer everywhere.
@@ -1961,28 +1702,6 @@ impl Db {
19611702 }
19621703 }
19631704
1964- /// Look up a single thread_metadata row by event_id.
1965- #[ datastore_span( name = "get_thread_metadata_by_event" , system = "postgresql" ) ]
1966- pub async fn get_thread_metadata_by_event (
1967- & self ,
1968- community_id : CommunityId ,
1969- event_id : & [ u8 ] ,
1970- ) -> Result < Option < thread:: ThreadMetadataRecord > > {
1971- thread:: get_thread_metadata_by_event ( & self . pool , community_id, event_id) . await
1972- }
1973-
1974- /// Decrement reply counts.
1975- #[ datastore_span( name = "decrement_reply_count" , system = "postgresql" ) ]
1976- pub async fn decrement_reply_count (
1977- & self ,
1978- community_id : CommunityId ,
1979- parent_event_id : & [ u8 ] ,
1980- root_event_id : Option < & [ u8 ] > ,
1981- ) -> Result < ( ) > {
1982- thread:: decrement_reply_count ( & self . pool , community_id, parent_event_id, root_event_id)
1983- . await
1984- }
1985-
19861705 /// Add (or re-activate) a reaction.
19871706 #[ datastore_span( name = "add_reaction" , system = "postgresql" ) ]
19881707 pub async fn add_reaction (
0 commit comments