From 0bdabf48a7da2a96cfc801a7831a92f5303d19c6 Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 16 Nov 2026 23:02:29 -0500 Subject: [PATCH 01/13] =?UTF-8?q?=F0=9F=90=9B=20Meetings:=20don't=20restar?= =?UTF-8?q?t-loop=20the=20Spektacular=20stream=20child=20when=20live=20mod?= =?UTF-8?q?e=20isn't=20available?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The pinned spektacular (0.31.0) has no `meeting` command (hivecommons/spektacular#126 is unreleased), so every live meeting child printed cobra's unknown-command envelope and exited 1. The stream restarted it five times per meeting, each restart a WARN, which the slog feed self-reported as unclassified_error_repeated (#18630), and the session warned once per transcript line it could not forward. - spektacular.Stream gains an optional Probe (`meeting --help`), run once per installed binary; an unknown command marks the stream Unsupported: no child is started or restarted, one INFO line per binary. A child whose own exit output says unknown command is not restarted either. - Restart lines are WARN at most once per BackoffMax window (INFO otherwise) and map to a new log-fed class spek_stream_unavailable. - The live session treats an unsupported Spek as off from the start (panel status unchanged), stops forwarding once Spek is gone, and warns about a failed forward only on the transition to paused. Fixes #18630 Signed-off-by: Andy Anderson --- .../fixed-18630-spek-stream-restart-loop.md | 1 + src/cmd/hive/meetingswire.go | 10 +- src/docs/fleet-report.md | 1 + src/pkg/fleetreport/runtime.go | 6 + src/pkg/fleetreport/runtime_test.go | 3 + src/pkg/meetings/live/session.go | 41 ++++- .../meetings/live/spek_unavailable_test.go | 95 ++++++++++ src/pkg/spektacular/stream.go | 167 +++++++++++++++++- src/pkg/spektacular/stream_test.go | 149 ++++++++++++++++ 9 files changed, 458 insertions(+), 15 deletions(-) create mode 100644 changelog.d/fixed-18630-spek-stream-restart-loop.md create mode 100644 src/pkg/meetings/live/spek_unavailable_test.go diff --git a/changelog.d/fixed-18630-spek-stream-restart-loop.md b/changelog.d/fixed-18630-spek-stream-restart-loop.md new file mode 100644 index 0000000000..e10f6be96d --- /dev/null +++ b/changelog.d/fixed-18630-spek-stream-restart-loop.md @@ -0,0 +1 @@ +- Meetings no longer restart-loop the Spektacular live child when the installed spektacular has no `meeting` command (before hivecommons/spektacular#126): hive probes `spektacular meeting --help` once per installed binary, keeps live mode off without starting the child, stops forwarding transcript lines to it, and the Meetings panel still says notes and clarifying questions are off. A supported child that keeps exiting still restarts with capped exponential backoff, warns at most once per backoff window, and is self-reported as `spek_stream_unavailable` instead of `unclassified_error_repeated` ([#18630](https://github.com/hivecommons/hive/issues/18630)). diff --git a/src/cmd/hive/meetingswire.go b/src/cmd/hive/meetingswire.go index b14aaef683..e634cbd051 100644 --- a/src/cmd/hive/meetingswire.go +++ b/src/cmd/hive/meetingswire.go @@ -160,6 +160,12 @@ func wireMeetingsManager(cfg *config.Config, sup quorumSupervisor, logger *slog. return mgr, store } +// meetingSpekProbeArgs checks once per installed binary that spektacular +// has the live `meeting` command (hivecommons/spektacular#126); a release +// without it answers "unknown command" and live mode stays off without a +// child restart loop (hivecommons/hive#18630). +var meetingSpekProbeArgs = []string{"meeting", "--help"} + // meetingSpekArgs is the contract's live-mode command line // (quorum.contract/v1 section 2). func meetingSpekArgs(m meetings.Meeting, contextDir string) []string { @@ -205,7 +211,9 @@ func wireMeetingsLive(cfg *config.Config, mgr *meetings.Manager, store *meetings Store: store, NewSpek: func(mt meetings.Meeting) live.Spek { env := meetingSpekEnv(os.Getenv, llm, hiveBin, meetingUsageFile(store, mt.ID), slotsDir, maxTokens) - return spektacular.StreamExec(binary, meetingSpekArgs(mt, filepath.Join(store.Dir(), mt.ID)), spektacular.StreamOptions{Logger: logger, Env: env}) + return spektacular.StreamExec(binary, meetingSpekArgs(mt, filepath.Join(store.Dir(), mt.ID)), spektacular.StreamOptions{ + Logger: logger, Env: env, Probe: meetingSpekProbeArgs, + }) }, SpekMode: llm.Label(), UsageFile: func(id string) string { diff --git a/src/docs/fleet-report.md b/src/docs/fleet-report.md index cc01870c5e..442f6632c4 100644 --- a/src/docs/fleet-report.md +++ b/src/docs/fleet-report.md @@ -155,6 +155,7 @@ title is `[selfreport] : `. | `github_fetch_transient` | a non-critical GitHub fetch failed and the feature degrades gracefully (`channel commit date: fetch failed — the row will render without a timestamp`, `pkg/hub/channel_commit_date.go`) — typically a rate limit or transient API error, not a hive defect. Reported only when persistent: log-fed, same ≥5-in-10-min repeat threshold and idle clear as `agent_pane_timeout`. The sibling `SHA poll: commit message fetch non-200` has its own class, `sha_poll_commit_message_unavailable` | low | | `label_provisioning_failed` | provisioning the issue stage labels on a repo failed (`issue stage labels: provisioning failed`, `pkg/github/issue_stage_labels.go`) — typically the App lacking label permission on that repo or a transient API error; diagnostics carry the repo and scrubbed error. Log-fed: same repeat threshold and idle clear as `agent_pane_timeout` | low | | `agent_cli_restart_loop` | the agent CLI crashed (bare shell) or sat on a consent screen and the harness restarted it before the kick (`agent CLI crashed or stuck on consent screen, restarting before kick`, `crux/agent/manager_kick.go`, `kick_async.go`), repeated ≥5 times in 10 min — a restart loop, not transient noise. Log-fed: same repeat threshold and idle clear as `agent_pane_timeout` | medium | +| `spek_stream_unavailable` | a Spektacular live meeting child (`spektacular meeting`, `pkg/spektacular/stream.go`) keeps exiting and is restarted with exponential backoff (`[spektacular] stream child exited; restarting`), or gave up after its restart cap (`[spektacular] stream child keeps exiting; giving up`). The restart line is a WARN at most once per backoff window and INFO otherwise. A spektacular release without the `meeting` command (before hivecommons/spektacular#126) is not this class: the stream probes `spektacular meeting --help` once per installed binary, logs one INFO and never starts or restarts the child, and the Meetings panel says notes and clarifying questions are off ([#18630](https://github.com/hivecommons/hive/issues/18630)). Log-fed: same repeat threshold and idle clear as `agent_pane_timeout` | low | | `copilot_cli_hung` | a Copilot agent showed none of the CLI's startup chrome (input prompt, help hint, login request) 180 s after launch, outside kick delivery, so the hang detector killed its session, ran bare `copilot` to capture the error and relaunched the agent (`copilot hung with no CLI prompt, running diagnostic`, `crux/agent/manager_poll.go`), or that diagnostic saw no output before its own deadline and relaunched anyway (`diagnostic: timed out waiting for copilot error output`, `crux/agent/manager_credentials.go`; the line carries the pane's `output_tail`). The key is the message, so ≥5 in 10 min counts every Copilot agent on the hive together: its Copilot agents keep failing to reach their prompt. Typical causes are a stale Copilot token the CLI hangs on (the diagnostic restores or clears it when it sees an auth error), a blocked network path to the Copilot API, or a CLI build whose startup screen the detector does not recognise (`cliReadyIndicators`, `crux/agent/pane_classify.go`). Log-fed: same repeat threshold and idle clear as `agent_pane_timeout` | medium | | `proxy_connect_dial_failed` | the hive proxy could not dial the upstream host of a CONNECT request (`proxy: CONNECT dial failed`, `crux/proxy/github_proxy_connect.go`) or of a Copilot usage-sniffed connection (`copilot sniff: upstream dial failed`, `crux/proxy/github_proxy_copilot.go`) — typically DNS or network unreachability of an agent-requested host (for example a dial timeout to `telemetry.enterprise.githubcopilot.com`), repeated ≥5 times in 10 min. Log-fed: same repeat threshold and idle clear as `agent_pane_timeout` | medium | | `proxy_plain_forward_failed` | the proxy could not complete an agent's plain-HTTP upstream request (`proxy: plain HTTP forward failed`, `crux/proxy/github_proxy_connect.go`), returning HTTP 502. Check the requested host and logged error, including DNS and outbound connectivity; this differs from a destination refused by the proxy policy. Log-fed: ≥5 occurrences in 10 min, clears after 20 quiet minutes | medium | diff --git a/src/pkg/fleetreport/runtime.go b/src/pkg/fleetreport/runtime.go index 3aad09475a..1b6c3c4770 100644 --- a/src/pkg/fleetreport/runtime.go +++ b/src/pkg/fleetreport/runtime.go @@ -103,6 +103,11 @@ const ( ClassTranscriptScanSlow = "transcript_scan_slow" ClassSharedCIRerunForbidden = "shared_ci_rerun_forbidden" ClassIssueClaimEffectFailed = "issue_claim_effect_failed" + // ClassSpekStreamUnavailable: a Spektacular live meeting child + // (`spektacular meeting`) keeps exiting and is restarted with backoff + // (hivecommons/hive#18630). A spektacular without the command is + // detected once and never restarted, so it does not reach this class. + ClassSpekStreamUnavailable = "spek_stream_unavailable" // A restart or normal stop can invalidate a pending bootstrap without // indicating a restart storm; only repeated interruptions are reportable. ClassStartupKickInterrupted = "startup_kick_interrupted" @@ -230,6 +235,7 @@ var Classes = map[string]ClassSpec{ ClassAgentLifecycleRepeated: {Component: "agent-runtime", Severity: "low", MinCount: unclassifiedRepeatCount, Window: unclassifiedRepeatWindow, IdleClear: unclassifiedIdleClear, LogFed: true, Keywords: []string{"failed to send kick", "agent cli crashed (bare shell detected)", "tmux kill-session before restart failed", "agent tmux session missing", "would restart dead agent"}}, ClassSharedCIRerunForbidden: {Component: "github-client", Severity: "low", MinCount: unclassifiedRepeatCount, Window: unclassifiedRepeatWindow, IdleClear: unclassifiedIdleClear, LogFed: true, Keywords: []string{"shared-ci rerun forbidden"}}, ClassIssueClaimEffectFailed: {Component: "agent-runtime", Severity: "low", MinCount: unclassifiedRepeatCount, Window: unclassifiedRepeatWindow, IdleClear: unclassifiedIdleClear, LogFed: true, Keywords: []string{"issue-claims: github side effect failed"}}, + ClassSpekStreamUnavailable: {Component: "agent-runtime", Severity: "low", MinCount: unclassifiedRepeatCount, Window: unclassifiedRepeatWindow, IdleClear: unclassifiedIdleClear, LogFed: true, Keywords: []string{"[spektacular] stream child exited; restarting", "[spektacular] stream child keeps exiting; giving up", "[spektacular] stream child did not restart"}}, ClassTranscriptScanSlow: {Component: "agent-runtime", Severity: "low", MinCount: unclassifiedRepeatCount, Window: unclassifiedRepeatWindow, IdleClear: unclassifiedIdleClear, LogFed: true, Keywords: []string{"transcript activity scan outlived its interval"}}, } diff --git a/src/pkg/fleetreport/runtime_test.go b/src/pkg/fleetreport/runtime_test.go index 9ffc7aa9c8..0a449c5b04 100644 --- a/src/pkg/fleetreport/runtime_test.go +++ b/src/pkg/fleetreport/runtime_test.go @@ -452,6 +452,7 @@ func TestEveryRuntimeClassTriggerAndNonTriggerThresholds(t *testing.T) { {class: ClassSharedCIRerunForbidden, message: "shared-ci rerun forbidden: the App installation lacks Actions write permission for rerun-failed-jobs"}, {class: ClassIssueClaimEffectFailed, message: "issue-claims: GitHub side effect failed"}, {class: ClassTranscriptScanSlow, message: "copilot transcript activity scan outlived its interval; agent liveness reads may lag"}, + {class: ClassSpekStreamUnavailable, message: "[spektacular] stream child exited; restarting"}, {class: ClassBeadFactIngestFailed, message: "failed to ingest bead fact"}, {class: ClassSelfReportClearedFailed, message: "fleet report: cleared comment failed"}, {class: ClassGitHubAppRepoNotCovered, message: "github app recheck: installation does not cover every configured repo"}, @@ -1276,6 +1277,8 @@ func TestRecurringRuntimeMessagesAreClassified(t *testing.T) { "ioscan classifier failed closed; text withheld": ClassIOScanClassifierUnavail, "watchdog: agent credential failure": ClassAgentLoginRequired, "ioscan classifier failed open; text passes on unclassified": ClassIOScanClassifierUnavail, + "[spektacular] stream child exited; restarting": ClassSpekStreamUnavailable, + "[spektacular] stream child keeps exiting; giving up": ClassSpekStreamUnavailable, "ioscan fail-closed blocked kick": ClassIOScanClassifierUnavail, "ioscan fail-closed blocked held-PR coordination kick": ClassIOScanClassifierUnavail, "proxy client TLS handshake failed": ClassProxyTLSHandshakeFailed, diff --git a/src/pkg/meetings/live/session.go b/src/pkg/meetings/live/session.go index da4e320d19..0b0295b266 100644 --- a/src/pkg/meetings/live/session.go +++ b/src/pkg/meetings/live/session.go @@ -36,6 +36,14 @@ const ( spekPausedStatus = "Notes and clarifying questions are paused: Spektacular live mode is not running right now (restarting, or it needs a spektacular with the meeting command, hivecommons/spektacular#126). The transcript, the persona and research still work." ) +// spekUnsupported reports whether spek says the installed spektacular has no +// live meeting command (spektacular.Stream.Unsupported). A Spek without that +// method is never unsupported. +func spekUnsupported(spek Spek) bool { + u, ok := spek.(interface{ Unsupported() bool }) + return ok && u.Unsupported() +} + // setSpekStatus updates the panel's Spektacular status when it changes. func (s *session) setSpekStatus(status string) { if s.state.SpekStatus == status { @@ -69,6 +77,11 @@ type session struct { spek Spek state State rate rateLimiter + // spekGone is set once Spek is down for good for this meeting (its + // lines closed, or the installed spektacular has no meeting command): + // nothing more is forwarded to it, so a meeting without live mode does + // not log a warning per transcript line (hivecommons/hive#18630). + spekGone bool // persona is the meeting's attendee persona, nil when it is off. With // it on, it owns the bot's voice in the meeting: Spek's say intents are // dropped, its chat posts are offered to the persona, and its @@ -159,7 +172,13 @@ func (s *session) save() { func (s *session) run(ctx context.Context) { defer close(s.done) lines := s.spek.Lines() - ending, spekGone := false, false + ending := false + if spekUnsupported(s.spek) { + // The installed spektacular has no `meeting` command: live mode is + // off from the start; the stream already logged it once per binary. + s.spekGone, lines = true, nil + s.setSpekStatus(spekStoppedStatus) + } var replies <-chan betty.Reply var researchTick <-chan time.Time if s.persona != nil { @@ -191,7 +210,7 @@ func (s *session) run(ctx context.Context) { if u.Meeting.State.Terminal() { ending = true close(s.ended) - if spekGone { + if s.spekGone { s.save() return } @@ -211,8 +230,12 @@ func (s *session) run(ctx context.Context) { // while the meeting goes on: keep the session for the panel, // research signals and approvals; only Spek's questions and // notes stop. - spekGone, lines = true, nil - s.log().Warn("[meetings] spektacular live mode stopped; continuing without it") + s.spekGone, lines = true, nil + if spekUnsupported(s.spek) { + s.log().Info("[meetings] spektacular live mode unavailable; continuing without it") + } else { + s.log().Warn("[meetings] spektacular live mode stopped; continuing without it") + } s.setSpekStatus(spekStoppedStatus) continue } @@ -263,13 +286,14 @@ func (s *session) onUpdate(u meetings.Update) { default: return } - if len(ev.Raw) > 0 { + if len(ev.Raw) > 0 && !s.spekGone { if err := s.spek.SendLine(ev.Raw); err != nil { - s.log().Warn("[meetings] forwarding to spek", "error", err) // The child is down (between restarts, or a spektacular // without the meeting command): say so on the panel now rather - // than only once it gives up. + // than only once it gives up. Warn on that transition only, not + // on every line while it stays down. if s.state.SpekStatus == "" { + s.log().Warn("[meetings] forwarding to spek", "error", err) s.setSpekStatus(spekPausedStatus) } } @@ -305,6 +329,9 @@ func (s *session) matchAnswer(ev *meetings.Event) { } a.Answer, a.AnsweredBy, a.AnsweredAt = ev.Text, ev.Who(), &now answered, changed = true, true + if s.spekGone { + continue + } if err := s.spek.Send(map[string]string{"type": "answer", "qid": a.QID, "text": ev.Text, "by": ev.Who()}); err != nil { s.log().Warn("[meetings] sending answer to spek", "error", err) } diff --git a/src/pkg/meetings/live/spek_unavailable_test.go b/src/pkg/meetings/live/spek_unavailable_test.go new file mode 100644 index 0000000000..bbc1432819 --- /dev/null +++ b/src/pkg/meetings/live/spek_unavailable_test.go @@ -0,0 +1,95 @@ +package live + +import ( + "bytes" + "errors" + "log/slog" + "strings" + "sync" + "testing" + + "github.com/hivecommons/hive/pkg/meetings" +) + +// syncLog is a goroutine-safe log sink. +type syncLog struct { + mu sync.Mutex + b bytes.Buffer +} + +func (l *syncLog) Write(p []byte) (int, error) { + l.mu.Lock() + defer l.mu.Unlock() + return l.b.Write(p) +} + +func (l *syncLog) String() string { + l.mu.Lock() + defer l.mu.Unlock() + return l.b.String() +} + +// end ends the meeting and waits for its session to finish, so every +// update pushed before it has been applied. +func (h *harness) end() { + h.t.Helper() + ended := h.m + ended.State = meetings.StateEnded + h.mt.push(meetings.Update{Meeting: ended}) + h.eventually("session gone", func() bool { _, ok := h.r.session(h.m.ID); return !ok }) +} + +// unsupportedSpek is a Spek for a spektacular without the meeting command: +// it is done from the start and says so (spektacular.Stream.Unsupported). +type unsupportedSpek struct{ *fakeSpek } + +func (unsupportedSpek) Unsupported() bool { return true } + +// TestSpekUnsupportedNoForwardingNoWarnings pins hivecommons/hive#18630: a +// spektacular without `meeting` turns live mode off for the meeting at once +// (the panel says notes and questions are off), and transcript lines are +// not forwarded or warned about one by one. +func TestSpekUnsupportedNoForwardingNoWarnings(t *testing.T) { + logs := &syncLog{} + spek := unsupportedSpek{newFakeSpek()} + spek.Close(nil) // done from the start: Lines() is closed + h := newHarness(t, Policy{}, func(o *Options) { + o.NewSpek = func(meetings.Meeting) Spek { return spek } + o.Logger = slog.New(slog.NewTextHandler(logs, nil)) + }) + h.live() + h.eventually("stopped status", func() bool { + return strings.HasPrefix(h.state().SpekStatus, "Notes and clarifying questions are off for this meeting") + }) + h.line(meetings.EventSegment, "Ana", "let's start", true) + h.line(meetings.EventChat, "Bo", "agenda?", true) + h.line(meetings.EventSegment, "Ana", "first item", true) + h.end() + if got := spek.stdin(); len(got) != 0 { + t.Fatalf("lines forwarded to an unsupported spek: %v", got) + } + if out := logs.String(); strings.Contains(out, "forwarding to spek") || strings.Contains(out, "level=WARN") { + t.Fatalf("unexpected warnings: %s", out) + } +} + +// TestSpekForwardingWarnsOncePerOutage pins that a child that is down +// between restarts warns on the first failed forward, not on every line. +func TestSpekForwardingWarnsOncePerOutage(t *testing.T) { + logs := &syncLog{} + h := newHarness(t, Policy{}, func(o *Options) { o.Logger = slog.New(slog.NewTextHandler(logs, nil)) }) + h.live() + h.spek.mu.Lock() + h.spek.sendErr = errors.New("spektacular: stream child is not running") + h.spek.mu.Unlock() + for _, text := range []string{"one", "two", "three"} { + h.line(meetings.EventSegment, "Ana", text, true) + } + h.eventually("paused status", func() bool { + return strings.HasPrefix(h.state().SpekStatus, "Notes and clarifying questions are paused") + }) + h.end() + if n := strings.Count(logs.String(), "forwarding to spek"); n != 1 { + t.Fatalf("forwarding warnings = %d, want 1:\n%s", n, logs.String()) + } +} diff --git a/src/pkg/spektacular/stream.go b/src/pkg/spektacular/stream.go index 1c3f4f68c3..88f3330fab 100644 --- a/src/pkg/spektacular/stream.go +++ b/src/pkg/spektacular/stream.go @@ -9,7 +9,9 @@ import ( "fmt" "io" "log/slog" + "os" "os/exec" + "strings" "sync" "time" ) @@ -30,10 +32,35 @@ const ( maxStreamLine = 4 << 20 // streamLineBuffer is the stdout line channel depth. streamLineBuffer = 256 + // DefaultStreamProbeTimeout bounds the one-time capability probe. + DefaultStreamProbeTimeout = 10 * time.Second + // maxExitOutput bounds the stdout kept from a child to read its exit + // reason (the CLI's JSON error envelope is a few hundred bytes). + maxExitOutput = 4 << 10 + // unknownCommandMarker is how the CLI (cobra, wrapped in its JSON + // error envelope) says the verb does not exist in this build, for + // example `unknown command "meeting" for "spektacular"`. + unknownCommandMarker = "unknown command" ) -// ErrStreamNotRunning is returned by Send when no child is running. -var ErrStreamNotRunning = errors.New("spektacular: stream child is not running") +var ( + // ErrStreamNotRunning is returned by Send when no child is running. + ErrStreamNotRunning = errors.New("spektacular: stream child is not running") + // ErrStreamUnsupported is Err() for a stream whose verb the installed + // spektacular does not have (a release without `meeting`, + // hivecommons/spektacular#126). Such a stream never starts or restarts + // a child: Lines() is closed at once and Unsupported reports true. + ErrStreamUnsupported = errors.New("spektacular: the installed spektacular has no such command") +) + +// probeCache remembers, per binary file and probe, whether the verb exists, +// so the probe runs once per installed binary rather than once per stream. +// Keys are probeKey values; values are bool (true means supported). +var probeCache sync.Map + +// unsupportedLogged remembers which binaries were already logged as lacking +// the verb, so the INFO line is written once per binary, not per meeting. +var unsupportedLogged sync.Map // StreamOptions tunes a Stream. Zero values use the defaults. type StreamOptions struct { @@ -47,6 +74,13 @@ type StreamOptions struct { OnRestart func() // Env is the child environment; nil inherits hive's. Env []string + // Probe, when set, is a cheap command line (for example + // `meeting --help`) Start runs once per installed binary before the + // first child. When the CLI answers that the command is unknown, the + // stream is unsupported: no child is started or restarted. + Probe []string + // ProbeTimeout bounds the probe. + ProbeTimeout time.Duration } // Stream supervises one long-lived child. Lines() carries stdout lines @@ -66,6 +100,9 @@ type Stream struct { started bool restarts int exitErr error + // lastWarn is when the restart WARN was last written; restarts inside + // one BackoffMax window after it log at INFO. + lastWarn time.Time } func newStream(binary string, args []string, opts StreamOptions) *Stream { @@ -81,6 +118,9 @@ func newStream(binary string, args []string, opts StreamOptions) *Stream { if opts.CloseGrace <= 0 { opts.CloseGrace = DefaultStreamCloseGrace } + if opts.ProbeTimeout <= 0 { + opts.ProbeTimeout = DefaultStreamProbeTimeout + } log := opts.Logger if log == nil { log = slog.New(slog.DiscardHandler) @@ -111,8 +151,15 @@ func (s *Stream) Err() error { return s.exitErr } +// Unsupported reports whether the installed spektacular lacks the stream's +// verb, so the stream ended without (re)starting a child. +func (s *Stream) Unsupported() bool { return errors.Is(s.Err(), ErrStreamUnsupported) } + // Start launches the first child and the supervision loop. It fails when -// the first child cannot be started at all. +// the first child cannot be started at all. A stream whose verb the +// installed spektacular lacks (see StreamOptions.Probe) is not an error: +// Start returns nil with the stream already done and Unsupported true, so +// the caller carries on without it. func (s *Stream) Start(ctx context.Context) error { s.mu.Lock() if s.started { @@ -121,6 +168,12 @@ func (s *Stream) Start(ctx context.Context) error { } s.started = true s.mu.Unlock() + if !s.supported(ctx) { + s.finishUnsupported() + close(s.lines) + close(s.done) + return nil + } cmd, out, err := s.spawn(ctx) if err != nil { close(s.lines) @@ -131,6 +184,63 @@ func (s *Stream) Start(ctx context.Context) error { return nil } +// finishUnsupported ends a stream whose verb does not exist, before or +// instead of any (re)start. +func (s *Stream) finishUnsupported() { + s.mu.Lock() + s.exitErr = ErrStreamUnsupported + s.mu.Unlock() + if _, seen := unsupportedLogged.LoadOrStore(s.binary, true); !seen { + s.log.Info("[spektacular] live mode unavailable: the installed spektacular has no such command; not starting it", + "binary", s.binary, "args", strings.Join(s.args, " ")) + } +} + +// probeKey identifies one installed binary file and probe command line, so +// a replaced binary is probed again. +func probeKey(path string, fi os.FileInfo, probe []string) string { + return fmt.Sprintf("%s|%d|%d|%s", path, fi.Size(), fi.ModTime().UnixNano(), strings.Join(probe, "\x00")) +} + +// supported runs the probe (once per binary) and reports whether the verb +// exists. Anything inconclusive (no probe, binary not found, probe timeout +// or a failure that is not "unknown command") counts as supported, so the +// normal start path and its errors apply. +func (s *Stream) supported(ctx context.Context) bool { + if len(s.opts.Probe) == 0 { + return true + } + path, err := exec.LookPath(s.binary) + if err != nil { + return true + } + fi, err := os.Stat(path) + if err != nil { + return true + } + key := probeKey(path, fi, s.opts.Probe) + if v, cached := probeCache.Load(key); cached { + known, _ := v.(bool) + return known + } + pctx, cancel := context.WithTimeout(ctx, s.opts.ProbeTimeout) + defer cancel() + cmd := exec.CommandContext(pctx, path, s.opts.Probe...) + cmd.Env = s.opts.Env + out, runErr := cmd.CombinedOutput() + if pctx.Err() != nil { + return true + } + ok := runErr == nil || !isUnknownCommand(out) + probeCache.Store(key, ok) + return ok +} + +// isUnknownCommand reports whether CLI output says the verb does not exist. +func isUnknownCommand(out []byte) bool { + return bytes.Contains(bytes.ToLower(out), []byte(unknownCommandMarker)) +} + func (s *Stream) spawn(ctx context.Context) (*exec.Cmd, io.ReadCloser, error) { cmd := exec.Command(s.binary, s.args...) cmd.Env = s.opts.Env @@ -160,7 +270,8 @@ func (s *Stream) loop(ctx context.Context, cmd *exec.Cmd, out io.ReadCloser) { defer close(s.lines) backoff := s.opts.BackoffMin for { - err := s.pump(ctx, cmd, out) + head := &headBuffer{max: maxExitOutput} + err := s.pump(ctx, cmd, out, head) s.mu.Lock() s.stdin, s.cmd, s.exitErr = nil, nil, err closing := s.closing @@ -169,11 +280,17 @@ func (s *Stream) loop(ctx context.Context, cmd *exec.Cmd, out io.ReadCloser) { if closing || ctx.Err() != nil || err == nil { return } + // The verb does not exist in this build (no probe was configured, + // or the binary changed under it): restarting cannot help. + if isUnknownCommand(head.b) { + s.finishUnsupported() + return + } if restarts >= s.opts.MaxRestarts { s.log.Error("[spektacular] stream child keeps exiting; giving up", "binary", s.binary, "restarts", restarts, "error", err) return } - s.log.Warn("[spektacular] stream child exited; restarting", "error", err, "backoff", backoff.String()) + s.logRestart(err, backoff, restarts) select { case <-ctx.Done(): return @@ -201,9 +318,42 @@ func (s *Stream) loop(ctx context.Context, cmd *exec.Cmd, out io.ReadCloser) { } } +// logRestart writes the restart line at WARN at most once per BackoffMax +// window and at INFO otherwise, so one child's backoff series is a single +// warning rather than one per attempt (hivecommons/hive#18630). +func (s *Stream) logRestart(err error, backoff time.Duration, restarts int) { + now := time.Now() + s.mu.Lock() + warn := s.lastWarn.IsZero() || now.Sub(s.lastWarn) >= s.opts.BackoffMax + if warn { + s.lastWarn = now + } + s.mu.Unlock() + level := slog.LevelInfo + if warn { + level = slog.LevelWarn + } + s.log.Log(context.Background(), level, "[spektacular] stream child exited; restarting", + "error", err, "backoff", backoff.String(), "restarts", restarts) +} + +// headBuffer keeps the first max bytes of a child's stdout. +type headBuffer struct { + max int + b []byte +} + +func (h *headBuffer) add(line []byte) { + if room := h.max - len(h.b); room > 0 { + h.b = append(h.b, line[:min(len(line), room)]...) + h.b = append(h.b, '\n') + } +} + // pump copies stdout lines until the child exits, killing it when ctx -// ends. It returns the child's exit error. -func (s *Stream) pump(ctx context.Context, cmd *exec.Cmd, out io.Reader) error { +// ends. It returns the child's exit error. head, when set, keeps the start +// of stdout so the caller can read the exit reason. +func (s *Stream) pump(ctx context.Context, cmd *exec.Cmd, out io.Reader, head *headBuffer) error { readDone := make(chan struct{}) go func() { defer close(readDone) @@ -214,6 +364,9 @@ func (s *Stream) pump(ctx context.Context, cmd *exec.Cmd, out io.Reader) error { if len(line) == 0 { continue } + if head != nil { + head.add(line) + } s.lines <- append([]byte(nil), line...) } }() diff --git a/src/pkg/spektacular/stream_test.go b/src/pkg/spektacular/stream_test.go index 4755398d85..b2e859cc68 100644 --- a/src/pkg/spektacular/stream_test.go +++ b/src/pkg/spektacular/stream_test.go @@ -5,6 +5,7 @@ import ( "context" "errors" "log/slog" + "os" "path/filepath" "strings" "sync" @@ -184,3 +185,151 @@ func TestStreamLogWriter(t *testing.T) { t.Fatalf("logs %q buf %d", logs.String(), len(w.buf)) } } + +// fakeCLI writes an executable shell script standing in for spektacular and +// returns its path and a file counting its invocations. +func fakeCLI(t *testing.T, body string) (bin, calls string) { + t.Helper() + dir := t.TempDir() + bin, calls = filepath.Join(dir, "spektacular"), filepath.Join(dir, "calls") + script := "#!/bin/sh\necho x >> '" + calls + "'\n" + body + "\n" + if err := os.WriteFile(bin, []byte(script), 0o700); err != nil { + t.Fatal(err) + } + return bin, calls +} + +func callCount(t *testing.T, calls string) int { + t.Helper() + b, err := os.ReadFile(calls) + if errors.Is(err, os.ErrNotExist) { + return 0 + } + if err != nil { + t.Fatal(err) + } + return strings.Count(string(b), "x\n") +} + +// oldCLIBody is what a spektacular release without `meeting` prints for it: +// cobra's unknown-command error in the CLI's indented JSON error envelope on +// stdout, exit 1 (hivecommons/hive#18630). +const oldCLIBody = `cat <<'JSON' +{ + "error": true, + "code": "internal_error", + "message": "unknown command \"meeting\" for \"spektacular\"", + "next_action": "" +} +JSON +exit 1` + +// TestStreamProbeUnsupportedNeverStartsChild pins #18630: when the probe +// says the verb is unknown, no child is started or restarted, the stream is +// done and Unsupported, and the probe runs once per installed binary. +func TestStreamProbeUnsupportedNeverStartsChild(t *testing.T) { + bin, calls := fakeCLI(t, oldCLIBody) + logs := &lockedLog{} + opts := StreamOptions{ + Logger: slog.New(slog.NewTextHandler(logs, nil)), Probe: []string{"meeting", "--help"}, + BackoffMin: time.Millisecond, + } + for i := 0; i < 2; i++ { + s := StreamExec(bin, []string{"meeting", "--from", "stdin"}, opts) + if err := s.Start(context.Background()); err != nil { + t.Fatalf("Start = %v, want nil for an unsupported verb", err) + } + waitDone(t, s) + if _, ok := <-s.Lines(); ok { + t.Fatal("lines must be closed") + } + if !s.Unsupported() || !errors.Is(s.Err(), ErrStreamUnsupported) || s.Restarts() != 0 { + t.Fatalf("unsupported=%v err=%v restarts=%d", s.Unsupported(), s.Err(), s.Restarts()) + } + if !errors.Is(s.SendLine([]byte("{}")), ErrStreamNotRunning) { + t.Fatal("Send must fail") + } + } + if n := callCount(t, calls); n != 1 { + t.Fatalf("binary ran %d times, want only the one probe", n) + } + out := logs.String() + if strings.Contains(out, "level=WARN") || strings.Contains(out, "level=ERROR") || strings.Count(out, "live mode unavailable") != 1 { + t.Fatalf("want one INFO line and no warnings:\n%s", out) + } +} + +// TestStreamUnknownCommandExitDoesNotRestart covers a stream without a +// probe: the child's exit reason says the verb is unknown, so it is not +// restarted. +func TestStreamUnknownCommandExitDoesNotRestart(t *testing.T) { + bin, calls := fakeCLI(t, oldCLIBody) + s := StreamExec(bin, []string{"meeting"}, StreamOptions{BackoffMin: time.Millisecond, MaxRestarts: 3}) + if err := s.Start(context.Background()); err != nil { + t.Fatal(err) + } + for range s.Lines() { + } + waitDone(t, s) + if !s.Unsupported() || s.Restarts() != 0 || callCount(t, calls) != 1 { + t.Fatalf("unsupported=%v restarts=%d calls=%d", s.Unsupported(), s.Restarts(), callCount(t, calls)) + } +} + +// TestStreamProbeSupportedStarts: a binary that knows the verb starts as +// before. +func TestStreamProbeSupportedStarts(t *testing.T) { + bin, calls := fakeCLI(t, `[ "$2" = "--help" ] && exit 0 +while read -r l; do echo "$l"; done`) + s := StreamExec(bin, []string{"meeting"}, StreamOptions{Probe: []string{"meeting", "--help"}}) + if err := s.Start(context.Background()); err != nil { + t.Fatal(err) + } + if err := s.SendLine([]byte(`{"type":"segment"}`)); err != nil { + t.Fatal(err) + } + if got := readLine(t, s); got != `{"type":"segment"}` { + t.Fatalf("echo = %q", got) + } + s.Close(nil) + waitDone(t, s) + if s.Unsupported() || callCount(t, calls) != 2 { + t.Fatalf("unsupported=%v calls=%d", s.Unsupported(), callCount(t, calls)) + } +} + +// TestStreamRestartWarnsOncePerBackoffWindow pins #18630: a child that keeps +// exiting is restarted with backoff, but the restart line is a WARN at most +// once per BackoffMax window (INFO otherwise), so one backoff series does not +// look like a repeated unclassified error. +func TestStreamRestartWarnsOncePerBackoffWindow(t *testing.T) { + logs := &lockedLog{} + s := StreamExec("sh", []string{"-c", "exit 3"}, StreamOptions{ + Logger: slog.New(slog.NewTextHandler(logs, nil)), + BackoffMin: time.Millisecond, BackoffMax: time.Hour, MaxRestarts: 4, + }) + if err := s.Start(context.Background()); err != nil { + t.Fatal(err) + } + for range s.Lines() { + } + waitDone(t, s) + if s.Restarts() != 4 { + t.Fatalf("restarts = %d", s.Restarts()) + } + warn, info := 0, 0 + for _, l := range strings.Split(logs.String(), "\n") { + if !strings.Contains(l, "stream child exited; restarting") { + continue + } + switch { + case strings.Contains(l, "level=WARN"): + warn++ + case strings.Contains(l, "level=INFO"): + info++ + } + } + if warn != 1 || info != 3 { + t.Fatalf("warn=%d info=%d, want 1 and 3:\n%s", warn, info, logs.String()) + } +} From c6f8149078957a494a27500b5c191b3d6dec2869 Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 16 Nov 2026 21:48:17 -0500 Subject: [PATCH 02/13] =?UTF-8?q?=E2=9C=A8=20Meetings:=20delete=20finished?= =?UTF-8?q?=20meetings=20and=20stale=20join=20requests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Owner-only DELETE /api/meetings/{id} for meetings in a final state and pending join requests (declined, then removed); 409 while hive is still in the meeting. Removes the record, transcript and live state, erases the meeting in Vexa with Quorum meeting.delete (best effort, surfaced as upstream_error) and drops the Calendar entry that existed only for it. POST /api/meetings/clear-finished deletes all finished meetings older than now; DELETE /api/meetings/schedule/{eid} deletes a past Calendar entry with its meetings. Knowledge facts and drafts are not touched. Audited as meeting_delete / meeting_schedule_delete. UI: 🗑 Delete on finished and join-request rows, Clear finished, and 🗑 Delete on past Calendar entries, all behind the in-page confirm. Signed-off-by: Andy Anderson --- changelog.d/added-meetings-delete.md | 1 + dashboard/openapi.json | 204 ++++++++++++++++++ src/cmd/hive/main.go | 7 +- src/cmd/hive/meetings_dashboard.go | 34 ++- src/cmd/hive/meetings_delete_test.go | 102 +++++++++ src/cmd/hive/meetings_schedule.go | 51 ++++- src/docs/api-reference/knowledge.md | 3 + src/docs/meetings.md | 3 + src/pkg/dashboard/api_meetings.go | 6 + src/pkg/dashboard/api_meetings_delete.go | 140 ++++++++++++ src/pkg/dashboard/api_meetings_delete_test.go | 183 ++++++++++++++++ src/pkg/dashboard/api_meetings_schedule.go | 3 + .../dashboard/api_meetings_schedule_test.go | 3 + src/pkg/dashboard/api_meetings_test.go | 3 + src/pkg/dashboard/meetings_delete.test.js | 108 ++++++++++ src/pkg/dashboard/static-src/js/calendar.js | 36 ++++ src/pkg/dashboard/static-src/js/meetings.js | 95 +++++++- src/pkg/dashboard/static/index.html | 131 ++++++++++- src/pkg/meetings/delete.go | 119 ++++++++++ src/pkg/meetings/delete_test.go | 138 ++++++++++++ src/pkg/meetings/schedule/delete.go | 98 +++++++++ src/pkg/meetings/schedule/delete_test.go | 74 +++++++ src/pkg/meetings/schedule/store.go | 24 +++ src/pkg/meetings/store.go | 15 ++ 24 files changed, 1573 insertions(+), 8 deletions(-) create mode 100644 changelog.d/added-meetings-delete.md create mode 100644 src/cmd/hive/meetings_delete_test.go create mode 100644 src/pkg/dashboard/api_meetings_delete.go create mode 100644 src/pkg/dashboard/api_meetings_delete_test.go create mode 100644 src/pkg/dashboard/meetings_delete.test.js create mode 100644 src/pkg/meetings/delete.go create mode 100644 src/pkg/meetings/delete_test.go create mode 100644 src/pkg/meetings/schedule/delete.go create mode 100644 src/pkg/meetings/schedule/delete_test.go diff --git a/changelog.d/added-meetings-delete.md b/changelog.d/added-meetings-delete.md new file mode 100644 index 0000000000..075ec6b901 --- /dev/null +++ b/changelog.d/added-meetings-delete.md @@ -0,0 +1 @@ +- Meetings: finished meetings (ended, failed, blocked, declined, left) and stale join requests get a 🗑 Delete action with an in-page confirm, plus **Clear finished** to delete all finished meetings at once. Delete removes the meeting record, its transcript and live state on the hive, erases it in Vexa with Quorum `meeting.delete` (best effort; a failure is reported and the record is still removed), and drops the Calendar entry that existed only for that meeting. Past Calendar entries get the same 🗑 Delete; scheduled ones keep Skip and Cancel. A meeting hive is still in answers 409 (leave it first). Knowledge facts and drafts made from a meeting are not touched. Owner only, audited as `meeting_delete` (`DELETE /api/meetings/{id}`, `POST /api/meetings/clear-finished`, `DELETE /api/meetings/schedule/{eid}`). diff --git a/dashboard/openapi.json b/dashboard/openapi.json index ed80a0500b..a84b7e7096 100644 --- a/dashboard/openapi.json +++ b/dashboard/openapi.json @@ -149,6 +149,152 @@ } } }, + "/api/meetings/clear-finished": { + "post": { + "tags": [ + "Meetings" + ], + "summary": "Clear finished meetings", + "description": "Owner only. Deletes every meeting in a final state that finished before `before` (RFC 3339; empty or later than now means now), each exactly like DELETE /api/meetings/{id}. Live meetings and pending join requests stay. Each deleted meeting is audited as meeting_delete.", + "requestBody": { + "required": false, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "before": { + "type": "string", + "format": "date-time" + } + } + } + } + } + }, + "responses": { + "200": { + "description": "Deleted meetings", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "deleted": { + "type": "array", + "items": { + "type": "object", + "properties": { + "id": { + "type": "string" + }, + "state": { + "type": "string", + "description": "The state the meeting was in" + }, + "upstream_error": { + "type": "string", + "description": "Set when Vexa (Quorum meeting.delete) could not confirm the erase; the record on this hive is removed anyway" + }, + "calendar_removed": { + "type": "integer", + "description": "Calendar entries removed because they existed only for this meeting" + } + } + } + }, + "count": { + "type": "integer" + } + } + } + } + } + }, + "400": { + "description": "before is not an RFC 3339 time" + }, + "403": { + "description": "Owner access required" + }, + "503": { + "description": "Meetings are not enabled on this hive" + } + } + } + }, + "/api/meetings/schedule/{eid}": { + "delete": { + "tags": [ + "Meetings" + ], + "summary": "Delete a past Calendar entry", + "description": "Owner only. Deletes a finished Calendar entry (ended, cancelled, declined, failed, blocked, skipped) and the meetings it created, each like DELETE /api/meetings/{id}. Entries still scheduled, awaiting approval or live keep Skip and Cancel and answer 409, as does an entry whose meeting hive is still in. Audited as meeting_schedule_delete, plus meeting_delete for each meeting.", + "parameters": [ + { + "name": "eid", + "in": "path", + "required": true, + "schema": { + "type": "string" + }, + "description": "Schedule event id" + } + ], + "responses": { + "200": { + "description": "Deleted", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "id": { + "type": "string" + }, + "meetings": { + "type": "array", + "items": { + "type": "object", + "properties": { + "id": { + "type": "string" + }, + "state": { + "type": "string", + "description": "The state the meeting was in" + }, + "upstream_error": { + "type": "string", + "description": "Set when Vexa (Quorum meeting.delete) could not confirm the erase; the record on this hive is removed anyway" + }, + "calendar_removed": { + "type": "integer", + "description": "Calendar entries removed because they existed only for this meeting" + } + } + } + } + } + } + } + } + }, + "403": { + "description": "Owner access required" + }, + "404": { + "description": "Unknown event" + }, + "409": { + "description": "The entry is still to come or live, or hive is still in its meeting" + }, + "503": { + "description": "Meetings are not enabled on this hive" + } + } + } + }, "/api/meetings/{id}/decline": { "post": { "tags": [ @@ -438,6 +584,64 @@ } }, "/api/meetings/{id}": { + "delete": { + "tags": [ + "Meetings" + ], + "summary": "Delete a finished meeting or a pending join request", + "description": "Owner only. Allowed for a meeting in a final state (ended, failed, blocked, declined, left, handed_off, purged) and for a pending platform join request (declined with meeting.leave, then removed). Removes the meeting record, its transcript and live state on this hive, erases it in Vexa with Quorum meeting.delete (best effort: not_found is fine, any other error is returned as upstream_error while the record is still removed) and removes the Calendar entry that existed only for this meeting. Knowledge facts and drafts already made from the meeting are not touched. Audited as meeting_delete.", + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "Deleted", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "id": { + "type": "string" + }, + "state": { + "type": "string", + "description": "The state the meeting was in" + }, + "upstream_error": { + "type": "string", + "description": "Set when Vexa (Quorum meeting.delete) could not confirm the erase; the record on this hive is removed anyway" + }, + "calendar_removed": { + "type": "integer", + "description": "Calendar entries removed because they existed only for this meeting" + } + } + } + } + } + }, + "403": { + "description": "Owner access required" + }, + "404": { + "description": "Unknown meeting" + }, + "409": { + "description": "Hive is still joining, in the lobby of or live in the meeting: leave the meeting first" + }, + "503": { + "description": "Meetings are not enabled on this hive" + } + } + }, "get": { "tags": [ "Meetings" diff --git a/src/cmd/hive/main.go b/src/cmd/hive/main.go index ed7f538ac7..deb20eda03 100644 --- a/src/cmd/hive/main.go +++ b/src/cmd/hive/main.go @@ -3601,12 +3601,15 @@ func (b *boot) bootDashboardAPIWith(deps bootDashboardAPIDeps) { // Drafts people ask Betty for, approved in the dashboard (#18406). b.wireMeetingsActions(store) // Owner API, live panel and /api/events frames (#18292). - b.dashSrv.SetMeetingsService(&meetingsDashboard{mgr: mgr, live: b.meetingsLive}) - // Scheduled meetings and the Calendar section (#18394). + md := &meetingsDashboard{mgr: mgr, live: b.meetingsLive} + // Scheduled meetings and the Calendar section (#18394). Wired + // first so a deleted meeting's Calendar entry goes with it. if svc, stop := wireMeetingsSchedule(b.cfg, mgr, b.logger); svc != nil { b.cleanup.push(stop) b.dashSrv.SetMeetingsScheduleService(svc) + md.sched = svc.sched } + b.dashSrv.SetMeetingsService(md) // Ended meetings become inbox items; transcripts are purged // after meetings.retention_days (#18293). b.wireMeetingsInbox(mgr, store) diff --git a/src/cmd/hive/meetings_dashboard.go b/src/cmd/hive/meetings_dashboard.go index 8bc8aeb0d6..299ffb0cf2 100644 --- a/src/cmd/hive/meetings_dashboard.go +++ b/src/cmd/hive/meetings_dashboard.go @@ -4,10 +4,12 @@ import ( "context" "errors" "fmt" + "time" "github.com/hivecommons/hive/pkg/dashboard" "github.com/hivecommons/hive/pkg/meetings" "github.com/hivecommons/hive/pkg/meetings/live" + "github.com/hivecommons/hive/pkg/meetings/schedule" ) // meetingsDashboard implements dashboard.MeetingsService over the meeting @@ -16,6 +18,8 @@ import ( type meetingsDashboard struct { mgr *meetings.Manager live *live.Runner + // sched, when the Calendar runs, forgets a deleted meeting's entry. + sched *schedule.Scheduler } // meetingsDashboardError maps meetings errors onto the dashboard's @@ -28,7 +32,8 @@ func meetingsDashboardError(err error) error { return fmt.Errorf("%w: %v", dashboard.ErrMeetingNotFound, err) case errors.Is(err, meetings.ErrInvalidRequest), meetings.IsCode(err, meetings.CodeInvalidArgument): return fmt.Errorf("%w: %v", dashboard.ErrMeetingInvalid, err) - case errors.Is(err, live.ErrNotPending), errors.Is(err, live.ErrSessionEnded), errors.Is(err, meetings.ErrNotLive), errors.Is(err, meetings.ErrNotRequested): + case errors.Is(err, live.ErrNotPending), errors.Is(err, live.ErrSessionEnded), errors.Is(err, meetings.ErrNotLive), errors.Is(err, meetings.ErrNotRequested), + errors.Is(err, meetings.ErrStillRunning): return fmt.Errorf("%w: %v", dashboard.ErrMeetingConflict, err) default: return err @@ -101,6 +106,33 @@ func (d *meetingsDashboard) DeclineRequest(ctx context.Context, id string) (any, return m, nil } +// meetingDeleted is the dashboard's view of a deleted meeting; with the +// Calendar running, the entry that existed only for it goes too. +func meetingDeleted(res meetings.DeleteResult, sched *schedule.Scheduler) dashboard.MeetingDeleted { + out := dashboard.MeetingDeleted{ID: res.Meeting.ID, State: string(res.Meeting.State), UpstreamError: res.UpstreamError} + if sched != nil { + out.CalendarRemoved = sched.ForgetMeeting(res.Meeting.ID) + } + return out +} + +func (d *meetingsDashboard) Delete(ctx context.Context, id string) (dashboard.MeetingDeleted, error) { + res, err := d.mgr.Delete(ctx, id) + if err != nil { + return dashboard.MeetingDeleted{}, meetingsDashboardError(err) + } + return meetingDeleted(res, d.sched), nil +} + +func (d *meetingsDashboard) ClearFinished(ctx context.Context, before time.Time) ([]dashboard.MeetingDeleted, error) { + list, err := d.mgr.ClearFinished(ctx, before) + out := make([]dashboard.MeetingDeleted, 0, len(list)) + for _, res := range list { + out = append(out, meetingDeleted(res, d.sched)) + } + return out, meetingsDashboardError(err) +} + // meetingLive is the panel snapshot. type meetingLive struct { Meeting meetings.Meeting `json:"meeting"` diff --git a/src/cmd/hive/meetings_delete_test.go b/src/cmd/hive/meetings_delete_test.go new file mode 100644 index 0000000000..76d7ef7edb --- /dev/null +++ b/src/cmd/hive/meetings_delete_test.go @@ -0,0 +1,102 @@ +package main + +import ( + "context" + "encoding/json" + "errors" + "os" + "path/filepath" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/dashboard" + "github.com/hivecommons/hive/pkg/meetings" + "github.com/hivecommons/hive/pkg/meetings/schedule" +) + +// TestMeetingsDeleteAdapters: deleting a meeting drops the Calendar entry +// that existed only for it; deleting a past Calendar entry deletes its +// meeting; live meetings and entries still to come answer 409. +func TestMeetingsDeleteAdapters(t *testing.T) { + dir := t.TempDir() + store, err := meetings.NewStore(dir) + if err != nil { + t.Fatal(err) + } + now := time.Now().UTC().Truncate(time.Second) + past := now.Add(-2 * time.Hour) + for _, m := range []meetings.Meeting{ + {ID: "mtg-ended", NativeID: "q-e", State: meetings.StateEnded, EndedAt: &past, CreatedAt: past}, + {ID: "mtg-cal", NativeID: "q-c", State: meetings.StateFailed, EndedAt: &past, CreatedAt: past}, + {ID: "mtg-live", NativeID: "q-l", State: meetings.StateLive, CreatedAt: past}, + } { + if err := store.Save(m); err != nil { + t.Fatal(err) + } + } + ev := func(uid string, st schedule.Status, start time.Time, mid string) schedule.Event { + e := schedule.Event{UID: uid, Start: start, Status: st, Platform: schedule.PlatformGoogleMeet, + JoinURL: "https://meet.google.com/abc", Source: schedule.SourceManual, MeetingID: mid, CreatedAt: past, UpdatedAt: now} + if mid != "" { + e.History = []schedule.Occurrence{{Start: start, MeetingID: mid, Status: st}} + } + e.ID = schedule.EventID(uid, nil) + return e + } + ended := ev("u-ended", schedule.StatusEnded, past, "mtg-ended") + failed := ev("u-cal", schedule.StatusFailed, past, "mtg-cal") + future := ev("u-future", schedule.StatusScheduled, now.Add(24*time.Hour), "") + data, err := json.Marshal(map[string]any{"version": 1, "events": []schedule.Event{ended, failed, future}}) + if err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, schedule.FileName), data, 0o600); err != nil { + t.Fatal(err) + } + mgr, err := meetings.NewManager(meetings.Options{Store: store, Quorum: &stubQuorum{events: make(chan meetings.Event, 1)}}) + if err != nil { + t.Fatal(err) + } + defer mgr.Close() + cfg := &config.Config{} + cfg.Meetings.DataDir = dir + svc, stop := wireMeetingsSchedule(cfg, mgr, nil) + if svc == nil { + t.Fatal("schedule not wired") + } + defer stop() + md := &meetingsDashboard{mgr: mgr, sched: svc.sched} + ctx := context.Background() + + if _, err := md.Delete(ctx, "mtg-live"); !errors.Is(err, dashboard.ErrMeetingConflict) { + t.Fatalf("delete live = %v, want conflict", err) + } + if _, err := md.Delete(ctx, "mtg-none"); !errors.Is(err, dashboard.ErrMeetingNotFound) { + t.Fatalf("delete unknown = %v, want not found", err) + } + d, err := md.Delete(ctx, "mtg-ended") + if err != nil || d.ID != "mtg-ended" || d.State != "ended" || d.CalendarRemoved != 1 { + t.Fatalf("delete ended = %+v, %v", d, err) + } + if _, err := svc.sched.Get(ended.ID); !errors.Is(err, schedule.ErrNotFound) { + t.Fatalf("calendar entry kept: %v", err) + } + + if _, err := svc.Delete(ctx, future.ID); !errors.Is(err, dashboard.ErrMeetingConflict) { + t.Fatalf("delete future entry = %v, want conflict", err) + } + cd, err := svc.Delete(ctx, failed.ID) + if err != nil || len(cd.Meetings) != 1 || cd.Meetings[0].ID != "mtg-cal" { + t.Fatalf("delete past entry = %+v, %v", cd, err) + } + if _, err := mgr.Get("mtg-cal"); !errors.Is(err, meetings.ErrNotFound) { + t.Fatalf("entry's meeting kept: %v", err) + } + if _, err := svc.sched.Get(future.ID); err != nil { + t.Fatalf("future entry touched: %v", err) + } + if _, err := mgr.Get("mtg-live"); err != nil { + t.Fatalf("live meeting touched: %v", err) + } +} diff --git a/src/cmd/hive/meetings_schedule.go b/src/cmd/hive/meetings_schedule.go index 06d2143aef..a7da1ae4c8 100644 --- a/src/cmd/hive/meetings_schedule.go +++ b/src/cmd/hive/meetings_schedule.go @@ -31,6 +31,8 @@ const ( // meeting scheduler (hivecommons/hive#18394). type meetingsSchedule struct { sched *schedule.Scheduler + // mgr deletes the meetings a deleted Calendar entry created. + mgr *meetings.Manager // attendee is the name the dashboard's "in a meeting" indicator uses. attendee string now func() time.Time @@ -129,6 +131,53 @@ func (d *meetingsSchedule) Act(_ context.Context, eid, action, by string) (any, return ev.Redacted(), nil } +// Delete removes a finished Calendar entry and the meetings it created. +// An entry still to come or live answers 409 (skip or cancel it); so does +// one whose meeting hive is still in. Meetings already gone are skipped. +func (d *meetingsSchedule) Delete(ctx context.Context, eid string) (dashboard.MeetingScheduleDeleted, error) { + out := dashboard.MeetingScheduleDeleted{ID: eid, Meetings: []dashboard.MeetingDeleted{}} + e, err := d.sched.Get(eid) + if err != nil { + return out, meetingsScheduleError(err) + } + if !e.Status.Terminal() { + return out, fmt.Errorf("%w: this meeting is %s; skip or cancel it instead", dashboard.ErrMeetingConflict, e.Status) + } + ids := schedule.LinkedMeetings(e) + if d.mgr != nil { + for _, id := range ids { + m, err := d.mgr.Get(id) + if errors.Is(err, meetings.ErrNotFound) { + continue + } + if err != nil { + return out, err + } + if !meetings.Deletable(m) { + return out, fmt.Errorf("%w: hive is still in this meeting; leave the meeting first", dashboard.ErrMeetingConflict) + } + } + } + if _, err := d.sched.Delete(eid); err != nil { + return out, meetingsScheduleError(err) + } + if d.mgr == nil { + return out, nil + } + for _, id := range ids { + res, err := d.mgr.Delete(ctx, id) + if errors.Is(err, meetings.ErrNotFound) { + continue + } + if err != nil { + out.Meetings = append(out.Meetings, dashboard.MeetingDeleted{ID: id, UpstreamError: err.Error()}) + continue + } + out.Meetings = append(out.Meetings, meetingDeleted(res, nil)) + } + return out, nil +} + // wireMeetingsSchedule starts the meeting scheduler over the manager // (hivecommons/hive#18394): schedule.json in the meetings data directory, // joins HIVE_MEETINGS_SCHEDULE_LEAD_S before start, scans every @@ -186,5 +235,5 @@ func wireMeetingsSchedule(cfg *config.Config, mgr *meetings.Manager, logger *slo <-done sched.Close() } - return &meetingsSchedule{sched: sched, attendee: attendeeName(cfg.Meetings.BotNameOrDefault()), now: time.Now}, stop + return &meetingsSchedule{sched: sched, mgr: mgr, attendee: attendeeName(cfg.Meetings.BotNameOrDefault()), now: time.Now}, stop } diff --git a/src/docs/api-reference/knowledge.md b/src/docs/api-reference/knowledge.md index a417a420ce..ecb46bfbe2 100644 --- a/src/docs/api-reference/knowledge.md +++ b/src/docs/api-reference/knowledge.md @@ -99,6 +99,8 @@ Part of the [Dashboard REST API reference](../api-reference.md); see that page f | `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:131` | | `POST` | `/api/meetings/{id}/approve` | Owner only | Approve a platform join request (Discord `/hive join`): joins that channel; optional `{voice}`. 409 when the meeting is not a pending request. Audited as `meeting_request_approve` | `pkg/dashboard/api_meetings.go:132` | | `POST` | `/api/meetings/{id}/decline` | Owner only | Decline a platform join request with `meeting.leave`. Audited as `meeting_request_decline` | `pkg/dashboard/api_meetings.go:133` | +| `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:134` | | `GET` | `/api/meetings/schedule` | Owner or read-write | The Calendar: `{upcoming, recent, attendee_name}`; 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:83` | | `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:84` | @@ -106,6 +108,7 @@ Part of the [Dashboard REST API reference](../api-reference.md); see that page f | `POST` | `/api/meetings/schedule/{eid}/decline` | Owner only | Decline an event awaiting approval; later updates of the same invite stay declined. Audited as `meeting_schedule_decline` | `pkg/dashboard/api_meetings_schedule.go:86` | | `POST` | `/api/meetings/schedule/{eid}/cancel` | Owner only | Stop hive joining the event (every occurrence of a series); a meeting already live is not left. Audited as `meeting_schedule_cancel` | `pkg/dashboard/api_meetings_schedule.go:87` | | `POST` | `/api/meetings/schedule/{eid}/skip` | Owner only | Skip the next occurrence hive has not joined: a single meeting becomes `skipped`, a series moves on. Audited as `meeting_schedule_skip` | `pkg/dashboard/api_meetings_schedule.go:88` | +| `DELETE` | `/api/meetings/schedule/{eid}` | Owner only | Delete a past Calendar entry (ended, cancelled, declined, failed, blocked, skipped) and the meetings it created, each as `DELETE /api/meetings/{id}`. 200 `{id, meetings}`; 404 unknown; 409 for an entry still scheduled, awaiting approval or live (skip or cancel it) or whose meeting hive is still in. Audited as `meeting_schedule_delete`, plus `meeting_delete` per meeting | `pkg/dashboard/api_meetings_delete.go:46` | | `GET` | `/api/meetings/actions` | Owner or read-write | Drafts from meetings: `{drafts, pending}`, newest first. Each draft carries `id`, `meeting_id`, `meeting_title`, `meeting_link`, `type` (`issue`, `rfc`, `spek`, `knowledge`), `title`, `body`, `repo`, `excerpt`, `requested_by` (a meeting display name), `request`, `spoken`, `status` (`pending`, `approving`, `done`, `failed`, `rejected`), `auto_approved`, `result_url`, `result_note`, `error`, `announced` (`chat` or `notes`), `edited_by`, `decided_by`, `decided_at`, `created_at`, `updated_at`. See [Asking Betty to do things](../meetings.md#asking-betty-to-do-things) | `pkg/dashboard/api_meetings_actions.go:69` | | `GET` | `/api/meetings/actions/settings` | Owner or read-write | `meetings.persona.actions` as set and in effect: `enabled`, `types`, `all_types`, `default_repo`, `effective_default_repo`, `repos` (the monitored repos a draft may target), `auto_approve` (per type), `auto_approvable` (`knowledge`) and `env_overrides` | `pkg/dashboard/api_meetings_actions.go:70` | | `PUT` | `/api/meetings/actions/settings` | Owner only | Save `{enabled?, types?, default_repo?, auto_approve?}` into `meetings.persona.actions`; applies at once. 400 for an unknown type, a repo that is not `owner/repo`, or auto-approve on anything but `knowledge`. Audited as `meetings_actions_settings` | `pkg/dashboard/api_meetings_actions.go:71` | diff --git a/src/docs/meetings.md b/src/docs/meetings.md index fb6e412eb1..ae5872b73c 100644 --- a/src/docs/meetings.md +++ b/src/docs/meetings.md @@ -143,6 +143,8 @@ Only the hive owner can send hive to a meeting. Voice is fixed when the bot joins: Vexa only lets a bot speak if it was started with voice. Hive starts every bot able to speak while `meetings.voice_on_address` is true (and when you tick **Voice**), so the toggle can turn speaking on later; with `meetings.voice_on_address` false and **Voice** unticked, turning voice on later has no effect and hive keeps answering in chat (the panel says so). +**🗑 Delete** (owner only) removes a meeting for good: a meeting that ended, failed, was blocked, declined or left, or a join request still waiting (deleting it declines it). It deletes the meeting record, its transcript, notes and live state on this hive, erases the meeting in Vexa with Quorum's `meeting.delete`, and removes its Calendar entry when that entry existed only for this meeting. If Vexa cannot confirm the erase, the meeting is still removed here and the dashboard says so. A meeting hive is still joining, waiting in the lobby of or live in cannot be deleted: **Leave** it first. **Clear finished** deletes every finished meeting after one confirm; live meetings and pending join requests stay. Knowledge facts and drafts already made from a meeting are not touched. Each delete is audited as `meeting_delete`. + If the meeting platform's chat is unavailable, hive cannot post the consent notice. It retries a few times and posts nothing else into the meeting until the notice is out; with voice on it speaks the notice instead, and otherwise it leaves (`consent_undelivered`). Until the notice is delivered the panel shows no transcript, notes or research: those lines are discarded, not held back. The same operations are available over the [meetings API](api-reference/knowledge.md) (`/api/meetings`). @@ -155,6 +157,7 @@ The **Calendar** section, right after **Inbox** in the left menu, lists the meet - **Each row** shows the title, organizer, start and end, platform, the next occurrence of a repeating meeting, the source (email invite, added here, or Discord) and the status: scheduled, awaiting approval, live, ended, cancelled, declined, failed, blocked or skipped. - **Approve** or **Decline** a meeting awaiting approval (owner only). A declined invite stays declined when its organizer updates it. - **Skip** skips the next occurrence: a single meeting becomes skipped, a repeating one moves on to the one after. **Cancel** stops hive joining it at all; a meeting already live is not left (use **Leave** in Meetings). +- **🗑 Delete** a past entry (ended, cancelled, declined, failed, blocked or skipped; owner only) to remove it together with the meetings it created, as **Delete** in Meetings does. Entries still to come keep **Skip** and **Cancel**. - **Open live panel** while hive is in the meeting and **Notes** once it ended open that meeting in the Meetings section. The schedule lives in `/schedule.json` (by default `/data/meetings/schedule.json`), rewritten atomically. Each occurrence is recorded before hive joins it, so a restart never joins the same occurrence twice; a join cut off by a restart is shown as failed rather than retried. Finished meetings stay listed for 14 days and are removed after 30. diff --git a/src/pkg/dashboard/api_meetings.go b/src/pkg/dashboard/api_meetings.go index 3e16c62fb4..1e9ba92d56 100644 --- a/src/pkg/dashboard/api_meetings.go +++ b/src/pkg/dashboard/api_meetings.go @@ -64,6 +64,11 @@ type MeetingsService interface { // (Discord `/hive join`) waiting for the owner. ApproveRequest(ctx context.Context, id string, voice bool) (any, error) DeclineRequest(ctx context.Context, id string) (any, error) + // Delete removes a finished meeting or a pending join request for good + // (ErrMeetingConflict while hive is still in it); ClearFinished deletes + // every finished meeting that ended before the cutoff. + Delete(ctx context.Context, id string) (MeetingDeleted, error) + ClearFinished(ctx context.Context, before time.Time) ([]MeetingDeleted, error) // Live is the panel snapshot: meeting, transcript, live state. Live(id string) (any, error) Subscribe(fn func(MeetingFrame)) (cancel func()) @@ -132,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.registerMeetingsDeleteRoutes() } // meetingsErrorStatus maps a MeetingsService error to its HTTP status: the diff --git a/src/pkg/dashboard/api_meetings_delete.go b/src/pkg/dashboard/api_meetings_delete.go new file mode 100644 index 0000000000..df69c6605a --- /dev/null +++ b/src/pkg/dashboard/api_meetings_delete.go @@ -0,0 +1,140 @@ +package dashboard + +import ( + "context" + "net/http" + "strconv" + "strings" + "time" +) + +// Deleting meetings (owner only): the Meetings section's 🗑 Delete and +// "Clear finished", and 🗑 Delete on past Calendar entries. A meeting in a +// final state (ended, failed, blocked, declined, left, handed off, purged) +// or a pending platform join request may be deleted; one hive is still +// joining, waiting in the lobby of or live in answers 409 (leave it +// first). Delete removes the meeting record, its transcript and live state +// on this hive, erases it in Vexa (Quorum meeting.delete, best effort), +// and drops the Calendar entry that existed only for it. Knowledge facts +// and drafts already made from the meeting are not touched. + +// MeetingDeleted is one deleted meeting. UpstreamError is set when Vexa +// could not confirm the erase; the record on this hive is gone anyway. +type MeetingDeleted struct { + ID string `json:"id"` + State string `json:"state,omitempty"` + UpstreamError string `json:"upstream_error,omitempty"` + CalendarRemoved int `json:"calendar_removed,omitempty"` +} + +// MeetingScheduleDeleted is a deleted Calendar entry and the meetings it +// created that went with it. +type MeetingScheduleDeleted struct { + ID string `json:"id"` + Meetings []MeetingDeleted `json:"meetings"` +} + +// meetingsClearRequest is POST /api/meetings/clear-finished: meetings that +// finished before `before` (RFC 3339; empty means now) are deleted. +type meetingsClearRequest struct { + Before string `json:"before,omitempty"` +} + +func (s *Server) registerMeetingsDeleteRoutes() { + s.mux.HandleFunc("DELETE /api/meetings/{id}", s.handleMeetingsDelete) + s.mux.HandleFunc("POST /api/meetings/clear-finished", s.handleMeetingsClearFinished) + s.mux.HandleFunc("DELETE /api/meetings/schedule/{eid}", s.handleMeetingsScheduleDelete) +} + +// auditMeetingDeleted records one meeting_delete. +func (s *Server) auditMeetingDeleted(r *http.Request, d MeetingDeleted, via string) { + s.auditFromRequest(r, "meeting_delete", auditDetail("meeting", d.ID, "state", d.State, "via", via, + "upstream_error", boolString(d.UpstreamError != ""), "calendar_removed", strconv.Itoa(d.CalendarRemoved)), "") +} + +func (s *Server) handleMeetingsDelete(w http.ResponseWriter, r *http.Request) { + if !requireOwnerRole(w, r) { + return + } + svc, ok := s.meetingsService(w) + if !ok { + return + } + ctx, cancel := context.WithTimeout(r.Context(), meetingsCallTimeout) + defer cancel() + d, err := svc.Delete(ctx, r.PathValue("id")) + if err != nil { + meetingsError(w, err) + return + } + s.auditMeetingDeleted(r, d, "meetings") + jsonResponse(w, d) +} + +func (s *Server) handleMeetingsClearFinished(w http.ResponseWriter, r *http.Request) { + if !requireOwnerRole(w, r) { + return + } + svc, ok := s.meetingsService(w) + if !ok { + return + } + var req meetingsClearRequest + if r.ContentLength > 0 { + if err := decodeBody(r, &req); err != nil { + jsonError(w, "invalid request body", http.StatusBadRequest) + return + } + } + before := time.Now() + if b := strings.TrimSpace(req.Before); b != "" { + t, err := time.Parse(time.RFC3339, b) + if err != nil { + jsonError(w, "before must be an RFC 3339 time", http.StatusBadRequest) + return + } + if t.Before(before) { + before = t + } + } + ctx, cancel := context.WithTimeout(r.Context(), meetingsCallTimeout) + defer cancel() + list, err := svc.ClearFinished(ctx, before) + if list == nil { + list = []MeetingDeleted{} + } + for _, d := range list { + s.auditMeetingDeleted(r, d, "clear_finished") + } + if err != nil { + meetingsError(w, err) + return + } + jsonResponse(w, map[string]any{"deleted": list, "count": len(list)}) +} + +func (s *Server) handleMeetingsScheduleDelete(w http.ResponseWriter, r *http.Request) { + if !requireOwnerRole(w, r) { + return + } + svc, ok := s.meetingsScheduleService(w) + if !ok { + return + } + ctx, cancel := context.WithTimeout(r.Context(), meetingsCallTimeout) + defer cancel() + eid := r.PathValue("eid") + d, err := svc.Delete(ctx, eid) + if err != nil { + meetingsError(w, err) + return + } + if d.Meetings == nil { + d.Meetings = []MeetingDeleted{} + } + for _, m := range d.Meetings { + s.auditMeetingDeleted(r, m, "calendar") + } + s.auditFromRequest(r, "meeting_schedule_delete", auditDetail("event", eid, "meetings", strconv.Itoa(len(d.Meetings))), "") + jsonResponse(w, d) +} diff --git a/src/pkg/dashboard/api_meetings_delete_test.go b/src/pkg/dashboard/api_meetings_delete_test.go new file mode 100644 index 0000000000..68dfdd759d --- /dev/null +++ b/src/pkg/dashboard/api_meetings_delete_test.go @@ -0,0 +1,183 @@ +package dashboard + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" +) + +func (f *fakeMeetingsService) Delete(_ context.Context, id string) (MeetingDeleted, error) { + f.mu.Lock() + defer f.mu.Unlock() + f.deleted = append(f.deleted, id) + if f.err != nil { + return MeetingDeleted{}, f.err + } + d := f.deleteRes + d.ID = id + return d, nil +} + +func (f *fakeMeetingsService) ClearFinished(_ context.Context, before time.Time) ([]MeetingDeleted, error) { + f.mu.Lock() + defer f.mu.Unlock() + f.cleared = append(f.cleared, before) + if f.err != nil { + return nil, f.err + } + return []MeetingDeleted{{ID: "mtg-a", State: "ended"}, {ID: "mtg-b", State: "blocked"}}, nil +} + +func (f *fakeScheduleService) Delete(_ context.Context, eid string) (MeetingScheduleDeleted, error) { + f.deleted = append(f.deleted, eid) + if f.err != nil { + return MeetingScheduleDeleted{}, f.err + } + d := f.deleteRes + d.ID = eid + return d, nil +} + +func meetingsDeleteAs(s *Server, method, path, body, role string, verified bool) *httptest.ResponseRecorder { + rec := httptest.NewRecorder() + req := httptest.NewRequest(method, path, strings.NewReader(body)) + if body != "" { + req.Header.Set("Content-Type", "application/json") + } + req.Header.Set("X-Hive-User", "octo") + if role != "" { + req.Header.Set("X-Hive-Role", role) + } + if verified { + req.Header.Set(ownerRoleVerifiedHeader, "true") + } + s.mux.ServeHTTP(rec, req) + return rec +} + +var meetingsDeleteRoutes = []struct{ method, path string }{ + {http.MethodDelete, "/api/meetings/mtg-1"}, + {http.MethodPost, "/api/meetings/clear-finished"}, + {http.MethodDelete, "/api/meetings/schedule/e1"}, +} + +func TestMeetingsDeleteRoleMatrix(t *testing.T) { + s, _ := apiServer(t) + for _, c := range meetingsDeleteRoutes { + if rec := meetingsDeleteAs(s, c.method, c.path, "", "owner", true); rec.Code != http.StatusServiceUnavailable { + t.Errorf("%s %s without service = %d, want 503", c.method, c.path, rec.Code) + } + } + f := &fakeMeetingsService{} + sf := &fakeScheduleService{} + s.SetMeetingsService(f) + s.SetMeetingsScheduleService(sf) + for _, c := range meetingsDeleteRoutes { + for _, who := range []struct { + role string + verified bool + }{{"read-write", true}, {"read", true}, {"", false}, {"owner", false}} { + if rec := meetingsDeleteAs(s, c.method, c.path, "", who.role, who.verified); rec.Code != http.StatusForbidden { + t.Errorf("%s %s as %q (verified=%v) = %d, want 403", c.method, c.path, who.role, who.verified, rec.Code) + } + } + } + if len(f.deleted) != 0 || len(f.cleared) != 0 || len(sf.deleted) != 0 { + t.Fatalf("a refused request reached the service: %v %v %v", f.deleted, f.cleared, sf.deleted) + } + for _, c := range meetingsDeleteRoutes { + if rec := meetingsDeleteAs(s, c.method, c.path, "", "owner", true); rec.Code != http.StatusOK { + t.Errorf("owner %s %s = %d %s", c.method, c.path, rec.Code, rec.Body.String()) + } + } + if len(f.deleted) != 1 || f.deleted[0] != "mtg-1" || len(f.cleared) != 1 || len(sf.deleted) != 1 || sf.deleted[0] != "e1" { + t.Fatalf("owner calls: %v %v %v", f.deleted, f.cleared, sf.deleted) + } +} + +func TestMeetingsDeleteStatesAndAudit(t *testing.T) { + s, _ := apiServer(t) + f := &fakeMeetingsService{deleteRes: MeetingDeleted{State: "failed", UpstreamError: "quorum meeting.delete: upstream_error: vexa 500", CalendarRemoved: 1}} + s.SetMeetingsService(f) + rec := meetingsDeleteAs(s, http.MethodDelete, "/api/meetings/mtg-1", "", "owner", true) + if rec.Code != http.StatusOK { + t.Fatalf("delete = %d %s", rec.Code, rec.Body.String()) + } + var got MeetingDeleted + if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil || got.ID != "mtg-1" || got.UpstreamError == "" || got.CalendarRemoved != 1 { + t.Fatalf("delete body = %s (%v)", rec.Body.String(), err) + } + e := s.audit.Recent(1) + if len(e) != 1 || e[0].Action != "meeting_delete" || !strings.Contains(e[0].Detail, "meeting=mtg-1") || + !strings.Contains(e[0].Detail, "upstream_error=true") || !strings.Contains(e[0].Detail, "calendar_removed=1") { + t.Fatalf("delete not audited: %+v", e) + } + // A meeting hive is still in answers 409; unknown 404. Neither is audited. + for err, want := range map[error]int{ + fmt.Errorf("%w: leave the meeting first", ErrMeetingConflict): http.StatusConflict, + fmt.Errorf("%w: mtg-x", ErrMeetingNotFound): http.StatusNotFound, + } { + f.err = err + before := len(s.audit.Recent(100)) + if rec := meetingsDeleteAs(s, http.MethodDelete, "/api/meetings/mtg-1", "", "owner", true); rec.Code != want { + t.Errorf("delete with %v = %d, want %d", err, rec.Code, want) + } + if len(s.audit.Recent(100)) != before { + t.Errorf("a refused delete (%v) was audited", err) + } + } + f.err = nil +} + +func TestMeetingsClearFinished(t *testing.T) { + s, _ := apiServer(t) + f := &fakeMeetingsService{} + s.SetMeetingsService(f) + cut := time.Now().Add(-time.Hour).UTC().Truncate(time.Second) + rec := meetingsDeleteAs(s, http.MethodPost, "/api/meetings/clear-finished", `{"before":"`+cut.Format(time.RFC3339)+`"}`, "owner", true) + if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), `"count":2`) { + t.Fatalf("clear = %d %s", rec.Code, rec.Body.String()) + } + if !f.cleared[0].Equal(cut) { + t.Fatalf("cutoff = %v, want %v", f.cleared[0], cut) + } + if e := s.audit.Recent(2); len(e) != 2 || e[0].Action != "meeting_delete" || e[1].Action != "meeting_delete" { + t.Fatalf("each cleared meeting is audited: %+v", e) + } + // A cutoff in the future is clamped to now: only meetings older than now go. + future := time.Now().Add(24 * time.Hour).UTC().Format(time.RFC3339) + if rec := meetingsDeleteAs(s, http.MethodPost, "/api/meetings/clear-finished", `{"before":"`+future+`"}`, "owner", true); rec.Code != http.StatusOK { + t.Fatalf("future clear = %d", rec.Code) + } + if f.cleared[1].After(time.Now()) { + t.Fatalf("future cutoff not clamped: %v", f.cleared[1]) + } + if rec := meetingsDeleteAs(s, http.MethodPost, "/api/meetings/clear-finished", `{"before":"yesterday"}`, "owner", true); rec.Code != http.StatusBadRequest { + t.Fatalf("bad cutoff = %d", rec.Code) + } +} + +func TestMeetingsScheduleDelete(t *testing.T) { + s, _ := apiServer(t) + sf := &fakeScheduleService{deleteRes: MeetingScheduleDeleted{Meetings: []MeetingDeleted{{ID: "mtg-1", State: "ended"}}}} + s.SetMeetingsScheduleService(sf) + rec := meetingsDeleteAs(s, http.MethodDelete, "/api/meetings/schedule/e1", "", "owner", true) + if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), `"mtg-1"`) { + t.Fatalf("calendar delete = %d %s", rec.Code, rec.Body.String()) + } + e := s.audit.Recent(2) + // Newest first: the entry, after the meeting it created. + if len(e) != 2 || e[0].Action != "meeting_schedule_delete" || !strings.Contains(e[0].Detail, "event=e1") || + e[1].Action != "meeting_delete" || !strings.Contains(e[1].Detail, "via=calendar") { + t.Fatalf("calendar delete audit: %+v", e) + } + sf.err = fmt.Errorf("%w: e1 is scheduled; skip or cancel it instead", ErrMeetingConflict) + if rec := meetingsDeleteAs(s, http.MethodDelete, "/api/meetings/schedule/e1", "", "owner", true); rec.Code != http.StatusConflict { + t.Fatalf("future entry delete = %d, want 409", rec.Code) + } +} diff --git a/src/pkg/dashboard/api_meetings_schedule.go b/src/pkg/dashboard/api_meetings_schedule.go index e81125601a..0f51c93511 100644 --- a/src/pkg/dashboard/api_meetings_schedule.go +++ b/src/pkg/dashboard/api_meetings_schedule.go @@ -61,6 +61,9 @@ type MeetingsScheduleService interface { Add(ctx context.Context, req MeetingScheduleAdd) (any, error) // Act runs approve, decline, cancel or skip on one event. Act(ctx context.Context, eid, action, by string) (any, error) + // Delete removes a finished event and the finished meetings it + // created (ErrMeetingConflict for an event still to come or live). + Delete(ctx context.Context, eid string) (MeetingScheduleDeleted, error) } type meetingsScheduleHolder struct{ svc MeetingsScheduleService } diff --git a/src/pkg/dashboard/api_meetings_schedule_test.go b/src/pkg/dashboard/api_meetings_schedule_test.go index a6b194132e..aded40f60e 100644 --- a/src/pkg/dashboard/api_meetings_schedule_test.go +++ b/src/pkg/dashboard/api_meetings_schedule_test.go @@ -15,6 +15,9 @@ type fakeScheduleService struct { added []MeetingScheduleAdd acted []string listed int + // deleted are the Delete calls; deleteRes is returned for them. + deleted []string + deleteRes MeetingScheduleDeleted } func (f *fakeScheduleService) List() (any, error) { diff --git a/src/pkg/dashboard/api_meetings_test.go b/src/pkg/dashboard/api_meetings_test.go index 9fdd94024c..c03184c71e 100644 --- a/src/pkg/dashboard/api_meetings_test.go +++ b/src/pkg/dashboard/api_meetings_test.go @@ -27,6 +27,9 @@ type fakeMeetingsService struct { invitedBy string subs map[int]func(MeetingFrame) next int + deleted []string + cleared []time.Time + deleteRes MeetingDeleted } func (f *fakeMeetingsService) Join(_ context.Context, req MeetingJoinRequest, by string) (any, error) { diff --git a/src/pkg/dashboard/meetings_delete.test.js b/src/pkg/dashboard/meetings_delete.test.js new file mode 100644 index 0000000000..f5b7fcfbe7 --- /dev/null +++ b/src/pkg/dashboard/meetings_delete.test.js @@ -0,0 +1,108 @@ +// Meetings 🗑 Delete, "Clear finished" and Calendar 🗑 Delete: who sees +// the action, which rows offer it, and the in-page confirm. +// Run: node --test src/pkg/dashboard/*.test.js +const test = require('node:test'); +const assert = require('node:assert'); +const fs = require('node:fs'); +const path = require('node:path'); + +const html = fs.readFileSync(path.join(__dirname, 'static', 'index.html'), 'utf8'); +const ASYNC_PREFIX = 'async '; +const fnSrc = name => { + let start = html.indexOf('function ' + name + '('); + assert.ok(start >= 0, name + ' not found'); + if (html.slice(start - ASYNC_PREFIX.length, start) === ASYNC_PREFIX) start -= ASYNC_PREFIX.length; + let depth = 0; + for (let i = html.indexOf('{', start); i < html.length; i++) { + if (html[i] === '{') depth++; + else if (html[i] === '}' && --depth === 0) return html.slice(start, i + 1); + } + throw new Error('unbalanced ' + name); +}; +const constSrc = name => { + const m = html.match(new RegExp('const ' + name + ' = \\{[^}]*\\};')); + assert.ok(m, name + ' not found'); + return m[0]; +}; +const escapeHtml = s => String(s).replace(/[&<>"']/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c])); + +const lib = new Function('escapeHtml', 'window', + constSrc('MEETINGS_FINAL_STATES') + '\n' + constSrc('CALENDAR_FINISHED_STATUSES') + '\n' + + ['meetingsCanDelete', 'meetingsDeleteButtonHtml', 'meetingsDeleteText', 'meetingsFinished', 'meetingsClearFinishedText', 'calendarCanDelete', 'calendarDeleteText'] + .map(fnSrc).join('\n') + + '\nreturn { meetingsCanDelete, meetingsDeleteButtonHtml, meetingsDeleteText, meetingsFinished, meetingsClearFinishedText, calendarCanDelete, calendarDeleteText };'); +const win = { _hiveRole: 'owner' }; +const m = lib(escapeHtml, win); + +test('finished meetings and pending join requests offer delete; running ones do not', () => { + for (const state of ['ended', 'failed', 'blocked', 'handed_off', 'purged']) { + assert.strictEqual(m.meetingsCanDelete({ id: 'x', state }, 'owner'), true, state); + } + assert.strictEqual(m.meetingsCanDelete({ id: 'x', state: 'requested', requested_by: { name: 'andy' } }, 'owner'), true, 'stale join request'); + for (const state of ['requested', 'joining', 'lobby', 'live']) { + assert.strictEqual(m.meetingsCanDelete({ id: 'x', state }, 'owner'), false, state); + } + for (const role of ['read-write', 'read', undefined]) { + assert.strictEqual(m.meetingsCanDelete({ id: 'x', state: 'ended' }, role), false, String(role)); + } +}); + +test('the row action is a 🗑 Delete button wired to meetingsDelete', () => { + win._hiveRole = 'owner'; + const btn = m.meetingsDeleteButtonHtml({ id: 'mtg-1', state: 'blocked' }); + assert.match(btn, /data-action="meetingsDelete"/); + assert.match(btn, /data-arg0="mtg-1"/); + assert.match(btn, /🗑 Delete/); + assert.match(btn, /class="hv-btn btn-secondary btn-sm"/); + assert.strictEqual(m.meetingsDeleteButtonHtml({ id: 'mtg-2', state: 'live' }), ''); + win._hiveRole = 'read-write'; + assert.strictEqual(m.meetingsDeleteButtonHtml({ id: 'mtg-1', state: 'ended' }), ''); + win._hiveRole = 'owner'; +}); + +test('the rows render the delete action', () => { + const row = fnSrc('meetingsRowHtml'); + assert.strictEqual((row.match(/meetingsDeleteButtonHtml\(m\)/g) || []).length, 2, 'join-request rows and meeting rows'); +}); + +test('the confirm says knowledge facts and drafts are not touched', () => { + assert.match(m.meetingsDeleteText({ state: 'ended' }), /Knowledge facts and drafts already made from it are not touched/); + assert.match(m.meetingsDeleteText({ state: 'requested' }), /declines the join request/); + assert.match(m.meetingsClearFinishedText(3), /^This deletes 3 finished meetings/); + assert.match(m.meetingsClearFinishedText(1), /not touched/); + assert.match(m.calendarDeleteText({ status: 'ended', history: [{ meeting_id: 'mtg-1' }] }), /and the meeting it created/); +}); + +test('Clear finished takes final-state meetings older than now only', () => { + const now = new Date('2026-11-16T12:00:00Z'); + const list = [ + { id: 'a', state: 'ended', ended_at: '2026-11-16T10:00:00Z' }, + { id: 'b', state: 'failed', updated_at: '2026-11-15T10:00:00Z' }, + { id: 'c', state: 'ended', ended_at: '2026-11-16T13:00:00Z' }, + { id: 'd', state: 'live', created_at: '2026-11-16T09:00:00Z' }, + { id: 'e', state: 'requested', requested_by: { name: 'andy' }, created_at: '2026-11-10T09:00:00Z' }, + ]; + assert.deepStrictEqual(m.meetingsFinished(list, now).map(x => x.id), ['a', 'b']); + assert.deepStrictEqual(m.meetingsFinished(null, now), []); +}); + +test('Calendar: past entries delete, scheduled ones keep Skip/Cancel', () => { + for (const status of ['ended', 'cancelled', 'declined', 'failed', 'blocked', 'skipped']) { + assert.strictEqual(m.calendarCanDelete({ status }, 'owner'), true, status); + } + for (const status of ['scheduled', 'awaiting_approval', 'live']) { + assert.strictEqual(m.calendarCanDelete({ status }, 'owner'), false, status); + } + assert.strictEqual(m.calendarCanDelete({ status: 'ended' }, 'read-write'), false); +}); + +test('deletes use the in-page confirm and the DELETE endpoints', () => { + for (const name of ['meetingsDelete', 'meetingsClearFinished', 'calendarDelete']) { + const src = fnSrc(name); + assert.match(src, /hiveConfirm\(/, name); + assert.doesNotMatch(src, /window\.confirm|\balert\(|[^.]confirm\(/, name); + } + assert.match(fnSrc('meetingsDelete'), /'\/api\/meetings\/' \+ encodeURIComponent\(id\), \{ method: 'DELETE' \}/); + assert.match(fnSrc('meetingsClearFinished'), /'\/api\/meetings\/clear-finished'/); + assert.match(fnSrc('calendarDelete'), /'\/api\/meetings\/schedule\/' \+ encodeURIComponent\(id\), \{ method: 'DELETE' \}/); +}); diff --git a/src/pkg/dashboard/static-src/js/calendar.js b/src/pkg/dashboard/static-src/js/calendar.js index 1ebcc9889d..f9218c81b2 100644 --- a/src/pkg/dashboard/static-src/js/calendar.js +++ b/src/pkg/dashboard/static-src/js/calendar.js @@ -181,9 +181,45 @@ if (e.status === 'awaiting_approval') out += btn('approve', 'Approve', 'btn-primary') + btn('decline', 'Decline', 'btn-secondary'); if (e.status === 'scheduled' || e.status === 'awaiting_approval' || (e.status === 'live' && e.rrule)) out += btn('skip', e.rrule ? 'Skip next' : 'Skip', 'btn-secondary'); if (e.status === 'scheduled' || e.status === 'awaiting_approval' || e.status === 'live') out += btn('cancel', 'Cancel', 'btn-danger'); + if (calendarCanDelete(e, window._hiveRole)) { + out += ''; + } return out; } + // Finished Calendar statuses: past entries get 🗑 Delete; scheduled, + // awaiting-approval and live ones keep Skip and Cancel. + const CALENDAR_FINISHED_STATUSES = { ended: 1, cancelled: 1, declined: 1, failed: 1, blocked: 1, skipped: 1 }; + function calendarCanDelete(e, role) { + return !!e && role === 'owner' && !!CALENDAR_FINISHED_STATUSES[e.status]; + } + + function calendarDeleteText(e) { + const n = ((e && e.history) || []).filter(h => h && h.meeting_id).length || (e && e.meeting_id ? 1 : 0); + return 'This deletes this Calendar entry' + (n ? ' and the meeting' + (n === 1 ? '' : 's') + ' it created: record, transcript, notes and live state on this hive, and the data in Vexa' : '') + '. ' + + 'Knowledge facts and drafts already made from it are not touched. It cannot be undone.'; + } + + async function calendarDelete(id) { + const e = ((_calendarData && _calendarData.recent) || []).concat((_calendarData && _calendarData.upcoming) || []).find(x => x && x.id === id); + if (!e) return; + const ok = await hiveConfirm({ title: 'Delete this Calendar entry?', message: calendarDeleteText(e), confirmLabel: 'Delete', danger: true }); + if (!ok) return; + try { + const r = await fetch('/api/meetings/schedule/' + encodeURIComponent(id), { method: 'DELETE' }); + const data = await r.json().catch(() => ({})); + if (!r.ok) throw new Error(data.error || ('HTTP ' + r.status)); + const upstream = (data.meetings || []).filter(m => m && m.upstream_error).length; + showToast(upstream ? 'Entry deleted; Vexa did not confirm the meeting data was erased' : 'Calendar entry deleted', upstream ? 'warning' : 'success'); + if (typeof fetchMeetings === 'function') fetchMeetings(); + calendarRefreshMeetings(); + } catch (err) { + showToast('Could not delete the entry: ' + err.message, 'error'); + } + fetchCalendar(); + } + function calendarMeetingLink(e) { const last = (e.history || []).length ? e.history[e.history.length - 1] : null; const mid = e.meeting_id || (last && last.meeting_id) || ''; diff --git a/src/pkg/dashboard/static-src/js/meetings.js b/src/pkg/dashboard/static-src/js/meetings.js index 29daff9442..b3afea78ac 100644 --- a/src/pkg/dashboard/static-src/js/meetings.js +++ b/src/pkg/dashboard/static-src/js/meetings.js @@ -20,6 +20,10 @@ // Meeting state to the kc-pill status palette (green, blue, red, muted). const MEETINGS_PILL_STATUS = { live: 'ok', requested: 'syncing', joining: 'syncing', lobby: 'syncing', failed: 'error', blocked: 'error' }; + // Final meeting states: the row offers 🗑 Delete and "Clear finished" + // takes it. Joining, lobby and live meetings are left first. + const MEETINGS_FINAL_STATES = { ended: 1, failed: 1, blocked: 1, handed_off: 1, purged: 1 }; + function meetingsIsOwner() { return window._hiveRole === 'owner'; } // The role is known once /api/role settled (answered, or its retries gave // up). Until then the panel shows a neutral loading state, never the @@ -123,7 +127,8 @@ 'join request' + meetingsReasonHtml(m) + '' + '
' + '' + - '
'; + '' + + meetingsDeleteButtonHtml(m) + ''; } const state = m.state || ''; const when = meetingsFormatTime(m.live_at || m.created_at); @@ -141,7 +146,88 @@ '' + (active ? '' + '' : '') + - ''; + meetingsDeleteButtonHtml(m) + ''; + } + + // meetingsCanDelete: a meeting in a final state, or a platform join + // request still waiting (deleting it declines it). Owner only. + function meetingsCanDelete(m, role) { + if (!m || role !== 'owner') return false; + if (MEETINGS_FINAL_STATES[m.state]) return true; + return m.state === 'requested' && !!m.requested_by; + } + + function meetingsDeleteButtonHtml(m) { + if (!meetingsCanDelete(m, window._hiveRole)) return ''; + return ''; + } + + // meetingsDeleteText is the in-page confirm text for one meeting. + function meetingsDeleteText(m) { + if (m && m.state === 'requested') { + return 'This declines the join request and removes it. It cannot be undone.'; + } + return 'This deletes the meeting record, its transcript, notes and live state on this hive, its data in Vexa, and its Calendar entry if it has no other occurrences. ' + + 'Knowledge facts and drafts already made from it are not touched. It cannot be undone.'; + } + + // meetingsFinished lists the meetings "Clear finished" deletes: final + // states that finished before now. + function meetingsFinished(list, now) { + const cut = now instanceof Date ? now.getTime() : Date.now(); + return (list || []).filter(m => { + if (!m || !MEETINGS_FINAL_STATES[m.state]) return false; + const t = new Date(m.ended_at || m.updated_at || m.created_at || 0).getTime(); + return !isNaN(t) && t < cut; + }); + } + + function meetingsClearFinishedText(n) { + return 'This deletes ' + n + ' finished meeting' + (n === 1 ? '' : 's') + ' (ended, failed, blocked or declined): their records, transcripts, notes and live state on this hive, their data in Vexa, and Calendar entries that existed only for them. ' + + 'Live meetings and pending join requests stay. Knowledge facts and drafts already made from them are not touched. It cannot be undone.'; + } + + function meetingsAfterDelete(ids) { + if (_meetingsOpen && ids.indexOf(_meetingsOpen.id) >= 0) meetingsClosePanel(); + if (typeof calendarRefreshMeetings === 'function') calendarRefreshMeetings(); + if (typeof fetchCalendar === 'function') fetchCalendar(); + } + + async function meetingsDelete(id) { + const m = (_meetingsList || []).find(x => x && x.id === id); + if (!m) return; + const ok = await hiveConfirm({ title: m.state === 'requested' ? 'Delete this join request?' : 'Delete this meeting?', message: meetingsDeleteText(m), confirmLabel: 'Delete', danger: true }); + if (!ok) return; + try { + const r = await fetch('/api/meetings/' + encodeURIComponent(id), { method: 'DELETE' }); + const data = await r.json().catch(() => ({})); + if (!r.ok) throw new Error(data.error || ('HTTP ' + r.status)); + if (data.upstream_error) showToast('Meeting deleted here; Vexa did not confirm its copy was erased: ' + data.upstream_error, 'warning'); + else showToast('Meeting deleted', 'success'); + meetingsAfterDelete([id]); + } catch (e) { + showToast('Could not delete the meeting: ' + e.message, 'error'); + } + await fetchMeetings(); + } + + async function meetingsClearFinished() { + const now = new Date(); + const finished = meetingsFinished(_meetingsList, now); + if (!finished.length) return; + const ok = await hiveConfirm({ title: 'Clear finished meetings?', message: meetingsClearFinishedText(finished.length), confirmLabel: 'Delete ' + finished.length, danger: true }); + if (!ok) return; + try { + const data = await meetingsPost('/api/meetings/clear-finished', { before: now.toISOString() }); + const deleted = data.deleted || []; + const upstream = deleted.filter(d => d && d.upstream_error).length; + showToast('Deleted ' + deleted.length + ' meeting' + (deleted.length === 1 ? '' : 's') + (upstream ? '; Vexa did not confirm ' + upstream + ' erase' + (upstream === 1 ? '' : 's') : ''), upstream ? 'warning' : 'success'); + meetingsAfterDelete(deleted.map(d => d.id)); + } catch (e) { + showToast('Could not clear finished meetings: ' + e.message, 'error'); + } + await fetchMeetings(); } function renderMeetings() { @@ -154,6 +240,11 @@ html += meetingsJoinFormHtml(); } const list = _meetingsList || []; + const finished = meetingsFinished(list); + if (finished.length && meetingsIsOwner()) { + html += '
'; + } html += '
' + (list.length ? list.map(meetingsRowHtml).join('') : (_meetingsError ? '' : '

No meetings yet.

')) + '
'; html += '
'; host.innerHTML = html; diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index 9c3e2ce72b..b3b6b5e1e3 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -50138,6 +50138,10 @@ // Meeting state to the kc-pill status palette (green, blue, red, muted). const MEETINGS_PILL_STATUS = { live: 'ok', requested: 'syncing', joining: 'syncing', lobby: 'syncing', failed: 'error', blocked: 'error' }; + // Final meeting states: the row offers 🗑 Delete and "Clear finished" + // takes it. Joining, lobby and live meetings are left first. + const MEETINGS_FINAL_STATES = { ended: 1, failed: 1, blocked: 1, handed_off: 1, purged: 1 }; + function meetingsIsOwner() { return window._hiveRole === 'owner'; } // The role is known once /api/role settled (answered, or its retries gave // up). Until then the panel shows a neutral loading state, never the @@ -50241,7 +50245,8 @@ 'join request' + meetingsReasonHtml(m) + '' + '
' + '' + - '
'; + '' + + meetingsDeleteButtonHtml(m) + ''; } const state = m.state || ''; const when = meetingsFormatTime(m.live_at || m.created_at); @@ -50259,7 +50264,88 @@ '' + (active ? '' + '' : '') + - ''; + meetingsDeleteButtonHtml(m) + ''; + } + + // meetingsCanDelete: a meeting in a final state, or a platform join + // request still waiting (deleting it declines it). Owner only. + function meetingsCanDelete(m, role) { + if (!m || role !== 'owner') return false; + if (MEETINGS_FINAL_STATES[m.state]) return true; + return m.state === 'requested' && !!m.requested_by; + } + + function meetingsDeleteButtonHtml(m) { + if (!meetingsCanDelete(m, window._hiveRole)) return ''; + return ''; + } + + // meetingsDeleteText is the in-page confirm text for one meeting. + function meetingsDeleteText(m) { + if (m && m.state === 'requested') { + return 'This declines the join request and removes it. It cannot be undone.'; + } + return 'This deletes the meeting record, its transcript, notes and live state on this hive, its data in Vexa, and its Calendar entry if it has no other occurrences. ' + + 'Knowledge facts and drafts already made from it are not touched. It cannot be undone.'; + } + + // meetingsFinished lists the meetings "Clear finished" deletes: final + // states that finished before now. + function meetingsFinished(list, now) { + const cut = now instanceof Date ? now.getTime() : Date.now(); + return (list || []).filter(m => { + if (!m || !MEETINGS_FINAL_STATES[m.state]) return false; + const t = new Date(m.ended_at || m.updated_at || m.created_at || 0).getTime(); + return !isNaN(t) && t < cut; + }); + } + + function meetingsClearFinishedText(n) { + return 'This deletes ' + n + ' finished meeting' + (n === 1 ? '' : 's') + ' (ended, failed, blocked or declined): their records, transcripts, notes and live state on this hive, their data in Vexa, and Calendar entries that existed only for them. ' + + 'Live meetings and pending join requests stay. Knowledge facts and drafts already made from them are not touched. It cannot be undone.'; + } + + function meetingsAfterDelete(ids) { + if (_meetingsOpen && ids.indexOf(_meetingsOpen.id) >= 0) meetingsClosePanel(); + if (typeof calendarRefreshMeetings === 'function') calendarRefreshMeetings(); + if (typeof fetchCalendar === 'function') fetchCalendar(); + } + + async function meetingsDelete(id) { + const m = (_meetingsList || []).find(x => x && x.id === id); + if (!m) return; + const ok = await hiveConfirm({ title: m.state === 'requested' ? 'Delete this join request?' : 'Delete this meeting?', message: meetingsDeleteText(m), confirmLabel: 'Delete', danger: true }); + if (!ok) return; + try { + const r = await fetch('/api/meetings/' + encodeURIComponent(id), { method: 'DELETE' }); + const data = await r.json().catch(() => ({})); + if (!r.ok) throw new Error(data.error || ('HTTP ' + r.status)); + if (data.upstream_error) showToast('Meeting deleted here; Vexa did not confirm its copy was erased: ' + data.upstream_error, 'warning'); + else showToast('Meeting deleted', 'success'); + meetingsAfterDelete([id]); + } catch (e) { + showToast('Could not delete the meeting: ' + e.message, 'error'); + } + await fetchMeetings(); + } + + async function meetingsClearFinished() { + const now = new Date(); + const finished = meetingsFinished(_meetingsList, now); + if (!finished.length) return; + const ok = await hiveConfirm({ title: 'Clear finished meetings?', message: meetingsClearFinishedText(finished.length), confirmLabel: 'Delete ' + finished.length, danger: true }); + if (!ok) return; + try { + const data = await meetingsPost('/api/meetings/clear-finished', { before: now.toISOString() }); + const deleted = data.deleted || []; + const upstream = deleted.filter(d => d && d.upstream_error).length; + showToast('Deleted ' + deleted.length + ' meeting' + (deleted.length === 1 ? '' : 's') + (upstream ? '; Vexa did not confirm ' + upstream + ' erase' + (upstream === 1 ? '' : 's') : ''), upstream ? 'warning' : 'success'); + meetingsAfterDelete(deleted.map(d => d.id)); + } catch (e) { + showToast('Could not clear finished meetings: ' + e.message, 'error'); + } + await fetchMeetings(); } function renderMeetings() { @@ -50272,6 +50358,11 @@ html += meetingsJoinFormHtml(); } const list = _meetingsList || []; + const finished = meetingsFinished(list); + if (finished.length && meetingsIsOwner()) { + html += '
'; + } html += '
' + (list.length ? list.map(meetingsRowHtml).join('') : (_meetingsError ? '' : '

No meetings yet.

')) + '
'; html += '
'; host.innerHTML = html; @@ -50677,9 +50768,45 @@ if (e.status === 'awaiting_approval') out += btn('approve', 'Approve', 'btn-primary') + btn('decline', 'Decline', 'btn-secondary'); if (e.status === 'scheduled' || e.status === 'awaiting_approval' || (e.status === 'live' && e.rrule)) out += btn('skip', e.rrule ? 'Skip next' : 'Skip', 'btn-secondary'); if (e.status === 'scheduled' || e.status === 'awaiting_approval' || e.status === 'live') out += btn('cancel', 'Cancel', 'btn-danger'); + if (calendarCanDelete(e, window._hiveRole)) { + out += ''; + } return out; } + // Finished Calendar statuses: past entries get 🗑 Delete; scheduled, + // awaiting-approval and live ones keep Skip and Cancel. + const CALENDAR_FINISHED_STATUSES = { ended: 1, cancelled: 1, declined: 1, failed: 1, blocked: 1, skipped: 1 }; + function calendarCanDelete(e, role) { + return !!e && role === 'owner' && !!CALENDAR_FINISHED_STATUSES[e.status]; + } + + function calendarDeleteText(e) { + const n = ((e && e.history) || []).filter(h => h && h.meeting_id).length || (e && e.meeting_id ? 1 : 0); + return 'This deletes this Calendar entry' + (n ? ' and the meeting' + (n === 1 ? '' : 's') + ' it created: record, transcript, notes and live state on this hive, and the data in Vexa' : '') + '. ' + + 'Knowledge facts and drafts already made from it are not touched. It cannot be undone.'; + } + + async function calendarDelete(id) { + const e = ((_calendarData && _calendarData.recent) || []).concat((_calendarData && _calendarData.upcoming) || []).find(x => x && x.id === id); + if (!e) return; + const ok = await hiveConfirm({ title: 'Delete this Calendar entry?', message: calendarDeleteText(e), confirmLabel: 'Delete', danger: true }); + if (!ok) return; + try { + const r = await fetch('/api/meetings/schedule/' + encodeURIComponent(id), { method: 'DELETE' }); + const data = await r.json().catch(() => ({})); + if (!r.ok) throw new Error(data.error || ('HTTP ' + r.status)); + const upstream = (data.meetings || []).filter(m => m && m.upstream_error).length; + showToast(upstream ? 'Entry deleted; Vexa did not confirm the meeting data was erased' : 'Calendar entry deleted', upstream ? 'warning' : 'success'); + if (typeof fetchMeetings === 'function') fetchMeetings(); + calendarRefreshMeetings(); + } catch (err) { + showToast('Could not delete the entry: ' + err.message, 'error'); + } + fetchCalendar(); + } + function calendarMeetingLink(e) { const last = (e.history || []).length ? e.history[e.history.length - 1] : null; const mid = e.meeting_id || (last && last.meeting_id) || ''; diff --git a/src/pkg/meetings/delete.go b/src/pkg/meetings/delete.go new file mode 100644 index 0000000000..c8e7ae0359 --- /dev/null +++ b/src/pkg/meetings/delete.go @@ -0,0 +1,119 @@ +package meetings + +import ( + "context" + "errors" + "time" +) + +// Deleting meetings (the dashboard's 🗑 Delete and "Clear finished"): the +// owner removes a finished meeting or a stale platform join request for +// good. Hive drops the record, its transcript and its side documents (the +// live loop's state and notes), and erases the meeting upstream with +// meeting.delete so Vexa forgets it too. Knowledge facts and drafts already +// made from the meeting live elsewhere and are not touched. + +// ErrStillRunning is returned when deleting a meeting hive is still in +// (joining, lobby or live): leave it first. +var ErrStillRunning = errors.New("meetings: the meeting is still running; leave the meeting first") + +// DeleteResult is a deleted meeting. UpstreamError is set when the Quorum +// call failed with anything but not_found: the record is removed anyway, +// and the error says what Vexa may still hold. +type DeleteResult struct { + Meeting Meeting `json:"meeting"` + UpstreamError string `json:"upstream_error,omitempty"` +} + +// Deletable reports whether the owner may delete mt: a meeting in a final +// state, or a platform join request still waiting (deleting it declines +// it). +func Deletable(mt Meeting) bool { + return mt.State.Terminal() || isPendingRequest(mt) +} + +// Finished reports whether mt is in a final state ("Clear finished"): +// pending join requests are not finished. +func Finished(mt Meeting) bool { return mt.State.Terminal() } + +func isPendingRequest(mt Meeting) bool { + return mt.State == StateRequested && mt.RequestedBy != nil +} + +// upstreamErased reports whether the meeting was already erased upstream: +// a hand-off deletes it, and the retention sweeper purges only after it +// did. +func upstreamErased(st State) bool { return st == StateHandedOff || st == StatePurged } + +// Delete removes one meeting for good. A pending join request is declined +// first (meeting.leave). Quorum errors are best effort: not_found means +// Vexa already purged it; any other error is reported in the result while +// the record is still removed. +func (m *Manager) Delete(ctx context.Context, id string) (DeleteResult, error) { + mt, err := m.opts.Store.Get(id) + if err != nil { + return DeleteResult{}, err + } + if !Deletable(mt) { + return DeleteResult{Meeting: mt}, ErrStillRunning + } + res := DeleteResult{Meeting: mt} + upstream := func(err error) { + if err != nil && !IsCode(err, CodeNotFound) && res.UpstreamError == "" { + res.UpstreamError = err.Error() + } + } + if mt.NativeID != "" { + if isPendingRequest(mt) { + _, lerr := m.opts.Quorum.Leave(ctx, mt.NativeID) + upstream(lerr) + } + if !upstreamErased(mt.State) { + upstream(m.opts.Quorum.Delete(ctx, mt.NativeID)) + } + } + m.stopWatch(id) + m.clearLobby(id) + m.mu.Lock() + delete(m.consentAt, id) + m.mu.Unlock() + if err := m.opts.Store.Remove(id); err != nil { + return res, err + } + m.log.Info("[meetings] meeting deleted", "meeting", id, "state", string(mt.State), "upstream_error", res.UpstreamError) + return res, nil +} + +// finishedAt is when a meeting stopped: its end, else its last change. +func finishedAt(mt Meeting) time.Time { + if mt.EndedAt != nil { + return *mt.EndedAt + } + return mt.UpdatedAt +} + +// ClearFinished deletes every meeting in a final state that finished +// before cutoff, one Delete each. It returns the deleted meetings; a +// meeting that could not be removed is skipped (logged). +func (m *Manager) ClearFinished(ctx context.Context, cutoff time.Time) ([]DeleteResult, error) { + list, err := m.opts.Store.List() + if err != nil { + return nil, err + } + var out []DeleteResult + for _, mt := range list { + if ctx.Err() != nil { + return out, ctx.Err() + } + if !Finished(mt) || !finishedAt(mt).Before(cutoff) { + continue + } + res, err := m.Delete(ctx, mt.ID) + if err != nil { + m.log.Warn("[meetings] clearing a finished meeting failed", "meeting", mt.ID, "error", err) + continue + } + out = append(out, res) + } + return out, nil +} diff --git a/src/pkg/meetings/delete_test.go b/src/pkg/meetings/delete_test.go new file mode 100644 index 0000000000..06ad916fe8 --- /dev/null +++ b/src/pkg/meetings/delete_test.go @@ -0,0 +1,138 @@ +package meetings + +import ( + "context" + "errors" + "os" + "path/filepath" + "testing" + "time" +) + +func TestDeleteStateMatrix(t *testing.T) { + now := time.Now() + cases := []struct { + name string + mt Meeting + ok bool + erase bool // meeting.delete called + }{ + {"ended", Meeting{State: StateEnded, EndedAt: &now}, true, true}, + {"failed", Meeting{State: StateFailed, StateReason: ReasonDeclined, EndedAt: &now}, true, true}, + {"blocked", Meeting{State: StateBlocked, EndedAt: &now}, true, true}, + {"handed off", Meeting{State: StateHandedOff, EndedAt: &now}, true, false}, + {"purged", Meeting{State: StatePurged, EndedAt: &now}, true, false}, + {"pending request", Meeting{State: StateRequested, RequestedBy: &Person{Name: "andy"}}, true, true}, + {"dashboard join in flight", Meeting{State: StateRequested}, false, false}, + {"joining", Meeting{State: StateJoining}, false, false}, + {"lobby", Meeting{State: StateLobby}, false, false}, + {"live", Meeting{State: StateLive}, false, false}, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + q := newFakeQuorum() + m := newTestManager(t, q, 0) + st := m.opts.Store + mt := c.mt + mt.ID, mt.NativeID, mt.CreatedAt = "mtg-x", "q-x", now + if err := st.Save(mt); err != nil { + t.Fatal(err) + } + if err := st.AppendTranscript(mt.ID, Event{Type: EventChat, Text: "hello"}); err != nil { + t.Fatal(err) + } + if err := st.SaveDoc(mt.ID, "state.json", map[string]string{"k": "v"}); err != nil { + t.Fatal(err) + } + res, err := m.Delete(context.Background(), mt.ID) + if !c.ok { + if !errors.Is(err, ErrStillRunning) { + t.Fatalf("Delete = %v, want ErrStillRunning", err) + } + if _, gerr := m.Get(mt.ID); gerr != nil { + t.Fatalf("a running meeting was removed: %v", gerr) + } + if len(q.deletes) != 0 || len(q.leaves) != 0 { + t.Fatalf("quorum called for a running meeting: deletes %v leaves %v", q.deletes, q.leaves) + } + return + } + if err != nil || res.UpstreamError != "" || res.Meeting.ID != mt.ID { + t.Fatalf("Delete = %+v, %v", res, err) + } + if _, err := m.Get(mt.ID); !errors.Is(err, ErrNotFound) { + t.Fatalf("record still there: %v", err) + } + if _, err := os.Stat(filepath.Join(st.Dir(), mt.ID)); !errors.Is(err, os.ErrNotExist) { + t.Fatalf("meeting dir (transcript, live state) still there: %v", err) + } + if erased := len(q.deletes) == 1 && q.deletes[0] == "q-x"; erased != c.erase { + t.Fatalf("meeting.delete calls %v, want called=%v", q.deletes, c.erase) + } + if pending := isPendingRequest(c.mt); pending != (len(q.leaves) == 1) { + t.Fatalf("leaves %v for pending=%v", q.leaves, pending) + } + }) + } +} + +func TestDeleteUpstreamErrors(t *testing.T) { + now := time.Now() + q := newFakeQuorum() + m := newTestManager(t, q, 0) + st := m.opts.Store + if _, err := m.Delete(context.Background(), "mtg-none"); !errors.Is(err, ErrNotFound) { + t.Fatalf("missing = %v", err) + } + // Vexa already purged it: not an error. + q.deleteErr = &ToolError{Tool: "meeting.delete", Code: CodeNotFound} + _ = st.Save(Meeting{ID: "mtg-gone", NativeID: "q-gone", State: StateEnded, EndedAt: &now, CreatedAt: now}) + if res, err := m.Delete(context.Background(), "mtg-gone"); err != nil || res.UpstreamError != "" { + t.Fatalf("not_found = %+v, %v", res, err) + } + // Any other Quorum error is surfaced, and the record is removed anyway. + q.deleteErr = &ToolError{Tool: "meeting.delete", Code: CodeUpstreamError, Message: "vexa 500"} + _ = st.Save(Meeting{ID: "mtg-err", NativeID: "q-err", State: StateFailed, EndedAt: &now, CreatedAt: now}) + res, err := m.Delete(context.Background(), "mtg-err") + if err != nil || res.UpstreamError == "" { + t.Fatalf("upstream error = %+v, %v", res, err) + } + if _, err := m.Get("mtg-err"); !errors.Is(err, ErrNotFound) { + t.Fatalf("record kept after an upstream error: %v", err) + } + // A meeting Quorum never knew (no native id) is removed without a call. + q.deleteErr = nil + before := len(q.deletes) + _ = st.Save(Meeting{ID: "mtg-local", State: StateFailed, EndedAt: &now, CreatedAt: now}) + if _, err := m.Delete(context.Background(), "mtg-local"); err != nil || len(q.deletes) != before { + t.Fatalf("local = %v, deletes %v", err, q.deletes) + } +} + +func TestClearFinished(t *testing.T) { + now := time.Now() + old := now.Add(-time.Hour) + later := now.Add(time.Hour) + q := newFakeQuorum() + m := newTestManager(t, q, 0) + st := m.opts.Store + _ = st.Save(Meeting{ID: "mtg-ended", NativeID: "q1", State: StateEnded, EndedAt: &old, CreatedAt: old}) + _ = st.Save(Meeting{ID: "mtg-blocked", NativeID: "q2", State: StateBlocked, EndedAt: &old, CreatedAt: old}) + _ = st.Save(Meeting{ID: "mtg-after", NativeID: "q3", State: StateEnded, EndedAt: &later, CreatedAt: old}) + _ = st.Save(Meeting{ID: "mtg-live", NativeID: "q4", State: StateLive, CreatedAt: old}) + _ = st.Save(Meeting{ID: "mtg-req", NativeID: "q5", State: StateRequested, RequestedBy: &Person{Name: "andy"}, CreatedAt: old}) + got, err := m.ClearFinished(context.Background(), now) + if err != nil || len(got) != 2 { + t.Fatalf("ClearFinished = %+v, %v", got, err) + } + for _, id := range []string{"mtg-ended", "mtg-blocked"} { + if _, err := m.Get(id); !errors.Is(err, ErrNotFound) { + t.Errorf("%s not cleared", id) + } + } + for _, id := range []string{"mtg-after", "mtg-live", "mtg-req"} { + if _, err := m.Get(id); err != nil { + t.Errorf("%s cleared: %v", id, err) + } + } +} diff --git a/src/pkg/meetings/schedule/delete.go b/src/pkg/meetings/schedule/delete.go new file mode 100644 index 0000000000..460a198ee7 --- /dev/null +++ b/src/pkg/meetings/schedule/delete.go @@ -0,0 +1,98 @@ +package schedule + +import ( + "errors" + "fmt" +) + +// Deleting Calendar entries (the dashboard's 🗑 Delete on past entries): +// only a finished event may be deleted; one still scheduled, awaiting +// approval or live keeps Skip and Cancel. Deleting a meeting from the +// Meetings section forgets it here too (ForgetMeeting). + +// LinkedMeetings returns the ids of the meetings an event created: the +// current one and those in its history, each once. +func LinkedMeetings(e Event) []string { + seen := map[string]bool{} + var out []string + add := func(id string) { + if id != "" && !seen[id] { + seen[id] = true + out = append(out, id) + } + } + add(e.MeetingID) + for _, h := range e.History { + add(h.MeetingID) + } + return out +} + +// Delete removes a finished event (ended, cancelled, declined, failed, +// blocked, skipped). Anything else is ErrConflict: skip or cancel it +// instead. +func (s *Scheduler) Delete(id string) (Event, error) { + return s.opts.Store.Remove(id, func(e Event) error { + if !e.Status.Terminal() { + return fmt.Errorf("%w: %s is %s; skip or cancel it instead", ErrConflict, id, e.Status) + } + return nil + }) +} + +// ForgetMeeting drops a deleted meeting from the Calendar: its history +// entries go, and a finished event that existed only for that meeting is +// removed. A series or an event with other occurrences stays. It returns +// how many events were removed. +func (s *Scheduler) ForgetMeeting(meetingID string) int { + if meetingID == "" { + return 0 + } + removed := 0 + for _, e := range s.opts.Store.List() { + linked := false + for _, id := range LinkedMeetings(e) { + if id == meetingID { + linked = true + } + } + if !linked { + continue + } + next, err := s.opts.Store.Update(e.ID, func(e *Event) error { + kept := e.History[:0:0] + for _, h := range e.History { + if h.MeetingID != meetingID { + kept = append(kept, h) + } + } + e.History = kept + // A live event keeps its link: reconcile records the missing + // meeting as ended on the next tick. + if e.MeetingID == meetingID && e.Status != StatusLive { + e.MeetingID = "" + } + return nil + }) + if err != nil { + s.log.Warn("[schedule] forgetting a deleted meeting failed", "event", e.ID, "meeting", meetingID, "error", err) + continue + } + if !next.Status.Terminal() || next.Recurring() || len(LinkedMeetings(next)) > 0 { + continue + } + if _, err := s.opts.Store.Remove(next.ID, func(cur Event) error { + if !cur.Status.Terminal() || len(LinkedMeetings(cur)) > 0 { + return errUnchanged + } + return nil + }); err != nil { + if !errors.Is(err, errUnchanged) { + s.log.Warn("[schedule] removing a deleted meeting's event failed", "event", next.ID, "error", err) + } + continue + } + removed++ + } + return removed +} diff --git a/src/pkg/meetings/schedule/delete_test.go b/src/pkg/meetings/schedule/delete_test.go new file mode 100644 index 0000000000..5d4e36f727 --- /dev/null +++ b/src/pkg/meetings/schedule/delete_test.go @@ -0,0 +1,74 @@ +package schedule + +import ( + "errors" + "testing" +) + +// putEvent stores ev as is (status, history and links included). +func putEvent(t *testing.T, s *Scheduler, ev Event) Event { + t.Helper() + ev.ID = EventID(ev.UID, ev.RecurrenceID) + got, err := s.opts.Store.Upsert(ev, func(_ *Event, in Event) Event { return in }) + if err != nil { + t.Fatal(err) + } + return got +} + +func TestDeleteOnlyFinishedEvents(t *testing.T) { + c := &clock{t: t0} + s := newTestScheduler(t, t.TempDir(), c, &fakeJoiner{}) + for _, st := range []Status{StatusScheduled, StatusAwaitingApproval, StatusLive} { + e := putEvent(t, s, Event{UID: "u-" + string(st), Start: t0, Status: st}) + if _, err := s.Delete(e.ID); !errors.Is(err, ErrConflict) { + t.Errorf("Delete %s = %v, want ErrConflict", st, err) + } + if _, err := s.Get(e.ID); err != nil { + t.Errorf("%s event removed: %v", st, err) + } + } + for _, st := range []Status{StatusEnded, StatusCancelled, StatusDeclined, StatusFailed, StatusBlocked, StatusSkipped} { + e := putEvent(t, s, Event{UID: "u-" + string(st), Start: t0, Status: st, MeetingID: "mtg-" + string(st)}) + got, err := s.Delete(e.ID) + if err != nil || got.MeetingID != "mtg-"+string(st) { + t.Errorf("Delete %s = %+v, %v", st, got, err) + } + if _, err := s.Get(e.ID); !errors.Is(err, ErrNotFound) { + t.Errorf("%s event kept: %v", st, err) + } + } + if _, err := s.Delete("nope"); !errors.Is(err, ErrNotFound) { + t.Errorf("unknown = %v", err) + } +} + +func TestForgetMeeting(t *testing.T) { + c := &clock{t: t0} + s := newTestScheduler(t, t.TempDir(), c, &fakeJoiner{}) + single := putEvent(t, s, Event{UID: "single", Start: t0, Status: StatusFailed, MeetingID: "mtg-1", + History: []Occurrence{{Start: t0, MeetingID: "mtg-1", Status: StatusFailed}}}) + series := putEvent(t, s, Event{UID: "series", Start: t0, Status: StatusScheduled, RRule: "FREQ=WEEKLY", MeetingID: "mtg-3", + History: []Occurrence{{Start: t0, MeetingID: "mtg-2", Status: StatusEnded}, {Start: t0.AddDate(0, 0, 7), MeetingID: "mtg-3", Status: StatusEnded}}}) + other := putEvent(t, s, Event{UID: "other", Start: t0, Status: StatusEnded, MeetingID: "mtg-9"}) + + if n := s.ForgetMeeting("mtg-1"); n != 1 { + t.Fatalf("ForgetMeeting single = %d, want 1 removed", n) + } + if _, err := s.Get(single.ID); !errors.Is(err, ErrNotFound) { + t.Fatalf("an event that existed only for the meeting was kept: %v", err) + } + if n := s.ForgetMeeting("mtg-3"); n != 0 { + t.Fatalf("ForgetMeeting series = %d, want 0 removed", n) + } + got, err := s.Get(series.ID) + if err != nil || len(got.History) != 1 || got.History[0].MeetingID != "mtg-2" || got.MeetingID != "" { + t.Fatalf("series after forget = %+v, %v", got, err) + } + if _, err := s.Get(other.ID); err != nil { + t.Fatalf("an unrelated event was touched: %v", err) + } + if n := s.ForgetMeeting(""); n != 0 { + t.Fatal("empty id must be a no-op") + } +} diff --git a/src/pkg/meetings/schedule/store.go b/src/pkg/meetings/schedule/store.go index 194afc0889..232ccd120f 100644 --- a/src/pkg/meetings/schedule/store.go +++ b/src/pkg/meetings/schedule/store.go @@ -167,6 +167,30 @@ func (s *Store) Update(id string, fn func(*Event) error) (Event, error) { return Event{}, fmt.Errorf("%w: %s", ErrNotFound, id) } +// Remove deletes one event by its id when check accepts it (nil accepts +// any). It returns the removed event. +func (s *Store) Remove(id string, check func(Event) error) (Event, error) { + s.mu.Lock() + defer s.mu.Unlock() + for k, e := range s.events { + if e.ID != id { + continue + } + if check != nil { + if err := check(e); err != nil { + return e, err + } + } + delete(s.events, k) + if err := s.saveLocked(); err != nil { + s.events[k] = e + return e, err + } + return e, nil + } + return Event{}, fmt.Errorf("%w: %s", ErrNotFound, id) +} + // Upsert stores a normalized event under the SEQUENCE rule: a new event is // added; an existing one is replaced only by a higher SEQUENCE (equal or // lower returns the stored event and ErrStaleSequence). merge builds the diff --git a/src/pkg/meetings/store.go b/src/pkg/meetings/store.go index f11d947d88..9c20fa4f2a 100644 --- a/src/pkg/meetings/store.go +++ b/src/pkg/meetings/store.go @@ -212,6 +212,21 @@ func (s *Store) PurgeTranscript(id string) error { return nil } +// Remove deletes a meeting's directory: the record, its transcript and its +// side documents. Removing a missing meeting is not an error. +func (s *Store) Remove(id string) error { + p, err := s.path(id, meetingFile) + if err != nil { + return err + } + s.mu.Lock() + defer s.mu.Unlock() + if err := os.RemoveAll(filepath.Dir(p)); err != nil { + return fmt.Errorf("meetings: removing %s: %w", id, err) + } + return nil +} + // validDocName keeps side document names to a plain file name. var validDocName = regexp.MustCompile(`^[a-z0-9_-]{1,32}\.json$`) From 0cc1f565bf3a1489e6a60138a77c0f67333a554a Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 16 Nov 2026 23:10:09 -0500 Subject: [PATCH 03/13] =?UTF-8?q?=F0=9F=90=9B=20Meetings=20delete:=20use?= =?UTF-8?q?=20s.meetingsError=20after=20the=20v5=20merge=20(#18600)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Andy Anderson --- src/pkg/dashboard/api_meetings_delete.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/pkg/dashboard/api_meetings_delete.go b/src/pkg/dashboard/api_meetings_delete.go index df69c6605a..0df528355a 100644 --- a/src/pkg/dashboard/api_meetings_delete.go +++ b/src/pkg/dashboard/api_meetings_delete.go @@ -64,7 +64,7 @@ func (s *Server) handleMeetingsDelete(w http.ResponseWriter, r *http.Request) { defer cancel() d, err := svc.Delete(ctx, r.PathValue("id")) if err != nil { - meetingsError(w, err) + s.meetingsError(w, r, err) return } s.auditMeetingDeleted(r, d, "meetings") @@ -107,7 +107,7 @@ func (s *Server) handleMeetingsClearFinished(w http.ResponseWriter, r *http.Requ s.auditMeetingDeleted(r, d, "clear_finished") } if err != nil { - meetingsError(w, err) + s.meetingsError(w, r, err) return } jsonResponse(w, map[string]any{"deleted": list, "count": len(list)}) @@ -126,7 +126,7 @@ func (s *Server) handleMeetingsScheduleDelete(w http.ResponseWriter, r *http.Req eid := r.PathValue("eid") d, err := svc.Delete(ctx, eid) if err != nil { - meetingsError(w, err) + s.meetingsError(w, r, err) return } if d.Meetings == nil { From 1a6f24a97edae8bd3c5a28947ed757094f66e0f8 Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 16 Nov 2026 23:12:37 -0500 Subject: [PATCH 04/13] =?UTF-8?q?=F0=9F=90=9B=20Meetings=20delete:=20clear?= =?UTF-8?q?=20consent=20tries=20and=20load=20the=20delete=20helpers=20in?= =?UTF-8?q?=20the=20role-race=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Carries the fixes from #18615's v5 merge: Delete also forgets the consent attempt count, and the meetings/calendar role-race harness extracts meetingsFinished, meetingsCanDelete, meetingsDeleteButtonHtml, calendarCanDelete and their constants. Signed-off-by: Andy Anderson --- dashboard/test/meetings-calendar-role-race.test.js | 7 ++++--- src/pkg/meetings/delete.go | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/dashboard/test/meetings-calendar-role-race.test.js b/dashboard/test/meetings-calendar-role-race.test.js index b2caab7f22..eb7c174fc4 100644 --- a/dashboard/test/meetings-calendar-role-race.test.js +++ b/dashboard/test/meetings-calendar-role-race.test.js @@ -36,11 +36,12 @@ function extractConst(name) { const GH_AUTH = ['hiveRoleSettled']; const MEETINGS = ['meetingsIsOwner', 'meetingsRoleKnown', 'meetingsOnRoleReady', 'fetchMeetings', 'renderMeetings', 'meetingsJoinFormHtml', - 'meetingsRowHtml', 'meetingsFormatTime', 'meetingsFriendlyReason', 'meetingsReasonHtml', 'meetingsLabel']; + 'meetingsRowHtml', 'meetingsFormatTime', 'meetingsFriendlyReason', 'meetingsReasonHtml', 'meetingsLabel', + 'meetingsFinished', 'meetingsCanDelete', 'meetingsDeleteButtonHtml']; const CALENDAR = ['calendarRoleKnown', 'calendarOnRoleReady', 'calendarCanView', 'calendarIsOwner', 'fetchCalendar', 'calendarRefreshMeetings', 'fetchMeetingDrafts', 'renderCalendar', 'renderMeetingDrafts', 'meetingDraftsSplit', 'calendarFormHtml', 'calendarViewerTimeZone', 'calendarRowHtml', 'calendarFormatTime', 'calendarPlatformLabel', 'calendarMeetingLink', 'calendarActionButtons', 'renderCalendarIndicator', - 'calendarIndicatorState', 'calendarBadgeCount', 'calendarMeetingList']; + 'calendarIndicatorState', 'calendarBadgeCount', 'calendarMeetingList', 'calendarCanDelete']; function makeSandbox(opts) { const els = {}; @@ -73,7 +74,7 @@ function makeSandbox(opts) { vm.createContext(sb); const consts = ['MEETINGS_ERROR_INLINE_CHARS', 'MEETINGS_PLATFORM_LABELS', 'MEETINGS_PILL_STATUS', 'MEETINGS_LIVE_STATES', 'CALENDAR_ACTIVE_STATES', 'CALENDAR_DEFAULT_NAME', 'CALENDAR_PLATFORM_LABELS', 'CALENDAR_PLATFORM_ICONS', 'CALENDAR_SOURCE_LABELS', - 'CALENDAR_STATUS_LABELS', 'CALENDAR_PILL_STATUS'].map(extractConst); + 'CALENDAR_STATUS_LABELS', 'CALENDAR_PILL_STATUS', 'MEETINGS_FINAL_STATES', 'CALENDAR_FINISHED_STATUSES'].map(extractConst); // const declarations stay script-scoped in a vm context; re-declare the // mutable module state with var so the extracted functions share it. vm.runInContext(consts.join('\n') + '\n' + GH_AUTH.concat(MEETINGS, CALENDAR).map(extractFunction).join('\n'), sb); diff --git a/src/pkg/meetings/delete.go b/src/pkg/meetings/delete.go index c8e7ae0359..e4945faf24 100644 --- a/src/pkg/meetings/delete.go +++ b/src/pkg/meetings/delete.go @@ -76,6 +76,7 @@ func (m *Manager) Delete(ctx context.Context, id string) (DeleteResult, error) { m.clearLobby(id) m.mu.Lock() delete(m.consentAt, id) + delete(m.consentTries, id) m.mu.Unlock() if err := m.opts.Store.Remove(id); err != nil { return res, err From 235c138bcd19b9f4bc444890075fc465badf2850 Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 16 Nov 2026 22:33:00 -0500 Subject: [PATCH 05/13] =?UTF-8?q?=E2=9C=A8=20Betty=20runs=20several=20mode?= =?UTF-8?q?l=20calls=20per=20meeting=20at=20once?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A meeting may now have up to meetings.llm.per_meeting_in_flight (default 3, never above max_in_flight) model calls in flight, so a second request no longer waits for the first answer. A per-meeting Gate bounds the calls and reserves each in-flight call's expected cost against the token budget, so concurrent calls cannot all pass the check on the same ledger total. On a warm CLI backend the meeting keeps a pool of warm CLIs (one turn per process, since stream results carry no request id) that grows only when calls overlap. Part of #18590. Signed-off-by: Andy Anderson --- .../added-18590-betty-parallel-model-calls.md | 1 + src/cmd/hive/meetingswire.go | 25 ++- src/cmd/hive/meetingswire_cli_test.go | 4 +- src/docs/meetings.md | 3 +- src/pkg/config/meetings_config.go | 17 +- src/pkg/config/meetings_config_test.go | 7 +- src/pkg/meetings/betty/answer.go | 5 +- src/pkg/meetings/meetllm/anthropic.go | 2 +- src/pkg/meetings/meetllm/cli.go | 24 ++- src/pkg/meetings/meetllm/cli_test.go | 3 + src/pkg/meetings/meetllm/gate.go | 98 ++++++++++ src/pkg/meetings/meetllm/gate_test.go | 154 +++++++++++++++ src/pkg/meetings/meetllm/meetllm.go | 70 ++++++- src/pkg/meetings/meetllm/pool.go | 178 ++++++++++++++++++ src/pkg/meetings/meetllm/pool_test.go | 93 +++++++++ 15 files changed, 659 insertions(+), 25 deletions(-) create mode 100644 changelog.d/added-18590-betty-parallel-model-calls.md create mode 100644 src/pkg/meetings/meetllm/gate.go create mode 100644 src/pkg/meetings/meetllm/gate_test.go create mode 100644 src/pkg/meetings/meetllm/pool.go create mode 100644 src/pkg/meetings/meetllm/pool_test.go diff --git a/changelog.d/added-18590-betty-parallel-model-calls.md b/changelog.d/added-18590-betty-parallel-model-calls.md new file mode 100644 index 0000000000..d43280a005 --- /dev/null +++ b/changelog.d/added-18590-betty-parallel-model-calls.md @@ -0,0 +1 @@ +- **Betty works on several requests at once.** One meeting may now run up to `meetings.llm.per_meeting_in_flight` (default 3, never above `meetings.llm.max_in_flight`) model calls in parallel, so a second question no longer waits for the first answer. On the Claude CLI backend the meeting keeps a small pool of warm CLIs that grows only when requests overlap, and every call in flight reserves budget so parallel answers cannot overspend the meeting's token budget. ([#18590](https://github.com/hivecommons/hive/issues/18590)) diff --git a/src/cmd/hive/meetingswire.go b/src/cmd/hive/meetingswire.go index e634cbd051..6810d8826c 100644 --- a/src/cmd/hive/meetingswire.go +++ b/src/cmd/hive/meetingswire.go @@ -304,6 +304,9 @@ func meetingPersonaFactory(cfg *config.Config, persona config.ResolvedMeetingsPe Timeout: betty.DefaultAddressedTimeout, UsageFile: meetingUsageFile(store, mt.ID), MaxTokens: maxTokens, SlotsDir: slotsDir, MaxInFlight: llm.MaxInFlight, + // Several requests are answered at once, each call reserving + // budget against the others in flight (#18590). + Gate: meetllm.NewGate(llm.PerMeetingInFlight), } switch llm.Backend { case config.MeetingsLLMModeOpenAI: @@ -328,9 +331,10 @@ func meetingPersonaFactory(cfg *config.Config, persona config.ResolvedMeetingsPe // meetingCLIModel is the persona's model on an agent backend // (hivecommons/hive#18517, #18549), in the backend's no-tools completion -// profile with the agents' sign-in: one warm CLI for the meeting, started -// now so the first question finds it ready, on a backend with a warm mode; -// one CLI per call otherwise. It reports false when the backend has no +// profile with the agents' sign-in: a pool of warm CLIs for the meeting +// (one per concurrent answer, #18590), the first started now so the first +// question finds it ready, on a backend with a warm mode; one CLI per call +// otherwise. It reports false when the backend has no // profile. func meetingCLIModel(llm config.ResolvedMeetingsLLM, mc meetllm.Config, meetingID string, logger *slog.Logger) (betty.MeetLLM, bool) { mc.Provider, mc.Backend, mc.Model = meetllm.ProviderAgentBackend, llm.AgentBackend, llm.Model @@ -346,21 +350,30 @@ func meetingCLIModel(llm config.ResolvedMeetingsLLM, mc meetllm.Config, meetingI } return betty.MeetLLM{Config: mc}, true } - sess, err := meetllm.NewCLISession(mc) + pool, err := meetllm.NewCLIPool(mc, meetingCLIPoolSize(llm, mc)) if err != nil { if logger != nil { logger.Error("[meetings] persona model not started", "meeting", meetingID, "backend", llm.AgentBackend, "error", err) } return betty.MeetLLM{}, false } - if err := sess.Warm(); err != nil && logger != nil { + if err := pool.Warm(); err != nil && logger != nil { // The first call retries the start. logger.Warn("[meetings] persona CLI not warmed", "meeting", meetingID, "backend", llm.AgentBackend, "error", err) } - mc.Session = sess + mc.Pool = pool return betty.MeetLLM{Config: mc}, true } +// meetingCLIPoolSize is how many warm CLIs one meeting may run: its +// concurrent answers (the gate's size), else the resolved per-meeting cap. +func meetingCLIPoolSize(llm config.ResolvedMeetingsLLM, mc meetllm.Config) int { + if mc.Gate != nil { + return mc.Gate.Size() + } + return llm.PerMeetingInFlight +} + // applyMeetingCLITimeouts widens the persona's per-call timeouts to the CLI // mode's: a warm turn is slower than an API call. func applyMeetingCLITimeouts(s *betty.Settings, cli config.ResolvedMeetingsCLI) { diff --git a/src/cmd/hive/meetingswire_cli_test.go b/src/cmd/hive/meetingswire_cli_test.go index e909163ac6..5e17328c10 100644 --- a/src/cmd/hive/meetingswire_cli_test.go +++ b/src/cmd/hive/meetingswire_cli_test.go @@ -97,8 +97,8 @@ func TestMeetingPersonaFactoryAgentBackend(t *testing.T) { e.Close() } missing := cliLLM(filepath.Join(dir, "absent"), dir) - if m, ok := meetingCLIModel(missing, meetllm.Config{MaxInFlight: 1}, "m3", logger); !ok || m.Config.Session == nil { - t.Fatal("a missing binary still yields a session that retries the start") + if m, ok := meetingCLIModel(missing, meetllm.Config{MaxInFlight: 1}, "m3", logger); !ok || m.Config.Pool == nil { + t.Fatal("a missing binary still yields a pool that retries the start") } else { _ = m.Close() } diff --git a/src/docs/meetings.md b/src/docs/meetings.md index ae5872b73c..9b187b5a59 100644 --- a/src/docs/meetings.md +++ b/src/docs/meetings.md @@ -128,7 +128,7 @@ The CLI uses the sign-in the agents get, without hive logging it or writing it t A refused backend fails config validation and `PUT /api/meetings/model` with its reason, and the Settings picker lists it disabled, with the reason under "Why some backends are unavailable for meetings". A `meetings.llm.backend` that is neither a known backend nor a configured gateway is refused the same way. -The warm process is replaced after `HIVE_MEETINGS_CLI_RECYCLE_TURNS` turns (default 8, which bounds the history each turn re-reads and pays for), and at once after any error, timeout or crash, so a late reply can never answer the next request and the next call finds a fresh process. Calls from one meeting run one at a time inside the shared in-flight limit. On a CLI backend Betty's timeouts are wider than the API modes' 4 s, 2.5 s and 800 ms: `HIVE_MEETINGS_CLI_ADDRESSED_TIMEOUT_MS`, `HIVE_MEETINGS_CLI_UNPROMPTED_TIMEOUT_MS` and `HIVE_MEETINGS_CLI_RESTYLE_TIMEOUT_MS`, default 8000, 6000 and 3000 on `claude` and 20000, 15000 and 10000 on `copilot` and `omp`, which start the CLI on every call. Spek's per-prompt call uses `HIVE_MEETINGS_CLI_ONESHOT_TIMEOUT_MS` (30000). `HIVE_MEETINGS_CLI_BINARY` overrides the CLI binary. The panel shows the model in use, for example "claude CLI claude-sonnet-5-5" or "vllm model llama-4". +The warm process is replaced after `HIVE_MEETINGS_CLI_RECYCLE_TURNS` turns (default 8, which bounds the history each turn re-reads and pays for), and at once after any error, timeout or crash, so a late reply can never answer the next request and the next call finds a fresh process. Calls from one meeting run in parallel, up to `meetings.llm.per_meeting_in_flight` (default 3) inside the shared in-flight limit: a warm CLI answers one turn at a time on one stream (its results carry no request id), so the meeting keeps a pool of up to that many warm CLIs. Only the first starts with the meeting; when two requests overlap and no idle warm CLI is left, the next one is warmed in the background, so a burst of questions pays at most one cold start (a few seconds on `claude`) and the rest of the meeting answers warm. A meeting that never overlaps requests runs one CLI. On a CLI backend Betty's timeouts are wider than the API modes' 4 s, 2.5 s and 800 ms: `HIVE_MEETINGS_CLI_ADDRESSED_TIMEOUT_MS`, `HIVE_MEETINGS_CLI_UNPROMPTED_TIMEOUT_MS` and `HIVE_MEETINGS_CLI_RESTYLE_TIMEOUT_MS`, default 8000, 6000 and 3000 on `claude` and 20000, 15000 and 10000 on `copilot` and `omp`, which start the CLI on every call. Spek's per-prompt call uses `HIVE_MEETINGS_CLI_ONESHOT_TIMEOUT_MS` (30000). `HIVE_MEETINGS_CLI_BINARY` overrides the CLI binary. The panel shows the model in use, for example "claude CLI claude-sonnet-5-5" or "vllm model llama-4". 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. @@ -329,6 +329,7 @@ Every key has an environment override that wins over `hive.yaml`. The full list | `meetings.llm.anthropic_model` | Spektacular's default | Anthropic model. | | `meetings.llm.timeout_ms` | `2500` | Per-call model timeout. | | `meetings.llm.max_in_flight` | `4` | Concurrent model calls across meetings. | +| `meetings.llm.per_meeting_in_flight` | `3` | Concurrent model calls in one meeting, so Betty answers several requests at once; never above `max_in_flight` (`HIVE_MEETINGS_LLM_PER_MEETING_IN_FLIGHT`). Each call in flight reserves its expected cost against the meeting's token budget, so parallel answers cannot overspend it. | | `meetings.email.invites_enabled` | `true` | Accept calendar invites sent to `hive@` (`HIVE_MEETINGS_EMAIL_INVITES_ENABLED`). | | `meetings.email.files_enabled` | `true` | Accept mail-in files as Inbox items (`HIVE_MEETINGS_EMAIL_FILES_ENABLED`). | | `meetings.persona.enabled` | `true` | The meeting attendee persona. | diff --git a/src/pkg/config/meetings_config.go b/src/pkg/config/meetings_config.go index 3fa746bb35..2de0614318 100644 --- a/src/pkg/config/meetings_config.go +++ b/src/pkg/config/meetings_config.go @@ -284,6 +284,10 @@ type MeetingsLLMConfig struct { // MaxInFlight caps concurrent model calls across meetings. Zero means // DefaultMeetingsLLMMaxInFlight. MaxInFlight int `yaml:"max_in_flight,omitempty" json:"max_in_flight,omitempty"` + // PerMeetingInFlight caps one meeting's concurrent model calls, so the + // persona answers several requests at once (#18590); it never exceeds + // MaxInFlight. Zero means DefaultMeetingsLLMPerMeetingInFlight. + PerMeetingInFlight int `yaml:"per_meeting_in_flight,omitempty" json:"per_meeting_in_flight,omitempty"` } // Meetings LLM modes and defaults. @@ -301,6 +305,9 @@ const ( DefaultMeetingsLLMTimeoutMS = 2500 // DefaultMeetingsLLMMaxInFlight lets a few meetings reason at once. DefaultMeetingsLLMMaxInFlight = 4 + // DefaultMeetingsLLMPerMeetingInFlight lets one meeting's persona work + // on a few requests at once. + DefaultMeetingsLLMPerMeetingInFlight = 3 // DefaultMeetingsLLMKeyFile and DefaultMeetingsAnthropicKeyFile are the // keystore-sealed secret files. DefaultMeetingsLLMKeyFile = WritableSecretsDir + "/meetings_llm_api_key" @@ -325,7 +332,9 @@ const ( MeetingsAnthropicModelEnv = "HIVE_MEETINGS_ANTHROPIC_MODEL" MeetingsLLMTimeoutEnv = "HIVE_MEETINGS_LLM_TIMEOUT_MS" MeetingsLLMMaxInFlightEnv = "HIVE_MEETINGS_LLM_MAX_IN_FLIGHT" - MeetingsLLMBackendEnv = "HIVE_MEETINGS_LLM_BACKEND" + // MeetingsLLMPerMeetingInFlightEnv overrides per_meeting_in_flight. + MeetingsLLMPerMeetingInFlightEnv = "HIVE_MEETINGS_LLM_PER_MEETING_IN_FLIGHT" + MeetingsLLMBackendEnv = "HIVE_MEETINGS_LLM_BACKEND" ) // ResolvedMeetingsLLM is the effective meetings model. Keys are secrets; @@ -343,6 +352,9 @@ type ResolvedMeetingsLLM struct { AnthropicModel string TimeoutMS int MaxInFlight int + // PerMeetingInFlight is one meeting's concurrent model calls, at most + // MaxInFlight. + PerMeetingInFlight int // Reason explains Backend none. Reason string // AgentBackend is the agent backend (claude, ...) on the agent-backend @@ -396,6 +408,8 @@ func (c *Config) ResolveMeetingsLLM(ctx context.Context) ResolvedMeetingsLLM { MaxInFlight: meetingsEnvInt(MeetingsLLMMaxInFlightEnv, l.MaxInFlight, DefaultMeetingsLLMMaxInFlight), AnthropicModel: meetingsEnvString(MeetingsAnthropicModelEnv, l.AnthropicModel, ""), } + r.PerMeetingInFlight = min(r.MaxInFlight, + meetingsEnvInt(MeetingsLLMPerMeetingInFlightEnv, l.PerMeetingInFlight, DefaultMeetingsLLMPerMeetingInFlight)) if mode == MeetingsLLMModeAgentBackend || mode == MeetingsLLMModeClaudeCLI { return c.resolveMeetingsAgentBackend(ctx, mode, r) } @@ -750,6 +764,7 @@ func (m MeetingsConfig) Validate() error { {"meetings.budget.max_intents_per_meeting", m.Budget.MaxIntentsPerMeeting}, {"meetings.llm.timeout_ms", m.LLM.TimeoutMS}, {"meetings.llm.max_in_flight", m.LLM.MaxInFlight}, + {"meetings.llm.per_meeting_in_flight", m.LLM.PerMeetingInFlight}, } { if f.v < 0 { return fmt.Errorf("%s must not be negative (got %d)", f.key, f.v) diff --git a/src/pkg/config/meetings_config_test.go b/src/pkg/config/meetings_config_test.go index 9541835c53..4096ae436f 100644 --- a/src/pkg/config/meetings_config_test.go +++ b/src/pkg/config/meetings_config_test.go @@ -352,7 +352,8 @@ func TestResolveMeetingsLLM(t *testing.T) { cfg.Governor.LiteLLM.DefaultModel = "gpt-x" r := cfg.ResolveMeetingsLLM(ctx) if r.Backend != MeetingsLLMModeOpenAI || r.Endpoint != "https://litellm.example" || r.Model != "gpt-x" || - r.TimeoutMS != DefaultMeetingsLLMTimeoutMS || r.MaxInFlight != DefaultMeetingsLLMMaxInFlight || r.Label() != "model gpt-x" { + r.TimeoutMS != DefaultMeetingsLLMTimeoutMS || r.MaxInFlight != DefaultMeetingsLLMMaxInFlight || r.Label() != "model gpt-x" || + r.PerMeetingInFlight != DefaultMeetingsLLMPerMeetingInFlight { t.Fatalf("litellm fallback %+v", r) } // Meetings' own endpoint, model and key win. @@ -366,6 +367,10 @@ func TestResolveMeetingsLLM(t *testing.T) { if r.Endpoint != "https://llm.example" || r.Model != "m1" || r.APIKey != "mkey" || r.TimeoutMS != 900 || r.MaxInFlight != 2 { t.Fatalf("meetings llm %+v", r) } + // One meeting never runs more calls than the hive-wide cap. + if r.PerMeetingInFlight != 2 { + t.Fatalf("per-meeting in flight %d above max_in_flight 2", r.PerMeetingInFlight) + } // An Anthropic key switches to anthropic mode. aFile := filepath.Join(dir, "a") if err := WriteMeetingsSecret(ctx, aFile, "akey"); err != nil { diff --git a/src/pkg/meetings/betty/answer.go b/src/pkg/meetings/betty/answer.go index ad70fe8a92..d56673ee04 100644 --- a/src/pkg/meetings/betty/answer.go +++ b/src/pkg/meetings/betty/answer.go @@ -55,8 +55,11 @@ func (m MeetLLM) Complete(ctx context.Context, system, prompt string) (string, e return meetllm.Complete(ctx, c, hc, prompt) } -// Close stops the meeting's warm CLI, if it has one. +// Close stops the meeting's warm CLIs, if it has any. func (m MeetLLM) Close() error { + if m.Config.Pool != nil { + _ = m.Config.Pool.Close() + } if m.Config.Session != nil { return m.Config.Session.Close() } diff --git a/src/pkg/meetings/meetllm/anthropic.go b/src/pkg/meetings/meetllm/anthropic.go index 0b52f4b4f4..3113363687 100644 --- a/src/pkg/meetings/meetllm/anthropic.go +++ b/src/pkg/meetings/meetllm/anthropic.go @@ -100,7 +100,7 @@ func completeAnthropic(ctx context.Context, c Config, hc *http.Client, prompt st if err := json.NewDecoder(io.LimitReader(resp.Body, maxResponseBytes)).Decode(&mr); err != nil { return "", fmt.Errorf("meeting-llm: decoding response: %w", err) } - if err := recordUsage(c.UsageFile, mr.Usage.InputTokens+mr.Usage.OutputTokens, time.Now()); err != nil { + if err := c.charge(mr.Usage.InputTokens+mr.Usage.OutputTokens, time.Now()); err != nil { return "", fmt.Errorf("meeting-llm: recording usage: %w", err) } if mr.StopReason == stopRefusal { diff --git a/src/pkg/meetings/meetllm/cli.go b/src/pkg/meetings/meetllm/cli.go index fbc5edfb8d..b5c959bc3c 100644 --- a/src/pkg/meetings/meetllm/cli.go +++ b/src/pkg/meetings/meetllm/cli.go @@ -532,6 +532,26 @@ func (s *CLISession) lock(ctx context.Context) error { func (s *CLISession) unlock() { <-s.sem } +// tryLock takes the turn lock if it is free. +func (s *CLISession) tryLock() bool { + select { + case s.sem <- struct{}{}: + return true + default: + return false + } +} + +// idle reports that no turn holds the session. +func (s *CLISession) idle() bool { return len(s.sem) == 0 } + +// running reports that the session has a live process. +func (s *CLISession) running() bool { + s.mu.Lock() + defer s.mu.Unlock() + return s.proc != nil +} + func (s *CLISession) turnLocked(ctx context.Context, system, prompt string) (string, int, error) { s.mu.Lock() p, err := s.ensureLocked() @@ -597,7 +617,7 @@ func completeSession(ctx context.Context, c Config, prompt string) (string, erro } defer release() reply, tokens, err := c.Session.turnLocked(ctx, c.System, prompt) - if uerr := recordUsage(c.UsageFile, tokens, time.Now()); uerr != nil && err == nil { + if uerr := c.charge(tokens, time.Now()); uerr != nil && err == nil { err = fmt.Errorf("meeting-llm: recording usage: %w", uerr) } return reply, err @@ -655,7 +675,7 @@ func completeCLIOnce(ctx context.Context, c Config, prompt string) (string, erro } return "", perr } - if err := recordUsage(c.UsageFile, out.tokens, time.Now()); err != nil { + if err := c.charge(out.tokens, time.Now()); err != nil { return "", fmt.Errorf("meeting-llm: recording usage: %w", err) } if out.err != nil { diff --git a/src/pkg/meetings/meetllm/cli_test.go b/src/pkg/meetings/meetllm/cli_test.go index b205a87b75..a179aa2cd8 100644 --- a/src/pkg/meetings/meetllm/cli_test.go +++ b/src/pkg/meetings/meetllm/cli_test.go @@ -23,6 +23,8 @@ const ( fakeCrash = "crash" fakeCrashOnce = "crashonce" fakeError = "error" + // fakeSlow answers after half a second, so turns overlap. + fakeSlow = "slow" ) // fakeTokensPerTurn is what the fake reports per turn: 10+5+3+2. @@ -46,6 +48,7 @@ while IFS= read -r line; do echo "$line" >> "$D/turns" case "` + behaviour + `" in hang) sleep 30 ;; + slow) sleep 0.5 ;; crash) exit 1 ;; crashonce) if [ ! -f "$D/crashed" ]; then : > "$D/crashed"; exit 1; fi ;; error) echo '{"type":"result","subtype":"error_during_execution","is_error":true,"result":"","usage":{"input_tokens":10,"output_tokens":5,"cache_creation_input_tokens":3,"cache_read_input_tokens":2}}'; continue ;; diff --git a/src/pkg/meetings/meetllm/gate.go b/src/pkg/meetings/meetllm/gate.go new file mode 100644 index 0000000000..d783ba8a15 --- /dev/null +++ b/src/pkg/meetings/meetllm/gate.go @@ -0,0 +1,98 @@ +package meetllm + +import ( + "context" + "fmt" + "sync" +) + +// Per-meeting parallelism (hivecommons/hive#18590): the meeting persona +// answers several requests at once, so one meeting may have more than one +// model call in flight. Gate bounds how many and keeps the meeting's token +// budget honest while they run. +const ( + // DefaultPerMeetingInFlight is how many model calls one meeting may + // have in flight when its gate is built with zero. + DefaultPerMeetingInFlight = 3 + // defaultCallEstimate is the tokens a call is assumed to cost before + // the meeting has seen one: a persona prompt with retrieval snippets + // and a short JSON answer. + defaultCallEstimate = 2000 +) + +// Gate is one meeting's in-process admission for model calls: at most N in +// flight, and a token reservation per call so concurrent calls cannot all +// pass the budget check on the same ledger total and overspend it. The +// ledger stays the single record of spend; the reservation only covers the +// calls whose usage is not written yet. +type Gate struct { + sem chan struct{} + + mu sync.Mutex + reserved int + // estimate is the largest call cost seen so far (defaultCallEstimate + // until a call reports usage). + estimate int +} + +// NewGate builds a gate admitting n concurrent calls; n <= 0 means +// DefaultPerMeetingInFlight. +func NewGate(n int) *Gate { + if n <= 0 { + n = DefaultPerMeetingInFlight + } + return &Gate{sem: make(chan struct{}, n), estimate: defaultCallEstimate} +} + +// Size is how many calls the gate admits at once. +func (g *Gate) Size() int { return cap(g.sem) } + +// InFlight is how many calls hold the gate now. +func (g *Gate) InFlight() int { return len(g.sem) } + +// enter waits for a free place until ctx ends. +func (g *Gate) enter(ctx context.Context) error { + select { + case g.sem <- struct{}{}: + return nil + case <-ctx.Done(): + return fmt.Errorf("meeting-llm: meeting's model calls all busy: %w", ctx.Err()) + } +} + +func (g *Gate) leave() { <-g.sem } + +// reserve admits one call against the budget: it refuses (ErrBudget) when +// the ledger total plus the calls still in flight already reach maxTokens, +// and otherwise holds this call's estimate until settle. +func (g *Gate) reserve(used, maxTokens int) (int, error) { + g.mu.Lock() + defer g.mu.Unlock() + if maxTokens > 0 && used+g.reserved >= maxTokens { + return 0, ErrBudget + } + est := g.estimate + g.reserved += est + return est, nil +} + +// settle releases a call's reservation once its usage is in the ledger and +// learns its cost for the next estimate. +func (g *Gate) settle(held, spent int) { + g.mu.Lock() + defer g.mu.Unlock() + g.reserved -= held + if g.reserved < 0 { + g.reserved = 0 + } + if spent > g.estimate { + g.estimate = spent + } +} + +// Reserved is the tokens held for calls in flight. +func (g *Gate) Reserved() int { + g.mu.Lock() + defer g.mu.Unlock() + return g.reserved +} diff --git a/src/pkg/meetings/meetllm/gate_test.go b/src/pkg/meetings/meetllm/gate_test.go new file mode 100644 index 0000000000..bdb1e58aae --- /dev/null +++ b/src/pkg/meetings/meetllm/gate_test.go @@ -0,0 +1,154 @@ +package meetllm + +import ( + "context" + "errors" + "net/http" + "net/http/httptest" + "path/filepath" + "sync" + "sync/atomic" + "testing" + "time" +) + +// gateServer is a chat endpoint that holds every request until release is +// closed, counting the most requests it held at once. +func gateServer(t *testing.T, tokens string) (*httptest.Server, chan struct{}, *atomic.Int32, *atomic.Int32) { + t.Helper() + release := make(chan struct{}) + var now, peak atomic.Int32 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + n := now.Add(1) + for { + p := peak.Load() + if n <= p || peak.CompareAndSwap(p, n) { + break + } + } + <-release + now.Add(-1) + _, _ = w.Write([]byte(`{"choices":[{"message":{"role":"assistant","content":"ok"}}],"usage":{"total_tokens":` + tokens + `}}`)) + })) + t.Cleanup(srv.Close) + t.Cleanup(func() { + select { + case <-release: + default: + close(release) + } + }) + return srv, release, &now, &peak +} + +func gateConfig(srv *httptest.Server, dir string, g *Gate) Config { + return Config{ + Endpoint: srv.URL, Model: "m", Timeout: 5 * time.Second, + UsageFile: filepath.Join(dir, "usage.jsonl"), MaxInFlight: 8, Gate: g, + } +} + +// A meeting's gate lets up to its size of calls run at once, and no more. +func TestCompleteGateRunsCallsInParallel(t *testing.T) { + for _, size := range []int{1, 3} { + srv, release, now, peak := gateServer(t, "10") + c := gateConfig(srv, t.TempDir(), NewGate(size)) + const calls = 4 + var wg sync.WaitGroup + errs := make(chan error, calls) + for range calls { + wg.Add(1) + go func() { + defer wg.Done() + _, err := Complete(context.Background(), c, srv.Client(), "q") + errs <- err + }() + } + waitFor(t, "calls in flight", func() bool { return int(now.Load()) == size }) + // Give a call past the gate's size time to show up if it could. + time.Sleep(50 * time.Millisecond) + if got := int(peak.Load()); got != size { + t.Fatalf("gate %d: %d calls in flight at once", size, got) + } + close(release) + wg.Wait() + close(errs) + for err := range errs { + if err != nil { + t.Fatalf("gate %d: %v", size, err) + } + } + if n, _ := UsageTotal(c.UsageFile); n != calls*10 { + t.Fatalf("gate %d: usage %d, want %d", size, n, calls*10) + } + } +} + +// Concurrent calls cannot all pass the budget check on the same ledger +// total: a call in flight holds a reservation, so the next one is refused +// before it reaches the model. +func TestCompleteGateReservesBudget(t *testing.T) { + srv, release, now, _ := gateServer(t, "100") + c := gateConfig(srv, t.TempDir(), NewGate(3)) + c.MaxTokens = 150 + first := make(chan error, 1) + go func() { + _, err := Complete(context.Background(), c, srv.Client(), "q") + first <- err + }() + waitFor(t, "the first call in flight", func() bool { return now.Load() == 1 }) + if _, err := Complete(context.Background(), c, srv.Client(), "q2"); !errors.Is(err, ErrBudget) { + t.Fatalf("second call while the first holds the budget: err = %v", err) + } + close(release) + if err := <-first; err != nil { + t.Fatal(err) + } + if r := c.Gate.Reserved(); r != 0 { + t.Fatalf("reservation left after the call: %d", r) + } + // Sequential calls keep the old rule: refused once the ledger is spent. + if _, err := Complete(context.Background(), c, srv.Client(), "q3"); err != nil { + t.Fatalf("a call under budget: %v", err) + } + if _, err := Complete(context.Background(), c, srv.Client(), "q4"); !errors.Is(err, ErrBudget) { + t.Fatalf("over budget err = %v", err) + } +} + +func TestGateReserveAndSettle(t *testing.T) { + g := NewGate(0) + if g.Size() != DefaultPerMeetingInFlight { + t.Fatalf("size %d", g.Size()) + } + held, err := g.reserve(0, 0) + if err != nil || held != defaultCallEstimate { + t.Fatalf("reserve without a budget = %d %v", held, err) + } + g.settle(held, defaultCallEstimate+500) + if g.Reserved() != 0 || g.estimate != defaultCallEstimate+500 { + t.Fatalf("after settle reserved %d estimate %d", g.Reserved(), g.estimate) + } + a, _ := g.reserve(0, 10_000) + if _, err := g.reserve(10_000-a, 10_000); !errors.Is(err, ErrBudget) { + t.Fatalf("reservation not counted: %v", err) + } + g.settle(a, 0) + g.settle(a, 0) + if g.Reserved() != 0 { + t.Fatalf("double settle went negative: %d", g.Reserved()) + } +} + +// A call waiting for the meeting's place gives up at its deadline. +func TestCompleteGateBusy(t *testing.T) { + srv, _, now, _ := gateServer(t, "1") + c := gateConfig(srv, t.TempDir(), NewGate(1)) + held := c + go func() { _, _ = Complete(context.Background(), held, srv.Client(), "hold") }() + waitFor(t, "the held call", func() bool { return now.Load() == 1 }) + c.Timeout = 50 * time.Millisecond + if _, err := Complete(context.Background(), c, srv.Client(), "q"); !errors.Is(err, context.DeadlineExceeded) { + t.Fatalf("busy err = %v", err) + } +} diff --git a/src/pkg/meetings/meetllm/meetllm.go b/src/pkg/meetings/meetllm/meetllm.go index 15f4b729a1..f84d16740e 100644 --- a/src/pkg/meetings/meetllm/meetllm.go +++ b/src/pkg/meetings/meetllm/meetllm.go @@ -119,8 +119,16 @@ type Config struct { // replaced; zero means DefaultRecycleTurns. RecycleTurns int // Session is the meeting's warm CLI. Nil on ProviderAgentBackend runs - // one CLI per call. + // one CLI per call. Pool, when set, takes its place. Session *CLISession + // Pool is the meeting's set of warm CLIs, so several turns run at once + // (hivecommons/hive#18590). + Pool *CLIPool + // Gate bounds the meeting's concurrent calls and reserves budget for + // the ones in flight; nil admits every call (the one-shot command). + Gate *Gate + // spent receives the tokens a call charged, for the gate. + spent *int // Getenv reads the environment the CLI inherits from; nil is os.Getenv. Getenv func(string) string // CABundle is an optional PEM file of private CAs the endpoint is @@ -193,6 +201,15 @@ func UsageTotal(path string) (int, error) { return total, sc.Err() } +// charge records a call's tokens in the ledger and reports them to the +// meeting's gate. +func (c Config) charge(tokens int, now time.Time) error { + if c.spent != nil && tokens > 0 { + *c.spent += tokens + } + return recordUsage(c.UsageFile, tokens, now) +} + func recordUsage(path string, tokens int, now time.Time) error { if path == "" || tokens <= 0 { return nil @@ -273,19 +290,52 @@ func chatURL(endpoint string) string { } // Complete makes one call and records its usage. It refuses once the -// meeting's budget is spent. +// meeting's budget is spent. With a Gate, the call first waits for one of +// the meeting's places and reserves budget against the calls still in +// flight, so concurrent answers cannot overspend it. func Complete(ctx context.Context, c Config, hc *http.Client, prompt string) (string, error) { - if c.MaxTokens > 0 && c.UsageFile != "" { - used, err := UsageTotal(c.UsageFile) - if err != nil { + used, err := c.used() + if err != nil { + return "", err + } + if c.MaxTokens > 0 && used >= c.MaxTokens { + return "", ErrBudget + } + ctx, cancel := context.WithTimeout(ctx, c.Timeout) + defer cancel() + if c.Gate != nil { + if err := c.Gate.enter(ctx); err != nil { return "", err } - if used >= c.MaxTokens { - return "", ErrBudget + defer c.Gate.leave() + // Calls that finished while this one waited are in the ledger now. + if used, err = c.used(); err != nil { + return "", err } + held, err := c.Gate.reserve(used, c.MaxTokens) + if err != nil { + return "", err + } + var spent int + c.spent = &spent + defer func() { c.Gate.settle(held, spent) }() + } + return complete(ctx, c, hc, prompt) +} + +// used is the meeting's ledger total when it has a budget. +func (c Config) used() (int, error) { + if c.MaxTokens <= 0 || c.UsageFile == "" { + return 0, nil + } + return UsageTotal(c.UsageFile) +} + +// complete is one call once admitted. +func complete(ctx context.Context, c Config, hc *http.Client, prompt string) (string, error) { + if c.Provider == ProviderAgentBackend && c.Pool != nil { + return completePool(ctx, c, prompt) } - ctx, cancel := context.WithTimeout(ctx, c.Timeout) - defer cancel() if c.Provider == ProviderAgentBackend && c.Session != nil { return completeSession(ctx, c, prompt) } @@ -330,7 +380,7 @@ func Complete(ctx context.Context, c Config, hc *http.Client, prompt string) (st if tokens == 0 { tokens = cr.Usage.PromptTokens + cr.Usage.CompletionTokens } - if err := recordUsage(c.UsageFile, tokens, time.Now()); err != nil { + if err := c.charge(tokens, time.Now()); err != nil { return "", fmt.Errorf("meeting-llm: recording usage: %w", err) } if len(cr.Choices) == 0 { diff --git a/src/pkg/meetings/meetllm/pool.go b/src/pkg/meetings/meetllm/pool.go new file mode 100644 index 0000000000..2c1250cf86 --- /dev/null +++ b/src/pkg/meetings/meetllm/pool.go @@ -0,0 +1,178 @@ +package meetllm + +import ( + "context" + "fmt" + "sync" + "time" +) + +// CLIPool is one meeting's set of warm completion CLIs, so the persona can +// run several turns at once (hivecommons/hive#18590). A warm CLI answers +// one turn at a time on one stdin/stdout stream (its result events carry no +// request id to tell turns apart), so concurrency comes from more +// processes, never from interleaving turns on one. +// +// The first CLI is warmed when the meeting starts. The others start only +// when the meeting actually overlaps requests: a call that takes the last +// idle warm CLI while another call is in flight warms the next one in the +// background, so a burst of questions pays at most one cold start and the +// rest of the meeting stays warm. Memory is bounded by Size processes. +type CLIPool struct { + cfg Config + // sem holds one place per call in the pool; a holder always finds an + // idle session (or room to start one). + sem chan struct{} + + mu sync.Mutex + sessions []*CLISession + closed bool +} + +// NewCLIPool builds a pool of up to n warm CLIs for c.Backend; n <= 0 +// means one. Warm starts the first. +func NewCLIPool(c Config, n int) (*CLIPool, error) { + if n <= 0 { + n = 1 + } + first, err := NewCLISession(c) + if err != nil { + return nil, err + } + return &CLIPool{cfg: c, sem: make(chan struct{}, n), sessions: []*CLISession{first}}, nil +} + +// Size is the most CLIs the pool runs. +func (p *CLIPool) Size() int { return cap(p.sem) } + +// Warm starts the first CLI if it is not running. +func (p *CLIPool) Warm() error { + p.mu.Lock() + s := p.sessions[0] + p.mu.Unlock() + return s.Warm() +} + +// Started is how many of the pool's CLIs have a process now. +func (p *CLIPool) Started() int { + p.mu.Lock() + defer p.mu.Unlock() + n := 0 + for _, s := range p.sessions { + if s.running() { + n++ + } + } + return n +} + +// Close stops every CLI; later turns fail with ErrSessionClosed. +func (p *CLIPool) Close() error { + p.mu.Lock() + p.closed = true + sessions := append([]*CLISession(nil), p.sessions...) + p.mu.Unlock() + for _, s := range sessions { + _ = s.Close() + } + return nil +} + +// take waits for a place, then returns an idle session holding its turn +// lock: a warm one when there is one, else an idle cold one, else a new +// session. Release it with give. +func (p *CLIPool) take(ctx context.Context) (*CLISession, error) { + select { + case p.sem <- struct{}{}: + case <-ctx.Done(): + return nil, fmt.Errorf("meeting-llm: CLI pool busy: %w", ctx.Err()) + } + p.mu.Lock() + defer p.mu.Unlock() + if p.closed { + <-p.sem + return nil, ErrSessionClosed + } + s := p.idleLocked(true) + if s == nil { + s = p.idleLocked(false) + } + if s == nil { + // The place guarantees room: every existing session is held. + var err error + if s, err = p.addLocked(); err != nil { + <-p.sem + return nil, err + } + s.tryLock() + } + p.warmAheadLocked() + return s, nil +} + +// give returns a session taken with take. +func (p *CLIPool) give(s *CLISession) { + s.unlock() + <-p.sem +} + +// idleLocked locks and returns an idle session, warm ones only when warm. +func (p *CLIPool) idleLocked(warm bool) *CLISession { + for _, s := range p.sessions { + if warm && !s.running() { + continue + } + if s.tryLock() { + return s + } + } + return nil +} + +func (p *CLIPool) addLocked() (*CLISession, error) { + s, err := NewCLISession(p.cfg) + if err != nil { + return nil, err + } + p.sessions = append(p.sessions, s) + return s, nil +} + +// warmAheadLocked starts one more CLI in the background when calls overlap +// and no idle warm CLI is left for the next one. +func (p *CLIPool) warmAheadLocked() { + if len(p.sem) < 2 || len(p.sessions) >= cap(p.sem) { + return + } + for _, s := range p.sessions { + if s.running() && s.idle() { + return + } + } + s, err := p.addLocked() + if err != nil { + return + } + go func() { _ = s.Warm() }() +} + +// completePool runs one call on an idle CLI of the meeting's pool under the +// hive-wide in-flight limit and records its usage. The pool place is taken +// before the slot, so a call queued behind its own meeting holds no slot. +func completePool(ctx context.Context, c Config, prompt string) (string, error) { + s, err := c.Pool.take(ctx) + if err != nil { + return "", err + } + defer c.Pool.give(s) + release, err := acquire(ctx, c.SlotsDir, c.MaxInFlight) + if err != nil { + return "", err + } + defer release() + reply, tokens, err := s.turnLocked(ctx, c.System, prompt) + if uerr := c.charge(tokens, time.Now()); uerr != nil && err == nil { + err = fmt.Errorf("meeting-llm: recording usage: %w", uerr) + } + return reply, err +} diff --git a/src/pkg/meetings/meetllm/pool_test.go b/src/pkg/meetings/meetllm/pool_test.go new file mode 100644 index 0000000000..0ba6f2993a --- /dev/null +++ b/src/pkg/meetings/meetllm/pool_test.go @@ -0,0 +1,93 @@ +package meetllm + +import ( + "context" + "errors" + "net/http" + "path/filepath" + "sync" + "testing" +) + +func newPool(t *testing.T, c Config, n int) *CLIPool { + t.Helper() + p, err := NewCLIPool(c, n) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = p.Close() }) + return p +} + +// Overlapping turns run on separate warm processes, never interleaved on +// one, and the pool grows no further than its size. +func TestCLIPoolParallelTurns(t *testing.T) { + dir := t.TempDir() + c := cliConfig(t, dir, fakeSlow) + c.UsageFile = filepath.Join(dir, "usage.jsonl") + c.MaxInFlight = 8 + c.Gate = NewGate(2) + c.Pool = newPool(t, c, 2) + if err := c.Pool.Warm(); err != nil { + t.Fatal(err) + } + const calls = 4 + var wg sync.WaitGroup + var mu sync.Mutex + pids := map[int]bool{} + for range calls { + wg.Add(1) + go func() { + defer wg.Done() + reply, err := Complete(context.Background(), c, http.DefaultClient, "q") + if err != nil { + t.Error(err) + return + } + mu.Lock() + pids[replyPID(t, reply)] = true + mu.Unlock() + }() + } + wg.Wait() + if len(pids) != 2 { + t.Fatalf("turns ran on %d processes, want 2: %v", len(pids), pids) + } + if n := c.Pool.Started(); n != 2 { + t.Fatalf("pool runs %d CLIs, want 2", n) + } + if n, _ := UsageTotal(c.UsageFile); n != calls*fakeTokensPerTurn { + t.Fatalf("usage %d, want %d", n, calls*fakeTokensPerTurn) + } +} + +// One request at a time keeps a single warm CLI: the pool only grows when +// calls overlap. +func TestCLIPoolSequentialStaysSingle(t *testing.T) { + dir := t.TempDir() + c := cliConfig(t, dir, fakeOK) + c.Pool = newPool(t, c, 3) + for range 3 { + if _, err := Complete(context.Background(), c, http.DefaultClient, "q"); err != nil { + t.Fatal(err) + } + } + if n := len(readLines(t, filepath.Join(dir, "starts"))); n != 1 { + t.Fatalf("sequential turns started %d CLIs", n) + } +} + +// A closed pool refuses turns. +func TestCLIPoolClosed(t *testing.T) { + c := cliConfig(t, t.TempDir(), fakeOK) + c.Pool = newPool(t, c, 2) + if err := c.Pool.Close(); err != nil { + t.Fatal(err) + } + if _, err := Complete(context.Background(), c, http.DefaultClient, "q"); !errors.Is(err, ErrSessionClosed) { + t.Fatalf("closed pool err = %v", err) + } + if _, err := NewCLIPool(Config{Backend: "codex"}, 2); err == nil { + t.Fatal("want an error for a backend with no warm mode") + } +} From 03fae0f428125d8338d00f543ffb707324e4f033 Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 16 Nov 2026 22:51:27 -0500 Subject: [PATCH 06/13] =?UTF-8?q?=F0=9F=8C=B1=20Show=20meetings.llm.per=5F?= =?UTF-8?q?meeting=5Fin=5Fflight=20in=20the=20meetings=20settings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Settings parity guard needs every hive.yaml leaf referenced by the Settings UI; the meetings settings' effective values now include it. Part of #18590. Signed-off-by: Andy Anderson --- src/pkg/dashboard/api_meetings.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/pkg/dashboard/api_meetings.go b/src/pkg/dashboard/api_meetings.go index 1e9ba92d56..fc78f57cdc 100644 --- a/src/pkg/dashboard/api_meetings.go +++ b/src/pkg/dashboard/api_meetings.go @@ -504,6 +504,7 @@ func (s *Server) meetingsSettings(ctx context.Context, m config.MeetingsConfig) "meetings.llm.anthropic_model": m.LLM.AnthropicModel, "meetings.llm.timeout_ms": strconv.Itoa(m.LLM.TimeoutMS), "meetings.llm.max_in_flight": strconv.Itoa(m.LLM.MaxInFlight), + "meetings.llm.per_meeting_in_flight": strconv.Itoa(meetingsPerMeetingInFlight(m.LLM)), "meetings.persona.enabled": boolString(persona.Enabled), "meetings.persona.preset": persona.Preset, "meetings.persona.name": persona.Name, @@ -628,3 +629,16 @@ func (s *Server) handleMeetingsSettingsPost(w http.ResponseWriter, r *http.Reque out.RestartRequired = changed jsonResponse(w, out) } + +// meetingsPerMeetingInFlight is meetings.llm.per_meeting_in_flight as it +// applies: the default when unset, never above max_in_flight. +func meetingsPerMeetingInFlight(l config.MeetingsLLMConfig) int { + n, ceiling := l.PerMeetingInFlight, l.MaxInFlight + if n <= 0 { + n = config.DefaultMeetingsLLMPerMeetingInFlight + } + if ceiling <= 0 { + ceiling = config.DefaultMeetingsLLMMaxInFlight + } + return min(n, ceiling) +} From 8dd661e0b4526f451edf04c8cab4305da7a9ecf3 Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 16 Nov 2026 22:39:36 -0500 Subject: [PATCH 07/13] =?UTF-8?q?=E2=9C=A8=20Betty=20names=20who=20and=20w?= =?UTF-8?q?hat=20each=20answer=20is=20for=20when=20requests=20overlap?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With more than one request in flight, other lines said since the request, or an answer later than 8 s, each answer opens with the asker's display name (first name when spoken, never an @mention) and a short topic phrase: the model's new "topic" label, else the request's own words. Two or more requests in hand post one queue note per minute (chat, rate limited, not charged), and spoken acks name their asker when others are waiting. Part of #18590. Signed-off-by: Andy Anderson --- .../added-18590-betty-attributed-answers.md | 1 + src/docs/meetings.md | 1 + src/pkg/meetings/betty/answer.go | 7 +- src/pkg/meetings/betty/attribution.go | 309 ++++++++++++++++++ src/pkg/meetings/betty/attribution_test.go | 190 +++++++++++ src/pkg/meetings/betty/betty.go | 7 + src/pkg/meetings/betty/persona.go | 2 +- src/pkg/meetings/betty/queue.go | 21 +- src/pkg/meetings/live/attribution_test.go | 48 +++ src/pkg/meetings/live/session.go | 15 + 10 files changed, 595 insertions(+), 6 deletions(-) create mode 100644 changelog.d/added-18590-betty-attributed-answers.md create mode 100644 src/pkg/meetings/betty/attribution.go create mode 100644 src/pkg/meetings/betty/attribution_test.go create mode 100644 src/pkg/meetings/live/attribution_test.go diff --git a/changelog.d/added-18590-betty-attributed-answers.md b/changelog.d/added-18590-betty-attributed-answers.md new file mode 100644 index 0000000000..65b20c5736 --- /dev/null +++ b/changelog.d/added-18590-betty-attributed-answers.md @@ -0,0 +1 @@ +- **Betty says who and what each answer is for when several requests overlap.** With more than one request in flight, after other people spoke, or when an answer is late, every answer opens with the asker's name and a short topic ("Andy (re: open PRs): ..." in chat, "Andy, about open PRs: ..." spoken), without @mentions that ping. With two or more requests in hand she posts one short "I have N questions; answering each as it's ready." note (at most once a minute), and a spoken acknowledgement names its asker when others are waiting. ([#18590](https://github.com/hivecommons/hive/issues/18590)) diff --git a/src/docs/meetings.md b/src/docs/meetings.md index 9b187b5a59..5ca4953a3d 100644 --- a/src/docs/meetings.md +++ b/src/docs/meetings.md @@ -36,6 +36,7 @@ Hive attends meetings as a persona: **Betty** ("Betty Bee from the hive", she/he - **Intro.** "Betty, introduce yourself", "who are you, Betty?" or "who's the bot?" gets the intro (`meetings.persona.intro_template`), which says what she does and how to send her home. - **What she answers.** When addressed she answers briefly from the hive's knowledge vaults, its beads (work items), its code maps (when `knowledge.code_maps.enabled` is on) and the web (when `meetings.persona.searxng_url` is set, see [Web answers](#web-answers)), plus the last three minutes of the meeting. If she does not know, she says so. A request that is not a question gets one clarifying question or one or two sentences on the recent discussion. - **Voice.** She speaks only when the request was spoken (addressed in the meeting's audio) or asks for it ("say it", "out loud"). She first says a short acknowledgement at once ("Let me check on that.", rotating, in the persona's language), then the answer, at most 2 sentences and 200 characters with no links or Markdown. The answer goes through the meeting's voice rules: it is spoken only when the meeting's voice is on and `meetings.voice_on_address` is true, and falls back to chat otherwise or when speaking fails; the acknowledgement is never posted to chat and is not charged to the budget. That gate is hive's, for every platform: Quorum v0.2.0 ignores `voice: false` on Discord, so hive never calls `say` for a meeting whose voice is off. A spoken "Betty?" with no question gets one clarifying question or one or two sentences on the last three minutes. +- **Several requests at once.** Each request is worked on as soon as it is heard and answered as soon as its answer is ready, so answers can arrive out of order. Whenever that could confuse the room (another request is in flight, someone else spoke since, or the answer took more than 8 s) the answer opens with who asked and a short topic phrase: "Andy (re: open PRs): ..." in chat, "Andy, about open PRs: ..." spoken (first name only). The topic is the model's 2 to 5 word label for the question, else the request's own words. Names are written plain, never as `@name`, so nobody is pinged. A single quick answer has no prefix. With two or more requests in hand she posts one short chat note ("I have 3 questions; answering each as it's ready."), at most once a minute, through the chat rate limit and never charged; a spoken acknowledgement names its asker ("Bo, one moment.") when another request is already in flight. - **Questions about the hive itself.** "How many open issues are there?", "what PRs are open in the hive repo?", "which agents are working?" are answered from the hive's live dashboard state (the same open, actionable, held and blocked counts, agents and open PRs the Overview and the dashboard chat use), read in-process, so the answer comes in seconds and never goes to research. Such a question names the hive (or "the repo") or asks for open issues, open PRs, actionable items or recent merges. - **Deep dives.** When an addressed question needs research (the model sets `needs_research`, or its confidence is below `min_confidence`) and the meeting has a research path, she says "I'll dig into that and report back", starts a research item through the same pool, cap and approval as Spek's research (shown in the panel as `betty-N`, dispatched with `betty=1` in its meta), and gives exactly one follow-up in her own voice when it completes (the summary as is if restyling fails), in the channel she was asked in: spoken for a spoken question when the meeting can hear her, else in chat. She always closes the loop: if the research cannot start (for example "no research agent is available on this hive"), the agent reports an error, or it is still running after 5 minutes, she says "I couldn't look that up: ."; one that runs past 30 seconds gets one "still looking" note first. - **Room questions.** Betty also answers questions people ask the room (a line ending in "?" or starting with "what", "how", "who" and the like, four words or more), but only if nobody answers within `answer_wait_s` (8 seconds), the knowledge sources had a hit, the model is at least `min_confidence` (0.75) sure, and the topic was not already answered. She never posts general commentary. diff --git a/src/pkg/meetings/betty/answer.go b/src/pkg/meetings/betty/answer.go index d56673ee04..80dcf0a655 100644 --- a/src/pkg/meetings/betty/answer.go +++ b/src/pkg/meetings/betty/answer.go @@ -72,6 +72,9 @@ type Answer struct { Confidence float64 `json:"confidence"` NeedsResearch bool `json:"needs_research"` Sources []string `json:"sources"` + // Topic is a short label for what was asked ("open PRs"), quoted when + // the answer is attributed. + Topic string `json:"topic,omitempty"` } // errNoJSON is returned when the reply holds no JSON object. @@ -89,11 +92,13 @@ func ParseAnswer(raw string) (Answer, error) { Confidence float64 `json:"confidence"` NeedsResearch bool `json:"needs_research"` Sources []json.RawMessage `json:"sources"` + Topic string `json:"topic"` } if err := json.Unmarshal([]byte(raw[i:j+1]), &loose); err != nil { return Answer{}, errNoJSON } - a := Answer{Answer: strings.TrimSpace(loose.Answer), Confidence: loose.Confidence, NeedsResearch: loose.NeedsResearch} + a := Answer{Answer: strings.TrimSpace(loose.Answer), Confidence: loose.Confidence, NeedsResearch: loose.NeedsResearch, + Topic: cleanTopic(loose.Topic)} for _, s := range loose.Sources { var str string if json.Unmarshal(s, &str) == nil { diff --git a/src/pkg/meetings/betty/attribution.go b/src/pkg/meetings/betty/attribution.go new file mode 100644 index 0000000000..10edbed2bd --- /dev/null +++ b/src/pkg/meetings/betty/attribution.go @@ -0,0 +1,309 @@ +package betty + +import ( + "fmt" + "strings" + "time" + "unicode" + "unicode/utf8" +) + +// Attribution (hivecommons/hive#18590): Betty works on several requests at +// once and answers each as soon as it is ready, so the order answers arrive +// in is not the order they were asked in. When that could confuse the room +// (more than one request in flight, other lines said since, or a late +// answer) every answer opens with who asked and a short topic phrase: +// "Andy (re: open PRs): ..." in chat, "Andy, about open PRs: ..." spoken. +// A single quick answer stays short, with no prefix. +// +// Names are written plain, never as "@name": a platform mention would ping +// the person, and on some chats "@everyone" style names would ping the room. +const ( + // attributeAfter is how late an answer may be before it names who and + // what even when it was the only request. + attributeAfter = 8 * time.Second + // maxTopicWords bounds the topic phrase quoted in an attribution. + maxTopicWords = 5 + // maxTopicChars bounds the topic phrase's length. + maxTopicChars = 40 + // maxNameChars bounds the asker's name in an attribution. + maxNameChars = 32 + // queueNoteGap is the least time between two "I have N questions" + // notes. + queueNoteGap = time.Minute + // minQueueNote is how many requests must be in flight before the note. + minQueueNote = 2 +) + +// TriggerQueue is the one short chat note that tells the room she has +// several requests in hand. +const TriggerQueue = "queue" + +// attrPhrases are the attribution formats in one language: chat and voice +// take the name, the topic and the answer; the "bare" forms have no topic; +// ack takes the name and the acknowledgement; queue takes the count. +type attrPhrases struct { + chat, chatBare string + voice, voiceBare string + ack string + queue string +} + +var attributions = map[string]attrPhrases{ + "en": {chat: "%s (re: %s): %s", chatBare: "%s: %s", voice: "%s, about %s: %s", voiceBare: "%s, %s", ack: "%s, %s", + queue: "I have %d questions; answering each as it's ready."}, + "es": {chat: "%s (sobre %s): %s", chatBare: "%s: %s", voice: "%s, sobre %s: %s", voiceBare: "%s, %s", ack: "%s, %s", + queue: "Tengo %d preguntas; respondo cada una en cuanto esté lista."}, + "fr": {chat: "%s (à propos de %s) : %s", chatBare: "%s : %s", voice: "%s, à propos de %s : %s", voiceBare: "%s, %s", ack: "%s, %s", + queue: "J'ai %d questions ; je réponds à chacune dès qu'elle est prête."}, + "de": {chat: "%s (zu %s): %s", chatBare: "%s: %s", voice: "%s, zu %s: %s", voiceBare: "%s, %s", ack: "%s, %s", + queue: "Ich habe %d Fragen; ich antworte auf jede, sobald sie fertig ist."}, + "pt": {chat: "%s (sobre %s): %s", chatBare: "%s: %s", voice: "%s, sobre %s: %s", voiceBare: "%s, %s", ack: "%s, %s", + queue: "Tenho %d perguntas; respondo cada uma assim que estiver pronta."}, + "it": {chat: "%s (su %s): %s", chatBare: "%s: %s", voice: "%s, riguardo a %s: %s", voiceBare: "%s, %s", ack: "%s, %s", + queue: "Ho %d domande; rispondo a ciascuna appena è pronta."}, +} + +func attrFor(lang string) attrPhrases { + primary := strings.ToLower(strings.SplitN(strings.TrimSpace(lang), "-", 2)[0]) + if p, ok := attributions[primary]; ok { + return p + } + return attributions["en"] +} + +// displayName is the asker's name as written in an attribution: chat keeps +// the display name, a spoken one only the first name. Mention marks are +// dropped so the text never pings anyone. +func displayName(who string, spoken bool) string { + who = strings.Join(strings.Fields(strings.TrimLeft(strings.TrimSpace(who), "@#<")), " ") + who = strings.TrimRight(who, ">") + if spoken { + if f := strings.Fields(who); len(f) > 0 { + who = f[0] + } + } + return clip(who, maxNameChars) +} + +// topicFillers open or close a request without saying what it is about. +var topicFillers = map[string]bool{ + "many": true, "much": true, "me": true, "us": true, "please": true, "tell": true, "give": true, + "show": true, "list": true, "quick": true, "quickly": true, "a": true, "an": true, "the": true, + "of": true, "in": true, "on": true, "to": true, "for": true, "is": true, "are": true, "there": true, + "right": true, "now": true, "currently": true, "today": true, "again": true, "remind": true, + "explain": true, "check": true, "look": true, "up": true, "find": true, "out": true, "it": true, + "i": true, "we": true, "our": true, "my": true, "be": true, "was": true, "were": true, +} + +// recase gives lower's words the casing they have in original ("prs" in +// "Betty, what PRs are open?" becomes "PRs"): a trigger's Rest is +// lowercased. +func recase(original, lower string) string { + orig := map[string]string{} + for _, w := range strings.FieldsFunc(original, topicSep) { + w = strings.Trim(w, "'.") + if k := strings.ToLower(w); k != "" { + if _, ok := orig[k]; !ok { + orig[k] = w + } + } + } + out := strings.Fields(lower) + for i, w := range out { + if o, ok := orig[strings.Trim(w, "'.")]; ok { + out[i] = o + } + } + return strings.Join(out, " ") +} + +func topicSep(r rune) bool { return unicode.IsSpace(r) || strings.ContainsRune(",;:!?\"()[]", r) } + +// topicPhrase is a short phrase for what a request is about, from its +// words: leading and trailing question and filler words dropped, at most +// maxTopicWords words, in the casing the person used ("open PRs"). +func topicPhrase(text string) string { + raw := strings.FieldsFunc(text, topicSep) + filler := func(w string) bool { + l := strings.Trim(strings.ToLower(w), "'.") + return l == "" || interrogatives[l] || topicFillers[l] || stopwords[l] + } + i, j := 0, len(raw) + for i < j && filler(raw[i]) { + i++ + } + for j > i && filler(raw[j-1]) { + j-- + } + w := raw[i:j] + if len(w) > maxTopicWords { + w = w[:maxTopicWords] + for len(w) > 1 && filler(w[len(w)-1]) { + w = w[:len(w)-1] + } + } + return cleanTopic(strings.Join(w, " ")) +} + +// cleanTopic bounds a topic phrase (the model's or the heuristic's) and +// strips what must not be quoted: mention marks, quotes, newlines and +// trailing punctuation. +func cleanTopic(t string) string { + t = strings.Join(strings.Fields(strings.NewReplacer("@", "", "\"", "", "`", "").Replace(t)), " ") + if f := strings.Fields(t); len(f) > maxTopicWords+1 { + t = strings.Join(f[:maxTopicWords+1], " ") + } + t = strings.TrimRight(t, ".,;:!? ") + if utf8.RuneCountInString(t) > maxTopicChars { + t = strings.TrimSpace(string([]rune(t)[:maxTopicChars])) + } + return t +} + +// attribute opens text with who asked and what about, in the persona's +// language. +func attribute(lang string, voice bool, who, topic, text string) string { + p := attrFor(lang) + name := displayName(who, voice) + if name == "" { + return text + } + var prefix string + switch { + case voice && topic != "": + prefix = fmt.Sprintf(p.voice, name, topic, "") + case voice: + prefix = fmt.Sprintf(p.voiceBare, name, "") + case topic != "": + prefix = fmt.Sprintf(p.chat, name, topic, "") + default: + prefix = fmt.Sprintf(p.chatBare, name, "") + } + // The answer was clipped to its limit already; the prefix (at most a + // name and a few words) rides on top rather than cutting the answer. + return prefix + text +} + +// namedAck is an acknowledgement that names who it is for: "Andy, one +// moment." +func namedAck(lang, who, ack string) string { + name := displayName(who, true) + if name == "" { + return ack + } + return fmt.Sprintf(attrFor(lang).ack, name, lowerFirst(ack)) +} + +// queueNote is the "I have N questions" note. +func queueNote(lang string, n int) string { + return fmt.Sprintf(attrFor(lang).queue, n) +} + +// lowerFirst lowercases a fixed phrase's first letter after a name prefix +// ("Andy, one moment."), leaving "I" and acronyms alone. +func lowerFirst(s string) string { + r, size := utf8.DecodeRuneInString(s) + if size == 0 || !unicode.IsUpper(r) { + return s + } + next, _ := utf8.DecodeRuneInString(s[size:]) + if r == 'I' && (next == ' ' || next == '\'' || next == '’') { + return s + } + if unicode.IsUpper(next) || unicode.IsDigit(next) { + return s + } + return string(unicode.ToLower(r)) + s[size:] +} + +// requestTopic is the topic phrase of a pending request: the model's label +// when it gave one, else the request's own words. +func requestTopic(p *pending, model string) string { + if t := cleanTopic(model); t != "" { + return t + } + if p.thoughts { + return "" + } + if p.addressed { + return topicPhrase(recase(p.line.Text, p.trig.Rest)) + } + return topicPhrase(p.line.Text) +} + +// inFlight counts the requests she is working on, but not except: the +// addressed ones, and room questions whose model call is running. Only +// addressed counts the addressed ones alone (what the room sees as +// "questions to her"). +func (e *Engine) inFlight(except *pending, onlyAddressed bool) int { + n := 0 + for _, o := range e.pending { + if o == except || o.answered { + continue + } + if o.addressed || (!onlyAddressed && o.asking) { + n++ + } + } + return n +} + +// markOverlap flags p and every other request in flight as overlapped. +func (e *Engine) markOverlap(p *pending) { + for _, o := range e.pending { + if o == p || o.answered || !(o.addressed || o.asking) { + continue + } + o.overlapped, p.overlapped = true, true + } +} + +// needsAttribution reports whether p's answer must name who and what: +// another request overlapped it, the answer is late, or something else was +// said since it was asked (by someone else, or another answer of hers). +func (e *Engine) needsAttribution(p *pending, now time.Time) bool { + if p.overlapped || now.Sub(p.created) > attributeAfter || e.lastAnswerAt.After(p.created) { + return true + } + if e.inFlight(p, false) > 0 { + return true + } + for _, l := range e.recent { + if l.at.After(p.created) && !strings.EqualFold(l.Who, p.line.Who) { + return true + } + } + return false +} + +// attributed is text as said for p: with who and what in front when the +// order could be unclear, else as is. +func (e *Engine) attributed(p *pending, voice bool, modelTopic, text string, now time.Time) string { + if strings.TrimSpace(text) == "" || !e.needsAttribution(p, now) { + return text + } + return attribute(e.s.Persona.Language, voice, p.line.Who, requestTopic(p, modelTopic), text) +} + +// ackFor is the next acknowledgement, naming who it is for when other +// requests are in flight. +func (e *Engine) ackFor(who string) string { + a := e.ack() + if e.inFlight(nil, true) > 0 { + return namedAck(e.s.Persona.Language, who, a) + } + return a +} + +// noteQueue posts one short chat note when several requests are in hand, +// at most once per queueNoteGap. +func (e *Engine) noteQueue(now time.Time) { + n := e.inFlight(nil, true) + if n < minQueueNote || (!e.queueNoteAt.IsZero() && now.Sub(e.queueNoteAt) < queueNoteGap) { + return + } + if e.emit(Reply{Kind: KindChat, Text: queueNote(e.s.Persona.Language, n), Trigger: TriggerQueue}) { + e.queueNoteAt = now + } +} diff --git a/src/pkg/meetings/betty/attribution_test.go b/src/pkg/meetings/betty/attribution_test.go new file mode 100644 index 0000000000..8d4a59e237 --- /dev/null +++ b/src/pkg/meetings/betty/attribution_test.go @@ -0,0 +1,190 @@ +package betty + +import ( + "strings" + "testing" + "time" +) + +// gatedLLM answers each request once its topic's gate is released, with a +// model topic label, so a test controls which answer is ready first. +func gatedLLM(gates map[string]chan struct{}, topics map[string]string) *fakeLLM { + return &fakeLLM{reply: func(_, prompt string) (string, error) { + for key, gate := range gates { + if strings.Contains(prompt, "Question from") && strings.Contains(promptQuestion(prompt), key) { + <-gate + return `{"answer":"Answer about ` + key + `.","confidence":0.9,"sources":[1],"topic":"` + topics[key] + `"}`, nil + } + } + return `{"answer":"Other.","confidence":0.9}`, nil + }} +} + +// promptQuestion is the "Question from" line of a prompt. +func promptQuestion(prompt string) string { + i := strings.Index(prompt, "Question from") + if i < 0 { + return "" + } + rest := prompt[i:] + if j := strings.IndexByte(rest, '\n'); j >= 0 { + rest = rest[:j] + } + return rest +} + +// Two requests in flight: a single queue note, and each answer, whichever +// is ready first, names who asked and what about. +func TestConcurrentRequestsAreAttributed(t *testing.T) { + gates := map[string]chan struct{}{"release": make(chan struct{}), "deploy": make(chan struct{})} + r := newRig(t, testSettings(), gatedLLM(gates, map[string]string{"release": "release date", "deploy": "deploy window"})) + r.e.Observe(Line{Who: "Andy Anderson", Text: "Betty, when does the release ship?"}) + r.e.Observe(Line{Who: "Bo", Text: "Betty, what is the deploy window?"}) + if n := r.reply(); n.Trigger != TriggerQueue || n.Text != "I have 2 questions; answering each as it's ready." || n.Kind != KindChat { + t.Fatalf("queue note %+v", n) + } + // The second request is ready first. + close(gates["deploy"]) + if a := r.reply(); a.Text != "Bo (re: deploy window): Answer about deploy." { + t.Fatalf("first answer %q", a.Text) + } + close(gates["release"]) + if a := r.reply(); a.Text != "Andy Anderson (re: release date): Answer about release." { + t.Fatalf("second answer %q", a.Text) + } + // A third request within the minute gets no second note. + r.e.Observe(Line{Who: "Cy", Text: "Betty, what is the plan?"}) + r.e.Observe(Line{Who: "Di", Text: "Betty, what is the status?"}) + for range 2 { + if a := r.reply(); a.Trigger == TriggerQueue { + t.Fatalf("a second queue note within a minute: %+v", a) + } + } +} + +// One request answered at once stays short; one answered late names who +// and what even alone. +func TestSingleRequestPrefixOnlyWhenLate(t *testing.T) { + r := newRig(t, testSettings(), nil) + r.llm = &fakeLLM{reply: func(string, string) (string, error) { + r.clk.add(attributeAfter + time.Second) + return `{"answer":"It ships Friday.","confidence":0.9}`, nil + }} + r.e.d.LLM = r.llm + r.e.Observe(Line{Who: "Bo", Text: "Betty, when does the release ship?"}) + if a := r.reply(); a.Text != "Bo (re: release ship): It ships Friday." { + t.Fatalf("late answer %q", a.Text) + } +} + +// Spoken requests: the ack names the asker once another request is in +// flight, and the spoken answer names them by first name. +func TestSpokenAcksAndAnswersNameTheAsker(t *testing.T) { + gates := map[string]chan struct{}{"release": make(chan struct{}), "deploy": make(chan struct{})} + r := newRig(t, testSettings(), gatedLLM(gates, map[string]string{"release": "the release", "deploy": "the deploy"})) + r.e.Observe(Line{Who: "Andy Anderson", Text: "Betty, when does the release ship?", Spoken: true}) + if a := r.reply(); a.Trigger != TriggerAck || strings.Contains(a.Text, "Andy") { + t.Fatalf("first ack %+v", a) + } + r.e.Observe(Line{Who: "Bo Li", Text: "Betty, what is the deploy window?", Spoken: true}) + if a := r.reply(); a.Trigger != TriggerAck || !strings.HasPrefix(a.Text, "Bo, ") { + t.Fatalf("second ack %+v", a) + } + if n := r.reply(); n.Trigger != TriggerQueue { + t.Fatalf("queue note %+v", n) + } + close(gates["release"]) + if a := r.reply(); a.Kind != KindVoice || a.Text != "Andy, about the release: Answer about release." { + t.Fatalf("spoken answer %+v", a) + } + close(gates["deploy"]) + if a := r.reply(); a.Text != "Bo, about the deploy: Answer about deploy." { + t.Fatalf("spoken answer %+v", a) + } +} + +// Another person speaking between the request and its answer is enough to +// name who and what. +func TestAnswerAfterOtherLinesIsAttributed(t *testing.T) { + gate := map[string]chan struct{}{"release": make(chan struct{})} + r := newRig(t, testSettings(), gatedLLM(gate, map[string]string{"release": ""})) + r.e.Observe(Line{Who: "Bo", Text: "Betty, what PRs touch the release?"}) + waitCalls(t, r.llm, 1) + r.clk.add(time.Second) + r.e.Observe(Line{Who: "Ana", Text: "meanwhile the build is green"}) + time.Sleep(50 * time.Millisecond) + close(gate["release"]) + // No model topic: the request's own words, in the casing used. + if a := r.reply(); a.Text != "Bo (re: PRs touch the release): Answer about release." { + t.Fatalf("answer %q", a.Text) + } +} + +func waitCalls(t *testing.T, f *fakeLLM, n int) { + t.Helper() + deadline := time.Now().Add(5 * time.Second) + for f.n() < n { + if time.Now().After(deadline) { + t.Fatalf("model called %d times, want %d", f.n(), n) + } + time.Sleep(5 * time.Millisecond) + } +} + +func TestTopicPhrase(t *testing.T) { + for in, want := range map[string]string{ + "how many open issues are there?": "open issues", + "what PRs are open in the hive repo": "PRs are open", + "can you tell me the deploy window": "deploy window", + "what's blocking the release on Friday": "blocking the release on Friday", + "what?": "", + "give us a quick summary of the cert rotation": "summary of the cert rotation", + } { + if got := topicPhrase(in); got != want { + t.Errorf("topicPhrase(%q) = %q, want %q", in, got, want) + } + } + if got := recase("Betty, what PRs are open?", "what prs are open"); got != "what PRs are open" { + t.Fatalf("recase = %q", got) + } + if got := cleanTopic(`"@everyone open PRs today and tomorrow and more."`); got != "everyone open PRs today and tomorrow" { + t.Fatalf("cleanTopic = %q", got) + } +} + +func TestDisplayNameNeverMentions(t *testing.T) { + for _, c := range []struct { + who string + spoken bool + want string + }{ + {"Andy Anderson", false, "Andy Anderson"}, + {"Andy Anderson", true, "Andy"}, + {"@Andy", false, "Andy"}, + {"<@1234>", false, "1234"}, + {" ", false, ""}, + } { + if got := displayName(c.who, c.spoken); got != c.want { + t.Errorf("displayName(%q, %v) = %q, want %q", c.who, c.spoken, got, c.want) + } + } + if got := attribute("en", false, "", "x", "Hello."); got != "Hello." { + t.Fatalf("no name: %q", got) + } + if got := attribute("de", true, "Andy A", "", "Ja."); got != "Andy, Ja." { + t.Fatalf("voice bare: %q", got) + } + if got := namedAck("en", "Bo Li", "One moment."); got != "Bo, one moment." { + t.Fatalf("named ack %q", got) + } + if got := namedAck("en", "Bo", "I see."); got != "Bo, I see." { + t.Fatalf("named ack keeps I: %q", got) + } +} + +func TestParseAnswerTopic(t *testing.T) { + a, err := ParseAnswer(`{"answer":"x","confidence":1,"topic":" open PRs. "}`) + if err != nil || a.Topic != "open PRs" { + t.Fatalf("%+v %v", a, err) + } +} diff --git a/src/pkg/meetings/betty/betty.go b/src/pkg/meetings/betty/betty.go index 4ba6f58fab..c469bb1575 100644 --- a/src/pkg/meetings/betty/betty.go +++ b/src/pkg/meetings/betty/betty.go @@ -204,6 +204,10 @@ type Engine struct { // earlyAck is when each speaker was acknowledged from a partial // transcript; their final line then gets no second ack. earlyAck map[string]time.Time + // queueNoteAt is when the last "I have N questions" note went out; + // lastAnswerAt is when she last answered anything (attribution.go). + queueNoteAt time.Time + lastAnswerAt time.Time } type timedLine struct { @@ -419,6 +423,9 @@ func (e *Engine) emit(r Reply) bool { } select { case e.out <- r: + if r.Trigger != TriggerAck && r.Trigger != TriggerQueue { + e.lastAnswerAt = e.d.Now() + } return true default: e.drop("reply_backlog") diff --git a/src/pkg/meetings/betty/persona.go b/src/pkg/meetings/betty/persona.go index 46baf09fe7..0ff420139f 100644 --- a/src/pkg/meetings/betty/persona.go +++ b/src/pkg/meetings/betty/persona.go @@ -106,7 +106,7 @@ func answerPrompt(mode, who, question string, snips []Snippet, recent []recentLi } } b.WriteString(dataClose + "\n\n") - b.WriteString(`Reply with only a JSON object: {"answer": "", "confidence": <0 to 1>, "needs_research": , "sources": [""]}`) + b.WriteString(`Reply with only a JSON object: {"answer": "", "confidence": <0 to 1>, "needs_research": , "sources": [""], "topic": "<2 to 5 words naming what was asked, such as open PRs>"}`) return b.String() } diff --git a/src/pkg/meetings/betty/queue.go b/src/pkg/meetings/betty/queue.go index dfc68f2dcb..b34734c6a6 100644 --- a/src/pkg/meetings/betty/queue.go +++ b/src/pkg/meetings/betty/queue.go @@ -45,6 +45,9 @@ type pending struct { snips []Snippet // thoughts: a spoken request with no question. thoughts bool + // overlapped: another request was in flight at the same time, so its + // answer names who and what (attribution.go). + overlapped bool } type retrieval struct { @@ -157,10 +160,11 @@ func (e *Engine) addressed(l Line, trig Trigger, now time.Time) { } if trig.VoiceWanted && e.d.LLM != nil && !e.takeEarlyAck(l.Who, now) { // Speak at once so the room knows she heard; the answer follows. - e.emit(Reply{Kind: KindVoice, Text: e.ack(), Trigger: TriggerAck, Question: l.Text, VoiceWanted: true}) + e.emit(Reply{Kind: KindVoice, Text: e.ackFor(l.Who), Trigger: TriggerAck, Question: l.Text, VoiceWanted: true}) } p := e.add(l, trig, TopicKey(trig.Rest), true, now) p.waited, p.thoughts = true, thoughts + e.noteQueue(now) e.retrieve(p, trig.Rest) } @@ -198,7 +202,7 @@ func (e *Engine) onPartial(l Line) { if at, ok := e.earlyAck[l.Who]; ok && now.Sub(at) < earlyAckWindow { return } - if e.emit(Reply{Kind: KindVoice, Text: e.ack(), Trigger: TriggerAck, Question: l.Text, VoiceWanted: true}) { + if e.emit(Reply{Kind: KindVoice, Text: e.ackFor(l.Who), Trigger: TriggerAck, Question: l.Text, VoiceWanted: true}) { e.earlyAck[l.Who] = now } } @@ -268,6 +272,9 @@ func (e *Engine) add(l Line, trig Trigger, topic []string, addressed bool, now t e.nextID++ p := &pending{id: e.nextID, line: l, trig: trig, topic: topic, addressed: addressed, created: now} e.pending[p.id] = p + if addressed { + e.markOverlap(p) + } return p } @@ -352,11 +359,15 @@ func (e *Engine) proceed(p *pending) { return } if e.d.LLM == nil { - e.emit(Reply{Kind: replyKind(p.trig), Text: fallbackNoModel, Trigger: TriggerAddressed, Question: p.line.Text, VoiceWanted: p.trig.VoiceWanted}) + text := e.attributed(p, p.trig.VoiceWanted, "", fallbackNoModel, now) + e.emit(Reply{Kind: replyKind(p.trig), Text: text, Trigger: TriggerAddressed, Question: p.line.Text, VoiceWanted: p.trig.VoiceWanted}) e.finish(p, "") return } p.asking = true + if !p.addressed { + e.markOverlap(p) + } mode, timeout := modeQuestion, e.s.UnpromptedTimeout var recent []recentLine if p.addressed { @@ -399,7 +410,8 @@ func (e *Engine) onAnswer(p *pending, a answered) { e.finish(p, reason) return } - if e.emit(Reply{Text: text, Trigger: TriggerQuestion, Question: p.line.Text, NeedsResearch: a.ans.NeedsResearch, Sources: a.ans.Sources}) { + said := e.attributed(p, false, a.ans.Topic, text, now) + if e.emit(Reply{Text: said, Trigger: TriggerQuestion, Question: p.line.Text, NeedsResearch: a.ans.NeedsResearch, Sources: a.ans.Sources}) { e.posts = append(e.posts, now) e.lastPost = now e.total++ @@ -461,6 +473,7 @@ func (e *Engine) onAddressedAnswer(p *pending, a answered) { rep.NeedsResearch = true rep.Research = clip(researchQuery(p), maxResearchQuery) } + rep.Text = e.attributed(p, p.trig.VoiceWanted, a.ans.Topic, rep.Text, e.d.Now()) e.emit(rep) e.rememberTopic(p.topic) e.rememberTopic(TopicKey(text)) diff --git a/src/pkg/meetings/live/attribution_test.go b/src/pkg/meetings/live/attribution_test.go new file mode 100644 index 0000000000..2c1369a128 --- /dev/null +++ b/src/pkg/meetings/live/attribution_test.go @@ -0,0 +1,48 @@ +package live + +import ( + "context" + "strings" + "testing" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/meetings" +) + +// heldLLM answers the release question only once release is closed, so a +// second request overlaps it. +type heldLLM struct { + personaLLM + release chan struct{} +} + +func (h *heldLLM) Complete(ctx context.Context, system, prompt string) (string, error) { + if strings.Contains(prompt, "Question from Bo") { + select { + case <-h.release: + case <-ctx.Done(): + return "", ctx.Err() + } + } + return h.personaLLM.Complete(ctx, system, prompt) +} + +// Overlapping requests (hivecommons/hive#18590): one queue note in chat, +// not charged, and each answer names who asked. +func TestOverlappingRequestsQueueNoteAndAttribution(t *testing.T) { + llm := &heldLLM{release: make(chan struct{})} + h := personaHarness(t, Policy{ChatPostsPerMinute: 20}, config.MeetingsPersonaConfig{}, llm) + h.live() + h.line(meetings.EventChat, "Bo", "Betty, when does the release ship?", false) + h.line(meetings.EventChat, "Cy", "Betty, who owns the deploy pipeline?", false) + h.eventually("queue note", h.posted("I have 2 questions; answering each as it's ready.")) + h.eventually("second request answered first", h.posted("Cy (re: ")) + close(llm.release) + h.eventually("first request answered", h.posted("Bo (re: ")) + if n := h.countPosts("I have 2 questions"); n != 1 { + t.Fatalf("queue notes %d, want 1", n) + } + if st := h.state(); st.PersonaReplies != 2 { + t.Fatalf("persona replies %d: the note is not an answer", st.PersonaReplies) + } +} diff --git a/src/pkg/meetings/live/session.go b/src/pkg/meetings/live/session.go index 0b0295b266..13caa30458 100644 --- a/src/pkg/meetings/live/session.go +++ b/src/pkg/meetings/live/session.go @@ -746,6 +746,10 @@ func (s *session) onPersonaReply(ctx context.Context, rep betty.Reply) { if text == "" { return } + if rep.Trigger == betty.TriggerQueue { + s.onQueueNote(ctx, rep, text) + return + } switch rep.Trigger { case betty.TriggerAck: if s.state.BudgetExhausted { @@ -791,6 +795,17 @@ func (s *session) onPersonaReply(ctx context.Context, rep betty.Reply) { s.save() } +// onQueueNote posts the persona's "I have N questions" note: chat only, +// through the rate limit, never charged (no model call made it). +func (s *session) onQueueNote(ctx context.Context, rep betty.Reply, text string) { + if s.state.BudgetExhausted { + return + } + if s.post(ctx, text) { + s.emitPersona(rep) + } +} + func (s *session) emitPersona(rep betty.Reply) { raw, _ := json.Marshal(rep) s.emit(Event{Kind: EventPersona, Reason: rep.Trigger, Intent: raw}) From c565604369760080eb8a9fc9c65072a16d0eb76e Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 16 Nov 2026 22:44:36 -0500 Subject: [PATCH 08/13] =?UTF-8?q?=E2=9C=A8=20Betty=20speaks=20ready=20answ?= =?UTF-8?q?ers=20shortest=20first=20and=20moves=20a=20long=20voice=20queue?= =?UTF-8?q?=20to=20chat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Quorum's say queues clips and plays them in order, so hive keeps an estimate of the queued speech. Spoken answers ready in the same loop turn are said back to back, shortest first; one that would wait behind more than 20 s goes to chat with one spoken "I've put the rest in chat"; an ack behind more than 8 s of speech is skipped. Answers to addressed requests may take meetings.chat_answer_burst (3) extra chat posts a minute past the chat rate. Part of #18590. Signed-off-by: Andy Anderson --- changelog.d/added-18590-betty-voice-queue.md | 1 + src/cmd/hive/meetingswire.go | 1 + src/docs/meetings.md | 4 +- src/pkg/config/meetings_config.go | 16 ++ src/pkg/meetings/betty/overflow.go | 29 ++++ src/pkg/meetings/live/policy.go | 15 +- src/pkg/meetings/live/session.go | 19 ++- src/pkg/meetings/live/voicequeue.go | 166 +++++++++++++++++++ src/pkg/meetings/live/voicequeue_test.go | 130 +++++++++++++++ 9 files changed, 375 insertions(+), 6 deletions(-) create mode 100644 changelog.d/added-18590-betty-voice-queue.md create mode 100644 src/pkg/meetings/betty/overflow.go create mode 100644 src/pkg/meetings/live/voicequeue.go create mode 100644 src/pkg/meetings/live/voicequeue_test.go diff --git a/changelog.d/added-18590-betty-voice-queue.md b/changelog.d/added-18590-betty-voice-queue.md new file mode 100644 index 0000000000..c208d4f9ef --- /dev/null +++ b/changelog.d/added-18590-betty-voice-queue.md @@ -0,0 +1 @@ +- **Betty speaks several ready answers back to back, shortest first, and moves the rest to chat.** Spoken answers ready together no longer wait behind a long one; an answer that would wait behind more than 20 s of queued speech goes to chat with a spoken "I've put the rest in chat.", and a stale acknowledgement is skipped. Answers to addressed requests may take `meetings.chat_answer_burst` (default 3) extra chat posts a minute beyond `meetings.chat_posts_per_minute`, so parallel answers are not held for a minute. ([#18590](https://github.com/hivecommons/hive/issues/18590)) diff --git a/src/cmd/hive/meetingswire.go b/src/cmd/hive/meetingswire.go index 6810d8826c..571c42905b 100644 --- a/src/cmd/hive/meetingswire.go +++ b/src/cmd/hive/meetingswire.go @@ -229,6 +229,7 @@ func wireMeetingsLive(cfg *config.Config, mgr *meetings.Manager, store *meetings Notes: notes, Policy: live.Policy{ ChatPostsPerMinute: m.ChatPostsPerMinuteOrDefault(), + ChatAnswerBurst: m.ChatAnswerBurstOrDefault(), ResearchPerMeeting: m.ResearchPerMeetingOrDefault(), VoiceOnAddress: m.VoiceOnAddressEnabled(), MaxTokensPerMeeting: m.Budget.MaxTokensPerMeetingOrDefault(), diff --git a/src/docs/meetings.md b/src/docs/meetings.md index 5ca4953a3d..49922a21eb 100644 --- a/src/docs/meetings.md +++ b/src/docs/meetings.md @@ -40,7 +40,8 @@ Hive attends meetings as a persona: **Betty** ("Betty Bee from the hive", she/he - **Questions about the hive itself.** "How many open issues are there?", "what PRs are open in the hive repo?", "which agents are working?" are answered from the hive's live dashboard state (the same open, actionable, held and blocked counts, agents and open PRs the Overview and the dashboard chat use), read in-process, so the answer comes in seconds and never goes to research. Such a question names the hive (or "the repo") or asks for open issues, open PRs, actionable items or recent merges. - **Deep dives.** When an addressed question needs research (the model sets `needs_research`, or its confidence is below `min_confidence`) and the meeting has a research path, she says "I'll dig into that and report back", starts a research item through the same pool, cap and approval as Spek's research (shown in the panel as `betty-N`, dispatched with `betty=1` in its meta), and gives exactly one follow-up in her own voice when it completes (the summary as is if restyling fails), in the channel she was asked in: spoken for a spoken question when the meeting can hear her, else in chat. She always closes the loop: if the research cannot start (for example "no research agent is available on this hive"), the agent reports an error, or it is still running after 5 minutes, she says "I couldn't look that up: ."; one that runs past 30 seconds gets one "still looking" note first. - **Room questions.** Betty also answers questions people ask the room (a line ending in "?" or starting with "what", "how", "who" and the like, four words or more), but only if nobody answers within `answer_wait_s` (8 seconds), the knowledge sources had a hit, the model is at least `min_confidence` (0.75) sure, and the topic was not already answered. She never posts general commentary. -- **Caps.** Unprompted answers are capped at `per_10m` (2 per ten minutes), at least `min_gap_s` (90 seconds) apart and `per_meeting` (12) per meeting, with one reply per question, at most `queue_depth` (3) questions waiting and none older than `stale_s` (60 seconds). Addressed requests skip these caps but still go through the chat rate limit (`meetings.chat_posts_per_minute`) and the meeting budget. Chat answers are at most 3 sentences and 300 characters. +- **Caps.** Unprompted answers are capped at `per_10m` (2 per ten minutes), at least `min_gap_s` (90 seconds) apart and `per_meeting` (12) per meeting, with one reply per question, at most `queue_depth` (3) questions waiting and none older than `stale_s` (60 seconds). Addressed requests skip these caps but still go through the chat rate limit (`meetings.chat_posts_per_minute`) and the meeting budget; once that rate is spent, answers to addressed requests may take `meetings.chat_answer_burst` (3) more posts a minute, so answers worked on in parallel are not held for a minute. +- **Several spoken answers.** Quorum queues each spoken clip and plays them in order, so she never talks over herself; hive estimates the queued speech (about 15 characters a second). Spoken answers that are ready together are said back to back, shortest first. An answer that would wait behind more than 20 s of queued speech is posted to chat instead, and she says "I've put the rest in chat." (at most once every 30 s). An acknowledgement that would play only after more than 8 s of earlier speech is skipped. Chat answers are at most 3 sentences and 300 characters. - **Spek.** With Betty on, Spek's spoken replies are dropped, Spek's chat posts go through Betty (posted unless she already covered the topic, so the meeting never gets two answers), and Spek's clarifying questions are reworded in Betty's voice with one quick model call (800 ms; on failure or timeout the original text is posted). The rewording runs beside the meeting loop, so the transcript, notes and other replies keep flowing; Spek's later questions and chat posts wait their turn so they still appear in Spek's order. Notes and research are unchanged. Lines addressed to Betty and leave phrases never count as answers to Spek's questions. - **Manners.** Plain, brief language; at most one light touch of humor, never about a person and none when the conversation sounds tense. Set `meetings.persona.humor: off` to turn humor off. Meeting speech, knowledge and work items reach the model as data, never as instructions. She never claims to have done something she did not do. - **Model.** Betty answers with the meetings model through the same path as `hive meeting-llm` (an OpenAI-compatible endpoint, charged to the meeting's usage ledger and budget). On a hive with only an Anthropic key (`HIVE_MEETINGS_ANTHROPIC_API_KEY` or `meetings.llm.anthropic_key_file`) she calls the Anthropic Messages API directly with `meetings.llm.model` (default `claude-haiku-5-5`), with the same timeouts, in-flight limit and usage ledger, so her tokens count toward the meeting's budget. With no model she still introduces herself but says she cannot answer questions. With `meetings.llm.mode: agent-backend` (or `claude-cli`) she answers through one of the hive's agent backends instead; see [Agent backends](#agent-backends). @@ -313,6 +314,7 @@ Every key has an environment override that wins over `hive.yaml`. The full list | `meetings.research_pool_size` | `2` | Research agents (`research-1..N`). | | `meetings.research_per_meeting` | `5` | Research items per meeting before approval is needed. | | `meetings.chat_posts_per_minute` | `6` | Chat post rate limit per meeting. | +| `meetings.chat_answer_burst` | `3` | Extra chat posts a minute for Betty's answers to requests addressed to her, once the rate limit is spent (`HIVE_MEETINGS_CHAT_ANSWER_BURST`). | | `meetings.lobby_timeout_s` | `600` | Seconds to wait in a lobby. | | `meetings.budget.max_tokens_per_meeting` | `200000` | LLM tokens per meeting (when Spek reports usage). | | `meetings.budget.max_tokens_per_hour` | `500000` | Hive-wide hourly ceiling. | diff --git a/src/pkg/config/meetings_config.go b/src/pkg/config/meetings_config.go index 2de0614318..372686f329 100644 --- a/src/pkg/config/meetings_config.go +++ b/src/pkg/config/meetings_config.go @@ -54,6 +54,9 @@ const ( // DefaultMeetingsChatPostsPerMinute caps hive's chat posts into one // meeting. DefaultMeetingsChatPostsPerMinute = 6 + // DefaultMeetingsChatAnswerBurst is the extra posts a minute addressed + // answers may take. + DefaultMeetingsChatAnswerBurst = 3 // DefaultMeetingsLobbyTimeoutS is how long hive waits in a meeting // lobby before it gives up and leaves. DefaultMeetingsLobbyTimeoutS = 600 @@ -128,6 +131,7 @@ const ( MeetingsResearchPoolSizeEnv = "HIVE_MEETINGS_RESEARCH_POOL_SIZE" MeetingsResearchPerMeetingEnv = "HIVE_MEETINGS_RESEARCH_PER_MEETING" MeetingsChatPostsPerMinuteEnv = "HIVE_MEETINGS_CHAT_POSTS_PER_MINUTE" + MeetingsChatAnswerBurstEnv = "HIVE_MEETINGS_CHAT_ANSWER_BURST" MeetingsLobbyTimeoutEnv = "HIVE_MEETINGS_LOBBY_TIMEOUT_S" MeetingsRetentionDaysEnv = "HIVE_MEETINGS_RETENTION_DAYS" MeetingsConsentTextEnv = "HIVE_MEETINGS_CONSENT_TEXT" @@ -182,6 +186,11 @@ type MeetingsConfig struct { // ChatPostsPerMinute caps hive's chat posts into one meeting. Zero or // negative means the default. ChatPostsPerMinute int `yaml:"chat_posts_per_minute,omitempty" json:"chat_posts_per_minute,omitempty"` + // ChatAnswerBurst is how many extra chat posts a minute the persona's + // answers to requests addressed to her may take beyond + // ChatPostsPerMinute, so parallel answers are not held for a minute + // (#18590). Zero or negative means the default. + ChatAnswerBurst int `yaml:"chat_answer_burst,omitempty" json:"chat_answer_burst,omitempty"` // LobbyTimeoutS is how long hive waits in a lobby. Zero or negative // means the default. LobbyTimeoutS int `yaml:"lobby_timeout_s,omitempty" json:"lobby_timeout_s,omitempty"` @@ -595,6 +604,12 @@ func (m MeetingsConfig) ChatPostsPerMinuteOrDefault() int { return meetingsEnvInt(MeetingsChatPostsPerMinuteEnv, m.ChatPostsPerMinute, DefaultMeetingsChatPostsPerMinute) } +// ChatAnswerBurstOrDefault returns the extra chat posts a minute addressed +// persona answers may take. +func (m MeetingsConfig) ChatAnswerBurstOrDefault() int { + return meetingsEnvInt(MeetingsChatAnswerBurstEnv, m.ChatAnswerBurst, DefaultMeetingsChatAnswerBurst) +} + // LobbyTimeoutSOrDefault returns the lobby timeout in seconds. func (m MeetingsConfig) LobbyTimeoutSOrDefault() int { return meetingsEnvInt(MeetingsLobbyTimeoutEnv, m.LobbyTimeoutS, DefaultMeetingsLobbyTimeoutS) @@ -756,6 +771,7 @@ func (m MeetingsConfig) Validate() error { {"meetings.research_pool_size", m.ResearchPoolSize}, {"meetings.research_per_meeting", m.ResearchPerMeeting}, {"meetings.chat_posts_per_minute", m.ChatPostsPerMinute}, + {"meetings.chat_answer_burst", m.ChatAnswerBurst}, {"meetings.lobby_timeout_s", m.LobbyTimeoutS}, {"meetings.retention_days", m.RetentionDays}, {"meetings.budget.max_tokens_per_meeting", m.Budget.MaxTokensPerMeeting}, diff --git a/src/pkg/meetings/betty/overflow.go b/src/pkg/meetings/betty/overflow.go new file mode 100644 index 0000000000..1f8b7df428 --- /dev/null +++ b/src/pkg/meetings/betty/overflow.go @@ -0,0 +1,29 @@ +package betty + +import "strings" + +// restInChat is what she says when her spoken answers would queue too long +// and the rest went to chat (hivecommons/hive#18590), by primary language +// subtag; English is the fallback. +var restInChat = map[string]string{ + "en": "I've put the rest in chat.", + "es": "El resto lo dejé en el chat.", + "fr": "J'ai mis la suite dans le chat.", + "de": "Den Rest habe ich in den Chat geschrieben.", + "pt": "Coloquei o resto no chat.", + "it": "Il resto l'ho scritto in chat.", +} + +// RestInChatText is the spoken note for answers moved to chat, in the +// persona's language. +func (e *Engine) RestInChatText() string { + if t, ok := restInChat[primaryTag(e.s.Persona.Language)]; ok { + return t + } + return restInChat["en"] +} + +// primaryTag is a BCP-47 tag's primary language subtag ("en-GB" is "en"). +func primaryTag(lang string) string { + return strings.ToLower(strings.SplitN(strings.TrimSpace(lang), "-", 2)[0]) +} diff --git a/src/pkg/meetings/live/policy.go b/src/pkg/meetings/live/policy.go index 247b58845a..890fed7d13 100644 --- a/src/pkg/meetings/live/policy.go +++ b/src/pkg/meetings/live/policy.go @@ -24,7 +24,11 @@ const ( // Policy is the per-meeting policy configuration. type Policy struct { - ChatPostsPerMinute int + ChatPostsPerMinute int + // ChatAnswerBurst is the extra chat posts a minute the persona's + // answers to addressed requests may take once ChatPostsPerMinute is + // spent; zero means none. + ChatAnswerBurst int ResearchPerMeeting int VoiceOnAddress bool MaxTokensPerMeeting int @@ -32,6 +36,15 @@ type Policy struct { AnswerWindow time.Duration } +// answerBurst is the extra chat room for addressed answers: none when the +// chat rate is unlimited or no burst is set. +func answerBurst(p Policy) rateLimiter { + if p.ChatPostsPerMinute <= 0 || p.ChatAnswerBurst <= 0 { + return rateLimiter{} + } + return rateLimiter{n: p.ChatAnswerBurst, window: rateWindow} +} + func (p Policy) withDefaults() Policy { if p.AnswerWindow <= 0 { p.AnswerWindow = DefaultAnswerWindow diff --git a/src/pkg/meetings/live/session.go b/src/pkg/meetings/live/session.go index 13caa30458..b29700fa09 100644 --- a/src/pkg/meetings/live/session.go +++ b/src/pkg/meetings/live/session.go @@ -82,6 +82,10 @@ type session struct { // nothing more is forwarded to it, so a meeting without live mode does // not log a warning per transcript line (hivecommons/hive#18630). spekGone bool + // burst is the extra chat room for answers to addressed requests; + // voice is the estimate of queued speech (voicequeue.go). + burst rateLimiter + voice voiceQueue // persona is the meeting's attendee persona, nil when it is off. With // it on, it owns the bot's voice in the meeting: Spek's say intents are // dropped, its chat posts are offered to the persona, and its @@ -108,6 +112,7 @@ func (r *Runner) newSession(m meetings.Meeting, spek Spek) *session { s := &session{ r: r, id: m.ID, spek: spek, rate: rateLimiter{n: r.opts.Policy.ChatPostsPerMinute, window: rateWindow}, + burst: answerBurst(r.opts.Policy), updates: make(chan meetings.Update, sessionInbox), signals: make(chan ResearchSignal, sessionInbox), approves: make(chan approveReq), @@ -198,6 +203,7 @@ func (s *session) run(ctx context.Context) { case rep := <-replies: if !ending { s.onPersonaReply(ctx, rep) + s.drainPersona(ctx, replies) } case <-ctx.Done(): s.spek.Close(nil) @@ -755,7 +761,7 @@ func (s *session) onPersonaReply(ctx context.Context, rep betty.Reply) { if s.state.BudgetExhausted { return } - if s.speak(ctx, text) { + if s.speakAck(ctx, text) { s.emitPersona(rep) } else { s.drop("ack_no_voice") @@ -780,9 +786,14 @@ func (s *session) onPersonaReply(ctx context.Context, rep betty.Reply) { return } posted := true - if rep.Kind == betty.KindVoice { - s.say(ctx, text) - } else { + switch { + case rep.Kind == betty.KindVoice: + // Spoken with the other answers ready now, shortest first + // (flushVoice), falling back to chat. + s.queueVoice(text) + case rep.Trigger == betty.TriggerAddressed: + posted = s.postAnswer(ctx, text) + default: posted = s.post(ctx, text) } if posted { diff --git a/src/pkg/meetings/live/voicequeue.go b/src/pkg/meetings/live/voicequeue.go new file mode 100644 index 0000000000..25ae2e318c --- /dev/null +++ b/src/pkg/meetings/live/voicequeue.go @@ -0,0 +1,166 @@ +package live + +import ( + "context" + "sort" + "time" + "unicode/utf8" + + "github.com/hivecommons/hive/pkg/meetings/betty" +) + +// Spoken answers when several are ready (hivecommons/hive#18590). Quorum's +// say returns once the clip is queued and plays clips in call order, so +// she never talks over herself, but hive cannot see when she stops +// talking. The session keeps an estimate of the queued speech instead and +// uses it to: +// +// - speak answers that are ready together back to back, shortest first, +// so the quick ones are not held behind a long one; +// - move answers to chat once the queued speech would pass +// voiceBacklogMax, with one spoken "I've put the rest in chat"; +// - skip an acknowledgement that would only play after seconds of +// earlier speech, when it no longer acknowledges anything promptly. +const ( + // voiceCharsPerSecond is the speaking rate the estimate assumes, about + // 160 words a minute. + voiceCharsPerSecond = 15 + // voiceMinClip is the least a clip is assumed to take (synthesis and + // the pauses around it). + voiceMinClip = time.Second + // voiceBacklogMax is the most queued speech an answer may wait behind; + // later answers go to chat. + voiceBacklogMax = 20 * time.Second + // ackMaxBacklog is the most queued speech an acknowledgement may wait + // behind: past it the ack would land seconds after the request, about + // when the answer itself is due. + ackMaxBacklog = 8 * time.Second + // restInChatGap is the least time between two "rest in chat" notes. + restInChatGap = 30 * time.Second + // maxReplyDrain bounds the ready replies one turn of the session loop + // takes together. + maxReplyDrain = 16 +) + +// speechTime estimates how long text takes to say. +func speechTime(text string) time.Duration { + d := time.Duration(utf8.RuneCountInString(text)) * time.Second / voiceCharsPerSecond + return max(d, voiceMinClip) +} + +// voiceQueue is the session's estimate of its queued speech. +type voiceQueue struct { + busyUntil time.Time + // restNoted is when "the rest is in chat" was last said. + restNoted time.Time + // pending are spoken answers ready this turn, flushed together. + pending []voiceAnswer +} + +// voiceAnswer is one spoken answer waiting for the flush. +type voiceAnswer struct { + text string + seq int +} + +// backlog is the speech still queued at now. +func (v *voiceQueue) backlog(now time.Time) time.Duration { + if v.busyUntil.After(now) { + return v.busyUntil.Sub(now) + } + return 0 +} + +// queued records a clip handed to the meeting at now. +func (v *voiceQueue) queued(now time.Time, text string) { + start := now + if v.busyUntil.After(now) { + start = v.busyUntil + } + v.busyUntil = start.Add(speechTime(text)) +} + +// drainPersona applies every further reply that is ready now, then speaks +// the spoken answers they produced together. +func (s *session) drainPersona(ctx context.Context, replies <-chan betty.Reply) { + defer s.flushVoice(ctx) + for range maxReplyDrain { + select { + case rep := <-replies: + s.onPersonaReply(ctx, rep) + default: + return + } + } +} + +// queueVoice holds a spoken answer for the flush. +func (s *session) queueVoice(text string) { + s.voice.pending = append(s.voice.pending, voiceAnswer{text: text, seq: len(s.voice.pending)}) +} + +// flushVoice speaks the ready answers shortest first; once the queued +// speech would pass voiceBacklogMax the rest go to chat, with one spoken +// note saying so. An answer that cannot be spoken falls back to chat as +// before. +func (s *session) flushVoice(ctx context.Context) { + batch := s.voice.pending + s.voice.pending = nil + if len(batch) == 0 { + return + } + sort.SliceStable(batch, func(i, j int) bool { + return utf8.RuneCountInString(batch[i].text) < utf8.RuneCountInString(batch[j].text) + }) + moved := false + for _, a := range batch { + now := s.now() + if b := s.voice.backlog(now); b > 0 && b+speechTime(a.text) > voiceBacklogMax { + s.postAnswer(ctx, a.text) + moved = true + continue + } + if s.speak(ctx, a.text) { + s.voice.queued(now, a.text) + continue + } + s.log().Info("[meetings] voice unavailable; answering in chat") + s.postAnswer(ctx, a.text) + } + if moved && s.persona != nil { + now := s.now() + if s.voice.restNoted.IsZero() || now.Sub(s.voice.restNoted) >= restInChatGap { + note := s.persona.RestInChatText() + if s.speak(ctx, note) { + s.voice.queued(now, note) + s.voice.restNoted = now + } + } + } +} + +// speakAck says an acknowledgement unless it would wait behind seconds of +// earlier speech; it reports whether it was spoken. +func (s *session) speakAck(ctx context.Context, text string) bool { + now := s.now() + if s.voice.backlog(now) > ackMaxBacklog { + return false + } + if !s.speak(ctx, text) { + return false + } + s.voice.queued(now, text) + return true +} + +// postAnswer posts one of the persona's answers to an addressed request: +// through the chat rate limit, and once that is spent through the small +// answer burst, so parallel answers are not held for a minute. +func (s *session) postAnswer(ctx context.Context, text string) bool { + now := s.now() + if !s.rate.allow(now) && (s.burst.n <= 0 || !s.burst.allow(now)) { + s.drop("rate_limited") + return false + } + return s.deliver(ctx, text) +} diff --git a/src/pkg/meetings/live/voicequeue_test.go b/src/pkg/meetings/live/voicequeue_test.go new file mode 100644 index 0000000000..f1d214b30a --- /dev/null +++ b/src/pkg/meetings/live/voicequeue_test.go @@ -0,0 +1,130 @@ +package live + +import ( + "context" + "strings" + "sync" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/meetings" + "github.com/hivecommons/hive/pkg/meetings/betty" +) + +type stepClock struct { + mu sync.Mutex + now time.Time +} + +func (c *stepClock) Now() time.Time { c.mu.Lock(); defer c.mu.Unlock(); return c.now } +func (c *stepClock) add(d time.Duration) { + c.mu.Lock() + c.now = c.now.Add(d) + c.mu.Unlock() +} + +// voiceSession is a session (not running) on a meeting with voice on and a +// controllable clock, for driving the voice queue directly. +func voiceSession(t *testing.T, p Policy) (*harness, *session, *stepClock) { + t.Helper() + clk := &stepClock{now: time.Date(2026, 11, 16, 9, 0, 0, 0, time.UTC)} + h := newHarness(t, p, func(o *Options) { + o.Now = clk.Now + o.Persona = func(meetings.Meeting) *betty.Engine { + return betty.New(betty.SettingsFrom(config.MeetingsPersonaConfig{}.Resolve()), betty.Deps{}) + } + }) + h.m.VoiceEnabled = true + h.mt.m[h.m.ID] = h.m + return h, h.r.newSession(h.m, h.spek), clk +} + +// Answers ready together are spoken shortest first; one that would wait +// behind more than voiceBacklogMax of speech goes to chat, with one spoken +// note. +func TestFlushVoiceShortestFirstAndOverflow(t *testing.T) { + h, s, _ := voiceSession(t, Policy{ChatPostsPerMinute: 6, VoiceOnAddress: true}) + short := "It ships Friday." + mid := strings.Repeat("m", 150) // 10 s + long := strings.Repeat("l", 200) // about 13 s + s.queueVoice(long) + s.queueVoice(short) + s.queueVoice(mid) + s.flushVoice(context.Background()) + posts, says := h.mt.got() + want := []string{short, mid, "I've put the rest in chat."} + if strings.Join(says, "|") != strings.Join(want, "|") { + t.Fatalf("says %q, want %q", says, want) + } + if len(posts) != 1 || posts[0] != long { + t.Fatalf("posts %q, want the long answer in chat", posts) + } + if len(s.voice.pending) != 0 { + t.Fatal("the flush leaves nothing pending") + } +} + +// An acknowledgement is skipped behind more than ackMaxBacklog of queued +// speech, and spoken again once it has played. +func TestAckSkippedBehindLongSpeech(t *testing.T) { + _, s, clk := voiceSession(t, Policy{ChatPostsPerMinute: 6, VoiceOnAddress: true}) + ctx := context.Background() + s.queueVoice(strings.Repeat("x", 180)) // 12 s + s.flushVoice(ctx) + if s.speakAck(ctx, "One moment.") { + t.Fatal("an ack behind 12 s of speech is skipped") + } + clk.add(10 * time.Second) + if !s.speakAck(ctx, "One moment.") { + t.Fatal("an ack behind 2 s of speech is spoken") + } +} + +// Answers to addressed requests may take a small burst past the chat rate; +// Spek's posts may not. +func TestAnswerBurstPastChatRate(t *testing.T) { + h, s, _ := voiceSession(t, Policy{ChatPostsPerMinute: 1, ChatAnswerBurst: 3}) + ctx := context.Background() + if !s.post(ctx, "spek one") { + t.Fatal("first post") + } + if s.post(ctx, "spek two") { + t.Fatal("spek posts stay within the chat rate") + } + for i := range 3 { + if !s.postAnswer(ctx, "answer") { + t.Fatalf("answer %d within the burst refused", i+1) + } + } + if s.postAnswer(ctx, "answer") { + t.Fatal("past the burst an answer waits") + } + if posts, _ := h.mt.got(); len(posts) != 4 { + t.Fatalf("posts %q", posts) + } + _, none, _ := voiceSession(t, Policy{ChatPostsPerMinute: 1}) + none.post(ctx, "x") + if none.postAnswer(ctx, "y") { + t.Fatal("no burst configured: the chat rate holds") + } +} + +func TestSpeechTime(t *testing.T) { + if d := speechTime("hi"); d != voiceMinClip { + t.Fatalf("short clip %v", d) + } + if d := speechTime(strings.Repeat("a", 150)); d != 10*time.Second { + t.Fatalf("150 chars %v", d) + } + var v voiceQueue + now := time.Date(2026, 11, 16, 9, 0, 0, 0, time.UTC) + v.queued(now, strings.Repeat("a", 30)) + v.queued(now, strings.Repeat("a", 30)) + if b := v.backlog(now); b != 4*time.Second { + t.Fatalf("two queued clips back to back: %v", b) + } + if b := v.backlog(now.Add(time.Minute)); b != 0 { + t.Fatalf("played out: %v", b) + } +} From 64525b99dd2f24b58ae1b704c00d6e32099076d2 Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 16 Nov 2026 22:51:33 -0500 Subject: [PATCH 09/13] =?UTF-8?q?=F0=9F=8C=B1=20Show=20meetings.chat=5Fans?= =?UTF-8?q?wer=5Fburst=20in=20the=20meetings=20settings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Part of #18590. Signed-off-by: Andy Anderson --- src/pkg/dashboard/api_meetings.go | 1 + 1 file changed, 1 insertion(+) diff --git a/src/pkg/dashboard/api_meetings.go b/src/pkg/dashboard/api_meetings.go index fc78f57cdc..c3419030d7 100644 --- a/src/pkg/dashboard/api_meetings.go +++ b/src/pkg/dashboard/api_meetings.go @@ -487,6 +487,7 @@ func (s *Server) meetingsSettings(ctx context.Context, m config.MeetingsConfig) "meetings.research_pool_size": strconv.Itoa(m.ResearchPoolSizeOrDefault()), "meetings.research_per_meeting": strconv.Itoa(m.ResearchPerMeetingOrDefault()), "meetings.chat_posts_per_minute": strconv.Itoa(m.ChatPostsPerMinuteOrDefault()), + "meetings.chat_answer_burst": strconv.Itoa(m.ChatAnswerBurstOrDefault()), "meetings.lobby_timeout_s": strconv.Itoa(m.LobbyTimeoutSOrDefault()), "meetings.retention_days": strconv.Itoa(m.RetentionDaysOrDefault()), "meetings.consent_text": m.ConsentTextOrDefault(), From f601d9586ec6048254d9738200a0cbda9d8463bd Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 16 Nov 2026 22:48:56 -0500 Subject: [PATCH 10/13] =?UTF-8?q?=E2=9C=A8=20Betty=20times=20every=20answe?= =?UTF-8?q?r=20stage=20and=20answers=20live-state=20questions=20without=20?= =?UTF-8?q?waiting=20for=20search?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Each answer carries heard, retrieval, model and answer-ready timings with its source and budget (4 s for the hive's live state, 8 s otherwise); the live session adds delivery and say times and keeps the last 20 with a latency summary in the live state served by the meeting detail API. Gather stops waiting once the hive status retriever answers. Part of #18590. Signed-off-by: Andy Anderson --- .../added-18590-betty-answer-timings.md | 1 + src/docs/meetings.md | 1 + src/pkg/meetings/betty/betty.go | 5 + src/pkg/meetings/betty/queue.go | 26 ++++- src/pkg/meetings/betty/retrieve.go | 11 +- src/pkg/meetings/betty/timing.go | 107 ++++++++++++++++++ src/pkg/meetings/betty/timing_test.go | 68 +++++++++++ src/pkg/meetings/live/session.go | 8 +- src/pkg/meetings/live/timing.go | 93 +++++++++++++++ src/pkg/meetings/live/timing_test.go | 51 +++++++++ src/pkg/meetings/live/types.go | 4 + 11 files changed, 369 insertions(+), 6 deletions(-) create mode 100644 changelog.d/added-18590-betty-answer-timings.md create mode 100644 src/pkg/meetings/betty/timing.go create mode 100644 src/pkg/meetings/betty/timing_test.go create mode 100644 src/pkg/meetings/live/timing.go create mode 100644 src/pkg/meetings/live/timing_test.go diff --git a/changelog.d/added-18590-betty-answer-timings.md b/changelog.d/added-18590-betty-answer-timings.md new file mode 100644 index 0000000000..8c46ba1d99 --- /dev/null +++ b/changelog.d/added-18590-betty-answer-timings.md @@ -0,0 +1 @@ +- **Betty's answers are timed stage by stage.** The meeting's live state (`/api/meetings/{id}/live`) now lists each recent answer's retrieval, model, answer-ready and delivery times, its source and its budget (4 s for questions about the hive's live state, 8 s otherwise), with a median and slowest summary; slow answers are logged. Questions about the hive no longer wait for slower sources such as web search once the live state is in. ([#18590](https://github.com/hivecommons/hive/issues/18590)) diff --git a/src/docs/meetings.md b/src/docs/meetings.md index 49922a21eb..62aad3bf10 100644 --- a/src/docs/meetings.md +++ b/src/docs/meetings.md @@ -42,6 +42,7 @@ Hive attends meetings as a persona: **Betty** ("Betty Bee from the hive", she/he - **Room questions.** Betty also answers questions people ask the room (a line ending in "?" or starting with "what", "how", "who" and the like, four words or more), but only if nobody answers within `answer_wait_s` (8 seconds), the knowledge sources had a hit, the model is at least `min_confidence` (0.75) sure, and the topic was not already answered. She never posts general commentary. - **Caps.** Unprompted answers are capped at `per_10m` (2 per ten minutes), at least `min_gap_s` (90 seconds) apart and `per_meeting` (12) per meeting, with one reply per question, at most `queue_depth` (3) questions waiting and none older than `stale_s` (60 seconds). Addressed requests skip these caps but still go through the chat rate limit (`meetings.chat_posts_per_minute`) and the meeting budget; once that rate is spent, answers to addressed requests may take `meetings.chat_answer_burst` (3) more posts a minute, so answers worked on in parallel are not held for a minute. - **Several spoken answers.** Quorum queues each spoken clip and plays them in order, so she never talks over herself; hive estimates the queued speech (about 15 characters a second). Spoken answers that are ready together are said back to back, shortest first. An answer that would wait behind more than 20 s of queued speech is posted to chat instead, and she says "I've put the rest in chat." (at most once every 30 s). An acknowledgement that would play only after more than 8 s of earlier speech is skipped. Chat answers are at most 3 sentences and 300 characters. +- **Speed.** Every answer records its stages: when the request was heard, retrieval, the model call, answer ready, and delivery (chat post, or the meeting accepting the speech). The meeting's live state (`GET /api/meetings/{id}/live`, under `live.persona_timings`, the last 20 answers) shows them with the source (`hive-status`, `retrieval`, `model` or `fallback`) and a budget: 4 s from heard to answer for a question about the hive's live state, 8 s otherwise; `live.persona_latency` sums answers, answers over budget, median and slowest. An answer over budget is logged with its stages. Retrieval stops waiting as soon as the hive's live state answers a question about the hive, so a slow web search never delays it. - **Spek.** With Betty on, Spek's spoken replies are dropped, Spek's chat posts go through Betty (posted unless she already covered the topic, so the meeting never gets two answers), and Spek's clarifying questions are reworded in Betty's voice with one quick model call (800 ms; on failure or timeout the original text is posted). The rewording runs beside the meeting loop, so the transcript, notes and other replies keep flowing; Spek's later questions and chat posts wait their turn so they still appear in Spek's order. Notes and research are unchanged. Lines addressed to Betty and leave phrases never count as answers to Spek's questions. - **Manners.** Plain, brief language; at most one light touch of humor, never about a person and none when the conversation sounds tense. Set `meetings.persona.humor: off` to turn humor off. Meeting speech, knowledge and work items reach the model as data, never as instructions. She never claims to have done something she did not do. - **Model.** Betty answers with the meetings model through the same path as `hive meeting-llm` (an OpenAI-compatible endpoint, charged to the meeting's usage ledger and budget). On a hive with only an Anthropic key (`HIVE_MEETINGS_ANTHROPIC_API_KEY` or `meetings.llm.anthropic_key_file`) she calls the Anthropic Messages API directly with `meetings.llm.model` (default `claude-haiku-5-5`), with the same timeouts, in-flight limit and usage ledger, so her tokens count toward the meeting's budget. With no model she still introduces herself but says she cannot answer questions. With `meetings.llm.mode: agent-backend` (or `claude-cli`) she answers through one of the hive's agent backends instead; see [Agent backends](#agent-backends). diff --git a/src/pkg/meetings/betty/betty.go b/src/pkg/meetings/betty/betty.go index c469bb1575..af76873a45 100644 --- a/src/pkg/meetings/betty/betty.go +++ b/src/pkg/meetings/betty/betty.go @@ -55,6 +55,9 @@ type Reply struct { // query through the meeting's research path and to report back with // FollowUp when it completes. Research string `json:"research,omitempty"` + // Timing is the answer's stage timings (timing.go), on answers to a + // request. + Timing *Timing `json:"timing,omitempty"` } // Line is one final transcript segment or chat line. @@ -456,10 +459,12 @@ func (e *Engine) run() { case r := <-e.retrieved: if p := e.pending[r.id]; p != nil { p.snips, p.retrieved = r.snips, true + p.stages.retrievedAt = e.d.Now() e.proceed(p) } case a := <-e.answers: if p := e.pending[a.id]; p != nil { + p.stages.modelDone = a.done e.onAnswer(p, a) } } diff --git a/src/pkg/meetings/betty/queue.go b/src/pkg/meetings/betty/queue.go index b34734c6a6..140eb053b5 100644 --- a/src/pkg/meetings/betty/queue.go +++ b/src/pkg/meetings/betty/queue.go @@ -48,6 +48,8 @@ type pending struct { // overlapped: another request was in flight at the same time, so its // answer names who and what (attribution.go). overlapped bool + // stages are the request's stage marks (timing.go). + stages stageTimes } type retrieval struct { @@ -59,6 +61,8 @@ type answered struct { id int ans Answer err error + // done is when the model call returned. + done time.Time } // rememberTopic records a topic as answered, for dedupe. @@ -158,6 +162,7 @@ func (e *Engine) addressed(l Line, trig Trigger, now time.Time) { e.drop("budget") return } + early := e.hadEarlyAck(l.Who, now) if trig.VoiceWanted && e.d.LLM != nil && !e.takeEarlyAck(l.Who, now) { // Speak at once so the room knows she heard; the answer follows. e.emit(Reply{Kind: KindVoice, Text: e.ackFor(l.Who), Trigger: TriggerAck, Question: l.Text, VoiceWanted: true}) @@ -165,6 +170,10 @@ func (e *Engine) addressed(l Line, trig Trigger, now time.Time) { p := e.add(l, trig, TopicKey(trig.Rest), true, now) p.waited, p.thoughts = true, thoughts e.noteQueue(now) + if trig.VoiceWanted && e.d.LLM != nil { + // Acknowledged from the partial transcript, or just now. + p.stages.ackAt, p.stages.earlyAck = now, early + } e.retrieve(p, trig.Rest) } @@ -207,6 +216,13 @@ func (e *Engine) onPartial(l Line) { } } +// hadEarlyAck reports whether this speaker has an early ack standing, +// without clearing it. +func (e *Engine) hadEarlyAck(who string, now time.Time) bool { + at, ok := e.earlyAck[who] + return ok && now.Sub(at) < earlyAckWindow +} + // takeEarlyAck reports whether this speaker was already acknowledged from // a partial of the line now final, and clears the mark. func (e *Engine) takeEarlyAck(who string, now time.Time) bool { @@ -360,7 +376,8 @@ func (e *Engine) proceed(p *pending) { } if e.d.LLM == nil { text := e.attributed(p, p.trig.VoiceWanted, "", fallbackNoModel, now) - e.emit(Reply{Kind: replyKind(p.trig), Text: text, Trigger: TriggerAddressed, Question: p.line.Text, VoiceWanted: p.trig.VoiceWanted}) + e.emit(Reply{Kind: replyKind(p.trig), Text: text, Trigger: TriggerAddressed, Question: p.line.Text, VoiceWanted: p.trig.VoiceWanted, + Timing: e.timing(p, true, now)}) e.finish(p, "") return } @@ -376,6 +393,7 @@ func (e *Engine) proceed(p *pending) { system := SystemPrompt(e.s.Persona) prompt := answerPrompt(mode, p.line.Who, p.line.Text, p.snips, recent, p.addressed && p.trig.VoiceWanted) id := p.id + p.stages.modelStart = now go func() { ctx, cancel := context.WithTimeout(e.ctx, timeout) defer cancel() @@ -385,7 +403,7 @@ func (e *Engine) proceed(p *pending) { a, err = ParseAnswer(raw) } select { - case e.answers <- answered{id: id, ans: a, err: err}: + case e.answers <- answered{id: id, ans: a, err: err, done: e.d.Now()}: case <-e.ctx.Done(): } }() @@ -411,7 +429,8 @@ func (e *Engine) onAnswer(p *pending, a answered) { return } said := e.attributed(p, false, a.ans.Topic, text, now) - if e.emit(Reply{Text: said, Trigger: TriggerQuestion, Question: p.line.Text, NeedsResearch: a.ans.NeedsResearch, Sources: a.ans.Sources}) { + if e.emit(Reply{Text: said, Trigger: TriggerQuestion, Question: p.line.Text, NeedsResearch: a.ans.NeedsResearch, Sources: a.ans.Sources, + Timing: e.timing(p, false, now)}) { e.posts = append(e.posts, now) e.lastPost = now e.total++ @@ -474,6 +493,7 @@ func (e *Engine) onAddressedAnswer(p *pending, a answered) { rep.Research = clip(researchQuery(p), maxResearchQuery) } rep.Text = e.attributed(p, p.trig.VoiceWanted, a.ans.Topic, rep.Text, e.d.Now()) + rep.Timing = e.timing(p, a.err != nil || a.ans.Answer == "", e.d.Now()) e.emit(rep) e.rememberTopic(p.topic) e.rememberTopic(TopicKey(text)) diff --git a/src/pkg/meetings/betty/retrieve.go b/src/pkg/meetings/betty/retrieve.go index 8004e0ee6e..d90940efe5 100644 --- a/src/pkg/meetings/betty/retrieve.go +++ b/src/pkg/meetings/betty/retrieve.go @@ -59,7 +59,16 @@ func Gather(ctx context.Context, rs []Retriever, query string, terms []string, t } for n := 0; n < len(rs); n++ { select { - case <-done: + case i := <-done: + // The hive's live state answers a question about the hive on + // its own: do not wait for slower sources such as web search + // (#18590, a live-state answer within HiveStatusBudget). + mu.Lock() + live := hasHiveStatus(results[i]) + mu.Unlock() + if live { + n = len(rs) + } case <-ctx.Done(): n = len(rs) } diff --git a/src/pkg/meetings/betty/timing.go b/src/pkg/meetings/betty/timing.go new file mode 100644 index 0000000000..f8593b4951 --- /dev/null +++ b/src/pkg/meetings/betty/timing.go @@ -0,0 +1,107 @@ +package betty + +import "time" + +// Per-request stage timings (hivecommons/hive#18590): every answer carries +// how long each stage took, so the meeting detail shows where the seconds +// went and whether the answer met its budget. + +// Answer sources, for Timing.Source. +const ( + // SourceHiveStatus: answered from the hive's live state. + SourceHiveStatus = "hive-status" + // SourceRetrieval: answered from knowledge, beads, code maps or web + // hits. + SourceRetrieval = "retrieval" + // SourceModel: answered by the model with no retrieval hits. + SourceModel = "model" + // SourceFallback: the model failed or is missing; a fixed reply. + SourceFallback = "fallback" +) + +// Answer budgets: the time from hearing a request to having its answer. +const ( + // HiveStatusBudget is the target for a question about the hive's live + // state, which needs no search: in-memory status and one model call. + HiveStatusBudget = 4 * time.Second + // AnswerBudget is the target for any other answer. + AnswerBudget = 8 * time.Second +) + +// Timing is one answer's stage timings, in milliseconds from when the +// request was heard (its final line). +type Timing struct { + HeardAt time.Time `json:"heard_at"` + // AckMS is when the spoken acknowledgement went out; EarlyAck marks one + // said from the partial transcript, before the final line (AckMS 0). + AckMS int64 `json:"ack_ms,omitempty"` + EarlyAck bool `json:"early_ack,omitempty"` + // RetrieveMS is how long retrieval took; ModelMS the model call. + RetrieveMS int64 `json:"retrieve_ms"` + ModelMS int64 `json:"model_ms"` + // AnswerMS is heard to answer ready. + AnswerMS int64 `json:"answer_ms"` + Source string `json:"source"` + BudgetMS int64 `json:"budget_ms"` + // OverBudget reports AnswerMS above BudgetMS. + OverBudget bool `json:"over_budget,omitempty"` +} + +// stageTimes are a pending request's stage marks. +type stageTimes struct { + ackAt time.Time + earlyAck bool + retrievedAt time.Time + modelStart time.Time + modelDone time.Time +} + +func ms(d time.Duration) int64 { + if d < 0 { + return 0 + } + return d.Milliseconds() +} + +// since is the milliseconds from a to b, zero when either is unset. +func since(a, b time.Time) int64 { + if a.IsZero() || b.IsZero() { + return 0 + } + return ms(b.Sub(a)) +} + +// timing is p's stage timings for an answer ready at now. +func (e *Engine) timing(p *pending, failed bool, now time.Time) *Timing { + t := &Timing{ + HeardAt: p.created, + EarlyAck: p.stages.earlyAck, + RetrieveMS: since(p.created, p.stages.retrievedAt), + ModelMS: since(p.stages.modelStart, p.stages.modelDone), + AnswerMS: since(p.created, now), + } + if !p.stages.earlyAck { + t.AckMS = since(p.created, p.stages.ackAt) + } + switch { + case failed: + t.Source = SourceFallback + case hasHiveStatus(p.snips): + t.Source = SourceHiveStatus + case len(p.snips) > 0: + t.Source = SourceRetrieval + default: + t.Source = SourceModel + } + budget := AnswerBudget + if t.Source == SourceHiveStatus { + budget = HiveStatusBudget + } + t.BudgetMS = budget.Milliseconds() + t.OverBudget = t.AnswerMS > t.BudgetMS + if t.OverBudget { + e.log.Info("[meetings] persona answer over budget", "source", t.Source, "answer_ms", t.AnswerMS, + "budget_ms", t.BudgetMS, "retrieve_ms", t.RetrieveMS, "model_ms", t.ModelMS) + } + return t +} diff --git a/src/pkg/meetings/betty/timing_test.go b/src/pkg/meetings/betty/timing_test.go new file mode 100644 index 0000000000..6a34527768 --- /dev/null +++ b/src/pkg/meetings/betty/timing_test.go @@ -0,0 +1,68 @@ +package betty + +import ( + "context" + "testing" + "time" +) + +// Each answer carries its stage timings: retrieval, the model call and the +// time to the answer, its source and its budget. +func TestAnswerTimingStages(t *testing.T) { + var r *rig + hook := hookRetriever{fn: func() { r.clk.add(300 * time.Millisecond) }} + llm := &fakeLLM{reply: func(string, string) (string, error) { + r.clk.add(time.Second) + return `{"answer":"It ships Friday.","confidence":0.9}`, nil + }} + r = newRig(t, testSettings(), llm, hook) + r.e.Observe(Line{Who: "Bo", Text: "Betty, when does the release ship?"}) + tm := r.reply().Timing + if tm == nil { + t.Fatal("no timing") + } + if tm.RetrieveMS != 300 || tm.ModelMS != 1000 || tm.AnswerMS != 1300 || tm.Source != SourceRetrieval || + tm.BudgetMS != AnswerBudget.Milliseconds() || tm.OverBudget { + t.Fatalf("timing %+v", tm) + } +} + +// A live-state answer has the tighter budget and is flagged when it +// misses it. +func TestHiveStatusAnswerBudget(t *testing.T) { + var r *rig + llm := &fakeLLM{reply: func(string, string) (string, error) { + r.clk.add(HiveStatusBudget + time.Second) + return `{"answer":"40 open issues.","confidence":0.9}`, nil + }} + r = newRig(t, testSettings(), llm, fakeRetriever{hits: []Snippet{{Source: HiveStatusSource, Title: "The hive right now", Text: "Open now: 40 issues."}}}) + r.e.Observe(Line{Who: "Bo", Text: "Betty, how many open issues are there?"}) + tm := r.reply().Timing + if tm == nil || tm.Source != SourceHiveStatus || tm.BudgetMS != HiveStatusBudget.Milliseconds() || !tm.OverBudget { + t.Fatalf("timing %+v", tm) + } + noModel := newRig(t, testSettings(), nil) + noModel.e.Observe(Line{Who: "Bo", Text: "Betty, when does the release ship?"}) + if tm := noModel.reply().Timing; tm == nil || tm.Source != SourceFallback { + t.Fatalf("fallback timing %+v", tm) + } +} + +// The hive's live state ends retrieval at once: a question about the hive +// does not wait for slower sources. +func TestGatherStopsAtHiveStatus(t *testing.T) { + live := fakeRetriever{hits: []Snippet{{Source: HiveStatusSource, Text: "Open now: 40 issues."}}} + slow := fakeRetriever{hits: []Snippet{{Source: "web", Text: "slow"}}, delay: time.Second} + start := time.Now() + got := Gather(context.Background(), []Retriever{slow, live}, "how many open issues", []string{"issue"}, 1500*time.Millisecond) + if d := time.Since(start); d > 500*time.Millisecond { + t.Fatalf("gather waited %v for slower sources", d) + } + if len(got) != 1 || got[0].Source != HiveStatusSource { + t.Fatalf("got %+v", got) + } + // Without the live state every source is waited for. + if got := Gather(context.Background(), []Retriever{slow}, "q", []string{"q"}, 1500*time.Millisecond); len(got) != 1 { + t.Fatalf("slow source dropped: %+v", got) + } +} diff --git a/src/pkg/meetings/live/session.go b/src/pkg/meetings/live/session.go index b29700fa09..1d9343b776 100644 --- a/src/pkg/meetings/live/session.go +++ b/src/pkg/meetings/live/session.go @@ -93,6 +93,8 @@ type session struct { persona *betty.Engine // fileTokens is the usage-ledger total already charged. fileTokens int + // lastSay is how long the meeting's last say call took (timing.go). + lastSay time.Duration // spekQueue keeps Spek's asks and chat offers in order while an ask is // being restyled by the persona off the session goroutine; restyled // carries the results back. @@ -480,7 +482,7 @@ func (s *session) say(ctx context.Context, text string) { m, err := s.r.opts.Meetings.Get(s.id) if err == nil && m.VoiceEnabled && s.r.opts.Policy.VoiceOnAddress { cctx, cancel := context.WithTimeout(ctx, s.r.opts.CallTimeout) - err = s.r.opts.Meetings.Say(cctx, s.id, text) + err = s.timedSay(func() error { return s.r.opts.Meetings.Say(cctx, s.id, text) }) cancel() if err == nil { return @@ -762,6 +764,7 @@ func (s *session) onPersonaReply(ctx context.Context, rep betty.Reply) { return } if s.speakAck(ctx, text) { + s.recordAckSay() s.emitPersona(rep) } else { s.drop("ack_no_voice") @@ -818,6 +821,7 @@ func (s *session) onQueueNote(ctx context.Context, rep betty.Reply, text string) } func (s *session) emitPersona(rep betty.Reply) { + s.recordTiming(rep) raw, _ := json.Marshal(rep) s.emit(Event{Kind: EventPersona, Reason: rep.Trigger, Intent: raw}) } @@ -925,7 +929,7 @@ func (s *session) speak(ctx context.Context, text string) bool { } cctx, cancel := context.WithTimeout(ctx, s.r.opts.CallTimeout) defer cancel() - return s.r.opts.Meetings.Say(cctx, s.id, text) == nil + return s.timedSay(func() error { return s.r.opts.Meetings.Say(cctx, s.id, text) }) == nil } // queuedSpek is one of Spek's asks or chat offers waiting its turn while an diff --git a/src/pkg/meetings/live/timing.go b/src/pkg/meetings/live/timing.go new file mode 100644 index 0000000000..eef4493c26 --- /dev/null +++ b/src/pkg/meetings/live/timing.go @@ -0,0 +1,93 @@ +package live + +import ( + "slices" + "time" + + "github.com/hivecommons/hive/pkg/meetings/betty" +) + +// Persona answer timings in the live state (hivecommons/hive#18590): the +// meeting detail API (GET /api/meetings/{id}/live, `live.persona_timings` +// and `live.persona_latency`) shows every recent answer's stages, heard to +// retrieval to model to answer ready to sent, and a summary against the +// answer budgets. + +// maxPersonaTimings bounds the answers kept with their timings. +const maxPersonaTimings = 20 + +// PersonaTiming is one persona answer's stages. +type PersonaTiming struct { + betty.Timing + Trigger string `json:"trigger"` + Kind string `json:"kind"` + // DeliverMS is answer ready to sent (chat) or handed to the meeting's + // voice (spoken), including any wait behind the chat rate. + DeliverMS int64 `json:"deliver_ms"` + // SayMS is how long the meeting took to accept the speech (TTS on + // Discord synthesises before it accepts), for a spoken answer. + SayMS int64 `json:"say_ms,omitempty"` + // TotalMS is heard to sent. + TotalMS int64 `json:"total_ms"` +} + +// PersonaLatency sums the persona's answer timings: counts over the +// meeting, percentiles over the kept answers. +type PersonaLatency struct { + Answers int `json:"answers"` + OverBudget int `json:"over_budget"` + P50MS int64 `json:"p50_ms"` + MaxMS int64 `json:"max_ms"` + // LastAckSayMS is how long the last spoken acknowledgement took to be + // accepted. + LastAckSayMS int64 `json:"last_ack_say_ms,omitempty"` +} + +// recordTiming adds a persona answer's timings once it is out. +func (s *session) recordTiming(rep betty.Reply) { + if rep.Timing == nil { + return + } + now := s.now() + ready := rep.Timing.HeardAt.Add(time.Duration(rep.Timing.AnswerMS) * time.Millisecond) + t := PersonaTiming{Timing: *rep.Timing, Trigger: rep.Trigger, Kind: rep.Kind, + DeliverMS: max(0, now.Sub(ready).Milliseconds()), TotalMS: max(0, now.Sub(rep.Timing.HeardAt).Milliseconds())} + if rep.Kind == betty.KindVoice { + t.SayMS = s.lastSay.Milliseconds() + } + s.state.PersonaTimings = append(s.state.PersonaTimings, t) + if n := len(s.state.PersonaTimings); n > maxPersonaTimings { + s.state.PersonaTimings = s.state.PersonaTimings[n-maxPersonaTimings:] + } + l := s.latency() + l.Answers++ + if t.OverBudget { + l.OverBudget++ + } + totals := make([]int64, 0, len(s.state.PersonaTimings)) + for _, x := range s.state.PersonaTimings { + totals = append(totals, x.TotalMS) + } + slices.Sort(totals) + l.P50MS, l.MaxMS = totals[len(totals)/2], totals[len(totals)-1] +} + +// recordAckSay notes how long a spoken acknowledgement took to go out. +func (s *session) recordAckSay() { + s.latency().LastAckSayMS = s.lastSay.Milliseconds() +} + +func (s *session) latency() *PersonaLatency { + if s.state.PersonaLatency == nil { + s.state.PersonaLatency = &PersonaLatency{} + } + return s.state.PersonaLatency +} + +// timedSay calls the meeting's say and keeps how long it took. +func (s *session) timedSay(say func() error) error { + start := time.Now() + err := say() + s.lastSay = time.Since(start) + return err +} diff --git a/src/pkg/meetings/live/timing_test.go b/src/pkg/meetings/live/timing_test.go new file mode 100644 index 0000000000..ee08829804 --- /dev/null +++ b/src/pkg/meetings/live/timing_test.go @@ -0,0 +1,51 @@ +package live + +import ( + "testing" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/meetings" + "github.com/hivecommons/hive/pkg/meetings/betty" +) + +// The live state (the meeting detail API's `live`) keeps each answer's +// stage timings and a latency summary. +func TestPersonaTimingsInLiveState(t *testing.T) { + h := personaHarness(t, Policy{ChatPostsPerMinute: 20}, config.MeetingsPersonaConfig{}, &personaLLM{}) + h.live() + h.line(meetings.EventChat, "Bo", "Betty, when does the release ship?", false) + h.eventually("answer", h.posted("The release ships Friday.")) + h.eventually("timing recorded", func() bool { return len(h.state().PersonaTimings) == 1 }) + st := h.state() + tm := st.PersonaTimings[0] + if tm.Source != betty.SourceRetrieval || tm.Trigger != betty.TriggerAddressed || tm.Kind != betty.KindChat || + tm.TotalMS < tm.AnswerMS || tm.BudgetMS == 0 { + t.Fatalf("timing %+v", tm) + } + if l := st.PersonaLatency; l == nil || l.Answers != 1 || l.MaxMS != tm.TotalMS || l.P50MS != tm.TotalMS { + t.Fatalf("latency %+v", st.PersonaLatency) + } +} + +func TestRecordTimingKeepsTheLastAnswers(t *testing.T) { + _, s := timingSession(t) + for i := range maxPersonaTimings + 5 { + s.recordTiming(betty.Reply{Trigger: betty.TriggerAddressed, Timing: &betty.Timing{HeardAt: s.now(), AnswerMS: int64(i), OverBudget: i%2 == 0}}) + } + if n := len(s.state.PersonaTimings); n != maxPersonaTimings { + t.Fatalf("kept %d timings", n) + } + if l := s.state.PersonaLatency; l.Answers != maxPersonaTimings+5 || l.OverBudget != (maxPersonaTimings+5+1)/2 { + t.Fatalf("latency %+v", l) + } + s.recordTiming(betty.Reply{Trigger: betty.TriggerAck}) + if l := s.state.PersonaLatency; l.Answers != maxPersonaTimings+5 { + t.Fatal("a reply without timings is not counted") + } +} + +func timingSession(t *testing.T) (*harness, *session) { + t.Helper() + h := newHarness(t, Policy{}, nil) + return h, h.r.newSession(h.m, h.spek) +} diff --git a/src/pkg/meetings/live/types.go b/src/pkg/meetings/live/types.go index 07f9aca3f9..292d366ac3 100644 --- a/src/pkg/meetings/live/types.go +++ b/src/pkg/meetings/live/types.go @@ -146,6 +146,10 @@ type State struct { Persona string `json:"persona,omitempty"` // PersonaReplies counts the persona's posted replies. PersonaReplies int `json:"persona_replies,omitempty"` + // PersonaTimings are the last answers' stage timings, newest last; + // PersonaLatency sums them (timing.go, #18590). + PersonaTimings []PersonaTiming `json:"persona_timings,omitempty"` + PersonaLatency *PersonaLatency `json:"persona_latency,omitempty"` } // StateDoc is the side document name the state is stored under. From 41e54f3e6d309f7f9bd4c2d1615d38f348b1d134 Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 16 Nov 2026 22:53:25 -0500 Subject: [PATCH 11/13] =?UTF-8?q?=E2=9C=A8=20Betty=20cites=20the=20sources?= =?UTF-8?q?=20of=20chat=20answers=20and=20says=20when=20she=20is=20not=20s?= =?UTF-8?q?ure?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Chat answers end with up to two sources the model reported using (web link, work item id, knowledge fact, code map or hive live status); the prompt asks for issue and PR numbers and forbids guessing; a low-confidence addressed answer not from the live state is hedged. Part of #18590. Signed-off-by: Andy Anderson --- .../added-18590-betty-cites-sources.md | 1 + src/docs/meetings.md | 1 + src/pkg/meetings/betty/betty_test.go | 4 +- src/pkg/meetings/betty/cite.go | 133 ++++++++++++++++++ src/pkg/meetings/betty/cite_test.go | 73 ++++++++++ src/pkg/meetings/betty/persona.go | 3 + src/pkg/meetings/betty/queue.go | 20 ++- 7 files changed, 232 insertions(+), 3 deletions(-) create mode 100644 changelog.d/added-18590-betty-cites-sources.md create mode 100644 src/pkg/meetings/betty/cite.go create mode 100644 src/pkg/meetings/betty/cite_test.go diff --git a/changelog.d/added-18590-betty-cites-sources.md b/changelog.d/added-18590-betty-cites-sources.md new file mode 100644 index 0000000000..e5646e8985 --- /dev/null +++ b/changelog.d/added-18590-betty-cites-sources.md @@ -0,0 +1 @@ +- **Betty says where her answers come from and when she is not sure.** Chat answers end with the sources they used (a web link, a work item id, the knowledge fact or "hive live status"), the model is told to give issue and PR numbers and never guess, and a low-confidence answer opens with "I'm not sure, but ..." instead of sounding certain. ([#18590](https://github.com/hivecommons/hive/issues/18590)) diff --git a/src/docs/meetings.md b/src/docs/meetings.md index 62aad3bf10..044fd9d3a5 100644 --- a/src/docs/meetings.md +++ b/src/docs/meetings.md @@ -42,6 +42,7 @@ Hive attends meetings as a persona: **Betty** ("Betty Bee from the hive", she/he - **Room questions.** Betty also answers questions people ask the room (a line ending in "?" or starting with "what", "how", "who" and the like, four words or more), but only if nobody answers within `answer_wait_s` (8 seconds), the knowledge sources had a hit, the model is at least `min_confidence` (0.75) sure, and the topic was not already answered. She never posts general commentary. - **Caps.** Unprompted answers are capped at `per_10m` (2 per ten minutes), at least `min_gap_s` (90 seconds) apart and `per_meeting` (12) per meeting, with one reply per question, at most `queue_depth` (3) questions waiting and none older than `stale_s` (60 seconds). Addressed requests skip these caps but still go through the chat rate limit (`meetings.chat_posts_per_minute`) and the meeting budget; once that rate is spent, answers to addressed requests may take `meetings.chat_answer_burst` (3) more posts a minute, so answers worked on in parallel are not held for a minute. - **Several spoken answers.** Quorum queues each spoken clip and plays them in order, so she never talks over herself; hive estimates the queued speech (about 15 characters a second). Spoken answers that are ready together are said back to back, shortest first. An answer that would wait behind more than 20 s of queued speech is posted to chat instead, and she says "I've put the rest in chat." (at most once every 30 s). An acknowledgement that would play only after more than 8 s of earlier speech is skipped. Chat answers are at most 3 sentences and 300 characters. +- **Sources and honesty.** A chat answer ends with the sources it used, from the context entries the model reports: a web hit's link, a work item's id, or the knowledge fact, code map or "hive live status" it came from, at most two, skipped when the answer already names them (`(Source: https://...; hive-42)`). The model is told to give issue and PR numbers as the context has them and never to guess numbers, names, dates or links. An addressed answer below `unprompted.min_confidence` that is not from the hive's live state, and that will not become a deep dive, opens with "I'm not sure, but ..." unless it already says so. Spoken answers carry no sources. - **Speed.** Every answer records its stages: when the request was heard, retrieval, the model call, answer ready, and delivery (chat post, or the meeting accepting the speech). The meeting's live state (`GET /api/meetings/{id}/live`, under `live.persona_timings`, the last 20 answers) shows them with the source (`hive-status`, `retrieval`, `model` or `fallback`) and a budget: 4 s from heard to answer for a question about the hive's live state, 8 s otherwise; `live.persona_latency` sums answers, answers over budget, median and slowest. An answer over budget is logged with its stages. Retrieval stops waiting as soon as the hive's live state answers a question about the hive, so a slow web search never delays it. - **Spek.** With Betty on, Spek's spoken replies are dropped, Spek's chat posts go through Betty (posted unless she already covered the topic, so the meeting never gets two answers), and Spek's clarifying questions are reworded in Betty's voice with one quick model call (800 ms; on failure or timeout the original text is posted). The rewording runs beside the meeting loop, so the transcript, notes and other replies keep flowing; Spek's later questions and chat posts wait their turn so they still appear in Spek's order. Notes and research are unchanged. Lines addressed to Betty and leave phrases never count as answers to Spek's questions. - **Manners.** Plain, brief language; at most one light touch of humor, never about a person and none when the conversation sounds tense. Set `meetings.persona.humor: off` to turn humor off. Meeting speech, knowledge and work items reach the model as data, never as instructions. She never claims to have done something she did not do. diff --git a/src/pkg/meetings/betty/betty_test.go b/src/pkg/meetings/betty/betty_test.go index fc22525fb4..9991c60f76 100644 --- a/src/pkg/meetings/betty/betty_test.go +++ b/src/pkg/meetings/betty/betty_test.go @@ -316,7 +316,7 @@ func TestEngineAddressedAnswer(t *testing.T) { r.e.Observe(Line{Who: "Ana", Text: "we moved the release to Friday"}) r.e.Observe(Line{Who: "Bo", Text: "Betty, when does the release ship?"}) rep := r.reply() - if rep.Trigger != TriggerAddressed || rep.Text != "It ships Friday." || len(rep.Sources) != 1 { + if rep.Trigger != TriggerAddressed || rep.Text != `It ships Friday. (Source: knowledge "Release")` || len(rep.Sources) != 1 { t.Fatalf("addressed %+v", rep) } c := llm.last() @@ -350,7 +350,7 @@ func TestEngineRoomQuestionAnsweredAfterWait(t *testing.T) { start := time.Now() r.e.Observe(Line{Who: "Ana", Text: "When does the release ship?"}) rep := r.reply() - if rep.Trigger != TriggerQuestion || rep.Text != "It ships Friday." || time.Since(start) < testWait { + if rep.Trigger != TriggerQuestion || rep.Text != `It ships Friday. (Source: knowledge "Release")` || time.Since(start) < testWait { t.Fatalf("question %+v after %v", rep, time.Since(start)) } if !strings.Contains(llm.last().prompt, "nobody has answered") { diff --git a/src/pkg/meetings/betty/cite.go b/src/pkg/meetings/betty/cite.go new file mode 100644 index 0000000000..b4bf6e11be --- /dev/null +++ b/src/pkg/meetings/betty/cite.go @@ -0,0 +1,133 @@ +package betty + +import ( + "regexp" + "strconv" + "strings" + "unicode/utf8" +) + +// Citations (hivecommons/hive#18590): a chat answer says where its facts +// came from, from the context entries the model reported using: the link +// of a web hit, a work item's id, or the name of a knowledge fact, code +// map or the hive's live status. Spoken answers carry none (no links +// aloud). +const ( + // maxCitations bounds the sources named after one answer. + maxCitations = 2 + // maxCitationChars bounds one named source. + maxCitationChars = 80 +) + +// citeFormat is the citation suffix by primary language subtag. +var citeFormat = map[string]string{ + "en": "Source: ", "es": "Fuente: ", "fr": "Source : ", "de": "Quelle: ", "pt": "Fonte: ", "it": "Fonte: ", +} + +// trailingURL is the link a web snippet's text ends with. +var trailingURL = regexp.MustCompile(`\((https?://[^\s()]+)\)\s*$`) + +// citation names one snippet as a source. +func citation(s Snippet) string { + switch { + case strings.HasPrefix(s.Source, webSource+" "): + if m := trailingURL.FindStringSubmatch(s.Text); m != nil { + return m[1] + } + return strings.TrimPrefix(s.Source, webSource+" ") + case strings.HasPrefix(s.Source, "work item "): + return strings.TrimPrefix(s.Source, "work item ") + case s.Source == HiveStatusSource: + return "hive live status" + case s.Title != "": + return s.Source + " \"" + s.Title + "\"" + } + return s.Source +} + +// citeSources appends the sources an answer used to a chat answer, unless +// the answer already names them. Sources are the model's context numbers +// (1-based); unknown ones are skipped. +func citeSources(lang, text string, used []string, snips []Snippet) string { + if strings.TrimSpace(text) == "" || len(used) == 0 { + return text + } + var names []string + seen := map[string]bool{} + for _, u := range used { + i, err := strconv.Atoi(strings.Trim(strings.TrimSpace(u), `"[]`)) + if err != nil || i < 1 || i > len(snips) { + continue + } + c := strings.TrimSpace(citation(snips[i-1])) + if c == "" || seen[c] || strings.Contains(text, c) { + continue + } + seen[c] = true + if utf8.RuneCountInString(c) > maxCitationChars { + c = string([]rune(c)[:maxCitationChars-1]) + "…" + } + names = append(names, c) + if len(names) == maxCitations { + break + } + } + if len(names) == 0 { + return text + } + prefix := citeFormat[primaryLang(lang)] + if prefix == "" { + prefix = citeFormat["en"] + } + return strings.TrimSpace(text) + " (" + prefix + strings.Join(names, "; ") + ")" +} + +func primaryLang(lang string) string { + return strings.ToLower(strings.SplitN(strings.TrimSpace(lang), "-", 2)[0]) +} + +// unsurePhrases already say she is not sure, in the persona languages. +var unsurePhrases = []string{ + "not sure", "don't know", "do not know", "no sé", "no estoy segur", "pas sûr", "je ne sais pas", + "nicht sicher", "weiß ich nicht", "não sei", "não tenho certeza", "non sono sicur", "non lo so", +} + +// unsurePrefix opens a low-confidence answer, by language. +var unsurePrefix = map[string]string{ + "en": "I'm not sure, but ", "es": "No estoy segura, pero ", "fr": "Je ne suis pas sûre, mais ", + "de": "Ich bin nicht sicher, aber ", "pt": "Não tenho certeza, mas ", "it": "Non ne sono sicura, ma ", +} + +// hedge marks a low-confidence answer as unsure, unless it already says +// so, rather than stating a guess as fact. +func hedge(lang, text string) string { + t := strings.TrimSpace(text) + if t == "" { + return text + } + low := strings.ToLower(t) + for _, p := range unsurePhrases { + if strings.Contains(low, p) { + return text + } + } + prefix := unsurePrefix[primaryLang(lang)] + if prefix == "" { + prefix = unsurePrefix["en"] + } + return prefix + lowerInitial(t) +} + +// lowerInitial lowercases the first letter unless it starts "I " or an +// acronym. +func lowerInitial(s string) string { + r, n := utf8.DecodeRuneInString(s) + if n == 0 || r < 'A' || r > 'Z' { + return s + } + next, _ := utf8.DecodeRuneInString(s[n:]) + if (r == 'I' && (next == ' ' || next == '\'')) || (next >= 'A' && next <= 'Z') || (next >= '0' && next <= '9') { + return s + } + return string(r+('a'-'A')) + s[n:] +} diff --git a/src/pkg/meetings/betty/cite_test.go b/src/pkg/meetings/betty/cite_test.go new file mode 100644 index 0000000000..3eabad49ef --- /dev/null +++ b/src/pkg/meetings/betty/cite_test.go @@ -0,0 +1,73 @@ +package betty + +import ( + "strings" + "testing" +) + +func TestCiteSources(t *testing.T) { + snips := []Snippet{ + {Source: "web example.com", Title: "Cert docs", Text: "Renews nightly (https://example.com/certs)"}, + {Source: "work item hive-42", Title: "Rotate keys", Text: "open"}, + {Source: "knowledge", Title: "Release plan", Text: "Friday"}, + {Source: HiveStatusSource, Title: "The hive right now", Text: "40 issues"}, + } + for _, c := range []struct { + text string + used []string + want string + }{ + {"It renews nightly.", []string{"1", "2"}, "It renews nightly. (Source: https://example.com/certs; hive-42)"}, + {"It renews nightly.", []string{"1", "2", "3"}, "It renews nightly. (Source: https://example.com/certs; hive-42)"}, + {"Friday.", []string{"[3]"}, `Friday. (Source: knowledge "Release plan")`}, + {"40 open issues.", []string{"4"}, "40 open issues. (Source: hive live status)"}, + {"See https://example.com/certs.", []string{"1"}, "See https://example.com/certs."}, + {"No sources.", nil, "No sources."}, + {"Bad index.", []string{"9", "x"}, "Bad index."}, + } { + if got := citeSources("en", c.text, c.used, snips); got != c.want { + t.Errorf("citeSources(%q, %v) = %q, want %q", c.text, c.used, got, c.want) + } + } + if got := citeSources("de-DE", "Freitag.", []string{"3"}, snips); !strings.HasPrefix(got, "Freitag. (Quelle: ") { + t.Fatalf("localized %q", got) + } +} + +func TestHedge(t *testing.T) { + for in, want := range map[string]string{ + "It ships Friday.": "I'm not sure, but it ships Friday.", + "Not sure, maybe Friday.": "Not sure, maybe Friday.", + "I don't know yet.": "I don't know yet.", + "PR 12 fixed it.": "I'm not sure, but PR 12 fixed it.", + "I think it ships on Friday.": "I'm not sure, but I think it ships on Friday.", + } { + if got := hedge("en", in); got != want { + t.Errorf("hedge(%q) = %q, want %q", in, got, want) + } + } +} + +// A low-confidence addressed answer is said as unsure, not as fact; the +// hive's live state is never hedged; spoken answers carry no citation. +func TestAddressedAnswerHedgedAndCited(t *testing.T) { + llm := &fakeLLM{reply: func(string, string) (string, error) { + return `{"answer":"It ships Friday.","confidence":0.4,"sources":[1]}`, nil + }} + r := newRig(t, testSettings(), llm) + r.e.Observe(Line{Who: "Bo", Text: "Betty, when does the release ship?"}) + if a := r.reply(); a.Text != `I'm not sure, but it ships Friday. (Source: knowledge "Release")` { + t.Fatalf("hedged %q", a.Text) + } + r.e.Observe(Line{Who: "Bo", Text: "Betty, when does the release ship, say it out loud?", Spoken: true}) + for { + a := r.reply() + if a.Trigger == TriggerAck { + continue + } + if a.Text != "I'm not sure, but it ships Friday." { + t.Fatalf("spoken %q", a.Text) + } + break + } +} diff --git a/src/pkg/meetings/betty/persona.go b/src/pkg/meetings/betty/persona.go index 0ff420139f..357684f2e4 100644 --- a/src/pkg/meetings/betty/persona.go +++ b/src/pkg/meetings/betty/persona.go @@ -87,7 +87,10 @@ func answerPrompt(mode, who, question string, snips []Snippet, recent []recentLi } if spoken { b.WriteString("Your answer will be spoken aloud in the meeting: at most 2 sentences and 200 characters, no links, no lists, no Markdown.\n") + } else { + b.WriteString("Name where facts come from: give issue and PR numbers (#123) and work item ids as the context has them, and list the context numbers you used in sources.\n") } + b.WriteString("Only state what the context or the transcript supports. If they do not answer it, say you are not sure and set confidence below 0.5; never guess numbers, names, dates or links.\n") if hasHiveStatus(snips) { b.WriteString("Context marked \"" + HiveStatusSource + "\" is the hive's own state read just now: answer questions about the hive's issues, PRs, agents and work from it directly, with its numbers, and set needs_research to false.\n") } diff --git a/src/pkg/meetings/betty/queue.go b/src/pkg/meetings/betty/queue.go index 140eb053b5..0c66962924 100644 --- a/src/pkg/meetings/betty/queue.go +++ b/src/pkg/meetings/betty/queue.go @@ -428,7 +428,7 @@ func (e *Engine) onAnswer(p *pending, a answered) { e.finish(p, reason) return } - said := e.attributed(p, false, a.ans.Topic, text, now) + said := e.attributed(p, false, a.ans.Topic, citeSources(e.s.Persona.Language, text, a.ans.Sources, p.snips), now) if e.emit(Reply{Text: said, Trigger: TriggerQuestion, Question: p.line.Text, NeedsResearch: a.ans.NeedsResearch, Sources: a.ans.Sources, Timing: e.timing(p, false, now)}) { e.posts = append(e.posts, now) @@ -492,6 +492,9 @@ func (e *Engine) onAddressedAnswer(p *pending, a answered) { rep.NeedsResearch = true rep.Research = clip(researchQuery(p), maxResearchQuery) } + if a.err == nil && rep.Research == "" && text != "" { + rep.Text = e.vouch(p, a.ans, rep.Text, live) + } rep.Text = e.attributed(p, p.trig.VoiceWanted, a.ans.Topic, rep.Text, e.d.Now()) rep.Timing = e.timing(p, a.err != nil || a.ans.Answer == "", e.d.Now()) e.emit(rep) @@ -500,6 +503,21 @@ func (e *Engine) onAddressedAnswer(p *pending, a answered) { e.finish(p, "") } +// vouch is an addressed model answer as said (#18590 accuracy): a +// low-confidence answer that is not from the hive's live state is marked +// unsure rather than stated as fact, and a chat answer names the sources +// it used. +func (e *Engine) vouch(p *pending, ans Answer, text string, live bool) string { + lang := e.s.Persona.Language + if !live && ans.Confidence < e.s.MinConfidence { + text = hedge(lang, text) + } + if !p.trig.VoiceWanted { + text = citeSources(lang, text, ans.Sources, p.snips) + } + return text +} + // maxResearchQuery bounds a deep dive's query. const maxResearchQuery = 300 From dd28287e5e16c8f4f3d00027df1441b98172f1fb Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 16 Nov 2026 23:15:15 -0500 Subject: [PATCH 12/13] =?UTF-8?q?=F0=9F=93=96=20Refresh=20meetings=20API?= =?UTF-8?q?=20doc=20citations=20for=20the=20combined=20Betty=20changes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Andy Anderson --- src/docs/api-reference/knowledge.md | 34 ++++++++++++++--------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/docs/api-reference/knowledge.md b/src/docs/api-reference/knowledge.md index ecb46bfbe2..8949a880c7 100644 --- a/src/docs/api-reference/knowledge.md +++ b/src/docs/api-reference/knowledge.md @@ -89,25 +89,25 @@ Part of the [Dashboard REST API reference](../api-reference.md); see that page f | `DELETE` | `/api/inbox/recordings/{id}` | The recorder, or an owner | Abort an in-progress recording and delete its chunks; audited as `inbox_recording_abort` (403, 404, 409 already ended) | `pkg/dashboard/api_inbox_recordings.go:23` | | `GET` | `/api/inbox/recordings/{id}/audio` | Owner or read-write | Play a finished recording from this hive's disk on the dashboard origin: `http.ServeContent` (Range, 206), `Content-Type` audio/webm or audio/mp4, `Cache-Control: no-store`, `Cross-Origin-Resource-Policy: same-origin`. Opening a playback is audited as `inbox_recording_audio` (seeks are not). `GET /api/inbox/hive-recording/{id}/audio/stream` serves the same file (404 unknown or not finished, 410 audio expired after `inbox.recording.retention_days`) | `pkg/dashboard/api_inbox_recordings.go:24` | | `DELETE` | `/api/inbox/hive-recording/{id}` | An owner, or the read-write member who recorded it | Delete a finished recording for good: its stored audio and chunks, the Inbox item and its state, and its Knowledge fact with the machine transcript. What was already shared from it (RFC, issues, DIBS ideas, spek seeds) is not touched. 200 `{id, deleted, fact_removed, shares_kept}`; audited as `inbox_recording_delete` (403 read role or someone else's recording, 404 unknown, 409 still in progress or being transcribed) | `pkg/dashboard/api_inbox_recordings.go:25` | -| `GET` | `/api/meetings/settings` | Owner only | Meetings settings: `enabled`, `enabled_env_override`, `running`, `vexa_url`, `vexa_key_set`, `discord_token_set`, `transcribe_token_set`, `tts_token_set` (secrets are never returned) and `effective` values of the other `meetings.*` keys ([env vars](../env-vars.md#meetings-quorum)) | `pkg/dashboard/api_meetings.go:124` | -| `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:125` | -| `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:126` | -| `GET` | `/api/meetings` | Owner only | `{meetings: [...]}`, newest first | `pkg/dashboard/api_meetings.go:127` | -| `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:128` | -| `POST` | `/api/meetings/{id}/leave` | Owner only | Make hive leave; returns the record. Audited as `meeting_leave` | `pkg/dashboard/api_meetings.go:129` | -| `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:130` | -| `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:131` | -| `POST` | `/api/meetings/{id}/approve` | Owner only | Approve a platform join request (Discord `/hive join`): joins that channel; optional `{voice}`. 409 when the meeting is not a pending request. Audited as `meeting_request_approve` | `pkg/dashboard/api_meetings.go:132` | -| `POST` | `/api/meetings/{id}/decline` | Owner only | Decline a platform join request with `meeting.leave`. Audited as `meeting_request_decline` | `pkg/dashboard/api_meetings.go:133` | +| `GET` | `/api/meetings/settings` | Owner only | Meetings settings: `enabled`, `enabled_env_override`, `running`, `vexa_url`, `vexa_key_set`, `discord_token_set`, `transcribe_token_set`, `tts_token_set` (secrets are never returned) and `effective` values of the other `meetings.*` keys ([env vars](../env-vars.md#meetings-quorum)) | `pkg/dashboard/api_meetings.go:129` | +| `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: [...]}`, newest first | `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` | +| `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` | +| `POST` | `/api/meetings/{id}/approve` | Owner only | Approve a platform join request (Discord `/hive join`): joins that channel; optional `{voice}`. 409 when the meeting is not a pending request. Audited as `meeting_request_approve` | `pkg/dashboard/api_meetings.go:137` | +| `POST` | `/api/meetings/{id}/decline` | Owner only | Decline a platform join request with `meeting.leave`. Audited as `meeting_request_decline` | `pkg/dashboard/api_meetings.go:138` | | `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:134` | -| `GET` | `/api/meetings/schedule` | Owner or read-write | The Calendar: `{upcoming, recent, attendee_name}`; 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:83` | -| `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:84` | -| `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:85` | -| `POST` | `/api/meetings/schedule/{eid}/decline` | Owner only | Decline an event awaiting approval; later updates of the same invite stay declined. Audited as `meeting_schedule_decline` | `pkg/dashboard/api_meetings_schedule.go:86` | -| `POST` | `/api/meetings/schedule/{eid}/cancel` | Owner only | Stop hive joining the event (every occurrence of a series); a meeting already live is not left. Audited as `meeting_schedule_cancel` | `pkg/dashboard/api_meetings_schedule.go:87` | -| `POST` | `/api/meetings/schedule/{eid}/skip` | Owner only | Skip the next occurrence hive has not joined: a single meeting becomes `skipped`, a series moves on. Audited as `meeting_schedule_skip` | `pkg/dashboard/api_meetings_schedule.go:88` | +| `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/schedule` | Owner or read-write | The Calendar: `{upcoming, recent, attendee_name}`; 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` | +| `POST` | `/api/meetings/schedule/{eid}/decline` | Owner only | Decline an event awaiting approval; later updates of the same invite stay declined. Audited as `meeting_schedule_decline` | `pkg/dashboard/api_meetings_schedule.go:89` | +| `POST` | `/api/meetings/schedule/{eid}/cancel` | Owner only | Stop hive joining the event (every occurrence of a series); a meeting already live is not left. Audited as `meeting_schedule_cancel` | `pkg/dashboard/api_meetings_schedule.go:90` | +| `POST` | `/api/meetings/schedule/{eid}/skip` | Owner only | Skip the next occurrence hive has not joined: a single meeting becomes `skipped`, a series moves on. Audited as `meeting_schedule_skip` | `pkg/dashboard/api_meetings_schedule.go:91` | | `DELETE` | `/api/meetings/schedule/{eid}` | Owner only | Delete a past Calendar entry (ended, cancelled, declined, failed, blocked, skipped) and the meetings it created, each as `DELETE /api/meetings/{id}`. 200 `{id, meetings}`; 404 unknown; 409 for an entry still scheduled, awaiting approval or live (skip or cancel it) or whose meeting hive is still in. Audited as `meeting_schedule_delete`, plus `meeting_delete` per meeting | `pkg/dashboard/api_meetings_delete.go:46` | | `GET` | `/api/meetings/actions` | Owner or read-write | Drafts from meetings: `{drafts, pending}`, newest first. Each draft carries `id`, `meeting_id`, `meeting_title`, `meeting_link`, `type` (`issue`, `rfc`, `spek`, `knowledge`), `title`, `body`, `repo`, `excerpt`, `requested_by` (a meeting display name), `request`, `spoken`, `status` (`pending`, `approving`, `done`, `failed`, `rejected`), `auto_approved`, `result_url`, `result_note`, `error`, `announced` (`chat` or `notes`), `edited_by`, `decided_by`, `decided_at`, `created_at`, `updated_at`. See [Asking Betty to do things](../meetings.md#asking-betty-to-do-things) | `pkg/dashboard/api_meetings_actions.go:69` | | `GET` | `/api/meetings/actions/settings` | Owner or read-write | `meetings.persona.actions` as set and in effect: `enabled`, `types`, `all_types`, `default_repo`, `effective_default_repo`, `repos` (the monitored repos a draft may target), `auto_approve` (per type), `auto_approvable` (`knowledge`) and `env_overrides` | `pkg/dashboard/api_meetings_actions.go:70` | From d437300feb452d1780830566aace8b608b93206e Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 16 Nov 2026 23:31:40 -0500 Subject: [PATCH 13/13] =?UTF-8?q?=F0=9F=90=9B=20Betty=20attribution=20test?= =?UTF-8?q?s=20expect=20the=20source=20a=20chat=20answer=20now=20cites?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With #18631's citations combined, an attributed chat answer ends with its source, so the attribution tests compare against that full text. Signed-off-by: Andy Anderson --- src/pkg/meetings/betty/attribution_test.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/pkg/meetings/betty/attribution_test.go b/src/pkg/meetings/betty/attribution_test.go index 8d4a59e237..ce604d1775 100644 --- a/src/pkg/meetings/betty/attribution_test.go +++ b/src/pkg/meetings/betty/attribution_test.go @@ -20,6 +20,10 @@ func gatedLLM(gates map[string]chan struct{}, topics map[string]string) *fakeLLM }} } +// releaseSource is the citation a chat answer from gatedLLM ends with: its +// source 1 is the rig's "Release" knowledge fact (cite.go). +const releaseSource = ` (Source: knowledge "Release")` + // promptQuestion is the "Question from" line of a prompt. func promptQuestion(prompt string) string { i := strings.Index(prompt, "Question from") @@ -45,11 +49,11 @@ func TestConcurrentRequestsAreAttributed(t *testing.T) { } // The second request is ready first. close(gates["deploy"]) - if a := r.reply(); a.Text != "Bo (re: deploy window): Answer about deploy." { + if a := r.reply(); a.Text != "Bo (re: deploy window): Answer about deploy."+releaseSource { t.Fatalf("first answer %q", a.Text) } close(gates["release"]) - if a := r.reply(); a.Text != "Andy Anderson (re: release date): Answer about release." { + if a := r.reply(); a.Text != "Andy Anderson (re: release date): Answer about release."+releaseSource { t.Fatalf("second answer %q", a.Text) } // A third request within the minute gets no second note. @@ -115,7 +119,7 @@ func TestAnswerAfterOtherLinesIsAttributed(t *testing.T) { time.Sleep(50 * time.Millisecond) close(gate["release"]) // No model topic: the request's own words, in the casing used. - if a := r.reply(); a.Text != "Bo (re: PRs touch the release): Answer about release." { + if a := r.reply(); a.Text != "Bo (re: PRs touch the release): Answer about release."+releaseSource { t.Fatalf("answer %q", a.Text) } }