@@ -66,96 +66,19 @@ class Fake implements Worker {
6666}
6767function setup ( maxWorkers = 1 ) {
6868 const workers : Fake [ ] = [ ] ;
69- const onSettled = vi . fn ( ) ;
7069 const c = new Coordinator ( {
7170 maxWorkers,
7271 createWorker : ( _options , sessionFile , continues ) => {
7372 const worker = new Fake ( sessionFile , continues ?? false ) ;
7473 workers . push ( worker ) ;
7574 return worker ;
7675 } ,
77- onSettled,
7876 } ) ;
7977 const start = ( prompt = "brief" ) =>
8078 c . start ( { prompt, description : prompt , worker : options } ) ;
81- return { c, workers, start, onSettled } ;
79+ return { c, workers, start } ;
8280}
8381
84- describe ( "a task is a conversation on one session" , ( ) => {
85- it ( "replies, continues on the same session with a fresh process, and is removed on acceptance" , async ( ) => {
86- const { c, workers, start, onSettled } = setup ( ) ;
87- const id = start ( ) ;
88- expect ( c . get ( id ) ) . toMatchObject ( { status : "active" , unsent : 0 } ) ;
89- expect ( workers [ 0 ] ! . run ) . toHaveBeenCalledExactlyOnceWith ( "brief" ) ;
90- expect ( workers [ 0 ] ! . continues ) . toBe ( false ) ;
91- workers [ 0 ] ! . emit ( { type : "agent_settled" , aborted : false } as WorkerEvent ) ;
92- expect ( c . get ( id ) . status ) . toBe ( "active" ) ; // Only process exit ends an execution.
93- workers [ 0 ] ! . close ( { text : "Which schema?" } ) ;
94- await tick ( ) ;
95- expect ( c . get ( id ) ) . toMatchObject ( {
96- status : "ready" ,
97- response : "Which schema?" ,
98- delivered : false ,
99- } ) ;
100- expect ( onSettled ) . toHaveBeenCalledOnce ( ) ;
101- c . markDelivered ( id ) ;
102- expect ( c . get ( id ) . delivered ) . toBe ( true ) ;
103-
104- await c . message ( id , "Production" ) ;
105- expect ( c . get ( id ) ) . toMatchObject ( {
106- status : "active" ,
107- response : "" ,
108- delivered : false ,
109- } ) ;
110- expect ( workers [ 1 ] ! . sessionFile ) . toBe ( workers [ 0 ] ! . sessionFile ) ;
111- expect ( workers [ 1 ] ! . continues ) . toBe ( true ) ;
112- expect ( workers [ 1 ] ! . run ) . toHaveBeenCalledExactlyOnceWith ( "Production" ) ;
113- workers [ 1 ] ! . close ( { text : "Checked production." } ) ;
114- await tick ( ) ;
115- expect ( c . accept ( id ) ) . toMatchObject ( {
116- status : "accepted" ,
117- response : "Checked production." ,
118- } ) ;
119- expect ( c . list ( ) ) . toEqual ( [ ] ) ;
120- expect ( ( ) => c . get ( id ) ) . toThrow ( "Unknown task" ) ;
121- await c . dispose ( ) ;
122- } ) ;
123-
124- it ( "runs up to the limit in creation order and combines corrections while queued" , async ( ) => {
125- const { c, workers, start } = setup ( ) ;
126- const a = start ( "a" ) ,
127- b = start ( "b" ) ,
128- d = start ( "d" ) ;
129- await c . message ( b , "also this" ) ;
130- expect ( c . list ( ) . map ( ( task ) => task . status ) ) . toEqual ( [
131- "active" ,
132- "queued" ,
133- "queued" ,
134- ] ) ;
135- expect ( c . get ( b ) . unsent ) . toBe ( 2 ) ;
136- workers [ 0 ] ! . close ( ) ;
137- await tick ( ) ;
138- expect ( c . get ( a ) . status ) . toBe ( "ready" ) ; // A ready task holds no slot.
139- expect ( workers [ 1 ] ! . run ) . toHaveBeenCalledExactlyOnceWith ( "b\n\nalso this" ) ;
140- workers [ 1 ] ! . close ( ) ;
141- await tick ( ) ;
142- expect ( c . get ( d ) . status ) . toBe ( "active" ) ;
143- workers [ 2 ] ! . close ( ) ;
144- await tick ( ) ;
145- await c . dispose ( ) ;
146- } ) ;
147-
148- it ( "only settled tasks can be accepted" , async ( ) => {
149- const { c, workers, start } = setup ( ) ;
150- const id = start ( ) ;
151- expect ( ( ) => c . accept ( id ) ) . toThrow ( "Cannot accept a active task" ) ;
152- workers [ 0 ] ! . close ( { error : "failed" } ) ;
153- await tick ( ) ;
154- expect ( c . accept ( id ) ) . toMatchObject ( { status : "accepted" , error : "failed" } ) ;
155- await c . dispose ( ) ;
156- } ) ;
157- } ) ;
158-
15982describe ( "pausing and redirecting" , ( ) => {
16083 it ( "pausing every task holds queued work too, and each continues on its own" , async ( ) => {
16184 const { c, workers, start } = setup ( ) ;
@@ -219,54 +142,10 @@ describe("pausing and redirecting", () => {
219142 await tick ( ) ;
220143 await c . dispose ( ) ;
221144 } ) ;
222-
223- it ( "a process that finished before the pause reached it keeps its reply" , async ( ) => {
224- const { c, workers, start, onSettled } = setup ( ) ;
225- const id = start ( ) ;
226- const pausing = c . pause ( id ) ;
227- workers [ 0 ] ! . close ( { text : "done" , interrupted : false } ) ;
228- await pausing ;
229- expect ( c . get ( id ) ) . toMatchObject ( { status : "ready" , response : "done" } ) ;
230- expect ( onSettled ) . toHaveBeenCalledOnce ( ) ;
231- await c . dispose ( ) ;
232- } ) ;
233145} ) ;
234146
235147describe ( "failures" , ( ) => {
236- it ( "a failed execution keeps its task, session and unsent input for a retry" , async ( ) => {
237- const { c, workers, start, onSettled } = setup ( ) ;
238- const id = start ( "valuable brief" ) ;
239- workers [ 0 ] ! . close ( {
240- text : "partial" ,
241- error : "Native process exited 1" ,
242- inputPersisted : false ,
243- } ) ;
244- await tick ( ) ;
245- expect ( c . get ( id ) ) . toMatchObject ( {
246- status : "failed" ,
247- error : "Native process exited 1" ,
248- response : "partial" ,
249- unsent : 1 ,
250- delivered : false ,
251- } ) ;
252- expect ( onSettled ) . toHaveBeenCalledOnce ( ) ;
253- c . markDelivered ( id ) ;
254-
255- await c . message ( id , "Retry." ) ;
256- const retried = c . get ( id ) ;
257- expect ( retried ) . toMatchObject ( { status : "active" , delivered : false } ) ;
258- expect ( retried ) . not . toHaveProperty ( "error" ) ;
259- expect ( workers [ 1 ] ! . sessionFile ) . toBe ( workers [ 0 ] ! . sessionFile ) ;
260- expect ( workers [ 1 ] ! . run ) . toHaveBeenCalledExactlyOnceWith (
261- "valuable brief\n\nRetry." ,
262- ) ;
263- workers [ 1 ] ! . close ( { text : "done" } ) ;
264- await tick ( ) ;
265- expect ( c . get ( id ) ) . toMatchObject ( { status : "ready" , response : "done" } ) ;
266- await c . dispose ( ) ;
267- } ) ;
268-
269- it ( "a first start that saved nothing starts again; a lost session is final" , async ( ) => {
148+ it ( "retries as a first start when no journal was saved" , async ( ) => {
270149 const { c, workers, start } = setup ( ) ;
271150 const id = start ( ) ;
272151 workers [ 0 ] ! . close ( {
@@ -277,152 +156,14 @@ describe("failures", () => {
277156 await tick ( ) ;
278157 await c . message ( id , "Again." ) ;
279158 expect ( workers [ 1 ] ! . continues ) . toBe ( false ) ;
280- workers [ 1 ] ! . close ( ) ;
281- await tick ( ) ;
282- await c . message ( id , "More." ) ;
283- expect ( workers [ 2 ] ! . continues ) . toBe ( true ) ;
284- workers [ 2 ] ! . close ( {
285- error : "Native session file is missing" ,
286- inputPersisted : false ,
287- saved : false ,
288- sessionLost : true ,
289- } ) ;
290- await tick ( ) ;
291- expect ( c . get ( id ) ) . toMatchObject ( {
292- status : "failed" ,
293- error : "Native session file is missing; the session cannot continue" ,
294- unsent : 1 ,
295- } ) ;
296- await expect ( c . message ( id , "Retry." ) ) . rejects . toThrow ( "session is lost" ) ;
297- expect ( workers ) . toHaveLength ( 3 ) ;
298- expect ( await c . stop ( id ) ) . toMatchObject ( { status : "stopped" , unsent : 1 } ) ;
299- await c . dispose ( ) ;
300- } ) ;
301- } ) ;
302-
303- describe ( "stopping" , ( ) => {
304- it ( "waits for the process, removes the task and frees its slot" , async ( ) => {
305- const { c, workers, start, onSettled } = setup ( ) ;
306- const id = start ( ) ;
307- const queued = start ( "queued" ) ;
308- let stopped = false ;
309- const stopping = c . stop ( id ) . then ( ( view ) => {
310- stopped = true ;
311- return view ;
312- } ) ;
313- await expect ( c . message ( id , "too late" ) ) . rejects . toThrow ( "stopping" ) ;
314- await tick ( ) ;
315- expect ( stopped ) . toBe ( false ) ;
316- expect ( workers ) . toHaveLength ( 1 ) ;
317- workers [ 0 ] ! . close ( { interrupted : true , inputPersisted : false } ) ;
318- expect ( await stopping ) . toMatchObject ( { id, status : "stopped" , unsent : 1 } ) ;
319- expect ( c . list ( ) . map ( ( task ) => task . id ) ) . toEqual ( [ queued ] ) ;
320- expect ( c . get ( queued ) . status ) . toBe ( "active" ) ;
321- expect ( onSettled ) . not . toHaveBeenCalled ( ) ;
322- // Work that never started stops at once.
323- const waiting = start ( "waiting" ) ;
324- expect ( await c . stop ( waiting ) ) . toMatchObject ( { status : "stopped" } ) ;
159+ expect ( workers [ 1 ] ! . run ) . toHaveBeenCalledExactlyOnceWith ( "brief\n\nAgain." ) ;
325160 workers [ 1 ] ! . close ( ) ;
326161 await tick ( ) ;
327162 await c . dispose ( ) ;
328163 } ) ;
329-
330- it ( "dispose ends every process before it resolves" , async ( ) => {
331- const { c, workers, start } = setup ( 2 ) ;
332- start ( ) ;
333- start ( ) ;
334- let done = false ;
335- const disposing = c . dispose ( ) . then ( ( ) => {
336- done = true ;
337- } ) ;
338- await tick ( ) ;
339- expect ( done ) . toBe ( false ) ;
340- for ( const worker of workers ) {
341- expect ( worker . stop ) . toHaveBeenCalledOnce ( ) ;
342- worker . close ( { interrupted : true } ) ;
343- }
344- await disposing ;
345- expect ( c . list ( ) ) . toEqual ( [ ] ) ;
346- expect ( ( ) => start ( ) ) . toThrow ( "disposed" ) ;
347- } ) ;
348164} ) ;
349165
350- describe ( "retained reports" , ( ) => {
351- it ( "keeps immutable full reports across continuations and removal until disposal" , async ( ) => {
352- const { c, workers, start } = setup ( ) ;
353- const id = start ( ) ;
354- const response = "é" . repeat ( 4000 ) ;
355- const error = "failure " . repeat ( 500 ) ;
356- workers [ 0 ] ! . close ( { text : response , error } ) ;
357- await tick ( ) ;
358- const first = c . get ( id ) ;
359- expect ( Buffer . byteLength ( first . response ) ) . toBeLessThanOrEqual ( 512 ) ;
360- expect ( Buffer . byteLength ( first . error ! ) ) . toBeLessThanOrEqual ( 240 ) ;
361- const ref = first . report ! ;
362- expect ( ref ) . toBeDefined ( ) ;
363- const original = ref . id ;
364- first . report ! . id = "modified" ;
365- expect ( c . get ( id ) . report ! . id ) . toBe ( original ) ;
366- let text = "" ,
367- offset : number | undefined = 0 ;
368- do {
369- const part = c . reports . read ( original , offset ) ;
370- text += part . text ;
371- offset = part . next ;
372- } while ( offset !== undefined ) ;
373- expect ( JSON . parse ( text ) ) . toMatchObject ( { response, error } ) ;
374- await c . message ( id , "continue" ) ;
375- expect ( c . get ( id ) . report ) . toBeUndefined ( ) ;
376- workers [ 1 ] ! . close ( { text : "next" } ) ;
377- await tick ( ) ;
378- const next = c . accept ( id ) . report ! ;
379- expect ( next . id ) . not . toBe ( original ) ;
380- expect ( c . reports . read ( original ) . text ) . toBe (
381- text . slice ( 0 , c . reports . read ( original ) . text . length ) ,
382- ) ;
383- expect ( JSON . parse ( c . reports . read ( next . id ) . text ) . response ) . toBe ( "next" ) ;
384- await c . dispose ( ) ;
385- expect ( ( ) => c . reports . read ( original ) ) . toThrow ( ) ;
386- } ) ;
387-
388- it ( "captures stopped executions and does not treat streamed previews as journal output" , async ( ) => {
389- const { c, workers, start } = setup ( ) ;
390- const id = start ( ) ;
391- workers [ 0 ] ! . emit ( {
392- type : "message_end" ,
393- message : {
394- role : "assistant" ,
395- content : [ { type : "text" , text : "x" . repeat ( 4000 ) } ] ,
396- stopReason : "stop" ,
397- usage : usage ( 0 ) ,
398- } ,
399- } as unknown as WorkerEvent ) ;
400- expect ( Buffer . byteLength ( c . get ( id ) . response ) ) . toBeLessThanOrEqual ( 512 ) ;
401- const stopping = c . stop ( id ) ;
402- workers [ 0 ] ! . close ( { text : "" , interrupted : true } ) ;
403- const view = await stopping ;
404- expect ( view . response ) . toBe ( "" ) ;
405- expect ( JSON . parse ( c . reports . read ( view . report ! . id ) . text ) ) . toMatchObject ( {
406- status : "stopped" ,
407- response : "" ,
408- } ) ;
409- await c . dispose ( ) ;
410- } ) ;
411-
412- it ( "removes a queued task synchronously before a message can start it" , async ( ) => {
413- const { c, workers, start } = setup ( ) ;
414- const active = start ( ) ;
415- const queued = start ( "queued" ) ;
416- const stopping = c . stop ( queued ) ;
417- await expect ( c . message ( queued , "late" ) ) . rejects . toThrow ( "Unknown task" ) ;
418- await stopping ;
419- workers [ 0 ] ! . close ( ) ;
420- await tick ( ) ;
421- expect ( workers ) . toHaveLength ( 1 ) ;
422- await c . stop ( active ) ;
423- await c . dispose ( ) ;
424- } ) ;
425-
166+ describe ( "synchronous races" , ( ) => {
426167 it ( "preserves the worker limit when observers synchronously queue work" , async ( ) => {
427168 const { c, workers, start } = setup ( 2 ) ;
428169 const first = start ( "first" ) ;
@@ -454,56 +195,6 @@ describe("retained reports", () => {
454195 await c . stop ( first ) ;
455196 await c . dispose ( ) ;
456197 } ) ;
457-
458- it ( "admits queued work even if onSettled throws" , async ( ) => {
459- const workers : Fake [ ] = [ ] ;
460- const c = new Coordinator ( {
461- maxWorkers : 1 ,
462- createWorker : ( _options , file , continues ) => {
463- const worker = new Fake ( file , continues ?? false ) ;
464- workers . push ( worker ) ;
465- return worker ;
466- } ,
467- onSettled : ( ) => {
468- throw new Error ( "observer" ) ;
469- } ,
470- } ) ;
471- const start = ( ) =>
472- c . start ( {
473- prompt : "work" ,
474- description : "work" ,
475- worker : options ,
476- } ) ;
477- start ( ) ;
478- start ( ) ;
479- workers [ 0 ] ! . close ( ) ;
480- await tick ( ) ;
481- expect ( workers ) . toHaveLength ( 2 ) ;
482- workers [ 1 ] ! . close ( ) ;
483- await tick ( ) ;
484- await c . dispose ( ) ;
485- } ) ;
486-
487- it ( "survives capture failure and admits queued work" , async ( ) => {
488- const { c, workers, start } = setup ( ) ;
489- const id = start ( ) ;
490- start ( "queued" ) ;
491- vi . spyOn ( c . reports , "put" ) . mockImplementationOnce ( ( ) => {
492- throw new Error ( "disk full" ) ;
493- } ) ;
494- workers [ 0 ] ! . close ( { text : "x" . repeat ( 5000 ) } ) ;
495- await tick ( ) ;
496- expect ( c . get ( id ) ) . toMatchObject ( {
497- status : "failed" ,
498- error : expect . stringContaining ( "Report capture failed" ) ,
499- } ) ;
500- expect ( c . get ( id ) . report ) . toBeUndefined ( ) ;
501- expect ( Buffer . byteLength ( c . get ( id ) . response ) ) . toBeLessThanOrEqual ( 512 ) ;
502- expect ( workers ) . toHaveLength ( 2 ) ;
503- workers [ 1 ] ! . close ( ) ;
504- await tick ( ) ;
505- await c . dispose ( ) ;
506- } ) ;
507198} ) ;
508199
509200describe ( "usage" , ( ) => {
@@ -552,7 +243,6 @@ describe("waiting observes", () => {
552243 const aborted = c . wait ( id , abort . signal ) ;
553244 abort . abort ( ) ;
554245 await expect ( aborted ) . rejects . toMatchObject ( { name : "AbortError" } ) ;
555- expect ( ( ) => c . wait ( "unknown" ) ) . toThrow ( "Unknown task" ) ;
556246
557247 const paused = c . wait ( id ) ;
558248 const pausing = c . pause ( id ) ;
@@ -565,12 +255,6 @@ describe("waiting observes", () => {
565255 expect ( await settled ) . toMatchObject ( { status : "failed" } ) ;
566256 expect ( await c . wait ( id ) ) . toMatchObject ( { status : "failed" } ) ;
567257
568- const other = start ( "other" ) ;
569- const removed = c . wait ( other ) ;
570- const stopping = c . stop ( other ) ;
571- workers [ 2 ] ! . close ( { interrupted : true } ) ;
572- await stopping ;
573- await expect ( removed ) . rejects . toThrow ( "Task was stopped" ) ;
574258 await c . dispose ( ) ;
575259 } ) ;
576260} ) ;
0 commit comments