The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): plugin probe/install from a repo subfolder downloads only that folder
teknium1d188a47e9a47
diff --git a/apps/desktop/electron/desktop-plugin-install.test.ts b/apps/desktop/electron/desktop-plugin-install.test.tsindex f455cc9826e..4aefbccd80f 100644--- a/apps/desktop/electron/desktop-plugin-install.test.ts+++ b/apps/desktop/electron/desktop-plugin-install.test.ts@@ -1,7 +1,8 @@+import { execFileSync } from 'node:child_process'import fs from 'node:fs'import os from 'node:os'import path from 'node:path'-import { fileURLToPath } from 'node:url'+import { fileURLToPath, pathToFileURL } from 'node:url'import { afterEach, describe, expect, it } from 'vitest'@@ -9,6 +10,7 @@ import {desktopPluginFolderName,detectPluginComponents,findDesktopEntry,+ probePluginRepo,resolvePluginGitUrl,resolveSubdirWithin} from './desktop-plugin-install'@@ -119,3 +121,32 @@ describe('detectPluginComponents', () => {})})})++describe('probePluginRepo', () => {+ const roots: string[] = []++ afterEach(() => {+ for (const root of roots.splice(0)) {+ fs.rmSync(root, { recursive: true, force: true })+ }+ })++ it('probes a monorepo subdirectory through the sparse partial clone', async () => {+ const repo = mkdtemp('hermes-plugin-monorepo-')+ roots.push(repo)+ const git = (...args: string[]) => execFileSync('git', args, { cwd: repo, stdio: 'pipe' })+ const plugin = path.join(repo, 'integrations', 'hermes')+ fs.mkdirSync(plugin, { recursive: true })+ fs.writeFileSync(path.join(plugin, 'plugin.yaml'), 'name: nested-agent\n')+ fs.writeFileSync(path.join(plugin, '__init__.py'), 'def register(ctx): pass\n')+ fs.writeFileSync(path.join(repo, 'unrelated.bin'), 'x'.repeat(4096))+ git('init', '-q')+ git('config', 'uploadpack.allowFilter', 'true')+ git('add', '.')+ git('-c', '[email protected]', '-c', 'user.name=Fixture', 'commit', '-qm', 'init')++ const result = await probePluginRepo('git', `${pathToFileURL(repo).href}#integrations/hermes`)++ expect(result).toMatchObject({ ok: true, agent: true, agentName: 'nested-agent' })+ })+})diff --git a/apps/desktop/electron/desktop-plugin-install.ts b/apps/desktop/electron/desktop-plugin-install.tsindex d34cea1c37b..abdc538bf5c 100644--- a/apps/desktop/electron/desktop-plugin-install.ts+++ b/apps/desktop/electron/desktop-plugin-install.ts@@ -262,6 +262,9 @@ function noninteractiveGitEnv(): NodeJS.ProcessEnv {}}+// Matches the backend's default `plugins.clone_timeout_seconds`.+const GIT_TIMEOUT_MS = 300_000+function runGit(gitBin: string, args: string[], cwd?: string): Promise<{ code: number; stderr: string }> {return new Promise((resolve, reject) => {const child = spawn(gitBin, args, {@@ -275,8 +278,8 @@ function runGit(gitBin: string, args: string[], cwd?: string): Promise<{ code: nconst timer = setTimeout(() => {child.kill('SIGKILL')- reject(new Error('Git clone timed out after 60 seconds.'))- }, 60_000)+ reject(new Error(`Git ${args[0]} timed out after ${GIT_TIMEOUT_MS / 1000} seconds.`))+ }, GIT_TIMEOUT_MS)child.stderr?.on('data', chunk => {stderr += String(chunk)@@ -294,16 +297,38 @@ function runGit(gitBin: string, args: string[], cwd?: string): Promise<{ code: n})}-async function cloneToTemp(gitBin: string, gitUrl: string): Promise<string> {+async function runGitOrThrow(gitBin: string, args: string[], cwd?: string): Promise<void> {+ const { code, stderr } = await runGit(gitBin, args, cwd)++ if (code !== 0) {+ throw new Error(`Git ${args[0]} failed:\n${stderr.trim()}`)+ }+}++/** Sparse-check-out only `subdir` via the classic pattern file, which older Git clients understand. */+function sparseCheckoutPattern(subdir: string): string {+ return `/${subdir.replace(/^\/+|\/+$/g, '').replace(/([\\*?[])/g, '\\$1')}/\n`+}++// A subdirectory install is a blobless clone with a sparse checkout of that folder: a plugin inside+// a monorepo (Hindsight: 170 MB at depth 1, 2 MB for its plugin folder) otherwise downloads every+// file in the repository and times out on slow connections.+async function cloneToTemp(gitBin: string, gitUrl: string, subdir: string | null): Promise<string> {const tmpRoot = await fsp.mkdtemp(path.join(os.tmpdir(), 'hermes-plugin-'))try {- const { code, stderr } = await runGit(gitBin, ['clone', '--depth', '1', gitUrl, tmpRoot])+ if (!subdir) {+ await runGitOrThrow(gitBin, ['clone', '--depth', '1', gitUrl, tmpRoot])- if (code !== 0) {- throw new Error(`Git clone failed:\n${stderr.trim()}`)+ return tmpRoot}+ await runGitOrThrow(gitBin, ['clone', '--depth', '1', '--filter=blob:none', '--no-checkout', gitUrl, tmpRoot])+ await runGitOrThrow(gitBin, ['config', 'core.sparseCheckout', 'true'], tmpRoot)+ await fsp.mkdir(path.join(tmpRoot, '.git', 'info'), { recursive: true })+ await fsp.writeFile(path.join(tmpRoot, '.git', 'info', 'sparse-checkout'), sparseCheckoutPattern(subdir), 'utf8')+ await runGitOrThrow(gitBin, ['checkout', 'HEAD'], tmpRoot)+return tmpRoot} catch (err) {await fsp.rm(tmpRoot, { recursive: true, force: true }).catch(() => undefined)@@ -340,7 +365,7 @@ export async function probePluginRepo(gitBin: string, identifier: string): Promitry {const { gitUrl, subdir } = resolvePluginGitUrl(identifier)const { warnings, insecure } = insecureSchemeWarnings(gitUrl)- const cloneRoot = await cloneToTemp(gitBin, gitUrl)+ const cloneRoot = await cloneToTemp(gitBin, gitUrl, subdir)try {const pluginRoot = await resolvePluginRoot(cloneRoot, subdir)@@ -390,7 +415,7 @@ export async function installDesktopPluginFromGit(): Promise<DesktopPluginInstallResult> {try {const { gitUrl, subdir } = resolvePluginGitUrl(identifier)- const cloneRoot = await cloneToTemp(gitBin, gitUrl)+ const cloneRoot = await cloneToTemp(gitBin, gitUrl, subdir)try {const pluginRoot = await resolvePluginRoot(cloneRoot, subdir)