diff --git a/packages/app/src/hooks/use-favicon-status.ts b/packages/app/src/hooks/use-favicon-status.ts index 4686b10ff..f6e8ac669 100644 --- a/packages/app/src/hooks/use-favicon-status.ts +++ b/packages/app/src/hooks/use-favicon-status.ts @@ -6,6 +6,7 @@ import { createMacDockBadgeCountSelector } from "@/utils/desktop-badge-state"; import { createFaviconStatusSelector, type FaviconStatus } from "@/utils/favicon-status"; import { isNative } from "@/constants/platform"; import { useSessionStore } from "@/stores/session-store"; +import { useSidebarViewStore } from "@/stores/sidebar-view-store"; import { useHosts } from "@/runtime/host-runtime"; type ColorScheme = "dark" | "light"; @@ -85,8 +86,16 @@ export function useFaviconStatus() { // The favicon reads the whole fleet, so it has to keep every host's directory loading even // though it no longer subscribes to the agent maps. useAllDirectoryDemand({ serverIds, enabled: !isNative }); - const selectFaviconStatus = useMemo(createFaviconStatusSelector, []); - const selectMacDockBadgeCount = useMemo(createMacDockBadgeCountSelector, []); + // Background workspaces count exactly when the sidebar shows them. + const showBackground = useSidebarViewStore((state) => state.showBackground); + const selectFaviconStatus = useMemo( + () => createFaviconStatusSelector({ showBackground }), + [showBackground], + ); + const selectMacDockBadgeCount = useMemo( + () => createMacDockBadgeCountSelector({ showBackground }), + [showBackground], + ); const status = useSessionStore(selectFaviconStatus); const dockBadgeCount = useSessionStore(selectMacDockBadgeCount); const [colorScheme, setColorScheme] = useState(getSystemColorScheme); diff --git a/packages/app/src/utils/desktop-badge-state.test.ts b/packages/app/src/utils/desktop-badge-state.test.ts index 69c747bcf..c02a66724 100644 --- a/packages/app/src/utils/desktop-badge-state.test.ts +++ b/packages/app/src/utils/desktop-badge-state.test.ts @@ -73,6 +73,18 @@ describe("desktop-badge-state", () => { expect(selectMacDockBadgeCount(state)).toBe(3); }); + it("leaves out background workspaces, which the sidebar hides", () => { + const hidden = { ...workspace("a", "attention"), background: true }; + expect(selectMacDockBadgeCount(stateWith(hidden))).toBeUndefined(); + expect(selectMacDockBadgeCount(stateWith(hidden, workspace("b", "failed")))).toBe(1); + }); + + it("counts background workspaces while Show background puts them in the sidebar", () => { + const hidden = { ...workspace("a", "attention"), background: true }; + expect(selectMacDockBadgeCount(stateWith(hidden), { showBackground: true })).toBe(1); + expect(createMacDockBadgeCountSelector({ showBackground: true })(stateWith(hidden))).toBe(1); + }); + it("recounts only when a workspace map is replaced", () => { const select = createMacDockBadgeCountSelector(); const state = stateWith(workspace("a", "attention")); diff --git a/packages/app/src/utils/desktop-badge-state.ts b/packages/app/src/utils/desktop-badge-state.ts index 94cf025b5..1888cb953 100644 --- a/packages/app/src/utils/desktop-badge-state.ts +++ b/packages/app/src/utils/desktop-badge-state.ts @@ -19,11 +19,21 @@ export function isWorkspaceActionableForDesktopBadge(status: DesktopBadgeWorkspa * The macOS dock badge only needs a count, so count here instead of materialising every * workspace status and comparing the whole list on every store write. */ -export function selectMacDockBadgeCount(state: WorkspaceStatusSource): number | undefined { +export interface DockBadgeOptions { + /** Count background workspaces too, as the sidebar shows them while Show background is on. */ + showBackground?: boolean; +} + +export function selectMacDockBadgeCount( + state: WorkspaceStatusSource, + options: DockBadgeOptions = {}, +): number | undefined { let actionableCount = 0; for (const session of Object.values(state.sessions)) { if (!session) continue; for (const workspace of session.workspaces.values()) { + // A hidden background workspace would leave the badge pointing at nothing in the sidebar. + if (workspace.background && !options.showBackground) continue; if (isWorkspaceActionableForDesktopBadge(workspace.status)) actionableCount += 1; } } @@ -31,11 +41,11 @@ export function selectMacDockBadgeCount(state: WorkspaceStatusSource): number | } /** A session-store selector for the dock badge count that recounts only when a workspace map changes. */ -export function createMacDockBadgeCountSelector(): ( - state: WorkspaceStatusSource, -) => number | undefined { +export function createMacDockBadgeCountSelector( + options: DockBadgeOptions = {}, +): (state: WorkspaceStatusSource) => number | undefined { return memoizeOnSessionMaps( - (session) => session.workspaces, - selectMacDockBadgeCount, + (session) => [session.workspaces], + (state) => selectMacDockBadgeCount(state, options), ); } diff --git a/packages/app/src/utils/favicon-status.test.ts b/packages/app/src/utils/favicon-status.test.ts index 6bb6a71d8..a2e26e2ac 100644 --- a/packages/app/src/utils/favicon-status.test.ts +++ b/packages/app/src/utils/favicon-status.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import type { Agent } from "@/stores/session-store"; +import type { Agent, WorkspaceDescriptor } from "@/stores/session-store"; import { createFaviconStatusSelector, deriveFaviconStatus } from "./favicon-status"; function agent(input: Partial & { id: string }): Agent { @@ -14,7 +14,21 @@ function agent(input: Partial & { id: string }): Agent { } function stateWith(...agents: Agent[]) { - return { sessions: { srv: { agents: new Map(agents.map((entry) => [entry.id, entry])) } } }; + return stateWithWorkspaces([], ...agents); +} + +function stateWithWorkspaces( + workspaces: Array>, + ...agents: Agent[] +) { + return { + sessions: { + srv: { + agents: new Map(agents.map((entry) => [entry.id, entry])), + workspaces: new Map(workspaces.map((entry) => [entry.id, entry])), + }, + }, + }; } describe("deriveFaviconStatus", () => { @@ -50,6 +64,37 @@ describe("deriveFaviconStatus", () => { ), ).toBe("none"); }); + + it("ignores agents in background workspaces, which the sidebar hides", () => { + const hidden = { id: "ws-hidden", background: true }; + expect( + deriveFaviconStatus( + stateWithWorkspaces( + [hidden], + agent({ id: "a", workspaceId: "ws-hidden", requiresAttention: true }), + agent({ id: "b", workspaceId: "ws-hidden", status: "running" }), + ), + ), + ).toBe("none"); + expect( + deriveFaviconStatus( + stateWithWorkspaces( + [hidden, { id: "ws-shown", background: false }], + agent({ id: "a", workspaceId: "ws-hidden", status: "running" }), + agent({ id: "b", workspaceId: "ws-shown", requiresAttention: true }), + ), + ), + ).toBe("attention"); + }); + + it("counts agents in background workspaces while Show background puts them in the sidebar", () => { + const state = stateWithWorkspaces( + [{ id: "ws-hidden", background: true }], + agent({ id: "a", workspaceId: "ws-hidden", requiresAttention: true }), + ); + expect(deriveFaviconStatus(state, { showBackground: true })).toBe("attention"); + expect(createFaviconStatusSelector({ showBackground: true })(state)).toBe("attention"); + }); }); describe("createFaviconStatusSelector", () => { @@ -63,6 +108,18 @@ describe("createFaviconStatusSelector", () => { expect(select({ sessions: { ...state.sessions } })).toBe("running"); }); + it("rescans when a workspace map is replaced", () => { + const select = createFaviconStatusSelector(); + const state = stateWithWorkspaces( + [], + agent({ id: "a", workspaceId: "ws-hidden", requiresAttention: true }), + ); + expect(select(state)).toBe("attention"); + // The agent synced before its workspace; only the workspace map changes. + const workspaces = new Map([["ws-hidden", { id: "ws-hidden", background: true }]]); + expect(select({ sessions: { srv: { ...state.sessions.srv, workspaces } } })).toBe("none"); + }); + it("rescans when an agent map is replaced", () => { const select = createFaviconStatusSelector(); expect(select(stateWith(agent({ id: "a", status: "running" })))).toBe("running"); diff --git a/packages/app/src/utils/favicon-status.ts b/packages/app/src/utils/favicon-status.ts index 62d1630ab..cea80b7f8 100644 --- a/packages/app/src/utils/favicon-status.ts +++ b/packages/app/src/utils/favicon-status.ts @@ -1,10 +1,11 @@ -import type { Agent } from "@/stores/session-store"; +import type { Agent, WorkspaceDescriptor } from "@/stores/session-store"; import { memoizeOnSessionMaps } from "./memoize-on-session-maps"; export type FaviconStatus = "none" | "running" | "attention"; interface FaviconAgentSession { agents: ReadonlyMap; + workspaces: ReadonlyMap>; } interface FaviconAgentSource { @@ -16,13 +17,22 @@ interface FaviconAgentSource { * three states here instead of building the aggregated agent list: the list allocates a * projection per agent, deep-compares it and sorts every agent on the host on each update. */ -export function deriveFaviconStatus(state: FaviconAgentSource): FaviconStatus { +export interface FaviconStatusOptions { + /** Count agents in background workspaces too, as the sidebar shows them while Show background is on. */ + showBackground?: boolean; +} + +export function deriveFaviconStatus( + state: FaviconAgentSource, + options: FaviconStatusOptions = {}, +): FaviconStatus { let hasAttention = false; let hasNeedsInput = false; for (const session of Object.values(state.sessions)) { if (!session) continue; for (const agent of session.agents.values()) { if (agent.background || agent.archivedAt) continue; + if (!options.showBackground && isInBackgroundWorkspace(agent, session)) continue; if (agent.status === "running") return "running"; if (agent.requiresAttention) hasAttention = true; if (agent.pendingPermissions.length > 0) hasNeedsInput = true; @@ -31,10 +41,16 @@ export function deriveFaviconStatus(state: FaviconAgentSource): FaviconStatus { return hasAttention || hasNeedsInput ? "attention" : "none"; } -/** A session-store selector for the favicon status that rescans only when an agent map changes. */ -export function createFaviconStatusSelector(): (state: FaviconAgentSource) => FaviconStatus { +function isInBackgroundWorkspace(agent: Agent, session: FaviconAgentSession): boolean { + return agent.workspaceId ? session.workspaces.get(agent.workspaceId)?.background === true : false; +} + +/** A session-store selector for the favicon status that rescans only when an agent or workspace map changes. */ +export function createFaviconStatusSelector( + options: FaviconStatusOptions = {}, +): (state: FaviconAgentSource) => FaviconStatus { return memoizeOnSessionMaps( - (session) => session.agents, - deriveFaviconStatus, + (session) => [session.agents, session.workspaces], + (state) => deriveFaviconStatus(state, options), ); } diff --git a/packages/app/src/utils/memoize-on-session-maps.ts b/packages/app/src/utils/memoize-on-session-maps.ts index ed621ee1c..5daeb5867 100644 --- a/packages/app/src/utils/memoize-on-session-maps.ts +++ b/packages/app/src/utils/memoize-on-session-maps.ts @@ -4,18 +4,18 @@ interface SessionsSource { /** * Wraps a whole-store selector so it reruns only when one of the per-session maps it reads has - * changed. Session-store writes that leave those maps alone (timeline updates, drafts) then cost + * changed. `pickMaps` returns every map the selector reads from one session. Session-store writes that leave those maps alone (timeline updates, drafts) then cost * a reference check per host instead of a walk over every entry. */ export function memoizeOnSessionMaps, TResult>( - pickMap: (session: TSession) => unknown, + pickMaps: (session: TSession) => readonly unknown[], derive: (state: TState) => TResult, ): (state: TState) => TResult { let previousMaps: unknown[] | null = null; let previousResult: TResult; return (state) => { - const maps = Object.values(state.sessions).map((session) => - session ? pickMap(session) : undefined, + const maps = Object.values(state.sessions).flatMap((session) => + session ? pickMaps(session) : [undefined], ); if ( previousMaps &&