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() # No durable replay receipt: this path observes the model before any action. service.execution_problem_overview = lambda _task: {'items': []} 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' 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() service.execution_problem_overview = lambda _task: {'items': []} 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(), [] service.execution_problem_overview = lambda _task: {'items': []} 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'] == 'cancelled' assert (tmp_path / 'dsh/incidents' / result['incident_id'] / 'incident.json').is_file() def test_certified_context_change_at_admission_never_reserves_or_mutates(tmp_path): service = Service() @contextmanager def scope(_action): service.progress.saved_items += 1 yield runner = make_runner(tmp_path, service) runner.action_scope = scope result = runner.run('t', 'worker_error', 'failure', execution_authorized=True) assert result['gates']['failure_policy']['retry_allowed'] is True assert result['retry_error']['detail'] == 'retry_gate_changed' assert not service.calls and not service.reserved assert result['attempts'] == 0 def test_certified_cancellation_at_admission_never_reserves_or_mutates(tmp_path): from threading import Event cancelled = Event() service, published = Service(), [] @contextmanager def scope(_action): cancelled.set() yield runner = make_runner(tmp_path, service, publisher=lambda *_args: published.append(True)) runner.action_scope = scope result = runner.run('t', 'worker_error', 'failure', execution_authorized=True, cancel_event=cancelled) assert not service.calls and not service.reserved and not published assert result['attempts'] == 0 and result['status'] == 'cancelled' def test_cancellation_in_post_retry_explanation_retains_completed_action(tmp_path): from threading import Event cancelled = Event() service, published = Service(), [] def model(*_args): assert service.calls == ['prepare', 'execute'] cancelled.set() return analyze() 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 service.calls == ['prepare', 'execute'] and service.reserved assert not published and result['status'] == 'cancelled' assert result['attempts'] == 1 assert result['verification']['failed_items'] == 0 assert (Path(result['evidence_dir']) / 'evidence.zip').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'))