diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsx index 0a19c2698ad78..97c56b5fa19b8 100644 --- a/apps/web/src/components/Sidebar.tsx +++ b/apps/web/src/components/Sidebar.tsx @@ -19,6 +19,7 @@ import { threadRoutingUpdateFromMenuAction } from "./threadActionMenu.logic"; import { attachSidebarTree } from "../lib/sidebarTree"; import { effectiveInboxFilter, + isHiddenFromFounder, staysActiveWhileWorking, type EnvironmentId, type InboxChoice, @@ -1234,11 +1235,14 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve }; // Search always looks across every inbox, sender and environment. const listFilterApplies = threadSearchQuery.trim().length === 0; + // Another founder's private threads stay out of search too (see `isHiddenFromFounder`). const inboxThreads = listFilterApplies ? threads.filter((thread) => matchesThreadListFilter(thread, threadListFilter, attributionOf(thread)), ) - : threads; + : threads.filter( + (thread) => !isHiddenFromFounder(thread.source.inbox, threadListFilter.viewingFounder), + ); // The same filter over threads servers counted but did not send, for the Settled total. const olderSettledFilter: OlderSettledFilter | null = listFilterApplies ? { diff --git a/apps/web/src/components/bots/botPresence.test.ts b/apps/web/src/components/bots/botPresence.test.ts index 179d3a039665f..8875183f8fa3d 100644 --- a/apps/web/src/components/bots/botPresence.test.ts +++ b/apps/web/src/components/bots/botPresence.test.ts @@ -88,11 +88,15 @@ describe("splitStandingThreads", () => { }); describe("partitionBotGroups", () => { + const thread = (id: string, audience?: string) => ({ + id, + source: { inbox: audience === undefined ? null : { audience } }, + }); + const owned = (id: string, owner: "kameron" | "jeff", audience: string | undefined) => ({ + id, + source: { inbox: { owner, audience } }, + }); it("puts shared-inbox bots in their own group and leaves the rest as mine", () => { - const thread = (id: string, audience?: string) => ({ - id, - source: { inbox: audience === undefined ? null : { audience } }, - }); expect( partitionBotGroups([thread("shared", "shared"), thread("jeff", "jeff"), thread("open")]), ).toEqual({ @@ -101,11 +105,7 @@ describe("partitionBotGroups", () => { }); }); - it("shows every standing bot to both founders", () => { - const thread = (id: string, audience?: string) => ({ - id, - source: { inbox: audience === undefined ? null : { audience } }, - }); + it("keeps Jeff's personal bots in Kameron's lists beside his own and shared ones", () => { const threads = [ thread("shared", "shared"), thread("mine", "kameron"), @@ -122,13 +122,27 @@ describe("partitionBotGroups", () => { thread("open"), ], }); + }); + + it("never lists Kameron's personal bots for Jeff", () => { + const threads = [ + thread("shared", "shared"), + thread("cos", "kameron"), + thread("cos-jeff", "jeff"), + thread("agents", "agent-only"), + thread("open"), + owned("kameron-agents", "kameron", "agent-only"), + owned("kameron-unfiled", "kameron", undefined), + owned("kameron-shared", "kameron", "shared"), + owned("kameron-jeff", "kameron", "jeff"), + ]; expect(partitionBotGroups(threads, "jeff")).toEqual({ - mine: [thread("other", "jeff")], + mine: [thread("cos-jeff", "jeff"), owned("kameron-jeff", "kameron", "jeff")], shared: [ thread("shared", "shared"), - thread("mine", "kameron"), thread("agents", "agent-only"), thread("open"), + owned("kameron-shared", "kameron", "shared"), ], }); }); diff --git a/apps/web/src/components/bots/botPresence.ts b/apps/web/src/components/bots/botPresence.ts index d120f9abf5aa4..134b5048ca677 100644 --- a/apps/web/src/components/bots/botPresence.ts +++ b/apps/web/src/components/bots/botPresence.ts @@ -1,4 +1,8 @@ -import type { OrchestrationV2ProviderFailureClass } from "@t3tools/contracts"; +import { + type Founder, + isHiddenFromFounder, + type OrchestrationV2ProviderFailureClass, +} from "@t3tools/contracts"; import type { ThreadStatusPill } from "../Sidebar.logic"; import type { BotAvatarState } from "./BotAvatar"; @@ -75,25 +79,28 @@ export function splitStandingThreads( /** A shared bot belongs to both founders; every other standing bot is one's own. */ function isSharedBotThread(thread: { - readonly source: { readonly inbox?: { readonly audience?: string } | null | undefined }; + readonly source: { readonly inbox?: BotInbox | null | undefined }; }): boolean { return thread.source.inbox?.audience === "shared"; } +type BotInbox = { + readonly owner?: Founder | null | undefined; + readonly audience?: string | null | undefined; +}; + /** - * The two bot groups. Shared audience is Shared Bots. With a viewer, an Agents inbox, a missing - * inbox, or the other founder's bot is Shared Bots too, so both founders see every standing bot. - * The viewer's own founder-audience bots stay in My Bots. A shared bot is never also listed there. + * The two bot groups. Shared audience is Shared Bots. With a viewer, an Agents or missing inbox + * and the other founder's bots are Shared Bots too; the viewer's own stay in My Bots. Jeff never + * lists Kameron's private bots (`isHiddenFromFounder`), while Kameron lists Jeff's. */ export function partitionBotGroups< - T extends { readonly source: { readonly inbox?: { readonly audience?: string } | null } }, ->( - threads: readonly T[], - viewer?: "kameron" | "jeff", -): { readonly mine: T[]; readonly shared: T[] } { + T extends { readonly source: { readonly inbox?: BotInbox | null } }, +>(threads: readonly T[], viewer?: Founder): { readonly mine: T[]; readonly shared: T[] } { const mine: T[] = []; const shared: T[] = []; for (const thread of threads) { + if (isHiddenFromFounder(thread.source.inbox, viewer)) continue; const audience = thread.source.inbox?.audience; const sharedWithBoth = isSharedBotThread(thread) || diff --git a/apps/web/src/components/sidebar/threadListFilter.logic.test.ts b/apps/web/src/components/sidebar/threadListFilter.logic.test.ts index 15af3facf7a1c..ad29af1c3c7e6 100644 --- a/apps/web/src/components/sidebar/threadListFilter.logic.test.ts +++ b/apps/web/src/components/sidebar/threadListFilter.logic.test.ts @@ -366,7 +366,7 @@ describe("thread list filter", () => { }); }); -describe("standing bots stay listed for both founders", () => { +describe("standing bots bypass the inbox and From checkboxes", () => { const persona = { name: "Infra" }; const stuck = { environmentId: crown, @@ -388,14 +388,25 @@ describe("standing bots stay listed for both founders", () => { }, }; - it("lists a background standing bot when Agents is unchecked, for either founder", () => { - for (const owner of ["kameron", "jeff"] as const) { - const filter = { - ...threadListFilterOf({ founderInboxOwner: owner }), - inboxes: [owner] as const, - }; - expect(matchesThreadListFilter(stuck, filter, "unknown")).toBe(true); - } + it("lists a Kameron-audience background standing bot for Kameron, never for Jeff", () => { + const kameronFilter = { + ...threadListFilterOf({ founderInboxOwner: "kameron" }), + inboxes: ["kameron"] as const, + }; + expect(matchesThreadListFilter(stuck, kameronFilter, "unknown")).toBe(true); + const jeffFilter = { + ...threadListFilterOf({ founderInboxOwner: "jeff" }), + inboxes: ["jeff"] as const, + }; + expect(matchesThreadListFilter(stuck, jeffFilter, "unknown")).toBe(false); + // Even with every inbox checked, Kameron's private bot stays out of Jeff's list. + expect( + matchesThreadListFilter( + stuck, + { ...jeffFilter, inboxes: ["kameron", "jeff", "shared", "agents"] }, + "unknown", + ), + ).toBe(false); expect( matchesThreadListFilter( stuck, @@ -407,21 +418,26 @@ describe("standing bots stay listed for both founders", () => { "unknown", ), ).toBe(false); - const jeffWithoutAgents = { - ...threadListFilterOf({ founderInboxOwner: "jeff" }), - inboxes: ["jeff", "shared"] as const, + const kameronWithoutAgents = { + ...threadListFilterOf({ founderInboxOwner: "kameron" }), + inboxes: ["kameron", "shared"] as const, }; // Background attention files the lane under Agents, so unchecking Agents hides it. - expect(matchesThreadListFilter({ ...stuck, standing: null }, jeffWithoutAgents)).toBe(false); - expect(matchesThreadListFilter(stuck, jeffWithoutAgents, "unknown")).toBe(true); + expect(matchesThreadListFilter({ ...stuck, standing: null }, kameronWithoutAgents)).toBe(false); + expect(matchesThreadListFilter(stuck, kameronWithoutAgents, "unknown")).toBe(true); }); it("counts a running remote standing bot even when its inbox is unchecked", () => { const counts = threadListFilterCounts( [stuck], - { inboxes: ["jeff"], hiddenEnvironmentIds: [], hiddenAttributions: ["unknown"] }, + { + inboxes: ["jeff"], + hiddenEnvironmentIds: [], + hiddenAttributions: ["unknown"], + viewingFounder: "kameron", + }, () => "unknown", - "jeff", + "kameron", ); expect(counts.environments.get(crown)).toEqual({ working: 1, @@ -432,6 +448,65 @@ describe("standing bots stay listed for both founders", () => { }); }); +describe("counts for an asymmetric founder view", () => { + const filterOf = (viewingFounder: "kameron" | "jeff") => ({ + inboxes: ["kameron", "jeff", "shared", "agents"] as const, + hiddenEnvironmentIds: [], + hiddenAttributions: [], + viewingFounder, + }); + const running = { runtime: { status: "running" } }; + const entries = [ + { ...thread(crown, kameron), ...running }, + { ...thread(crown, jeff), ...running }, + { + environmentId: crown, + ...running, + source: { + inbox: makeThreadInbox({ + actor: kameron, + routing: { owner: "kameron", audience: "agent-only", attentionKind: "background" }, + }), + }, + }, + ]; + const total = (counts: ReturnType) => + Object.values(counts.environments.get(crown) ?? {}).reduce((sum, n) => sum + n, 0); + + it("leaves Kameron's private threads out of Jeff's counts", () => { + const counts = threadListFilterCounts(entries, filterOf("jeff"), () => "unknown", "jeff"); + expect(total(counts)).toBe(1); + expect(counts.inboxes.get("kameron")).toBeUndefined(); + expect(counts.inboxes.get("jeff")).toBeDefined(); + }); + + it("keeps every thread in Kameron's counts", () => { + const counts = threadListFilterCounts(entries, filterOf("kameron"), () => "unknown", "kameron"); + expect(total(counts)).toBe(3); + }); + + it("leaves Kameron's older settled groups out of Jeff's counts", () => { + const group = (inbox: "kameron" | "jeff") => ({ + environmentId: crown, + inbox, + agentCreated: false, + attribution: "unknown" as const, + count: 4, + }); + const groups = [group("kameron"), group("jeff")]; + const forJeff = threadListFilterCounts([], filterOf("jeff"), () => "unknown", "jeff", groups); + const forKameron = threadListFilterCounts( + [], + filterOf("kameron"), + () => "unknown", + "kameron", + groups, + ); + expect(forJeff.environments.get(crown)?.settled).toBe(4); + expect(forKameron.environments.get(crown)?.settled).toBe(8); + }); +}); + describe("sidebar filter labels", () => { it("keeps the popover wide enough to show full inbox and environment names", () => { expect(SIDEBAR_FILTER_POPUP_CLASS).not.toContain("available-width"); diff --git a/apps/web/src/components/sidebar/threadListFilter.logic.ts b/apps/web/src/components/sidebar/threadListFilter.logic.ts index a9a3cdacd434e..7980868109f57 100644 --- a/apps/web/src/components/sidebar/threadListFilter.logic.ts +++ b/apps/web/src/components/sidebar/threadListFilter.logic.ts @@ -14,6 +14,7 @@ import { founderInboxLane, Founder, InboxChoice, + isHiddenFromFounder, primaryInbox, type EnvironmentId, type OrchestrationV2ThreadShell, @@ -295,6 +296,7 @@ export function threadListFilterCounts( } }; for (const thread of threads) { + if (isHiddenFromFounder(thread.source.inbox, filter.viewingFounder)) continue; tally({ environmentId: thread.environmentId, inbox: primaryInbox(thread.source.inbox), @@ -306,6 +308,7 @@ export function threadListFilterCounts( }); } for (const group of olderGroups) { + if (filter.viewingFounder === "jeff" && group.inbox === "kameron") continue; tally({ ...group, bucket: "settled", amount: group.count }); } return { inboxes, environments, from, unclassified }; diff --git a/apps/web/src/lib/archivedThreadsState.ts b/apps/web/src/lib/archivedThreadsState.ts index fbd17226cefc8..e654d1a63e9a7 100644 --- a/apps/web/src/lib/archivedThreadsState.ts +++ b/apps/web/src/lib/archivedThreadsState.ts @@ -8,7 +8,7 @@ import type { EnvironmentId } from "@t3tools/contracts"; import { orchestrationEnvironment } from "../state/orchestration"; import { serverEnvironment } from "../state/server"; -import { environmentThreadShells } from "../state/threads"; +import { environmentThreadShells, viewerFounderAtom } from "../state/threads"; import { appAtomRegistry } from "../rpc/atomRegistry"; function archivedSnapshotAtom(environmentId: EnvironmentId) { @@ -24,6 +24,7 @@ const archivedSnapshotsAtom = createArchivedThreadSnapshotsAtomFamily({ hiddenThreadKeysAtom: environmentThreadShells.hiddenThreadKeysAtom, liveThreadRefsAtom: environmentThreadShells.threadRefsAtom, anyEnvironmentHidingAtom: environmentThreadShells.anyEnvironmentHidingAtom, + viewerFounderAtom, labelPrefix: "web:archived-thread-snapshots", }); diff --git a/packages/client-runtime/src/state/archivedThreads.ts b/packages/client-runtime/src/state/archivedThreads.ts index 067a913d018b4..647890bb580fb 100644 --- a/packages/client-runtime/src/state/archivedThreads.ts +++ b/packages/client-runtime/src/state/archivedThreads.ts @@ -1,5 +1,6 @@ import { EnvironmentId, + type Founder, type OrchestrationV2ArchivedShellSnapshot, type ScopedThreadRef, } from "@t3tools/contracts"; @@ -56,6 +57,8 @@ export function createArchivedThreadSnapshotsAtomFamily(options: { readonly liveThreadRefsAtom: Atom.Atom>; /** Whether any connected environment hides tags, including ones outside this archive's scope. */ readonly anyEnvironmentHidingAtom: Atom.Atom; + /** The founder reading this client, whose lists leave out the other founder's private threads. */ + readonly viewerFounderAtom?: Atom.Atom; readonly labelPrefix: string; }) { return Atom.family((environmentKey: string) => @@ -93,6 +96,7 @@ export function createArchivedThreadSnapshotsAtomFamily(options: { get(options.hiddenThreadKeysAtom), hidingAnywhere ? new Set(get(options.liveThreadRefsAtom).map(threadKey)) : undefined, hidingAnywhere, + options.viewerFounderAtom === undefined ? null : get(options.viewerFounderAtom), ); if (hidden.size === 0) return { snapshots, error, isLoading }; return { diff --git a/packages/client-runtime/src/state/threadListFilter.test.ts b/packages/client-runtime/src/state/threadListFilter.test.ts new file mode 100644 index 0000000000000..521df3278f2ab --- /dev/null +++ b/packages/client-runtime/src/state/threadListFilter.test.ts @@ -0,0 +1,140 @@ +import { EnvironmentId, type Founder } from "@t3tools/contracts"; +import { describe, expect, it } from "vite-plus/test"; + +import { + matchesOlderSettledGroup, + matchesThreadListFilter, + threadListFilterOf, +} from "./threadListFilter.ts"; + +const environmentId = EnvironmentId.make("crown"); + +const inboxOf = (owner: Founder | null, audience: string | undefined) => + ({ owner, audience }) as never; + +const thread = ( + inbox: ReturnType | undefined, + options: { readonly standing?: boolean; readonly archived?: boolean } = {}, +) => ({ + environmentId, + standing: options.standing === true ? { name: "Bot" } : null, + archivedAt: options.archived === true ? "2026-11-25T00:00:00.000Z" : null, + source: { inbox }, +}); + +/** Every inbox is checked, so only the founder rule can hide a thread. */ +const filterFor = (viewingFounder: Founder | undefined) => + threadListFilterOf({ + ...(viewingFounder === undefined ? {} : { founderInboxOwner: viewingFounder }), + founderInboxFilter: ["kameron", "jeff", "shared", "agents"], + }); + +const cos = inboxOf("kameron", "kameron"); +const cosJeff = inboxOf("jeff", "jeff"); +const shared = inboxOf(null, "shared"); + +describe("matchesThreadListFilter asymmetric founder visibility", () => { + it("takes the viewing founder from the settings", () => { + expect(threadListFilterOf({ founderInboxOwner: "jeff" }).viewingFounder).toBe("jeff"); + expect(threadListFilterOf({}).viewingFounder).toBeUndefined(); + }); + + describe("viewing as jeff", () => { + const filter = filterFor("jeff"); + + it("hides Kameron's threads and standing bots, even with every inbox checked", () => { + expect(matchesThreadListFilter(thread(cos), filter)).toBe(false); + expect(matchesThreadListFilter(thread(cos, { standing: true }), filter)).toBe(false); + expect(matchesThreadListFilter(thread(inboxOf(null, "kameron")), filter)).toBe(false); + expect( + matchesThreadListFilter(thread(inboxOf(null, "kameron"), { standing: true }), filter), + ).toBe(false); + }); + + it("hides a Kameron-owned thread or bot filed Agents-only or with no audience", () => { + for (const audience of ["agent-only", undefined]) { + const inbox = inboxOf("kameron", audience); + expect(matchesThreadListFilter(thread(inbox), filter)).toBe(false); + expect(matchesThreadListFilter(thread(inbox, { standing: true }), filter)).toBe(false); + } + }); + + it("keeps Jeff's, shared and Kameron-owned shared or Jeff-audience threads and bots", () => { + for (const inbox of [ + cosJeff, + shared, + inboxOf("kameron", "shared"), + inboxOf("kameron", "jeff"), + ]) { + expect(matchesThreadListFilter(thread(inbox), filter)).toBe(true); + expect(matchesThreadListFilter(thread(inbox, { standing: true }), filter)).toBe(true); + } + }); + + it("keeps a thread with no inbox", () => { + expect(matchesThreadListFilter(thread(undefined), filter)).toBe(true); + expect(matchesThreadListFilter(thread(null as never), filter)).toBe(true); + }); + + it("still lists Jeff's own standing bot when his inbox checkboxes would drop it", () => { + const onlyShared = { ...filter, inboxes: ["shared"] as const }; + expect(matchesThreadListFilter(thread(cosJeff), onlyShared)).toBe(false); + expect(matchesThreadListFilter(thread(cosJeff, { standing: true }), onlyShared)).toBe(true); + }); + + it("hides Kameron's older settled groups", () => { + const group = (inbox: "kameron" | "jeff" | "shared" | "agents" | null) => ({ + inbox, + agentCreated: false, + founder: "machine" as const, + byAgent: false, + count: 3, + }); + expect(matchesOlderSettledGroup(environmentId, group("kameron"), filter, {})).toBe(false); + expect(matchesOlderSettledGroup(environmentId, group("jeff"), filter, {})).toBe(true); + expect(matchesOlderSettledGroup(environmentId, group("shared"), filter, {})).toBe(true); + }); + }); + + describe("viewing as kameron", () => { + const filter = filterFor("kameron"); + + it("keeps Jeff's personal threads and bots next to his own and shared ones", () => { + for (const inbox of [cosJeff, cos, shared, inboxOf("jeff", "jeff")]) { + expect(matchesThreadListFilter(thread(inbox), filter)).toBe(true); + expect(matchesThreadListFilter(thread(inbox, { standing: true }), filter)).toBe(true); + } + }); + + it("keeps Kameron-owned Agents-only threads and a thread with no inbox", () => { + expect(matchesThreadListFilter(thread(inboxOf("kameron", "agent-only")), filter)).toBe(true); + expect(matchesThreadListFilter(thread(undefined), filter)).toBe(true); + }); + + it("still hides an archived standing bot, as before", () => { + expect( + matchesThreadListFilter(thread(cosJeff, { standing: true, archived: true }), { + ...filter, + inboxes: ["kameron"], + }), + ).toBe(false); + }); + + it("keeps Jeff's older settled groups", () => { + expect( + matchesOlderSettledGroup( + environmentId, + { inbox: "jeff", agentCreated: false, founder: "machine", byAgent: false, count: 1 }, + filter, + {}, + ), + ).toBe(true); + }); + }); + + it("hides nothing for a client that chose no founder", () => { + const filter = filterFor(undefined); + expect(matchesThreadListFilter(thread(cos, { standing: true }), filter)).toBe(true); + expect(matchesThreadListFilter(thread(cosJeff, { standing: true }), filter)).toBe(true); + }); +}); diff --git a/packages/client-runtime/src/state/threadListFilter.ts b/packages/client-runtime/src/state/threadListFilter.ts index 352045424b1e5..ece932155f5e1 100644 --- a/packages/client-runtime/src/state/threadListFilter.ts +++ b/packages/client-runtime/src/state/threadListFilter.ts @@ -1,6 +1,8 @@ import { effectiveInboxFilter, + type Founder, InboxChoice, + isHiddenFromFounder, matchesPrimaryInbox, primaryInbox, type EnvironmentId, @@ -16,11 +18,16 @@ export interface ThreadListFilter { readonly inboxes: ReadonlyArray; readonly hiddenEnvironmentIds: ReadonlyArray; readonly hiddenAttributions: ReadonlyArray; + /** + * The founder reading this list. Jeff's list never holds Kameron's private threads or bots, + * whatever the inbox checkboxes say (`isHiddenFromFounder`). Absent: no founder chosen. + */ + readonly viewingFounder?: Founder | undefined; } interface FilterableThread { readonly environmentId: EnvironmentId; - /** Set on a standing bot. Those stay listed for both founders. */ + /** Set on a standing bot. Those bypass the inbox and From checkboxes. */ readonly standing?: unknown; readonly archivedAt?: unknown; readonly source: { @@ -41,6 +48,7 @@ export const threadListFilterOf = ( inboxes: effectiveInboxFilter(settings), hiddenEnvironmentIds: settings.sidebarHiddenEnvironmentIds ?? [], hiddenAttributions: settings.sidebarHiddenThreadAttributions ?? [], + viewingFounder: settings.founderInboxOwner, }); /** Inbox membership from what a thread or an older settled group carries. */ @@ -53,7 +61,7 @@ export const matchesListedInboxes = ( // Agent-created threads from before inbox routing carry no Agents routing. (inboxes.includes("agents") && agentCreated && inbox === null); -/** A non-archived standing bot stays in the list for both founders. */ +/** A non-archived standing bot stays in the list unless it is private to the other founder. */ export const standingBotBypassesListFilters = (thread: { readonly standing?: unknown; readonly archivedAt?: unknown; @@ -66,6 +74,8 @@ export const matchesThreadListFilter = ( attribution?: ThreadAttribution, ) => { if (filter.hiddenEnvironmentIds.includes(thread.environmentId)) return false; + // Private to the other founder: hidden even for a standing bot, which otherwise bypasses. + if (isHiddenFromFounder(thread.source.inbox, filter.viewingFounder)) return false; // Inbox and From checkboxes hide ordinary threads. A standing bot is hidden only by // archiving it or by hiding its environment. Background attention must not drop it. if (standingBotBypassesListFilters(thread)) return true; @@ -87,6 +97,8 @@ export const matchesOlderSettledGroup = ( owners: FounderByEnvironmentId, ) => !filter.hiddenEnvironmentIds.includes(environmentId) && + // A group counts only its primary inbox, so Kameron-filed groups are the ones Jeff can tell apart. + !(filter.viewingFounder === "jeff" && group.inbox === "kameron") && !filter.hiddenAttributions.includes(attributionOfSender(group, owners[environmentId])) && matchesListedInboxes(group.inbox, group.agentCreated, filter.inboxes); diff --git a/packages/client-runtime/src/state/threadShell.test.ts b/packages/client-runtime/src/state/threadShell.test.ts index de1bc04aeda8a..6a4208bc8d0c5 100644 --- a/packages/client-runtime/src/state/threadShell.test.ts +++ b/packages/client-runtime/src/state/threadShell.test.ts @@ -1,11 +1,13 @@ import { EnvironmentId, + type Founder, ProjectId, RunId, RuntimeRequestId, ThreadId, type OrchestrationV2ShellSnapshot, type OrchestrationV2ThreadShell, + makeThreadInbox, } from "@t3tools/contracts"; import * as DateTime from "effect/DateTime"; import * as Option from "effect/Option"; @@ -26,7 +28,10 @@ const environmentId = EnvironmentId.make("environment-v2"); const remoteEnvironmentId = EnvironmentId.make("remote-environment-v2"); const otherProjectId = ProjectId.make("other-project"); -function makeHarness(environmentIds: ReadonlyArray = [environmentId]) { +function makeHarness( + environmentIds: ReadonlyArray = [environmentId], + viewerFounderAtom?: Atom.Atom, +) { const snapshotAtom = Atom.family((_environmentId: EnvironmentId) => Atom.make(v2ShellSnapshot), ); @@ -56,10 +61,70 @@ function makeHarness(environmentIds: ReadonlyArray = [environment catalogValueAtom, snapshotAtom, hiddenThreadTagsAtom: () => Atom.make(null), + ...(viewerFounderAtom === undefined ? {} : { viewerFounderAtom }), }), }; } +describe("asymmetric founder visibility in thread shell lists", () => { + const withInbox = ( + id: string, + owner: Founder | null, + audience: string, + ): OrchestrationV2ThreadShell => ({ + ...v2ThreadShell, + id: ThreadId.make(id), + inbox: { ...makeThreadInbox({ actor: null }), owner, audience: audience as never }, + }); + const snapshot = { + ...v2ShellSnapshot, + threads: [ + withInbox("cos", "kameron", "kameron"), + withInbox("cos-jeff", "jeff", "jeff"), + withInbox("shared-bot", null, "shared"), + withInbox("kameron-agents", "kameron", "agent-only"), + ], + }; + const idsOf = (list: ReadonlyArray<{ readonly id: ThreadId }>) => list.map((thread) => thread.id); + + it("keeps Kameron's private threads out of Jeff's lists and Jeff's in Kameron's", () => { + const viewer = Atom.make("jeff"); + const { registry, threads, snapshotAtom } = makeHarness([environmentId], viewer); + registry.set(snapshotAtom(environmentId), snapshot); + const disposers = [ + registry.mount(threads.hiddenThreadKeysAtom), + registry.mount(threads.navigationThreadShellsAtom), + registry.mount(threads.visibleThreadShellsAtom), + registry.mount(threads.quietThreadShellsAtom), + ]; + const everyList = () => + [ + threads.navigationThreadShellsAtom, + threads.visibleThreadShellsAtom, + threads.quietThreadShellsAtom, + ].map((atom) => idsOf(registry.get(atom))); + for (const ids of everyList()) expect(ids).toEqual(["cos-jeff", "shared-bot"]); + // Point reads stay complete, so outboxes and drafts never mistake a hidden thread for gone. + expect(registry.get(threads.threadShellsAtom)).toHaveLength(4); + + registry.set(viewer, "kameron"); + for (const ids of everyList()) { + expect(ids).toEqual(["cos", "cos-jeff", "shared-bot", "kameron-agents"]); + } + for (const dispose of disposers) dispose(); + registry.dispose(); + }); + + it("hides nothing on a client with no viewer founder", () => { + const { registry, threads, snapshotAtom } = makeHarness(); + registry.set(snapshotAtom(environmentId), snapshot); + const dispose = registry.mount(threads.visibleThreadShellsAtom); + expect(registry.get(threads.visibleThreadShellsAtom)).toHaveLength(4); + dispose(); + registry.dispose(); + }); +}); + describe("v2 thread shell lists", () => { it("preserves ordered reference arrays when a middle thread changes", () => { const { registry, threads, snapshotAtom } = makeHarness(); diff --git a/packages/client-runtime/src/state/threadShell.ts b/packages/client-runtime/src/state/threadShell.ts index 9241ca8cc5631..b98409ca4cedd 100644 --- a/packages/client-runtime/src/state/threadShell.ts +++ b/packages/client-runtime/src/state/threadShell.ts @@ -669,6 +669,7 @@ export function createEnvironmentThreadShellAtoms(input: { // exists (outboxes, drafts, removals) never mistakes a hidden thread for a missing one. let previousHiddenThreadKeys: ReadonlySet = NO_HIDDEN_THREADS; const hiddenThreadKeysAtom = Atom.make((get) => { + const viewer = viewerOf(get); const environments = Array.from( enabledEnvironmentIds(get(input.catalogValueAtom)), (environmentId) => ({ @@ -676,8 +677,9 @@ export function createEnvironmentThreadShellAtoms(input: { hiddenTags: get(input.hiddenThreadTagsAtom(environmentId)), }), ); - // Read shells only while some environment hides tags, so streaming ticks cost nothing otherwise. - if (!environments.some((environment) => environment.hiddenTags !== null)) { + // Read shells only while some environment hides tags or Jeff is viewing, so streaming ticks + // cost nothing otherwise. Jeff's lists also leave out Kameron's private threads. + if (viewer !== "jeff" && !environments.some((environment) => environment.hiddenTags !== null)) { previousHiddenThreadKeys = NO_HIDDEN_THREADS; return NO_HIDDEN_THREADS; } @@ -686,6 +688,10 @@ export function createEnvironmentThreadShellAtoms(input: { ...environment, threads: get(environmentThreadsAtom(environment.environmentId)), })), + undefined, + undefined, + undefined, + viewer, ); if ( next.size === previousHiddenThreadKeys.size && diff --git a/packages/client-runtime/src/state/threadTagFilter.test.ts b/packages/client-runtime/src/state/threadTagFilter.test.ts index d38e5386d5243..563478095b2d1 100644 --- a/packages/client-runtime/src/state/threadTagFilter.test.ts +++ b/packages/client-runtime/src/state/threadTagFilter.test.ts @@ -51,6 +51,11 @@ const hiddenIds = (localTags: Array | null, linkedTags: Array | ].toSorted(); const key = (environmentId: EnvironmentId, id: string) => threadKey({ environmentId, threadId: ThreadId.make(id) }); +const withInbox = ( + entry: ReturnType, + owner: "kameron" | "jeff" | null, + audience: string | undefined, +) => ({ ...entry, inbox: { owner, audience } as never }); describe("hiddenThreadKeys", () => { it("hides nothing while every filter is off", () => { @@ -124,4 +129,84 @@ describe("hiddenThreadKeys", () => { ); expect([...archived]).toEqual([key(linked, "delegated-from-private")]); }); + + describe("founder viewer", () => { + const threads = [ + withInbox(thread("cos", []), "kameron", "kameron"), + withInbox( + thread("cos-subagent", [], { id: "cos", relationship: "subagent" }), + null, + "agent-only", + ), + withInbox(thread("cos-agents-only", []), "kameron", "agent-only"), + withInbox(thread("kameron-shared", []), "kameron", "shared"), + withInbox(thread("kameron-jeff", []), "kameron", "jeff"), + withInbox(thread("cos-jeff", []), "jeff", "jeff"), + withInbox(thread("shared", []), null, "shared"), + thread("no-inbox", []), + ]; + const hiddenFor = (viewer: "kameron" | "jeff" | null) => + [ + ...hiddenThreadKeys( + [{ environmentId: local, threads, hiddenTags: null }], + undefined, + undefined, + undefined, + viewer, + ), + ].toSorted(); + + it("hides Kameron's private threads, and their subagents, from Jeff", () => { + expect(hiddenFor("jeff")).toEqual( + [key(local, "cos"), key(local, "cos-subagent"), key(local, "cos-agents-only")].toSorted(), + ); + }); + + it("hides nothing from Kameron or from a client without a founder", () => { + expect(hiddenFor("kameron")).toEqual([]); + expect(hiddenFor(null)).toEqual([]); + }); + + it("keeps hiding private threads for Jeff while tags are hidden as well", () => { + const keys = hiddenThreadKeys( + [ + { + environmentId: local, + threads: [...threads, { ...thread("tagged", ["private"]) }], + hiddenTags: ["private"], + }, + ], + undefined, + undefined, + undefined, + "jeff", + ); + expect([...keys]).toContain(key(local, "tagged")); + expect([...keys]).toContain(key(local, "cos")); + expect([...keys]).not.toContain(key(local, "cos-jeff")); + }); + + it("judges an archive page's private thread whose parent is loaded elsewhere", () => { + const archived = hiddenThreadKeys( + [ + { + environmentId: linked, + threads: [ + withInbox( + thread("archived-private", [], undefined, { environmentId: local, id: "plain" }), + "kameron", + "kameron", + ), + ], + hiddenTags: null, + }, + ], + NO_HIDDEN_THREADS, + new Set([key(local, "plain")]), + true, + "jeff", + ); + expect([...archived]).toEqual([key(linked, "archived-private")]); + }); + }); }); diff --git a/packages/client-runtime/src/state/threadTagFilter.ts b/packages/client-runtime/src/state/threadTagFilter.ts index 78e314445bfb5..85c759a59f7f6 100644 --- a/packages/client-runtime/src/state/threadTagFilter.ts +++ b/packages/client-runtime/src/state/threadTagFilter.ts @@ -1,5 +1,7 @@ import { type EnvironmentId, + type Founder, + isHiddenFromFounder, isListedThreadLineage, type OrchestrationV2ThreadShell, type ScopedThreadRef, @@ -10,7 +12,7 @@ import { threadKey } from "./entities.ts"; type FilterableThread = Pick< OrchestrationV2ThreadShell, - "id" | "lineage" | "tags" | "delegatedFrom" + "id" | "lineage" | "tags" | "delegatedFrom" | "inbox" >; export interface TagFilteredEnvironment { @@ -63,18 +65,18 @@ function hasHiddenTag( } /** - * Keys of threads the tag filter hides. Children (subagents, side chats, delegated tasks) follow - * their parent; while any environment hides tags, a child whose parent is not loaded stays hidden. - * For a partial list such as an archive page, `alreadyHidden` and `loadedElsewhere` give the - * hidden and loaded keys of live threads, and `hidingAnywhere` the global flag. + * Keys of threads the tag filter hides. Children follow their parent; while any environment hides + * tags, a child whose parent is not loaded stays hidden. `alreadyHidden`, `loadedElsewhere` and + * `hidingAnywhere` judge an archive page. A `viewer` also hides the other founder's private threads. */ export function hiddenThreadKeys( environments: ReadonlyArray, alreadyHidden: ReadonlySet = NO_HIDDEN_THREADS, loadedElsewhere: ReadonlySet = NO_HIDDEN_THREADS, hidingAnywhere: boolean = environments.some((environment) => environment.hiddenTags !== null), + viewer: Founder | null = null, ): ReadonlySet { - if (alreadyHidden.size === 0 && !hidingAnywhere) return NO_HIDDEN_THREADS; + if (alreadyHidden.size === 0 && !hidingAnywhere && viewer !== "jeff") return NO_HIDDEN_THREADS; const byKey = new Map< string, { thread: FilterableThread; environment: TagFilteredEnvironment } @@ -99,21 +101,24 @@ export function hiddenThreadKeys( const followsParent = parentKey !== null && (byKey.has(parentKey) || alreadyHidden.has(parentKey)); if (parentKey !== null && !followsParent && loadedElsewhere.has(parentKey)) { - hiddenByKey.set(key, false); - return false; + const hidden = isHiddenFromFounder(thread.inbox, viewer); + hiddenByKey.set(key, hidden); + return hidden; } if (parentKey !== null && !followsParent && hidingAnywhere) { hiddenByKey.set(key, true); return true; } if (!followsParent) { - const hidden = hasHiddenTag(thread.tags, environment.hiddenTags); + const hidden = + hasHiddenTag(thread.tags, environment.hiddenTags) || + isHiddenFromFounder(thread.inbox, viewer); hiddenByKey.set(key, hidden); return hidden; } // Seed before following the parent so a malformed cycle ends visible. hiddenByKey.set(key, false); - const hidden = isHidden(parentKey); + const hidden = isHiddenFromFounder(thread.inbox, viewer) || isHidden(parentKey); hiddenByKey.set(key, hidden); return hidden; }; diff --git a/packages/contracts/src/threadInbox.test.ts b/packages/contracts/src/threadInbox.test.ts index 570ec83f8ef4e..c92d313e81c15 100644 --- a/packages/contracts/src/threadInbox.test.ts +++ b/packages/contracts/src/threadInbox.test.ts @@ -22,6 +22,7 @@ import { projectThreadInbox, inboxAfterFounderReply, inboxRoutingFor, + isHiddenFromFounder, primaryInbox, withLegacyPeerInboxReason, type ThreadInboxRoutingPatch, @@ -652,3 +653,58 @@ describe("joint sign-off", () => { ]); }); }); + +describe("isHiddenFromFounder", () => { + const inbox = (owner: "kameron" | "jeff" | null | undefined, audience: string | undefined) => ({ + owner, + audience, + }); + + it("hides Kameron's private threads and bots from Jeff", () => { + expect(isHiddenFromFounder(inbox("kameron", "kameron"), "jeff")).toBe(true); + // Audience Kameron hides whoever owns it. + expect(isHiddenFromFounder(inbox(null, "kameron"), "jeff")).toBe(true); + expect(isHiddenFromFounder(inbox("jeff", "kameron"), "jeff")).toBe(true); + expect(isHiddenFromFounder(inbox(undefined, "kameron"), "jeff")).toBe(true); + }); + + it("hides Kameron-owned threads filed neither Shared nor to Jeff", () => { + expect(isHiddenFromFounder(inbox("kameron", "agent-only"), "jeff")).toBe(true); + expect(isHiddenFromFounder(inbox("kameron", undefined), "jeff")).toBe(true); + }); + + it("keeps Kameron-owned threads filed Shared or to Jeff visible to Jeff", () => { + expect(isHiddenFromFounder(inbox("kameron", "shared"), "jeff")).toBe(false); + expect(isHiddenFromFounder(inbox("kameron", "jeff"), "jeff")).toBe(false); + }); + + it("keeps Jeff's, shared and unowned threads visible to Jeff", () => { + expect(isHiddenFromFounder(inbox("jeff", "jeff"), "jeff")).toBe(false); + expect(isHiddenFromFounder(inbox(null, "shared"), "jeff")).toBe(false); + expect(isHiddenFromFounder(inbox(null, "agent-only"), "jeff")).toBe(false); + expect(isHiddenFromFounder(inbox(null, undefined), "jeff")).toBe(false); + }); + + it("never hides a thread without an inbox", () => { + expect(isHiddenFromFounder(undefined, "jeff")).toBe(false); + expect(isHiddenFromFounder(null, "jeff")).toBe(false); + expect(isHiddenFromFounder({}, "jeff")).toBe(false); + }); + + it("hides nothing from Kameron, so Jeff's private threads stay in his lists", () => { + for (const entry of [ + inbox("jeff", "jeff"), + inbox("kameron", "kameron"), + inbox("kameron", "agent-only"), + inbox("jeff", undefined), + inbox(null, "shared"), + ]) { + expect(isHiddenFromFounder(entry, "kameron")).toBe(false); + } + }); + + it("hides nothing from a client without a founder", () => { + expect(isHiddenFromFounder(inbox("kameron", "kameron"), undefined)).toBe(false); + expect(isHiddenFromFounder(inbox("kameron", "kameron"), null)).toBe(false); + }); +}); diff --git a/packages/contracts/src/threadInbox.ts b/packages/contracts/src/threadInbox.ts index 9c29b4080a47b..affc5e118ce68 100644 --- a/packages/contracts/src/threadInbox.ts +++ b/packages/contracts/src/threadInbox.ts @@ -454,6 +454,26 @@ export function founderInboxLane(inbox: ThreadInbox | null | undefined, founder: return inbox.reason === "fyi" && inbox.attentionKind === "action" ? "FYI" : inbox.attentionKind; } +/** + * Whether a thread is private to Kameron, so Jeff's lists must leave it out. Asymmetric: Kameron + * and a client with no founder see everything. Hidden for Jeff: audience Kameron, or Kameron-owned + * and filed neither Shared nor to Jeff. A thread without an inbox stays visible. + */ +export function isHiddenFromFounder( + inbox: + | { + readonly owner?: Founder | null | undefined; + readonly audience?: string | null | undefined; + } + | null + | undefined, + viewer: Founder | null | undefined, +): boolean { + if (viewer !== "jeff" || !inbox) return false; + if (inbox.audience === "kameron") return true; + return inbox.owner === "kameron" && inbox.audience !== "shared" && inbox.audience !== "jeff"; +} + /** * The inboxes the sidebar lists: the chosen ones, else the chosen founder's plus Agents and * Shared, else every inbox so unclassified history stays visible. Shared is a default so both