@@ -65,10 +65,16 @@ export type AccountActivity<E extends ActivityEntry> = {
6565
6666/**
6767 * Why an account stopped before reading all its activity: a listing could not page past items
68- * newer than its bound, more entries than a cursor can name sat at one instant, a page made no
69- * progress at all, or the account failed again on its one retry.
68+ * newer than its bound, more entries than a cursor can name sat at one instant, it held the
69+ * others at a bound it could not move past, it failed again on its one retry, or it failed and
70+ * the listing ended before it could be retried.
7071 */
71- export type ActivityStopReason = 'unreachable' | 'crowded' | 'no_progress' | 'failed_again'
72+ export type ActivityStopReason =
73+ | 'unreachable'
74+ | 'crowded'
75+ | 'no_progress'
76+ | 'failed_again'
77+ | 'not_retried'
7278
7379interface ActivityPageResult < E extends ActivityEntry > {
7480 /** Newest first. */
@@ -200,28 +206,36 @@ export function mergeActivityPage<E extends ActivityEntry>(
200206 }
201207 const entries = returned . map ( ( { entry } ) => entry )
202208 /**
203- * A page that returned nothing and moved no bound would be read again unchanged, so it ends
204- * here. Returning entries is progress even when the remaining bounds stay put, as when a
205- * recovered account catches up above the others.
209+ * A page that returned nothing and moved no bound would be read again unchanged. The accounts
210+ * whose listings pin the horizon are the ones that cannot advance, so only they stop; the
211+ * others continue below it on the next page. Returning entries is progress even when the
212+ * remaining bounds stay put, as when a recovered account catches up above the others.
206213 */
207- if ( continuing . length && ! progressed && ! entries . length )
208- return {
209- entries,
210- stopped : [
211- ...stopped ,
212- ...continuing . map ( ( accountId ) => ( { accountId, reason : 'no_progress' as const } ) ) ,
213- ] ,
214+ if ( continuing . length && ! progressed && ! entries . length ) {
215+ const pinning = continuing . filter ( ( accountId ) => {
216+ const account = accounts . find ( ( candidate ) => candidate . accountId === accountId )
217+ return (
218+ account ?. read &&
219+ account . streams . some (
220+ ( stream ) => stream . more && Date . parse ( stream . scannedTo ?? '' ) === horizon
221+ )
222+ )
223+ } )
224+ for ( const accountId of pinning . length ? pinning : [ ...continuing ] ) {
225+ stopped . push ( { accountId, reason : 'no_progress' } )
226+ delete next [ accountId ]
227+ continuing . splice ( continuing . indexOf ( accountId ) , 1 )
214228 }
229+ }
215230 /**
216231 * An account that failed is retried on the next page while this one made progress; a page
217232 * where only failed accounts were left, and failed again, ends the listing.
218233 */
219- const retrying = Object . keys ( next ) . some ( ( accountId ) => ! continuing . includes ( accountId ) )
220- return {
221- entries,
222- ...( continuing . length || ( retrying && entries . length ) ? { next } : { } ) ,
223- stopped,
224- }
234+ const retrying = Object . keys ( next ) . filter ( ( accountId ) => ! continuing . includes ( accountId ) )
235+ if ( continuing . length || ( retrying . length && entries . length ) ) return { entries, next, stopped }
236+ /** A listing that ends here leaves the failed accounts it would have retried unread. */
237+ for ( const accountId of retrying ) stopped . push ( { accountId, reason : 'not_retried' } )
238+ return { entries, stopped }
225239}
226240
227241const boundSchema = z . object ( {
@@ -273,3 +287,18 @@ export function readActivityCursor(
273287 return undefined
274288 }
275289}
290+
291+ /**
292+ * How many accounts a cursor continues, for charging a page before it is read. The cursor is not
293+ * verified here; a forged one is refused when the page reads it.
294+ */
295+ export function activityCursorAccountCount ( value : string ) : number | undefined {
296+ try {
297+ const parsed = cursorSchema
298+ . pick ( { accounts : true } )
299+ . safeParse ( JSON . parse ( Buffer . from ( value , 'base64url' ) . toString ( 'utf8' ) ) )
300+ return parsed . success ? Object . keys ( parsed . data . accounts ) . length : undefined
301+ } catch {
302+ return undefined
303+ }
304+ }
0 commit comments