Skip to content

Agent Zero AI framework

Discard model search results for changed drafts and queries

Alessandro70c817e0e359
diff --git a/plugins/_model_config/AGENTS.md b/plugins/_model_config/AGENTS.md
index e015aff87..2314e15fd 100644
--- a/plugins/_model_config/AGENTS.md
+++ b/plugins/_model_config/AGENTS.md
@@ -53,7 +53,7 @@
- Migration must preserve existing definitions and distinct scoped model choices, back up replaced user files once, strip inline secrets, and remain idempotent.
- Venice migration applies chat-only kwargs to chat slots and removes them from embedding slots without discarding embedding-specific kwargs.
- On every startup after migration, short-circuit when `usr/plugins/_model_config/presets.yaml` exists. Only a missing collection may fetch `agent0ai/a0-presets`; parse remote and plugin-local fallback YAML through the same validator, strip secrets before persistence, and persist `mode_presets_fallback.yaml` when download or validation fails.
-- Model-name catalogs open below the input from either a field click or the embedded magnifier.
+- Model-name catalogs open below the input from either a field click or the embedded magnifier. Discard asynchronous results if their provider, API base, model draft, or query changed while the request was pending.
- Additional parameter drafts retain plain-string compatibility but reject malformed JSON-shaped values and invalid KEY=VALUE lines with a standard notification. Reparse every preset slot before saving presets or API keys; invalid drafts stay editable and never silently save stale kwargs.
## Work Guidance
diff --git a/plugins/_model_config/webui/model-field.html b/plugins/_model_config/webui/model-field.html
index f16cd9526..16e36b8f9 100644
--- a/plugins/_model_config/webui/model-field.html
+++ b/plugins/_model_config/webui/model-field.html
@@ -49,9 +49,9 @@
doSearch() {
if (this.searching) return;
this.searching = true;
- const provider = _prov, apiBase = _apiBase;
- $store.modelConfig.searchModelsDetailed(provider, model.name, searchType, apiBase).then(r => {
- if (provider !== _prov || apiBase !== _apiBase) return;
+ const provider = _prov, apiBase = _apiBase, draft = model, query = model.name;
+ $store.modelConfig.searchModelsDetailed(provider, query, searchType, apiBase).then(r => {
+ if (provider !== _prov || apiBase !== _apiBase || draft !== model || query !== model.name) return;
this.results = r.models;
this.source = r.source;
this.open = true;
diff --git a/tests/test_model_search_controller.py b/tests/test_model_search_controller.py
new file mode 100644
index 000000000..efac600d4
--- /dev/null
+++ b/tests/test_model_search_controller.py
@@ -0,0 +1,41 @@
+import html
+import json
+from pathlib import Path
+import re
+import shutil
+import subprocess
+
+import pytest
+
+
[email protected](not shutil.which('node'), reason='node is required')
+def test_model_search_discards_stale_requests_and_allows_fresh_search():
+ source = (Path(__file__).parents[1] / 'plugins/_model_config/webui/model-field.html').read_text()
+ controller = html.unescape(re.search(r'x-data="(\{ results: \[\], source:.*?})"', source, re.S).group(1))
+ script = r'''
+import assert from 'node:assert/strict';
+for (const change of ['query', 'draft', 'provider', 'base']) {
+ let model = {name:'first'}, _prov = 'openai', _apiBase = 'same';
+ const searchType = 'chat';
+ let resolve;
+ const $store = {modelConfig:{searchModelsDetailed:()=>new Promise(r=>resolve=r)}};
+ const field = eval('(' + CONTROLLER + ')');
+ field.doSearch();
+ if (change === 'query') model.name = 'second';
+ if (change === 'draft') model = {name:'first'};
+ if (change === 'provider') _prov = 'other';
+ if (change === 'base') _apiBase = 'other';
+ resolve({models:['stale'],source:'provider_endpoint'});
+ await new Promise(setImmediate);
+ assert.deepEqual(field.results, []);
+ assert.equal(field.open, false);
+ assert.equal(field.searching, false);
+ field.doSearch();
+ resolve({models:['fresh'],source:'provider_endpoint'});
+ await new Promise(setImmediate);
+ assert.deepEqual(field.results, ['fresh']);
+ assert.equal(field.open, true);
+ assert.equal(field.searching, false);
+}
+'''.replace('CONTROLLER', json.dumps(controller))
+ subprocess.run(['node', '--input-type=module', '-e', script], check=True)