from afk_developer.planner.task_model import ( TaskFailureKind, TaskNode, TaskPlan, TaskStatus, ) from afk_developer.recovery import RecoveryMode from afk_developer.scheduler.failure_classification import ( classify_review_failure, classify_worker_failure, should_bypass_immediate_escalation, worker_failure_should_escalate, ) from afk_developer.scheduler.reviewer import ReviewResult def test_copilot_timeout_is_a_transient_provider_failure() -> None: assert ( classify_worker_failure(TaskStatus.FAILED, "Copilot timed out after 900s") is TaskFailureKind.PROVIDER_TIMEOUT ) def test_unrelated_worker_failure_is_not_misclassified_as_provider_outage() -> None: assert ( classify_worker_failure( TaskStatus.FAILED, "Copilot ended without calling task_complete.", ) is TaskFailureKind.WORKER_FAILED ) def test_task_command_policy_rejection_is_terminal_even_with_failed_status() -> None: kind = classify_worker_failure( TaskStatus.FAILED, "TASK_COMMAND_POLICY_REJECTED: include a project-relative test path", ) assert kind is TaskFailureKind.COMMAND_POLICY_REJECTED assert worker_failure_should_escalate(TaskStatus.FAILED, kind, RecoveryMode.BOUNDED) def test_discovery_progress_stall_is_distinct_and_normally_retriable() -> None: kind = classify_worker_failure( TaskStatus.FAILED, "DISCOVERY_PROGRESS_STALLED: worker ignored write-or-block mode", ) assert kind is TaskFailureKind.DISCOVERY_PROGRESS_STALLED assert not worker_failure_should_escalate(TaskStatus.FAILED, kind, RecoveryMode.BOUNDED) def test_research_insufficient_outcome_blocks_dependents_without_retry() -> None: assert ( classify_worker_failure( TaskStatus.ESCALATED, "RESEARCH_INSUFFICIENT: The corpus omits the required payload shape.", ) is TaskFailureKind.RESEARCH_INSUFFICIENT ) def test_verification_runner_failure_is_not_misclassified_as_research() -> None: assert ( classify_worker_failure( TaskStatus.ESCALATED, "VERIFICATION_POLICY_REJECTED: ruff check --fix is not read-only.", ) is TaskFailureKind.VERIFICATION_POLICY_REJECTED ) def test_failure_classification_survives_plan_serialization() -> None: plan = TaskPlan( project_name="example", goal="Persist provider recovery state", intent="implement", tasks=[ TaskNode( id="task", title="Task", description="Do work", status=TaskStatus.ESCALATED, failure_kind=TaskFailureKind.PROVIDER_UNAVAILABLE, failure_detail="Copilot could not fetch the authenticated GitHub user.", ) ], ) restored = TaskPlan.model_validate_json(plan.model_dump_json()) assert restored.tasks[0].failure_kind is TaskFailureKind.PROVIDER_UNAVAILABLE assert "authenticated GitHub user" in restored.tasks[0].failure_detail def test_scope_guard_has_a_distinct_failure_kind() -> None: review = ReviewResult( passed=False, tests_passed=True, lint_passed=True, scope_passed=False, ) assert classify_review_failure(review) is TaskFailureKind.MUTATION_SCOPE_EXCEEDED def test_verification_policy_rejection_bypasses_escalation_when_bounded() -> None: assert should_bypass_immediate_escalation( TaskFailureKind.VERIFICATION_POLICY_REJECTED, RecoveryMode.BOUNDED ) def test_verification_policy_rejection_still_escalates_when_recovery_off() -> None: assert not should_bypass_immediate_escalation( TaskFailureKind.VERIFICATION_POLICY_REJECTED, RecoveryMode.OFF ) def test_other_escalated_failure_kinds_never_bypass_escalation() -> None: # Even with bounded recovery on, only VERIFICATION_POLICY_REJECTED is eligible -- # provider outages, timeouts, and insufficient-research are still always terminal. for kind in ( TaskFailureKind.PROVIDER_UNAVAILABLE, TaskFailureKind.PROVIDER_TIMEOUT, TaskFailureKind.RESEARCH_INSUFFICIENT, TaskFailureKind.WORKER_FAILED, None, ): assert not should_bypass_immediate_escalation(kind, RecoveryMode.BOUNDED) def test_worker_failure_should_escalate_when_status_is_escalated_and_not_bypassed() -> None: assert worker_failure_should_escalate( TaskStatus.ESCALATED, TaskFailureKind.WORKER_FAILED, RecoveryMode.OFF ) def test_worker_failure_should_not_escalate_a_bypassed_kind_when_bounded() -> None: assert not worker_failure_should_escalate( TaskStatus.ESCALATED, TaskFailureKind.VERIFICATION_POLICY_REJECTED, RecoveryMode.BOUNDED ) def test_worker_failure_should_not_escalate_a_non_escalated_status() -> None: assert not worker_failure_should_escalate( TaskStatus.FAILED, TaskFailureKind.WORKER_FAILED, RecoveryMode.OFF ) def test_worker_failure_should_escalate_a_transient_provider_failure_even_if_status_is_failed() -> ( None ): # Transient provider failures always escalate (to avoid burning a retry # attempt against an outage) regardless of the worker's reported status. assert worker_failure_should_escalate( TaskStatus.FAILED, TaskFailureKind.PROVIDER_TIMEOUT, RecoveryMode.BOUNDED )