-
Notifications
You must be signed in to change notification settings - Fork 29
fix: keep discovered model capabilities out of config #1761
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Aaronontheweb
merged 2 commits into
netclaw-dev:dev
from
Aaronontheweb:fix/1756-dynamic-model-capabilities
Aug 5, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| using System.Text.Json; | ||
| using Netclaw.Cli.Config; | ||
| using Netclaw.Configuration; | ||
| using Netclaw.Daemon.Configuration; | ||
| using Xunit; | ||
| using ModalityOverride = Netclaw.Cli.Config.ValueOverride<Netclaw.Configuration.ModelModality>; | ||
| using ContextWindowOverride = Netclaw.Cli.Config.ValueOverride<int>; | ||
|
|
@@ -14,10 +15,9 @@ namespace Netclaw.Cli.Tests.Config; | |
|
|
||
| /// <summary> | ||
| /// Guards the single persist path shared by `model set`, the init wizard, and the TUI | ||
| /// model manager. The rule under test: modalities are written only when the discovery | ||
| /// source genuinely reported them, so an unknown is never frozen into config as a | ||
| /// permanent "Text" override (#1290), and operator-owned overrides survive re-selection | ||
| /// (#1127 / #1610). | ||
| /// model manager. The writer persists capability values only after explicit operator input. | ||
| /// Runtime detection owns provider capability data (#1756). | ||
| /// Operator overrides survive model re-selection (#1127 and #1610). | ||
| /// </summary> | ||
| public class ModelEntryWriterTests | ||
| { | ||
|
|
@@ -74,64 +74,90 @@ public void WriteRole_SameModelWithoutModalities_PreservesHandSetModalities() | |
| // Re-set the same model with only a context-window change; no modality intent supplied. | ||
| ModelEntryWriter.WriteRole( | ||
| models, "Main", "spark", "qwen-vl", ModelDiscoverySource.Manual, | ||
| ContextWindowOverride.Set(131072), ModalityOverride.Unset, ModalityOverride.Unset, discovered: null); | ||
| ContextWindowOverride.Set(131072), ModalityOverride.Unset, ModalityOverride.Unset); | ||
|
|
||
| var entry = ActiveEntry(models, "Main"); | ||
| Assert.Equal("Text, Image", entry["InputModalities"]); // preserved (#1127) | ||
| Assert.Equal(131072, entry["ContextWindow"]); // explicit override applied | ||
| } | ||
|
|
||
| [Fact] | ||
| public void WriteRole_SameModel_PreservesExistingClampOverDiscoveredWindow() | ||
| public void WriteRole_SameModel_PreservesExistingClampWithoutExplicitChange() | ||
| { | ||
| // Operator clamped ContextWindow below what the provider reports. | ||
| var models = Models( | ||
| """ | ||
| { "Main": { "Provider": "spark", "ModelId": "qwen-vl", "ContextWindow": 32000 } } | ||
| """); | ||
|
|
||
| // Re-select the same model (picker path): no explicit --context-window, but the probe | ||
| // reports the model's full window. The operator's clamp must win (#1610): ContextWindow | ||
| // is documented to take precedence over provider-reported detection. | ||
| // Re-select the same model through a picker that has no override input. | ||
| ModelEntryWriter.WriteRole( | ||
| models, "Main", "spark", "qwen-vl", ModelDiscoverySource.Live, | ||
| ContextWindowOverride.Unset, ModalityOverride.Unset, ModalityOverride.Unset, | ||
| Discovered(contextWindow: 128000)); | ||
| ContextWindowOverride.Unset, ModalityOverride.Unset, ModalityOverride.Unset); | ||
|
|
||
| var entry = ActiveEntry(models, "Main"); | ||
| Assert.Equal(32000, entry["ContextWindow"]); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void WriteRole_FirstTimeSet_UsesDiscoveredWindow() | ||
| public void WriteRole_FirstTimeSet_OmitsCapabilityOverrides() | ||
| { | ||
| // No existing entry for the role: discovery is the fallback that seeds the value. | ||
| var models = new Dictionary<string, object>(); | ||
|
|
||
| ModelEntryWriter.WriteRole( | ||
| models, "Main", "spark", "qwen-vl", ModelDiscoverySource.Live, | ||
| ContextWindowOverride.Unset, ModalityOverride.Unset, ModalityOverride.Unset, | ||
| Discovered(contextWindow: 128000)); | ||
| ContextWindowOverride.Unset, ModalityOverride.Unset, ModalityOverride.Unset); | ||
|
|
||
| var entry = ActiveEntry(models, "Main"); | ||
| Assert.Equal(128000, entry["ContextWindow"]); | ||
| Assert.False(entry.ContainsKey("ContextWindow")); | ||
| Assert.False(entry.ContainsKey("InputModalities")); | ||
| Assert.False(entry.ContainsKey("OutputModalities")); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void WriteRole_ClearContextWindow_DropsStoredClampAndDiscoveredWindow() | ||
| public void WriteRole_ProviderCapabilityChange_ReachesRuntimeResolution() | ||
| { | ||
| // Operator clamped the window; now --clear-context-window removes the clamp so runtime | ||
| // detection resolves it. Clear wins over BOTH the stored value and a probe that reports | ||
| // a window (#1610) — mirroring --clear-modalities. | ||
| var models = new Dictionary<string, object>(); | ||
| var selectedDuringProbe = new DiscoveredModel | ||
| { | ||
| ModelId = new ModelId("deepseek-v4-flash-dspark"), | ||
| ContextWindowTokens = 327680, | ||
| InputModalities = ModelModality.Text, | ||
| OutputModalities = ModelModality.Text, | ||
| }; | ||
|
|
||
| // The writer receives the selected identity, but it does not receive dynamic capabilities. | ||
| ModelEntryWriter.WriteRole( | ||
| models, "Main", "spark", selectedDuringProbe.ModelId.Value, ModelDiscoverySource.Live, | ||
| ContextWindowOverride.Unset, ModalityOverride.Unset, ModalityOverride.Unset); | ||
|
|
||
| var main = ConfigFileHelper.DeserializeSection<ModelReference>(ActiveEntry(models, "Main"))!; | ||
| var detectedAtStartup = new ResolvedModelCapabilities( | ||
| main.ModelId, | ||
| ModelModality.Text | ModelModality.Image, | ||
| ModelModality.Text, | ||
| 100000); | ||
|
|
||
| var resolved = ModelCapabilityResolution.ResolveModelCapabilities( | ||
| new ModelSelection { Main = main }, detectedAtStartup); | ||
|
|
||
| Assert.Equal(100000, resolved.ContextWindowTokens); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rationale: This test covers the complete producer-to-consumer contract. Selection sees |
||
| Assert.Equal(ModelModality.Text | ModelModality.Image, resolved.InputModalities); | ||
| Assert.Equal(ModelModality.Text, resolved.OutputModalities); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void WriteRole_ClearContextWindow_DropsStoredClamp() | ||
| { | ||
| // The operator removes the clamp. Runtime detection resolves the window. | ||
| var models = Models( | ||
| """ | ||
| { "Main": { "Provider": "spark", "ModelId": "qwen-vl", "ContextWindow": 32000 } } | ||
| """); | ||
|
|
||
| ModelEntryWriter.WriteRole( | ||
| models, "Main", "spark", "qwen-vl", ModelDiscoverySource.Live, | ||
| ContextWindowOverride.Clear, ModalityOverride.Unset, ModalityOverride.Unset, | ||
| Discovered(contextWindow: 128000)); | ||
| ContextWindowOverride.Clear, ModalityOverride.Unset, ModalityOverride.Unset); | ||
|
|
||
| var entry = ActiveEntry(models, "Main"); | ||
| Assert.False(entry.ContainsKey("ContextWindow")); // clamp removed → runtime detection | ||
|
|
@@ -147,27 +173,22 @@ public void WriteRole_SameDefinitionWithoutWindow_DiscoveryDoesNotResurrect() | |
|
|
||
| ModelEntryWriter.WriteRole( | ||
| models, "Main", "spark", "qwen-vl", ModelDiscoverySource.Live, | ||
| ContextWindowOverride.Unset, ModalityOverride.Unset, ModalityOverride.Unset, | ||
| Discovered(contextWindow: 128000)); | ||
| ContextWindowOverride.Unset, ModalityOverride.Unset, ModalityOverride.Unset); | ||
|
|
||
| Assert.False(ActiveEntry(models, "Main").ContainsKey("ContextWindow")); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void WriteRole_SameModelFreshProbe_DoesNotOverrideExistingModalities() | ||
| public void WriteRole_SameModel_PreservesExistingModalities() | ||
| { | ||
| // Operator override on disk; a fresh probe reports a coarser Text-only capability. | ||
| var models = Models( | ||
| """ | ||
| { "Main": { "Provider": "spark", "ModelId": "qwen-vl", "InputModalities": "Text, Image" } } | ||
| """); | ||
|
|
||
| // The stored override wins — discovery never silently overwrites it (#1610 / #5). This is | ||
| // the same rule as ContextWindow: the field is documented to bypass automated detection. | ||
| ModelEntryWriter.WriteRole( | ||
| models, "Main", "spark", "qwen-vl", ModelDiscoverySource.Live, | ||
| ContextWindowOverride.Unset, ModalityOverride.Unset, ModalityOverride.Unset, | ||
| Discovered(input: ModelModality.Text, output: ModelModality.Text)); | ||
| ContextWindowOverride.Unset, ModalityOverride.Unset, ModalityOverride.Unset); | ||
|
|
||
| var entry = ActiveEntry(models, "Main"); | ||
| Assert.Equal("Text, Image", entry["InputModalities"]); | ||
|
|
@@ -187,8 +208,7 @@ public void WriteRole_SameModelEntryClearedModality_DiscoveryDoesNotResurrect() | |
|
|
||
| ModelEntryWriter.WriteRole( | ||
| models, "Main", "spark", "qwen-vl", ModelDiscoverySource.Live, | ||
| ContextWindowOverride.Unset, ModalityOverride.Unset, ModalityOverride.Unset, | ||
| Discovered(input: ModelModality.Text | ModelModality.Image)); | ||
| ContextWindowOverride.Unset, ModalityOverride.Unset, ModalityOverride.Unset); | ||
|
|
||
| var entry = ActiveEntry(models, "Main"); | ||
| Assert.False(entry.ContainsKey("InputModalities")); // stays cleared, not resurrected | ||
|
|
@@ -207,7 +227,7 @@ public void WriteRole_ExplicitModalityOverride_ReplacesExisting() | |
| ModelEntryWriter.WriteRole( | ||
| models, "Main", "spark", "qwen-vl", ModelDiscoverySource.Manual, | ||
| ContextWindowOverride.Unset, | ||
| ModalityOverride.Set(ModelModality.Text), ModalityOverride.Unset, discovered: null); | ||
| ModalityOverride.Set(ModelModality.Text), ModalityOverride.Unset); | ||
|
|
||
| var entry = ActiveEntry(models, "Main"); | ||
| Assert.Equal("Text", entry["InputModalities"]); | ||
|
|
@@ -221,12 +241,10 @@ public void WriteRole_ClearModalities_RemovesOverrideEvenWhenProbeReportsOne() | |
| { "Main": { "Provider": "spark", "ModelId": "qwen-vl", "InputModalities": "Text, Image", "OutputModalities": "Text" } } | ||
| """); | ||
|
|
||
| // --clear-modalities removes both overrides so runtime detection resolves them; clear | ||
| // wins over BOTH the stored value and a probe that still reports modalities (#1610 / #4). | ||
| // The command removes both overrides. Runtime detection resolves the modalities. | ||
| ModelEntryWriter.WriteRole( | ||
| models, "Main", "spark", "qwen-vl", ModelDiscoverySource.Manual, | ||
| ContextWindowOverride.Unset, ModalityOverride.Clear, ModalityOverride.Clear, | ||
| Discovered(input: ModelModality.Text | ModelModality.Image)); | ||
| ContextWindowOverride.Unset, ModalityOverride.Clear, ModalityOverride.Clear); | ||
|
|
||
| var entry = ActiveEntry(models, "Main"); | ||
| Assert.False(entry.ContainsKey("InputModalities")); | ||
|
|
@@ -246,7 +264,7 @@ public void WriteRole_ExistingEntryOmitsModelId_DoesNotFalseMatchDefaultModel() | |
|
|
||
| ModelEntryWriter.WriteRole( | ||
| models, "Main", "local-ollama", "qwen3:30b", ModelDiscoverySource.Manual, | ||
| ContextWindowOverride.Unset, ModalityOverride.Unset, ModalityOverride.Unset, discovered: null); | ||
| ContextWindowOverride.Unset, ModalityOverride.Unset, ModalityOverride.Unset); | ||
|
|
||
| var entry = ActiveEntry(models, "Main"); | ||
| Assert.Equal("qwen3:30b", entry["ModelId"]); | ||
|
|
@@ -269,7 +287,7 @@ public void WriteRole_SameModel_PreservesProvenanceUnlessFreshlyDiscovered( | |
|
|
||
| ModelEntryWriter.WriteRole( | ||
| models, "Main", "spark", "qwen-vl", incoming, | ||
| ContextWindowOverride.Unset, ModalityOverride.Unset, ModalityOverride.Unset, discovered: null); | ||
| ContextWindowOverride.Unset, ModalityOverride.Unset, ModalityOverride.Unset); | ||
|
|
||
| var entry = ActiveEntry(models, "Main"); | ||
| Assert.Equal(expected.ToString(), entry["Provenance"]); | ||
|
|
@@ -288,8 +306,7 @@ public void WriteRole_CorruptExistingEntry_OverwritesInsteadOfThrowing() | |
|
|
||
| var ex = Record.Exception(() => ModelEntryWriter.WriteRole( | ||
| models, "Main", "spark", "qwen-vl", ModelDiscoverySource.Live, | ||
| ContextWindowOverride.Unset, ModalityOverride.Unset, ModalityOverride.Unset, | ||
| Discovered(contextWindow: 128000))); | ||
| ContextWindowOverride.Unset, ModalityOverride.Unset, ModalityOverride.Unset)); | ||
|
|
||
| Assert.Null(ex); | ||
| var entry = ActiveEntry(models, "Main"); | ||
|
|
@@ -311,8 +328,7 @@ public void WriteRole_CorruptModalityButValidWindow_PreservesWindow() | |
|
|
||
| ModelEntryWriter.WriteRole( | ||
| models, "Main", "spark", "qwen-vl", ModelDiscoverySource.Live, | ||
| ContextWindowOverride.Unset, ModalityOverride.Unset, ModalityOverride.Unset, | ||
| Discovered(contextWindow: 128000)); | ||
| ContextWindowOverride.Unset, ModalityOverride.Unset, ModalityOverride.Unset); | ||
|
|
||
| var entry = ActiveEntry(models, "Main"); | ||
| Assert.Equal(32768, entry["ContextWindow"]); // operator clamp preserved, not clobbered | ||
|
|
@@ -331,12 +347,11 @@ public void WriteRole_CorruptEntryForDifferentModel_DoesNotLeakWindow() | |
|
|
||
| ModelEntryWriter.WriteRole( | ||
| models, "Main", "spark", "qwen-vl", ModelDiscoverySource.Live, | ||
| ContextWindowOverride.Unset, ModalityOverride.Unset, ModalityOverride.Unset, | ||
| Discovered(contextWindow: 128000)); | ||
| ContextWindowOverride.Unset, ModalityOverride.Unset, ModalityOverride.Unset); | ||
|
|
||
| var entry = ActiveEntry(models, "Main"); | ||
| Assert.Equal("qwen-vl", entry["ModelId"]); | ||
| Assert.Equal(128000, entry["ContextWindow"]); // discovered window, not the other model's 32768 | ||
| Assert.False(entry.ContainsKey("ContextWindow")); | ||
| } | ||
|
|
||
| [Fact] | ||
|
|
@@ -350,13 +365,13 @@ public void WriteRole_SwitchAwayAndBack_PreservesPreviousModelModalities() | |
| // Switching roles changes only the role reference. The old definition remains intact. | ||
| ModelEntryWriter.WriteRole( | ||
| models, "Main", "spark", "other-model", ModelDiscoverySource.Manual, | ||
| ContextWindowOverride.Unset, ModalityOverride.Unset, ModalityOverride.Unset, discovered: null); | ||
| ContextWindowOverride.Unset, ModalityOverride.Unset, ModalityOverride.Unset); | ||
|
|
||
| Assert.Equal("other-model", ActiveEntry(models, "Main")["ModelId"]); | ||
|
|
||
| ModelEntryWriter.WriteRole( | ||
| models, "Main", "spark", "qwen-vl", ModelDiscoverySource.Manual, | ||
| ContextWindowOverride.Unset, ModalityOverride.Unset, ModalityOverride.Unset, discovered: null); | ||
| ContextWindowOverride.Unset, ModalityOverride.Unset, ModalityOverride.Unset); | ||
|
|
||
| Assert.Equal("Text, Image", ActiveEntry(models, "Main")["InputModalities"]); | ||
| } | ||
|
|
@@ -373,13 +388,4 @@ private static Dictionary<string, object> ActiveEntry( | |
| return (Dictionary<string, object>)definitions[definitionName]; | ||
| } | ||
|
|
||
| private static DiscoveredModel Discovered( | ||
| int? contextWindow = null, ModelModality? input = null, ModelModality? output = null) | ||
| => new() | ||
| { | ||
| ModelId = new ModelId("qwen-vl"), | ||
| ContextWindowTokens = contextWindow, | ||
| InputModalities = input, | ||
| OutputModalities = output, | ||
| }; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rationale: I did not add an automatic migration. Older config does not record whether a field came from discovery or an operator. Automatic deletion could remove a real override. The existing clear flags provide a safe, explicit repair.