@@ -7,6 +7,7 @@ import { dirname, join, relative } from 'node:path';
77import { fileURLToPath } from 'node:url' ;
88import { get , type Server } from 'node:http' ;
99import type { AddressInfo } from 'node:net' ;
10+ import { setImmediate } from 'node:timers/promises' ;
1011import { gunzipSync } from 'node:zlib' ;
1112import { AgentRegistry , defaultInstances } from '../src/agents.js' ;
1213import { AgyAdapter } from '../src/adapters/agy.js' ;
@@ -3012,11 +3013,16 @@ test('agents: rename, hide, restore and removal leave a used instance in place',
30123013 }
30133014} ) ;
30143015
3015- /** Appends events straight to the log, the way a turn would have, without running an agent. */
3016- function writeLog ( threadId : string , turnId : string | null , entries : Array < [ type : Parameters < Store [ 'appendEvent' ] > [ 2 ] , data : Record < string , unknown > ] > ) {
3017- store . transaction ( ( ) => {
3018- for ( const [ type , data ] of entries ) store . appendEvent ( threadId , turnId , type , data ) ;
3019- } ) ;
3016+ /** Seeds the stored log without monopolizing the event loop shared with the HTTP fixture. */
3017+ async function writeLog ( threadId : string , turnId : string | null , entries : Array < [ type : Parameters < Store [ 'appendEvent' ] > [ 2 ] , data : Record < string , unknown > ] > ) {
3018+ const batchSize = 500 ;
3019+ for ( let offset = 0 ; offset < entries . length ; offset += batchSize ) {
3020+ const batch = entries . slice ( offset , offset + batchSize ) ;
3021+ store . transaction ( ( ) => {
3022+ for ( const [ type , data ] of batch ) store . appendEvent ( threadId , turnId , type , data ) ;
3023+ } ) ;
3024+ await setImmediate ( ) ;
3025+ }
30203026}
30213027
30223028type Logged = { seq : number ; type : string ; turnId : string | null ; data : Record < string , any > } ;
@@ -3026,7 +3032,7 @@ test('events endpoint: a page begins where a turn begins, and streamed fragments
30263032 const page = async ( query : string ) => ( await ( await api ( `/api/threads/${ thread . id } /events?${ query } ` ) ) . json ( ) ) as Logged [ ] ;
30273033
30283034 // A short turn, then one far longer than a page reaches back.
3029- writeLog ( thread . id , 'a' , [
3035+ await writeLog ( thread . id , 'a' , [
30303036 [ 'turn.started' , { text : 'first' } ] ,
30313037 [ 'reasoning.delta' , { text : 'th' } ] ,
30323038 [ 'reasoning.delta' , { text : 'ink' } ] ,
@@ -3037,7 +3043,7 @@ test('events endpoint: a page begins where a turn begins, and streamed fragments
30373043 [ 'message.delta' , { text : 'two' } ] ,
30383044 [ 'turn.done' , { stopReason : 'end_turn' } ] ,
30393045 ] ) ;
3040- writeLog ( thread . id , 'b' , [
3046+ await writeLog ( thread . id , 'b' , [
30413047 [ 'turn.started' , { text : 'second' } ] ,
30423048 ...Array . from ( { length : 21_000 } , ( _ , i ) : [ 'reasoning.delta' , { text : string } ] => [ 'reasoning.delta' , { text : i % 2 ? 'b' : 'a' } ] ) ,
30433049 ] ) ;
@@ -3052,8 +3058,8 @@ test('events endpoint: a page begins where a turn begins, and streamed fragments
30523058 assert . equal ( running [ 1 ] . seq , all [ 0 ] . seq , 'the first fragment keeps its own seq and time' ) ;
30533059 assert . equal ( running . at ( - 1 ) ! . seq , running [ 0 ] . seq + 21_000 , 'the merged rest carries the last seq, so a stream can resume after it' ) ;
30543060
3055- writeLog ( thread . id , 'b' , [ [ 'turn.done' , { stopReason : 'end_turn' } ] ] ) ;
3056- writeLog ( thread . id , 'c' , [
3061+ await writeLog ( thread . id , 'b' , [ [ 'turn.done' , { stopReason : 'end_turn' } ] ] ) ;
3062+ await writeLog ( thread . id , 'c' , [
30573063 [ 'turn.started' , { text : 'third' } ] ,
30583064 [ 'message.delta' , { text : 'short' } ] ,
30593065 [ 'turn.done' , { stopReason : 'end_turn' } ] ,
@@ -3097,7 +3103,7 @@ test('events endpoint: a page begins where a turn begins, and streamed fragments
30973103test ( 'history pages limit short turns too, and paging back preserves every event needed for replay' , async ( ) => {
30983104 const thread = await newThread ( { agent : 'pi' , cwd : workdir } ) ;
30993105 for ( let i = 0 ; i < 8 ; i ++ ) {
3100- writeLog ( thread . id , `turn-${ i } ` , [
3106+ await writeLog ( thread . id , `turn-${ i } ` , [
31013107 [ 'turn.started' , { text : `question ${ i } ` } ] ,
31023108 [ 'tool.start' , { id : `tool-${ i } ` , kind : 'execute' , title : 'check' , status : 'in_progress' } ] ,
31033109 ...Array . from ( { length : 30 } , ( _ , n ) : [ 'tool.update' , Record < string , unknown > ] => [ 'tool.update' , { id : `tool-${ i } ` , output : `snapshot ${ n } \n${ 'output\n' . repeat ( 1000 ) } ` } ] ) ,
@@ -3132,7 +3138,7 @@ test('history pages limit short turns too, and paging back preserves every event
31323138test ( 'SSE stream replays everything after the cursor, however long, before going live' , async ( ) => {
31333139 const thread = await newThread ( { agent : 'pi' , cwd : workdir } ) ;
31343140 // More than one page of the stored log, none of it mergeable.
3135- writeLog ( thread . id , 'a' , [
3141+ await writeLog ( thread . id , 'a' , [
31363142 [ 'turn.started' , { text : 'long' } ] ,
31373143 ...Array . from ( { length : 6000 } , ( _ , i ) : [ 'tool.update' , { id : string } ] => [ 'tool.update' , { id : `u${ i } ` } ] ) ,
31383144 [ 'turn.done' , { stopReason : 'end_turn' } ] ,
@@ -3160,7 +3166,7 @@ test('sync: a hub turn deep in a long log is still known, and is not appended ag
31603166 await post ( `/api/threads/${ thread . id } /turns` , { text : 'hello' } ) ;
31613167 await untilIdle ( thread . id ) ;
31623168 // A long stretch of output, then a second turn far past the first page of the log.
3163- writeLog ( thread . id , null , Array . from ( { length : 6000 } , ( ) : [ 'thread.updated' , { } ] => [ 'thread.updated' , { } ] ) ) ;
3169+ await writeLog ( thread . id , null , Array . from ( { length : 6000 } , ( ) : [ 'thread.updated' , { } ] => [ 'thread.updated' , { } ] ) ) ;
31643170 await post ( `/api/threads/${ thread . id } /turns` , { text : 'much later' } ) ;
31653171 await untilIdle ( thread . id ) ;
31663172 const sessionId = ( await viewOf ( thread . id ) ) . agentSessionId ! ;
@@ -3186,14 +3192,14 @@ test('sync: a hub turn deep in a long log is still known, and is not appended ag
31863192
31873193test ( 'restart: a turn the last daemon left running is ended as interrupted' , async ( ) => {
31883194 const cut = await newThread ( { agent : 'pi' , cwd : workdir } ) ;
3189- writeLog ( cut . id , 'open' , [
3195+ await writeLog ( cut . id , 'open' , [
31903196 [ 'turn.started' , { text : 'never finished' } ] ,
31913197 [ 'message.delta' , { text : 'half an ans' } ] ,
31923198 ] ) ;
31933199 store . updateThread ( cut . id , { status : 'running' } ) ;
31943200 // Marked running, but its turn did end: only the mark is stale.
31953201 const stale = await newThread ( { agent : 'pi' , cwd : workdir } ) ;
3196- writeLog ( stale . id , 'closed' , [
3202+ await writeLog ( stale . id , 'closed' , [
31973203 [ 'turn.started' , { text : 'finished' } ] ,
31983204 [ 'turn.done' , { stopReason : 'end_turn' } ] ,
31993205 ] ) ;
@@ -3216,7 +3222,7 @@ test('restart: a turn the last daemon left running is ended as interrupted', asy
32163222
32173223test ( 'a page of history collapses a tool call\'s update burst, and the payload travels gzipped' , async ( ) => {
32183224 const thread = await newThread ( { agent : 'pi' , cwd : workdir } ) ;
3219- writeLog ( thread . id , 'a1' , [
3225+ await writeLog ( thread . id , 'a1' , [
32203226 [ 'turn.started' , { text : 'edit it' } ] ,
32213227 [ 'tool.start' , { id : 'c1' , kind : 'edit' , title : 'Edit a.ts' , status : 'in_progress' } ] ,
32223228 ...Array . from ( { length : 40 } , ( _ , i ) => [
0 commit comments