From a0f50f813f170be9bc15e714d73753c86c0733bf Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 16 Nov 2026 22:39:36 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Betty=20names=20who=20and=20what=20?= =?UTF-8?q?each=20answer=20is=20for=20when=20requests=20overlap?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With more than one request in flight, other lines said since the request, or an answer later than 8 s, each answer opens with the asker's display name (first name when spoken, never an @mention) and a short topic phrase: the model's new "topic" label, else the request's own words. Two or more requests in hand post one queue note per minute (chat, rate limited, not charged), and spoken acks name their asker when others are waiting. Part of #18590. Signed-off-by: Andy Anderson --- .../added-18590-betty-attributed-answers.md | 1 + src/docs/meetings.md | 1 + src/pkg/meetings/betty/answer.go | 7 +- src/pkg/meetings/betty/attribution.go | 309 ++++++++++++++++++ src/pkg/meetings/betty/attribution_test.go | 190 +++++++++++ src/pkg/meetings/betty/betty.go | 7 + src/pkg/meetings/betty/persona.go | 2 +- src/pkg/meetings/betty/queue.go | 21 +- src/pkg/meetings/live/attribution_test.go | 48 +++ src/pkg/meetings/live/session.go | 15 + 10 files changed, 595 insertions(+), 6 deletions(-) create mode 100644 changelog.d/added-18590-betty-attributed-answers.md create mode 100644 src/pkg/meetings/betty/attribution.go create mode 100644 src/pkg/meetings/betty/attribution_test.go create mode 100644 src/pkg/meetings/live/attribution_test.go diff --git a/changelog.d/added-18590-betty-attributed-answers.md b/changelog.d/added-18590-betty-attributed-answers.md new file mode 100644 index 0000000000..65b20c5736 --- /dev/null +++ b/changelog.d/added-18590-betty-attributed-answers.md @@ -0,0 +1 @@ +- **Betty says who and what each answer is for when several requests overlap.** With more than one request in flight, after other people spoke, or when an answer is late, every answer opens with the asker's name and a short topic ("Andy (re: open PRs): ..." in chat, "Andy, about open PRs: ..." spoken), without @mentions that ping. With two or more requests in hand she posts one short "I have N questions; answering each as it's ready." note (at most once a minute), and a spoken acknowledgement names its asker when others are waiting. ([#18590](https://github.com/hivecommons/hive/issues/18590)) diff --git a/src/docs/meetings.md b/src/docs/meetings.md index a9ae7b1bbd..63dd31a611 100644 --- a/src/docs/meetings.md +++ b/src/docs/meetings.md @@ -33,6 +33,7 @@ Hive attends meetings as a persona: **Betty** ("Betty Bee from the hive", she/he - **Intro.** "Betty, introduce yourself", "who are you, Betty?" or "who's the bot?" gets the intro (`meetings.persona.intro_template`), which says what she does and how to send her home. - **What she answers.** When addressed she answers briefly from the hive's knowledge vaults, its beads (work items), its code maps (when `knowledge.code_maps.enabled` is on) and the web (when `meetings.persona.searxng_url` is set, see [Web answers](#web-answers)), plus the last three minutes of the meeting. If she does not know, she says so. A request that is not a question gets one clarifying question or one or two sentences on the recent discussion. - **Voice.** She speaks only when the request was spoken (addressed in the meeting's audio) or asks for it ("say it", "out loud"). She first says a short acknowledgement at once ("Let me check on that.", rotating, in the persona's language), then the answer, at most 2 sentences and 200 characters with no links or Markdown. The answer goes through the meeting's voice rules: it is spoken only when the meeting's voice is on and `meetings.voice_on_address` is true, and falls back to chat otherwise or when speaking fails; the acknowledgement is never posted to chat and is not charged to the budget. That gate is hive's, for every platform: Quorum v0.2.0 ignores `voice: false` on Discord, so hive never calls `say` for a meeting whose voice is off. A spoken "Betty?" with no question gets one clarifying question or one or two sentences on the last three minutes. +- **Several requests at once.** Each request is worked on as soon as it is heard and answered as soon as its answer is ready, so answers can arrive out of order. Whenever that could confuse the room (another request is in flight, someone else spoke since, or the answer took more than 8 s) the answer opens with who asked and a short topic phrase: "Andy (re: open PRs): ..." in chat, "Andy, about open PRs: ..." spoken (first name only). The topic is the model's 2 to 5 word label for the question, else the request's own words. Names are written plain, never as `@name`, so nobody is pinged. A single quick answer has no prefix. With two or more requests in hand she posts one short chat note ("I have 3 questions; answering each as it's ready."), at most once a minute, through the chat rate limit and never charged; a spoken acknowledgement names its asker ("Bo, one moment.") when another request is already in flight. - **Questions about the hive itself.** "How many open issues are there?", "what PRs are open in the hive repo?", "which agents are working?" are answered from the hive's live dashboard state (the same open, actionable, held and blocked counts, agents and open PRs the Overview and the dashboard chat use), read in-process, so the answer comes in seconds and never goes to research. Such a question names the hive (or "the repo") or asks for open issues, open PRs, actionable items or recent merges. - **Deep dives.** When an addressed question needs research (the model sets `needs_research`, or its confidence is below `min_confidence`) and the meeting has a research path, she says "I'll dig into that and report back", starts a research item through the same pool, cap and approval as Spek's research (shown in the panel as `betty-N`, dispatched with `betty=1` in its meta), and posts exactly one follow-up in her own voice when it completes (the summary as is if restyling fails). - **Room questions.** Betty also answers questions people ask the room (a line ending in "?" or starting with "what", "how", "who" and the like, four words or more), but only if nobody answers within `answer_wait_s` (8 seconds), the knowledge sources had a hit, the model is at least `min_confidence` (0.75) sure, and the topic was not already answered. She never posts general commentary. diff --git a/src/pkg/meetings/betty/answer.go b/src/pkg/meetings/betty/answer.go index ad70fe8a92..a9bf9d020d 100644 --- a/src/pkg/meetings/betty/answer.go +++ b/src/pkg/meetings/betty/answer.go @@ -69,6 +69,9 @@ type Answer struct { Confidence float64 `json:"confidence"` NeedsResearch bool `json:"needs_research"` Sources []string `json:"sources"` + // Topic is a short label for what was asked ("open PRs"), quoted when + // the answer is attributed. + Topic string `json:"topic,omitempty"` } // errNoJSON is returned when the reply holds no JSON object. @@ -86,11 +89,13 @@ func ParseAnswer(raw string) (Answer, error) { Confidence float64 `json:"confidence"` NeedsResearch bool `json:"needs_research"` Sources []json.RawMessage `json:"sources"` + Topic string `json:"topic"` } if err := json.Unmarshal([]byte(raw[i:j+1]), &loose); err != nil { return Answer{}, errNoJSON } - a := Answer{Answer: strings.TrimSpace(loose.Answer), Confidence: loose.Confidence, NeedsResearch: loose.NeedsResearch} + a := Answer{Answer: strings.TrimSpace(loose.Answer), Confidence: loose.Confidence, NeedsResearch: loose.NeedsResearch, + Topic: cleanTopic(loose.Topic)} for _, s := range loose.Sources { var str string if json.Unmarshal(s, &str) == nil { diff --git a/src/pkg/meetings/betty/attribution.go b/src/pkg/meetings/betty/attribution.go new file mode 100644 index 0000000000..10edbed2bd --- /dev/null +++ b/src/pkg/meetings/betty/attribution.go @@ -0,0 +1,309 @@ +package betty + +import ( + "fmt" + "strings" + "time" + "unicode" + "unicode/utf8" +) + +// Attribution (hivecommons/hive#18590): Betty works on several requests at +// once and answers each as soon as it is ready, so the order answers arrive +// in is not the order they were asked in. When that could confuse the room +// (more than one request in flight, other lines said since, or a late +// answer) every answer opens with who asked and a short topic phrase: +// "Andy (re: open PRs): ..." in chat, "Andy, about open PRs: ..." spoken. +// A single quick answer stays short, with no prefix. +// +// Names are written plain, never as "@name": a platform mention would ping +// the person, and on some chats "@everyone" style names would ping the room. +const ( + // attributeAfter is how late an answer may be before it names who and + // what even when it was the only request. + attributeAfter = 8 * time.Second + // maxTopicWords bounds the topic phrase quoted in an attribution. + maxTopicWords = 5 + // maxTopicChars bounds the topic phrase's length. + maxTopicChars = 40 + // maxNameChars bounds the asker's name in an attribution. + maxNameChars = 32 + // queueNoteGap is the least time between two "I have N questions" + // notes. + queueNoteGap = time.Minute + // minQueueNote is how many requests must be in flight before the note. + minQueueNote = 2 +) + +// TriggerQueue is the one short chat note that tells the room she has +// several requests in hand. +const TriggerQueue = "queue" + +// attrPhrases are the attribution formats in one language: chat and voice +// take the name, the topic and the answer; the "bare" forms have no topic; +// ack takes the name and the acknowledgement; queue takes the count. +type attrPhrases struct { + chat, chatBare string + voice, voiceBare string + ack string + queue string +} + +var attributions = map[string]attrPhrases{ + "en": {chat: "%s (re: %s): %s", chatBare: "%s: %s", voice: "%s, about %s: %s", voiceBare: "%s, %s", ack: "%s, %s", + queue: "I have %d questions; answering each as it's ready."}, + "es": {chat: "%s (sobre %s): %s", chatBare: "%s: %s", voice: "%s, sobre %s: %s", voiceBare: "%s, %s", ack: "%s, %s", + queue: "Tengo %d preguntas; respondo cada una en cuanto esté lista."}, + "fr": {chat: "%s (à propos de %s) : %s", chatBare: "%s : %s", voice: "%s, à propos de %s : %s", voiceBare: "%s, %s", ack: "%s, %s", + queue: "J'ai %d questions ; je réponds à chacune dès qu'elle est prête."}, + "de": {chat: "%s (zu %s): %s", chatBare: "%s: %s", voice: "%s, zu %s: %s", voiceBare: "%s, %s", ack: "%s, %s", + queue: "Ich habe %d Fragen; ich antworte auf jede, sobald sie fertig ist."}, + "pt": {chat: "%s (sobre %s): %s", chatBare: "%s: %s", voice: "%s, sobre %s: %s", voiceBare: "%s, %s", ack: "%s, %s", + queue: "Tenho %d perguntas; respondo cada uma assim que estiver pronta."}, + "it": {chat: "%s (su %s): %s", chatBare: "%s: %s", voice: "%s, riguardo a %s: %s", voiceBare: "%s, %s", ack: "%s, %s", + queue: "Ho %d domande; rispondo a ciascuna appena è pronta."}, +} + +func attrFor(lang string) attrPhrases { + primary := strings.ToLower(strings.SplitN(strings.TrimSpace(lang), "-", 2)[0]) + if p, ok := attributions[primary]; ok { + return p + } + return attributions["en"] +} + +// displayName is the asker's name as written in an attribution: chat keeps +// the display name, a spoken one only the first name. Mention marks are +// dropped so the text never pings anyone. +func displayName(who string, spoken bool) string { + who = strings.Join(strings.Fields(strings.TrimLeft(strings.TrimSpace(who), "@#<")), " ") + who = strings.TrimRight(who, ">") + if spoken { + if f := strings.Fields(who); len(f) > 0 { + who = f[0] + } + } + return clip(who, maxNameChars) +} + +// topicFillers open or close a request without saying what it is about. +var topicFillers = map[string]bool{ + "many": true, "much": true, "me": true, "us": true, "please": true, "tell": true, "give": true, + "show": true, "list": true, "quick": true, "quickly": true, "a": true, "an": true, "the": true, + "of": true, "in": true, "on": true, "to": true, "for": true, "is": true, "are": true, "there": true, + "right": true, "now": true, "currently": true, "today": true, "again": true, "remind": true, + "explain": true, "check": true, "look": true, "up": true, "find": true, "out": true, "it": true, + "i": true, "we": true, "our": true, "my": true, "be": true, "was": true, "were": true, +} + +// recase gives lower's words the casing they have in original ("prs" in +// "Betty, what PRs are open?" becomes "PRs"): a trigger's Rest is +// lowercased. +func recase(original, lower string) string { + orig := map[string]string{} + for _, w := range strings.FieldsFunc(original, topicSep) { + w = strings.Trim(w, "'.") + if k := strings.ToLower(w); k != "" { + if _, ok := orig[k]; !ok { + orig[k] = w + } + } + } + out := strings.Fields(lower) + for i, w := range out { + if o, ok := orig[strings.Trim(w, "'.")]; ok { + out[i] = o + } + } + return strings.Join(out, " ") +} + +func topicSep(r rune) bool { return unicode.IsSpace(r) || strings.ContainsRune(",;:!?\"()[]", r) } + +// topicPhrase is a short phrase for what a request is about, from its +// words: leading and trailing question and filler words dropped, at most +// maxTopicWords words, in the casing the person used ("open PRs"). +func topicPhrase(text string) string { + raw := strings.FieldsFunc(text, topicSep) + filler := func(w string) bool { + l := strings.Trim(strings.ToLower(w), "'.") + return l == "" || interrogatives[l] || topicFillers[l] || stopwords[l] + } + i, j := 0, len(raw) + for i < j && filler(raw[i]) { + i++ + } + for j > i && filler(raw[j-1]) { + j-- + } + w := raw[i:j] + if len(w) > maxTopicWords { + w = w[:maxTopicWords] + for len(w) > 1 && filler(w[len(w)-1]) { + w = w[:len(w)-1] + } + } + return cleanTopic(strings.Join(w, " ")) +} + +// cleanTopic bounds a topic phrase (the model's or the heuristic's) and +// strips what must not be quoted: mention marks, quotes, newlines and +// trailing punctuation. +func cleanTopic(t string) string { + t = strings.Join(strings.Fields(strings.NewReplacer("@", "", "\"", "", "`", "").Replace(t)), " ") + if f := strings.Fields(t); len(f) > maxTopicWords+1 { + t = strings.Join(f[:maxTopicWords+1], " ") + } + t = strings.TrimRight(t, ".,;:!? ") + if utf8.RuneCountInString(t) > maxTopicChars { + t = strings.TrimSpace(string([]rune(t)[:maxTopicChars])) + } + return t +} + +// attribute opens text with who asked and what about, in the persona's +// language. +func attribute(lang string, voice bool, who, topic, text string) string { + p := attrFor(lang) + name := displayName(who, voice) + if name == "" { + return text + } + var prefix string + switch { + case voice && topic != "": + prefix = fmt.Sprintf(p.voice, name, topic, "") + case voice: + prefix = fmt.Sprintf(p.voiceBare, name, "") + case topic != "": + prefix = fmt.Sprintf(p.chat, name, topic, "") + default: + prefix = fmt.Sprintf(p.chatBare, name, "") + } + // The answer was clipped to its limit already; the prefix (at most a + // name and a few words) rides on top rather than cutting the answer. + return prefix + text +} + +// namedAck is an acknowledgement that names who it is for: "Andy, one +// moment." +func namedAck(lang, who, ack string) string { + name := displayName(who, true) + if name == "" { + return ack + } + return fmt.Sprintf(attrFor(lang).ack, name, lowerFirst(ack)) +} + +// queueNote is the "I have N questions" note. +func queueNote(lang string, n int) string { + return fmt.Sprintf(attrFor(lang).queue, n) +} + +// lowerFirst lowercases a fixed phrase's first letter after a name prefix +// ("Andy, one moment."), leaving "I" and acronyms alone. +func lowerFirst(s string) string { + r, size := utf8.DecodeRuneInString(s) + if size == 0 || !unicode.IsUpper(r) { + return s + } + next, _ := utf8.DecodeRuneInString(s[size:]) + if r == 'I' && (next == ' ' || next == '\'' || next == '’') { + return s + } + if unicode.IsUpper(next) || unicode.IsDigit(next) { + return s + } + return string(unicode.ToLower(r)) + s[size:] +} + +// requestTopic is the topic phrase of a pending request: the model's label +// when it gave one, else the request's own words. +func requestTopic(p *pending, model string) string { + if t := cleanTopic(model); t != "" { + return t + } + if p.thoughts { + return "" + } + if p.addressed { + return topicPhrase(recase(p.line.Text, p.trig.Rest)) + } + return topicPhrase(p.line.Text) +} + +// inFlight counts the requests she is working on, but not except: the +// addressed ones, and room questions whose model call is running. Only +// addressed counts the addressed ones alone (what the room sees as +// "questions to her"). +func (e *Engine) inFlight(except *pending, onlyAddressed bool) int { + n := 0 + for _, o := range e.pending { + if o == except || o.answered { + continue + } + if o.addressed || (!onlyAddressed && o.asking) { + n++ + } + } + return n +} + +// markOverlap flags p and every other request in flight as overlapped. +func (e *Engine) markOverlap(p *pending) { + for _, o := range e.pending { + if o == p || o.answered || !(o.addressed || o.asking) { + continue + } + o.overlapped, p.overlapped = true, true + } +} + +// needsAttribution reports whether p's answer must name who and what: +// another request overlapped it, the answer is late, or something else was +// said since it was asked (by someone else, or another answer of hers). +func (e *Engine) needsAttribution(p *pending, now time.Time) bool { + if p.overlapped || now.Sub(p.created) > attributeAfter || e.lastAnswerAt.After(p.created) { + return true + } + if e.inFlight(p, false) > 0 { + return true + } + for _, l := range e.recent { + if l.at.After(p.created) && !strings.EqualFold(l.Who, p.line.Who) { + return true + } + } + return false +} + +// attributed is text as said for p: with who and what in front when the +// order could be unclear, else as is. +func (e *Engine) attributed(p *pending, voice bool, modelTopic, text string, now time.Time) string { + if strings.TrimSpace(text) == "" || !e.needsAttribution(p, now) { + return text + } + return attribute(e.s.Persona.Language, voice, p.line.Who, requestTopic(p, modelTopic), text) +} + +// ackFor is the next acknowledgement, naming who it is for when other +// requests are in flight. +func (e *Engine) ackFor(who string) string { + a := e.ack() + if e.inFlight(nil, true) > 0 { + return namedAck(e.s.Persona.Language, who, a) + } + return a +} + +// noteQueue posts one short chat note when several requests are in hand, +// at most once per queueNoteGap. +func (e *Engine) noteQueue(now time.Time) { + n := e.inFlight(nil, true) + if n < minQueueNote || (!e.queueNoteAt.IsZero() && now.Sub(e.queueNoteAt) < queueNoteGap) { + return + } + if e.emit(Reply{Kind: KindChat, Text: queueNote(e.s.Persona.Language, n), Trigger: TriggerQueue}) { + e.queueNoteAt = now + } +} diff --git a/src/pkg/meetings/betty/attribution_test.go b/src/pkg/meetings/betty/attribution_test.go new file mode 100644 index 0000000000..8d4a59e237 --- /dev/null +++ b/src/pkg/meetings/betty/attribution_test.go @@ -0,0 +1,190 @@ +package betty + +import ( + "strings" + "testing" + "time" +) + +// gatedLLM answers each request once its topic's gate is released, with a +// model topic label, so a test controls which answer is ready first. +func gatedLLM(gates map[string]chan struct{}, topics map[string]string) *fakeLLM { + return &fakeLLM{reply: func(_, prompt string) (string, error) { + for key, gate := range gates { + if strings.Contains(prompt, "Question from") && strings.Contains(promptQuestion(prompt), key) { + <-gate + return `{"answer":"Answer about ` + key + `.","confidence":0.9,"sources":[1],"topic":"` + topics[key] + `"}`, nil + } + } + return `{"answer":"Other.","confidence":0.9}`, nil + }} +} + +// promptQuestion is the "Question from" line of a prompt. +func promptQuestion(prompt string) string { + i := strings.Index(prompt, "Question from") + if i < 0 { + return "" + } + rest := prompt[i:] + if j := strings.IndexByte(rest, '\n'); j >= 0 { + rest = rest[:j] + } + return rest +} + +// Two requests in flight: a single queue note, and each answer, whichever +// is ready first, names who asked and what about. +func TestConcurrentRequestsAreAttributed(t *testing.T) { + gates := map[string]chan struct{}{"release": make(chan struct{}), "deploy": make(chan struct{})} + r := newRig(t, testSettings(), gatedLLM(gates, map[string]string{"release": "release date", "deploy": "deploy window"})) + r.e.Observe(Line{Who: "Andy Anderson", Text: "Betty, when does the release ship?"}) + r.e.Observe(Line{Who: "Bo", Text: "Betty, what is the deploy window?"}) + if n := r.reply(); n.Trigger != TriggerQueue || n.Text != "I have 2 questions; answering each as it's ready." || n.Kind != KindChat { + t.Fatalf("queue note %+v", n) + } + // The second request is ready first. + close(gates["deploy"]) + if a := r.reply(); a.Text != "Bo (re: deploy window): Answer about deploy." { + t.Fatalf("first answer %q", a.Text) + } + close(gates["release"]) + if a := r.reply(); a.Text != "Andy Anderson (re: release date): Answer about release." { + t.Fatalf("second answer %q", a.Text) + } + // A third request within the minute gets no second note. + r.e.Observe(Line{Who: "Cy", Text: "Betty, what is the plan?"}) + r.e.Observe(Line{Who: "Di", Text: "Betty, what is the status?"}) + for range 2 { + if a := r.reply(); a.Trigger == TriggerQueue { + t.Fatalf("a second queue note within a minute: %+v", a) + } + } +} + +// One request answered at once stays short; one answered late names who +// and what even alone. +func TestSingleRequestPrefixOnlyWhenLate(t *testing.T) { + r := newRig(t, testSettings(), nil) + r.llm = &fakeLLM{reply: func(string, string) (string, error) { + r.clk.add(attributeAfter + time.Second) + return `{"answer":"It ships Friday.","confidence":0.9}`, nil + }} + r.e.d.LLM = r.llm + r.e.Observe(Line{Who: "Bo", Text: "Betty, when does the release ship?"}) + if a := r.reply(); a.Text != "Bo (re: release ship): It ships Friday." { + t.Fatalf("late answer %q", a.Text) + } +} + +// Spoken requests: the ack names the asker once another request is in +// flight, and the spoken answer names them by first name. +func TestSpokenAcksAndAnswersNameTheAsker(t *testing.T) { + gates := map[string]chan struct{}{"release": make(chan struct{}), "deploy": make(chan struct{})} + r := newRig(t, testSettings(), gatedLLM(gates, map[string]string{"release": "the release", "deploy": "the deploy"})) + r.e.Observe(Line{Who: "Andy Anderson", Text: "Betty, when does the release ship?", Spoken: true}) + if a := r.reply(); a.Trigger != TriggerAck || strings.Contains(a.Text, "Andy") { + t.Fatalf("first ack %+v", a) + } + r.e.Observe(Line{Who: "Bo Li", Text: "Betty, what is the deploy window?", Spoken: true}) + if a := r.reply(); a.Trigger != TriggerAck || !strings.HasPrefix(a.Text, "Bo, ") { + t.Fatalf("second ack %+v", a) + } + if n := r.reply(); n.Trigger != TriggerQueue { + t.Fatalf("queue note %+v", n) + } + close(gates["release"]) + if a := r.reply(); a.Kind != KindVoice || a.Text != "Andy, about the release: Answer about release." { + t.Fatalf("spoken answer %+v", a) + } + close(gates["deploy"]) + if a := r.reply(); a.Text != "Bo, about the deploy: Answer about deploy." { + t.Fatalf("spoken answer %+v", a) + } +} + +// Another person speaking between the request and its answer is enough to +// name who and what. +func TestAnswerAfterOtherLinesIsAttributed(t *testing.T) { + gate := map[string]chan struct{}{"release": make(chan struct{})} + r := newRig(t, testSettings(), gatedLLM(gate, map[string]string{"release": ""})) + r.e.Observe(Line{Who: "Bo", Text: "Betty, what PRs touch the release?"}) + waitCalls(t, r.llm, 1) + r.clk.add(time.Second) + r.e.Observe(Line{Who: "Ana", Text: "meanwhile the build is green"}) + time.Sleep(50 * time.Millisecond) + close(gate["release"]) + // No model topic: the request's own words, in the casing used. + if a := r.reply(); a.Text != "Bo (re: PRs touch the release): Answer about release." { + t.Fatalf("answer %q", a.Text) + } +} + +func waitCalls(t *testing.T, f *fakeLLM, n int) { + t.Helper() + deadline := time.Now().Add(5 * time.Second) + for f.n() < n { + if time.Now().After(deadline) { + t.Fatalf("model called %d times, want %d", f.n(), n) + } + time.Sleep(5 * time.Millisecond) + } +} + +func TestTopicPhrase(t *testing.T) { + for in, want := range map[string]string{ + "how many open issues are there?": "open issues", + "what PRs are open in the hive repo": "PRs are open", + "can you tell me the deploy window": "deploy window", + "what's blocking the release on Friday": "blocking the release on Friday", + "what?": "", + "give us a quick summary of the cert rotation": "summary of the cert rotation", + } { + if got := topicPhrase(in); got != want { + t.Errorf("topicPhrase(%q) = %q, want %q", in, got, want) + } + } + if got := recase("Betty, what PRs are open?", "what prs are open"); got != "what PRs are open" { + t.Fatalf("recase = %q", got) + } + if got := cleanTopic(`"@everyone open PRs today and tomorrow and more."`); got != "everyone open PRs today and tomorrow" { + t.Fatalf("cleanTopic = %q", got) + } +} + +func TestDisplayNameNeverMentions(t *testing.T) { + for _, c := range []struct { + who string + spoken bool + want string + }{ + {"Andy Anderson", false, "Andy Anderson"}, + {"Andy Anderson", true, "Andy"}, + {"@Andy", false, "Andy"}, + {"<@1234>", false, "1234"}, + {" ", false, ""}, + } { + if got := displayName(c.who, c.spoken); got != c.want { + t.Errorf("displayName(%q, %v) = %q, want %q", c.who, c.spoken, got, c.want) + } + } + if got := attribute("en", false, "", "x", "Hello."); got != "Hello." { + t.Fatalf("no name: %q", got) + } + if got := attribute("de", true, "Andy A", "", "Ja."); got != "Andy, Ja." { + t.Fatalf("voice bare: %q", got) + } + if got := namedAck("en", "Bo Li", "One moment."); got != "Bo, one moment." { + t.Fatalf("named ack %q", got) + } + if got := namedAck("en", "Bo", "I see."); got != "Bo, I see." { + t.Fatalf("named ack keeps I: %q", got) + } +} + +func TestParseAnswerTopic(t *testing.T) { + a, err := ParseAnswer(`{"answer":"x","confidence":1,"topic":" open PRs. "}`) + if err != nil || a.Topic != "open PRs" { + t.Fatalf("%+v %v", a, err) + } +} diff --git a/src/pkg/meetings/betty/betty.go b/src/pkg/meetings/betty/betty.go index ced7c6d0e5..f316f5ac47 100644 --- a/src/pkg/meetings/betty/betty.go +++ b/src/pkg/meetings/betty/betty.go @@ -204,6 +204,10 @@ type Engine struct { // earlyAck is when each speaker was acknowledged from a partial // transcript; their final line then gets no second ack. earlyAck map[string]time.Time + // queueNoteAt is when the last "I have N questions" note went out; + // lastAnswerAt is when she last answered anything (attribution.go). + queueNoteAt time.Time + lastAnswerAt time.Time } type timedLine struct { @@ -392,6 +396,9 @@ func (e *Engine) emit(r Reply) bool { } select { case e.out <- r: + if r.Trigger != TriggerAck && r.Trigger != TriggerQueue { + e.lastAnswerAt = e.d.Now() + } return true default: e.drop("reply_backlog") diff --git a/src/pkg/meetings/betty/persona.go b/src/pkg/meetings/betty/persona.go index 46baf09fe7..0ff420139f 100644 --- a/src/pkg/meetings/betty/persona.go +++ b/src/pkg/meetings/betty/persona.go @@ -106,7 +106,7 @@ func answerPrompt(mode, who, question string, snips []Snippet, recent []recentLi } } b.WriteString(dataClose + "\n\n") - b.WriteString(`Reply with only a JSON object: {"answer": "", "confidence": <0 to 1>, "needs_research": , "sources": [""]}`) + b.WriteString(`Reply with only a JSON object: {"answer": "", "confidence": <0 to 1>, "needs_research": , "sources": [""], "topic": "<2 to 5 words naming what was asked, such as open PRs>"}`) return b.String() } diff --git a/src/pkg/meetings/betty/queue.go b/src/pkg/meetings/betty/queue.go index dfc68f2dcb..b34734c6a6 100644 --- a/src/pkg/meetings/betty/queue.go +++ b/src/pkg/meetings/betty/queue.go @@ -45,6 +45,9 @@ type pending struct { snips []Snippet // thoughts: a spoken request with no question. thoughts bool + // overlapped: another request was in flight at the same time, so its + // answer names who and what (attribution.go). + overlapped bool } type retrieval struct { @@ -157,10 +160,11 @@ func (e *Engine) addressed(l Line, trig Trigger, now time.Time) { } if trig.VoiceWanted && e.d.LLM != nil && !e.takeEarlyAck(l.Who, now) { // Speak at once so the room knows she heard; the answer follows. - e.emit(Reply{Kind: KindVoice, Text: e.ack(), Trigger: TriggerAck, Question: l.Text, VoiceWanted: true}) + e.emit(Reply{Kind: KindVoice, Text: e.ackFor(l.Who), Trigger: TriggerAck, Question: l.Text, VoiceWanted: true}) } p := e.add(l, trig, TopicKey(trig.Rest), true, now) p.waited, p.thoughts = true, thoughts + e.noteQueue(now) e.retrieve(p, trig.Rest) } @@ -198,7 +202,7 @@ func (e *Engine) onPartial(l Line) { if at, ok := e.earlyAck[l.Who]; ok && now.Sub(at) < earlyAckWindow { return } - if e.emit(Reply{Kind: KindVoice, Text: e.ack(), Trigger: TriggerAck, Question: l.Text, VoiceWanted: true}) { + if e.emit(Reply{Kind: KindVoice, Text: e.ackFor(l.Who), Trigger: TriggerAck, Question: l.Text, VoiceWanted: true}) { e.earlyAck[l.Who] = now } } @@ -268,6 +272,9 @@ func (e *Engine) add(l Line, trig Trigger, topic []string, addressed bool, now t e.nextID++ p := &pending{id: e.nextID, line: l, trig: trig, topic: topic, addressed: addressed, created: now} e.pending[p.id] = p + if addressed { + e.markOverlap(p) + } return p } @@ -352,11 +359,15 @@ func (e *Engine) proceed(p *pending) { return } if e.d.LLM == nil { - e.emit(Reply{Kind: replyKind(p.trig), Text: fallbackNoModel, Trigger: TriggerAddressed, Question: p.line.Text, VoiceWanted: p.trig.VoiceWanted}) + text := e.attributed(p, p.trig.VoiceWanted, "", fallbackNoModel, now) + e.emit(Reply{Kind: replyKind(p.trig), Text: text, Trigger: TriggerAddressed, Question: p.line.Text, VoiceWanted: p.trig.VoiceWanted}) e.finish(p, "") return } p.asking = true + if !p.addressed { + e.markOverlap(p) + } mode, timeout := modeQuestion, e.s.UnpromptedTimeout var recent []recentLine if p.addressed { @@ -399,7 +410,8 @@ func (e *Engine) onAnswer(p *pending, a answered) { e.finish(p, reason) return } - if e.emit(Reply{Text: text, Trigger: TriggerQuestion, Question: p.line.Text, NeedsResearch: a.ans.NeedsResearch, Sources: a.ans.Sources}) { + said := e.attributed(p, false, a.ans.Topic, text, now) + if e.emit(Reply{Text: said, Trigger: TriggerQuestion, Question: p.line.Text, NeedsResearch: a.ans.NeedsResearch, Sources: a.ans.Sources}) { e.posts = append(e.posts, now) e.lastPost = now e.total++ @@ -461,6 +473,7 @@ func (e *Engine) onAddressedAnswer(p *pending, a answered) { rep.NeedsResearch = true rep.Research = clip(researchQuery(p), maxResearchQuery) } + rep.Text = e.attributed(p, p.trig.VoiceWanted, a.ans.Topic, rep.Text, e.d.Now()) e.emit(rep) e.rememberTopic(p.topic) e.rememberTopic(TopicKey(text)) diff --git a/src/pkg/meetings/live/attribution_test.go b/src/pkg/meetings/live/attribution_test.go new file mode 100644 index 0000000000..2c1369a128 --- /dev/null +++ b/src/pkg/meetings/live/attribution_test.go @@ -0,0 +1,48 @@ +package live + +import ( + "context" + "strings" + "testing" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/meetings" +) + +// heldLLM answers the release question only once release is closed, so a +// second request overlaps it. +type heldLLM struct { + personaLLM + release chan struct{} +} + +func (h *heldLLM) Complete(ctx context.Context, system, prompt string) (string, error) { + if strings.Contains(prompt, "Question from Bo") { + select { + case <-h.release: + case <-ctx.Done(): + return "", ctx.Err() + } + } + return h.personaLLM.Complete(ctx, system, prompt) +} + +// Overlapping requests (hivecommons/hive#18590): one queue note in chat, +// not charged, and each answer names who asked. +func TestOverlappingRequestsQueueNoteAndAttribution(t *testing.T) { + llm := &heldLLM{release: make(chan struct{})} + h := personaHarness(t, Policy{ChatPostsPerMinute: 20}, config.MeetingsPersonaConfig{}, llm) + h.live() + h.line(meetings.EventChat, "Bo", "Betty, when does the release ship?", false) + h.line(meetings.EventChat, "Cy", "Betty, who owns the deploy pipeline?", false) + h.eventually("queue note", h.posted("I have 2 questions; answering each as it's ready.")) + h.eventually("second request answered first", h.posted("Cy (re: ")) + close(llm.release) + h.eventually("first request answered", h.posted("Bo (re: ")) + if n := h.countPosts("I have 2 questions"); n != 1 { + t.Fatalf("queue notes %d, want 1", n) + } + if st := h.state(); st.PersonaReplies != 2 { + t.Fatalf("persona replies %d: the note is not an answer", st.PersonaReplies) + } +} diff --git a/src/pkg/meetings/live/session.go b/src/pkg/meetings/live/session.go index 36ecaad3da..386c171e9d 100644 --- a/src/pkg/meetings/live/session.go +++ b/src/pkg/meetings/live/session.go @@ -701,6 +701,10 @@ func (s *session) onPersonaReply(ctx context.Context, rep betty.Reply) { if text == "" { return } + if rep.Trigger == betty.TriggerQueue { + s.onQueueNote(ctx, rep, text) + return + } switch rep.Trigger { case betty.TriggerAck: if s.state.BudgetExhausted { @@ -744,6 +748,17 @@ func (s *session) onPersonaReply(ctx context.Context, rep betty.Reply) { s.save() } +// onQueueNote posts the persona's "I have N questions" note: chat only, +// through the rate limit, never charged (no model call made it). +func (s *session) onQueueNote(ctx context.Context, rep betty.Reply, text string) { + if s.state.BudgetExhausted { + return + } + if s.post(ctx, text) { + s.emitPersona(rep) + } +} + func (s *session) emitPersona(rep betty.Reply) { raw, _ := json.Marshal(rep) s.emit(Event{Kind: EventPersona, Reason: rep.Trigger, Intent: raw})