diff --git a/.github/workflows/closura-arc-ci.yml b/.github/workflows/closura-arc-ci.yml index f2f15f00a1d5..a8ef448a23bc 100644 --- a/.github/workflows/closura-arc-ci.yml +++ b/.github/workflows/closura-arc-ci.yml @@ -116,7 +116,8 @@ jobs: 'packages/contracts/src/speech.ts': ['assemblyai'], 'apps/web/src/components/settings/CloudVoiceConfiguration.tsx': ['assemblyai'], 'packages/contracts/src/threadInbox.ts': ['matchesInboxView', 'agent-only'], - 'apps/web/src/components/sidebar/SidebarInboxBar.tsx': ['All threads', 'Jeff', 'Kameron'], + 'apps/web/src/components/sidebar/SidebarThreadFilterMenu.tsx': ['Kameron', 'Jeff', 'Agents', 'Environments'], + 'apps/web/src/components/sidebar/threadListFilter.logic.ts': ['matchesInboxFilter', 'hiddenEnvironmentIds'], }; for (const [file, needles] of Object.entries(required)) { const source = readFileSync(file, 'utf8'); @@ -147,6 +148,7 @@ jobs: packages/contracts/src/threadInbox.test.ts \ packages/contracts/src/threadMetadataMcp.test.ts \ apps/web/src/components/threadActionMenu.logic.test.ts \ + apps/web/src/components/sidebar/threadListFilter.logic.test.ts \ apps/server/src/orchestration-v2/ThreadLaunchService.test.ts \ apps/server/src/orchestration-v2/SubagentProjection.test.ts \ packages/shared/src/threadPlacement.test.ts \ diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsx index 65d66899c080..4bd3b3fb86e0 100644 --- a/apps/web/src/components/Sidebar.tsx +++ b/apps/web/src/components/Sidebar.tsx @@ -1,5 +1,5 @@ -import { effectiveInboxView, matchesInboxView } from "@t3tools/contracts"; -import { SidebarInboxBar } from "./sidebar/SidebarInboxBar"; +import { SidebarThreadFilterMenu } from "./sidebar/SidebarThreadFilterMenu"; +import { matchesThreadListFilter, threadListFilterOf } from "./sidebar/threadListFilter.logic"; import { threadRoutingUpdateFromMenuAction } from "./threadActionMenu.logic"; import { attachSidebarTree } from "../lib/sidebarTree"; import { type EnvironmentId, type ScopedProjectRef } from "@t3tools/contracts"; @@ -1103,13 +1103,12 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve ); const threads = useQuietThreadShells(); const promotingDraftThreadKeys = usePromotingDraftThreadKeys(); - const inboxView = useClientSettings(effectiveInboxView); - const inboxThreads = threads.filter( - (thread) => - threadSearchQuery.trim().length > 0 || - matchesInboxView(thread.source.inbox, inboxView) || - (inboxView === "agents" && thread.source.createdBy === "agent"), - ); + const threadListFilter = useClientSettings(threadListFilterOf); + // Search always looks across every inbox and environment. + const inboxThreads = + threadSearchQuery.trim().length > 0 + ? threads + : threads.filter((thread) => matchesThreadListFilter(thread, threadListFilter)); const listedThreads = promotingDraftThreadKeys.length === 0 ? inboxThreads @@ -2992,6 +2991,17 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve 0} + threadFilter={ + + filterSidebarV2VisibleThreads( + threads, + scopedProjectKeys, + delegatedUnderParent, + ).filter((thread) => thread.settledOverride !== "settled") + } + /> + } projectScope={ - } > diff --git a/apps/web/src/components/ThreadNotificationCoordinator.tsx b/apps/web/src/components/ThreadNotificationCoordinator.tsx index 6bd4ab973ecc..b000cdc63a37 100644 --- a/apps/web/src/components/ThreadNotificationCoordinator.tsx +++ b/apps/web/src/components/ThreadNotificationCoordinator.tsx @@ -1,4 +1,3 @@ -import { effectiveInboxView, matchesInboxView } from "@t3tools/contracts"; import { type EnvironmentThreadShell, type PendingSubagentRequest, @@ -21,6 +20,7 @@ import { import { getClientSettings, subscribeClientSettings } from "../hooks/useSettings"; import { isWindowsPlatform } from "../lib/utils"; +import { matchesThreadListFilter, threadListFilterOf } from "./sidebar/threadListFilter.logic"; import { environmentSummaries } from "../state/presentation"; import { environmentShell } from "../state/shell"; import { environmentThreadShells } from "../state/threads"; @@ -114,9 +114,10 @@ export function startThreadNotifications( ? new Set(registry.get(environmentThreadShells.threadRefsAtom).map(threadKey)) : null; const outsideInbox = new Set(); + const filter = threadListFilterOf(getClientSettings()); for (const [environmentId, state] of watched) { for (const [threadId, entry] of state.previous) { - if (!matchesInboxView(entry.thread.source.inbox, effectiveInboxView(getClientSettings()))) + if (!matchesThreadListFilter({ environmentId, source: entry.thread.source }, filter)) outsideInbox.add(threadKey({ environmentId, threadId })); } } @@ -143,7 +144,13 @@ export function startThreadNotifications( kind: ThreadNotificationKind, ) => { const { thread, status } = state; - if (!matchesInboxView(thread.source.inbox, effectiveInboxView(getClientSettings()))) return; + if ( + !matchesThreadListFilter( + { environmentId, source: thread.source }, + threadListFilterOf(getClientSettings()), + ) + ) + return; if ( registry .get(environmentThreadShells.hiddenThreadKeysAtom) diff --git a/apps/web/src/components/settings/NewThreadInboxSettings.tsx b/apps/web/src/components/settings/NewThreadInboxSettings.tsx new file mode 100644 index 000000000000..7068e694def4 --- /dev/null +++ b/apps/web/src/components/settings/NewThreadInboxSettings.tsx @@ -0,0 +1,36 @@ +import { updateClientSettings, useClientSettings } from "../../hooks/useSettings"; +import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select"; +import { SettingsRow } from "./settingsLayout"; +import { searchableSetting } from "./settingsSearch"; + +const OWNER_LABELS = { kameron: "Kameron", jeff: "Jeff" } as const; + +/** Which founder new threads from this device are filed under; the sidebar filter only views. */ +export function NewThreadInboxRow() { + const owner = useClientSettings((settings) => settings.founderInboxOwner); + return ( + + + + } + /> + ); +} diff --git a/apps/web/src/components/settings/SettingsPanels.tsx b/apps/web/src/components/settings/SettingsPanels.tsx index 633e18a567b2..edadc0c84a17 100644 --- a/apps/web/src/components/settings/SettingsPanels.tsx +++ b/apps/web/src/components/settings/SettingsPanels.tsx @@ -64,6 +64,7 @@ import { scopedIdleCompaction } from "./scopedSettings"; import { ProjectDefaultsSettings } from "./ProjectDefaultsSettings"; import { ThreadTitleInstructionsSettings } from "./ThreadTitleInstructionsSettings"; import { SnoozePresetsRow } from "./SnoozePresetsSettings"; +import { NewThreadInboxRow } from "./NewThreadInboxSettings"; import { useDesktopUpdateState } from "../../state/desktopUpdate"; import { managedTelemetryOffMessage, usePrimaryManagedPolicy } from "../../state/managedPolicy"; import { @@ -1840,6 +1841,12 @@ export function ThreadsSettingsPanel() { + {!isProjectScope ? ( + + + + ) : null} + {!isProjectScope ? ( diff --git a/apps/web/src/components/settings/settingsSearch.ts b/apps/web/src/components/settings/settingsSearch.ts index 937e37dd8ec3..bb1d67cd9f19 100644 --- a/apps/web/src/components/settings/settingsSearch.ts +++ b/apps/web/src/components/settings/settingsSearch.ts @@ -820,6 +820,12 @@ export const SETTINGS_SEARCH_ITEMS = [ scope: "project-defaults", searchTerms: ["new worktrees start remote origin latest fetched branch local"], }, + { + id: "new-thread-inbox", + title: "New threads belong to", + to: "/settings/threads", + searchTerms: ["inbox owner founder kameron jeff attribute filed"], + }, { id: "add-project-starts-in", title: "Add project starts in", diff --git a/apps/web/src/components/sidebar/SidebarInboxBar.tsx b/apps/web/src/components/sidebar/SidebarInboxBar.tsx deleted file mode 100644 index 6d49242b29f5..000000000000 --- a/apps/web/src/components/sidebar/SidebarInboxBar.tsx +++ /dev/null @@ -1,54 +0,0 @@ -import { effectiveInboxView, type InboxView } from "@t3tools/contracts"; -import { updateClientSettings, useClientSettings } from "../../hooks/useSettings"; - -/** A local view of the shared pool; All and search always include every thread. */ -export function SidebarInboxBar() { - const view = useClientSettings(effectiveInboxView); - const owner = useClientSettings((settings) => settings.founderInboxOwner); - return ( -
- - -
- ); -} diff --git a/apps/web/src/components/sidebar/SidebarThreadFilterMenu.tsx b/apps/web/src/components/sidebar/SidebarThreadFilterMenu.tsx new file mode 100644 index 000000000000..466eb7bb93bc --- /dev/null +++ b/apps/web/src/components/sidebar/SidebarThreadFilterMenu.tsx @@ -0,0 +1,219 @@ +import { + effectiveInboxFilter, + type EnvironmentId, + type Founder, + type InboxChoice, +} from "@t3tools/contracts"; +import { SlidersHorizontalIcon } from "lucide-react"; +import type { ReactNode } from "react"; + +import { updateClientSettings, useClientSettings } from "../../hooks/useSettings"; +import { cn } from "../../lib/utils"; +import { useConnectedEnvironmentIds, useEnvironmentIdentities } from "../../state/environments"; +import { + Menu, + MenuCheckboxItem, + MenuGroup, + MenuGroupLabel, + MenuItem, + MenuPopup, + MenuSeparator, + MenuTrigger, +} from "../ui/menu"; +import { SidebarHeaderIconButton } from "./SidebarThreadHeader"; +import { + changedThreadListFilters, + inboxMenuOrder, + threadListFilterCounts, + threadListFilterOf, + toggleFilterOption, + toggleHiddenEnvironment, + type ThreadListFilter, +} from "./threadListFilter.logic"; + +const INBOX_LABELS: Record = { + kameron: "Kameron", + jeff: "Jeff", + agents: "Agents", + shared: "Shared", +}; + +type CountedThreads = Parameters[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, +}: { + /** Called only while the menu is open. */ + countedThreads: () => CountedThreads; +}) { + const filter = useClientSettings(threadListFilterOf); + const owner = useClientSettings((settings) => settings.founderInboxOwner); + const environments = useEnvironmentIdentities(); + const environmentIds = environments.map((environment) => environment.environmentId); + const changed = changedThreadListFilters(filter, owner, environmentIds); + const hiddenCount = environmentIds.filter((id) => + filter.hiddenEnvironmentIds.includes(id), + ).length; + const summary = [ + changed.inboxes ? filter.inboxes.map((inbox) => INBOX_LABELS[inbox]).join(", ") : null, + changed.environments + ? `${hiddenCount} environment${hiddenCount === 1 ? "" : "s"} hidden` + : null, + ].filter((part) => part !== null); + const label = + summary.length === 0 + ? "Filter threads by inbox and environment" + : `Filter threads by inbox and environment (${summary.join("; ")})`; + return ( + + }> + + {summary.length > 0 ? ( + + ) : null} + + + 0} + /> + + + ); +} + +// Mounted only while the menu is open, so counting never runs on streaming ticks. +function SidebarThreadFilterOptions({ + countedThreads, + filter, + owner, + environments, + isChanged, +}: { + countedThreads: () => CountedThreads; + filter: ThreadListFilter; + owner: Founder | undefined; + environments: ReadonlyArray<{ readonly environmentId: EnvironmentId; readonly label: string }>; + isChanged: boolean; +}) { + const connectedEnvironmentIds = useConnectedEnvironmentIds(); + const counts = threadListFilterCounts(countedThreads(), filter); + const inboxOrder = inboxMenuOrder(owner); + const environmentIds = environments.map((environment) => environment.environmentId); + const shownEnvironmentIds = environmentIds.filter( + (id) => !filter.hiddenEnvironmentIds.includes(id), + ); + return ( + <> + + Inbox + {inboxOrder.map((inbox) => { + const checked = filter.inboxes.includes(inbox); + const isLast = checked && filter.inboxes.length === 1; + const count = counts.inboxes.get(inbox) ?? 0; + return ( + + void updateClientSettings({ + founderInboxFilter: toggleFilterOption(filter.inboxes, inbox, inboxOrder), + }) + } + > + + + ); + })} + + {environments.length > 1 ? ( + <> + + + Environments + {environments.map((environment) => { + 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; + return ( + + void updateClientSettings({ + sidebarHiddenEnvironmentIds: toggleHiddenEnvironment( + filter.hiddenEnvironmentIds, + environment.environmentId, + environmentIds, + ), + }) + } + > + + } + /> + + ); + })} + + + ) : null} + {isChanged ? ( + <> + + + void updateClientSettings({ + founderInboxFilter: effectiveInboxFilter({ founderInboxOwner: owner }), + sidebarHiddenEnvironmentIds: [], + }) + } + > + Reset to default + + + ) : null} + + ); +} + +function FilterOptionRow({ + label, + count, + indicator, +}: { + label: string; + count: number; + indicator?: ReactNode; +}) { + return ( + + {indicator} + {label} + {count} + + ); +} diff --git a/apps/web/src/components/sidebar/SidebarThreadHeader.tsx b/apps/web/src/components/sidebar/SidebarThreadHeader.tsx index e400e03d2758..c8d4cdbde5f3 100644 --- a/apps/web/src/components/sidebar/SidebarThreadHeader.tsx +++ b/apps/web/src/components/sidebar/SidebarThreadHeader.tsx @@ -14,6 +14,7 @@ import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; export interface SidebarThreadHeaderProps { hasProjects: boolean; + threadFilter: ReactNode; projectScope: ReactNode; onNewProject: () => void; onNewThread: (event: ReactMouseEvent) => void; @@ -33,6 +34,7 @@ export interface SidebarThreadHeaderProps { export function SidebarThreadHeader({ hasProjects, + threadFilter, projectScope, onNewProject, onNewThread, @@ -99,6 +101,7 @@ export function SidebarThreadHeader({
{hasProjects ? ( <> + {threadFilter} {projectScope} diff --git a/apps/web/src/components/sidebar/threadListFilter.logic.test.ts b/apps/web/src/components/sidebar/threadListFilter.logic.test.ts new file mode 100644 index 000000000000..40116d2abad7 --- /dev/null +++ b/apps/web/src/components/sidebar/threadListFilter.logic.test.ts @@ -0,0 +1,92 @@ +import { EnvironmentId, makeThreadInbox } from "@t3tools/contracts"; +import { describe, expect, it } from "vite-plus/test"; + +import { + changedThreadListFilters, + inboxMenuOrder, + matchesThreadListFilter, + threadListFilterCounts, + threadListFilterOf, + toggleFilterOption, + toggleHiddenEnvironment, +} from "./threadListFilter.logic"; + +const laptop = EnvironmentId.make("laptop"); +const crown = EnvironmentId.make("crown"); +const thread = ( + environmentId: EnvironmentId, + actor: Parameters[0]["actor"], + createdBy?: string, +) => ({ + environmentId, + source: { inbox: actor === null ? null : makeThreadInbox({ actor }), createdBy }, +}); +const kameron = { kind: "human", id: "kameron" } as const; +const jeff = { kind: "human", id: "jeff" } as const; +const bot = { kind: "agent", id: "lead" } as const; + +describe("thread list filter", () => { + it("shows a founder their own and agent threads, in the environments they keep", () => { + const filter = threadListFilterOf({ founderInboxOwner: "kameron" }); + const visible = (candidate: ReturnType) => + matchesThreadListFilter(candidate, filter); + expect(visible(thread(laptop, kameron))).toBe(true); + expect(visible(thread(laptop, bot))).toBe(true); + // Agent threads from before inbox routing count as Agents. + expect(visible(thread(laptop, null, "agent"))).toBe(true); + expect(visible(thread(laptop, jeff))).toBe(false); + expect(visible(thread(laptop, null))).toBe(false); + const hidingCrown = { ...filter, hiddenEnvironmentIds: [crown] }; + expect(matchesThreadListFilter(thread(crown, kameron), hidingCrown)).toBe(false); + expect(matchesThreadListFilter(thread(laptop, kameron), hidingCrown)).toBe(true); + }); + + it("counts each section within the other section's selection", () => { + const threads = [ + thread(laptop, kameron), + thread(laptop, bot), + thread(crown, jeff), + thread(crown, kameron), + ]; + const counts = threadListFilterCounts(threads, { + inboxes: ["kameron"], + hiddenEnvironmentIds: [crown], + }); + expect(Object.fromEntries(counts.inboxes)).toEqual({ kameron: 1, agents: 1 }); + expect(Object.fromEntries(counts.environments)).toEqual({ laptop: 1, crown: 1 }); + }); + + it("reports only sections that differ from this device's default", () => { + const environments = [laptop, crown]; + const unchanged = threadListFilterOf({ founderInboxOwner: "jeff" }); + expect(changedThreadListFilters(unchanged, "jeff", environments)).toEqual({ + inboxes: false, + environments: false, + }); + const reordered = { ...unchanged, inboxes: ["agents", "jeff"] as const }; + expect(changedThreadListFilters(reordered, "jeff", environments).inboxes).toBe(false); + // A hidden environment that was since removed is not a visible filter. + const removed = { ...unchanged, hiddenEnvironmentIds: [EnvironmentId.make("gone")] }; + expect(changedThreadListFilters(removed, "jeff", environments).environments).toBe(false); + expect( + changedThreadListFilters( + { ...unchanged, hiddenEnvironmentIds: [crown] }, + "jeff", + environments, + ), + ).toEqual({ inboxes: false, environments: true }); + }); + + it("toggles in menu order and never unchecks the last option", () => { + const order = inboxMenuOrder("jeff"); + expect(order).toEqual(["jeff", "agents", "kameron", "shared"]); + expect(toggleFilterOption(["agents"], "jeff", order)).toEqual(["jeff", "agents"]); + expect(toggleFilterOption(["jeff", "agents"], "jeff", order)).toEqual(["agents"]); + expect(toggleFilterOption(["agents"], "agents", order)).toEqual(["agents"]); + // An environment that has not loaded yet stays hidden when another one is toggled. + const offline = EnvironmentId.make("offline"); + expect(toggleHiddenEnvironment([offline], crown, [laptop, crown])).toEqual([offline, crown]); + expect(toggleHiddenEnvironment([offline, crown], crown, [laptop, crown])).toEqual([offline]); + expect(toggleHiddenEnvironment([crown], laptop, [laptop, crown])).toEqual([crown]); + }); +}); diff --git a/apps/web/src/components/sidebar/threadListFilter.logic.ts b/apps/web/src/components/sidebar/threadListFilter.logic.ts new file mode 100644 index 000000000000..94840afb100a --- /dev/null +++ b/apps/web/src/components/sidebar/threadListFilter.logic.ts @@ -0,0 +1,121 @@ +import { + effectiveInboxFilter, + Founder, + InboxChoice, + matchesInboxFilter, + primaryInbox, + type EnvironmentId, + type ThreadInbox, +} from "@t3tools/contracts"; +import type { ClientSettings } from "@t3tools/contracts/settings"; + +export interface ThreadListFilter { + readonly inboxes: ReadonlyArray; + readonly hiddenEnvironmentIds: ReadonlyArray; +} + +interface FilterableThread { + readonly environmentId: EnvironmentId; + readonly source: { + readonly inbox?: ThreadInbox | null | undefined; + readonly createdBy?: string | undefined; + }; +} + +export const threadListFilterOf = ( + settings: Pick< + ClientSettings, + "founderInboxFilter" | "founderInboxOwner" | "sidebarHiddenEnvironmentIds" + >, +): ThreadListFilter => ({ + inboxes: effectiveInboxFilter(settings), + hiddenEnvironmentIds: settings.sidebarHiddenEnvironmentIds ?? [], +}); + +const matchesInboxes = (thread: FilterableThread, inboxes: ReadonlyArray) => + matchesInboxFilter(thread.source.inbox, inboxes) || + // Agent-created threads from before inbox routing carry no Agents routing. + (inboxes.includes("agents") && + thread.source.createdBy === "agent" && + primaryInbox(thread.source.inbox) === null); + +export const matchesThreadListFilter = (thread: FilterableThread, filter: ThreadListFilter) => + !filter.hiddenEnvironmentIds.includes(thread.environmentId) && + matchesInboxes(thread, filter.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]; + return [first, "agents", ...Founder.literals.filter((founder) => founder !== first), "shared"]; +}; + +/** + * The hidden environments after showing or hiding one. Hidden ids of environments this + * client has not loaded yet (startup, an offline machine) stay hidden. + */ +export function toggleHiddenEnvironment( + hidden: ReadonlyArray, + environmentId: EnvironmentId, + knownEnvironmentIds: ReadonlyArray, +): ReadonlyArray { + const shown = toggleFilterOption( + knownEnvironmentIds.filter((id) => !hidden.includes(id)), + environmentId, + knownEnvironmentIds, + ); + return [ + ...hidden.filter((id) => !knownEnvironmentIds.includes(id)), + ...knownEnvironmentIds.filter((id) => !shown.includes(id)), + ]; +} + +const sameMembers = (left: ReadonlyArray, right: ReadonlyArray) => + left.length === right.length && left.every((value) => right.includes(value)); + +/** Filter sections that differ from this device's default, ignoring removed environments. */ +export function changedThreadListFilters( + filter: ThreadListFilter, + owner: Founder | undefined, + environmentIds: ReadonlyArray, +): { readonly inboxes: boolean; readonly environments: boolean } { + return { + inboxes: !sameMembers(filter.inboxes, effectiveInboxFilter({ founderInboxOwner: owner })), + environments: filter.hiddenEnvironmentIds.some((id) => environmentIds.includes(id)), + }; +} + +/** + * Faceted counts: each inbox counts threads in the shown environments, and each environment + * counts threads in the chosen inboxes, so a count is what checking that option would add. + */ +export function threadListFilterCounts( + threads: ReadonlyArray, + filter: ThreadListFilter, +) { + const inboxes = new Map(); + const environments = new Map(); + for (const thread of threads) { + if (!filter.hiddenEnvironmentIds.includes(thread.environmentId)) { + for (const choice of InboxChoice.literals) { + if (matchesInboxes(thread, [choice])) inboxes.set(choice, (inboxes.get(choice) ?? 0) + 1); + } + } + if (matchesInboxes(thread, filter.inboxes)) { + environments.set(thread.environmentId, (environments.get(thread.environmentId) ?? 0) + 1); + } + } + return { inboxes, environments }; +} + +/** Toggles one option, keeping at least one checked so the list never empties by accident. */ +export function toggleFilterOption( + selected: ReadonlyArray, + option: A, + order: ReadonlyArray, +): ReadonlyArray { + const next = selected.includes(option) + ? selected.filter((value) => value !== option) + : [...selected, option]; + if (next.length === 0) return selected; + return order.filter((value) => next.includes(value)); +} diff --git a/packages/contracts/src/settings.ts b/packages/contracts/src/settings.ts index 47dce811574d..d91ab131da19 100644 --- a/packages/contracts/src/settings.ts +++ b/packages/contracts/src/settings.ts @@ -1,5 +1,5 @@ import { EasDeviceHostConfigs, SimlockDeviceHostConfigs, SshDeviceHostConfigs } from "./device.ts"; -import { Founder, InboxView } from "./threadInbox.ts"; +import { Founder, InboxChoice } from "./threadInbox.ts"; import { AuthOrchestrationOperateScope, AuthAccessWriteScope, @@ -406,7 +406,8 @@ export type VoiceTranscriptionDestination = typeof VoiceTranscriptionDestination export const ClientSettingsSchema = Schema.Struct({ // Absent until chosen; whole-snapshot saves must not record a default as a choice. founderInboxOwner: Schema.optionalKey(Founder), - founderInboxView: Schema.optionalKey(InboxView), + founderInboxFilter: Schema.optionalKey(Schema.Array(InboxChoice)), + sidebarHiddenEnvironmentIds: Schema.optionalKey(Schema.Array(EnvironmentId)), notificationMode: NotificationMode.pipe( Schema.withDecodingDefault(Effect.succeed("off" as const)), ), @@ -2054,7 +2055,8 @@ export function requiredScopesForProjectMutation( export const ClientSettingsPatch = Schema.Struct({ founderInboxOwner: Schema.optionalKey(Founder), - founderInboxView: Schema.optionalKey(InboxView), + founderInboxFilter: Schema.optionalKey(Schema.Array(InboxChoice)), + sidebarHiddenEnvironmentIds: Schema.optionalKey(Schema.Array(EnvironmentId)), notificationMode: Schema.optionalKey(NotificationMode), inAppNotificationsEnabled: Schema.optionalKey(Schema.Boolean), diffColorScheme: Schema.optionalKey(DiffColorScheme), diff --git a/packages/contracts/src/threadInbox.test.ts b/packages/contracts/src/threadInbox.test.ts index c52ba992dd60..8ebd2ff02dcb 100644 --- a/packages/contracts/src/threadInbox.test.ts +++ b/packages/contracts/src/threadInbox.test.ts @@ -6,11 +6,13 @@ import { makeThreadInbox, routeThreadInbox, matchesInboxView, - effectiveInboxView, + matchesInboxFilter, + effectiveInboxFilter, inboxOriginFor, founderInboxLane, compositeThreadKey, agentLaunchInboxRouting, + inboxRoutingFor, } from "./threadInbox.ts"; const kameron = { kind: "human", id: "kameron" } as const; const jeff = { kind: "human", id: "jeff" } as const; @@ -30,27 +32,50 @@ describe("shared fleet organizational inbox", () => { expect(matchesInboxView(j, "all")).toBe(true); expect(matchesInboxView(k, "all")).toBe(true); }); - it("keeps All until a founder is chosen, then defaults to that founder's view", () => { + it("keeps All until a founder is chosen, then defaults to that founder plus Agents", () => { const decode = Schema.decodeUnknownSync(ClientSettingsSchema); const encode = Schema.encodeSync(ClientSettingsSchema); - const upgraded = decode({ notificationMode: "off" }); + // Settings saved by the single-choice selector still decode; that choice is not carried over. + const upgraded = decode({ notificationMode: "off", founderInboxView: "all" }); // Any later settings write saves the whole snapshot; it must not record an unmade choice. const resaved = encode(decode(encode(upgraded))); - expect(["founderInboxView", "founderInboxOwner"].filter((key) => key in resaved)).toEqual([]); - expect(effectiveInboxView(upgraded)).toBe("all"); - expect(matchesInboxView(undefined, effectiveInboxView(upgraded))).toBe(true); + expect( + ["founderInboxView", "founderInboxFilter", "founderInboxOwner"].filter( + (key) => key in resaved, + ), + ).toEqual([]); + expect(matchesInboxFilter(undefined, effectiveInboxFilter(upgraded))).toBe(true); const jeffClient = decode(encode({ ...upgraded, founderInboxOwner: "jeff" })); - const view = effectiveInboxView(jeffClient); - expect(view).toBe("jeff"); - expect(matchesInboxView(makeThreadInbox({ actor: jeff }), view)).toBe(true); - expect(matchesInboxView(makeThreadInbox({ actor: kameron }), view)).toBe(false); - expect(matchesInboxView(makeThreadInbox({ actor: bot }), view)).toBe(false); - expect(matchesInboxView(makeThreadInbox({ actor: bot }), "agents")).toBe(true); - expect(matchesInboxView(undefined, "all")).toBe(true); - for (const picked of ["jeff", "kameron", "shared", "agents", "all"] as const) { - const saved = decode(encode({ ...jeffClient, founderInboxView: picked })); - expect([effectiveInboxView(saved), saved.founderInboxOwner]).toEqual([picked, "jeff"]); - } + const filter = effectiveInboxFilter(jeffClient); + expect(filter).toEqual(["jeff", "agents"]); + expect(matchesInboxFilter(makeThreadInbox({ actor: jeff }), filter)).toBe(true); + expect(matchesInboxFilter(makeThreadInbox({ actor: bot }), filter)).toBe(true); + expect(matchesInboxFilter(makeThreadInbox({ actor: kameron }), filter)).toBe(false); + expect(matchesInboxFilter(undefined, filter)).toBe(false); + const picked = decode(encode({ ...jeffClient, founderInboxFilter: ["kameron", "shared"] })); + expect([effectiveInboxFilter(picked), picked.founderInboxOwner]).toEqual([ + ["kameron", "shared"], + "jeff", + ]); + // Agent work filed to another founder or Shared stays out of "Kameron + Agents". + const mineAndAgents = ["kameron", "agents"] as const; + const filedToJeff = makeThreadInbox({ actor: bot, routing: inboxRoutingFor("jeff") }); + const sharedByKameron = makeThreadInbox({ actor: kameron, routing: { audience: "shared" } }); + expect(matchesInboxFilter(filedToJeff, mineAndAgents)).toBe(false); + expect(matchesInboxFilter(filedToJeff, ["jeff"])).toBe(true); + expect(matchesInboxFilter(sharedByKameron, mineAndAgents)).toBe(false); + expect(matchesInboxFilter(sharedByKameron, ["shared"])).toBe(true); + const filedToKameron = makeThreadInbox({ actor: bot, routing: inboxRoutingFor("kameron") }); + expect(matchesInboxFilter(filedToKameron, ["kameron"])).toBe(true); + expect(matchesInboxFilter(filedToKameron, ["agents"])).toBe(false); + expect(effectiveInboxFilter({ founderInboxOwner: "jeff", founderInboxFilter: [] })).toEqual([ + "jeff", + "agents", + ]); + // Every inbox is All, which keeps unclassified history. + const every = ["shared", "jeff", "agents", "kameron"] as const; + expect(matchesInboxFilter(undefined, every)).toBe(true); + expect(matchesInboxFilter(undefined, every.slice(1))).toBe(false); }); it("attributes new work only to a chosen founder and leaves unchosen work in All", () => { expect(inboxOriginFor({})).toBeUndefined(); @@ -58,7 +83,7 @@ describe("shared fleet organizational inbox", () => { for (const view of ["kameron", "jeff", "agents", "shared"] as const) { expect(matchesInboxView(undefined, view)).toBe(false); } - expect(matchesInboxView(undefined, effectiveInboxView({}))).toBe(true); + expect(matchesInboxFilter(undefined, effectiveInboxFilter({}))).toBe(true); const origin = inboxOriginFor({ founderInboxOwner: "jeff" }); expect(origin).toEqual(jeff); const inbox = makeThreadInbox({ actor: origin ?? null }); diff --git a/packages/contracts/src/threadInbox.ts b/packages/contracts/src/threadInbox.ts index 2ac7787b190f..916023ae6500 100644 --- a/packages/contracts/src/threadInbox.ts +++ b/packages/contracts/src/threadInbox.ts @@ -131,11 +131,43 @@ export function founderInboxLane(inbox: ThreadInbox | null | undefined, founder: return inbox.attentionKind; } -/** The chosen view, else the chosen founder's, else All so unclassified history stays visible. */ -export const effectiveInboxView = (settings: { - readonly founderInboxView?: InboxView | undefined; +/** + * The inboxes the sidebar lists: the chosen ones, else the chosen founder's plus Agents, else + * every inbox so unclassified history stays visible. + */ +export const effectiveInboxFilter = (settings: { + readonly founderInboxFilter?: ReadonlyArray | undefined; readonly founderInboxOwner?: Founder | undefined; -}): InboxView => settings.founderInboxView ?? settings.founderInboxOwner ?? "all"; +}): ReadonlyArray => + // An empty choice, from another client or a hand edit, would blank the list; treat it as unset. + settings.founderInboxFilter !== undefined && settings.founderInboxFilter.length > 0 + ? settings.founderInboxFilter + : settings.founderInboxOwner === undefined + ? InboxChoice.literals + : [settings.founderInboxOwner, "agents"]; + +/** Choosing every inbox means All, which also keeps unclassified history. */ +const isAllInboxes = (filter: ReadonlyArray) => + InboxChoice.literals.every((choice) => filter.includes(choice)); + +/** + * The one inbox a thread is filtered under, so unchecking an inbox hides all of its threads: + * Shared, else the founder it is filed to, else Agents. Null is unclassified history. + */ +export function primaryInbox(inbox: ThreadInbox | null | undefined): InboxChoice | null { + if (matchesInboxView(inbox, "shared")) return "shared"; + for (const founder of Founder.literals) if (matchesInboxView(inbox, founder)) return founder; + return matchesInboxView(inbox, "agents") ? "agents" : null; +} + +export const matchesInboxFilter = ( + inbox: ThreadInbox | null | undefined, + filter: ReadonlyArray, +) => { + if (isAllInboxes(filter)) return true; + const primary = primaryInbox(inbox); + return primary !== null && filter.includes(primary); +}; /** New work is attributed only to a chosen founder; otherwise it stays unclassified (All). */ export const inboxOriginFor = (settings: {