@@ -249,6 +249,33 @@ const deliver = Effect.fnUntraced(function* (console: ReturnType<typeof fakeCons
249249 yield * waitUntil ( Effect . sync ( ( ) => console . state . acks . flat ( ) . includes ( event . id ) ) )
250250} )
251251
252+ /**
253+ * Pushes status changes and waits until the client holds them. Frames are read in order, so once a marker pushed after
254+ * them is acknowledged, their wait has begun. The marker's session is marked deleted, so it acknowledges without a wake.
255+ */
256+ const hold = Effect . fnUntraced ( function * (
257+ console : ReturnType < typeof fakeConsole > ,
258+ host : PluginContext . Context ,
259+ ...events : WatchEvent [ ]
260+ ) {
261+ const marker = `evt_marker_${ crypto . randomUUID ( ) } `
262+ yield * host . storage . set ( "watch-deleted:ses_marker" , 0 )
263+ console . push ( ...events )
264+ console . push ( watchEvent ( "ses_marker" , marker ) )
265+ yield * waitUntil ( Effect . sync ( ( ) => console . state . acks . flat ( ) . includes ( marker ) ) )
266+ } )
267+
268+ /** Pushes status changes, lets them settle, and waits until the plugin acknowledges them. */
269+ const settle = Effect . fnUntraced ( function * (
270+ console : ReturnType < typeof fakeConsole > ,
271+ host : PluginContext . Context ,
272+ ...events : WatchEvent [ ]
273+ ) {
274+ yield * hold ( console , host , ...events )
275+ yield * TestClock . adjust ( OpencodeWatches . SETTLE )
276+ yield * waitUntil ( Effect . sync ( ( ) => events . every ( ( event ) => console . state . acks . flat ( ) . includes ( event . id ) ) ) )
277+ } )
278+
252279const watchTools = Effect . gen ( function * ( ) {
253280 const tools = yield * Tool . Service
254281 return ( yield * tools . list ( ) ) . filter ( ( tool ) => tool . options ?. namespace === "console_watch" ) . map ( ( tool ) => tool . name )
@@ -339,7 +366,9 @@ describe("OpencodePlugin event watches", () => {
339366 const own = yield * host . session . create ( { } )
340367 const ci = yield * host . session . create ( { } )
341368 yield * waitUntil ( Effect . sync ( ( ) => console . state . opens === 1 ) )
342- console . push (
369+ yield * settle (
370+ console ,
371+ host ,
343372 watchEvent ( own . id , "evt_own" , {
344373 source : "slack" ,
345374 kind : "message" ,
@@ -351,7 +380,6 @@ describe("OpencodePlugin event watches", () => {
351380 } ) ,
352381 watchEvent ( ci . id , "evt_ci" , { kind : "workflow_run" , status : "failure" , body : "Run tests: exit 1" } ) ,
353382 )
354- yield * waitUntil ( Effect . sync ( ( ) => console . state . acks . flat ( ) . length === 2 ) )
355383
356384 const text = ( sessionID : Session . ID ) => wakes . find ( ( wake ) => wake . sessionID === sessionID ) ?. text ?? ""
357385 expect ( text ( own . id ) ) . toContain (
@@ -651,8 +679,9 @@ describe("OpencodePlugin event watches", () => {
651679 yield * ( yield * watchTool ( "watch" ) ) . execute ( { target : "acme/app#1" , unattended : true } , context ( session . id ) )
652680 yield * waitUntil ( Effect . sync ( ( ) => console . state . opens === 1 ) )
653681 for ( const index of [ 1 , 2 , 3 , 4 , 5 , 6 ] )
654- yield * deliver (
682+ yield * settle (
655683 console ,
684+ host ,
656685 watchEvent ( session . id , `evt_${ index } ` , { watchID : "ew_new" , kind : "workflow_run" , status : "success" } ) ,
657686 )
658687 expect ( wakes . map ( ( wake ) => wake . resume ) ) . toEqual ( [ true , true , true , true , true , false ] )
@@ -694,7 +723,7 @@ describe("OpencodePlugin event watches", () => {
694723 const session = yield * host . session . create ( { } )
695724 yield * waitUntil ( Effect . sync ( ( ) => console . state . opens === 1 ) )
696725 for ( const index of [ 1 , 2 , 3 , 4 , 5 , 6 ] )
697- yield * deliver ( console , watchEvent ( session . id , `evt_${ index } ` , { kind : "workflow_run" , status : "success" } ) )
726+ yield * settle ( console , host , watchEvent ( session . id , `evt_${ index } ` , { kind : "workflow_run" , status : "success" } ) )
698727 yield * deliver (
699728 console ,
700729 watchEvent ( session . id , "evt_reply" , { source : "slack" , kind : "message" , status : null } ) ,
@@ -711,7 +740,7 @@ describe("OpencodePlugin event watches", () => {
711740 const session = yield * host . session . create ( { } )
712741 yield * waitUntil ( Effect . sync ( ( ) => console . state . opens === 1 ) )
713742 for ( const index of Array . from ( { length : 21 } , ( _ , index ) => index ) )
714- yield * deliver ( console , watchEvent ( session . id , `evt_${ index } ` , { kind : "workflow_job" , status : "failure" } ) )
743+ yield * settle ( console , host , watchEvent ( session . id , `evt_${ index } ` , { kind : "workflow_job" , status : "failure" } ) )
715744 expect ( wakes . map ( ( wake ) => wake . resume ) ) . toEqual ( [ ...Array . from ( { length : 20 } , ( ) => true ) , false ] )
716745 } ) ,
717746 ) ,
@@ -730,13 +759,86 @@ describe("OpencodePlugin event watches", () => {
730759 )
731760 yield * waitUntil ( Effect . sync ( ( ) => console . state . acks . flat ( ) . includes ( `evt_review_${ index } ` ) ) )
732761 }
733- yield * deliver ( console , watchEvent ( session . id , "evt_ci_6" , { kind : "workflow_run" , status : "success" } ) )
762+ yield * settle ( console , host , watchEvent ( session . id , "evt_ci_6" , { kind : "workflow_run" , status : "success" } ) )
734763 yield * deliver ( console , watchEvent ( session . id , "evt_review_6" ) )
735764 expect ( wakes . map ( ( wake ) => wake . resume ) ) . toEqual ( [ true , true , true , true , true , true , false ] )
736765 } ) ,
737766 ) ,
738767 )
739768
769+ it . effect ( "wakes once for status changes seconds apart, once 15 seconds pass without another or 30 at most" , ( ) =>
770+ withConsole ( { } , ( console ) =>
771+ Effect . gen ( function * ( ) {
772+ const { host, wakes } = yield * start ( console )
773+ const session = yield * host . session . create ( { } )
774+ yield * waitUntil ( Effect . sync ( ( ) => console . state . opens === 1 ) )
775+ const status = ( id : string , kind : string , status : string ) => watchEvent ( session . id , id , { kind, status } )
776+ yield * hold ( console , host , status ( "evt_checks" , "check" , "all 1 passed" ) )
777+ yield * TestClock . adjust ( "10 seconds" )
778+ yield * hold ( console , host , status ( "evt_merged" , "state" , "merged" ) )
779+ yield * TestClock . adjust ( "14 seconds" )
780+ expect ( wakes ) . toEqual ( [ ] )
781+ yield * TestClock . adjust ( "1 second" )
782+ yield * waitUntil ( Effect . sync ( ( ) => wakes . length === 1 ) )
783+ expect ( wakes [ 0 ] ?. metadata ?. events ) . toEqual ( [ "evt_checks" , "evt_merged" ] )
784+ expect ( wakes [ 0 ] ?. text ) . toStartWith ( "2 updates arrived" )
785+ expect ( wakes [ 0 ] ?. text ) . not . toContain ( "Looks good" )
786+
787+ // Each arrives before the last has been quiet for 15 seconds, so the cap sends them 30 seconds after the first.
788+ for ( const index of [ 1 , 2 , 3 ] ) {
789+ yield * hold ( console , host , status ( `evt_slow_${ index } ` , "workflow_run" , "success" ) )
790+ yield * TestClock . adjust ( "10 seconds" )
791+ }
792+ yield * waitUntil ( Effect . sync ( ( ) => console . state . acks . flat ( ) . includes ( "evt_slow_3" ) ) )
793+ expect ( wakes [ 1 ] ?. metadata ?. events ) . toEqual ( [ "evt_slow_1" , "evt_slow_2" , "evt_slow_3" ] )
794+ expect ( console . state . acks . flat ( ) . filter ( ( id ) => ! id . startsWith ( "evt_marker" ) ) ) . toEqual ( [
795+ "evt_checks" ,
796+ "evt_merged" ,
797+ "evt_slow_1" ,
798+ "evt_slow_2" ,
799+ "evt_slow_3" ,
800+ ] )
801+ } ) ,
802+ ) ,
803+ )
804+
805+ it . effect ( "wakes at once for something someone wrote, with the status changes still settling" , ( ) =>
806+ withConsole ( { } , ( console ) =>
807+ Effect . gen ( function * ( ) {
808+ const { host, wakes } = yield * start ( console )
809+ const session = yield * host . session . create ( { } )
810+ yield * waitUntil ( Effect . sync ( ( ) => console . state . opens === 1 ) )
811+ yield * hold ( console , host , watchEvent ( session . id , "evt_ci" , { kind : "workflow_run" , status : "failure" } ) )
812+ yield * deliver ( console , watchEvent ( session . id , "evt_review" ) )
813+ expect ( wakes . map ( ( wake ) => wake . metadata ?. events ) ) . toEqual ( [ [ "evt_ci" , "evt_review" ] ] )
814+
815+ // The wait it was in is over: the clock moving on wakes nothing more.
816+ yield * TestClock . adjust ( OpencodeWatches . MAX_SETTLE )
817+ yield * Effect . promise ( ( ) => Bun . sleep ( 50 ) )
818+ expect ( wakes ) . toHaveLength ( 1 )
819+ } ) ,
820+ ) ,
821+ )
822+
823+ it . effect ( "leaves status changes still settling unacknowledged when the socket closes, and settles them again" , ( ) =>
824+ withConsole ( { } , ( console ) =>
825+ Effect . gen ( function * ( ) {
826+ const { host, wakes } = yield * start ( console )
827+ const session = yield * host . session . create ( { } )
828+ yield * waitUntil ( Effect . sync ( ( ) => console . state . opens === 1 ) )
829+ yield * hold ( console , host , watchEvent ( session . id , "evt_ci" , { kind : "workflow_run" , status : "success" } ) )
830+ console . state . sockets . forEach ( ( socket ) => socket . close ( OpencodeWatches . LEASE_CLOSE_CODE , "lease" ) )
831+ yield * waitUntil ( Effect . sync ( ( ) => console . state . opens === 2 ) )
832+ expect ( console . state . acks . flat ( ) ) . not . toContain ( "evt_ci" )
833+
834+ // The next connection's snapshot sends it again, and it waits there anew.
835+ yield * settle ( console , host )
836+ yield * waitUntil ( Effect . sync ( ( ) => console . state . acks . flat ( ) . includes ( "evt_ci" ) ) )
837+ expect ( wakes . map ( ( wake ) => wake . metadata ?. events ) ) . toEqual ( [ [ "evt_ci" ] ] )
838+ } ) ,
839+ ) ,
840+ )
841+
740842 it . effect ( "resumes a held session with a later wake once 10 minutes pass" , ( ) =>
741843 withConsole ( { } , ( console ) =>
742844 Effect . gen ( function * ( ) {
0 commit comments