diff --git a/changelog.d/added-18549-meetings-more-backends.md b/changelog.d/added-18549-meetings-more-backends.md new file mode 100644 index 0000000000..3f6f69da4c --- /dev/null +++ b/changelog.d/added-18549-meetings-more-backends.md @@ -0,0 +1 @@ +- Meetings agent-backend mode now answers through `copilot` and `omp` (one tool-less CLI per call with the agents' sign-in, usage charged to the meeting), through `vllm`, `llm-d` and every named model gateway on the OpenAI-compatible path, and on hives that sign Claude in only with `HIVE_CLAUDE_OAUTH_TOKEN`; `codex`, `gemini`, watsonx and the other CLIs are refused at validation and in the Settings picker with the specific reason. diff --git a/dashboard/openapi.json b/dashboard/openapi.json index 3635763ae6..5ba6f9db19 100644 --- a/dashboard/openapi.json +++ b/dashboard/openapi.json @@ -1537,6 +1537,13 @@ "type": "string" } }, + "refused_backends": { + "type": "object", + "description": "Why each known backend that cannot answer for meetings is refused", + "additionalProperties": { + "type": "string" + } + }, "agent_backends": { "type": "array", "items": { @@ -1645,6 +1652,13 @@ "type": "string" } }, + "refused_backends": { + "type": "object", + "description": "Why each known backend that cannot answer for meetings is refused", + "additionalProperties": { + "type": "string" + } + }, "agent_backends": { "type": "array", "items": { diff --git a/src/cmd/hive/llmd_endpoint.go b/src/cmd/hive/llmd_endpoint.go index 4f3fef740c..0f48443907 100644 --- a/src/cmd/hive/llmd_endpoint.go +++ b/src/cmd/hive/llmd_endpoint.go @@ -1,7 +1,9 @@ package main +import "github.com/hivecommons/hive/pkg/config" + // defaultLLMDEndpoint is the in-cluster llm-d endpoint picker Service used when // HIVE_LLMD_ENDPOINT is unset. The shipped Kubernetes base // (src/deploy/k8s/deployment.yaml) sets the same value, so keep the two in // step (llmd_endpoint_test.go). -const defaultLLMDEndpoint = "http://hive-llm-d-epp.hive-inference.svc.cluster.local:8000" +const defaultLLMDEndpoint = config.DefaultLLMDEndpoint diff --git a/src/cmd/hive/meetingswire.go b/src/cmd/hive/meetingswire.go index 631ac26107..9ec7f72c8b 100644 --- a/src/cmd/hive/meetingswire.go +++ b/src/cmd/hive/meetingswire.go @@ -7,8 +7,12 @@ import ( "os" "path/filepath" "strconv" + "strings" "time" + "github.com/hivecommons/hive/crux" + "github.com/hivecommons/hive/crux/claude" + "github.com/hivecommons/hive/crux/keystore" "github.com/hivecommons/hive/pkg/beads" "github.com/hivecommons/hive/pkg/config" "github.com/hivecommons/hive/pkg/knowledge" @@ -268,6 +272,13 @@ func meetingPersonaFactory(cfg *config.Config, persona config.ResolvedMeetingsPe if persona.SearxngURL != "" { retrievers = append(retrievers, betty.WebRetriever{BaseURL: persona.SearxngURL, Client: client}) } + // A model gateway with a private CA (its ca_bundle) gets its own client; + // one that cannot be built leaves the persona model-less, never + // unverified. + modelClient, clientErr := meetllm.HTTPClient(llm.CABundle) + if clientErr != nil && logger != nil && llm.Backend == config.MeetingsLLMModeOpenAI { + logger.Error("[meetings] persona model client not built; the persona answers without a model", "backend", llm.AgentBackend, "error", clientErr) + } return func(mt meetings.Meeting) *betty.Engine { var model betty.LLM mc := meetllm.Config{ @@ -280,7 +291,9 @@ func meetingPersonaFactory(cfg *config.Config, persona config.ResolvedMeetingsPe switch llm.Backend { case config.MeetingsLLMModeOpenAI: mc.Endpoint, mc.Model, mc.APIKey = llm.Endpoint, llm.Model, llm.APIKey - model = betty.MeetLLM{Client: client, Config: mc} + if clientErr == nil { + model = betty.MeetLLM{Client: modelClient, Config: mc} + } case config.MeetingsLLMModeAnthropic: mc.Provider, mc.Model, mc.APIKey = meetllm.ProviderAnthropic, llm.Model, llm.AnthropicKey model = betty.MeetLLM{Client: client, Config: mc} @@ -297,13 +310,25 @@ func meetingPersonaFactory(cfg *config.Config, persona config.ResolvedMeetingsPe } // meetingCLIModel is the persona's model on an agent backend -// (hivecommons/hive#18517): one warm CLI for the meeting, in the backend's -// no-tools completion profile, started now so the first question finds it -// ready. It reports false when the backend has no profile. +// (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. 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 mc.CLIBinary, mc.CLIHome, mc.RecycleTurns = llm.CLI.Binary, llm.CLI.Home, llm.CLI.RecycleTurns mc.Timeout = time.Duration(llm.CLI.AddressedTimeoutMS) * time.Millisecond + mc.Getenv = meetingCLIGetenv(os.Getenv) + if !meetllm.SupportsWarm(llm.AgentBackend) { + if !meetllm.HasCompletionProfile(llm.AgentBackend) { + if logger != nil { + logger.Error("[meetings] persona model not started; the backend has no completion profile", "meeting", meetingID, "backend", llm.AgentBackend) + } + return betty.MeetLLM{}, false + } + return betty.MeetLLM{Config: mc}, true + } sess, err := meetllm.NewCLISession(mc) if err != nil { if logger != nil { @@ -334,6 +359,55 @@ func applyMeetingCLITimeouts(s *betty.Settings, cli config.ResolvedMeetingsCLI) } } +// meetingClaudeOperatorToken is the operator's long-lived Claude token +// (HIVE_CLAUDE_OAUTH_TOKEN), the seam tests replace. +var meetingClaudeOperatorToken = claude.OperatorToken + +// meetingCopilotTokenFile is the dashboard Copilot sign-in the agents fall +// back to (a var so tests can point it elsewhere). +var meetingCopilotTokenFile = crux.CopilotUserTokenPath + +// meetingCopilotToken is the Copilot sign-in the agents get: +// COPILOT_GITHUB_TOKEN, else the dashboard device-flow token read through +// the keystore. +func meetingCopilotToken(getenv func(string) string) string { + if v := strings.TrimSpace(getenv(meetllm.EnvCopilotToken)); v != "" { + return v + } + data, err := keystore.ReadFile(context.Background(), meetingCopilotTokenFile) + if err != nil { + return "" + } + return strings.TrimSpace(string(data)) +} + +// meetingCLIGetenv is the environment a meetings CLI reads its sign-in +// from: base, with the sign-in variables set the way the agent launcher +// sets them for agents. CLAUDE_CODE_OAUTH_TOKEN is the operator's +// long-lived token only (a hive signed in with the credentials file needs +// none), and COPILOT_GITHUB_TOKEN is the agents' Copilot token. The values +// go to the CLI's environment only: hive never logs them or writes them to +// disk. +func meetingCLIGetenv(base func(string) string) func(string) string { + return func(k string) string { + switch k { + case meetllm.EnvClaudeOAuthToken: + return meetingClaudeOperatorToken() + case meetllm.EnvCopilotToken: + return meetingCopilotToken(base) + } + return base(k) + } +} + +// meetingCLISignInEnv are, per backend, the sign-in variables `hive +// meeting-llm` needs in the Spek child's environment to hand to the CLI. +var meetingCLISignInEnv = map[string][]string{ + meetllm.BackendClaude: {"CLAUDE_CONFIG_DIR", meetllm.EnvClaudeOAuthToken}, + meetllm.BackendCopilot: {meetllm.EnvCopilotToken, "COPILOT_GH_HOST", "GH_HOST"}, + meetllm.BackendOMP: {"OMP_AUTH_BROKER_URL", "OMP_AUTH_BROKER_TOKEN"}, +} + // meetingLLMSlotsDir holds the in-flight slot locks `hive meeting-llm` // shares across meetings; the leading dot keeps it out of the meeting list. const meetingLLMSlotsDir = ".llm-slots" @@ -381,6 +455,9 @@ func meetingSpekEnv(getenv func(string) string, llm config.ResolvedMeetingsLLM, if llm.APIKey != "" { env = append(env, meetllm.EnvAPIKey+"="+llm.APIKey) } + if llm.CABundle != "" { + env = append(env, meetllm.EnvCABundle+"="+llm.CABundle) + } case config.MeetingsLLMModeAgentBackend: // One CLI per prompt through `hive meeting-llm`, in the backend's // completion profile, with the cold-start timeout (#18517). @@ -402,9 +479,13 @@ func meetingSpekEnv(getenv func(string) string, llm config.ResolvedMeetingsLLM, if llm.CLI.Home != "" { env = append(env, meetllm.EnvCLIHome+"="+llm.CLI.Home) } - // The CLI's config-dir override is a path, not a credential. - if v := getenv("CLAUDE_CONFIG_DIR"); v != "" { - env = append(env, "CLAUDE_CONFIG_DIR="+v) + // The CLI's sign-in, as the agents get it: a config-dir path, or a + // token, kept off argv like the API keys above. + signIn := meetingCLIGetenv(getenv) + for _, k := range meetingCLISignInEnv[llm.AgentBackend] { + if v := signIn(k); v != "" { + env = append(env, k+"="+v) + } } case config.MeetingsLLMModeAnthropic: env = append(env, "SPEK_MEETING_LLM=anthropic", "ANTHROPIC_API_KEY="+llm.AnthropicKey, diff --git a/src/cmd/hive/meetingswire_signin_test.go b/src/cmd/hive/meetingswire_signin_test.go new file mode 100644 index 0000000000..c1348f50ec --- /dev/null +++ b/src/cmd/hive/meetingswire_signin_test.go @@ -0,0 +1,132 @@ +package main + +import ( + "log/slog" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/meetings" + "github.com/hivecommons/hive/pkg/meetings/meetllm" +) + +// withMeetingSignIn stubs the operator Claude token and the dashboard +// Copilot token file for one test. +func withMeetingSignIn(t *testing.T, claudeToken, copilotFile string) { + t.Helper() + prevClaude, prevFile := meetingClaudeOperatorToken, meetingCopilotTokenFile + meetingClaudeOperatorToken = func() string { return claudeToken } + meetingCopilotTokenFile = copilotFile + t.Cleanup(func() { meetingClaudeOperatorToken, meetingCopilotTokenFile = prevClaude, prevFile }) +} + +func mapEnv(m map[string]string) func(string) string { return func(k string) string { return m[k] } } + +// The meetings CLI gets the agents' sign-in: the operator's Claude token +// (never a stray CLAUDE_CODE_OAUTH_TOKEN of the hive's own), and the +// Copilot token from the environment or the dashboard sign-in file. +func TestMeetingCLIGetenv(t *testing.T) { + dir := t.TempDir() + file := filepath.Join(dir, "copilot-user-token") + if err := os.WriteFile(file, []byte("gho_file\n"), 0o600); err != nil { + t.Fatal(err) + } + withMeetingSignIn(t, "sk-ant-oat-operator", file) + get := meetingCLIGetenv(mapEnv(map[string]string{meetllm.EnvClaudeOAuthToken: "stale", "PATH": "/bin"})) + if get(meetllm.EnvClaudeOAuthToken) != "sk-ant-oat-operator" || get(meetllm.EnvCopilotToken) != "gho_file" || get("PATH") != "/bin" { + t.Fatal("sign-in overlay") + } + if got := meetingCLIGetenv(mapEnv(map[string]string{meetllm.EnvCopilotToken: " gho_env "}))(meetllm.EnvCopilotToken); got != "gho_env" { + t.Fatalf("env copilot token %q", got) + } + withMeetingSignIn(t, "", filepath.Join(dir, "absent")) + get = meetingCLIGetenv(mapEnv(map[string]string{meetllm.EnvClaudeOAuthToken: "stale"})) + if get(meetllm.EnvClaudeOAuthToken) != "" || get(meetllm.EnvCopilotToken) != "" { + t.Fatal("no sign-in configured, yet one was passed") + } +} + +// Spek's `hive meeting-llm` child gets each backend's sign-in variables, +// a gateway's CA bundle, and never the hive's GitHub tokens. +func TestMeetingSpekEnvSignIn(t *testing.T) { + withMeetingSignIn(t, "sk-ant-oat-operator", filepath.Join(t.TempDir(), "absent")) + base := mapEnv(map[string]string{ + "PATH": "/bin", "GH_TOKEN": "ghp_hive", "HIVE_GITHUB_TOKEN": "x", meetllm.EnvCopilotToken: "gho_x", + "OMP_AUTH_BROKER_URL": "https://broker", "OMP_AUTH_BROKER_TOKEN": "obt", + }) + envOf := func(llm config.ResolvedMeetingsLLM) map[string]string { + out := map[string]string{} + for _, kv := range meetingSpekEnv(base, llm, "/hive", "/u", "/s", 0) { + k, v, _ := strings.Cut(kv, "=") + out[k] = v + } + if out["GH_TOKEN"] != "" || out["HIVE_GITHUB_TOKEN"] != "" { + t.Fatalf("hive tokens reached spek: %v", out) + } + return out + } + claudeLLM := cliLLM("", "") + if env := envOf(claudeLLM); env[meetllm.EnvClaudeOAuthToken] != "sk-ant-oat-operator" || env[meetllm.EnvCopilotToken] != "" { + t.Fatalf("claude env %v", env) + } + copilotLLM := cliLLM("", "") + copilotLLM.AgentBackend = meetllm.BackendCopilot + if env := envOf(copilotLLM); env[meetllm.EnvCopilotToken] != "gho_x" || env[meetllm.EnvClaudeOAuthToken] != "" { + t.Fatalf("copilot env %v", env) + } + ompLLM := cliLLM("", "") + ompLLM.AgentBackend = meetllm.BackendOMP + if env := envOf(ompLLM); env["OMP_AUTH_BROKER_URL"] != "https://broker" || env["OMP_AUTH_BROKER_TOKEN"] != "obt" { + t.Fatalf("omp env %v", env) + } + gw := config.ResolvedMeetingsLLM{Backend: config.MeetingsLLMModeOpenAI, Endpoint: "https://gw", Model: "m", CABundle: "/ca.pem", AgentBackend: "corp-gw"} + if env := envOf(gw); env[meetllm.EnvCABundle] != "/ca.pem" || env[meetllm.EnvEndpoint] != "https://gw" { + t.Fatalf("gateway env %v", env) + } + gw.CABundle = "" + if _, ok := envOf(gw)[meetllm.EnvCABundle]; ok { + t.Fatal("empty CA bundle exported") + } +} + +// On a backend with no warm mode the persona runs one CLI per call, with +// the agents' sign-in and the cold timeouts; no process starts up front. +func TestMeetingCLIModelCold(t *testing.T) { + logger := slog.New(slog.DiscardHandler) + llm := cliLLM(filepath.Join(t.TempDir(), "copilot"), "") + llm.AgentBackend = meetllm.BackendCopilot + llm.CLI.AddressedTimeoutMS = config.DefaultMeetingsColdCLIAddressedTimeoutMS + m, ok := meetingCLIModel(llm, meetllm.Config{MaxInFlight: 1}, "m1", logger) + if !ok || m.Config.Session != nil || m.Config.Backend != meetllm.BackendCopilot || m.Config.Getenv == nil || + m.Config.Timeout.Milliseconds() != config.DefaultMeetingsColdCLIAddressedTimeoutMS { + t.Fatalf("cold model %+v %v", m.Config, ok) + } + llm.AgentBackend = "codex" + if _, ok := meetingCLIModel(llm, meetllm.Config{}, "m2", logger); ok { + t.Fatal("codex has no profile") + } + if _, ok := meetingCLIModel(llm, meetllm.Config{}, "m3", nil); ok { + t.Fatal("codex has no profile (no logger)") + } +} + +// A gateway CA bundle that cannot be read leaves the persona without a +// model instead of an unverified client. +func TestMeetingPersonaFactoryBadCABundle(t *testing.T) { + store, err := meetings.NewStore(t.TempDir()) + if err != nil { + t.Fatal(err) + } + cfg := &config.Config{} + persona := cfg.Meetings.Persona.Resolve() + persona.Enabled = true + llm := config.ResolvedMeetingsLLM{Backend: config.MeetingsLLMModeOpenAI, Endpoint: "https://gw", Model: "m", AgentBackend: "corp-gw", + CABundle: filepath.Join(t.TempDir(), "absent.pem"), MaxInFlight: 1} + e := meetingPersonaFactory(cfg, persona, llm, store, nil, nil, "", 0, false, slog.New(slog.DiscardHandler))(meetings.Meeting{ID: "m1"}) + if e == nil { + t.Fatal("want an engine") + } + e.Close() +} diff --git a/src/docs/api-reference/knowledge.md b/src/docs/api-reference/knowledge.md index 9bb4e9cac0..33257dccbe 100644 --- a/src/docs/api-reference/knowledge.md +++ b/src/docs/api-reference/knowledge.md @@ -108,8 +108,8 @@ Part of the [Dashboard REST API reference](../api-reference.md); see that page f | `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` | -| `GET` | `/api/meetings/model` | Owner or read-write | The meetings model as set and in effect: `mode`, `backend`, `model`, `label` (what the meeting panel shows), `modes`, `supported_backends` (backends that can answer for meetings), `agent_backends` (backends the hive's agents use), `default_backend`, `default_model`, `default_models` and `env_overrides`. See [Agent backends](../meetings.md#agent-backends) | `pkg/dashboard/api_meetings_model.go:58` | -| `PUT` | `/api/meetings/model` | Owner only | Save `{mode?, backend?, model?}` into `meetings.llm`; applies after a restart (`restart_required`). 400 for an unknown mode or a backend with no meetings completion profile. Audited as `meetings_model_settings` | `pkg/dashboard/api_meetings_model.go:59` | +| `GET` | `/api/meetings/model` | Owner or read-write | The meetings model as set and in effect: `mode`, `backend`, `model`, `label` (what the meeting panel shows), `modes`, `supported_backends` (backends that can answer for meetings, named model gateways included), `refused_backends` (why each other known backend cannot), `agent_backends` (backends the hive's agents use), `default_backend`, `default_model`, `default_models` and `env_overrides`. See [Agent backends](../meetings.md#agent-backends) | `pkg/dashboard/api_meetings_model.go:63` | +| `PUT` | `/api/meetings/model` | Owner only | Save `{mode?, backend?, model?}` into `meetings.llm`; applies after a restart (`restart_required`). 400 for an unknown mode or a backend that cannot answer for meetings, with the reason. Audited as `meetings_model_settings` | `pkg/dashboard/api_meetings_model.go:64` | | `POST` | `/api/meetings/actions/{aid}/edit` | Owner or read-write | Change a pending or failed draft: `{type?, title?, body?, repo?}`; the repo must be a monitored one. 404 unknown, 409 already decided. Audited as `meeting_action_edit` | `pkg/dashboard/api_meetings_actions.go:72` | | `POST` | `/api/meetings/actions/{aid}/approve` | Owner or read-write | Create the draft (issue or RFC on GitHub, Spek session through the Inbox `spek_rfc` destination, knowledge note in the `meetings` vault) and report the result to the meeting. Returns the draft, `done` with `result_url` or `failed` with `error`. 409 when it is not pending or failed, or action requests are off. Audited as `meeting_action_approve` | `pkg/dashboard/api_meetings_actions.go:73` | | `POST` | `/api/meetings/actions/{aid}/reject` | Owner or read-write | Reject a pending or failed draft; nothing is created. Audited as `meeting_action_reject` | `pkg/dashboard/api_meetings_actions.go:74` | diff --git a/src/docs/meetings.md b/src/docs/meetings.md index 7728496d65..adfca57afe 100644 --- a/src/docs/meetings.md +++ b/src/docs/meetings.md @@ -63,7 +63,7 @@ Settings, **Meeting attendee** edits all of this from the dashboard ([#18396](ht - **Persona.** Preset cards show each persona's flag, language, accent, voice type and voice id. Picking one fills the form; Save stores it. A value equal to the preset's is stored as no override, so the persona keeps following the preset. The voice list shows the Kokoro voices for the chosen language, and changing the language picks a voice of the same type. The intro has a live preview. **Play sample** speaks a short line in the persona's language through the meetings TTS service (the browser plays it from `GET /api/meetings/persona/sample`, using the form's unsaved voice, language and name); until that service is configured (`meetings.tts_url` and its token, phase B) the button is disabled and the API answers "TTS not configured". - **Behaviour.** The unprompted caps above (`per_10m`, `min_gap_s`, `per_meeting`, `answer_wait_s`, `min_confidence`, `queue_depth`, `stale_s`; 0 restores a default) and whether hive may speak aloud when addressed (`meetings.voice_on_address`). -- **Model.** The model Betty and Spek answer with: `meetings.llm.mode`, and on the agent-backend mode the backend and model (`meetings.llm.backend`, `meetings.llm.model`). The backend and model lists are the agents' own (the catalog the agent backend and model pickers use); a backend with no meetings completion profile is listed but cannot be picked. **Default** means the backend most of the hive's agents run on and that backend's meetings default model. Saved on its own with **Save model** (owner, `PUT /api/meetings/model`); it applies after a restart. See [Agent backends](#agent-backends). +- **Model.** The model Betty and Spek answer with: `meetings.llm.mode`, and on the agent-backend mode the backend and model (`meetings.llm.backend`, `meetings.llm.model`). The backend and model lists are the agents' own (the catalog the agent backend and model pickers use); a backend that cannot answer for meetings is listed but cannot be picked, and the section says why. **Default** means the backend most of the hive's agents run on and that backend's meetings default model. Saved on its own with **Save model** (owner, `PUT /api/meetings/model`); it applies after a restart. See [Agent backends](#agent-backends). - **Email.** The attendee address, `hive@`, with a copy button, the people with access to this hive (owner, read-write and read) with their registered calendar emails and proof status, the mailed-in files waiting for approval (invites waiting for approval are in the **Calendar**), **My calendar emails** where each user adds, proves and removes their own addresses (see [Proving calendar emails](#proving-calendar-emails)), and the owner's switches for calendar invites and mailed-in files (`meetings.email.*`, saved with **Save**; a read-write user sees them read-only). The section is live once the hive's calendar email allowlist is wired ([#18395](https://github.com/hivecommons/hive/issues/18395)); before that its controls are disabled. The presets use only voices the self-hosted Kokoro TTS ([Kokoro-FastAPI](https://github.com/remsky/Kokoro-FastAPI) v0.10.0) ships. Leave phrases stay in Latin script because the leave matcher uses ASCII word boundaries, and every preset keeps "hive" as an alias and "hive leave" as a leave phrase. @@ -97,17 +97,34 @@ Hive owns the model credential and the budget; Spektacular never receives an Ope ### Agent backends -`meetings.llm.mode: agent-backend` lets Betty and Spek use a backend and model the hive's agents are configured with ([#18517](https://github.com/hivecommons/hive/issues/18517)); `claude-cli` is the same mode with the `claude` backend. `meetings.llm.backend` names the backend (empty: the backend most of the hive's agents run on) and `meetings.llm.model` the model (empty: `claude-sonnet-5-5` on `claude`, otherwise the model most of that backend's agents use). The `openai`, `anthropic` and `none` modes are unchanged, and `auto` never picks an agent backend. +`meetings.llm.mode: agent-backend` lets Betty and Spek use a backend and model the hive's agents are configured with ([#18517](https://github.com/hivecommons/hive/issues/18517), [#18549](https://github.com/hivecommons/hive/issues/18549)); `claude-cli` is the same mode with the `claude` backend. `meetings.llm.backend` names the backend (empty: the backend most of the hive's agents run on) and `meetings.llm.model` the model (empty: `claude-sonnet-5-5` on `claude`, otherwise the model most of that backend's agents use, then a gateway's `default_model`). The `openai`, `anthropic` and `none` modes are unchanged, and `auto` never picks an agent backend. -Each backend runs in a narrow completion profile: one request per turn, every tool and MCP server off, no hooks, no settings files, no session written, an empty temporary working directory, and a fixed system prompt that treats meeting text as untrusted data. The request's own instructions travel inside the turn. The CLI runs with the shared agent home (`/data/home`, override `HIVE_MEETINGS_CLI_HOME`), so it uses the sign-in the agents use; hive never reads, copies or passes the token itself, and the CLI's environment is an allowlist (path, locale, proxy and CA settings, the CLI's own config directory), so GitHub and hive tokens never reach it. Usage from the CLI's result (input, output and cache tokens) is charged to the meeting's usage ledger, the per-meeting budget and the hive-wide ceilings, as with the API modes, and a call is refused once the budget is spent. +A CLI backend runs in a narrow completion profile: one request per call, every tool and MCP server off, no hooks, no settings files, no session written to the shared home, an empty temporary working directory, and a fixed system prompt that treats meeting text as untrusted data. The request's own instructions travel inside the turn, and the prompt goes in on stdin, never in argv. The CLI's environment is an allowlist (path, locale, proxy and CA settings, the CLI's own config directory and sign-in variables), so GitHub and hive tokens never reach it. Usage from the CLI's result is charged to the meeting's usage ledger, the per-meeting budget and the hive-wide ceilings, as with the API modes, and a call is refused once the budget is spent. + +The CLI uses the sign-in the agents get, without hive logging it or writing it to disk: + +- **claude:** the shared agent home (`/data/home`, override `HIVE_MEETINGS_CLI_HOME`) with its credentials file. On a hive that signs Claude in only with the operator's long-lived token (`HIVE_CLAUDE_OAUTH_TOKEN` or `HIVE_CLAUDE_OAUTH_TOKEN_FILE`), the token reaches the meetings CLI as `CLAUDE_CODE_OAUTH_TOKEN` in its environment, exactly as the agent launcher gives it to claude agents; a `CLAUDE_CODE_OAUTH_TOKEN` in hive's own environment is not passed on. +- **copilot:** `COPILOT_GITHUB_TOKEN`, else the dashboard Copilot sign-in (`/data/copilot-user-token`, read through the keystore), as the agents get it. Copilot runs with its home (`HOME`, `COPILOT_HOME`) in the call's own temporary directory, so no MCP config, plugin or hook of the agents' home loads and its session log is removed with the call. Without a Copilot token the call fails rather than fall back to the shared home. +- **omp:** the fleet's auth broker (`OMP_AUTH_BROKER_URL`, `OMP_AUTH_BROKER_TOKEN`), in an empty `hive-meetings` settings profile. +- **Model gateways** (`litellm`, `vllm`, `llm-d`, named gateways): the gateway's own key, sent as the bearer of the chat completion. | Backend | How it answers | Latency | Status | |---|---|---|---| -| `claude` | Betty: one warm `claude -p --input-format stream-json --output-format stream-json` per meeting, started when the meeting starts; each call is one turn. Spek: one `claude -p --output-format json` per prompt. | Warm turn about 1 s with Sonnet 5.5 (measured 0.9 to 1.2 s on Claude Code 2.1.325); a cold `claude -p` adds the CLI start, several seconds. | Supported. | +| `claude` (2.1.325) | Betty: one warm `claude -p --input-format stream-json --output-format stream-json` per meeting, started when the meeting starts; each call is one turn. Spek: one `claude -p --output-format json` per prompt. | Warm turn about 1 s with Sonnet 5.5 (measured 0.9 to 1.2 s on Claude Code 2.1.325); a cold `claude -p` adds the CLI start, several seconds. | Supported. | +| `copilot` (1.0.88) | One `copilot --output-format json --available-tools --disable-builtin-mcps --no-custom-instructions --no-ask-user --no-auto-update --usage-output-file ` per call for Betty and Spek, the prompt piped on stdin. The reply is the last `assistant.message`; tokens are input plus output per model from the usage report. | No warm mode: about 4.5 s of CLI start and sign-in before the model answers (measured 4.4 to 4.8 s on Copilot CLI 1.0.109 from launch to the model's answer, there a quota refusal), so a reply takes about 5 to 8 s. | Supported. | +| `omp` (18.20.0) | One `omp -p --mode json --no-tools --no-lsp --no-extensions --no-skills --no-rules --no-session --no-title --no-context-files --profile hive-meetings --system-prompt ` per call, prompt on stdin. The reply is the last assistant `message_end`; tokens are its `usage.totalTokens`. Needs a model (the isolated profile has no default). | No warm mode: the CLI start plus one model call per reply, a few seconds; not yet measured against a signed-in CLI. | Supported. | | `litellm` | The OpenAI-compatible path (`governor.litellm`), as `meetings.llm.mode: openai`. | One chat completion. | Supported. | -| `copilot`, `codex`, `gemini`, `omp` and the other CLIs | | | Refused at validation for now: a follow-up adds each CLI whose non-interactive mode can run with no tools at a meeting-friendly latency. | +| `vllm`, `llm-d` | The OpenAI-compatible path at the first endpoint the agents use (`HIVE_VLLM_ENDPOINT`, `HIVE_LLMD_ENDPOINT`, default the in-cluster llm-d endpoint picker), no key. Needs a model: `meetings.llm.model` or the model of the hive's agents on that backend. | One chat completion. | Supported. | +| Named model gateways | Every gateway under Settings, Model Gateways with an endpoint answers through the OpenAI-compatible path with its endpoint, key, `default_model` and `ca_bundle`, and shows in the picker under its name. | One chat completion. | Supported, except watsonx gateways. | +| `codex` | | | Refused: `codex` has no flag that removes its shell tool. With a read-only sandbox, approval policy never and an empty workspace, `codex exec` still lets the model run read-only commands, which can read the CLI's sign-in and other files on the hive and repeat them into a meeting. | +| `gemini` | | | Refused: the gemini CLI is not installed in the hive image (no pinned version in `src/Dockerfile`), has no flag that removes every tool (its non-interactive mode still runs read-only shell and file tools), and stopped serving personal and Google AI Pro accounts on 2026-06-18. | +| `agy`, `bob` | | | Refused: neither has a flag that turns its tools off (the agent launcher cannot pass them tool rules either). | +| `goose`, `aider`, `cline`, `pi`, `opencode`, `kilo`, `muse`, `openhands`, `omnigent` | | | Refused: hive has no verified launch of the CLI that answers one prompt with every tool, MCP server and file write turned off. | +| `watsonx` | | | Refused: watsonx needs a short-lived IAM bearer minted from the IBM Cloud key and a project header, which the meetings OpenAI-compatible path does not send. | + +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` (8000), `HIVE_MEETINGS_CLI_UNPROMPTED_TIMEOUT_MS` (6000) and `HIVE_MEETINGS_CLI_RESTYLE_TIMEOUT_MS` (3000); 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". +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". 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 Spektacular live mode is not running. diff --git a/src/pkg/config/meetings_config.go b/src/pkg/config/meetings_config.go index fa39f21284..3fa746bb35 100644 --- a/src/pkg/config/meetings_config.go +++ b/src/pkg/config/meetings_config.go @@ -346,8 +346,13 @@ type ResolvedMeetingsLLM struct { // Reason explains Backend none. Reason string // AgentBackend is the agent backend (claude, ...) on the agent-backend - // backend; Model is then that backend's model. + // backend; Model is then that backend's model. On the openai backend it + // names the model gateway (vllm, llm-d or a named gateway) the + // agent-backend mode resolved, and is empty otherwise. AgentBackend string + // CABundle is an optional PEM file of private CAs the openai backend's + // endpoint is verified against (a named gateway's ca_bundle). + CABundle string // CLI is the completion-profile launch settings on the agent-backend // backend. CLI ResolvedMeetingsCLI @@ -357,6 +362,9 @@ type ResolvedMeetingsLLM struct { func (r ResolvedMeetingsLLM) Label() string { switch r.Backend { case MeetingsLLMModeOpenAI: + if r.AgentBackend != "" { + return r.AgentBackend + " model " + r.Model + } return "model " + r.Model case MeetingsLLMModeAnthropic: if r.AnthropicModel != "" { diff --git a/src/pkg/config/meetings_llm_agent.go b/src/pkg/config/meetings_llm_agent.go index d3529167e0..8407933a68 100644 --- a/src/pkg/config/meetings_llm_agent.go +++ b/src/pkg/config/meetings_llm_agent.go @@ -8,19 +8,31 @@ import ( "strings" ) -// Meetings agent-backend mode (hivecommons/hive#18517): the meeting persona -// and Spektacular live mode answer through one of the agent backends the -// hive's agents are configured with, in that backend's narrow completion -// profile (single turn, no tools, no MCP, no writes, a fixed system prompt, -// usage taken from the CLI's result). Only backends whose CLI supports such -// a profile at a meeting-friendly latency are accepted; the rest are refused -// at validation with MeetingsLLMBackendRefusal. +// Meetings agent-backend mode (hivecommons/hive#18517, #18549): the meeting +// persona and Spektacular live mode answer through one of the agent backends +// the hive's agents are configured with. A CLI backend answers in its narrow +// completion profile (single turn, no tools, no MCP, no writes, a fixed +// system prompt, usage taken from the CLI's result); a model-gateway backend +// (LiteLLM, vllm, llm-d or a named gateway) answers through the +// OpenAI-compatible path. Every other backend the agents' picker offers is +// refused at validation and in the picker with the reason in +// meetingsRefusedBackends. const ( // MeetingsCLIBackendClaude is the Claude Code CLI backend. MeetingsCLIBackendClaude = "claude" + // MeetingsCLIBackendCopilot is the GitHub Copilot CLI backend. + MeetingsCLIBackendCopilot = "copilot" + // MeetingsCLIBackendOMP is the Oh My Pi CLI backend. + MeetingsCLIBackendOMP = "omp" // MeetingsOpenAIBackendLiteLLM is the LiteLLM backend, answered through // the existing OpenAI-compatible path (governor.litellm). MeetingsOpenAIBackendLiteLLM = "litellm" + // MeetingsOpenAIBackendVLLM and MeetingsOpenAIBackendLLMD are the + // self-hosted inference backends, answered through the OpenAI-compatible + // path at the endpoints the agents use (HIVE_VLLM_ENDPOINT, + // HIVE_LLMD_ENDPOINT). + MeetingsOpenAIBackendVLLM = "vllm" + MeetingsOpenAIBackendLLMD = "llm-d" // DefaultMeetingsClaudeCLIModel is the model the claude backend answers // with when meetings.llm.model is unset: fast enough for a warm turn // inside the persona's addressed timeout. @@ -36,35 +48,79 @@ const ( DefaultMeetingsCLIAddressedTimeoutMS = 8000 DefaultMeetingsCLIUnpromptedTimeoutMS = 6000 DefaultMeetingsCLIRestyleTimeoutMS = 3000 - // DefaultMeetingsCLIOneShotTimeoutMS bounds one cold `claude -p` call + // DefaultMeetingsColdCLIAddressedTimeoutMS, + // DefaultMeetingsColdCLIUnpromptedTimeoutMS and + // DefaultMeetingsColdCLIRestyleTimeoutMS are the same bounds on a CLI + // backend with no warm mode (copilot, omp): every call starts the CLI, + // which costs about four to five seconds before the model answers. + DefaultMeetingsColdCLIAddressedTimeoutMS = 20000 + DefaultMeetingsColdCLIUnpromptedTimeoutMS = 15000 + DefaultMeetingsColdCLIRestyleTimeoutMS = 10000 + // DefaultMeetingsCLIOneShotTimeoutMS bounds one cold CLI call // (Spektacular's `hive meeting-llm`, one process per prompt). DefaultMeetingsCLIOneShotTimeoutMS = 30000 // DefaultMeetingsCLIRecycleTurns is how many turns a warm process // answers before it is replaced, which bounds the history every turn // re-reads (and pays for). DefaultMeetingsCLIRecycleTurns = 8 + // DefaultLLMDEndpoint is the in-cluster llm-d endpoint picker Service + // the agents use when HIVE_LLMD_ENDPOINT is unset. + DefaultLLMDEndpoint = "http://hive-llm-d-epp.hive-inference.svc.cluster.local:8000" ) // Meetings CLI env overrides. const ( - MeetingsCLIHomeEnv = "HIVE_MEETINGS_CLI_HOME" - MeetingsCLIBinaryEnv = "HIVE_MEETINGS_CLI_BINARY" - MeetingsCLIAddressedTimeoutEnv = "HIVE_MEETINGS_CLI_ADDRESSED_TIMEOUT_MS" - MeetingsCLIUnpromptedTimeoutEnv = "HIVE_MEETINGS_CLI_UNPROMPTED_TIMEOUT_MS" - MeetingsCLIRestyleTimeoutEnv = "HIVE_MEETINGS_CLI_RESTYLE_TIMEOUT_MS" - MeetingsCLIOneShotTimeoutEnv = "HIVE_MEETINGS_CLI_ONESHOT_TIMEOUT_MS" - MeetingsCLIRecycleTurnsEnv = "HIVE_MEETINGS_CLI_RECYCLE_TURNS" - meetingsCLIDefaultBinaryForClaude = "claude" + MeetingsCLIHomeEnv = "HIVE_MEETINGS_CLI_HOME" + MeetingsCLIBinaryEnv = "HIVE_MEETINGS_CLI_BINARY" + MeetingsCLIAddressedTimeoutEnv = "HIVE_MEETINGS_CLI_ADDRESSED_TIMEOUT_MS" + MeetingsCLIUnpromptedTimeoutEnv = "HIVE_MEETINGS_CLI_UNPROMPTED_TIMEOUT_MS" + MeetingsCLIRestyleTimeoutEnv = "HIVE_MEETINGS_CLI_RESTYLE_TIMEOUT_MS" + MeetingsCLIOneShotTimeoutEnv = "HIVE_MEETINGS_CLI_ONESHOT_TIMEOUT_MS" + MeetingsCLIRecycleTurnsEnv = "HIVE_MEETINGS_CLI_RECYCLE_TURNS" + // VLLMEndpointEnv and LLMDEndpointEnv are the agents' inference + // endpoints (comma-separated lists; meetings use the first). + VLLMEndpointEnv = "HIVE_VLLM_ENDPOINT" + LLMDEndpointEnv = "HIVE_LLMD_ENDPOINT" ) // MeetingsCompletionBackends are the CLI backends with a meetings -// completion profile. MeetingsOpenAIBackends answer through the -// OpenAI-compatible path. +// completion profile; MeetingsWarmCLIBackends are those that also keep one +// warm process per meeting. MeetingsOpenAIBackends answer through the +// OpenAI-compatible path, as does every named model gateway +// (Config.MeetingsGatewayBackends). var ( - MeetingsCompletionBackends = []string{MeetingsCLIBackendClaude} - MeetingsOpenAIBackends = []string{MeetingsOpenAIBackendLiteLLM} + MeetingsCompletionBackends = []string{MeetingsCLIBackendClaude, MeetingsCLIBackendCopilot, MeetingsCLIBackendOMP} + MeetingsWarmCLIBackends = []string{MeetingsCLIBackendClaude} + MeetingsOpenAIBackends = []string{MeetingsOpenAIBackendLiteLLM, MeetingsOpenAIBackendLLMD, MeetingsOpenAIBackendVLLM} ) +// meetingsNoProfileReason is the refusal of an agentic CLI hive has not +// fitted with a completion profile. +const meetingsNoProfileReason = "hive has no verified launch of this CLI that answers one prompt with every tool, MCP server and file write turned off" + +// meetingsRefusedBackends are the agent backends the agents' picker offers +// that cannot answer for meetings, each with the reason. Tested against +// the agents' backend list so a new backend gets a decision here. +var meetingsRefusedBackends = map[string]string{ + "codex": "codex has no flag that removes its shell tool: even with a read-only sandbox, approval policy never and an empty workspace, " + + "`codex exec` lets the model run read-only commands, which can read the CLI's sign-in and other files on the hive and repeat them into a meeting", + "gemini": "the gemini CLI is not installed in the hive image (src/Dockerfile pins no version), has no flag that removes every tool " + + "(its non-interactive mode still runs read-only shell and file tools), and stopped serving personal and Google AI Pro accounts on 2026-06-18", + "agy": "agy has no flag that turns its tools off (the agent launcher cannot pass it tool rules either)", + "bob": "bob has no flag that turns its tools off (the agent launcher cannot pass it tool rules either)", + "goose": meetingsNoProfileReason, + "aider": meetingsNoProfileReason, + "cline": meetingsNoProfileReason, + "pi": meetingsNoProfileReason, + "opencode": meetingsNoProfileReason, + "kilo": meetingsNoProfileReason, + "muse": meetingsNoProfileReason, + "openhands": meetingsNoProfileReason, + "omnigent": meetingsNoProfileReason, + "watsonx": "watsonx needs a short-lived IAM bearer minted from the IBM Cloud key and a project header, which the meetings " + + "OpenAI-compatible path does not send", +} + // ResolvedMeetingsCLI is how the completion CLI is launched. type ResolvedMeetingsCLI struct { // Binary is the CLI executable (a name looked up in PATH, or a path). @@ -76,16 +132,21 @@ type ResolvedMeetingsCLI struct { RestyleTimeoutMS int OneShotTimeoutMS int RecycleTurns int + // Warm reports whether the backend keeps one warm process per meeting; + // otherwise every call starts the CLI. + Warm bool } // MeetingsLLMBackendSupported reports whether backend can answer for -// meetings in the agent-backend mode. +// meetings in the agent-backend mode on any hive (named gateways depend on +// the config: see Config.MeetingsLLMBackendRefusalFor). func MeetingsLLMBackendSupported(backend string) bool { b := strings.ToLower(strings.TrimSpace(backend)) return meetingsContains(MeetingsCompletionBackends, b) || meetingsContains(MeetingsOpenAIBackends, b) } -// MeetingsLLMSupportedBackends lists the accepted backends, sorted. +// MeetingsLLMSupportedBackends lists the backends accepted on every hive, +// sorted. func MeetingsLLMSupportedBackends() []string { out := append(append([]string{}, MeetingsCompletionBackends...), MeetingsOpenAIBackends...) sort.Strings(out) @@ -94,10 +155,29 @@ func MeetingsLLMSupportedBackends() []string { // MeetingsLLMBackendRefusal explains why backend cannot answer for meetings. func MeetingsLLMBackendRefusal(backend string) string { - return fmt.Sprintf("meetings.llm.backend %q has no meetings completion profile yet: hive only answers meetings through backends whose CLI runs a fast single-turn completion with no tools, no MCP servers and no writes (supported: %s); pick one of those, or use meetings.llm.mode openai or anthropic", + b := strings.ToLower(strings.TrimSpace(backend)) + if why, ok := meetingsRefusedBackends[b]; ok { + return meetingsRefusal(backend, why) + } + return fmt.Sprintf("meetings.llm.backend %q is neither an agent backend with a meetings completion profile nor a configured model gateway (supported: %s, or a gateway under Settings, Model Gateways); pick one of those, or use meetings.llm.mode openai or anthropic", backend, strings.Join(MeetingsLLMSupportedBackends(), ", ")) } +func meetingsRefusal(backend, why string) string { + return fmt.Sprintf("meetings.llm.backend %q cannot answer for meetings: %s; pick one of %s or a model gateway, or use meetings.llm.mode openai or anthropic", + backend, why, strings.Join(MeetingsLLMSupportedBackends(), ", ")) +} + +// MeetingsRefusedBackendReasons is every known agent backend that cannot +// answer for meetings, with the reason (for the Settings picker). +func MeetingsRefusedBackendReasons() map[string]string { + out := make(map[string]string, len(meetingsRefusedBackends)) + for k, v := range meetingsRefusedBackends { + out[k] = v + } + return out +} + func meetingsContains(list []string, v string) bool { for _, x := range list { if x == v { @@ -108,8 +188,10 @@ func meetingsContains(list []string, v string) bool { } // validateMeetingsAgentBackend checks the backend named for the -// agent-backend and claude-cli modes (env included). An empty backend is -// resolved from the hive's agents at boot. +// agent-backend and claude-cli modes (env included) without the rest of the +// config: a known refused backend fails here, and a name that may be a +// model gateway is checked by Config.validateMeetingsGatewayBackend. An +// empty backend is resolved from the hive's agents at boot. func validateMeetingsAgentBackend(mode, backend string) error { b := strings.ToLower(meetingsEnvString(MeetingsLLMBackendEnv, backend, "")) if mode == MeetingsLLMModeClaudeCLI { @@ -118,12 +200,82 @@ func validateMeetingsAgentBackend(mode, backend string) error { } return nil } - if b != "" && !MeetingsLLMBackendSupported(b) { + if _, refused := meetingsRefusedBackends[b]; refused { return fmt.Errorf("%s", MeetingsLLMBackendRefusal(b)) } return nil } +// validateMeetingsGatewayBackend checks, with the gateways in view, that a +// meetings.llm.backend which is not a built-in backend names a usable model +// gateway. +func (c *Config) validateMeetingsGatewayBackend() error { + if !c.Meetings.IsEnabled() { + return nil + } + mode := strings.ToLower(meetingsEnvString(MeetingsLLMModeEnv, c.Meetings.LLM.Mode, MeetingsLLMModeAuto)) + if mode != MeetingsLLMModeAgentBackend { + return nil + } + b := strings.ToLower(meetingsEnvString(MeetingsLLMBackendEnv, c.Meetings.LLM.Backend, "")) + if why := c.MeetingsLLMBackendRefusalFor(b); why != "" { + return fmt.Errorf("%s", why) + } + return nil +} + +// MeetingsGatewayBackends are the named model gateways (Settings, Model +// Gateways) that can answer for meetings, sorted: every explicitly +// configured gateway with an endpoint except watsonx ones, under names that +// are not built-in backends. +func (c *Config) MeetingsGatewayBackends() []string { + if c == nil { + return nil + } + var out []string + for _, gw := range c.Governor.Gateways { + name := strings.ToLower(strings.TrimSpace(gw.Name)) + if name == "" || strings.TrimSpace(gw.Endpoint) == "" || strings.EqualFold(gw.Kind, GatewayKindWatsonx) || + MeetingsLLMBackendSupported(name) || meetingsRefusedBackends[name] != "" || meetingsContains(out, name) { + continue + } + out = append(out, name) + } + sort.Strings(out) + return out +} + +// MeetingsLLMSupportedBackendsFor is MeetingsLLMSupportedBackends plus the +// hive's named model gateways, sorted. +func (c *Config) MeetingsLLMSupportedBackendsFor() []string { + out := append(MeetingsLLMSupportedBackends(), c.MeetingsGatewayBackends()...) + sort.Strings(out) + return out +} + +// MeetingsLLMBackendRefusalFor is why backend cannot answer for meetings on +// this hive, or "" when it can (an empty backend follows the agents). +func (c *Config) MeetingsLLMBackendRefusalFor(backend string) string { + b := strings.ToLower(strings.TrimSpace(backend)) + if b == "" || MeetingsLLMBackendSupported(b) || meetingsContains(c.MeetingsGatewayBackends(), b) { + return "" + } + if c != nil { + for _, gw := range c.Governor.Gateways { + if !strings.EqualFold(strings.TrimSpace(gw.Name), b) { + continue + } + if strings.EqualFold(gw.Kind, GatewayKindWatsonx) { + return meetingsRefusal(backend, "it is a watsonx gateway: "+meetingsRefusedBackends[GatewayKindWatsonx]) + } + if strings.TrimSpace(gw.Endpoint) == "" { + return fmt.Sprintf("meetings.llm.backend %q is a model gateway with no endpoint; set its endpoint under Settings, Model Gateways", backend) + } + } + } + return MeetingsLLMBackendRefusal(b) +} + // MeetingsAgentDefault is the backend most of the hive's agents run on // (ties broken by name, so the choice is stable across boots) and the model // most of those agents use. Empty when no agent names a backend. @@ -199,39 +351,98 @@ func (c *Config) resolveMeetingsAgentBackend(ctx context.Context, mode string, r case backend == "": none.Reason = "rules only, no agent backend configured (set meetings.llm.backend)" return none - case meetingsContains(MeetingsOpenAIBackends, backend): + case backend == MeetingsOpenAIBackendLiteLLM: def := c.MeetingsDefaultModelFor(backend) if def == "" { def = strings.TrimSpace(c.Governor.LiteLLM.DefaultModel) } return c.resolveMeetingsOpenAI(ctx, r, def) - case backend == MeetingsCLIBackendClaude: + case backend == MeetingsOpenAIBackendVLLM || backend == MeetingsOpenAIBackendLLMD: + return c.resolveMeetingsInference(r, backend) + case meetingsContains(c.MeetingsGatewayBackends(), backend): + return c.resolveMeetingsGateway(r, backend) + case meetingsContains(MeetingsCompletionBackends, backend): r.Backend, r.AgentBackend = MeetingsLLMModeAgentBackend, backend - r.Model = meetingsEnvString(MeetingsLLMModelEnv, l.Model, DefaultMeetingsClaudeCLIModel) - r.CLI = resolveMeetingsCLI(meetingsCLIDefaultBinaryForClaude) + r.Model = meetingsEnvString(MeetingsLLMModelEnv, l.Model, c.MeetingsDefaultModelFor(backend)) + if r.Model == "" && backend == MeetingsCLIBackendOMP { + // omp runs in an isolated profile with no default model. + none.Reason = "rules only, backend omp needs a model (set meetings.llm.model or give the omp agents one)" + return none + } + r.CLI = resolveMeetingsCLI(backend) return r default: - none.Reason = "rules only, backend " + backend + " has no meetings completion profile yet" + none.Reason = "rules only, backend " + backend + " cannot answer for meetings" return none } } -// resolveMeetingsCLI resolves the CLI launch settings. -func resolveMeetingsCLI(defaultBinary string) ResolvedMeetingsCLI { +// resolveMeetingsInference resolves vllm and llm-d onto the +// OpenAI-compatible path at the first endpoint the agents use. These +// endpoints are unauthenticated, like the agents' route to them. +func (c *Config) resolveMeetingsInference(r ResolvedMeetingsLLM, backend string) ResolvedMeetingsLLM { + raw := strings.TrimSpace(os.Getenv(VLLMEndpointEnv)) + if backend == MeetingsOpenAIBackendLLMD { + raw = meetingsEnvString(LLMDEndpointEnv, "", DefaultLLMDEndpoint) + } + endpoint, _, _ := strings.Cut(raw, ",") + return c.meetingsEndpointBackend(r, backend, strings.TrimSpace(endpoint), "", "", "") +} + +// resolveMeetingsGateway resolves a named model gateway onto the +// OpenAI-compatible path with the gateway's endpoint, key and CA bundle. +func (c *Config) resolveMeetingsGateway(r ResolvedMeetingsLLM, name string) ResolvedMeetingsLLM { + gw := c.Governor.ResolveGateway(name) + if gw == nil { + return ResolvedMeetingsLLM{Backend: MeetingsLLMModeNone, Reason: "rules only, model gateway " + name + " is not configured"} + } + return c.meetingsEndpointBackend(r, name, gw.Endpoint, gw.ResolveAPIKey(), gw.DefaultModel, gw.CABundle) +} + +// meetingsEndpointBackend fills an OpenAI-compatible model for an agent +// backend: the model is meetings.llm.model, else the one the hive's agents +// on that backend use, else fallbackModel. +func (c *Config) meetingsEndpointBackend(r ResolvedMeetingsLLM, backend, endpoint, key, fallbackModel, caBundle string) ResolvedMeetingsLLM { + endpoint = strings.TrimRight(strings.TrimSpace(endpoint), "/") + model := meetingsEnvString(MeetingsLLMModelEnv, c.Meetings.LLM.Model, c.MeetingsDefaultModelFor(backend)) + if model == "" { + model = strings.TrimSpace(fallbackModel) + } + switch { + case endpoint == "": + return ResolvedMeetingsLLM{Backend: MeetingsLLMModeNone, Reason: "rules only, backend " + backend + " has no endpoint configured"} + case model == "": + return ResolvedMeetingsLLM{Backend: MeetingsLLMModeNone, Reason: "rules only, backend " + backend + " needs a model (set meetings.llm.model)"} + } + r.Backend, r.AgentBackend = MeetingsLLMModeOpenAI, backend + r.Endpoint, r.APIKey, r.Model, r.CABundle = endpoint, key, model, strings.TrimSpace(caBundle) + return r +} + +// resolveMeetingsCLI resolves the CLI launch settings for backend. A +// backend with no warm mode starts the CLI on every call, so its persona +// timeouts default higher. +func resolveMeetingsCLI(backend string) ResolvedMeetingsCLI { home := meetingsEnvString(MeetingsCLIHomeEnv, "", "") if home == "" { if st, err := os.Stat(DefaultMeetingsCLIHome); err == nil && st.IsDir() { home = DefaultMeetingsCLIHome } } + warm := meetingsContains(MeetingsWarmCLIBackends, backend) + addressed, unprompted, restyle := DefaultMeetingsCLIAddressedTimeoutMS, DefaultMeetingsCLIUnpromptedTimeoutMS, DefaultMeetingsCLIRestyleTimeoutMS + if !warm { + addressed, unprompted, restyle = DefaultMeetingsColdCLIAddressedTimeoutMS, DefaultMeetingsColdCLIUnpromptedTimeoutMS, DefaultMeetingsColdCLIRestyleTimeoutMS + } return ResolvedMeetingsCLI{ - Binary: meetingsEnvString(MeetingsCLIBinaryEnv, "", defaultBinary), + Binary: meetingsEnvString(MeetingsCLIBinaryEnv, "", backend), Home: home, - AddressedTimeoutMS: meetingsEnvInt(MeetingsCLIAddressedTimeoutEnv, 0, DefaultMeetingsCLIAddressedTimeoutMS), - UnpromptedTimeoutMS: meetingsEnvInt(MeetingsCLIUnpromptedTimeoutEnv, 0, DefaultMeetingsCLIUnpromptedTimeoutMS), - RestyleTimeoutMS: meetingsEnvInt(MeetingsCLIRestyleTimeoutEnv, 0, DefaultMeetingsCLIRestyleTimeoutMS), + AddressedTimeoutMS: meetingsEnvInt(MeetingsCLIAddressedTimeoutEnv, 0, addressed), + UnpromptedTimeoutMS: meetingsEnvInt(MeetingsCLIUnpromptedTimeoutEnv, 0, unprompted), + RestyleTimeoutMS: meetingsEnvInt(MeetingsCLIRestyleTimeoutEnv, 0, restyle), OneShotTimeoutMS: meetingsEnvInt(MeetingsCLIOneShotTimeoutEnv, 0, DefaultMeetingsCLIOneShotTimeoutMS), RecycleTurns: meetingsEnvInt(MeetingsCLIRecycleTurnsEnv, 0, DefaultMeetingsCLIRecycleTurns), + Warm: warm, } } @@ -258,3 +469,17 @@ func ValidateMeetingsLLMChoice(mode, backend string) error { } return nil } + +// ValidateMeetingsLLMSelection is ValidateMeetingsLLMChoice with this hive's +// model gateways accepted as backends. +func (c *Config) ValidateMeetingsLLMSelection(mode, backend string) error { + m := strings.ToLower(strings.TrimSpace(mode)) + b := strings.ToLower(strings.TrimSpace(backend)) + if m == MeetingsLLMModeAgentBackend && b != "" { + if why := c.MeetingsLLMBackendRefusalFor(b); why != "" { + return fmt.Errorf("%s", why) + } + return nil + } + return ValidateMeetingsLLMChoice(mode, backend) +} diff --git a/src/pkg/config/meetings_llm_agent_test.go b/src/pkg/config/meetings_llm_agent_test.go index ba1238f9c2..e8abaf12f4 100644 --- a/src/pkg/config/meetings_llm_agent_test.go +++ b/src/pkg/config/meetings_llm_agent_test.go @@ -5,6 +5,8 @@ import ( "path/filepath" "strings" "testing" + + cruxconfig "github.com/hivecommons/hive/crux/config" ) func meetingsAgentCfg(t *testing.T) *Config { @@ -71,9 +73,24 @@ func TestResolveMeetingsLLMAgentBackendChoices(t *testing.T) { t.Fatalf("litellm agent model %+v", r) } cfg.Meetings.LLM.Backend = "copilot" - if r := cfg.ResolveMeetingsLLM(ctx); r.Backend != MeetingsLLMModeNone || !strings.Contains(r.Reason, "copilot") { + r := cfg.ResolveMeetingsLLM(ctx) + if r.Backend != MeetingsLLMModeAgentBackend || r.AgentBackend != "copilot" || r.Model != "gpt-x" || r.CLI.Warm || + r.CLI.Binary != "copilot" || r.CLI.AddressedTimeoutMS != DefaultMeetingsColdCLIAddressedTimeoutMS || + r.CLI.UnpromptedTimeoutMS != DefaultMeetingsColdCLIUnpromptedTimeoutMS || r.CLI.RestyleTimeoutMS != DefaultMeetingsColdCLIRestyleTimeoutMS { t.Fatalf("copilot %+v", r) } + cfg.Meetings.LLM.Backend = "omp" + if r := cfg.ResolveMeetingsLLM(ctx); r.Backend != MeetingsLLMModeNone || !strings.Contains(r.Reason, "omp needs a model") { + t.Fatalf("omp without a model %+v", r) + } + cfg.Agents["o"] = AgentConfig{Backend: "omp", Model: "anthropic/claude-sonnet-5-5"} + if r := cfg.ResolveMeetingsLLM(ctx); r.AgentBackend != "omp" || r.Model != "anthropic/claude-sonnet-5-5" || r.CLI.Warm { + t.Fatalf("omp %+v", r) + } + cfg.Meetings.LLM.Backend = "codex" + if r := cfg.ResolveMeetingsLLM(ctx); r.Backend != MeetingsLLMModeNone || !strings.Contains(r.Reason, "codex") { + t.Fatalf("codex %+v", r) + } t.Setenv(MeetingsLLMBackendEnv, "claude") if r := cfg.ResolveMeetingsLLM(ctx); r.AgentBackend != "claude" { t.Fatalf("env backend %+v", r) @@ -118,7 +135,8 @@ func TestMeetingsAgentBackendValidation(t *testing.T) { llm MeetingsLLMConfig want string }{ - {MeetingsLLMConfig{Mode: "agent-backend", Backend: "codex"}, "no meetings completion profile"}, + {MeetingsLLMConfig{Mode: "agent-backend", Backend: "codex"}, "cannot answer for meetings"}, + {MeetingsLLMConfig{Mode: "agent-backend", Backend: "gemini"}, "not installed in the hive image"}, {MeetingsLLMConfig{Mode: "claude-cli", Backend: "copilot"}, "always uses the claude backend"}, {MeetingsLLMConfig{Mode: "gpt"}, "agent-backend, claude-cli"}, } { @@ -129,15 +147,20 @@ func TestMeetingsAgentBackendValidation(t *testing.T) { } for _, llm := range []MeetingsLLMConfig{ {Mode: "agent-backend"}, {Mode: "agent-backend", Backend: "claude"}, {Mode: "agent-backend", Backend: "litellm"}, + {Mode: "agent-backend", Backend: "copilot"}, {Mode: "agent-backend", Backend: "omp"}, {Mode: "agent-backend", Backend: "vllm"}, + {Mode: "agent-backend", Backend: "llm-d"}, {Mode: "agent-backend", Backend: "corp-gw"}, {Mode: "claude-cli"}, {Mode: "claude-cli", Backend: "claude"}, } { if err := (MeetingsConfig{Enabled: true, LLM: llm}).Validate(); err != nil { t.Errorf("%+v: %v", llm, err) } } - if !strings.Contains(MeetingsLLMBackendRefusal("gemini"), "claude, litellm") { + if !strings.Contains(MeetingsLLMBackendRefusal("gemini"), "claude, copilot, litellm, llm-d, omp, vllm") { t.Fatal(MeetingsLLMBackendRefusal("gemini")) } + if !strings.Contains(MeetingsLLMBackendRefusal("nope"), "configured model gateway") { + t.Fatal(MeetingsLLMBackendRefusal("nope")) + } } func TestValidateMeetingsLLMChoice(t *testing.T) { @@ -165,3 +188,138 @@ func TestResolveMeetingsCLIHome(t *testing.T) { t.Fatalf("home %q", r.Home) } } + +// vllm and llm-d answer through the OpenAI-compatible path at the agents' +// first endpoint, keyless; a named gateway brings its endpoint, key, model +// and CA bundle; a missing endpoint or model is rules only with a reason. +func TestResolveMeetingsLLMInferenceAndGateways(t *testing.T) { + ctx := context.Background() + cfg := meetingsAgentCfg(t) + cfg.Meetings.LLM.Mode = MeetingsLLMModeAgentBackend + cfg.Agents["v"] = AgentConfig{Backend: "vllm", Model: "llama-4"} + cfg.Meetings.LLM.Backend = "vllm" + t.Setenv(VLLMEndpointEnv, " http://vllm-a:8000/ , http://vllm-b:8000") + r := cfg.ResolveMeetingsLLM(ctx) + if r.Backend != MeetingsLLMModeOpenAI || r.Endpoint != "http://vllm-a:8000" || r.Model != "llama-4" || r.APIKey != "" || + r.AgentBackend != "vllm" || r.Label() != "vllm model llama-4" { + t.Fatalf("vllm %+v %q", r, r.Label()) + } + t.Setenv(VLLMEndpointEnv, "") + if r := cfg.ResolveMeetingsLLM(ctx); r.Backend != MeetingsLLMModeNone || !strings.Contains(r.Reason, "no endpoint") { + t.Fatalf("vllm without endpoint %+v", r) + } + cfg.Meetings.LLM.Backend = "llm-d" + t.Setenv(LLMDEndpointEnv, "") + if r := cfg.ResolveMeetingsLLM(ctx); r.Backend != MeetingsLLMModeNone || !strings.Contains(r.Reason, "needs a model") { + t.Fatalf("llm-d without model %+v", r) + } + cfg.Meetings.LLM.Model = "qwen-3" + if r := cfg.ResolveMeetingsLLM(ctx); r.Endpoint != DefaultLLMDEndpoint || r.Model != "qwen-3" { + t.Fatalf("llm-d %+v", r) + } + cfg.Meetings.LLM.Model = "" + t.Setenv("CORP_GW_KEY", "sk-corp") + cfg.Governor.Gateways = []GatewayConfig{{Name: "Corp-GW", Kind: "litellm", Endpoint: "https://gw.example/v1/", APIKeyEnv: "CORP_GW_KEY", + DefaultModel: "gw-default", CABundle: "/etc/ca.pem"}} + cfg.Meetings.LLM.Backend = "corp-gw" + r = cfg.ResolveMeetingsLLM(ctx) + if r.Backend != MeetingsLLMModeOpenAI || r.Endpoint != "https://gw.example/v1" || r.APIKey != "sk-corp" || r.Model != "gw-default" || + r.CABundle != "/etc/ca.pem" || r.AgentBackend != "corp-gw" { + t.Fatalf("gateway %+v", r) + } + if r := cfg.resolveMeetingsGateway(ResolvedMeetingsLLM{}, "absent"); r.Backend != MeetingsLLMModeNone || !strings.Contains(r.Reason, "not configured") { + t.Fatalf("absent gateway %+v", r) + } +} + +func TestMeetingsGatewayBackends(t *testing.T) { + if got := (*Config)(nil).MeetingsGatewayBackends(); got != nil { + t.Fatal(got) + } + cfg := &Config{} + cfg.Governor.Gateways = []GatewayConfig{ + {Name: "zeta", Endpoint: "https://z"}, + {Name: "Alpha", Endpoint: "https://a"}, + {Name: "alpha", Endpoint: "https://a2"}, + {Name: "ibm", Kind: GatewayKindWatsonx, Endpoint: "https://w"}, + {Name: "empty"}, + {Name: "litellm", Endpoint: "https://l"}, + {Name: "codex", Endpoint: "https://c"}, + {Name: "", Endpoint: "https://n"}, + } + if got := strings.Join(cfg.MeetingsGatewayBackends(), ","); got != "alpha,zeta" { + t.Fatalf("gateways %q", got) + } + if got := strings.Join(cfg.MeetingsLLMSupportedBackendsFor(), ","); got != "alpha,claude,copilot,litellm,llm-d,omp,vllm,zeta" { + t.Fatalf("supported %q", got) + } + for backend, want := range map[string]string{ + "": "", "claude": "", "vllm": "", "zeta": "", "ALPHA": "", + "ibm": "watsonx gateway", "empty": "no endpoint", "codex": "shell tool", "nope": "configured model gateway", + } { + got := cfg.MeetingsLLMBackendRefusalFor(backend) + if (want == "") != (got == "") || !strings.Contains(got, want) { + t.Errorf("%q: refusal %q, want %q", backend, got, want) + } + } + if got := (*Config)(nil).MeetingsLLMBackendRefusalFor("nope"); !strings.Contains(got, "configured model gateway") { + t.Fatal(got) + } +} + +func TestValidateMeetingsGatewayBackend(t *testing.T) { + cfg := &Config{} + cfg.Governor.Gateways = []GatewayConfig{{Name: "corp-gw", Endpoint: "https://gw"}} + cfg.Meetings.LLM = MeetingsLLMConfig{Mode: MeetingsLLMModeAgentBackend, Backend: "nope"} + if err := cfg.validateMeetingsGatewayBackend(); err != nil { + t.Fatalf("disabled meetings rejected: %v", err) + } + cfg.Meetings.Enabled = true + if err := cfg.validateMeetingsGatewayBackend(); err == nil || !strings.Contains(err.Error(), "configured model gateway") { + t.Fatalf("unknown backend err %v", err) + } + cfg.Meetings.LLM.Backend = "corp-gw" + if err := cfg.validateMeetingsGatewayBackend(); err != nil { + t.Fatal(err) + } + cfg.Meetings.LLM = MeetingsLLMConfig{Mode: MeetingsLLMModeAuto, Backend: "nope"} + if err := cfg.validateMeetingsGatewayBackend(); err != nil { + t.Fatalf("auto mode checked the backend: %v", err) + } +} + +func TestValidateMeetingsLLMSelection(t *testing.T) { + cfg := &Config{} + cfg.Governor.Gateways = []GatewayConfig{{Name: "corp-gw", Endpoint: "https://gw"}} + for _, ok := range [][2]string{{"agent-backend", "Corp-GW"}, {"agent-backend", "copilot"}, {"agent-backend", ""}, {"auto", ""}} { + if err := cfg.ValidateMeetingsLLMSelection(ok[0], ok[1]); err != nil { + t.Errorf("%v: %v", ok, err) + } + } + for _, bad := range [][2]string{{"agent-backend", "codex"}, {"agent-backend", "nope"}, {"claude-cli", "copilot"}, {"gpt", ""}} { + if err := cfg.ValidateMeetingsLLMSelection(bad[0], bad[1]); err == nil { + t.Errorf("%v accepted", bad) + } + } +} + +// Every backend the agents can run on either answers for meetings or has a +// refusal reason, and none is both. +func TestMeetingsBackendDecisions(t *testing.T) { + for _, b := range append(append([]string{}, cruxconfig.CLIBackends...), cruxconfig.InferenceBackends...) { + _, refused := meetingsRefusedBackends[b] + if MeetingsLLMBackendSupported(b) == refused { + t.Errorf("backend %q: supported %v, refused %v", b, MeetingsLLMBackendSupported(b), refused) + } + } + reasons := MeetingsRefusedBackendReasons() + reasons["codex"] = "changed" + if meetingsRefusedBackends["codex"] == "changed" { + t.Fatal("reasons not copied") + } + for _, b := range MeetingsWarmCLIBackends { + if !meetingsContains(MeetingsCompletionBackends, b) { + t.Errorf("warm backend %q has no completion profile", b) + } + } +} diff --git a/src/pkg/config/validate.go b/src/pkg/config/validate.go index d5c9d8f69f..46d16f27a1 100644 --- a/src/pkg/config/validate.go +++ b/src/pkg/config/validate.go @@ -113,6 +113,9 @@ func (c *Config) ValidateWithOptions(opts ValidateOptions) error { if err := c.Meetings.Validate(); err != nil { return err } + if err := c.validateMeetingsGatewayBackend(); err != nil { + return err + } if err := c.Review.ValidateFixerFallback(); err != nil { return err } diff --git a/src/pkg/dashboard/api_meetings_model.go b/src/pkg/dashboard/api_meetings_model.go index c5674c3bbb..ff882bdf86 100644 --- a/src/pkg/dashboard/api_meetings_model.go +++ b/src/pkg/dashboard/api_meetings_model.go @@ -30,9 +30,14 @@ type MeetingsModelView struct { Label string `json:"label"` // Modes are the selectable meetings.llm.mode values. Modes []string `json:"modes"` - // SupportedBackends can answer for meetings; any other backend is - // listed by the picker but cannot be chosen. + // SupportedBackends can answer for meetings (CLI backends with a + // completion profile, LiteLLM, vllm, llm-d and the hive's named model + // gateways); any other backend is listed by the picker but cannot be + // chosen. SupportedBackends []string `json:"supported_backends"` + // RefusedBackends says, per known backend that cannot answer for + // meetings, why (#18549). + RefusedBackends map[string]string `json:"refused_backends"` // AgentBackends are the backends the hive's agents are configured with. AgentBackends []string `json:"agent_backends"` // DefaultBackend and DefaultModel apply when backend and model are @@ -66,7 +71,8 @@ func meetingsModelView(r *http.Request, cfg *config.Config, getenv func(string) Mode: l.Mode, Backend: l.Backend, Model: l.Model, Label: cfg.ResolveMeetingsLLM(r.Context()).Label(), Modes: append([]string{}, config.MeetingsLLMModes...), - SupportedBackends: config.MeetingsLLMSupportedBackends(), + SupportedBackends: cfg.MeetingsLLMSupportedBackendsFor(), + RefusedBackends: config.MeetingsRefusedBackendReasons(), AgentBackends: []string{}, DefaultBackend: defBackend, DefaultModel: cfg.MeetingsDefaultModelFor(defBackend), @@ -85,6 +91,16 @@ func meetingsModelView(r *http.Request, cfg *config.Config, getenv func(string) } } sort.Strings(v.AgentBackends) + // A gateway the picker lists that cannot answer (a watsonx one, or one + // with no endpoint) says why too. + for _, gw := range cfg.Governor.Gateways { + name := strings.ToLower(strings.TrimSpace(gw.Name)) + if why := cfg.MeetingsLLMBackendRefusalFor(name); name != "" && why != "" { + if _, known := v.RefusedBackends[name]; !known { + v.RefusedBackends[name] = why + } + } + } for _, b := range v.SupportedBackends { v.DefaultModels[b] = cfg.MeetingsDefaultModelFor(b) } @@ -104,7 +120,8 @@ func (s *Server) handleMeetingsModelGet(w http.ResponseWriter, r *http.Request) } // handleMeetingsModelPut saves meetings.llm.mode, backend and model (owner -// only). A backend without a meetings completion profile is refused. +// only). A backend that cannot answer for meetings is refused with the +// reason. func (s *Server) handleMeetingsModelPut(w http.ResponseWriter, r *http.Request) { if !requireOwnerRole(w, r) { return @@ -127,7 +144,7 @@ func (s *Server) handleMeetingsModelPut(w http.ResponseWriter, r *http.Request) if req.Model != nil { next.Model = strings.TrimSpace(*req.Model) } - if err := config.ValidateMeetingsLLMChoice(next.Mode, next.Backend); err != nil { + if err := s.deps.Config.ValidateMeetingsLLMSelection(next.Mode, next.Backend); err != nil { jsonError(w, err.Error(), http.StatusBadRequest) return } diff --git a/src/pkg/dashboard/api_meetings_model_test.go b/src/pkg/dashboard/api_meetings_model_test.go index 28d31becdd..3162eaa9f7 100644 --- a/src/pkg/dashboard/api_meetings_model_test.go +++ b/src/pkg/dashboard/api_meetings_model_test.go @@ -38,7 +38,7 @@ func TestMeetingsModelAPI(t *testing.T) { } code, v := meetingsModelGet(t, s, "read-write", false) if code != http.StatusOK || v.Mode != config.MeetingsLLMModeAuto || v.DefaultBackend != "claude" || - v.DefaultModel != config.DefaultMeetingsClaudeCLIModel || strings.Join(v.SupportedBackends, ",") != "claude,litellm" || + v.DefaultModel != config.DefaultMeetingsClaudeCLIModel || strings.Join(v.SupportedBackends, ",") != "claude,copilot,litellm,llm-d,omp,vllm" || strings.Join(v.AgentBackends, ",") != "claude,copilot" || v.DefaultModels["claude"] != config.DefaultMeetingsClaudeCLIModel || len(v.Modes) != len(config.MeetingsLLMModes) { t.Fatalf("GET = %d %+v", code, v) @@ -46,9 +46,16 @@ func TestMeetingsModelAPI(t *testing.T) { if rec := actionsRequest(s, http.MethodPut, "/api/meetings/model", `{"mode":"agent-backend"}`, "read-write", false); rec.Code != http.StatusForbidden { t.Fatalf("read-write PUT = %d", rec.Code) } - rec := actionsRequest(s, http.MethodPut, "/api/meetings/model", `{"mode":"agent-backend","backend":"copilot"}`, "owner", true) - if rec.Code != http.StatusBadRequest || !strings.Contains(rec.Body.String(), "no meetings completion profile") { - t.Fatalf("copilot PUT = %d %s", rec.Code, rec.Body.String()) + if !strings.Contains(v.RefusedBackends["codex"], "shell tool") || v.RefusedBackends["gemini"] == "" { + t.Fatalf("refused backends %v", v.RefusedBackends) + } + rec := actionsRequest(s, http.MethodPut, "/api/meetings/model", `{"mode":"agent-backend","backend":"codex"}`, "owner", true) + if rec.Code != http.StatusBadRequest || !strings.Contains(rec.Body.String(), "cannot answer for meetings") { + t.Fatalf("codex PUT = %d %s", rec.Code, rec.Body.String()) + } + rec = actionsRequest(s, http.MethodPut, "/api/meetings/model", `{"mode":"agent-backend","backend":"corp-gw"}`, "owner", true) + if rec.Code != http.StatusBadRequest || !strings.Contains(rec.Body.String(), "configured model gateway") { + t.Fatalf("unknown gateway PUT = %d %s", rec.Code, rec.Body.String()) } if rec := actionsRequest(s, http.MethodPut, "/api/meetings/model", `not json`, "owner", true); rec.Code != http.StatusBadRequest { t.Fatalf("bad body = %d", rec.Code) @@ -84,6 +91,27 @@ func TestMeetingsModelAPI(t *testing.T) { } } +// A hive's named model gateways can answer for meetings; a watsonx one is +// listed with its reason. +func TestMeetingsModelAPIGateways(t *testing.T) { + s, deps := apiServer(t) + deps.Config.Governor.Gateways = []config.GatewayConfig{ + {Name: "corp-gw", Kind: "litellm", Endpoint: "https://gw.example/v1", DefaultModel: "gw-model"}, + {Name: "ibm", Kind: config.GatewayKindWatsonx, Endpoint: "https://us-south.ml.cloud.ibm.com/ml/gateway"}, + } + _, v := meetingsModelGet(t, s, "owner", true) + if !strings.Contains(strings.Join(v.SupportedBackends, ","), "corp-gw") || !strings.Contains(v.RefusedBackends["ibm"], "IAM") { + t.Fatalf("gateways %v %v", v.SupportedBackends, v.RefusedBackends) + } + rec := actionsRequest(s, http.MethodPut, "/api/meetings/model", `{"mode":"agent-backend","backend":"corp-gw"}`, "owner", true) + if rec.Code != http.StatusOK || deps.Config.Meetings.LLM.Backend != "corp-gw" { + t.Fatalf("gateway PUT = %d %s", rec.Code, rec.Body.String()) + } + if rec := actionsRequest(s, http.MethodPut, "/api/meetings/model", `{"mode":"agent-backend","backend":"ibm"}`, "owner", true); rec.Code != http.StatusBadRequest { + t.Fatalf("watsonx gateway PUT = %d", rec.Code) + } +} + func TestMeetingsModelAPINoConfig(t *testing.T) { s := NewServer(0, slog.New(slog.DiscardHandler)) s.registerMeetingsModelRoutes() @@ -114,7 +142,8 @@ func TestMeetingAttendeeModelSectionWiring(t *testing.T) { `data-change-action="maModelInput"`, "yamlHint('meetings.llm.backend')", "'agent-backend': 'An agent backend (the CLI and sign-in the agents use)'", - " (not yet for meetings)", + " (not for meetings)", + "Why some backends are unavailable for meetings", } { if !strings.Contains(html, want) { t.Errorf("Meeting attendee model wiring missing %q", want) @@ -143,15 +172,16 @@ func TestMeetingAttendeeModelSectionBehaviour(t *testing.T) { "let _maModel = null;", jsConstBlock(t, html, "MA_MODEL_MODE_LABELS"), } - for _, f := range []string{"maModelBackends", "maModelSupported", "maModelEffectiveBackend", "maModelOptions", "maModelBodyHtml", + for _, f := range []string{"maModelBackends", "maModelSupported", "maModelRefusal", "maModelRefusalsHtml", "maModelEffectiveBackend", "maModelOptions", "maModelBodyHtml", "maModelRender", "maModelSetData", "maModelInput"} { parts = append(parts, jsFunc(t, html, f)) } parts = append(parts, - `const data = { mode: 'agent-backend', backend: '', model: '', label: 'claude CLI claude-sonnet-5-5', modes: ['auto', 'openai', 'anthropic', 'agent-backend', 'none'], supported_backends: ['claude', 'litellm'], agent_backends: ['claude', 'gemini'], default_backend: 'claude', default_models: { claude: 'claude-sonnet-5-5' }, env_overrides: [] };`, + `const data = { mode: 'agent-backend', backend: '', model: '', label: 'claude CLI claude-sonnet-5-5', modes: ['auto', 'openai', 'anthropic', 'agent-backend', 'none'], supported_backends: ['claude', 'litellm'], refused_backends: { gemini: 'no tool-off flag' }, agent_backends: ['claude', 'gemini'], default_backend: 'claude', default_models: { claude: 'claude-sonnet-5-5' }, env_overrides: [] };`, `maModelSetData(data, false);`, `let h = maModelBodyHtml();`, - `assert.ok(h.includes('value="copilot" disabled>copilot (not yet for meetings)'), 'unsupported backend disabled');`, + `assert.ok(h.includes('value="copilot" disabled title="it has no meetings completion profile and is not a configured model gateway">copilot (not for meetings)'), 'unsupported backend disabled with the reason');`, + `assert.ok(h.includes('value="gemini" disabled title="no tool-off flag"') && h.includes('gemini: no tool-off flag'), 'refusal reason shown');`, `assert.ok(h.includes('value="gemini" disabled'), 'agent backends outside the catalog listed');`, `assert.ok(h.includes('value="litellm">litellm<'), 'litellm selectable');`, `assert.ok(h.includes('Default (claude)') && h.includes('Default (claude-sonnet-5-5)') && h.includes('Opus 5'), h);`, diff --git a/src/pkg/dashboard/static-src/js/meeting-attendee-model.js b/src/pkg/dashboard/static-src/js/meeting-attendee-model.js index d7df628cd3..309cbf3047 100644 --- a/src/pkg/dashboard/static-src/js/meeting-attendee-model.js +++ b/src/pkg/dashboard/static-src/js/meeting-attendee-model.js @@ -27,6 +27,20 @@ return (((_maModel && _maModel.data) || {}).supported_backends || []).indexOf(b) >= 0; } + // Why a backend cannot answer for meetings (GET's refused_backends). + function maModelRefusal(b) { + const r = (((_maModel && _maModel.data) || {}).refused_backends || {})[b]; + return r || 'it has no meetings completion profile and is not a configured model gateway'; + } + + function maModelRefusalsHtml() { + const refused = maModelBackends().filter(b => !maModelSupported(b)); + if (!refused.length) return ''; + return '
Why some backends are unavailable for meetings
    ' + + refused.map(b => '
  • ' + escapeHtml(typeof backendLabel === 'function' ? backendLabel(b) : b) + ': ' + escapeHtml(maModelRefusal(b)) + '
  • ').join('') + + '
'; + } + function maModelEffectiveBackend() { const f = _maModel.form; if (f.mode === 'claude-cli') return 'claude'; @@ -70,8 +84,8 @@ const backendOpts = '' + maModelBackends().map(b => { const ok = maModelSupported(b); - const label = (typeof backendLabel === 'function' ? backendLabel(b) : b) + (ok ? '' : ' (not yet for meetings)'); - return ''; + const label = (typeof backendLabel === 'function' ? backendLabel(b) : b) + (ok ? '' : ' (not for meetings)'); + return ''; }).join(''); picker = '' + @@ -81,7 +95,7 @@ const env = (d.env_overrides || []).length ? '

Set by the environment, which wins over a saved value: ' + escapeHtml(d.env_overrides.join(', ')) + '

' : ''; return '

What the attendee and Spektacular answer with. On an agent backend they use the same CLI and sign-in as the agents, with every tool off. In use now: ' + escapeHtml(d.label || '') + '.

' + '
' + - picker + '
' + env + + picker + '' + (agentMode ? maModelRefusalsHtml() : '') + env + (_maModel.restart ? '

Saved. Restart the hive to apply the new model.

' : '') + '' + ''; diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index 43c2c24d97..98ef17edbd 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -51312,6 +51312,20 @@ return (((_maModel && _maModel.data) || {}).supported_backends || []).indexOf(b) >= 0; } + // Why a backend cannot answer for meetings (GET's refused_backends). + function maModelRefusal(b) { + const r = (((_maModel && _maModel.data) || {}).refused_backends || {})[b]; + return r || 'it has no meetings completion profile and is not a configured model gateway'; + } + + function maModelRefusalsHtml() { + const refused = maModelBackends().filter(b => !maModelSupported(b)); + if (!refused.length) return ''; + return '
Why some backends are unavailable for meetings
    ' + + refused.map(b => '
  • ' + escapeHtml(typeof backendLabel === 'function' ? backendLabel(b) : b) + ': ' + escapeHtml(maModelRefusal(b)) + '
  • ').join('') + + '
'; + } + function maModelEffectiveBackend() { const f = _maModel.form; if (f.mode === 'claude-cli') return 'claude'; @@ -51355,8 +51369,8 @@ const backendOpts = '' + maModelBackends().map(b => { const ok = maModelSupported(b); - const label = (typeof backendLabel === 'function' ? backendLabel(b) : b) + (ok ? '' : ' (not yet for meetings)'); - return ''; + const label = (typeof backendLabel === 'function' ? backendLabel(b) : b) + (ok ? '' : ' (not for meetings)'); + return ''; }).join(''); picker = '' + @@ -51366,7 +51380,7 @@ const env = (d.env_overrides || []).length ? '

Set by the environment, which wins over a saved value: ' + escapeHtml(d.env_overrides.join(', ')) + '

' : ''; return '

What the attendee and Spektacular answer with. On an agent backend they use the same CLI and sign-in as the agents, with every tool off. In use now: ' + escapeHtml(d.label || '') + '.

' + '
' + - picker + '
' + env + + picker + '' + (agentMode ? maModelRefusalsHtml() : '') + env + (_maModel.restart ? '

Saved. Restart the hive to apply the new model.

' : '') + '' + ''; diff --git a/src/pkg/meetings/meetllm/cli.go b/src/pkg/meetings/meetllm/cli.go index 5ee34aaf9b..fbc5edfb8d 100644 --- a/src/pkg/meetings/meetllm/cli.go +++ b/src/pkg/meetings/meetllm/cli.go @@ -10,26 +10,31 @@ import ( "io" "os" "os/exec" + "path/filepath" "sort" + "strconv" "strings" "sync" "syscall" "time" ) -// Agent-backend completion profiles (hivecommons/hive#18517): the meetings -// model can be one of the agent backends the hive's agents run on. Each -// backend gets a narrow completion profile: one prompt in, one reply out, -// every tool and MCP server off, no session written, an empty temporary -// working directory, a fixed system prompt, and the token usage read from -// the CLI's result. The CLI runs with the hive's own sign-in for that -// backend (the shared agent home), never with a token hive reads or copies: -// the environment is an allowlist, so GitHub and hive tokens never reach a -// process that reads meeting text. +// Agent-backend completion profiles (hivecommons/hive#18517, #18549): the +// meetings model can be one of the agent backends the hive's agents run on. +// Each backend gets a narrow completion profile: one prompt in, one reply +// out, every tool and MCP server off, no session written to the shared +// home, an empty temporary working directory, a fixed system prompt, and +// the token usage read from the CLI's result. The CLI runs with the same +// sign-in the agents get for that backend (the shared agent home, or the +// sign-in variables the caller's Getenv supplies); the environment is an +// allowlist, so GitHub and hive tokens never reach a process that reads +// meeting text. // -// The persona keeps one warm CLI per meeting (CLISession) in streaming JSON -// mode, so a turn costs about a second instead of a cold start. `hive -// meeting-llm` (Spektacular) runs one CLI per prompt (completeCLIOnce). +// The persona keeps one warm CLI per meeting (CLISession) on backends with +// a streaming mode (claude), so a turn costs about a second instead of a +// cold start. Every other call runs one CLI per prompt (completeCLIOnce): +// `hive meeting-llm` (Spektacular) always, and the persona on copilot and +// omp. // ProviderAgentBackend selects an agent backend's completion profile; // Config.Backend names the backend. @@ -38,6 +43,10 @@ const ProviderAgentBackend = "agent-backend" const ( // BackendClaude is the Claude Code CLI. BackendClaude = "claude" + // BackendCopilot is the GitHub Copilot CLI (pinned 1.0.88). + BackendCopilot = "copilot" + // BackendOMP is the Oh My Pi CLI (pinned 18.20.0). + BackendOMP = "omp" // cliSystemPrompt is the fixed system prompt of every completion CLI. // The per-call instructions arrive inside the user turn. cliSystemPrompt = "You are the text-completion engine behind a meeting assistant. You have no tools. " + @@ -48,6 +57,13 @@ const ( // claudeNoHooksSettings turns off every user, project and plugin hook // and extended thinking for the run; login keys still load. claudeNoHooksSettings = `{"disableAllHooks":true,"alwaysThinkingEnabled":false}` + // copilotNoToolSentinel is copilot's tool allowlist: it names no real + // tool, so the model has none (an empty list is ignored by the CLI). + copilotNoToolSentinel = "hive-meetings-no-tools" + // ompProfile is the omp settings profile the call runs in. It is never + // configured, so it carries no MCP servers, hooks, rules or context + // files of the agents' default profile. + ompProfile = "hive-meetings" // cliResultSuccess is the result subtype of a completed turn. cliResultSuccess = "success" // cliEventResult is the stream event that ends a turn. @@ -58,25 +74,71 @@ const ( cliResultBuffer = 4 // cliWaitDelay bounds the wait for a killed CLI's pipes to close. cliWaitDelay = 2 * time.Second - // cliTempPrefix names the CLI's empty working directory. + // cliTempPrefix names the CLI's temporary directory. cliTempPrefix = "hive-meeting-cli-" + // cliWorkDir and cliStateDir are the call's empty working directory + // and its private state directory (a CLI home, the usage file) under + // the temporary directory. + cliWorkDir = "work" + cliStateDir = "state" + cliDirMode = 0o700 + // cliUsageFile is the usage report a CLI writes into the state dir. + cliUsageFile = "usage.json" // DefaultRecycleTurns is how many turns a warm CLI answers before it is // replaced when Config.RecycleTurns is unset. DefaultRecycleTurns = 8 ) +// Sign-in variables the meetings CLIs accept. The caller's Getenv supplies +// them the way the agent launcher gives them to agents; hive never logs +// them or writes them to disk. +const ( + // EnvClaudeOAuthToken is the Claude Code static token variable. + EnvClaudeOAuthToken = "CLAUDE_CODE_OAUTH_TOKEN" + // EnvCopilotToken is the Copilot CLI's dedicated token variable. + EnvCopilotToken = "COPILOT_GITHUB_TOKEN" +) + +// cliOutcome is one call's parsed output: the reply, the tokens it used +// (counted also when it failed) and the turn's own failure. +type cliOutcome struct { + reply string + tokens int + err error +} + // completionProfile is one backend's completion launch. type completionProfile struct { // args is the shared argv (without the binary) that turns off tools, // MCP, hooks, slash commands and session files. args []string - // warm and oneShot are the streaming and single-call output flags. + // warm and oneShot are the streaming and single-call output flags; a + // profile with no warm flags has no warm mode. warm []string oneShot []string // env is the backend's own login variables passed through when set. env []string // fixed are variables always set. fixed []string + // requiresModel refuses a call with no model: the CLI has no default + // model in the isolated state it runs in. + requiresModel bool + // systemInTurn puts the fixed system prompt at the head of the turn, + // for a CLI with no system-prompt flag. + systemInTurn bool + // stateHome points HOME and stateEnv at the call's private state dir, + // so the CLI writes its session files there and they are removed with + // it. + stateHome bool + stateEnv []string + // requiredEnv must be set: the CLI's only sign-in in the isolated home. + requiredEnv string + // usageFlag, when set, asks the CLI to write its usage report to + // /usage.json. + usageFlag string + // parse reads a one-shot call's stdout (and the state dir); it fails + // when the output holds no result at all. + parse func(stdout []byte, stateDir string) (cliOutcome, error) } var completionProfiles = map[string]completionProfile{ @@ -91,8 +153,51 @@ var completionProfiles = map[string]completionProfile{ "--system-prompt", cliSystemPrompt}, warm: []string{"--input-format", "stream-json", "--output-format", "stream-json", "--verbose"}, oneShot: []string{"--output-format", "json"}, - env: []string{"CLAUDE_CONFIG_DIR"}, - fixed: []string{"DISABLE_AUTOUPDATER=1"}, + // CLAUDE_CODE_OAUTH_TOKEN carries the operator's long-lived token + // (HIVE_CLAUDE_OAUTH_TOKEN) on a hive with no credentials file. + env: []string{"CLAUDE_CONFIG_DIR", EnvClaudeOAuthToken}, + fixed: []string{"DISABLE_AUTOUPDATER=1"}, + requiresModel: true, + parse: parseClaudeOutput, + }, + BackendCopilot: { + // The prompt is piped on stdin (a piped prompt is a non-interactive + // run), so no meeting text shows in a process listing. + // --available-tools naming no real tool leaves the model no tool, + // built-in or MCP; --disable-builtin-mcps turns off the GitHub MCP + // servers; --no-custom-instructions keeps AGENTS.md and friends + // out; --no-ask-user removes the ask tool; --no-auto-update keeps + // the pinned CLI. --allow-all-tools is never passed. The home is + // the call's own state dir, so no MCP config, plugin or hook of the + // agents' home loads and the session log is removed with the call; + // the sign-in is COPILOT_GITHUB_TOKEN, as the agents get it. + args: []string{"--no-color", "--available-tools", copilotNoToolSentinel, "--disable-builtin-mcps", + "--no-custom-instructions", "--no-ask-user", "--no-auto-update"}, + oneShot: []string{"--output-format", "json"}, + env: []string{EnvCopilotToken, "COPILOT_GH_HOST", "GH_HOST"}, + fixed: []string{"NO_COLOR=1"}, + systemInTurn: true, + stateHome: true, + stateEnv: []string{"COPILOT_HOME"}, + requiredEnv: EnvCopilotToken, + usageFlag: "--usage-output-file", + parse: parseCopilotOutput, + }, + BackendOMP: { + // --no-tools removes every built-in tool and --no-lsp the LSP + // tools; --no-extensions, --no-skills and --no-rules stop their + // discovery; --profile runs in an empty settings profile (no MCP + // servers, hooks or context files); --no-session writes no session + // file and --no-title makes no extra model call; --no-context-files + // skips AGENTS.md-style files. The prompt is read from stdin. Login + // comes from the fleet's auth broker (OMP_AUTH_BROKER_*). The + // isolated profile holds no default model, so one is required. + args: []string{"-p", "--mode", "json", "--no-tools", "--no-lsp", "--no-extensions", "--no-skills", + "--no-rules", "--no-session", "--no-title", "--no-context-files", "--profile", ompProfile, + "--system-prompt", cliSystemPrompt}, + env: []string{"OMP_AUTH_BROKER_URL", "OMP_AUTH_BROKER_TOKEN"}, + requiresModel: true, + parse: parseOMPOutput, }, } @@ -115,6 +220,24 @@ func CompletionBackends() []string { return out } +// WarmBackends lists the backends with a warm streaming mode, sorted. +func WarmBackends() []string { + var out []string + for b, p := range completionProfiles { + if len(p.warm) > 0 { + out = append(out, b) + } + } + sort.Strings(out) + return out +} + +// SupportsWarm reports whether backend keeps a warm CLI (NewCLISession). +func SupportsWarm(backend string) bool { + p, err := profileFor(backend) + return err == nil && len(p.warm) > 0 +} + func profileFor(backend string) (completionProfile, error) { p, ok := completionProfiles[strings.ToLower(strings.TrimSpace(backend))] if !ok { @@ -155,6 +278,35 @@ func (p completionProfile) environ(getenv func(string) string, home string) []st return append(env, p.fixed...) } +// callEnviron is a one-shot call's environment: environ, with HOME and the +// profile's state variables on the call's state dir when the profile runs +// in its own home. It fails when the profile's required sign-in is unset. +func (p completionProfile) callEnviron(getenv func(string) string, home, state string) ([]string, error) { + if getenv == nil { + getenv = os.Getenv + } + if p.requiredEnv != "" && strings.TrimSpace(getenv(p.requiredEnv)) == "" { + return nil, fmt.Errorf("meeting-llm: this backend needs the hive's sign-in in %s (the one its agents get), and none is set", p.requiredEnv) + } + if p.stateHome { + home = state + } + env := p.environ(getenv, home) + for _, k := range p.stateEnv { + env = append(env, k+"="+state) + } + return env, nil +} + +// turnText is the turn a profile sends: cliTurnText, led by the fixed +// system prompt on a CLI with no system-prompt flag. +func (p completionProfile) turnText(system, prompt string) string { + if !p.systemInTurn { + return cliTurnText(system, prompt) + } + return "\n" + cliSystemPrompt + "\n\n\n" + cliTurnText(system, prompt) +} + func cliBinary(c Config) string { if b := strings.TrimSpace(c.CLIBinary); b != "" { return b @@ -302,6 +454,9 @@ func NewCLISession(c Config) (*CLISession, error) { if err != nil { return nil, err } + if len(p.warm) == 0 { + return nil, fmt.Errorf("meeting-llm: backend %q has no warm mode; it runs one CLI per call", c.Backend) + } n := c.RecycleTurns if n <= 0 { n = DefaultRecycleTurns @@ -448,28 +603,44 @@ func completeSession(ctx context.Context, c Config, prompt string) (string, erro return reply, err } -// completeCLIOnce runs one CLI per call (the `hive meeting-llm` path); the -// caller holds the budget check, timeout and slot. +// completeCLIOnce runs one CLI per call (the `hive meeting-llm` path, and +// the persona on a backend with no warm mode); the caller holds the budget +// check, timeout and slot. The CLI runs in an empty working directory +// beside a private state directory, both removed afterwards. func completeCLIOnce(ctx context.Context, c Config, prompt string) (string, error) { p, err := profileFor(c.Backend) if err != nil { return "", err } - if strings.TrimSpace(c.Model) == "" { + if p.requiresModel && strings.TrimSpace(c.Model) == "" { return "", errors.New("meeting-llm: the CLI path needs a model") } - dir, err := os.MkdirTemp("", cliTempPrefix) + root, err := os.MkdirTemp("", cliTempPrefix) if err != nil { return "", fmt.Errorf("meeting-llm: CLI workdir: %w", err) } - defer func() { _ = os.RemoveAll(dir) }() - cmd := exec.CommandContext(ctx, cliBinary(c), p.argv(c.Model, false)...) - cmd.Dir = dir - cmd.Env = p.environ(c.Getenv, c.CLIHome) + defer func() { _ = os.RemoveAll(root) }() + work, state := filepath.Join(root, cliWorkDir), filepath.Join(root, cliStateDir) + for _, d := range []string{work, state} { + if err := os.Mkdir(d, cliDirMode); err != nil { + return "", fmt.Errorf("meeting-llm: CLI workdir: %w", err) + } + } + env, err := p.callEnviron(c.Getenv, c.CLIHome, state) + if err != nil { + return "", err + } + argv := p.argv(c.Model, false) + if p.usageFlag != "" { + argv = append(argv, p.usageFlag, filepath.Join(state, cliUsageFile)) + } + cmd := exec.CommandContext(ctx, cliBinary(c), argv...) + cmd.Dir = work + cmd.Env = env cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} cmd.Cancel = func() error { killGroup(cmd); return nil } cmd.WaitDelay = cliWaitDelay - cmd.Stdin = strings.NewReader(cliTurnText(c.System, prompt)) + cmd.Stdin = strings.NewReader(p.turnText(c.System, prompt)) var stdout bytes.Buffer cmd.Stdout = &limitedWriter{w: &stdout, n: maxResponseBytes} cmd.Stderr = io.Discard @@ -477,17 +648,33 @@ func completeCLIOnce(ctx context.Context, c Config, prompt string) (string, erro if ctx.Err() != nil { return "", fmt.Errorf("meeting-llm: CLI call: %w", ctx.Err()) } - ev, perr := parseCLIResult(stdout.Bytes()) + out, perr := p.parse(stdout.Bytes(), state) if perr != nil { if runErr != nil { return "", fmt.Errorf("meeting-llm: CLI failed: %w", runErr) } return "", perr } - if err := recordUsage(c.UsageFile, ev.Usage.total(), time.Now()); err != nil { + if err := recordUsage(c.UsageFile, out.tokens, time.Now()); err != nil { return "", fmt.Errorf("meeting-llm: recording usage: %w", err) } - return ev.reply() + if out.err != nil { + return "", out.err + } + if strings.TrimSpace(out.reply) == "" { + return "", errors.New("meeting-llm: CLI returned no text") + } + return out.reply, nil +} + +// parseClaudeOutput reads `claude -p --output-format json`. +func parseClaudeOutput(b []byte, _ string) (cliOutcome, error) { + ev, err := parseCLIResult(b) + if err != nil { + return cliOutcome{}, err + } + reply, rerr := ev.reply() + return cliOutcome{reply: reply, tokens: ev.Usage.total(), err: rerr}, nil } // parseCLIResult reads the result of `--output-format json`: one result @@ -509,6 +696,136 @@ func parseCLIResult(b []byte) (cliEvent, error) { return cliEvent{}, errors.New("meeting-llm: no result in CLI output") } +// copilotEvent is one line of `copilot --output-format json` (JSONL): the +// reply is the last assistant.message, a session.error fails the turn, and +// the final result record carries the exit code. +type copilotEvent struct { + Type string `json:"type"` + ExitCode *int `json:"exitCode"` + Data struct { + Content string `json:"content"` + ErrorType string `json:"errorType"` + } `json:"data"` +} + +// copilotUsageReport is the --usage-output-file report. Per model, +// inputTokens is the whole prompt, cached tokens included. +type copilotUsageReport struct { + ModelMetrics map[string]struct { + Usage struct { + InputTokens int `json:"inputTokens"` + OutputTokens int `json:"outputTokens"` + } `json:"usage"` + } `json:"modelMetrics"` +} + +// parseCopilotOutput reads copilot's JSONL events and its usage report. +func parseCopilotOutput(b []byte, stateDir string) (cliOutcome, error) { + var out cliOutcome + var failure string + saw := false + for _, line := range bytes.Split(b, []byte("\n")) { + var ev copilotEvent + if json.Unmarshal(bytes.TrimSpace(line), &ev) != nil { + continue + } + switch ev.Type { + case "assistant.message": + saw = true + if strings.TrimSpace(ev.Data.Content) != "" { + out.reply = ev.Data.Content + } + case "session.error": + saw = true + failure = ev.Data.ErrorType + if failure == "" { + failure = "error" + } + case cliEventResult: + saw = true + if ev.ExitCode != nil && *ev.ExitCode != 0 && failure == "" { + failure = "exit " + strconv.Itoa(*ev.ExitCode) + } + } + } + if !saw { + return cliOutcome{}, errors.New("meeting-llm: no result in CLI output") + } + if rb, err := os.ReadFile(filepath.Join(stateDir, cliUsageFile)); err == nil { + var rep copilotUsageReport + if json.Unmarshal(rb, &rep) == nil { + for _, m := range rep.ModelMetrics { + out.tokens += m.Usage.InputTokens + m.Usage.OutputTokens + } + } + } + if failure != "" { + out.reply, out.err = "", fmt.Errorf("meeting-llm: CLI turn failed (%s)", failure) + } + return out, nil +} + +// ompUsage is an omp assistant message's usage; totalTokens is every +// bucket (input, output, cache read and write). +type ompUsage struct { + Input int `json:"input"` + Output int `json:"output"` + CacheRead int `json:"cacheRead"` + CacheWrite int `json:"cacheWrite"` + TotalTokens int `json:"totalTokens"` +} + +func (u ompUsage) total() int { + if u.TotalTokens > 0 { + return u.TotalTokens + } + return u.Input + u.Output + u.CacheRead + u.CacheWrite +} + +// ompEvent is one line of `omp -p --mode json`; each finished message is a +// message_end event. +type ompEvent struct { + Type string `json:"type"` + Message struct { + Role string `json:"role"` + Content []struct { + Type string `json:"type"` + Text string `json:"text"` + } `json:"content"` + Usage ompUsage `json:"usage"` + StopReason string `json:"stopReason"` + } `json:"message"` +} + +// parseOMPOutput reads omp's JSON events: the reply is the last assistant +// message's text, and every assistant message's usage counts. +func parseOMPOutput(b []byte, _ string) (cliOutcome, error) { + var out cliOutcome + saw := false + for _, line := range bytes.Split(b, []byte("\n")) { + var ev ompEvent + if json.Unmarshal(bytes.TrimSpace(line), &ev) != nil || ev.Type != "message_end" || ev.Message.Role != "assistant" { + continue + } + saw = true + out.tokens += ev.Message.Usage.total() + var text strings.Builder + for _, c := range ev.Message.Content { + if c.Type == "text" { + text.WriteString(c.Text) + } + } + out.reply, out.err = text.String(), nil + if r := ev.Message.StopReason; r == "error" || r == "aborted" { + out.reply, out.err = "", fmt.Errorf("meeting-llm: CLI turn failed (%s)", r) + } + } + if !saw { + return cliOutcome{}, errors.New("meeting-llm: no result in CLI output") + } + return out, nil +} + // limitedWriter keeps the first n bytes and discards the rest. type limitedWriter struct { w io.Writer @@ -526,3 +843,9 @@ func (l *limitedWriter) Write(p []byte) (int, error) { } return len(p), nil } + +// HasCompletionProfile reports whether backend has a completion profile. +func HasCompletionProfile(backend string) bool { + _, err := profileFor(backend) + return err == nil +} diff --git a/src/pkg/meetings/meetllm/cli_profiles_test.go b/src/pkg/meetings/meetllm/cli_profiles_test.go new file mode 100644 index 0000000000..48575d1997 --- /dev/null +++ b/src/pkg/meetings/meetllm/cli_profiles_test.go @@ -0,0 +1,330 @@ +package meetllm + +import ( + "bytes" + "context" + "encoding/pem" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "reflect" + "strconv" + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/config" +) + +// fakeProfileCLI writes a fake one-shot CLI into dir: it records its argv, +// environment, working directory (and what is in it) and stdin, writes +// usage to the path after --usage-output-file when given, prints stdout and +// exits with code. +func fakeProfileCLI(t *testing.T, dir, name, stdout, usage string, code int) string { + t.Helper() + script := `#!/bin/sh +D='` + dir + `' +printf '%s\n' "$@" > "$D/argv" +env > "$D/env" +pwd > "$D/cwd" +ls -A > "$D/cwdlist" +cat > "$D/stdin" +u="" +prev="" +for a in "$@"; do + if [ "$prev" = "--usage-output-file" ]; then u="$a"; fi + prev="$a" +done +if [ -n "$u" ] && [ -n '` + usage + `' ]; then printf '%s' '` + usage + `' > "$u"; fi +printf '%s' '` + stdout + `' +exit ` + strconv.Itoa(code) + ` +` + p := filepath.Join(dir, name) + if err := os.WriteFile(p, []byte(script), 0o755); err != nil { + t.Fatal(err) + } + return p +} + +func readEnv(t *testing.T, dir string) map[string]string { + t.Helper() + env := map[string]string{} + for _, kv := range readLines(t, filepath.Join(dir, "env")) { + k, v, _ := strings.Cut(kv, "=") + env[k] = v + } + return env +} + +const ( + copilotOK = `{"type":"session.start","data":{"selectedModel":"gpt-x"}} +{"type":"assistant.message","data":{"content":""}} +{"type":"assistant.message","data":{"content":"copilot reply"}} +{"type":"result","exitCode":0,"usage":{"premiumRequests":1}} +` + copilotUsage = `{"modelMetrics":{"gpt-x":{"usage":{"inputTokens":120,"outputTokens":30,"cacheReadTokens":100}},"aux":{"usage":{"inputTokens":5,"outputTokens":1}}}}` + // copilotUsageTokens is 120+30 + 5+1: inputTokens already holds the + // cached tokens. + copilotUsageTokens = 156 +) + +func copilotConfig(t *testing.T, dir, stdout, usage string, code int, extra map[string]string) Config { + env := map[string]string{EnvCopilotToken: "gho_meetings", "GH_TOKEN": "ghp_hive", "HIVE_GITHUB_TOKEN": "x"} + for k, v := range extra { + env[k] = v + } + return Config{ + Provider: ProviderAgentBackend, Backend: BackendCopilot, Model: "gpt-x", + CLIBinary: fakeProfileCLI(t, dir, "copilot", stdout, usage, code), CLIHome: filepath.Join(dir, "home"), + Timeout: 5 * time.Second, MaxInFlight: 1, UsageFile: filepath.Join(dir, "usage.jsonl"), + Getenv: testEnv(env), + } +} + +// copilot runs once per call with no tools, the prompt on stdin, its own +// throwaway home, the agents' token and the usage from its report. +func TestCompleteCLIOnceCopilot(t *testing.T) { + dir := t.TempDir() + c := copilotConfig(t, dir, copilotOK, copilotUsage, 0, nil) + c.System = "answer briefly" + reply, err := Complete(context.Background(), c, nil, "what is hive?") + if err != nil || reply != "copilot reply" { + t.Fatalf("Complete = %q %v", reply, err) + } + if n, _ := UsageTotal(c.UsageFile); n != copilotUsageTokens { + t.Fatalf("usage = %d", n) + } + argv := readLines(t, filepath.Join(dir, "argv")) + for _, seq := range [][]string{ + {"--available-tools", copilotNoToolSentinel}, {"--disable-builtin-mcps"}, {"--no-custom-instructions"}, {"--no-ask-user"}, + {"--no-auto-update"}, {"--output-format", "json"}, {"--model", "gpt-x"}, {"--usage-output-file"}, + } { + if !containsSeq(argv, seq) { + t.Errorf("argv %q lacks %q", argv, seq) + } + } + for _, banned := range []string{"-p", "--allow-all", "--allow-all-tools", "--yolo"} { + if containsSeq(argv, []string{banned}) { + t.Errorf("argv %q has %q", argv, banned) + } + } + stdin, _ := os.ReadFile(filepath.Join(dir, "stdin")) + if !strings.HasPrefix(string(stdin), "\n"+cliSystemPrompt) || !strings.Contains(string(stdin), "\nanswer briefly") || + !strings.Contains(string(stdin), "\nwhat is hive?\n") { + t.Fatalf("stdin %q", stdin) + } + env := readEnv(t, dir) + cwd := strings.TrimSpace(strings.Join(readLines(t, filepath.Join(dir, "cwd")), "")) + // pwd may print the physical path (macOS /private/var), so compare the + // temporary directory by name. + state := env["HOME"] + if filepath.Base(cwd) != cliWorkDir || filepath.Base(state) != cliStateDir || filepath.Base(filepath.Dir(state)) != filepath.Base(filepath.Dir(cwd)) || + env["COPILOT_HOME"] != state || env[EnvCopilotToken] != "gho_meetings" || env["NO_COLOR"] != "1" { + t.Fatalf("cwd %q env %v", cwd, env) + } + for _, k := range []string{"GH_TOKEN", "HIVE_GITHUB_TOKEN"} { + if _, ok := env[k]; ok { + t.Errorf("%s reached the CLI", k) + } + } + if l := readLines(t, filepath.Join(dir, "cwdlist")); len(l) != 1 || l[0] != "" { + t.Fatalf("working directory not empty: %q", l) + } + if _, err := os.Stat(filepath.Dir(cwd)); !os.IsNotExist(err) { + t.Fatalf("temporary directory kept: %v", err) + } +} + +func TestCompleteCLIOnceCopilotFailures(t *testing.T) { + ctx := context.Background() + dir := t.TempDir() + c := copilotConfig(t, dir, copilotOK, "", 0, map[string]string{EnvCopilotToken: ""}) + if _, err := completeCLIOnce(ctx, c, "q"); err == nil || !strings.Contains(err.Error(), EnvCopilotToken) { + t.Fatalf("no token err = %v", err) + } + quota := `{"type":"session.error","data":{"errorType":"quota","message":"over"}}` + "\n" + `{"type":"result","exitCode":1}` + c = copilotConfig(t, t.TempDir(), quota, copilotUsage, 1, nil) + if _, err := completeCLIOnce(ctx, c, "q"); err == nil || !strings.Contains(err.Error(), "(quota)") { + t.Fatalf("quota err = %v", err) + } + if n, _ := UsageTotal(c.UsageFile); n != copilotUsageTokens { + t.Fatalf("failed turn usage = %d", n) + } + c = copilotConfig(t, t.TempDir(), `{"type":"result","exitCode":3}`, "", 3, nil) + if _, err := completeCLIOnce(ctx, c, "q"); err == nil || !strings.Contains(err.Error(), "(exit 3)") { + t.Fatalf("exit err = %v", err) + } + c = copilotConfig(t, t.TempDir(), `{"type":"session.error","data":{}}`, "", 0, nil) + if _, err := completeCLIOnce(ctx, c, "q"); err == nil || !strings.Contains(err.Error(), "(error)") { + t.Fatalf("untyped error err = %v", err) + } + c = copilotConfig(t, t.TempDir(), "banner only", "", 1, nil) + if _, err := completeCLIOnce(ctx, c, "q"); err == nil || !strings.Contains(err.Error(), "CLI failed") { + t.Fatalf("garbage err = %v", err) + } + c = copilotConfig(t, t.TempDir(), `{"type":"result","exitCode":0}`, "not json", 0, nil) + if _, err := completeCLIOnce(ctx, c, "q"); err == nil || !strings.Contains(err.Error(), "no text") { + t.Fatalf("empty reply err = %v", err) + } + // Without a model copilot uses its own default: no --model is passed. + dir = t.TempDir() + c = copilotConfig(t, dir, copilotOK, "", 0, nil) + c.Model = "" + if reply, err := completeCLIOnce(ctx, c, "q"); err != nil || reply != "copilot reply" { + t.Fatalf("no model = %q %v", reply, err) + } + if containsSeq(readLines(t, filepath.Join(dir, "argv")), []string{"--model"}) { + t.Fatal("empty model passed") + } +} + +const ompOK = `{"type":"session","version":3} +{"type":"message_end","message":{"role":"user","content":[{"type":"text","text":"q"}]}} +{"type":"message_end","message":{"role":"assistant","content":[{"type":"thinking","thinking":"hm"},{"type":"text","text":"omp "},{"type":"text","text":"reply"}],"usage":{"input":10,"output":4,"cacheRead":0,"cacheWrite":0,"totalTokens":14},"stopReason":"stop"}} +{"type":"agent_end","messages":[]} +` + +func ompConfig(t *testing.T, dir, stdout string, code int) Config { + return Config{ + Provider: ProviderAgentBackend, Backend: BackendOMP, Model: "anthropic/claude-sonnet-5-5", + CLIBinary: fakeProfileCLI(t, dir, "omp", stdout, "", code), CLIHome: filepath.Join(dir, "home"), + Timeout: 5 * time.Second, MaxInFlight: 1, UsageFile: filepath.Join(dir, "usage.jsonl"), + Getenv: testEnv(map[string]string{"OMP_AUTH_BROKER_URL": "https://broker", "OMP_AUTH_BROKER_TOKEN": "obt", "GH_TOKEN": "ghp"}), + } +} + +// omp runs once per call with no tools in an isolated profile, the prompt on +// stdin, and the usage from its assistant messages. +func TestCompleteCLIOnceOMP(t *testing.T) { + ctx := context.Background() + dir := t.TempDir() + c := ompConfig(t, dir, ompOK, 0) + reply, err := completeCLIOnce(ctx, c, "what is hive?") + if err != nil || reply != "omp reply" { + t.Fatalf("omp = %q %v", reply, err) + } + if n, _ := UsageTotal(c.UsageFile); n != 14 { + t.Fatalf("usage = %d", n) + } + argv := readLines(t, filepath.Join(dir, "argv")) + for _, seq := range [][]string{ + {"-p", "--mode", "json", "--no-tools"}, {"--no-lsp"}, {"--no-extensions"}, {"--no-skills"}, {"--no-rules"}, {"--no-session"}, + {"--no-context-files"}, {"--profile", ompProfile}, {"--system-prompt", cliSystemPrompt}, {"--model", "anthropic/claude-sonnet-5-5"}, + } { + if !containsSeq(argv, seq) { + t.Errorf("argv %q lacks %q", argv, seq) + } + } + env := readEnv(t, dir) + if env["HOME"] != filepath.Join(dir, "home") || env["OMP_AUTH_BROKER_URL"] != "https://broker" || env["OMP_AUTH_BROKER_TOKEN"] != "obt" { + t.Fatalf("env %v", env) + } + if _, ok := env["GH_TOKEN"]; ok { + t.Fatal("GH_TOKEN reached omp") + } + if b, _ := os.ReadFile(filepath.Join(dir, "stdin")); strings.Contains(string(b), "") || !strings.Contains(string(b), "\nwhat is hive?") { + t.Fatalf("stdin %q", b) + } + + failed := `{"type":"message_end","message":{"role":"assistant","content":[],"usage":{"input":3,"output":0,"cacheRead":2,"cacheWrite":1},"stopReason":"error"}}` + c = ompConfig(t, t.TempDir(), failed, 1) + if _, err := completeCLIOnce(ctx, c, "q"); err == nil || !strings.Contains(err.Error(), "(error)") { + t.Fatalf("error turn err = %v", err) + } + if n, _ := UsageTotal(c.UsageFile); n != 6 { + t.Fatalf("summed usage = %d", n) + } + c = ompConfig(t, t.TempDir(), `{"type":"session"}`, 0) + if _, err := completeCLIOnce(ctx, c, "q"); err == nil || !strings.Contains(err.Error(), "no result") { + t.Fatalf("no message err = %v", err) + } + c.Model = "" + if _, err := completeCLIOnce(ctx, c, "q"); err == nil || !strings.Contains(err.Error(), "needs a model") { + t.Fatalf("no model err = %v", err) + } +} + +// Only claude keeps a warm process; the profile list matches the config's. +func TestCompletionProfileCapabilities(t *testing.T) { + if !SupportsWarm(BackendClaude) || SupportsWarm(BackendCopilot) || SupportsWarm(BackendOMP) || SupportsWarm("codex") { + t.Fatal("warm capability") + } + if !HasCompletionProfile("Copilot") || HasCompletionProfile("gemini") { + t.Fatal("profile lookup") + } + if got, want := WarmBackends(), config.MeetingsWarmCLIBackends; !reflect.DeepEqual(got, want) { + t.Fatalf("warm %v, config %v", got, want) + } + if _, err := NewCLISession(Config{Backend: BackendCopilot}); err == nil || !strings.Contains(err.Error(), "no warm mode") { + t.Fatalf("copilot session err = %v", err) + } + if _, err := (completionProfile{}).callEnviron(nil, "", "/s"); err != nil { + t.Fatal(err) + } + t.Setenv(EnvCopilotToken, "") + if _, err := completionProfiles[BackendCopilot].callEnviron(nil, "", "/s"); err == nil { + t.Fatal("want the copilot token required") + } +} + +// `hive meeting-llm` runs copilot with no model and passes the sign-in +// variables it was given. +func TestRunCopilotOneShot(t *testing.T) { + dir := t.TempDir() + bin := fakeProfileCLI(t, dir, "copilot", copilotOK, copilotUsage, 0) + usage := filepath.Join(dir, "usage.jsonl") + env := testEnv(map[string]string{ + EnvBackend: "copilot", EnvCLIBinary: bin, EnvUsageFile: usage, EnvTimeoutMS: "5000", EnvCopilotToken: "gho_x", + }) + var out, errb bytes.Buffer + if code := Run(nil, strings.NewReader("p"), &out, &errb, env); code != 0 || out.String() != "copilot reply" { + t.Fatalf("Run = %d %q %q", code, out.String(), errb.String()) + } + if n, _ := UsageTotal(usage); n != copilotUsageTokens { + t.Fatalf("usage = %d", n) + } + if readEnv(t, dir)[EnvCopilotToken] != "gho_x" { + t.Fatal("token not passed") + } + if _, err := FromEnv(testEnv(map[string]string{EnvBackend: "omp"})); err == nil { + t.Fatal("want omp to need a model") + } +} + +func TestHTTPClientCABundle(t *testing.T) { + if c, err := HTTPClient(""); err != nil || c == nil || c.Transport != nil { + t.Fatalf("default client %+v %v", c, err) + } + dir := t.TempDir() + if _, err := HTTPClient(filepath.Join(dir, "absent.pem")); err == nil { + t.Fatal("want a read error") + } + bad := filepath.Join(dir, "bad.pem") + if err := os.WriteFile(bad, []byte("not pem"), 0o600); err != nil { + t.Fatal(err) + } + if _, err := HTTPClient(bad); err == nil { + t.Fatal("want a PEM error") + } + srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write([]byte("ok")) })) + defer srv.Close() + ca := filepath.Join(dir, "ca.pem") + if err := os.WriteFile(ca, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: srv.Certificate().Raw}), 0o600); err != nil { + t.Fatal(err) + } + c, err := HTTPClient(ca) + if err != nil { + t.Fatal(err) + } + resp, err := c.Get(srv.URL) + if err != nil { + t.Fatalf("private CA not trusted: %v", err) + } + _ = resp.Body.Close() + var out, errb bytes.Buffer + env := testEnv(map[string]string{EnvEndpoint: srv.URL, EnvModel: "m", EnvCABundle: bad}) + if code := Run(nil, strings.NewReader("p"), &out, &errb, env); code != 2 || !strings.Contains(errb.String(), "PEM") { + t.Fatalf("Run with a bad CA bundle = %d %q", code, errb.String()) + } +} diff --git a/src/pkg/meetings/meetllm/meetllm.go b/src/pkg/meetings/meetllm/meetllm.go index 0f2522b539..15f4b729a1 100644 --- a/src/pkg/meetings/meetllm/meetllm.go +++ b/src/pkg/meetings/meetllm/meetllm.go @@ -19,10 +19,13 @@ // HIVE_MEETING_LLM_BACKEND agent backend (claude) instead of an endpoint // HIVE_MEETING_LLM_CLI_BINARY that backend's CLI (default: its name) // HIVE_MEETING_LLM_CLI_HOME HOME holding the CLI's sign-in +// HIVE_MEETING_LLM_CA_BUNDLE PEM file of private CAs for the endpoint // // With HIVE_MEETING_LLM_BACKEND set the call runs that backend's CLI once -// in its no-tools completion profile (cli.go, hivecommons/hive#18517) and -// no endpoint is needed. +// in its no-tools completion profile (cli.go, hivecommons/hive#18517, +// #18549) and no endpoint is needed. The CLI's sign-in variables +// (CLAUDE_CODE_OAUTH_TOKEN, COPILOT_GITHUB_TOKEN, OMP_AUTH_BROKER_*) pass +// through from this environment when set. // // Usage ledger: one line per call, {"at":"","tokens":N}, appended // to the meeting's usage file. The live loop sums it for the panel and the @@ -35,6 +38,8 @@ import ( "bufio" "bytes" "context" + "crypto/tls" + "crypto/x509" "encoding/json" "errors" "fmt" @@ -64,6 +69,7 @@ const ( EnvBackend = "HIVE_MEETING_LLM_BACKEND" EnvCLIBinary = "HIVE_MEETING_LLM_CLI_BINARY" EnvCLIHome = "HIVE_MEETING_LLM_CLI_HOME" + EnvCABundle = "HIVE_MEETING_LLM_CA_BUNDLE" ) const ( @@ -117,6 +123,9 @@ type Config struct { Session *CLISession // 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 + // verified against, added to the system roots (`hive meeting-llm`). + CABundle string } func envInt(getenv func(string) string, name string, def int) int { @@ -141,14 +150,18 @@ func FromEnv(getenv func(string) string) (Config, error) { CLIBinary: strings.TrimSpace(getenv(EnvCLIBinary)), CLIHome: strings.TrimSpace(getenv(EnvCLIHome)), Getenv: getenv, + CABundle: strings.TrimSpace(getenv(EnvCABundle)), } if c.Backend != "" { c.Provider = ProviderAgentBackend - if c.Model == "" { + p, err := profileFor(c.Backend) + if err != nil { + return c, err + } + if p.requiresModel && c.Model == "" { return c, fmt.Errorf("meeting-llm: %s is required with %s", EnvModel, EnvBackend) } - _, err := profileFor(c.Backend) - return c, err + return c, nil } if c.Endpoint == "" || c.Model == "" { return c, fmt.Errorf("meeting-llm: %s and %s are required", EnvEndpoint, EnvModel) @@ -339,7 +352,12 @@ func Run(_ []string, stdin io.Reader, stdout, stderr io.Writer, getenv func(stri fmt.Fprintln(stderr, "meeting-llm: reading prompt:", err) return 1 } - reply, err := Complete(context.Background(), c, &http.Client{}, string(prompt)) + hc, err := HTTPClient(c.CABundle) + if err != nil { + fmt.Fprintln(stderr, err) + return 2 + } + reply, err := Complete(context.Background(), c, hc, string(prompt)) if err != nil { fmt.Fprintln(stderr, err) return 1 @@ -347,3 +365,29 @@ func Run(_ []string, stdin io.Reader, stdout, stderr io.Writer, getenv func(stri fmt.Fprint(stdout, reply) return 0 } + +// HTTPClient is the client for an OpenAI-compatible endpoint: the default +// one, or, with caBundle set, one that also trusts the PEM file's CAs (a +// model gateway's ca_bundle). Verification is never turned off. +func HTTPClient(caBundle string) (*http.Client, error) { + if strings.TrimSpace(caBundle) == "" { + return &http.Client{}, nil + } + pem, err := os.ReadFile(caBundle) + if err != nil { + return nil, fmt.Errorf("meeting-llm: reading CA bundle: %w", err) + } + pool, err := x509.SystemCertPool() + if err != nil || pool == nil { + pool = x509.NewCertPool() + } + if !pool.AppendCertsFromPEM(pem) { + return nil, errors.New("meeting-llm: CA bundle holds no PEM certificate") + } + tr := &http.Transport{Proxy: http.ProxyFromEnvironment} + if def, ok := http.DefaultTransport.(*http.Transport); ok { + tr = def.Clone() + } + tr.TLSClientConfig = &tls.Config{RootCAs: pool, MinVersion: tls.VersionTLS12} + return &http.Client{Transport: tr}, nil +}