Skip to content

fix: handle zero-duration clusters in diff_sessions (closes #79)#87

Closed
conorluddy wants to merge 1 commit into
mainfrom
fix/79-zero-duration-drift
Closed

fix: handle zero-duration clusters in diff_sessions (closes #79)#87
conorluddy wants to merge 1 commit into
mainfrom
fix/79-zero-duration-drift

Conversation

@conorluddy

Copy link
Copy Markdown
Owner

Closes #79.

Problem

diff_sessions() had a divide-by-zero guard that did a bare continue:

if ca.max_duration_ms == 0:
    continue

A zero-duration cluster in session A simply vanished from the diff. Worse, a real regression — a previously-silent cluster (0 ms) that started hanging (e.g. 800 ms in B) — was dropped entirely instead of being flagged as drift.

Fix

Replace with explicit 4-way branching:

Case Result
A == 0, B == 0 stable++
A == 0, B > 0 drift, delta_pct = float("inf") (worsened, "new signal")
A > 0, B == 0 drift, delta_pct = -100.0 (fully improved)
A > 0, B > 0 existing percentage logic

The inf delta is special-cased in format_diff to render as new rather than +inf%.

Tests

5 new in tests/test_diff.py:

  • test_diff_zero_to_zero_counts_as_stable
  • test_diff_zero_to_nonzero_is_drift_with_inf_delta
  • test_diff_nonzero_to_zero_is_drift_with_minus_one_hundred
  • test_diff_nonzero_unchanged_still_uses_threshold (regression guard)
  • test_format_diff_renders_inf_delta_as_new

A small helper _zero_dur_summary() builds SessionSummary instances with controlled max_duration_ms per cluster — keeps the test isolated from the event-aggregation pipeline.

Verification

ruff check: All checks passed
black --check: 42 files would be left unchanged
python -m pytest tests/: 93 passed (was 88)
pytest tests/: 93 passed

Closes #79.

The divide-by-zero guard in diff_sessions() used a bare `continue`,
silently dropping clusters whose A-side max_duration_ms was 0. A real
regression where a previously-silent cluster started hanging (0 → N ms)
was lost from the diff entirely.

Replace with explicit 4-way branching:

- both zero      → stable
- A=0, B>0       → drift, delta_pct = +inf (treated as worsened)
- A>0, B=0       → drift, delta_pct = -100.0 (fully improved)
- both nonzero   → existing percentage logic, unchanged

format_diff() renders the inf delta as `new` instead of `+inf%` for
readability.

Tests: 5 new in tests/test_diff.py covering each branch + a regression
guard that the standard nonzero path still uses the drift threshold,
plus format_diff render assertion for the inf case.

Verification: ruff/black clean; 93 passed (88 → 93).
@conorluddy

Copy link
Copy Markdown
Owner Author

Superseded by #90 — same commits combined into one PR.

@conorluddy conorluddy closed this May 23, 2026
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.

bug: diff_sessions() silently drops zero-duration clusters

1 participant