@@ -1501,7 +1501,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive", (it) => {
15011501 } ) ,
15021502 ) ;
15031503
1504- it . effect ( "merges an explicit provider-finished run while checkpoint capture is pending " , ( ) =>
1504+ it . effect ( "refuses busy merge-back targets without blocking the queue " , ( ) =>
15051505 Effect . gen ( function * ( ) {
15061506 const orchestrator = yield * Orchestrator . OrchestratorV2 ;
15071507 const eventSink = yield * EventSink . EventSinkV2 ;
@@ -1516,6 +1516,13 @@ it.layer(layerTest)("OrchestrationV2LayerLive", (it) => {
15161516 ) ;
15171517 const forkTransferId = ContextTransferId . make ( "runtime-layer-waiting-merge-fork-transfer" ) ;
15181518
1519+ yield * seedProject ( {
1520+ projectId,
1521+ title : "Merge-back project" ,
1522+ workspaceRoot : process . cwd ( ) ,
1523+ defaultModelSelection : modelSelection ,
1524+ createdAt : DateTime . formatIso ( now ) ,
1525+ } ) ;
15191526 yield * orchestrator . dispatch ( {
15201527 type : "thread.create" ,
15211528 createdBy : "user" ,
@@ -1643,6 +1650,87 @@ it.layer(layerTest)("OrchestrationV2LayerLive", (it) => {
16431650 ] ,
16441651 } ) ;
16451652
1653+ yield * orchestrator . dispatch ( {
1654+ type : "message.dispatch" ,
1655+ createdBy : "user" ,
1656+ creationSource : "web" ,
1657+ commandId : CommandId . make ( "runtime-layer-merge-target-start" ) ,
1658+ threadId : targetThreadId ,
1659+ messageId : MessageId . make ( "runtime-layer-merge-target-message" ) ,
1660+ text : "Keep working in the parent." ,
1661+ attachments : [ ] ,
1662+ dispatchMode : { type : "start_immediately" } ,
1663+ } ) ;
1664+ const activeTarget = yield * orchestrator . getThreadProjection ( targetThreadId ) ;
1665+ const targetRun = activeTarget . runs [ 0 ] ! ;
1666+ for ( const status of [ "preparing" , "starting" , "waiting" , "queued" , "running" ] as const ) {
1667+ yield * eventSink . write ( {
1668+ commandId : CommandId . make ( `runtime-layer-merge-target-${ status } ` ) ,
1669+ events : [
1670+ {
1671+ id : EventId . make ( `runtime-layer-merge-target-${ status } -event` ) ,
1672+ type : "run.updated" ,
1673+ threadId : targetThreadId ,
1674+ runId : targetRun . id ,
1675+ occurredAt : now ,
1676+ payload : { ...targetRun , status } ,
1677+ } ,
1678+ ] ,
1679+ } ) ;
1680+ const error = yield * orchestrator
1681+ . dispatch ( {
1682+ type : "thread.merge_back" ,
1683+ createdBy : "user" ,
1684+ creationSource : "mobile" ,
1685+ commandId : CommandId . make ( `runtime-layer-merge-refused-${ status } ` ) ,
1686+ sourceThreadId,
1687+ targetThreadId,
1688+ sourcePoint : { type : "run" , runId : sourceRunId } ,
1689+ } )
1690+ . pipe ( Effect . flip ) ;
1691+ assert . instanceOf ( error , Orchestrator . OrchestratorDispatchError ) ;
1692+ assert . equal (
1693+ error . cause ,
1694+ "Finish or stop the target thread's active work and clear its queue before merging back." ,
1695+ ) ;
1696+ const refusedTarget = yield * orchestrator . getThreadProjection ( targetThreadId ) ;
1697+ assert . deepEqual ( refusedTarget . contextTransfers , activeTarget . contextTransfers ) ;
1698+ assert . equal ( refusedTarget . runs [ 0 ] ?. status , status ) ;
1699+ }
1700+ yield * orchestrator . dispatch ( {
1701+ type : "message.dispatch" ,
1702+ createdBy : "user" ,
1703+ creationSource : "web" ,
1704+ commandId : CommandId . make ( "runtime-layer-merge-target-queue" ) ,
1705+ threadId : targetThreadId ,
1706+ messageId : MessageId . make ( "runtime-layer-merge-target-queued-message" ) ,
1707+ text : "Run this after the current turn." ,
1708+ attachments : [ ] ,
1709+ dispatchMode : { type : "queue_after_active" } ,
1710+ } ) ;
1711+ const queuedTarget = yield * orchestrator . getThreadProjection ( targetThreadId ) ;
1712+ const queuedRun = queuedTarget . runs . find ( ( run ) => run . status === "queued" ) ;
1713+ assert . isDefined ( queuedRun ) ;
1714+ yield * orchestrator . dispatch ( {
1715+ type : "queued-run.cancel" ,
1716+ commandId : CommandId . make ( "runtime-layer-merge-target-cancel-queue" ) ,
1717+ threadId : targetThreadId ,
1718+ runId : queuedRun . id ,
1719+ } ) ;
1720+ yield * eventSink . write ( {
1721+ commandId : CommandId . make ( "runtime-layer-merge-target-complete" ) ,
1722+ events : [
1723+ {
1724+ id : EventId . make ( "runtime-layer-merge-target-completed-event" ) ,
1725+ type : "run.updated" ,
1726+ threadId : targetThreadId ,
1727+ runId : targetRun . id ,
1728+ occurredAt : now ,
1729+ payload : { ...targetRun , status : "completed" , completedAt : now } ,
1730+ } ,
1731+ ] ,
1732+ } ) ;
1733+
16461734 yield * orchestrator . dispatch ( {
16471735 type : "thread.merge_back" ,
16481736 createdBy : "user" ,
0 commit comments