diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsx index fc47b59ee670b..182eec0612df3 100644 --- a/apps/web/src/components/Sidebar.tsx +++ b/apps/web/src/components/Sidebar.tsx @@ -1,5 +1,9 @@ import { SidebarThreadFilterMenu } from "./sidebar/SidebarThreadFilterMenu"; -import { matchesThreadListFilter, threadListFilterOf } from "./sidebar/threadListFilter.logic"; +import { + matchesThreadListFilter, + olderSettledFilterCounts, + threadListFilterOf, +} from "./sidebar/threadListFilter.logic"; import { launchingThreadIdOf, resolveMachineOwners, @@ -359,6 +363,7 @@ const EMPTY_PROVIDER_ENTRIES: ReadonlyMap = new Ma const EMPTY_THREADS: readonly EnvironmentThreadShell[] = []; const SETTLED_TAIL_INITIAL_COUNT = 10; +const NO_THREAD_KEYS: ReadonlySet = new Set(); const SETTLED_TAIL_PAGE_COUNT = 25; const SETTLED_SHELF_EXPANDED_KEY = "t3code:sidebar:settled-expanded"; const SNOOZED_SHELF_EXPANDED_KEY = "t3code:sidebar:snoozed-expanded"; @@ -3112,11 +3117,15 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve - filterSidebarV2VisibleThreads( - threads, - scopedProjectKeys, - delegatedUnderParent, - ).filter((thread) => thread.settledOverride !== "settled") + filterSidebarV2VisibleThreads(threads, scopedProjectKeys, NO_THREAD_KEYS) + } + olderSettled={() => + olderSettledFilterCounts( + olderSettledScopes([...serverConfigs.keys()], scopedProjectKeys), + (environmentId) => + olderSettledShelf.environments.get(environmentId)?.older ?? null, + machineOwners, + ) } /> } diff --git a/apps/web/src/components/sidebar/SidebarThreadFilterMenu.tsx b/apps/web/src/components/sidebar/SidebarThreadFilterMenu.tsx index ac4716755cb33..73de563cdba85 100644 --- a/apps/web/src/components/sidebar/SidebarThreadFilterMenu.tsx +++ b/apps/web/src/components/sidebar/SidebarThreadFilterMenu.tsx @@ -31,6 +31,7 @@ import { changedThreadListFilters, EMPTY_THREAD_LIST_FILTER_BUCKETS, inboxMenuOrder, + type olderSettledFilterCounts, threadListFilterCounts, threadListFilterOf, toggleFilterOption, @@ -46,16 +47,19 @@ const INBOX_LABELS: Record = { shared: "Shared", }; -type CountedThread = Parameters[0][number]; +type CountedThread = Parameters[0][number] & + Parameters["covers"]>[0]; const LAST_OPTION_NOTE = ", the last one shown, so it stays checked"; /** Chooses which inboxes and environments the thread list shows. Search still finds every thread. */ export function SidebarThreadFilterMenu({ countedThreads, + olderSettled, attributionOf, }: { /** Called only while the menu is open. */ countedThreads: () => ReadonlyArray; + olderSettled: () => ReturnType; attributionOf: (thread: T) => ThreadAttribution; }) { const filter = useClientSettings(threadListFilterOf); @@ -86,9 +90,10 @@ export function SidebarThreadFilterMenu({ }> 0 && "text-primary")} /> - + ({ // Mounted only while the menu is open, so counting never runs on streaming ticks. function SidebarThreadFilterOptions({ countedThreads, + olderSettled, attributionOf, filter, owner, @@ -110,6 +116,7 @@ function SidebarThreadFilterOptions({ isChanged, }: { countedThreads: () => ReadonlyArray; + olderSettled: () => ReturnType; attributionOf: (thread: T) => ThreadAttribution; filter: ThreadListFilter; owner: Founder | undefined; @@ -117,11 +124,19 @@ function SidebarThreadFilterOptions({ isChanged: boolean; }) { const connectedEnvironmentIds = useConnectedEnvironmentIds(); - const counts = threadListFilterCounts(countedThreads(), filter, attributionOf, owner); + const inboxOrder = inboxMenuOrder(owner); + const older = olderSettled(); + const counts = threadListFilterCounts( + countedThreads().filter((thread) => !older.covers(thread)), + filter, + attributionOf, + owner, + older.groups, + ); + const allInboxesShown = inboxOrder.every((inbox) => filter.inboxes.includes(inbox)); const shownAttributions = THREAD_ATTRIBUTIONS.filter( (from) => !filter.hiddenAttributions.includes(from), ); - const inboxOrder = inboxMenuOrder(owner); const environmentIds = environments.map((environment) => environment.environmentId); const shownEnvironmentIds = environmentIds.filter( (id) => !filter.hiddenEnvironmentIds.includes(id), @@ -225,6 +240,16 @@ function SidebarThreadFilterOptions({ ) : null} + +

+ {!allInboxesShown && counts.unclassified > 0 + ? `${counts.unclassified} thread${counts.unclassified === 1 ? " has" : "s have"} no inbox and ${counts.unclassified === 1 ? "shows" : "show"} only with every inbox checked. ` + : null} + {older.lowerBound + ? "Some servers send settled totals only, so settled counts are minimums. " + : null} + Subagent threads open from their parent and are not counted. +

{isChanged ? ( <> @@ -249,6 +274,7 @@ const STATE_COUNT_COLUMNS = [ ["working", "working"], ["done", "done"], ["needsMe", "needs me"], + ["settled", "settled"], ] as const satisfies ReadonlyArray; function stateCountPhrase(counts: ThreadListFilterBucketCounts) { @@ -268,7 +294,7 @@ function FilterOptionRow({ {indicator} {label} - + {STATE_COUNT_COLUMNS.map(([key, name]) => { const value = count[key]; return ( @@ -282,7 +308,9 @@ function FilterOptionRow({ ? "text-info-foreground" : key === "done" ? "text-success-foreground" - : "text-warning-foreground", + : key === "needsMe" + ? "text-warning-foreground" + : "text-muted-foreground", )} > {value} diff --git a/apps/web/src/components/sidebar/threadListFilter.logic.test.ts b/apps/web/src/components/sidebar/threadListFilter.logic.test.ts index 65d32f9f23ff3..ca4170986265c 100644 --- a/apps/web/src/components/sidebar/threadListFilter.logic.test.ts +++ b/apps/web/src/components/sidebar/threadListFilter.logic.test.ts @@ -1,10 +1,12 @@ -import { EnvironmentId, makeThreadInbox } from "@t3tools/contracts"; +import { EnvironmentId, ProjectId, ThreadId, makeThreadInbox } from "@t3tools/contracts"; +import * as DateTime from "effect/DateTime"; import { describe, expect, it } from "vite-plus/test"; import { changedThreadListFilters, inboxMenuOrder, matchesThreadListFilter, + olderSettledFilterCounts, threadListFilterBucket, threadListFilterCounts, threadListFilterOf, @@ -57,16 +59,16 @@ describe("thread list filter", () => { (candidate) => candidate.from, ); expect(Object.fromEntries(counts.inboxes)).toEqual({ - kameron: { working: 0, done: 1, needsMe: 0 }, - agents: { working: 0, done: 1, needsMe: 0 }, + kameron: { working: 0, done: 1, needsMe: 0, settled: 0 }, + agents: { working: 0, done: 1, needsMe: 0, settled: 0 }, }); expect(Object.fromEntries(counts.environments)).toEqual({ - laptop: { working: 0, done: 1, needsMe: 0 }, - crown: { working: 0, done: 1, needsMe: 0 }, + laptop: { working: 0, done: 1, needsMe: 0, settled: 0 }, + crown: { working: 0, done: 1, needsMe: 0, settled: 0 }, }); expect(Object.fromEntries(counts.from)).toEqual({ - kameron: { working: 0, done: 1, needsMe: 0 }, - unknown: { working: 0, done: 1, needsMe: 0 }, + kameron: { working: 0, done: 1, needsMe: 0, settled: 0 }, + unknown: { working: 0, done: 1, needsMe: 0, settled: 0 }, }); expect( matchesThreadListFilter( @@ -233,7 +235,7 @@ describe("thread list filter", () => { 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.settled, "kameron")).toBe("settled"); expect(threadListFilterBucket(fixtures.otherFounder, "kameron")).toBe("done"); expect(threadListFilterBucket(fixtures.unstarted, "kameron")).toBeNull(); @@ -251,11 +253,95 @@ describe("thread list filter", () => { (candidate) => candidate.from, "kameron", ); - const kameronRow = { working: 4, done: 2, needsMe: 4 }; - const everyRow = { working: 4, done: 3, needsMe: 4 }; + const kameronRow = { working: 4, done: 2, needsMe: 4, settled: 1 }; + const everyRow = { working: 4, done: 3, needsMe: 4, settled: 1 }; expect(counts.inboxes.get("kameron")).toEqual(kameronRow); - expect(counts.inboxes.get("jeff")).toEqual({ working: 0, done: 1, needsMe: 0 }); + expect(counts.inboxes.get("jeff")).toEqual({ working: 0, done: 1, needsMe: 0, settled: 0 }); expect(counts.environments.get(laptop)).toEqual(everyRow); expect(counts.from.get("kameron")).toEqual(everyRow); }); + + it("counts older settled threads from the servers' groups instead of the loaded rows", () => { + const beforeMs = Date.parse("2026-11-18T00:00:00.000Z"); + const lineage = { + relationshipToParent: null, + parentThreadId: null, + rootThreadId: ThreadId.make("root"), + }; + const settledRow = (environmentId: EnvironmentId, settledAt: string) => ({ + ...thread(environmentId, kameron), + settledOverride: "settled" as const, + source: { + ...thread(environmentId, kameron).source, + lineage, + settledOverride: "settled" as const, + settledAt: DateTime.makeUnsafe(settledAt), + }, + }); + const group = (inbox: "kameron" | "jeff" | null, count: number, agentCreated = false) => ({ + inbox, + agentCreated, + founder: "kameron" as const, + byAgent: false, + count, + }); + const project = ProjectId.make("app"); + const other = ProjectId.make("other"); + const older = new Map([ + [ + laptop, + { + before: new Date(beforeMs).toISOString(), + beforeMs, + counts: new Map([ + [project, 55], + [other, 9], + ]), + groups: new Map([ + [ + project, + [group("kameron", 40), group("jeff", 7), group(null, 5), group(null, 3, true)], + ], + [other, [group("kameron", 9)]], + ]), + }, + ], + [ + crown, + { + before: new Date(beforeMs).toISOString(), + beforeMs, + counts: new Map([[project, 12]]), + groups: null, + }, + ], + ]); + const source = olderSettledFilterCounts( + [ + { environmentId: laptop, projectIds: [project] }, + { environmentId: crown, projectIds: null }, + ], + (environmentId) => older.get(environmentId) ?? null, + {}, + ); + expect(source.lowerBound).toBe(true); + const loaded = [ + settledRow(laptop, "2026-11-19T00:00:00.000Z"), + settledRow(laptop, "2026-11-17T00:00:00.000Z"), + settledRow(crown, "2026-11-17T00:00:00.000Z"), + ]; + const counts = threadListFilterCounts( + loaded.filter((row) => !source.covers(row)), + threadListFilterOf({ founderInboxOwner: "kameron" }), + () => "kameron" as const, + "kameron", + source.groups, + ); + expect(counts.inboxes.get("kameron")?.settled).toBe(1 + 40 + 1); + expect(counts.inboxes.get("jeff")?.settled).toBe(7); + expect(counts.inboxes.get("agents")?.settled).toBe(3); + expect(counts.environments.get(laptop)?.settled).toBe(1 + 40 + 3); + expect(counts.environments.get(crown)?.settled).toBe(1); + expect(counts.unclassified).toBe(5); + }); }); diff --git a/apps/web/src/components/sidebar/threadListFilter.logic.ts b/apps/web/src/components/sidebar/threadListFilter.logic.ts index 2ebaad0a2f2cb..dd2bc6b3fc368 100644 --- a/apps/web/src/components/sidebar/threadListFilter.logic.ts +++ b/apps/web/src/components/sidebar/threadListFilter.logic.ts @@ -2,13 +2,20 @@ import { matchesListedInboxes, type ThreadListFilter, } from "@t3tools/client-runtime/state/thread-list-filter"; +import { + attributionOfSender, + type FounderByEnvironmentId, +} from "@t3tools/client-runtime/state/thread-attribution"; +import type { OlderSettled, OlderSettledScope } from "@t3tools/client-runtime/state/threads"; import { effectiveInboxFilter, + isOlderSettledThreadShell, founderInboxLane, Founder, InboxChoice, primaryInbox, type EnvironmentId, + type OrchestrationV2ThreadShell, type ThreadInbox, } from "@t3tools/contracts"; import type { ThreadAttribution } from "@t3tools/contracts/settings"; @@ -49,20 +56,75 @@ const FINISHED_RUN_STATUSES: ReadonlySet = new Set([ "rolled_back", ]); -export type ThreadListFilterBucket = "working" | "done" | "needsMe"; +export type ThreadListFilterBucket = "working" | "done" | "needsMe" | "settled"; export interface ThreadListFilterBucketCounts { readonly working: number; readonly done: number; readonly needsMe: number; + readonly settled: number; } export const EMPTY_THREAD_LIST_FILTER_BUCKETS: ThreadListFilterBucketCounts = { working: 0, done: 0, needsMe: 0, + settled: 0, }; +export interface OlderSettledGroupCount { + readonly environmentId: EnvironmentId; + readonly inbox: InboxChoice | null; + readonly agentCreated: boolean; + readonly attribution: ThreadAttribution; + readonly count: number; +} + +export function olderSettledFilterCounts( + scopes: ReadonlyArray, + olderOf: (environmentId: EnvironmentId) => OlderSettled | null, + owners: FounderByEnvironmentId, +) { + const grouped = scopes.flatMap((scope) => { + const older = olderOf(scope.environmentId); + return older?.groups == null ? [] : [{ scope, older, groups: older.groups }]; + }); + const groups: ReadonlyArray = grouped.flatMap(({ scope, groups }) => + (scope.projectIds ?? [...groups.keys()]).flatMap((projectId) => + (groups.get(projectId) ?? []).map((group) => ({ + environmentId: scope.environmentId, + inbox: group.inbox, + agentCreated: group.agentCreated, + attribution: attributionOfSender(group, owners[scope.environmentId]), + count: group.count, + })), + ), + ); + const lowerBound = scopes.some((scope) => { + const older = olderOf(scope.environmentId); + if (older === null || older.groups !== null) return false; + return (scope.projectIds ?? [...older.counts.keys()]).some( + (projectId) => (older.counts.get(projectId) ?? 0) > 0, + ); + }); + return { + groups, + lowerBound, + covers: (thread: { + readonly environmentId: EnvironmentId; + readonly source: Pick< + OrchestrationV2ThreadShell, + "lineage" | "settledOverride" | "settledAt" + >; + }) => + grouped.some( + ({ scope, older }) => + scope.environmentId === thread.environmentId && + isOlderSettledThreadShell(thread.source, older.beforeMs), + ), + }; +} + const activityStatus = (thread: FilterableThread) => thread.runtime?.status ?? thread.latestRun?.status ?? null; @@ -94,7 +156,7 @@ export function threadListFilterBucket( thread: FilterableThread, viewingFounder?: Founder, ): ThreadListFilterBucket | null { - if (thread.settledOverride === "settled") return null; + if (thread.settledOverride === "settled") return "settled"; const status = activityStatus(thread); if (waitsOnHuman(thread, viewingFounder)) return "needsMe"; if (thread.runtime?.status === "idle") return "working"; @@ -109,13 +171,6 @@ export function threadListFilterBucket( return null; } -const matchesInboxes = (thread: FilterableThread, inboxes: ReadonlyArray) => - matchesListedInboxes( - primaryInbox(thread.source.inbox), - thread.source.createdBy === "agent", - inboxes, - ); - /** The chosen founder's inbox first, then Agents, so the common pair sits together. */ export const inboxMenuOrder = (owner: Founder | undefined): ReadonlyArray => { const first = owner ?? Founder.literals[0]; @@ -162,9 +217,10 @@ const addBucket = ( counts: Map, key: K, bucket: ThreadListFilterBucket | null, + amount: number, ) => { const current = counts.get(key) ?? EMPTY_THREAD_LIST_FILTER_BUCKETS; - counts.set(key, bucket === null ? current : { ...current, [bucket]: current[bucket] + 1 }); + counts.set(key, bucket === null ? current : { ...current, [bucket]: current[bucket] + amount }); }; /** @@ -176,25 +232,54 @@ export function threadListFilterCounts( filter: ThreadListFilter, attributionOf: (thread: T) => ThreadAttribution, viewingFounder?: Founder, + olderGroups: ReadonlyArray = [], ) { 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); + let unclassified = 0; + const tally = (entry: { + readonly environmentId: EnvironmentId; + readonly inbox: InboxChoice | null; + readonly agentCreated: boolean; + readonly attribution: ThreadAttribution; + readonly bucket: ThreadListFilterBucket | null; + readonly amount: number; + }) => { + const environmentShown = !filter.hiddenEnvironmentIds.includes(entry.environmentId); + const fromShown = !filter.hiddenAttributions.includes(entry.attribution); + const inboxShown = matchesListedInboxes(entry.inbox, entry.agentCreated, filter.inboxes); if (environmentShown && fromShown) { for (const choice of InboxChoice.literals) { - if (matchesInboxes(thread, [choice])) addBucket(inboxes, choice, bucket); + if (matchesListedInboxes(entry.inbox, entry.agentCreated, [choice])) { + addBucket(inboxes, choice, entry.bucket, entry.amount); + } } + if (entry.bucket !== null && entry.inbox === null && !entry.agentCreated) { + unclassified += entry.amount; + } + } + if (inboxShown && fromShown) { + addBucket(environments, entry.environmentId, entry.bucket, entry.amount); } - if (inboxShown && fromShown) addBucket(environments, thread.environmentId, bucket); - if (inboxShown && environmentShown) addBucket(from, attribution, bucket); + if (inboxShown && environmentShown) { + addBucket(from, entry.attribution, entry.bucket, entry.amount); + } + }; + for (const thread of threads) { + tally({ + environmentId: thread.environmentId, + inbox: primaryInbox(thread.source.inbox), + agentCreated: thread.source.createdBy === "agent", + attribution: attributionOf(thread), + bucket: threadListFilterBucket(thread, viewingFounder), + amount: 1, + }); + } + for (const group of olderGroups) { + tally({ ...group, bucket: "settled", amount: group.count }); } - return { inboxes, environments, from }; + return { inboxes, environments, from, unclassified }; } /** Toggles one option, keeping at least one checked so the list never empties by accident. */