Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

perf(ui): Spend is already read by the time it is opened

rubenluetke10-beep4d6d30b1a2bf
diff --git a/jarvis/ui/web/frontend/src/components/layout/MainView.tsx b/jarvis/ui/web/frontend/src/components/layout/MainView.tsx
index 5a6b59f941..7e4e880392 100644
--- a/jarvis/ui/web/frontend/src/components/layout/MainView.tsx
+++ b/jarvis/ui/web/frontend/src/components/layout/MainView.tsx
@@ -9,6 +9,11 @@ import {
} from "react";
import { QueryClientContext } from "@tanstack/react-query";
import { Loader2 } from "lucide-react";
+import {
+ costDailyQueryOptions,
+ costSummaryQueryOptions,
+ EMPTY_FILTERS,
+} from "@/hooks/useCosts";
import { overviewQueryOptions } from "@/hooks/useLocalModels";
import { readLocalModelsSeed } from "@/lib/localModelsSeed";
import { useEventStore } from "@/store/events";
@@ -207,6 +212,13 @@ function scheduleIdle(task: () => void): () => void {
* stall this is meant to remove. A failed prefetch is ignored — the chunk is
* simply fetched again on navigation, where Suspense handles it.
*/
+/**
+ * How long after mount the Spend warm-up runs. Past the boot burst — this app
+ * makes about thirty API calls before it settles — and well inside the time it
+ * takes anyone to read the home screen and reach for the sidebar.
+ */
+const SPEND_WARM_DELAY_MS = 2_500;
+
function useIdleViewPrefetch(): void {
// Optional on purpose: the shell renders without a QueryClient in some
// tests, and the data warm-up is a bonus, never a requirement.
@@ -240,9 +252,31 @@ function useIdleViewPrefetch(): void {
};
cancelSlot = scheduleIdle(pump);
+
+ // Spend, on its own clock rather than at the end of that queue.
+ //
+ // It is the one section whose first read is measured in a second rather
+ // than milliseconds — it aggregates every store the app writes plus the
+ // coding-CLI transcripts — so it is the one where arriving to a finished
+ // page instead of a loading one is the whole difference. Queued behind
+ // twenty module imports, each waiting for its own idle slot, it had not
+ // run twenty-five seconds into a fresh bundle. A plain timer is late
+ // enough to stay out of the boot burst and early enough to beat a click.
+ // The section opens on `EMPTY_FILTERS`, so these are the keys it asks for.
+ const warmSpend = window.setTimeout(() => {
+ if (cancelled || !queryClient) return;
+ void queryClient
+ .prefetchQuery(costSummaryQueryOptions(EMPTY_FILTERS))
+ .catch(() => undefined);
+ void queryClient
+ .prefetchQuery(costDailyQueryOptions(EMPTY_FILTERS))
+ .catch(() => undefined);
+ }, SPEND_WARM_DELAY_MS);
+
return () => {
cancelled = true;
cancelSlot?.();
+ window.clearTimeout(warmSpend);
};
}, [queryClient]);
}
diff --git a/jarvis/ui/web/frontend/src/hooks/useCosts.ts b/jarvis/ui/web/frontend/src/hooks/useCosts.ts
index 5262e9c3e9..e3c299ef4c 100644
--- a/jarvis/ui/web/frontend/src/hooks/useCosts.ts
+++ b/jarvis/ui/web/frontend/src/hooks/useCosts.ts
@@ -290,17 +290,40 @@ async function getJson<T>(url: string, signal?: AbortSignal): Promise<T> {
// Hooks
// ---------------------------------------------------------------------------
-export function useCostSummary(filters: CostFilters) {
+/**
+ * Options for the summary query — shared by `useCostSummary` and the idle
+ * prefetch in `MainView`, so the warm-up fills the very key the section then
+ * reads. Spelling the key twice is how a prefetch silently warms nothing.
+ */
+export function costSummaryQueryOptions(filters: CostFilters) {
const params = toParams(filters);
- return useQuery({
+ return {
queryKey: ["costs", "summary", params.toString()],
- queryFn: ({ signal }) => getJson<CostSummary>(`/api/costs/summary?${params.toString()}`, signal),
+ queryFn: ({ signal }: { signal?: AbortSignal }) =>
+ getJson<CostSummary>(`/api/costs/summary?${params.toString()}`, signal),
+ staleTime: 10_000,
+ };
+}
+
+/** The daily ledger's options, on the same terms as the summary's. */
+export function costDailyQueryOptions(filters: CostFilters) {
+ const params = toParams(filters);
+ return {
+ queryKey: ["costs", "daily", params.toString()],
+ queryFn: ({ signal }: { signal?: AbortSignal }) =>
+ getJson<CostDailyLedger>(`/api/costs/daily?${params.toString()}`, signal),
+ staleTime: 10_000,
+ };
+}
+
+export function useCostSummary(filters: CostFilters) {
+ return useQuery({
+ ...costSummaryQueryOptions(filters),
// Spend only moves when a turn finishes, and this is the most expensive
// read in the app. At thirty seconds a slow answer was still in flight
// when the next poll fired, so the section spent its whole life queueing
// behind itself.
refetchInterval: 120_000,
- staleTime: 10_000,
// Without this a filter click empties every table on the page while the
// new numbers are fetched. Both sibling queries already keep the old
// rows on screen; the summary is what the eye is actually on.
@@ -316,16 +339,12 @@ export function useCostSummary(filters: CostFilters) {
* screen.
*/
export function useCostDaily(filters: CostFilters) {
- const params = toParams(filters);
return useQuery({
- queryKey: ["costs", "daily", params.toString()],
- queryFn: ({ signal }) =>
- getJson<CostDailyLedger>(`/api/costs/daily?${params.toString()}`, signal),
// No poll of its own: this is the same underlying data as the summary,
// re-read whenever the filters change. Two independent polls over the
// two heaviest endpoints meant the section refetched everything twice
// per cycle for numbers that move only when a turn ends.
- staleTime: 10_000,
+ ...costDailyQueryOptions(filters),
placeholderData: (prev) => prev,
});
}