Files
buzz/desktop/src-tauri/src/commands/agent_models_databricks_tests.rs
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

110 lines
4.0 KiB
Rust

//! Cooldown and interactive-auth policy tests for Databricks discovery.
//!
//! Housed as a child of `agent_models_databricks` (not the shared
//! `agent_models_tests`) so the async timeout/cooldown cases sit next to the
//! code they exercise and reach its `pub(super)` items directly via
//! `use super::*` — and so the shared test file stays under its size ratchet.
use super::*;
#[test]
fn databricks_cooldown_suppresses_passive_relaunch_but_never_the_picker() {
let cooldowns = AuthCooldown::default();
let host = "https://example.cloud.databricks.com";
let now = Instant::now();
// A fresh host permits either surface to launch.
assert!(cooldowns.permits_launch(DatabricksAuthIntent::PassiveDraftDiscovery, host, now));
assert!(cooldowns.permits_launch(DatabricksAuthIntent::InteractiveModelPicker, host, now));
// After a failed/cancelled attempt, passive discovery must NOT re-pop the
// browser while the window is active...
cooldowns.record(host, now);
assert!(!cooldowns.permits_launch(DatabricksAuthIntent::PassiveDraftDiscovery, host, now));
// ...but an explicit picker click always launches, and clears the window so
// a later passive read is unblocked too.
assert!(cooldowns.permits_launch(DatabricksAuthIntent::InteractiveModelPicker, host, now));
assert!(cooldowns.permits_launch(DatabricksAuthIntent::PassiveDraftDiscovery, host, now));
}
#[test]
fn databricks_cooldown_expires_after_its_window_and_is_host_scoped() {
let cooldowns = AuthCooldown::default();
let host = "https://a.cloud.databricks.com";
let other = "https://b.cloud.databricks.com";
let now = Instant::now();
cooldowns.record(host, now);
// A cooldown on one host never suppresses another.
assert!(!cooldowns.is_active(other, now));
assert!(cooldowns.is_active(host, now));
// The window is closed the instant it elapses, so a genuine later retry
// launches again.
let after = now + AUTH_COOLDOWN;
assert!(!cooldowns.is_active(host, after));
}
#[tokio::test]
async fn databricks_interactive_auth_success_clears_a_prior_cooldown() {
let cooldowns = AuthCooldown::default();
let host = "https://example.cloud.databricks.com";
let redaction = BTreeMap::new();
cooldowns.record(host, Instant::now());
let result = run_interactive_databricks_auth(
async { Ok(()) },
Duration::from_secs(150),
&cooldowns,
host,
&redaction,
)
.await;
assert!(result.is_ok());
assert!(!cooldowns.is_active(host, Instant::now()));
}
#[tokio::test]
async fn databricks_interactive_auth_failure_records_a_cooldown() {
let cooldowns = AuthCooldown::default();
let host = "https://example.cloud.databricks.com";
let redaction = BTreeMap::new();
let result = run_interactive_databricks_auth(
async { Err(buzz_agent_pkg::AgentError::LlmAuth("closed the tab".into())) },
Duration::from_secs(150),
&cooldowns,
host,
&redaction,
)
.await;
let error = result.expect_err("a failed sign-in must surface an error");
assert!(error.contains("Databricks sign-in failed"));
assert!(cooldowns.is_active(host, Instant::now()));
}
#[tokio::test(start_paused = true)]
async fn databricks_interactive_auth_timeout_records_cooldown_and_returns_timeout_copy() {
let cooldowns = AuthCooldown::default();
let host = "https://example.cloud.databricks.com";
let redaction = BTreeMap::new();
// An abandoned SSO tab: the flow never resolves. Under the paused clock the
// injected timeout fires deterministically without real waiting.
let result = run_interactive_databricks_auth(
std::future::pending::<Result<(), buzz_agent_pkg::AgentError>>(),
Duration::from_secs(150),
&cooldowns,
host,
&redaction,
)
.await;
let error = result.expect_err("a timed-out sign-in must surface an error");
assert_eq!(error, databricks_sign_in_timed_out_error());
assert!(cooldowns.is_active(host, Instant::now()));
}