fix: revert delegate-task to string category schema, fix mock isolation and restore UB7 originals

This commit is contained in:
YeonGyu-Kim
2026-03-31 17:25:00 -07:00
parent 92d70cff5b
commit ce0d3581f0
9 changed files with 74 additions and 187 deletions
@@ -8,9 +8,11 @@ import type { PluginInput } from "@opencode-ai/plugin"
import { createOpencodeClient, type Project } from "@opencode-ai/sdk"
const isCallerOrchestratorMock = mock(async () => true)
const collectGitDiffStatsMock = mock(() => {
throw new Error("background launches should not trigger verification")
})
const collectGitDiffStatsMock = mock(() => ({
filesChanged: 0,
insertions: 0,
deletions: 0,
}))
mock.module("../../shared/session-utils", () => ({
isCallerOrchestrator: isCallerOrchestratorMock,
@@ -1,5 +1,3 @@
/// <reference path="../../../bun-test.d.ts" />
import { afterEach, beforeEach, describe, expect, it } from "bun:test"
import { mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"
import { tmpdir } from "node:os"
@@ -120,37 +118,6 @@ describe("autoMigrateLegacyPluginEntry", () => {
})
})
describe("#given opencode.jsonc contains a nested plugin key before the top-level plugin array", () => {
it("#then rewrites only the top-level plugin array", async () => {
// given
writeFileSync(
join(testConfigDir, "opencode.jsonc"),
`{
"nested": {
"plugin": ["oh-my-opencode"]
},
"plugin": ["oh-my-opencode@latest"]
}
`,
)
const { autoMigrateLegacyPluginEntry } = await importFreshAutoMigrateModule()
// when
const result = autoMigrateLegacyPluginEntry(testConfigDir)
// then
expect(result.migrated).toBe(true)
const content = readFileSync(join(testConfigDir, "opencode.jsonc"), "utf-8")
expect(content).toContain(`"nested": {
"plugin": ["oh-my-opencode"]
}`)
expect(content).toContain(`"plugin": [
"oh-my-openagent@latest"
]`)
})
})
describe("#given only canonical entry exists", () => {
it("#then returns migrated false and leaves file untouched", async () => {
// given
+23 -4
View File
@@ -1,8 +1,7 @@
import { existsSync, readFileSync } from "node:fs"
import { existsSync, readFileSync, writeFileSync } from "node:fs"
import { join } from "node:path"
import { parseJsoncSafe } from "../../shared/jsonc-parser"
import { migrateLegacyPluginEntry } from "../../shared/migrate-legacy-plugin-entry"
import { getOpenCodeConfigPaths } from "../../shared/opencode-config-dir"
import { LEGACY_PLUGIN_NAME, PLUGIN_NAME } from "../../shared/plugin-identity"
@@ -21,6 +20,10 @@ function isLegacyEntry(entry: string): boolean {
return entry === LEGACY_PLUGIN_NAME || entry.startsWith(`${LEGACY_PLUGIN_NAME}@`)
}
function isCanonicalEntry(entry: string): boolean {
return entry === PLUGIN_NAME || entry.startsWith(`${PLUGIN_NAME}@`)
}
function toLegacyCanonical(entry: string): string {
if (entry === LEGACY_PLUGIN_NAME) return PLUGIN_NAME
if (entry.startsWith(`${LEGACY_PLUGIN_NAME}@`)) {
@@ -57,13 +60,29 @@ export function autoMigrateLegacyPluginEntry(overrideConfigDir?: string): Migrat
const legacyEntries = plugins.filter(isLegacyEntry)
if (legacyEntries.length === 0) return { migrated: false, from: null, to: null, configPath }
const hasCanonical = plugins.some(isCanonicalEntry)
const from = legacyEntries[0]
const to = toLegacyCanonical(from)
if (!migrateLegacyPluginEntry(configPath)) {
return { migrated: false, from: null, to: null, configPath }
const normalized = hasCanonical
? plugins.filter((p) => !isLegacyEntry(p))
: plugins.map((p) => (isLegacyEntry(p) ? toLegacyCanonical(p) : p))
const isJsonc = configPath.endsWith(".jsonc")
if (isJsonc) {
const pluginArrayRegex = /((?:"plugin"|plugin)\s*:\s*)\[([\s\S]*?)\]/
const match = content.match(pluginArrayRegex)
if (match) {
const formattedPlugins = normalized.map((p) => `"${p}"`).join(",\n ")
const newContent = content.replace(pluginArrayRegex, `$1[\n ${formattedPlugins}\n ]`)
writeFileSync(configPath, newContent)
return { migrated: true, from, to, configPath }
}
}
const parsed = JSON.parse(content) as Record<string, unknown>
parsed.plugin = normalized
writeFileSync(configPath, JSON.stringify(parsed, null, 2) + "\n")
return { migrated: true, from, to, configPath }
} catch {
return { migrated: false, from: null, to: null, configPath }
+1 -33
View File
@@ -1,15 +1,10 @@
/// <reference path="../../../bun-test.d.ts" />
import { afterAll, beforeEach, describe, expect, it, mock } from "bun:test"
import type { LegacyPluginCheckResult } from "../../shared/legacy-plugin-warning"
import type { MigrationResult } from "./auto-migrate"
const mockCheckForLegacyPluginEntry = mock((): LegacyPluginCheckResult => ({
const mockCheckForLegacyPluginEntry = mock(() => ({
hasLegacyEntry: false,
hasCanonicalEntry: false,
legacyEntries: [] as string[],
configPath: null,
}))
const mockAutoMigrate = mock((): MigrationResult => ({
@@ -72,7 +67,6 @@ describe("createLegacyPluginToastHook", () => {
hasLegacyEntry: false,
hasCanonicalEntry: true,
legacyEntries: [],
configPath: null,
})
mockAutoMigrate.mockReturnValue({ migrated: false, from: null, to: null, configPath: null })
mockShowToast.mockResolvedValue(undefined)
@@ -99,7 +93,6 @@ describe("createLegacyPluginToastHook", () => {
hasLegacyEntry: true,
hasCanonicalEntry: false,
legacyEntries: ["oh-my-opencode"],
configPath: "/tmp/opencode.json",
})
mockAutoMigrate.mockReturnValue({
migrated: true,
@@ -127,7 +120,6 @@ describe("createLegacyPluginToastHook", () => {
hasLegacyEntry: true,
hasCanonicalEntry: false,
legacyEntries: ["oh-my-opencode"],
configPath: "/tmp/opencode.json",
})
mockAutoMigrate.mockReturnValue({
migrated: false,
@@ -155,7 +147,6 @@ describe("createLegacyPluginToastHook", () => {
hasLegacyEntry: true,
hasCanonicalEntry: false,
legacyEntries: ["oh-my-opencode"],
configPath: "/tmp/opencode.json",
})
mockAutoMigrate.mockReturnValue({
migrated: true,
@@ -182,7 +173,6 @@ describe("createLegacyPluginToastHook", () => {
hasLegacyEntry: true,
hasCanonicalEntry: false,
legacyEntries: ["oh-my-opencode"],
configPath: "/tmp/opencode.json",
})
const { createLegacyPluginToastHook } = await importFreshModule()
const hook = createLegacyPluginToastHook(createMockCtx())
@@ -202,7 +192,6 @@ describe("createLegacyPluginToastHook", () => {
hasLegacyEntry: true,
hasCanonicalEntry: false,
legacyEntries: ["oh-my-opencode"],
configPath: "/tmp/opencode.json",
})
const { createLegacyPluginToastHook } = await importFreshModule()
const hook = createLegacyPluginToastHook(createMockCtx())
@@ -214,25 +203,4 @@ describe("createLegacyPluginToastHook", () => {
expect(mockCheckForLegacyPluginEntry).not.toHaveBeenCalled()
})
})
describe("#given a project directory is available", () => {
it("#then passes the project directory into legacy config detection", async () => {
// given
mockCheckForLegacyPluginEntry.mockReturnValue({
hasLegacyEntry: true,
hasCanonicalEntry: false,
legacyEntries: ["oh-my-opencode"],
configPath: "/tmp/test/.opencode/opencode.json",
})
const { createLegacyPluginToastHook } = await importFreshModule()
const hook = createLegacyPluginToastHook(createMockCtx())
// when
await hook.event(createEvent("session.created"))
// then
expect(mockCheckForLegacyPluginEntry).toHaveBeenCalledWith(undefined, "/tmp/test")
expect(mockAutoMigrate).toHaveBeenCalledWith("/tmp/test/.opencode")
})
})
})
+2 -4
View File
@@ -1,5 +1,3 @@
import { dirname } from "node:path"
import type { PluginInput } from "@opencode-ai/plugin"
import { checkForLegacyPluginEntry } from "../../shared/legacy-plugin-warning"
@@ -19,10 +17,10 @@ export function createLegacyPluginToastHook(ctx: PluginInput) {
fired = true
const result = checkForLegacyPluginEntry(undefined, ctx.directory)
const result = checkForLegacyPluginEntry()
if (!result.hasLegacyEntry) return
const migration = autoMigrateLegacyPluginEntry(result.configPath ? dirname(result.configPath) : undefined)
const migration = autoMigrateLegacyPluginEntry()
if (migration.migrated) {
log("[legacy-plugin-toast] Auto-migrated opencode.json plugin entry", {
-24
View File
@@ -1,5 +1,3 @@
/// <reference path="../../bun-test.d.ts" />
import { afterEach, beforeEach, describe, expect, it } from "bun:test"
import { mkdirSync, rmSync, writeFileSync } from "node:fs"
import { tmpdir } from "node:os"
@@ -82,26 +80,4 @@ describe("checkForLegacyPluginEntry", () => {
expect(result.legacyEntries).toEqual([])
expect(result.configPath).toBeNull()
})
describe("#given a project-local .opencode config contains a legacy plugin entry", () => {
it("#then detects the project-local config path", () => {
// given
const projectDir = join(testConfigDir, "project")
const projectConfigDir = join(projectDir, ".opencode")
mkdirSync(projectConfigDir, { recursive: true })
writeFileSync(
join(projectConfigDir, "opencode.json"),
JSON.stringify({ plugin: ["oh-my-opencode"] }, null, 2),
)
// when
const result = checkForLegacyPluginEntry(undefined, projectDir)
// then
expect(result.hasLegacyEntry).toBe(true)
expect(result.hasCanonicalEntry).toBe(false)
expect(result.legacyEntries).toEqual(["oh-my-opencode"])
expect(result.configPath).toBe(join(projectConfigDir, "opencode.json"))
})
})
})
+28 -66
View File
@@ -16,36 +16,20 @@ export interface LegacyPluginCheckResult {
configPath: string | null
}
function getConfigPathFromDirectory(configDir: string): string | null {
const jsonPath = join(configDir, "opencode.json")
const jsoncPath = join(configDir, "opencode.jsonc")
if (existsSync(jsoncPath)) return jsoncPath
if (existsSync(jsonPath)) return jsonPath
return null
}
function getOpenCodeConfigPathsToCheck(overrideConfigDir?: string, projectDir?: string): string[] {
function getOpenCodeConfigPath(overrideConfigDir?: string): string | null {
if (overrideConfigDir) {
const overridePath = getConfigPathFromDirectory(overrideConfigDir)
return overridePath ? [overridePath] : []
}
const configPaths: string[] = []
if (projectDir) {
const projectConfigPath = getConfigPathFromDirectory(join(projectDir, ".opencode"))
if (projectConfigPath) {
configPaths.push(projectConfigPath)
}
const jsonPath = join(overrideConfigDir, "opencode.json")
const jsoncPath = join(overrideConfigDir, "opencode.jsonc")
if (existsSync(jsoncPath)) return jsoncPath
if (existsSync(jsonPath)) return jsonPath
return null
}
const { configJsonc, configJson } = getOpenCodeConfigPaths({ binary: "opencode", version: null })
if (existsSync(configJsonc)) configPaths.push(configJsonc)
else if (existsSync(configJson)) configPaths.push(configJson)
return configPaths
if (existsSync(configJsonc)) return configJsonc
if (existsSync(configJson)) return configJson
return null
}
function isLegacyPluginEntry(entry: string): boolean {
@@ -56,51 +40,29 @@ function isCanonicalPluginEntry(entry: string): boolean {
return entry === PLUGIN_NAME || entry.startsWith(`${PLUGIN_NAME}@`)
}
export function checkForLegacyPluginEntry(
overrideConfigDir?: string,
projectDir?: string,
): LegacyPluginCheckResult {
const configPaths = getOpenCodeConfigPathsToCheck(overrideConfigDir, projectDir)
if (configPaths.length === 0) {
export function checkForLegacyPluginEntry(overrideConfigDir?: string): LegacyPluginCheckResult {
const configPath = getOpenCodeConfigPath(overrideConfigDir)
if (!configPath) {
return { hasLegacyEntry: false, hasCanonicalEntry: false, legacyEntries: [], configPath: null }
}
let hasCanonicalEntry = false
let detectedConfigPath: string | null = null
for (const configPath of configPaths) {
detectedConfigPath ??= configPath
try {
const content = readFileSync(configPath, "utf-8")
const parseResult = parseJsoncSafe<OpenCodeConfig>(content)
if (!parseResult.data) {
continue
}
const pluginEntries = parseResult.data.plugin ?? []
const legacyEntries = pluginEntries.filter(isLegacyPluginEntry)
const fileHasCanonicalEntry = pluginEntries.some(isCanonicalPluginEntry)
if (legacyEntries.length > 0) {
return {
hasLegacyEntry: true,
hasCanonicalEntry: fileHasCanonicalEntry,
legacyEntries,
configPath,
}
}
hasCanonicalEntry ||= fileHasCanonicalEntry
} catch {
continue
try {
const content = readFileSync(configPath, "utf-8")
const parseResult = parseJsoncSafe<OpenCodeConfig>(content)
if (!parseResult.data) {
return { hasLegacyEntry: false, hasCanonicalEntry: false, legacyEntries: [], configPath }
}
}
return {
hasLegacyEntry: false,
hasCanonicalEntry,
legacyEntries: [],
configPath: detectedConfigPath,
const legacyEntries = (parseResult.data.plugin ?? []).filter(isLegacyPluginEntry)
const hasCanonicalEntry = (parseResult.data.plugin ?? []).some(isCanonicalPluginEntry)
return {
hasLegacyEntry: legacyEntries.length > 0,
hasCanonicalEntry,
legacyEntries,
configPath,
}
} catch {
return { hasLegacyEntry: false, hasCanonicalEntry: false, legacyEntries: [], configPath: null }
}
}
+2 -6
View File
@@ -3,7 +3,7 @@ const { describe, expect, test } = require("bun:test")
import { createDelegateTask } from "./tools"
describe("createDelegateTask schema", () => {
test("#given category arg #when tool is created #then category is constrained to available enum values", () => {
test("#given category arg #when tool is created #then category accepts any string", () => {
//#given
const toolDefinition = createDelegateTask({ manager: {} as never, client: {} as never, directory: "/tmp/test" })
@@ -13,16 +13,12 @@ import { createDelegateTask } from "./tools"
type: string
innerType: {
def: { type: string }
options: string[]
}
}
}
//#then
expect(categorySchema.def.type).toBe("optional")
expect(categorySchema.def.innerType.def.type).toBe("enum")
expect(categorySchema.def.innerType.options).toContain("quick")
expect(categorySchema.def.innerType.options).toContain("deep")
expect(categorySchema.def.innerType.options).toContain("ultrabrain")
expect(categorySchema.def.innerType.def.type).toBe("string")
})
})
+13 -14
View File
@@ -76,13 +76,13 @@ export function createDelegateTask(options: DelegateTaskToolOptions): ToolDefini
- category: For task delegation (uses Sisyphus-Junior with category-optimized model)
- subagent_type: For direct agent invocation (explore, librarian, oracle, etc.)
**DO NOT provide both.** category and subagent_type are mutually exclusive.
**DO NOT provide both.** If category is provided, subagent_type is ignored.
- load_skills: ALWAYS REQUIRED. Pass [] if no skills needed, or ["skill-1", "skill-2"] for category tasks.
- category: Use predefined category → Spawns Sisyphus-Junior with category config
Available categories:
${categoryList}
- subagent_type: Use a specific callable non-primary agent directly (for example: explore, librarian, oracle, metis, momus)
- subagent_type: Use specific agent directly (explore, librarian, oracle, metis, momus)
- run_in_background: REQUIRED. true=async (returns task_id), false=sync (waits). Use background=true ONLY for parallel exploration with 5+ independent queries.
- session_id: Existing Task session to continue (from previous task output). Continues agent with FULL CONTEXT PRESERVED - saves tokens, maintains continuity.
- command: The command that triggered this task (optional, for slash command tracking).
@@ -101,19 +101,21 @@ export function createDelegateTask(options: DelegateTaskToolOptions): ToolDefini
description: tool.schema.string().describe("Short task description (3-5 words)"),
prompt: tool.schema.string().describe("Full detailed prompt for the agent"),
run_in_background: tool.schema.boolean().describe("REQUIRED. true=async (returns task_id), false=sync (waits). Use false for task delegation, true ONLY for parallel exploration."),
category: tool.schema.enum(categoryNames).optional().describe(`REQUIRED if subagent_type not provided. Do NOT provide both category and subagent_type.`),
subagent_type: tool.schema.string().optional().describe("REQUIRED if category not provided. Do NOT provide both category and subagent_type. Must be a callable non-primary agent name returned by app.agents()."),
category: tool.schema.string().optional().describe(`REQUIRED if subagent_type not provided. Do NOT provide both category and subagent_type.`),
subagent_type: tool.schema.string().optional().describe("REQUIRED if category not provided. Do NOT provide both category and subagent_type."),
session_id: tool.schema.string().optional().describe("Existing Task session to continue"),
command: tool.schema.string().optional().describe("The command that triggered this task"),
},
async execute(args: DelegateTaskArgs, toolContext) {
const ctx = toolContext as ToolContextWithMetadata
let categoryOverrideNote: string | undefined
if (args.category && args.subagent_type) {
categoryOverrideNote = `[Note: You provided both category="${args.category}" and subagent_type="${args.subagent_type}". category takes precedence \u2014 subagent_type was ignored. Next time, provide ONLY category.]`
}
if (args.category) {
if (args.subagent_type && args.subagent_type !== SISYPHUS_JUNIOR_AGENT) {
log("[task] category provided - overriding subagent_type to sisyphus-junior", {
category: args.category,
subagent_type: args.subagent_type,
})
}
args.subagent_type = SISYPHUS_JUNIOR_AGENT
}
await ctx.metadata?.({
@@ -221,8 +223,7 @@ export function createDelegateTask(options: DelegateTaskToolOptions): ToolDefini
availableCategories,
availableSkills,
})
const result = await executeUnstableAgentTask(args, ctx, options, parentContext, agentToUse, categoryModel, systemContent, actualModel)
return categoryOverrideNote ? `${categoryOverrideNote}\n\n${result}` : result
return executeUnstableAgentTask(args, ctx, options, parentContext, agentToUse, categoryModel, systemContent, actualModel)
}
} else {
const resolution = await resolveSubagentExecution(args, options, parentContext.agent, categoryExamples)
@@ -245,13 +246,11 @@ export function createDelegateTask(options: DelegateTaskToolOptions): ToolDefini
availableSkills,
})
const prependNote = (result: string) => categoryOverrideNote ? `${categoryOverrideNote}\n\n${result}` : result
if (runInBackground) {
return prependNote(await executeBackgroundTask(args, ctx, options, parentContext, agentToUse, categoryModel, systemContent, fallbackChain))
return executeBackgroundTask(args, ctx, options, parentContext, agentToUse, categoryModel, systemContent, fallbackChain)
}
return prependNote(await executeSyncTask(args, ctx, options, parentContext, agentToUse, categoryModel, systemContent, modelInfo, fallbackChain))
return executeSyncTask(args, ctx, options, parentContext, agentToUse, categoryModel, systemContent, modelInfo, fallbackChain)
},
})
}