Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions apps/web/src/components/Sidebar.logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<string>) => {
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]);
});
});
12 changes: 9 additions & 3 deletions apps/web/src/components/Sidebar.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>;
}): 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. */
Expand Down Expand Up @@ -1530,6 +1534,7 @@ export function placeSystemScheduledThreads<T>(input: {
readonly scheduledThreadKeys: ReadonlySet<string>;
readonly keyOf: (thread: T) => string;
readonly standingRoleOf: (thread: T) => string | null;
readonly isStandingOf?: (thread: T) => boolean;
}): {
readonly rest: T[];
readonly system: T[];
Expand All @@ -1542,6 +1547,7 @@ export function placeSystemScheduledThreads<T>(input: {
isSystemScheduledThread({
key: input.keyOf(thread),
standingRole: input.standingRoleOf(thread),
standing: input.isStandingOf?.(thread) ?? false,
scheduledThreadKeys: input.scheduledThreadKeys,
})
) {
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
23 changes: 22 additions & 1 deletion apps/web/src/components/bots/botOrgTree.test.ts
Original file line number Diff line number Diff line change
@@ -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 } });
Expand Down Expand Up @@ -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([]));
});
});
29 changes: 29 additions & 0 deletions apps/web/src/components/bots/botOrgTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,32 @@ export function orderBotTree<T extends Named>(threads: readonly T[]): BotTreeRow
for (const root of roots.toSorted(compare)) visit(root, 0);
return rows;
}

export interface VisibleBotRow<T> extends BotTreeRow<T> {
/** 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<T>(
rows: readonly BotTreeRow<T>[],
isCollapsed: (thread: T) => boolean,
): VisibleBotRow<T>[] {
const visible: VisibleBotRow<T>[] = [];
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;
}
Loading
Loading