Files
buzz/desktop/src/features/agents/ui/personaModelDiscoveryStatus.test.mjs
Will Pfleger 1ff98fa685 fix(desktop): launch Databricks OAuth from passive model discovery (#5607)
When a user's agent runtime is `buzz-agent` with no cached Databricks
OAuth token, the desktop app's passive model-discovery surfaces were
forbidden from launching interactive auth. Discovery failed silently, so
the model dropdown showed only built-in fallback models behind a vague
"Could not load live models for `databricks_v2`" note (reported
internally by Nick and Jose).

## What changed

Both discovery surfaces — the passive draft-form discovery and the
explicit saved-model picker — now launch the browser OAuth flow,
matching goose's behavior. The only behavioral difference between them
is cooldown handling:

- **Passive draft discovery** fires on every form-state change, so a
failed, cancelled, or timed-out sign-in records a per-host cooldown (5
min) that suppresses re-popping the browser on the next keystroke. While
the cooldown is active it returns the "sign-in required" guidance
instead of relaunching.
- **The explicit model picker** is a deliberate user action, so it
always launches and clears any stale cooldown first.

Safety rails:

- A 150s hard timeout (`AUTH_FLOW_TIMEOUT`) bounds the whole interactive
flow so an abandoned SSO tab fails discovery cleanly rather than wedging
the dropdown. Success clears the cooldown; failure and timeout both
record it.
- `AuthCooldown` recovers from a poisoned lock rather than wedging every
future sign-in on one panic.

The frontend maps the terminal Databricks sign-in states to typed,
actionable copy in `formatModelDiscoveryErrorStatus`: "sign-in required"
is a muted note pointing at the picker and `buzz-agent auth databricks`;
a failed or timed-out sign-in is a warning pointing at the explicit
retry. Other Databricks failures fall through to the existing generic
notice.

## Scope

Changes are confined to Databricks discovery and its frontend status
formatter — no `agent_models.rs` call sites are touched. The
interactive-auth helper takes an injected timeout so the
timeout/cooldown policy is unit-testable without a live browser.

## Deferred

Cooldown keys use the raw trimmed `DATABRICKS_HOST`, while the catalog
and OAuth cache normalize trailing slashes
(`crates/buzz-agent/src/catalog.rs:96`,
`crates/buzz-agent/src/llm.rs:2046`). So `https://workspace/` and
`https://workspace` share credentials but get separate cooldown entries
— an equivalent-spelling change to the host field mid-cooldown can
re-pop passive OAuth once within the 5-minute window. Self-limiting (one
extra browser launch, never auth corruption). Follow-up: a
`trim_end_matches('/')` on the cooldown key plus an equivalent-host
test, picked up with the coordinator migration if
[#5545](https://github.com/block/buzz/pull/5545) ever merges.

Signed-off-by: Will Pfleger <pfleger.will@gmail.com>
Co-authored-by: Duncan <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@buzz.block.builderlab.xyz>
2026-08-12 09:35:48 -04:00

161 lines
5.7 KiB
JavaScript

import assert from "node:assert/strict";
import test from "node:test";
import { formatModelDiscoveryErrorStatus } from "./personaModelDiscoveryStatus.ts";
test("model discovery status names missing Anthropic credentials", () => {
const status = formatModelDiscoveryErrorStatus(
new Error("config: ANTHROPIC_API_KEY required"),
"anthropic",
);
assert.equal(status?.tone, "warning");
assert.match(status?.message ?? "", /Anthropic API key/);
assert.match(status?.message ?? "", /Anthropic models/);
});
test("model discovery status names missing OpenAI-compatible credentials", () => {
const status = formatModelDiscoveryErrorStatus(
new Error("config: OPENAI_COMPAT_API_KEY required"),
"openai-compat",
);
assert.equal(status?.tone, "warning");
assert.match(status?.message ?? "", /OpenAI runtime API key/);
assert.match(status?.message ?? "", /OPENAI_COMPAT_API_KEY/);
assert.match(status?.message ?? "", /OpenAI models/);
});
test("Buzz shared compute names the empty state and next action", () => {
const status = formatModelDiscoveryErrorStatus(
new Error("no Buzz shared compute serving members are available"),
"relay-mesh",
);
assert.equal(status?.tone, "warning");
assert.match(status?.message ?? "", /No members are sharing compute/);
assert.match(status?.message ?? "", /Settings > Compute/);
});
test("Buzz shared compute distinguishes relay lookup failures", () => {
const status = formatModelDiscoveryErrorStatus(
new Error("Buzz shared compute model discovery failed: relay offline"),
"relay-mesh",
);
assert.equal(status?.tone, "warning");
assert.match(status?.message ?? "", /couldn't check shared compute/);
assert.match(status?.message ?? "", /relay connection/);
});
test("Buzz shared compute names a missing relay member roster", () => {
const status = formatModelDiscoveryErrorStatus(
new Error("Buzz shared compute is waiting for the current member roster"),
"relay-mesh",
);
assert.equal(status?.tone, "warning");
assert.match(status?.message ?? "", /waiting for the relay's member roster/);
assert.match(status?.message ?? "", /membership configuration/);
assert.doesNotMatch(status?.message ?? "", /relay connection/);
});
test("model discovery status stays quiet for missing Databricks defaults", () => {
const status = formatModelDiscoveryErrorStatus(
new Error("config: DATABRICKS_HOST required"),
"databricks",
);
assert.equal(status, null);
});
test("Databricks sign-in-required is a muted note pointing at the picker and CLI", () => {
const status = formatModelDiscoveryErrorStatus(
new Error(
"Databricks sign-in is required; save this agent, then open its model picker to sign in, or run `buzz-agent auth databricks`",
),
"databricks_v2",
);
assert.equal(status?.tone, "muted");
assert.match(status?.message ?? "", /model picker/);
assert.match(status?.message ?? "", /buzz-agent auth databricks/);
});
test("Databricks sign-in failure warns and points at the explicit retry", () => {
const status = formatModelDiscoveryErrorStatus(
new Error("Databricks sign-in failed: oauth callback: access_denied"),
"databricks_v2",
);
assert.equal(status?.tone, "warning");
assert.match(status?.message ?? "", /didn't complete/);
assert.match(status?.message ?? "", /model picker/);
});
test("Databricks sign-in timeout warns and points at the explicit retry", () => {
const status = formatModelDiscoveryErrorStatus(
new Error(
"Databricks sign-in timed out; open the model picker to retry, or run `buzz-agent auth databricks`",
),
"databricks_v2",
);
assert.equal(status?.tone, "warning");
assert.match(status?.message ?? "", /didn't complete/);
assert.match(status?.message ?? "", /buzz-agent auth databricks/);
});
test("other Databricks discovery failures fall through to the generic notice", () => {
const status = formatModelDiscoveryErrorStatus(
new Error("Databricks model discovery failed: relay offline"),
"databricks_v2",
);
assert.equal(status?.tone, "warning");
assert.match(status?.message ?? "", /Using built-in model options/);
});
test("auth-required errors name the agent and ask for sign-in", () => {
// Real shape from run_agent_models_command wrapping buzz-acp stderr when
// cursor-agent is signed out (spec ErrorCode::AuthRequired text).
const status = formatModelDiscoveryErrorStatus(
new Error(
"buzz-acp models failed (exit 1): agent communication failed: Agent reported error (code -32000): Authentication required",
),
"",
"Cursor",
);
assert.equal(status?.tone, "warning");
assert.match(status?.message ?? "", /Cursor requires sign-in/);
assert.match(status?.message ?? "", /Sign in with the Cursor CLI/);
});
test("auth-required copy degrades gracefully without an agent label", () => {
const status = formatModelDiscoveryErrorStatus(
new Error("Agent reported error (code -32000): Authentication required"),
"",
);
assert.equal(status?.tone, "warning");
assert.match(status?.message ?? "", /This agent requires sign-in/);
});
test("non-auth -32000 errors do NOT get the sign-in copy", () => {
// -32000 is the catch-all fallback code for unclassified agent errors
// (agent_error_from_json unwrap_or(-32000)); only the spec-reserved
// "Authentication required" text may route to the sign-in message.
const status = formatModelDiscoveryErrorStatus(
new Error(
"buzz-acp models failed (exit 1): Agent reported error (code -32000): model catalog fetch timed out",
),
"anthropic",
"Cursor",
);
assert.equal(status?.tone, "warning");
assert.doesNotMatch(status?.message ?? "", /sign-in/i);
assert.match(status?.message ?? "", /Using built-in model options/);
});