Skip to content

fix(transform): sort by (timestamp, duration) and add flood idempotency tests - #145

Merged
ErikBjare merged 1 commit into
ActivityWatch:masterfrom
TimeToBuildBob:fix/flood-sort-and-tests
Jul 22, 2026
Merged

fix(transform): sort by (timestamp, duration) and add flood idempotency tests#145
ErikBjare merged 1 commit into
ActivityWatch:masterfrom
TimeToBuildBob:fix/flood-sort-and-tests

Conversation

@TimeToBuildBob

Copy link
Copy Markdown
Contributor

Ports two remaining improvements from #105 that weren't included in #143.

Changes

1. Sort key: (timestamp, duration) instead of just timestamp

When multiple events share the same timestamp, shorter events now sort first. This gives a deterministic processing order for same-timestamp inputs and aligns with aw-server-rust's behavior (its sort_by_timestamp processes shorter events first when timestamps are equal).

2. Two new tests

Both tests pass on the current master (no other code changes needed).

aw-server-rust spec comparison

Per Erik's request, I compared the Python implementation against aw-server-rust's flood.rs. The sort key change above aligns them. One notable remaining divergence:

Positive gap, differing data: aw-server-rust does "meet in the middle" — each event extends by gap/2. The Python implementation does "longer wins" — the longer event fills the entire gap. The normalization pass from #143 means both implementations produce overlap-free output, but the gap-split point differs. Leaving this for a separate discussion since it's a more significant behaviour change.

…cy tests

Port two improvements from ActivityWatch#105 that weren't included in ActivityWatch#143:

1. Sort key change: sort by (timestamp, duration) instead of just timestamp.
   When multiple events share the same timestamp, shorter events now sort first.
   This matches aw-server-rust's sort_by_timestamp behavior and gives
   deterministic, spec-aligned ordering.

2. Two new tests from ActivityWatch#105:
   - test_flood_idempotent: verifies repeated flood() calls produce the same
     result (important correctness property)
   - test_flood_unsafe_gap: verifies overlapping differing-data events don't
     double-count time (validates the normalization pass from ActivityWatch#143)
@greptile-apps

greptile-apps Bot commented Jul 22, 2026

Copy link
Copy Markdown

Greptile Summary

This PR makes flood processing deterministic for events with equal timestamps. The main changes are:

  • Sort equal-timestamp events by ascending duration.
  • Add a test for repeated-call idempotency.
  • Add a test for avoiding double-counted overlapping time.

Confidence Score: 5/5

This looks safe to merge.

  • No blocking issues found in the changed code.
  • Equal timestamps now have deterministic ordering without breaking overlap normalization.
  • The added tests match the existing event equality and duration behavior.

Important Files Changed

Filename Overview
aw_transform/flood.py Adds duration as the secondary event sort key; downstream normalization remains consistent with this ordering.
tests/test_flood.py Adds focused tests for idempotency and duration conservation across overlapping events.

Reviews (1): Last reviewed commit: "fix(transform): sort by (timestamp, dura..." | Re-trigger Greptile

@ErikBjare
ErikBjare merged commit b706a50 into ActivityWatch:master Jul 22, 2026
5 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