The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): run the plain-tree check where every desktop tree is published
Teknium5e6585779948
diff --git a/apps/desktop/electron/desktop-plugin-install.ts b/apps/desktop/electron/desktop-plugin-install.tsindex f897f90f34e..d4e0113fe6a 100644--- a/apps/desktop/electron/desktop-plugin-install.ts+++ b/apps/desktop/electron/desktop-plugin-install.ts@@ -454,45 +454,6 @@ export function guiInstallSourceRefusal(identifier: string): null | string {return 'Plugins are installed from Hermes Desktop over https:// only. Use `hermes plugins install` in a terminal for other sources.'}-// The same cap the Webapp's host-side desktop install applies to the copied tree.-const DESKTOP_TREE_MAX_BYTES = 64 * 1024 * 1024--/** Refuse a desktop tree that reaches outside the clone or is too big to copy. A symlink anywhere on the- * way to it, or inside it, would publish whatever it points at on this machine (or a dangling link into- * the deleted clone) as part of the plugin, so the tree must be plain files and folders. */-async function assertPlainDesktopTree(cloneRoot: string, sourceDir: string): Promise<void> {- const realRoot = await fsp.realpath(cloneRoot)-- if ((await fsp.realpath(sourceDir)) !== path.join(realRoot, path.relative(cloneRoot, sourceDir))) {- throw new Error('The desktop plugin folder is a symbolic link; it is not installed.')- }-- let total = 0- const pending = [sourceDir]-- while (pending.length > 0) {- const dir = pending.pop()!-- for (const entry of await fsp.readdir(dir, { withFileTypes: true })) {- const full = path.join(dir, entry.name)-- if (entry.isSymbolicLink()) {- throw new Error(`The desktop plugin contains a symbolic link (${path.relative(sourceDir, full)}); it is not installed.`)- }-- if (entry.isDirectory()) {- pending.push(full)- } else {- total += (await fsp.lstat(full)).size-- if (total > DESKTOP_TREE_MAX_BYTES) {- throw new Error(`The desktop plugin is larger than ${DESKTOP_TREE_MAX_BYTES / (1024 * 1024)} MB.`)- }- }- }- }-}-function insecureSchemeWarnings(gitUrl: string): { warnings: string[]; insecure: boolean } {if (gitUrl.startsWith('http://') || gitUrl.startsWith('file://')) {return {@@ -579,8 +540,6 @@ export async function installDesktopPluginFromGit(const sourceDir =detected.desktopSourceSubdir === '.' ? pluginRoot : path.join(pluginRoot, detected.desktopSourceSubdir)- await assertPlainDesktopTree(cloneRoot, sourceDir)-// A repo carrying BOTH halves is one package: land its desktop half under// the AGENT package name, so the copy this app makes and the one// `reconcileUnifiedDesktopHalves` would make are the same folder (#100412)@@ -611,7 +570,7 @@ export async function installDesktopPluginFromGit(// part of that publication — without it the Plugins page cannot tell this// copy belongs to the agent row (it sits on "copying…" forever) and the// half loads default-enabled instead of opt-in.- await publishDesktopTree(sourceDir, targetDir, async staged => {+ await publishDesktopTree({ dir: sourceDir, plainWithin: cloneRoot }, targetDir, async staged => {if (!packageName) {// A desktop-only repo is a standalone plugin. A package marker it// ships would name another package's agent half as its own (pairingdiff --git a/apps/desktop/electron/desktop-plugins-root.test.ts b/apps/desktop/electron/desktop-plugins-root.test.tsindex 426f4611766..a1ce00675aa 100644--- a/apps/desktop/electron/desktop-plugins-root.test.ts+++ b/apps/desktop/electron/desktop-plugins-root.test.ts@@ -425,3 +425,61 @@ describe('reconcileUnifiedDesktopHalves', () => {expect(fs.readdirSync(appRoot)).toEqual([])})})++describe('desktop tree publication', () => {+ it.skipIf(process.platform === 'win32')(+ 'refuses an installed combined package whose desktop half links elsewhere in the package',+ async () => {+ // The Desktop install modal lands a combined package through the agent install, then publishes its+ // desktop half from plugins/<name>/desktop via this reconcile, not through the git installer.+ const home = makeHome()+ const appRoot = path.join(home, 'desktop-plugins')+ const pkg = path.join(home, 'plugins', 'media')+ write(path.join(pkg, 'plugin.yaml'), 'name: media\n')+ write(path.join(pkg, 'assets', 'asset.txt'), 'package file')+ write(path.join(pkg, 'desktop', 'plugin.js'), 'export default {}')+ fs.symlinkSync('../assets/asset.txt', path.join(pkg, 'desktop', 'asset.txt'))+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)++ try {+ expect(await reconcileUnifiedDesktopHalves(home, appRoot)).toEqual([])+ expect(warn.mock.calls.flat().join(' ')).toContain('symbolic link (asset.txt)')+ } finally {+ warn.mockRestore()+ }++ // Nothing published and nothing staged.+ expect(fs.existsSync(appRoot) ? fs.readdirSync(appRoot) : []).toEqual([])+ }+ )++ it.skipIf(process.platform === 'win32')('refuses an installed package whose desktop folder is a link', async () => {+ const home = makeHome()+ const appRoot = path.join(home, 'desktop-plugins')+ write(path.join(home, 'elsewhere', 'plugin.js'), 'export default {}')+ fs.mkdirSync(path.join(home, 'plugins', 'media'), { recursive: true })+ fs.symlinkSync(path.join(home, 'elsewhere'), path.join(home, 'plugins', 'media', 'desktop'))++ await expect(materializeDesktopHalf(path.join(home, 'plugins', 'media'), appRoot)).rejects.toThrow(+ 'folder is a symbolic link'+ )+ expect(fs.existsSync(path.join(appRoot, 'media'))).toBe(false)+ })++ it.skipIf(process.platform === 'win32')(+ 'still publishes a development checkout the user linked in, links inside it included',+ async () => {+ const home = makeHome()+ const appRoot = path.join(home, 'desktop-plugins')+ const checkout = path.join(home, 'src', 'media')+ write(path.join(checkout, 'desktop', 'plugin.js'), 'export default {}')+ write(path.join(checkout, 'assets', 'asset.txt'), 'checkout file')+ fs.symlinkSync('../assets/asset.txt', path.join(checkout, 'desktop', 'asset.txt'))+ fs.mkdirSync(path.join(home, 'plugins'), { recursive: true })+ fs.symlinkSync(checkout, path.join(home, 'plugins', 'media'))++ expect(await reconcileUnifiedDesktopHalves(home, appRoot)).toEqual([path.join(appRoot, 'media')])+ expect(fs.readFileSync(path.join(appRoot, 'media', 'asset.txt'), 'utf8')).toBe('checkout file')+ }+ )+})diff --git a/apps/desktop/electron/desktop-plugins-root.ts b/apps/desktop/electron/desktop-plugins-root.tsindex 48b9060febc..b981b902637 100644--- a/apps/desktop/electron/desktop-plugins-root.ts+++ b/apps/desktop/electron/desktop-plugins-root.ts@@ -264,51 +264,12 @@ async function desktopHalfEntry(return null}-/** The whole published folder; the loader reads at most a 16 MB `plugin.js` from it. */-const TREE_MAX_BYTES = 64 * 1024 * 1024-/** A root-layout half publishes the package root, never its clone's `.git` or* the agent half's `__pycache__` (the gateway's `ROOT_LAYOUT_SKIP`). */const ROOT_LAYOUT_SKIP = new Set(['.git', '__pycache__'])const rootLayoutFilter = (src: string) => !ROOT_LAYOUT_SKIP.has(path.basename(src))-/** Why `source` cannot be published as a desktop plugin, else `null`: it must- * be a plain tree (a link could publish any folder of this machine) under- * `TREE_MAX_BYTES` (the gateway's `check_desktop_tree`). `source` itself may- * be a linked dev checkout; nothing inside it may be a link. */-async function plainTreeRefusal(source: string): Promise<null | string> {- let total = 0-- const walk = async (dir: string): Promise<null | string> => {- for (const entry of await fs.promises.readdir(dir, { withFileTypes: true })) {- const full = path.join(dir, entry.name)-- if (entry.isDirectory() && ROOT_LAYOUT_SKIP.has(entry.name)) {- continue- }-- if (entry.isSymbolicLink()) {- return `The desktop plugin contains a link (${path.relative(source, full)}); links are not installed.`- }-- if (entry.isDirectory()) {- const refused = await walk(full)-- if (refused) {- return refused- }- } else if ((total += (await fs.promises.lstat(full)).size) > TREE_MAX_BYTES) {- return `The desktop plugin is larger than ${TREE_MAX_BYTES / (1024 * 1024)} MB.`- }- }-- return null- }-- return walk(source)-}-/** Copy one unified package's desktop half into the app root as* `<appRoot>/<packageName>/`, stamping the marker: its `desktop/` folder, or* the package root (minus `ROOT_LAYOUT_SKIP`) when `plugin.js` sits at the root, as the@@ -391,20 +352,12 @@ export async function materializeDesktopHalf(}}- if (rootLayout) {- // The whole package root is published, agent payload included: hold it to- // the installer's plain-tree rule.- const refused = await plainTreeRefusal(sourceDir)-- if (refused) {- console.warn(`[desktop-plugins] not copying the desktop half of ${packageName}: ${refused}`)-- return null- }- }-+ // A package folder symlinked in by hand is the user's own development checkout and its desktop/+ // folder is copied as it stands. An installed package is checked like a fresh clone (a link inside+ // it publishes host files), and so is a root-layout half, linked or not: it publishes the whole+ // package root, agent payload included.await publishDesktopTree(- sourceDir,+ { dir: sourceDir, plainWithin: linked && !rootLayout ? null : packageDir },target,staged => writeDesktopHalfMarker(staged, marker),rootLayout ? rootLayoutFilter : undefined@@ -413,19 +366,83 @@ export async function materializeDesktopHalf(return target}-/** 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.+/** The whole published folder (the Webapp's host-side install applies the same cap); the loader reads+ * at most a 16 MB `plugin.js` from it. */+const DESKTOP_TREE_MAX_BYTES = 64 * 1024 * 1024++/** What `publishDesktopTree` copies. `plainWithin` names the clone or installed package the tree must+ * stay inside as plain files and folders; `null` only for a development checkout the user linked in. */+export interface DesktopTreeSource {+ dir: string+ plainWithin: null | string+}++/** Refuse a desktop tree that reaches outside its package or is too big to copy. A symlink anywhere on+ * the way to it, or inside it, would publish whatever it points at on this machine (or a dangling link+ * into a deleted clone) as part of the plugin, so the tree must be plain files and folders. */+async function assertPlainDesktopTree(+ root: string,+ sourceDir: string,+ filter: (src: string) => boolean = () => true+): Promise<void> {+ const realRoot = await fs.promises.realpath(root)++ if ((await fs.promises.realpath(sourceDir)) !== path.join(realRoot, path.relative(root, sourceDir))) {+ throw new Error('The desktop plugin folder is a symbolic link; it is not installed.')+ }++ let total = 0+ const pending = [sourceDir]++ while (pending.length > 0) {+ const dir = pending.pop()!++ for (const entry of await fs.promises.readdir(dir, { withFileTypes: true })) {+ const full = path.join(dir, entry.name)++ // What the copy leaves out (a root-layout half's `.git`, `__pycache__`) is not published.+ if (!filter(full)) {+ continue+ }++ if (entry.isSymbolicLink()) {+ throw new Error(+ `The desktop plugin contains a symbolic link (${path.relative(sourceDir, full)}); it is not installed.`+ )+ }++ if (entry.isDirectory()) {+ pending.push(full)+ } else {+ total += (await fs.promises.lstat(full)).size++ if (total > DESKTOP_TREE_MAX_BYTES) {+ throw new Error(`The desktop plugin is larger than ${DESKTOP_TREE_MAX_BYTES / (1024 * 1024)} MB.`)+ }+ }+ }+ }+}++/** Copy `source.dir` to `target` through a staging sibling (`<parent>/.<name>.staging-*`)+ * and rename the finished tree into place. Every route that publishes a desktop tree (git install,+ * package reconcile) comes through here, so the plain-tree check runs once for all of them, before+ * anything is staged. `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,+ source: DesktopTreeSource,target: string,finalize?: (staged: string) => Promise<void>,filter?: (src: string) => boolean): Promise<void> {+ if (source.plainWithin !== null) {+ await assertPlainDesktopTree(source.plainWithin, source.dir, filter)+ }+const parent = path.dirname(target)const name = path.basename(target)@@ -436,7 +453,7 @@ export async function publishDesktopTree(try {// From the resolved folder: `cp` keeps a source that is itself a link (a dev checkout linked into// `plugins/`) as a link, so the published "copy" was the checkout and the marker landed in it.- await fs.promises.cp(await fs.promises.realpath(sourceDir), staged, { filter, force: true, recursive: true })+ await fs.promises.cp(await fs.promises.realpath(source.dir), staged, { filter, 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 })@@ -507,7 +524,8 @@ async function reconcilePass(hermesHome: string, appRoot: string): Promise<strin// One package the app cannot read (Windows ACL EPERM on lstat/copy, a// mode-000 folder) must not reject the whole reconcile — the root would// never resolve and EVERY desktop plugin would silently stop loading.- console.warn(`[desktop-plugins] skipping unreadable package ${name}: ${String(error)}`)+ // A desktop half refused by the plain-tree check lands here too.+ console.warn(`[desktop-plugins] skipping package ${name}: ${String(error)}`)continue}