Skip to content

The agent that grows with you

fix(desktop): route every desktop-plugin tree copy through staged publication

teknium18170c16cca56
diff --git a/apps/desktop/electron/desktop-plugin-install.ts b/apps/desktop/electron/desktop-plugin-install.ts
index ed6096d7205..d34cea1c37b 100644
--- a/apps/desktop/electron/desktop-plugin-install.ts
+++ b/apps/desktop/electron/desktop-plugin-install.ts
@@ -10,6 +10,8 @@ import fsp from 'node:fs/promises'
import os from 'node:os'
import path from 'node:path'
+import { publishDesktopTree } from './desktop-plugins-root'
+
const GITHUB_BROWSER_SEGMENTS = new Set(['tree', 'blob', 'commit'])
export interface ResolvedGitUrl {
@@ -380,11 +382,6 @@ export async function probePluginRepo(gitBin: string, identifier: string): Promi
}
}
-async function copyDesktopTree(sourceDir: string, targetDir: string): Promise<void> {
- await fsp.mkdir(path.dirname(targetDir), { recursive: true })
- await fsp.cp(sourceDir, targetDir, { recursive: true, force: true })
-}
-
export async function installDesktopPluginFromGit(
gitBin: string,
identifier: string,
@@ -421,7 +418,9 @@ export async function installDesktopPluginFromGit(
await fsp.rm(targetDir, { recursive: true, force: true })
}
- await copyDesktopTree(sourceDir, targetDir)
+ // Staged copy + rename: a failed copy must not leave an empty `targetDir`
+ // that turns every retry into "already exists. Enable force reinstall".
+ await publishDesktopTree(sourceDir, targetDir)
if (!(await pathIsFile(targetPlugin))) {
return { ok: false, error: `Install completed but ${targetPlugin} is missing.` }
diff --git a/apps/desktop/electron/desktop-plugins-root.ts b/apps/desktop/electron/desktop-plugins-root.ts
index 1db5eec0e13..5e84d059d41 100644
--- a/apps/desktop/electron/desktop-plugins-root.ts
+++ b/apps/desktop/electron/desktop-plugins-root.ts
@@ -194,33 +194,48 @@ export async function materializeDesktopHalf(
}
}
- await fs.promises.mkdir(appRoot, { recursive: true })
- const stagingRoot = await fs.promises.mkdtemp(path.join(appRoot, `.${packageName}.staging-`))
- const stagedTarget = path.join(stagingRoot, packageName)
+ const marker: DesktopHalfMarker = {
+ package: packageName,
+ source: sourceDir,
+ sourceMtimeMs: stat.mtimeMs,
+ ...(await packageOrigin(packageDir))
+ }
- try {
- await fs.promises.cp(sourceDir, stagedTarget, { force: true, recursive: true })
+ await publishDesktopTree(sourceDir, target, staged =>
+ fs.promises.writeFile(path.join(staged, PACKAGE_MARKER), JSON.stringify(marker, null, 2) + '\n')
+ )
- const marker: DesktopHalfMarker = {
- package: packageName,
- source: sourceDir,
- sourceMtimeMs: stat.mtimeMs,
- ...(await packageOrigin(packageDir))
- }
+ return target
+}
- await fs.promises.writeFile(path.join(stagedTarget, PACKAGE_MARKER), JSON.stringify(marker, null, 2) + '\n')
+/** Copy `sourceDir` to `target` through a staging sibling (`<parent>/.<name>.staging-*`)
+ * and rename the finished tree into place. `finalize` runs on the staged tree
+ * before publication, so a marker is never missing from a published folder.
+ * Directory replacement is not atomic on every platform Electron supports, but
+ * the complete copy exists before the old one is removed, so a failure leaves
+ * either the old folder or none — never a partial, marker-less one that a
+ * later pass would mistake for a manual install (#112450). */
+export async function publishDesktopTree(
+ sourceDir: string,
+ target: string,
+ finalize?: (staged: string) => Promise<void>
+): Promise<void> {
+ const parent = path.dirname(target)
+ const name = path.basename(target)
+
+ await fs.promises.mkdir(parent, { recursive: true })
+ const stagingRoot = await fs.promises.mkdtemp(path.join(parent, `.${name}.staging-`))
+ const staged = path.join(stagingRoot, name)
- // Directory replacement is not atomic across the platforms Electron
- // supports, but the complete, marked copy is prepared before removing an
- // old copy. This prevents a failed copy from becoming a permanent
- // marker-less target.
+ try {
+ await fs.promises.cp(sourceDir, staged, { force: true, recursive: true })
+ await finalize?.(staged)
+ // rename() refuses to replace a non-empty directory, so the old copy goes first.
await fs.promises.rm(target, { force: true, recursive: true })
- await fs.promises.rename(stagedTarget, target)
+ await fs.promises.rename(staged, target)
} finally {
await fs.promises.rm(stagingRoot, { force: true, recursive: true })
}
-
- return target
}
function isMissing(error: unknown): boolean {
diff --git a/website/docs/developer-guide/desktop-plugin-sdk.md b/website/docs/developer-guide/desktop-plugin-sdk.md
index 3cf661d3298..481c40ba590 100644
--- a/website/docs/developer-guide/desktop-plugin-sdk.md
+++ b/website/docs/developer-guide/desktop-plugin-sdk.md
@@ -724,7 +724,13 @@ carry the package, and it never appears or disappears when the user switches
the Capabilities profile selector. The renderer never scans `plugins/` itself.
The marker records the package name and its origin (catalog sidecar or git
remote), which is what the **Install here** button on the Plugins page uses to
-install the agent half into another profile.
+install the agent half into another profile. The copy is staged beside the
+target and renamed into place, so an interrupted copy (a transient file lock, a
+crash mid-copy) never leaves a half-written folder behind; a leftover
+`desktop-plugins/<id>/` that has no marker and no `plugin.js` is treated as
+such damage and replaced on the next **Rescan**, while a marker-less folder
+that *does* hold a `plugin.js` is a standalone plugin you installed by hand and
+is never overwritten.
Two enable switches still apply, on purpose, and both default to **off**: the
desktop half ships opt-in — it inventories in **Capabilities → Plugins** but stays