chunk_events_by_key: gap/pulsetime check is broken (events[-1] is always the last event)
Maintainers usually reply within 10 days
Assessment
- Difficulty
- 4/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 45/100
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
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_keyhas 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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from ActivityWatch/aw-core
-
Peewee explicit event IDs can move events between bucketsPossibly taken @TimeToBuildBob claimed this 26 days ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 48/100
ActivityWatch/aw-core#195 ·
Maintainers usually reply within 10 days
-
Peewee range queries omit accepted events longer than 24 hoursPossibly taken @TimeToBuildBob claimed this 26 days ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 35/100
ActivityWatch/aw-core#194 · 1 comment ·
Maintainers usually reply within 10 days
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
ActivityWatch/aw-core#174 ·
Maintainers usually reply within 10 days
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
ActivityWatch/aw-core#78 · 4 comments ·
Maintainers usually reply within 10 days
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
ActivityWatch/aw-core#52 · 4 comments · 3 reactions ·
Maintainers usually reply within 10 days
All issues in ActivityWatch/aw-core
Similar issues
-
bug docs
Difficulty 1/5 Under an hour Newbie friendliness 85/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
ActivityWatch/docs#223 ·
-
[Bug]: OpenCode v2 skips an assistant message that gains text after an earlier capturePossibly taken @jianyx1 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
volcengine/OpenViking#8354 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
ARTPARK-SAHAI-ORG/calibrate-frontend#842 ·
Maintainers usually reply within 1 day
-
comp/tools P2 tool/skills type/bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
NousResearch/hermes-agent#159541 ·
Maintainers usually reply within 1 day