Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Drop the unreachable settings-open refetch, cover the rest

danielhanchen4cc15d0a174e
diff --git a/studio/frontend/src/features/profile/hooks/use-profile-stats.ts b/studio/frontend/src/features/profile/hooks/use-profile-stats.ts
index 35f8ae566ee..d4a22105098 100644
--- a/studio/frontend/src/features/profile/hooks/use-profile-stats.ts
+++ b/studio/frontend/src/features/profile/hooks/use-profile-stats.ts
@@ -3,7 +3,6 @@
import { useCallback, useEffect, useRef, useState } from "react";
import { subscribeResidentStatusRefresh } from "@/features/hub/lib/resident-status-refresh";
-import { useSettingsDialogStore } from "@/features/settings/stores/settings-dialog-store";
import { type ProfileStats, loadProfileStats } from "../api/profile-stats";
type ProfileStatsState = {
@@ -45,18 +44,13 @@ export function useProfileStats(): ProfileStatsState {
return () => abortRef.current?.abort();
}, [load]);
+ // Coming back to the tab is when the numbers can have moved: the user chatted
+ // in another window, or an API client spent tokens while this one sat open.
+ // Reopening Settings needs no watcher of its own -- the dialog content is not
+ // force-mounted, so the panel unmounts on close and the effect above refetches
+ // when it mounts again.
useEffect(() => subscribeResidentStatusRefresh(load), [load]);
- const settingsOpen = useSettingsDialogStore((state) => state.open);
- const sawSettingsOpenRef = useRef(settingsOpen);
- useEffect(() => {
- const wasOpen = sawSettingsOpenRef.current;
- sawSettingsOpenRef.current = settingsOpen;
- if (!wasOpen && settingsOpen) {
- void load();
- }
- }, [settingsOpen, load]);
-
const reload = useCallback(() => {
void load();
}, [load]);
diff --git a/studio/frontend/tests/profile-stats-format.test.ts b/studio/frontend/tests/profile-stats-format.test.ts
index a66fc44a6bf..82dd4dbb5c5 100644
--- a/studio/frontend/tests/profile-stats-format.test.ts
+++ b/studio/frontend/tests/profile-stats-format.test.ts
@@ -5,6 +5,7 @@ import assert from "node:assert/strict";
import test from "node:test";
import {
+ type ActivityGridColumn,
activitySummaryForMode,
formatCompactNumber,
formatDayCount,
@@ -210,6 +211,65 @@ test("activity summaries follow the selected chart mode", () => {
assert.equal(activitySummaryForMode([], "daily"), 0);
});
+test("daily and cumulative still total the window, as the card always did", () => {
+ // The helper replaced an inline reduce over the same grid. If these two modes
+ // ever stop matching it, every existing subtitle silently changes number, and
+ // nothing else in the suite would notice.
+ const legacyVisibleTotal = (grid: ActivityGridColumn[]) =>
+ grid.reduce(
+ (sum, column) =>
+ column.reduce((total, cell) => total + (cell.day?.tokens ?? 0), sum),
+ 0,
+ );
+
+ let seed = 20260908;
+ const random = () => {
+ seed = (seed * 1664525 + 1013904223) >>> 0;
+ return seed / 2 ** 32;
+ };
+ for (let trial = 0; trial < 200; trial += 1) {
+ const grid: ActivityGridColumn[] = Array.from(
+ { length: 1 + Math.floor(random() * 55) },
+ () =>
+ Array.from({ length: 7 }, () =>
+ random() < 0.3
+ ? { day: null }
+ : { day: { tokens: Math.floor(random() * 50_000) } },
+ ),
+ );
+ const expected = legacyVisibleTotal(grid);
+ assert.equal(activitySummaryForMode(grid, "daily"), expected);
+ assert.equal(activitySummaryForMode(grid, "cumulative"), expected);
+ // A peak week is one column, so it can never exceed the whole window.
+ assert.ok(activitySummaryForMode(grid, "weekly") <= expected);
+ }
+});
+
+test("the shapes a real grid actually takes summarise without NaN", () => {
+ // Every column is padded to a Monday-started week, so null cells are the norm
+ // rather than an edge case, and the final column is short.
+ const allPadding = [Array.from({ length: 7 }, () => ({ day: null }))];
+ const ragged = [
+ [
+ { day: { tokens: 1 } },
+ { day: { tokens: 2 } },
+ { day: { tokens: 3 } },
+ { day: { tokens: 4 } },
+ { day: { tokens: 5 } },
+ { day: { tokens: 6 } },
+ { day: { tokens: 7 } },
+ ],
+ [{ day: { tokens: 100 } }, { day: { tokens: 200 } }],
+ ];
+
+ for (const mode of ["daily", "weekly", "cumulative"] as const) {
+ assert.equal(activitySummaryForMode(allPadding, mode), 0);
+ assert.ok(!Number.isNaN(activitySummaryForMode(ragged, mode)));
+ }
+ assert.equal(activitySummaryForMode(ragged, "daily"), 328);
+ assert.equal(activitySummaryForMode(ragged, "weekly"), 300);
+});
+
test("series modes reshape the same daily data", () => {
// 2026-03-02 is a Monday, so this spans exactly two calendar weeks.
const daily = [
diff --git a/studio/frontend/tests/profile-stats-refresh.test.ts b/studio/frontend/tests/profile-stats-refresh.test.ts
new file mode 100644
index 00000000000..1fe92da4a32
--- /dev/null
+++ b/studio/frontend/tests/profile-stats-refresh.test.ts
@@ -0,0 +1,124 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import assert from "node:assert/strict";
+import { readFileSync } from "node:fs";
+import test from "node:test";
+
+import { subscribeResidentStatusRefresh } from "../src/features/hub/lib/resident-status-refresh.ts";
+
+const HOOK = readFileSync(
+ new URL("../src/features/profile/hooks/use-profile-stats.ts", import.meta.url),
+ "utf8",
+);
+
+type Listener = () => void;
+
+function fakeTargets(hidden = false) {
+ const windowListeners = new Map<string, Set<Listener>>();
+ const documentListeners = new Map<string, Set<Listener>>();
+ const state = { hidden };
+ const add = (map: Map<string, Set<Listener>>) => (type: string, fn: Listener) => {
+ if (!map.has(type)) map.set(type, new Set());
+ map.get(type)?.add(fn);
+ };
+ const remove = (map: Map<string, Set<Listener>>) => (type: string, fn: Listener) => {
+ map.get(type)?.delete(fn);
+ };
+ return {
+ windowListeners,
+ documentListeners,
+ setHidden: (value: boolean) => {
+ state.hidden = value;
+ },
+ fire: (map: Map<string, Set<Listener>>, type: string) => {
+ for (const fn of [...(map.get(type) ?? [])]) fn();
+ },
+ targets: {
+ window: {
+ addEventListener: add(windowListeners) as never,
+ removeEventListener: remove(windowListeners) as never,
+ },
+ document: {
+ addEventListener: add(documentListeners) as never,
+ removeEventListener: remove(documentListeners) as never,
+ get hidden() {
+ return state.hidden;
+ },
+ },
+ },
+ };
+}
+
+test("the profile stats hook refreshes when the tab returns to the foreground", () => {
+ // Source-asserted, like tests/chat-speech-model-not-adopted.test.ts does for
+ // the chat runtime: the wiring is one line and a renderer is not worth it, but
+ // dropping the line would silently restore the stale-stats bug.
+ assert.match(HOOK, /subscribeResidentStatusRefresh/);
+ assert.match(
+ HOOK,
+ /useEffect\(\(\)\s*=>\s*subscribeResidentStatusRefresh\(load\), \[load\]\)/,
+ "the subscription must be RETURNED from the effect, so React uses its " +
+ "unsubscribe as the cleanup",
+ );
+});
+
+test("the hook does not watch the settings dialog store", () => {
+ // The panel lives inside a DialogContent that is not force-mounted, so it
+ // unmounts on close and the mount effect already refetches on reopen. A
+ // closed->open watcher here would be unreachable code plus a dependency from
+ // features/profile on features/settings.
+ assert.doesNotMatch(HOOK, /useSettingsDialogStore/);
+});
+
+test("the refresh subscription hands back a working unsubscribe", () => {
+ // The contract the effect above depends on. If this ever returned void, every
+ // reopen of Settings would leave another listener behind and one focus would
+ // fire a request per listener.
+ const harness = fakeTargets();
+ let calls = 0;
+ const stop = subscribeResidentStatusRefresh(() => {
+ calls += 1;
+ }, harness.targets);
+ assert.equal(typeof stop, "function");
+
+ harness.fire(harness.windowListeners, "focus");
+ assert.equal(calls, 1);
+
+ stop();
+ harness.fire(harness.windowListeners, "focus");
+ harness.fire(harness.documentListeners, "visibilitychange");
+ assert.equal(calls, 1, "a listener survived unmount");
+});
+
+test("a tab going hidden does not refetch; coming back does", () => {
+ const harness = fakeTargets(true);
+ let calls = 0;
+ const stop = subscribeResidentStatusRefresh(() => {
+ calls += 1;
+ }, harness.targets);
+
+ harness.fire(harness.documentListeners, "visibilitychange");
+ assert.equal(calls, 0, "refetched while the tab was hidden");
+
+ harness.setHidden(false);
+ harness.fire(harness.documentListeners, "visibilitychange");
+ assert.equal(calls, 1);
+ stop();
+});
+
+test("mounting and unmounting repeatedly leaves no listeners behind", () => {
+ // Opening and closing Settings twenty times should not make one focus fire
+ // twenty requests.
+ const harness = fakeTargets();
+ let calls = 0;
+ const stops = Array.from({ length: 20 }, () =>
+ subscribeResidentStatusRefresh(() => {
+ calls += 1;
+ }, harness.targets),
+ );
+ for (const stop of stops) stop();
+
+ harness.fire(harness.windowListeners, "focus");
+ assert.equal(calls, 0);
+});