@@ -346,3 +346,92 @@ def test_codex_app_server_resolves_ref_and_freezes_profile_while_inline_remains_
346346 source .path .write_bytes (source .path .read_bytes () + b" " )
347347 with pytest .raises (ValueError , match = "content changed" ):
348348 run_codex_operation_host (resumed , ** options )
349+
350+
351+ @pytest .mark .parametrize (("host" , "transport" , "other" ), [
352+ ("dsh" , "oar-dsh-operation-mcp-v0" , "dsh-native-operation-tools-v0" ),
353+ ("codex-cli" , "oar-codex-operation-mcp-v0" , "app-server-operation-tools-v0" ),
354+ ])
355+ def test_oar_session_resumes_only_its_recorded_operation_transport (tmp_path , host , transport , other ):
356+ from loopx .control_plane .turn_driver .codex_sessions import _lineage , select_managed_operation_session
357+
358+ request = _managed_operation_turn_request ()
359+ options = dict (request = request , lineage = _lineage (request ), host = host ,
360+ profile_digest = "a" * 64 , model = "fixture-model" , reasoning_effort = "high" )
361+ select_managed_operation_session (tmp_path , transport = transport , ** options ).store ("owned-session" )
362+ request ["session" ]["action" ] = "resume"
363+ selected = select_managed_operation_session (tmp_path , transport = transport , ** options )
364+ assert selected .binding ["session_id" ] == "owned-session"
365+ with pytest .raises (ValueError , match = "profile changed" ):
366+ select_managed_operation_session (tmp_path , transport = other , ** options )
367+
368+
369+ @pytest .mark .parametrize ("returned_id" , ["replacement-session" , None ])
370+ def test_oar_refuses_missing_or_replaced_native_session_before_prompt (tmp_path , returned_id ):
371+ from loopx .control_plane .turn_driver .oar_operation_host import run_oar_operation_host
372+
373+ class Client :
374+ native_id = "original-session"
375+
376+ def __init__ (self ):
377+ self .prompts = []
378+ self .closed = []
379+
380+ def open_session (self , params ):
381+ if self .prompts :
382+ assert params ["resume" ] == "original-session"
383+ return {"session_key" : "fixture-key" , "native_session_id" : self .native_id }
384+
385+ def prompt (self , params , * , timeout ):
386+ self .prompts .append (params ["sessionKey" ])
387+ return {"text" : "{}" }
388+
389+ def close_session (self , key ):
390+ self .closed .append (key )
391+
392+ service , store = fixtures .service (tmp_path )
393+ client = Client ()
394+ request = _managed_operation_turn_request ()
395+ options = dict (host = "dsh" , runtime_id = "dsh" , runtime_root = store .root .parent .parent ,
396+ registry_path = service .registry_path , project = tmp_path / "project" ,
397+ dsh_home = tmp_path / "dsh" , max_tokens = 64 , provider = "deepseek-official" ,
398+ model = "fixture-model" , reasoning_effort = "high" , oar_client = client )
399+ run_oar_operation_host (request , ** options )
400+ request ["session" ]["action" ] = "resume"
401+ client .native_id = returned_id
402+ with pytest .raises (ValueError , match = "selected native operation session" ):
403+ run_oar_operation_host (request , ** options )
404+ assert client .prompts == ["fixture-key" ]
405+ assert client .closed == ["fixture-key" , "fixture-key" ]
406+
407+
408+ @pytest .mark .parametrize ("host" , ["dsh" , "codex-cli" ])
409+ def test_oar_closes_opened_session_when_another_writer_wins_initialization (tmp_path , host ):
410+ from unittest .mock import Mock
411+
412+ from loopx .control_plane .turn_driver .codex_sessions import _lineage , load_codex_cli_session
413+ from loopx .control_plane .turn_driver .oar_operation_host import run_oar_operation_host
414+ from loopx .hosts .oar_client import OarSessionClient
415+
416+ service , store = fixtures .service (tmp_path )
417+ runtime = store .root .parent .parent
418+ request = _managed_operation_turn_request ()
419+ lineage = _lineage (request )
420+ client = Mock (spec = OarSessionClient )
421+
422+ def open_then_replace (params ):
423+ _store_codex_cli_session (runtime , lineage = lineage , host = host , session_id = "winner" )
424+ return {"session_key" : "owned-handle" , "native_session_id" : "new-session" }
425+
426+ client .open_session .side_effect = open_then_replace
427+ with pytest .raises (ValueError , match = "binding changed during" ):
428+ run_oar_operation_host (
429+ request , host = host , runtime_id = "dsh" if host == "dsh" else "codex" ,
430+ runtime_root = runtime , registry_path = service .registry_path , project = tmp_path ,
431+ dsh_home = tmp_path / "dsh" , max_tokens = 256 , provider = "fixture-provider" ,
432+ model = "fixture-model" , reasoning_effort = "high" , oar_client = client ,
433+ )
434+
435+ client .close_session .assert_called_once_with ("owned-handle" )
436+ client .prompt .assert_not_called ()
437+ assert load_codex_cli_session (runtime , lineage = lineage , host = host )["session_id" ] == "winner"
0 commit comments