Skip to content

The agent that grows with you

fix(desktop): recover interrupted unified plugin copies

KoNit-K27fc1d9fb204
diff --git a/apps/desktop/electron/desktop-plugins-root.test.ts b/apps/desktop/electron/desktop-plugins-root.test.ts
index 7874644ac65..1461571e719 100644
--- a/apps/desktop/electron/desktop-plugins-root.test.ts
+++ b/apps/desktop/electron/desktop-plugins-root.test.ts
@@ -5,6 +5,7 @@ import path from 'node:path'
import { afterEach, describe, expect, it, vi } from 'vitest'
import {
+ materializeDesktopHalf,
migrateProfileScopedDesktopPlugins,
PACKAGE_MARKER,
reconcileUnifiedDesktopHalves
@@ -180,4 +181,33 @@ describe('reconcileUnifiedDesktopHalves', () => {
expect(await reconcileUnifiedDesktopHalves(home, appRoot)).toEqual([])
expect(fs.readFileSync(path.join(appRoot, 'media', 'plugin.js'), 'utf8')).toBe('user standalone')
})
+
+ it('replaces an interrupted marker-less copy, stamps it, and converges on retry', async () => {
+ const home = makeHome()
+ const appRoot = path.join(home, 'desktop-plugins')
+ write(path.join(home, 'plugins', 'media', 'desktop', 'plugin.js'), 'package half')
+ // A failed pre-marker copy has files but no entry point and is not a
+ // standalone plugin the user can run.
+ write(path.join(appRoot, 'media', 'partial.js'), 'interrupted copy')
+
+ expect(await reconcileUnifiedDesktopHalves(home, appRoot)).toEqual([path.join(appRoot, 'media')])
+ expect(fs.readFileSync(path.join(appRoot, 'media', 'plugin.js'), 'utf8')).toBe('package half')
+ expect(fs.existsSync(path.join(appRoot, 'media', 'partial.js'))).toBe(false)
+ expect(fs.existsSync(path.join(appRoot, 'media', PACKAGE_MARKER))).toBe(true)
+ expect(await reconcileUnifiedDesktopHalves(home, appRoot)).toEqual([])
+ })
+
+ it('cleans up failed staging copies without exposing a marker-less target', async () => {
+ const home = makeHome()
+ const appRoot = path.join(home, 'desktop-plugins')
+ const packageDir = path.join(home, 'plugins', 'media')
+ write(path.join(packageDir, 'desktop', 'plugin.js'), 'package half')
+ const copy = vi.spyOn(fs.promises, 'cp').mockRejectedValueOnce(new Error('disk full'))
+
+ await expect(materializeDesktopHalf(packageDir, appRoot)).rejects.toThrow('disk full')
+
+ copy.mockRestore()
+ expect(fs.existsSync(path.join(appRoot, 'media'))).toBe(false)
+ expect(fs.readdirSync(appRoot)).toEqual([])
+ })
})
diff --git a/apps/desktop/electron/desktop-plugins-root.ts b/apps/desktop/electron/desktop-plugins-root.ts
index 527bbc664ad..1db5eec0e13 100644
--- a/apps/desktop/electron/desktop-plugins-root.ts
+++ b/apps/desktop/electron/desktop-plugins-root.ts
@@ -181,27 +181,44 @@ export async function materializeDesktopHalf(
if (fs.existsSync(target)) {
if (!existing) {
- return null
+ // A real standalone install has its entry point. A marker-less folder
+ // without one is an interrupted unified-package copy: the old copy
+ // wrote the marker last, so leaving it here would block every retry.
+ if (fs.existsSync(path.join(target, 'plugin.js'))) {
+ return null
+ }
}
- if (existing.source === sourceDir && existing.sourceMtimeMs >= stat.mtimeMs) {
+ if (existing && existing.source === sourceDir && existing.sourceMtimeMs >= stat.mtimeMs) {
return null
}
-
- await fs.promises.rm(target, { force: true, recursive: true })
}
await fs.promises.mkdir(appRoot, { recursive: true })
- await fs.promises.cp(sourceDir, target, { force: true, 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 fs.promises.writeFile(path.join(target, PACKAGE_MARKER), JSON.stringify(marker, null, 2) + '\n')
+ const marker: DesktopHalfMarker = {
+ package: packageName,
+ source: sourceDir,
+ sourceMtimeMs: stat.mtimeMs,
+ ...(await packageOrigin(packageDir))
+ }
+
+ await fs.promises.writeFile(path.join(stagedTarget, PACKAGE_MARKER), JSON.stringify(marker, null, 2) + '\n')
+
+ // 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.
+ await fs.promises.rm(target, { force: true, recursive: true })
+ await fs.promises.rename(stagedTarget, target)
+ } finally {
+ await fs.promises.rm(stagingRoot, { force: true, recursive: true })
+ }
return target
}