Skip to content

Commit 37f0997

Browse files
authored
Merge pull request #73 from VibeEngineeringAI/fix/pr62-17-resolve-dataset-exact-case
PR #62 follow-up 17: resolve_dataset prefers exact-case selection; correct not_found message
2 parents 4ca557a + 15f71f5 commit 37f0997

2 files changed

Lines changed: 107 additions & 17 deletions

File tree

‎docs/tool-api.md‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,20 +161,22 @@ datasets.
161161

162162
`resolve_dataset` accepts an optional `telemetryType` (`logs`, `metrics` or
163163
`traces`) and `limit` (1–20, default 5). Its `status` is `resolved`,
164-
`ambiguous`, `not_found` or `error`. Dataset names are matched
165-
case-insensitively.
164+
`ambiguous`, `not_found` or `error`.
166165

167166
- A `selectedDataset` or `contextDataset` resolves to the visible dataset with
168-
that name. If no visible dataset has that name, the result has
169-
`status: "error"`. If its telemetry type does not match `telemetryType`, the
170-
result is `not_found`.
167+
exactly that (case-sensitive) name. If none has that exact name, it resolves
168+
to the one visible dataset whose name matches case-insensitively; if several
169+
do, the result is `ambiguous` with those datasets as `candidates`. If no
170+
visible dataset matches, the result has `status: "error"`. If the selected
171+
dataset's telemetry type does not match `telemetryType`, the result is
172+
`not_found`.
171173
- Otherwise, if exactly one dataset is visible, it resolves to that dataset, or
172174
returns `not_found` if its telemetry type does not match.
173175
- Otherwise it scores the visible datasets that match `telemetryType` by name
174-
against `query`: 1000 if the name equals the query, 750 if the query contains
175-
the name, or 500 if the name contains the query, plus 25 for each query term
176-
longer than two characters that the name contains, plus 15 when
177-
`telemetryType` is given. It resolves only a unique top score of at least
176+
against `query`, ignoring case: 1000 if the name equals the query, 750 if the
177+
query contains the name, or 500 if the name contains the query, plus 25 for
178+
each query term longer than two characters that the name contains, plus 15
179+
when `telemetryType` is given. It resolves only a unique top score of at least
178180
500. In every other case it returns `ambiguous` with up to `limit` scored
179181
`candidates`, or `not_found` if no visible dataset matches `telemetryType`.
180182

‎src/tool_executor.rs‎

Lines changed: 96 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -366,12 +366,43 @@ pub fn resolve_dataset_candidates(payload: Value, arguments: &Value) -> Value {
366366
.and_then(Value::as_str)
367367
.filter(|name| !name.is_empty());
368368
if let Some(requested_dataset) = requested_dataset {
369-
let selected = datasets.iter().find(|dataset| {
370-
dataset
371-
.get("name")
372-
.and_then(Value::as_str)
373-
.is_some_and(|name| name.eq_ignore_ascii_case(requested_dataset))
374-
});
369+
// Dataset names are case-sensitive: prefer an exact match and fall
370+
// back to a case-insensitive match only when it is unique.
371+
fn dataset_name(dataset: &Value) -> Option<&str> {
372+
dataset.get("name").and_then(Value::as_str)
373+
}
374+
let exact = datasets
375+
.iter()
376+
.find(|dataset| dataset_name(dataset) == Some(requested_dataset));
377+
let case_insensitive = datasets
378+
.iter()
379+
.filter(|dataset| {
380+
dataset_name(dataset)
381+
.is_some_and(|name| name.eq_ignore_ascii_case(requested_dataset))
382+
})
383+
.collect::<Vec<_>>();
384+
let selected = match (exact, case_insensitive.as_slice()) {
385+
(Some(dataset), _) => Some(dataset),
386+
(None, [dataset]) => Some(*dataset),
387+
(None, []) => None,
388+
(None, matches) => {
389+
return json!({
390+
"status": "ambiguous",
391+
"datasets": [],
392+
"candidates": matches
393+
.iter()
394+
.map(|dataset| json!({
395+
"name": dataset_name(dataset),
396+
"reasons": ["case-insensitive dataset name match"],
397+
"dataset": dataset,
398+
}))
399+
.collect::<Vec<_>>(),
400+
"message": format!(
401+
"Dataset `{requested_dataset}` matches several caller-visible datasets that differ only in case. Call resolve_dataset again with the exact dataset name."
402+
),
403+
});
404+
}
405+
};
375406
return match selected {
376407
Some(dataset) if dataset_matches_telemetry_type(dataset, telemetry_type.as_deref()) => {
377408
json!({
@@ -491,9 +522,16 @@ pub fn resolve_dataset_candidates(payload: Value, arguments: &Value) -> Value {
491522
"candidates": candidates,
492523
"query": query,
493524
"message": if datasets.is_empty() {
494-
"No caller-visible datasets were found."
525+
"No caller-visible datasets were found.".to_owned()
526+
} else if candidates.is_empty() {
527+
match telemetry_type.as_deref() {
528+
Some(telemetry_type) => format!(
529+
"No caller-visible datasets match requested telemetry type `{telemetry_type}`."
530+
),
531+
None => "No caller-visible datasets match the request.".to_owned(),
532+
}
495533
} else {
496-
"Dataset resolution is ambiguous. Inspect candidate schemas, then call resolve_dataset again with selectedDataset."
534+
"Dataset resolution is ambiguous. Inspect candidate schemas, then call resolve_dataset again with selectedDataset.".to_owned()
497535
}
498536
})
499537
}
@@ -2165,6 +2203,56 @@ mod tests {
21652203
assert_eq!(sole_mismatch["status"], "not_found");
21662204
}
21672205

2206+
#[actix_web::test]
2207+
async fn shared_dataset_resolution_prefers_exact_case_selection() {
2208+
let datasets = json!([{ "name": "Logs" }, { "name": "logs" }, { "name": "Traces" }]);
2209+
for (requested, expected) in [("logs", "logs"), ("Logs", "Logs")] {
2210+
let resolved = resolve_dataset_candidates(
2211+
datasets.clone(),
2212+
&json!({ "selectedDataset": requested }),
2213+
);
2214+
assert_eq!(resolved["status"], "resolved");
2215+
assert_eq!(resolved["selectedDatasets"], json!([expected]));
2216+
assert_eq!(resolved["datasets"][0]["name"], expected);
2217+
}
2218+
2219+
let unique_fallback =
2220+
resolve_dataset_candidates(datasets.clone(), &json!({ "contextDataset": "traces" }));
2221+
assert_eq!(unique_fallback["status"], "resolved");
2222+
assert_eq!(unique_fallback["selectedDatasets"], json!(["Traces"]));
2223+
2224+
let ambiguous_case =
2225+
resolve_dataset_candidates(datasets, &json!({ "selectedDataset": "LOGS" }));
2226+
assert_eq!(ambiguous_case["status"], "ambiguous");
2227+
assert_eq!(ambiguous_case["datasets"], json!([]));
2228+
let names = ambiguous_case["candidates"]
2229+
.as_array()
2230+
.unwrap()
2231+
.iter()
2232+
.map(|candidate| candidate["name"].clone())
2233+
.collect::<Vec<_>>();
2234+
assert_eq!(names, vec![json!("Logs"), json!("logs")]);
2235+
}
2236+
2237+
#[actix_web::test]
2238+
async fn shared_dataset_resolution_reports_no_matching_candidates() {
2239+
let none_match = resolve_dataset_candidates(
2240+
json!([
2241+
{ "name": "app-logs", "telemetryType": "logs" },
2242+
{ "name": "otel-metrics", "telemetryType": "metrics" }
2243+
]),
2244+
&json!({ "query": "checkout", "telemetryType": "traces" }),
2245+
);
2246+
assert_eq!(none_match["status"], "not_found");
2247+
assert_eq!(none_match["candidates"], json!([]));
2248+
let message = none_match["message"].as_str().unwrap();
2249+
assert!(!message.contains("ambiguous"), "{message}");
2250+
assert_eq!(
2251+
message,
2252+
"No caller-visible datasets match requested telemetry type `traces`."
2253+
);
2254+
}
2255+
21682256
#[actix_web::test]
21692257
async fn shared_time_resolution_honors_timezone_and_reference() {
21702258
let reference = DateTime::parse_from_rfc3339("2026-10-01T12:00:00Z")

0 commit comments

Comments
 (0)