@@ -370,6 +370,157 @@ describe("OrchestrationEngine", () => {
370370 await system . dispose ( ) ;
371371 } ) ;
372372
373+ it ( "prioritizes client commands ahead of queued internal stream commands" , async ( ) => {
374+ const system = await createOrchestrationSystem ( ) ;
375+ const { engine } = system ;
376+ const createdAt = now ( ) ;
377+ const threadId = ThreadId . makeUnsafe ( "thread-priority-lane" ) ;
378+
379+ await system . run (
380+ engine . dispatch ( {
381+ type : "project.create" ,
382+ commandId : CommandId . makeUnsafe ( "cmd-project-priority-lane-create" ) ,
383+ projectId : asProjectId ( "project-priority-lane" ) ,
384+ title : "Priority Lane Project" ,
385+ workspaceRoot : "/tmp/project-priority-lane" ,
386+ defaultModelSelection : {
387+ provider : "codex" ,
388+ model : "gpt-5-codex" ,
389+ } ,
390+ createdAt,
391+ } ) ,
392+ ) ;
393+ await system . run (
394+ engine . dispatch ( {
395+ type : "thread.create" ,
396+ commandId : CommandId . makeUnsafe ( "cmd-thread-priority-lane-create" ) ,
397+ threadId,
398+ projectId : asProjectId ( "project-priority-lane" ) ,
399+ title : "Priority Lane Thread" ,
400+ modelSelection : {
401+ provider : "codex" ,
402+ model : "gpt-5-codex" ,
403+ } ,
404+ interactionMode : DEFAULT_PROVIDER_INTERACTION_MODE ,
405+ runtimeMode : "full-access" ,
406+ branch : null ,
407+ worktreePath : null ,
408+ createdAt,
409+ } ) ,
410+ ) ;
411+
412+ const lowPriorityCommandCount = 250 ;
413+ const lowPriorityDispatches = Array . from ( { length : lowPriorityCommandCount } , ( _ , index ) =>
414+ system . run (
415+ engine . dispatch ( {
416+ type : "thread.message.assistant.delta" ,
417+ commandId : CommandId . makeUnsafe ( `cmd-thread-priority-lane-delta-${ index } ` ) ,
418+ threadId,
419+ messageId : asMessageId ( "assistant-priority-lane" ) ,
420+ delta : `chunk-${ index } ` ,
421+ turnId : asTurnId ( "turn-priority-lane" ) ,
422+ createdAt,
423+ } ) ,
424+ ) ,
425+ ) ;
426+
427+ const archiveResult = await system . run (
428+ engine . dispatch ( {
429+ type : "thread.archive" ,
430+ commandId : CommandId . makeUnsafe ( "cmd-thread-priority-lane-archive" ) ,
431+ threadId,
432+ } ) ,
433+ ) ;
434+ const lowPriorityResults = await Promise . all ( lowPriorityDispatches ) ;
435+ const lowPriorityCommandsAheadOfArchive = lowPriorityResults . filter (
436+ ( result ) => result . sequence < archiveResult . sequence ,
437+ ) . length ;
438+
439+ expect ( lowPriorityCommandsAheadOfArchive ) . toBeLessThan ( lowPriorityCommandCount / 3 ) ;
440+ expect ( archiveResult . sequence ) . toBeLessThan ( lowPriorityResults . at ( - 1 ) ?. sequence ?? Infinity ) ;
441+ await system . dispose ( ) ;
442+ } ) ;
443+
444+ it ( "treats normalized thread.turn.start as a prioritized client command" , async ( ) => {
445+ const system = await createOrchestrationSystem ( ) ;
446+ const { engine } = system ;
447+ const createdAt = now ( ) ;
448+ const threadId = ThreadId . makeUnsafe ( "thread-priority-turn-start" ) ;
449+
450+ await system . run (
451+ engine . dispatch ( {
452+ type : "project.create" ,
453+ commandId : CommandId . makeUnsafe ( "cmd-project-priority-turn-start-create" ) ,
454+ projectId : asProjectId ( "project-priority-turn-start" ) ,
455+ title : "Priority Turn Start Project" ,
456+ workspaceRoot : "/tmp/project-priority-turn-start" ,
457+ defaultModelSelection : {
458+ provider : "codex" ,
459+ model : "gpt-5-codex" ,
460+ } ,
461+ createdAt,
462+ } ) ,
463+ ) ;
464+ await system . run (
465+ engine . dispatch ( {
466+ type : "thread.create" ,
467+ commandId : CommandId . makeUnsafe ( "cmd-thread-priority-turn-start-create" ) ,
468+ threadId,
469+ projectId : asProjectId ( "project-priority-turn-start" ) ,
470+ title : "Priority Turn Start Thread" ,
471+ modelSelection : {
472+ provider : "codex" ,
473+ model : "gpt-5-codex" ,
474+ } ,
475+ interactionMode : DEFAULT_PROVIDER_INTERACTION_MODE ,
476+ runtimeMode : "full-access" ,
477+ branch : null ,
478+ worktreePath : null ,
479+ createdAt,
480+ } ) ,
481+ ) ;
482+
483+ const lowPriorityCommandCount = 150 ;
484+ const lowPriorityDispatches = Array . from ( { length : lowPriorityCommandCount } , ( _ , index ) =>
485+ system . run (
486+ engine . dispatch ( {
487+ type : "thread.message.assistant.delta" ,
488+ commandId : CommandId . makeUnsafe ( `cmd-thread-priority-turn-start-delta-${ index } ` ) ,
489+ threadId,
490+ messageId : asMessageId ( "assistant-priority-turn-start" ) ,
491+ delta : `chunk-${ index } ` ,
492+ turnId : asTurnId ( "turn-priority-turn-start-internal" ) ,
493+ createdAt,
494+ } ) ,
495+ ) ,
496+ ) ;
497+
498+ const turnStartResult = await system . run (
499+ engine . dispatch ( {
500+ type : "thread.turn.start" ,
501+ commandId : CommandId . makeUnsafe ( "cmd-thread-priority-turn-start" ) ,
502+ threadId,
503+ message : {
504+ messageId : asMessageId ( "user-priority-turn-start" ) ,
505+ role : "user" ,
506+ text : "hello" ,
507+ attachments : [ ] ,
508+ } ,
509+ interactionMode : DEFAULT_PROVIDER_INTERACTION_MODE ,
510+ runtimeMode : "full-access" ,
511+ createdAt,
512+ } ) ,
513+ ) ;
514+ const lowPriorityResults = await Promise . all ( lowPriorityDispatches ) ;
515+ const lowPriorityCommandsAheadOfTurnStart = lowPriorityResults . filter (
516+ ( result ) => result . sequence < turnStartResult . sequence ,
517+ ) . length ;
518+
519+ expect ( lowPriorityCommandsAheadOfTurnStart ) . toBeLessThan ( lowPriorityCommandCount / 3 ) ;
520+ expect ( turnStartResult . sequence ) . toBeLessThan ( lowPriorityResults . at ( - 1 ) ?. sequence ?? Infinity ) ;
521+ await system . dispose ( ) ;
522+ } ) ;
523+
373524 it ( "streams persisted domain events in order" , async ( ) => {
374525 const system = await createOrchestrationSystem ( ) ;
375526 const { engine } = system ;
0 commit comments