Skip to content

The agent that grows with you

Desktop stops polling local-models status every 2 seconds when idle (#129638)

Siddharth Balyan70d02a1017fd
diff --git a/apps/desktop/src/app/settings/local-models-settings.tsx b/apps/desktop/src/app/settings/local-models-settings.tsx
index 4a1d28f5d03..1adaba9210a 100644
--- a/apps/desktop/src/app/settings/local-models-settings.tsx
+++ b/apps/desktop/src/app/settings/local-models-settings.tsx
@@ -37,7 +37,7 @@ export function LocalModelsSettings(): ReactElement {
function ScopedLocalModelsSettings(): ReactElement {
const owner: LocalModelsOwner = useScopedLocalModelsOwner()
const installStarting: boolean = useIsMutating({ mutationKey: localModelsKey(owner, 'install') }) > 0
- const { data: status } = useLocalModelsStatus(owner)
+ const { data: status } = useLocalModelsStatus(owner, true, true)
const { data: hardware } = useQuery(localModelsHardwareOptions(owner))
const { data: catalog } = useQuery(localModelsCatalogOptions(owner))
// Quickstart escape hatch: true once the user asks for the full pane
diff --git a/apps/desktop/src/app/shell/model-catalog-menu.tsx b/apps/desktop/src/app/shell/model-catalog-menu.tsx
index 0229b8c8348..a39a6da4fe6 100644
--- a/apps/desktop/src/app/shell/model-catalog-menu.tsx
+++ b/apps/desktop/src/app/shell/model-catalog-menu.tsx
@@ -242,7 +242,7 @@ export function ModelCatalogMenu({
// (it unmounts on close); errors read as "nothing loading" — remote-only
// installs have no local-models routes.
const owner: LocalModelsOwner = useLocalModelsOwner(profile, ownerConnectionId)
- const localStatus = useLocalModelsStatus(owner, localModelsEnabled)
+ const localStatus = useLocalModelsStatus(owner, localModelsEnabled, true)
const loadingModels: Record<string, LocalModelLoadProgress> = localStatus.data?.loading ?? {}
diff --git a/apps/desktop/src/components/assistant-ui/thread/status.tsx b/apps/desktop/src/components/assistant-ui/thread/status.tsx
index 4148f4b8124..cc84c744aa0 100644
--- a/apps/desktop/src/components/assistant-ui/thread/status.tsx
+++ b/apps/desktop/src/components/assistant-ui/thread/status.tsx
@@ -21,7 +21,7 @@ import { $localModelsEnabled } from '@/store/local-models-flag'
import { sessionAwaitingInput } from '@/store/prompts'
import { parseModelLoadWait, sessionProviderWait } from '@/store/provider-wait'
import { $showReasoning } from '@/store/reasoning-disclosure'
-import { $currentModel } from '@/store/session'
+import { $currentModel, $currentProvider } from '@/store/session'
import { type DraftingTool, sessionDraftingTool } from '@/store/tool-drafting'
import type { LocalModelLoadProgress } from '@/types/hermes'
@@ -57,6 +57,9 @@ const HintText: FC<{ children: ReactNode }> = ({ children }) => (
<span className={cn(SCAFFOLD_LABEL_CLASS, 'shimmer min-w-0 flex-1 truncate')}>{children}</span>
)
+// Provider slug of the managed local server (inventory.py's _local_runtime_row).
+const LOCAL_PROVIDER_SLUG = 'llamacpp'
+
/** Renderer-side load synthesis: poll the local-models status while a turn
* is busy with NO progress frame from the backend. The backend's wait loop
* only narrates the MAIN chat request — a model load triggered while the
@@ -66,11 +69,13 @@ const HintText: FC<{ children: ReactNode }> = ({ children }) => (
* the same SSE snapshot, so this bar carries the identical percent. */
function useLocalModelLoad(active: boolean): (LocalModelLoadProgress & { model: string }) | null {
const model = useStore($currentModel)
+ const provider = useStore($currentProvider)
const [progress, setProgress] = useState<(LocalModelLoadProgress & { model: string }) | null>(null)
- // Behind the --local launch flag: without it, no status polling and no
- // load bar (the local server can't be the current provider anyway).
- const enabled = $localModelsEnabled.get()
+ // Behind the --local launch flag, and only while the managed local server
+ // is the current provider: a remote model never waits on a local load, so
+ // busy turns on it must not poll.
+ const enabled = $localModelsEnabled.get() && provider === LOCAL_PROVIDER_SLUG
useEffect(() => {
if (!enabled || !active || !model) {
diff --git a/apps/desktop/src/components/model-picker.tsx b/apps/desktop/src/components/model-picker.tsx
index 785787d60ba..84e0cdf4ed8 100644
--- a/apps/desktop/src/components/model-picker.tsx
+++ b/apps/desktop/src/components/model-picker.tsx
@@ -95,7 +95,7 @@ export function ModelPickerDialog({
const localModelsEnabled = $localModelsEnabled.get()
const owner: LocalModelsOwner = useLocalModelsOwner(profile, ownerConnectionId)
- const localStatus = useLocalModelsStatus(owner, open && localModelsEnabled)
+ const localStatus = useLocalModelsStatus(owner, open && localModelsEnabled, true)
const loadingModels: Record<string, LocalModelLoadProgress> = localStatus.data?.loading ?? {}
diff --git a/apps/desktop/src/store/local-runtime-jobs.ts b/apps/desktop/src/store/local-runtime-jobs.ts
index 71173c20172..871df342cb7 100644
--- a/apps/desktop/src/store/local-runtime-jobs.ts
+++ b/apps/desktop/src/store/local-runtime-jobs.ts
@@ -106,15 +106,23 @@ export function localModelsStatusOptions(owner: LocalModelsOwner): UseQueryOptio
})
}
+const STATUS_POLL_MS = 2_000
+
interface StatusWatch {
observer: QueryObserver<LocalModelsStatus>
+ liveUsers: number
users: number
}
const statusWatches: WeakMap<QueryClient, Map<string, StatusWatch>> = new WeakMap()
+// One cached read per owner. It polls only while a `live` consumer (the
+// Local Models page, an open picker) is mounted or the server itself reports
+// a model loading; job completions and owner changes re-read through
+// refreshLocalModels, so idle sessions stay quiet.
export function useLocalModelsStatus(
owner: LocalModelsOwner,
- enabled: boolean = true
+ enabled: boolean = true,
+ live: boolean = false
): UseQueryResult<LocalModelsStatus> {
const client: QueryClient = useQueryClient()
const result: UseQueryResult<LocalModelsStatus> = useQuery({ ...localModelsStatusOptions(owner), enabled: false })
@@ -134,28 +142,43 @@ export function useLocalModelsStatus(
let watch: StatusWatch | undefined = watches.get(key)
if (!watch) {
- const observer: QueryObserver<LocalModelsStatus> = new QueryObserver<LocalModelsStatus>(client, {
+ const created: StatusWatch = { liveUsers: 0, observer: undefined as never, users: 0 }
+
+ created.observer = new QueryObserver<LocalModelsStatus>(client, {
...localModelsStatusOptions(owner),
- refetchInterval: 2_000
+ refetchInterval: (query): number | false =>
+ created.liveUsers > 0 || Object.keys(query.state.data?.loading ?? {}).length > 0 ? STATUS_POLL_MS : false
})
-
- watch = { observer, users: 0 }
+ watch = created
watches.set(key, watch)
- observer.subscribe((): void => {})
+ created.observer.subscribe((): void => {})
}
const acquired: StatusWatch = watch
acquired.users += 1
+ if (live) {
+ acquired.liveUsers += 1
+ acquired.observer.setOptions(acquired.observer.options)
+
+ if (Date.now() - acquired.observer.getCurrentQuery().state.dataUpdatedAt >= STATUS_POLL_MS) {
+ void acquired.observer.refetch()
+ }
+ }
+
return (): void => {
acquired.users -= 1
+ if (live) {
+ acquired.liveUsers -= 1
+ }
+
if (acquired.users === 0) {
acquired.observer.destroy()
watches.delete(key)
}
}
- }, [client, owner, enabled])
+ }, [client, owner, enabled, live])
return result
}