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/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, / { 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 '
'; + } 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.