Skip to content

refactor(cleanup): per-sweep strategies for _run_cleanup_inner (#1026) - #1035

Merged
vybe merged 1 commit into
devfrom
refactor/1026-cleanup-sweeps
Jun 3, 2026
Merged

vybe merged 1 commit into
devfrom
refactor/1026-cleanup-sweeps

Conversation

@dolho

@dolho dolho commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Summary

First (lowest-difficulty) refactor from #1026 — decompose the cleanup-service hotspot.

CleanupService._run_cleanup_inner was a single 290-line method running ~11 sequential sweeps, each wrapped in its own try/except. This extracts each sweep into a focused _sweep_* method; the orchestrator is now a 33-line sequence that reads top-to-bottom.

No behavior change. Preserved exactly:

  • per-sweep error isolation (one sweep raising never aborts the cycle)
  • the rate-limit prune cycle gate (_cycle_count % 12, read before the orchestrator's end-of-cycle increment)
  • the watchdog → slot-sweep confirmed_running_ids handoff (bug: Stale slot cleanup uses fixed 20-min TTL regardless of agent timeout #226)
  • the WAL-checkpoint retention gate (fires only when a retention sweep reclaimed rows)

Sweeps extracted

_sweep_watchdog (#129) · _sweep_stale_executions · _sweep_no_session_executions (#106) · _sweep_orphaned_skipped (#106) · _sweep_stale_activities · _sweep_stale_slots (#219/#226/#378) · _sweep_rate_limit_events (#476) · _sweep_shared_files (FILES-001) · _sweep_retention_772 (#772) · _sweep_soft_deleted_agents (#834 1a) · _sweep_soft_deleted_schedules (#834 1b) · _maybe_wal_checkpoint

Tests

Adds tests/unit/test_cleanup_inner_sweeps.py — characterization tests pinning the cycle's contract (sweep set, report-field population, error isolation, cycle-gating, WAL gate). Green before and after the extraction — the proof of behavior preservation.

  • ✅ 5/5 new characterization tests green pre- and post-refactor
  • ✅ 1730 passed in the unit suite (-m "not slow"); the only failures are 5 pre-existing/environmental test_git_pull_branch.py cases (need a real origin/main repo) — unrelated to this diff
  • Acceptance: orchestrator now 33 lines / cyclomatic ~2 (target <100 / <20)

Scope

Single function per the issue's "small incremental PRs" guidance. Next in #1026: adapters/message_router._handle_message_inner. execute_task (25 params) saved for last, after the #1027 request-object pattern.

Related to #1026

…gies (#1026)

The cleanup cycle was a single 290-line method running ~11 sequential
sweeps, each in its own try/except. Extract each sweep into a focused
`_sweep_*` method; the orchestrator is now a 33-line sequence. No behavior
change — per-sweep error isolation, the rate-limit cycle gate, the
watchdog→slot-sweep `confirmed_running_ids` handoff, and the WAL-checkpoint
retention gate are all preserved.

Adds tests/unit/test_cleanup_inner_sweeps.py characterizing the cycle
(sweep set, report population, error isolation, cycle-gating, WAL gate);
green before and after the extraction.

Related to #1026

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@dolho
dolho force-pushed the refactor/1026-cleanup-sweeps branch from dbf52f8 to 653aeb0 Compare June 3, 2026 08:44
@dolho

dolho commented Jun 3, 2026

Copy link
Copy Markdown
Contributor Author

✅ /review — positive

Structural pre-landing review (branch diff vs dev). Pure behavior-preserving extraction; 0 critical, 0 informational.

Scope: CLEAN — delivers exactly the #1026 acceptance item (_run_cleanup_inner split so each sweep is its own strategy fn), one-function-per-PR as the issue recommends. Committed diff is the 2 intended files only.

Semantic-equivalence checks (the real risk for this kind of refactor):

  • Sweep order identical to the original 0→1→1b→1c→2→3→4→4b→4c→4c-bis→4c-ter→4c-quater→4d, then cycle_count+=1 / last_run_at / last_report / conditional log. ✓
  • Cycle gate _sweep_rate_limit_events reads _cycle_count % 12 before the end-of-cycle increment — same value as before. ✓
  • confirmed_running_ids handoff _sweep_watchdog returns the unpacked set on success, set() on exception (matches the original pre-init fallback) → fed to _sweep_stale_slots. ✓
  • WAL gate retention_total includes the merged idempotency_keys_purged (feat: idempotency keys at all execution trigger boundaries (RELIABILITY-006) #525). ✓
  • Error isolation every sweep keeps its own try/except; one failing never aborts the cycle. ✓

Clean categories: SQL/data safety (no SQL touched) · concurrency (runs under existing self._lock) · auth (no endpoints) · credentials (log strings moved verbatim) · enum completeness (idempotency_keys_purged already in total/to_dict) · perf (identical DB call pattern).

Verification: 5 characterization unit tests green pre+post extraction; live-instance cleanup cycle ran end-to-end (status: completed, full report) with zero cleanup errors in logs; 7/7 test_cleanup_service.py integration green against a backend running this branch.

Recommend merge.

@github-actions

github-actions Bot commented Jun 3, 2026

Copy link
Copy Markdown

⚠️ Nightly unit-suite check skipped — merge conflict against dev.

Resolve by running git merge dev locally and pushing the result. The next nightly run will re-test once the conflict is gone.

@vybe vybe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Validated via /validate-pr: faithful extract-method decomposition (290-line method → 33-line orchestrator). Sweep order, per-sweep error isolation, the #226 watchdog→slot handoff, the _cycle_count%12 gate (read before increment), and the WAL-checkpoint retention gate all preserved. Characterization tests green pre+post. Approving.

@vybe
vybe merged commit fd6a977 into dev Jun 3, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants