Files
cleveragents-core/tools/controller/master/__init__.py
T
drew dfdfbf762b feat(controller): Phase 1h — prefetch callbacks (V1 input assembly)
Per plan v9, the master assembles the worker input_payload at
attempt-enqueue time so the worker dequeues a ready-to-use payload
with no extra Forgejo I/O of its own.

This phase ships per-role V1-input builders + a factory matching the
scheduler's PrefetchCallback protocol:

- build_implementer_input  → ImplementerInputV1 shape (head_sha,
  head_ref, base_branch, active_reviews, pr_comments_since_last_attempt,
  prior_attempts, diff_summary)
- build_reviewer_input     → ReviewerInputV1 shape (full_diff,
  prior_implementer_attempts, implementer_claim, prior_reviews)
- build_estimator_input    → EstimatorInputV1 shape (pr_title, pr_body,
  diff_summary) — works for both PR and issue kinds
- build_conflict_resolver_input → ConflictResolverInputV1 shape with
  conflicted_files=[] stub (worker patches via git rebase)
- make_prefetch_callback(engine, callbacks) → routes by role; returns
  (payload, "V1") matching the scheduler's PrefetchCallback signature

Forgejo HTTP wiring adds four new callbacks (get_pr_details,
get_pr_diff, list_pr_reviews, list_pr_comments) plumbed through
ForgejoCallbacks.

Worker-side patches (post-dequeue, pre-validation):
- attempt_id, attempt_number (known from dequeue)
- workspace_dir (worker filesystem path)
- wallclock_budget_s (worker config)

What this phase DOES NOT yet produce:
- ci_summary / failing_gates — Phase 1j (deterministic CI summarizer)
- Issue-kind estimator's title/body — needs list_issue_details
  callback (defer to future phase)
- conflict_resolver's actual conflicted_files — needs worker-side git
  rebase + conflict-parse pass

Tests (+26 in test_master_prefetch.py, 0 regressions):
- Per-role shape validation + V1 contract parse after worker patches
- Prior-attempts merge (verbatim cap=3, oldest-first, total count)
- Active-reviews projection (filters invalid states/missing user)
- pr_comments_since_last_attempt filtering by finished_at
- Factory routes by role; unknown role raises
- Scheduler integration end-to-end (real prefetch → real INSERT)

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

170 lines
4.2 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 .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",
]