diff --git a/modules/memory/internal/store/notes.go b/modules/memory/internal/store/notes.go index b16262164..793bd7c36 100644 --- a/modules/memory/internal/store/notes.go +++ b/modules/memory/internal/store/notes.go @@ -100,13 +100,13 @@ func (s *Store) Notes(ctx context.Context, orgID, owner, class string, limit int // Owned returns every note of one class in owner's own scope, including the // ones they disabled or that have outlived their retention. It is the inspection // read: a person can see and re-enable what they turned off, while a -// conversation reads only what Notes returns. +// conversation reads only what Notes returns. A zero limit is no limit. func (s *Store) Owned(ctx context.Context, orgID, owner, class string, limit int) ([]Note, error) { var out []Note if err := s.db.SelectContext(ctx, &out, ` SELECT `+noteColumns+` FROM memory.notes WHERE org_id = $1 AND owner_id = $2 AND class = $3 AND scope = 'person' - ORDER BY created_at LIMIT $4`, orgID, owner, class, limit); err != nil { + ORDER BY created_at LIMIT NULLIF($4, 0)`, orgID, owner, class, limit); err != nil { return nil, fmt.Errorf("list own notes: %w", err) } return out, nil @@ -168,7 +168,7 @@ func (s *Store) Shared(ctx context.Context, orgID, class string, limit int) ([]N if err := s.db.SelectContext(ctx, &out, ` SELECT `+noteColumns+` FROM memory.notes WHERE org_id = $1 AND class = $2 AND scope = 'shared' AND state = 'published' - ORDER BY created_at LIMIT $3`, orgID, class, limit); err != nil { + ORDER BY created_at LIMIT NULLIF($3, 0)`, orgID, class, limit); err != nil { return nil, fmt.Errorf("list shared notes: %w", err) } return out, nil diff --git a/modules/memory/notes.go b/modules/memory/notes.go index 646bb8a06..c3ff3395d 100644 --- a/modules/memory/notes.go +++ b/modules/memory/notes.go @@ -20,6 +20,11 @@ import ( // out the conversation itself. const maxNotes = 200 +// allNotes is no bound at all, for the reads a person inspects their own memory +// with: those must reach every note they may read, whatever the conversation +// bound is. +const allNotes = 0 + // notesTable names the table a recorded deletion applies to. const notesTable = "memory.notes" @@ -250,7 +255,7 @@ func (m *Module) Mine(ctx context.Context, owner string, class Class) ([]Note, e if err := class.Validate(); err != nil { return nil, err } - rows, err := m.notes.Owned(ctx, m.orgID, owner, string(class), maxNotes) + rows, err := m.notes.Owned(ctx, m.orgID, owner, string(class), allNotes) if err != nil { return nil, err } @@ -335,7 +340,7 @@ func (m *Module) Shared(ctx context.Context, class Class) ([]Note, error) { if err := class.Validate(); err != nil { return nil, err } - rows, err := m.shared.Shared(ctx, m.orgID, string(class), maxNotes) + rows, err := m.shared.Shared(ctx, m.orgID, string(class), allNotes) if err != nil { return nil, err } diff --git a/modules/memory/notes_test.go b/modules/memory/notes_test.go index 09e54ace6..8228103e9 100644 --- a/modules/memory/notes_test.go +++ b/modules/memory/notes_test.go @@ -28,6 +28,11 @@ type noteFake struct { published []uuid.UUID witnesses []store.Witness fail error + // The bounds each read was given, so a test can see that the conversation + // bound never reaches the inspection reads. + notesLimit int + ownedLimit int + sharedLimit int } func (f *noteFake) AddNote(_ context.Context, orgID string, n store.Note) (store.Note, error) { @@ -66,8 +71,9 @@ func (f *noteFake) SharedExists(_ context.Context, orgID string, id uuid.UUID) ( // either the reader's own note or one published to the org. A note in neither // scope is read by no one, which is what a test can only see if the fake // refuses it too. -func (f *noteFake) Notes(_ context.Context, orgID, owner, class string, _ int) ([]store.Note, error) { +func (f *noteFake) Notes(_ context.Context, orgID, owner, class string, limit int) ([]store.Note, error) { f.asked = append(f.asked, owner) + f.notesLimit = limit if f.fail != nil { return nil, f.fail } @@ -82,7 +88,8 @@ func (f *noteFake) Notes(_ context.Context, orgID, owner, class string, _ int) ( return out, nil } -func (f *noteFake) Owned(_ context.Context, orgID, owner, class string, _ int) ([]store.Note, error) { +func (f *noteFake) Owned(_ context.Context, orgID, owner, class string, limit int) ([]store.Note, error) { + f.ownedLimit = limit if f.fail != nil { return nil, f.fail } @@ -146,7 +153,8 @@ func (f *noteFake) Pending(context.Context, string, int) ([]store.Note, error) { return nil, f.fail } -func (f *noteFake) Shared(context.Context, string, string, int) ([]store.Note, error) { +func (f *noteFake) Shared(_ context.Context, _, _ string, limit int) ([]store.Note, error) { + f.sharedLimit = limit return nil, f.fail } @@ -685,3 +693,31 @@ func TestNotesFailClosed(t *testing.T) { t.Fatalf("a read of an unknown class returned %v, want a refusal", err) } } + +// TestTheConversationBoundStaysOffTheInspectionRead pins #2141: maxNotes bounds +// what a conversation is given, and it was also the only bound on the read a +// person and the shared list use, so a person past 200 notes of one class could +// no longer see the oldest ones, with no total and no offset to say the list +// was cut. The inspection reads must reach every note. +func TestTheConversationBoundStaysOffTheInspectionRead(t *testing.T) { + notes := ¬eFake{} + m := newNotes(t, notes, &deletionFake{}) + if _, err := m.Mine(t.Context(), "alice", ClassDurableFact); err != nil { + t.Fatal(err) + } + if notes.ownedLimit != allNotes { + t.Fatalf("the read of a person's own notes passed limit %d, want %d (no bound)", notes.ownedLimit, allNotes) + } + if _, err := m.Shared(t.Context(), ClassDurableFact); err != nil { + t.Fatal(err) + } + if notes.sharedLimit != allNotes { + t.Fatalf("the shared list passed limit %d, want %d (no bound)", notes.sharedLimit, allNotes) + } + if _, err := m.Notes(t.Context(), "alice", ClassDurableFact); err != nil { + t.Fatal(err) + } + if notes.notesLimit != maxNotes { + t.Fatalf("the conversation read passed limit %d, want %d", notes.notesLimit, maxNotes) + } +}