Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(model-hub): reconcile kept usage filters and admit Source names trimmed (#2219)

Call White198bd3e0ea4a
diff --git a/core/handlers/model_hub/service.py b/core/handlers/model_hub/service.py
index aaa06f981..4e47f1daa 100644
--- a/core/handlers/model_hub/service.py
+++ b/core/handlers/model_hub/service.py
@@ -646,6 +646,18 @@ def _validated_base_url(value: object) -> Optional[str]:
raise ModelHubError("discovery_failed") from None
+def _validated_source_name(value: object) -> str:
+ """A Source name as the Web UI reads it: trimmed, present, and never a pasted credential.
+
+ Admission owns the trim, not the config parser: a name an earlier release
+ stored with edge or only whitespace must keep loading.
+ """
+ name = value.strip() if isinstance(value, str) else ""
+ if not name or len(name) > 64 or contains_credential_material(name):
+ raise ModelHubError("discovery_failed")
+ return name
+
+
def _builtin_model_ids(backend: str) -> tuple[str, ...]:
"""Real built-in model ids for a fixed-menu backend, from the bundled catalog.
@@ -3224,17 +3236,14 @@ class ModelHubService:
vendor = normalize_model_hub_vendor_id(vendor)
except ValueError:
raise ModelHubError("discovery_failed") from None
- display_name_defaulted = not payload.get("display_name")
- display_name = payload.get("display_name") or seeded_source_name(vendor)
+ # A blank name is no name, so the Source starts with the vendor's seed.
+ requested_name = payload.get("display_name")
+ if isinstance(requested_name, str):
+ requested_name = requested_name.strip()
+ display_name_defaulted = not requested_name
if kind not in {"subscription", "api_key"}:
raise ModelHubError("discovery_failed")
- if (
- not isinstance(display_name, str)
- or not display_name
- or len(display_name) > 64
- or contains_credential_material(display_name)
- ):
- raise ModelHubError("discovery_failed")
+ display_name = _validated_source_name(requested_name or seeded_source_name(vendor))
channel = payload.get("supply_channel") or ("native_cli" if kind == "subscription" else "hub")
if channel not in {"native_cli", "hub"} or (kind == "api_key" and channel != "hub"):
raise ModelHubError("discovery_failed")
@@ -3634,15 +3643,7 @@ class ModelHubService:
removed_hops: list[dict] = []
interrupted: list[dict] = []
if "display_name" in payload:
- display_name = payload["display_name"]
- if (
- not isinstance(display_name, str)
- or not display_name
- or len(display_name) > 64
- or contains_credential_material(display_name)
- ):
- raise ModelHubError("discovery_failed")
- source.display_name = display_name
+ source.display_name = _validated_source_name(payload["display_name"])
base_url_changed = (
"base_url" in payload and source.base_url != base_url
)
diff --git a/tests/fixtures/model_hub_source_edit_validation.json b/tests/fixtures/model_hub_source_edit_validation.json
index df850585c..26ab4f767 100644
--- a/tests/fixtures/model_hub_source_edit_validation.json
+++ b/tests/fixtures/model_hub_source_edit_validation.json
@@ -11,6 +11,18 @@
"valid": false,
"reason": "displayNameRequired"
},
+ {
+ "id": "whitespace_only",
+ "value": " ",
+ "valid": false,
+ "reason": "displayNameRequired"
+ },
+ {
+ "id": "edge_whitespace",
+ "value": " Relay key ",
+ "valid": true,
+ "normalized": "Relay key"
+ },
{
"id": "too_long",
"value": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
diff --git a/tests/scenarios/model_hub/catalog.yaml b/tests/scenarios/model_hub/catalog.yaml
index 7aadbeb77..1ed749a59 100644
--- a/tests/scenarios/model_hub/catalog.yaml
+++ b/tests/scenarios/model_hub/catalog.yaml
@@ -292,6 +292,15 @@ scenarios:
related_tests:
- ui/src/components/settings/models/supply.test.ts
- ui/e2e/model-catalog/gateway-status.spec.ts
+ - id: MH-SRC-NAME-001
+ name: A Source name is admitted trimmed; a blank one on create takes the vendor seed and an edit to blank is refused, matching the Web UI's own check
+ status: covered
+ kind: contract
+ layer: unit
+ test: tests/test_model_hub_api.py::test_source_names_are_admitted_trimmed
+ related_tests:
+ - tests/test_model_hub_api.py::test_source_edit_validation_contract_fixture
+ - ui/src/components/settings/models/manage.test.ts
- id: MH-SRC-DELETE-001
name: Source deletion removes every reference while preserving survivor order
status: covered
@@ -749,6 +758,15 @@ scenarios:
- ui/src/components/settings/models/UsageTab.test.tsx::MH-USAGE-017
- ui/src/components/settings/models/UsageTab.test.tsx::MH-USAGE-034
- ui/e2e/model-catalog/usage.spec.ts
+ - id: MH-USAGE-035
+ name: A Source or model filter kept across reports holds only what the current report offers, folding a Source config let go into the removed aggregate and dropping any other stale key, the aggregate included
+ status: covered
+ kind: contract
+ layer: unit
+ test: ui/src/components/settings/models/usageProjection.test.ts::MH-USAGE-035
+ related_tests:
+ - ui/src/components/settings/models/UsageTab.test.tsx::MH-USAGE-035
+ - ui/src/components/settings/models/UsageTab.test.tsx::MH-USAGE-034
- id: MH-USAGE-BACKEND-001
name: Modern usage reports keep dense hourly buckets and distinct Unicode Source/model identities
status: covered
diff --git a/tests/test_model_hub_api.py b/tests/test_model_hub_api.py
index a3172e8b9..bca4fd97f 100644
--- a/tests/test_model_hub_api.py
+++ b/tests/test_model_hub_api.py
@@ -63,6 +63,7 @@ from core.handlers.model_hub.service import (
ModelHubError,
ModelHubService,
create_default_service,
+ seeded_source_name,
)
from tests.ui_server_test_helpers import (
_save_config,
@@ -9498,6 +9499,26 @@ def test_an_emptied_endpoint_is_judged_by_the_vendor_alone():
assert {vendor: verdicts for vendor, verdicts in by_vendor.items() if len(verdicts) != 1} == {}
+def test_source_names_are_admitted_trimmed(tmp_path):
+ """MH-SRC-NAME-001: a Source name is stored as it reads, trimmed.
+
+ A blank name on create is no name and takes the vendor's seed, as an omitted
+ one does; editing a name to blank is refused (the shared fixture holds the
+ edit cases). Neither may store a name the Web UI renders empty.
+ """
+ service, store, _ = _service(tmp_path)
+ seeded = asyncio.run(
+ _create_source(service, {"kind": "api_key", "vendor": "anthropic", "display_name": " ", "key": "sk-test-a"})
+ )
+ named = asyncio.run(
+ _create_source(service, {"kind": "api_key", "vendor": "anthropic", "display_name": " Relay ", "key": "sk-test-b"})
+ )
+
+ assert seeded["display_name"] == seeded_source_name("anthropic")
+ assert named["display_name"] == "Relay"
+ assert [source.display_name for source in store.config.sources] == [seeded_source_name("anthropic"), "Relay"]
+
+
def test_source_display_names_reject_credential_material(tmp_path):
service, store, adapter = _service(tmp_path)
pasted_key = "sk-test-never-persist-this"
diff --git a/ui/src/components/settings/models/UsageTab.test.tsx b/ui/src/components/settings/models/UsageTab.test.tsx
index 5c4a7c283..ecab8bae2 100644
--- a/ui/src/components/settings/models/UsageTab.test.tsx
+++ b/ui/src/components/settings/models/UsageTab.test.tsx
@@ -187,6 +187,36 @@ describe('UsageTab', () => {
expect(container.textContent).not.toContain('src_');
});
+ it('MH-USAGE-035: a picked aggregate the next report no longer offers is dropped, not kept for a later one', async () => {
+ const page = (value: UsageReport) => (
+ <I18nextProvider i18n={i18n}>
+ <UsageTab usage={readyRegion(value)} windowKey="24h" onWindowChange={vi.fn()} />
+ </I18nextProvider>
+ );
+ const live = report();
+ const withRemoved = {
+ ...live,
+ sources: [...live.sources, { source_id: 'src_gone01', label: null, last_metered_at: null, ...counters(), models: [] }],
+ buckets: [bucket('00', [row(), row({ source_id: 'src_gone01', model_id: 'model-g' })])],
+ };
+ const { container, rerender } = render(page(withRemoved));
+ await userEvent.click(screen.getByRole('button', { name: 'All Source' }));
+ await userEvent.click(screen.getByRole('option', { name: /Removed providers/ }));
+ await userEvent.keyboard('{Escape}');
+ expect(screen.getByRole('button', { name: 'Reset filters' })).toBeTruthy();
+
+ rerender(page(live));
+ expect(screen.getByRole('button', { name: 'All Source' })).toBeTruthy();
+ expect(screen.queryByRole('button', { name: 'Reset filters' })).toBeNull();
+ expect(container.querySelector('.model-hub-usage-empty-state')).toBeNull();
+ expect(within(screen.getByRole('table')).getAllByText('Shared label · Shared model').length).toBeGreaterThan(0);
+
+ // Dropped means gone: a later report offering it again does not bring it back.
+ rerender(page(withRemoved));
+ expect(screen.getByRole('button', { name: 'All Source' })).toBeTruthy();
+ expect(screen.queryByRole('button', { name: 'Reset filters' })).toBeNull();
+ });
+
it('MH-USAGE-018: reads a shortfall as reports that never arrived', () => {
const shortfall = counters({
requests: 4,
diff --git a/ui/src/components/settings/models/UsageTab.tsx b/ui/src/components/settings/models/UsageTab.tsx
index 0beb001a2..99e60328e 100644
--- a/ui/src/components/settings/models/UsageTab.tsx
+++ b/ui/src/components/settings/models/UsageTab.tsx
@@ -970,7 +970,12 @@ export const UsageTab: React.FC<{
const [tableGroup, setTableGroup] = React.useState<'model' | 'source'>('model');
const [sortAscending, setSortAscending] = React.useState(false);
const [pinnedKey, setPinnedKey] = React.useState<string | null>(null);
- const scopeKey = JSON.stringify([windowKey, sourceIds, modelKeys, metric, group]);
+ // What the filters show selected: this report's own options, never a key it no longer offers.
+ const selection = React.useMemo(
+ () => (report === null ? { sourceIds, modelKeys } : foldUsageSelection(report, { sourceIds, modelKeys })),
+ [report, sourceIds, modelKeys],
+ );
+ const scopeKey = JSON.stringify([windowKey, selection.sourceIds, selection.modelKeys, metric, group]);
React.useEffect(() => {
setSourceIds([]);
@@ -992,6 +997,15 @@ export const UsageTab: React.FC<{
}
}, [report, pinnedKey]);
+ // The kept selection is the reconciled one, so a key this report dropped
+ // cannot come back unseen once a later report offers it again.
+ React.useEffect(() => {
+ const same = (left: readonly string[], right: readonly string[]) =>
+ left.length === right.length && left.every((key, index) => key === right[index]);
+ if (!same(selection.sourceIds, sourceIds)) setSourceIds([...selection.sourceIds]);
+ if (!same(selection.modelKeys, modelKeys)) setModelKeys([...selection.modelKeys]);
+ }, [selection, sourceIds, modelKeys]);
+
if (report === null) {
return (
<div className="model-hub-usage-analytics">
@@ -1018,8 +1032,6 @@ export const UsageTab: React.FC<{
label: identity.label,
detail: identity.unlisted ? t('settings.models.usage.removedModel') : undefined,
}));
- // What the filters show selected; a Source deleted since it was picked reads as the aggregate.
- const selection = foldUsageSelection(report, { sourceIds, modelKeys });
const filter = resolveUsageFilter(report, selection);
const scopedRows = filteredRows(report, filter);
const totals = aggregateCounters(scopedRows);
@@ -1112,7 +1124,7 @@ export const UsageTab: React.FC<{
</select>
<ChevronDown aria-hidden className="size-3" />
</div>
- {(sourceIds.length > 0 || modelKeys.length > 0 || metric !== 'tokens') && (
+ {(selection.sourceIds.length > 0 || selection.modelKeys.length > 0 || metric !== 'tokens') && (
<Button
type="button"
variant="ghost"
diff --git a/ui/src/components/settings/models/usageProjection.test.ts b/ui/src/components/settings/models/usageProjection.test.ts
index d9bede21a..97c20ba07 100644
--- a/ui/src/components/settings/models/usageProjection.test.ts
+++ b/ui/src/components/settings/models/usageProjection.test.ts
@@ -4,6 +4,7 @@ import type { UsageBucket, UsageBucketRow, UsageCounters, UsageReport } from './
import {
aggregateCounters,
filteredRows,
+ foldUsageSelection,
formatBucketAxisLabel,
formatBucketHeading,
formatBucketRange,
@@ -164,6 +165,33 @@ describe('usageProjection', () => {
expect(live).toEqual([]);
});
+ // A selection outlives the report it was made in; the next one may no
+ // longer offer what it names.
+ it('MH-USAGE-035: reconcile a kept selection with what this report offers', () => {
+ const live = reportWith([bucket('00', [row()])]);
+ const removed = withRemoved();
+ const livePair = pairKey('source-a', 'model-a');
+ const removedPair = pairKey('src_d1f4adc47c3f', 'glm-5.3');
+ // Config let the picked Source go: it, and any pair of it, reads as the aggregate.
+ expect(foldUsageSelection(removed, { sourceIds: ['src_d1f4adc47c3f', 'src_b52ba34d0659'], modelKeys: [removedPair] }))
+ .toEqual({ sourceIds: [REMOVED_SOURCES_KEY], modelKeys: [REMOVED_SOURCES_KEY] });
+ // The aggregate was picked, and nothing folds into it any more.
+ expect(foldUsageSelection(live, { sourceIds: [REMOVED_SOURCES_KEY], modelKeys: [REMOVED_SOURCES_KEY] }))
+ .toEqual(NO_FILTER);
+ // A removed Source with no metered row still offers the aggregate as a Source, never as a model.
+ const unmetered = reportWith([bucket('00', [row()])]);
+ unmetered.sources.push({ source_id: 'src_d1f4adc47c3f', label: null, last_metered_at: null, ...counters(), models: [] });
+ expect(foldUsageSelection(unmetered, { sourceIds: [REMOVED_SOURCES_KEY], modelKeys: [REMOVED_SOURCES_KEY] }))
+ .toEqual({ sourceIds: [REMOVED_SOURCES_KEY], modelKeys: [] });
+ // A picked model, or Source, this report never metered is dropped; what it still offers stays.
+ const later = reportWith([bucket('00', [row({ model_id: 'model-b' })])]);
+ expect(foldUsageSelection(later, { sourceIds: ['source-a', 'source-z'], modelKeys: [livePair, pairKey('source-a', 'model-b')] }))
+ .toEqual({ sourceIds: ['source-a'], modelKeys: [pairKey('source-a', 'model-b')] });
+ // Nothing left picked means no filter, not a filter that matches nothing.
+ expect(filteredRows(later, resolveUsageFilter(later, { sourceIds: [], modelKeys: [livePair] })))
+ .toEqual(filteredRows(later, NO_FILTER));
+ });
+
it('never fold a live Source, even one that metered nothing in the window', () => {
const value = reportWith([bucket('00', [row({ source_id: 'src_d1f4adc47c3f', model_id: 'glm-5.3' })])]);
value.sources = [
diff --git a/ui/src/components/settings/models/usageProjection.ts b/ui/src/components/settings/models/usageProjection.ts
index ffb847023..2c73fee06 100644
--- a/ui/src/components/settings/models/usageProjection.ts
+++ b/ui/src/components/settings/models/usageProjection.ts
@@ -377,20 +377,32 @@ export function usageIdentities(report: UsageReport, group: 'model' | 'source',
}
/**
- * A selection made while a Source was live still names its ID, or its pairs,
- * after config lets it go. From then on it names the removed aggregate, the
- * only identity the filters and the table still offer for that Source.
+ * A selection is kept across reports, so it can name identities the current
+ * one no longer offers. A Source config let go since it was picked, or any
+ * pair of it, now reads as the removed aggregate. Any other key this report has
+ * no option for is dropped, the aggregate itself once nothing folds into it, so
+ * the filters never hold a choice they cannot show.
*/
export function foldUsageSelection(report: UsageReport, selection: UsageFilter): UsageFilter {
const live = liveSources(report);
- const known = new Set([...report.sources.map((source) => source.source_id), ...allRows(report).map((row) => row.source_id)]);
- const removed = (sourceId: string) => known.has(sourceId) && !live.has(sourceId);
- const fold = (keys: readonly string[], sourceOf: (key: string) => string) => [...new Set(keys.map((key) => (
- removed(sourceOf(key)) ? REMOVED_SOURCES_KEY : key
- )))];
+ const rows = allRows(report);
+ const known = new Set([...report.sources.map((source) => source.source_id), ...rows.map((row) => row.source_id)]);
+ const fold = (sourceId: string, key: string) => (live.has(sourceId) ? key : REMOVED_SOURCES_KEY);
+ // What each filter lists: every Source the report names, and every pair it metered.
+ const offered = {
+ sourceIds: new Set([...known].map((sourceId) => fold(sourceId, sourceId))),
+ modelKeys: new Set(rows.map((row) => fold(row.source_id, pairKey(row.source_id, row.model_id)))),
+ };
+ const reconcile = (keys: readonly string[], options: ReadonlySet<string>, sourceOf: (key: string) => string) => [
+ ...new Set(keys.map((key) => (known.has(sourceOf(key)) ? fold(sourceOf(key), key) : key))),
+ ].filter((key) => options.has(key));
return {
- sourceIds: fold(selection.sourceIds, (key) => key),
- modelKeys: fold(selection.modelKeys, (key) => key.slice(0, Math.max(0, key.indexOf(PAIR_SEPARATOR)))),
+ sourceIds: reconcile(selection.sourceIds, offered.sourceIds, (key) => key),
+ modelKeys: reconcile(
+ selection.modelKeys,
+ offered.modelKeys,
+ (key) => key.slice(0, Math.max(0, key.indexOf(PAIR_SEPARATOR))),
+ ),
};
}
@@ -398,7 +410,7 @@ export function foldUsageSelection(report: UsageReport, selection: UsageFilter):
* A selection names identities; the rows it keeps are named by the IDs they
* were metered under. The removed aggregate therefore stands for every Source
* ID, and every pair, it folded. It stays in the list too, matching no row, so
- * a selection whose rows are gone never widens to everything.
+ * picking it never widens to everything while it is still offered.
*/
export function resolveUsageFilter(report: UsageReport, picked: UsageFilter): UsageFilter {
const selection = foldUsageSelection(report, picked);