Skip to content

Commit ed85e9a

Browse files
atishpatelclaude
andcommitted
feat(agents): order the Databricks catalog newest-first
The gateway pages `api/ai-gateway/v2/endpoints` in two phases — Databricks-managed endpoints first, then workspace-created ones (the page token literally decodes to `{"phase":"user"}`) — each alphabetical by name. Buzz sorted nothing, so that order reached the picker unchanged and buried brand-new models: `databricks-claude-opus-5` sat 8th behind five older Claude endpoints, and `goose-claude-opus-5` (the newest endpoint in the catalog) sat 55th of 63. Each endpoint carries `created_timestamp`, so sort on it: newest first, ties broken by name. The tiebreak matters — eleven managed endpoints share one placeholder timestamp (1699610000000), and without it their relative order would be arbitrary between runs. Sorting in `fetch_v2_models` means both discovery paths inherit it with no wire or type changes: the desktop's in-process `discover_databricks_models` and the `buzz-acp models --json` subprocess both go through this function, and the combobox filter preserves incoming order. Endpoints with an absent or unparseable timestamp sort last rather than first, so a wire-shape change degrades to "unordered at the bottom" instead of "shuffled to the top". `created_timestamp` is accepted as either a JSON string (what the gateway sends) or a bare number. Live against block-lakehouse-production, top of the list is now: goose-claude-opus-5 2026-07-24 databricks-claude-opus-5 2026-07-23 databricks-gemini-3-6-flash 2026-07-20 databricks-gemini-3-5-flash-lite 2026-07-20 databricks-inkling 2026-07-14 Caveat worth knowing: the placeholder timestamp is not always accurate for managed endpoints, so a few genuinely recent ones (`databricks-kimi-k2-7-code`, `databricks-llama-4-maverick`) land at the bottom with the 2023 batch. The gateway gives us nothing better to sort on. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 425ec77 commit ed85e9a

1 file changed

Lines changed: 120 additions & 15 deletions

File tree

‎crates/buzz-agent/src/catalog.rs‎

Lines changed: 120 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ async fn fetch_v2_models(
233233
host: &str,
234234
bearer: &str,
235235
) -> Result<Vec<ModelEntry>, AgentError> {
236-
let mut all_models: Vec<ModelEntry> = Vec::new();
236+
let mut all_endpoints: Vec<V2Endpoint> = Vec::new();
237237
let mut page_token: Option<String> = None;
238238
let base_url = format!("{host}/api/ai-gateway/v2/endpoints");
239239

@@ -271,8 +271,8 @@ async fn fetch_v2_models(
271271
))
272272
})?;
273273

274-
let (page_models, next) = parse_v2_endpoints_page(&json)?;
275-
all_models.extend(page_models);
274+
let (page_endpoints, next) = parse_v2_endpoints_page(&json)?;
275+
all_endpoints.extend(page_endpoints);
276276

277277
match next {
278278
Some(tok) if Some(&tok) != page_token.as_ref() => page_token = Some(tok),
@@ -281,28 +281,74 @@ async fn fetch_v2_models(
281281
}
282282

283283
// Fall back to known-model list if the API returned nothing.
284-
if all_models.is_empty() {
285-
all_models = DATABRICKS_V2_KNOWN_MODELS
284+
if all_endpoints.is_empty() {
285+
return Ok(DATABRICKS_V2_KNOWN_MODELS
286286
.iter()
287287
.map(|id| ModelEntry {
288288
id: id.to_string(),
289289
name: id.to_string(),
290290
})
291-
.collect();
291+
.collect());
292292
}
293293

294-
Ok(all_models)
294+
sort_v2_endpoints_newest_first(&mut all_endpoints);
295+
296+
Ok(all_endpoints
297+
.into_iter()
298+
.map(|endpoint| endpoint.entry)
299+
.collect())
300+
}
301+
302+
/// A v2 gateway endpoint plus the key discovery orders the catalog by.
303+
#[derive(Debug, Clone, PartialEq, Eq)]
304+
pub(crate) struct V2Endpoint {
305+
pub(crate) entry: ModelEntry,
306+
/// `created_timestamp` as epoch milliseconds. `None` when the field is
307+
/// absent or unparseable — those sort last rather than jumping the queue.
308+
pub(crate) created_ms: Option<i64>,
309+
}
310+
311+
/// Read `created_timestamp` from one endpoint object.
312+
///
313+
/// The gateway sends epoch milliseconds as a JSON *string*
314+
/// (`"created_timestamp": "1699610000000"`); accept a bare number too, so a
315+
/// wire-shape change doesn't silently drop every endpoint to the bottom.
316+
fn endpoint_created_ms(endpoint: &serde_json::Value) -> Option<i64> {
317+
let value = endpoint.get("created_timestamp")?;
318+
value
319+
.as_i64()
320+
.or_else(|| value.as_str()?.trim().parse::<i64>().ok())
321+
}
322+
323+
/// Order the catalog newest-first, breaking ties by name.
324+
///
325+
/// The gateway returns endpoints in two phases — Databricks-managed first, then
326+
/// workspace-created — each alphabetical by name, which buries a brand-new
327+
/// frontier model deep in the list. Newest-first puts the models people are
328+
/// reaching for at the top of the picker.
329+
///
330+
/// Endpoints with no usable timestamp sort last, and the name tiebreak keeps the
331+
/// result stable: several managed endpoints share one placeholder timestamp, so
332+
/// without it their relative order would be arbitrary.
333+
pub(crate) fn sort_v2_endpoints_newest_first(endpoints: &mut [V2Endpoint]) {
334+
endpoints.sort_by(|a, b| {
335+
// `None` < `Some(_)`, so reversing puts timestamped endpoints first.
336+
b.created_ms
337+
.cmp(&a.created_ms)
338+
.then_with(|| a.entry.name.cmp(&b.entry.name))
339+
});
295340
}
296341

297342
/// Parse one page of a `GET api/ai-gateway/v2/endpoints` response.
298343
///
299-
/// Returns `(models, next_page_token)`. An empty or absent `next_page_token`
344+
/// Returns `(endpoints, next_page_token)`. An empty or absent `next_page_token`
300345
/// signals the last page. Endpoints that cannot serve chat traffic are dropped
301346
/// (see [`is_chat_capable_endpoint`]) so the model picker only offers models the
302-
/// agent can actually run.
347+
/// agent can actually run. Page order is preserved here; the caller sorts once
348+
/// every page is in (see [`sort_v2_endpoints_newest_first`]).
303349
pub(crate) fn parse_v2_endpoints_page(
304350
json: &serde_json::Value,
305-
) -> Result<(Vec<ModelEntry>, Option<String>), AgentError> {
351+
) -> Result<(Vec<V2Endpoint>, Option<String>), AgentError> {
306352
let endpoints = json
307353
.get("endpoints")
308354
.and_then(|v| v.as_array())
@@ -320,9 +366,12 @@ pub(crate) fn parse_v2_endpoints_page(
320366
if !is_chat_capable_endpoint(&name) {
321367
return None;
322368
}
323-
Some(ModelEntry {
324-
id: name.clone(),
325-
name,
369+
Some(V2Endpoint {
370+
entry: ModelEntry {
371+
id: name.clone(),
372+
name,
373+
},
374+
created_ms: endpoint_created_ms(endpoint),
326375
})
327376
})
328377
.collect();
@@ -397,7 +446,7 @@ mod tests {
397446
});
398447

399448
let (models, next) = parse_v2_endpoints_page(&json).unwrap();
400-
let ids: Vec<&str> = models.iter().map(|m| m.id.as_str()).collect();
449+
let ids: Vec<&str> = models.iter().map(|m| m.entry.id.as_str()).collect();
401450
assert_eq!(
402451
ids,
403452
vec![
@@ -457,14 +506,70 @@ mod tests {
457506
});
458507

459508
let (models, _) = parse_v2_endpoints_page(&json).unwrap();
460-
let ids: Vec<&str> = models.iter().map(|m| m.id.as_str()).collect();
509+
let ids: Vec<&str> = models.iter().map(|m| m.entry.id.as_str()).collect();
461510
// Image endpoints DO answer chat requests, so they are retained.
462511
assert_eq!(
463512
ids,
464513
vec!["databricks-claude-opus-5", "databricks-gemini-3-pro-image"]
465514
);
466515
}
467516

517+
#[test]
518+
fn v2_parse_reads_created_timestamp_in_either_wire_shape() {
519+
// The gateway sends epoch ms as a string; a bare number must work too.
520+
let json = serde_json::json!({
521+
"endpoints": [
522+
{"name": "string-ts", "created_timestamp": "1784932442251"},
523+
{"name": "number-ts", "created_timestamp": 1784932442251i64},
524+
{"name": "junk-ts", "created_timestamp": "not-a-number"},
525+
{"name": "no-ts"},
526+
]
527+
});
528+
529+
let (models, _) = parse_v2_endpoints_page(&json).unwrap();
530+
let stamps: Vec<Option<i64>> = models.iter().map(|m| m.created_ms).collect();
531+
assert_eq!(
532+
stamps,
533+
vec![Some(1784932442251), Some(1784932442251), None, None,]
534+
);
535+
}
536+
537+
#[test]
538+
fn v2_endpoints_sort_newest_first_then_by_name() {
539+
// Mirrors the real catalog: the gateway pages Databricks-managed
540+
// endpoints first, then workspace-created ones, each alphabetical — so
541+
// the newest model is buried mid-list until this sort runs.
542+
let json = serde_json::json!({
543+
"endpoints": [
544+
{"name": "databricks-claude-opus-5", "created_timestamp": "1784851200000"},
545+
{"name": "databricks-gpt-5-6-sol", "created_timestamp": "1784073600000"},
546+
{"name": "databricks-gpt-5-6-luna", "created_timestamp": "1784073600000"},
547+
{"name": "databricks-llama-4-maverick", "created_timestamp": "1699610000000"},
548+
{"name": "goose-claude-opus-5", "created_timestamp": "1784932442251"},
549+
{"name": "endpoint-without-timestamp"},
550+
]
551+
});
552+
553+
let (mut models, _) = parse_v2_endpoints_page(&json).unwrap();
554+
sort_v2_endpoints_newest_first(&mut models);
555+
556+
let ids: Vec<&str> = models.iter().map(|m| m.entry.id.as_str()).collect();
557+
assert_eq!(
558+
ids,
559+
vec![
560+
// Newest first, across both pagination phases.
561+
"goose-claude-opus-5",
562+
"databricks-claude-opus-5",
563+
// Same timestamp — the name tiebreak keeps this deterministic.
564+
"databricks-gpt-5-6-luna",
565+
"databricks-gpt-5-6-sol",
566+
"databricks-llama-4-maverick",
567+
// No usable timestamp sorts last, never first.
568+
"endpoint-without-timestamp",
569+
]
570+
);
571+
}
572+
468573
#[test]
469574
fn is_chat_capable_endpoint_keeps_unrecognised_names() {
470575
// Prefer including over silently dropping — an unknown family is kept.

0 commit comments

Comments
 (0)