@@ -140,6 +140,94 @@ def now(cls, tz=None):
140140 return point
141141
142142
143+ @pytest .mark .parametrize ("provider" , ["file" , "sqlite" ])
144+ def test_original_session_discovers_repeatable_tasks_without_enrolling_or_replaying (steward , tmp_path , monkeypatch , provider ):
145+ from loopx .capabilities .native_chat .steward_managed_mode import ManagedSessionTurn
146+ from loopx .capabilities .native_chat .steward_monitor import StewardMonitor
147+ session , actor , unrelated , capture , _ , config = setup_task (steward , tmp_path , monkeypatch , provider )
148+ store , runtime , bot , * _ = steward
149+ sid = session ["session_id" ]
150+ fixed_clock (monkeypatch )
151+ calls , history , writes = capture .read_bytes (), store .messages (sid ), list (bot .writes )
152+ before = load_goal_from_registry (runtime .registry_path , actor ["goal_id" ])
153+ with settings_server (steward ) as request :
154+ url = f"/api/chat/sessions/{ sid } /native-turn"
155+ status , plain = request (url , None , method = "GET" )
156+ assert status == 200 and "calendar_task_sources" not in plain
157+ status , view = request (url + "?calendar_task_sources=true" , None , method = "GET" )
158+ assert status == 200 , view
159+ options = view ["calendar_task_sources" ]
160+ assert options ["session_id" ] == sid and options ["goal_id" ] == actor ["goal_id" ]
161+ assert options ["agent_id" ] == actor ["agent_id" ] and options ["read_only" ]
162+ assert options ["authority_read" ]["source_authority" ] == ("file_v0" if provider == "file" else "sqlite_v0" )
163+ assert [row ["source_todo_id" ] for row in options ["sources" ]] == [config ["task" ]["source_todo_id" ]]
164+ chosen = options ["sources" ][0 ]
165+ assert chosen ["title" ] and len (chosen ["completion_validation_sha256" ]) == 64
166+ assert not chosen ["archived" ] and "validation_command_argv" not in json .dumps (options )
167+ for selector in ("yes" , "true&calendar_task_sources=false" , f"true&agent_id={ actor ['agent_id' ]} -other" ,
168+ f"true&agent_id={ actor ['agent_id' ]} &agent_id=other" , "true&goal_id=other" ):
169+ status , rejected = request (url + "?calendar_task_sources=" + selector , None , method = "GET" )
170+ assert status == 409 and rejected ["ok" ] is False
171+ # Discovery is opt-in and has not changed the original Goal, Task,
172+ # model calls, original messages, or private provider delivery.
173+ assert load_goal_from_registry (runtime .registry_path , actor ["goal_id" ]) == before
174+ assert capture .read_bytes () == calls and store .messages (sid ) == history and bot .writes == writes
175+ assert read_cadence_journal (runtime_root = runtime .coordination_runtime_root , goal_id = actor ["goal_id" ]) is None
176+ apply_configuration (request , actor ["goal_id" ], {** config , "task" : {"source_todo_id" : chosen ["source_todo_id" ]}})
177+ completed = finish (runtime , pump_steward_monitors (runtime )[0 ])
178+ assert completed ["response" ]["native_loopx" ]["status" ] == "committed"
179+ # A completed generated period cannot become another repeating source.
180+ assert [row ["source_todo_id" ] for row in ManagedSessionTurn (runtime ).inspect (
181+ sid , include_calendar_task_sources = True )["calendar_task_sources" ]["sources" ]] == [chosen ["source_todo_id" ]]
182+ cli (runtime , "todo" , "archive-completed" , "--goal-id" , actor ["goal_id" ], "--role" , "agent" ,
183+ "--max-active-done" , "0" , "--execute" )
184+ calls = capture .read_bytes ()
185+ status , archived = request (url + "?calendar_task_sources=true" , None , method = "GET" )
186+ assert status == 200 , archived
187+ assert archived ["calendar_task_sources" ]["sources" ] == [{** chosen , "archived" : True }]
188+ assert archived ["scheduled_task" ]["completion_verified" ] and capture .read_bytes () == calls
189+ assert next (row for row in StewardMonitor (runtime ).todos (actor ) if row ["todo_id" ] == unrelated )["status" ] == "open"
190+
191+
192+ @pytest .mark .parametrize ("provider" , ["file" , "sqlite" ])
193+ @pytest .mark .parametrize ("fault" , ["malformed" , "directory" ])
194+ def test_calendar_task_discovery_retains_unreadable_validator_and_recovers_without_work (steward , tmp_path , monkeypatch , provider , fault ):
195+ from loopx .control_plane .todos .completion_validation_store import completion_validation_declaration_path
196+ session , actor , _ , capture , _ , config = setup_task (steward , tmp_path , monkeypatch , provider )
197+ store , runtime , bot , * _ = steward
198+ service = SessionCalendarTasks (runtime )
199+ options = service .source_options (session ["session_id" ])
200+ source = options ["sources" ][0 ]
201+ path = completion_validation_declaration_path (runtime_root = runtime .coordination_runtime_root ,
202+ goal_id = actor ["goal_id" ], todo_id = source ["source_todo_id" ]).parent / "blobs" / (source ["completion_validation_sha256" ] + ".json" )
203+ original = path .read_bytes ()
204+ calls , history , writes = capture .read_bytes (), store .messages (session ["session_id" ]), list (bot .writes )
205+ path .unlink ()
206+ if fault == "directory" :
207+ path .mkdir ()
208+ else :
209+ path .write_text ("{}" )
210+ try :
211+ with settings_server (steward ) as request :
212+ url = f"/api/chat/sessions/{ session ['session_id' ]} /native-turn?calendar_task_sources=true"
213+ status , failed = request (url , None , method = "GET" )
214+ assert status == 409 and failed ["error_code" ] == "managed_turn_unavailable" , failed
215+ assert "calendar_task_sources" not in failed
216+ with pytest .raises (OSError if fault == "directory" else ValueError ):
217+ service .source_task (actor , config ["task" ])
218+ if path .is_dir ():
219+ path .rmdir ()
220+ path .write_bytes (original )
221+ status , recovered = request (url , None , method = "GET" )
222+ assert status == 200 and recovered ["calendar_task_sources" ]["sources" ] == options ["sources" ]
223+ finally :
224+ if path .is_dir ():
225+ path .rmdir ()
226+ path .write_bytes (original )
227+ assert capture .read_bytes () == calls and store .messages (session ["session_id" ]) == history and bot .writes == writes
228+ assert read_cadence_journal (runtime_root = runtime .coordination_runtime_root , goal_id = actor ["goal_id" ]) is None
229+
230+
143231@pytest .mark .parametrize ("provider" , ["file" , "sqlite" ])
144232def test_calendar_preserves_canonical_decision_scopes_through_original_return (steward , tmp_path , monkeypatch , provider ):
145233 from loopx .control_plane .todos .contract import normalize_todo_required_decision_scopes
0 commit comments