@@ -547,6 +547,116 @@ def lost_response(**kwargs):
547547 recovered .close ()
548548
549549
550+ @pytest .mark .parametrize ("provider" , ["file" , "sqlite" ])
551+ def test_same_calendar_task_lifecycle_retains_results_and_rejects_late_dispatch (
552+ steward , tmp_path , monkeypatch , provider ):
553+ from loopx .chat_runtime import ChatRuntimeController
554+ from loopx .chat_store import ChatSessionStore
555+ from loopx .capabilities .native_chat .steward_managed_mode import ManagedSessionTurn
556+ from loopx .extensions .lark .private_conversations import LarkPrivateConversations
557+
558+ session , actor , _ , capture , workspace , config = setup_task (steward , tmp_path , monkeypatch , provider )
559+ store , runtime , bot , _ , binding , _ , original_workspace = steward
560+ sid , goal_id = session ["session_id" ], actor ["goal_id" ]
561+ actor = SessionCalendarTasks (runtime ).selected (sid )["actor" ]
562+ point = fixed_clock (monkeypatch )
563+ point [0 ] = datetime (2020 , 1 , 1 , 8 , 59 , 59 , tzinfo = timezone .utc )
564+ initial = capture .read_bytes (), store .messages (sid ), list (bot .writes )
565+ later = {** config , "schedule" : {** config ["schedule" ], "rrule" : "FREQ=DAILY;BYHOUR=10;BYMINUTE=0" }}
566+ with settings_server (steward ) as request :
567+ for proposed in (config , later , {** later , "enabled" : False }, None , config ):
568+ apply_configuration (request , goal_id , proposed )
569+ getattr (runtime , "_steward_monitor_next_check" , {}).clear ()
570+ assert not pump_steward_monitors (runtime )
571+ assert read_cadence_journal (runtime_root = runtime .coordination_runtime_root , goal_id = goal_id ) is None
572+ assert (capture .read_bytes (), store .messages (sid ), list (bot .writes )) == initial
573+ point [0 ] += timedelta (seconds = 2 )
574+ runtime ._steward_monitor_next_check .clear ()
575+ first = finish (runtime , pump_steward_monitors (runtime )[0 ])
576+ assert first ["response" ]["native_loopx" ]["status" ] == "committed" , first
577+ runtime ._steward_monitor_next_check .clear ()
578+ assert not pump_steward_monitors (runtime )
579+ original_result = store .load_turn (sid , first ["turn_id" ])
580+ completion = SessionCalendarTasks (runtime ).health (sid )["completion" ]
581+ apply_configuration (request , goal_id , {** config , "enabled" : False })
582+
583+ # A new runtime reads the same physical store, actor, paused plan and body.
584+ runtime .close ()
585+ recovered = ChatRuntimeController (store = ChatSessionStore (store .root .parent ), codex_bin = runtime .codex_bin ,
586+ project_contexts = runtime .project_contexts , registry_path = runtime .registry_path )
587+ transport = LarkPrivateConversations (controller = recovered , runtime_root = store .root .parent ,
588+ runner = bot , cli_bin = "lark-cli" )
589+ resumed = (recovered .store , recovered , bot , transport , binding , capture , original_workspace )
590+ try :
591+ service = SessionCalendarTasks (recovered )
592+ assert service .selected (sid )["actor" ] == actor
593+ assert service .health (sid )["completion" ] == completion and not service .health (sid )["enabled" ]
594+ paused = capture .read_bytes (), recovered .store .messages (sid ), list (bot .writes )
595+ point [0 ] += timedelta (days = 1 )
596+ assert not pump_steward_monitors (recovered )
597+ assert (capture .read_bytes (), recovered .store .messages (sid ), list (bot .writes )) == paused
598+ with settings_server (resumed ) as request :
599+ apply_configuration (request , goal_id , config )
600+ recovered ._steward_monitor_next_check .clear ()
601+ second = finish (recovered , pump_steward_monitors (recovered )[0 ])
602+ assert second ["response" ]["native_loopx" ]["status" ] == "committed" , second
603+ assert second ["turn_id" ] != first ["turn_id" ]
604+ assert recovered .store .load_turn (sid , first ["turn_id" ]) == original_result
605+ recovered ._steward_monitor_next_check .clear ()
606+ assert not pump_steward_monitors (recovered )
607+ apply_configuration (request , goal_id , later )
608+ point [0 ] = datetime (2020 , 1 , 2 , 10 , 0 , 1 , tzinfo = timezone .utc )
609+ # Freeze through the real calendar owner, before any model dispatch.
610+ with monkeypatch .context () as waiting :
611+ waiting .setattr (ManagedSessionTurn , "submit" , lambda * _a , ** _kw : (None , False ))
612+ assert service .frontier (sid ) == (None , False )
613+ window = read_cadence_journal (runtime_root = recovered .coordination_runtime_root , goal_id = goal_id )["window" ]
614+ pending = service ._todo (actor , window )
615+ before = capture .read_bytes (), recovered .store .messages (sid ), list (bot .writes )
616+ registry_bytes = recovered .registry_path .read_bytes ()
617+ registry = json .loads (registry_bytes )
618+ goal = next (row for row in registry ["goals" ] if row ["id" ] == goal_id )
619+ goal ["coordination" ]["registered_agents" ] = ["peer" ]
620+ recovered .registry_path .write_text (json .dumps (registry ))
621+ try :
622+ assert not pump_steward_monitors (recovered )
623+ with pytest .raises (ValueError ):
624+ ManagedSessionTurn (recovered ).submit (sid , agent_id = actor ["agent_id" ],
625+ operation_id = "task." + window ["window_id" ], todo_id = pending ["todo_id" ], scheduled_window = window )
626+ finally :
627+ # Restore only this disposable counterfactual, never live grants.
628+ recovered .registry_path .write_bytes (registry_bytes )
629+ apply_configuration (request , goal_id , {** later , "enabled" : False })
630+ with pytest .raises (ValueError , match = "subscription changed" ):
631+ ManagedSessionTurn (recovered ).submit (sid , agent_id = actor ["agent_id" ],
632+ operation_id = "task." + window ["window_id" ], todo_id = pending ["todo_id" ], scheduled_window = window )
633+ assert (capture .read_bytes (), recovered .store .messages (sid ), list (bot .writes )) == before
634+ apply_configuration (request , goal_id , later )
635+ recovered ._steward_monitor_next_check .clear ()
636+ third = finish (recovered , pump_steward_monitors (recovered )[0 ])
637+ assert third ["response" ]["native_loopx" ]["status" ] == "committed" , third
638+ assert third ["loopx_request" ]["todo_id" ] == pending ["todo_id" ]
639+ assert len ({row ["loopx_request" ]["schedule_window_id" ] for row in (first , second , third )}) == 3
640+ assert json .loads ((workspace / "total.json" ).read_text ()) == {"total" : 42 }
641+ final = capture .read_bytes (), recovered .store .messages (sid ), list (bot .writes )
642+ apply_configuration (request , goal_id , None )
643+ point [0 ] += timedelta (days = 1 )
644+ recovered ._steward_monitor_next_check .clear ()
645+ assert not pump_steward_monitors (recovered )
646+ assert (capture .read_bytes (), recovered .store .messages (sid ), list (bot .writes )) == final
647+ assert recovered .store .load_turn (sid , first ["turn_id" ]) == original_result
648+ calls = [json .loads (line ) for line in capture .read_bytes ()[len (initial [0 ]):].splitlines ()]
649+ assert sum (row .get ("method" ) == "turn/start" for row in calls ) == 3
650+ messages = recovered .store .messages (sid )
651+ assert messages [:len (initial [1 ])] == initial [1 ]
652+ for turn in (first , second , third ):
653+ replies = [row for row in messages if row ["role" ] == "agent" and row .get ("turn_id" ) == turn ["turn_id" ]]
654+ assert len (replies ) == 1 and replies [0 ]["text" ] == turn ["response" ]["message" ]
655+ assert bot .writes == initial [2 ]
656+ finally :
657+ recovered .close ()
658+
659+
550660@pytest .mark .parametrize ("provider" , ["file" , "sqlite" ])
551661@pytest .mark .parametrize ("fault" , ["malformed-message" , "non-object-message" , "conflicting-result" , "duplicate-result" , "none" ])
552662def test_calendar_completion_requires_complete_unambiguous_original_return (
0 commit comments