perf: reduce agent shell command probing

This commit is contained in:
jxxghp
2026-05-20 18:50:59 +08:00
parent 8a375e022c
commit c661bc4764
2 changed files with 67 additions and 59 deletions

View File

@@ -24,80 +24,29 @@ from app.utils.system import SystemUtils
SYSTEM_TASKS_FILE = "System Tasks.yaml"
SYSTEM_TASKS_SCHEMA_VERSION = 2
COMMON_SHELL_COMMANDS = (
# 只探测会明显改变 Agent 执行策略的可选能力。基础命令、语言运行时、
# 包管理器、服务管理器和数据库客户端默认不做启动探测,减少 which 扫描量。
"ssh",
"scp",
"sftp",
"rsync",
"git",
"gh",
"rg",
"fd",
"find",
"grep",
"sed",
"awk",
"jq",
"yq",
"curl",
"wget",
"tar",
"gzip",
"gunzip",
"zip",
"unzip",
"xz",
"7z",
"docker",
"docker-compose",
"kubectl",
"helm",
"sqlite3",
"psql",
"mysql",
"redis-cli",
"python",
"python3",
"pip",
"pip3",
"uv",
"node",
"npm",
"yarn",
"pnpm",
"bun",
"ffmpeg",
"ffprobe",
"mediainfo",
"rclone",
"aria2c",
"yt-dlp",
"openssl",
"base64",
"sha256sum",
"shasum",
"du",
"df",
"ps",
"top",
"lsof",
"netstat",
"ss",
"ping",
"traceroute",
"dig",
"nslookup",
"nc",
"telnet",
"crontab",
"systemctl",
"service",
"journalctl",
"launchctl",
"brew",
"apt",
"apk",
"yum",
"dnf",
)

View File

@@ -49,7 +49,7 @@ class TestAgentPromptStyle(unittest.TestCase):
def test_base_prompt_injects_available_shell_commands(self):
"""系统信息应注入 PATH 中已安装的常用命令,帮助 Agent 选择 execute_command。"""
command_paths = {
"ssh": "/usr/bin/ssh",
"git": "/usr/bin/git",
"rg": "/opt/homebrew/bin/rg",
}
with patch(
@@ -59,13 +59,13 @@ class TestAgentPromptStyle(unittest.TestCase):
prompt = prompt_manager.get_agent_prompt()
self.assertIn("- 可用系统命令(可通过 `execute_command` 调用):", prompt)
self.assertIn(" - ssh: /usr/bin/ssh", prompt)
self.assertIn(" - git: /usr/bin/git", prompt)
self.assertIn(" - rg: /opt/homebrew/bin/rg", prompt)
self.assertIn(
"When searching files or text, prefer `rg` / `rg --files`",
prompt,
)
self.assertNotIn(" - git:", prompt)
self.assertNotIn(" - ssh:", prompt)
def test_base_prompt_omits_shell_command_section_when_none_available(self):
"""PATH 中没有命中白名单命令时,不注入空的系统命令段落。"""
@@ -76,7 +76,7 @@ class TestAgentPromptStyle(unittest.TestCase):
def test_available_shell_commands_are_cached_after_first_scan(self):
"""常用命令探测应只在首次加载时扫描 PATH后续提示词复用缓存。"""
command_paths = {"ssh": "/usr/bin/ssh"}
command_paths = {"git": "/usr/bin/git"}
with patch(
"app.agent.prompt.shutil.which",
side_effect=lambda command: command_paths.get(command),
@@ -84,10 +84,69 @@ class TestAgentPromptStyle(unittest.TestCase):
first_prompt = prompt_manager.get_agent_prompt()
second_prompt = prompt_manager.get_agent_prompt()
self.assertIn(" - ssh: /usr/bin/ssh", first_prompt)
self.assertIn(" - ssh: /usr/bin/ssh", second_prompt)
self.assertIn(" - git: /usr/bin/git", first_prompt)
self.assertIn(" - git: /usr/bin/git", second_prompt)
self.assertEqual(which_mock.call_count, len(COMMON_SHELL_COMMANDS))
def test_common_shell_commands_skip_linux_basics(self):
"""不影响任务策略的通用命令不进入启动探测列表,避免重复 which。"""
low_value_commands = {
"rsync",
"find",
"grep",
"sed",
"awk",
"tar",
"gzip",
"gunzip",
"base64",
"du",
"df",
"ps",
"top",
"ping",
"pip",
"pip3",
"uv",
"node",
"npm",
"yarn",
"pnpm",
"bun",
"sqlite3",
"psql",
"mysql",
"redis-cli",
"kubectl",
"helm",
"lsof",
"netstat",
"ss",
"traceroute",
"dig",
"nslookup",
"nc",
"telnet",
"crontab",
"systemctl",
"service",
"journalctl",
"launchctl",
"brew",
"apt",
"apk",
"yum",
"dnf",
}
self.assertFalse(low_value_commands & set(COMMON_SHELL_COMMANDS))
def test_common_shell_commands_keep_extra_install_runtime_tools(self):
"""需要额外安装且会影响执行方式的运行时工具应保留探测。"""
expected_commands = {"ssh", "scp", "sftp", "python", "python3"}
self.assertTrue(expected_commands <= set(COMMON_SHELL_COMMANDS))
def test_runtime_config_middleware_injects_persona_only(self):
middleware = RuntimeConfigMiddleware()
updated_request = middleware.modify_request(_FakeRequest())