Skip to content

LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows

10.0 KB
Raw Blame
"""Remove the Agent-KV maintenance periodics -- the CronJob owns the schedule.

**The ``pg_owned=False`` scope below is wrong on cloud, and
``0009_drop_agent_kv_periodic_tasks`` finishes the job. Read that first.** In
short: the premise this file argues from -- "nothing automated ever sets
``pg_owned=True``" -- is false. The chart's ``backend.pgScheduleMirror`` hook
runs ``converge_pg_scheduler --periodics`` on every install, upgrade and
rollback, and its blanket ``--adopt`` flips every row in ``pg_periodic_task``,
these two included. So on cloud the guard spares exactly the rows that are
double-firing, which was then observed live on a dev namespace. 0009 deletes
by name alone, and explains why the self-hosted operator this guard protects
cannot exist at the point either migration runs.

This file is left as it was, for the same reason it exists rather than being
an edit to 0004: it is recorded as applied in every environment that has the
problem, so changing its predicate would reach none of them.

Everything below is the original reasoning, kept for the record.

----


Migration 0004 seeded two ``PgPeriodicTask`` rows (``agent_kv.sweep`` at
``*/10``, ``agent_kv.ttl_cleanup`` at ``17 3 * * *``) on the stated premise
that nothing anywhere fired the two maintenance commands -- "no
``PgPeriodicTask``, no beat entry, no CronJob". **That premise was false when
it was written.** ``templates/backend/agent-kv-cronjobs.yaml`` in the cloud
chart already ran the same two management commands, added several commits
earlier on the same branch. 0004 was written in response to a review finding
about the unscheduled sweep without noticing the finding had already been
answered elsewhere in the same change.

Keeping both would have been worse than either alone:

* **Two owners for one schedule.** The rows are seeded ``pg_owned: False`` and
  the PG scheduler claims only ``WHERE pg_owned AND enabled``
  (``pg_queue/models.py``), so they are inert *today*. But that flag exists to
  be flipped by a rollout -- and the moment it was, the ``*/10`` rows and the
  CronJob's ``*/15`` ticks would both fire, colliding exactly at ``:30`` and
  contending on the same job rows. The TTL cadences disagreed by 24x (daily
  here, hourly in the chart), so whichever ran would be whichever owner someone
  happened to remember.
* **Only one owner has operational bounds.** The CronJob carries
  ``concurrencyPolicy: Forbid`` and ``activeDeadlineSeconds``; a PG periodic has
  neither, so a hung sweep under the scheduler has nothing stopping it.
* **The feature only runs where the CronJob is.** This deployment routes
  ``table`` only, and the ``agentic_table`` plugin ships in the cloud image. A
  pure-OSS install cannot dispatch an Agent-KV job at all, so there is no
  stranded row for a sweep to reap there.

**Why this is a new migration and not an edit to 0004.** Django records a
migration as applied and never re-runs it. Rewriting 0004's forward operation
would therefore have had no effect whatsoever in exactly the environments that
have the rows -- every dev namespace that applied the seeding version -- while
changing behaviour only for fresh installs. The first cut of this fix did that,
and its own docstring claimed to clean up environments it could not reach.

So: 0004 stays as it was, this removes. A fresh install seeds and then deletes
(two trivial writes); an environment that already applied 0004 just deletes.
Both land on the same state, which is the point.

The delete is scoped by name AND by ``pg_owned=False``, and is idempotent.

The name scope is the obvious one: an unscoped delete would wipe the
dashboard_metrics periodics this table is shared with.

The ``pg_owned=False`` scope is the important one. These rows are seeded inert,
so deleting an inert row removes the accidental second owner and nothing else.
But a self-hosted operator running the PG scheduler has every reason to flip
``pg_owned: True`` on them -- that is exactly what the docs tell them to do, and
there is no CronJob outside the cloud chart to replace it. Deleting by name
alone would silently remove a LIVE schedule from such a deployment: stuck jobs
would stop being swept (holding their concurrency slots until the 6 h Redis TTL)
and expired customer documents would stay in the bucket indefinitely, with
nothing reporting it.

So a row the operator has taken ownership of is left alone. That is the correct
reading of this migration's purpose: it removes a second owner that 0004 added
by mistake, not any schedule that happens to carry these two names.

**The limit of that, stated plainly: a migration cannot tell a cloud
deployment from a self-hosted one, and ``pg_owned=True`` means opposite things
in the two.** Self-hosted, it is the operator's only maintenance mechanism and
must survive. On cloud, where the chart's CronJobs are the owner, it would be a
second owner and the collision this migration exists to remove.

The reason inert-only is still right: nothing automated ever sets
``pg_owned=True``. The per-schedule ramp control that would is explicitly not
built yet (``pg_queue/models.py`` -- "that's the ramp control, which does not
exist yet"), so on cloud these rows are inert as seeded and this migration
removes all of them. Reaching the cloud double-fire case takes a deliberate
manual flip on a deployment whose chart already runs the CronJobs -- and
silently deleting an operator's explicit configuration during an unrelated
migration is not the remedy for that. It is visible in
``SELECT name, pg_owned FROM pg_periodic_task WHERE name LIKE 'agent_kv%'``

(``pg_periodic_task`` -- the table ``PgPeriodicTask`` maps to, which is what
0004 seeded. NOT ``pg_periodic_schedule``: that is the separate
``PgPeriodicSchedule`` model, which also carries a ``pg_owned`` column but has
no ``name`` at all -- it keys on ``pipeline_id``/``workflow_id`` -- so the
query named there failed outright with "column name does not exist".)
and belongs in the cloud runbook, not in a guess made here.

Choosing the other way round -- delete by name regardless of ownership -- trades
a manual-only cloud misconfiguration for silently stranding every self-hosted
deployment that followed the documented instructions. That is the worse trade.

**Self-hosted OSS operators who enable the Agent-KV API must schedule
``manage.py agent_kv_sweep`` and ``manage.py agent_kv_ttl_cleanup``
themselves** (cron, a systemd timer, or their own ``PgPeriodicTask`` rows with
``pg_owned: True``). Without them a stranded job holds its concurrency slot
until the 6 h Redis TTL, and ``AGENT_KV_RESULT_TTL_DAYS`` is advisory --
staged documents and results stay in the bucket indefinitely, which is a
retention failure rather than a disk-usage one. ``docs/agent-kv-api.md`` §12
carries this in the deploy checklist.

Reverse re-creates the rows (still inert), so ``migrate agent_kv 0006`` lands
back on the previous release's state exactly rather than leaving an environment
with no rows and no way to restore them.
"""

import importlib

from django.db import migrations

#: The rows 0004 created, read from 0004 itself rather than re-declared here,
#: so the two files cannot drift into deleting a different set than was seeded.
#: `importlib` because the module name starts with a digit and cannot be a
#: plain `import` (same reason `tests/test_periodic_tasks_migration.py` does
#: it this way).
PG_PERIODIC_TASKS = importlib.import_module(
    "agent_kv.migrations.0004_pg_periodic_tasks"
).PG_PERIODIC_TASKS


def remove_pg_periodic_tasks(apps, schema_editor):
    """Delete the seeded rows, but only while they are still inert.

    Idempotent: a fresh install matches nothing.

    `pg_owned=False` in the filter is load-bearing, not defensive. A
    self-hosted operator who flipped these rows to `pg_owned: True` is running
    the PG scheduler as their ONLY maintenance mechanism -- there is no CronJob
    outside the cloud chart -- so deleting them by name would strand jobs and
    retain expired documents silently. Their row is left exactly as they set it.
    """
    # snake_case, not the usual `PgPeriodicTask = apps.get_model(...)` Django
    # idiom: it is a local variable, and sonar's S117 reads the CamelCase name
    # as a naming violation. The historical-model object is the same either way.
    periodic_task = apps.get_model("pg_queue", "PgPeriodicTask")
    names = [spec["name"] for spec in PG_PERIODIC_TASKS]
    periodic_task.objects.filter(name__in=names, pg_owned=False).delete()


def restore_pg_periodic_tasks(apps, schema_editor):
    """Reverse only. Re-create 0004's rows if they are MISSING -- nothing more.

    `get_or_create`, deliberately not `update_or_create`. Forward leaves a row
    alone when the operator has adopted it (`pg_owned=True`), so on a rollback
    that row is still there -- and an `update_or_create` whose defaults carry
    `pg_owned: False` would reset it, silently disabling the self-hosted
    operator's only maintenance mechanism as a side effect of rolling back an
    unrelated release. The reverse must restore what forward removed, not
    normalize what it preserved.

    New rows are created inert. A reverse that restored them ENABLED would turn
    a rollback into the double-fire this migration exists to prevent.
    """
    periodic_task = apps.get_model("pg_queue", "PgPeriodicTask")
    for spec in PG_PERIODIC_TASKS:
        periodic_task.objects.get_or_create(
            name=spec["name"],
            defaults={
                "task_name": spec["task_name"],
                "queue": spec["queue"],
                "task_args": spec["task_args"],
                "task_kwargs": spec["task_kwargs"],
                "cron_string": spec["cron_string"],
                # Platform-wide, not per-org (spec §5.4): both internal
                # endpoints sweep across every organization.
                "org_id": "",
                "enabled": True,
                "pg_owned": False,
            },
        )


class Migration(migrations.Migration):
    dependencies = [
        ("agent_kv", "0006_agentkvjob_adapters"),
    ]

    operations = [
        migrations.RunPython(remove_pg_periodic_tasks, restore_pg_periodic_tasks),
    ]