From 7c2ed8540eca734fef4075d40898fd40a19f460a Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Wed, 18 Nov 2026 00:25:54 -0500 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8=20Betty:=20meeting-budget=20meter?= =?UTF-8?q?,=2080/90=20percent=20steps,=20and=20no=20silently=20dropped=20?= =?UTF-8?q?questions=20(#18914)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Meter per meeting and per hive on the meetings read API and in the dashboard; one 80 percent warn line per meeting; shorter answers and no new research at 90 percent; an exhaustion notice that says what to do; questions dropped for budget are counted, shown and listed in the inbox record. Signed-off-by: Andrew Anderson --- .../added-18914-betty-meeting-budget-meter.md | 1 + dashboard/openapi.json | 52 +++- src/cmd/hive/meetings_dashboard.go | 25 ++ src/cmd/hive/meetings_inbox.go | 6 + src/cmd/hive/meetings_inbox_test.go | 28 ++ src/docs/api-reference/knowledge.md | 3 +- src/docs/meetings.md | 20 +- src/pkg/dashboard/api_meetings.go | 1 + src/pkg/dashboard/api_meetings_budget.go | 34 ++ src/pkg/dashboard/api_meetings_budget_test.go | 81 +++++ src/pkg/dashboard/static-src/css/meetings.css | 5 + src/pkg/dashboard/static-src/js/meetings.js | 65 +++- src/pkg/dashboard/static/index.html | 70 ++++- src/pkg/meetings/betty/betty.go | 29 ++ src/pkg/meetings/betty/budget_test.go | 48 +++ src/pkg/meetings/betty/queue.go | 49 ++- src/pkg/meetings/live/budget.go | 293 ++++++++++++++++++ src/pkg/meetings/live/budget_test.go | 163 ++++++++++ src/pkg/meetings/live/policy.go | 30 ++ src/pkg/meetings/live/session.go | 65 +++- src/pkg/meetings/live/types.go | 16 +- 21 files changed, 1055 insertions(+), 29 deletions(-) create mode 100644 changelog.d/added-18914-betty-meeting-budget-meter.md create mode 100644 src/pkg/dashboard/api_meetings_budget.go create mode 100644 src/pkg/dashboard/api_meetings_budget_test.go create mode 100644 src/pkg/meetings/betty/budget_test.go create mode 100644 src/pkg/meetings/live/budget.go create mode 100644 src/pkg/meetings/live/budget_test.go diff --git a/changelog.d/added-18914-betty-meeting-budget-meter.md b/changelog.d/added-18914-betty-meeting-budget-meter.md new file mode 100644 index 0000000000..d2816e2637 --- /dev/null +++ b/changelog.d/added-18914-betty-meeting-budget-meter.md @@ -0,0 +1 @@ +- Betty: a visible meeting-budget meter (tokens used, percent, intents answered, hive hourly and daily ceilings) on the meeting row and live view and at `GET /api/meetings/budget`, one 80 percent warning per meeting, shorter answers and no new research at 90 percent, a notice that says what to do at 100 percent, and questions dropped for budget are counted, shown and listed as "not answered: budget" in the inbox record (#18914) diff --git a/dashboard/openapi.json b/dashboard/openapi.json index 4a685adee4..0f2a42481c 100644 --- a/dashboard/openapi.json +++ b/dashboard/openapi.json @@ -1149,6 +1149,51 @@ } } }, + "/api/meetings/budget": { + "get": { + "tags": [ + "Meetings" + ], + "summary": "Meetings budget meter", + "description": "Any authenticated role, read-only included. The per-meeting token and intent budgets, the hive-wide hourly and daily token ceilings with what is used in the rolling hour and day, and one meter per meeting: tokens used, budget, percent, intents answered, level (ok, warning at 80 percent, degraded at 90 percent, exhausted), and the number of questions skipped because the budget ran out. Numbers only: no question or transcript text (the skipped questions' text is on the owner-only GET /api/meetings/{id}).", + "responses": { + "200": { + "description": "Budget overview", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "token_budget": { + "type": "integer", + "description": "meetings.budget.max_tokens_per_meeting in effect" + }, + "intent_budget": { + "type": "integer", + "description": "meetings.budget.max_intents_per_meeting in effect" + }, + "ledger": { + "type": "object", + "description": "Hive-wide spend: hourly_used, hourly_ceiling, daily_used, daily_ceiling (a zero ceiling means none)" + }, + "meetings": { + "type": "array", + "items": { + "type": "object", + "description": "meeting_id, tokens_used, token_budget, percent, intents_answered, intent_budget, level, warned, degraded, exhausted, exhausted_by, skipped, dropped" + } + } + } + } + } + } + }, + "503": { + "description": "Meetings are not enabled on this hive" + } + } + } + }, "/api/meetings/{id}": { "delete": { "tags": [ @@ -1243,7 +1288,12 @@ } }, "live": { - "type": "object" + "type": "object", + "description": "Live-loop state. Includes skipped (the questions dropped because the budget ran out, with their text, first 200) and skipped_count." + }, + "budget": { + "type": "object", + "description": "The meeting's budget meter, as in GET /api/meetings/budget." } } } diff --git a/src/cmd/hive/meetings_dashboard.go b/src/cmd/hive/meetings_dashboard.go index 1b16deb486..2de05d84e7 100644 --- a/src/cmd/hive/meetings_dashboard.go +++ b/src/cmd/hive/meetings_dashboard.go @@ -138,6 +138,8 @@ type meetingLive struct { Meeting meetings.Meeting `json:"meeting"` Transcript []meetings.Event `json:"transcript"` Live *live.State `json:"live,omitempty"` + // Budget is the meeting's budget meter (numbers only). + Budget *live.BudgetView `json:"budget,omitempty"` } func (d *meetingsDashboard) Live(id string) (any, error) { @@ -156,11 +158,34 @@ func (d *meetingsDashboard) Live(id string) (any, error) { if d.live != nil { if st, err := d.live.State(id); err == nil { out.Live = &st + bv := live.NewBudgetView(d.live.Policy(), st, id) + out.Budget = &bv } } return out, nil } +// Budget is the hive's meetings budget: the per-meeting limits, the +// hourly and daily ceilings used, and every meeting's meter. It carries +// numbers only, so the dashboard serves it to any role. +func (d *meetingsDashboard) Budget() (any, error) { + if d.live == nil { + return live.BudgetOverview{Meetings: []live.BudgetView{}}, nil + } + list, err := d.mgr.List() + if err != nil { + return nil, meetingsDashboardError(err) + } + ids := make([]string, 0, len(list)) + for _, m := range list { + ids = append(ids, m.ID) + } + return d.live.NewBudgetOverview(ids, func(id string) (live.State, bool) { + st, err := d.live.State(id) + return st, err == nil + }), nil +} + // Subscribe forwards meeting changes, transcript lines and live-loop // events as panel frames. func (d *meetingsDashboard) Subscribe(fn func(dashboard.MeetingFrame)) func() { diff --git a/src/cmd/hive/meetings_inbox.go b/src/cmd/hive/meetings_inbox.go index f5100ac39a..e0b975af21 100644 --- a/src/cmd/hive/meetings_inbox.go +++ b/src/cmd/hive/meetings_inbox.go @@ -56,6 +56,12 @@ func (mi *meetingsInbox) build(m meetings.Meeting) (inbox.LiveMeeting, error) { out.Notes = live.RenderNotesWithReplies(mi.attendee(), m, st.Notes, tr) out.Summary = strings.Join(st.Notes[live.SectionSummary], "\n") } + // Questions nobody answered because the budget ran out stay in the + // record, so nothing asked is lost. + if skipped := live.RenderSkippedNote(st); skipped != "" { + out.Notes = strings.TrimRight(out.Notes, "\n") + "\n\n" + skipped + out.Notes = strings.TrimLeft(out.Notes, "\n") + } return out, nil } diff --git a/src/cmd/hive/meetings_inbox_test.go b/src/cmd/hive/meetings_inbox_test.go index 579a02e746..ba172c64e8 100644 --- a/src/cmd/hive/meetings_inbox_test.go +++ b/src/cmd/hive/meetings_inbox_test.go @@ -141,3 +141,31 @@ func TestWireMeetingsInbox(t *testing.T) { t.Fatalf("sources %+v", srcs) } } + +// A question dropped for the meeting budget is listed in the meeting's +// inbox record, with or without other notes, so nothing asked is lost. +func TestMeetingsInboxListsQuestionsNotAnsweredForBudget(t *testing.T) { + store, err := meetings.NewStore(t.TempDir()) + if err != nil { + t.Fatal(err) + } + now := time.Date(2026, 11, 15, 10, 0, 0, 0, time.UTC) + m := meetings.Meeting{ID: "mtg-budget", State: meetings.StateEnded, CreatedAt: now, EndedAt: &now} + if err := store.Save(m); err != nil { + t.Fatal(err) + } + skipped := []live.SkippedQuestion{{Who: "Ana", Text: "when does the release ship?"}} + if err := store.SaveDoc(m.ID, live.StateDoc, live.State{SkippedCount: 2, Skipped: skipped}); err != nil { + t.Fatal(err) + } + mi := &meetingsInbox{store: store} + got, err := mi.build(m) + if err != nil { + t.Fatal(err) + } + for _, want := range []string{"## Not answered: budget", "Ana: when does the release ship?", "and 1 more not listed"} { + if !strings.Contains(got.Notes, want) { + t.Errorf("inbox notes lack %q:\n%s", want, got.Notes) + } + } +} diff --git a/src/docs/api-reference/knowledge.md b/src/docs/api-reference/knowledge.md index 3acc54c55c..eb26b64647 100644 --- a/src/docs/api-reference/knowledge.md +++ b/src/docs/api-reference/knowledge.md @@ -96,7 +96,7 @@ Part of the [Dashboard REST API reference](../api-reference.md); see that page f | `POST` | `/api/meetings/settings` | Owner only | Update `meetings.enabled` and `meetings.vexa.url`, and write the Vexa tenant key, Discord bot token, transcription service token and TTS service token to keystore-sealed secret files (`""` clears one): `{enabled?, vexa_url?, vexa_api_key?, discord_token?, transcribe_token?, tts_token?}`. Returns the settings with `restart_required`; 400 on an invalid value. Audited as `meetings_settings` | `pkg/dashboard/api_meetings.go:130` | | `POST` | `/api/meetings` | Owner only | Send hive to a meeting: `{url}` for Google Meet, Teams or Zoom, or `{platform: "discord", guild_id, channel_id}`, plus `title?` and `voice?`. Returns the meeting record; 400 invalid request, 502 Quorum refused, 503 meetings off. Audited as `meeting_join` | `pkg/dashboard/api_meetings.go:131` | | `GET` | `/api/meetings` | Owner only | `{meetings: [...], email_in}`, newest first; `email_in` is the hive's email-in address as in `GET /api/inbox` | `pkg/dashboard/api_meetings.go:132` | -| `GET` | `/api/meetings/{id}` | Owner only | `{meeting, transcript, live}`: the record, stored final segments and chat, and the live-loop state (notes, research, asks, budget); 404 unknown | `pkg/dashboard/api_meetings.go:133` | +| `GET` | `/api/meetings/{id}` | Owner only | `{meeting, transcript, live}`: the record, stored final segments and chat, and the live-loop state (notes, research, asks, budget, and `skipped`: the questions dropped because the budget ran out, with their text) plus `budget`, the meter; 404 unknown | `pkg/dashboard/api_meetings.go:133` | | `POST` | `/api/meetings/{id}/leave` | Owner only | Make hive leave; returns the record. Audited as `meeting_leave` | `pkg/dashboard/api_meetings.go:134` | | `POST` | `/api/meetings/{id}/voice` | Owner only | `{enabled}`: allow hive to answer aloud when addressed (with `meetings.voice_on_address`). Audited as `meeting_voice` | `pkg/dashboard/api_meetings.go:135` | | `POST` | `/api/meetings/{id}/research/{rid}/approve` | Owner only | Dispatch a research item waiting past the per-meeting cap; 409 when it is not waiting or the live session ended. Audited as `meeting_research_approve` | `pkg/dashboard/api_meetings.go:136` | @@ -105,6 +105,7 @@ Part of the [Dashboard REST API reference](../api-reference.md); see that page f | `DELETE` | `/api/meetings/{id}` | Owner only | Delete a meeting in a final state (ended, failed, blocked, declined, left, handed off, purged) or a pending join request (declined with `meeting.leave`, then removed): the record, transcript and live state on this hive, the meeting in Vexa (Quorum `meeting.delete`, best effort: `not_found` is fine, any other error comes back as `upstream_error` while the record is still removed), and the Calendar entry that existed only for it. Knowledge facts and drafts made from it are not touched. 200 `{id, state, upstream_error?, calendar_removed?}`; 404 unknown; 409 while hive is joining, in the lobby or live (leave the meeting first). Audited as `meeting_delete` | `pkg/dashboard/api_meetings_delete.go:44` | | `POST` | `/api/meetings/clear-finished` | Owner only | Delete every meeting in a final state that finished before `{before?}` (RFC 3339; empty or later than now means now), each as `DELETE /api/meetings/{id}`; live meetings and pending join requests stay. 200 `{deleted, count}`; 400 bad `before`. Each meeting audited as `meeting_delete` | `pkg/dashboard/api_meetings_delete.go:45` | | `GET` | `/api/meetings/{id}/ws` | Owner only | WebSocket live panel: a `snapshot` frame, then `meeting`, `line`, `intent`, `research`, `notes` and `dropped` frames for the meeting | `pkg/dashboard/api_meetings.go:139` | +| `GET` | `/api/meetings/budget` | Any role | The meetings budget meter: `token_budget`, `intent_budget`, `ledger` (`hourly_used`, `hourly_ceiling`, `daily_used`, `daily_ceiling`) and `meetings`: per meeting `tokens_used`, `token_budget`, `percent`, `intents_answered`, `level` (`ok`, `warning` from 80 percent, `degraded` from 90, `exhausted`), `skipped`. Numbers only, no question text, so read-only roles may read it | `pkg/dashboard/api_meetings.go:140` | | `GET` | `/api/meetings/schedule` | Owner or read-write | The Calendar: `{upcoming, recent, attendee_name, email_in}` (`email_in` is the hive's email-in address as in `GET /api/inbox`); upcoming events (scheduled, awaiting approval, live) by start and events finished in the last 14 days, newest first. Each event carries its platform, source, status, next occurrences and the meeting it created; a passcode is shown only as `set`. See [Calendar](../meetings.md#calendar) | `pkg/dashboard/api_meetings_schedule.go:86` | | `POST` | `/api/meetings/schedule` | Owner or read-write | Schedule a meeting: `{url, start, end?, title?, rrule?, timezone?}` (RFC 3339 times, RFC 5545 `RRULE` expanded in an IANA `timezone`). The platform comes from the link (Google Meet, Teams, Zoom). The owner's add is scheduled; a read-write user's add awaits approval. 400 invalid or already ended. Audited as `meeting_schedule_add` | `pkg/dashboard/api_meetings_schedule.go:87` | | `POST` | `/api/meetings/schedule/{eid}/approve` | Owner only | Schedule an event awaiting approval. 409 when it is not awaiting approval. Audited as `meeting_schedule_approve` | `pkg/dashboard/api_meetings_schedule.go:88` | diff --git a/src/docs/meetings.md b/src/docs/meetings.md index fced2d5444..f701d27123 100644 --- a/src/docs/meetings.md +++ b/src/docs/meetings.md @@ -152,6 +152,24 @@ The warm process is replaced after `HIVE_MEETINGS_CLI_RECYCLE_TURNS` turns (defa Spektacular must be a version that includes the `meeting` command ([hivecommons/spektacular#126](https://github.com/hivecommons/spektacular/pull/126)). With an older binary the live mode exits at once; hive keeps the meeting going (transcript, consent, `hive leave`, research approvals, the panel) without Spek's questions and notes, and the panel says plainly that notes and clarifying questions are off for the meeting. While hive cannot reach a Spektacular child that is restarting, the panel says they are paused, and clears it when Spek writes again. +## The meeting budget + +Every meeting has its own token budget (`meetings.budget.max_tokens_per_meeting`, 200000 by default), and the hive has hourly and daily ceilings across all meetings (`meetings.budget.max_tokens_per_hour`, `max_tokens_per_day`). When Spek reports no usage, `meetings.budget.max_intents_per_meeting` (500) caps intents instead. + +**How it is counted.** The meeting's usage ledger charges fresh input tokens, output tokens and cache writes in full, and cache reads at a tenth (the weight set in #18911: a warm session re-reads its whole history every turn, and counting those reads in full spent a meeting budget in about fifteen answers). Each answer Betty gives also counts as one intent. Hive-wide ceilings use the same token count. + +**Where the owner changes it.** In `hive.yaml` under `meetings.budget`, or with `HIVE_MEETINGS_MAX_TOKENS_PER_MEETING`, `HIVE_MEETINGS_MAX_TOKENS_PER_HOUR`, `HIVE_MEETINGS_MAX_TOKENS_PER_DAY` and `HIVE_MEETINGS_MAX_INTENTS_PER_MEETING`; Settings, Meetings shows the values in effect. The 80 and 90 percent steps below are fixed behaviour, not settings. + +**The meter.** The meeting row and the live meeting view show a slim meter with the percent used, with the text alternative "62% of the meeting budget used". `GET /api/meetings/budget` returns it for every meeting plus the hourly and daily ceilings used, to any role, as numbers only; the owner-only `GET /api/meetings/{id}` also carries the questions that were skipped. + +| Used | What happens | +|---|---| +| 80% | One warn log line per meeting (`[meetings] meeting budget at 80%`, ids and numbers only) and a "budget 80%" badge on the meeting. Nothing changes in the meeting. | +| 90% | Betty says once that answers will be shorter and no new long research runs will start. From then on she answers in one short sentence (at most 160 characters) and does not start deep dives; research Spek asks for is not started. Research already running finishes, and the owner can still approve a waiting item by hand. | +| 100% | Betty posts the existing notice, now saying what to do: ask the owner to raise the budget or start a new meeting. She keeps taking notes but stops asking, answering and researching. | + +A question dropped because the budget ran out is never lost. It is counted, shown in the live meeting view, and listed under "Not answered: budget" in the meeting's inbox record. While the budget is out, Betty repeats the notice with the running count of skipped questions each time that count doubles (1, 2, 4, 8, and so on), so the room learns questions are being skipped without a post for each one. Logs and audit carry counts and ids only, never the question text. + ## Using it Only the hive owner can send hive to a meeting. @@ -456,4 +474,4 @@ Every key has an environment override that wins over `hive.yaml`. The full list | `meetings.persona.unprompted.stale_s` | `60` | Seconds before a waiting question is dropped. | | `meetings.persona.searxng_url` | none | SearXNG instance for web answers (for example `http://searxng.meetings.svc.cluster.local:8080`). | -When the budget runs out, hive posts one notice in the meeting and keeps taking notes, but stops asking questions, starting research and speaking. +When the budget runs out, hive posts one notice in the meeting and keeps taking notes, but stops asking questions, starting research and speaking. See [The meeting budget](#the-meeting-budget) for how it is counted and what happens at 80, 90 and 100 percent. diff --git a/src/pkg/dashboard/api_meetings.go b/src/pkg/dashboard/api_meetings.go index 49990f6f44..c603bfb70e 100644 --- a/src/pkg/dashboard/api_meetings.go +++ b/src/pkg/dashboard/api_meetings.go @@ -137,6 +137,7 @@ func (s *Server) registerMeetingsRoutes() { s.mux.HandleFunc("POST /api/meetings/{id}/approve", s.handleMeetingsRequestApprove) s.mux.HandleFunc("POST /api/meetings/{id}/decline", s.handleMeetingsRequestDecline) s.mux.HandleFunc("GET /api/meetings/{id}/ws", s.handleMeetingsWS) + s.mux.HandleFunc("GET /api/meetings/budget", s.handleMeetingsBudget) s.registerMeetingsDeleteRoutes() } diff --git a/src/pkg/dashboard/api_meetings_budget.go b/src/pkg/dashboard/api_meetings_budget.go new file mode 100644 index 0000000000..2ba7f05d1f --- /dev/null +++ b/src/pkg/dashboard/api_meetings_budget.go @@ -0,0 +1,34 @@ +package dashboard + +import "net/http" + +// MeetingsBudgetService is the optional budget view of a MeetingsService +// (hivecommons/hive#18914): the per-meeting limits, the hive-wide hourly +// and daily ceilings used, and each meeting's meter. It carries numbers +// only, never question or transcript text. +type MeetingsBudgetService interface { + Budget() (any, error) +} + +// handleMeetingsBudget answers GET /api/meetings/budget. Unlike the rest +// of the meetings API it is open to every authenticated role, read-only +// included: the meter is how an operator sees that Betty is about to stop +// answering. The skipped questions' text stays on the owner-only +// GET /api/meetings/{id}. +func (s *Server) handleMeetingsBudget(w http.ResponseWriter, r *http.Request) { + svc, ok := s.meetingsService(w) + if !ok { + return + } + b, ok := svc.(MeetingsBudgetService) + if !ok { + jsonError(w, "meeting budget is not available on this hive", http.StatusServiceUnavailable) + return + } + out, err := b.Budget() + if err != nil { + s.meetingsError(w, r, err) + return + } + jsonResponse(w, out) +} diff --git a/src/pkg/dashboard/api_meetings_budget_test.go b/src/pkg/dashboard/api_meetings_budget_test.go new file mode 100644 index 0000000000..b1dc79b34b --- /dev/null +++ b/src/pkg/dashboard/api_meetings_budget_test.go @@ -0,0 +1,81 @@ +package dashboard + +import ( + "net/http" + "strings" + "testing" +) + +const budgetTestQuestion = "Betty, when does the release ship?" + +// budgetMeetingsService adds the budget view to the fake meetings service. +// Its owner-only Live carries a skipped question's text; the budget never +// does. +type budgetMeetingsService struct{ fakeMeetingsService } + +func (b *budgetMeetingsService) Budget() (any, error) { + return map[string]any{ + "token_budget": 200000, "intent_budget": 500, + "ledger": map[string]any{"hourly_used": 10, "hourly_ceiling": 500000, "daily_used": 20, "daily_ceiling": 2000000}, + "meetings": []map[string]any{{"meeting_id": "mtg-1", "tokens_used": 124000, "token_budget": 200000, "percent": 62, "level": "ok", "skipped": 1}}, + }, nil +} + +func (b *budgetMeetingsService) Live(id string) (any, error) { + return map[string]any{"meeting": map[string]string{"id": id}, "live": map[string]any{"skipped": []map[string]string{{"text": budgetTestQuestion}}}}, nil +} + +// A read-only role sees the meter but never question text; the text is on +// the owner-only meeting route. +func TestMeetingsBudgetReadableByAnyRoleWithoutQuestionText(t *testing.T) { + s, _ := apiServer(t) + if rec := doGetRole(s, "/api/meetings/budget", "read", false); rec.Code != http.StatusServiceUnavailable { + t.Fatalf("no service = %d", rec.Code) + } + s.SetMeetingsService(&budgetMeetingsService{}) + rec := doGetRole(s, "/api/meetings/budget", "read", false) + if rec.Code != http.StatusOK { + t.Fatalf("read-only role on the meter = %d: %s", rec.Code, rec.Body) + } + body := rec.Body.String() + for _, want := range []string{`"percent":62`, `"hourly_ceiling":500000`, `"daily_used":20`, `"skipped":1`} { + if !strings.Contains(body, want) { + t.Errorf("meter lacks %s: %s", want, body) + } + } + if strings.Contains(body, "release") { + t.Fatalf("the meter must carry no question text: %s", body) + } + if rec := doGetRole(s, "/api/meetings/mtg-1", "read", false); rec.Code != http.StatusForbidden { + t.Fatalf("read-only role on the meeting = %d, want 403", rec.Code) + } + if rec := doOwnerGet(s, "/api/meetings/mtg-1"); !strings.Contains(rec.Body.String(), budgetTestQuestion) { + t.Fatalf("the owner sees the skipped question: %s", rec.Body) + } +} + +// A meetings service without a budget view answers 503, not a panic. +func TestMeetingsBudgetUnsupportedService(t *testing.T) { + s, _ := apiServer(t) + s.SetMeetingsService(&fakeMeetingsService{}) + if rec := doGetRole(s, "/api/meetings/budget", "read", false); rec.Code != http.StatusServiceUnavailable { + t.Fatalf("service without a budget = %d", rec.Code) + } +} + +// The dashboard renders the meter with a text alternative and the badge for +// each budget level, and lists skipped questions; the JS is generated from +// static-src. +func TestMeetingsBudgetMeterMarkup(t *testing.T) { + html := indexHTML(t) + for _, want := range []string{ + "% of the meeting budget used", + "meetingsBudgetHtml(_meetingsBudget[m.id])", + "Not answered: budget", + "/api/meetings/budget", + } { + if !strings.Contains(html, want) { + t.Errorf("dashboard lacks %q", want) + } + } +} diff --git a/src/pkg/dashboard/static-src/css/meetings.css b/src/pkg/dashboard/static-src/css/meetings.css index b65494ed66..0f479e32a0 100644 --- a/src/pkg/dashboard/static-src/css/meetings.css +++ b/src/pkg/dashboard/static-src/css/meetings.css @@ -14,6 +14,11 @@ .meetings-row-error { font-size: var(--fs-xs); color: var(--red); overflow-wrap: anywhere; } .meetings-row-raw { margin-top: var(--sp-1); font-family: var(--font-mono); font-size: var(--fs-xs); color: var(--muted); white-space: pre-wrap; overflow-wrap: anywhere; } .meetings-row-actions { display: flex; flex-wrap: wrap; align-items: center; gap: var(--sp-2); } + .meetings-budget { display: flex; flex-wrap: wrap; align-items: center; gap: var(--sp-2); } + .meetings-meter { flex: 0 0 8em; height: var(--sp-2); } + .meetings-budget[data-level="warning"] .muted-xs, .meetings-budget[data-level="degraded"] .muted-xs { color: var(--yellow); } + .meetings-budget[data-level="exhausted"] .muted-xs { color: var(--red); } + .meetings-skipped { margin: 0; padding-left: var(--sp-4); font-size: var(--fs-sm); overflow-wrap: anywhere; } .meetings-live { display: grid; grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); gap: var(--sp-4); } .meetings-transcript { max-height: 28em; overflow: auto; font-size: var(--fs-sm); padding: var(--sp-2); border: 1px solid var(--border); border-radius: var(--r); background: var(--bg); } .meetings-line { margin: 0 0 var(--sp-1); white-space: pre-wrap; } diff --git a/src/pkg/dashboard/static-src/js/meetings.js b/src/pkg/dashboard/static-src/js/meetings.js index dfa3a16e5b..f48502e2ba 100644 --- a/src/pkg/dashboard/static-src/js/meetings.js +++ b/src/pkg/dashboard/static-src/js/meetings.js @@ -10,6 +10,11 @@ const MEETINGS_LIVE_STATES = { requested: 1, joining: 1, lobby: 1, live: 1 }; let _meetingsList = null; let _meetingsError = ''; + // Budget meters by meeting id and the hive-wide ceilings, from GET /api/meetings/budget. + let _meetingsBudget = {}; + let _meetingsLedger = null; + // Levels the meter badges (the server's budget steps). + const MEETINGS_BUDGET_BADGES = { warning: 'budget 80%', degraded: 'budget 90%: shorter answers', exhausted: 'budget exhausted' }; let _meetingsOpen = null; // { id, ws, meeting, lines, partial, intents, live } // Ended meetings are inbox items too (source "meeting"). if (typeof INBOX_SOURCE_ICONS === 'object' && !INBOX_SOURCE_ICONS.meeting) INBOX_SOURCE_ICONS.meeting = '🎙'; @@ -60,8 +65,55 @@ if (_meetingsList === null) _meetingsList = []; _meetingsError = 'Meetings are unreachable right now; retrying.'; } + await fetchMeetingsBudget(); renderMeetings(); } + + // The meter is numbers only; a failure leaves the last one. + async function fetchMeetingsBudget() { + try { + const r = await fetch('/api/meetings/budget'); + if (!r.ok) return; + const data = await r.json(); + const byId = {}; + (data.meetings || []).forEach(b => { byId[b.meeting_id] = b; }); + _meetingsBudget = byId; + _meetingsLedger = data.ledger || null; + } catch (e) { /* transient */ } + } + + // meetingsBudgetText is the meter's text alternative. + function meetingsBudgetText(b) { + return (b.percent || 0) + '% of the meeting budget used'; + } + + function meetingsBudgetHtml(b) { + if (!b || !b.token_budget) return ''; + const text = meetingsBudgetText(b); + const badge = MEETINGS_BUDGET_BADGES[b.level]; + const detail = (b.tokens_used || 0) + ' of ' + b.token_budget + ' tokens · ' + (b.intents_answered || 0) + ' answered' + + (b.skipped ? ' · ' + b.skipped + ' question' + (b.skipped === 1 ? '' : 's') + ' not answered' : ''); + return '
' + + '' + escapeHtml(text) + '' + + '' + escapeHtml(text) + ' · ' + escapeHtml(detail) + '' + + (badge ? '' + escapeHtml(badge) + '' : '') + + '
'; + } + + // The ceilings hive-wide, for the live view. + function meetingsLedgerText(l) { + if (!l) return ''; + const part = (name, used, ceil) => ceil ? name + ' ' + used + ' of ' + ceil + ' tokens' : ''; + return [part('hour', l.hourly_used || 0, l.hourly_ceiling), part('day', l.daily_used || 0, l.daily_ceiling)].filter(Boolean).join(' · '); + } + + // The questions skipped for budget, with their text (owner view only). + function meetingsSkippedHtml(live) { + const list = (live && live.skipped) || []; + if (!list.length) return ''; + return '
    ' + + list.map(q => '
  • ' + escapeHtml((q.who ? q.who + ': ' : '') + (q.text || '')) + '
  • ').join('') + '
'; + } dashboardScheduleWork('meetings', fetchMeetings).catch(console.error); dashboardSetInterval('meetings', fetchMeetings, MEETINGS_POLL_MS); @@ -139,6 +191,7 @@ '' + escapeHtml(meetingsLabel(m)) + '' + '' + escapeHtml(state) + '' + (meta ? '
' + escapeHtml(meta) + '
' : '') + + meetingsBudgetHtml(_meetingsBudget[m.id]) + meetingsReasonHtml(m) + (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
' : '') + @@ -373,6 +426,8 @@ break; } case 'notes': meetingsRefreshLive(o.id); break; + case 'budget': meetingsRefreshLive(o.id); break; + case 'dropped': meetingsRefreshLive(o.id); break; case 'reach': meetingsRefreshLive(o.id); break; case 'spek': meetingsRefreshLive(o.id); break; default: break; @@ -385,7 +440,11 @@ const r = await fetch('/api/meetings/' + encodeURIComponent(id)); if (!r.ok) return; const data = await r.json(); - if (_meetingsOpen && _meetingsOpen.id === id) { _meetingsOpen.live = data.live || null; renderMeetingsLive(); } + if (_meetingsOpen && _meetingsOpen.id === id) { + _meetingsOpen.live = data.live || null; + if (data.budget) _meetingsBudget[id] = data.budget; + renderMeetingsLive(); + } } catch (e) { /* transient */ } } @@ -416,9 +475,11 @@ const intents = o.intents.map(i => '
' + escapeHtml(typeof i === 'string' ? i : JSON.stringify(i)) + '
').join(''); const spek = live.spek_status ? '

' + escapeHtml(live.spek_status) + '

' : (live.spek_mode ? '

Spektacular: ' + escapeHtml(live.spek_mode) + '

' : ''); - host.innerHTML = spek + meetingsReachHtml(live) + '
' + + const ledger = meetingsLedgerText(_meetingsLedger); + host.innerHTML = spek + meetingsBudgetHtml(_meetingsBudget[o.id]) + (ledger ? '

Hive-wide: ' + escapeHtml(ledger) + '

' : '') + meetingsReachHtml(live) + '
' + '
' + (lines || '

Waiting for the first words…

') + '
' + '
' + notesHtml + '
' + + meetingsSkippedHtml(live) + '
' + research + '
' + '
' + (intents || '

None yet.

') + '
'; const tr = host.querySelector('.meetings-transcript'); diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index 662f272a3b..f8431305bd 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -6316,6 +6316,11 @@ .meetings-row-error { font-size: var(--fs-xs); color: var(--red); overflow-wrap: anywhere; } .meetings-row-raw { margin-top: var(--sp-1); font-family: var(--font-mono); font-size: var(--fs-xs); color: var(--muted); white-space: pre-wrap; overflow-wrap: anywhere; } .meetings-row-actions { display: flex; flex-wrap: wrap; align-items: center; gap: var(--sp-2); } + .meetings-budget { display: flex; flex-wrap: wrap; align-items: center; gap: var(--sp-2); } + .meetings-meter { flex: 0 0 8em; height: var(--sp-2); } + .meetings-budget[data-level="warning"] .muted-xs, .meetings-budget[data-level="degraded"] .muted-xs { color: var(--yellow); } + .meetings-budget[data-level="exhausted"] .muted-xs { color: var(--red); } + .meetings-skipped { margin: 0; padding-left: var(--sp-4); font-size: var(--fs-sm); overflow-wrap: anywhere; } .meetings-live { display: grid; grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); gap: var(--sp-4); } .meetings-transcript { max-height: 28em; overflow: auto; font-size: var(--fs-sm); padding: var(--sp-2); border: 1px solid var(--border); border-radius: var(--r); background: var(--bg); } .meetings-line { margin: 0 0 var(--sp-1); white-space: pre-wrap; } @@ -51646,6 +51651,11 @@ const MEETINGS_LIVE_STATES = { requested: 1, joining: 1, lobby: 1, live: 1 }; let _meetingsList = null; let _meetingsError = ''; + // Budget meters by meeting id and the hive-wide ceilings, from GET /api/meetings/budget. + let _meetingsBudget = {}; + let _meetingsLedger = null; + // Levels the meter badges (the server's budget steps). + const MEETINGS_BUDGET_BADGES = { warning: 'budget 80%', degraded: 'budget 90%: shorter answers', exhausted: 'budget exhausted' }; let _meetingsOpen = null; // { id, ws, meeting, lines, partial, intents, live } // Ended meetings are inbox items too (source "meeting"). if (typeof INBOX_SOURCE_ICONS === 'object' && !INBOX_SOURCE_ICONS.meeting) INBOX_SOURCE_ICONS.meeting = '🎙'; @@ -51696,8 +51706,55 @@ if (_meetingsList === null) _meetingsList = []; _meetingsError = 'Meetings are unreachable right now; retrying.'; } + await fetchMeetingsBudget(); renderMeetings(); } + + // The meter is numbers only; a failure leaves the last one. + async function fetchMeetingsBudget() { + try { + const r = await fetch('/api/meetings/budget'); + if (!r.ok) return; + const data = await r.json(); + const byId = {}; + (data.meetings || []).forEach(b => { byId[b.meeting_id] = b; }); + _meetingsBudget = byId; + _meetingsLedger = data.ledger || null; + } catch (e) { /* transient */ } + } + + // meetingsBudgetText is the meter's text alternative. + function meetingsBudgetText(b) { + return (b.percent || 0) + '% of the meeting budget used'; + } + + function meetingsBudgetHtml(b) { + if (!b || !b.token_budget) return ''; + const text = meetingsBudgetText(b); + const badge = MEETINGS_BUDGET_BADGES[b.level]; + const detail = (b.tokens_used || 0) + ' of ' + b.token_budget + ' tokens · ' + (b.intents_answered || 0) + ' answered' + + (b.skipped ? ' · ' + b.skipped + ' question' + (b.skipped === 1 ? '' : 's') + ' not answered' : ''); + return '
' + + '' + escapeHtml(text) + '' + + '' + escapeHtml(text) + ' · ' + escapeHtml(detail) + '' + + (badge ? '' + escapeHtml(badge) + '' : '') + + '
'; + } + + // The ceilings hive-wide, for the live view. + function meetingsLedgerText(l) { + if (!l) return ''; + const part = (name, used, ceil) => ceil ? name + ' ' + used + ' of ' + ceil + ' tokens' : ''; + return [part('hour', l.hourly_used || 0, l.hourly_ceiling), part('day', l.daily_used || 0, l.daily_ceiling)].filter(Boolean).join(' · '); + } + + // The questions skipped for budget, with their text (owner view only). + function meetingsSkippedHtml(live) { + const list = (live && live.skipped) || []; + if (!list.length) return ''; + return '
    ' + + list.map(q => '
  • ' + escapeHtml((q.who ? q.who + ': ' : '') + (q.text || '')) + '
  • ').join('') + '
'; + } dashboardScheduleWork('meetings', fetchMeetings).catch(console.error); dashboardSetInterval('meetings', fetchMeetings, MEETINGS_POLL_MS); @@ -51775,6 +51832,7 @@ '' + escapeHtml(meetingsLabel(m)) + '' + '' + escapeHtml(state) + '
' + (meta ? '
' + escapeHtml(meta) + '
' : '') + + meetingsBudgetHtml(_meetingsBudget[m.id]) + meetingsReasonHtml(m) + (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
' : '') + @@ -52009,6 +52067,8 @@ break; } case 'notes': meetingsRefreshLive(o.id); break; + case 'budget': meetingsRefreshLive(o.id); break; + case 'dropped': meetingsRefreshLive(o.id); break; case 'reach': meetingsRefreshLive(o.id); break; case 'spek': meetingsRefreshLive(o.id); break; default: break; @@ -52021,7 +52081,11 @@ const r = await fetch('/api/meetings/' + encodeURIComponent(id)); if (!r.ok) return; const data = await r.json(); - if (_meetingsOpen && _meetingsOpen.id === id) { _meetingsOpen.live = data.live || null; renderMeetingsLive(); } + if (_meetingsOpen && _meetingsOpen.id === id) { + _meetingsOpen.live = data.live || null; + if (data.budget) _meetingsBudget[id] = data.budget; + renderMeetingsLive(); + } } catch (e) { /* transient */ } } @@ -52052,9 +52116,11 @@ const intents = o.intents.map(i => '
' + escapeHtml(typeof i === 'string' ? i : JSON.stringify(i)) + '
').join(''); const spek = live.spek_status ? '

' + escapeHtml(live.spek_status) + '

' : (live.spek_mode ? '

Spektacular: ' + escapeHtml(live.spek_mode) + '

' : ''); - host.innerHTML = spek + meetingsReachHtml(live) + '
' + + const ledger = meetingsLedgerText(_meetingsLedger); + host.innerHTML = spek + meetingsBudgetHtml(_meetingsBudget[o.id]) + (ledger ? '

Hive-wide: ' + escapeHtml(ledger) + '

' : '') + meetingsReachHtml(live) + '
' + '
' + (lines || '

Waiting for the first words…

') + '
' + '
' + notesHtml + '
' + + meetingsSkippedHtml(live) + '
' + research + '
' + '
' + (intents || '

None yet.

') + '
'; const tr = host.querySelector('.meetings-transcript'); diff --git a/src/pkg/meetings/betty/betty.go b/src/pkg/meetings/betty/betty.go index 7a71d13292..e66de980ab 100644 --- a/src/pkg/meetings/betty/betty.go +++ b/src/pkg/meetings/betty/betty.go @@ -226,6 +226,11 @@ type Engine struct { retrieved chan retrieval answers chan answered exhausted atomic.Bool + // degraded is set at BudgetDegradePercent of the meeting budget: + // shorter answers, no deep dives (budget.go). + degraded atomic.Bool + // onSkip observes a question dropped for budget; set before Start. + onSkip func(BudgetSkip) startOnce sync.Once closeOnce sync.Once @@ -361,6 +366,30 @@ func (e *Engine) Offer(text string) { // spent. func (e *Engine) SetExhausted() { e.exhausted.Store(true) } +// SetDegraded shortens answers and stops asking for deep dives once the +// meeting's budget is nearly spent. +func (e *Engine) SetDegraded() { e.degraded.Store(true) } + +// BudgetSkip is a question the engine did not answer because the meeting's +// budget is spent. The text is meeting speech: the session keeps it in the +// meeting's own records, never in logs. +type BudgetSkip struct { + Who string + Text string + Addressed bool +} + +// OnBudgetSkip sets the observer of questions dropped for budget. It must +// be set before Start and must not block. +func (e *Engine) OnBudgetSkip(fn func(BudgetSkip)) { e.onSkip = fn } + +func (e *Engine) budgetSkip(l Line, addressed bool) { + e.drop("budget") + if e.onSkip != nil { + e.onSkip(BudgetSkip{Who: l.Who, Text: l.Text, Addressed: addressed}) + } +} + // Skip reports whether a line is for the persona rather than an answer to // someone's question: addressed to her, or a leave command. func (e *Engine) Skip(text string) bool { diff --git a/src/pkg/meetings/betty/budget_test.go b/src/pkg/meetings/betty/budget_test.go new file mode 100644 index 0000000000..cd701a3f1b --- /dev/null +++ b/src/pkg/meetings/betty/budget_test.go @@ -0,0 +1,48 @@ +package betty + +import ( + "strings" + "testing" + + "github.com/hivecommons/hive/pkg/config" +) + +func budgetTestEngine(t *testing.T, llm LLM) *Engine { + t.Helper() + return New(SettingsFrom(config.MeetingsPersonaConfig{}.Resolve()), Deps{LLM: llm, CanResearch: true}) +} + +// At the degrade step an answer is one short sentence, and the prompt says +// so; before it, answers keep the normal bounds. +func TestDegradedAnswersAreShortened(t *testing.T) { + e := budgetTestEngine(t, &fakeLLM{}) + long := "The release ships on Friday after the freeze. The notes are in the wiki. Ping the owner for the rest of the details, which are many." + normal := e.clipper(false)(long) + if !strings.Contains(normal, "wiki") || e.degradedNote() != "" { + t.Fatalf("normal answers keep their bounds: %q", normal) + } + e.SetDegraded() + short := e.clipper(false)(long) + if short != "The release ships on Friday after the freeze." || len(short) > DegradedMaxChars { + t.Fatalf("degraded answer %q", short) + } + if !strings.Contains(e.degradedNote(), "one short sentence") { + t.Fatalf("degraded prompt note %q", e.degradedNote()) + } + if v := e.clipper(true)(long); len(v) > DegradedMaxChars || strings.Count(v, ".") > DegradedMaxSentences { + t.Fatalf("degraded voice answer %q", v) + } +} + +// A question dropped for budget reaches the observer with who asked and +// what, so the session can record it. +func TestBudgetSkipReportsTheQuestion(t *testing.T) { + e := budgetTestEngine(t, &fakeLLM{}) + var got []BudgetSkip + e.OnBudgetSkip(func(s BudgetSkip) { got = append(got, s) }) + e.SetExhausted() + e.budgetSkip(Line{Who: "Ana", Text: "when does it ship?"}, true) + if len(got) != 1 || got[0].Who != "Ana" || got[0].Text != "when does it ship?" || !got[0].Addressed { + t.Fatalf("skip %+v", got) + } +} diff --git a/src/pkg/meetings/betty/queue.go b/src/pkg/meetings/betty/queue.go index dfea688c97..a8e6bb26cd 100644 --- a/src/pkg/meetings/betty/queue.go +++ b/src/pkg/meetings/betty/queue.go @@ -224,7 +224,7 @@ func (e *Engine) addressed(l Line, trig Trigger, now time.Time) { thoughts = true } if e.exhausted.Load() { - e.drop("budget") + e.budgetSkip(l, true) return } if q := trig.Clarify; q != "" { @@ -375,7 +375,7 @@ func (e *Engine) question(l Line, topic []string, now time.Time) { e.drop("no_topic") return case e.exhausted.Load(): - e.drop("budget") + e.budgetSkip(l, false) return case e.d.LLM == nil: e.drop("no_model") @@ -504,7 +504,8 @@ func (e *Engine) proceed(p *pending) { } } if e.exhausted.Load() { - e.finish(p, "budget") + delete(e.pending, p.id) + e.budgetSkip(p.line, p.addressed) return } if e.d.LLM == nil { @@ -525,7 +526,7 @@ func (e *Engine) proceed(p *pending) { mode, timeout, recent = modeAddressed, e.s.AddressedTimeout, e.recentLines() } system := SystemPrompt(e.s.Persona) - prompt := answerPrompt(mode, p.line.Who, p.line.Text, p.snips, recent, p.addressed && p.trig.VoiceWanted) + prompt := answerPrompt(mode, p.line.Who, p.line.Text, p.snips, recent, p.addressed && p.trig.VoiceWanted) + e.degradedNote() id := p.id p.stages.modelStart = now go func() { @@ -545,7 +546,7 @@ func (e *Engine) proceed(p *pending) { func (e *Engine) onAnswer(p *pending, a answered) { now := e.d.Now() - text := ClipChat(a.ans.Answer) + text := e.clipper(false)(a.ans.Answer) if p.addressed { e.onAddressedAnswer(p, a) return @@ -603,10 +604,7 @@ func (e *Engine) onOffer(text string) { // meeting has a research path: she says she will dig in, the session // dispatches the research, and FollowUp reports back once. func (e *Engine) onAddressedAnswer(p *pending, a answered) { - clipFn := ClipChat - if p.trig.VoiceWanted { - clipFn = ClipVoice - } + clipFn := e.clipper(p.trig.VoiceWanted) text := clipFn(a.ans.Answer) if a.err != nil || text == "" { e.log.Info("[meetings] persona answer unavailable; replying unsure", "error", a.err) @@ -621,7 +619,7 @@ func (e *Engine) onAddressedAnswer(p *pending, a answered) { // A question about the hive itself never goes to the research agents // (public web only, and slow): it is answered from the live state, or // she says she does not have the number. - if a.err == nil && weak && !live && e.d.CanResearch && !p.thoughts && e.isQuestion(p) && !IsHiveStatusQuestion(p.trig.Rest) && + if a.err == nil && weak && !live && e.d.CanResearch && !e.degraded.Load() && !p.thoughts && e.isQuestion(p) && !IsHiveStatusQuestion(p.trig.Rest) && !AnsweredLocally(p.snips) { dig := phrasesFor(e.s.Persona.Language).digIn rep.Text = dig @@ -712,3 +710,34 @@ func splitQuestions(text string) []string { } return parts } + +// Degraded answers: one short sentence, and a prompt note saying so, once +// the meeting has used BudgetDegradePercent of its budget. +const ( + DegradedMaxSentences = 1 + DegradedMaxChars = 160 + degradedPromptNote = "\n\nThe meeting's budget is nearly used: answer in one short sentence." +) + +// clipper is the answer clip: the normal chat or voice clip, tightened +// when the meeting's budget is nearly used. +func (e *Engine) clipper(voice bool) func(string) string { + base := ClipChat + if voice { + base = ClipVoice + } + if !e.degraded.Load() { + return base + } + return func(text string) string { + return clipSentences(speakable(base(text)), DegradedMaxSentences, DegradedMaxChars) + } +} + +// degradedNote is added to the answer prompt in degraded mode. +func (e *Engine) degradedNote() string { + if e.degraded.Load() { + return degradedPromptNote + } + return "" +} diff --git a/src/pkg/meetings/live/budget.go b/src/pkg/meetings/live/budget.go new file mode 100644 index 0000000000..acbbc41b2c --- /dev/null +++ b/src/pkg/meetings/live/budget.go @@ -0,0 +1,293 @@ +package live + +import ( + "context" + "fmt" + "strings" + "sync" + "time" +) + +// Budget steps for a meeting (hivecommons/hive#18914). They are fixed +// behaviour, not settings: the owner sets the budget itself +// (meetings.budget.max_tokens_per_meeting and the other ceilings). +const ( + // BudgetWarnPercent is where the meeting gets one warn log line and the + // dashboard shows a badge. + BudgetWarnPercent = 80 + // BudgetDegradePercent is where the attendee shortens answers and stops + // starting long research runs, saying so once. + BudgetDegradePercent = 90 + // SkippedKept bounds the skipped questions kept with their text per + // meeting; SkippedCount keeps counting past it. + SkippedKept = 200 + // maxSkippedText bounds one kept question. + maxSkippedText = 500 + // percentBase turns a ratio into a percent. + percentBase = 100 +) + +// Budget levels, as the meter reports them. +const ( + BudgetLevelOK = "ok" + BudgetLevelWarning = "warning" + BudgetLevelDegraded = "degraded" + BudgetLevelExhausted = "exhausted" +) + +// Budget exhaustion reasons. +const ( + BudgetReasonTokens = "tokens" + BudgetReasonIntents = "intents" + BudgetReasonHiveCeiling = "hive_ceiling" +) + +// SkippedQuestion is a question the meeting asked that was not answered +// because the budget was spent. The text is meeting speech: it belongs in +// the meeting's own records (session.json, the inbox record), never in +// logs or audit, which carry counts only. +type SkippedQuestion struct { + At time.Time `json:"at"` + Who string `json:"who,omitempty"` + Text string `json:"text"` + Addressed bool `json:"addressed,omitempty"` +} + +// BudgetView is one meeting's budget meter. It carries numbers only, never +// question text, so any role may read it. +type BudgetView struct { + MeetingID string `json:"meeting_id,omitempty"` + TokensUsed int `json:"tokens_used"` + TokenBudget int `json:"token_budget"` + Percent int `json:"percent"` + IntentsAnswered int `json:"intents_answered"` + IntentBudget int `json:"intent_budget"` + Level string `json:"level"` + Warned bool `json:"warned"` + Degraded bool `json:"degraded"` + Exhausted bool `json:"exhausted"` + ExhaustedBy string `json:"exhausted_by,omitempty"` + // Skipped counts the questions not answered because of the budget. + Skipped int `json:"skipped"` + // Dropped counts everything the live loop dropped, for any reason. + Dropped int `json:"dropped"` +} + +// BudgetOverview is the hive's meetings budget: the per-meeting limits, the +// hive-wide hourly and daily ceilings with what is used, and each meeting's +// meter. +type BudgetOverview struct { + TokenBudget int `json:"token_budget"` + IntentBudget int `json:"intent_budget"` + Ledger LedgerUsage `json:"ledger"` + Meetings []BudgetView `json:"meetings"` +} + +// budgetPercent is how much of the meeting budget is used: the larger of +// the token and intent shares, 100 once exhausted, never above 100. +func budgetPercent(p Policy, st State) int { + if st.BudgetExhausted { + return percentBase + } + pct := 0 + if p.MaxTokensPerMeeting > 0 { + pct = st.Tokens * percentBase / p.MaxTokensPerMeeting + } + if p.MaxIntentsPerMeeting > 0 { + if i := st.Intents * percentBase / p.MaxIntentsPerMeeting; i > pct { + pct = i + } + } + if pct > percentBase { + pct = percentBase + } + return pct +} + +// BudgetLevelFor names the level a percent (and exhaustion) is at. +func BudgetLevelFor(percent int, exhausted bool) string { + switch { + case exhausted: + return BudgetLevelExhausted + case percent >= BudgetDegradePercent: + return BudgetLevelDegraded + case percent >= BudgetWarnPercent: + return BudgetLevelWarning + } + return BudgetLevelOK +} + +// NewBudgetView builds the meter for a meeting's state under a policy. +func NewBudgetView(p Policy, st State, id string) BudgetView { + pct := budgetPercent(p, st) + return BudgetView{ + MeetingID: id, TokensUsed: st.Tokens, TokenBudget: p.MaxTokensPerMeeting, Percent: pct, + IntentsAnswered: st.Intents, IntentBudget: p.MaxIntentsPerMeeting, + Level: BudgetLevelFor(pct, st.BudgetExhausted), + Warned: st.BudgetWarned, Degraded: st.BudgetDegraded, Exhausted: st.BudgetExhausted, + ExhaustedBy: st.BudgetReason, Skipped: st.SkippedCount, Dropped: st.Dropped, + } +} + +// NewBudgetOverview assembles the hive view from the per-meeting states +// (keyed by meeting id, in the order of ids). +func (r *Runner) NewBudgetOverview(ids []string, state func(id string) (State, bool)) BudgetOverview { + out := BudgetOverview{ + TokenBudget: r.opts.Policy.MaxTokensPerMeeting, IntentBudget: r.opts.Policy.MaxIntentsPerMeeting, + Ledger: r.opts.Ledger.Usage(r.opts.Now()), Meetings: []BudgetView{}, + } + for _, id := range ids { + if st, ok := state(id); ok { + out.Meetings = append(out.Meetings, NewBudgetView(r.opts.Policy, st, id)) + } + } + return out +} + +// RenderSkippedNote renders the "not answered: budget" note for a +// meeting's inbox record; empty when nothing was skipped. +func RenderSkippedNote(st State) string { + if st.SkippedCount == 0 { + return "" + } + var b strings.Builder + b.WriteString("## Not answered: budget\n\n") + fmt.Fprintf(&b, "The meeting budget ran out, so %d question(s) were not answered.\n\n", st.SkippedCount) + for _, q := range st.Skipped { + who := strings.TrimSpace(q.Who) + if who == "" { + who = "someone" + } + fmt.Fprintf(&b, "- %s: %s\n", who, strings.TrimSpace(q.Text)) + } + if more := st.SkippedCount - len(st.Skipped); more > 0 { + fmt.Fprintf(&b, "- and %d more not listed\n", more) + } + return b.String() +} + +// skipQueue carries skipped questions from any goroutine (the persona +// engine's, the session's own) to the session goroutine, which owns State. +type skipQueue struct { + mu sync.Mutex + in []SkippedQuestion +} + +func (q *skipQueue) push(sq SkippedQuestion) { + q.mu.Lock() + q.in = append(q.in, sq) + q.mu.Unlock() +} + +func (q *skipQueue) take() []SkippedQuestion { + q.mu.Lock() + defer q.mu.Unlock() + out := q.in + q.in = nil + return out +} + +// queueSkip records a question dropped for budget. It is safe from any +// goroutine and never loses the question: the session merges it into its +// state on its next turn (onSkips), save or snapshot. +func (s *session) queueSkip(who, text string, addressed bool) { + text = strings.TrimSpace(text) + if text == "" { + return + } + s.skips.push(SkippedQuestion{At: s.now(), Who: who, Text: truncate(text, maxSkippedText), Addressed: addressed}) + select { + case s.skipWake <- struct{}{}: + default: + } +} + +// mergeSkips moves queued skipped questions into the state. +func (s *session) mergeSkips() { + for _, q := range s.skips.take() { + s.state.SkippedCount++ + if len(s.state.Skipped) < SkippedKept { + s.state.Skipped = append(s.state.Skipped, q) + } + // Counts only on the panel's stream: the text stays in the state. + s.emit(Event{Kind: EventDropped, Reason: "budget"}) + } +} + +// onSkips merges queued skips and, once exhausted, repeats the budget +// notice with the running count each time the count doubles (1, 2, 4, ...), +// so the room learns questions are being skipped without a post per one. +func (s *session) onSkips(ctx context.Context) { + s.mergeSkips() + if s.state.BudgetExhausted && s.state.SkippedCount >= s.nextSkipNotice { + for s.nextSkipNotice <= s.state.SkippedCount { + s.nextSkipNotice *= 2 + } + s.sayLine(ctx, TriggerNotice, false, budgetSkipNotice(s.personaName(), s.state.SkippedCount)) + } + s.save() +} + +// nextNoticeAfter is the first notice count above n: the powers of two. +func nextNoticeAfter(n int) int { + next := 1 + for next <= n { + next *= 2 + } + return next +} + +// budgetSteps takes the 80 and 90 percent steps once each as the meeting +// uses its budget; exhaustion is handled by account. +func (s *session) budgetSteps(ctx context.Context) { + pct := budgetPercent(s.r.opts.Policy, s.state) + if pct >= BudgetWarnPercent && !s.state.BudgetWarned { + s.state.BudgetWarned = true + // Ids and numbers only: no transcript or question text. + s.log().Warn(fmt.Sprintf("[meetings] meeting budget at %d%%", BudgetWarnPercent), + "tokens", s.state.Tokens, "budget", s.r.opts.Policy.MaxTokensPerMeeting, + "intents", s.state.Intents, "percent", pct) + s.emit(Event{Kind: EventBudget, Reason: BudgetLevelWarning}) + } + if pct >= BudgetDegradePercent && !s.state.BudgetDegraded { + s.state.BudgetDegraded = true + s.log().Info(fmt.Sprintf("[meetings] meeting budget at %d%%; shortening answers and not starting research", BudgetDegradePercent), + "tokens", s.state.Tokens, "percent", pct) + s.emit(Event{Kind: EventBudget, Reason: BudgetLevelDegraded}) + if s.persona != nil { + s.persona.SetDegraded() + s.sayLine(ctx, TriggerNotice, false, budgetDegradeNotice(s.personaName())) + } + } +} + +// budgetNotice is posted once when a meeting's budget runs out. +func budgetNotice(name string) string { + return name + " reached the budget for this meeting and keeps taking notes, but will not ask or answer questions or start research. " + budgetNextStep +} + +// budgetSkipNotice repeats it with the questions skipped so far. +func budgetSkipNotice(name string, skipped int) string { + return fmt.Sprintf("%s is out of budget for this meeting: %d question(s) not answered so far. %s", name, skipped, budgetNextStep) +} + +// budgetNextStep is what to do about it. +const budgetNextStep = "Ask the owner to raise the budget or start a new meeting." + +// budgetDegradeNotice is said once at BudgetDegradePercent. +func budgetDegradeNotice(name string) string { + return fmt.Sprintf("%s has used %d%% of this meeting's budget: answers will be shorter and no new long research runs will start.", name, BudgetDegradePercent) +} + +// Budget is one meeting's meter: the running session's, or the stored +// one after the meeting ended. +func (r *Runner) Budget(meetingID string) (BudgetView, error) { + st, err := r.State(meetingID) + if err != nil { + return BudgetView{}, err + } + return NewBudgetView(r.opts.Policy, st, meetingID), nil +} + +// Policy is the policy the runner applies to every meeting. +func (r *Runner) Policy() Policy { return r.opts.Policy } diff --git a/src/pkg/meetings/live/budget_test.go b/src/pkg/meetings/live/budget_test.go new file mode 100644 index 0000000000..b82147de70 --- /dev/null +++ b/src/pkg/meetings/live/budget_test.go @@ -0,0 +1,163 @@ +package live + +import ( + "bytes" + "encoding/json" + "log/slog" + "strings" + "sync" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/meetings" +) + +// syncBuf is a log sink safe for the session goroutine and the test. +type syncBuf struct { + mu sync.Mutex + b bytes.Buffer +} + +func (s *syncBuf) Write(p []byte) (int, error) { + s.mu.Lock() + defer s.mu.Unlock() + return s.b.Write(p) +} + +func (s *syncBuf) String() string { + s.mu.Lock() + defer s.mu.Unlock() + return s.b.String() +} + +const skippedQuestion = "Betty, when does the release ship?" + +func TestBudgetWarningFiresOncePerMeeting(t *testing.T) { + logs := &syncBuf{} + h := newHarness(t, Policy{MaxTokensPerMeeting: 100}, func(o *Options) { + o.Logger = slog.New(slog.NewTextHandler(logs, &slog.HandlerOptions{Level: slog.LevelDebug})) + }) + h.live() + h.spek.intent(`{"type":"notes.append","section":"notes","text":"secret transcript words"}`) + h.spek.intent(`{"type":"chat.post","text":"a","usage":{"total_tokens":81}}`) + h.eventually("warned", func() bool { return h.state().BudgetWarned }) + h.spek.intent(`{"type":"chat.post","text":"b","usage":{"total_tokens":2}}`) + h.spek.intent(`{"type":"chat.post","text":"c","usage":{"total_tokens":2}}`) + h.eventually("tokens counted", func() bool { return h.state().Tokens == 85 }) + if n := strings.Count(logs.String(), "meeting budget at 80%"); n != 1 { + t.Fatalf("the 80 percent warning must fire once per meeting, fired %d times:\n%s", n, logs.String()) + } + if strings.Contains(logs.String(), "secret transcript words") { + t.Fatal("no transcript text in logs") + } + if st := h.state(); st.BudgetDegraded || st.BudgetExhausted { + t.Fatalf("85 percent is a warning only: %+v", st) + } +} + +func TestNoResearchStartedAtNinetyPercent(t *testing.T) { + h := newHarness(t, Policy{MaxTokensPerMeeting: 100}, nil) + h.live() + h.spek.intent(`{"type":"chat.post","text":"a","usage":{"total_tokens":91}}`) + h.eventually("degraded", func() bool { return h.state().BudgetDegraded }) + h.spek.intent(`{"type":"research","rid":"r1","query":"how do certs rotate"}`) + h.eventually("research dropped", func() bool { return h.state().Dropped == 1 }) + h.res.mu.Lock() + n := h.res.n + h.res.mu.Unlock() + if n != 0 || len(h.state().Research) != 0 { + t.Fatalf("research must not start at 90 percent: dispatched %d, items %+v", n, h.state().Research) + } +} + +func TestDegradeNoticeSaidOnce(t *testing.T) { + h := personaHarness(t, Policy{ChatPostsPerMinute: 20, MaxTokensPerMeeting: 100}, config.MeetingsPersonaConfig{}, &personaLLM{}) + h.live() + h.spek.intent(`{"type":"notes.append","section":"notes","text":"n"}`) + h.spek.intent(`{"type":"chat.post","text":"a","usage":{"total_tokens":92}}`) + h.eventually("degrade notice", h.posted("answers will be shorter")) + h.spek.intent(`{"type":"chat.post","text":"b","usage":{"total_tokens":2}}`) + h.eventually("tokens counted", func() bool { return h.state().Tokens == 94 }) + if n := h.countPosts("answers will be shorter"); n != 1 { + t.Fatalf("the degrade notice is said once, said %d times", n) + } +} + +func TestQuestionDroppedForBudgetIsRecordedAndShown(t *testing.T) { + h := personaHarness(t, Policy{ChatPostsPerMinute: 20, MaxTokensPerMeeting: 100}, config.MeetingsPersonaConfig{}, &personaLLM{}) + h.live() + h.spek.intent(`{"type":"chat.post","text":"a","usage":{"total_tokens":150}}`) + h.eventually("exhausted", func() bool { return h.state().BudgetExhausted }) + h.eventually("notice says what to do", h.posted("raise the budget or start a new meeting")) + h.line(meetings.EventChat, "Ana", skippedQuestion, false) + h.eventually("skipped recorded", func() bool { return h.state().SkippedCount == 1 }) + st := h.state() + if len(st.Skipped) != 1 || st.Skipped[0].Text != skippedQuestion || st.Skipped[0].Who != "Ana" { + t.Fatalf("the skipped question must be kept with its text: %+v", st.Skipped) + } + // The count reaches the room, and the meter and inbox note carry it. + h.eventually("count in the notice", h.posted("1 question(s) not answered")) + if v := NewBudgetView(Policy{MaxTokensPerMeeting: 100}, st, "mtg-1"); v.Skipped != 1 || v.Level != BudgetLevelExhausted || v.Percent != 100 { + t.Fatalf("meter %+v", v) + } + if note := RenderSkippedNote(st); !strings.Contains(note, "Not answered: budget") || !strings.Contains(note, skippedQuestion) { + t.Fatalf("inbox note %q", note) + } + if h.countPosts("release ships") != 0 { + t.Fatal("no answer past the budget") + } +} + +func TestSkippedQuestionsSurviveRestart(t *testing.T) { + h := personaHarness(t, Policy{ChatPostsPerMinute: 20, MaxTokensPerMeeting: 100}, config.MeetingsPersonaConfig{}, &personaLLM{}) + h.live() + h.spek.intent(`{"type":"chat.post","text":"a","usage":{"total_tokens":150}}`) + h.eventually("exhausted", func() bool { return h.state().BudgetExhausted }) + h.line(meetings.EventChat, "Ana", skippedQuestion, false) + h.eventually("skipped", func() bool { return h.state().SkippedCount == 1 }) + h.r.Close() + var st State + if err := h.store.LoadDoc(h.m.ID, StateDoc, &st); err != nil { + t.Fatal(err) + } + if st.SkippedCount != 1 || len(st.Skipped) != 1 { + t.Fatalf("the skipped question must be persisted: %+v", st) + } +} + +func TestBudgetViewHasNoQuestionText(t *testing.T) { + st := State{Tokens: 62, Intents: 3, SkippedCount: 1, Skipped: []SkippedQuestion{{Who: "Ana", Text: skippedQuestion}}} + v := NewBudgetView(Policy{MaxTokensPerMeeting: 100, MaxIntentsPerMeeting: 500}, st, "mtg-1") + raw, err := json.Marshal(NewBudgetOverviewForTest(v)) + if err != nil { + t.Fatal(err) + } + if strings.Contains(string(raw), "release") || strings.Contains(string(raw), "Ana") { + t.Fatalf("the meter every role reads must carry no question text: %s", raw) + } + if v.Percent != 62 || v.Level != BudgetLevelOK || v.Skipped != 1 || v.IntentsAnswered != 3 { + t.Fatalf("view %+v", v) + } + for pct, want := range map[int]string{79: BudgetLevelOK, BudgetWarnPercent: BudgetLevelWarning, BudgetDegradePercent: BudgetLevelDegraded} { + if got := BudgetLevelFor(pct, false); got != want { + t.Fatalf("level at %d = %s, want %s", pct, got, want) + } + } +} + +// NewBudgetOverviewForTest wraps a view the way the API serves it. +func NewBudgetOverviewForTest(v BudgetView) BudgetOverview { + return BudgetOverview{TokenBudget: v.TokenBudget, Meetings: []BudgetView{v}} +} + +func TestLedgerUsage(t *testing.T) { + l := NewLedger(10, 100) + now := time.Date(2026, 11, 15, 12, 0, 0, 0, time.UTC) + l.Add(now.Add(-2*ledgerHour), 30) + l.Add(now.Add(-time.Minute), 7) + u := l.Usage(now) + if u.HourlyUsed != 7 || u.DailyUsed != 37 || u.HourlyCeiling != 10 || u.DailyCeiling != 100 { + t.Fatalf("usage %+v", u) + } +} diff --git a/src/pkg/meetings/live/policy.go b/src/pkg/meetings/live/policy.go index 890fed7d13..f130be7c0a 100644 --- a/src/pkg/meetings/live/policy.go +++ b/src/pkg/meetings/live/policy.go @@ -130,6 +130,36 @@ func (l *Ledger) Exceeded(now time.Time) bool { return (l.hourly > 0 && hour >= l.hourly) || (l.daily > 0 && day >= l.daily) } +// LedgerUsage is the hive-wide meetings spend in the last hour and day +// against the ceilings (zero ceiling: none). +type LedgerUsage struct { + HourlyUsed int `json:"hourly_used"` + HourlyCeiling int `json:"hourly_ceiling"` + DailyUsed int `json:"daily_used"` + DailyCeiling int `json:"daily_ceiling"` +} + +// Usage reports the spend in the rolling hour and day at now. +func (l *Ledger) Usage(now time.Time) LedgerUsage { + if l == nil { + return LedgerUsage{} + } + l.mu.Lock() + defer l.mu.Unlock() + u := LedgerUsage{HourlyCeiling: l.hourly, DailyCeiling: l.daily} + dayCut, hourCut := now.Add(-ledgerDay), now.Add(-ledgerHour) + for _, e := range l.entries { + if !e.at.After(dayCut) { + continue + } + u.DailyUsed += e.tokens + if e.at.After(hourCut) { + u.HourlyUsed += e.tokens + } + } + return u +} + // truncate caps text at limit runes. func truncate(text string, limit int) string { r := []rune(text) diff --git a/src/pkg/meetings/live/session.go b/src/pkg/meetings/live/session.go index e2ed49a4ce..0ca697b68e 100644 --- a/src/pkg/meetings/live/session.go +++ b/src/pkg/meetings/live/session.go @@ -58,11 +58,6 @@ func (s *session) setSpekStatus(status string) { // persona. const defaultPersonaName = "Hive" -// budgetNotice is posted once when a meeting's budget runs out. -func budgetNotice(name string) string { - return name + " reached the budget for this meeting and keeps taking notes, but will not ask or answer questions or start research." -} - type approveReq struct { rid string reply chan error @@ -93,6 +88,12 @@ type session struct { persona *betty.Engine // fileTokens is the usage-ledger total already charged. fileTokens int + // skips carries questions dropped for budget from any goroutine; + // skipWake wakes the session to merge them; nextSkipNotice is the + // skipped count at which the budget notice is next repeated (budget.go). + skips skipQueue + skipWake chan struct{} + nextSkipNotice int // voiceQ and chatQ are the ordered queues of lines going out by voice // and in chat, each drained by its own worker so a slow say or post // never stops the session; outDone brings each result back. async is @@ -131,6 +132,7 @@ func (r *Runner) newSession(m meetings.Meeting, spek Spek) *session { approves: make(chan approveReq), snaps: make(chan chan State), restyled: make(chan restyledAsk, sessionInbox), + skipWake: make(chan struct{}, 1), outDone: make(chan outDone, sessionInbox), ended: make(chan struct{}), done: make(chan struct{}), @@ -151,7 +153,12 @@ func (r *Runner) newSession(m meetings.Meeting, spek Spek) *session { if s.state.BudgetExhausted { s.persona.SetExhausted() } + if s.state.BudgetDegraded { + s.persona.SetDegraded() + } + s.persona.OnBudgetSkip(func(q betty.BudgetSkip) { s.queueSkip(q.Who, q.Text, q.Addressed) }) } + s.nextSkipNotice = nextNoticeAfter(s.state.SkippedCount) if r.opts.UsageFile != nil { s.fileTokens, _ = meetllm.UsageTotal(r.opts.UsageFile(m.ID)) } @@ -182,6 +189,7 @@ func (s *session) isLeave(text string) bool { func (s *session) now() time.Time { return s.r.opts.Now() } func (s *session) save() { + s.mergeSkips() if err := s.r.opts.Store.SaveDoc(s.id, StateDoc, s.state); err != nil { s.log().Warn("[meetings] saving live state", "error", err) } @@ -218,6 +226,8 @@ func (s *session) run(ctx context.Context) { } s.checkReach(ctx) } + case <-s.skipWake: + s.onSkips(ctx) case d := <-s.outDone: d.job.after(ctx, d.res) case rep := <-replies: @@ -300,6 +310,7 @@ func (s *session) warmResearch(ctx context.Context) { } func (s *session) snapshot() State { + s.mergeSkips() b, _ := json.Marshal(s.state) var out State _ = json.Unmarshal(b, &out) @@ -461,12 +472,27 @@ func (s *session) account(ctx context.Context, in intent) { } else { s.state.Intents++ } - over := (p.MaxTokensPerMeeting > 0 && s.state.Tokens > p.MaxTokensPerMeeting) || - (p.MaxIntentsPerMeeting > 0 && s.state.Intents > p.MaxIntentsPerMeeting) || - s.r.opts.Ledger.Exceeded(s.now()) - if over && !s.state.BudgetExhausted { - s.state.BudgetExhausted = true - s.log().Warn("[meetings] meeting budget exhausted", "tokens", s.state.Tokens, "intents", s.state.Intents) + reason := "" + switch { + case p.MaxTokensPerMeeting > 0 && s.state.Tokens > p.MaxTokensPerMeeting: + reason = BudgetReasonTokens + case p.MaxIntentsPerMeeting > 0 && s.state.Intents > p.MaxIntentsPerMeeting: + reason = BudgetReasonIntents + case s.r.opts.Ledger.Exceeded(s.now()): + reason = BudgetReasonHiveCeiling + } + if reason == "" { + if !s.state.BudgetExhausted { + s.budgetSteps(ctx) + } + return + } + if !s.state.BudgetExhausted { + s.state.BudgetExhausted, s.state.BudgetReason = true, reason + // The earlier steps are moot: no repeat warn or degrade later. + s.state.BudgetWarned, s.state.BudgetDegraded = true, true + s.log().Warn("[meetings] meeting budget exhausted", "tokens", s.state.Tokens, "intents", s.state.Intents, "reason", reason) + s.emit(Event{Kind: EventBudget, Reason: BudgetLevelExhausted}) if s.persona != nil { s.persona.SetExhausted() } @@ -619,6 +645,14 @@ func (s *session) update(it *ResearchItem, status, summary string) { // else hold for owner approval. func (s *session) research(ctx context.Context, in intent) { rid, query := strings.TrimSpace(in.RID), strings.TrimSpace(in.Query) + if s.state.BudgetDegraded { + // At BudgetDegradePercent no new research starts. + s.drop("budget_degraded") + if rid != "" { + _ = s.spek.Send(map[string]string{"type": "research_update", "rid": rid, "status": ResearchFailed, "summary": "not started: the meeting budget is nearly used"}) + } + return + } if rid == "" || query == "" || s.find(rid) != nil { s.drop("research_invalid_or_duplicate") return @@ -828,6 +862,11 @@ func (s *session) onPersonaReply(ctx context.Context, rep betty.Reply) { s.account(ctx, intent{Type: intentPersona}) } if !s.spendable() { + // The model answered but the budget ran out before it could be + // posted: the question is still recorded as not answered. + if rep.Question != "" && (rep.Trigger == betty.TriggerAddressed || rep.Trigger == betty.TriggerQuestion) { + s.queueSkip(rep.Asker, rep.Question, rep.Trigger == betty.TriggerAddressed) + } s.save() return } @@ -857,6 +896,10 @@ const bettyRIDPrefix = "betty-" // the meeting's research path (same cap and approval as Spek's research), // marked betty=1. func (s *session) deepDive(ctx context.Context, rep betty.Reply, voice bool) { + if s.state.BudgetDegraded { + s.drop("budget_degraded") + return + } query := rep.Research n := 1 for _, it := range s.state.Research { diff --git a/src/pkg/meetings/live/types.go b/src/pkg/meetings/live/types.go index 0ed794b4b0..de3d2a6867 100644 --- a/src/pkg/meetings/live/types.go +++ b/src/pkg/meetings/live/types.go @@ -177,7 +177,18 @@ type State struct { Tokens int `json:"tokens"` BudgetExhausted bool `json:"budget_exhausted,omitempty"` Dropped int `json:"dropped"` - LastHeartbeat *time.Time `json:"last_heartbeat,omitempty"` + // BudgetWarned and BudgetDegraded record that the 80 and 90 percent + // steps were taken, so each happens once per meeting even across a + // restart (budget.go). + BudgetWarned bool `json:"budget_warned,omitempty"` + BudgetDegraded bool `json:"budget_degraded,omitempty"` + // BudgetReason is what ran out: "tokens", "intents" or "hive_ceiling". + BudgetReason string `json:"budget_reason,omitempty"` + // SkippedCount counts every question dropped for budget; Skipped keeps + // the first SkippedKept of them with their text for the inbox record. + SkippedCount int `json:"skipped_count,omitempty"` + Skipped []SkippedQuestion `json:"skipped,omitempty"` + LastHeartbeat *time.Time `json:"last_heartbeat,omitempty"` // SpekMode describes the model Spek reasons with ("model X", // "Anthropic ...", or "rules only, no model configured"). SpekMode string `json:"spek_mode,omitempty"` @@ -220,6 +231,9 @@ const ( // EventReach reports a change in whether the bot can reach the meeting // (State.Undelivered, State.Unreachable). EventReach = "reach" + // EventBudget reports a budget step (80 percent warning, 90 percent + // degrade, exhaustion); Reason is the level. + EventBudget = "budget" ) // Event is what live-panel observers receive. From ee882196d2e848928799f1cfa5bd265b8f1c5e83 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Wed, 18 Nov 2026 00:30:11 -0500 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=90=9B=20Meetings=20dashboard=20test:?= =?UTF-8?q?=20stub=20the=20budget=20meter=20helpers=20the=20role-race=20sl?= =?UTF-8?q?ice=20now=20needs=20(#18914)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Andrew Anderson --- .../test/meetings-calendar-role-race.test.js | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/dashboard/test/meetings-calendar-role-race.test.js b/dashboard/test/meetings-calendar-role-race.test.js index b62beb8057..9bd2081f9e 100644 --- a/dashboard/test/meetings-calendar-role-race.test.js +++ b/dashboard/test/meetings-calendar-role-race.test.js @@ -37,7 +37,7 @@ function extractConst(name) { const GH_AUTH = ['hiveRoleSettled']; const MEETINGS = ['meetingsIsOwner', 'meetingsRoleKnown', 'meetingsOnRoleReady', 'fetchMeetings', 'renderMeetings', 'meetingsJoinFormHtml', 'meetingsRowHtml', 'meetingsFormatTime', 'meetingsFriendlyReason', 'meetingsReasonHtml', 'meetingsLabel', - 'meetingsFinished', 'meetingsCanDelete', 'meetingsDeleteButtonHtml']; + 'meetingsFinished', 'meetingsCanDelete', 'meetingsDeleteButtonHtml', 'fetchMeetingsBudget', 'meetingsBudgetHtml', 'meetingsBudgetText']; const CALENDAR = ['calendarRoleKnown', 'calendarOnRoleReady', 'calendarCanView', 'calendarIsOwner', 'fetchCalendar', 'calendarRefreshMeetings', 'fetchMeetingDrafts', 'renderCalendar', 'renderMeetingDrafts', 'meetingDraftsSplit', 'calendarFormHtml', 'calendarViewerTimeZone', 'calendarRowHtml', 'calendarFormatTime', 'calendarPlatformLabel', 'calendarMeetingLink', 'calendarActionButtons', 'renderCalendarIndicator', @@ -67,13 +67,13 @@ function makeSandbox(opts) { if (url === '/api/meetings/actions') return { ok: true, status: 200, json: async () => ({ drafts: [] }) }; return { ok: true, status: 200, json: async () => ({}) }; }, - _meetingsList: null, _meetingsError: '', _meetingsOpen: null, + _meetingsList: null, _meetingsError: '', _meetingsOpen: null, _meetingsBudget: {}, _meetingsLedger: null, _calendarData: null, _calendarError: '', _calendarMeetings: {}, _meetingDrafts: null, _meetingDraftsError: '', _meetingDraftRepos: ['o/r'], console, Math, Number, Array, Error, Promise, String, Object, Date, Intl, isNaN, }; vm.createContext(sb); - const consts = ['MEETINGS_ERROR_INLINE_CHARS', 'MEETINGS_PLATFORM_LABELS', 'MEETINGS_PILL_STATUS', 'MEETINGS_LIVE_STATES', + const consts = ['MEETINGS_ERROR_INLINE_CHARS', 'MEETINGS_PLATFORM_LABELS', 'MEETINGS_PILL_STATUS', 'MEETINGS_LIVE_STATES', 'MEETINGS_BUDGET_BADGES', 'CALENDAR_ACTIVE_STATES', 'CALENDAR_DEFAULT_NAME', 'CALENDAR_PLATFORM_LABELS', 'CALENDAR_PLATFORM_ICONS', 'CALENDAR_SOURCE_LABELS', 'CALENDAR_STATUS_LABELS', 'CALENDAR_PILL_STATUS', 'CALENDAR_FWD_PREFIX', 'MEETINGS_FINAL_STATES', 'CALENDAR_FINISHED_STATUSES'].map(extractConst); // const declarations stay script-scoped in a vm context; re-declare the @@ -173,3 +173,17 @@ test('calendar row: forwarded prefix stripped for display, series leads with nex assert.match(html, /calendar-row-actions/); assert.equal(sb.calendarApprovalWhy({ status: 'scheduled', source: 'email' }), ''); }); + +test('the meeting row shows the budget meter with a text alternative and a badge, never question text', async () => { + const sb = makeSandbox({}); + const b = { meeting_id: 'm1', tokens_used: 124000, token_budget: 200000, percent: 62, intents_answered: 15, level: 'ok', skipped: 0 }; + const ok = sb.meetingsBudgetHtml(b); + assert.match(ok, /aria-label="62% of the meeting budget used"/); + assert.match(ok, /