package autotrack import ( "os" "time" "ticktock/internal/config" ) // Run executes the poll loop: sample → Classify → Step → WriteSegment → // sleep, until a signal arrives on sigc; then it flushes the open segment and // returns (parity with the Python daemon's SIGTERM/SIGINT flush). sample is // injected (ReadSample in production) so the loop is testable. func Run(dataDir string, tr config.Tracking, sample func() Sample, sigc <-chan os.Signal) { var cur *Segment var curKey Key poll := time.Duration(tr.PollSecs) * time.Second for { now := time.Now() s := sample() hard := s.Locked || Away(s.Raw) ctx := Classify(s.Idle, s.Raw, tr.IdleGraceSecs, s.Locked) var emitted *Segment cur, curKey, emitted = Step(cur, curKey, now, s.Idle, ctx, tr.IdleGraceSecs, hard) if emitted != nil { _ = WriteSegment(dataDir, emitted, tr.MinSecs) // an IO hiccup must not kill the loop } select { case <-sigc: if cur != nil { closed := *cur closed.End = time.Now() _ = WriteSegment(dataDir, &closed, tr.MinSecs) } return case <-time.After(poll): } } }