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.

test(ui): guard Model Hub token scope in rendered consumers (#2025)

Call White3cc76ea45a84
diff --git a/docs/plans/issue-1856-model-hub-token-guard.md b/docs/plans/issue-1856-model-hub-token-guard.md
new file mode 100644
index 000000000..48c1b687d
--- /dev/null
+++ b/docs/plans/issue-1856-model-hub-token-guard.md
@@ -0,0 +1,94 @@
+# Model Hub rendered token scope contract
+
+## Problem and decision
+
+At `0e5a672ad183ac56f5ee8df7bcb770754840b3d9`, the 23 travelling-token
+tests protect only `GuardImpact`, `GuardGapList`, and their body wrapper.
+Moving `--model-hub-model-control-height` from its control class to
+`.model-hub-catalog-dialog` still passes that check. The editor portals outside
+the catalog DOM subtree, so this declaration cannot supply its inputs.
+The picker also portals, but carries the catalog class itself: moving a token
+there is not the same counterexample.
+
+The five findings-bearing heads of #1850 produced 16 analyzer findings against
+CSS/JSX models. That implementation was withdrawn. Name existence alone cannot
+prove inheritance, and a blanket markup-token policy would restrict valid
+component styling without demonstrating this boundary.
+
+Reuse the existing Model Catalog Playwright fixture and `lint` / `ui-checks`
+owner. Assert finite computed geometry and text-role contracts on production
+catalog, picker, editor, and shared guard-body markup. Let Chromium evaluate
+selectors, generated Tailwind utilities, conditions, and React's actual DOM.
+No new dependency, analyzer, workflow, or component authoring rule is needed.
+
+## Invariant and boundaries
+
+- The named catalog, picker, editor, and guard-body metrics retain their
+ current computed values on desktop/mobile under explicit and system themes.
+- A shared guard body outside a dialog retains its spacing, type, and count
+ padding. Its label follows a nested light theme rather than a dark root alias.
+- The editor's forwarded field label and its portalled input are measured at
+ the DOM elements that receive the production props.
+- Moving/removing a relevant declaration and freezing a label alias must fail
+ the corresponding assertion. Equivalent selector spellings and conditional
+ declarations active in the tested state must still pass.
+
+This is finite rendering evidence, not proof of all classes, mount locations,
+React branches, browser engines, viewports, CSS conditions, or arbitrary token
+resolution. No production appearance or behavior is changed. The narrow static
+analyzer remains at its existing scope; its `:where` / `:is` false positive is
+not generalized or repaired here.
+
+The existing root-derived washes/accents and mixed-theme behavior documented
+in #1850 remain outside this invariant. A current visual defect there requires
+a separate product-scope decision, not silently changing the baseline to make
+this guard pass.
+
+## Validation plan
+
+- Baseline travelling-token tests and same-source browser measurements.
+- Missing/misplaced metric and frozen-theme-alias sensitivity controls.
+- Valid equivalent selector/scope controls, using the same rendering assertion.
+- Targeted and full Model Catalog browser suite, affected UI unit suite,
+ lint, test typecheck, theme validation, and production build.
+- Local task-isolated Incus only if live integration is needed and capacity is
+ available; never use a shared or running personal service as a fixture.
+
+## Status
+
+The targeted suite passes all 26 desktop/mobile cases. A source-level
+sensitivity experiment temporarily made both changes below, then ran the
+unchanged baseline assertions through Vite/Tailwind:
+
+1. Move the sole `--model-hub-model-control-height: 40px` declaration onto
+ `.model-hub-catalog-dialog`. The editor input computes to **28.75px**, and the
+ new 40px assertion fails.
+2. Add `:root { --scope-control-ink: var(--model-hub-ink-73) }` and make
+ `.model-hub-guard-label` read it. Under nested light on dark, the label
+ computes to **rgba(255, 255, 255, 0.45)** rather than the local muted-ink role
+ **color(srgb 0.360784 0.376471 0.47451 / 0.8)**, and the new assertion fails.
+
+The original 23 travelling-token tests pass with both mutations present.
+The stylesheet was restored exactly afterward; there is no production diff.
+Equivalent declaration controls and the unchanged source pass in Chromium.
+The committed sensitivity cases mutate only the emitted CSS in disposable
+browser contexts, so CI can repeat them without rewriting the checkout.
+
+Historical findings are covered only where this mechanism makes a claim:
+functional/context selectors use the actual matched element; conditional
+availability is observed in screen/print and active supports controls; missing
+alias dependencies and nested theme substitution are observed at the consuming
+element. The editor's real forwarded label is measured after React renders it.
+No source-text/JSX parsing, arbitrary branch enumeration, `@theme` emission-mode
+classifier, or universal alias analysis is introduced. Those old analyzer
+findings therefore do not become claims of coverage here.
+
+Broader validation passed: 130 Model Catalog browser cases; 304 UI unit-test
+files / 4,137 tests; lint with no baseline drift; Model Catalog fixture and
+Model Hub unit-test typechecks; theme and scenario-catalog validation; and
+the production build. No Python source changed.
+
+No live backend integration is required for this test-only rendering guard:
+the fixture integrates the shipped components, Radix, Vite/Tailwind, and real
+Chromium while replacing only the API boundary. It starts no Avibe instance
+and does not use Incus or the personal service.
diff --git a/tests/scenarios/model_hub/catalog.yaml b/tests/scenarios/model_hub/catalog.yaml
index 092f37192..b96c97e87 100644
--- a/tests/scenarios/model_hub/catalog.yaml
+++ b/tests/scenarios/model_hub/catalog.yaml
@@ -235,6 +235,7 @@ scenarios:
related_tests:
- ui/src/components/settings/models/BackendModelPickerDialog.test.tsx::finds an already-added model by its supplier and shows it as already added
- ui/src/components/settings/models/BackendModelCatalogDialog.test.tsx::asks which row a typed ID names as the ID it would be saved as
+ - ui/e2e/model-catalog/token-scope.spec.ts
- id: MH-GATEWAY-STATUS-001
name: Gateway route coverage stays independent of Agent selections and attributes Agent issues below the catalog
status: covered
diff --git a/ui/e2e/model-catalog/README.md b/ui/e2e/model-catalog/README.md
index 06a8238b8..3d006d87c 100644
--- a/ui/e2e/model-catalog/README.md
+++ b/ui/e2e/model-catalog/README.md
@@ -43,3 +43,34 @@ Cancel changes rereads saved intent in place, nested picker dismissal preserves
the parent, and only Save persists the exact ordered pairs. Desktop/mobile EN/ZH
screenshots also cover explicit pinning, long identities and light/dark footer
geometry. Its in-memory route API is installed only for the route fixture view.
+
+`token-scope.spec.ts` extends `MH-MENU-COMPOSE-001` with finite rendering
+contracts for the catalog name, picker row/name, portalled editor input/body,
+and the editor's forwarded field label. An opt-in fixture also mounts the real
+`GuardImpact` / `GuardGapList` with no dialog ancestor and checks their body,
+label/count, list/hop, and hint geometry. Expected values are painted CSS
+properties, not the existence of custom-property names.
+
+The matrix covers desktop/mobile Chromium and explicit dark/light themes
+against the opposite system preference, plus both system preferences with no
+explicit theme. A nested light boundary on the standalone guard body checks its
+label against the local muted-ink role. The catalog → picker → custom-editor
+path uses production components and Radix portals. The direct edit path is also
+used by the controls. Tailwind's generated utilities and the imported production
+stylesheets participate in every run.
+
+Sensitivity controls remove/relocate emitted declarations, put a metric under
+inactive print media, use a root alias with a missing dependency, and freeze an
+ink above the nested light boundary. Positive controls use `:where`, `:is`,
+an ancestor-qualified selector, active media/supports conditions, a repaired
+dependency, and a theme selector list. These run in fresh browser contexts;
+they never rewrite source or contact Avibe. They verify this contract's
+sensitivity, not a new cascade evaluator.
+
+The suite does not certify every Model Hub class, arbitrary React branches or
+CSS conditions, theme nesting in both directions, Tailwind directive modes,
+pseudo-elements, or other browser engines. The existing root-derived washes,
+accent mixes, and shadows retain their known mixed-theme limitation. Production
+CSS and component behavior are unchanged. See
+`docs/plans/issue-1856-model-hub-token-guard.md` (repository root) for the source
+mutation reproduction and the historical analyzer boundary.
diff --git a/ui/e2e/model-catalog/fixture.tsx b/ui/e2e/model-catalog/fixture.tsx
index e4c08cafb..021c31c7b 100644
--- a/ui/e2e/model-catalog/fixture.tsx
+++ b/ui/e2e/model-catalog/fixture.tsx
@@ -12,6 +12,7 @@ import { modelsApi } from '../../src/components/settings/models/modelsApi';
import type { AgentBackend, AgentSupply, BackendModel, BackendModelsPut } from '../../src/components/settings/models/types';
import { GatewayFixture } from './gatewayFixture';
import { RouteFixture } from './routeFixture';
+import { TokenScopeFixture } from './tokenScopeFixture';
const params = new URLSearchParams(location.search);
const backend = params.get('backend') as AgentBackend;
@@ -46,6 +47,7 @@ export function Fixture() {
const [open, setOpen] = useState(false);
const [, setRevision] = useState(0);
return <I18nextProvider i18n={language}>
+ {params.has('tokens') && <TokenScopeFixture />}
<button type="button" onClick={() => setOpen(true)}>Manage models</button>
<output data-testid="saved">{JSON.stringify({ saved, writes })}</output>
{open && <BackendModelCatalogDialog open backend={backend} canReadSources sourceNames={{}}
diff --git a/ui/e2e/model-catalog/token-scope.spec.ts b/ui/e2e/model-catalog/token-scope.spec.ts
new file mode 100644
index 000000000..6c3f9d5e5
--- /dev/null
+++ b/ui/e2e/model-catalog/token-scope.spec.ts
@@ -0,0 +1,203 @@
+import { expect, test, type Locator, type Page } from '@playwright/test';
+import { hub } from '../support/copy';
+
+const text = (key: string, vars?: Record<string, string | number>) => hub(`gateway.${key}`, vars, 'en');
+
+// The expected painted properties are independent of the declarations being
+// checked. A token's presence, or an equally broken reference copy, is no oracle.
+const guardMetrics = [
+ ['.model-hub-guard-body', { padding: '20px', gap: '14px' }],
+ ['.model-hub-guard-label', { gap: '6px', fontSize: '10.5px' }],
+ ['.model-hub-guard-label > span', { padding: '3px 8px', fontSize: '10px' }],
+ ['.model-hub-guard-list', { padding: '8px', gap: '6px', borderRadius: '10px' }],
+ ['.model-hub-guard-hop', { minHeight: '52px', padding: '0px 10px', gap: '10px', borderRadius: '8px' }],
+ ['.model-hub-guard-hop strong', { fontSize: '12px' }],
+ ['.model-hub-guard-hop span span', { fontSize: '10.5px', marginTop: '3px' }],
+ ['.model-hub-guard-hint', { gap: '7px', fontSize: '11.5px', lineHeight: '17px' }],
+] as const;
+
+async function metrics(locator: Locator, expected: Readonly<Record<string, string>>) {
+ await expect(locator.first()).toBeVisible();
+ const actual = await locator.evaluateAll((nodes, properties) => nodes.map((node) => {
+ const style = getComputedStyle(node);
+ return Object.fromEntries(properties.map((property) => [property, style[property as keyof CSSStyleDeclaration]]));
+ }), Object.keys(expected));
+ expect(actual.length).toBeGreaterThan(0);
+ for (const value of actual) expect(value).toEqual(expected);
+}
+
+async function guardContract(page: Page) {
+ for (const [selector, expected] of guardMetrics) {
+ await metrics(page.getByTestId('token-scope-body').locator(selector), expected);
+ }
+}
+
+async function inkContract(locator: Locator, light: boolean) {
+ const expected = await locator.first().evaluate((node, isLight) => {
+ const probe = document.createElement('span');
+ probe.style.color = isLight
+ ? 'color-mix(in srgb, var(--muted) 80%, transparent)'
+ : 'rgb(255 255 255 / 45.1%)';
+ node.append(probe);
+ const ink = getComputedStyle(probe).color;
+ probe.remove();
+ return ink;
+ }, light);
+ await expect(locator.first()).toHaveCSS('color', expected);
+}
+
+async function openCatalog(page: Page) {
+ await page.getByRole('button', { name: 'Manage models', exact: true }).click();
+ await expect(page.getByRole('button', { name: text('catalog.edit', { model: 'Existing model' }), exact: true })).toBeVisible();
+}
+
+async function openEditor(page: Page) {
+ await openCatalog(page);
+ await page.getByRole('button', { name: text('catalog.edit', { model: 'Existing model' }), exact: true }).click();
+ const editor = page.locator('.model-hub-model-editor');
+ await expect(editor).toBeVisible();
+ // React ownership does not imply DOM inheritance through the Radix portal.
+ expect(await editor.evaluate((node) => node.closest('.model-hub-catalog-dialog'))).toBeNull();
+ return editor;
+}
+
+async function editorContract(editor: Locator) {
+ await metrics(editor.locator('input.model-hub-model-control'), { height: '40px' });
+ // Field forwards labelClassName to its own label element.
+ await metrics(editor.locator('.model-hub-model-field-label'), { fontSize: '12px' });
+ await metrics(editor.locator('.model-hub-model-editor-body'), { gap: '20px', padding: '18px 24px' });
+}
+
+test.beforeEach(async ({ page }) => {
+ await page.route('**/api/**', (route) => route.abort());
+});
+
+for (const mode of ['dark', 'light', 'system-dark', 'system-light'] as const) {
+ test(`MH-MENU-COMPOSE-001: token scope survives portalled consumers in ${mode}`, async ({ page }, testInfo) => {
+ const light = mode.endsWith('light');
+ await page.emulateMedia({ colorScheme: mode.startsWith('system') ? (light ? 'light' : 'dark') : (light ? 'dark' : 'light') });
+ await page.goto('/e2e/model-catalog/fixture.html?backend=codex&tokens');
+ await page.evaluate((theme) => {
+ if (theme.startsWith('system')) document.documentElement.removeAttribute('data-theme');
+ else document.documentElement.setAttribute('data-theme', theme);
+ }, mode);
+ await guardContract(page);
+ await inkContract(page.getByTestId('token-scope-body').locator('.model-hub-guard-label'), light);
+
+ await openCatalog(page);
+ await metrics(page.locator('.model-hub-catalog-name'), { fontSize: '13.5px' });
+ await page.getByRole('button', { name: text('catalog.add'), exact: true }).click();
+ const row = page.getByRole('checkbox', { name: /claude-candidate-alpha/ });
+ await metrics(row, { minHeight: '36px', borderRadius: '8px', columnGap: '10px', padding: '0px 12px' });
+ await metrics(row.locator('.model-hub-picker-name'), { fontSize: '12.5px' });
+ await page.getByRole('button', { name: text('picker.custom'), exact: true }).click();
+ const editor = page.locator('.model-hub-model-editor');
+ await editorContract(editor);
+ expect(await editor.evaluate((node) => node.closest('.model-hub-catalog-dialog'))).toBeNull();
+ await editor.evaluate(async (node) => {
+ await Promise.all(node.getAnimations().map((animation) => animation.finished));
+ });
+ await page.screenshot({ path: testInfo.outputPath(`token-scope-${mode}.png`), scale: 'css' });
+ });
+}
+
+test('MH-MENU-COMPOSE-001: shared label tracks a nested light boundary', async ({ page }) => {
+ await page.goto('/e2e/model-catalog/fixture.html?backend=codex&tokens');
+ await page.evaluate(() => {
+ document.documentElement.setAttribute('data-theme', 'dark');
+ document.querySelector('[data-testid="token-scope-body"]')!.setAttribute('data-theme', 'light');
+ });
+ await guardContract(page);
+ await inkContract(page.getByTestId('token-scope-body').locator('.model-hub-guard-label'), true);
+});
+
+// These controls edit the emitted stylesheet in an isolated browser context.
+// They do not modify the checkout or implement selector/cascade evaluation.
+async function removeDeclaration(page: Page, property: string) {
+ return page.evaluate((name) => {
+ const removed: string[] = [];
+ const visit = (rules: CSSRuleList) => {
+ for (const rule of rules) {
+ if (rule instanceof CSSStyleRule && rule.style.getPropertyValue(name)) {
+ removed.push(rule.style.getPropertyValue(name));
+ rule.style.removeProperty(name);
+ }
+ if ('cssRules' in rule) visit((rule as CSSGroupingRule).cssRules);
+ }
+ };
+ for (const sheet of document.styleSheets) visit(sheet.cssRules);
+ if (!removed.length) throw new Error(`Control could not find ${name}`);
+ return removed;
+ }, property);
+}
+
+test('MH-MENU-COMPOSE-001: misplaced editor metric is observable after portalling', async ({ page }) => {
+ await page.goto('/e2e/model-catalog/fixture.html?backend=codex&tokens');
+ const editor = await openEditor(page);
+ await editorContract(editor);
+ const values = await removeDeclaration(page, '--model-hub-model-control-height');
+ expect(new Set(values)).toEqual(new Set(['40px']));
+ await page.addStyleTag({ content: '.model-hub-catalog-dialog { --model-hub-model-control-height: 40px; }' });
+ await expect(editor.locator('input.model-hub-model-control').first()).not.toHaveCSS('height', '40px');
+});
+
+test('MH-MENU-COMPOSE-001: missing guard declaration is observable at its consumer', async ({ page }) => {
+ await page.goto('/e2e/model-catalog/fixture.html?backend=codex&tokens');
+ await guardContract(page);
+ await removeDeclaration(page, '--model-hub-guard-count-padding-x');
+ await expect(page.getByTestId('token-scope-body').locator('.model-hub-guard-label > span').first()).toHaveCSS('padding', '0px');
+});
+
+for (const selector of [':where(.model-hub-model-control)', ':is(.model-hub-model-control, .unused-control)', '.model-hub-model-editor .model-hub-model-control']) {
+ test(`MH-MENU-COMPOSE-001: equivalent declaration ${selector} retains rendered geometry`, async ({ page }) => {
+ await page.goto('/e2e/model-catalog/fixture.html?backend=codex&tokens');
+ const editor = await openEditor(page);
+ await removeDeclaration(page, '--model-hub-model-control-height');
+ await page.addStyleTag({ content: `${selector} { --model-hub-model-control-height: 40px; }` });
+ await editorContract(editor);
+ });
+}
+
+test('MH-MENU-COMPOSE-001: a metric must be declared in the active media condition', async ({ page }) => {
+ await page.goto('/e2e/model-catalog/fixture.html?backend=codex&tokens');
+ const editor = await openEditor(page);
+ await editorContract(editor);
+ await removeDeclaration(page, '--model-hub-model-control-height');
+ const conditional = await page.addStyleTag({
+ content: '@media print { .model-hub-model-control { --model-hub-model-control-height: 40px; } }',
+ });
+ await expect(editor.locator('input.model-hub-model-control').first()).not.toHaveCSS('height', '40px');
+ await conditional.evaluate((node) => node.parentNode!.removeChild(node));
+ await page.addStyleTag({
+ content: '@media screen { @supports (height: 40px) { :where(.model-hub-model-control) { --model-hub-model-control-height: 40px; } } }',
+ });
+ await editorContract(editor);
+});
+
+test('MH-MENU-COMPOSE-001: inherited names with missing dependencies cannot supply a metric', async ({ page }) => {
+ await page.goto('/e2e/model-catalog/fixture.html?backend=codex&tokens');
+ const editor = await openEditor(page);
+ await editorContract(editor);
+ await removeDeclaration(page, '--model-hub-model-control-height');
+ await page.addStyleTag({ content: ':root { --model-hub-model-control-height: var(--scope-missing-height); }' });
+ await expect(editor.locator('input.model-hub-model-control').first()).not.toHaveCSS('height', '40px');
+ await page.addStyleTag({ content: ':root { --scope-missing-height: 40px; }' });
+ await editorContract(editor);
+});
+
+test('MH-MENU-COMPOSE-001: a root-frozen ink differs from the nested theme role', async ({ page }) => {
+ await page.goto('/e2e/model-catalog/fixture.html?backend=codex&tokens');
+ await page.evaluate(() => {
+ document.documentElement.setAttribute('data-theme', 'dark');
+ document.querySelector('[data-testid="token-scope-body"]')!.setAttribute('data-theme', 'light');
+ });
+ const label = page.getByTestId('token-scope-body').locator('.model-hub-guard-label').first();
+ await inkContract(label, true);
+ const before = await label.evaluate((node) => getComputedStyle(node).color);
+ await page.addStyleTag({ content: ':root { --scope-control-ink: var(--model-hub-ink-73); } .model-hub-guard-label { color: var(--scope-control-ink); }' });
+ await expect(label).not.toHaveCSS('color', before);
+ await inkContract(label, false);
+ // A local selector-list override restores the intended role.
+ await page.addStyleTag({ content: '[data-theme="light"], .scope-light { --scope-control-ink: var(--model-hub-ink-73); }' });
+ await inkContract(label, true);
+});
diff --git a/ui/e2e/model-catalog/tokenScopeFixture.tsx b/ui/e2e/model-catalog/tokenScopeFixture.tsx
new file mode 100644
index 000000000..ba91266f0
--- /dev/null
+++ b/ui/e2e/model-catalog/tokenScopeFixture.tsx
@@ -0,0 +1,14 @@
+import { GuardImpact } from '../../src/components/settings/models/GuardImpact';
+
+/** The shared body deliberately has no dialog ancestor. */
+export function TokenScopeFixture() {
+ return <section data-testid="token-scope-body">
+ <div className="model-hub-guard-body">
+ <GuardImpact
+ hops={[{ backend: 'codex', menu_model: '模型/gpt-test', source_id: 'src_test', model_id: 'gpt-test', position: 1 }]}
+ gaps={[{ backend: 'codex', model_id: '模型/gpt-test', agents: ['Release bot', '发布助手'] }]}
+ sourceNames={{ src_test: 'Provider A' }}
+ />
+ </div>
+ </section>;
+}