chunk_events_by_key: gap/pulsetime check is broken (events[-1] is always the last event)

Open
#205 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 10 days

@TimeToBuildBob is already working on this.

Since Nov 19, 2026.

  • #207 by @TimeToBuildBob — open

Assessment

Difficulty
4/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
python
Domain
backend

Research direction

Read aw_transform/chunk_events_by_key.py and the existing test_chunk_events_by_key tests first; reproduce the one-hour gap case from the issue. The issue leaves open whether to fix pulse semantics or remove the check to match Rust, so get that behavior decision settled before changing code. Done means the chosen behavior is covered by a gap test and the Python implementation matches that decision.

Written by the indexing model from the issue text.

Description

bug

The gap check in chunk_events_by_key has never worked — events[-1] always refers to the last event of the input list, not the previous event, so the timediff computed is garbage and unrelated events get merged across arbitrarily large gaps.

Repro (aw-core master, 4496e2e):

from datetime import datetime, timezone, timedelta
from aw_core.models import Event
from aw_transform.chunk_events_by_key import chunk_events_by_key

t0 = datetime(2026, 1, 1, 10, 0, 0, tzinfo=timezone.utc)
events = [
    Event(timestamp=t0,                      duration=timedelta(seconds=10), data={"app": "A"}),
    Event(timestamp=t0 + timedelta(hours=1),  duration=timedelta(seconds=10), data={"app": "A"}),
]
chunks = chunk_events_by_key(events, "app", pulsetime=5)
assert len(chunks) == 1          # BUG: gap is 1 hour, pulse is 5s
assert chunks[0].duration == timedelta(seconds=20)

Two events one hour apart get chunked into one with pulsetime=5.

The line at fault:

timediff = event.timestamp - (events[-1].timestamp + events[-1].duration)

events[-1] is the last event of the whole input, not the one before event, so timediff is meaningless unless event happens to be the final element. This has been there since the pulsetime parameter was introduced in 59e6912 (2018, "Cleaned up sum_durations and fixed pulsetime in chunk_events_by_key").

Fun fact: since sort_by_timestamp is normally applied first and input from actual watchers is usually chronological, events[-1] at the last iteration equals the previous event, so the bug hides in normal daily use and only fires for backfilled/reordered data... except it also means the pulse window is enforced only for the very last pair of events in the list. The variant that shows it is any same-key pair with a large gap that is not at the tail (or missing keys after it, or list end).

Also worth noting:

  • aw-server-rust's chunk_events_by_key has no pulsetime at all and deliberately merges across gaps, so Python and Rust already behave differently here
  • the whole transform is deprecated (see #1466) with no drop-in replacement, so whether to fix the check or remove the function entirely is genuinely ambiguous — happy to PR either way:

Option A (minimal fix, keeps pulse semantics):

timediff = event.timestamp - (chunked_events[-1].timestamp + chunked_events[-1].duration)

Option B: drop the pulse check entirely and match Rust (merging across gaps is arguably the "spec" now, given it's what both the Rust implementation does and what the buggy Python code accidentally does in sorted-input practice).

Existing tests don't cover gaps (all three events in test_chunk_events_by_key share the same timestamp), so whichever direction is chosen I'd add a gap test.

Dominant language
Python
Stars
65
Forks
74
Avg merge
7d 7h
Merged PRs (30d)
11

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ActivityWatch/aw-core

All issues in ActivityWatch/aw-core

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.