diff --git a/src/plugin-handlers/agent-config-handler.ts b/src/plugin-handlers/agent-config-handler.ts index 9d5c3b2ca..026afdad2 100644 --- a/src/plugin-handlers/agent-config-handler.ts +++ b/src/plugin-handlers/agent-config-handler.ts @@ -8,6 +8,7 @@ import { normalizeAgentForPromptKey, } from "../shared/agent-display-names"; import { AGENT_NAME_MAP } from "../shared/migration"; +import { setDefaultAgentForSort } from "../shared/agent-sort-shim"; import { registerAgentName } from "../features/claude-code-session-state"; import { discoverConfigSourceSkills, @@ -399,6 +400,12 @@ export async function applyAgentConfig(params: { ); } + if (configuredDefaultAgent) { + setDefaultAgentForSort( + (params.config as { default_agent?: string }).default_agent ?? configuredDefaultAgent, + ); + } + const agentResult = params.config.agent as Record; for (const name of Object.keys(agentResult)) { registerAgentName(name); diff --git a/src/shared/agent-sort-shim.test.ts b/src/shared/agent-sort-shim.test.ts index 47145924a..4b26609c4 100644 --- a/src/shared/agent-sort-shim.test.ts +++ b/src/shared/agent-sort-shim.test.ts @@ -2,7 +2,7 @@ import { afterEach, beforeAll, describe, expect, test } from "bun:test" -import { installAgentSortShim, setAgentSortOrder } from "./agent-sort-shim" +import { installAgentSortShim, setAgentSortOrder, setDefaultAgentForSort } from "./agent-sort-shim" import { AGENT_DISPLAY_NAMES } from "./agent-display-names" type AgentListItem = { @@ -224,4 +224,66 @@ describe("agent-sort-shim", () => { }) }) }) + + describe("#given a custom default_agent configured via setDefaultAgentForSort", () => { + describe("#when toSorted is called on core agents mixed with the custom default agent", () => { + test("#then the custom default agent sorts first, followed by core agents in canonical order", () => { + // given + setAgentSortOrder(undefined) + setDefaultAgentForSort("crystal") + const sisyphus = { name: "Sisyphus - Ultraworker" } + const hephaestus = { name: "Hephaestus - Deep Agent" } + const prometheus = { name: "Prometheus - Plan Builder" } + const atlas = { name: "Atlas - Plan Executor" } + const crystal = { name: "crystal" } + const input = [atlas, crystal, prometheus, hephaestus, sisyphus] + + // when + const result = input.toSorted((a, b) => a.name.localeCompare(b.name)) + + // then + expect(result).toEqual([crystal, sisyphus, hephaestus, prometheus, atlas]) + }) + }) + + describe("#when setDefaultAgentForSort is called with a core agent name", () => { + test("#then that core agent sorts first, others follow in remaining canonical order", () => { + // given + setAgentSortOrder(undefined) + setDefaultAgentForSort("Hephaestus - Deep Agent") + const sisyphus = { name: "Sisyphus - Ultraworker" } + const hephaestus = { name: "Hephaestus - Deep Agent" } + const prometheus = { name: "Prometheus - Plan Builder" } + const atlas = { name: "Atlas - Plan Executor" } + const input = [atlas, prometheus, hephaestus, sisyphus] + + // when + const result = input.toSorted((a, b) => a.name.localeCompare(b.name)) + + // then + expect(result).toEqual([hephaestus, sisyphus, prometheus, atlas]) + }) + }) + }) + + describe("#given agent_order configured without default_agent", () => { + describe("#when setAgentSortOrder sets a non-canonical order and setDefaultAgentForSort is NOT called", () => { + test("#then the custom agent_order is preserved without implicit override", () => { + // given + setAgentSortOrder(["hephaestus", "sisyphus", "prometheus", "atlas"]) + // setDefaultAgentForSort is intentionally NOT called (user did not set default_agent) + const sisyphus = { name: "Sisyphus - Ultraworker" } + const hephaestus = { name: "Hephaestus - Deep Agent" } + const prometheus = { name: "Prometheus - Plan Builder" } + const atlas = { name: "Atlas - Plan Executor" } + const input = [atlas, sisyphus, prometheus, hephaestus] + + // when + const result = input.toSorted((a, b) => a.name.localeCompare(b.name)) + + // then — Hephaestus must remain first per the user's agent_order + expect(result).toEqual([hephaestus, sisyphus, prometheus, atlas]) + }) + }) + }) }) diff --git a/src/shared/agent-sort-shim.ts b/src/shared/agent-sort-shim.ts index 479a20719..d23b1315e 100644 --- a/src/shared/agent-sort-shim.ts +++ b/src/shared/agent-sort-shim.ts @@ -82,6 +82,17 @@ export function setAgentSortOrder(agentOrder: readonly string[] | undefined): vo agentRank = createAgentRank(agentOrder) } +export function setDefaultAgentForSort(agentName: string | undefined): void { + if (!agentName) return + if (agentRank.get(agentName) === 0) return + const updated = new Map() + updated.set(agentName, 0) + for (const [key, rank] of agentRank) { + if (key !== agentName) updated.set(key, rank + 1) + } + agentRank = updated +} + export function installAgentSortShim(): void { if (installed) return