Merge pull request #4020 from scw1109/fix/default-agent-sort-order

fix: respect default_agent in sort shim ordering
This commit is contained in:
YeonGyu-Kim
2026-05-18 13:48:58 +09:00
committed by GitHub
3 changed files with 81 additions and 1 deletions
@@ -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<string, unknown>;
for (const name of Object.keys(agentResult)) {
registerAgentName(name);
+63 -1
View File
@@ -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])
})
})
})
})
+11
View File
@@ -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<string, number>()
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