@@ -1373,7 +1373,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive", (it) => {
13731373 } ) ,
13741374 ) ;
13751375
1376- it . effect ( "merges an explicit provider-finished run while checkpoint capture is pending " , ( ) =>
1376+ it . effect ( "refuses busy merge-back targets without blocking the queue " , ( ) =>
13771377 Effect . gen ( function * ( ) {
13781378 const orchestrator = yield * Orchestrator . OrchestratorV2 ;
13791379 const eventSink = yield * EventSink . EventSinkV2 ;
@@ -1388,6 +1388,13 @@ it.layer(layerTest)("OrchestrationV2LayerLive", (it) => {
13881388 ) ;
13891389 const forkTransferId = ContextTransferId . make ( "runtime-layer-waiting-merge-fork-transfer" ) ;
13901390
1391+ yield * seedProject ( {
1392+ projectId,
1393+ title : "Merge-back project" ,
1394+ workspaceRoot : process . cwd ( ) ,
1395+ defaultModelSelection : modelSelection ,
1396+ createdAt : DateTime . formatIso ( now ) ,
1397+ } ) ;
13911398 yield * orchestrator . dispatch ( {
13921399 type : "thread.create" ,
13931400 createdBy : "user" ,
@@ -1515,6 +1522,87 @@ it.layer(layerTest)("OrchestrationV2LayerLive", (it) => {
15151522 ] ,
15161523 } ) ;
15171524
1525+ yield * orchestrator . dispatch ( {
1526+ type : "message.dispatch" ,
1527+ createdBy : "user" ,
1528+ creationSource : "web" ,
1529+ commandId : CommandId . make ( "runtime-layer-merge-target-start" ) ,
1530+ threadId : targetThreadId ,
1531+ messageId : MessageId . make ( "runtime-layer-merge-target-message" ) ,
1532+ text : "Keep working in the parent." ,
1533+ attachments : [ ] ,
1534+ dispatchMode : { type : "start_immediately" } ,
1535+ } ) ;
1536+ const activeTarget = yield * orchestrator . getThreadProjection ( targetThreadId ) ;
1537+ const targetRun = activeTarget . runs [ 0 ] ! ;
1538+ for ( const status of [ "preparing" , "starting" , "waiting" , "queued" , "running" ] as const ) {
1539+ yield * eventSink . write ( {
1540+ commandId : CommandId . make ( `runtime-layer-merge-target-${ status } ` ) ,
1541+ events : [
1542+ {
1543+ id : EventId . make ( `runtime-layer-merge-target-${ status } -event` ) ,
1544+ type : "run.updated" ,
1545+ threadId : targetThreadId ,
1546+ runId : targetRun . id ,
1547+ occurredAt : now ,
1548+ payload : { ...targetRun , status } ,
1549+ } ,
1550+ ] ,
1551+ } ) ;
1552+ const error = yield * orchestrator
1553+ . dispatch ( {
1554+ type : "thread.merge_back" ,
1555+ createdBy : "user" ,
1556+ creationSource : "mobile" ,
1557+ commandId : CommandId . make ( `runtime-layer-merge-refused-${ status } ` ) ,
1558+ sourceThreadId,
1559+ targetThreadId,
1560+ sourcePoint : { type : "run" , runId : sourceRunId } ,
1561+ } )
1562+ . pipe ( Effect . flip ) ;
1563+ assert . instanceOf ( error , Orchestrator . OrchestratorDispatchError ) ;
1564+ assert . equal (
1565+ error . cause ,
1566+ "Finish or stop the target thread's active work and clear its queue before merging back." ,
1567+ ) ;
1568+ const refusedTarget = yield * orchestrator . getThreadProjection ( targetThreadId ) ;
1569+ assert . deepEqual ( refusedTarget . contextTransfers , activeTarget . contextTransfers ) ;
1570+ assert . equal ( refusedTarget . runs [ 0 ] ?. status , status ) ;
1571+ }
1572+ yield * orchestrator . dispatch ( {
1573+ type : "message.dispatch" ,
1574+ createdBy : "user" ,
1575+ creationSource : "web" ,
1576+ commandId : CommandId . make ( "runtime-layer-merge-target-queue" ) ,
1577+ threadId : targetThreadId ,
1578+ messageId : MessageId . make ( "runtime-layer-merge-target-queued-message" ) ,
1579+ text : "Run this after the current turn." ,
1580+ attachments : [ ] ,
1581+ dispatchMode : { type : "queue_after_active" } ,
1582+ } ) ;
1583+ const queuedTarget = yield * orchestrator . getThreadProjection ( targetThreadId ) ;
1584+ const queuedRun = queuedTarget . runs . find ( ( run ) => run . status === "queued" ) ;
1585+ assert . isDefined ( queuedRun ) ;
1586+ yield * orchestrator . dispatch ( {
1587+ type : "queued-run.cancel" ,
1588+ commandId : CommandId . make ( "runtime-layer-merge-target-cancel-queue" ) ,
1589+ threadId : targetThreadId ,
1590+ runId : queuedRun . id ,
1591+ } ) ;
1592+ yield * eventSink . write ( {
1593+ commandId : CommandId . make ( "runtime-layer-merge-target-complete" ) ,
1594+ events : [
1595+ {
1596+ id : EventId . make ( "runtime-layer-merge-target-completed-event" ) ,
1597+ type : "run.updated" ,
1598+ threadId : targetThreadId ,
1599+ runId : targetRun . id ,
1600+ occurredAt : now ,
1601+ payload : { ...targetRun , status : "completed" , completedAt : now } ,
1602+ } ,
1603+ ] ,
1604+ } ) ;
1605+
15181606 yield * orchestrator . dispatch ( {
15191607 type : "thread.merge_back" ,
15201608 createdBy : "user" ,
0 commit comments