#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
from __future__ import annotations

import datetime
from collections import defaultdict
from collections.abc import Mapping
from functools import reduce
from typing import TYPE_CHECKING
from unittest import mock
from unittest.mock import ANY, call

import pendulum
import pytest
from opentelemetry import trace as otel_trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
from opentelemetry.trace import StatusCode
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator
from sqlalchemy import (
    func,
    select,
    update,
)
from sqlalchemy.orm import joinedload
from sqlalchemy.orm.exc import StaleDataError

from airflow import settings
from airflow._shared.observability.metrics.base_stats_logger import StatsLogger
from airflow._shared.observability.traces import OverrideableRandomIdGenerator
from airflow._shared.timezones import timezone
from airflow.callbacks.callback_requests import DagCallbackRequest, DagRunContext
from airflow.models.dag import DagModel, infer_automated_data_interval
from airflow.models.dag_version import DagVersion
from airflow.models.dagrun import DagRun, DagRunNote
from airflow.models.deadline import Deadline
from airflow.models.deadline_alert import DeadlineAlert as DeadlineAlertModel
from airflow.models.serialized_dag import SerializedDagModel
from airflow.models.taskinstance import TaskInstance, TaskInstanceNote, clear_task_instances
from airflow.models.taskmap import TaskMap
from airflow.models.taskreschedule import TaskReschedule
from airflow.models.trigger import Trigger
from airflow.providers.standard.operators.bash import BashOperator
from airflow.providers.standard.operators.empty import EmptyOperator
from airflow.providers.standard.operators.python import PythonOperator, ShortCircuitOperator
from airflow.sdk import DAG, BaseOperator, get_current_context, setup, task, task_group, teardown
from airflow.sdk.definitions.callback import AsyncCallback
from airflow.sdk.definitions.deadline import DeadlineAlert, DeadlineReference
from airflow.serialization.definitions.deadline import SerializedReferenceModels
from airflow.serialization.serialized_objects import LazyDeserializedDAG
from airflow.settings import get_policy_plugin_manager
from airflow.task.trigger_rule import TriggerRule
from airflow.triggers.base import StartTriggerArgs
from airflow.utils.session import create_session
from airflow.utils.state import DagRunState, State, TaskInstanceState
from airflow.utils.types import DagRunTriggeredByType, DagRunType

from tests_common.test_utils import db
from tests_common.test_utils.config import conf_vars
from tests_common.test_utils.dag import sync_dag_to_db
from tests_common.test_utils.mock_operators import MockOperator
from tests_common.test_utils.taskinstance import create_task_instance, run_task_instance
from unit.models import DEFAULT_DATE as _DEFAULT_DATE

if TYPE_CHECKING:
    from sqlalchemy.orm.session import Session

    from airflow.serialization.definitions.dag import SerializedDAG

pytestmark = [pytest.mark.db_test, pytest.mark.need_serialized_dag]

TI = TaskInstance
DEFAULT_DATE = pendulum.instance(_DEFAULT_DATE)


async def empty_callback_for_deadline():
    """Used in a number of tests to confirm that Deadlines and DeadlineAlerts function correctly."""
    pass


@pytest.fixture(scope="module")
def dagbag():
    from airflow.dag_processing.dagbag import DagBag

    return DagBag(include_examples=True)


@pytest.fixture
def deadline_test_dag(session):
    """Fixture that creates and syncs a basic DAG with two tasks."""

    def _make_dag(deadline=None, on_success_callback=None):
        dag_kwargs = {"dag_id": "test_dag", "schedule": datetime.timedelta(days=1)}
        if deadline:
            dag_kwargs["deadline"] = deadline
        if on_success_callback:
            dag_kwargs["on_success_callback"] = on_success_callback

        dag = DAG(**dag_kwargs)
        task_1 = EmptyOperator(task_id="task_1", dag=dag)
        task_2 = EmptyOperator(task_id="task_2", dag=dag)
        task_1.set_downstream(task_2)

        scheduler_dag = sync_dag_to_db(dag, session=session)
        return scheduler_dag

    return _make_dag


class TestDagRun:
    @pytest.fixture(autouse=True)
    def setup_test_cases(self):
        self._clean_db()
        yield
        self._clean_db()

    @staticmethod
    def _clean_db():
        db.clear_db_runs()
        db.clear_db_pools()
        db.clear_db_dags()
        db.clear_db_dag_bundles()
        db.clear_db_variables()
        db.clear_db_assets()
        db.clear_db_xcom()
        db.clear_db_dags()

    @staticmethod
    def create_dag_run(
        dag: SerializedDAG,
        *,
        task_states: Mapping[str, TaskInstanceState] | None = None,
        logical_date: datetime.datetime | None = None,
        is_backfill: bool = False,
        state: DagRunState = DagRunState.RUNNING,
        session: Session,
    ):
        now = timezone.utcnow()
        logical_date = pendulum.instance(logical_date or now)
        if is_backfill:
            run_type = DagRunType.BACKFILL_JOB
            data_interval = infer_automated_data_interval(dag.timetable, logical_date)
        else:
            run_type = DagRunType.MANUAL
            data_interval = dag.timetable.infer_manual_data_interval(run_after=logical_date)

        dag_run = dag.create_dagrun(
            run_id=dag.timetable.generate_run_id(
                run_type=run_type,
                run_after=logical_date,
                data_interval=data_interval,
            ),
            run_type=run_type,
            logical_date=logical_date,
            data_interval=data_interval,
            run_after=data_interval.end,
            start_date=now,
            state=state,
            triggered_by=DagRunTriggeredByType.TEST,
            session=session,
        )

        if task_states is not None:
            for task_id, task_state in task_states.items():
                ti = dag_run.get_task_instance(task_id)
                if TYPE_CHECKING:
                    assert ti
                ti.set_state(task_state, session)
            session.flush()

        return dag_run

    def test_clear_task_instances_for_backfill_running_dagrun(self, dag_maker, session):
        now = timezone.utcnow()
        state = DagRunState.RUNNING
        dag_id = "test_clear_task_instances_for_backfill_running_dagrun"
        with dag_maker(dag_id=dag_id) as dag:
            EmptyOperator(task_id="backfill_task_0")
        self.create_dag_run(dag, logical_date=now, is_backfill=True, state=state, session=session)

        qry = session.scalars(select(TI).where(TI.dag_id == dag.dag_id)).all()
        clear_task_instances(qry, session)
        session.flush()
        dr0 = session.scalar(select(DagRun).where(DagRun.dag_id == dag_id, DagRun.logical_date == now))
        assert dr0.state == state
        # clear_number should be incremented even for running dag runs
        assert dr0.clear_number == 1
        # queued_at should also be updated
        assert dr0.queued_at is not None

    @pytest.mark.parametrize("state", [DagRunState.SUCCESS, DagRunState.FAILED])
    def test_clear_task_instances_for_backfill_finished_dagrun(self, dag_maker, state, session):
        now = timezone.utcnow()
        dag_id = "test_clear_task_instances_for_backfill_finished_dagrun"
        with dag_maker(dag_id=dag_id) as dag:
            EmptyOperator(task_id="backfill_task_0")
        self.create_dag_run(dag, logical_date=now, is_backfill=True, state=state, session=session)

        qry = session.scalars(select(TI).where(TI.dag_id == dag.dag_id)).all()
        clear_task_instances(qry, session)
        session.flush()
        dr0 = session.scalar(select(DagRun).where(DagRun.dag_id == dag_id, DagRun.logical_date == now))
        assert dr0.state == DagRunState.QUEUED
        assert dr0.clear_number == 1

    def test_dagrun_find(self, session):
        now = timezone.utcnow()

        dag_id1 = "test_dagrun_find_externally_triggered"
        dag_run = DagRun(
            dag_id=dag_id1,
            run_id=dag_id1,
            run_type=DagRunType.MANUAL,
            logical_date=now,
            start_date=now,
            state=DagRunState.RUNNING,
        )
        session.add(dag_run)

        dag_id2 = "test_dagrun_find_not_externally_triggered"
        dag_run = DagRun(
            dag_id=dag_id2,
            run_id=dag_id2,
            run_type=DagRunType.SCHEDULED,
            logical_date=now,
            start_date=now,
            state=DagRunState.RUNNING,
        )
        session.add(dag_run)

        session.commit()

        assert len(DagRun.find(dag_id=dag_id1, run_type=DagRunType.MANUAL)) == 1
        assert len(DagRun.find(run_id=dag_id1)) == 1
        assert len(DagRun.find(run_id=[dag_id1, dag_id2])) == 2
        assert len(DagRun.find(logical_date=[now, now])) == 2
        assert len(DagRun.find(logical_date=now)) == 2
        assert len(DagRun.find(dag_id=dag_id1, run_type=DagRunType.SCHEDULED)) == 0
        assert len(DagRun.find(dag_id=dag_id2, run_type=DagRunType.MANUAL)) == 0
        assert len(DagRun.find(dag_id=dag_id2)) == 1

    def test_dagrun_find_duplicate(self, session):
        now = timezone.utcnow()

        dag_id = "test_dagrun_find_duplicate"
        dag_run = DagRun(
            dag_id=dag_id,
            run_id=dag_id,
            run_type=DagRunType.MANUAL,
            logical_date=now,
            start_date=now,
            state=DagRunState.RUNNING,
        )
        session.add(dag_run)

        session.commit()

        assert DagRun.find_duplicate(dag_id=dag_id, run_id=dag_id) is not None
        assert DagRun.find_duplicate(dag_id=dag_id, run_id=dag_id) is not None
        assert DagRun.find_duplicate(dag_id=dag_id, run_id=None) is None

    def test_dagrun_success_when_all_skipped(self, dag_maker, session):
        """
        Tests that a DAG run succeeds when all tasks are skipped
        """
        with dag_maker(
            dag_id="test_dagrun_success_when_all_skipped",
            schedule=datetime.timedelta(days=1),
            start_date=timezone.datetime(2017, 1, 1),
        ) as dag:
            dag_task1 = ShortCircuitOperator(task_id="test_short_circuit_false", python_callable=bool)
            dag_task2 = EmptyOperator(task_id="test_state_skipped1")
            dag_task3 = EmptyOperator(task_id="test_state_skipped2")
            dag_task1.set_downstream(dag_task2)
            dag_task2.set_downstream(dag_task3)

        initial_task_states = {
            "test_short_circuit_false": TaskInstanceState.SUCCESS,
            "test_state_skipped1": TaskInstanceState.SKIPPED,
            "test_state_skipped2": TaskInstanceState.SKIPPED,
        }

        dag_run = self.create_dag_run(dag=dag, task_states=initial_task_states, session=session)
        dag_run.update_state()
        assert dag_run.state == DagRunState.SUCCESS

    def test_dagrun_not_stuck_in_running_when_all_tasks_instances_are_removed(self, dag_maker, session):
        """
        Tests that a DAG run succeeds when all tasks are removed
        """
        with dag_maker(
            dag_id="test_dagrun_success_when_all_skipped",
            schedule=datetime.timedelta(days=1),
            start_date=timezone.datetime(2017, 1, 1),
        ) as dag:
            dag_task1 = ShortCircuitOperator(task_id="test_short_circuit_false", python_callable=bool)
            dag_task2 = EmptyOperator(task_id="test_state_skipped1")
            dag_task3 = EmptyOperator(task_id="test_state_skipped2")
            dag_task1.set_downstream(dag_task2)
            dag_task2.set_downstream(dag_task3)

        initial_task_states = {
            "test_short_circuit_false": TaskInstanceState.REMOVED,
            "test_state_skipped1": TaskInstanceState.REMOVED,
            "test_state_skipped2": TaskInstanceState.REMOVED,
        }

        dag_run = self.create_dag_run(dag=dag, task_states=initial_task_states, session=session)
        dag_run.update_state()
        assert dag_run.state == DagRunState.SUCCESS

    def test_dagrun_success_conditions(self, dag_maker, session):
        # A -> B
        # A -> C -> D
        # ordered: B, D, C, A or D, B, C, A or D, C, B, A
        with dag_maker(schedule=datetime.timedelta(days=1), session=session):
            op1 = EmptyOperator(task_id="A")
            op2 = EmptyOperator(task_id="B")
            op3 = EmptyOperator(task_id="C")
            op4 = EmptyOperator(task_id="D")
            op1.set_upstream([op2, op3])
            op3.set_upstream(op4)

        dr = dag_maker.create_dagrun()

        # op1 = root
        ti_op1 = dr.get_task_instance(task_id=op1.task_id)
        ti_op1.set_state(state=TaskInstanceState.SUCCESS, session=session)

        ti_op2 = dr.get_task_instance(task_id=op2.task_id)
        ti_op3 = dr.get_task_instance(task_id=op3.task_id)
        ti_op4 = dr.get_task_instance(task_id=op4.task_id)

        # root is successful, but unfinished tasks
        dr.update_state()
        assert dr.state == DagRunState.RUNNING

        # one has failed, but root is successful
        ti_op2.set_state(state=TaskInstanceState.FAILED, session=session)
        ti_op3.set_state(state=TaskInstanceState.SUCCESS, session=session)
        ti_op4.set_state(state=TaskInstanceState.SUCCESS, session=session)
        dr.update_state()
        assert dr.state == DagRunState.SUCCESS

    def test_dagrun_deadlock(self, dag_maker, session):
        with dag_maker(schedule=datetime.timedelta(days=1), session=session):
            op1 = EmptyOperator(task_id="A")
            op2 = EmptyOperator(task_id="B")
            op2.trigger_rule = TriggerRule.ONE_FAILED
            op2.set_upstream(op1)

        dr = dag_maker.create_dagrun()

        ti_op1: TI = dr.get_task_instance(task_id=op1.task_id, session=session)
        ti_op2: TI = dr.get_task_instance(task_id=op2.task_id, session=session)
        ti_op1.set_state(state=TaskInstanceState.SUCCESS, session=session)
        ti_op2.set_state(state=None, session=session)

        dr.update_state(session=session)
        assert dr.state == DagRunState.RUNNING

        ti_op2.set_state(state=None, session=session)
        ti_op2.task.trigger_rule = "invalid"
        dr.update_state(session=session)
        assert dr.state == DagRunState.FAILED

    def test_dagrun_no_deadlock_with_restarting(self, dag_maker, session):
        with dag_maker(schedule=datetime.timedelta(days=1)):
            op1 = EmptyOperator(task_id="upstream_task")
            op2 = EmptyOperator(task_id="downstream_task")
            op2.set_upstream(op1)

        dr = dag_maker.create_dagrun()
        upstream_ti = dr.get_task_instance(task_id="upstream_task")
        upstream_ti.set_state(TaskInstanceState.RESTARTING, session=session)

        dr.update_state()
        assert dr.state == DagRunState.RUNNING

    def test_dagrun_no_deadlock_with_depends_on_past(self, dag_maker, session):
        with dag_maker(schedule=datetime.timedelta(days=1)):
            EmptyOperator(task_id="dop", depends_on_past=True)
            EmptyOperator(task_id="tc", max_active_tis_per_dag=1)

        dr = dag_maker.create_dagrun(
            run_id="test_dagrun_no_deadlock_1",
            run_type=DagRunType.SCHEDULED,
            start_date=DEFAULT_DATE,
        )
        next_date = DEFAULT_DATE + datetime.timedelta(days=1)
        dr2 = dag_maker.create_dagrun(
            run_id="test_dagrun_no_deadlock_2",
            start_date=DEFAULT_DATE + datetime.timedelta(days=1),
            logical_date=next_date,
        )
        ti1_op1 = dr.get_task_instance(task_id="dop")
        dr2.get_task_instance(task_id="dop")
        ti2_op1 = dr.get_task_instance(task_id="tc")
        dr.get_task_instance(task_id="tc")
        ti1_op1.set_state(state=TaskInstanceState.RUNNING, session=session)
        dr.update_state()
        dr2.update_state()
        assert dr.state == DagRunState.RUNNING
        assert dr2.state == DagRunState.RUNNING

        ti2_op1.set_state(state=TaskInstanceState.RUNNING, session=session)
        dr.update_state()
        dr2.update_state()
        assert dr.state == DagRunState.RUNNING
        assert dr2.state == DagRunState.RUNNING

    def test_dagrun_success_callback(self, dag_maker, session):
        def on_success_callable(context):
            assert context["dag_run"].dag_id == "test_dagrun_success_callback"

        with dag_maker(
            dag_id="test_dagrun_success_callback",
            schedule=datetime.timedelta(days=1),
            start_date=datetime.datetime(2017, 1, 1),
            on_success_callback=on_success_callable,
        ) as dag:
            dag_task1 = EmptyOperator(task_id="test_state_succeeded1")
            dag_task2 = EmptyOperator(task_id="test_state_succeeded2")
            dag_task1.set_downstream(dag_task2)

        initial_task_states = {
            "test_state_succeeded1": TaskInstanceState.SUCCESS,
            "test_state_succeeded2": TaskInstanceState.SUCCESS,
        }

        dag_run = self.create_dag_run(dag=dag, task_states=initial_task_states, session=session)
        with mock.patch.object(dag_run, "execute_dag_callbacks") as execute_dag_callbacks:
            _, callback = dag_run.update_state()
        assert execute_dag_callbacks.mock_calls == [
            mock.call(dag=dag, success=True, relevant_ti=ANY, reason="success")
        ]
        # Make sure the correct TI is passed on success
        call_args = execute_dag_callbacks.call_args
        ti_passed = call_args.kwargs["relevant_ti"]
        assert ti_passed.task_id == "test_state_succeeded2"

        assert dag_run.state == DagRunState.SUCCESS
        # Callbacks are not added until handle_callback = False is passed to dag_run.update_state()
        assert callback is None

    def test_dagrun_failure_callback(self, dag_maker, session):
        def on_failure_callable(context):
            assert context["dag_run"].dag_id == "test_dagrun_failure_callback"

        with dag_maker(
            dag_id="test_dagrun_failure_callback",
            schedule=datetime.timedelta(days=1),
            start_date=datetime.datetime(2017, 1, 1),
            on_failure_callback=on_failure_callable,
        ) as dag:
            dag_task1 = EmptyOperator(task_id="test_state_succeeded1")
            dag_task2 = EmptyOperator(task_id="test_state_failed2")

        initial_task_states = {
            "test_state_succeeded1": TaskInstanceState.SUCCESS,
            "test_state_failed2": TaskInstanceState.FAILED,
        }
        dag_task1.set_downstream(dag_task2)

        dag_run = self.create_dag_run(dag=dag, task_states=initial_task_states, session=session)
        with mock.patch.object(dag_run, "execute_dag_callbacks") as execute_dag_callbacks:
            _, callback = dag_run.update_state()
        assert execute_dag_callbacks.mock_calls == [
            mock.call(dag=dag, success=False, relevant_ti=ANY, reason="task_failure")
        ]
        # Make sure the correct TI is passed on failure
        call_args = execute_dag_callbacks.call_args
        ti_passed = call_args.kwargs["relevant_ti"]
        assert ti_passed.task_id == "test_state_failed2"

        assert dag_run.state == DagRunState.FAILED
        # Callbacks are not added until handle_callback = False is passed to dag_run.update_state()
        assert callback is None

    def test_dagrun_failure_callback_on_tasks_deadlocked(self, dag_maker, session):
        def on_failure_callable(context):
            assert context["dag_run"].dag_id == "test_dagrun_failure_callback_on_tasks_deadlocked"

        with dag_maker(
            dag_id="test_dagrun_failure_callback_on_tasks_deadlocked",
            schedule=datetime.timedelta(days=1),
            start_date=datetime.datetime(2017, 1, 1),
            on_failure_callback=on_failure_callable,
        ):
            up = EmptyOperator(task_id="upstream")
            middle = EmptyOperator(task_id="wrong")
            down = EmptyOperator(task_id="downstream")

            middle.trigger_rule = TriggerRule.ONE_FAILED
            middle.set_upstream(up)
            middle.set_downstream(down)

        dr = dag_maker.create_dagrun()

        ti_up: TI = dr.get_task_instance(task_id=up.task_id, session=session)
        ti_middle: TI = dr.get_task_instance(task_id=middle.task_id, session=session)
        ti_up.set_state(state=TaskInstanceState.SUCCESS, session=session)
        ti_middle.set_state(state=None, session=session)
        ti_middle.task.trigger_rule = "invalid"

        serialized_dag = dr.get_dag()

        with mock.patch.object(dr, "execute_dag_callbacks") as execute_dag_callbacks:
            _, callback = dr.update_state(execute_callbacks=True)
        assert execute_dag_callbacks.mock_calls == [
            mock.call(dag=serialized_dag, success=False, relevant_ti=ti_middle, reason="all_tasks_deadlocked")
        ]
        # Make sure the correct TI is passed on deadlock
        call_args = execute_dag_callbacks.call_args
        ti_passed = call_args.kwargs["relevant_ti"]
        assert ti_passed.task_id == "wrong"

        assert dr.state == DagRunState.FAILED
        # Callbacks is None as execute_callbacks=True
        assert callback is None

    def test_on_success_callback_when_task_skipped(self, session, testing_dag_bundle):
        mock_on_success = mock.MagicMock()
        mock_on_success.__name__ = "mock_on_success"

        dag = DAG(
            dag_id="test_dagrun_update_state_with_handle_callback_success",
            start_date=datetime.datetime(2017, 1, 1),
            on_success_callback=mock_on_success,
            schedule=datetime.timedelta(days=1),
        )

        _ = EmptyOperator(task_id="test_state_succeeded1", dag=dag)

        # Create DagModel directly with bundle_name
        dag_model = DagModel(
            dag_id=dag.dag_id,
            bundle_name="testing",
        )
        session.merge(dag_model)
        session.flush()

        scheduler_dag = sync_dag_to_db(dag, session=session)
        scheduler_dag.on_success_callback = mock_on_success

        initial_task_states = {
            "test_state_succeeded1": TaskInstanceState.SKIPPED,
        }

        dag_run = self.create_dag_run(scheduler_dag, task_states=initial_task_states, session=session)
        _, _ = dag_run.update_state(execute_callbacks=True)
        task = dag_run.get_task_instances()[0]

        assert task.state == TaskInstanceState.SKIPPED
        assert dag_run.state == DagRunState.SUCCESS
        mock_on_success.assert_called_once()

    def test_dagrun_update_state_with_handle_callback_success(self, testing_dag_bundle, dag_maker, session):
        def on_success_callable(context):
            assert context["dag_run"].dag_id == "test_dagrun_update_state_with_handle_callback_success"

        relative_fileloc = "test_dagrun_update_state_with_handle_callback_success.py"
        with dag_maker(
            dag_id="test_dagrun_update_state_with_handle_callback_success",
            schedule=datetime.timedelta(days=1),
            start_date=datetime.datetime(2017, 1, 1),
            on_success_callback=on_success_callable,
        ) as dag:
            dag_task1 = EmptyOperator(task_id="test_state_succeeded1")
            dag_task2 = EmptyOperator(task_id="test_state_succeeded2")
            dag_task1.set_downstream(dag_task2)
        dm = DagModel.get_dagmodel(dag.dag_id, session=session)
        dm.relative_fileloc = relative_fileloc
        session.merge(dm)
        session.commit()

        initial_task_states = {
            "test_state_succeeded1": TaskInstanceState.SUCCESS,
            "test_state_succeeded2": TaskInstanceState.SUCCESS,
        }
        dag.relative_fileloc = relative_fileloc
        SerializedDagModel.write_dag(LazyDeserializedDAG.from_dag(dag), bundle_name="dag_maker")
        session.commit()

        dag_run = self.create_dag_run(dag=dag, task_states=initial_task_states, session=session)
        dag_run.dag_model = dm

        _, callback = dag_run.update_state(execute_callbacks=False)
        assert dag_run.state == DagRunState.SUCCESS
        # Callbacks are not added until handle_callback = False is passed to dag_run.update_state()
        assert callback == DagCallbackRequest(
            filepath=dag_run.dag.relative_fileloc,
            dag_id="test_dagrun_update_state_with_handle_callback_success",
            run_id=dag_run.run_id,
            is_failure_callback=False,
            bundle_name="dag_maker",
            bundle_version=None,
            context_from_server=DagRunContext(
                dag_run=dag_run,
                last_ti=dag_run.get_task_instance(task_id="test_state_succeeded2"),
            ),
            msg="success",
        )

    def test_dagrun_update_state_with_handle_callback_failure(self, testing_dag_bundle, dag_maker, session):
        def on_failure_callable(context):
            assert context["dag_run"].dag_id == "test_dagrun_update_state_with_handle_callback_failure"

        relative_fileloc = "test_dagrun_update_state_with_handle_callback_failure.py"
        with dag_maker(
            dag_id="test_dagrun_update_state_with_handle_callback_failure",
            schedule=datetime.timedelta(days=1),
            start_date=datetime.datetime(2017, 1, 1),
            on_failure_callback=on_failure_callable,
        ) as dag:
            dag_task1 = EmptyOperator(task_id="test_state_succeeded1")
            dag_task2 = EmptyOperator(task_id="test_state_failed2")
            dag_task1.set_downstream(dag_task2)
        dm = DagModel.get_dagmodel(dag.dag_id, session=session)
        dm.relative_fileloc = relative_fileloc
        session.merge(dm)
        session.commit()

        initial_task_states = {
            "test_state_succeeded1": TaskInstanceState.SUCCESS,
            "test_state_failed2": TaskInstanceState.FAILED,
        }
        dag.relative_fileloc = relative_fileloc
        SerializedDagModel.write_dag(LazyDeserializedDAG.from_dag(dag), bundle_name="dag_maker")
        session.commit()

        dag_run = self.create_dag_run(dag=dag, task_states=initial_task_states, session=session)
        dag_run.dag_model = dm

        _, callback = dag_run.update_state(execute_callbacks=False)
        assert dag_run.state == DagRunState.FAILED
        # Callbacks are not added until handle_callback = False is passed to dag_run.update_state()

        assert callback == DagCallbackRequest(
            filepath=dag.relative_fileloc,
            dag_id="test_dagrun_update_state_with_handle_callback_failure",
            run_id=dag_run.run_id,
            is_failure_callback=True,
            msg="task_failure",
            bundle_name="dag_maker",
            bundle_version=None,
            context_from_server=DagRunContext(
                dag_run=dag_run,
                last_ti=dag_run.get_task_instance(task_id="test_state_failed2"),
            ),
        )

    def test_dagrun_set_state_end_date(self, dag_maker, session):
        with dag_maker(schedule=datetime.timedelta(days=1), start_date=DEFAULT_DATE):
            pass

        dr = dag_maker.create_dagrun()

        # Initial end_date should be NULL
        # DagRunState.SUCCESS and DagRunState.FAILED are all ending state and should set end_date
        # DagRunState.RUNNING set end_date back to NULL
        session.add(dr)
        session.commit()
        assert dr.end_date is None

        dr.set_state(DagRunState.SUCCESS)
        session.merge(dr)
        session.commit()

        dr_database = session.scalar(select(DagRun).where(DagRun.run_id == dr.run_id))
        assert dr_database.end_date is not None
        assert dr.end_date == dr_database.end_date

        dr.set_state(DagRunState.RUNNING)
        session.merge(dr)
        session.commit()

        dr_database = session.scalar(select(DagRun).where(DagRun.run_id == dr.run_id))

        assert dr_database.end_date is None

        dr.set_state(DagRunState.FAILED)
        session.merge(dr)
        session.commit()
        dr_database = session.scalar(select(DagRun).where(DagRun.run_id == dr.run_id))

        assert dr_database.end_date is not None
        assert dr.end_date == dr_database.end_date

    def test_dagrun_update_state_end_date(self, dag_maker, session):
        # A -> B
        with dag_maker(schedule=datetime.timedelta(days=1)):
            op1 = EmptyOperator(task_id="A")
            op2 = EmptyOperator(task_id="B")
            op1.set_upstream(op2)

        dr = dag_maker.create_dagrun()

        # Initial end_date should be NULL
        # DagRunState.SUCCESS and DagRunState.FAILED are all ending state and should set end_date
        # DagRunState.RUNNING set end_date back to NULL
        session.merge(dr)
        session.commit()
        assert dr.end_date is None

        ti_op1 = dr.get_task_instance(task_id=op1.task_id)
        ti_op1.set_state(state=TaskInstanceState.SUCCESS, session=session)
        ti_op2 = dr.get_task_instance(task_id=op2.task_id)
        ti_op2.set_state(state=TaskInstanceState.SUCCESS, session=session)

        dr.update_state()

        dr_database = session.scalar(select(DagRun).where(DagRun.run_id == dr.run_id))
        assert dr_database.end_date is not None
        assert dr.end_date == dr_database.end_date

        ti_op1.set_state(state=TaskInstanceState.RUNNING, session=session)
        ti_op2.set_state(state=TaskInstanceState.RUNNING, session=session)
        dr.update_state()

        dr_database = session.scalar(select(DagRun).where(DagRun.run_id == dr.run_id))

        assert dr._state == DagRunState.RUNNING
        assert dr.end_date is None
        assert dr_database.end_date is None

        ti_op1.set_state(state=TaskInstanceState.FAILED, session=session)
        ti_op2.set_state(state=TaskInstanceState.FAILED, session=session)
        dr.update_state()

        dr_database = session.scalar(select(DagRun).where(DagRun.run_id == dr.run_id))

        assert dr_database.end_date is not None
        assert dr.end_date == dr_database.end_date

    def test_get_task_instance_on_empty_dagrun(self, dag_maker, session):
        """
        Make sure that a proper value is returned when a dagrun has no task instances
        """
        with dag_maker(
            dag_id="test_get_task_instance_on_empty_dagrun",
            schedule=datetime.timedelta(days=1),
            start_date=timezone.datetime(2017, 1, 1),
        ) as dag:
            ShortCircuitOperator(task_id="test_short_circuit_false", python_callable=lambda: False)

        now = timezone.utcnow()

        # Don't use create_dagrun since it will create the task instances too which we
        # don't want
        dag_run = DagRun(
            dag_id=dag.dag_id,
            run_id="test_get_task_instance_on_empty_dagrun",
            run_type=DagRunType.MANUAL,
            logical_date=now,
            start_date=now,
            state=DagRunState.RUNNING,
        )
        session.add(dag_run)
        session.commit()

        ti = dag_run.get_task_instance("test_short_circuit_false")
        assert ti is None

    def test_get_latest_runs(self, dag_maker, session):
        with dag_maker(
            dag_id="test_latest_runs_1", schedule=datetime.timedelta(days=1), start_date=DEFAULT_DATE
        ) as dag:
            ...
        self.create_dag_run(dag, logical_date=timezone.datetime(2015, 1, 1), session=session)
        self.create_dag_run(dag, logical_date=timezone.datetime(2015, 1, 2), session=session)
        dagruns = DagRun.get_latest_runs(session)
        session.close()
        for dagrun in dagruns:
            if dagrun.dag_id == "test_latest_runs_1":
                assert dagrun.logical_date == timezone.datetime(2015, 1, 2)

    def test_removed_task_instances_can_be_restored(self, dag_maker, session):
        def create_dag():
            return dag_maker(
                dag_id="test_task_restoration",
                schedule=datetime.timedelta(days=1),
                start_date=DEFAULT_DATE,
            )

        with create_dag() as dag:
            EmptyOperator(task_id="flaky_task", owner="test")

        dagrun = self.create_dag_run(dag, session=session)
        flaky_ti = dagrun.get_task_instances()[0]
        assert flaky_ti.task_id == "flaky_task"
        assert flaky_ti.state is None

        with create_dag() as dag:
            pass

        dagrun.dag = dag
        dag_version_id = DagVersion.get_latest_version(dag.dag_id, session=session).id
        dagrun.verify_integrity(dag_version_id=dag_version_id)
        flaky_ti.refresh_from_db()
        assert flaky_ti.state is None

        with create_dag() as dag:
            EmptyOperator(task_id="flaky_task", owner="test")

        dagrun.verify_integrity(dag_version_id=dag_version_id)
        flaky_ti.refresh_from_db()
        assert flaky_ti.state is None

    def test_already_added_task_instances_can_be_ignored(self, dag_maker, session):
        with dag_maker("triggered_dag", schedule=datetime.timedelta(days=1), start_date=DEFAULT_DATE) as dag:
            ...
        dag.add_task(EmptyOperator(task_id="first_task", owner="test"))

        dagrun = self.create_dag_run(dag, session=session)
        first_ti = dagrun.get_task_instances()[0]
        assert first_ti.task_id == "first_task"
        assert first_ti.state is None

        # Lets assume that the above TI was added into DB by webserver, but if scheduler
        # is running the same method at the same time it would find 0 TIs for this dag
        # and proceeds further to create TIs. Hence mocking DagRun.get_task_instances
        # method to return an empty list of TIs.
        with mock.patch.object(DagRun, "get_task_instances") as mock_gtis:
            mock_gtis.return_value = []
            dagrun.verify_integrity(
                dag_version_id=DagVersion.get_latest_version(dag.dag_id, session=session).id
            )
            first_ti.refresh_from_db()
            assert first_ti.state is None

    @pytest.mark.parametrize("state", State.task_states)
    @mock.patch.object(settings, "task_instance_mutation_hook", autospec=True)
    def test_task_instance_mutation_hook(self, mock_hook, dag_maker, session, state):
        def mutate_task_instance(task_instance):
            if task_instance.queue == "queue1":
                task_instance.queue = "queue2"
            else:
                task_instance.queue = "queue1"

        mock_hook.side_effect = mutate_task_instance

        with dag_maker(
            "test_task_instance_mutation_hook",
            schedule=datetime.timedelta(days=1),
            start_date=DEFAULT_DATE,
        ) as dag:
            EmptyOperator(task_id="task_to_mutate", owner="test", queue="queue1")

        dagrun = self.create_dag_run(dag, session=session)
        task = dagrun.get_task_instances()[0]
        task.state = state
        session.merge(task)
        session.commit()
        assert task.queue == "queue2"

        dagrun.verify_integrity(dag_version_id=DagVersion.get_latest_version(dag.dag_id, session=session).id)
        task = dagrun.get_task_instances()[0]
        assert task.queue == "queue1"

    def test_task_instance_mutation_hook_has_run_id(self, dag_maker, session):
        """Test that task_instance_mutation_hook receives a TI with run_id set (not None).

        Regression test for https://github.com/apache/airflow/issues/61945
        """
        observed_run_ids = []

        def mutate_task_instance(task_instance):
            observed_run_ids.append(task_instance.run_id)
            if task_instance.run_id and task_instance.run_id.startswith("manual__"):
                task_instance.pool = "manual_pool"

        with mock.patch.object(
            get_policy_plugin_manager().hook, "task_instance_mutation_hook", autospec=True
        ) as mock_hook:
            mock_hook.side_effect = mutate_task_instance
            with dag_maker(
                dag_id="test_mutation_hook_run_id", schedule=datetime.timedelta(days=1), session=session
            ) as dag:
                EmptyOperator(task_id="mutated_task", owner="test")

            self.create_dag_run(dag, session=session)
        # The hook should have been called during TI creation with run_id set
        assert any(rid is not None for rid in observed_run_ids), (
            f"task_instance_mutation_hook was called with run_id=None. Observed run_ids: {observed_run_ids}"
        )

    @pytest.mark.parametrize(
        ("prev_ti_state", "is_ti_schedulable"),
        [
            (TaskInstanceState.SUCCESS, True),
            (TaskInstanceState.SKIPPED, True),
            (TaskInstanceState.RUNNING, False),
            (TaskInstanceState.FAILED, False),
            (None, False),
        ],
    )
    def test_depends_on_past(self, dag_maker, session, prev_ti_state, is_ti_schedulable):
        # DAG tests depends_on_past dependencies
        with dag_maker(
            dag_id="test_depends_on_past", schedule=datetime.timedelta(days=1), session=session
        ) as dag:
            BaseOperator(
                task_id="test_dop_task",
                depends_on_past=True,
            )

        task = dag.tasks[0]

        dag_run_1: DagRun = dag_maker.create_dagrun(
            logical_date=timezone.datetime(2016, 1, 1, 0, 0, 0),
            run_type=DagRunType.SCHEDULED,
        )
        dag_run_2: DagRun = dag_maker.create_dagrun(
            logical_date=timezone.datetime(2016, 1, 2, 0, 0, 0),
            run_type=DagRunType.SCHEDULED,
        )

        prev_ti = TI(task, run_id=dag_run_1.run_id, dag_version_id=dag_run_1.created_dag_version_id)
        prev_ti.refresh_from_db(session=session)
        prev_ti.set_state(prev_ti_state, session=session)
        session.flush()
        ti = TI(task, run_id=dag_run_2.run_id, dag_version_id=dag_run_1.created_dag_version_id)
        ti.refresh_from_db(session=session)

        decision = dag_run_2.task_instance_scheduling_decisions(session=session)
        schedulable_tis = [ti.task_id for ti in decision.schedulable_tis]
        assert ("test_dop_task" in schedulable_tis) == is_ti_schedulable

    @pytest.mark.parametrize(
        ("prev_ti_state", "is_ti_schedulable"),
        [
            (TaskInstanceState.SUCCESS, True),
            (TaskInstanceState.SKIPPED, True),
            (TaskInstanceState.RUNNING, False),
            (TaskInstanceState.FAILED, False),
            (None, False),
        ],
    )
    def test_wait_for_downstream(self, dag_maker, session, prev_ti_state, is_ti_schedulable):
        dag_id = "test_wait_for_downstream"

        with dag_maker(dag_id=dag_id, session=session, serialized=True) as dag:
            dag_wfd_upstream = EmptyOperator(
                task_id="upstream_task",
                wait_for_downstream=True,
            )
            dag_wfd_downstream = EmptyOperator(task_id="downstream_task")
            dag_wfd_upstream >> dag_wfd_downstream
        upstream, downstream = dag.tasks

        # For ti.set_state() to work, the DagRun has to exist,
        # Otherwise ti.previous_ti returns an unpersisted TI
        dag_run_1: DagRun = dag_maker.create_dagrun(
            logical_date=timezone.datetime(2016, 1, 1, 0, 0, 0),
            run_type=DagRunType.SCHEDULED,
        )
        dag_run_2: DagRun = dag_maker.create_dagrun(
            logical_date=timezone.datetime(2016, 1, 2, 0, 0, 0),
            run_type=DagRunType.SCHEDULED,
        )

        ti = dag_run_2.get_task_instance(task_id=upstream.task_id, session=session)

        # Operate on serialized operator since it is Scheduler code
        ti.task = dag.task_dict[ti.task_id]
        prev_ti_downstream = dag_run_1.get_task_instance(task_id=downstream.task_id, session=session)
        prev_ti_upstream = ti.get_previous_ti(session=session)
        assert ti
        assert prev_ti_upstream
        assert prev_ti_downstream
        prev_ti_upstream.state = TaskInstanceState.SUCCESS

        prev_ti_downstream.state = prev_ti_state
        session.flush()

        decision = dag_run_2.task_instance_scheduling_decisions(session=session)
        schedulable_tis = [ti.task_id for ti in decision.schedulable_tis]
        assert (upstream.task_id in schedulable_tis) == is_ti_schedulable

    @pytest.mark.parametrize("state", [DagRunState.QUEUED, DagRunState.RUNNING])
    def test_next_dagruns_to_examine_only_unpaused(self, session, state, testing_dag_bundle):
        """
        Check that "next_dagruns_to_examine" ignores runs from paused/inactive DAGs
        and gets running/queued dagruns
        """
        dag = DAG(dag_id="test_dags", schedule=datetime.timedelta(days=1), start_date=DEFAULT_DATE)
        EmptyOperator(task_id="dummy", dag=dag, owner="airflow")

        orm_dag = DagModel(
            dag_id=dag.dag_id,
            bundle_name="testing",
            has_task_concurrency_limits=False,
            next_dagrun=DEFAULT_DATE,
            next_dagrun_create_after=DEFAULT_DATE + datetime.timedelta(days=1),
            is_stale=False,
        )
        session.add(orm_dag)
        session.flush()
        scheduler_dag = sync_dag_to_db(dag, session=session)
        dr = scheduler_dag.create_dagrun(
            run_id=scheduler_dag.timetable.generate_run_id(
                run_type=DagRunType.SCHEDULED,
                run_after=DEFAULT_DATE,
                data_interval=infer_automated_data_interval(scheduler_dag.timetable, DEFAULT_DATE),
            ),
            run_type=DagRunType.SCHEDULED,
            state=state,
            logical_date=DEFAULT_DATE,
            data_interval=infer_automated_data_interval(scheduler_dag.timetable, DEFAULT_DATE),
            run_after=DEFAULT_DATE,
            start_date=DEFAULT_DATE if state == DagRunState.RUNNING else None,
            session=session,
            triggered_by=DagRunTriggeredByType.TEST,
        )

        if state == DagRunState.RUNNING:
            func = DagRun.get_running_dag_runs_to_examine
        else:
            func = DagRun.get_queued_dag_runs_to_set_running
        runs = func(session).all()

        assert runs == [dr]

        orm_dag.is_paused = True
        session.merge(orm_dag)
        session.commit()

        runs = func(session).all()
        assert runs == []

    @mock.patch("airflow._shared.observability.metrics.stats.timing")
    def test_no_scheduling_delay_for_nonscheduled_runs(self, stats_mock, session, testing_dag_bundle):
        """
        Tests that dag scheduling delay stat is not called if the dagrun is not a scheduled run.
        This case is manual run. Simple test for coherence check.
        """
        dag = DAG(dag_id="test_dagrun_stats", schedule=datetime.timedelta(days=1), start_date=DEFAULT_DATE)
        dag_task = EmptyOperator(task_id="dummy", dag=dag)

        # Create DagModel directly with bundle_name
        dag_model = DagModel(
            dag_id=dag.dag_id,
            bundle_name="testing",
        )
        session.merge(dag_model)
        session.flush()

        scheduler_dag = sync_dag_to_db(dag, session=session)
        initial_task_states = {dag_task.task_id: TaskInstanceState.SUCCESS}
        dag_run = self.create_dag_run(scheduler_dag, task_states=initial_task_states, session=session)
        dag_run.update_state(session=session)
        assert call(f"dagrun.{dag.dag_id}.first_task_scheduling_delay") not in stats_mock.mock_calls

    @pytest.mark.parametrize(
        ("schedule", "expected"),
        [
            ("*/5 * * * *", True),
            (None, False),
            ("@once", False),
        ],
    )
    def test_emit_scheduling_delay(self, session, schedule, expected, testing_dag_bundle):
        """
        Tests that dag scheduling delay stat is set properly once running scheduled dag.
        dag_run.update_state() invokes the _emit_true_scheduling_delay_stats_for_finished_state method.
        """
        dag = DAG(dag_id="test_emit_dag_stats", start_date=DEFAULT_DATE, schedule=schedule)
        dag_task = EmptyOperator(task_id="dummy", dag=dag, owner="airflow")
        expected_stat_tags = {"dag_id": f"{dag.dag_id}", "run_type": DagRunType.SCHEDULED}
        scheduler_dag = sync_dag_to_db(dag, session=session)
        try:
            info = scheduler_dag.next_dagrun_info(last_automated_run_info=None)
            orm_dag_kwargs = {
                "dag_id": dag.dag_id,
                "bundle_name": "testing",
                "has_task_concurrency_limits": False,
                "is_stale": False,
            }
            if info is not None:
                orm_dag_kwargs.update(
                    {
                        "next_dagrun": info.logical_date,
                        "next_dagrun_data_interval": info.data_interval,
                        "next_dagrun_create_after": info.run_after,
                    },
                )
            orm_dag = DagModel(**orm_dag_kwargs)
            session.merge(orm_dag)
            session.flush()
            dag_run = scheduler_dag.create_dagrun(
                run_id=scheduler_dag.timetable.generate_run_id(
                    run_type=DagRunType.SCHEDULED,
                    run_after=dag.start_date,
                    data_interval=infer_automated_data_interval(scheduler_dag.timetable, dag.start_date),
                ),
                run_type=DagRunType.SCHEDULED,
                state=DagRunState.SUCCESS,
                logical_date=dag.start_date,
                data_interval=infer_automated_data_interval(scheduler_dag.timetable, dag.start_date),
                run_after=dag.start_date,
                start_date=dag.start_date,
                triggered_by=DagRunTriggeredByType.TEST,
                session=session,
            )
            ti = dag_run.get_task_instance(dag_task.task_id, session)
            ti.set_state(TaskInstanceState.SUCCESS, session)
            session.flush()

            with mock.patch("airflow._shared.observability.metrics.stats.timing") as stats_mock:
                dag_run.update_state(session)

            metric_name = f"dagrun.{dag.dag_id}.first_task_scheduling_delay"

            if expected:
                true_delay = ti.start_date - dag_run.run_after
                sched_delay_stat_call = call(metric_name, true_delay, tags=expected_stat_tags)
                sched_delay_stat_call_with_tags = call(
                    "dagrun.first_task_scheduling_delay", true_delay, tags=expected_stat_tags
                )
                assert sched_delay_stat_call in stats_mock.mock_calls
                assert sched_delay_stat_call_with_tags in stats_mock.mock_calls
            else:
                # Assert that we never passed the metric
                sched_delay_stat_call = call(
                    metric_name,
                    mock.ANY,
                )
                assert sched_delay_stat_call not in stats_mock.mock_calls
        finally:
            # Don't write anything to the DB
            session.rollback()
            session.close()

    @pytest.mark.parametrize(
        ("queued_at_offset", "expected"),
        [
            (datetime.timedelta(seconds=30), True),
            (None, False),
        ],
    )
    def test_emit_first_task_start_delay(self, session, queued_at_offset, expected, testing_dag_bundle):
        """
        Tests that `dagrun.first_task_start_delay` measures `queued_at -> first_start_date`
        and is only emitted when `queued_at` is set and the delta is positive.
        """
        dag = DAG(
            dag_id="test_emit_first_task_start_delay",
            start_date=DEFAULT_DATE,
            schedule="*/5 * * * *",
        )
        dag_task = EmptyOperator(task_id="dummy", dag=dag, owner="airflow")
        expected_stat_tags = {"dag_id": dag.dag_id, "run_type": DagRunType.SCHEDULED}
        scheduler_dag = sync_dag_to_db(dag, session=session)
        try:
            info = scheduler_dag.next_dagrun_info(last_automated_run_info=None)
            orm_dag_kwargs = {
                "dag_id": dag.dag_id,
                "bundle_name": "testing",
                "has_task_concurrency_limits": False,
                "is_stale": False,
            }
            if info is not None:
                orm_dag_kwargs.update(
                    {
                        "next_dagrun": info.logical_date,
                        "next_dagrun_data_interval": info.data_interval,
                        "next_dagrun_create_after": info.run_after,
                    },
                )
            orm_dag = DagModel(**orm_dag_kwargs)
            session.merge(orm_dag)
            session.flush()

            ti_start_date = dag.start_date + datetime.timedelta(minutes=1)
            queued_at = ti_start_date - queued_at_offset if queued_at_offset else None

            dag_run = scheduler_dag.create_dagrun(
                run_id=scheduler_dag.timetable.generate_run_id(
                    run_type=DagRunType.SCHEDULED,
                    run_after=dag.start_date,
                    data_interval=infer_automated_data_interval(scheduler_dag.timetable, dag.start_date),
                ),
                run_type=DagRunType.SCHEDULED,
                state=DagRunState.SUCCESS,
                logical_date=dag.start_date,
                data_interval=infer_automated_data_interval(scheduler_dag.timetable, dag.start_date),
                run_after=dag.start_date,
                start_date=dag.start_date,
                triggered_by=DagRunTriggeredByType.TEST,
                session=session,
            )
            dag_run.queued_at = queued_at
            ti = dag_run.get_task_instance(dag_task.task_id, session)
            ti.set_state(TaskInstanceState.SUCCESS, session)
            ti.start_date = ti_start_date
            session.flush()

            with mock.patch("airflow._shared.observability.metrics.stats.timing") as stats_mock:
                dag_run.update_state(session)

            start_delay_call = call("dagrun.first_task_start_delay", mock.ANY, tags=expected_stat_tags)
            if expected:
                expected_delta = ti_start_date - queued_at
                assert (
                    call("dagrun.first_task_start_delay", expected_delta, tags=expected_stat_tags)
                    in stats_mock.mock_calls
                )
            else:
                assert start_delay_call not in stats_mock.mock_calls
        finally:
            session.rollback()
            session.close()

    def test_states_sets(self, dag_maker, session):
        """
        Tests that adding State.failed_states and State.success_states work as expected.
        """
        with dag_maker(
            dag_id="test_dagrun_states", schedule=datetime.timedelta(days=1), start_date=DEFAULT_DATE
        ) as dag:
            dag_task_success = EmptyOperator(task_id="dummy")
            dag_task_failed = EmptyOperator(task_id="dummy2")

        initial_task_states = {
            dag_task_success.task_id: TaskInstanceState.SUCCESS,
            dag_task_failed.task_id: TaskInstanceState.FAILED,
        }
        dag_run = self.create_dag_run(dag=dag, task_states=initial_task_states, session=session)
        ti_success = dag_run.get_task_instance(dag_task_success.task_id)
        ti_failed = dag_run.get_task_instance(dag_task_failed.task_id)
        assert ti_success.state in State.success_states
        assert ti_failed.state in State.failed_states

    def test_update_state_one_unfinished(self, dag_maker, session):
        """
        Previously this lived in test_scheduler_job.py

        It only really tested the behavior of DagRun.update_state.

        As far as I can tell, it checks that if you null out the state on a TI of a finished dag,
        and then you call ``update_state``, then the DR will be set to running.
        """
        with dag_maker(session=session) as dag:
            PythonOperator(task_id="t1", python_callable=lambda: print)
            PythonOperator(task_id="t2", python_callable=lambda: print)
        dr = dag_maker.create_dagrun(state=DagRunState.FAILED)
        for ti in dr.get_task_instances(session=session):
            ti.state = TaskInstanceState.FAILED
        session.commit()
        session.expunge_all()
        dr = session.get(DagRun, dr.id)
        assert dr.state == DagRunState.FAILED
        ti = dr.get_task_instance("t1", session=session)
        ti.state = State.NONE
        session.commit()
        dr = session.get(DagRun, dr.id)
        assert dr.state == DagRunState.FAILED
        dr.dag = dag
        dr.update_state(session=session)
        session.commit()
        dr = session.get(DagRun, dr.id)
        assert dr.state == State.RUNNING

    def test_dag_run_dag_versions_method(self, dag_maker, session):
        with dag_maker(
            "test_dag_run_dag_versions", schedule=datetime.timedelta(days=1), start_date=DEFAULT_DATE
        ):
            EmptyOperator(task_id="empty")
        dag_run = dag_maker.create_dagrun()

        dm = session.scalar(select(DagModel).options(joinedload(DagModel.dag_versions)))
        assert dag_run.dag_versions[0].id == dm.dag_versions[0].id

    def test_dag_run_version_number(self, dag_maker, session):
        with dag_maker(
            "test_dag_run_version_number", schedule=datetime.timedelta(days=1), start_date=DEFAULT_DATE
        ):
            EmptyOperator(task_id="empty") >> EmptyOperator(task_id="empty2")
        dag_run = dag_maker.create_dagrun()
        tis = dag_run.task_instances
        tis[0].set_state(TaskInstanceState.SUCCESS)
        dag_v = DagVersion.write_dag(dag_id=dag_run.dag_id, bundle_name="testing", version_number=2)
        tis[1].dag_version = dag_v
        session.merge(tis[1])
        session.flush()
        dag_run = session.scalar(select(DagRun).where(DagRun.run_id == dag_run.run_id))
        # Check that dag_run.version_number returns the version number of
        # the latest task instance dag_version
        assert dag_run.version_number == dag_v.version_number

    def test_dag_run_dag_versions_with_null_created_dag_version(self, dag_maker, session):
        """Test that dag_versions returns empty list when created_dag_version is None and bundle_version is populated."""
        with dag_maker(
            "test_dag_run_null_created_dag_version",
            schedule=datetime.timedelta(days=1),
            start_date=DEFAULT_DATE,
        ):
            EmptyOperator(task_id="empty")
        dag_run = dag_maker.create_dagrun()

        dag_run.bundle_version = "some_bundle_version"
        dag_run.created_dag_version_id = None
        dag_run.created_dag_version = None
        session.merge(dag_run)
        session.flush()

        # This should return empty list, not [None]
        assert dag_run.dag_versions == []
        assert isinstance(dag_run.dag_versions, list)
        assert len(dag_run.dag_versions) == 0

    @mock.patch.object(Deadline, "prune_deadlines")
    def test_dagrun_success_deadline(self, _, session, deadline_test_dag):
        def on_success_callable(context):
            assert context["dag_run"].dag_id == "test_dag"

        future_date = datetime.datetime.now() + datetime.timedelta(days=365)

        scheduler_dag = deadline_test_dag(
            deadline=DeadlineAlert(
                reference=DeadlineReference.FIXED_DATETIME(future_date),
                interval=datetime.timedelta(hours=1),
                callback=AsyncCallback(empty_callback_for_deadline),
            ),
            on_success_callback=on_success_callable,
        )

        dag_run = self.create_dag_run(
            dag=scheduler_dag,
            task_states={"task_1": TaskInstanceState.SUCCESS, "task_2": TaskInstanceState.SUCCESS},
            session=session,
        )
        dag_run.dag = scheduler_dag

        with mock.patch.object(dag_run, "execute_dag_callbacks") as execute_dag_callbacks:
            _, callback = dag_run.update_state()
        assert execute_dag_callbacks.mock_calls == [
            mock.call(dag=scheduler_dag, success=True, relevant_ti=ANY, reason="success")
        ]
        # Make sure the correct TI is passed on success
        call_args = execute_dag_callbacks.call_args
        ti_passed = call_args.kwargs["relevant_ti"]
        assert ti_passed.task_id == "task_2"

        assert dag_run.state == DagRunState.SUCCESS
        # Callbacks are not added until handle_callback = False is passed to dag_run.update_state()
        assert callback is None

    @mock.patch.object(Deadline, "prune_deadlines")
    @mock.patch.object(DeadlineAlertModel, "get_by_id")
    def test_dagrun_success_prunes_dagrun_deadlines(
        self, mock_get_by_id, mock_prune, session, deadline_test_dag
    ):
        mock_deadline_alert = mock.MagicMock()
        mock_deadline_alert.reference_class = SerializedReferenceModels.FixedDatetimeDeadline
        mock_get_by_id.return_value = mock_deadline_alert

        scheduler_dag = deadline_test_dag()

        deadline_ids = ["deadline-uuid-1", "deadline-uuid-2"]
        scheduler_dag.deadline = deadline_ids

        dag_run = self.create_dag_run(
            dag=scheduler_dag,
            task_states={"task_1": TaskInstanceState.SUCCESS, "task_2": TaskInstanceState.SUCCESS},
            session=session,
        )
        dag_run.dag = scheduler_dag

        dag_run.update_state(session=session)

        assert mock_get_by_id.call_count == len(deadline_ids)
        for deadline_id in deadline_ids:
            mock_get_by_id.assert_any_call(deadline_id, session)
        mock_prune.assert_called_once_with(session=session, conditions={DagRun.id: dag_run.id})
        assert dag_run.state == DagRunState.SUCCESS

    @mock.patch.object(Deadline, "prune_deadlines")
    @mock.patch.object(DeadlineAlertModel, "get_by_id")
    def test_dagrun_success_skips_pruning_non_dagrun_deadlines(
        self, mock_get_by_id, mock_prune, dag_maker, session
    ):
        mock_deadline_alert = mock.MagicMock()
        mock_deadline_alert.reference_class = "TASK"  # Not DAGRUN
        mock_get_by_id.return_value = mock_deadline_alert

        deadline_id = "deadline_alert_uuid"

        with dag_maker(
            dag_id="test_dagrun_no_prune",
            schedule=datetime.timedelta(days=1),
        ) as dag:
            EmptyOperator(task_id="task_1")

        initial_task_states = {"task_1": TaskInstanceState.SUCCESS}

        dag_run = self.create_dag_run(dag=dag, task_states=initial_task_states, session=session)
        dag_run = session.merge(dag_run)
        dag.deadline = [deadline_id]
        dag_run.dag = dag

        dag_run.update_state(session=session)

        mock_get_by_id.assert_called_once_with(deadline_id, session)
        mock_prune.assert_not_called()
        assert dag_run.state == DagRunState.SUCCESS

    @mock.patch.object(Deadline, "prune_deadlines")
    def test_dagrun_success_handles_empty_deadline_list(self, mock_prune, dag_maker, session):
        with dag_maker(
            dag_id="test_dagrun_empty_deadlines",
            schedule=datetime.timedelta(days=1),
        ) as dag:
            EmptyOperator(task_id="task_1")

        initial_task_states = {"task_1": TaskInstanceState.SUCCESS}
        dag_run = self.create_dag_run(dag=dag, task_states=initial_task_states, session=session)
        dag_run = session.merge(dag_run)
        dag.deadline = []
        dag_run.dag = dag

        dag_run.update_state(session=session)

        mock_prune.assert_not_called()
        assert dag_run.state == DagRunState.SUCCESS


@pytest.mark.parametrize(
    ("run_type", "expected_tis"),
    [
        pytest.param(DagRunType.MANUAL, 1, id="manual"),
        pytest.param(DagRunType.BACKFILL_JOB, 3, id="backfill"),
    ],
)
@mock.patch("airflow._shared.observability.metrics.stats._get_backend")
def test_verify_integrity_task_start_and_end_date(
    mock_get_backend, dag_maker, session, run_type, expected_tis
):
    """Test that tasks with specific dates are only created for backfill runs"""
    mock_stats = mock.MagicMock(spec=StatsLogger)
    mock_get_backend.return_value = mock_stats

    with dag_maker("test", schedule=datetime.timedelta(days=1), start_date=DEFAULT_DATE) as dag:
        EmptyOperator(task_id="without")
        EmptyOperator(task_id="with_start_date", start_date=DEFAULT_DATE + datetime.timedelta(1))
        EmptyOperator(task_id="with_end_date", end_date=DEFAULT_DATE - datetime.timedelta(1))

    dag_run = DagRun(
        dag_id=dag.dag_id,
        run_type=run_type,
        logical_date=DEFAULT_DATE,
        run_id=DagRun.generate_run_id(run_type=run_type, logical_date=DEFAULT_DATE, run_after=DEFAULT_DATE),
    )
    dag_run.dag = dag

    session.add(dag_run)
    session.flush()
    dag_version_id = DagVersion.get_latest_version(dag.dag_id, session=session).id
    dag_run.verify_integrity(dag_version_id=dag_version_id, session=session)

    tis = dag_run.task_instances
    assert len(tis) == expected_tis

    mock_stats.incr.assert_any_call(
        "task_instance_created_EmptyOperator",
        count=expected_tis,
        tags={"dag_id": "test", "run_type": run_type},
    )
    mock_stats.incr.assert_any_call(
        "task_instance_created",
        count=expected_tis,
        tags={"dag_id": "test", "run_type": run_type, "task_type": "EmptyOperator"},
    )


@pytest.mark.parametrize("is_noop", [True, False])
def test_expand_mapped_task_instance_at_create(is_noop, dag_maker, session):
    with mock.patch("airflow.settings.task_instance_mutation_hook") as mock_mut:
        mock_mut.is_noop = is_noop
        literal = [1, 2, 3, 4]
        with dag_maker(session=session, dag_id="test_dag"):
            mapped = MockOperator.partial(task_id="task_2").expand(arg2=literal)

        dr = dag_maker.create_dagrun()
        indices = session.scalars(
            select(TI.map_index)
            .where(TI.task_id == mapped.task_id, TI.dag_id == mapped.dag_id, TI.run_id == dr.run_id)
            .order_by(TI.map_index)
        ).all()
        assert indices == [0, 1, 2, 3]


@pytest.mark.parametrize("is_noop", [True, False])
def test_expand_mapped_task_instance_task_decorator(is_noop, dag_maker, session):
    with mock.patch("airflow.settings.task_instance_mutation_hook") as mock_mut:
        mock_mut.is_noop = is_noop

        @task
        def mynameis(arg):
            print(arg)

        literal = [1, 2, 3, 4]
        with dag_maker(session=session, dag_id="test_dag"):
            mynameis.expand(arg=literal)

        dr = dag_maker.create_dagrun()
        indices = session.scalars(
            select(TI.map_index)
            .where(TI.task_id == "mynameis", TI.dag_id == dr.dag_id, TI.run_id == dr.run_id)
            .order_by(TI.map_index)
        ).all()
        assert indices == [0, 1, 2, 3]


def test_verify_integrity_handles_stale_data_error(dag_maker, session):
    """Test that StaleDataError during _create_task_instances is caught and session is rolled back."""
    with dag_maker("test_stale_data_error_dag", session=session) as dag:
        task = EmptyOperator(task_id="task1")

    dr = dag_maker.create_dagrun()
    dag_version_id = DagVersion.get_latest_version(dag.dag_id, session=session).id

    with mock.patch.object(session, "flush", side_effect=StaleDataError()):
        with mock.patch.object(session, "rollback") as mock_rollback:
            # Should not raise — StaleDataError must be caught gracefully.
            # Call _create_task_instances directly with a non-empty task list so the
            # test exercises the session.flush() → StaleDataError → session.rollback() path.
            dr._create_task_instances(
                dag_id=dag.dag_id,
                tasks=[TI(task=task, run_id=dr.run_id, dag_version_id=dag_version_id)],
                created_counts={"EmptyOperator": 1},
                hook_is_noop=False,
                session=session,
            )
            mock_rollback.assert_called_once()


def test_mapped_literal_verify_integrity(dag_maker, session):
    """Test that when the length of a mapped literal changes we remove extra TIs"""

    @task
    def task_2(arg2): ...

    with dag_maker(session=session):
        task_2.expand(arg2=[1, 2, 3, 4])

    dr = dag_maker.create_dagrun()

    query = (
        select(TI.map_index, TI.state)
        .where(TI.task_id == "task_2", TI.dag_id == dr.dag_id, TI.run_id == dr.run_id)
        .order_by(TI.map_index)
    )
    indices = session.execute(query).all()

    assert indices == [(0, None), (1, None), (2, None), (3, None)]

    # Now "change" the DAG and we should see verify_integrity REMOVE some TIs
    with dag_maker(session=session):
        task_2.expand(arg2=[1, 2])

    # Update it to use the new serialized DAG
    dr.dag = dag_maker.serialized_dag
    dag_version_id = DagVersion.get_latest_version(dag_id=dr.dag_id, session=session).id
    dr.verify_integrity(dag_version_id=dag_version_id, session=session)

    indices = session.execute(query).all()
    assert indices == [(0, None), (1, None), (2, TaskInstanceState.REMOVED), (3, TaskInstanceState.REMOVED)]


def test_mapped_literal_to_xcom_arg_verify_integrity(dag_maker, session):
    """Test that when we change from literal to a XComArg the TIs are removed"""

    @task
    def task_2(arg2): ...

    with dag_maker(session=session):
        task_2.expand(arg2=[1, 2, 3, 4])

    dr = dag_maker.create_dagrun()

    with dag_maker(session=session):
        t1 = BaseOperator(task_id="task_1")
        task_2.expand(arg2=t1.output)

    dr.dag = dag_maker.serialized_dag
    dag_version_id = DagVersion.get_latest_version(dag_id=dr.dag_id, session=session).id
    dr.verify_integrity(dag_version_id=dag_version_id, session=session)

    indices = session.execute(
        select(TI.map_index, TI.state)
        .where(TI.task_id == "task_2", TI.dag_id == dr.dag_id, TI.run_id == dr.run_id)
        .order_by(TI.map_index)
    ).all()

    assert indices == [
        (0, TaskInstanceState.REMOVED),
        (1, TaskInstanceState.REMOVED),
        (2, TaskInstanceState.REMOVED),
        (3, TaskInstanceState.REMOVED),
    ]


def test_mapped_literal_length_increase_adds_additional_ti(dag_maker, session):
    """Test that when the length of mapped literal increases, additional ti is added"""

    @task
    def task_2(arg2): ...

    with dag_maker(session=session, serialized=True):
        task_2.expand(arg2=[1, 2, 3, 4])

    dr = dag_maker.create_dagrun()

    query = (
        select(TI.map_index, TI.state)
        .where(TI.task_id == "task_2", TI.dag_id == dr.dag_id, TI.run_id == dr.run_id)
        .order_by(TI.map_index)
    )
    indices = session.execute(query).all()
    assert sorted(indices) == [
        (0, State.NONE),
        (1, State.NONE),
        (2, State.NONE),
        (3, State.NONE),
    ]

    # Now "increase" the length of literal
    with dag_maker(session=session, serialized=True):
        task_2.expand(arg2=[1, 2, 3, 4, 5])

    dr.dag = dag_maker.serialized_dag
    # Every mapped task is revised at task_instance_scheduling_decision
    dr.task_instance_scheduling_decisions()

    indices = session.execute(query).all()
    assert sorted(indices) == [
        (0, State.NONE),
        (1, State.NONE),
        (2, State.NONE),
        (3, State.NONE),
        (4, State.NONE),
    ]


def test_mapped_literal_length_reduction_adds_removed_state(dag_maker, session):
    """Test that when the length of mapped literal reduces, removed state is added"""

    @task
    def task_2(arg2): ...

    with dag_maker(session=session):
        task_2.expand(arg2=[1, 2, 3, 4])

    dr = dag_maker.create_dagrun()
    query = (
        select(TI.map_index, TI.state)
        .where(TI.task_id == "task_2", TI.dag_id == dr.dag_id, TI.run_id == dr.run_id)
        .order_by(TI.map_index)
    )
    indices = session.execute(query).all()
    assert sorted(indices) == [
        (0, State.NONE),
        (1, State.NONE),
        (2, State.NONE),
        (3, State.NONE),
    ]

    with dag_maker(session=session):
        task_2.expand(arg2=[1, 2])

    dr.dag = dag_maker.serialized_dag
    # Since we change the literal on the dag file itself, the dag_hash will
    # change which will have the scheduler verify the dr integrity
    dag_version_id = DagVersion.get_latest_version(dag_id=dr.dag_id, session=session).id
    dr.verify_integrity(dag_version_id=dag_version_id, session=session)

    indices = session.execute(query).all()
    assert sorted(indices) == [
        (0, State.NONE),
        (1, State.NONE),
        (2, State.REMOVED),
        (3, State.REMOVED),
    ]


def test_mapped_length_increase_at_runtime_adds_additional_tis(dag_maker, session):
    """Test that when the length of mapped literal increases at runtime, additional ti is added"""
    # Variable.set(key="arg1", value=[1, 2, 3])

    @task
    def task_1():
        # Behave as if we did this
        # return Variable.get("arg1", deserialize_json=True)
        ...

    with dag_maker(session=session) as dag:

        @task
        def task_2(arg2): ...

        task_2.expand(arg2=task_1())

    dr: DagRun = dag_maker.create_dagrun()
    ti = dr.get_task_instance(task_id="task_1", session=session)
    assert ti
    ti.state = TaskInstanceState.SUCCESS
    # Behave as if TI ran after: Variable.set(key="arg1", value=[1, 2, 3])
    session.add(TaskMap.from_task_instance_xcom(ti, [1, 2, 3]))
    session.flush()

    decision = dr.task_instance_scheduling_decisions(session=session)
    indices = [(ti.task_id, ti.map_index) for ti in decision.schedulable_tis]
    assert indices == [("task_2", 0), ("task_2", 1), ("task_2", 2)]

    # Now "clear" and "increase" the length of literal
    dag.clear()

    # "Run" the first task again to get the new lengths
    ti = dr.get_task_instance(task_id="task_1", session=session)
    assert ti
    # Behave as if we did and re-ran the task: Variable.set(key="arg1", value=[1, 2, 3, 4])
    session.merge(TaskMap.from_task_instance_xcom(ti, [1, 2, 3, 4]))
    ti.state = TaskInstanceState.SUCCESS
    session.flush()

    # this would be called by the localtask job
    decision = dr.task_instance_scheduling_decisions(session=session)
    indices = [(ti.task_id, ti.state, ti.map_index) for ti in decision.schedulable_tis]
    assert sorted(indices) == [
        ("task_2", None, 0),
        ("task_2", None, 1),
        ("task_2", None, 2),
        ("task_2", None, 3),
    ]


def test_mapped_literal_length_reduction_at_runtime_adds_removed_state(dag_maker, session):
    """
    Test that when the length of mapped literal reduces at runtime, the missing task instances
    are marked as removed
    """

    @task
    def task_1():
        # return Variable.get("arg1", deserialize_json=True)
        ...

    with dag_maker(session=session) as dag:

        @task
        def task_2(arg2): ...

        task_2.expand(arg2=task_1())

    dr: DagRun = dag_maker.create_dagrun()
    ti = dr.get_task_instance(task_id="task_1", session=session)
    assert ti
    ti.state = TaskInstanceState.SUCCESS
    # Behave as if TI ran after: Variable.set(key="arg1", value=[1, 2, 3])
    session.add(TaskMap.from_task_instance_xcom(ti, [1, 2, 3]))
    session.flush()

    dr.task_instance_scheduling_decisions(session=session)
    query = (
        select(TI.map_index, TI.state)
        .where(TI.task_id == "task_2", TI.dag_id == dr.dag_id, TI.run_id == dr.run_id)
        .order_by(TI.map_index)
    )
    indices = session.execute(query).all()
    assert indices == [(0, None), (1, None), (2, None)]

    # Now "clear" and "reduce" the length of literal
    dag.clear()

    # "Run" the first task again to get the new lengths
    ti = dr.get_task_instance(task_id="task_1", session=session)
    assert ti
    # Behave as if we did and re-ran the task: Variable.set(key="arg1", value=[1, 2])
    session.merge(TaskMap.from_task_instance_xcom(ti, [1, 2]))
    ti.state = TaskInstanceState.SUCCESS
    session.flush()
    dag_version_id = DagVersion.get_latest_version(dag.dag_id, session=session).id
    dr.verify_integrity(dag_version_id=dag_version_id, session=session)
    indices = session.execute(query).all()
    assert sorted(indices) == [
        (0, State.NONE),
        (1, State.NONE),
        (2, TaskInstanceState.REMOVED),
    ]


def test_mapped_literal_faulty_state_in_db(dag_maker, session):
    """
    This test tries to recreate a faulty state in the database and checks if we can recover from it.
    The state that happens is that there exists mapped task instances and the unmapped task instance.
    So we have instances with map_index [-1, 0, 1]. The -1 task instances should be removed in this case.
    """
    with dag_maker(session=session) as dag:

        @task
        def task_1():
            return [1, 2]

        @task
        def task_2(arg2): ...

        task_2.expand(arg2=task_1())

    dr = dag_maker.create_dagrun()
    ti = dr.get_task_instance(task_id="task_1")
    run_task_instance(ti, dag_maker.dag.get_task("task_1"))
    decision = dr.task_instance_scheduling_decisions()
    assert len(decision.schedulable_tis) == 2

    # We insert a faulty record
    session.add(
        create_task_instance(task=dag.get_task("task_2"), run_id=dr.run_id, dag_version_id=ti.dag_version_id)
    )
    session.flush()

    decision = dr.task_instance_scheduling_decisions()
    assert len(decision.schedulable_tis) == 2


def test_calls_to_verify_integrity_with_mapped_task_zero_length_at_runtime(dag_maker, session, caplog):
    """
    Test zero length reduction in mapped task at runtime with calls to dagrun.verify_integrity
    """
    import logging

    with dag_maker(session=session) as dag:

        @task
        def task_1():
            # return Variable.get("arg1", deserialize_json=True)
            ...

        @task
        def task_2(arg2): ...

        task_2.expand(arg2=task_1())

    dr: DagRun = dag_maker.create_dagrun()
    ti = dr.get_task_instance(task_id="task_1", session=session)
    assert ti
    # "Run" task_1
    ti.state = TaskInstanceState.SUCCESS
    # Behave as if TI ran after: Variable.set(key="arg1", value=[1, 2, 3])
    session.add(TaskMap.from_task_instance_xcom(ti, [1, 2, 3]))
    session.flush()

    decision = dr.task_instance_scheduling_decisions(session=session)
    ti_2 = decision.schedulable_tis[0]
    assert ti_2

    query = (
        select(TI.map_index, TI.state)
        .where(TI.task_id == "task_2", TI.dag_id == dr.dag_id, TI.run_id == dr.run_id)
        .order_by(TI.map_index)
    )
    indices = session.execute(query).all()
    assert sorted(indices) == [(0, State.NONE), (1, State.NONE), (2, State.NONE)]

    # Now "clear" and "reduce" the length to empty list
    dag.clear()
    # We don't execute task anymore, but this is what we are
    # simulating happened:
    # Variable.set(key="arg1", value=[])
    session.merge(TaskMap.from_task_instance_xcom(ti, []))
    session.flush()

    # Run the first task again to get the new lengths
    with caplog.at_level(logging.DEBUG):
        # Run verify_integrity as a whole and assert the tasks were removed
        dag_version = DagVersion.get_latest_version(dag.dag_id)
        dr.verify_integrity(dag_version_id=dag_version.id, session=session)
    indices = session.execute(query).all()
    assert indices == [
        (0, TaskInstanceState.REMOVED),
        (1, TaskInstanceState.REMOVED),
        (2, TaskInstanceState.REMOVED),
    ]


def test_mapped_mixed_literal_not_expanded_at_create(dag_maker, session):
    literal = [1, 2, 3, 4]
    with dag_maker(session=session):
        task = BaseOperator(task_id="task_1")
        mapped = MockOperator.partial(task_id="task_2").expand(arg1=literal, arg2=task.output)

    dr = dag_maker.create_dagrun()
    query = (
        select(TI.map_index, TI.state)
        .where(TI.task_id == mapped.task_id, TI.dag_id == mapped.dag_id, TI.run_id == dr.run_id)
        .order_by(TI.map_index)
    )

    assert session.execute(query).all() == [(-1, None)]

    # Verify_integrity shouldn't change the result now that the TIs exist
    dag_version_id = DagVersion.get_latest_version(dag_id=dr.dag_id, session=session).id
    dr.verify_integrity(dag_version_id=dag_version_id, session=session)
    assert session.execute(query).all() == [(-1, None)]


def test_mapped_task_group_expands_at_create(dag_maker, session):
    literal = [[1, 2], [3, 4]]

    with dag_maker(session=session):

        @task_group
        def tg(x):
            # Normal operator in mapped task group, expands to 2 tis.
            MockOperator(task_id="t1")
            # Mapped operator expands *again* against mapped task group arguments to 4 tis.
            with pytest.raises(NotImplementedError) as ctx:
                MockOperator.partial(task_id="t2").expand(arg1=literal)
            assert str(ctx.value) == "operator expansion in an expanded task group is not yet supported"
            # Normal operator referencing mapped task group arguments does not further expand, only 2 tis.
            MockOperator(task_id="t3", arg1=x)
            # It can expand *again* (since each item in x is a list) but this is not done at parse time.
            with pytest.raises(NotImplementedError) as ctx:
                MockOperator.partial(task_id="t4").expand(arg1=x)
            assert str(ctx.value) == "operator expansion in an expanded task group is not yet supported"

        tg.expand(x=literal)

    dr = dag_maker.create_dagrun()
    query = (
        select(TI.task_id, TI.map_index, TI.state)
        .where(TI.dag_id == dr.dag_id, TI.run_id == dr.run_id)
        .order_by(TI.task_id, TI.map_index)
    )
    assert session.execute(query).all() == [
        ("tg.t1", 0, None),
        ("tg.t1", 1, None),
        # ("tg.t2", 0, None),
        # ("tg.t2", 1, None),
        # ("tg.t2", 2, None),
        # ("tg.t2", 3, None),
        ("tg.t3", 0, None),
        ("tg.t3", 1, None),
        # ("tg.t4", -1, None),
    ]


def test_mapped_task_group_empty_operator(dag_maker, session):
    """
    Test that dynamic task inside a dynamic task group only marks
    the corresponding downstream EmptyOperator as success.
    """
    literal = [1, 2, 3]

    with dag_maker(session=session) as dag:

        @task_group
        def tg(x):
            @task
            def t1(x):
                return x

            t2 = EmptyOperator(task_id="t2")

            @task
            def t3(x):
                return x

            t1(x) >> t2 >> t3(x)

        tg.expand(x=literal)

    dr = dag_maker.create_dagrun()

    t2_task = dag.get_task("tg.t2")
    t2_0 = dr.get_task_instance(task_id="tg.t2", map_index=0)
    t2_0.refresh_from_task(t2_task)
    assert t2_0.state is None

    t2_1 = dr.get_task_instance(task_id="tg.t2", map_index=1)
    t2_1.refresh_from_task(t2_task)
    assert t2_1.state is None

    dr.schedule_tis([t2_0])

    t2_0 = dr.get_task_instance(task_id="tg.t2", map_index=0)
    assert t2_0.state == TaskInstanceState.SUCCESS

    t2_1 = dr.get_task_instance(task_id="tg.t2", map_index=1)
    assert t2_1.state is None


def test_ti_scheduling_mapped_zero_length(dag_maker, session):
    with dag_maker(session=session):
        task = BaseOperator(task_id="task_1")
        mapped = MockOperator.partial(task_id="task_2").expand(arg2=task.output)

    dr: DagRun = dag_maker.create_dagrun()
    ti1, ti2 = sorted(dr.task_instances, key=lambda ti: ti.task_id)
    ti1.state = TaskInstanceState.SUCCESS
    session.add(
        TaskMap(dag_id=dr.dag_id, task_id=ti1.task_id, run_id=dr.run_id, map_index=-1, length=0, keys=None)
    )
    session.flush()

    decision = dr.task_instance_scheduling_decisions(session=session)

    # ti1 finished execution. ti2 goes directly to finished state because it's
    # expanded against a zero-length XCom.
    assert decision.finished_tis == [ti1, ti2]

    indices = session.execute(
        select(TI.map_index, TI.state)
        .where(TI.task_id == mapped.task_id, TI.dag_id == mapped.dag_id, TI.run_id == dr.run_id)
        .order_by(TI.map_index)
    ).all()

    assert indices == [(-1, TaskInstanceState.SKIPPED)]


@pytest.mark.parametrize("trigger_rule", [TriggerRule.ALL_DONE, TriggerRule.ALL_SUCCESS])
def test_mapped_task_upstream_failed(dag_maker, session, trigger_rule):
    from airflow.providers.standard.operators.python import PythonOperator

    with dag_maker(session=session) as dag:

        @dag.task
        def make_list():
            return [f'echo "{a!r}"' for a in [1, 2, {"a": "b"}]]

        def consumer(*args):
            print(repr(args))

        PythonOperator.partial(
            task_id="consumer",
            trigger_rule=trigger_rule,
            python_callable=consumer,
        ).expand(op_args=make_list())

    dr = dag_maker.create_dagrun()
    _, make_list_ti = sorted(dr.task_instances, key=lambda ti: ti.task_id)
    make_list_ti.state = TaskInstanceState.FAILED
    session.flush()

    tis, _ = dr.update_state(execute_callbacks=False, session=session)
    assert tis == []
    tis = sorted(dr.task_instances, key=lambda ti: ti.task_id)

    assert sorted((ti.task_id, ti.map_index, ti.state) for ti in tis) == [
        ("consumer", -1, TaskInstanceState.UPSTREAM_FAILED),
        ("make_list", -1, TaskInstanceState.FAILED),
    ]
    # Bug/possible source of optimization: The DR isn't marked as failed until
    # in the loop that marks the last task as UPSTREAM_FAILED
    tis, _ = dr.update_state(execute_callbacks=False, session=session)
    assert tis == []
    assert dr.state == DagRunState.FAILED


def test_mapped_task_all_finish_before_downstream(dag_maker, session):
    with dag_maker(session=session) as dag:

        @dag.task
        def make_list():
            return [1, 2]

        @dag.task
        def double(value):
            return value * 2

        @dag.task
        def consumer(value):
            ...
            # result = list(value)

        consumer(value=double.expand(value=make_list()))

    dr: DagRun = dag_maker.create_dagrun()

    def _task_ids(tis):
        return [ti.task_id for ti in tis]

    # The first task is always make_list.
    decision = dr.task_instance_scheduling_decisions(session=session)
    assert _task_ids(decision.schedulable_tis) == ["make_list"]

    # After make_list is run, double is expanded.
    ti = decision.schedulable_tis[0]
    ti.state = TaskInstanceState.SUCCESS
    session.add(TaskMap.from_task_instance_xcom(ti, [1, 2]))
    session.flush()

    decision = dr.task_instance_scheduling_decisions(session=session)
    assert _task_ids(decision.schedulable_tis) == ["double", "double"]

    # Running just one of the mapped tis does not make downstream schedulable.
    ti = decision.schedulable_tis[0]
    ti.state = TaskInstanceState.SUCCESS
    session.flush()

    decision = dr.task_instance_scheduling_decisions(session=session)
    assert _task_ids(decision.schedulable_tis) == ["double"]

    # Downstream is scheduleable after all mapped tis are run.
    ti = decision.schedulable_tis[0]
    ti.state = TaskInstanceState.SUCCESS
    session.flush()
    decision = dr.task_instance_scheduling_decisions(session=session)
    assert _task_ids(decision.schedulable_tis) == ["consumer"]


def test_schedule_tis_map_index(dag_maker, session):
    with dag_maker(session=session, dag_id="test"):
        task = BaseOperator(task_id="task_1")

    dr = DagRun(dag_id="test", run_id="test", run_type=DagRunType.MANUAL)
    dag_version = DagVersion.get_latest_version(dag_id=dr.dag_id)
    ti0 = create_task_instance(
        task=task,
        run_id=dr.run_id,
        map_index=0,
        state=TaskInstanceState.SUCCESS,
        dag_version_id=dag_version.id,
    )
    ti1 = create_task_instance(
        task=task,
        run_id=dr.run_id,
        map_index=1,
        state=None,
        dag_version_id=dag_version.id,
    )
    ti2 = create_task_instance(
        task=task,
        run_id=dr.run_id,
        map_index=2,
        state=TaskInstanceState.SUCCESS,
        dag_version_id=dag_version.id,
    )
    session.add_all((dr, ti0, ti1, ti2))
    session.flush()

    assert dr.schedule_tis((ti1,), session=session) == 1

    session.refresh(ti0)
    session.refresh(ti1)
    session.refresh(ti2)
    assert ti0.state == TaskInstanceState.SUCCESS
    assert ti1.state == TaskInstanceState.SCHEDULED
    assert ti2.state == TaskInstanceState.SUCCESS


def test_schedule_tis_does_not_increment_try_number_if_ti_already_queued_by_other_scheduler(
    dag_maker, session
):
    with dag_maker(session=session) as dag:
        BashOperator(task_id="task", bash_command="echo 1")

    dr = dag_maker.create_dagrun(session=session)
    ti = dr.get_task_instance("task", session=session)
    assert ti is not None
    ti.refresh_from_task(dag.get_task("task"))
    assert ti.state is None

    # The stale scheduler picks try 1.
    ti.try_number = 1
    session.flush()
    session.commit()

    # Another scheduler already queued the TI in DB (same try).
    with create_session() as other_session:
        filter_for_tis = TI.filter_for_tis([ti])
        assert filter_for_tis is not None
        other_session.execute(
            update(TI)
            .where(filter_for_tis)
            .values(
                state=TaskInstanceState.QUEUED,
                try_number=1,
            )
            .execution_options(synchronize_session=False)
        )

    # This stale scheduler still has a stale TI object; schedule_tis must be a no-op.
    assert dr.schedule_tis((ti,), session=session) == 0

    refreshed_ti = session.scalar(
        select(TI).where(
            TI.dag_id == ti.dag_id,
            TI.task_id == ti.task_id,
            TI.run_id == ti.run_id,
            TI.map_index == ti.map_index,
        )
    )
    assert refreshed_ti.state == TaskInstanceState.QUEUED
    assert refreshed_ti.try_number == 1


def test_schedule_tis_empty_operator_does_not_short_circuit_if_ti_already_queued(dag_maker, session):
    with dag_maker(session=session) as dag:
        EmptyOperator(task_id="empty_task")

    dr = dag_maker.create_dagrun(session=session)
    ti = dr.get_task_instance("empty_task", session=session)
    ti.refresh_from_task(dag.get_task("empty_task"))
    assert ti.state is None

    # Stale scheduler picks TI
    ti.try_number = 1
    session.flush()
    session.commit()

    # Another scheduler already queued it.
    with create_session() as other_session:
        filter_for_tis = TI.filter_for_tis([ti])
        assert filter_for_tis is not None
        other_session.execute(
            update(TI)
            .where(filter_for_tis)
            .values(
                state=TaskInstanceState.QUEUED,
                try_number=1,
            )
            .execution_options(synchronize_session=False)
        )

    # no shortcircuit
    assert dr.schedule_tis((ti,), session=session) == 0

    refreshed_ti = session.scalar(
        select(TI).where(
            TI.dag_id == ti.dag_id,
            TI.task_id == ti.task_id,
            TI.run_id == ti.run_id,
            TI.map_index == ti.map_index,
        )
    )
    assert refreshed_ti is not None
    assert refreshed_ti.state == TaskInstanceState.QUEUED
    assert refreshed_ti.try_number == 1


def test_schedule_tis_up_for_reschedule_does_not_increment_try_number(dag_maker, session):
    with dag_maker(session=session) as dag:
        BashOperator(task_id="task", bash_command="echo 1")

    dr = dag_maker.create_dagrun(session=session)
    ti = dr.get_task_instance("task", session=session)
    ti.refresh_from_task(dag.get_task("task"))

    ti.state = TaskInstanceState.UP_FOR_RESCHEDULE
    ti.try_number = 3
    session.commit()

    assert dr.schedule_tis((ti,), session=session) == 1
    session.commit()

    # schedule_tis uses synchronize_session=False, so the session may still hold a stale instance.
    # Expire the identity map so the SELECT reflects the DB row.
    session.expire_all()
    refreshed_ti = session.scalar(
        select(TI).where(
            TI.dag_id == ti.dag_id,
            TI.task_id == ti.task_id,
            TI.run_id == ti.run_id,
            TI.map_index == ti.map_index,
        )
    )
    assert refreshed_ti.state == TaskInstanceState.SCHEDULED
    assert refreshed_ti.try_number == 3


def test_schedule_tis_empty_operator_is_noop_if_ti_already_running(dag_maker, session):
    with dag_maker(session=session) as dag:
        EmptyOperator(task_id="empty_task")

    dr = dag_maker.create_dagrun(session=session)
    ti = dr.get_task_instance("empty_task", session=session)
    ti.refresh_from_task(dag.get_task("empty_task"))

    ti.try_number = 3
    session.commit()

    with create_session() as other_session:
        filter_for_tis = TI.filter_for_tis([ti])
        assert filter_for_tis is not None
        other_session.execute(
            update(TI)
            .where(filter_for_tis)
            .values(
                state=TaskInstanceState.RUNNING,
                try_number=3,
            )
            .execution_options(synchronize_session=False)
        )

    assert dr.schedule_tis((ti,), session=session) == 0

    refreshed_ti = session.scalar(
        select(TI).where(
            TI.dag_id == ti.dag_id,
            TI.task_id == ti.task_id,
            TI.run_id == ti.run_id,
            TI.map_index == ti.map_index,
        )
    )
    assert refreshed_ti.state == TaskInstanceState.RUNNING
    assert refreshed_ti.try_number == 3


def test_schedule_tis_only_one_scheduler_update_succeeds_when_competing(dag_maker, session):
    with dag_maker(session=session) as dag:
        BashOperator(task_id="task", bash_command="echo 1")

    dr = dag_maker.create_dagrun(session=session)
    ti = dr.get_task_instance("task", session=session)
    ti.refresh_from_task(dag.get_task("task"))
    assert ti.state is None

    ti.try_number = 0
    session.commit()

    # Scheduler B loads TI *before* Scheduler A commits — both see state=None.
    with create_session() as scheduler_b_session:
        ti_b = scheduler_b_session.scalar(
            select(TI).where(
                TI.dag_id == ti.dag_id,
                TI.task_id == ti.task_id,
                TI.run_id == ti.run_id,
                TI.map_index == ti.map_index,
            )
        )
        assert ti_b is not None
        assert ti_b.state is None

        # Scheduler A schedules first.
        assert dr.schedule_tis((ti,), session=session) == 1
        session.commit()

        # Scheduler B tries with its stale TI object; should be a no-op.
        assert dr.schedule_tis((ti_b,), session=scheduler_b_session) == 0

    session.expire_all()
    refreshed_ti = session.scalar(
        select(TI).where(
            TI.dag_id == ti.dag_id,
            TI.task_id == ti.task_id,
            TI.run_id == ti.run_id,
            TI.map_index == ti.map_index,
        )
    )
    assert refreshed_ti.state == TaskInstanceState.SCHEDULED
    assert refreshed_ti.try_number == 1


@pytest.mark.need_serialized_dag
def test_schedule_tis_start_trigger(dag_maker, session):
    """
    Test that an operator with start_trigger_args set can be directly deferred during scheduling.
    """

    class TestOperator(BaseOperator):
        start_trigger_args = StartTriggerArgs(
            trigger_cls="airflow.triggers.testing.SuccessTrigger",
            trigger_kwargs=None,
            next_method="execute_complete",
            timeout=None,
        )
        start_from_trigger = True

        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.start_trigger_args.trigger_kwargs = {}

        def execute_complete(self):
            pass

    with dag_maker(session=session):
        TestOperator(task_id="test_task")

    dr: DagRun = dag_maker.create_dagrun()
    ti = dr.get_task_instance("test_task")
    assert ti.state is None

    ti.task = dr.dag.get_task("test_task")
    dr.schedule_tis((ti,), session=session)
    assert ti.state == TaskInstanceState.DEFERRED


@pytest.mark.need_serialized_dag
def test_schedule_tis_start_trigger_next_kwargs_round_trip(dag_maker, session):
    """next_kwargs with encoded values (timedelta) must survive the defer_task round-trip."""
    import datetime

    from airflow.sdk.serde import deserialize

    class TestOperator(BaseOperator):
        start_trigger_args = StartTriggerArgs(
            trigger_cls="airflow.triggers.testing.SuccessTrigger",
            trigger_kwargs={},
            next_method="execute_complete",
            next_kwargs={"delay": datetime.timedelta(seconds=30)},
            timeout=None,
        )
        start_from_trigger = True

        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)

        def execute_complete(self):
            pass

    with dag_maker(session=session):
        TestOperator(task_id="test_task")

    dr: DagRun = dag_maker.create_dagrun()
    ti = dr.get_task_instance("test_task")
    ti.task = dr.dag.get_task("test_task")
    dr.schedule_tis((ti,), session=session)

    assert ti.state == TaskInstanceState.DEFERRED
    assert deserialize(ti.next_kwargs) == {"delay": datetime.timedelta(seconds=30)}


@pytest.mark.need_serialized_dag
def test_schedule_tis_start_trigger_kwargs_e2e(dag_maker, session):
    """
    End to end test of scheduler defer_task with non-trivial trigger_kwargs (timedelta) ->
    Trigger row -> Trigger.kwargs returns correct Python objects.

    Covers the path: BaseSerialization encodes trigger_kwargs with Encoding enum keys,
    defer_task passes them to Trigger as kwargs which calls encrypt_kwargs -> _stringify_encoding_keys
    -> serde.serialize -> stores them.

    On reading, serde.deserialize + _convert must reconstruct the original values.
    """
    import datetime

    class TestOperator(BaseOperator):
        start_trigger_args = StartTriggerArgs(
            trigger_cls="airflow.triggers.testing.SuccessTrigger",
            trigger_kwargs={"delta": datetime.timedelta(seconds=2)},
            next_method="execute_complete",
            next_kwargs=None,
            timeout=None,
        )
        start_from_trigger = True

        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)

        def execute_complete(self):
            pass

    with dag_maker(session=session):
        TestOperator(task_id="test_task")

    dr: DagRun = dag_maker.create_dagrun()
    ti = dr.get_task_instance("test_task")
    ti.task = dr.dag.get_task("test_task")
    dr.schedule_tis((ti,), session=session)

    assert ti.state == TaskInstanceState.DEFERRED

    trigger_row = session.get(Trigger, ti.trigger_id)
    assert trigger_row is not None
    # trigger_kwargs must round-trip correctly through encrypt_kwargs → _decrypt_kwargs
    assert trigger_row.kwargs == {"delta": datetime.timedelta(seconds=2)}


def test_schedule_tis_empty_operator_try_number(dag_maker, session: Session):
    """
    When empty operator is not actually run, then we need to increment the try_number,
    since ordinarily it's incremented when scheduled, but empty operator is generally not scheduled.
    """
    with dag_maker(session=session):
        BashOperator(task_id="real_task", bash_command="echo 1")
        EmptyOperator(task_id="empty_task")

    dr: DagRun = dag_maker.create_dagrun(session=session)
    session.commit()
    tis = dr.task_instances
    dr.schedule_tis(tis, session=session)
    session.commit()
    session.expunge_all()
    tis = dr.get_task_instances(session=session)
    real_ti = next(x for x in tis if x.task_id == "real_task")
    empty_ti = next(x for x in tis if x.task_id == "empty_task")
    assert real_ti.try_number == 1
    assert empty_ti.try_number == 1


def test_schedule_tis_try_number_mismatch_logs_warning(dag_maker, session: Session, monkeypatch):
    with dag_maker(session=session):
        BaseOperator(task_id="task_1")

    dr: DagRun = dag_maker.create_dagrun(session=session)
    ti = dr.get_task_instance("task_1", session=session)
    assert ti is not None

    original_execute = session.execute

    class _FakeSelectResult:
        def all(self):
            return [(ti.id, ti.try_number + 2, TaskInstanceState.SCHEDULED)]

    def execute_with_mismatch(statement, *args, **kwargs):
        if getattr(statement, "is_select", False):
            return _FakeSelectResult()
        return original_execute(statement, *args, **kwargs)

    monkeypatch.setattr(session, "execute", execute_with_mismatch)

    with (
        mock.patch.object(dr.log, "isEnabledFor", return_value=True),
        mock.patch.object(dr.log, "warning") as warning_mock,
    ):
        dr.schedule_tis((ti,), session=session)

    assert any(
        "schedule_tis: try_number mismatch after scheduling" in call.args[0]
        for call in warning_mock.call_args_list
    )


def test_schedule_tis_try_number_match_has_no_warning(dag_maker, session: Session):
    with dag_maker(session=session):
        BaseOperator(task_id="task_1")

    dr: DagRun = dag_maker.create_dagrun(session=session)
    ti = dr.get_task_instance("task_1", session=session)
    assert ti is not None

    with (
        mock.patch.object(dr.log, "isEnabledFor", return_value=True),
        mock.patch.object(dr.log, "warning") as warning_mock,
    ):
        dr.schedule_tis((ti,), session=session)

    assert all(
        "schedule_tis: try_number mismatch after scheduling" not in call.args[0]
        for call in warning_mock.call_args_list
    )


def test_schedule_tis_try_number_check_is_debug_only(dag_maker, session: Session, monkeypatch):
    with dag_maker(session=session):
        BaseOperator(task_id="task_1")

    dr: DagRun = dag_maker.create_dagrun(session=session)
    ti = dr.get_task_instance("task_1", session=session)
    assert ti is not None

    original_execute = session.execute
    select_calls = 0

    def execute_with_counter(statement, *args, **kwargs):
        nonlocal select_calls
        if getattr(statement, "is_select", False):
            select_calls += 1
        return original_execute(statement, *args, **kwargs)

    monkeypatch.setattr(session, "execute", execute_with_counter)

    with mock.patch.object(dr.log, "isEnabledFor", return_value=False):
        dr.schedule_tis((ti,), session=session)

    assert select_calls == 0


@pytest.mark.xfail(reason="We can't keep this behaviour with remote workers where scheduler can't reach xcom")
def test_schedule_tis_start_trigger_through_expand(dag_maker, session):
    """
    Test that an operator with start_trigger_args set can be directly deferred during scheduling.
    """

    class TestOperator(BaseOperator):
        start_trigger_args = StartTriggerArgs(
            trigger_cls="airflow.triggers.testing.SuccessTrigger",
            trigger_kwargs={},
            next_method="execute_complete",
            timeout=None,
        )
        start_from_trigger = False

        def __init__(self, *args, start_from_trigger: bool = False, **kwargs):
            super().__init__(*args, **kwargs)
            self.start_from_trigger = start_from_trigger

        def execute_complete(self):
            pass

    with dag_maker(session=session):
        TestOperator.partial(task_id="test_task").expand(start_from_trigger=[True, False])

    dr: DagRun = dag_maker.create_dagrun()

    dr.schedule_tis(dr.task_instances, session=session)
    tis = [(ti.state, ti.map_index) for ti in dr.task_instances]
    assert tis[0] == (TaskInstanceState.DEFERRED, 0)
    assert tis[1] == (None, 1)


def test_mapped_expand_kwargs(dag_maker):
    with dag_maker():

        @task
        def task_0():
            return {"arg1": "a", "arg2": "b"}

        @task
        def task_1(args_0):
            return [args_0, {"arg1": "y"}, {"arg2": "z"}]

        args_0 = task_0()
        args_list = task_1(args_0=args_0)

        MockOperator.partial(task_id="task_2").expand_kwargs(args_list)
        MockOperator.partial(task_id="task_3").expand_kwargs(
            [{"arg1": "a", "arg2": "b"}, {"arg1": "y"}, {"arg2": "z"}],
        )
        MockOperator.partial(task_id="task_4").expand_kwargs([args_0, {"arg1": "y"}, {"arg2": "z"}])

    dr: DagRun = dag_maker.create_dagrun()
    tis = {(ti.task_id, ti.map_index): ti for ti in dr.task_instances}

    # task_2 is not expanded yet since it relies on one single XCom input.
    # task_3 and task_4 received a pure literal and can expanded right away.
    # task_4 relies on an XCom input in the list, but can also be expanded.
    assert sorted(map_index for (task_id, map_index) in tis if task_id == "task_2") == [-1]
    assert sorted(map_index for (task_id, map_index) in tis if task_id == "task_3") == [0, 1, 2]
    assert sorted(map_index for (task_id, map_index) in tis if task_id == "task_4") == [0, 1, 2]

    run_task_instance(tis[("task_0", -1)], args_0.operator)
    run_task_instance(tis[("task_1", -1)], args_list.operator)

    # With the upstreams available, everything should get expanded now.
    decision = dr.task_instance_scheduling_decisions()
    assert {(ti.task_id, ti.map_index): ti.state for ti in decision.schedulable_tis} == {
        ("task_2", 0): None,
        ("task_2", 1): None,
        ("task_2", 2): None,
        ("task_3", 0): None,
        ("task_3", 1): None,
        ("task_3", 2): None,
        ("task_4", 0): None,
        ("task_4", 1): None,
        ("task_4", 2): None,
    }


def test_mapped_skip_upstream_not_deadlock(dag_maker):
    with dag_maker() as dag:

        @dag.task
        def add_one(x: int):
            return x + 1

        @dag.task
        def say_hi():
            print("Hi")

        added_values = add_one.expand(x=[])
        added_more_values = add_one.expand(x=[])
        say_hi() >> added_values
        added_values >> added_more_values

    dr = dag_maker.create_dagrun()

    session = dag_maker.session
    tis = {ti.task_id: ti for ti in dr.task_instances}

    tis["say_hi"].state = TaskInstanceState.SUCCESS
    session.flush()

    dr.update_state(session=session)  # expands the mapped tasks
    dr.update_state(session=session)  # marks the task as skipped
    dr.update_state(session=session)  # marks dagrun as success
    assert dr.state == DagRunState.SUCCESS
    assert tis["add_one__1"].state == TaskInstanceState.SKIPPED


def test_schedulable_task_exist_when_rerun_removed_upstream_mapped_task(session, dag_maker):
    from airflow.sdk import task

    @task
    def do_something(i):
        return 1

    @task
    def do_something_else(i):
        return 1

    with dag_maker():
        nums = do_something.expand(i=[i + 1 for i in range(5)])
        do_something_else.expand(i=nums)

    dr = dag_maker.create_dagrun()

    tis = dr.get_task_instances()
    for ti in tis:
        if ti.task_id == "do_something_else":
            ti.map_index = 0
            task = ti.task
            for map_index in range(1, 5):
                ti_new = create_task_instance(
                    task,
                    run_id=dr.run_id,
                    map_index=map_index,
                    dag_version_id=ti.dag_version_id,
                )
                session.add(ti_new)
                ti_new.dag_run = dr
        else:
            # run tasks "do_something" to get XCOMs for correct downstream length
            run_task_instance(ti, dag_maker.dag.get_task(ti.task_id))
    session.flush()

    tis = dr.get_task_instances()
    for ti in tis:
        if ti.task_id == "do_something":
            if ti.map_index > 2:
                ti.state = TaskInstanceState.REMOVED
            else:
                ti.state = TaskInstanceState.SUCCESS
            session.merge(ti)
    session.commit()
    # The Upstream is done with 2 removed tis and 3 success tis
    (tis, _) = dr.update_state()
    assert len(tis) == 3
    assert dr.state != DagRunState.FAILED


@pytest.mark.parametrize(
    ("partial_params", "mapped_params", "expected"),
    [
        pytest.param(None, [{"a": 1}], 1, id="simple"),
        pytest.param({"b": 2}, [{"a": 1}], 1, id="merge"),
        pytest.param({"b": 2}, [{"a": 1, "b": 3}], 1, id="override"),
    ],
)
def test_mapped_expand_against_params(dag_maker, partial_params, mapped_params, expected):
    with dag_maker():
        BaseOperator.partial(task_id="t", params=partial_params).expand(params=mapped_params)

    dr: DagRun = dag_maker.create_dagrun()
    decision = dr.task_instance_scheduling_decisions()
    assert len(decision.schedulable_tis) == expected


def test_mapped_task_group_expands(dag_maker, session):
    with dag_maker(session=session):

        @task_group
        def tg(x, y):
            return MockOperator(task_id="task_2", arg1=x, arg2=y)

        task_1 = BaseOperator(task_id="task_1")
        tg.expand(x=task_1.output, y=[1, 2, 3])

    dr: DagRun = dag_maker.create_dagrun()

    # Not expanding task_2 yet since it depends on result from task_1.
    decision = dr.task_instance_scheduling_decisions(session=session)
    assert {(ti.task_id, ti.map_index, ti.state) for ti in decision.tis} == {
        ("task_1", -1, None),
        ("tg.task_2", -1, None),
    }

    # Simulate task_1 execution to produce TaskMap.
    (ti_1,) = decision.schedulable_tis
    assert ti_1.task_id == "task_1"
    ti_1.state = TaskInstanceState.SUCCESS
    session.add(TaskMap.from_task_instance_xcom(ti_1, ["a", "b"]))
    session.flush()

    # Now task_2 in mapped tagk group is expanded.
    decision = dr.task_instance_scheduling_decisions(session=session)
    assert {(ti.task_id, ti.map_index, ti.state) for ti in decision.schedulable_tis} == {
        ("tg.task_2", 0, None),
        ("tg.task_2", 1, None),
        ("tg.task_2", 2, None),
        ("tg.task_2", 3, None),
        ("tg.task_2", 4, None),
        ("tg.task_2", 5, None),
    }


@pytest.mark.parametrize("rerun_length", [0, 1, 2, 3])
def test_mapped_task_rerun_with_different_length_of_args(session, dag_maker, rerun_length):
    @task
    def generate_mapping_args():
        context = get_current_context()
        if context["ti"].try_number == 0:
            args = [i for i in range(2)]
        else:
            args = [i for i in range(rerun_length)]
        return args

    @task
    def mapped_print_value(arg):
        return arg

    with dag_maker(session=session):
        args = generate_mapping_args()
        mapped_print_value.expand(arg=args)

    # First Run
    dr = dag_maker.create_dagrun()
    dag_maker.run_ti("generate_mapping_args", dr)

    decision = dr.task_instance_scheduling_decisions(session=session)
    for ti in decision.schedulable_tis:
        dag_maker.run_ti(ti.task_id, dr, map_index=ti.map_index)

    clear_task_instances(dr.get_task_instances(), session=session)

    # Second Run
    ti = dr.get_task_instance(task_id="generate_mapping_args", session=session)
    ti.try_number += 1
    session.merge(ti)
    dag_maker.run_ti("generate_mapping_args", dr)

    # Check if the new mapped task instances are correctly scheduled
    decision = dr.task_instance_scheduling_decisions(session=session)
    assert len(decision.schedulable_tis) == rerun_length
    assert all([ti.task_id == "mapped_print_value" for ti in decision.schedulable_tis])

    # Check if mapped task rerun successfully
    for ti in decision.schedulable_tis:
        dag_maker.run_ti(ti.task_id, dr, map_index=ti.map_index)
    query = select(TI).where(
        TI.dag_id == dr.dag_id,
        TI.run_id == dr.run_id,
        TI.task_id == "mapped_print_value",
        TI.state == TaskInstanceState.SUCCESS,
    )
    success_tis = session.execute(query).all()
    assert len(success_tis) == rerun_length


def test_operator_mapped_task_group_receives_value(dag_maker, session):
    with dag_maker(session=session):

        @task
        def t(value): ...

        @task_group
        def tg(va):
            # Each expanded group has one t1 and t2 each.
            t1 = t.override(task_id="t1")(va)
            t2 = t.override(task_id="t2")(t1)

            with pytest.raises(NotImplementedError) as ctx:
                t.override(task_id="t4").expand(value=va)
            assert str(ctx.value) == "operator expansion in an expanded task group is not yet supported"

            return t2

        # The group is mapped by 3.
        t2 = tg.expand(va=[["a", "b"], [4], ["z"]])

        # Aggregates results from task group.
        t.override(task_id="t3")(t2)

    dr: DagRun = dag_maker.create_dagrun()

    results = set()
    decision = dr.task_instance_scheduling_decisions(session=session)
    for ti in decision.schedulable_tis:
        results.add((ti.task_id, ti.map_index))
        ti.state = TaskInstanceState.SUCCESS
    session.flush()
    assert results == {("tg.t1", 0), ("tg.t1", 1), ("tg.t1", 2)}

    results.clear()
    decision = dr.task_instance_scheduling_decisions(session=session)
    for ti in decision.schedulable_tis:
        results.add((ti.task_id, ti.map_index))
        ti.state = TaskInstanceState.SUCCESS
    session.flush()
    assert results == {("tg.t2", 0), ("tg.t2", 1), ("tg.t2", 2)}

    results.clear()
    decision = dr.task_instance_scheduling_decisions(session=session)
    for ti in decision.schedulable_tis:
        results.add((ti.task_id, ti.map_index))
        ti.state = TaskInstanceState.SUCCESS
    session.flush()
    assert results == {("t3", -1)}


def test_mapping_against_empty_list(dag_maker, session):
    with dag_maker(session=session):

        @task
        def add_one(x: int):
            return x + 1

        @task
        def say_hi():
            print("Hi")

        @task
        def say_bye():
            print("Bye")

        added_values = add_one.expand(x=[])
        added_more_values = add_one.expand(x=[])
        added_more_more_values = add_one.expand(x=[])
        say_hi() >> say_bye() >> added_values
        added_values >> added_more_values >> added_more_more_values

    dr: DagRun = dag_maker.create_dagrun()

    tis = {ti.task_id: ti for ti in dr.get_task_instances(session=session)}
    say_hi_ti = tis["say_hi"]
    say_bye_ti = tis["say_bye"]
    say_hi_ti.state = TaskInstanceState.SUCCESS
    say_bye_ti.state = TaskInstanceState.SUCCESS
    session.merge(say_hi_ti)
    session.merge(say_bye_ti)
    session.flush()

    dr.update_state(session=session)
    dr.update_state(session=session)  # marks first empty mapped task as skipped
    dr.update_state(session=session)  # marks second empty mapped task as skipped
    dr.update_state(session=session)  # marks the third empty mapped task as skipped and dagrun as success
    tis = {ti.task_id: ti.state for ti in dr.get_task_instances(session=session)}
    assert tis["say_hi"] == TaskInstanceState.SUCCESS
    assert tis["say_bye"] == TaskInstanceState.SUCCESS
    assert tis["add_one"] == TaskInstanceState.SKIPPED
    assert tis["add_one__1"] == TaskInstanceState.SKIPPED
    assert tis["add_one__2"] == TaskInstanceState.SKIPPED
    assert dr.state == State.SUCCESS


def test_mapped_task_depends_on_past(dag_maker, session):
    with dag_maker(session=session):

        @task(depends_on_past=True)
        def print_value(value):
            print(value)

        print_value.expand_kwargs([{"value": i} for i in range(2)])

    dr1: DagRun = dag_maker.create_dagrun(run_type=DagRunType.SCHEDULED)
    dr2: DagRun = dag_maker.create_dagrun_after(dr1, run_type=DagRunType.SCHEDULED)

    # print_value in dr2 is not ready yet since the task depends on past.
    decision = dr2.task_instance_scheduling_decisions(session=session)
    assert len(decision.schedulable_tis) == 0

    # Run print_value in dr1.
    decision = dr1.task_instance_scheduling_decisions(session=session)
    assert len(decision.schedulable_tis) == 2
    for ti in decision.schedulable_tis:
        ti.state = TaskInstanceState.SUCCESS
    session.flush()

    # Now print_value in dr2 can run
    decision = dr2.task_instance_scheduling_decisions(session=session)
    assert len(decision.schedulable_tis) == 2
    for ti in decision.schedulable_tis:
        ti.state = TaskInstanceState.SUCCESS
    session.flush()

    # Both runs are finished now.
    decision = dr1.task_instance_scheduling_decisions(session=session)
    assert len(decision.unfinished_tis) == 0
    decision = dr2.task_instance_scheduling_decisions(session=session)
    assert len(decision.unfinished_tis) == 0


def test_xcom_map_skip_raised(dag_maker, session):
    result = None

    with dag_maker(session=session) as dag:
        # Note: this doesn't actually run this dag, the callbacks are for reference only.

        @dag.task()
        def push():
            return ["a", "b", "c"]

        @dag.task()
        def forward(value):
            return value

        @dag.task(trigger_rule=TriggerRule.ALL_DONE)
        def collect(value):
            nonlocal result
            result = list(value)

        def skip_c(v):
            ...
            # if v == "c":
            #     raise AirflowSkipException
            # return {"value": v}

        collect(value=forward.expand_kwargs(push().map(skip_c)))

    dr: DagRun = dag_maker.create_dagrun(session=session)

    def _task_ids(tis):
        return [(ti.task_id, ti.map_index) for ti in tis]

    # Check that when forward w/ map_index=2 ends up skipping, that the collect task can still be
    # scheduled!

    # Run "push".
    decision = dr.task_instance_scheduling_decisions(session=session)
    assert _task_ids(decision.schedulable_tis) == [("push", -1)]
    ti = decision.schedulable_tis[0]
    ti.state = TaskInstanceState.SUCCESS
    session.add(TaskMap.from_task_instance_xcom(ti, push.function()))
    session.flush()

    decision = dr.task_instance_scheduling_decisions(session=session)
    assert _task_ids(decision.schedulable_tis) == [
        ("forward", 0),
        ("forward", 1),
        ("forward", 2),
    ]
    # Run "forward". "c"/index 2 is skipped. Runtime behaviour checked in test_xcom_map_raise_to_skip in
    # TaskSDK
    for ti, state in zip(
        decision.schedulable_tis,
        [TaskInstanceState.SUCCESS, TaskInstanceState.SUCCESS, TaskInstanceState.SKIPPED],
    ):
        ti.state = state
    session.flush()

    # Now "collect" should only get "a" and "b".
    decision = dr.task_instance_scheduling_decisions(session=session)
    assert _task_ids(decision.schedulable_tis) == [("collect", -1)]


def test_clearing_task_and_moving_from_non_mapped_to_mapped(dag_maker, session):
    """
    Test that clearing a task and moving from non-mapped to mapped clears existing
    references in XCom, TaskInstanceNote, TaskReschedule and
    RenderedTaskInstanceFields. To be able to test this, RenderedTaskInstanceFields
    was not used in the test since it would require that the task is expanded first.
    """
    from airflow.models.xcom import XComModel

    @task
    def printx(x):
        print(x)

    with dag_maker() as dag:
        printx.expand(x=[1])

    dr1: DagRun = dag_maker.create_dagrun(run_type=DagRunType.SCHEDULED)
    ti = dr1.get_task_instances()[0]
    ti = session.scalar(
        select(TaskInstance).where(
            TaskInstance.dag_id == ti.dag_id,
            TaskInstance.task_id == ti.task_id,
            TaskInstance.run_id == ti.run_id,
            TaskInstance.map_index == ti.map_index,
        )
    )

    tr = TaskReschedule(
        ti_id=ti.id,
        start_date=timezone.datetime(2017, 1, 1),
        end_date=timezone.datetime(2017, 1, 2),
        reschedule_date=timezone.datetime(2017, 1, 1),
    )

    # mimicking a case where task moved from non-mapped to mapped
    # in that case, it would have map_index of -1 even though mapped
    ti.map_index = -1
    ti.note = "sample note"
    session.merge(ti)
    session.flush()
    # Purposely omitted RenderedTaskInstanceFields because the ti need
    # to be expanded but here we are mimicking and made it map_index -1
    session.add(tr)
    XComModel.set(key="test", value="value", task_id=ti.task_id, dag_id=dag.dag_id, run_id=ti.run_id)
    session.commit()
    for table in [TaskInstanceNote, TaskReschedule, XComModel]:
        assert session.scalar(select(func.count()).select_from(table)) == 1
    dr1.task_instance_scheduling_decisions(session)
    for table in [TaskInstanceNote, TaskReschedule, XComModel]:
        assert session.scalar(select(func.count()).select_from(table)) == 0


def test_dagrun_with_note(dag_maker, session):
    with dag_maker():

        @task
        def the_task():
            print("Hi")

        the_task()

    dr: DagRun = dag_maker.create_dagrun()
    dr.note = "dag run with note"

    session.add(dr)
    session.commit()

    dr_note = session.scalar(select(DagRunNote).where(DagRunNote.dag_run_id == dr.id))
    assert dr_note.content == "dag run with note"

    session.delete(dr)
    session.commit()

    assert session.scalar(select(DagRun).where(DagRun.id == dr.id)) is None
    assert session.scalar(select(DagRunNote).where(DagRunNote.dag_run_id == dr.id)) is None


@pytest.mark.parametrize(
    ("dag_run_state", "on_failure_fail_dagrun"), [[DagRunState.SUCCESS, False], [DagRunState.FAILED, True]]
)
def test_teardown_failure_behaviour_on_dagrun(dag_maker, session, dag_run_state, on_failure_fail_dagrun):
    with dag_maker():

        @teardown(on_failure_fail_dagrun=on_failure_fail_dagrun)
        def teardowntask():
            print(1)

        @task
        def mytask():
            print(1)

        mytask() >> teardowntask()

    dr = dag_maker.create_dagrun()
    ti1 = dr.get_task_instance(task_id="mytask")
    td1 = dr.get_task_instance(task_id="teardowntask")
    ti1.state = State.SUCCESS
    td1.state = State.FAILED
    session.merge(ti1)
    session.merge(td1)
    session.flush()
    dr.update_state()
    session.flush()
    dr = session.scalar(select(DagRun))
    assert dr.state == dag_run_state


@pytest.mark.parametrize(
    ("dag_run_state", "on_failure_fail_dagrun"), [[DagRunState.SUCCESS, False], [DagRunState.FAILED, True]]
)
def test_teardown_failure_on_non_leaf_behaviour_on_dagrun(
    dag_maker, session, dag_run_state, on_failure_fail_dagrun
):
    with dag_maker():

        @teardown(on_failure_fail_dagrun=on_failure_fail_dagrun)
        def teardowntask():
            print(1)

        @teardown
        def teardowntask2():
            print(1)

        @task
        def mytask():
            print(1)

        mytask() >> teardowntask() >> teardowntask2()

    dr = dag_maker.create_dagrun()
    ti1 = dr.get_task_instance(task_id="mytask")
    td1 = dr.get_task_instance(task_id="teardowntask")
    td2 = dr.get_task_instance(task_id="teardowntask2")
    ti1.state = State.SUCCESS
    td1.state = State.FAILED
    td2.state = State.FAILED
    session.merge(ti1)
    session.merge(td1)
    session.merge(td2)
    session.flush()
    dr.update_state()
    session.flush()
    dr = session.scalar(select(DagRun))
    assert dr.state == dag_run_state


def test_work_task_failure_when_setup_teardown_are_successful(dag_maker, session):
    with dag_maker():

        @setup
        def setuptask():
            print(2)

        @teardown
        def teardown_task():
            print(1)

        @task
        def mytask():
            print(1)

        with setuptask() >> teardown_task():
            mytask()

    dr = dag_maker.create_dagrun()
    s1 = dr.get_task_instance(task_id="setuptask")
    td1 = dr.get_task_instance(task_id="teardown_task")
    t1 = dr.get_task_instance(task_id="mytask")
    s1.state = TaskInstanceState.SUCCESS
    td1.state = TaskInstanceState.SUCCESS
    t1.state = TaskInstanceState.FAILED
    session.merge(s1)
    session.merge(td1)
    session.merge(t1)
    session.flush()
    dr.update_state()
    session.flush()
    dr = session.scalar(select(DagRun))
    assert dr.state == DagRunState.FAILED


def test_failure_of_leaf_task_not_connected_to_teardown_task(dag_maker, session):
    with dag_maker():

        @setup
        def setuptask():
            print(2)

        @teardown
        def teardown_task():
            print(1)

        @task
        def mytask():
            print(1)

        setuptask()
        teardown_task()
        mytask()

    dr = dag_maker.create_dagrun()
    s1 = dr.get_task_instance(task_id="setuptask")
    td1 = dr.get_task_instance(task_id="teardown_task")
    t1 = dr.get_task_instance(task_id="mytask")
    s1.state = TaskInstanceState.SUCCESS
    td1.state = TaskInstanceState.SUCCESS
    t1.state = TaskInstanceState.FAILED
    session.merge(s1)
    session.merge(td1)
    session.merge(t1)
    session.flush()
    dr.update_state()
    session.flush()
    dr = session.scalar(select(DagRun))
    assert dr.state == DagRunState.FAILED


@pytest.mark.parametrize(
    ("input", "expected"),
    [
        (["s1 >> w1 >> t1"], {"w1"}),  # t1 ignored
        (["s1 >> w1 >> t1", "s1 >> t1"], {"w1"}),  # t1 ignored; properly wired to setup
        (["s1 >> w1"], {"w1"}),  # no teardown
        (["s1 >> w1 >> t1_"], {"t1_"}),  # t1_ is natural leaf and OFFD=True;
        (["s1 >> w1 >> t1_", "s1 >> t1_"], {"t1_"}),  # t1_ is natural leaf and OFFD=True; wired to setup
        (["s1 >> w1 >> t1_ >> w2", "s1 >> t1_"], {"w2"}),  # t1_ is not a natural leaf so excluded anyway
        (["t1 >> t2"], {"t2"}),  # all teardowns -- default to "leaves"
        (["w1 >> t1_ >> t2"], {"t1_"}),  # teardown to teardown
    ],
)
def test_tis_considered_for_state(dag_maker, session, input, expected):
    """
    We use a convenience notation to wire up test scenarios:

    t<num> -- teardown task
    t<num>_ -- teardown task with on_failure_fail_dagrun = True
    s<num> -- setup task
    w<num> -- work task (a.k.a. normal task)

    In the test input, each line is a statement. We'll automatically create the tasks and wire them up
    as indicated in the test input.
    """

    @teardown
    def teardown_task():
        print(1)

    @task
    def work_task():
        print(1)

    @setup
    def setup_task():
        print(1)

    def make_task(task_id, dag):
        """
        Task factory helper.

        Will give a setup, teardown, work, or teardown-with-dagrun-failure task depending on input.
        """
        if task_id.startswith("s"):
            factory = setup_task
        elif task_id.startswith("w"):
            factory = work_task
        elif task_id.endswith("_"):
            factory = teardown_task.override(on_failure_fail_dagrun=True)
        else:
            factory = teardown_task
        return dag.task_dict.get(task_id) or factory.override(task_id=task_id)()

    with dag_maker() as dag:
        for line in input:
            tasks = [make_task(x, dag_maker.dag) for x in line.split(" >> ")]
            reduce(lambda x, y: x >> y, tasks)

    dr = dag_maker.create_dagrun()
    tis = dr.task_instance_scheduling_decisions(session).tis
    tis_for_state = {x.task_id for x in dr._tis_for_dagrun_state(dag=dag, tis=tis)}
    assert tis_for_state == expected


@pytest.mark.parametrize(
    ("pattern", "run_id", "result"),
    [
        ["^[A-Z]", "ABC", True],
        ["^[A-Z]", "abc", False],
        ["^[0-9]", "123", True],
        # The below params tests that user configuration does not affect internally generated
        # run_ids
        ["", "scheduled__2023-01-01T00:00:00+00:00", True],
        ["", "manual__2023-01-01T00:00:00+00:00", True],
        ["", "asset_triggered__2023-01-01T00:00:00+00:00", True],
        ["", "scheduled_2023-01-01T00", False],
        ["", "manual_2023-01-01T00", False],
        ["", "asset_triggered_2023-01-01T00", False],
        ["^[0-9]", "scheduled__2023-01-01T00:00:00+00:00", True],
        ["^[0-9]", "manual__2023-01-01T00:00:00+00:00", True],
        ["^[a-z]", "asset_triggered__2023-01-01T00:00:00+00:00", True],
    ],
)
def test_dag_run_id_config(session, dag_maker, pattern, run_id, result):
    with conf_vars({("scheduler", "allowed_run_id_pattern"): pattern}):
        with dag_maker():
            pass
        run_type = DagRunType.from_run_id(run_id)
        if result:
            dag_maker.create_dagrun(run_id=run_id, run_type=run_type)
        else:
            with pytest.raises(ValueError, match=r"The run_id provided '.+' does not match regex pattern"):
                dag_maker.create_dagrun(run_id=run_id, run_type=run_type)


@pytest.mark.db_test
@pytest.mark.need_serialized_dag(False)
@pytest.mark.parametrize(
    "run_id",
    [
        "manual__..%2F..%2Fetc%2Fpasswd",
        "my..run",
        "..",
    ],
)
def test_dag_run_id_rejects_path_traversal(session, dag_maker, run_id):
    """run_id containing '..' should be rejected to prevent path traversal."""
    with dag_maker():
        pass
    with pytest.raises(ValueError, match=r"must not contain '\.\.'"):
        dag_maker.create_dagrun(run_id=run_id, run_type=DagRunType.MANUAL)


def _get_states(dr):
    """
    For a given dag run, get a dict of states.

    Example::
        {
            "my_setup": "success",
            "my_teardown": {0: "success", 1: "success", 2: "success"},
            "my_work": "failed",
        }
    """
    ti_dict = defaultdict(dict)
    for ti in dr.get_task_instances():
        if ti.map_index == -1:
            ti_dict[ti.task_id] = ti.state
        else:
            ti_dict[ti.task_id][ti.map_index] = ti.state
    return dict(ti_dict)


@pytest.mark.db_test
@pytest.mark.need_serialized_dag(False)
def test_teardown_and_fail_fast(dag_maker):
    """
    when fail_fast enabled, teardowns should run according to their setups.
    in this case, the second teardown skips because its setup skips.
    """
    from airflow.sdk import task as task_decorator
    from airflow.sdk.definitions.taskgroup import TaskGroup

    with dag_maker(fail_fast=True) as dag:
        for num in (1, 2):
            with TaskGroup(f"tg_{num}"):

                @task_decorator
                def my_setup():
                    print("setting up multiple things")
                    return [1, 2, 3]

                @task_decorator
                def my_work(val):
                    print(f"doing work with multiple things: {val}")
                    raise ValueError("this fails")
                    return val

                @task_decorator
                def my_teardown():
                    print("teardown")

                s = my_setup()
                t = my_teardown().as_teardown(setups=s)
                with t:
                    my_work(s)

    tg1, tg2 = dag.task_group.children.values()
    tg1 >> tg2

    dr = dag.test()
    states = _get_states(dr)
    assert states == {
        "tg_1.my_setup": "success",
        "tg_1.my_teardown": "success",
        "tg_1.my_work": "failed",
        "tg_2.my_setup": "skipped",
        "tg_2.my_teardown": "skipped",
        "tg_2.my_work": "skipped",
    }


class TestDagRunHandleDagCallback:
    """Test the execute_dag_callbacks method (only uses in dag.test)."""

    def test_execute_dag_callbacks_success(self, dag_maker, session):
        """Test execute_dag_callbacks executes success callback with RuntimeTaskInstance context"""
        called = False
        context_received = None

        def on_success(context):
            nonlocal called, context_received
            called = True
            context_received = context

        with dag_maker("test_dag", session=session, on_success_callback=on_success) as dag:
            BashOperator(task_id="test_task", bash_command="echo 1")

        dr = dag_maker.create_dagrun()

        dag.on_success_callback = on_success
        dag.has_on_success_callback = True

        dr.execute_dag_callbacks(
            dag, success=True, relevant_ti=dr.get_task_instance("test_task"), reason="test_success"
        )

        assert called is True
        assert context_received is not None
        # Should have RuntimeTaskInstance context with template variables
        assert "dag_run" in context_received
        assert "logical_date" in context_received
        assert "reason" in context_received
        assert context_received["reason"] == "test_success"
        assert "ts" in context_received
        assert "params" in context_received

    def test_execute_dag_callbacks_failure(self, dag_maker, session):
        """Test execute_dag_callbacks executes failure callback with RuntimeTaskInstance context"""
        called = False
        context_received = None

        def on_failure(context):
            nonlocal called, context_received
            called = True
            context_received = context

        with dag_maker("test_dag", session=session, on_failure_callback=on_failure) as dag:
            BashOperator(task_id="test_task", bash_command="echo 1")

        dr = dag_maker.create_dagrun()

        dag.on_failure_callback = on_failure
        dag.has_on_failure_callback = True

        dr.execute_dag_callbacks(
            dag, success=False, relevant_ti=dr.get_task_instance("test_task"), reason="test_failure"
        )

        assert called is True
        assert context_received is not None
        # Should have RuntimeTaskInstance context with template variables
        assert "dag_run" in context_received
        assert "logical_date" in context_received
        assert "reason" in context_received
        assert context_received["reason"] == "test_failure"
        assert "ts" in context_received
        assert "params" in context_received

    def test_execute_dag_callbacks_multiple_callbacks(self, dag_maker, session):
        """Test execute_dag_callbacks executes multiple callbacks"""
        call_count = 0

        def on_failure_1(context):
            nonlocal call_count
            call_count += 1

        def on_failure_2(context):
            nonlocal call_count
            call_count += 1

        with dag_maker("test_dag", session=session, on_failure_callback=[on_failure_1, on_failure_2]) as dag:
            BashOperator(task_id="test_task", bash_command="echo 1")

        dr = dag_maker.create_dagrun()

        dag.on_failure_callback = [on_failure_1, on_failure_2]
        dag.has_on_failure_callback = True

        dr.execute_dag_callbacks(
            dag,
            success=False,
            relevant_ti=dr.get_task_instance("test_task"),
            reason="test_failure",
        )

        assert call_count == 2

    def test_execute_dag_callbacks_context_has_correct_ti_info(self, dag_maker, session):
        """Test execute_dag_callbacks context contains correct task instance information"""
        context_received = None

        def on_failure(context):
            nonlocal context_received
            context_received = context

        with dag_maker("test_dag", session=session, on_failure_callback=on_failure) as dag:
            BashOperator(task_id="test_task", bash_command="echo 1", retries=2)

        dr = dag_maker.create_dagrun()

        dag.on_failure_callback = on_failure
        dag.has_on_failure_callback = True

        dr.execute_dag_callbacks(
            dag,
            success=False,
            relevant_ti=dr.get_task_instance("test_task"),
            reason="test_failure",
        )

        assert context_received is not None
        # Check that context contains correct task info
        assert context_received["ti"].task_id == "test_task"
        assert context_received["ti"].dag_id == "test_dag"
        assert context_received["ti"].run_id == dr.run_id


class TestDagRunTracing:
    """Tests for DagRun OpenTelemetry span behavior."""

    @pytest.fixture(autouse=True)
    def sdk_tracer_provider(self):
        """Patch the module-level tracer with one backed by a real SDK provider so spans have valid IDs."""
        provider = TracerProvider()
        real_tracer = provider.get_tracer("airflow.models.dagrun")
        with mock.patch("airflow.models.dagrun.tracer", real_tracer):
            yield

    def test_context_carrier_set_on_init(self, dag_maker):
        """DagRun.__init__ should populate context_carrier with a W3C traceparent."""
        with dag_maker("test_tracing_init"):
            EmptyOperator(task_id="t1")
        dr = dag_maker.create_dagrun()

        assert dr.context_carrier is not None
        assert isinstance(dr.context_carrier, dict)
        assert "traceparent" in dr.context_carrier

    def test_context_carrier_unique_per_dagrun(self, dag_maker):
        """Each DagRun should get a distinct trace context."""
        with dag_maker("test_tracing_unique1"):
            EmptyOperator(task_id="t1")
        dr1 = dag_maker.create_dagrun()

        with dag_maker("test_tracing_unique2"):
            EmptyOperator(task_id="t1")
        dr2 = dag_maker.create_dagrun()

        assert dr1.context_carrier["traceparent"] != dr2.context_carrier["traceparent"]

    @pytest.mark.parametrize("final_state", [DagRunState.SUCCESS, DagRunState.FAILED])
    def test_emit_dagrun_span_called_on_completion(self, dag_maker, session, final_state):
        """_emit_dagrun_span should be called exactly once when a dag run finishes."""
        with dag_maker("test_tracing_emit", session=session) as dag:
            EmptyOperator(task_id="t1")

        dr = dag_maker.create_dagrun(state=DagRunState.RUNNING)
        ti = dr.get_task_instance("t1", session=session)
        ti.state = (
            TaskInstanceState.SUCCESS if final_state == DagRunState.SUCCESS else TaskInstanceState.FAILED
        )
        session.flush()

        dr.dag = dag

        with mock.patch.object(dr, "_emit_dagrun_span") as mock_emit:
            dr.update_state(session=session)

        mock_emit.assert_called_once_with(state=final_state)

    def test_emit_dagrun_span_not_called_while_running(self, dag_maker, session):
        """_emit_dagrun_span should not be called while the dag run is still running."""
        with dag_maker("test_tracing_no_emit_running", session=session) as dag:
            EmptyOperator(task_id="t1")
            EmptyOperator(task_id="t2")

        dr = dag_maker.create_dagrun(state=DagRunState.RUNNING)
        tis = dr.get_task_instances(session=session)
        for ti in tis:
            if ti.task_id == "t1":
                ti.state = TaskInstanceState.SUCCESS
            else:
                ti.state = TaskInstanceState.RUNNING
        session.flush()

        dr.dag = dag

        with mock.patch.object(dr, "_emit_dagrun_span") as mock_emit:
            dr.update_state(session=session)

        mock_emit.assert_not_called()

    def test_emit_dagrun_span_uses_context_carrier_ids(self, dag_maker, session):
        """The emitted span should inherit trace_id/span_id from the context_carrier."""
        in_mem_exporter = InMemorySpanExporter()
        provider = TracerProvider(id_generator=OverrideableRandomIdGenerator())
        provider.add_span_processor(SimpleSpanProcessor(in_mem_exporter))
        test_tracer = provider.get_tracer("test")

        with dag_maker("test_tracing_ids", session=session) as dag:
            EmptyOperator(task_id="t1")

        dr = dag_maker.create_dagrun(state=DagRunState.RUNNING)
        ti = dr.get_task_instance("t1", session=session)
        ti.state = TaskInstanceState.SUCCESS
        session.flush()
        dr.dag = dag

        with mock.patch("airflow.models.dagrun.tracer", test_tracer):
            dr.update_state(session=session)

        spans = in_mem_exporter.get_finished_spans()
        assert len(spans) == 1
        span = spans[0]

        # Decode the expected trace_id/span_id from the stored context_carrier
        ctx = TraceContextTextMapPropagator().extract(dr.context_carrier)
        stored_span = otel_trace.get_current_span(context=ctx)
        stored_ctx = stored_span.get_span_context()

        assert span.context.trace_id == stored_ctx.trace_id
        assert span.context.span_id == stored_ctx.span_id

    @pytest.mark.parametrize("final_state", [DagRunState.SUCCESS, DagRunState.FAILED])
    def test_emit_dagrun_span_attributes_and_status(self, dag_maker, session, final_state):
        """The emitted span should have the correct name, attributes, and status code."""
        in_mem_exporter = InMemorySpanExporter()
        provider = TracerProvider(id_generator=OverrideableRandomIdGenerator())
        provider.add_span_processor(SimpleSpanProcessor(in_mem_exporter))
        test_tracer = provider.get_tracer("test")

        with dag_maker("test_tracing_attrs", session=session) as dag:
            EmptyOperator(task_id="t1")

        dr = dag_maker.create_dagrun(state=DagRunState.RUNNING)
        ti = dr.get_task_instance("t1", session=session)
        ti.state = (
            TaskInstanceState.SUCCESS if final_state == DagRunState.SUCCESS else TaskInstanceState.FAILED
        )
        session.flush()
        dr.dag = dag

        with mock.patch("airflow.models.dagrun.tracer", test_tracer):
            dr.update_state(session=session)

        spans = in_mem_exporter.get_finished_spans()
        assert len(spans) == 1
        span = spans[0]

        assert span.name == f"dag_run.{dr.dag_id}"
        assert span.attributes["airflow.dag_id"] == dr.dag_id
        assert span.attributes["airflow.dag_run.run_id"] == dr.run_id

        expected_status = StatusCode.OK if final_state == DagRunState.SUCCESS else StatusCode.ERROR
        assert span.status.status_code == expected_status

    @pytest.mark.parametrize("carrier_value", [None, {}])
    def test_emit_dagrun_span_with_none_or_empty_carrier(self, dag_maker, session, carrier_value):
        """_emit_dagrun_span should emit a root span when context_carrier is None or empty.

        This happens for DagRuns created before OTel tracing was enabled, or whose
        context_carrier was cleared/backfilled to NULL. Per OTel spec, missing context
        results in a new root span rather than a crash.
        """
        in_mem_exporter = InMemorySpanExporter()
        provider = TracerProvider(id_generator=OverrideableRandomIdGenerator())
        provider.add_span_processor(SimpleSpanProcessor(in_mem_exporter))
        test_tracer = provider.get_tracer("test")

        with dag_maker("test_tracing_none_carrier", session=session) as dag:
            EmptyOperator(task_id="t1")

        dr = dag_maker.create_dagrun(state=DagRunState.RUNNING)
        ti = dr.get_task_instance("t1", session=session)
        ti.state = TaskInstanceState.SUCCESS
        session.flush()
        dr.dag = dag

        # Simulate a DagRun with missing context_carrier
        dr.context_carrier = carrier_value

        with mock.patch("airflow.models.dagrun.tracer", test_tracer):
            dr.update_state(session=session)

        # A root span should still be emitted
        spans = in_mem_exporter.get_finished_spans()
        if isinstance(carrier_value, dict):
            assert len(spans) == 1
            assert spans[0].name == f"dag_run.{dr.dag_id}"
        else:
            assert len(spans) == 0

    @pytest.mark.db_test
    def test_context_carrier_includes_detail_level_from_conf(self, dag_maker):
        """DagRun created with TASK_SPAN_DETAIL_LEVEL_KEY in conf should encode the level in trace state."""
        from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator

        from airflow._shared.observability.traces import (
            TASK_SPAN_DETAIL_LEVEL_KEY,
            get_task_span_detail_level,
        )

        with dag_maker("test_tracing_detail_level"):
            EmptyOperator(task_id="t1")
        dr = dag_maker.create_dagrun(conf={TASK_SPAN_DETAIL_LEVEL_KEY: 2})

        ctx = TraceContextTextMapPropagator().extract(dr.context_carrier)
        from opentelemetry import trace

        span = trace.get_current_span(ctx)
        assert get_task_span_detail_level(span) == 2
