diff --git a/apps/web/src/components/sidebar/SidebarThreadFilterMenu.tsx b/apps/web/src/components/sidebar/SidebarThreadFilterMenu.tsx index 18c3a06a4db74..89bd5c89aab81 100644 --- a/apps/web/src/components/sidebar/SidebarThreadFilterMenu.tsx +++ b/apps/web/src/components/sidebar/SidebarThreadFilterMenu.tsx @@ -28,12 +28,14 @@ import { import { SidebarHeaderIconButton } from "./SidebarThreadHeader"; import { changedThreadListFilters, + EMPTY_THREAD_LIST_FILTER_BUCKETS, inboxMenuOrder, threadListFilterCounts, threadListFilterOf, toggleFilterOption, toggleHiddenEnvironment, type ThreadListFilter, + type ThreadListFilterBucketCounts, } from "./threadListFilter.logic"; const INBOX_LABELS: Record = { @@ -114,7 +116,7 @@ function SidebarThreadFilterOptions({ isChanged: boolean; }) { const connectedEnvironmentIds = useConnectedEnvironmentIds(); - const counts = threadListFilterCounts(countedThreads(), filter, attributionOf); + const counts = threadListFilterCounts(countedThreads(), filter, attributionOf, owner); const shownAttributions = THREAD_ATTRIBUTIONS.filter( (from) => !filter.hiddenAttributions.includes(from), ); @@ -130,13 +132,13 @@ function SidebarThreadFilterOptions({ {inboxOrder.map((inbox) => { const checked = filter.inboxes.includes(inbox); const isLast = checked && filter.inboxes.length === 1; - const count = counts.inboxes.get(inbox) ?? 0; + const count = counts.inboxes.get(inbox) ?? EMPTY_THREAD_LIST_FILTER_BUCKETS; return ( void updateClientSettings({ founderInboxFilter: toggleFilterOption(filter.inboxes, inbox, inboxOrder), @@ -154,13 +156,13 @@ function SidebarThreadFilterOptions({ {THREAD_ATTRIBUTIONS.map((from) => { const checked = shownAttributions.includes(from); const isLast = checked && shownAttributions.length === 1; - const count = counts.from.get(from) ?? 0; + const count = counts.from.get(from) ?? EMPTY_THREAD_LIST_FILTER_BUCKETS; return ( { const shown = toggleFilterOption(shownAttributions, from, THREAD_ATTRIBUTIONS); void updateClientSettings({ @@ -184,13 +186,15 @@ function SidebarThreadFilterOptions({ const checked = shownEnvironmentIds.includes(environment.environmentId); const isLast = checked && shownEnvironmentIds.length === 1; const connected = connectedEnvironmentIds.includes(environment.environmentId); - const count = counts.environments.get(environment.environmentId) ?? 0; + const count = + counts.environments.get(environment.environmentId) ?? + EMPTY_THREAD_LIST_FILTER_BUCKETS; return ( void updateClientSettings({ sidebarHiddenEnvironmentIds: toggleHiddenEnvironment( @@ -240,20 +244,52 @@ function SidebarThreadFilterOptions({ ); } +const STATE_COUNT_COLUMNS = [ + ["working", "working"], + ["done", "done"], + ["needsMe", "needs me"], +] as const satisfies ReadonlyArray; + +function stateCountPhrase(counts: ThreadListFilterBucketCounts) { + return STATE_COUNT_COLUMNS.map(([key, label]) => `${counts[key]} ${label}`).join(", "); +} + function FilterOptionRow({ label, count, indicator, }: { label: string; - count: number; + count: ThreadListFilterBucketCounts; indicator?: ReactNode; }) { return ( {indicator} {label} - {count} + + {STATE_COUNT_COLUMNS.map(([key, name]) => { + const value = count[key]; + return ( + + {value} + {name} + + ); + })} + ); } diff --git a/apps/web/src/components/sidebar/threadListFilter.logic.test.ts b/apps/web/src/components/sidebar/threadListFilter.logic.test.ts index b47e842b49fe0..65d32f9f23ff3 100644 --- a/apps/web/src/components/sidebar/threadListFilter.logic.test.ts +++ b/apps/web/src/components/sidebar/threadListFilter.logic.test.ts @@ -5,6 +5,7 @@ import { changedThreadListFilters, inboxMenuOrder, matchesThreadListFilter, + threadListFilterBucket, threadListFilterCounts, threadListFilterOf, toggleFilterOption, @@ -42,21 +43,31 @@ describe("thread list filter", () => { }); it("counts each section within the other sections' selection", () => { + const finished = { runtime: { status: "completed" }, latestRun: { status: "completed" } }; const threads = [ - { ...thread(laptop, kameron), from: "kameron" as const }, - { ...thread(laptop, bot), from: "kameron-agents" as const }, - { ...thread(crown, jeff), from: "jeff" as const }, - { ...thread(crown, kameron), from: "kameron" as const }, - { ...thread(laptop, kameron), from: "unknown" as const }, + { ...thread(laptop, kameron), ...finished, from: "kameron" as const }, + { ...thread(laptop, bot), ...finished, from: "kameron-agents" as const }, + { ...thread(crown, jeff), ...finished, from: "jeff" as const }, + { ...thread(crown, kameron), ...finished, from: "kameron" as const }, + { ...thread(laptop, kameron), ...finished, from: "unknown" as const }, ]; const counts = threadListFilterCounts( threads, { inboxes: ["kameron"], hiddenEnvironmentIds: [crown], hiddenAttributions: ["unknown"] }, (candidate) => candidate.from, ); - expect(Object.fromEntries(counts.inboxes)).toEqual({ kameron: 1, agents: 1 }); - expect(Object.fromEntries(counts.environments)).toEqual({ laptop: 1, crown: 1 }); - expect(Object.fromEntries(counts.from)).toEqual({ kameron: 1, unknown: 1 }); + expect(Object.fromEntries(counts.inboxes)).toEqual({ + kameron: { working: 0, done: 1, needsMe: 0 }, + agents: { working: 0, done: 1, needsMe: 0 }, + }); + expect(Object.fromEntries(counts.environments)).toEqual({ + laptop: { working: 0, done: 1, needsMe: 0 }, + crown: { working: 0, done: 1, needsMe: 0 }, + }); + expect(Object.fromEntries(counts.from)).toEqual({ + kameron: { working: 0, done: 1, needsMe: 0 }, + unknown: { working: 0, done: 1, needsMe: 0 }, + }); expect( matchesThreadListFilter( threads[4]!, @@ -107,4 +118,144 @@ describe("thread list filter", () => { expect(toggleHiddenEnvironment([offline, crown], crown, [laptop, crown])).toEqual([offline]); expect(toggleHiddenEnvironment([crown], laptop, [laptop, crown])).toEqual([crown]); }); + + it("buckets working, done and needs-me the same way on every row", () => { + const finished = { runtime: { status: "completed" }, latestRun: { status: "completed" } }; + const quiet = { + ...thread(laptop, kameron), + source: { + inbox: makeThreadInbox({ + actor: kameron, + routing: { attentionKind: "FYI", reason: "fyi" }, + }), + }, + }; + const asked = { + ...quiet, + source: { + inbox: { + ...quiet.source.inbox, + decisionRequest: { + founder: "kameron" as const, + kind: "decision" as const, + question: "Ship it?", + requestedAt: "2026-11-20T00:00:00.000Z", + }, + }, + }, + }; + const attention = { + ...thread(laptop, bot), + ...finished, + source: { + inbox: makeThreadInbox({ + actor: bot, + routing: { + owner: "kameron", + assignee: { kind: "human", id: "kameron" }, + audience: "kameron", + attentionKind: "action", + reason: "review", + }, + }), + }, + }; + const otherAsk = { + ...thread(laptop, jeff), + ...finished, + source: { + inbox: { + ...makeThreadInbox({ + actor: jeff, + routing: { attentionKind: "FYI", reason: "fyi" }, + }), + decisionRequest: { + founder: "jeff" as const, + kind: "decision" as const, + question: "Your call?", + requestedAt: "2026-11-20T00:00:00.000Z", + }, + }, + }, + }; + const fixtures = { + queued: { + ...quiet, + runtime: { status: "queued" }, + latestRun: { status: "queued" }, + }, + running: { + ...quiet, + runtime: { status: "running" }, + latestRun: { status: "running" }, + }, + done: { ...quiet, ...finished }, + failed: { + ...quiet, + runtime: { status: "failed" }, + latestRun: { status: "failed" }, + }, + approval: { + ...quiet, + runtime: { status: "running" }, + latestRun: { status: "running" }, + hasPendingApprovals: true, + }, + question: { + ...quiet, + runtime: { status: "waiting" }, + latestRun: { status: "waiting" }, + hasPendingUserInput: true, + }, + decision: { ...asked, ...finished }, + attention, + parked: { + ...quiet, + runtime: { status: "idle" }, + latestRun: { status: "completed" }, + }, + parkedAttention: { + ...attention, + runtime: { status: "idle" }, + latestRun: { status: "completed" }, + }, + settled: { ...quiet, ...finished, settledOverride: "settled" as const }, + otherFounder: otherAsk, + unstarted: quiet, + }; + expect(threadListFilterBucket(fixtures.queued, "kameron")).toBe("working"); + expect(threadListFilterBucket(fixtures.running, "kameron")).toBe("working"); + expect(threadListFilterBucket(fixtures.done, "kameron")).toBe("done"); + expect(threadListFilterBucket(fixtures.failed, "kameron")).toBe("done"); + expect(threadListFilterBucket(fixtures.approval, "kameron")).toBe("needsMe"); + expect(threadListFilterBucket(fixtures.question, "kameron")).toBe("needsMe"); + expect(threadListFilterBucket(fixtures.decision, "kameron")).toBe("needsMe"); + expect(threadListFilterBucket(fixtures.attention, "kameron")).toBe("needsMe"); + expect(threadListFilterBucket(fixtures.parked, "kameron")).toBe("working"); + expect(threadListFilterBucket(fixtures.parkedAttention, "kameron")).toBe("working"); + expect(threadListFilterBucket(fixtures.settled, "kameron")).toBeNull(); + expect(threadListFilterBucket(fixtures.otherFounder, "kameron")).toBe("done"); + expect(threadListFilterBucket(fixtures.unstarted, "kameron")).toBeNull(); + + const threads = Object.values(fixtures).map((candidate) => ({ + ...candidate, + from: "kameron" as const, + })); + const counts = threadListFilterCounts( + threads, + { + inboxes: ["kameron", "jeff", "agents", "shared"], + hiddenEnvironmentIds: [], + hiddenAttributions: [], + }, + (candidate) => candidate.from, + "kameron", + ); + const kameronRow = { working: 4, done: 2, needsMe: 4 }; + const everyRow = { working: 4, done: 3, needsMe: 4 }; + expect(counts.inboxes.get("kameron")).toEqual(kameronRow); + expect(counts.inboxes.get("jeff")).toEqual({ working: 0, done: 1, needsMe: 0 }); + expect(counts.environments.get(laptop)).toEqual(everyRow); + expect(counts.from.get("kameron")).toEqual(everyRow); + }); }); diff --git a/apps/web/src/components/sidebar/threadListFilter.logic.ts b/apps/web/src/components/sidebar/threadListFilter.logic.ts index 6991999624709..2ebaad0a2f2cb 100644 --- a/apps/web/src/components/sidebar/threadListFilter.logic.ts +++ b/apps/web/src/components/sidebar/threadListFilter.logic.ts @@ -4,6 +4,7 @@ import { } from "@t3tools/client-runtime/state/thread-list-filter"; import { effectiveInboxFilter, + founderInboxLane, Founder, InboxChoice, primaryInbox, @@ -20,12 +21,94 @@ export { interface FilterableThread { readonly environmentId: EnvironmentId; + readonly settledOverride?: "settled" | "active" | null; + readonly hasPendingApprovals?: boolean; + readonly hasPendingUserInput?: boolean; + readonly pendingSubagentRequest?: "approval" | "user_input" | null; + readonly runtime?: { readonly status: string } | null; + readonly latestRun?: { readonly status: string } | null; readonly source: { readonly inbox?: ThreadInbox | null | undefined; readonly createdBy?: string | undefined; }; } +const ACTIVE_RUN_STATUSES: ReadonlySet = new Set([ + "preparing", + "queued", + "starting", + "running", + "waiting", +]); + +const FINISHED_RUN_STATUSES: ReadonlySet = new Set([ + "completed", + "interrupted", + "failed", + "cancelled", + "rolled_back", +]); + +export type ThreadListFilterBucket = "working" | "done" | "needsMe"; + +export interface ThreadListFilterBucketCounts { + readonly working: number; + readonly done: number; + readonly needsMe: number; +} + +export const EMPTY_THREAD_LIST_FILTER_BUCKETS: ThreadListFilterBucketCounts = { + working: 0, + done: 0, + needsMe: 0, +}; + +const activityStatus = (thread: FilterableThread) => + thread.runtime?.status ?? thread.latestRun?.status ?? null; + +const waitsOnHuman = (thread: FilterableThread, viewingFounder: Founder | undefined) => { + if ( + thread.hasPendingApprovals === true || + thread.hasPendingUserInput === true || + thread.pendingSubagentRequest != null + ) { + return true; + } + const inbox = thread.source.inbox; + if (inbox?.pendingHumanRequest === true) { + if (viewingFounder === undefined || inbox.owner === viewingFounder || inbox.owner === null) { + return true; + } + } + const ask = inbox?.decisionRequest; + return ask !== undefined && (viewingFounder === undefined || ask.founder === viewingFounder); +}; + +const attentionForFounder = (thread: FilterableThread, viewingFounder: Founder | undefined) => { + if (viewingFounder === undefined) return false; + const inbox = thread.source.inbox; + return inbox?.attentionKind === "action" && founderInboxLane(inbox, viewingFounder) === "action"; +}; + +export function threadListFilterBucket( + thread: FilterableThread, + viewingFounder?: Founder, +): ThreadListFilterBucket | null { + if (thread.settledOverride === "settled") return null; + const status = activityStatus(thread); + if (waitsOnHuman(thread, viewingFounder)) return "needsMe"; + if (thread.runtime?.status === "idle") return "working"; + if (status !== null && ACTIVE_RUN_STATUSES.has(status)) return "working"; + if (attentionForFounder(thread, viewingFounder)) return "needsMe"; + if ( + (status !== null && FINISHED_RUN_STATUSES.has(status)) || + (thread.latestRun != null && FINISHED_RUN_STATUSES.has(thread.latestRun.status)) + ) { + return "done"; + } + return null; +} + const matchesInboxes = (thread: FilterableThread, inboxes: ReadonlyArray) => matchesListedInboxes( primaryInbox(thread.source.inbox), @@ -75,8 +158,14 @@ export function changedThreadListFilters( }; } -const increment = (counts: Map, key: K) => - counts.set(key, (counts.get(key) ?? 0) + 1); +const addBucket = ( + counts: Map, + key: K, + bucket: ThreadListFilterBucket | null, +) => { + const current = counts.get(key) ?? EMPTY_THREAD_LIST_FILTER_BUCKETS; + counts.set(key, bucket === null ? current : { ...current, [bucket]: current[bucket] + 1 }); +}; /** * Faceted counts: each section counts threads that pass the other two sections, so a count @@ -86,22 +175,24 @@ export function threadListFilterCounts( threads: ReadonlyArray, filter: ThreadListFilter, attributionOf: (thread: T) => ThreadAttribution, + viewingFounder?: Founder, ) { - const inboxes = new Map(); - const environments = new Map(); - const from = new Map(); + const inboxes = new Map(); + const environments = new Map(); + const from = new Map(); for (const thread of threads) { + const bucket = threadListFilterBucket(thread, viewingFounder); const attribution = attributionOf(thread); const environmentShown = !filter.hiddenEnvironmentIds.includes(thread.environmentId); const fromShown = !filter.hiddenAttributions.includes(attribution); const inboxShown = matchesInboxes(thread, filter.inboxes); if (environmentShown && fromShown) { for (const choice of InboxChoice.literals) { - if (matchesInboxes(thread, [choice])) increment(inboxes, choice); + if (matchesInboxes(thread, [choice])) addBucket(inboxes, choice, bucket); } } - if (inboxShown && fromShown) increment(environments, thread.environmentId); - if (inboxShown && environmentShown) increment(from, attribution); + if (inboxShown && fromShown) addBucket(environments, thread.environmentId, bucket); + if (inboxShown && environmentShown) addBucket(from, attribution, bucket); } return { inboxes, environments, from }; }