Files
cleveragents-core/tools/controller
drew ba2e9472bc feat(controller): Phase 1f — master startup backfill
When the master starts (first deploy or after a long outage), it
needs to learn about existing open PRs/issues that weren't created
via discovery-tick-during-uptime. Backfill = discovery + a one-time
marker so subsequent restarts know "this isn't the first time."

tools/controller/master/backfill.py:

- run_startup_backfill(engine, owner, repo, list_prs, list_issues):
  - Calls run_discovery (already idempotent — skips existing entities)
  - Records a 'controller-backfill-complete' marker in controller_events
    associated with the first new workflow OR an existing workflow OR
    skipped if Forgejo is truly empty (no FK target)
  - Returns BackfillReport{first_time, discovery}.

- has_backfill_run(engine, owner, repo): existence-check on the marker
  by parsing controller_events.payload. Multi-tenant isolated — a
  marker for (owner_a, repo_a) doesn't satisfy a check for
  (owner_b, repo_b).

- The marker is informational; the dedup is provided by discovery's
  unique-constraint skip. The marker exists so operators can answer
  "has backfill ever run for this repo?" in one SQL query.

Wired into master __main__:
- Runs AFTER engine/create_all + Forgejo callback wiring, BEFORE
  master_main_loop.
- Try/except wrapped so Forgejo flake at startup doesn't prevent
  the main loop from running (discovery tick will retry).
- New --skip-backfill flag for tests + warm restarts.

8 new tests in test_master_backfill.py:
- has_backfill_run: no marker → False; multi-tenant isolation
  (different owner OR different repo → False).
- First-time backfill creates workflows + marker; empty Forgejo
  skips marker (no FK target).
- Second run reports first_time=False; picks up newly-appeared PRs
  + emits a second marker.
- Multi-tenant (owner-a, repo-a) and (owner-b, repo-b) both get
  their own marker.
- New workflows are in DISCOVERED state.

Total: 415 controller tests; full auto_agents suite 2777 pass.
2026-05-18 14:16:23 -04:00
..