Add the model matching engine and make capabilities a flags enum

This commit is contained in:
Thorsten Sommer committed 2026-09-11 17:50:36 +02:00
1 parent 87566c74e4
commit 3302915d84
27 files changed
+2198 -48

No files matched your search

@@ -0,0 +1,42 @@
namespace AIStudio.Models.Matching;
/// <summary>
/// How tightly a pattern is bound to the name it matches.
/// </summary>
/// <remarks>
/// This is the first thing that decides which of two rules wins, and it is ordered by how much the
/// pattern claims to know: naming the whole model says more than naming how the name begins, which
/// says more than naming a part of it, which says more than appearing somewhere inside it.
/// </remarks>
public enum MatchKind
{
/// <summary>
/// The pattern is the whole name.
/// </summary>
EXACT,
/// <summary>
/// The name begins with the pattern, and a name part ends where the pattern ends.
/// </summary>
PREFIX,
/// <summary>
/// The pattern appears in the name as one or more whole name parts.
/// </summary>
/// <remarks>
/// This is the one to reach for by default. It is what the old rules meant when they said that
/// a family name counts "only where a name part begins", so that looking for the Yi family does
/// not answer for every model whose name happens to contain those two letters.
/// </remarks>
SEGMENT,
/// <summary>
/// The pattern appears anywhere in the name, boundaries or not.
/// </summary>
/// <remarks>
/// The last resort, for the names where a vendor glues things together, such as a version
/// number sitting inside a name part. It claims the least and therefore loses against every
/// other kind, which is what keeps it from swallowing families it was never meant for.
/// </remarks>
SUBSTRING,
}
@@ -0,0 +1,165 @@
using AIStudio.Provider;
namespace AIStudio.Models.Matching;
/// <summary>
/// What a rule says about the names it answers for.
/// </summary>
/// <remarks>
/// A pattern is written in the normalized form a model name is brought into: lowercase, hyphens
/// between the parts, dots kept. A pattern which is not in that form can never match anything, so
/// it is a mistake rather than a rule which happens to be quiet.
///
/// The extra conditions and the bindings are not only there to narrow a pattern down. They also
/// make it more specific, which is how a rule earns the right to win against a shorter one without
/// anybody writing an order.
/// </remarks>
public sealed record MatchPattern
{
/// <summary>
/// How tightly the text is bound to the name.
/// </summary>
public required MatchKind Kind { get; init; }
/// <summary>
/// The text to look for, in normalized form.
/// </summary>
public required string Text { get; init; }
/// <summary>
/// Name parts which have to be present as well.
/// </summary>
/// <remarks>
/// Each one is looked for as a whole name part, the same way the SEGMENT kind looks for its
/// text. Writing a hyphen into one of these is therefore both unnecessary and impossible: it
/// would not be a normalized pattern any more.
/// </remarks>
public IReadOnlyList<string> AlsoContains { get; init; } = [];
/// <summary>
/// Name parts whose presence rules this pattern out.
/// </summary>
public IReadOnlyList<string> NotContains { get; init; } = [];
/// <summary>
/// The provider this rule is written for, or null when it holds anywhere.
/// </summary>
/// <remarks>
/// This is what settles the cases where one name means two models depending on who serves it.
/// On Alibaba, "qwq" is qwq-plus, a commercial model; everywhere else it is the open weights
/// built on Qwen 2.5. Two rules, one of them bound.
/// </remarks>
public LLMProviders? OnlyOn { get; init; }
/// <summary>
/// The vendor this rule is written for, or null when it holds for any.
/// </summary>
/// <remarks>
/// A gateway which unwraps "anthropic/claude-sonnet-4-0" knows who built the model, and a rule
/// may insist on that instead of trusting a name.
/// </remarks>
public ModelVendor? OnlyFrom { get; init; }
/// <summary>
/// Moves this rule ahead of, or behind, everything the computed specificity would decide.
/// </summary>
/// <remarks>
/// The emergency exit, and it is meant to stay unused: the whole point of computing specificity
/// is that nobody writes an order by hand any more. A rule which sets this needs a comment
/// saying what the computation gets wrong, because the next person will read the rank as noise
/// otherwise. Negative values push a rule back.
/// </remarks>
public int ExplicitRank { get; init; }
/// <summary>
/// Whether every text of this pattern is written in normalized form.
/// </summary>
/// <remarks>
/// Normalizing is idempotent, so a text is normalized exactly when normalizing does not change
/// it. The compile time rule checks the same thing; this is what the tests and the verification
/// run use, and what catches a pattern which arrived from a plugin rather than from source.
/// </remarks>
public bool IsWellFormed => IsNormalized(this.Text) && this.AlsoContains.All(IsNormalized) && this.NotContains.All(IsNormalized);
/// <summary>
/// Whether this pattern answers for the given model.
/// </summary>
/// <param name="id">The model name, already normalized.</param>
/// <param name="provider">Who serves the model.</param>
/// <param name="vendor">Who built it, as far as anybody knows.</param>
/// <returns>True, when the rule applies.</returns>
public bool Matches(in ModelId id, LLMProviders provider, ModelVendor vendor)
{
if (this.OnlyOn is not null && this.OnlyOn.Value != provider)
return false;
if (this.OnlyFrom is not null && this.OnlyFrom.Value != vendor)
return false;
if (!this.MatchesText(id))
return false;
foreach (var required in this.AlsoContains)
if (!id.ContainsSegments(required))
return false;
foreach (var forbidden in this.NotContains)
if (id.ContainsSegments(forbidden))
return false;
return true;
}
/// <summary>
/// The name part the index files this pattern under, or an empty span when it cannot file it.
/// </summary>
/// <remarks>
/// A pattern which is bound to the start of a name, or to whole name parts, always begins at a
/// name part, so the first part of the pattern has to appear as a part of any name it matches.
/// That is what lets the index skip it for every other name. A substring pattern makes no such
/// promise and has to be checked against every name.
/// </remarks>
/// <returns>The first name part of the pattern, or empty.</returns>
public ReadOnlySpan<char> IndexKey()
{
if (this.Kind is MatchKind.SUBSTRING || string.IsNullOrWhiteSpace(this.Text))
return [];
var text = this.Text.AsSpan();
var separator = text.IndexOf(ModelId.SEGMENT_SEPARATOR);
return separator is -1 ? text : text[..separator];
}
/// <summary>
/// Everything about this pattern which decides what it matches, as one line of text.
/// </summary>
/// <remarks>
/// Two patterns with the same signature match exactly the same names, which is how the index
/// finds the rules that collide without having to reason about what a pattern could match. The
/// conditions are sorted, because stating them in a different order states the same thing.
/// </remarks>
/// <returns>The signature.</returns>
public string Signature()
{
var required = string.Join(',', this.AlsoContains.Order(StringComparer.Ordinal));
var forbidden = string.Join(',', this.NotContains.Order(StringComparer.Ordinal));
return $"{this.Kind}|{this.Text}|{this.OnlyOn}|{this.OnlyFrom}|+{required}|-{forbidden}";
}
/// <summary>
/// Whether a text is written the way a normalized model name is written.
/// </summary>
/// <param name="text">The text to check.</param>
/// <returns>True, when normalizing it would change nothing.</returns>
public static bool IsNormalized(string text) => !string.IsNullOrEmpty(text) && string.Equals(new ModelId(text).Normalized, text, StringComparison.Ordinal);
private bool MatchesText(in ModelId id) => this.Kind switch
{
MatchKind.EXACT => id.EqualsText(this.Text),
MatchKind.PREFIX => id.StartsWithSegments(this.Text),
MatchKind.SEGMENT => id.ContainsSegments(this.Text),
MatchKind.SUBSTRING => id.ContainsText(this.Text),
_ => false,
};
}
@@ -0,0 +1,234 @@
using System.Collections.Frozen;
using AIStudio.Provider;
namespace AIStudio.Models.Matching;
/// <summary>
/// Answers what is known about a model name, out of all the rules there are.
/// </summary>
/// <remarks>
/// The old rules asked every question in turn: a name arriving at the open weights block walked
/// past more than a hundred string comparisons before anything answered it, and it did so on every
/// render of every component which shows a provider. Here the name is cut into its parts and each
/// part looks up the handful of rules which mention it, so a name is measured against the rules
/// which could possibly apply to it and against nothing else.
///
/// Building the index costs a sort and a dictionary; that happens once. Answering allocates a small
/// list when several rules apply, which is the cold path -- the registry keeps the answers, so the
/// same model is not resolved twice.
///
/// Nothing here reaches for application state. A test can build an index and ask it questions
/// without the app ever having started.
/// </remarks>
public sealed class ModelFamilyIndex
{
private readonly FrozenDictionary<string, ModelRule[]>.AlternateLookup<ReadOnlySpan<char>> byNamePartLookup;
private readonly bool canLookUpNameParts;
private readonly ModelRule[] alwaysChecked;
private ModelFamilyIndex(ModelRule[] rules, FrozenDictionary<string, ModelRule[]> byNamePart, ModelRule[] alwaysChecked, IReadOnlyList<RuleAmbiguity> ambiguities)
{
this.alwaysChecked = alwaysChecked;
this.Rules = rules;
this.Ambiguities = ambiguities;
//
// Looking a name part up as a span rather than as a string is what keeps the lookup free of
// allocations. It needs a comparer which knows how to hash a span, and an index holding no
// rules at all has no comparer to speak of -- there is nothing to look up in that case
// either, so the flag simply skips the walk.
//
this.canLookUpNameParts = byNamePart.TryGetAlternateLookup(out this.byNamePartLookup);
}
/// <summary>
/// Every rule the index was built from, ordered by name.
/// </summary>
public IReadOnlyList<ModelRule> Rules { get; }
/// <summary>
/// Rules which claim exactly the same names as another rule.
/// </summary>
/// <remarks>
/// Found by comparing what the patterns say, which catches the case of two families claiming
/// one name outright. Two patterns which merely happen to overlap on some name cannot be found
/// this way -- deciding that in general is not a question about text any more. Those show up
/// when a name is actually resolved, as tied selectors, which is why the verification run
/// resolves the whole corpus instead of only reading the rules.
/// </remarks>
public IReadOnlyList<RuleAmbiguity> Ambiguities { get; }
/// <summary>
/// Builds an index over a set of rules.
/// </summary>
/// <param name="rules">The rules, in any order. The order they arrive in changes nothing.</param>
/// <returns>The index.</returns>
public static ModelFamilyIndex Build(IEnumerable<ModelRule> rules)
{
//
// Sorting by name, not by specificity: the comparison does the deciding, and a stable order
// is what makes two builds of the same rules produce the same answers, down to which rule
// is reported first in a conflict.
//
var ordered = rules.OrderBy(rule => rule.Description, StringComparer.Ordinal).ToArray();
var buckets = new Dictionary<string, List<ModelRule>>(StringComparer.Ordinal);
var alwaysChecked = new List<ModelRule>();
foreach (var rule in ordered)
{
var namePart = rule.Pattern.IndexKey();
if (namePart.IsEmpty)
{
alwaysChecked.Add(rule);
continue;
}
var key = namePart.ToString();
if (!buckets.TryGetValue(key, out var bucket))
buckets[key] = bucket = [];
bucket.Add(rule);
}
var byNamePart = buckets.ToFrozenDictionary(bucket => bucket.Key, bucket => bucket.Value.ToArray(), StringComparer.Ordinal);
return new(ordered, byNamePart, alwaysChecked.ToArray(), FindAmbiguities(ordered));
}
/// <summary>
/// Says what is known about a model.
/// </summary>
/// <param name="id">The model name.</param>
/// <param name="provider">Who serves the model.</param>
/// <param name="vendor">Who built it, as far as anybody knows.</param>
/// <returns>The profile, which is empty when no rule knows the name.</returns>
public ModelProfile Resolve(in ModelId id, LLMProviders provider, ModelVendor vendor) => this.Explain(id, provider, vendor).Profile;
/// <summary>
/// Says what is known about a model, and which rules said it.
/// </summary>
/// <param name="id">The model name.</param>
/// <param name="provider">Who serves the model.</param>
/// <param name="vendor">Who built it, as far as anybody knows.</param>
/// <returns>The profile together with the rules behind it.</returns>
public ModelResolution Explain(in ModelId id, LLMProviders provider, ModelVendor vendor)
{
if (id.IsEmpty)
return ModelResolution.NOTHING;
var match = new Match();
Consider(this.alwaysChecked, id, provider, vendor, ref match);
if (this.canLookUpNameParts)
foreach (var namePart in id.Segments)
if (this.byNamePartLookup.TryGetValue(namePart, out var candidates))
Consider(candidates, id, provider, vendor, ref match);
//
// Least specific first, so that the rule saying the most about this name has the last word.
// Sorting a list is not stable, so equally specific modifiers are ordered by name: applying
// them in a different order could otherwise produce a different profile on another machine.
//
match.Modifiers?.Sort(static (left, right) =>
{
var order = left.Specificity.CompareTo(right.Specificity);
return order is not 0 ? order : string.CompareOrdinal(left.Description, right.Description);
});
var profile = match.Selector?.Change.ApplyTo(ModelProfile.UNKNOWN) ?? ModelProfile.UNKNOWN;
if (match.Modifiers is not null)
foreach (var modifier in match.Modifiers)
profile = modifier.Change.ApplyTo(profile);
return new(profile, match.Selector, match.Modifiers ?? [], match.TiedSelectors ?? []);
}
private static void Consider(ModelRule[] candidates, in ModelId id, LLMProviders provider, ModelVendor vendor, ref Match match)
{
foreach (var rule in candidates)
{
if (!rule.Pattern.Matches(id, provider, vendor))
continue;
if (rule.Kind is ModelRuleKind.MODIFIER)
{
//
// A rule can be reached twice when a name repeats one of its parts. Applying a
// modifier twice would change nothing, but reporting it twice would read as if two
// rules had spoken.
//
match.Modifiers ??= [];
if (!match.Modifiers.Contains(rule))
match.Modifiers.Add(rule);
continue;
}
if (match.Selector is null)
{
match.Selector = rule;
continue;
}
if (ReferenceEquals(match.Selector, rule))
continue;
var order = rule.Specificity.CompareTo(match.Selector.Specificity);
if (order > 0)
{
match.Selector = rule;
match.TiedSelectors = null;
continue;
}
if (order < 0)
continue;
//
// Both rules claim the name with the same right, which the rules should not allow. The
// answer still has to be the same one on every machine and in every build, so the name
// of the rule decides rather than the order the rules arrived in.
//
var winner = string.CompareOrdinal(rule.Description, match.Selector.Description) < 0 ? rule : match.Selector;
var loser = ReferenceEquals(winner, rule) ? match.Selector : rule;
match.Selector = winner;
(match.TiedSelectors ??= []).Add(loser);
}
}
private static IReadOnlyList<RuleAmbiguity> FindAmbiguities(IReadOnlyList<ModelRule> rules)
{
var ambiguities = new List<RuleAmbiguity>();
var claimed = new Dictionary<string, ModelRule>(StringComparer.Ordinal);
foreach (var rule in rules)
{
if (rule.Kind is not ModelRuleKind.SELECTOR)
continue;
var signature = rule.Pattern.Signature();
if (claimed.TryGetValue(signature, out var other))
{
ambiguities.Add(new(other, rule, "Two selectors claim exactly the same model names."));
continue;
}
claimed[signature] = rule;
}
return ambiguities;
}
/// <summary>
/// What the walk over the candidate rules has found so far.
/// </summary>
private struct Match
{
public ModelRule? Selector;
public List<ModelRule>? TiedSelectors;
public List<ModelRule>? Modifiers;
}
}
@@ -0,0 +1,179 @@
namespace AIStudio.Models.Matching;
/// <summary>
/// A model ID in the form the rules are written in, next to the form the provider reported.
/// </summary>
/// <remarks>
/// Every provider names the same model differently, and the difference is rarely in the words: it
/// is in what sits between them. Ollama separates the variant with a colon ("qwen3.8:27b-mlx"),
/// Blablador answers with a whole sentence ("10 - Muse Glimmer 30b - the newest META model"),
/// Fireworks puts a path in front ("accounts/fireworks/models/llama-v3p1-405b-instruct"), and the
/// hubs use hyphens. Normalizing once, here, is what lets a rule be written once.
///
/// The dots stay. They carry the version boundary: llama3 and llama3.1 are different models, and
/// only the latter calls functions. Dropping them would merge the two. A hyphen, on the other hand,
/// is where one part of a name ends and the next begins -- which is why the patterns can say "at a
/// name part" and mean something.
/// </remarks>
/// <param name="modelId">The model ID as the provider reports it.</param>
public readonly struct ModelId(string modelId) : IEquatable<ModelId>
{
/// <summary>
/// What separates two parts of a normalized name.
/// </summary>
public const char SEGMENT_SEPARATOR = '-';
/// <summary>
/// The longest model ID we normalize without going to the heap.
/// </summary>
private const int MAX_STACK_ALLOCATED_MODEL_ID_LENGTH = 256;
private readonly string originalId = modelId ?? string.Empty;
private readonly string normalizedId = Normalize(modelId);
/// <summary>
/// The ID exactly as the provider reported it. This is what a person sees.
/// </summary>
public string Original => this.originalId ?? string.Empty;
/// <summary>
/// The ID in lowercase, with every separator written as a single hyphen.
/// </summary>
public string Normalized => this.normalizedId ?? string.Empty;
/// <summary>
/// Whether there is nothing here to match against.
/// </summary>
public bool IsEmpty => string.IsNullOrEmpty(this.normalizedId);
/// <summary>
/// The parts of the name, in order, without allocating anything.
/// </summary>
public ModelIdSegments Segments => new(this.Normalized.AsSpan());
/// <summary>
/// Whether the whole name is exactly this text.
/// </summary>
/// <param name="text">The text to compare against, already normalized.</param>
/// <returns>True, when the name and the text are the same.</returns>
public bool EqualsText(ReadOnlySpan<char> text) => !text.IsEmpty && this.Normalized.AsSpan().SequenceEqual(text);
/// <summary>
/// Whether the name begins with this text and a name part ends there.
/// </summary>
/// <remarks>
/// The boundary is what keeps "gpt-5" away from "gpt-55", and what keeps it away from "gpt-5.1"
/// as well: a dot is a version boundary, not a name part boundary, so those are two models and
/// a rule for one of them does not answer for the other.
/// </remarks>
/// <param name="text">The text to look for, already normalized.</param>
/// <returns>True, when the name starts with the text.</returns>
public bool StartsWithSegments(ReadOnlySpan<char> text)
{
if (text.IsEmpty)
return false;
var name = this.Normalized.AsSpan();
return name.StartsWith(text) && IsBoundaryAt(name, text.Length);
}
/// <summary>
/// Whether this text appears in the name as one or more whole name parts.
/// </summary>
/// <param name="text">The text to look for, already normalized.</param>
/// <returns>True, when the text sits between two name part boundaries.</returns>
public bool ContainsSegments(ReadOnlySpan<char> text)
{
if (text.IsEmpty)
return false;
var name = this.Normalized.AsSpan();
var searchedUpTo = 0;
while (searchedUpTo <= name.Length - text.Length)
{
var offset = name[searchedUpTo..].IndexOf(text);
if (offset is -1)
return false;
var start = searchedUpTo + offset;
if (IsBoundaryAt(name, start - 1) && IsBoundaryAt(name, start + text.Length))
return true;
// The same text may appear again further on, at a boundary this time:
searchedUpTo = start + 1;
}
return false;
}
/// <summary>
/// Whether this text appears anywhere in the name, boundaries or not.
/// </summary>
/// <param name="text">The text to look for, already normalized.</param>
/// <returns>True, when the name contains the text.</returns>
public bool ContainsText(ReadOnlySpan<char> text) => !text.IsEmpty && this.Normalized.AsSpan().IndexOf(text) is not -1;
public bool Equals(ModelId other) => string.Equals(this.Normalized, other.Normalized, StringComparison.Ordinal);
public override bool Equals(object? obj) => obj is ModelId other && this.Equals(other);
public override int GetHashCode() => StringComparer.Ordinal.GetHashCode(this.Normalized);
public override string ToString() => this.Original;
/// <summary>
/// Whether a name part begins or ends at this position.
/// </summary>
/// <remarks>
/// Positions outside the name count: the start of the name and its end are boundaries, which is
/// what makes a one part name match a rule written for that part.
/// </remarks>
/// <param name="name">The normalized name.</param>
/// <param name="index">The position to look at, which may be outside the name.</param>
/// <returns>True, when there is a boundary at this position.</returns>
private static bool IsBoundaryAt(ReadOnlySpan<char> name, int index) => index < 0 || index >= name.Length || name[index] is SEGMENT_SEPARATOR;
/// <summary>
/// Brings a model ID into the form the capability rules are written in.
/// </summary>
/// <param name="modelId">The model ID as the provider reports it, which may be nothing at all.</param>
/// <returns>The model ID in lowercase, with every separator written as a single hyphen.</returns>
private static string Normalize(string? modelId)
{
if (string.IsNullOrWhiteSpace(modelId))
return string.Empty;
//
// Normalizing never makes a name longer, so the original length is always enough room.
// Model IDs are short, which is why the buffer lives on the stack: the longest ones we
// know of are the descriptive names Blablador answers with, at around 75 characters. A
// provider reporting something longer still gets a correct answer, just from the heap.
//
Span<char> normalized = modelId.Length <= MAX_STACK_ALLOCATED_MODEL_ID_LENGTH
? stackalloc char[modelId.Length]
: new char[modelId.Length];
var length = 0;
foreach (var character in modelId)
{
if (char.IsAsciiLetterOrDigit(character) || character is '.')
{
normalized[length++] = char.ToLowerInvariant(character);
continue;
}
// Anything else separates two parts of the name. A leading separator, and a repeated
// one, say nothing and would only get in the way of the patterns:
if (length is 0 || normalized[length - 1] is SEGMENT_SEPARATOR)
continue;
normalized[length++] = SEGMENT_SEPARATOR;
}
// A trailing separator carries no meaning either:
if (length > 0 && normalized[length - 1] is SEGMENT_SEPARATOR)
length--;
return new string(normalized[..length]);
}
}
@@ -0,0 +1,57 @@
namespace AIStudio.Models.Matching;
/// <summary>
/// Walks the parts of a normalized model name without cutting it into strings.
/// </summary>
/// <remarks>
/// The index looks up every part of a name to find the rules which could possibly apply to it. That
/// happens for every model of every configured provider, so the walk itself must not allocate: the
/// parts stay slices of the name they came from. This is both the enumerable and the enumerator,
/// which is what lets foreach use it without an interface in between.
/// </remarks>
/// <param name="normalizedId">The normalized model name to walk.</param>
public ref struct ModelIdSegments(ReadOnlySpan<char> normalizedId)
{
private ReadOnlySpan<char> remaining = normalizedId;
/// <summary>
/// The part the walk currently stands on.
/// </summary>
public ReadOnlySpan<char> Current { get; private set; } = default;
/// <summary>
/// Hands foreach the walk itself.
/// </summary>
/// <returns>This walk, at its beginning.</returns>
public readonly ModelIdSegments GetEnumerator() => this;
/// <summary>
/// Steps to the next part of the name.
/// </summary>
/// <returns>True, as long as there was one.</returns>
public bool MoveNext()
{
while (!this.remaining.IsEmpty)
{
var separator = this.remaining.IndexOf(ModelId.SEGMENT_SEPARATOR);
if (separator is -1)
{
this.Current = this.remaining;
this.remaining = default;
return true;
}
this.Current = this.remaining[..separator];
this.remaining = this.remaining[(separator + 1)..];
//
// Normalizing leaves no empty part behind, so this only guards against a name which
// never went through it. Skipping is the right answer: an empty part matches nothing.
//
if (!this.Current.IsEmpty)
return true;
}
return false;
}
}
@@ -0,0 +1,37 @@
namespace AIStudio.Models.Matching;
/// <summary>
/// What the index made of one model name, and how it got there.
/// </summary>
/// <remarks>
/// The profile alone is what the app asks for. The rest is for the people maintaining the rules:
/// which rule answered, what adjusted the answer afterwards, and whether two rules claimed the name
/// with the same right. The verification run reads all of it; a test that wants to know why a model
/// came out the way it did reads it too.
/// </remarks>
/// <param name="Profile">Everything known about the model.</param>
/// <param name="Selector">The rule which chose the model, or null when no rule knows the name.</param>
/// <param name="Modifiers">The rules which adjusted the answer, in the order they were applied.</param>
/// <param name="TiedSelectors">Rules which claimed the name just as strongly as the selector did.</param>
public sealed record ModelResolution(ModelProfile Profile, ModelRule? Selector, IReadOnlyList<ModelRule> Modifiers, IReadOnlyList<ModelRule> TiedSelectors)
{
/// <summary>
/// The answer for a name no rule was even asked about.
/// </summary>
public static readonly ModelResolution NOTHING = new(ModelProfile.UNKNOWN, null, [], []);
/// <summary>
/// Whether more than one rule claimed this name with the same specificity.
/// </summary>
/// <remarks>
/// Always a mistake in the rules. The answer is still the same one every time, so a build never
/// depends on the order the rules were registered in, but which of the two was meant is
/// something only a person can say.
/// </remarks>
public bool IsAmbiguous => this.TiedSelectors.Count > 0;
/// <summary>
/// Whether any rule at all knew this name.
/// </summary>
public bool IsKnown => this.Selector is not null;
}
@@ -0,0 +1,43 @@
namespace AIStudio.Models.Matching;
/// <summary>
/// One statement about a set of model names: which names, and what holds for them.
/// </summary>
/// <param name="pattern">Which names this rule answers for.</param>
/// <param name="kind">Whether the rule chooses the model or adjusts the choice.</param>
/// <param name="change">What the rule states.</param>
/// <param name="origin">Who wrote the rule, so that a conflict can name both sides.</param>
public sealed class ModelRule(MatchPattern pattern, ModelRuleKind kind, ModelProfileChange change, string origin)
{
/// <summary>
/// Which names this rule answers for.
/// </summary>
public MatchPattern Pattern { get; } = pattern;
/// <summary>
/// Whether the rule chooses the model or adjusts the choice.
/// </summary>
public ModelRuleKind Kind { get; } = kind;
/// <summary>
/// What the rule states.
/// </summary>
public ModelProfileChange Change { get; } = change;
/// <summary>
/// Who wrote the rule: a family, a host, or a plugin.
/// </summary>
public string Origin { get; } = origin;
/// <summary>
/// How much this rule claims to know, worked out once when the rule is built.
/// </summary>
public RuleSpecificity Specificity { get; } = RuleSpecificity.Of(pattern);
/// <summary>
/// Names the rule in one line, for conflict reports and for breaking ties the same way twice.
/// </summary>
public string Description { get; } = $"{origin}: {kind} {pattern.Kind} \"{pattern.Text}\"";
public override string ToString() => this.Description;
}
@@ -0,0 +1,24 @@
namespace AIStudio.Models.Matching;
/// <summary>
/// What a rule does once it matches.
/// </summary>
public enum ModelRuleKind
{
/// <summary>
/// Chooses which model this is. Exactly one selector wins, the most specific one.
/// </summary>
SELECTOR,
/// <summary>
/// Adjusts whatever the selector chose. Every matching modifier applies.
/// </summary>
/// <remarks>
/// This is for the statements which hold across families, and which every family would
/// otherwise have to repeat: a base checkpoint was never instruction tuned no matter who built
/// it, and a gateway serving somebody else's model cannot offer that vendor's own API. In the
/// old rules those had to sit at the very top of the file, which is why anything below them
/// could not state an exception.
/// </remarks>
MODIFIER,
}
@@ -0,0 +1,12 @@
namespace AIStudio.Models.Matching;
/// <summary>
/// Two rules which claim the same names with the same right.
/// </summary>
/// <param name="First">One of the two rules.</param>
/// <param name="Second">The other one.</param>
/// <param name="Reason">What makes them collide, in a sentence a person can act on.</param>
public sealed record RuleAmbiguity(ModelRule First, ModelRule Second, string Reason)
{
public override string ToString() => $"{this.Reason} ({this.First.Description} <-> {this.Second.Description})";
}
@@ -0,0 +1,60 @@
namespace AIStudio.Models.Matching;
/// <summary>
/// How much a rule claims to know, computed from the rule itself.
/// </summary>
/// <remarks>
/// This is the heart of the whole rebuild. In the old rules, which branch won was decided by where
/// it stood in the file, so a block for one family could swallow another one -- the Llama block ate
/// the DeepSeek distills because it happened to come first -- and nothing in the language noticed.
/// Here nobody writes an order. A rule saying more about a name beats a rule saying less, and
/// "deepseek-r1" says more than "llama" without anyone deciding that it should.
///
/// Two rules of equal specificity which can match the same name are a mistake, not a coin toss.
/// The index reports them, and resolving still picks the same one every time, so a build never
/// depends on which rule was registered first.
/// </remarks>
/// <param name="ExplicitRank">What a rule wrote down by hand to override all of the below.</param>
/// <param name="Kind">How tightly the pattern is bound to the name.</param>
/// <param name="PatternLength">How much of the name the pattern spells out.</param>
/// <param name="Conditions">How many further name parts the rule requires or forbids.</param>
/// <param name="Binding">Whether the rule is tied to a provider, a vendor, or both.</param>
public readonly record struct RuleSpecificity(int ExplicitRank, int Kind, int PatternLength, int Conditions, int Binding) : IComparable<RuleSpecificity>
{
/// <summary>
/// Works out how specific a pattern is.
/// </summary>
/// <param name="pattern">The pattern to measure.</param>
/// <returns>Its specificity.</returns>
public static RuleSpecificity Of(MatchPattern pattern) => new(
ExplicitRank: pattern.ExplicitRank,
Kind: WeightOf(pattern.Kind),
PatternLength: pattern.Text.Length,
Conditions: pattern.AlsoContains.Count + pattern.NotContains.Count,
Binding: (pattern.OnlyOn is null ? 0 : 1) + (pattern.OnlyFrom is null ? 0 : 1));
/// <summary>
/// Compares two specificities, most specific last.
/// </summary>
/// <remarks>
/// The criteria are weighed in the order they are written in this type, and a tuple compares
/// exactly that way: the first difference decides, the rest is never looked at. The hand
/// written rank comes first because an emergency exit which the length of some other pattern
/// can overrule is not an exit at all.
/// </remarks>
/// <param name="other">The specificity to compare against.</param>
/// <returns>A negative number when this one is less specific, zero when they are equal.</returns>
public int CompareTo(RuleSpecificity other) =>
(this.ExplicitRank, this.Kind, this.PatternLength, this.Conditions, this.Binding)
.CompareTo((other.ExplicitRank, other.Kind, other.PatternLength, other.Conditions, other.Binding));
private static int WeightOf(MatchKind kind) => kind switch
{
MatchKind.EXACT => 3,
MatchKind.PREFIX => 2,
MatchKind.SEGMENT => 1,
MatchKind.SUBSTRING => 0,
_ => 0,
};
}