fix: respect default_agent in sort shim ordering (#3900)

The sort shim always placed core agents (sisyphus, hephaestus, prometheus,
atlas) before any custom agent, ignoring the user's configured default_agent
in opencode.json. This caused the TUI to always show Sisyphus as the selected
agent on startup regardless of default_agent.

Add setDefaultAgentForSort() which inserts the configured default_agent at
rank 0 in the sort shim's rank map. Called from applyAgentConfig after
resolving the effective default_agent value.

Fixes #3900
This commit is contained in:
Alan She
2026-05-14 20:45:52 +08:00
parent ca178f411c
commit ec7168eb6c
3 changed files with 59 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,11 @@ export async function applyAgentConfig(params: {
);
}
const resolvedDefault = (params.config as { default_agent?: string }).default_agent;
if (resolvedDefault) {
setDefaultAgentForSort(resolvedDefault);
}
const agentResult = params.config.agent as Record<string, unknown>;
for (const name of Object.keys(agentResult)) {
registerAgentName(name);
+42 -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,45 @@ 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])
})
})
})
})
+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