Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

2.1 KB
Raw Blame
"""
Django signal receivers for the autoresearch product.

Registered in AutoresearchConfig.ready() (apps.py) after the app registry
is initialised so cross-product model imports are safe.
"""

from __future__ import annotations

from typing import TYPE_CHECKING, Any

import structlog

from posthog.models.scoping import team_scope

if TYPE_CHECKING:
    from products.autoresearch.backend.training.ingestion import FinishedTaskRun

logger = structlog.get_logger(__name__)


def on_task_run_saved(sender: Any, instance: FinishedTaskRun, created: bool, **kwargs: Any) -> None:
    """
    Detect autoresearch TaskRun completion and trigger recipe ingestion.

    Fires on every TaskRun post_save. Fast-path: reads instance.state (no I/O)
    to bail out quickly for non-autoresearch runs.
    """
    if created:
        return

    # Both imports are deferred: this receiver is connected during django.setup(), and
    # the ingestion path reaches pandas. Importing either at module level puts that cost
    # on every process boot (see posthog/test/repo_invariants/test_startup_import_budget.py).
    from products.tasks.backend.facade.api import TaskRunStatus  # noqa: PLC0415

    if instance.status not in {
        TaskRunStatus.COMPLETED,
        TaskRunStatus.FAILED,
        TaskRunStatus.CANCELLED,
    }:
        return

    # TaskRun.state is client-writable JSON: a non-dict would raise outside the try below,
    # rolling back the caller's terminal-status transaction.
    state = instance.state if isinstance(instance.state, dict) else {}
    training_run_id = state.get("autoresearch_training_run_id")
    if not training_run_id:
        return

    try:
        from products.autoresearch.backend.training.ingestion import handle_task_run_completed  # noqa: PLC0415

        # The signal is the entry boundary for this path, so it sets the team scope the
        # fail-closed managers need.
        with team_scope(instance.team_id):
            handle_task_run_completed(instance)
    except Exception:
        logger.exception(
            "autoresearch_signal_handler_failed",
            task_run_id=str(instance.id),
            training_run_id=training_run_id,
        )