From 4b480bedad96cf765ffffe6b067ff91548349221 Mon Sep 17 00:00:00 2001 From: Kameron Smith Date: Mon, 23 Nov 2026 23:27:20 -0500 Subject: [PATCH] fix(web): keep standing bots listed when a scheduled task binds their thread; collapsible bot tree A fresh-session scheduled task that binds a standing bot's own thread put that thread in scheduledThreadKeys, so isSystemScheduledThread moved it to the System shelf before the bot groups saw it. CEO, CTO and CoS vanished (and the org tree went flat) once their scheduled tasks ran after app start. A thread with a standing persona now only goes to the System shelf by the sys:daemon role. Also make the bot tree collapsible: a chevron on any bot with reports folds its subtree, and each group header (My Bots, Shared Bots) folds the group. State persists in uiStateStore as botCollapsedById. Co-Authored-By: Claude Sonnet 5.5 --- apps/web/src/components/Sidebar.logic.test.ts | 67 +++++ apps/web/src/components/Sidebar.logic.ts | 12 +- apps/web/src/components/Sidebar.tsx | 1 + .../src/components/bots/botOrgTree.test.ts | 23 +- apps/web/src/components/bots/botOrgTree.ts | 29 +++ .../components/sidebar/SidebarBotsGroup.tsx | 239 +++++++++++------- apps/web/src/uiStateStore.test.ts | 21 ++ apps/web/src/uiStateStore.ts | 24 +- 8 files changed, 318 insertions(+), 98 deletions(-) diff --git a/apps/web/src/components/Sidebar.logic.test.ts b/apps/web/src/components/Sidebar.logic.test.ts index 14e6f619d7cb5..a37375d4e4b22 100644 --- a/apps/web/src/components/Sidebar.logic.test.ts +++ b/apps/web/src/components/Sidebar.logic.test.ts @@ -2,6 +2,7 @@ import { presentThreadShell } from "@t3tools/client-runtime/state/models"; import { threadKey } from "@t3tools/client-runtime/state/entities"; import { isThreadWorking as isSidebarThreadWorking } from "@t3tools/client-runtime/state/thread-inbox"; import * as DateTime from "effect/DateTime"; +import { partitionBotGroups, splitStandingThreads } from "./bots/botPresence"; import { deriveActiveWorkStartedAt } from "../session-logic.ts"; import { describe, expect, it } from "vite-plus/test"; import * as Cause from "effect/Cause"; @@ -1810,3 +1811,69 @@ describe("placeSystemScheduledThreads", () => { expect(place(false).section).toBeNull(); }); }); + +describe("standing bots bound by a scheduled task", () => { + const cosId = ThreadId.make("cos"); + const ctoId = ThreadId.make("cto"); + const ordinaryId = ThreadId.make("digest"); + const keyOf = (id: ThreadId) => scopedThreadKey(scopeThreadRef(localEnvironmentId, id)); + const shell = ( + id: ThreadId, + standing: { name: string; role: string } | null, + audience: string, + ) => ({ + id, + standing, + source: { inbox: { audience } }, + }); + const threads = [ + shell(cosId, { name: "CoS", role: "Chief of Staff" }, "kameron"), + shell(ctoId, { name: "CTO", role: "Engineering & Architecture" }, "shared"), + shell(ordinaryId, null, "kameron"), + ]; + const listedBots = (scheduledThreadKeys: ReadonlySet) => { + const { rest } = placeSystemScheduledThreads({ + threads, + revealed: false, + scheduledThreadKeys, + keyOf: (thread) => keyOf(thread.id), + standingRoleOf: (thread) => thread.standing?.role ?? null, + isStandingOf: (thread) => thread.standing !== null, + }); + return partitionBotGroups(splitStandingThreads(rest).bots); + }; + + it("keeps every standing bot listed when a scheduled task binds its thread after the snapshot", () => { + const snapshot = listedBots(new Set()); + expect(snapshot.mine.map((thread) => thread.id)).toEqual([cosId]); + expect(snapshot.shared.map((thread) => thread.id)).toEqual([ctoId]); + + // A running scheduled task now reports these threads as its fresh-session threads. + const updated = listedBots( + freshSessionThreadKeys([ + { + environmentId: localEnvironmentId, + tasks: [ + { threadId: cosId, threadMode: "fresh_session" as const }, + { threadId: ctoId, threadMode: "fresh_session" as const }, + { threadId: ordinaryId, threadMode: "fresh_session" as const }, + ], + }, + ]), + ); + expect(updated).toEqual(snapshot); + }); + + it("still sends an ordinary scheduled thread to the System shelf", () => { + const keys = new Set([keyOf(ordinaryId)]); + const { system } = placeSystemScheduledThreads({ + threads, + revealed: true, + scheduledThreadKeys: keys, + keyOf: (thread) => keyOf(thread.id), + standingRoleOf: (thread) => thread.standing?.role ?? null, + isStandingOf: (thread) => thread.standing !== null, + }); + expect(system.map((thread) => thread.id)).toEqual([ordinaryId]); + }); +}); diff --git a/apps/web/src/components/Sidebar.logic.ts b/apps/web/src/components/Sidebar.logic.ts index 79263ffcbcd1c..bf2bbb5d1cd9f 100644 --- a/apps/web/src/components/Sidebar.logic.ts +++ b/apps/web/src/components/Sidebar.logic.ts @@ -1492,11 +1492,15 @@ export function sortScopedProjectsForSidebar< export function isSystemScheduledThread(input: { readonly key: string; readonly standingRole: string | null; + /** A standing bot has a persona; it is the founder's conversation, never a task's output. */ + readonly standing?: boolean; readonly scheduledThreadKeys: ReadonlySet; }): boolean { - return ( - input.standingRole === SYS_DAEMON_STANDING_ROLE || input.scheduledThreadKeys.has(input.key) - ); + if (input.standingRole === SYS_DAEMON_STANDING_ROLE) return true; + // A scheduled task can bind a standing bot's own thread; that must not pull the bot off + // the Bots shelf and out of the sidebar's lists. + if (input.standing === true || input.standingRole !== null) return false; + return input.scheduledThreadKeys.has(input.key); } /** Scoped keys of fresh-session scheduled tasks. Unbound and continue-mode tasks are left out. */ @@ -1530,6 +1534,7 @@ export function placeSystemScheduledThreads(input: { readonly scheduledThreadKeys: ReadonlySet; readonly keyOf: (thread: T) => string; readonly standingRoleOf: (thread: T) => string | null; + readonly isStandingOf?: (thread: T) => boolean; }): { readonly rest: T[]; readonly system: T[]; @@ -1542,6 +1547,7 @@ export function placeSystemScheduledThreads(input: { isSystemScheduledThread({ key: input.keyOf(thread), standingRole: input.standingRoleOf(thread), + standing: input.isStandingOf?.(thread) ?? false, scheduledThreadKeys: input.scheduledThreadKeys, }) ) { diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsx index 239e8c3e40ee8..7a422b035ab55 100644 --- a/apps/web/src/components/Sidebar.tsx +++ b/apps/web/src/components/Sidebar.tsx @@ -1249,6 +1249,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve isSystemScheduledThread({ key: threadShellKey(thread), standingRole: thread.standing?.role ?? null, + standing: thread.standing != null, scheduledThreadKeys, }); const conversationThreads = inboxThreads.filter((thread) => !isSystemThread(thread)); diff --git a/apps/web/src/components/bots/botOrgTree.test.ts b/apps/web/src/components/bots/botOrgTree.test.ts index 2bd3ce7ace3bd..dd3d9922cb218 100644 --- a/apps/web/src/components/bots/botOrgTree.test.ts +++ b/apps/web/src/components/bots/botOrgTree.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from "vite-plus/test"; -import { isFounderName, orderBotTree } from "./botOrgTree"; +import { isFounderName, orderBotTree, visibleBotRows } from "./botOrgTree"; import { splitStandingThreads } from "./botPresence"; const bot = (name: string) => ({ id: name, standing: { name } }); @@ -74,3 +74,24 @@ describe("founders are not bots", () => { expect(rest.map((t) => t.id)).toEqual(["k", "w"]); }); }); + +describe("visibleBotRows", () => { + const rows = orderBotTree(["CEO", "CTO", "Platform", "CFO", "CoS"].map(bot)); + const shown = (collapsed: string[]) => + visibleBotRows(rows, (thread) => collapsed.includes(thread.id)).map( + (row) => `${row.depth}:${row.thread.id}${row.hasReports ? (row.collapsed ? "+" : "-") : ""}`, + ); + + it("marks bots with reports and lists everything when nothing is collapsed", () => { + expect(shown([])).toEqual(["0:CoS", "0:CEO-", "1:CTO-", "2:Platform", "1:CFO"]); + }); + + it("hides a collapsed bot's whole subtree but not its siblings", () => { + expect(shown(["CTO"])).toEqual(["0:CoS", "0:CEO-", "1:CTO+", "1:CFO"]); + expect(shown(["CEO"])).toEqual(["0:CoS", "0:CEO+"]); + }); + + it("ignores a collapse flag on a bot with no reports", () => { + expect(shown(["Platform"])).toEqual(shown([])); + }); +}); diff --git a/apps/web/src/components/bots/botOrgTree.ts b/apps/web/src/components/bots/botOrgTree.ts index 4fc55fcbbf0ce..d2d9c80c48c94 100644 --- a/apps/web/src/components/bots/botOrgTree.ts +++ b/apps/web/src/components/bots/botOrgTree.ts @@ -91,3 +91,32 @@ export function orderBotTree(threads: readonly T[]): BotTreeRow for (const root of roots.toSorted(compare)) visit(root, 0); return rows; } + +export interface VisibleBotRow extends BotTreeRow { + /** Whether this bot has reports, so it gets a chevron. */ + readonly hasReports: boolean; + readonly collapsed: boolean; +} + +/** + * Drops the rows under a collapsed bot. `rows` is `orderBotTree` output (depth-first), so a bot's + * reports are the rows right after it that sit deeper. A collapsed bot still shows its own row. + */ +export function visibleBotRows( + rows: readonly BotTreeRow[], + isCollapsed: (thread: T) => boolean, +): VisibleBotRow[] { + const visible: VisibleBotRow[] = []; + let hideBelowDepth: number | null = null; + rows.forEach((row, index) => { + if (hideBelowDepth !== null) { + if (row.depth > hideBelowDepth) return; + hideBelowDepth = null; + } + const hasReports = (rows[index + 1]?.depth ?? 0) > row.depth; + const collapsed = hasReports && isCollapsed(row.thread); + visible.push({ ...row, hasReports, collapsed }); + if (collapsed) hideBelowDepth = row.depth; + }); + return visible; +} diff --git a/apps/web/src/components/sidebar/SidebarBotsGroup.tsx b/apps/web/src/components/sidebar/SidebarBotsGroup.tsx index 70c16165df19d..68fd9b229bc76 100644 --- a/apps/web/src/components/sidebar/SidebarBotsGroup.tsx +++ b/apps/web/src/components/sidebar/SidebarBotsGroup.tsx @@ -9,9 +9,9 @@ import { } from "@t3tools/contracts"; import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment"; import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/models"; +import { ChevronRightIcon } from "lucide-react"; import { memo, - useCallback, useEffect, useState, type MouseEvent as ReactMouseEvent, @@ -25,11 +25,14 @@ import { contextMenuSourceOf } from "../../contextMenuSource"; import { useThreadSelectionStore } from "../../threadSelectionStore"; import { useUiStateStore } from "../../uiStateStore"; import { BotAvatar, BOT_ACCENT_CLASSES } from "../bots/BotAvatar"; -import { orderBotTree } from "../bots/botOrgTree"; +import { orderBotTree, visibleBotRows } from "../bots/botOrgTree"; import { resolveBotPresence } from "../bots/botPresence"; import { resolveThreadLastVisitedAt, resolveThreadStatusPill } from "../Sidebar.logic"; import { useClientSettings } from "../../hooks/useSettings"; +// One shared empty value, so an idle environment never looks like a change. +const NO_TYPING: TypingByThread = {}; + interface BotRowHandlers { readonly onThreadClick: (event: ReactMouseEvent, threadRef: ScopedThreadRef) => void; readonly onThreadActivate: (threadRef: ScopedThreadRef) => void; @@ -57,14 +60,22 @@ export function SidebarBotsGroup( const [typingByEnvironment, setTypingByEnvironment] = useState< Readonly> >({}); - const onTyping = useCallback((environmentId: string, next: TypingByThread) => { + const onTyping = (environmentId: string, next: TypingByThread) => { setTypingByEnvironment((current) => current[environmentId] === next ? current : { ...current, [environmentId]: next }, ); - }, []); - const rows = orderBotTree(props.threads); - if (props.threads.length === 0) return null; + }; + const botCollapsedById = useUiStateStore((state) => state.botCollapsedById); + const setBotCollapsed = useUiStateStore((state) => state.setBotCollapsed); const label = props.label ?? "Bots"; + const groupKey = `group:${label}`; + const groupCollapsed = botCollapsedById[groupKey] === true; + const rows = visibleBotRows( + orderBotTree(props.threads), + (thread) => + botCollapsedById[scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id))] === true, + ); + if (props.threads.length === 0) return null; const environmentIds = [...new Set(props.threads.map((thread) => thread.environmentId))]; return (
( ))} -
+
-
    - {rows.map(({ thread, depth }) => { + + +