Guard the confidence threshold against an empty list

This commit is contained in:
Thorsten Sommer committed 2026-09-14 11:03:14 +02:00
1 parent cd6a3f1daa
commit 48779ff144
3 files changed
+74 -7

No files matched your search

@@ -2,6 +2,8 @@ namespace AIStudio.Tools;
public static class IConfidenceExtensions
{
private static readonly ILogger<IConfidence> LOGGER = Program.LOGGER_FACTORY.CreateLogger<IConfidence>();
public static TargetWindow DetermineTargetWindow<T>(this IReadOnlyList<T> items, TargetWindowStrategy strategy, int numMaximumItems = 30) where T : IConfidence
{
switch (strategy)
@@ -53,8 +55,18 @@ public static class IConfidenceExtensions
{
if(!targetWindow.IsValid())
{
var logger = Program.SERVICE_PROVIDER.GetService<ILogger<IConfidence>>()!;
logger.LogWarning("The target window is invalid. Returning 0f as threshold.");
LOGGER.LogWarning("The target window is invalid. Returning 0f as threshold.");
return 0f;
}
//
// Without items there is no threshold to find, and the Min and Max calls below would throw
// on an empty sequence. Every caller checks this today, which is precisely how such a guard
// goes missing once a new caller arrives. It belongs here, next to the calls it protects:
//
if(items.Count == 0)
{
LOGGER.LogWarning("There are no items to determine a confidence threshold for. Returning 0f as threshold.");
return 0f;
}
@@ -91,10 +103,7 @@ public static class IConfidenceExtensions
}
}
else
{
var logger = Program.SERVICE_PROVIDER.GetService<ILogger<IConfidence>>()!;
logger.LogWarning("The confidence values are too close. Returning 0f as threshold.");
}
LOGGER.LogWarning("The confidence values are too close. Returning 0f as threshold.");
return threshold;
}
@@ -50,7 +50,7 @@ public class AgenticSrcSelWithDynHeur : IDataSourceSelectionProcess
}
// Log the selected data sources:
var selectedDataSourceInfo = aiSelectedDataSources.Select(ds => $"[Id={ds.Id}, reason={ds.Reason}, confidence={ds.Confidence}]").Aggregate((a, b) => $"'{a}', '{b}'");
var selectedDataSourceInfo = string.Join(", ", aiSelectedDataSources.Select(ds => $"'[Id={ds.Id}, reason={ds.Reason}, confidence={ds.Confidence}]'"));
LOGGER.LogInformation($"The AI selected the data sources automatically. {aiSelectedDataSources.Count} data source(s) are selected: {selectedDataSourceInfo}.");
//