From 6c93e807f4f46ddfd38a0035e03eecec4e98da14 Mon Sep 17 00:00:00 2001 From: devinfoley <139239+devinfoley@users.noreply.github.com> Date: Fri, 4 Dec 2026 01:35:02 +0000 Subject: [PATCH] fix(ui): address review on the manager picker sort Drop the doc comment, build the name collator once, index disabled agent ids in a Set instead of scanning per row, and add a 1,000-agent test that bounds name reads with a throwing counter. Co-Authored-By: Claude Opus 5.5 --- ui/src/components/ReportsToPicker.test.ts | 24 +++++++++++++++++++++++ ui/src/components/ReportsToPicker.tsx | 10 ++++++---- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/ui/src/components/ReportsToPicker.test.ts b/ui/src/components/ReportsToPicker.test.ts index b887e510e35..4b41f83be69 100644 --- a/ui/src/components/ReportsToPicker.test.ts +++ b/ui/src/components/ReportsToPicker.test.ts @@ -26,6 +26,30 @@ describe("reportsToPickerRows", () => { ]); }); + it("sorts a large agent directory with n log n name reads", () => { + const size = 1000; + const limit = 2 * size * Math.ceil(Math.log2(size)) + size; + let nameReads = 0; + const agents = Array.from({ length: size }, (_, index) => { + const name = `Agent ${(index * 7919) % size}`; + const row = { id: String(index), status: "idle" } as Agent; + Object.defineProperty(row, "name", { + get() { + nameReads += 1; + if (nameReads > limit) throw new Error(`name read more than ${limit} times`); + return name; + }, + }); + return row; + }); + + const rows = reportsToPickerRows(agents); + + expect(rows).toHaveLength(size); + expect(rows[0]!.name).toBe("Agent 0"); + expect(rows[size - 1]!.name).toBe(`Agent ${size - 1}`); + }); + it("drops terminated and excluded agents without mutating the input", () => { const agents = [agent("1", "Zed"), agent("2", "Gone", "terminated"), agent("3", "Self"), agent("4", "Amy")]; const rows = reportsToPickerRows(agents, ["3"]); diff --git a/ui/src/components/ReportsToPicker.tsx b/ui/src/components/ReportsToPicker.tsx index 9525ef7eb2f..70b0049ed28 100644 --- a/ui/src/components/ReportsToPicker.tsx +++ b/ui/src/components/ReportsToPicker.tsx @@ -10,12 +10,13 @@ import { FlyoutAppearanceProvider, FlyoutHeader, FlyoutOptions } from "./ui/flyo import { Check, User } from "lucide-react"; import { cn } from "../lib/utils"; -/** Candidate managers in alphabetical order, without terminated or excluded agents. */ +const managerNameCollator = new Intl.Collator(undefined, { sensitivity: "base", numeric: true }); + export function reportsToPickerRows(agents: Agent[], excludeAgentIds: string[] = []): Agent[] { const exclude = new Set(excludeAgentIds); return agents .filter((a) => a.status !== "terminated" && !exclude.has(a.id)) - .sort((left, right) => left.name.localeCompare(right.name, undefined, { sensitivity: "base", numeric: true })); + .sort((left, right) => managerNameCollator.compare(left.name, right.name)); } export function ReportsToPicker({ @@ -41,6 +42,7 @@ export function ReportsToPicker({ }) { const [open, setOpen] = useState(false); const rows = reportsToPickerRows(agents, excludeAgentIds); + const disabledIds = new Set(disabledAgentIds); const current = value ? agents.find((a) => a.id === value) : null; const terminatedManager = current?.status === "terminated"; const unknownManager = Boolean(value && !current); @@ -121,8 +123,8 @@ export function ReportsToPicker({ type="button" key={a.id} aria-pressed={a.id === value} - disabled={disabledAgentIds.includes(a.id)} - title={disabledAgentIds.includes(a.id) ? "Would create a reporting loop" : undefined} + disabled={disabledIds.has(a.id)} + title={disabledIds.has(a.id) ? "Would create a reporting loop" : undefined} className={cn( "flex items-center gap-2 w-full min-w-0 px-2 py-1.5 text-xs rounded hover:bg-accent/50 overflow-hidden disabled:opacity-50 disabled:cursor-not-allowed", a.id === value && "bg-accent",