Elffuss BraidHub

utopia-ia/elffuss-claw public

coordinated: speed chip opens after the engine is chosen again, hides for other brains

web/js/velocimetro.js is untracked in the live tree (and byte-identical in
Elffuss Code's live tree, sha1 11cf89c5). This commit adds that file as it is
there plus the fix, so that the fix can be reviewed and tested here:
- The DOM listeners are attached once. montar() runs every time an engine
  brain is chosen and added one more click listener each time, so after
  choosing the engine again a click opened and closed the panel at once and
  it never opened.
- montar(null), or a brain that does not measure, cancels the subscription
  and hides the chip and the panel. The chip used to keep the engine's last
  figure after switching to Basic mode.
- The chip has aria-controls and aria-expanded; Escape closes the panel and
  returns focus to the chip; the panel is a labelled region.

kernel.js mounts it from changeModel() after every successful load (the
engine provider measures, any other brain gets null) and with null when going
to Basic mode, after a failed load, on "free the model" and when the memory
watchdog frees the model.

index.html (the chip and the panel) and the velocimeter CSS block are the
live tree's lines, unchanged, so they merge as identical changes.

tests/ux-velocimetro.mjs mounts a stand-in engine twice and checks that the
click opens the panel, aria, Escape and outside click, and that choosing
Basic mode hides the chip and unsubscribes. With the live, unfixed
velocimetro.js served in its place, the click does not open the panel.

Coordinated: when rebasing onto the live work, velocimetro.js is an add/add
conflict: keep this version (the live file plus the montar() and listener
hunks). The live kernel also mounts it in resolveProvider() for engine ids;
with this version a second montar() is harmless, and that call can go. The
live CSS block appended after the velocimeter rules (prefill bar, 44 px touch
targets) stays as it is.

Integration (onto stop-fix 7147228 / main 10073b7): main 66c2865 committed
the live velocimetro.js, so the add/add conflict takes this version (main's
file plus the montar() and listener hunks above), and elffuss.css keeps
main's prefill bar and touch-target rules after the velocimeter block. The
extra montar() in resolveProvider() for engine ids goes, as the note says:
changeModel() mounts it after every load and with null otherwise.
Changeset3e1357bd3b63c9b8c38d77b0981fcec22f47ccc7c77e6ff1266a4a672299f3c9
AuthorKikoCis <kikocisneros@gmail.com> · 2026-10-10 16:41 +02:00
CommitterKikoCis <kikocisneros@gmail.com> · 2026-10-11 17:49 +02:00
Parentsb7ab4d7e8303

Browse the files at this changeset

3 files changed

added tests/ux-velocimetro.mjs +58 −0
@@ -0,0 +1,58 @@
1+// Velocímetro (⚡ tok/s) con un motor de prueba (sin GPU): el que mide es el
2+// motor propio (alMedir/ultimaMedida), y aquí se le da uno de mentira.
3+// · Elegir el motor dos veces (montar dos veces) y el chip sigue abriendo el
4+// panel (antes cada montaje añadía otro clic y se cerraba al abrirse).
5+// · aria-expanded / aria-controls; Escape cierra y devuelve el foco; un clic
6+// fuera cierra.
7+// · Al pasar a otro cerebro el chip se esconde y deja de escuchar al motor
8+// (antes se quedaba con la última cifra).
9+//
10+// BASE=http://localhost:8743 node tests/ux-velocimetro.mjs
11+import { navegador, contexto, pagina, ok, hasta, fin } from './ux-comun.mjs';
12+
13+const b = await navegador();
14+const { ctx } = await contexto(b);
15+const { p, errores } = await pagina(ctx);
16+
17+ok('en Básico no hay velocímetro', await p.evaluate(() => document.getElementById('speed-chip').hidden));
18+await p.evaluate(async () => {
19+ const vm = await import('/js/velocimetro.js');
20+ const subs = new Set();
21+ window.__subs = subs;
22+ window.__motor = {
23+ alMedir: fn => { subs.add(fn); return () => subs.delete(fn); },
24+ ultimaMedida: () => ({ prefillTokS: 300, genTokS: 20, ttftMs: 900, genTokens: 50, procesados: 400, reusados: 0, enCurso: false }),
25+ };
26+ vm.montar(window.__motor);
27+ vm.montar(window.__motor); // elegir el motor otra vez
28+});
29+const chip = p.locator('#speed-chip');
30+ok('con el motor, el chip enseña la velocidad', !(await chip.isHidden()) && /20\.0 tok\/s/.test(await chip.innerText()));
31+ok('un solo oyente aunque se monte dos veces', await p.evaluate(() => window.__subs.size) === 1);
32+ok('aria-controls y aria-expanded', await chip.getAttribute('aria-controls') === 'speed-panel' && await chip.getAttribute('aria-expanded') === 'false');
33+await chip.click();
34+ok('tras montar dos veces, el clic ABRE el panel', await p.evaluate(() => !document.getElementById('speed-panel').hidden)
35+ && await chip.getAttribute('aria-expanded') === 'true');
36+await chip.click();
37+ok('y otro clic lo cierra', await p.evaluate(() => document.getElementById('speed-panel').hidden));
38+await chip.click();
39+await p.keyboard.press('Escape');
40+ok('Escape lo cierra y el foco vuelve al chip', await p.evaluate(() =>
41+ document.getElementById('speed-panel').hidden && document.activeElement?.id === 'speed-chip'));
42+await chip.click();
43+await p.click('#log');
44+ok('un clic fuera lo cierra', await p.evaluate(() => document.getElementById('speed-panel').hidden));
45+await chip.click();
46+
47+// pasar a otro cerebro (Básico) por el selector, como el usuario
48+await p.evaluate(() => {
49+ const s = document.getElementById('model-select');
50+ s.value = 'rules';
51+ s.dispatchEvent(new Event('change'));
52+});
53+ok('al pasar a otro cerebro el chip y el panel se esconden', await hasta(() => p.evaluate(() =>
54+ document.getElementById('speed-chip').hidden && document.getElementById('speed-panel').hidden)));
55+ok('…y deja de escuchar al motor', await p.evaluate(() => window.__subs.size) === 0);
56+
57+ok('sin errores de página', errores.length === 0, errores.slice(0, 3).join(' | '));
58+await fin(b, 'VELOCÍMETRO: abre aunque se monte dos veces, Escape, y se esconde con otro cerebro');
modified web/js/kernel.js +15 −7
@@ -55,13 +55,9 @@
5555 // changeModel lo trata como cualquier otro fallo de carga.
5656 const mod = await import('./engine/provider.js');
5757 mod.configure(id.slice(7));
58- // El velocímetro se engancha AQUÍ y no al arrancar la app: solo el motor
59- // propio mide tok/s (`alMedir`), así que con un proveedor externo o con
60- // ONNX el chip no tiene nada que decir y se queda oculto.
61- try {
62- const vm = await import('./velocimetro.js');
63- vm.montar(mod);
64- } catch (e) { console.warn('[claw] velocímetro no disponible:', e.message); }
58+ // El velocímetro lo monta changeModel() tras cargar (montarVelocimetro):
59+ // solo el motor propio mide tok/s (`alMedir`), y con cualquier otro cerebro
60+ // se monta con null y el chip se esconde.
6561 return mod;
6662 }
6763 if (id.startsWith('ext:')) {
@@ -700,11 +696,19 @@
700696 recargar();
701697 });
702698
699+// El velocímetro (⚡ tok/s) solo tiene algo que decir con el motor propio, que
700+// es quien mide (`alMedir`). Con cualquier otro cerebro se monta con null y el
701+// chip se esconde: antes se quedaba con la última cifra del motor a la vista.
702+function montarVelocimetro(mod) {
703+ import('./velocimetro.js').then(vm => vm.montar(mod?.alMedir ? mod : null)).catch(() => { /* sin velocímetro la app funciona igual */ });
704+}
705+
703706 async function changeModel(id) {
704707 if (id === 'rules') { // modo básico
705708 agent.setProvider(rules);
706709 activeModel = 'rules';
707710 activeMod = null;
711+ montarVelocimetro(null);
708712 ui.modelStatus('off');
709713 localStorage.setItem('elffuss.model', 'rules');
710714 ui.setModel('rules');
@@ -725,6 +729,7 @@
725729 agent.setProvider(mod);
726730 activeModel = id;
727731 activeMod = mod;
732+ montarVelocimetro(mod);
728733 localStorage.setItem('elffuss.model', id);
729734 ui.setModel(id);
730735 const where = isLocal(id) ? (realGPU ? t('whereGpu') : t('whereCpu')) : t('whereExt');
@@ -747,6 +752,7 @@
747752 ui.modelStatus('off');
748753 agent.setProvider(rules);
749754 activeModel = 'rules';
755+ montarVelocimetro(null);
750756 ui.setModel('rules');
751757 ui.toast('⚠️ No se pudo cargar ' + id + ': ' + (e?.message || String(e)));
752758 return false;
@@ -786,6 +792,7 @@
786792 ui.setOnFreeModel(async () => {
787793 const mod = activeMod;
788794 agent.setProvider(rules); activeModel = 'rules'; activeMod = null;
795+ montarVelocimetro(null);
789796 ui.setModel('rules'); ui.modelStatus('off');
790797 try { await mod?.unload?.(); } catch { /* mejor esfuerzo */ }
791798 });
@@ -1015,6 +1022,7 @@
10151022 agent.setProvider(rules);
10161023 activeModel = 'rules';
10171024 activeMod = null;
1025+ montarVelocimetro(null);
10181026 ui.setModel('rules');
10191027 ui.modelStatus('off');
10201028 try { await mod?.unload?.(); } catch { /* mejor esfuerzo */ }
modified web/js/velocimetro.js +49 −14
@@ -134,15 +134,61 @@
134134 if (panel && !panel.hidden) pintarPanel();
135135 }
136136
137-/** Engancha el velocímetro. `provider` es el módulo del motor, o null si no hay. */
137+// Abrir y cerrar el panel. aria-expanded en el chip dice si está abierto; al
138+// cerrarlo con Escape el foco vuelve al chip.
139+function abrirPanel(abrir) {
140+ if (!panel || !chip) return;
141+ panel.hidden = !abrir;
142+ chip.setAttribute('aria-expanded', abrir ? 'true' : 'false');
143+ if (abrir) pintarPanel();
144+}
145+
146+// Los escuchadores del DOM se ponen UNA vez. montar() se llama cada vez que se
147+// elige un cerebro del motor, y antes añadía otro clic al chip en cada llamada:
148+// tras volver a elegir el motor, un clic abría y el siguiente escuchador lo
149+// cerraba en el mismo instante, y el panel ya no se abría nunca.
150+let cableado = false;
151+function cablear() {
152+ if (cableado || !chip) return;
153+ cableado = true;
154+ chip.setAttribute('aria-controls', panel?.id || 'speed-panel');
155+ chip.setAttribute('aria-expanded', 'false');
156+ if (panel) {
157+ panel.setAttribute('role', 'region');
158+ panel.setAttribute('aria-label', 'Velocidad de esta máquina');
159+ }
160+ chip.addEventListener('click', () => abrirPanel(!!panel?.hidden));
161+ // Cerrar al pulsar fuera, como el resto de los desplegables de la cabecera.
162+ document.addEventListener('click', e => {
163+ if (!panel || panel.hidden) return;
164+ if (e.target === chip || chip?.contains(e.target) || panel.contains(e.target)) return;
165+ abrirPanel(false);
166+ });
167+ // …y con Escape, devolviendo el foco al chip.
168+ document.addEventListener('keydown', e => {
169+ if (e.key !== 'Escape' || !panel || panel.hidden) return;
170+ abrirPanel(false);
171+ chip?.focus();
172+ });
173+}
174+
175+/** Engancha el velocímetro. `provider` es el módulo del motor, o null si no hay.
176+ * Con null (o un cerebro que no mide) el chip y el panel se ESCONDEN: antes,
177+ * al pasar a otro cerebro, el chip seguía enseñando la última cifra del motor. */
138178 export function montar(provider, chipId = 'speed-chip', panelId = 'speed-panel') {
139179 prov = provider || null;
140180 chip = document.getElementById(chipId);
141181 panel = document.getElementById(panelId);
142182 leer();
143- if (!chip || !prov?.alMedir) { if (chip) chip.hidden = true; return; }
144-
183+ cablear();
145184 cancelar?.();
185+ cancelar = null;
186+ if (!chip || !prov?.alMedir) {
187+ if (chip) chip.hidden = true;
188+ abrirPanel(false);
189+ return;
190+ }
191+
146192 cancelar = prov.alMedir(m => {
147193 pintarChip(m);
148194 // El historial se apunta UNA vez por turno, al cerrarse: apuntando en cada
@@ -155,17 +201,6 @@
155201 if (panel && !panel.hidden) pintarPanel();
156202 });
157203
158- chip.addEventListener('click', () => {
159- if (!panel) return;
160- panel.hidden = !panel.hidden;
161- if (!panel.hidden) pintarPanel();
162- });
163- // Cerrar al pulsar fuera, como el resto de los desplegables de la cabecera.
164- document.addEventListener('click', e => {
165- if (!panel || panel.hidden) return;
166- if (e.target === chip || panel.contains(e.target)) return;
167- panel.hidden = true;
168- });
169204 const m = prov.ultimaMedida?.();
170205 if (m) pintarChip(m); else chip.hidden = true;
171206 }