diff --git a/internal/sessions/hours.go b/internal/sessions/hours.go new file mode 100644 index 000000000..7d4759977 --- /dev/null +++ b/internal/sessions/hours.go @@ -0,0 +1,81 @@ +package sessions + +import ( + "encoding/json" + "slices" + "strconv" +) + +// hours is a day's Active by the local hour it was put on, kept as only +// the hours that have some: of the owner's 19,191 days, 13,406 had hours, +// and 19,107 hours had any, so 24 int64s a day (192 bytes) were ~2.6 MB of +// zeros held for good. Each entry is hour<> hourShift) } +func msOf(e uint64) int64 { return int64(e & (1< h: + *hs = slices.Insert(*hs, i, hourEntry(h, ms)) + return + } + } + *hs = append(*hs, hourEntry(h, ms)) +} + +func (hs hours) MarshalJSON() ([]byte, error) { + var all [24]int64 + for _, e := range hs { + all[hourOf(e)] = msOf(e) + } + b := make([]byte, 0, 2+24*2) + b = append(b, '[') + for i, ms := range all { + if i > 0 { + b = append(b, ',') + } + b = strconv.AppendInt(b, ms, 10) + } + return append(b, ']'), nil +} + +func (hs *hours) UnmarshalJSON(b []byte) error { + var all []int64 + if err := json.Unmarshal(b, &all); err != nil { + return err + } + if all == nil { + *hs = nil + return nil + } + n := 0 + for _, ms := range all[:min(len(all), 24)] { + if ms > 0 { + n++ + } + } + out := make(hours, 0, max(n, 1)) + for h, ms := range all[:min(len(all), 24)] { + if ms > 0 { + out = append(out, hourEntry(h, ms)) + } + } + if len(out) == 0 { + out = append(out, hourEntry(0, 0)) + } + *hs = out + return nil +} diff --git a/internal/sessions/hours_test.go b/internal/sessions/hours_test.go new file mode 100644 index 000000000..98eca878e --- /dev/null +++ b/internal/sessions/hours_test.go @@ -0,0 +1,54 @@ +package sessions + +import ( + "encoding/json" + "testing" +) + +// TestHoursKeepTheCacheShape: a day's hours read from the cache's 24 +// numbers are written back as the same 24, an hour of 0ms included, and +// hold only the hours that have some. +func TestHoursKeepTheCacheShape(t *testing.T) { + for _, src := range []string{ + `{"h":[0,0,0,0,0,0,0,0,0,1500,0,0,0,0,0,0,0,0,0,0,0,0,0,42]}`, + `{"h":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}`, + `{"a":3}`, + } { + var d day + if err := json.Unmarshal([]byte(src), &d); err != nil { + t.Fatal(err) + } + if len(d.Hours) > 2 || cap(d.Hours) > 2 { + t.Fatalf("%s: %d entries (cap %d)", src, len(d.Hours), cap(d.Hours)) + } + b, _ := json.Marshal(&d) + if string(b) != src { + t.Fatalf("%s came back as %s", src, b) + } + } +} + +// TestHoursAddInOrder: a day's hours add up per hour, in hour order, +// as the 24 numbers did. +func TestHoursAddInOrder(t *testing.T) { + var hs hours + var want [24]int64 + for i, h := range []int{9, 3, 23, 9, 0, 3, 12} { + ms := int64(i*1000 + 7) + hs.add(h, ms) + want[h] += ms + } + hs.add(5, 0) + var got [24]int64 + last := -1 + for _, e := range hs { + if hourOf(e) <= last { + t.Fatalf("hours out of order: %v", hs) + } + last = hourOf(e) + got[hourOf(e)] = msOf(e) + } + if got != want || len(hs) != 6 { + t.Fatalf("got %v (%d entries), want %v", got, len(hs), want) + } +} diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go index 946f84f0d..c155d80fa 100644 --- a/internal/sessions/sessions.go +++ b/internal/sessions/sessions.go @@ -363,8 +363,8 @@ type day struct { // it runs while its session's own file goes on. Active int64 `json:"a,omitempty"` // Hours is Active again, by the local hour of the day it was put on - // (24 of them, or none) - Hours []int64 `json:"h,omitempty"` + // (only the hours that have some, or none; 24 numbers in the cache) + Hours hours `json:"h,omitempty"` // Prompts are the messages typed in the session's own file, Replies // the agent's messages back (Claude Code's, Codex's, Pi's and omp's) Prompts int `json:"u,omitempty"` @@ -469,10 +469,7 @@ func (s *state) saw(t time.Time, main bool) { if gap := t.Sub(s.Last); main && !s.Last.IsZero() && gap < idleGap { d := s.day(dateOf(t)) d.Active += gap.Milliseconds() - if d.Hours == nil { - d.Hours = make([]int64, 24) - } - d.Hours[t.In(time.Local).Hour()] += gap.Milliseconds() + d.Hours.add(t.In(time.Local).Hour(), gap.Milliseconds()) } s.Last = t } @@ -505,9 +502,7 @@ func (s *state) clone() *state { for i, e := range c.Days { v := e.v d := &day{Active: v.Active, Models: slices.Clone(v.Models), Prompts: v.Prompts, Replies: v.Replies} - if v.Hours != nil { - d.Hours = append([]int64(nil), v.Hours...) - } + d.Hours = slices.Clone(v.Hours) d.Tools, d.Skills = slices.Clone(v.Tools), slices.Clone(v.Skills) c.Days[i].v = d } diff --git a/internal/sessions/stats.go b/internal/sessions/stats.go index d67a4127d..83f49a257 100644 --- a/internal/sessions/stats.go +++ b/internal/sessions/stats.go @@ -422,12 +422,12 @@ func statsOfRange(days int, today time.Time, files []file, copies map[string]str act[k] = a } a.ms += d.Active - if len(d.Hours) == 24 { + if d.Hours != nil { if a.hours == nil { a.hours = make([]int64, 24) } - for h, ms := range d.Hours { - a.hours[h] += ms + for _, e := range d.Hours { + a.hours[hourOf(e)] += msOf(e) } } dates[date] = true