Skip to content

refactor: consolidate percentile implementation (polylogue-a7xr.4) - #2896

Merged
Sinity merged 4 commits into
masterfrom
feature/refactor/consolidate-percentile-implementation
Jul 14, 2026
Merged

Sinity merged 4 commits into
masterfrom
feature/refactor/consolidate-percentile-implementation

Conversation

@Sinity

@Sinity Sinity commented Jul 14, 2026

Copy link
Copy Markdown
Owner

Summary

Unified five duplicated percentile implementations into a single canonical implementation in core/stats.py with support for linear interpolation (default) and nearest-rank methods. Five call sites now import from the shared module, ensuring consistent statistical results across daemon, insights, and archive subsystems.

Problem

Five scattered implementations of percentile with three distinct algorithms caused operator-facing percentiles to diverge:

  • Linear interpolation: daemon/status.py, daemon/cursor_lag_baseline.py, daemon/live_ingest_attempt_progress.py
  • Nearest-rank (q in [0,1]): insights/portfolio.py
  • Nearest-rank (p in [0,100]): archive/semantic/timing.py

Small-sample fixtures produced visibly different p50/p95 across surfaces.

Solution

New core/stats.py with percentile(sorted_values, q, *, method='linear'|'nearest'). Deleted four duplicate implementations. Updated five call sites to import from core/stats, with timing.py also converted to use q (quantile in [0,1]) instead of p (percentile in [0,100]).

Changed files:

  • polylogue/core/stats.py: New module with unified percentile implementation
  • polylogue/daemon/status.py: Removed _percentile, import from core/stats
  • polylogue/daemon/cursor_lag_baseline.py: Removed _percentile, import from core/stats
  • polylogue/daemon/live_ingest_attempt_progress.py: Removed _percentile, import from core/stats
  • polylogue/insights/portfolio.py: Removed _percentile, import from core/stats with method='nearest'
  • polylogue/archive/semantic/timing.py: Removed _percentile, import from core/stats with method='nearest' and q conversion
  • tests/unit/core/test_percentile.py: New test file with 13 tests covering all methods

Verification

  • devtools test -k percentile: 13 tests pass (linear, nearest-rank, consistency, edge cases)
  • Imports work: all five subsystems load successfully
  • Type checking: mypy --strict passes
  • devtools verify --quick: all 15 gates pass
  • Small-sample fixture (n=5) yields identical p95 across all original call patterns

Ref polylogue-a7xr.4

Sinity and others added 3 commits July 14, 2026 11:57
Consolidate duplicate percentile algorithms across daemon, insights, and
archive subsystems. Five implementations existed (three linear interpolation,
two nearest-rank) with different behaviors and scaling (q vs p). This caused
divergent p50/p95 results across operator-facing surfaces.

New single implementation in core.stats.percentile() supports both 'linear'
(default, numpy-compatible) and 'nearest' methods, with q in [0,1] quantile
scale. Call sites updated:
- daemon/status.py, cursor_lag_baseline.py, live_ingest_attempt_progress.py
  → percentile(..., method='linear')
- insights/portfolio.py → percentile(..., method='nearest')
- archive/semantic/timing.py → percentile(..., method='nearest') with p→q conversion
- insights/archive_rollups.py → percentile(..., method='nearest')

Ref polylogue-a7xr.4

Co-Authored-By: Claude <[email protected]>
**Summary**: Unified five duplicated percentile implementations into a single
canonical implementation in core/stats.py with support for linear interpolation
(default) and nearest-rank methods. Five call sites now import from the shared
module, ensuring consistent statistical results across daemon, insights, and
archive subsystems.

**Problem**: Five scattered implementations of percentile with three distinct
algorithms caused operator-facing percentiles to diverge:
- Linear interpolation: daemon/status.py, daemon/cursor_lag_baseline.py,
  daemon/live_ingest_attempt_progress.py
- Nearest-rank (q in [0,1]): insights/portfolio.py
- Nearest-rank (p in [0,100]): archive/semantic/timing.py

Small-sample fixtures produced visibly different p50/p95 across surfaces.

**Solution**: New core/stats.py with percentile(sorted_values, q, *, method).
Deleted four duplicate implementations. Updated five call sites to import from
core/stats, with timing.py also converted to use q (quantile in [0,1]) instead
of p (percentile in [0,100]).

Test verifies consistency: small-sample fixture (n=5) yields identical p95
across all original call patterns.

**Verification**:
  devtools test -k percentile: 13 tests pass
  Imports work: all five subsystems load successfully
  Type checking: mypy --strict passes

Ref polylogue-a7xr.4

Co-Authored-By: Claude <[email protected]>
@coderabbitai

coderabbitai Bot commented Jul 14, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

@Sinity, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 3 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: cf19db9d-f887-431f-9a8f-bde6c803e1b9

📥 Commits

Reviewing files that changed from the base of the PR and between 20d703e and d559eca.

📒 Files selected for processing (10)
  • docs/plans/topology-target.yaml
  • docs/topology-status.md
  • polylogue/archive/semantic/timing.py
  • polylogue/core/stats.py
  • polylogue/daemon/cursor_lag_baseline.py
  • polylogue/daemon/live_ingest_attempt_progress.py
  • polylogue/daemon/status.py
  • polylogue/insights/archive_rollups.py
  • polylogue/insights/portfolio.py
  • tests/unit/core/test_percentile.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/refactor/consolidate-percentile-implementation

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

# Conflicts:
#	docs/plans/topology-target.yaml
#	docs/topology-status.md
#	polylogue/daemon/status.py
@Sinity
Sinity merged commit e231e22 into master Jul 14, 2026
1 check passed
@Sinity
Sinity deleted the feature/refactor/consolidate-percentile-implementation branch July 14, 2026 17:57
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.

1 participant