Skip to content

Trace-native CI/CD for AI agents — production failures become regression tests that block the PR. Auto-detect, cluster, freeze into hermetic cases, replay in CI for $0.

5.5 KB
Raw Blame
"""Shared Celery app. The API (producer) and worker (consumer) both import this.
Tasks live in tracely.workers.tasks (included via `include`).
"""

from __future__ import annotations

from celery import Celery
from celery.schedules import crontab
from celery.signals import task_postrun

from tracely.config import settings

celery_app = Celery(
    "tracely",
    broker=settings.redis_url,
    backend=settings.redis_url,
    include=["tracely.workers.tasks"],
)

celery_app.conf.update(
    task_default_queue=settings.celery_queue,
    task_acks_late=True,
    worker_prefetch_multiplier=4,
    task_serializer="json",
    result_serializer="json",
    accept_content=["json"],
    # With acks_late on Redis, an unacked task is redelivered after `visibility_timeout` — so a long
    # task (cluster rebuild, batch eval) that outruns the default 1h gets run a SECOND time while the
    # first is still going (double work, double LLM spend). Raise the window past our slowest task.
    broker_transport_options={"visibility_timeout": 3 * 60 * 60},  # 3h
    # Bound task runtime so a hung task can't pin the (solo) worker forever.
    task_time_limit=30 * 60,  # hard kill at 30m
    task_soft_time_limit=25 * 60,  # SoftTimeLimitExceeded at 25m (lets a task clean up)
    # Periodic schedule. NOTHING here fires unless a beat scheduler is running: the local docker
    # worker embeds one (`--beat` in docker-compose.yml), and prod must run exactly one beat
    # process. Monitors stay quiet by default — they only fire when a condition crosses its
    # threshold and the dedup interval has elapsed.
    beat_schedule={
        "tracely.evaluate_monitors-every-5-min": {
            "task": "tracely.evaluate_monitors",
            "schedule": crontab(minute="*/5"),
        },
        # The deployment watching itself: queue depth, worker liveness, ingest freshness.
        # Logs every tick (a heartbeat log-based alerting can watch) and pages when degraded.
        "tracely.selfcheck-every-5-min": {
            "task": "tracely.selfcheck",
            "schedule": crontab(minute="*/5"),
        },
        # An audit advances as a chain of short ticks; a worker restart breaks the chain. This
        # picks up any run that has stopped checkpointing.
        "tracely.audit_watchdog-every-5-min": {
            "task": "tracely.audit_watchdog",
            "schedule": crontab(minute="*/5"),
        },
        # Audit sandboxes left behind by a rollout that ended without cleaning up. A no-op on a
        # deployment with no sandbox backend.
        "tracely.reap_sandboxes-every-15-min": {
            "task": "tracely.reap_sandboxes",
            "schedule": crontab(minute="*/15"),
        },
        # Judge-conversation checkpoints grow quadratically and are never re-read. Nightly is
        # plenty — this reclaims space, it does not keep anything correct.
        "tracely.prune_chats-nightly": {
            "task": "tracely.prune_chats",
            "schedule": crontab(hour="4", minute="17"),
        },
        # Plan retention (hosted cloud only): the free tier keeps a week, the table TTL keeps
        # the rest. Offset from the chat prune so two sweeps don't share the solo worker's slot.
        "tracely.enforce_retention-nightly": {
            "task": "tracely.enforce_retention",
            "schedule": crontab(hour="4", minute="47"),
        },
        # The Postgres span store's 90-day horizon (ClickHouse's table TTL did this itself; on
        # ClickHouse the task is a no-op). Every deployment, billing or not.
        "tracely.expire_spans-nightly": {
            "task": "tracely.expire_spans",
            "schedule": crontab(hour="4", minute="57"),
        },
        # ClickHouse's own logs have no TTL and outgrow the product's data on a small box. Same
        # nightly window, offset again so the solo worker takes one sweep at a time.
        "tracely.cap_system_logs-nightly": {
            "task": "tracely.cap_system_logs",
            "schedule": crontab(hour="5", minute="7"),
        },
        # A lightweight DELETE only masks rows and a tombstoned eval recording only collapses in a
        # merge — neither leaves the disk until a part is rewritten, and every read pays for them
        # until then. After the system-log sweep so the solo worker takes one at a time.
        "tracely.compact_tables-nightly": {
            "task": "tracely.compact_tables",
            "schedule": crontab(hour="5", minute="27"),
        },
        # Data whose workspace no longer exists (a delete whose purge never finished, or one from
        # before purges existed). Refuses on any sign the registry read is wrong.
        "tracely.purge_orphans-nightly": {
            "task": "tracely.purge_orphans",
            "schedule": crontab(hour="5", minute="47"),
        },
        # Raw OTLP bodies past the 90-day horizon the tables already enforce. Last in the window:
        # it lists every body, so it holds the solo worker longest.
        "tracely.expire_otlp_blobs-nightly": {
            "task": "tracely.expire_otlp_blobs",
            "schedule": crontab(hour="6", minute="7"),
        },
    },
    timezone="UTC",
)


@task_postrun.connect
def _stamp_worker_alive(**_: object) -> None:
    """The worker's pulse: "a task finished just now". A deep queue is only an incident when
    nothing is coming off it, and this is what tells those two apart (`domain/ops/selfcheck.py`).
    Imported lazily — this module is imported by the API producer too."""
    from tracely.services.selfcheck_service import LAST_TASK_KEY, stamp

    stamp(LAST_TASK_KEY)