Add the model family API, its compile-time registry, and MWAIS0013

This commit is contained in:
Thorsten Sommer 2026-09-11 18:28:24 +02:00
parent 3302915d84
commit d3b28134d9
Signed by untrusted user who does not match committer: tsommer
GPG Key ID: 371BBA77A02C0108
15 changed files with 1554 additions and 0 deletions

View File

@ -0,0 +1,56 @@
using AIStudio.Models.Matching;
using AIStudio.Provider;
namespace AIStudio.Models.Hosting;
/// <summary>
/// One place a model can be reached from, and what reaching it that way does to the answer.
/// </summary>
/// <remarks>
/// This is the routing graph, written down instead of grown into the rules. The old code solved
/// gateways and resellers by having one vendor's rules call another's, which turned into mutual
/// recursion -- Mistral into the open weights, the open weights back into Anthropic, Google, and
/// OpenAI -- and nobody could say from reading it which way a name would travel.
///
/// A host does two things, and only these two. It unwraps a name until the model underneath is
/// visible, and it says what the transport takes away. Unwrapping is iterative on purpose, because
/// the wrappings stack: Hugging Face first drops the routing suffix, then the organization prefix.
/// A host which serves other people's models under their plain names unwraps nothing and only
/// trims the transport, which is the same mechanism rather than a special case.
/// </remarks>
public interface IModelHost
{
/// <summary>
/// The provider this host answers for.
/// </summary>
LLMProviders Provider { get; }
/// <summary>
/// Where the statements about this host were read, and when.
/// </summary>
ModelSource Source { get; }
/// <summary>
/// Takes one wrapping off a name, if there is one.
/// </summary>
/// <remarks>
/// Called again with whatever comes out, until it says no. A host which declares who built the
/// model saves the rules from having to guess it from the name.
/// </remarks>
/// <param name="id">The name as it arrived.</param>
/// <param name="inner">The name with one wrapping removed.</param>
/// <param name="declaredVendor">Who the wrapping says built the model, when it says so.</param>
/// <returns>True, when a wrapping was removed.</returns>
bool TryUnwrap(in ModelId id, out ModelId inner, out ModelVendor? declaredVendor);
/// <summary>
/// Takes away what this host cannot offer, whatever the model itself can do.
/// </summary>
/// <remarks>
/// A provider reselling somebody else's model speaks its own dialect, not the vendor's: the
/// model may well be able to answer through a vendor specific API, but not here.
/// </remarks>
/// <param name="profile">What the model can do.</param>
/// <returns>What it can do through this host.</returns>
ModelProfile ApplyTransport(in ModelProfile profile);
}

View File

@ -0,0 +1,71 @@
using AIStudio.Models.Matching;
namespace AIStudio.Models;
/// <summary>
/// Everything the app knows about one family of models, in one place.
/// </summary>
/// <remarks>
/// A family is a class, and adding one is all it takes: the source generator finds it at compile
/// time and the registry asks it for its rules. There is no list to remember to add it to, which is
/// what the old code got wrong in the other direction -- there, a new family meant editing a file
/// which had already grown past a thousand lines, and putting the block in the wrong place changed
/// the answer for models nobody was thinking about.
///
/// The source is an abstract member, so the compiler asks for it. That is deliberate: a rule
/// without a page behind it is a guess, and a guess which nobody can check ages into a defect.
/// </remarks>
public abstract class ModelFamily
{
private IReadOnlyList<ModelRule>? declaredRules;
/// <summary>
/// Who builds the models of this family.
/// </summary>
public abstract ModelVendor Vendor { get; }
/// <summary>
/// Where the statements below were read, and when.
/// </summary>
public abstract ModelSource Source { get; }
/// <summary>
/// What this family is called, which is what its rules name as their origin.
/// </summary>
public string Name => this.GetType().Name;
/// <summary>
/// The rules this family states, worked out once.
/// </summary>
public IReadOnlyList<ModelRule> Rules => this.declaredRules ??= this.BuildRules();
/// <summary>
/// Adjusts a profile in a way no pattern can express.
/// </summary>
/// <remarks>
/// The way out for the handful of families whose capabilities are computed from the name rather
/// than looked up: Mistral encodes a release date as four digits and gains abilities from a
/// certain date onwards, and Z AI marks its vision models by putting a "v" behind the version
/// number. Writing one rule per possible date is not a rule set, it is a table of everything.
///
/// Everything which can be said with a pattern belongs in a pattern, where the specificity can
/// see it. This runs afterwards, on the family whose rule won.
/// </remarks>
/// <param name="id">The model name.</param>
/// <param name="selected">What the rules made of it.</param>
/// <returns>The profile, adjusted.</returns>
public virtual ModelProfile Refine(in ModelId id, in ModelProfile selected) => selected;
/// <summary>
/// States the rules of this family.
/// </summary>
/// <param name="builder">What to state them with.</param>
protected abstract void Declare(ModelFamilyBuilder builder);
private IReadOnlyList<ModelRule> BuildRules()
{
var builder = new ModelFamilyBuilder(this.Name);
this.Declare(builder);
return builder.Build();
}
}

View File

@ -0,0 +1,61 @@
using AIStudio.Models.Matching;
namespace AIStudio.Models;
/// <summary>
/// Collects the rules of one family as they are stated.
/// </summary>
/// <remarks>
/// The order rules are stated in changes nothing about which one wins -- that is what the computed
/// specificity is for. It matters in one place only: a variant which inherits takes what the rule
/// before it stated, so that a family can say what its models have in common once and then say
/// only what makes each variant different.
/// </remarks>
/// <param name="origin">What the rules name as their origin, which is the family's name.</param>
public sealed class ModelFamilyBuilder(string origin)
{
private readonly List<ModelRuleBuilder> stated = [];
/// <summary>
/// States a rule which chooses the model.
/// </summary>
/// <param name="text">The name, or the part of it, this rule answers for. In normalized form.</param>
/// <returns>The rule, to go on stating.</returns>
public ModelRuleBuilder Rule(string text) => this.Add(text, ModelRuleKind.SELECTOR);
/// <summary>
/// States a rule which adjusts whatever chose the model.
/// </summary>
/// <param name="text">The name, or the part of it, this rule answers for. In normalized form.</param>
/// <returns>The rule, to go on stating.</returns>
public ModelRuleBuilder Modifier(string text) => this.Add(text, ModelRuleKind.MODIFIER);
/// <summary>
/// Turns everything stated into rules.
/// </summary>
/// <returns>The rules, in the order they were stated.</returns>
internal IReadOnlyList<ModelRule> Build()
{
var built = new List<ModelRule>(this.stated.Count);
var byPatternText = new Dictionary<string, ModelProfileChange>(StringComparer.Ordinal);
ModelProfileChange? previous = null;
foreach (var statement in this.stated)
{
var rule = statement.Build(statement.InheritanceBasis(byPatternText, previous));
built.Add(rule);
byPatternText[rule.Pattern.Text] = rule.Change;
previous = rule.Change;
}
return built;
}
private ModelRuleBuilder Add(string text, ModelRuleKind kind)
{
var statement = new ModelRuleBuilder(text, kind, origin);
this.stated.Add(statement);
return statement;
}
}

View File

@ -0,0 +1,308 @@
using AIStudio.Models.Matching;
using AIStudio.Provider;
namespace AIStudio.Models;
/// <summary>
/// One rule, while it is being stated.
/// </summary>
/// <remarks>
/// Everything left unsaid stays unsaid: a rule which says nothing about the context window does not
/// claim that nobody knows it, it simply makes no statement, and whatever else does gets to keep
/// its answer. That is what lets a variant state one sentence instead of repeating its family.
/// </remarks>
/// <param name="patternText">The name, or the part of it, this rule answers for. In normalized form.</param>
/// <param name="ruleKind">Whether the rule chooses the model or adjusts the choice.</param>
/// <param name="origin">What the rule names as its origin, which is the family's name.</param>
public sealed class ModelRuleBuilder(string patternText, ModelRuleKind ruleKind, string origin)
{
private readonly List<string> alsoContains = [];
private readonly List<string> notContains = [];
private MatchKind matchKind = MatchKind.SEGMENT;
private LLMProviders? onlyOn;
private ModelVendor? onlyFrom;
private int explicitRank;
private bool inheritsFromPrevious;
private string? inheritsFromText;
private Capability adds;
private Capability removes;
private ReasoningSupport? reasoning;
private ModelKind? modelKind;
private ContextWindow? context;
private TokenizerRef? tokenizer;
private ImageLimits? images;
/// <summary>
/// The text is the whole model name.
/// </summary>
/// <returns>The rule, to go on stating.</returns>
public ModelRuleBuilder AsExact() => this.MatchingAs(MatchKind.EXACT);
/// <summary>
/// The name begins with the text, and a name part ends there.
/// </summary>
/// <returns>The rule, to go on stating.</returns>
public ModelRuleBuilder AsPrefix() => this.MatchingAs(MatchKind.PREFIX);
/// <summary>
/// The text appears in the name as one or more whole name parts. This is the default.
/// </summary>
/// <returns>The rule, to go on stating.</returns>
public ModelRuleBuilder AsSegment() => this.MatchingAs(MatchKind.SEGMENT);
/// <summary>
/// The text appears anywhere in the name, boundaries or not.
/// </summary>
/// <remarks>
/// The last resort, for the names where a vendor glues things together. It claims the least and
/// therefore loses against every other kind.
/// </remarks>
/// <returns>The rule, to go on stating.</returns>
public ModelRuleBuilder AsSubstring() => this.MatchingAs(MatchKind.SUBSTRING);
/// <summary>
/// Further name parts the model's name has to carry.
/// </summary>
/// <param name="nameParts">The name parts, each in normalized form.</param>
/// <returns>The rule, to go on stating.</returns>
public ModelRuleBuilder AlsoContains(params string[] nameParts)
{
this.alsoContains.AddRange(nameParts);
return this;
}
/// <summary>
/// Name parts whose presence rules this rule out.
/// </summary>
/// <param name="nameParts">The name parts, each in normalized form.</param>
/// <returns>The rule, to go on stating.</returns>
public ModelRuleBuilder NotContains(params string[] nameParts)
{
this.notContains.AddRange(nameParts);
return this;
}
/// <summary>
/// Restricts this rule to one provider.
/// </summary>
/// <param name="provider">The provider serving the model.</param>
/// <returns>The rule, to go on stating.</returns>
public ModelRuleBuilder OnlyOn(LLMProviders provider)
{
this.onlyOn = provider;
return this;
}
/// <summary>
/// Restricts this rule to models of one vendor.
/// </summary>
/// <param name="vendor">Who built the model.</param>
/// <returns>The rule, to go on stating.</returns>
public ModelRuleBuilder OnlyFrom(ModelVendor vendor)
{
this.onlyFrom = vendor;
return this;
}
/// <summary>
/// What the model can do.
/// </summary>
/// <param name="capabilities">The capabilities, combined with the or operator.</param>
/// <returns>The rule, to go on stating.</returns>
public ModelRuleBuilder Capabilities(Capability capabilities)
{
this.adds |= capabilities;
return this;
}
/// <summary>
/// Which APIs the model answers through.
/// </summary>
/// <remarks>
/// The same thing as stating a capability, said separately because it reads as a different kind
/// of sentence: what a model is able to do, and how one talks to it.
/// </remarks>
/// <param name="apis">The API capabilities, combined with the or operator.</param>
/// <returns>The rule, to go on stating.</returns>
public ModelRuleBuilder Apis(Capability apis)
{
this.adds |= apis;
return this;
}
/// <summary>
/// What the model cannot do, applied after everything it can.
/// </summary>
/// <param name="capabilities">The capabilities, combined with the or operator.</param>
/// <returns>The rule, to go on stating.</returns>
public ModelRuleBuilder Removes(Capability capabilities)
{
this.removes |= capabilities;
return this;
}
/// <summary>
/// How the model reasons.
/// </summary>
/// <param name="support">The way it reasons.</param>
/// <returns>The rule, to go on stating.</returns>
public ModelRuleBuilder Reasoning(ReasoningSupport support)
{
this.reasoning = support;
return this;
}
/// <summary>
/// What the model is made for, when it is not a chat model.
/// </summary>
/// <param name="kind">The kind of model.</param>
/// <returns>The rule, to go on stating.</returns>
public ModelRuleBuilder Kind(ModelKind kind)
{
this.modelKind = kind;
return this;
}
/// <summary>
/// How much the model reads and writes in one conversation.
/// </summary>
/// <param name="defaultTokens">What it does as it ships.</param>
/// <param name="raisableTo">What an operator can raise it to, where that is documented.</param>
/// <returns>The rule, to go on stating.</returns>
public ModelRuleBuilder ContextWindow(int defaultTokens, int? raisableTo = null)
{
this.context = Models.ContextWindow.Of(defaultTokens, raisableTo);
return this;
}
/// <summary>
/// Which tokenizer counts this model's tokens.
/// </summary>
/// <param name="kind">What sort of tokenizer it is.</param>
/// <param name="id">Its name, in whatever spelling that sort uses.</param>
/// <returns>The rule, to go on stating.</returns>
public ModelRuleBuilder Tokenizer(TokenizerKind kind, string id)
{
this.tokenizer = new TokenizerRef(kind, id);
return this;
}
/// <summary>
/// How many images the model accepts.
/// </summary>
/// <param name="maxPerMessage">How many fit into one message, where that is documented.</param>
/// <param name="maxPerRequest">How many fit into one request, where that is documented.</param>
/// <returns>The rule, to go on stating.</returns>
public ModelRuleBuilder Images(int? maxPerMessage = null, int? maxPerRequest = null)
{
this.images = new ImageLimits(maxPerMessage, maxPerRequest);
return this;
}
/// <summary>
/// Takes everything the rule stated before this one and goes on from there.
/// </summary>
/// <returns>The rule, to go on stating.</returns>
public ModelRuleBuilder Inherits()
{
this.inheritsFromPrevious = true;
return this;
}
/// <summary>
/// Takes everything one particular rule of this family stated and goes on from there.
/// </summary>
/// <remarks>
/// Worth preferring over the plain form in a family with more than one generation: naming the
/// rule survives somebody reordering the file, while "the one before" does not.
/// </remarks>
/// <param name="patternText">The text of the rule to inherit from.</param>
/// <returns>The rule, to go on stating.</returns>
public ModelRuleBuilder InheritsFrom(string patternText)
{
this.inheritsFromText = patternText;
return this;
}
/// <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.
/// </remarks>
/// <param name="rank">Positive to move the rule ahead, negative to push it back.</param>
/// <param name="reason">
/// What the computation gets wrong here. It is not kept: it stands in the source so that the
/// next reader finds an explanation next to the rank instead of a number nobody can account for.
/// </param>
/// <returns>The rule, to go on stating.</returns>
public ModelRuleBuilder Rank(int rank, string reason)
{
_ = reason;
this.explicitRank = rank;
return this;
}
/// <summary>
/// What this rule goes on from, if it goes on from anything.
/// </summary>
/// <param name="byPatternText">What the rules stated so far, by their pattern text.</param>
/// <param name="previous">What the rule stated right before this one, if there was one.</param>
/// <returns>The statement to start from, or null when the rule states everything itself.</returns>
internal ModelProfileChange? InheritanceBasis(IReadOnlyDictionary<string, ModelProfileChange> byPatternText, ModelProfileChange? previous)
{
if (this.inheritsFromText is not null)
return byPatternText.TryGetValue(this.inheritsFromText, out var named)
? named
: throw new InvalidOperationException($"The rule \"{patternText}\" of {origin} inherits from \"{this.inheritsFromText}\", which this family does not state before it.");
if (!this.inheritsFromPrevious)
return null;
return previous ?? throw new InvalidOperationException($"The rule \"{patternText}\" of {origin} inherits, but it is the first rule this family states.");
}
/// <summary>
/// Turns the statement into a rule.
/// </summary>
/// <param name="basis">What to go on from, or null to state everything from nothing.</param>
/// <returns>The rule.</returns>
internal ModelRule Build(ModelProfileChange? basis)
{
var pattern = new MatchPattern
{
Kind = this.matchKind,
Text = patternText,
AlsoContains = this.alsoContains.ToArray(),
NotContains = this.notContains.ToArray(),
OnlyOn = this.onlyOn,
OnlyFrom = this.onlyFrom,
ExplicitRank = this.explicitRank,
};
return new(pattern, ruleKind, this.ChangeOnTopOf(basis), origin);
}
private ModelProfileChange ChangeOnTopOf(ModelProfileChange? basis) => new()
{
//
// What this rule states wins over what it inherited, in both directions: a variant may take
// away what its family has, and it may hand back what its family took away.
//
Adds = ((basis?.Adds ?? Capability.NONE) | this.adds) & ~this.removes,
Removes = ((basis?.Removes ?? Capability.NONE) | this.removes) & ~this.adds,
Reasoning = this.reasoning ?? basis?.Reasoning,
Kind = this.modelKind ?? basis?.Kind,
Context = this.context ?? basis?.Context,
Tokenizer = this.tokenizer ?? basis?.Tokenizer,
Images = this.images ?? basis?.Images,
};
private ModelRuleBuilder MatchingAs(MatchKind kind)
{
this.matchKind = kind;
return this;
}
}

View File

@ -0,0 +1,28 @@
namespace AIStudio.Models;
/// <summary>
/// Where the statements about a model were read, and when somebody last looked.
/// </summary>
/// <remarks>
/// Model cards change without telling anybody. A vendor adds tool calling to a checkpoint, raises a
/// context window, or quietly stops offering an API, and the rule written from the old page keeps
/// answering as if nothing happened. Naming the page and the day it was read is what turns "this is
/// what the rules say" into something a person can check in a minute.
///
/// This is not optional: a family has to state it, and the compiler asks for it. The verification
/// run reports the ones which have gone stale.
/// </remarks>
/// <param name="Url">The page the statements were read from.</param>
/// <param name="CheckedOn">The day somebody last read it.</param>
/// <param name="Note">What that page actually said, in a sentence, so a reader knows what to look for.</param>
public sealed record ModelSource(string Url, DateOnly CheckedOn, string Note)
{
/// <summary>
/// Whether this source names a page and a day.
/// </summary>
/// <remarks>
/// The compiler can insist that a family states a source; it cannot insist that the source says
/// anything. This is what the verification run asks.
/// </remarks>
public bool IsStated => !string.IsNullOrWhiteSpace(this.Url) && this.CheckedOn != default;
}

View File

@ -16,3 +16,4 @@
MWAIS0010 | Usage | Error | CanonicalJsonConfigurationAnalyzer
MWAIS0011 | Usage | Error | CanonicalJsonShapeAnalyzer
MWAIS0012 | Usage | Error | DirectI18NGetTextAnalyzer
MWAIS0013 | Usage | Error | ModelPatternLiteralAnalyzer

View File

@ -14,4 +14,5 @@ public static class Identifier
public const string CANONICAL_JSON_CONFIGURATION_ANALYZER = $"{Tools.ID_PREFIX}0010";
public const string CANONICAL_JSON_SHAPE_ANALYZER = $"{Tools.ID_PREFIX}0011";
public const string DIRECT_I18N_GET_TEXT_ANALYZER = $"{Tools.ID_PREFIX}0012";
public const string MODEL_PATTERN_LITERAL_ANALYZER = $"{Tools.ID_PREFIX}0013";
}

View File

@ -0,0 +1,150 @@
using System.Collections.Immutable;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
namespace SourceCodeRules.UsageAnalyzers;
/// <summary>
/// Reports a model pattern which is not written the way a model name arrives.
/// </summary>
/// <remarks>
/// Model names are brought into one form before any rule looks at them: lowercase, a single hyphen
/// between the parts, dots kept. A pattern carrying a capital letter, an underscore, a space, or a
/// double hyphen therefore matches nothing, ever. Nothing about that looks wrong at runtime -- the
/// family simply never answers, its models fall into the global default, and they look merely
/// unremarkable rather than broken. So it is caught while compiling.
///
/// The normalization below is deliberately a second copy of the one in ModelId, because an analyzer
/// cannot reference the app. The two have to be changed together; a test in the app compares them
/// against the same table of cases.
/// </remarks>
#pragma warning disable RS1038
[DiagnosticAnalyzer(LanguageNames.CSharp)]
#pragma warning restore RS1038
public sealed class ModelPatternLiteralAnalyzer : DiagnosticAnalyzer
{
private const string DIAGNOSTIC_ID = Identifier.MODEL_PATTERN_LITERAL_ANALYZER;
private const string CATEGORY = "Usage";
private const string FAMILY_BUILDER_TYPE = "AIStudio.Models.ModelFamilyBuilder";
private const string RULE_BUILDER_TYPE = "AIStudio.Models.ModelRuleBuilder";
private const string TITLE = "A model pattern has to be written the way a model name arrives";
private const string MESSAGE_FORMAT = "The model pattern \"{0}\" can never match a model: {1}";
private const string DESCRIPTION = "Model names are normalized to lowercase with single hyphens between their parts before any rule is asked. A pattern which is not in that form matches nothing and makes its family silently ineffective.";
private static readonly string[] PATTERN_METHOD_NAMES = ["Rule", "Modifier", "AlsoContains", "NotContains", "InheritsFrom"];
private static readonly DiagnosticDescriptor RULE = new(DIAGNOSTIC_ID, TITLE, MESSAGE_FORMAT, CATEGORY, DiagnosticSeverity.Error, isEnabledByDefault: true, description: DESCRIPTION);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => [RULE];
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
context.RegisterSyntaxNodeAction(AnalyzeInvocation, SyntaxKind.InvocationExpression);
}
private static void AnalyzeInvocation(SyntaxNodeAnalysisContext context)
{
var invocation = (InvocationExpressionSyntax) context.Node;
if (context.SemanticModel.GetSymbolInfo(invocation).Symbol is not IMethodSymbol method)
return;
if (!StatesAPattern(method))
return;
foreach (var argument in invocation.ArgumentList.Arguments)
CheckArgument(context, argument.Expression);
}
private static bool StatesAPattern(IMethodSymbol method)
{
var declaringType = method.ContainingType?.ToDisplayString();
if (declaringType != FAMILY_BUILDER_TYPE && declaringType != RULE_BUILDER_TYPE)
return false;
foreach (var name in PATTERN_METHOD_NAMES)
if (method.Name == name)
return true;
return false;
}
private static void CheckArgument(SyntaxNodeAnalysisContext context, ExpressionSyntax expression)
{
//
// Asking for the constant value rather than for a literal, so that a pattern written once as
// a constant and used in several rules is checked as well.
//
var constant = context.SemanticModel.GetConstantValue(expression);
if (!constant.HasValue || constant.Value is not string text)
return;
var normalized = Normalize(text);
if (normalized == text)
return;
var advice = normalized.Length == 0
? "nothing of it survives the way names are normalized"
: $"write it as \"{normalized}\"";
context.ReportDiagnostic(Diagnostic.Create(RULE, expression.GetLocation(), text, advice));
}
/// <summary>
/// Brings a text into the form a model name arrives in.
/// </summary>
/// <remarks>
/// The same rule as ModelId.Normalize in the app, written again here because an analyzer cannot
/// reference the code it analyzes. Keep the two in step.
/// </remarks>
/// <param name="text">The text to normalize.</param>
/// <returns>The text in lowercase, with every separator written as a single hyphen.</returns>
private static string Normalize(string text)
{
var normalized = new StringBuilder(text.Length);
foreach (var character in text)
{
if (IsKept(character))
{
normalized.Append(char.ToLowerInvariant(character));
continue;
}
// Anything else separates two parts of the name. A leading separator, and a repeated
// one, say nothing:
if (normalized.Length == 0 || normalized[normalized.Length - 1] == '-')
continue;
normalized.Append('-');
}
// A trailing separator carries no meaning either:
if (normalized.Length > 0 && normalized[normalized.Length - 1] == '-')
normalized.Length--;
return normalized.ToString();
}
/// <summary>
/// Whether a character survives normalization as itself.
/// </summary>
/// <remarks>
/// Letters and digits, and the dot: it carries the version boundary, so llama3 and llama3.1 stay
/// two different names.
/// </remarks>
/// <param name="character">The character to look at.</param>
/// <returns>True, when it is kept.</returns>
private static bool IsKept(char character) =>
character is >= 'a' and <= 'z' ||
character is >= 'A' and <= 'Z' ||
character is >= '0' and <= '9' ||
character is '.';
}

View File

@ -6,3 +6,4 @@
---------|------------------|----------|--------------------------
MBI001 | SourceGeneration | Info | MappingRegistryGenerator
MBI002 | SourceGeneration | Warning | MappingRegistryGenerator
MDR001 | SourceGeneration | Warning | ModelRegistryGenerator

View File

@ -0,0 +1,219 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
namespace SourceGeneratedMappings;
/// <summary>
/// Collects every model family and every model host of the compilation into one list.
/// </summary>
/// <remarks>
/// Adding a family has to be one action, not two. A registry which somebody has to remember to add
/// to is a registry which will be incomplete, and the failure it produces is the quietest one there
/// is: a family which simply never answers, so its models fall into the global default and look
/// merely unremarkable.
///
/// Searching for the types at startup through reflection would do the same job, but this app
/// publishes trimmed and uses reflection nowhere else. So the search happens while compiling, and
/// what ships is a plain array.
/// </remarks>
[Generator]
#pragma warning disable RS1036
public sealed class ModelRegistryGenerator : IIncrementalGenerator
#pragma warning restore RS1036
{
private const string GENERATED_NAMESPACE = "AIStudio.Models.Registry";
private const string GENERATED_TYPE_NAME = "ModelRegistrations";
private const string FAMILY_BASE_TYPE = "AIStudio.Models.ModelFamily";
private const string HOST_INTERFACE = "AIStudio.Models.Hosting.IModelHost";
private static readonly DiagnosticDescriptor CANNOT_BE_REGISTERED = new(
id: "MDR001",
title: "A model family or host cannot be registered",
messageFormat: "'{0}' is a model family or host, but the generated registry cannot create it: {1}. It will answer for no model at all.",
category: "SourceGeneration",
DiagnosticSeverity.Warning,
isEnabledByDefault: true,
description: "The generated registry creates every family and host with its parameterless constructor. A type it cannot create is left out, which makes it silently ineffective.");
public void Initialize(IncrementalGeneratorInitializationContext context)
{
var candidates = context.SyntaxProvider
.CreateSyntaxProvider(static (node, _) => CouldBeARegistration(node), static (syntax, _) => Inspect(syntax))
.Where(static candidate => candidate.FullName is not null)
.Collect();
context.RegisterSourceOutput(candidates, Generate);
}
/// <summary>
/// Whether a syntax node is worth asking the semantic model about.
/// </summary>
/// <remarks>
/// Runs on every node of every keystroke, so it only looks at the syntax: a class with a base
/// list which is neither abstract nor static. Everything else is decided once a symbol exists.
/// </remarks>
/// <param name="node">The node to look at.</param>
/// <returns>True, when the node could be a family or a host.</returns>
private static bool CouldBeARegistration(SyntaxNode node) =>
node is ClassDeclarationSyntax declaration &&
declaration.BaseList is { Types.Count: > 0 } &&
!declaration.Modifiers.Any(SyntaxKind.AbstractKeyword) &&
!declaration.Modifiers.Any(SyntaxKind.StaticKeyword);
private static Candidate Inspect(GeneratorSyntaxContext context)
{
var declaration = (ClassDeclarationSyntax) context.Node;
if (context.SemanticModel.GetDeclaredSymbol(declaration) is not { } symbol)
return default;
var isFamily = DerivesFrom(symbol, FAMILY_BASE_TYPE);
var isHost = symbol.AllInterfaces.Any(candidate => candidate.ToDisplayString() == HOST_INTERFACE);
if (!isFamily && !isHost)
return default;
return new Candidate(symbol.ToDisplayString(), isFamily, isHost, WhyItCannotBeCreated(symbol), declaration.Identifier.GetLocation());
}
private static bool DerivesFrom(INamedTypeSymbol symbol, string baseTypeName)
{
for (var current = symbol.BaseType; current is not null; current = current.BaseType)
if (current.ToDisplayString() == baseTypeName)
return true;
return false;
}
/// <summary>
/// Why the generated registry could not create this type, or null when it can.
/// </summary>
/// <param name="symbol">The type to look at.</param>
/// <returns>A phrase which completes the diagnostic message, or null.</returns>
private static string? WhyItCannotBeCreated(INamedTypeSymbol symbol)
{
if (symbol.IsAbstract)
return "it is abstract";
if (symbol.IsGenericType)
return "it is generic";
if (symbol.ContainingType is not null)
return "it is nested inside another type";
if (symbol.DeclaredAccessibility is Accessibility.Private or Accessibility.Protected or Accessibility.ProtectedAndInternal)
return "the registry cannot reach it from outside its own type";
var hasParameterlessConstructor = symbol.InstanceConstructors.Any(constructor =>
constructor.Parameters.Length == 0 &&
constructor.DeclaredAccessibility is Accessibility.Public or Accessibility.Internal);
return hasParameterlessConstructor ? null : "it has no parameterless constructor the registry can reach";
}
private static void Generate(SourceProductionContext context, ImmutableArray<Candidate> candidates)
{
var families = new List<string>();
var hosts = new List<string>();
foreach (var candidate in candidates)
{
if (candidate.FullName is null)
continue;
if (candidate.Problem is not null)
{
context.ReportDiagnostic(Diagnostic.Create(CANNOT_BE_REGISTERED, candidate.Location ?? Location.None, candidate.FullName, candidate.Problem));
continue;
}
if (candidate.IsFamily)
families.Add(candidate.FullName);
if (candidate.IsHost)
hosts.Add(candidate.FullName);
}
//
// Sorted by name and without repeats, so that the same sources produce the same file: a
// partial class arrives here once per part, and the order syntax nodes are visited in is
// not something to build a shipped artefact on.
//
var source = RenderSource(Ordered(families), Ordered(hosts));
context.AddSource("ModelFamilies.g.cs", SourceText.From(source, Encoding.UTF8));
}
private static IReadOnlyList<string> Ordered(IEnumerable<string> typeNames) => typeNames.Distinct(StringComparer.Ordinal).OrderBy(static name => name, StringComparer.Ordinal).ToList();
private static string RenderSource(IReadOnlyList<string> families, IReadOnlyList<string> hosts)
{
var builder = new StringBuilder();
builder.AppendLine("// <auto-generated />");
builder.AppendLine("#nullable enable");
builder.AppendLine();
builder.Append("namespace ").Append(GENERATED_NAMESPACE).AppendLine(";");
builder.AppendLine();
builder.AppendLine("/// <summary>");
builder.AppendLine("/// Every model family and every model host this assembly declares.");
builder.AppendLine("/// </summary>");
builder.Append("public static class ").AppendLine(GENERATED_TYPE_NAME);
builder.AppendLine("{");
AppendFactory(builder, "CreateFamilies", FAMILY_BASE_TYPE, families);
builder.AppendLine();
AppendFactory(builder, "CreateHosts", HOST_INTERFACE, hosts);
builder.AppendLine("}");
return builder.ToString();
}
private static void AppendFactory(StringBuilder builder, string methodName, string typeName, IReadOnlyList<string> typeNames)
{
builder.Append(" public static global::System.Collections.Generic.IReadOnlyList<global::").Append(typeName).Append("> ").Append(methodName).AppendLine("() =>");
builder.Append(" new global::").Append(typeName).AppendLine("[]");
builder.AppendLine(" {");
foreach (var name in typeNames)
builder.Append(" new global::").Append(name).AppendLine("(),");
builder.AppendLine(" };");
}
/// <summary>
/// What the syntax pass found out about one type.
/// </summary>
/// <remarks>
/// A struct with value equality, because this travels through the incremental pipeline: two
/// runs finding the same types have to compare as equal, or nothing downstream is ever cached.
/// </remarks>
private readonly struct Candidate(string? fullName, bool isFamily, bool isHost, string? problem, Location? location) : IEquatable<Candidate>
{
public string? FullName { get; } = fullName;
public bool IsFamily { get; } = isFamily;
public bool IsHost { get; } = isHost;
public string? Problem { get; } = problem;
public Location? Location { get; } = location;
public bool Equals(Candidate other) =>
this.FullName == other.FullName &&
this.IsFamily == other.IsFamily &&
this.IsHost == other.IsHost &&
this.Problem == other.Problem &&
Equals(this.Location, other.Location);
public override bool Equals(object? obj) => obj is Candidate other && this.Equals(other);
public override int GetHashCode() => this.FullName?.GetHashCode() ?? 0;
}
}

View File

@ -0,0 +1,88 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
namespace AIStudio.Tests.Models.Generation;
/// <summary>
/// Compiles a snippet in memory so that a generator or an analyzer can be asked what it makes of it.
/// </summary>
/// <remarks>
/// Both of them are code which runs while the app is being built, and both fail quietly when they
/// are wrong: a generator which finds nothing produces an empty registry, and an analyzer which
/// recognizes nothing reports nothing. Neither shows up as a broken build, so neither can be
/// checked by building the app. It has to be done here, against source written for the purpose.
/// </remarks>
public static class CompilationHarness
{
/// <summary>
/// Everything the test process itself was loaded with, which includes the app assembly.
/// </summary>
/// <remarks>
/// Gathered once. Reading a couple of hundred assemblies off disk per test case would make
/// these tests slow enough that somebody stops running them.
/// </remarks>
private static readonly Lazy<MetadataReference[]> REFERENCES = new(GatherReferences);
/// <summary>
/// Compiles a snippet against the same assemblies the app is built against.
/// </summary>
/// <param name="source">The C# source to compile.</param>
/// <returns>The compilation.</returns>
public static CSharpCompilation Compile(string source)
{
var tree = CSharpSyntaxTree.ParseText(source, new CSharpParseOptions(LanguageVersion.Latest));
var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, nullableContextOptions: NullableContextOptions.Enable);
return CSharpCompilation.Create("SnippetUnderTest", [tree], REFERENCES.Value, options);
}
/// <summary>
/// Compiles a snippet and reports what it does not even parse or bind.
/// </summary>
/// <remarks>
/// Worth asking before believing a generator found nothing: a snippet with a typo in it also
/// produces an empty result, and the two look exactly alike from the outside.
/// </remarks>
/// <param name="compilation">The compilation to check.</param>
/// <returns>The errors, each on its own line, or an empty string.</returns>
public static string ErrorsOf(Compilation compilation)
{
var errors = compilation.GetDiagnostics()
.Where(diagnostic => diagnostic.Severity is DiagnosticSeverity.Error)
.Select(diagnostic => diagnostic.ToString());
return string.Join(Environment.NewLine, errors);
}
/// <summary>
/// Runs one analyzer over a snippet.
/// </summary>
/// <param name="source">The C# source to analyze.</param>
/// <param name="analyzer">The analyzer to run.</param>
/// <returns>What the analyzer reported.</returns>
public static async Task<IReadOnlyList<Diagnostic>> AnalyzeAsync(string source, DiagnosticAnalyzer analyzer)
{
var compilation = Compile(source);
Assert.That(ErrorsOf(compilation), Is.Empty, "The snippet has to compile, or the analyzer is being asked about code which does not exist.");
var reported = await compilation.WithAnalyzers([analyzer]).GetAnalyzerDiagnosticsAsync();
return reported;
}
private static MetadataReference[] GatherReferences()
{
//
// The set the runtime resolves types from, which is exactly what this test assembly was
// built against: the framework, the NuGet packages, and the app itself.
//
if (AppContext.GetData("TRUSTED_PLATFORM_ASSEMBLIES") is not string assemblyPaths)
throw new InvalidOperationException("The test host did not say which assemblies it trusts, so no compilation can be built against them.");
return assemblyPaths
.Split(Path.PathSeparator)
.Where(path => path.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) && File.Exists(path))
.Select(path => (MetadataReference) MetadataReference.CreateFromFile(path))
.ToArray();
}
}

View File

@ -0,0 +1,132 @@
using AIStudio.Models.Matching;
using Microsoft.CodeAnalysis;
using SourceCodeRules.UsageAnalyzers;
namespace AIStudio.Tests.Models.Generation;
/// <summary>
/// Checks that a pattern which can never match is refused while compiling.
/// </summary>
/// <remarks>
/// A pattern carrying a capital letter, an underscore, or a space matches no model name, because
/// names are normalized before any rule sees them. At runtime that looks like nothing: the family
/// answers for nobody and its models quietly take the global default. MWAIS0013 turns it into a
/// build error, and these tests are what says that it actually recognizes the calls it is meant to.
/// </remarks>
[TestFixture]
public sealed class ModelPatternLiteralAnalyzerTests
{
/// <summary>
/// Patterns and whether the app considers them normalized, checked from both ends.
/// </summary>
/// <remarks>
/// The analyzer carries its own copy of the normalization, because it cannot reference the app.
/// This is the table which keeps the two honest: whatever MatchPattern.IsNormalized says at
/// runtime, the compile time rule has to say the same.
/// </remarks>
private static readonly string[] PATTERNS_TO_AGREE_ON =
[
"gpt-5.1", "qwen3.8-27b", "deepseek-r1", "yi", "01",
"GPT-5.1", "gpt_5", "gpt 5", "gpt--5", "-gpt-5", "gpt-5-", "Qwen3.8:27B", "___",
];
[Test]
public async Task APatternWrittenTheWayNamesArriveIsAccepted()
{
var reported = await AnalyzeAsync("""builder.Rule("gpt-5.1").AsPrefix();""");
Assert.That(reported, Is.Empty);
}
[TestCase("""builder.Rule("GPT-5.1");""", "gpt-5.1")]
[TestCase("""builder.Rule("gpt_5");""", "gpt-5")]
[TestCase("""builder.Rule("gpt 5");""", "gpt-5")]
[TestCase("""builder.Modifier("BASE");""", "base")]
[TestCase("""builder.Rule("gpt-5").AlsoContains("Codex");""", "codex")]
[TestCase("""builder.Rule("gpt-5").NotContains("Chat");""", "chat")]
[TestCase("""builder.Rule("gpt-5"); builder.Rule("gpt-5-mini").InheritsFrom("GPT-5");""", "gpt-5")]
public async Task APatternWhichCanNeverMatchIsRefusedAndTheRightSpellingIsNamed(string statements, string expectedSpelling)
{
var reported = await AnalyzeAsync(statements);
Assert.Multiple(() =>
{
Assert.That(reported, Has.Count.EqualTo(1));
Assert.That(reported.FirstOrDefault()?.Id, Is.EqualTo("MWAIS0013"));
Assert.That(reported.FirstOrDefault()?.GetMessage(), Does.Contain($"write it as \"{expectedSpelling}\""));
});
}
[Test]
public async Task APatternOfWhichNothingSurvivesSaysThatInsteadOfSuggestingAnEmptyOne()
{
var reported = await AnalyzeAsync("""builder.Rule("___");""");
Assert.Multiple(() =>
{
Assert.That(reported, Has.Count.EqualTo(1));
Assert.That(reported.FirstOrDefault()?.GetMessage(), Does.Contain("nothing of it survives"));
});
}
[Test]
public async Task APatternWrittenOnceAsAConstantIsCheckedToo()
{
var reported = await AnalyzeAsync("""const string THE_PATTERN = "GPT-5"; builder.Rule(THE_PATTERN);""");
Assert.That(reported, Has.Count.EqualTo(1));
}
[Test]
public async Task TextWhichIsNotAPatternIsLeftAlone()
{
//
// A tokenizer is named the way its vendor names it, and o200k_base carries an underscore
// because OpenAI writes it that way. An analyzer which cannot tell the two kinds of string
// apart would make it impossible to state the truth.
//
var reported = await AnalyzeAsync("""builder.Rule("gpt-5.1").Tokenizer(TokenizerKind.TIKTOKEN, "o200k_base");""");
Assert.That(reported, Is.Empty);
}
[Test]
public async Task TheCompileTimeRuleAndTheRuntimeCheckNeverDisagree()
{
foreach (var pattern in PATTERNS_TO_AGREE_ON)
{
var reported = await AnalyzeAsync($"""builder.Rule("{pattern}");""");
var acceptedWhileCompiling = reported.Count is 0;
Assert.That(acceptedWhileCompiling, Is.EqualTo(MatchPattern.IsNormalized(pattern)), $"The two normalizations disagree about \"{pattern}\".");
}
}
private static async Task<IReadOnlyList<Diagnostic>> AnalyzeAsync(string statements)
{
var source =
$$"""
using System;
using AIStudio.Models;
namespace Sample;
public sealed class SampleFamily : ModelFamily
{
public override ModelVendor Vendor => ModelVendor.OPEN_AI;
public override ModelSource Source => new("https://example.invalid", new DateOnly(2026, 9, 11), "a note");
protected override void Declare(ModelFamilyBuilder builder)
{
{{statements}}
}
}
""";
return await CompilationHarness.AnalyzeAsync(source, new ModelPatternLiteralAnalyzer());
}
}

View File

@ -0,0 +1,191 @@
using AIStudio.Models.Registry;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using SourceGeneratedMappings;
namespace AIStudio.Tests.Models.Generation;
/// <summary>
/// Checks that adding a family is one action, and that nothing else is needed to make it count.
/// </summary>
/// <remarks>
/// The whole point of generating the registry is that nobody has to remember a list. If the
/// generator misses a family, the family answers for nothing, its models fall into the global
/// default, and they look unremarkable rather than broken -- which is the hardest kind of defect to
/// notice. So the generator is asked directly, against source written for the purpose.
/// </remarks>
[TestFixture]
public sealed class ModelRegistryGeneratorTests
{
/// <summary>
/// Two families, one of them two levels down, one host, and an abstract class in between.
/// </summary>
private const string TWO_FAMILIES_AND_A_HOST =
"""
using System;
using AIStudio.Models;
using AIStudio.Models.Hosting;
using AIStudio.Models.Matching;
using AIStudio.Provider;
namespace Sample;
public abstract class HalfAFamily : ModelFamily
{
public override ModelVendor Vendor => ModelVendor.OPEN_AI;
public override ModelSource Source => new("https://example.invalid/half", new DateOnly(2026, 9, 11), "a note");
}
public sealed class SecondFamily : HalfAFamily
{
protected override void Declare(ModelFamilyBuilder builder) => builder.Rule("second");
}
public sealed class FirstFamily : ModelFamily
{
public override ModelVendor Vendor => ModelVendor.ANTHROPIC;
public override ModelSource Source => new("https://example.invalid/first", new DateOnly(2026, 9, 11), "a note");
protected override void Declare(ModelFamilyBuilder builder) => builder.Rule("first");
}
public sealed class SampleHost : IModelHost
{
public LLMProviders Provider => LLMProviders.NONE;
public ModelSource Source => new("https://example.invalid/host", new DateOnly(2026, 9, 11), "a note");
public bool TryUnwrap(in ModelId id, out ModelId inner, out ModelVendor? declaredVendor)
{
inner = id;
declaredVendor = null;
return false;
}
public ModelProfile ApplyTransport(in ModelProfile profile) => profile;
}
""";
/// <summary>
/// A family the registry cannot create, because it asks for something to be handed in.
/// </summary>
private const string A_FAMILY_NEEDING_AN_ARGUMENT =
"""
using System;
using AIStudio.Models;
namespace Sample;
public sealed class DemandingFamily(int somethingItNeeds) : ModelFamily
{
public override ModelVendor Vendor => ModelVendor.OPEN_AI;
public override ModelSource Source => new("https://example.invalid/demanding", new DateOnly(2026, 9, 11), $"needs {somethingItNeeds}");
protected override void Declare(ModelFamilyBuilder builder) => builder.Rule("demanding");
}
""";
[Test]
public void EveryFamilyIsFoundWithoutBeingAddedToAnything()
{
var generated = Generate(TWO_FAMILIES_AND_A_HOST, out _, out _);
Assert.Multiple(() =>
{
Assert.That(generated, Does.Contain("new global::Sample.FirstFamily()"));
Assert.That(generated, Does.Contain("new global::Sample.SecondFamily()"), "A family which inherits through another class is still a family.");
Assert.That(generated, Does.Contain("new global::Sample.SampleHost()"));
});
}
[Test]
public void AClassWhichCannotBeAFamilyOnItsOwnIsNotRegistered()
{
var generated = Generate(TWO_FAMILIES_AND_A_HOST, out _, out _);
Assert.That(generated, Does.Not.Contain("HalfAFamily"));
}
[Test]
public void TheRegistryIsWrittenInTheSameOrderEveryTime()
{
//
// The order syntax nodes are visited in is not something a shipped file may depend on: the
// same sources have to produce the same bytes, or a rebuild shows up as a change.
//
var generated = Generate(TWO_FAMILIES_AND_A_HOST, out _, out _);
Assert.That(generated.IndexOf("Sample.FirstFamily", StringComparison.Ordinal), Is.LessThan(generated.IndexOf("Sample.SecondFamily", StringComparison.Ordinal)));
}
[Test]
public void WhatIsGeneratedCompiles()
{
Generate(TWO_FAMILIES_AND_A_HOST, out var updated, out _);
Assert.That(CompilationHarness.ErrorsOf(updated), Is.Empty);
}
[Test]
public void AnAssemblyWithoutAnyFamiliesStillGetsARegistry()
{
//
// Otherwise the registry would fail to compile in exactly the situation where somebody is
// about to write their first family.
//
var generated = Generate("namespace Sample;\n\npublic sealed class NothingToDoWithModels;", out var updated, out _);
Assert.Multiple(() =>
{
Assert.That(generated, Does.Contain("public static class ModelRegistrations"));
Assert.That(CompilationHarness.ErrorsOf(updated), Is.Empty);
});
}
[Test]
public void AFamilyTheRegistryCannotCreateIsReportedRatherThanSkippedQuietly()
{
var generated = Generate(A_FAMILY_NEEDING_AN_ARGUMENT, out _, out var diagnostics);
var reported = diagnostics.Where(diagnostic => diagnostic.Id is "MDR001").ToList();
Assert.Multiple(() =>
{
Assert.That(reported, Has.Count.EqualTo(1));
Assert.That(reported.FirstOrDefault()?.GetMessage(), Does.Contain("DemandingFamily"));
Assert.That(generated, Does.Not.Contain("DemandingFamily"));
});
}
[Test]
public void TheAppItselfHasARegistryTheGeneratorWrote()
{
//
// The tests above run the generator by hand. This one asks whether it also ran while the app
// was built, which is a different question and the one that actually matters.
//
Assert.Multiple(() =>
{
Assert.That(ModelRegistrations.CreateFamilies(), Is.Not.Null);
Assert.That(ModelRegistrations.CreateHosts(), Is.Not.Null);
});
}
private static string Generate(string source, out Compilation updated, out IReadOnlyList<Diagnostic> diagnostics)
{
var compilation = CompilationHarness.Compile(source);
Assert.That(CompilationHarness.ErrorsOf(compilation), Is.Empty, "The snippet has to compile, or the generator is being asked about code which does not exist.");
var driver = CSharpGeneratorDriver.Create(new ModelRegistryGenerator().AsSourceGenerator());
var afterwards = driver.RunGeneratorsAndUpdateCompilation(compilation, out updated, out var reported);
diagnostics = reported;
return afterwards.GetRunResult().Results.Single().GeneratedSources.Single().SourceText.ToString();
}
}

View File

@ -0,0 +1,232 @@
using AIStudio.Models;
using AIStudio.Models.Matching;
using AIStudio.Provider;
namespace AIStudio.Tests.Models;
/// <summary>
/// Checks how a family states its rules, and what a variant inherits from the family it belongs to.
/// </summary>
/// <remarks>
/// Inheritance here happens while the rules are being built, not while a name is being answered. A
/// variant takes what its family stated and goes on from there, and what comes out is one complete
/// rule -- so at runtime there is still exactly one selector winning, and the specificity remains
/// the only thing deciding which.
/// </remarks>
[TestFixture]
public sealed class ModelFamilyTests
{
[Test]
public void AFamilyNamesItselfAsTheOriginOfItsRules()
{
var family = new SampleFamily();
Assert.Multiple(() =>
{
Assert.That(family.Name, Is.EqualTo(nameof(SampleFamily)));
Assert.That(family.Rules.Select(rule => rule.Origin), Is.All.EqualTo(nameof(SampleFamily)));
});
}
[Test]
public void AFamilyStatesItsRulesOnlyOnce()
{
var family = new SampleFamily();
var whenFirstAsked = family.Rules;
var whenAskedAgain = family.Rules;
Assert.That(whenAskedAgain, Is.SameAs(whenFirstAsked));
}
[Test]
public void ARuleIsAboutWholeNamePartsUnlessItSaysOtherwise()
{
var family = new PlainFamily();
Assert.That(family.Rules.Single().Pattern.Kind, Is.EqualTo(MatchKind.SEGMENT));
}
[Test]
public void AVariantKeepsEverythingItsFamilyStatedAndOnlyChangesWhatItSays()
{
var index = ModelFamilyIndex.Build(new SampleFamily().Rules);
var codex = index.Resolve(new ModelId("gpt-5.1-codex-max"), LLMProviders.OPEN_AI, ModelVendor.OPEN_AI);
Assert.Multiple(() =>
{
Assert.That(codex.Has(Capability.WEB_SEARCH), Is.False, "This is the one thing the variant takes away.");
Assert.That(codex.Has(Capability.TEXT_INPUT | Capability.MULTIPLE_IMAGE_INPUT | Capability.FUNCTION_CALLING), Is.True);
Assert.That(codex.Reasoning, Is.EqualTo(ReasoningSupport.OPTIONAL));
Assert.That(codex.Context.DefaultTokens, Is.EqualTo(400_000));
Assert.That(codex.Tokenizer.Id, Is.EqualTo("o200k_base"));
});
}
[Test]
public void WhatAVariantTakesAwayIsNotTakenAwayFromTheFamily()
{
var index = ModelFamilyIndex.Build(new SampleFamily().Rules);
var plain = index.Resolve(new ModelId("gpt-5.1-mini"), LLMProviders.OPEN_AI, ModelVendor.OPEN_AI);
Assert.That(plain.Has(Capability.WEB_SEARCH), Is.True);
}
[Test]
public void AVariantCanHandBackWhatItsFamilyTookAway()
{
var index = ModelFamilyIndex.Build(new FamilyWhichTakesSomethingBack().Rules);
var withTools = index.Resolve(new ModelId("thing-with-tools"), LLMProviders.SELF_HOSTED, ModelVendor.UNKNOWN);
Assert.That(withTools.Has(Capability.FUNCTION_CALLING), Is.True);
}
[Test]
public void AVariantMayNameTheRuleItInheritsFromInsteadOfTakingTheOneBefore()
{
var index = ModelFamilyIndex.Build(new FamilyWithTwoGenerations().Rules);
Assert.Multiple(() =>
{
Assert.That(index.Resolve(new ModelId("thing3-mini"), LLMProviders.SELF_HOSTED, ModelVendor.UNKNOWN).Reasoning, Is.EqualTo(ReasoningSupport.ALWAYS));
Assert.That(index.Resolve(new ModelId("thing4"), LLMProviders.SELF_HOSTED, ModelVendor.UNKNOWN).Reasoning, Is.EqualTo(ReasoningSupport.NONE));
});
}
[Test]
public void AFirstRuleHasNothingToInheritFromAndSaysSo()
{
var family = new FamilyInheritingFromNothing();
var refused = Assert.Throws<InvalidOperationException>(() => _ = family.Rules);
Assert.That(refused?.Message, Does.Contain("first rule"));
}
[Test]
public void InheritingFromARuleWhichWasNeverStatedSaysSo()
{
var family = new FamilyInheritingFromSomethingMissing();
var refused = Assert.Throws<InvalidOperationException>(() => _ = family.Rules);
Assert.That(refused?.Message, Does.Contain("does not state"));
}
[Test]
public void AFamilyWhichAdjustsRatherThanChoosesStatesAModifier()
{
var family = new FamilyWithAModifier();
Assert.That(family.Rules.Single().Kind, Is.EqualTo(ModelRuleKind.MODIFIER));
}
[Test]
public void AFamilyLeavesTheProfileAloneUnlessItSaysItRefinesIt()
{
var family = new PlainFamily();
var profile = new ModelProfile { Capabilities = Capability.TEXT_INPUT };
Assert.That(family.Refine(new ModelId("thing"), profile), Is.EqualTo(profile));
}
[Test]
public void ASourceWithoutAPageOrADayIsNotAStatement()
{
Assert.Multiple(() =>
{
Assert.That(new SampleFamily().Source.IsStated, Is.True);
Assert.That(new ModelSource(string.Empty, new DateOnly(2026, 9, 11), "a note").IsStated, Is.False);
Assert.That(new ModelSource("https://example.invalid", default, "a note").IsStated, Is.False);
});
}
/// <summary>
/// The family from the plan, written the way a real one will be.
/// </summary>
private sealed class SampleFamily : ModelFamily
{
public override ModelVendor Vendor => ModelVendor.OPEN_AI;
public override ModelSource Source => new("https://example.invalid/gpt-5.1", new DateOnly(2026, 9, 11), "Made up for this test, so that no real page is claimed to have been read.");
protected override void Declare(ModelFamilyBuilder builder)
{
builder.Rule("gpt-5.1").AsPrefix()
.Capabilities(Capability.TEXT_INPUT | Capability.MULTIPLE_IMAGE_INPUT | Capability.TEXT_OUTPUT | Capability.FUNCTION_CALLING | Capability.WEB_SEARCH)
.Apis(Capability.RESPONSES_API | Capability.CHAT_COMPLETION_API)
.Reasoning(ReasoningSupport.OPTIONAL)
.ContextWindow(400_000)
.Tokenizer(TokenizerKind.TIKTOKEN, "o200k_base");
builder.Rule("gpt-5.1-codex").AsPrefix().Inherits().Removes(Capability.WEB_SEARCH);
}
}
private sealed class PlainFamily : ModelFamily
{
public override ModelVendor Vendor => ModelVendor.UNKNOWN;
public override ModelSource Source => new("https://example.invalid/plain", new DateOnly(2026, 9, 11), "A family stating one rule and nothing else.");
protected override void Declare(ModelFamilyBuilder builder) => builder.Rule("thing").Capabilities(Capability.TEXT_INPUT);
}
private sealed class FamilyWhichTakesSomethingBack : ModelFamily
{
public override ModelVendor Vendor => ModelVendor.UNKNOWN;
public override ModelSource Source => new("https://example.invalid/back", new DateOnly(2026, 9, 11), "A family whose variant regains what the family lacks.");
protected override void Declare(ModelFamilyBuilder builder)
{
builder.Rule("thing").Capabilities(Capability.TEXT_INPUT).Removes(Capability.FUNCTION_CALLING);
builder.Rule("thing-with-tools").Inherits().Capabilities(Capability.FUNCTION_CALLING);
}
}
private sealed class FamilyWithTwoGenerations : ModelFamily
{
public override ModelVendor Vendor => ModelVendor.UNKNOWN;
public override ModelSource Source => new("https://example.invalid/generations", new DateOnly(2026, 9, 11), "A family with two generations which reason differently.");
protected override void Declare(ModelFamilyBuilder builder)
{
builder.Rule("thing3").AsPrefix().Capabilities(Capability.TEXT_INPUT).Reasoning(ReasoningSupport.ALWAYS);
builder.Rule("thing4").AsPrefix().Capabilities(Capability.TEXT_INPUT).Reasoning(ReasoningSupport.NONE);
// Naming the generation rather than taking whatever stands above, which here is the
// other one:
builder.Rule("thing3-mini").AsPrefix().InheritsFrom("thing3");
}
}
private sealed class FamilyInheritingFromNothing : ModelFamily
{
public override ModelVendor Vendor => ModelVendor.UNKNOWN;
public override ModelSource Source => new("https://example.invalid/nothing", new DateOnly(2026, 9, 11), "A family whose first rule inherits.");
protected override void Declare(ModelFamilyBuilder builder) => builder.Rule("thing").Inherits();
}
private sealed class FamilyInheritingFromSomethingMissing : ModelFamily
{
public override ModelVendor Vendor => ModelVendor.UNKNOWN;
public override ModelSource Source => new("https://example.invalid/missing", new DateOnly(2026, 9, 11), "A family inheriting from a rule it never states.");
protected override void Declare(ModelFamilyBuilder builder)
{
builder.Rule("thing").Capabilities(Capability.TEXT_INPUT);
builder.Rule("thing-mini").InheritsFrom("something-else");
}
}
private sealed class FamilyWithAModifier : ModelFamily
{
public override ModelVendor Vendor => ModelVendor.UNKNOWN;
public override ModelSource Source => new("https://example.invalid/modifier", new DateOnly(2026, 9, 11), "A family stating a modifier.");
protected override void Declare(ModelFamilyBuilder builder) => builder.Modifier("base").Removes(Capability.FUNCTION_CALLING);
}
}

View File

@ -11,6 +11,12 @@
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="10.0.1" />
<!--
Held at the version the two analyzer projects build against, so that a test compiling a
snippet uses the same Roslyn the analyzers themselves were written for. Raise all three
together.
-->
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.10.0" />
<!--
Pinned below 4.6.0 on purpose, verified by bisecting 4.4.0, 4.5.0, 4.5.1, 4.6.0 and 4.6.1
@ -31,6 +37,15 @@
<ItemGroup>
<ProjectReference Include="..\MindWork AI Studio\MindWork AI Studio.csproj" />
<!--
Referenced as plain libraries, not as analyzers: the tests run the generator and the
analyzer themselves, against a snippet they compile in memory. Building them into this
project as analyzers would have them judge the test code instead, which is not what is
being checked here.
-->
<ProjectReference Include="..\SourceCodeRules\SourceCodeRules\SourceCodeRules.csproj" />
<ProjectReference Include="..\SourceGeneratedMappings\SourceGeneratedMappings.csproj" />
</ItemGroup>
<ItemGroup>