Skip to content

A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.

Find local agent CLIs without blocking DS Bot's start

Fei2411cf36a5178af9
diff --git a/ds-bot/src/host/local-agents/discover.js b/ds-bot/src/host/local-agents/discover.js
index 6970aa68..f27babfa 100644
--- a/ds-bot/src/host/local-agents/discover.js
+++ b/ds-bot/src/host/local-agents/discover.js
@@ -1,7 +1,7 @@
// Finding agent CLIs and the environment they run in: PATH plus the user's
// login-shell PATH plus the usual install folders, and an environment that
// keeps their own sign-in but leaks nothing of ours.
-import { execFile, execFileSync } from 'node:child_process'
+import { execFile } from 'node:child_process'
import { existsSync } from 'node:fs'
import { homedir } from 'node:os'
import { delimiter, join } from 'node:path'
@@ -23,26 +23,28 @@ export function cleanEnv(extra = {}) {
return { ...env, ...extra }
}
-// macOS/Linux apps launched from a dock icon inherit a sparse PATH; the login
-// shell knows where the user actually installed things. Cached for a minute:
-// a lookup burst (a page that scans every engine) pays for it once.
-let loginPath = { at: 0, value: undefined }
-function loginShellPath() {
- if (Date.now() - loginPath.at < 60_000) return loginPath.value
- loginPath = { at: Date.now(), value: undefined }
- if (process.platform === 'win32') return undefined
- const shell = process.env.SHELL ?? '/bin/sh'
- try {
- const out = execFileSync(shell, ['-l', '-c', 'printf %s "$PATH"'], { encoding: 'utf8', timeout: 5_000, env: cleanEnv() })
- loginPath.value = out.split(delimiter).filter(Boolean)
- } catch { /* no login shell PATH; what we have is enough */ }
- return loginPath.value
-}
+// A command's output, or undefined. Never synchronous: this runs inside the DSH
+// host, and a blocking child process there freezes every Session.
+const outputOf = (file, args) => new Promise((resolve) => {
+ execFile(file, args, { encoding: 'utf8', timeout: 5_000, env: cleanEnv() }, (error, stdout) => resolve(error === null ? stdout : undefined))
+})
-function npmGlobalBin() {
- try {
- return execFileSync(process.platform === 'win32' ? 'npm.cmd' : 'npm', ['bin', '-g'], { encoding: 'utf8', timeout: 5_000, env: cleanEnv() }).trim()
- } catch { return undefined }
+// macOS/Linux apps launched from a dock icon inherit a sparse PATH; the login
+// shell knows where the user actually installed things, and so does npm's global
+// prefix. Both are looked up in the background and kept for a minute.
+let found = { at: 0, dirs: [] }
+let looking
+export function refreshSearchDirs() {
+ if (Date.now() - found.at < 60_000) return Promise.resolve()
+ looking ??= Promise.all([
+ process.platform === 'win32' ? undefined : outputOf(process.env.SHELL ?? '/bin/sh', ['-l', '-c', 'printf %s "$PATH"']),
+ // `npm bin -g` is gone since npm 9; the prefix holds the bin folder.
+ outputOf(process.platform === 'win32' ? 'npm.cmd' : 'npm', ['prefix', '-g']),
+ ]).then(([loginPath, prefix]) => {
+ const npmBin = prefix?.trim() ? (process.platform === 'win32' ? prefix.trim() : join(prefix.trim(), 'bin')) : undefined
+ found = { at: Date.now(), dirs: [...(loginPath ?? '').split(delimiter).filter(Boolean), ...(npmBin ? [npmBin] : [])] }
+ }).finally(() => { looking = undefined })
+ return looking
}
const WIN_EXT = ['.cmd', '.exe', '.bat', '']
@@ -52,21 +54,21 @@ const candidates = (name) => (process.platform === 'win32' ? WIN_EXT.map(ext =>
function searchDirs() {
const home = homedir()
const dirs = (process.env.PATH ?? '').split(delimiter).filter(Boolean)
- for (const dir of loginShellPath() ?? []) if (!dirs.includes(dir)) dirs.push(dir)
for (const extra of [
+ ...found.dirs,
join(home, '.local', 'bin'),
join(home, '.local', 'share', 'devin', 'cli', '_versions', 'current', 'bin'),
join(home, 'bin'),
'/opt/homebrew/bin',
'/usr/local/bin',
- npmGlobalBin(),
]) {
- if (extra !== undefined && extra !== '' && !dirs.includes(extra) && existsSync(extra)) dirs.push(extra)
+ if (extra !== '' && !dirs.includes(extra) && existsSync(extra)) dirs.push(extra)
}
return dirs
}
-// The executable's full path, or undefined when the CLI is not installed.
+// The executable's full path, or undefined when the CLI is not installed. Only
+// file checks: the folders from refreshSearchDirs() join once it has run.
export function findExecutable(name) {
if (name.includes('/') || name.includes('\\')) {
const path = name.startsWith('~') ? join(homedir(), name.slice(1)) : name
diff --git a/ds-bot/src/host/local-agents/index.js b/ds-bot/src/host/local-agents/index.js
index 75d4e5a2..4967db9f 100644
--- a/ds-bot/src/host/local-agents/index.js
+++ b/ds-bot/src/host/local-agents/index.js
@@ -13,7 +13,7 @@ import { AcpEngine } from './acp.js'
import { CodexEngine } from './codex.js'
import { ClaudeEngine } from './claude.js'
import { ENGINES, customRoute, isLocalAgentRoute, validateCustom } from './engines.js'
-import { findExecutable, probeVersion } from './discover.js'
+import { findExecutable, probeVersion, refreshSearchDirs } from './discover.js'
const SIGN_IN = {
'devin-cli': 'devin auth login',
@@ -50,6 +50,7 @@ export function install(rt) {
// Only CLIs found on this machine get a route; the scan result feeds the page.
const found = new Map()
async function scan() {
+ await refreshSearchDirs()
for (const route of Object.keys(ENGINES)) {
const spec = ENGINES[route]
const path = findExecutable(spec.command)
@@ -191,6 +192,7 @@ export function install(rt) {
}
case 'local-agent-add': {
const clean = validateCustom(body)
+ await refreshSearchDirs()
if (findExecutable(clean.command) === undefined && !clean.command.includes('/') && !clean.command.includes('\\')) {
throw new Error(`No program named ${clean.command} was found`)
}
diff --git a/ds-bot/test/local-agents-discover.test.js b/ds-bot/test/local-agents-discover.test.js
new file mode 100644
index 00000000..29666c99
--- /dev/null
+++ b/ds-bot/test/local-agents-discover.test.js
@@ -0,0 +1,32 @@
+import assert from 'node:assert/strict'
+import { chmodSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
+import { tmpdir } from 'node:os'
+import { delimiter, join } from 'node:path'
+import { test } from 'node:test'
+import { findExecutable, refreshSearchDirs } from '../src/host/local-agents/discover.js'
+
+// A slow npm on PATH stands for the real one: it takes seconds to start, and the
+// host must not wait for it on the way to a Session.
+test('finding a CLI never waits on npm, and npm\'s global folder joins in the background', { skip: process.platform === 'win32' }, async (t) => {
+ const root = mkdtempSync(join(tmpdir(), 'dsb-discover-'))
+ t.after(() => rmSync(root, { recursive: true, force: true }))
+ const fakeBin = join(root, 'bin')
+ const prefix = join(root, 'npm-global')
+ mkdirSync(fakeBin)
+ mkdirSync(join(prefix, 'bin'), { recursive: true })
+ writeFileSync(join(fakeBin, 'npm'), `#!/bin/sh\nsleep 2\nprintf '%s\\n' '${prefix}'\n`)
+ writeFileSync(join(prefix, 'bin', 'dsb-fake-agent'), '#!/bin/sh\n')
+ chmodSync(join(fakeBin, 'npm'), 0o755)
+ chmodSync(join(prefix, 'bin', 'dsb-fake-agent'), 0o755)
+ const { PATH, SHELL } = process.env
+ process.env.PATH = `${fakeBin}${delimiter}${PATH}`
+ process.env.SHELL = '/bin/false'
+ t.after(() => { process.env.PATH = PATH; process.env.SHELL = SHELL })
+
+ const started = Date.now()
+ assert.equal(findExecutable('dsb-fake-agent'), undefined)
+ const refresh = refreshSearchDirs()
+ assert.ok(Date.now() - started < 1000, 'the lookup waited on npm')
+ await refresh
+ assert.equal(findExecutable('dsb-fake-agent'), join(prefix, 'bin', 'dsb-fake-agent'))
+})