@@ -520,6 +520,31 @@ function reconnectHarness(
520520 live . established ( "c" ) ;
521521 await flush ( ) ;
522522 } ;
523+ /** The relay's grant notice for `channelId`, delivered live. Any roster
524+ * check it schedules is answered with c's roster. */
525+ const grant = async ( channelId = "c" ) => {
526+ live . receive ( [
527+ signed ( relay , {
528+ kind : 44100 ,
529+ content : "" ,
530+ created_at : 55 ,
531+ tags : [
532+ [ "p" , viewer . pubkey ] ,
533+ [ "h" , channelId ] ,
534+ ] ,
535+ } ) ,
536+ ] ) ;
537+ await flush ( ) ;
538+ for ( const entry of wire . pending . splice ( 0 ) )
539+ if ( entry . filters [ 0 ] ?. top_level === true ) wire . pending . push ( entry ) ;
540+ else
541+ entry . respond (
542+ entry . filters [ 0 ] ?. kinds ?. includes ( 0 )
543+ ? [ ]
544+ : [ roster ( relay , "c" , [ viewer . pubkey ] ) , metadata ( relay , "c" , "C" ) ] ,
545+ ) ;
546+ await flush ( ) ;
547+ } ;
523548 /** Drop the stream, with no reconnect yet. */
524549 const drop = async ( ) => {
525550 live . state ( { status : "retrying" , routes : [ ] } ) ;
@@ -556,6 +581,7 @@ function reconnectHarness(
556581 list,
557582 open,
558583 reconnect,
584+ grant,
559585 drop,
560586 connectAndReplay,
561587 dropAndReplay,
@@ -1862,20 +1888,22 @@ describe("a reconnect whose replay was continuous", () => {
18621888 }
18631889 } ) ;
18641890
1865- it ( "catches up from the rows held at the report when a resumed channel is reported again with nothing continuous" , async ( ) => {
1891+ it ( "catches up from the rows held at a grant when a resumed channel is reported again with nothing continuous" , async ( ) => {
18661892 const h = reconnectHarness ( ) ;
18671893 try {
18681894 const seen = await verifiedThenDropped ( h ) ;
18691895 await resume ( h , [ ] , true ) ;
18701896 expect ( h . reads ( ) ) . toHaveLength ( 0 ) ;
1871- // A grant the broker learned of after the replay: the relay may have
1872- // left the channel out, so the stream delivered nothing for it since.
1873- h . live . established ( "c" ) ;
1874- await flush ( ) ;
1875- // A live event after the re-sent batch is live, before the catch-up reads .
1897+ // Access ended during the outage, so the relay left c out of the batch,
1898+ // and the stream has delivered nothing for it since. A grant re-sends it .
1899+ await h . grant ( ) ;
1900+ expect ( h . reads ( ) ) . toHaveLength ( 0 ) ;
1901+ // The re-sent batch delivers a live event before its report .
18761902 const fresh = message ( h . alice , "c" , "fresh" , 60 ) ;
18771903 h . live . receive ( [ fresh ] ) ;
18781904 await flush ( ) ;
1905+ h . live . established ( "c" ) ;
1906+ await flush ( ) ;
18791907 const hidden = message ( h . alice , "c" , "hidden" , 30 ) ,
18801908 newer = message ( h . alice , "c" , "newer" , 50 ) ;
18811909 // The head holds the live row; the gap lies below it, above `seen`.
@@ -1892,21 +1920,58 @@ describe("a reconnect whose replay was continuous", () => {
18921920 }
18931921 } ) ;
18941922
1895- it ( "keeps the rows held at the report when the stream drops again before a retracted resume catches up" , async ( ) => {
1923+ it ( "catches up from the drop's rows when a delayed continuous report arrives after a grant and its traffic" , async ( ) => {
1924+ // The browser broker writes live events at once but holds a report under
1925+ // backpressure: the grant and the re-sent batch's first event reach the
1926+ // session before the report of the replay that left c out.
18961927 const h = reconnectHarness ( ) ;
18971928 try {
18981929 const seen = await verifiedThenDropped ( h ) ;
1899- await resume ( h , [ ] , true ) ;
1930+ h . live . state ( { status : "connected" , routes : [ ] } ) ;
1931+ await flush ( ) ;
1932+ await h . grant ( ) ;
1933+ const fresh = message ( h . alice , "c" , "fresh" , 60 ) ;
1934+ h . live . receive ( [ fresh ] ) ;
1935+ await flush ( ) ;
1936+ h . live . established ( "c" , undefined , [ "c" ] ) ;
1937+ await flush ( ) ;
1938+ expect ( h . window ( ) . freshness ) . toBe ( "cached" ) ;
1939+ const hidden = message ( h . alice , "c" , "hidden" , 30 ) ,
1940+ newer = message ( h . alice , "c" , "newer" , 50 ) ;
1941+ h . read ( ) . respond ( h . page ( "head" , [ fresh , newer ] , newer ) ) ;
1942+ await flush ( ) ;
1943+ const older = h . read ( ) ;
1944+ expect ( older . filters [ 0 ] ) . toMatchObject ( { before_id : newer . id } ) ;
1945+ older . respond ( h . page ( h . cursor ( newer ) , [ hidden , seen ] , seen ) ) ;
1946+ await flush ( ) ;
1947+ expect ( h . rows ( ) ) . toEqual ( [ seen . id , hidden . id , newer . id , fresh . id ] ) ;
1948+ expect ( h . window ( ) . freshness ) . toBe ( "verified" ) ;
1949+ // The re-sent batch's own report then needs only the head.
19001950 h . live . established ( "c" ) ;
19011951 await flush ( ) ;
1952+ h . read ( ) . respond ( h . page ( "head" , [ fresh , newer ] , newer ) ) ;
1953+ await flush ( ) ;
1954+ expect ( h . reads ( ) ) . toHaveLength ( 0 ) ;
1955+ expect ( h . window ( ) . freshness ) . toBe ( "verified" ) ;
1956+ } finally {
1957+ h . owner . dispose ( ) ;
1958+ }
1959+ } ) ;
1960+
1961+ it ( "keeps the rows held at a grant when the stream drops again before its catch-up" , async ( ) => {
1962+ const h = reconnectHarness ( ) ;
1963+ try {
1964+ const seen = await verifiedThenDropped ( h ) ;
1965+ await resume ( h , [ ] , true ) ;
1966+ await h . grant ( ) ;
19021967 const fresh = message ( h . alice , "c" , "fresh" , 60 ) ;
19031968 h . live . receive ( [ fresh ] ) ;
19041969 await flush ( ) ;
1905- // The retraction's head is still outstanding when the stream drops again.
1906- h . read ( ) ;
1970+ // The stream drops before the re-sent batch reports.
19071971 await h . drop ( ) ;
19081972 const later = message ( h . alice , "c" , "later" , 70 ) ;
1909- await resume ( h , [ later ] , false ) ;
1973+ await resume ( h , [ later ] , true ) ;
1974+ expect ( h . window ( ) . freshness ) . toBe ( "cached" ) ;
19101975 const hidden = message ( h . alice , "c" , "hidden" , 30 ) ,
19111976 newer = message ( h . alice , "c" , "newer" , 50 ) ;
19121977 h . read ( ) . respond ( h . page ( "head" , [ later , fresh , newer ] , newer ) ) ;
@@ -2212,15 +2277,20 @@ describe("a reconnect whose replay was continuous", () => {
22122277 await verifiedThenDropped ( h ) ;
22132278 await resume ( h , [ ] , true ) ;
22142279 const thread = h . owner . session . thread ( "c" , root . id ) ;
2280+ /** Replies the relay holds for the root. */
2281+ let replies : RelayEvent [ ] = [ ] ;
22152282 const answer = async ( ) => {
22162283 for ( let quiet = 0 ; quiet < 3 ; ) {
22172284 await flush ( ) ;
22182285 if ( ! h . wire . pending . length ) quiet ++ ;
22192286 else quiet = 0 ;
22202287 for ( const entry of h . wire . pending . splice ( 0 ) )
2221- entry . respond (
2222- entry . filters [ 0 ] ?. ids ?. includes ( root . id ) ? [ root ] : [ ] ,
2223- ) ;
2288+ entry . respond ( [
2289+ ...( entry . filters [ 0 ] ?. ids ?. includes ( root . id ) ? [ root ] : [ ] ) ,
2290+ ...( entry . filters . some ( ( f ) => f [ "#e" ] ?. includes ( root . id ) )
2291+ ? replies
2292+ : [ ] ) ,
2293+ ] ) ;
22242294 }
22252295 } ;
22262296 const threadReads = ( ) =>
@@ -2232,19 +2302,28 @@ describe("a reconnect whose replay was continuous", () => {
22322302 void thread . refresh ( ) ;
22332303 await answer ( ) ;
22342304 expect ( thread . snapshot ( ) . status ) . toBe ( "ready" ) ;
2305+ // A reply the live stream dropped before the replay window.
2306+ const reply = message ( h . alice , "c" , "reply" , 20 , [
2307+ [ "e" , root . id , "" , "reply" ] ,
2308+ ] ) ;
2309+ replies = [ reply ] ;
22352310 clock += 15 * 60_000 ;
22362311 await h . drop ( ) ;
22372312 h . queries . length = 0 ;
22382313 await resume ( h , [ ] , true ) ;
22392314 await answer ( ) ;
22402315 expect ( threadReads ( ) ) . toEqual ( [ ] ) ;
2316+ expect ( thread . snapshot ( ) . replies ) . toEqual ( [ ] ) ;
22412317 clock += 1 ;
22422318 await h . drop ( ) ;
22432319 h . queries . length = 0 ;
22442320 await resume ( h , [ ] , true ) ;
22452321 await answer ( ) ;
22462322 expect ( threadReads ( ) . length ) . toBeGreaterThan ( 0 ) ;
22472323 expect ( thread . snapshot ( ) . status ) . toBe ( "ready" ) ;
2324+ expect ( thread . snapshot ( ) . replies . map ( ( row ) => row . id ) ) . toEqual ( [
2325+ reply . id ,
2326+ ] ) ;
22482327 thread . dispose ( ) ;
22492328 } finally {
22502329 h . owner . dispose ( ) ;
0 commit comments