refactor: the replay loop gets its next event and sends one event in their own methods

Merged
HA
15h ago
+43-38

Summary

Follow-up to #403: SonarCloud failed it on python:S3776. Wrapping the old dispatch body in a loop put _dispatch_events at cognitive complexity 21 against the allowed 15. Sonar isn't the required check, so #403 merged anyway, and main now carries the finding.

Changes

_next_event returns the session's next event and loads the next batch once the loaded one runs out. _dispatch_event sends one event and returns how long to wait before the next one; a failure without skip_errors ends the session as before. The loop in _dispatch_events only joins the two. Behaviour is unchanged.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Refactoring (no functional changes)
  • CI/CD or tooling changes

Test Plan

  • Backend unit tests pass (cd backend && uv run pytest tests/unit)
  • Frontend unit tests pass (cd frontend && npm run test)
  • Backend E2E tests pass (docker compose exec -T backend sh -c 'uv sync --group test --no-dev --frozen --no-install-project --no-build && pytest tests/e2e')
  • Frontend E2E tests pass (cd frontend && npm run test:e2e)
  • API spec and client regenerated if routes or schemas changed (./deploy.sh openapi, then ./deploy.sh types), never edited by hand
  • Manual testing performed

Validation: unit 572 passed, including #403's full-speed replay test. complexipy now scores the methods 3 and 7.

Checklist

  • My code follows the project's style guidelines
  • I have run pre-commit run --all-files and all checks pass
  • I have added tests that prove my fix/feature works
  • I have updated documentation if needed
  • My changes don't introduce new warnings

Summary by cubic

The replay dispatch loop now pulls events via _next_event and sends each one through _dispatch_event, each method returning control to the loop. This brings _dispatch_events under SonarCloud's cognitive-complexity limit (python:S3776) with no behavior changes.