@@ -49,6 +49,7 @@ import { yieldToHost } from "./yield";
4949import { locate , SEEK_PAGE , type SeekRead } from "./history-seek" ;
5050import type { WindowPage } from "./window" ;
5151import { MatchRank , matchRank } from "../search/match" ;
52+ import { RESUME_READ_AGE } from "./live" ;
5253
5354type Listener = ( ) => void ;
5455type WindowState = {
@@ -87,15 +88,13 @@ type WindowState = {
8788 * gap, not overlap with it. */
8889 kept ?: Set < string > | undefined ;
8990 /** The window was verified when the stream dropped and `kept` was taken,
90- * and a head read started at most `RESUME_READ_AGE` before: its rows reached
91+ * and its `readAt` was at most `RESUME_READ_AGE` before: its rows reached
9192 * the live stream then. A continuous replay of the channel closes the gap
9293 * from them, so `resume` needs no read. */
9394 keptLive ?: boolean | undefined ;
94- /** `resume` verified the window with no read. `kept` still holds the rows
95- * of the drop, so a later report on this connection that the channel was
96- * not continuous (a grant the broker learned of after its replay) catches up
97- * from them; the next drop takes a new snapshot. */
98- resumed ?: boolean | undefined ;
95+ /** When the head read whose rows the window holds started. A head read the
96+ * window never took (a prefetch, a cancelled catch-up) does not count. */
97+ readAt ?: number | undefined ;
9998 /** Relay membership notices, read apart from the pages (see `fillNotices`). */
10099 notices : Map < string , RelayEvent > ;
101100 /** Where the next older notice read starts: null once a read reached the
@@ -111,6 +110,8 @@ type Head = {
111110 hasMore : boolean ;
112111 events : readonly RelayEvent [ ] ;
113112 savedAt : number ;
113+ /** When the relay read that produced it started; none for a saved head. */
114+ readAt ?: number ;
114115 cached : boolean ;
115116 restored ?: true ;
116117} ;
@@ -149,15 +150,6 @@ const sameKeys = (a?: readonly string[], b?: readonly string[]) =>
149150 ( a ?? [ ] ) . every ( ( key , index ) => key === b ?. [ index ] ) ) ;
150151/** Relay page size, separate from discovery's retained-entry budget. */
151152const DISCOVERY_LIMIT = 500 ;
152- /** A continuous replay starts `LIVE_RESUME_LAG` (16 minutes) before the drop,
153- * so it closes the gap only back to then. A live frame the relay dropped
154- * earlier (a full send buffer drops it without a notice) is not in it; only a
155- * head read after that frame brings it back. The live batch refresh reads each
156- * caught-up head about every 15 minutes. So a window resumes with no read only
157- * when a head read started at most this long before the drop; any other
158- * window catches up. The minute under the lag absorbs read latency and clock
159- * skew. */
160- const RESUME_READ_AGE = 15 * 60_000 ;
161153/** Channels one creator-proof filter names. Each has one creation, so a limit
162154 * of twice this, the relay's maximum, leaves room for strays; a filter that
163155 * reaches it proves only the channels whose creation arrived. */
@@ -317,8 +309,6 @@ export function createChannelStore(
317309 // Full cache clears. Profiles outlive access purges (which move `epoch`),
318310 // so only a clear or disposal fences a profile restore.
319311 let cleared = 0 ;
320- /** When the latest head read of each channel started (see `RESUME_READ_AGE`). */
321- const headReads = new Map < string , number > ( ) ;
322312 let listAgain = false ;
323313 // Epoch of the latest access revocation. When that revocation is what made a
324314 // full read stale, it interrupted rather than failed it: a pass is still owed.
@@ -744,6 +734,7 @@ export function createChannelStore(
744734 moveCursor ( state , head . cursor ) ;
745735 state . atHead = true ;
746736 state . restored = head . restored === true ;
737+ state . readAt = head . readAt ;
747738 if ( dropped ) {
748739 state . kept = rowIds ( state ) ;
749740 state . keptLive = false ;
@@ -932,11 +923,9 @@ export function createChannelStore(
932923 } ) ;
933924 // Head reads belong to session recovery; only manual pagination needs a
934925 // finite retry owner here.
935- const startedAt = now ( ) ;
936926 const events = cursor
937927 ? await retryRead ( read , settings . signal ?? new AbortController ( ) . signal )
938928 : await read ( ) ;
939- if ( ! cursor ) headReads . set ( channelId , startedAt ) ;
940929 return {
941930 events,
942931 page : parseWindow ( channelId , cursor , transport . relayAuthor , events ) ,
@@ -976,6 +965,7 @@ export function createChannelStore(
976965 hasMore : page . hasMore ,
977966 events,
978967 savedAt : now ( ) ,
968+ readAt : startedAt ,
979969 cached : false ,
980970 } ;
981971 if (
@@ -4162,13 +4152,11 @@ export function createChannelStore(
41624152 for ( const [ id , head ] of heads . entries ( ) )
41634153 heads . set ( id , { ...head , cached : true } ) ;
41644154 for ( const state of windows . values ( ) ) {
4165- if ( ! state . kept || state . resumed ) {
4166- state . resumed = undefined ;
4155+ if ( ! state . kept ) {
41674156 state . kept = rowIds ( state ) ;
41684157 state . keptLive =
41694158 state . snapshot . freshness === "verified" &&
4170- now ( ) - ( headReads . get ( state . channelId ) ?? - Infinity ) <=
4171- RESUME_READ_AGE ;
4159+ now ( ) - ( state . readAt ?? - Infinity ) <= RESUME_READ_AGE ;
41724160 }
41734161 setWindow ( state , {
41744162 freshness : "cached" ,
@@ -4225,7 +4213,6 @@ export function createChannelStore(
42254213 ) ;
42264214 for ( const id of kept ) retained . traffic . delete ( id ) ;
42274215 retained . kept = keptAfter ( ) ;
4228- retained . resumed = undefined ;
42294216 retained . floor = undefined ;
42304217 retained . atHead = true ;
42314218 moveCursor ( retained , last . cursor ) ;
@@ -4243,6 +4230,7 @@ export function createChannelStore(
42434230 if ( ! valid ( ) )
42444231 throw new DOMException ( "Stale live catch-up" , "AbortError" ) ;
42454232 retained . restored = false ;
4233+ retained . readAt = head . readAt ;
42464234 accept ( head . events ) ;
42474235 page = head ;
42484236 const reached = ( last : Page ) =>
@@ -4271,7 +4259,6 @@ export function createChannelStore(
42714259 throw new DOMException ( "Stale live catch-up" , "AbortError" ) ;
42724260 if ( retained . atHead ) moveCursor ( retained , page . cursor ) ;
42734261 retained . kept = keptAfter ( ) ;
4274- retained . resumed = undefined ;
42754262 // Live replay covers minutes; notices from a longer drop come by read.
42764263 void fillNotices ( retained , true ) ;
42774264 // A drop mid-fill has marked the window cached; the stream is retrying.
@@ -4328,8 +4315,8 @@ export function createChannelStore(
43284315 dropped = false ;
43294316 const head = heads . peek ( channelId ) ;
43304317 if ( head ) heads . set ( channelId , { ...head , cached : true } ) ;
4318+ state . kept = undefined ;
43314319 state . keptLive = undefined ;
4332- state . resumed = true ;
43334320 setWindow ( state , { freshness : "verified" , error : undefined } ) ;
43344321 // The replay holds only the outage's notices: finish a notice read that
43354322 // failed or was cut before the drop. It reads nothing when none is owed.
@@ -4366,11 +4353,15 @@ export function createChannelStore(
43664353 if ( head ) heads . set ( channelId , { ...head , cached : true } ) ;
43674354 const state = windows . get ( channelId ) ;
43684355 if ( state ) {
4369- // This establishment's replay may stop short of the kept rows. A
4370- // resumed window owes a catch-up again: a drop before it settles keeps
4371- // the rows of the first drop.
4356+ // This establishment's replay may stop short of the kept rows. A window
4357+ // with none (resumed with no read, or not dropped) takes the rows it
4358+ // holds now: they reached the stream, and rows it delivers from here on
4359+ // are newer than any gap. While the relay leaves a channel out (access
4360+ // ended, and the grant reached the broker after its replay), the stream
4361+ // delivers none for it, so these are still the rows of the drop. A drop
4362+ // before the catch-up settles keeps them.
4363+ state . kept ??= rowIds ( state ) ;
43724364 state . keptLive = false ;
4373- state . resumed = undefined ;
43744365 setWindow ( state , {
43754366 freshness : "cached" ,
43764367 error : undefined ,
0 commit comments