From 8c7704ae31e1ce7514c2a43a583000138b94ffd6 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Wed, 4 Nov 2026 21:10:17 -0600 Subject: [PATCH 1/2] fix(desktop): reduce background status polling load --- desktop/src/main/__tests__/watcher.test.ts | 74 +++++++++++++++++++++- desktop/src/main/watcher.ts | 60 +++++++++++++----- 2 files changed, 117 insertions(+), 17 deletions(-) diff --git a/desktop/src/main/__tests__/watcher.test.ts b/desktop/src/main/__tests__/watcher.test.ts index c9576194b..e09b650a8 100644 --- a/desktop/src/main/__tests__/watcher.test.ts +++ b/desktop/src/main/__tests__/watcher.test.ts @@ -15,6 +15,7 @@ function makeWatcher( runProviderList: () => Promise>, workspaces: Array<{ id: string; status?: string }> = [], runRaw: (args: string[]) => Promise = async () => ({}), + getMainWindow: () => unknown = () => null, ) { const state = { updateProviders: vi.fn().mockReturnValue(false), @@ -43,7 +44,7 @@ function makeWatcher( const watcher = new Watcher({ cli: cli as never, state: state as never, - getMainWindow: () => null, + getMainWindow: getMainWindow as never, providerJobs: providerJobs as never, workspaceJobs: workspaceJobs as never, }) @@ -531,3 +532,74 @@ describe("Watcher workspace list freshness", () => { expect(daemonList).toHaveBeenCalledExactlyOnceWith({ fresh: false }) }) }) + +describe("Watcher scheduled status polling", () => { + const visibleWindow = { + isDestroyed: () => false, + isVisible: () => true, + isMinimized: () => false, + } + const trayWindow = { ...visibleWindow, isVisible: () => false } + const scheduled = (watcher: Watcher) => + ( + watcher as unknown as { scheduleWorkspaceStatusPoll: () => void } + ).scheduleWorkspaceStatusPoll() + const settle = () => new Promise((resolve) => setTimeout(resolve, 10)) + + it("skips scheduled status polls while the window is hidden", async () => { + const { watcher, cli } = makeWatcher( + async () => ({}), + [{ id: "ws-1" }], + async () => JSON.stringify({ state: "running" }), + () => trayWindow, + ) + + scheduled(watcher) + await settle() + + expect(cli.runRaw).not.toHaveBeenCalled() + }) + + it("backs off a failing workspace on scheduled polls only", async () => { + const { watcher, cli } = makeWatcher( + async () => ({}), + [{ id: "ws-1" }], + async () => { + throw new Error("offline") + }, + () => visibleWindow, + ) + + scheduled(watcher) + await settle() + scheduled(watcher) + await settle() + expect(cli.runRaw).toHaveBeenCalledTimes(1) + + await watcher.refreshWorkspaceStatuses() + expect(cli.runRaw).toHaveBeenCalledTimes(2) + }) + + it("does not rerun immediately when a tick overlaps a running sweep", async () => { + let release: () => void = () => {} + const gate = new Promise((resolve) => { + release = resolve + }) + const { watcher, cli } = makeWatcher( + async () => ({}), + [{ id: "ws-1" }], + async () => { + await gate + return JSON.stringify({ state: "running" }) + }, + () => visibleWindow, + ) + + scheduled(watcher) + scheduled(watcher) + release() + await settle() + + expect(cli.runRaw).toHaveBeenCalledTimes(1) + }) +}) diff --git a/desktop/src/main/watcher.ts b/desktop/src/main/watcher.ts index df81d2746..251879c4f 100644 --- a/desktop/src/main/watcher.ts +++ b/desktop/src/main/watcher.ts @@ -12,6 +12,9 @@ import type { DaemonState, Machine, Workspace } from "./state.js" import type { WorkspaceJobs } from "./workspace-jobs.js" import { normalizeWorkspaceStatus } from "./workspace-status.js" +const WORKSPACE_POLL_INTERVAL_MS = 3000 +const WORKSPACE_STATUS_INTERVAL_MS = 10_000 +const STATUS_BACKOFF_MAX_MS = 300_000 interface WatcherDeps { cli: CliRunner daemon?: DaemonClient @@ -81,15 +84,17 @@ export class Watcher { private workspacePollChain: Promise = Promise.resolve() private workspaceStatusChain: Promise = Promise.resolve() private workspaceStatusPolling = false - private workspaceStatusQueued = false + private statusFailures = new Map() constructor(private deps: WatcherDeps) {} start(): Promise { - this.pollTimer = setInterval(() => this.schedulePoll(), 3000) + this.pollTimer = setInterval(() => { + if (this.windowActive()) this.schedulePoll() + }, WORKSPACE_POLL_INTERVAL_MS) this.workspaceStatusTimer = setInterval( () => this.scheduleWorkspaceStatusPoll(), - 10_000, + WORKSPACE_STATUS_INTERVAL_MS, ) const devsyDir = join(homedir(), ".devsy") @@ -116,7 +121,6 @@ export class Watcher { clearInterval(this.workspaceStatusTimer) this.workspaceStatusTimer = null } - this.workspaceStatusQueued = false if (this.fsWatcher) { this.fsWatcher.close() this.fsWatcher = null @@ -182,34 +186,61 @@ export class Watcher { await this.queueWorkspaceStatusPoll() } + private windowActive(): boolean { + const win = this.deps.getMainWindow() + return ( + win !== null && + !win.isDestroyed() && + win.isVisible() && + !win.isMinimized() + ) + } + private scheduleWorkspaceStatusPoll(): void { - if (this.workspaceStatusPolling) { - this.workspaceStatusQueued = true - return - } - void this.refreshWorkspaceStatuses() + if (this.workspaceStatusPolling || !this.windowActive()) return + this.workspaceStatusPolling = true + void this.queueWorkspaceStatusPoll(undefined, false, true) } private queueWorkspaceStatusPoll( workspaceIds?: string[], strict = false, + scheduled = false, ): Promise { const run = this.workspaceStatusChain.then(() => - this.pollWorkspaceStatuses(workspaceIds, strict), + this.pollWorkspaceStatuses(workspaceIds, strict, scheduled), ) this.workspaceStatusChain = run.catch(() => {}) return run } + private statusBackoffElapsed(id: string): boolean { + const failure = this.statusFailures.get(id) + return !failure || Date.now() >= failure.retryAt + } + + private recordStatusFailure(id: string): void { + const count = (this.statusFailures.get(id)?.count ?? 0) + 1 + const delay = Math.min( + STATUS_BACKOFF_MAX_MS, + WORKSPACE_STATUS_INTERVAL_MS * 2 ** count, + ) + this.statusFailures.set(id, { count, retryAt: Date.now() + delay }) + } + private async pollWorkspaceStatuses( workspaceIds?: string[], strict = false, + scheduled = false, ): Promise { this.workspaceStatusPolling = true let changed = false try { - const ids = + const requested = workspaceIds ?? this.deps.state.workspaceList().map((ws) => ws.id) + const ids = scheduled + ? requested.filter((id) => this.statusBackoffElapsed(id)) + : requested const concurrency = 6 let next = 0 const worker = async (): Promise => { @@ -234,12 +265,13 @@ export class Watcher { const status = normalizeWorkspaceStatus(raw) if (!status && strict) throw new Error("Workspace status unavailable") + if (status) this.statusFailures.delete(id) if (status && this.deps.state.updateWorkspaceStatus(id, status)) { changed = true } } catch (error) { if (strict) throw error - // Preserve the last known status and retry on the next sweep. + this.recordStatusFailure(id) } } } @@ -249,10 +281,6 @@ export class Watcher { if (changed) this.broadcastWorkspaces() } finally { this.workspaceStatusPolling = false - if (this.workspaceStatusQueued) { - this.workspaceStatusQueued = false - void this.refreshWorkspaceStatuses() - } } } From 9b30d47d2e313d5ce645d6aa2fc6897c976f7b76 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Wed, 4 Nov 2026 23:43:40 -0600 Subject: [PATCH 2/2] fix(desktop): keep job retries running and prune stale status backoff --- desktop/src/main/__tests__/watcher.test.ts | 42 ++++++++++++++++++++++ desktop/src/main/watcher.ts | 25 +++++++++---- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/desktop/src/main/__tests__/watcher.test.ts b/desktop/src/main/__tests__/watcher.test.ts index e09b650a8..2746728a5 100644 --- a/desktop/src/main/__tests__/watcher.test.ts +++ b/desktop/src/main/__tests__/watcher.test.ts @@ -580,6 +580,48 @@ describe("Watcher scheduled status polling", () => { expect(cli.runRaw).toHaveBeenCalledTimes(2) }) + it("resumes scheduled polls after the backoff expires", async () => { + const { watcher, cli } = makeWatcher( + async () => ({}), + [{ id: "ws-1" }], + async () => { + throw new Error("offline") + }, + () => visibleWindow, + ) + + scheduled(watcher) + await settle() + expect(cli.runRaw).toHaveBeenCalledTimes(1) + + const now = Date.now() + const nowSpy = vi.spyOn(Date, "now").mockReturnValue(now + 6 * 60_000) + scheduled(watcher) + await settle() + nowSpy.mockRestore() + + expect(cli.runRaw).toHaveBeenCalledTimes(2) + }) + + it("drops backoff for workspaces that leave the list", async () => { + const { watcher, cli } = makeWatcher( + async () => ({}), + [{ id: "ws-1" }], + async () => { + throw new Error("offline") + }, + () => visibleWindow, + ) + + scheduled(watcher) + await settle() + await watcher.refreshWorkspaces() + scheduled(watcher) + await settle() + + expect(cli.runRaw).toHaveBeenCalledTimes(2) + }) + it("does not rerun immediately when a tick overlaps a running sweep", async () => { let release: () => void = () => {} const gate = new Promise((resolve) => { diff --git a/desktop/src/main/watcher.ts b/desktop/src/main/watcher.ts index 251879c4f..25e452d12 100644 --- a/desktop/src/main/watcher.ts +++ b/desktop/src/main/watcher.ts @@ -91,6 +91,7 @@ export class Watcher { start(): Promise { this.pollTimer = setInterval(() => { if (this.windowActive()) this.schedulePoll() + else this.retryReconcilingJobs() }, WORKSPACE_POLL_INTERVAL_MS) this.workspaceStatusTimer = setInterval( () => this.scheduleWorkspaceStatusPoll(), @@ -144,12 +145,7 @@ export class Watcher { this.pollMachines(), this.pollContexts(), ]) - for (const [id, job] of Object.entries( - this.deps.workspaceJobs.snapshot(), - )) { - if (job.state === "reconciling" && job.refreshError) - void this.deps.workspaceJobs.retryRefresh(id) - } + this.retryReconcilingJobs() } finally { this.polling = false if (this.pollQueued) { @@ -159,6 +155,15 @@ export class Watcher { } } + private retryReconcilingJobs(): void { + for (const [id, job] of Object.entries( + this.deps.workspaceJobs.snapshot(), + )) { + if (job.state === "reconciling" && job.refreshError) + void this.deps.workspaceJobs.retryRefresh(id) + } + } + private async queryWithFallback( daemonFn: (() => Promise) | undefined, cliFn: () => Promise, @@ -310,6 +315,7 @@ export class Watcher { throw new Error("Workspace operation changed during refresh") return } + this.pruneStatusFailures(workspaces) const changed = this.deps.state.updateWorkspaces(workspaces) if (changed) { this.broadcastWorkspaces() @@ -322,6 +328,13 @@ export class Watcher { } } + private pruneStatusFailures(workspaces: Workspace[]): void { + const listed = new Set(workspaces.map((ws) => ws.id)) + for (const id of this.statusFailures.keys()) { + if (!listed.has(id)) this.statusFailures.delete(id) + } + } + /** * Push the workspace list plus in-flight job state. Sent on one channel so * the two can't arrive out of order and show a deleted-but-still-listed