@@ -1664,3 +1664,81 @@ it.effect("an unanswered Stop is flagged until Force stop ends the run with its
16641664 assert . lengthOf ( late . storedEvents , 0 ) ;
16651665 } ) . pipe ( Effect . provide ( layerTest ) ) ,
16661666) ;
1667+
1668+ it . effect . each ( [ "Stop" , "Force stop" ] as const ) (
1669+ "%s ends a running run whose provider turn already completed" ,
1670+ ( action ) =>
1671+ Effect . gen ( function * ( ) {
1672+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
1673+ const projections = yield * ProjectionStore . ProjectionStoreV2 ;
1674+ const threadId = ThreadId . make ( "thread:stop-completed-turn" ) ;
1675+ yield * createWatchingThread ( threadId , 7 ) ;
1676+ yield * send ( threadId , "go" , "start_immediately" ) ;
1677+ const now = yield * DateTime . now ;
1678+ const started = yield * orchestrator . getThreadProjection ( threadId ) ;
1679+ const run = started . runs [ 0 ] ! ;
1680+ const attempt = started . attempts [ 0 ] ! ;
1681+ const providerThreadId = run . providerThreadId ! ;
1682+ const providerTurnId = ProviderTurnId . make ( "provider-turn:stop-completed-turn" ) ;
1683+ const apply = ( event : Omit < OrchestrationV2DomainEvent , "id" | "threadId" > ) =>
1684+ projections . apply ( {
1685+ ...event ,
1686+ id : EventId . make ( `event:stop-completed-turn:${ event . type } ` ) ,
1687+ threadId,
1688+ } as OrchestrationV2DomainEvent ) ;
1689+ yield * apply ( {
1690+ type : "run.updated" ,
1691+ runId : run . id ,
1692+ occurredAt : now ,
1693+ payload : { ...run , status : "running" , startedAt : now , activeAttemptId : attempt . id } ,
1694+ } ) ;
1695+ yield * apply ( {
1696+ type : "run-attempt.updated" ,
1697+ occurredAt : now ,
1698+ payload : { ...attempt , status : "completed" , startedAt : now , completedAt : now } ,
1699+ } ) ;
1700+ yield * apply ( {
1701+ type : "provider-turn.updated" ,
1702+ occurredAt : now ,
1703+ payload : {
1704+ id : providerTurnId ,
1705+ providerThreadId,
1706+ nodeId : run . rootNodeId ! ,
1707+ runAttemptId : attempt . id ,
1708+ nativeTurnRef : null ,
1709+ ordinal : 1 ,
1710+ status : "completed" ,
1711+ startedAt : now ,
1712+ completedAt : now ,
1713+ } ,
1714+ } ) ;
1715+ // The provider session was already released.
1716+ yield * apply ( {
1717+ type : "provider-thread.updated" ,
1718+ occurredAt : now ,
1719+ payload : {
1720+ ...started . providerThreads . find ( ( thread ) => thread . id === providerThreadId ) ! ,
1721+ providerSessionId : ProviderSessionId . make ( "session:gone" ) ,
1722+ } ,
1723+ } ) ;
1724+ yield * send ( threadId , "follow" , "queue_after_active" ) ;
1725+
1726+ if ( action === "Stop" ) {
1727+ yield * orchestrator . dispatch ( {
1728+ type : "thread.stop" ,
1729+ commandId : CommandId . make ( "stop-completed-turn" ) ,
1730+ threadId,
1731+ } ) ;
1732+ } else {
1733+ const threads = yield * ThreadManagementService . ThreadManagementService ;
1734+ yield * threads . forceStopThread ( { threadId } ) ;
1735+ }
1736+ const stopped = yield * orchestrator . getThreadProjection ( threadId ) ;
1737+ assert . notEqual ( stopped . runs [ 0 ] ?. status , "running" , "the stopped run is still running" ) ;
1738+ assert . deepInclude ( stopped . attempts [ 0 ] , { status : "completed" , completedAt : now } ) ;
1739+ assert . isTrue (
1740+ stopped . turnItems . some ( ( item ) => item . type === "run_interrupt_result" ) ,
1741+ "the stopped run has no interrupt result" ,
1742+ ) ;
1743+ } ) . pipe ( Effect . provide ( layerTest ) ) ,
1744+ ) ;
0 commit comments