From ed99fe9c453ca0baea9500602a70bb6598fae40f Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 16 Nov 2026 22:30:00 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Meetings:=20keep=20nothing=20fro?= =?UTF-8?q?m=20before=20the=20consent=20notice=20is=20delivered?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Until the consent notice is delivered (consent_posted_at), every transcript segment, draft and chat line is discarded in the manager: not stored and not emitted to listeners (Betty, Spektacular, research, facts, Inbox hand-off). Only a pre_consent_dropped count is kept. A leave phrase is still honoured and the line itself is not kept. Lines that arrive after the notice but belong before it are dropped too: seq at or below Quorum's transcript cursor when the notice landed (consent_seq), chat stamped earlier, and for two minutes segments that began before the last speech heard while pending (consent_speech_end). A reconnect or restart while pending asks Quorum for no replay, and dropped lines advance the replay cursor. Refs #18590 Signed-off-by: Andy Anderson --- ...fixed-18590-meetings-preconsent-discard.md | 1 + src/docs/meetings.md | 5 +- src/pkg/meetings/manager.go | 145 +++++++- src/pkg/meetings/meeting.go | 27 +- src/pkg/meetings/preconsent_test.go | 318 ++++++++++++++++++ src/pkg/meetings/requests_test.go | 6 +- 6 files changed, 485 insertions(+), 17 deletions(-) create mode 100644 changelog.d/fixed-18590-meetings-preconsent-discard.md create mode 100644 src/pkg/meetings/preconsent_test.go diff --git a/changelog.d/fixed-18590-meetings-preconsent-discard.md b/changelog.d/fixed-18590-meetings-preconsent-discard.md new file mode 100644 index 0000000000..b8787d79d7 --- /dev/null +++ b/changelog.d/fixed-18590-meetings-preconsent-discard.md @@ -0,0 +1 @@ +- Meetings keep nothing from before the consent notice is delivered: until Quorum confirms the notice in chat (or hive speaks it), every transcript segment, draft and chat line is discarded rather than stored or handed to Betty, Spektacular, research, knowledge facts or the Inbox, and lines that arrive later but belong before the notice are discarded too; only a `pre_consent_dropped` count is kept, a leave phrase still removes the bot, and a reconnect or restart while the notice is pending no longer replays Quorum's buffer ([#18590](https://github.com/hivecommons/hive/issues/18590)). diff --git a/src/docs/meetings.md b/src/docs/meetings.md index a9ae7b1bbd..48d4e9caae 100644 --- a/src/docs/meetings.md +++ b/src/docs/meetings.md @@ -16,6 +16,9 @@ The feature is off until `meetings.enabled` is set. ## Consent and privacy - The first thing hive posts in a meeting is the consent notice (`meetings.consent_text`), before any other message. The default names the persona: "Betty Bee from the hive has joined to take notes and help with research. Betty records this meeting's transcript. Type 'betty leave' or 'hive leave' to remove Betty." With the persona off it is "Hive has joined to take notes and help with research. It records this meeting's transcript. Type 'hive leave' to remove it." +- **Nothing from before the notice is kept.** Until the notice is delivered (`consent_posted_at` set: Quorum confirmed the chat line, or hive spoke it), every transcript segment, draft and chat line is discarded: not stored, and not handed to Betty, Spektacular, research, knowledge facts, the Inbox hand-off or drafts. A line that arrives after the notice but belongs before it is discarded too: one at or below Quorum's transcript cursor when the notice landed (`consent_seq`), a chat line stamped earlier, or, for two minutes, a segment that began before the last speech heard while the notice was pending. Hive keeps only the count, `pre_consent_dropped` on the meeting. It does not buffer and release: those lines are gone. A reconnect or restart while the notice is pending asks Quorum for no replay, so nothing buffered from that time comes back. +- A leave phrase is honoured before the notice too: the bot leaves, and the line itself is not kept. +- Vexa keeps its own copy of what the bot transcribes from the moment it is admitted, pre-consent lines included, and has no API to delete part of a live meeting's transcript. That copy goes when hive deletes the meeting upstream (`meeting.delete` at hand-off or purge), which erases the whole meeting. Quorum's in-memory replay buffer is cleared at the same time. - Anyone in the meeting can type or say a leave phrase (`meetings.persona.leave_phrases`: "betty leave" or "hive leave", also "Betty, leave" or "hive, please leave") and the bot leaves at once. - The bot joins as the persona's display name, `Betty Bee from the hive` (`meetings.persona.display_name`; `meetings.bot_name` overrides it). With the persona off it joins as `hive (notes)`. - No audio is stored. Hive keeps the transcript under `/data/meetings` for `meetings.retention_days` (30 by default) after the meeting ends, then purges it. The transcript is deleted from Vexa at hand-off, and at the latest when hive purges it. @@ -140,7 +143,7 @@ Only the hive owner can send hive to a meeting. Voice is fixed when the bot joins: Vexa only lets a bot speak if it was started with voice. Hive starts every bot able to speak while `meetings.voice_on_address` is true (and when you tick **Voice**), so the toggle can turn speaking on later; with `meetings.voice_on_address` false and **Voice** unticked, turning voice on later has no effect and hive keeps answering in chat (the panel says so). -If the meeting platform's chat is unavailable, hive cannot post the consent notice. It retries about once a minute and posts nothing else into the meeting until the notice is out; the transcript, notes and research still show in the panel. +If the meeting platform's chat is unavailable, hive cannot post the consent notice. It retries a few times and posts nothing else into the meeting until the notice is out; with voice on it speaks the notice instead, and otherwise it leaves (`consent_undelivered`). Until the notice is delivered the panel shows no transcript, notes or research: those lines are discarded, not held back. The same operations are available over the [meetings API](api-reference/knowledge.md) (`/api/meetings`). diff --git a/src/pkg/meetings/manager.go b/src/pkg/meetings/manager.go index 96008101f1..817af31d2b 100644 --- a/src/pkg/meetings/manager.go +++ b/src/pkg/meetings/manager.go @@ -2,6 +2,7 @@ package meetings import ( "context" + "encoding/json" "errors" "fmt" "log/slog" @@ -21,6 +22,15 @@ type ConfirmingChat interface { ChatPostConfirmed(ctx context.Context, nativeID, text string) (bool, error) } +// TranscriptCursor is a Quorum that can say how far its transcript has got +// (transcript.read next_seq). The real Client implements it; the manager +// reads it once the consent notice is delivered, so a final segment Quorum +// already held when the notice landed is discarded even if the event stream +// delivers it later. A Quorum without it (in-process fakes) skips that fence. +type TranscriptCursor interface { + TranscriptRead(ctx context.Context, nativeID string, afterSeq int64) (TranscriptResult, error) +} + // Quorum is the slice of the Quorum client the manager uses; tests fake it. type Quorum interface { Join(ctx context.Context, a JoinArgs) (JoinResult, error) @@ -51,6 +61,11 @@ const ( // ConsentViaChat and ConsentViaVoice record how the notice was delivered. ConsentViaChat = "chat" ConsentViaVoice = "voice" + // ConsentSpeechWindow is how long after the notice a segment can still + // be one that began before it (a long utterance finalized late). The + // ConsentSpeechEnd fence applies only this long, so a platform that + // restarts its session clock cannot silence the rest of the meeting. + ConsentSpeechWindow = 2 * time.Minute // ReasonDeclined is the state reason for a platform join request the // owner declined. ReasonDeclined = "declined" @@ -495,7 +510,7 @@ func (m *Manager) follow(ctx context.Context, id, nativeID string, lastSeq int64 backoff := m.opts.ReconnectMin for ctx.Err() == nil { done := false - err := m.opts.Quorum.Events(ctx, nativeID, lastSeq, func(ev Event) bool { + err := m.opts.Quorum.Events(ctx, nativeID, m.replayFrom(id, lastSeq), func(ev Event) bool { backoff = m.opts.ReconnectMin if ev.Type == EventSegment || ev.Type == EventChat { if ev.Seq > 0 && ev.Seq <= lastSeq { @@ -525,6 +540,18 @@ func (m *Manager) follow(ctx context.Context, id, nativeID string, lastSeq int64 } } +// replayFrom is the after_seq for a (re)connect. While the consent notice +// is still pending it is 0, no replay: whatever Quorum buffered while hive +// was away came before the notice and is not wanted. The stream opens with +// the meeting's state, which is where the notice is posted, so a replay +// would otherwise hand pre-consent lines over as if they came after it. +func (m *Manager) replayFrom(id string, lastSeq int64) int64 { + if mt, err := m.opts.Store.Get(id); err == nil && mt.ConsentPostedAt == nil { + return 0 + } + return lastSeq +} + // handle applies one event. It reports whether the meeting is over. func (m *Manager) handle(ctx context.Context, id string, ev Event) bool { switch ev.Type { @@ -604,7 +631,7 @@ func (m *Manager) ensureConsent(ctx context.Context, mt Meeting) bool { chatErr := m.postConsentChat(ctx, mt) if chatErr == nil { - m.markConsent(mt.ID, ConsentViaChat) + m.markConsent(ctx, mt, ConsentViaChat) return false } if consentTransient(chatErr) && attempt < m.opts.ConsentAttempts { @@ -621,7 +648,7 @@ func (m *Manager) ensureConsent(ctx context.Context, mt Meeting) bool { cancel() if sayErr == nil { m.log.Info("[meetings] consent notice spoken; chat could not deliver it", "meeting", mt.ID, "chat_error", chatErr) - m.markConsent(mt.ID, ConsentViaVoice) + m.markConsent(ctx, mt, ConsentViaVoice) return false } why += ", voice failed (" + sayErr.Error() + ")" @@ -676,10 +703,24 @@ func consentTransient(err error) bool { return true } -// markConsent records the notice as delivered. -func (m *Manager) markConsent(id, via string) { - _, _ = m.update(id, nil, func(x *Meeting) { - x.ConsentPostedAt, x.ConsentVia, x.ConsentError = timePtr(m.opts.Now()), via, "" +// markConsent records the notice as delivered, and raises ConsentSeq to +// Quorum's transcript cursor at that moment when Quorum can report it. +func (m *Manager) markConsent(ctx context.Context, mt Meeting, via string) { + at := m.opts.Now() + var fence int64 + if tc, ok := m.opts.Quorum.(TranscriptCursor); ok { + cctx, cancel := context.WithTimeout(ctx, m.opts.CallTimeout) + res, err := tc.TranscriptRead(cctx, mt.NativeID, mt.LastSeq) + cancel() + if err != nil { + m.log.Warn("[meetings] reading quorum's transcript cursor for the consent fence", "meeting", mt.ID, "error", err) + } else { + fence = res.NextSeq + } + } + _, _ = m.update(mt.ID, nil, func(x *Meeting) { + x.ConsentPostedAt, x.ConsentVia, x.ConsentError = timePtr(at), via, "" + x.ConsentSeq = max(x.ConsentSeq, fence) }) } @@ -702,9 +743,18 @@ func (m *Manager) handleLine(ctx context.Context, id string, ev Event) bool { if err != nil { return false } + pending := mt.ConsentPostedAt == nil if m.ensureConsent(ctx, mt) { return true } + if pending { + // The line came before the notice, even if this line is what got + // the notice delivered just now. + return m.dropPreConsent(ctx, id, ev, true) + } + if beforeConsent(mt, ev, m.opts.Now()) { + return m.dropPreConsent(ctx, id, ev, false) + } if ev.Type == EventSegment && !ev.Final { m.emit(Update{Meeting: mt, Event: &ev}) return false @@ -733,6 +783,87 @@ func (m *Manager) handleLine(ctx context.Context, id string, ev Event) bool { return false } +// beforeConsent reports whether a line that arrived after the notice was +// delivered still belongs to the time before it: a seq Quorum had already +// reached when the notice landed, a chat line stamped earlier, or a +// segment that began before the last speech heard while consent was pending. +func beforeConsent(mt Meeting, ev Event, now time.Time) bool { + if mt.ConsentPostedAt == nil { + return true + } + if ev.Seq > 0 && ev.Seq <= mt.ConsentSeq { + return true + } + if ev.Type == EventChat { + var at time.Time + if len(ev.At) > 0 && json.Unmarshal(ev.At, &at) == nil && !at.IsZero() && at.Before(*mt.ConsentPostedAt) { + return true + } + return false + } + if mt.ConsentSpeechEnd > 0 && now.Sub(*mt.ConsentPostedAt) < ConsentSpeechWindow { + if start, ok := sessionSecs(ev.Start); ok && start < mt.ConsentSpeechEnd { + return true + } + } + return false +} + +// sessionSecs decodes a segment's start or end (seconds from the session +// start). +func sessionSecs(raw json.RawMessage) (float64, bool) { + if len(raw) == 0 { + return 0, false + } + var v float64 + if json.Unmarshal(raw, &v) != nil { + return 0, false + } + return v, true +} + +// dropPreConsent discards a line from before the consent notice: it is not +// stored and not handed to listeners (Betty, Spektacular, research, facts, +// the Inbox hand-off), only counted. The replay cursor moves past it so a +// reconnect or restart never brings it back. A leave phrase is still +// honoured: a participant can always remove the bot. pending says the +// notice was not delivered when the line arrived, so the line also raises +// the fences that catch its late-finalized neighbours. +func (m *Manager) dropPreConsent(ctx context.Context, id string, ev Event, pending bool) bool { + if cur, err := m.opts.Store.Get(id); err == nil { + cur.PreConsentDropped++ + if ev.Seq > cur.LastSeq && (ev.Type == EventChat || ev.Final) { + cur.LastSeq = ev.Seq + } + if pending { + cur.ConsentSeq = max(cur.ConsentSeq, ev.Seq) + if end, ok := sessionSecs(ev.End); ok { + cur.ConsentSpeechEnd = max(cur.ConsentSpeechEnd, end) + } + } + // Quietly, like the replay cursor: bookkeeping, not an update. + if err := m.opts.Store.Save(cur); err != nil { + m.log.Warn("[meetings] counting a pre-consent line", "meeting", id, "error", err) + } + } + if ev.Type == EventSegment && !ev.Final || !m.leave.Match(ev.Text) { + return false + } + m.log.Info("[meetings] leave requested before the consent notice was delivered", "meeting", id) + mt, err := m.opts.Store.Get(id) + if err != nil { + return false + } + cctx, cancel := context.WithTimeout(ctx, m.opts.CallTimeout) + defer cancel() + if _, err := m.opts.Quorum.Leave(cctx, mt.NativeID); err != nil && !IsCode(err, CodeNotFound) { + m.log.Warn("[meetings] leave failed", "meeting", id, "error", err) + } + // No event on the update: the line itself is not kept or forwarded. + _, _ = m.finish(id, StateEnded, ReasonHiveLeave, nil) + return true +} + // armLobby (re)starts the lobby timer: when it fires and the meeting is // still in the lobby, hive leaves and marks it failed with lobby_timeout. func (m *Manager) armLobby(id string, after time.Duration) { diff --git a/src/pkg/meetings/meeting.go b/src/pkg/meetings/meeting.go index 63d0915df0..a2ca66fbf1 100644 --- a/src/pkg/meetings/meeting.go +++ b/src/pkg/meetings/meeting.go @@ -85,14 +85,25 @@ type Meeting struct { // ConsentVia is how the notice was delivered: "chat" (Quorum confirmed // the line in the meeting chat) or "voice" (spoken because chat could // not deliver it). - ConsentVia string `json:"consent_via,omitempty"` - LobbySince *time.Time `json:"lobby_since,omitempty"` - LiveAt *time.Time `json:"live_at,omitempty"` - EndedAt *time.Time `json:"ended_at,omitempty"` - HandedOffAt *time.Time `json:"handed_off_at,omitempty"` - PurgedAt *time.Time `json:"purged_at,omitempty"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` + ConsentVia string `json:"consent_via,omitempty"` + // PreConsentDropped counts the lines (segments, drafts and chat) hive + // discarded because they came before the consent notice was delivered. + // Only the count is kept, never the content. + PreConsentDropped int `json:"pre_consent_dropped,omitempty"` + // ConsentSeq is the highest Quorum seq known to come before the notice: + // a line at or below it is discarded even if it arrives after. + ConsentSeq int64 `json:"consent_seq,omitempty"` + // ConsentSpeechEnd is the latest session time (seconds, a segment's + // `end`) heard before the notice; a segment that starts before it, and + // is finalized just after the notice, is discarded too. + ConsentSpeechEnd float64 `json:"consent_speech_end,omitempty"` + LobbySince *time.Time `json:"lobby_since,omitempty"` + LiveAt *time.Time `json:"live_at,omitempty"` + EndedAt *time.Time `json:"ended_at,omitempty"` + HandedOffAt *time.Time `json:"handed_off_at,omitempty"` + PurgedAt *time.Time `json:"purged_at,omitempty"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` } // Person is a meeting participant as Quorum reports it. diff --git a/src/pkg/meetings/preconsent_test.go b/src/pkg/meetings/preconsent_test.go new file mode 100644 index 0000000000..fb69312c61 --- /dev/null +++ b/src/pkg/meetings/preconsent_test.go @@ -0,0 +1,318 @@ +package meetings + +import ( + "context" + "encoding/json" + "strings" + "sync" + "sync/atomic" + "testing" + "time" +) + +// gatedQuorum holds the consent notice back (a transient rate limit) until +// open is set, so a test can send lines while consent is pending. +type gatedQuorum struct { + *fakeQuorum + open atomic.Bool + cursor int64 // cursorQuorum's transcript.read next_seq +} + +var errSlowDown = &ToolError{Tool: "chat.post", Code: CodeRateLimited, Message: "slow down"} + +func (g *gatedQuorum) ChatPostConfirmed(_ context.Context, _ string, text string) (bool, error) { + if !g.open.Load() { + return false, errSlowDown + } + g.fakeQuorum.mu.Lock() + g.fakeQuorum.posts = append(g.fakeQuorum.posts, text) + g.fakeQuorum.mu.Unlock() + return true, nil +} + +// cursorQuorum is a gatedQuorum that also reports Quorum's transcript cursor. +type cursorQuorum struct{ *gatedQuorum } + +func (c cursorQuorum) TranscriptRead(context.Context, string, int64) (TranscriptResult, error) { + return TranscriptResult{NextSeq: c.cursor}, nil +} + +// recorder keeps every update's event text a listener saw. +type recorder struct { + mu sync.Mutex + texts []string +} + +func (r *recorder) on(u Update) { + if u.Event == nil { + return + } + r.mu.Lock() + defer r.mu.Unlock() + r.texts = append(r.texts, u.Event.Text) +} + +func (r *recorder) saw(text string) bool { + r.mu.Lock() + defer r.mu.Unlock() + for _, t := range r.texts { + if t == text { + return true + } + } + return false +} + +func newPendingManager(t *testing.T, st *Store, q Quorum) *Manager { + t.Helper() + m, err := NewManager(Options{ + Store: st, Quorum: q, BotName: "Betty Bee from the hive", ConsentText: testConsent, + LeavePhrases: []string{"betty leave", "hive leave"}, + ReconnectMin: 5 * time.Millisecond, ReconnectMax: 20 * time.Millisecond, + ConsentRetry: time.Millisecond, ConsentAttempts: 1000, + }) + if err != nil { + t.Fatal(err) + } + t.Cleanup(m.Close) + return m +} + +func newStore(t *testing.T) *Store { + t.Helper() + st, err := NewStore(t.TempDir()) + if err != nil { + t.Fatal(err) + } + return st +} + +// goLive joins a meeting and takes it live with consent still pending. +func goLive(t *testing.T, m *Manager, q *fakeQuorum) Meeting { + t.Helper() + mt, err := m.Join(context.Background(), JoinRequest{URL: "https://meet.google.com/abc-defg-hij"}) + if err != nil { + t.Fatal(err) + } + q.stream("q1") <- Event{Type: EventState, State: StateLive} + eventually(t, "live with consent pending", func() bool { + got, _ := m.Get(mt.ID) + return got.State == StateLive && got.ConsentError != "" + }) + return mt +} + +func dropped(m *Manager, id string, n int) func() bool { + return func() bool { got, _ := m.Get(id); return got.PreConsentDropped >= n } +} + +func TestPreConsentLinesNeitherStoredNorForwarded(t *testing.T) { + g := &gatedQuorum{fakeQuorum: newFakeQuorum()} + m := newPendingManager(t, newStore(t), g) + rec := &recorder{} + defer m.Subscribe(rec.on)() + mt := goLive(t, m, g.fakeQuorum) + ch := g.stream("q1") + + ch <- Event{Type: EventSegment, Seq: 1, Speaker: &Person{Name: "Ana"}, Text: "secret draft", Final: false} + ch <- Event{Type: EventSegment, Seq: 2, Speaker: &Person{Name: "Ana"}, Text: "secret plan", Final: true} + ch <- Event{Type: EventChat, Seq: 3, Author: &Person{Name: "Bo"}, Text: "secret chat"} + eventually(t, "three drops", dropped(m, mt.ID, 3)) + + // The notice gets through on the next line; that line still came + // before it and is dropped too. + g.open.Store(true) + seq := int64(4) + for i := 0; ; i++ { + ch <- Event{Type: EventChat, Seq: seq, Author: &Person{Name: "Bo"}, Text: "secret late"} + seq++ + time.Sleep(5 * time.Millisecond) + if got, _ := m.Get(mt.ID); got.ConsentPostedAt != nil { + break + } + if i > 200 { + t.Fatal("consent never delivered") + } + } + ch <- Event{Type: EventSegment, Seq: seq, Speaker: &Person{Name: "Ana"}, Text: "after consent", Final: true} + eventually(t, "post-consent line stored", func() bool { tr, _ := m.Transcript(mt.ID); return len(tr) == 1 }) + + tr, _ := m.Transcript(mt.ID) + if tr[0].Text != "after consent" { + t.Fatalf("transcript %+v", tr) + } + for _, s := range []string{"secret draft", "secret plan", "secret chat", "secret late"} { + if rec.saw(s) { + t.Fatalf("listener saw pre-consent line %q", s) + } + } + if !rec.saw("after consent") { + t.Fatal("the post-consent line must reach listeners") + } + got, _ := m.Get(mt.ID) + if got.PreConsentDropped < 4 || got.LastSeq != seq || got.State != StateLive { + t.Fatalf("meeting %+v", got) + } + // Only the count is kept: nothing of the content is in the record. + b, _ := json.Marshal(got) + if strings.Contains(string(b), "secret") { + t.Fatalf("pre-consent content in the meeting record: %s", b) + } +} + +func TestPreConsentLeaveHonoured(t *testing.T) { + g := &gatedQuorum{fakeQuorum: newFakeQuorum()} + m := newPendingManager(t, newStore(t), g) + rec := &recorder{} + defer m.Subscribe(rec.on)() + mt := goLive(t, m, g.fakeQuorum) + + g.stream("q1") <- Event{Type: EventChat, Seq: 1, Author: &Person{Name: "Ana"}, Text: "Betty, please leave"} + eventually(t, "left", stateIs(m, mt.ID, StateEnded)) + got, _ := m.Get(mt.ID) + if got.StateReason != ReasonHiveLeave || got.ConsentPostedAt != nil || got.PreConsentDropped != 1 { + t.Fatalf("meeting %+v", got) + } + if _, leaves := g.snapshot(); len(leaves) != 1 || leaves[0] != "q1" { + t.Fatalf("leaves %q", leaves) + } + if tr, _ := m.Transcript(mt.ID); len(tr) != 0 { + t.Fatalf("the leave line was stored: %+v", tr) + } + if rec.saw("Betty, please leave") { + t.Fatal("the leave line was forwarded") + } +} + +func TestPreConsentBotOwnNoticeIgnored(t *testing.T) { + g := &gatedQuorum{fakeQuorum: newFakeQuorum()} + m := newPendingManager(t, newStore(t), g) + mt := goLive(t, m, g.fakeQuorum) + // The notice itself says "hive leave"; its echo must not remove the bot + // or count as a participant's line. + g.stream("q1") <- Event{Type: EventChat, Seq: 1, Author: &Person{Name: "Betty Bee from the hive (Guest)"}, Text: testConsent} + g.stream("q1") <- Event{Type: EventChat, Seq: 2, Author: &Person{Name: "Someone"}, Text: testConsent} + g.stream("q1") <- Event{Type: EventChat, Seq: 3, Author: &Person{Name: "Ana"}, Text: "hi"} + eventually(t, "a participant line dropped", dropped(m, mt.ID, 1)) + got, _ := m.Get(mt.ID) + if got.State != StateLive || got.PreConsentDropped != 1 { + t.Fatalf("meeting %+v", got) + } +} + +func TestPreConsentNoReplayOnReconnectOrRestart(t *testing.T) { + st := newStore(t) + g := &gatedQuorum{fakeQuorum: newFakeQuorum()} + m := newPendingManager(t, st, g) + mt := goLive(t, m, g.fakeQuorum) + ch := g.stream("q1") + ch <- Event{Type: EventSegment, Seq: 5, Speaker: &Person{Name: "Ana"}, Text: "secret", Final: true} + eventually(t, "drop", dropped(m, mt.ID, 1)) + if got, _ := m.Get(mt.ID); got.LastSeq != 5 { + t.Fatalf("the cursor must move past a dropped line: %+v", got) + } + // A reconnect while consent is pending asks for no replay. + close(ch) + eventually(t, "reconnect", func() bool { g.mu.Lock(); defer g.mu.Unlock(); return g.connects >= 2 }) + g.mu.Lock() + if last := g.afters[len(g.afters)-1]; last != 0 { + g.mu.Unlock() + t.Fatalf("reconnect while pending replayed from %d", last) + } + g.mu.Unlock() + m.Close() + + // A restart while pending: same, and the first line after the notice is + // the first one kept. + g2 := &gatedQuorum{fakeQuorum: newFakeQuorum()} + m2 := newPendingManager(t, st, g2) + if n := m2.Resume(); n != 1 { + t.Fatalf("resumed %d", n) + } + eventually(t, "resume connect", func() bool { g2.mu.Lock(); defer g2.mu.Unlock(); return g2.connects >= 1 }) + g2.mu.Lock() + if g2.afters[0] != 0 { + g2.mu.Unlock() + t.Fatalf("resume while pending replayed from %d", g2.afters[0]) + } + g2.mu.Unlock() + ch2 := g2.stream("q1") + g2.open.Store(true) + ch2 <- Event{Type: EventState, State: StateLive} + eventually(t, "consent after resume", func() bool { got, _ := m2.Get(mt.ID); return got.ConsentPostedAt != nil }) + ch2 <- Event{Type: EventSegment, Seq: 4, Speaker: &Person{Name: "Ana"}, Text: "old", Final: true} + ch2 <- Event{Type: EventSegment, Seq: 7, Speaker: &Person{Name: "Ana"}, Text: "kept", Final: true} + eventually(t, "kept", func() bool { tr, _ := m2.Transcript(mt.ID); return len(tr) == 1 }) + if tr, _ := m2.Transcript(mt.ID); tr[0].Text != "kept" { + t.Fatalf("transcript %+v", tr) + } +} + +func TestConsentCursorFence(t *testing.T) { + g := &gatedQuorum{fakeQuorum: newFakeQuorum(), cursor: 9} + g.open.Store(true) + m := newPendingManager(t, newStore(t), cursorQuorum{g}) + mt, err := m.Join(context.Background(), JoinRequest{URL: "https://meet.google.com/x"}) + if err != nil { + t.Fatal(err) + } + ch := g.stream("q1") + ch <- Event{Type: EventState, State: StateLive} + eventually(t, "consent", func() bool { got, _ := m.Get(mt.ID); return got.ConsentPostedAt != nil }) + if got, _ := m.Get(mt.ID); got.ConsentSeq != 9 { + t.Fatalf("consent seq %d, want quorum's cursor 9", got.ConsentSeq) + } + // Quorum had seq 8 before the notice landed; it arrives after it. + ch <- Event{Type: EventSegment, Seq: 8, Speaker: &Person{Name: "Ana"}, Text: "before", Final: true} + ch <- Event{Type: EventSegment, Seq: 10, Speaker: &Person{Name: "Ana"}, Text: "after", Final: true} + eventually(t, "after stored", func() bool { tr, _ := m.Transcript(mt.ID); return len(tr) == 1 }) + if tr, _ := m.Transcript(mt.ID); tr[0].Text != "after" { + t.Fatalf("transcript %+v", tr) + } + if got, _ := m.Get(mt.ID); got.PreConsentDropped != 1 { + t.Fatalf("dropped %d", got.PreConsentDropped) + } +} + +func TestBeforeConsent(t *testing.T) { + posted := time.Date(2026, 11, 16, 10, 0, 0, 0, time.UTC) + mt := Meeting{ConsentPostedAt: &posted, ConsentSeq: 5, ConsentSpeechEnd: 30} + soon := posted.Add(time.Second) + raw := func(s string) json.RawMessage { return json.RawMessage(s) } + cases := []struct { + name string + mt Meeting + ev Event + now time.Time + want bool + }{ + {"pending", Meeting{}, Event{Type: EventChat, Seq: 99}, soon, true}, + {"seq at fence", mt, Event{Type: EventSegment, Seq: 5, Final: true}, soon, true}, + {"seq after fence", mt, Event{Type: EventSegment, Seq: 6, Final: true, Start: raw("31")}, soon, false}, + {"chat stamped before", mt, Event{Type: EventChat, Seq: 6, At: raw(`"2026-11-16T09:59:59Z"`)}, soon, true}, + {"chat stamped after", mt, Event{Type: EventChat, Seq: 6, At: raw(`"2026-11-16T10:00:01Z"`)}, soon, false}, + {"chat unstamped", mt, Event{Type: EventChat, Seq: 6}, soon, false}, + {"chat bad stamp", mt, Event{Type: EventChat, Seq: 6, At: raw(`"yesterday"`)}, soon, false}, + {"segment began before speech end", mt, Event{Type: EventSegment, Seq: 6, Final: true, Start: raw("29.5")}, soon, true}, + {"segment began before, window over", mt, Event{Type: EventSegment, Seq: 6, Final: true, Start: raw("1")}, posted.Add(ConsentSpeechWindow), false}, + {"segment without start", mt, Event{Type: EventSegment, Seq: 6, Final: true}, soon, false}, + } + for _, c := range cases { + if got := beforeConsent(c.mt, c.ev, c.now); got != c.want { + t.Errorf("%s: beforeConsent = %v, want %v", c.name, got, c.want) + } + } +} + +func TestPreConsentSpeechEndFence(t *testing.T) { + g := &gatedQuorum{fakeQuorum: newFakeQuorum()} + m := newPendingManager(t, newStore(t), g) + mt := goLive(t, m, g.fakeQuorum) + ch := g.stream("q1") + ch <- Event{Type: EventSegment, Seq: 1, Speaker: &Person{Name: "Ana"}, Text: "secret", Final: false, + Start: json.RawMessage("10"), End: json.RawMessage("12.5")} + eventually(t, "drop", dropped(m, mt.ID, 1)) + if got, _ := m.Get(mt.ID); got.ConsentSpeechEnd != 12.5 || got.ConsentSeq != 1 { + t.Fatalf("fences %+v", got) + } +} diff --git a/src/pkg/meetings/requests_test.go b/src/pkg/meetings/requests_test.go index 215863b3c9..9ea72506b0 100644 --- a/src/pkg/meetings/requests_test.go +++ b/src/pkg/meetings/requests_test.go @@ -66,10 +66,14 @@ func TestConsentErrorRecordedAndThrottled(t *testing.T) { ch <- Event{Type: EventState, State: StateLive} eventually(t, "consent error", func() bool { got, _ := m.Get(mt.ID); return got.ConsentError != "" }) ch <- Event{Type: EventChat, Author: &Person{Name: "Ana"}, Text: "hi"} - eventually(t, "line stored", func() bool { tr, _ := m.Transcript(mt.ID); return len(tr) == 1 }) + // The line came before the notice: counted, never stored. + eventually(t, "line dropped", func() bool { got, _ := m.Get(mt.ID); return got.PreConsentDropped == 1 }) if got, _ := m.Get(mt.ID); got.ConsentPostedAt != nil || got.State != StateLive { t.Fatalf("a throttled retry must not post: %+v", got) } + if tr, _ := m.Transcript(mt.ID); len(tr) != 0 { + t.Fatalf("a pre-consent line was stored: %+v", tr) + } } func TestConsentChatUnavailableWithoutVoiceLeaves(t *testing.T) {