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 <noreply@anthropic.com>
This commit is contained in:
devinfoleyandClaude Opus 5.5 committed 2026-12-04 01:35:02 +00:00
1 parent 18cb8b75ac
commit 6c93e807f4
2 files changed
+30 -4

No files matched your search

+24
View File
@@ -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"]);
+6 -4
View File
@@ -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",