from contextlib import contextmanager from pathlib import Path from test_incidents import Service, ResourceService, analyze, cleanup_model, make_runner def test_rejected_admission_never_mutates_or_spends_retry_budget(tmp_path): service = Service() @contextmanager def scope(action): raise ValueError('execution_lane_changed') yield runner = make_runner(tmp_path, service) runner.action_scope = scope result = runner.run('t', 'worker_error', 'failure', execution_authorized=True) assert service.calls == [] assert result['attempts'] == 0 assert not list((tmp_path / 'dsh/retry-budget').glob('*.json')) def test_changed_task_after_model_wait_never_retries(tmp_path): service = Service() def model(*args): service.progress.saved_items += 1 return analyze() result = make_runner(tmp_path, service, model).run('t', 'worker_error', 'failure', execution_authorized=True) assert not service.calls assert result['attempts'] == 0 assert not list((tmp_path / 'dsh/retry-budget').glob('*.json')) def test_action_scope_released_before_reflection_and_publication(tmp_path): active, phases = [], [] @contextmanager def scope(action): active.append(action) try: yield finally: active.pop() service = Service(fixed=False) execute = service.execute_quick_run_queue def execute_scoped(*args, **kwargs): assert active == ['retry_failed'] return execute(*args, **kwargs) service.execute_quick_run_queue = execute_scoped def model(*args): assert not active phases.append(args[-1]) return analyze() def publish(*args): assert not active return {'status': 'unavailable'} runner = make_runner(tmp_path, service, model, publish) runner.action_scope = scope runner.run('t', 'worker_error', 'failure', execution_authorized=True) assert phases == ['diagnose', 'reflect'] assert service.calls == ['prepare', 'execute'] assert not active def test_cleanup_rejected_admission_does_not_release_resources(tmp_path): service = ResourceService() @contextmanager def scope(action): raise ValueError('execution_lane_changed') yield runner = make_runner(tmp_path, service, cleanup_model) runner.action_scope = scope result = runner.run('t', 'batch_resources_pending', 'failure', cleanup_allowed=True) assert not service.calls assert result['cleanup_attempts'] == 0 assert result['status'] != 'resolved'