@@ -1707,9 +1707,20 @@ it.effect.each([
17071707 [ "switch" , "other-claude" , true , true ] ,
17081708 [ "switch" , "codex" , true , false ] ,
17091709 [ "switch" , "other-codex" , false , false ] ,
1710+ [ "runtime-mode" , "codex" , true , true , false ] ,
1711+ [ "runtime-mode" , "codex" , true , false , true ] ,
1712+ [ "runtime-mode" , "codex" , true , false , false , false ] ,
1713+ [ "runtime-mode" , "codex" , false , false , false ] ,
17101714] as const ) (
17111715 "%s to %s with temporary=%s rejects=%s before changing a live session" ,
1712- ( [ operation , target , temporary , rejects ] ) => {
1716+ ( [
1717+ operation ,
1718+ target ,
1719+ temporary ,
1720+ rejects ,
1721+ supportsRuntimeModeSwitchInSession = true ,
1722+ hasSession = true ,
1723+ ] ) => {
17131724 const threadId = ThreadId . make ( "thread:temporary-provider-switch" ) ;
17141725 const otherSelection = { instanceId : ProviderInstanceId . make ( target ) , model : "test" } ;
17151726 const owned = new Set < ThreadId > ( temporary ? [ threadId ] : [ ] ) ;
@@ -1760,74 +1771,95 @@ it.effect.each([
17601771 } ) ;
17611772 const now = yield * DateTime . now ;
17621773 const sessionId = ProviderSessionId . make ( "temporary-switch-session" ) ;
1763- yield * eventSink . write ( {
1764- events : [
1765- {
1766- id : EventId . make ( "temporary-switch-session-attached" ) ,
1767- type : "provider-session.attached" ,
1768- threadId,
1769- occurredAt : now ,
1770- payload : {
1771- id : sessionId ,
1772- driver : adapter . driver ,
1773- providerInstanceId : instanceId ,
1774- status : "ready" ,
1775- cwd : "/repo" ,
1776- model : modelSelection . model ,
1777- capabilities : {
1778- ...CodexProviderCapabilitiesV2 ,
1779- sessions : {
1780- ...CodexProviderCapabilitiesV2 . sessions ,
1781- supportsProviderSwitchingViaHandoff : false ,
1774+ if ( hasSession )
1775+ yield * eventSink . write ( {
1776+ events : [
1777+ {
1778+ id : EventId . make ( "temporary-switch-session-attached" ) ,
1779+ type : "provider-session.attached" ,
1780+ threadId,
1781+ occurredAt : now ,
1782+ payload : {
1783+ id : sessionId ,
1784+ driver : adapter . driver ,
1785+ providerInstanceId : instanceId ,
1786+ status : "ready" ,
1787+ cwd : "/repo" ,
1788+ model : modelSelection . model ,
1789+ capabilities : {
1790+ ...CodexProviderCapabilitiesV2 ,
1791+ sessions : {
1792+ ...CodexProviderCapabilitiesV2 . sessions ,
1793+ supportsProviderSwitchingViaHandoff : false ,
1794+ supportsRuntimeModeSwitchInSession,
1795+ } ,
17821796 } ,
1797+ createdAt : now ,
1798+ updatedAt : now ,
1799+ lastError : null ,
17831800 } ,
1784- createdAt : now ,
1785- updatedAt : now ,
1786- lastError : null ,
17871801 } ,
1788- } ,
1789- ] ,
1790- } ) ;
1802+ ] ,
1803+ } ) ;
17911804 const before = yield * projections . getThreadProviderContext ( threadId ) ;
17921805 const sequence = yield * orchestrator . getThreadEventSequence ( threadId ) ;
17931806 const commandId = CommandId . make ( "temporary-switch" ) ;
17941807 const refused = yield * Effect . result (
17951808 orchestrator . dispatch (
1796- operation !== "message "
1809+ operation === "runtime-mode "
17971810 ? {
1798- type : operation === "selection" ? " thread.model-selection .set" : "provider.switch ",
1811+ type : " thread.runtime-mode .set",
17991812 commandId,
18001813 threadId,
1801- modelSelection : otherSelection ,
1814+ runtimeMode : "approval-required" ,
18021815 }
1803- : {
1804- type : "message.dispatch" ,
1805- commandId,
1806- threadId,
1807- modelSelection : otherSelection ,
1808- messageId : MessageId . make ( "temporary-switch-message" ) ,
1809- text : "Switch provider" ,
1810- attachments : [ ] ,
1811- dispatchMode : { type : "start_immediately" } ,
1812- createdBy : "user" ,
1813- creationSource : "web" ,
1814- } ,
1816+ : operation !== "message"
1817+ ? {
1818+ type :
1819+ operation === "selection" ? "thread.model-selection.set" : "provider.switch" ,
1820+ commandId,
1821+ threadId,
1822+ modelSelection : otherSelection ,
1823+ }
1824+ : {
1825+ type : "message.dispatch" ,
1826+ commandId,
1827+ threadId,
1828+ modelSelection : otherSelection ,
1829+ messageId : MessageId . make ( "temporary-switch-message" ) ,
1830+ text : "Switch provider" ,
1831+ attachments : [ ] ,
1832+ dispatchMode : { type : "start_immediately" } ,
1833+ createdBy : "user" ,
1834+ creationSource : "web" ,
1835+ } ,
18151836 ) ,
18161837 ) ;
18171838 assert . equal ( Result . isFailure ( refused ) , rejects ) ;
18181839 const projection = yield * orchestrator . getThreadRecords ( threadId , [ "runs" ] ) ;
18191840 assert . isEmpty ( projection . runs ) ;
18201841 if ( rejects ) {
1842+ if ( operation === "runtime-mode" && Result . isFailure ( refused ) ) {
1843+ assert . instanceOf ( refused . failure , Orchestrator . OrchestratorDispatchError ) ;
1844+ if ( refused . failure . _tag === "OrchestratorDispatchError" ) {
1845+ assert . equal ( refused . failure . cause , "Keep this thread before using this operation." ) ;
1846+ }
1847+ }
18211848 assert . deepEqual ( yield * projections . getThreadProviderContext ( threadId ) , before ) ;
18221849 assert . equal ( yield * orchestrator . getThreadEventSequence ( threadId ) , sequence ) ;
18231850 assert . isEmpty ( yield * eventSink . readByCommandId ( { commandId } ) . pipe ( Stream . runCollect ) ) ;
18241851 assert . isEmpty ( yield * outbox . listByCommandId ( commandId ) ) ;
18251852 } else {
1826- assert . deepEqual ( projection . thread . modelSelection , otherSelection ) ;
1853+ if ( operation === "runtime-mode" ) {
1854+ assert . equal ( projection . thread . runtimeMode , "approval-required" ) ;
1855+ assert . deepEqual ( projection . thread . modelSelection , modelSelection ) ;
1856+ } else {
1857+ assert . deepEqual ( projection . thread . modelSelection , otherSelection ) ;
1858+ }
18271859 if ( temporary ) {
18281860 assert . equal (
18291861 ( yield * projections . getThreadProviderContext ( threadId ) ) . providerSessions [ 0 ] ?. id ,
1830- sessionId ,
1862+ hasSession ? sessionId : undefined ,
18311863 ) ;
18321864 assert . isEmpty ( yield * outbox . listByCommandId ( commandId ) ) ;
18331865 }
0 commit comments