List events: cap pages at 48 KB; keep sub-second range ends
Review round 1 on calendar_list_events. Pages were bounded by count only, and two hundred events with valid 200-byte titles and locations already exceed the tool frame. Rows are now serialized as they are added and a page stops before 48 KB; next points just after the last included event. to.Unix() truncated fractional seconds, so an event starting at 11:00:00 was dropped from a range ending 11:00:00.5, and the last civil day was found by subtracting a whole second. Timed overlap now compares full- precision instants, and the last day is the one holding to unless to is exactly its start. Regression tests for both; the 200-count test now walks the cursor chain. Tests split to stay under the line cap. Spec amendment updated.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+180
−49
docs/superpowers/specs/2026-09-02-calendar-design.md+5 −3internal/calendar/agent_list.go+34 −11internal/calendar/agent_list_bounds_test.go+120 −0internal/calendar/agent_list_events_test.go+21 −35
CI passed — run details
CI log
=== vet === go vet ./... go vet -tags browser ./internal/calendartest/ === fmt === === test === go test ./... ? amadan.net/oficina/calendar/cmd/calendar [no test files] ? amadan.net/oficina/calendar/cmd/calendar-contract [no test files] ok amadan.net/oficina/calendar/internal/calendar 25.506s ? amadan.net/oficina/calendar/internal/calendar/icons [no test files] ? amadan.net/oficina/calendar/internal/calendar/keymailtest [no test files] ? amadan.net/oficina/calendar/internal/calendar/seal [no test files] ? amadan.net/oficina/calendar/internal/calendar/teamstate [no test files] ok amadan.net/oficina/calendar/internal/calendartest 3.869s ok amadan.net/oficina/calendar/internal/guardstest 0.086s ? amadan.net/oficina/calendar/scripts/writeestilo [no test files] === migration-check === go tool rastrillo migration check rastrillo migration check: models and migrations agree
diff --git a/docs/superpowers/specs/2026-09-02-calendar-design.md b/docs/superpowers/specs/2026-09-02-calendar-design.md| index 3ca030d..673494c 100644 |
| --- a/docs/superpowers/specs/2026-09-02-calendar-design.md |
| +++ b/docs/superpowers/specs/2026-09-02-calendar-design.md |
| @@ -721,9 +721,11 @@ calendar or the call is a 404; an optional `query` matches the opened title or |
| location in memory, never in SQL. Timed events report RFC3339 instants in the |
| event's own zone. All-day events report dates with an exclusive end and are |
| matched against the person's own civil days, not their UTC index. Each |
| -occurrence of a series is its own row under the series ID. A page holds at |
| -most 200 events; `next` is an offset cursor into the same filtered, ordered |
| -list. Attendees are opened for the page only. |
| +occurrence of a series is its own row under the series ID. Range ends are |
| +compared at full precision, fractional seconds included. A page holds at |
| +most 200 events and at most 48 KB of serialized events, so it fits the tool |
| +frame; `next` is an offset cursor, just after the last included event, into |
| +the same filtered, ordered list. Attendees are opened for the page only. |
| `calendar_create_event` takes optional `attendees`, and |
| `calendar_update_event` takes `changes.attendees` as a whole-list |
diff --git a/internal/calendar/agent_list.go b/internal/calendar/agent_list.go| index 9af9df9..2e6721d 100644 |
| --- a/internal/calendar/agent_list.go |
| +++ b/internal/calendar/agent_list.go |
| @@ -10,9 +10,15 @@ import ( |
| access "amadan.net/oficina/agent-access.git" |
| ) |
| -// agentListPage bounds one calendar_list_events response. A month of a |
| -// busy team is more than this; the cursor carries the rest. |
| -const agentListPage = 200 |
| +// agentListPage bounds one calendar_list_events response by count and |
| +// agentListBytes by its serialized size, which keeps a page inside the |
| +// tool frame: two hundred events with valid 200-byte titles and |
| +// locations are already far past it. A month of a busy team is more than |
| +// one page; the cursor carries the rest. |
| +const ( |
| + agentListPage = 200 |
| + agentListBytes = 48 * 1024 |
| +) |
| func (a *App) agentListEventsOperation(team string) access.Operation { |
| listed := agentObject(map[string]any{ |
| @@ -21,7 +27,7 @@ func (a *App) agentListEventsOperation(team string) access.Operation { |
| "status": agentString(32), "kind": agentString(32), "recurring": map[string]any{"type": "boolean"}, |
| "attendees": map[string]any{"type": "array", "items": agentString(agentAttendeeLen)}, "url": agentString(2048), |
| }, "id", "calendar_id", "calendar", "title", "location", "all_day", "start", "end", "tz", "status", "kind", "recurring", "attendees", "url") |
| - return access.Operation{Name: "calendar_list_events", Description: "List the events the person can currently see that overlap a half-open RFC3339 range [from, to) with explicit offsets, at most 31 days, in start order. Timed events give RFC3339 start/end in the event's own zone; all-day events give YYYY-MM-DD dates with an exclusive end, matched against the person's own days. Each occurrence of a recurring event is listed separately under the series id. Optional calendar_id narrows to one visible calendar; optional query matches title or location, case-insensitively. At most limit (default and maximum 200) events per call; pass next back as cursor, with the same other arguments, for the rest. Attendees are addresses recorded on the event; Calendar has never emailed them.", Scope: "calendar:read", ReadOnly: true, Idempotent: true, |
| + return access.Operation{Name: "calendar_list_events", Description: "List the events the person can currently see that overlap a half-open RFC3339 range [from, to) with explicit offsets, at most 31 days, in start order. Timed events give RFC3339 start/end in the event's own zone; all-day events give YYYY-MM-DD dates with an exclusive end, matched against the person's own days. Each occurrence of a recurring event is listed separately under the series id. Optional calendar_id narrows to one visible calendar; optional query matches title or location, case-insensitively. At most limit (default and maximum 200) events per call, fewer when they are large; pass next back as cursor, with the same other arguments, for the rest. Attendees are addresses recorded on the event; Calendar has never emailed them.", Scope: "calendar:read", ReadOnly: true, Idempotent: true, |
| Input: agentInput(map[string]any{"from": agentString(35), "to": agentString(35), "calendar_id": agentID(), "query": agentString(200), |
| "limit": map[string]any{"type": "integer", "minimum": 1, "maximum": agentListPage}, "cursor": agentString(12)}, "from", "to"), |
| Output: agentObject(map[string]any{"events": map[string]any{"type": "array", "maxItems": agentListPage, "items": listed}, "next": agentString(12)}, "events"), |
| @@ -78,7 +84,14 @@ func (a *App) agentListEvents(ctx context.Context, _ access.Principal, p Person, |
| if err != nil { |
| return nil, err |
| } |
| - lowDay, highDay := civilDate(from.In(personLoc)), civilDate(to.Add(-time.Second).In(personLoc)) |
| + // The last civil day is the one holding the last instant before `to`: |
| + // the day `to` falls in, unless `to` is exactly that day's start. |
| + // Compared at full precision, so 00:00:00.5 still reaches into the day. |
| + end := to.In(personLoc) |
| + lowDay, highDay := civilDate(from.In(personLoc)), civilDate(end) |
| + if to.Equal(time.Date(end.Year(), end.Month(), end.Day(), 0, 0, 0, 0, personLoc)) { |
| + highDay = highDay.AddDate(0, 0, -1) |
| + } |
| query := strings.ToLower(strings.TrimSpace(in.Query)) |
| matched := []EventView{} |
| for _, e := range events { |
| @@ -90,7 +103,7 @@ func (a *App) agentListEvents(ctx context.Context, _ access.Principal, p Person, |
| if last.Before(lowDay) || first.After(highDay) { |
| continue |
| } |
| - } else if e.OccEndUTC <= from.Unix() || e.OccStartUTC >= to.Unix() { |
| + } else if !time.Unix(e.OccEndUTC, 0).After(from) || !time.Unix(e.OccStartUTC, 0).Before(to) { |
| continue |
| } |
| if query != "" && !strings.Contains(strings.ToLower(e.Title), query) && !strings.Contains(strings.ToLower(e.Location), query) { |
| @@ -105,9 +118,9 @@ func (a *App) agentListEvents(ctx context.Context, _ access.Principal, p Person, |
| if err := a.store.AttachAttendees(ctx, page); err != nil { |
| return nil, err |
| } |
| - out := []map[string]any{} |
| + out := []json.RawMessage{} |
| + size := len(`{"events":[],"next":"000000000000"}`) |
| for _, e := range page { |
| - captureAgentCalendar(ctx, e.CalendarID) |
| start, end, tz := "", "", e.TZ |
| if e.AllDay { |
| first, last := allDayDates(e) |
| @@ -123,13 +136,23 @@ func (a *App) agentListEvents(ctx context.Context, _ access.Principal, p Person, |
| if attendees == nil { |
| attendees = []string{} |
| } |
| - out = append(out, map[string]any{"id": occurrenceOwner(e), "calendar_id": e.CalendarID, "calendar": e.CalendarName, "title": e.Title, "location": e.Location, |
| + row, err := json.Marshal(map[string]any{"id": occurrenceOwner(e), "calendar_id": e.CalendarID, "calendar": e.CalendarName, "title": e.Title, "location": e.Location, |
| "all_day": e.AllDay, "start": start, "end": end, "tz": tz, "status": e.Status, "kind": e.Kind, "recurring": e.OccurrenceLocal != "", |
| "attendees": attendees, "url": a.cfg.Origin + eventHref(e)}) |
| + if err != nil { |
| + return nil, err |
| + } |
| + // One event always fits: its fields are bounded far below the budget. |
| + if len(out) > 0 && size+len(row)+1 > agentListBytes { |
| + break |
| + } |
| + size += len(row) + 1 |
| + out = append(out, row) |
| + captureAgentCalendar(ctx, e.CalendarID) |
| } |
| result := map[string]any{"events": out} |
| - if offset+limit < len(matched) { |
| - result["next"] = strconv.Itoa(offset + limit) |
| + if offset+len(out) < len(matched) { |
| + result["next"] = strconv.Itoa(offset + len(out)) |
| } |
| return result, nil |
| } |
diff --git a/internal/calendar/agent_list_bounds_test.go b/internal/calendar/agent_list_bounds_test.go| new file mode 100644 |
| index 0000000..4716425 |
| --- /dev/null |
| +++ b/internal/calendar/agent_list_bounds_test.go |
| @@ -0,0 +1,120 @@ |
| +package calendar |
| + |
| +import ( |
| + "encoding/json" |
| + "fmt" |
| + "slices" |
| + "strings" |
| + "testing" |
| + "time" |
| +) |
| + |
| +func TestAgentListEventsPagesStayUnderTheFrameBudget(t *testing.T) { |
| + f, _, person, cal := linkedCalendarAgent(t) |
| + day := time.Date(2027, 3, 10, 0, 0, 0, 0, time.UTC) |
| + // Valid at the limits: 200-byte title and location, with characters |
| + // JSON escapes, and attendees on every event. |
| + long := func(i int, tag string) string { |
| + s := fmt.Sprintf("%s%03d<&>", tag, i) |
| + return s + strings.Repeat("x", MaxTitleLen-len(s)) |
| + } |
| + for i := range 200 { |
| + start := day.Add(time.Duration(i) * time.Minute) |
| + e, err := f.app.store.CreateEvent(t.Context(), person.ID, EventInput{CalendarID: cal, Title: long(i, "t"), Location: long(i, "l"), StartLocal: start.Format(LayoutLocal), EndLocal: start.Add(time.Minute).Format(LayoutLocal), TZ: "UTC"}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if err = f.app.store.SetAttendees(t.Context(), e.ID, []string{fmt.Sprintf("first.attendee.%03d@example.com", i), fmt.Sprintf("second.attendee.%03d@example.com", i)}); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + window := map[string]any{"team": "acme", "from": day.Format(time.RFC3339), "to": day.Add(24 * time.Hour).Format(time.RFC3339)} |
| + seen, pages := []string{}, 0 |
| + for { |
| + w := calendarAgentCall(f.app, "calendar_list_events", window) |
| + if w.Code != 200 { |
| + t.Fatal(w.Code, w.Body.String()) |
| + } |
| + if w.Body.Len() > 48*1024 { |
| + t.Fatal("page over budget", pages, w.Body.Len()) |
| + } |
| + var out agentListed |
| + if err := json.Unmarshal(w.Body.Bytes(), &out); err != nil || len(out.Events) == 0 { |
| + t.Fatal("empty page", err) |
| + } |
| + for _, e := range out.Events { |
| + if len(e.Attendees) != 2 { |
| + t.Fatal("attendees", e.Attendees) |
| + } |
| + seen = append(seen, e.Title) |
| + } |
| + pages++ |
| + if out.Next == "" { |
| + break |
| + } |
| + window["cursor"] = out.Next |
| + } |
| + if pages < 2 || len(seen) != 200 { |
| + t.Fatal("pages", pages, len(seen)) |
| + } |
| + for i, title := range seen { |
| + if title != long(i, "t") { |
| + t.Fatal("skipped or repeated event", i) |
| + } |
| + } |
| +} |
| + |
| +func TestAgentListEventsKeepsSubSecondBoundaries(t *testing.T) { |
| + f, _, person, cal := linkedCalendarAgent(t) |
| + if err := f.app.db.G.Model(&Person{}).Where("id=?", person.ID).Update("zone", "UTC").Error; err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, in := range []EventInput{ |
| + {CalendarID: cal, Title: "Eleven", StartLocal: "2027-03-10T11:00", EndLocal: "2027-03-10T12:00", TZ: "UTC"}, |
| + {CalendarID: cal, Title: "Thursday", AllDay: true, StartLocal: "2027-03-11", EndLocal: "2027-03-12"}, |
| + } { |
| + if _, err := f.app.store.CreateEvent(t.Context(), person.ID, in); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + for _, tc := range []struct { |
| + from, to string |
| + want []string |
| + }{ |
| + {"2027-03-10T10:00:00Z", "2027-03-10T11:00:00.5Z", []string{"Eleven"}}, |
| + {"2027-03-10T10:00:00Z", "2027-03-10T11:00:00Z", []string{}}, |
| + {"2027-03-10T12:30:00Z", "2027-03-11T00:00:00.5Z", []string{"Thursday"}}, |
| + {"2027-03-10T12:30:00Z", "2027-03-11T00:00:00Z", []string{}}, |
| + } { |
| + if got := listedTitles(listAgentEvents(t, f.app, map[string]any{"from": tc.from, "to": tc.to})); !slices.Equal(got, tc.want) { |
| + t.Fatal(tc.from, tc.to, got) |
| + } |
| + } |
| +} |
| + |
| +func TestAgentListEventsContract(t *testing.T) { |
| + ops, err := AgentContracts() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + found := false |
| + for _, op := range ops { |
| + if op.Name == "calendar_list_events" { |
| + found = true |
| + if !op.ReadOnly || !op.Idempotent || op.Destructive || op.Scope != "calendar:read" { |
| + t.Fatal("list flags", op) |
| + } |
| + } |
| + if op.Name == "calendar_create_event" || op.Name == "calendar_update_event" { |
| + if !strings.Contains(op.Description, "attendees") || !strings.Contains(op.Description, "never invited") || !strings.Contains(string(op.Input), `"attendees"`) { |
| + t.Fatal("attendee contract", op.Name, op.Description) |
| + } |
| + if strings.Contains(string(op.Input), "send_invitations") || !strings.Contains(string(op.Output), `"invitations_sent":{"const":false}`) { |
| + t.Fatal("invitation contract", op.Name) |
| + } |
| + } |
| + } |
| + if !found { |
| + t.Fatal("calendar_list_events missing") |
| + } |
| +} |
diff --git a/internal/calendar/agent_list_events_test.go b/internal/calendar/agent_list_events_test.go| index 921f65a..4d0fc81 100644 |
| --- a/internal/calendar/agent_list_events_test.go |
| +++ b/internal/calendar/agent_list_events_test.go |
| @@ -147,7 +147,7 @@ func TestAgentListEventsRefusesUnboundedInput(t *testing.T) { |
| } |
| } |
| -func TestAgentListEventsPagesAtTwoHundred(t *testing.T) { |
| +func TestAgentListEventsPagesThroughEverything(t *testing.T) { |
| f, _, person, cal := linkedCalendarAgent(t) |
| day := time.Date(2027, 3, 10, 0, 0, 0, 0, time.UTC) |
| for i := range 205 { |
| @@ -157,14 +157,27 @@ func TestAgentListEventsPagesAtTwoHundred(t *testing.T) { |
| } |
| } |
| window := map[string]any{"from": day.Format(time.RFC3339), "to": day.Add(24 * time.Hour).Format(time.RFC3339)} |
| - first := listAgentEvents(t, f.app, window) |
| - if len(first.Events) != 200 || first.Next == "" || first.Events[0].Title != "e000" || first.Events[199].Title != "e199" { |
| - t.Fatal("first page", len(first.Events), first.Next) |
| + // Whichever bound ends a page — 200 events or the byte budget — the |
| + // cursor chain covers every event once, in order. |
| + all, page := []string{}, listAgentEvents(t, f.app, window) |
| + if len(page.Events) > 200 || page.Next == "" || page.Events[0].Title != "e000" { |
| + t.Fatal("first page", len(page.Events), page.Next) |
| + } |
| + for { |
| + all = append(all, listedTitles(page)...) |
| + if page.Next == "" { |
| + break |
| + } |
| + window["cursor"] = page.Next |
| + page = listAgentEvents(t, f.app, window) |
| + } |
| + if len(all) != 205 { |
| + t.Fatal("chain", len(all)) |
| } |
| - window["cursor"] = first.Next |
| - second := listAgentEvents(t, f.app, window) |
| - if !slices.Equal(listedTitles(second), []string{"e200", "e201", "e202", "e203", "e204"}) || second.Next != "" { |
| - t.Fatal("second page", listedTitles(second), second.Next) |
| + for i, title := range all { |
| + if title != fmt.Sprintf("e%03d", i) { |
| + t.Fatal("skipped or repeated", i, title) |
| + } |
| } |
| last := listAgentEvents(t, f.app, map[string]any{"from": window["from"], "to": window["to"], "limit": 5, "cursor": "200"}) |
| if len(last.Events) != 5 || last.Next != "" { |
| @@ -270,30 +283,3 @@ func TestAgentListEventsFollowsCalendarGrants(t *testing.T) { |
| t.Fatal("read scope", w.Code) |
| } |
| } |
| - |
| -func TestAgentListEventsContract(t *testing.T) { |
| - ops, err := AgentContracts() |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - found := false |
| - for _, op := range ops { |
| - if op.Name == "calendar_list_events" { |
| - found = true |
| - if !op.ReadOnly || !op.Idempotent || op.Destructive || op.Scope != "calendar:read" { |
| - t.Fatal("list flags", op) |
| - } |
| - } |
| - if op.Name == "calendar_create_event" || op.Name == "calendar_update_event" { |
| - if !strings.Contains(op.Description, "attendees") || !strings.Contains(op.Description, "never invited") || !strings.Contains(string(op.Input), `"attendees"`) { |
| - t.Fatal("attendee contract", op.Name, op.Description) |
| - } |
| - if strings.Contains(string(op.Input), "send_invitations") || !strings.Contains(string(op.Output), `"invitations_sent":{"const":false}`) { |
| - t.Fatal("invitation contract", op.Name) |
| - } |
| - } |
| - } |
| - if !found { |
| - t.Fatal("calendar_list_events missing") |
| - } |
| -} |