List events: bound attendees and text per event so every page fits
Review round 2 on calendar_list_events. The first event on a page was exempt from the 48 KB cap, and stored attendee lists are unbounded: quick add, import and booking store every address they find, so one event could still produce an oversized response. Responses now carry at most 50 attendee addresses or 4 KB of them per event, in stored order, with attendees_more counting the rest; an address longer than 254 bytes is left out and counted. Titles, locations and calendar names are clipped to the schema's lengths. With every field bounded, the cap applies to the first event too, and a row that still did not fit would be refused rather than sent. calendar_get_event carries attendees the same way. Regression test: one event with 2,001 attendees (one over-long) and a 40,000-character title, through both operations.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
6 files changed,
+132
−19
docs/superpowers/specs/2026-09-02-calendar-design.md+5 −0internal/calendar/agent_attendees.go+51 −0internal/calendar/agent_list.go+13 −14internal/calendar/agent_list_bounds_test.go+58 −0internal/calendar/agent_operations.go+4 −4internal/calendar/agent_read.go+1 −1
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 26.239s ? 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.901s ok amadan.net/oficina/calendar/internal/guardstest 0.083s ? 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 673494c..8a9f044 100644 |
| --- a/docs/superpowers/specs/2026-09-02-calendar-design.md |
| +++ b/docs/superpowers/specs/2026-09-02-calendar-design.md |
| @@ -726,6 +726,11 @@ 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. |
| +Stored attendee lists have no bound (quick add, import and booking store what |
| +they find), so a response carries at most 50 addresses or 4 KB of them per |
| +event, with `attendees_more` counting the rest, and clips titles, locations |
| +and calendar names to the schema's lengths; the 48 KB cap then holds for every |
| +event, the first included. `calendar_get_event` carries attendees the same way. |
| `calendar_create_event` takes optional `attendees`, and |
| `calendar_update_event` takes `changes.attendees` as a whole-list |
diff --git a/internal/calendar/agent_attendees.go b/internal/calendar/agent_attendees.go| index 64c1ed3..aa6124b 100644 |
| --- a/internal/calendar/agent_attendees.go |
| +++ b/internal/calendar/agent_attendees.go |
| @@ -1,7 +1,9 @@ |
| package calendar |
| import ( |
| + "encoding/json" |
| "regexp" |
| + "unicode/utf8" |
| access "amadan.net/oficina/agent-access.git" |
| ) |
| @@ -13,6 +15,10 @@ import ( |
| const ( |
| agentAttendeeLen = 254 |
| agentAttendeeMax = 50 |
| + // agentAttendeeBytes bounds one event's attendees in a response. Quick |
| + // add, import and booking store whatever they find, so the stored list |
| + // has no length an agent response can rely on. |
| + agentAttendeeBytes = 4096 |
| ) |
| // reWholeAddress is quickadd_who.go's reAddress, anchored: an agent hands |
| @@ -40,3 +46,48 @@ func agentAttendees(in []string) ([]string, error) { |
| } |
| return out, nil |
| } |
| + |
| +// agentAttendeeView is the part of a stored attendee list a response |
| +// carries: the first addresses that fit agentAttendeeMax and |
| +// agentAttendeeBytes, in stored order, and how many were left out. An |
| +// address longer than any agent could have supplied is left out too. |
| +func agentAttendeeView(all []string) ([]string, int) { |
| + shown, size, full := []string{}, 2, false |
| + for _, addr := range all { |
| + encoded, _ := json.Marshal(addr) |
| + if full || len(addr) > agentAttendeeLen || len(shown) == agentAttendeeMax || size+len(encoded)+1 > agentAttendeeBytes { |
| + full = full || len(addr) <= agentAttendeeLen |
| + continue |
| + } |
| + size += len(encoded) + 1 |
| + shown = append(shown, addr) |
| + } |
| + return shown, len(all) - len(shown) |
| +} |
| + |
| +// agentAttendeeFields adds attendees, and attendees_more when any were |
| +// left out, to a response object. |
| +func agentAttendeeFields(out map[string]any, all []string) map[string]any { |
| + shown, more := agentAttendeeView(all) |
| + out["attendees"] = shown |
| + if more > 0 { |
| + out["attendees_more"] = more |
| + } |
| + return out |
| +} |
| + |
| +// agentAttendeesOutput is the response schema for those two fields. |
| +func agentAttendeesOutput(properties map[string]any) map[string]any { |
| + properties["attendees"] = map[string]any{"type": "array", "maxItems": agentAttendeeMax, "items": agentString(agentAttendeeLen)} |
| + properties["attendees_more"] = map[string]any{"type": "integer", "minimum": 1} |
| + return properties |
| +} |
| + |
| +// agentClip shortens text from storage that the form never limited — |
| +// imported or older rows — to what an operation's schema promises. |
| +func agentClip(s string, runes int) string { |
| + if utf8.RuneCountInString(s) <= runes { |
| + return s |
| + } |
| + return string([]rune(s)[:runes]) |
| +} |
diff --git a/internal/calendar/agent_list.go b/internal/calendar/agent_list.go| index 2e6721d..5d9a41a 100644 |
| --- a/internal/calendar/agent_list.go |
| +++ b/internal/calendar/agent_list.go |
| @@ -21,13 +21,12 @@ const ( |
| ) |
| func (a *App) agentListEventsOperation(team string) access.Operation { |
| - listed := agentObject(map[string]any{ |
| + listed := agentObject(agentAttendeesOutput(map[string]any{ |
| "id": agentID(), "calendar_id": agentID(), "calendar": agentString(2048), "title": agentString(MaxTitleLen), "location": agentString(MaxLocationLen), |
| "all_day": map[string]any{"type": "boolean"}, "start": agentString(35), "end": agentString(35), "tz": agentString(128), |
| - "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, 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, |
| + "status": agentString(32), "kind": agentString(32), "recurring": map[string]any{"type": "boolean"}, "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, 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. At most 50 are listed per event, with attendees_more counting the rest; long titles, locations and calendar names are shortened.", 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"), |
| @@ -132,18 +131,18 @@ func (a *App) agentListEvents(ctx context.Context, _ access.Principal, p Person, |
| } |
| start, end = time.Unix(e.OccStartUTC, 0).In(loc).Format(time.RFC3339), time.Unix(e.OccEndUTC, 0).In(loc).Format(time.RFC3339) |
| } |
| - attendees := e.Attendees |
| - if attendees == nil { |
| - attendees = []string{} |
| - } |
| - 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)}) |
| + row, err := json.Marshal(agentAttendeeFields(map[string]any{"id": occurrenceOwner(e), "calendar_id": e.CalendarID, "calendar": agentClip(e.CalendarName, MaxTitleLen), |
| + "title": agentClip(e.Title, MaxTitleLen), "location": agentClip(e.Location, MaxLocationLen), "all_day": e.AllDay, "start": start, "end": end, |
| + "tz": agentClip(tz, 128), "status": e.Status, "kind": e.Kind, "recurring": e.OccurrenceLocal != "", "url": a.cfg.Origin + eventHref(e)}, e.Attendees)) |
| 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 { |
| + // Every field above is bounded, so one event is far below the |
| + // budget; the check still holds for the first, never exempting it. |
| + if size+len(row)+1 > agentListBytes { |
| + if len(out) == 0 { |
| + return nil, access.Fail("too_large", "This event is too large to list.") |
| + } |
| break |
| } |
| size += len(row) + 1 |
diff --git a/internal/calendar/agent_list_bounds_test.go b/internal/calendar/agent_list_bounds_test.go| index 4716425..00d1227 100644 |
| --- a/internal/calendar/agent_list_bounds_test.go |
| +++ b/internal/calendar/agent_list_bounds_test.go |
| @@ -7,6 +7,9 @@ import ( |
| "strings" |
| "testing" |
| "time" |
| + "unicode/utf8" |
| + |
| + "amadan.net/oficina/calendar/internal/calendar/seal" |
| ) |
| func TestAgentListEventsPagesStayUnderTheFrameBudget(t *testing.T) { |
| @@ -118,3 +121,58 @@ func TestAgentListEventsContract(t *testing.T) { |
| t.Fatal("calendar_list_events missing") |
| } |
| } |
| + |
| +func TestAgentListEventsFitsOneOversizedEvent(t *testing.T) { |
| + f, _, person, cal := linkedCalendarAgent(t) |
| + e, err := f.app.store.CreateEvent(t.Context(), person.ID, EventInput{CalendarID: cal, Title: "Town hall", StartLocal: "2027-03-10T10:00", EndLocal: "2027-03-10T11:00", TZ: "UTC"}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + // Quick add and import store whatever they find: an over-long |
| + // "address", thousands of attendees, a title past the form's limit. |
| + addresses := []string{strings.Repeat("a", 1000) + "@example.com"} |
| + for i := range 2000 { |
| + addresses = append(addresses, fmt.Sprintf("attendee.number.%04d@example.com", i)) |
| + } |
| + if err = f.app.store.SetAttendees(t.Context(), e.ID, addresses); err != nil { |
| + t.Fatal(err) |
| + } |
| + title, err := f.app.store.seal.SealString(seal.PurposeEventTitle, strings.Repeat("é<", 20000)) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if err = f.app.db.G.Model(&Event{}).Where("id=?", e.ID).Update("title_sealed", title).Error; err != nil { |
| + t.Fatal(err) |
| + } |
| + check := func(op string, input map[string]any) { |
| + t.Helper() |
| + w := calendarAgentCall(f.app, op, input) |
| + if w.Code != 200 || w.Body.Len() > 48*1024 { |
| + t.Fatal(op, w.Code, w.Body.Len()) |
| + } |
| + var out struct { |
| + Events []struct { |
| + Title string `json:"title"` |
| + Attendees []string `json:"attendees"` |
| + More int `json:"attendees_more"` |
| + } `json:"events"` |
| + Title string `json:"title"` |
| + Attendees []string `json:"attendees"` |
| + More int `json:"attendees_more"` |
| + } |
| + if err := json.Unmarshal(w.Body.Bytes(), &out); err != nil { |
| + t.Fatal(err) |
| + } |
| + if len(out.Events) == 1 { |
| + out.Title, out.Attendees, out.More = out.Events[0].Title, out.Events[0].Attendees, out.Events[0].More |
| + } |
| + if len(out.Attendees) == 0 || len(out.Attendees) > 50 || out.Attendees[0] != "attendee.number.0000@example.com" || len(out.Attendees)+out.More != 2001 { |
| + t.Fatal(op, "attendees", len(out.Attendees), out.More) |
| + } |
| + if n := utf8.RuneCountInString(out.Title); n == 0 || n > MaxTitleLen { |
| + t.Fatal(op, "title", n) |
| + } |
| + } |
| + check("calendar_list_events", map[string]any{"team": "acme", "from": "2027-03-10T00:00:00Z", "to": "2027-03-11T00:00:00Z"}) |
| + check("calendar_get_event", map[string]any{"team": "acme", "id": e.ID}) |
| +} |
diff --git a/internal/calendar/agent_operations.go b/internal/calendar/agent_operations.go| index ed49d6e..f2bb1d0 100644 |
| --- a/internal/calendar/agent_operations.go |
| +++ b/internal/calendar/agent_operations.go |
| @@ -19,19 +19,19 @@ func agentInput(properties map[string]any, required ...string) json.RawMessage { |
| return agentObject(properties, append([]string{"team"}, required...)...) |
| } |
| -var agentEventSchema = agentObject(map[string]any{ |
| +var agentEventSchema = agentObject(agentAttendeesOutput(map[string]any{ |
| "id": agentID(), "calendar_id": agentID(), "title": agentString(MaxTitleLen), "location": agentString(MaxLocationLen), "notes": agentString(MaxNotesLen), |
| "all_day": map[string]any{"type": "boolean"}, "start_local": agentString(16), "end_local": agentString(16), "tz": agentString(128), |
| "start_utc": map[string]any{"type": "integer"}, "end_utc": map[string]any{"type": "integer"}, "sequence": map[string]any{"type": "integer"}, |
| - "kind": agentString(32), "status": agentString(32), "url": agentString(2048), "attendees": map[string]any{"type": "array", "items": agentString(agentAttendeeLen)}, |
| -}, "id", "calendar_id", "title", "location", "notes", "all_day", "start_local", "end_local", "tz", "start_utc", "end_utc", "sequence", "kind", "status", "url", "attendees") |
| + "kind": agentString(32), "status": agentString(32), "url": agentString(2048), |
| +}), "id", "calendar_id", "title", "location", "notes", "all_day", "start_local", "end_local", "tz", "start_utc", "end_utc", "sequence", "kind", "status", "url", "attendees") |
| func (a *App) agentOperations(team string) []access.Operation { |
| return []access.Operation{ |
| a.agentUpdateOperation(team), |
| {Name: "calendar_list", Description: "List calendars currently visible to the linked person, with their roles. Choose a calendar_id explicitly before creating an event.", Scope: "calendar:read", ReadOnly: true, Idempotent: true, |
| Input: agentInput(map[string]any{}), Output: agentObject(map[string]any{"calendars": map[string]any{"type": "array", "items": agentObject(map[string]any{"id": agentID(), "name": agentString(2048), "kind": agentString(32), "role": agentString(32)}, "id", "name", "kind", "role")}}, "calendars"), Invoke: a.agentGuard(team, "calendar:read", a.agentCalendars)}, |
| - {Name: "calendar_get_event", Description: "Read an event through the person's current calendar grant. Returns its time zone, canonical app URL and recorded attendee email addresses (never invited).", Scope: "calendar:read", ReadOnly: true, Idempotent: true, |
| + {Name: "calendar_get_event", Description: "Read an event through the person's current calendar grant. Returns its time zone, canonical app URL and recorded attendee email addresses (never invited): at most 50, with attendees_more counting the rest.", Scope: "calendar:read", ReadOnly: true, Idempotent: true, |
| Input: agentInput(map[string]any{"id": agentID()}, "id"), Output: agentEventSchema, Invoke: a.agentGuard(team, "calendar:read", a.agentGetEvent)}, |
| a.agentListEventsOperation(team), |
| {Name: "calendar_free_busy", Description: "Return complete busy spans across calendars the person can currently see. Supply RFC3339 start/end with offsets, at most 31 days. All-day events use the person's calendar time zone. Availability is advisory; creating an event does not reserve an otherwise free slot.", Scope: "calendar:read", ReadOnly: true, Idempotent: true, |
diff --git a/internal/calendar/agent_read.go b/internal/calendar/agent_read.go| index e9217e6..b64a443 100644 |
| --- a/internal/calendar/agent_read.go |
| +++ b/internal/calendar/agent_read.go |
| @@ -45,7 +45,7 @@ func (a *App) agentGetEvent(ctx context.Context, _ access.Principal, p Person, r |
| return nil, err |
| } |
| captureAgentEvent(ctx, e.ID, e.CalendarID) |
| - return map[string]any{"attendees": attendees, "id": e.ID, "calendar_id": e.CalendarID, "title": e.Title, "location": e.Location, "notes": e.Notes, "all_day": e.AllDay, "start_local": e.StartLocal, "end_local": e.EndLocal, "tz": e.TZ, "start_utc": e.StartUTC, "end_utc": e.EndUTC, "sequence": e.Sequence, "kind": e.Kind, "status": e.Status, "url": a.cfg.Origin + "/events/" + strconv.FormatInt(e.ID, 10)}, nil |
| + return agentAttendeeFields(map[string]any{"id": e.ID, "calendar_id": e.CalendarID, "title": agentClip(e.Title, MaxTitleLen), "location": agentClip(e.Location, MaxLocationLen), "notes": e.Notes, "all_day": e.AllDay, "start_local": e.StartLocal, "end_local": e.EndLocal, "tz": e.TZ, "start_utc": e.StartUTC, "end_utc": e.EndUTC, "sequence": e.Sequence, "kind": e.Kind, "status": e.Status, "url": a.cfg.Origin + "/events/" + strconv.FormatInt(e.ID, 10)}, attendees), nil |
| } |
| func (a *App) agentBusy(ctx context.Context, _ access.Principal, p Person, raw json.RawMessage) (any, error) { |
| var in struct{ Start, End string } |