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

Aperta
#205 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 10 giorni

@TimeToBuildBob ci sta già lavorando.

Dal 19/11/2026.

  • #207 di @TimeToBuildBob — aperta

Valutazione

Difficoltà
4/5
Tempo stimato
1-2 giorni
Idoneità per principianti
45/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
65
Fork
74
Merge medio
7g 7h
PR unite (30g)
11

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di ActivityWatch/aw-core

Tutte le issue di ActivityWatch/aw-core

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.