π€― LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
π fix(heterogeneous-agents): rescan finds CLIs installed after the device host started (#23736)
LiJian12d3156bbed7
diff --git a/apps/cli/src/tools/scanHeterogeneousAgents.ts b/apps/cli/src/tools/scanHeterogeneousAgents.tsindex 458dd6bfffa..21ae39229a0 100644--- a/apps/cli/src/tools/scanHeterogeneousAgents.ts+++ b/apps/cli/src/tools/scanHeterogeneousAgents.ts@@ -8,6 +8,9 @@ export interface ScanHeterogeneousAgentsResult {/*** Probe every known heterogeneous agent type on this device in one pass.* Dispatched by the server via the `device.scanAgents` tRPC procedure.+ *+ * The scan re-reads the login-shell PATH on every call, so a CLI installed+ * after `lh connect` started shows up on the next scan without a restart.*/export async function scanHeterogeneousAgents(): Promise<ScanHeterogeneousAgentsResult> {return { agents: await scanHeterogeneousAgentsOnHost() };diff --git a/packages/heterogeneous-agents/src/scan/scanHost.rescan.test.ts b/packages/heterogeneous-agents/src/scan/scanHost.rescan.test.tsnew file mode 100644index 00000000000..ca5b58d669e--- /dev/null+++ b/packages/heterogeneous-agents/src/scan/scanHost.rescan.test.ts@@ -0,0 +1,113 @@+import * as childProcess from 'node:child_process';+import { chmodSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';+import type * as os from 'node:os';+import { tmpdir } from 'node:os';+import path from 'node:path';++import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';++// The resolver is real here: these cases are about what a second scan sees in+// a process that is still running, which a mocked resolver cannot show. Only+// process launches are faked, and the home folder is an empty temp directory.+const fake = vi.hoisted(() => ({ home: '' }));++vi.mock('node:os', async () => {+ const actual = await vi.importActual<typeof os>('node:os');+ return { ...actual, homedir: vi.fn(() => fake.home), platform: vi.fn(() => 'linux') };+});++vi.mock('node:child_process', () => ({ exec: vi.fn(), execFile: vi.fn() }));++const execFileMock = vi.mocked(childProcess.execFile);++const DAEMON_PATH = '/usr/local/bin:/usr/bin:/bin';+const LOGIN_SHELL = '/bin/bash';++/** The fake device: what the login shell prints and where codex is installed. */+const host: { codex?: string; loginShellPath: string } = { loginShellPath: DAEMON_PATH };++const loginShellReads = () =>+ execFileMock.mock.calls.filter(+ ([file, args]) => file === LOGIN_SHELL && Array.isArray(args) && args[0] === '-ilc',+ ).length;++const answer = (file: string, args: string[], env?: NodeJS.ProcessEnv): string | undefined => {+ if (file === LOGIN_SHELL && args[0] === '-ilc') return host.loginShellPath;+ if (!host.codex) return;++ if (file === 'which' && args[0] === 'codex') {+ const searched = (env?.PATH ?? process.env.PATH ?? '').split(':');+ return searched.includes(path.dirname(host.codex)) ? `${host.codex}\n` : undefined;+ }+ if (file === host.codex && args[0] === '--version') return 'codex-cli 0.171.0';+};++describe('scanHeterogeneousAgentsOnHost in a long-running host', () => {+ const original = { PATH: process.env.PATH, SHELL: process.env.SHELL };++ beforeEach(() => {+ vi.resetModules();+ fake.home = mkdtempSync(path.join(tmpdir(), 'scan-host-'));+ host.codex = undefined;+ host.loginShellPath = DAEMON_PATH;+ process.env.PATH = DAEMON_PATH;+ process.env.SHELL = LOGIN_SHELL;++ execFileMock.mockReset();+ execFileMock.mockImplementation(((file: string, args: string[], opts: any, cb: any) => {+ const callback = typeof opts === 'function' ? opts : cb;+ const stdout = answer(file, args, typeof opts === 'function' ? undefined : opts?.env);+ if (stdout === undefined) callback(new Error(`not found: ${file} ${args.join(' ')}`));+ else callback(null, { stderr: '', stdout });+ return {} as any;+ }) as any);+ });++ afterEach(() => {+ rmSync(fake.home, { force: true, recursive: true });+ process.env.PATH = original.PATH;+ if (original.SHELL === undefined) delete process.env.SHELL;+ else process.env.SHELL = original.SHELL;+ });++ it('finds a CLI installed after the first scan without restarting the host', async () => {+ const { scanHeterogeneousAgentsOnHost } = await import('./scanHost');++ const before = await scanHeterogeneousAgentsOnHost();+ expect(before.codex).toEqual({ available: false, version: undefined });++ // The user installs codex into a folder only their shell profile knows+ // about while `lh connect` keeps running, then presses "Rescan device".+ host.codex = path.join(fake.home, 'tools', 'bin', 'codex');+ host.loginShellPath = `${path.dirname(host.codex)}:${DAEMON_PATH}`;++ const after = await scanHeterogeneousAgentsOnHost();+ expect(after.codex).toEqual({ available: true, version: '0.171.0' });+ });++ it('reads the login shell once per scan, not once per agent', async () => {+ const { scanHeterogeneousAgentsOnHost } = await import('./scanHost');++ await scanHeterogeneousAgentsOnHost();+ expect(loginShellReads()).toBe(1);++ await scanHeterogeneousAgentsOnHost();+ expect(loginShellReads()).toBe(2);+ });++ it('finds codex in ~/.local/bin on Linux with no login shell to ask', async () => {+ // A user-level `npm i -g --prefix ~/.local` on a device whose service PATH+ // has no home folders and whose environment names no shell.+ delete process.env.SHELL;+ host.codex = path.join(fake.home, '.local', 'bin', 'codex');+ mkdirSync(path.dirname(host.codex), { recursive: true });+ writeFileSync(host.codex, '');+ chmodSync(host.codex, 0o755);++ const { scanHeterogeneousAgentsOnHost } = await import('./scanHost');+ const agents = await scanHeterogeneousAgentsOnHost();++ expect(agents.codex).toEqual({ available: true, version: '0.171.0' });+ expect(loginShellReads()).toBe(0);+ });+});diff --git a/packages/heterogeneous-agents/src/scan/scanHost.test.ts b/packages/heterogeneous-agents/src/scan/scanHost.test.tsindex 49b11fdf3aa..97c90242b8d 100644--- a/packages/heterogeneous-agents/src/scan/scanHost.test.ts+++ b/packages/heterogeneous-agents/src/scan/scanHost.test.ts@@ -8,6 +8,7 @@ import {probeRemotePlatform,resolveRemotePlatformCommand,resolveRemotePlatformRuntime,+ scanHeterogeneousAgentsOnHost,} from './scanHost';vi.mock('node:child_process', () => ({ execFile: vi.fn() }));@@ -20,16 +21,19 @@ vi.mock('node:os', async () => {const {detectHeterogeneousCliCommandMock,detectValidatedCommandCandidatesMock,+ invalidateLoginShellPathCacheMock,resolveCliSpawnPlanMock,} = vi.hoisted(() => ({detectHeterogeneousCliCommandMock: vi.fn(),detectValidatedCommandCandidatesMock: vi.fn(),+ invalidateLoginShellPathCacheMock: vi.fn(),resolveCliSpawnPlanMock: vi.fn(),}));vi.mock('../spawn/resolveCliCommand', () => ({detectHeterogeneousCliCommand: detectHeterogeneousCliCommandMock,detectValidatedCommandCandidates: detectValidatedCommandCandidatesMock,+ invalidateLoginShellPathCache: invalidateLoginShellPathCacheMock,}));vi.mock('../spawn/cliSpawn', () => ({@@ -140,6 +144,21 @@ describe('platform command scanning', () => {});});+ it('drops the cached login-shell PATH before a host scan probes anything', async () => {+ detectHeterogeneousCliCommandMock.mockResolvedValue({ available: false });+ detectValidatedCommandCandidatesMock.mockResolvedValue({ available: false });++ await scanHeterogeneousAgentsOnHost();++ expect(invalidateLoginShellPathCacheMock).toHaveBeenCalledTimes(1);+ const invalidatedAt = invalidateLoginShellPathCacheMock.mock.invocationCallOrder[0]!;+ const firstProbeAt = Math.min(+ ...detectHeterogeneousCliCommandMock.mock.invocationCallOrder,+ ...detectValidatedCommandCandidatesMock.mock.invocationCallOrder,+ );+ expect(invalidatedAt).toBeLessThan(firstProbeAt);+ });+it('binds spawn preparation to the validated executable and recovered PATH', async () => {detectValidatedCommandCandidatesMock.mockResolvedValue({available: true,diff --git a/packages/heterogeneous-agents/src/scan/scanHost.ts b/packages/heterogeneous-agents/src/scan/scanHost.tsindex e16a9878922..d8223ddb913 100644--- a/packages/heterogeneous-agents/src/scan/scanHost.ts+++ b/packages/heterogeneous-agents/src/scan/scanHost.ts@@ -9,6 +9,7 @@ import type { CliCommandStatus } from '../spawn/resolveCliCommand';import {detectHeterogeneousCliCommand,detectValidatedCommandCandidates,+ invalidateLoginShellPathCache,} from '../spawn/resolveCliCommand';import type { HeterogeneousAgentScanMap, HeterogeneousAgentScanStatus } from './types';@@ -183,7 +184,18 @@ export const probeRemotePlatform = async (: { available: false, reason: status.error };};+/**+ * Probe every known agent type on this machine.+ *+ * Each scan re-reads the login-shell PATH first. The scanner lives in a+ * long-running host (`lh connect`, Electron main) that otherwise keeps the PATH+ * it read at startup for the whole process, so a CLI installed afterwards β+ * the reason someone presses "Rescan device" β stayed invisible until the host+ * was restarted. All probes of one scan still share a single shell read.+ */export const scanHeterogeneousAgentsOnHost = async (): Promise<HeterogeneousAgentScanMap> => {+ invalidateLoginShellPathCache();+const entries = await Promise.all([...HETEROGENEOUS_AGENT_CONFIGS.map(async (config) => {const status = await detectHeterogeneousCliCommand(config.type, config.defaultCommand);