fix(plugin): disable duplicate OMO plugin startup
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test"
|
||||
import { detectExternalNotificationPlugin, getNotificationConflictWarning, detectExternalSkillPlugin, getSkillPluginConflictWarning } from "./external-plugin-detector"
|
||||
import {
|
||||
detectExternalNotificationPlugin,
|
||||
detectExternalSkillPlugin,
|
||||
getDuplicateOmoPluginWarning,
|
||||
getNotificationConflictWarning,
|
||||
getSkillPluginConflictWarning,
|
||||
} from "./external-plugin-detector"
|
||||
import * as fs from "node:fs"
|
||||
import * as path from "node:path"
|
||||
import * as os from "node:os"
|
||||
@@ -11,13 +17,21 @@ async function importFreshExternalPluginDetectorModule(): Promise<typeof import(
|
||||
describe("external-plugin-detector", () => {
|
||||
let tempDir: string
|
||||
let tempHomeDir: string
|
||||
let originalOpencodeConfigDir: string | undefined
|
||||
|
||||
beforeEach(() => {
|
||||
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "omo-test-"))
|
||||
tempHomeDir = fs.mkdtempSync(path.join(os.tmpdir(), "omo-home-"))
|
||||
originalOpencodeConfigDir = process.env.OPENCODE_CONFIG_DIR
|
||||
delete process.env.OPENCODE_CONFIG_DIR
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
if (originalOpencodeConfigDir === undefined) {
|
||||
delete process.env.OPENCODE_CONFIG_DIR
|
||||
} else {
|
||||
process.env.OPENCODE_CONFIG_DIR = originalOpencodeConfigDir
|
||||
}
|
||||
mock.restore()
|
||||
fs.rmSync(tempDir, { recursive: true, force: true })
|
||||
fs.rmSync(tempHomeDir, { recursive: true, force: true })
|
||||
@@ -476,6 +490,66 @@ describe("external-plugin-detector", () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe("detectDuplicateOmoPlugin", () => {
|
||||
test("#given a source plugin and active profile package alias #when detecting duplicates #then it reports the self-conflict", async () => {
|
||||
// given
|
||||
const projectConfigDir = path.join(tempDir, ".opencode")
|
||||
const profileConfigDir = path.join(tempHomeDir, ".config", "opencode", "profiles", "today")
|
||||
fs.mkdirSync(projectConfigDir, { recursive: true })
|
||||
fs.mkdirSync(profileConfigDir, { recursive: true })
|
||||
fs.writeFileSync(
|
||||
path.join(projectConfigDir, "opencode.json"),
|
||||
JSON.stringify({ plugin: ["file:///Users/yeongyu/local-workspaces/omo/src/index.ts"] }),
|
||||
)
|
||||
fs.writeFileSync(
|
||||
path.join(profileConfigDir, "opencode.json"),
|
||||
JSON.stringify({ plugin: ["oh-my-openagent@latest"] }),
|
||||
)
|
||||
process.env.OPENCODE_CONFIG_DIR = profileConfigDir
|
||||
|
||||
const nodeOs = await import("node:os")
|
||||
mock.module("node:os", () => ({
|
||||
...nodeOs,
|
||||
homedir: () => tempHomeDir,
|
||||
}))
|
||||
const { detectDuplicateOmoPlugin: detectDuplicateOmoPluginFresh } = await importFreshExternalPluginDetectorModule()
|
||||
|
||||
// when
|
||||
const result = detectDuplicateOmoPluginFresh(tempDir)
|
||||
|
||||
// then
|
||||
expect(result.detected).toBe(true)
|
||||
expect(result.pluginName).toBe("oh-my-openagent")
|
||||
expect(result.duplicatePlugins).toEqual([
|
||||
"file:///Users/yeongyu/local-workspaces/omo/src/index.ts",
|
||||
"oh-my-openagent@latest",
|
||||
])
|
||||
})
|
||||
|
||||
test("#given both package names from the rename window #when detecting duplicates #then it treats them as the same OMO plugin", async () => {
|
||||
// given
|
||||
const opencodeDir = path.join(tempDir, ".opencode")
|
||||
fs.mkdirSync(opencodeDir, { recursive: true })
|
||||
fs.writeFileSync(
|
||||
path.join(opencodeDir, "opencode.json"),
|
||||
JSON.stringify({ plugin: ["oh-my-opencode", "npm:oh-my-openagent@latest"] }),
|
||||
)
|
||||
const nodeOs = await import("node:os")
|
||||
mock.module("node:os", () => ({
|
||||
...nodeOs,
|
||||
homedir: () => tempHomeDir,
|
||||
}))
|
||||
const { detectDuplicateOmoPlugin: detectDuplicateOmoPluginFresh } = await importFreshExternalPluginDetectorModule()
|
||||
|
||||
// when
|
||||
const result = detectDuplicateOmoPluginFresh(tempDir)
|
||||
|
||||
// then
|
||||
expect(result.detected).toBe(true)
|
||||
expect(result.duplicatePlugins).toEqual(["oh-my-opencode", "npm:oh-my-openagent@latest"])
|
||||
})
|
||||
})
|
||||
|
||||
describe("getSkillPluginConflictWarning", () => {
|
||||
test("should generate warning message with plugin name", () => {
|
||||
// when
|
||||
@@ -488,4 +562,19 @@ describe("external-plugin-detector", () => {
|
||||
expect(warning).toContain("skills")
|
||||
})
|
||||
})
|
||||
|
||||
describe("getDuplicateOmoPluginWarning", () => {
|
||||
test("#given duplicate OMO entries #when generating a warning #then it tells the user startup is disabled", () => {
|
||||
// when
|
||||
const warning = getDuplicateOmoPluginWarning([
|
||||
"file:///Users/yeongyu/local-workspaces/omo/src/index.ts",
|
||||
"oh-my-openagent@latest",
|
||||
])
|
||||
|
||||
// then
|
||||
expect(warning).toContain("Duplicate OMO plugin entries detected")
|
||||
expect(warning).toContain("startup has been disabled")
|
||||
expect(warning).toContain("oh-my-openagent@latest")
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -28,6 +28,13 @@ const KNOWN_SKILL_PLUGINS = [
|
||||
"@opencode/skills",
|
||||
]
|
||||
|
||||
const OMO_PACKAGE_PLUGINS = [
|
||||
"oh-my-opencode",
|
||||
"oh-my-openagent",
|
||||
"@code-yeongyu/oh-my-opencode",
|
||||
"@code-yeongyu/oh-my-openagent",
|
||||
]
|
||||
|
||||
function matchesKnownPlugin(entry: string, knownPlugins: readonly string[]): string | null {
|
||||
const normalized = entry.toLowerCase()
|
||||
for (const known of knownPlugins) {
|
||||
@@ -43,6 +50,20 @@ function matchesKnownPlugin(entry: string, knownPlugins: readonly string[]): str
|
||||
return null
|
||||
}
|
||||
|
||||
function isOmoFilePlugin(entry: string): boolean {
|
||||
const normalized = entry.toLowerCase().replaceAll("\\", "/")
|
||||
if (!normalized.startsWith("file://")) return false
|
||||
|
||||
return /\/(omo(?:-[^/]*)?|oh-my-opencode|oh-my-openagent)\/(src|dist)\/index\.(ts|js)$/.test(normalized)
|
||||
}
|
||||
|
||||
function matchesOmoPlugin(entry: string): string | null {
|
||||
const packageMatch = matchesKnownPlugin(entry, OMO_PACKAGE_PLUGINS)
|
||||
if (packageMatch) return packageMatch
|
||||
if (isOmoFilePlugin(entry)) return "oh-my-openagent"
|
||||
return null
|
||||
}
|
||||
|
||||
export interface ExternalNotifierResult {
|
||||
detected: boolean
|
||||
pluginName: string | null
|
||||
@@ -55,6 +76,13 @@ export interface ExternalSkillPluginResult {
|
||||
allPlugins: string[]
|
||||
}
|
||||
|
||||
export interface DuplicateOmoPluginResult {
|
||||
detected: boolean
|
||||
pluginName: string | null
|
||||
duplicatePlugins: string[]
|
||||
allPlugins: string[]
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect if any external notification plugin is configured.
|
||||
* Returns information about detected plugins for logging/warning.
|
||||
@@ -107,6 +135,31 @@ export function detectExternalSkillPlugin(directory: string): ExternalSkillPlugi
|
||||
}
|
||||
}
|
||||
|
||||
export function detectDuplicateOmoPlugin(directory: string): DuplicateOmoPluginResult {
|
||||
const plugins = loadOpencodePlugins(directory)
|
||||
const duplicatePlugins = plugins.filter((plugin) => matchesOmoPlugin(plugin) !== null)
|
||||
|
||||
if (duplicatePlugins.length > 1) {
|
||||
log("[oh-my-openagent] Duplicate OMO plugin entries detected", {
|
||||
duplicatePlugins,
|
||||
allPlugins: plugins,
|
||||
})
|
||||
return {
|
||||
detected: true,
|
||||
pluginName: "oh-my-openagent",
|
||||
duplicatePlugins,
|
||||
allPlugins: plugins,
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
detected: false,
|
||||
pluginName: null,
|
||||
duplicatePlugins,
|
||||
allPlugins: plugins,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a warning message for users with conflicting notification plugins.
|
||||
*/
|
||||
@@ -137,3 +190,19 @@ Both ${PLUGIN_NAME} and ${pluginName} scan ~/.config/opencode/skills/ and regist
|
||||
2. Or disable ${PLUGIN_NAME}'s skill loading by setting "claude_code.skills": false in ${CONFIG_BASENAME}.json
|
||||
3. Or uninstall ${PLUGIN_NAME} if you prefer ${pluginName}'s skill management`
|
||||
}
|
||||
|
||||
export function getDuplicateOmoPluginWarning(duplicatePlugins: readonly string[]): string {
|
||||
const formattedPlugins = duplicatePlugins.map((plugin) => ` - ${plugin}`).join("\n")
|
||||
|
||||
return `[${PLUGIN_NAME}] Duplicate OMO plugin entries detected:
|
||||
${formattedPlugins}
|
||||
|
||||
Multiple ${PLUGIN_NAME} instances can inject internal prompts into the same live OpenCode session.
|
||||
That can create overlapping assistant turns and corrupt session state.
|
||||
|
||||
${PLUGIN_NAME} startup has been disabled for this plugin instance.
|
||||
|
||||
Keep exactly one OMO plugin entry in your OpenCode config. Check both:
|
||||
1. ~/.config/opencode/opencode.json
|
||||
2. Any active OPENCODE_CONFIG_DIR profile, such as ~/.config/opencode/profiles/<name>/opencode.json`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user