@@ -1726,7 +1726,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive", (it) => {
17261726 } ) ,
17271727 ) ;
17281728
1729- it . effect ( "merges an explicit provider-finished run while checkpoint capture is pending " , ( ) =>
1729+ it . effect ( "refuses busy merge-back targets without blocking the queue " , ( ) =>
17301730 Effect . gen ( function * ( ) {
17311731 const orchestrator = yield * Orchestrator . OrchestratorV2 ;
17321732 const eventSink = yield * EventSink . EventSinkV2 ;
@@ -1741,6 +1741,13 @@ it.layer(layerTest)("OrchestrationV2LayerLive", (it) => {
17411741 ) ;
17421742 const forkTransferId = ContextTransferId . make ( "runtime-layer-waiting-merge-fork-transfer" ) ;
17431743
1744+ yield * seedProject ( {
1745+ projectId,
1746+ title : "Merge-back project" ,
1747+ workspaceRoot : process . cwd ( ) ,
1748+ defaultModelSelection : modelSelection ,
1749+ createdAt : DateTime . formatIso ( now ) ,
1750+ } ) ;
17441751 yield * orchestrator . dispatch ( {
17451752 type : "thread.create" ,
17461753 createdBy : "user" ,
@@ -1868,6 +1875,87 @@ it.layer(layerTest)("OrchestrationV2LayerLive", (it) => {
18681875 ] ,
18691876 } ) ;
18701877
1878+ yield * orchestrator . dispatch ( {
1879+ type : "message.dispatch" ,
1880+ createdBy : "user" ,
1881+ creationSource : "web" ,
1882+ commandId : CommandId . make ( "runtime-layer-merge-target-start" ) ,
1883+ threadId : targetThreadId ,
1884+ messageId : MessageId . make ( "runtime-layer-merge-target-message" ) ,
1885+ text : "Keep working in the parent." ,
1886+ attachments : [ ] ,
1887+ dispatchMode : { type : "start_immediately" } ,
1888+ } ) ;
1889+ const activeTarget = yield * orchestrator . getThreadProjection ( targetThreadId ) ;
1890+ const targetRun = activeTarget . runs [ 0 ] ! ;
1891+ for ( const status of [ "preparing" , "starting" , "waiting" , "queued" , "running" ] as const ) {
1892+ yield * eventSink . write ( {
1893+ commandId : CommandId . make ( `runtime-layer-merge-target-${ status } ` ) ,
1894+ events : [
1895+ {
1896+ id : EventId . make ( `runtime-layer-merge-target-${ status } -event` ) ,
1897+ type : "run.updated" ,
1898+ threadId : targetThreadId ,
1899+ runId : targetRun . id ,
1900+ occurredAt : now ,
1901+ payload : { ...targetRun , status } ,
1902+ } ,
1903+ ] ,
1904+ } ) ;
1905+ const error = yield * orchestrator
1906+ . dispatch ( {
1907+ type : "thread.merge_back" ,
1908+ createdBy : "user" ,
1909+ creationSource : "mobile" ,
1910+ commandId : CommandId . make ( `runtime-layer-merge-refused-${ status } ` ) ,
1911+ sourceThreadId,
1912+ targetThreadId,
1913+ sourcePoint : { type : "run" , runId : sourceRunId } ,
1914+ } )
1915+ . pipe ( Effect . flip ) ;
1916+ assert . instanceOf ( error , Orchestrator . OrchestratorDispatchError ) ;
1917+ assert . equal (
1918+ error . cause ,
1919+ "Finish or stop the target thread's active work and clear its queue before merging back." ,
1920+ ) ;
1921+ const refusedTarget = yield * orchestrator . getThreadProjection ( targetThreadId ) ;
1922+ assert . deepEqual ( refusedTarget . contextTransfers , activeTarget . contextTransfers ) ;
1923+ assert . equal ( refusedTarget . runs [ 0 ] ?. status , status ) ;
1924+ }
1925+ yield * orchestrator . dispatch ( {
1926+ type : "message.dispatch" ,
1927+ createdBy : "user" ,
1928+ creationSource : "web" ,
1929+ commandId : CommandId . make ( "runtime-layer-merge-target-queue" ) ,
1930+ threadId : targetThreadId ,
1931+ messageId : MessageId . make ( "runtime-layer-merge-target-queued-message" ) ,
1932+ text : "Run this after the current turn." ,
1933+ attachments : [ ] ,
1934+ dispatchMode : { type : "queue_after_active" } ,
1935+ } ) ;
1936+ const queuedTarget = yield * orchestrator . getThreadProjection ( targetThreadId ) ;
1937+ const queuedRun = queuedTarget . runs . find ( ( run ) => run . status === "queued" ) ;
1938+ assert . isDefined ( queuedRun ) ;
1939+ yield * orchestrator . dispatch ( {
1940+ type : "queued-run.cancel" ,
1941+ commandId : CommandId . make ( "runtime-layer-merge-target-cancel-queue" ) ,
1942+ threadId : targetThreadId ,
1943+ runId : queuedRun . id ,
1944+ } ) ;
1945+ yield * eventSink . write ( {
1946+ commandId : CommandId . make ( "runtime-layer-merge-target-complete" ) ,
1947+ events : [
1948+ {
1949+ id : EventId . make ( "runtime-layer-merge-target-completed-event" ) ,
1950+ type : "run.updated" ,
1951+ threadId : targetThreadId ,
1952+ runId : targetRun . id ,
1953+ occurredAt : now ,
1954+ payload : { ...targetRun , status : "completed" , completedAt : now } ,
1955+ } ,
1956+ ] ,
1957+ } ) ;
1958+
18711959 yield * orchestrator . dispatch ( {
18721960 type : "thread.merge_back" ,
18731961 createdBy : "user" ,
0 commit comments