if hasattr(args, "external_executor_id"):
        return getattr(args, "external_executor_id")
    return os.environ.get("external_executor_id", None)
@contextmanager
def _move_task_handlers_to_root(ti: TaskInstance | TaskInstancePydantic) -> Generator[None, None, None]:
    """
    Move handlers for task logging to root logger.
    We want anything logged during task run to be propagated to task log handlers.
    If running in a k8s executor pod, also keep the stream handler on root logger
    so that logs are still emitted to stdout.
    """
    
    if not ti.log.handlers or settings.DONOT_MODIFY_HANDLERS:
        yield
        return
    
    
    
    root_logger = logging.getLogger()
    console_handler = next((h for h in root_logger.handlers if h.name == "console"), None)
    with LoggerMutationHelper(root_logger), LoggerMutationHelper(ti.log) as task_helper:
        task_helper.move(root_logger)
        if IS_K8S_EXECUTOR_POD or IS_EXECUTOR_CONTAINER:
            if console_handler and console_handler not in root_logger.handlers:
                root_logger.addHandler(console_handler)
        yield
@contextmanager
def _redirect_stdout_to_ti_log(ti: TaskInstance | TaskInstancePydantic) -> Generator[None, None, None]: