@@ -94,6 +94,76 @@ describe("batchNearby", () => {
9494 expect ( result [ 0 ] . text ) . toBe ( "BATCH:match-1,match-2" )
9595 } )
9696
97+ test ( "repeated assistant preambles between matching tools are consumed by a successful batch" , ( ) => {
98+ const preamble = "I'll read the files now."
99+ const messages = [
100+ msg ( preamble , "say" , "text" ) ,
101+ msg ( "match-1" , "ask" ) ,
102+ msg ( "" , "say" , "api_req_started" ) ,
103+ msg ( preamble , "say" , "text" ) ,
104+ msg ( "match-2" , "ask" ) ,
105+ msg ( preamble , "say" , "text" ) ,
106+ msg ( "match-3" , "ask" ) ,
107+ ]
108+ const result = batchNearby ( messages , {
109+ isTarget : isMatch ,
110+ isIgnorableBetweenTargets,
111+ isBoundary,
112+ synthesize : synthesizeBatch ,
113+ } )
114+
115+ expect ( result ) . toHaveLength ( 2 )
116+ expect ( result [ 0 ] . text ) . toBe ( preamble )
117+ expect ( result [ 1 ] . text ) . toBe ( "BATCH:match-1,match-2,match-3" )
118+ } )
119+
120+ test ( "distinct assistant text between matching tools remains a boundary" , ( ) => {
121+ const messages = [
122+ msg ( "I'll read the files now." , "say" , "text" ) ,
123+ msg ( "match-1" , "ask" ) ,
124+ msg ( "I found something important." , "say" , "text" ) ,
125+ msg ( "match-2" , "ask" ) ,
126+ ]
127+ const result = batchNearby ( messages , {
128+ isTarget : isMatch ,
129+ isIgnorableBetweenTargets,
130+ isBoundary,
131+ synthesize : synthesizeBatch ,
132+ } )
133+
134+ expect ( result ) . toEqual ( messages )
135+ } )
136+
137+ test ( "restores a repeated preamble when no later target is found" , ( ) => {
138+ const preamble = "I'll read the files now."
139+ const messages = [
140+ msg ( preamble , "say" , "text" ) ,
141+ msg ( "match-1" , "ask" ) ,
142+ msg ( preamble , "say" , "text" ) ,
143+ msg ( "different tool" , "ask" ) ,
144+ ]
145+ const result = batchNearby ( messages , {
146+ isTarget : isMatch ,
147+ isIgnorableBetweenTargets,
148+ isBoundary,
149+ synthesize : synthesizeBatch ,
150+ } )
151+
152+ expect ( result ) . toEqual ( messages )
153+ } )
154+
155+ test ( "an item matching target and boundary stops the current batch" , ( ) => {
156+ const messages = [ msg ( "match-1" , "ask" ) , msg ( "match-boundary" , "ask" ) , msg ( "match-2" , "ask" ) ]
157+ const result = batchNearby ( messages , {
158+ isTarget : isMatch ,
159+ isIgnorableBetweenTargets : ( ) => false ,
160+ isBoundary : ( item ) => item . text === "match-boundary" ,
161+ synthesize : synthesizeBatch ,
162+ } )
163+
164+ expect ( result ) . toEqual ( messages )
165+ } )
166+
97167 test ( "boundary message stops batching" , ( ) => {
98168 const messages = [ msg ( "match-1" , "ask" ) , msg ( "visible text" , "say" , "text" ) , msg ( "match-2" , "ask" ) ]
99169 const result = batchNearby ( messages , {
0 commit comments