Files
cleveragents-core/tools/controller/master/__init__.py
T
drew 3ca794be75 feat(controller): autonomous CI status polling — closes the last trial gap
The Phase 2 trial previously required operator-intervention SQL to
advance workflows from AWAITING_CI → REVIEWING (no automated CI
status polling). This commit wires the missing tick so the trial
runs end-to-end without manual help.

Components:
- ``master/forgejo_http.py``: new ``get_ci_status`` callback wraps
  Forgejo's ``/commits/{sha}/status`` combined-status endpoint;
  added to ``ForgejoCallbacks``.
- ``master/ci_status_poll.py`` (NEW): ``run_ci_status_poll_tick``
  scans AWAITING_CI workflows, fetches CI status keyed on the
  latest implementer attempt's ``head_sha_after``, and applies
  state transitions via ``apply_event``. TOCTOU-defended UPDATE
  (``WHERE current_state='AWAITING_CI'``) + per-row exception
  isolation.
- ``master/loop.py``: new ``ci_status_poll_args=(owner, repo,
  get_ci_status)`` kwarg + ``ci_status_poll_interval_s`` config
  (default 60s) + ``MasterTickReport.ci_status_poll`` field.
- ``master/__main__.py``: threads ``callbacks.get_ci_status`` into
  the loop.

State mapping (Forgejo combined-status state → event):
- success / neutral / skipped / warning → ci_green → REVIEWING
- failure / error / cancelled / timed_out / stale →
  ci_red_retry_same_tier → IMPLEMENTING
- pending / queued / in_progress / action_required → no-op (wait)
- None / unknown / fetch failure → no-op (transient)

The ``ci_polling_exhausted`` timeout (default 2h) remains as the
safety net for CI that genuinely never reports.

Tests (+14 in test_master_ci_status_poll.py):
- Happy paths (success→green, failure→red, pending→wait)
- Error paths (callback raises; workflow without head_sha)
- Event row shape (event_type='ci-green'/'ci-red', reason payload)
- Extended state mapping (cancelled, neutral, in_progress)
- Other-repo isolation
- LoopIntegration end-to-end via master_main_loop with safety timer

RUNBOOK updated: removed the manual SQL workaround; added the
autonomous CI poll's tunables.

Total: 726 controller tests pass (+14 net), 0 regressions.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-18 17:37:04 -04:00

215 lines
5.4 KiB
Python

"""Master controller — singleton orchestrator.
Per plan v9: the master process is a singleton per (owner, repo). It:
1. Runs discovery (Forgejo poll) — Phase 1d-3
2. Drives the state machine: for each non-terminal workflow, advance
one step based on the outcome of its latest completed attempt
3. Runs the reaper + pickup guard each tick
4. Performs Forgejo writes (status comments, labels, merges) —
Phase 1d-3+
5. Runs periodic reconciliation (DB↔Forgejo) — Phase 1d-3+
This package ships incrementally. Phase 1d-1 (already shipped) had
the state machine + reaper + pickup guard. This Phase 1d-2 adds:
- ``outcomes.py``: maps worker output_payload outcomes → state machine
events (e.g., implementer outcome='resolved''implementer_pushed';
outcome='rebase-failed''implementer_rebase_failed').
- ``tick.py``: one tick handler — scans recently-completed attempts,
applies events to their workflows, emits controller_events.
"""
from .outcomes import (
EventMapResult,
map_outcome_to_event,
)
from .tick import (
TickReport,
run_tick,
)
from .scheduler import (
MAX_TIER,
PrefetchCallback,
ScheduledAttempt,
SchedulerReport,
schedule_next_attempts,
)
from .discovery import (
DiscoveredEntity,
DiscoveryReport,
ListIssuesCallback,
ListPRsCallback,
run_discovery,
)
from .forgejo_writes import (
AddLabelCallback,
GetLabelsCallback,
LabelAdjustResult,
ListCommentsCallback,
PostCommentCallback,
RemoveLabelCallback,
StatusCommentResult,
adjust_labels,
build_marker,
comment_has_fingerprint,
compute_fingerprint,
post_status_comment,
)
from .merging import (
MAX_BACKOFF_S,
MAX_MERGE_RETRIES,
MergeCallback,
MergeResponse,
MergingHandlerReport,
run_merging_tick,
)
from .forgejo_http import (
ForgejoCallbacks,
build_callbacks,
)
from .backfill import (
BACKFILL_MARKER_EVENT_TYPE,
BackfillReport,
has_backfill_run,
run_startup_backfill,
)
from .ci_poll import (
CIPollExhaustionReport,
DEFAULT_AWAITING_CI_TIMEOUT_S,
run_ci_poll_exhaustion_tick,
)
from .ci_status_poll import (
CIStatusPollReport,
GetCIStatusCallback,
run_ci_status_poll_tick,
)
from .promote import (
PromoteDiscoveredReport,
run_promote_discovered_tick,
)
from .ci_summarize import (
LogFetcher,
summarize_ci_status,
)
from .label_gate import (
DEFAULT_OPT_IN_LABEL,
count_filtered,
filter_by_opt_in_label,
has_opt_in_label,
opt_in_label_name,
)
from .prefetch import (
GetPRDetailsCallback,
GetPRDiffCallback,
ListPRCommentsCallback,
ListPRReviewsCallback,
PrefetchDataCallbacks,
build_conflict_resolver_input,
build_estimator_input,
build_implementer_input,
build_reviewer_input,
make_prefetch_callback,
)
from .reconciliation import (
GetIssueStateCallback,
GetPRStateCallback,
ReconciliationAction,
ReconciliationReport,
run_reconciliation_tick,
)
from .loop import (
MasterConfig,
MasterTickReport,
master_main_loop,
run_master_iteration,
)
__all__ = [
# Discovery
"DiscoveredEntity",
"DiscoveryReport",
"ListIssuesCallback",
"ListPRsCallback",
"run_discovery",
# Outcome mapping
"EventMapResult",
"map_outcome_to_event",
# Scheduler
"MAX_TIER",
"PrefetchCallback",
"ScheduledAttempt",
"SchedulerReport",
"schedule_next_attempts",
# Tick
"TickReport",
"run_tick",
# Main loop
"MasterConfig",
"MasterTickReport",
"master_main_loop",
"run_master_iteration",
# Forgejo writes
"AddLabelCallback",
"GetLabelsCallback",
"LabelAdjustResult",
"ListCommentsCallback",
"PostCommentCallback",
"RemoveLabelCallback",
"StatusCommentResult",
"adjust_labels",
"build_marker",
"comment_has_fingerprint",
"compute_fingerprint",
"post_status_comment",
# MERGING handler
"MAX_BACKOFF_S",
"MAX_MERGE_RETRIES",
"MergeCallback",
"MergeResponse",
"MergingHandlerReport",
"run_merging_tick",
# HTTP adapter (Forgejo wiring)
"ForgejoCallbacks",
"build_callbacks",
# Backfill (master startup)
"BACKFILL_MARKER_EVENT_TYPE",
"BackfillReport",
"has_backfill_run",
"run_startup_backfill",
# Reconciliation (periodic DB ↔ Forgejo sync)
"GetIssueStateCallback",
"GetPRStateCallback",
"ReconciliationAction",
"ReconciliationReport",
"run_reconciliation_tick",
# Prefetch (Phase 1h)
"GetPRDetailsCallback",
"GetPRDiffCallback",
"ListPRCommentsCallback",
"ListPRReviewsCallback",
"PrefetchDataCallbacks",
"build_conflict_resolver_input",
"build_estimator_input",
"build_implementer_input",
"build_reviewer_input",
"make_prefetch_callback",
# CI summarizer (Phase 1j)
"LogFetcher",
"summarize_ci_status",
# CI poll-exhaustion (Phase 1k++)
"CIPollExhaustionReport",
"DEFAULT_AWAITING_CI_TIMEOUT_S",
"run_ci_poll_exhaustion_tick",
# CI status poll (Phase 1k++++)
"CIStatusPollReport",
"GetCIStatusCallback",
"run_ci_status_poll_tick",
# Promote DISCOVERED → ANALYZING (Phase 1k+++)
"PromoteDiscoveredReport",
"run_promote_discovered_tick",
# Opt-in label gate (Phase 1k)
"DEFAULT_OPT_IN_LABEL",
"count_filtered",
"filter_by_opt_in_label",
"has_opt_in_label",
"opt_in_label_name",
]