diff --git a/changelog.d/fixed-18590-consent-confirmed.md b/changelog.d/fixed-18590-consent-confirmed.md new file mode 100644 index 0000000000..1eec6be42f --- /dev/null +++ b/changelog.d/fixed-18590-consent-confirmed.md @@ -0,0 +1 @@ +- Meetings: the consent notice now counts as posted only when Quorum confirms the line reached the meeting. In a live Teams test the bot could not open the meeting chat (`chat_send_failed reason=no_composer`, chat closed to guests) yet hive recorded `consent_posted_at` and kept listening (#18590, finding 11). Hive now posts the notice with `chat.post {confirm:true}` (quorum 0.2.2) and treats anything but `delivery: confirmed`, including an older Quorum that cannot say, as not posted. When chat cannot deliver it and the meeting has voice on, Betty speaks the notice instead (`consent_via: voice`, shown in the Meetings panel); when neither works she leaves at once with state reason `consent_undelivered`, and the panel says "Left: the consent notice could not be delivered (chat unavailable (...), voice off)". Transient failures (bot not admitted yet, rate limit, a dropped call) are retried up to three times before that. The image now ships quorum v0.2.2. diff --git a/src/Dockerfile b/src/Dockerfile index 5d6892c7aa..b2cdd9ab23 100644 --- a/src/Dockerfile +++ b/src/Dockerfile @@ -529,8 +529,8 @@ RUN ARCH="${TARGETARCH:-$(dpkg --print-architecture)}" && \ # Bump note: see src/docs/cli-pins.md (quorum row); update QUORUM_VERSION and # QUORUM_SHA256 together from the release's quorum.mjs.sha256, and # QUORUM_DAVEY_VERSION from quorum's package-lock.json. -ARG QUORUM_VERSION=v0.2.1 -ARG QUORUM_SHA256=4c5c374a7bab1fd71cb62ae76c29de28f395e96784ad2ea40605b071c2761fbb +ARG QUORUM_VERSION=v0.2.2 +ARG QUORUM_SHA256=f36ab9b3618f9a0b9a017f67a8404001e449255f74ce8b34f2c70f287c08e1fc ARG QUORUM_DAVEY_VERSION=0.1.12 RUN if [ -n "${QUORUM_VERSION}" ]; then \ test -n "${QUORUM_SHA256}" || { echo "QUORUM_SHA256 is required with QUORUM_VERSION" >&2; exit 1; } && \ diff --git a/src/pkg/dashboard/static-src/js/meetings.js b/src/pkg/dashboard/static-src/js/meetings.js index 24ec960ac6..e98a30afd2 100644 --- a/src/pkg/dashboard/static-src/js/meetings.js +++ b/src/pkg/dashboard/static-src/js/meetings.js @@ -89,6 +89,7 @@ const raw = String(m.state_reason || ''); if (!raw) return ''; if (m.state === 'blocked') return 'The meeting refused a guest bot.'; + if (/consent_undelivered/.test(raw)) return 'Left: the consent notice could not be delivered' + (m.consent_error ? ' (' + m.consent_error + ')' : '') + '.'; if (/lobby_timeout/.test(raw)) return 'Nobody admitted hive from the lobby in time.'; if (/platform_unavailable/.test(raw)) return 'This meeting platform is not available on this hive right now.'; if (/invalid_argument/.test(raw)) return 'The meeting service did not accept this meeting link.'; @@ -128,7 +129,8 @@ '' + escapeHtml(state) + '' + (meta ? '
' + escapeHtml(meta) + '
' : '') + meetingsReasonHtml(m) + - (m.consent_error && !m.consent_posted_at ? '
chat unavailable: consent notice not posted yet
' : '') + + (active && m.consent_error && !m.consent_posted_at ? '
chat unavailable: consent notice not posted yet
' : '') + + (m.consent_via === 'voice' ? '
consent notice spoken: the meeting chat could not deliver it
' : '') + (m.voice_enabled && !m.voice_capable ? '
joined without voice; replies go to chat
' : '') + '
' + '' + diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index bcd0387d4e..c16a3e37d4 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -50189,6 +50189,7 @@ const raw = String(m.state_reason || ''); if (!raw) return ''; if (m.state === 'blocked') return 'The meeting refused a guest bot.'; + if (/consent_undelivered/.test(raw)) return 'Left: the consent notice could not be delivered' + (m.consent_error ? ' (' + m.consent_error + ')' : '') + '.'; if (/lobby_timeout/.test(raw)) return 'Nobody admitted hive from the lobby in time.'; if (/platform_unavailable/.test(raw)) return 'This meeting platform is not available on this hive right now.'; if (/invalid_argument/.test(raw)) return 'The meeting service did not accept this meeting link.'; @@ -50228,7 +50229,8 @@ '' + escapeHtml(state) + '
' + (meta ? '
' + escapeHtml(meta) + '
' : '') + meetingsReasonHtml(m) + - (m.consent_error && !m.consent_posted_at ? '
chat unavailable: consent notice not posted yet
' : '') + + (active && m.consent_error && !m.consent_posted_at ? '
chat unavailable: consent notice not posted yet
' : '') + + (m.consent_via === 'voice' ? '
consent notice spoken: the meeting chat could not deliver it
' : '') + (m.voice_enabled && !m.voice_capable ? '
joined without voice; replies go to chat
' : '') + '
' + '' + diff --git a/src/pkg/meetings/client.go b/src/pkg/meetings/client.go index 7ebcdd7bea..c651a875e0 100644 --- a/src/pkg/meetings/client.go +++ b/src/pkg/meetings/client.go @@ -362,6 +362,32 @@ func (c *Client) ChatPost(ctx context.Context, nativeID, text string) error { return c.Call(ctx, "chat.post", textArgs{nativeID, text}, nil) } +// confirmArgs is chat.post with confirm:true (quorum 0.2.2+). +type confirmArgs struct { + MeetingID string `json:"meeting_id"` + Text string `json:"text"` + Confirm bool `json:"confirm"` +} + +// DeliveryConfirmed is chat.post's delivery when the platform reported the +// line in the meeting. +const DeliveryConfirmed = "confirmed" + +// ChatPostConfirmed calls chat.post with confirm:true and reports whether +// Quorum confirmed the line reached the meeting. A send the platform +// reports as failed is an error (platform_unavailable); an older Quorum that +// answers no delivery, or a delivery Quorum could not verify in time, is +// (false, nil): not known to be posted. +func (c *Client) ChatPostConfirmed(ctx context.Context, nativeID, text string) (bool, error) { + var r struct { + Delivery string `json:"delivery"` + } + if err := c.Call(ctx, "chat.post", confirmArgs{nativeID, text, true}, &r); err != nil { + return false, err + } + return r.Delivery == DeliveryConfirmed, nil +} + // Say calls say. func (c *Client) Say(ctx context.Context, nativeID, text string) error { return c.Call(ctx, "say", textArgs{nativeID, text}, nil) diff --git a/src/pkg/meetings/client_test.go b/src/pkg/meetings/client_test.go index 37e0d0527c..75c8691779 100644 --- a/src/pkg/meetings/client_test.go +++ b/src/pkg/meetings/client_test.go @@ -328,3 +328,32 @@ func TestReadSSE(t *testing.T) { t.Fatal("empty event has no author") } } + +func TestClientChatPostConfirmed(t *testing.T) { + f, srv := newFakeQuorumServer(t) + c := NewClient(srv.URL, "tok", nil) + ctx := context.Background() + f.results["chat.post"] = `{"ok":true,"posted_at":"2026-11-17T01:52:00Z","delivery":"confirmed"}` + if ok, err := c.ChatPostConfirmed(ctx, "q1", "notice"); err != nil || !ok { + t.Fatalf("confirmed = %v, %v", ok, err) + } + f.mu.Lock() + sent := f.args[len(f.args)-1] + f.mu.Unlock() + if sent["confirm"] != true || sent["text"] != "notice" || sent["meeting_id"] != "q1" { + t.Fatalf("args %+v", sent) + } + f.results["chat.post"] = `{"ok":true,"posted_at":"2026-11-17T01:52:00Z","delivery":"unconfirmed"}` + if ok, err := c.ChatPostConfirmed(ctx, "q1", "notice"); err != nil || ok { + t.Fatalf("unconfirmed = %v, %v", ok, err) + } + // A Quorum older than 0.2.2 answers no delivery: not known to be posted. + f.results["chat.post"] = `{"ok":true,"posted_at":"2026-11-17T01:52:00Z"}` + if ok, err := c.ChatPostConfirmed(ctx, "q1", "notice"); err != nil || ok { + t.Fatalf("legacy = %v, %v", ok, err) + } + f.results["chat.post"] = `{"ok":false,"error":{"code":"platform_unavailable","message":"the chat line was not posted: no_composer"}}` + if _, err := c.ChatPostConfirmed(ctx, "q1", "notice"); !IsCode(err, CodePlatformUnavailable) { + t.Fatalf("failed send = %v", err) + } +} diff --git a/src/pkg/meetings/consent_test.go b/src/pkg/meetings/consent_test.go new file mode 100644 index 0000000000..fcd934c00a --- /dev/null +++ b/src/pkg/meetings/consent_test.go @@ -0,0 +1,191 @@ +package meetings + +import ( + "context" + "strings" + "sync" + "testing" + "time" +) + +// confirmingQuorum is a fakeQuorum whose chat.post can confirm delivery +// (quorum 0.2.2 chat.post confirm:true). +type confirmingQuorum struct { + *fakeQuorum + cmu sync.Mutex + confirm []bool // answers in order; the last one repeats + confirmEr []error // errors in order; the last one repeats + tries int +} + +func (c *confirmingQuorum) ChatPostConfirmed(_ context.Context, _ string, text string) (bool, error) { + c.cmu.Lock() + defer c.cmu.Unlock() + i := c.tries + c.tries++ + pick := func(n int) int { return min(i, n-1) } + var err error + if len(c.confirmEr) > 0 { + err = c.confirmEr[pick(len(c.confirmEr))] + } + if err != nil { + return false, err + } + ok := true + if len(c.confirm) > 0 { + ok = c.confirm[pick(len(c.confirm))] + } + if ok { + c.fakeQuorum.mu.Lock() + c.fakeQuorum.posts = append(c.fakeQuorum.posts, text) + c.fakeQuorum.mu.Unlock() + } + return ok, nil +} + +func (c *confirmingQuorum) says() []string { + c.fakeQuorum.mu.Lock() + defer c.fakeQuorum.mu.Unlock() + return append([]string(nil), c.fakeQuorum.says...) +} + +var errNoComposer = &ToolError{Tool: "chat.post", Code: CodePlatformUnavailable, Message: "the chat line was not posted: no_composer"} + +// liveTeams joins a Teams meeting (voice as given), runs before (if any) +// while it is still joining, and takes it live. +func liveTeams(t *testing.T, q *confirmingQuorum, voice bool, before ...func(*Manager, Meeting)) (*Manager, Meeting) { + t.Helper() + m := newTestManager(t, q, 0) + mt, err := m.Join(context.Background(), JoinRequest{URL: "https://teams.microsoft.com/l/x", Voice: voice}) + if err != nil { + t.Fatal(err) + } + if mt.VoiceCapable != voice || mt.VoiceEnabled != voice { + t.Fatalf("joined %+v", mt) + } + for _, fn := range before { + fn(m, mt) + } + q.stream("q1") <- Event{Type: EventState, State: StateLive} + return m, mt +} + +func TestConsentConfirmedInChat(t *testing.T) { + q := &confirmingQuorum{fakeQuorum: newFakeQuorum()} + m, mt := liveTeams(t, q, true) + eventually(t, "consent", func() bool { got, _ := m.Get(mt.ID); return got.ConsentPostedAt != nil }) + got, _ := m.Get(mt.ID) + if got.ConsentVia != ConsentViaChat || got.State != StateLive || got.ConsentError != "" { + t.Fatalf("meeting %+v", got) + } + if s := q.says(); len(s) != 0 { + t.Fatalf("a confirmed chat notice must not be spoken too: %q", s) + } +} + +func TestConsentSpokenWhenChatFails(t *testing.T) { + q := &confirmingQuorum{fakeQuorum: newFakeQuorum(), confirmEr: []error{errNoComposer}} + m, mt := liveTeams(t, q, true) + eventually(t, "consent", func() bool { got, _ := m.Get(mt.ID); return got.ConsentPostedAt != nil }) + got, _ := m.Get(mt.ID) + if got.ConsentVia != ConsentViaVoice || got.State != StateLive { + t.Fatalf("meeting %+v", got) + } + if s := q.says(); len(s) != 1 || s[0] != testConsent { + t.Fatalf("says %q, want the consent notice", s) + } + if posts, leaves := q.snapshot(); len(posts) != 0 || len(leaves) != 0 { + t.Fatalf("posts %q leaves %q", posts, leaves) + } + // Once the notice is delivered, replies go through. + if err := m.Say(context.Background(), mt.ID, "hello"); err != nil { + t.Fatal(err) + } +} + +func TestConsentUnconfirmedChatAndVoiceOffLeaves(t *testing.T) { + // Unconfirmed (an older Quorum, or no verdict in time) is not posted. + q := &confirmingQuorum{fakeQuorum: newFakeQuorum(), confirm: []bool{false}} + m, mt := liveTeams(t, q, false) + eventually(t, "left", stateIs(m, mt.ID, StateEnded)) + got, _ := m.Get(mt.ID) + if got.ConsentPostedAt != nil || got.StateReason != ReasonConsentUndelivered { + t.Fatalf("meeting %+v", got) + } + if !strings.Contains(got.ConsentError, "chat delivery not confirmed") || !strings.Contains(got.ConsentError, "voice off") { + t.Fatalf("consent error %q", got.ConsentError) + } + if _, leaves := q.snapshot(); len(leaves) != 1 || leaves[0] != "q1" { + t.Fatalf("leaves %q", leaves) + } + if s := q.says(); len(s) != 0 { + t.Fatalf("voice is off, nothing may be spoken: %q", s) + } +} + +func TestConsentChatAndVoiceFailLeaves(t *testing.T) { + fq := newFakeQuorum() + fq.sayErr = &ToolError{Tool: "say", Code: CodePlatformUnavailable, Message: "speak is not available"} + q := &confirmingQuorum{fakeQuorum: fq, confirmEr: []error{errNoComposer}} + m, mt := liveTeams(t, q, true) + eventually(t, "left", stateIs(m, mt.ID, StateEnded)) + got, _ := m.Get(mt.ID) + if got.ConsentPostedAt != nil || got.StateReason != ReasonConsentUndelivered || + !strings.Contains(got.ConsentError, "no_composer") || !strings.Contains(got.ConsentError, "voice failed") { + t.Fatalf("meeting %+v", got) + } +} + +func TestConsentVoiceCapableButMutedLeaves(t *testing.T) { + q := &confirmingQuorum{fakeQuorum: newFakeQuorum(), confirmEr: []error{errNoComposer}} + // The owner turned voice off before the bot went live: hive may not speak. + m, mt := liveTeams(t, q, true, func(m *Manager, mt Meeting) { + if _, err := m.SetVoice(mt.ID, false); err != nil { + t.Fatal(err) + } + }) + eventually(t, "left", stateIs(m, mt.ID, StateEnded)) + if s := q.says(); len(s) != 0 { + t.Fatalf("says %q", s) + } +} + +func TestConsentTransientFailuresBounded(t *testing.T) { + rl := &ToolError{Tool: "chat.post", Code: CodeRateLimited, Message: "slow down"} + q := &confirmingQuorum{fakeQuorum: newFakeQuorum(), confirmEr: []error{rl}} + m, mt := liveTeams(t, q, false) + ch := q.stream("q1") + // Each line is a chance to retry; after ConsentAttempts hive gives up. + go func() { + for i := 0; i < 20; i++ { + select { + case ch <- Event{Type: EventSegment, Speaker: &Person{Name: "Ana"}, Text: "hello", Final: true}: + case <-time.After(time.Second): + return + } + time.Sleep(5 * time.Millisecond) + } + }() + eventually(t, "left", stateIs(m, mt.ID, StateEnded)) + q.cmu.Lock() + tries := q.tries + q.cmu.Unlock() + if tries != DefaultConsentAttempts { + t.Fatalf("consent tried %d times, want %d", tries, DefaultConsentAttempts) + } + if got, _ := m.Get(mt.ID); got.StateReason != ReasonConsentUndelivered { + t.Fatalf("meeting %+v", got) + } +} + +func TestConsentTransientThenConfirmed(t *testing.T) { + nl := &ToolError{Tool: "chat.post", Code: CodeNotLive, Message: "the bot is not in the meeting"} + q := &confirmingQuorum{fakeQuorum: newFakeQuorum(), confirmEr: []error{nl, nil}} + m, mt := liveTeams(t, q, false) + eventually(t, "consent error", func() bool { got, _ := m.Get(mt.ID); return got.ConsentError != "" }) + q.stream("q1") <- Event{Type: EventSegment, Speaker: &Person{Name: "Ana"}, Text: "hello", Final: true} + eventually(t, "consent", func() bool { got, _ := m.Get(mt.ID); return got.ConsentPostedAt != nil }) + if got, _ := m.Get(mt.ID); got.ConsentVia != ConsentViaChat || got.ConsentError != "" || got.State != StateLive { + t.Fatalf("meeting %+v", got) + } +} diff --git a/src/pkg/meetings/manager.go b/src/pkg/meetings/manager.go index 323a5d738a..96008101f1 100644 --- a/src/pkg/meetings/manager.go +++ b/src/pkg/meetings/manager.go @@ -12,6 +12,15 @@ import ( "time" ) +// ConfirmingChat is a Quorum that can confirm a chat line reached the +// meeting (chat.post confirm:true, quorum 0.2.2+). The real Client +// implements it; the manager then marks the consent notice posted only on a +// confirmed delivery. A Quorum without it (in-process fakes) is trusted on a +// nil ChatPost error. +type ConfirmingChat interface { + ChatPostConfirmed(ctx context.Context, nativeID, text string) (bool, 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) @@ -32,6 +41,16 @@ const ( // DefaultConsentRetry spaces consent-notice retries while chat is // unavailable, so a platform without chat is not called on every line. DefaultConsentRetry = time.Minute + // DefaultConsentAttempts bounds how often a transient consent failure + // (not_live, rate_limited, a dropped call) is retried before hive gives + // up and leaves: it must not stay in a meeting that was never told. + DefaultConsentAttempts = 3 + // ReasonConsentUndelivered is the state reason when hive left because + // the consent notice could be delivered neither in chat nor by voice. + ReasonConsentUndelivered = "consent_undelivered" + // ConsentViaChat and ConsentViaVoice record how the notice was delivered. + ConsentViaChat = "chat" + ConsentViaVoice = "voice" // ReasonDeclined is the state reason for a platform join request the // owner declined. ReasonDeclined = "declined" @@ -104,6 +123,9 @@ type Options struct { CallTimeout time.Duration // ConsentRetry spaces consent retries; zero means DefaultConsentRetry. ConsentRetry time.Duration + // ConsentAttempts bounds consent tries after transient failures; zero + // means DefaultConsentAttempts. + ConsentAttempts int // VoiceOnAddress starts every bot able to speak (meetings.voice_on_address), // so voice can be turned on later in the meeting; whether hive actually // speaks is still the meeting's voice toggle. @@ -145,13 +167,15 @@ type Manager struct { mu sync.Mutex consentAt map[string]time.Time - watchers map[string]context.CancelFunc - lobby map[string]*time.Timer - listeners map[int]func(Update) - nextL int - ctx context.Context - cancel context.CancelFunc - wg sync.WaitGroup + // consentTries counts consent attempts per meeting. + consentTries map[string]int + watchers map[string]context.CancelFunc + lobby map[string]*time.Timer + listeners map[int]func(Update) + nextL int + ctx context.Context + cancel context.CancelFunc + wg sync.WaitGroup } // ErrInvalidRequest wraps a rejected JoinRequest. @@ -185,6 +209,9 @@ func NewManager(opts Options) (*Manager, error) { if opts.ConsentRetry <= 0 { opts.ConsentRetry = DefaultConsentRetry } + if opts.ConsentAttempts <= 0 { + opts.ConsentAttempts = DefaultConsentAttempts + } log := opts.Logger if log == nil { log = slog.New(slog.DiscardHandler) @@ -192,11 +219,12 @@ func NewManager(opts Options) (*Manager, error) { ctx, cancel := context.WithCancel(context.Background()) return &Manager{ opts: opts, log: log, leave: NewLeaveMatcher(opts.LeavePhrases), - watchers: map[string]context.CancelFunc{}, - consentAt: map[string]time.Time{}, - lobby: map[string]*time.Timer{}, - listeners: map[int]func(Update){}, - ctx: ctx, cancel: cancel, + watchers: map[string]context.CancelFunc{}, + consentAt: map[string]time.Time{}, + consentTries: map[string]int{}, + lobby: map[string]*time.Timer{}, + listeners: map[int]func(Update){}, + ctx: ctx, cancel: cancel, }, nil } @@ -544,40 +572,115 @@ func (m *Manager) handleState(ctx context.Context, id string, ev Event) bool { } else { m.clearLobby(id) } - if mt.State == StateLive { - m.ensureConsent(ctx, mt) + if mt.State == StateLive && m.ensureConsent(ctx, mt) { + return true } return mt.State.Terminal() } -// ensureConsent posts the consent notice once, the first thing hive says -// in the meeting. A failed post is retried on the next event. -func (m *Manager) ensureConsent(ctx context.Context, mt Meeting) { +// ensureConsent delivers the consent notice once, the first thing hive +// says in the meeting, and reports whether hive left the meeting because it +// could not. The notice counts as posted only when Quorum confirms the chat +// line reached the meeting. If chat cannot deliver it and the meeting has +// voice, hive speaks it instead; if neither works, hive leaves +// (ReasonConsentUndelivered) rather than stay in a meeting that was never +// told. Transient failures (the bot not admitted yet, a rate limit, a +// dropped call) are retried on later events, at most ConsentAttempts times. +func (m *Manager) ensureConsent(ctx context.Context, mt Meeting) bool { if mt.ConsentPostedAt != nil || mt.State != StateLive { - return + return false } now := m.opts.Now() m.mu.Lock() last, tried := m.consentAt[mt.ID] if tried && now.Sub(last) < m.opts.ConsentRetry { m.mu.Unlock() - return + return false } m.consentAt[mt.ID] = now + m.consentTries[mt.ID]++ + attempt := m.consentTries[mt.ID] m.mu.Unlock() + + chatErr := m.postConsentChat(ctx, mt) + if chatErr == nil { + m.markConsent(mt.ID, ConsentViaChat) + return false + } + if consentTransient(chatErr) && attempt < m.opts.ConsentAttempts { + m.log.Warn("[meetings] consent notice not posted; will retry", "meeting", mt.ID, "attempt", attempt, "error", chatErr) + if mt.ConsentError != chatErr.Error() { + _, _ = m.update(mt.ID, nil, func(x *Meeting) { x.ConsentError = chatErr.Error() }) + } + return false + } + why := "chat unavailable (" + chatErr.Error() + ")" + if mt.VoiceCapable && mt.VoiceEnabled { + cctx, cancel := context.WithTimeout(ctx, m.opts.CallTimeout) + sayErr := m.opts.Quorum.Say(cctx, mt.NativeID, m.opts.ConsentText) + 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) + return false + } + why += ", voice failed (" + sayErr.Error() + ")" + } else { + why += ", voice off" + } + m.log.Warn("[meetings] consent notice could not be delivered; leaving the meeting", "meeting", mt.ID, "reason", why) + _, _ = m.update(mt.ID, nil, func(x *Meeting) { x.ConsentError = why }) + lctx, cancel := context.WithTimeout(m.ctx, m.opts.CallTimeout) + defer cancel() + if _, err := m.Leave(lctx, mt.ID, ReasonConsentUndelivered); err != nil { + // The bot may still be in the meeting: record the end anyway so + // nothing else is said, and keep the error visible. + m.log.Warn("[meetings] leave after undelivered consent failed", "meeting", mt.ID, "error", err) + m.stopWatch(mt.ID) + _, _ = m.finish(mt.ID, StateEnded, ReasonConsentUndelivered, nil) + } + return true +} + +// postConsentChat posts the consent notice in chat. It returns nil only +// when the line is known to be in the meeting. +func (m *Manager) postConsentChat(ctx context.Context, mt Meeting) error { cctx, cancel := context.WithTimeout(ctx, m.opts.CallTimeout) defer cancel() - if err := m.opts.Quorum.ChatPost(cctx, mt.NativeID, m.opts.ConsentText); err != nil { - // Soft failure (for example platform_unavailable while the - // platform's chat is down): nothing else reaches the meeting until - // the notice is posted, and the panel keeps showing everything. - m.log.Warn("[meetings] consent notice not posted; will retry", "meeting", mt.ID, "error", err) - if mt.ConsentError != err.Error() { - _, _ = m.update(mt.ID, nil, func(x *Meeting) { x.ConsentError = err.Error() }) - } - return + cq, ok := m.opts.Quorum.(ConfirmingChat) + if !ok { + return m.opts.Quorum.ChatPost(cctx, mt.NativeID, m.opts.ConsentText) + } + confirmed, err := cq.ChatPostConfirmed(cctx, mt.NativeID, m.opts.ConsentText) + if err != nil { + return err + } + if !confirmed { + return errConsentUnconfirmed + } + return nil +} + +// errConsentUnconfirmed is a chat post Quorum accepted but could not +// confirm reached the meeting (or a Quorum too old to say). +var errConsentUnconfirmed = errors.New("chat delivery not confirmed") + +// consentTransient reports whether a failed consent post is worth retrying +// rather than a definite "chat cannot deliver this": the bot not admitted +// yet, a rate limit, or a call that never got an answer. +func consentTransient(err error) bool { + if errors.Is(err, errConsentUnconfirmed) || IsCode(err, CodePlatformUnavailable) || + IsCode(err, CodeInvalidArgument) || IsCode(err, CodeNotFound) { + return false } - _, _ = m.update(mt.ID, nil, func(x *Meeting) { x.ConsentPostedAt, x.ConsentError = timePtr(m.opts.Now()), "" }) + 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, "" + }) } // fromHive reports whether a line is hive's own (its chat posts echo back). @@ -599,7 +702,9 @@ func (m *Manager) handleLine(ctx context.Context, id string, ev Event) bool { if err != nil { return false } - m.ensureConsent(ctx, mt) + if m.ensureConsent(ctx, mt) { + return true + } if ev.Type == EventSegment && !ev.Final { m.emit(Update{Meeting: mt, Event: &ev}) return false diff --git a/src/pkg/meetings/meeting.go b/src/pkg/meetings/meeting.go index 1d9e69da6d..63d0915df0 100644 --- a/src/pkg/meetings/meeting.go +++ b/src/pkg/meetings/meeting.go @@ -82,13 +82,17 @@ type Meeting struct { // (for example platform_unavailable while chat is down). ConsentError string `json:"consent_error,omitempty"` ConsentPostedAt *time.Time `json:"consent_posted_at,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 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"` } // Person is a meeting participant as Quorum reports it. diff --git a/src/pkg/meetings/requests_test.go b/src/pkg/meetings/requests_test.go index a28a69ec9e..215863b3c9 100644 --- a/src/pkg/meetings/requests_test.go +++ b/src/pkg/meetings/requests_test.go @@ -58,15 +58,32 @@ func TestConsentErrorRecordedAndThrottled(t *testing.T) { q := newFakeQuorum() m := newTestManager(t, q, 0) m.opts.ConsentRetry = time.Hour - q.chatErr = &ToolError{Code: CodePlatformUnavailable, Message: "chat not available"} + // A transient failure (a rate limit) is recorded and retried later, not + // on every line. + q.chatErr = &ToolError{Code: CodeRateLimited, Message: "slow down"} mt, _ := m.Join(context.Background(), JoinRequest{URL: "https://meet.google.com/x"}) ch := q.stream("q1") 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 }) - if got, _ := m.Get(mt.ID); got.ConsentPostedAt != nil { - t.Fatal("a throttled retry must not post") + if got, _ := m.Get(mt.ID); got.ConsentPostedAt != nil || got.State != StateLive { + t.Fatalf("a throttled retry must not post: %+v", got) + } +} + +func TestConsentChatUnavailableWithoutVoiceLeaves(t *testing.T) { + // Chat that cannot deliver the notice (platform_unavailable) on a + // meeting without voice: hive leaves instead of listening untold. + q := newFakeQuorum() + m := newTestManager(t, q, 0) + q.chatErr = &ToolError{Code: CodePlatformUnavailable, Message: "chat not available"} + mt, _ := m.Join(context.Background(), JoinRequest{URL: "https://meet.google.com/x"}) + q.stream("q1") <- Event{Type: EventState, State: StateLive} + eventually(t, "left", stateIs(m, mt.ID, StateEnded)) + got, _ := m.Get(mt.ID) + if got.ConsentPostedAt != nil || got.StateReason != ReasonConsentUndelivered || got.ConsentError == "" { + t.Fatalf("meeting %+v", got) } }