from contextlib import contextmanager from test_incidents import Service, analyze, make_runner def test_rejected_admission_does_not_mutate_or_spend_budget(tmp_path): @contextmanager def scope(_action): raise ValueError('execution_lane_changed') yield service = Service() 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.glob('dsh/retry-budget/*.json')) def test_model_wait_changed_context_cannot_retry(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.glob('dsh/retry-budget/*.json')) def test_retry_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 scoped_execute(*args, **kwargs): assert active == ['retry_failed'] return execute(*args, **kwargs) service.execute_quick_run_queue = scoped_execute 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_cancellation_during_model_wait_retains_evidence_without_publication(tmp_path): from threading import Event cancelled = Event() def model(*_args): cancelled.set() return analyze() service, published = Service(), [] runner = make_runner(tmp_path, service, model, lambda *_args: published.append(True)) result = runner.run('t', 'worker_error', 'failure', execution_authorized=True, cancel_event=cancelled) assert not service.calls and not published assert result['status'] == 'pending_issue' assert (tmp_path / 'dsh/incidents' / result['incident_id'] / 'incident.json').is_file() def test_shared_retry_reservation_denial_never_uses_legacy_budget(tmp_path): service = Service() service.reserve_problem_retry = lambda *args, **kwargs: () result = make_runner(tmp_path, service).run('t', 'worker_error', 'failure', execution_authorized=True) assert not service.calls assert result['attempts'] == 0 assert result['gates']['retry_budget_available'] is False assert not list(tmp_path.glob('dsh/retry-budget/*.json')) def test_shared_reservation_limits_preparation_to_reserved_operation_keys(tmp_path): service, reserved, prepared_keys = Service(), ('uid-operation',), [] service.reserve_problem_retry = lambda *args, **kwargs: reserved prepare = service.prepare_failed_quick_run def prepare_reserved(task_id, *, operation_keys=None): prepared_keys.append(operation_keys) prepare(task_id) service.prepare_failed_quick_run = prepare_reserved result = make_runner(tmp_path, service).run('t', 'worker_error', 'failure', execution_authorized=True) assert prepared_keys == [reserved] assert result['status'] == 'resolved' assert not list(tmp_path.glob('dsh/retry-budget/*.json'))