Files
hermes-agent/apps/desktop/electron/desktop-plugin-remove.ts
Teknium f1b5275ca8 fix(desktop): serialize writers of the desktop-plugins root
Every window's 5 s disk poll and the Plugins page resolve the root through
IPC, and each resolution runs a full reconcile of unified packages' desktop
halves. Nothing serialized those passes in the main process, so overlapping
passes each decided to copy the same half and each published it with
rm + rename. When the renames interleaved, the loser failed with ENOTEMPTY,
logged "skipping unreadable package", and skipped the package for the rest of
its pass, so a same-named package in a later profile overwrote the default
home's copy (the next pass copied it back).

Writers of the root (reconcile, the profile-root migration, the git
installer's check + publish, standalone uninstall) now queue on one
per-root lock, so a later pass sees the earlier pass's copy as current.
2026-10-24 03:59:08 -07:00

67 lines
2.5 KiB
TypeScript

// Uninstall of a STANDALONE desktop plugin: delete one folder directly under
// the app-level `<HERMES_HOME>/desktop-plugins` root. The renderer names the
// folder, never a path — containment is by construction (single segment) and
// re-checked after resolution so nothing outside the root can be addressed.
//
// A unified package's desktop half (folder carrying `.hermes-package.json`) is
// refused here: the reconcile re-copies it from `plugins/<name>/desktop` on
// the next pass while the agent package exists, so deleting the copy would
// only make it flicker. That half goes away with `plugins.manage remove`.
import fs from 'node:fs'
import path from 'node:path'
import { PACKAGE_MARKER, withDesktopPluginsRoot } from './desktop-plugins-root'
export interface RemoveDesktopPluginResult {
ok: boolean
error?: string
/** The folder that was deleted (on success). */
path?: string
}
export function removeDesktopPlugin(appRoot: string, rawName: unknown): Promise<RemoveDesktopPluginResult> {
// Under the root's writer lock: a reconcile pass adopting this folder (it
// stamps the marker) must not land between the marker check and the delete.
return withDesktopPluginsRoot(appRoot, () => removeFolder(appRoot, rawName))
}
async function removeFolder(appRoot: string, rawName: unknown): Promise<RemoveDesktopPluginResult> {
const name = String(rawName ?? '').trim()
if (!name || name === '.' || name === '..' || /[\\/]/.test(name)) {
return { ok: false, error: 'invalid plugin folder name' }
}
const root = path.resolve(appRoot)
const target = path.resolve(root, name)
if (path.relative(root, target) !== name) {
return { ok: false, error: `${name} is not inside the desktop-plugins folder` }
}
let stat: fs.Stats
try {
// lstat: a symlinked folder is removed as the LINK, never by following it.
stat = await fs.promises.lstat(target)
} catch {
return { ok: false, error: `${name} is not installed` }
}
if (!stat.isDirectory() && !stat.isSymbolicLink()) {
return { ok: false, error: `${name} is not a plugin folder` }
}
if (stat.isDirectory() && fs.existsSync(path.join(target, PACKAGE_MARKER))) {
return { ok: false, error: `${name} is the desktop half of an agent plugin — uninstall that plugin instead` }
}
try {
await fs.promises.rm(target, { force: true, recursive: true })
} catch (error) {
return { ok: false, error: error instanceof Error ? error.message : String(error) }
}
return { ok: true, path: target }
}