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.

5.1 KB
Raw Blame
"""Scheduled entrypoints for the AEO citation runner.

The dispatcher is double-gated: a team must be in the AEO_CITATION_TEAM_IDS env
allowlist AND have the `aeo-citation-tracking` feature flag enabled. Both are
empty/off by default, so this is a no-op everywhere until deliberately turned
on. Each eligible team gets its own long-running task so one slow team can't
block another and a worker restart only loses one team's in-flight run.
"""

from __future__ import annotations

from django.conf import settings

import structlog
from celery import shared_task

from posthog.celery_queues import CeleryQueue
from posthog.models.team import Team
from posthog.ph_client import feature_enabled_or_false
from posthog.scoping_audit import skip_team_scope_audit

from products.aeo.backend import suggestions
from products.aeo.backend.facade.contracts import AEO_CITATION_TRACKING_FLAG, PromptSuggestionRequest
from products.aeo.backend.runner import run_citation_checks

logger = structlog.get_logger(__name__)


# A full run covers the whole prompt set: each prompt makes one web-search call per
# engine (about a minute each), AEO_RUN_CONCURRENCY prompts at a time. Ten hours
# leaves headroom for a slow day and still ends a hung run well before the next
# daily dispatch.
RUN_SOFT_TIME_LIMIT_SECONDS = 10 * 60 * 60
RUN_TIME_LIMIT_SECONDS = RUN_SOFT_TIME_LIMIT_SECONDS + 300
SUGGESTION_SOFT_TIME_LIMIT_SECONDS = 6 * 60
SUGGESTION_TIME_LIMIT_SECONDS = SUGGESTION_SOFT_TIME_LIMIT_SECONDS + 60


def _citation_tracking_enabled(team: Team) -> bool:
    """The double gate: a team runs only when it is in the env allowlist AND has
    the flag on. Both default off, so this is fail-closed. Shared by the dispatcher
    and the per-team task so a task queued directly cannot skip the gate."""
    allowlist = {str(raw).strip() for raw in settings.AEO_CITATION_TEAM_IDS}
    if str(team.id) not in allowlist:
        return False
    # No person is behind this scheduled run; the team UUID keeps the flag call
    # well-formed and the organization group lets the flag target teams.
    return feature_enabled_or_false(
        AEO_CITATION_TRACKING_FLAG,
        str(team.uuid),
        groups={"organization": str(team.organization_id)},
        group_properties={"organization": {"id": str(team.organization_id)}},
    )


@shared_task(ignore_result=True)
@skip_team_scope_audit  # Team is the tenant, not tenant data; the per-team gate scopes what follows
def run_aeo_citation_checks_task() -> None:
    """Beat entrypoint: fan out one runner task per allowlisted, flag-enabled team."""
    for raw_team_id in settings.AEO_CITATION_TEAM_IDS:
        try:
            team = Team.objects.get(id=int(raw_team_id))
        except (Team.DoesNotExist, ValueError):
            logger.warning("aeo_citation_task_unknown_team", team_id=raw_team_id)
            continue
        if not _citation_tracking_enabled(team):
            logger.info("aeo_citation_task_flag_disabled", team_id=team.id)
            continue
        run_aeo_citation_checks_for_team_task.delay(team.id)


@shared_task(
    ignore_result=True,
    queue=CeleryQueue.LONG_RUNNING.value,
    soft_time_limit=RUN_SOFT_TIME_LIMIT_SECONDS,
    time_limit=RUN_TIME_LIMIT_SECONDS,
)
@skip_team_scope_audit  # Team is the tenant, not tenant data; prompts are read via AEOPrompt.objects.for_team
def run_aeo_citation_checks_for_team_task(team_id: int) -> None:
    try:
        team = Team.objects.get(id=team_id)
    except Team.DoesNotExist:
        logger.warning("aeo_citation_task_unknown_team", team_id=team_id)
        return
    # Re-check the gate here too, so this task fails closed even if it is queued
    # outside the dispatcher (which already checks before fan-out).
    if not _citation_tracking_enabled(team):
        logger.info("aeo_citation_task_flag_disabled", team_id=team_id)
        return
    try:
        run_citation_checks(team)
    except Exception:
        # The runner already absorbs per-prompt failures, so anything reaching here is
        # unexpected. Re-raise so Celery marks the task failed and alerting sees it.
        logger.exception("aeo_citation_task_failed", team_id=team_id)
        raise


@shared_task(
    ignore_result=True,
    queue=CeleryQueue.LONG_RUNNING.value,
    soft_time_limit=SUGGESTION_SOFT_TIME_LIMIT_SECONDS,
    time_limit=SUGGESTION_TIME_LIMIT_SECONDS,
)
@skip_team_scope_audit  # Team is the tenant; prompts are read via AEOPrompt.objects.for_team
def suggest_aeo_prompts_task(team_id: int, job_id: str, request: dict) -> None:
    try:
        suggestions.run_job(
            team_id,
            job_id,
            PromptSuggestionRequest(
                domain=request["domain"],
                brand=request["brand"],
                description=request["description"],
                competitors=tuple(request["competitors"]),
                topics=tuple(request["topics"]),
                count=request["count"],
            ),
        )
    except Exception:
        # Without this the client would poll a pending job until it expires.
        suggestions.fail_job(team_id, job_id, "Prompt suggestions failed. Try again in a minute.")
        logger.exception("aeo_prompt_suggestions_task_failed", team_id=team_id)
        raise