Hermes Agent refactors itself with 1393 agents
This demonstrates a powerful new capability for large-scale code refactoring using AI agents, which could be useful for developers managing complex codebases.
NousResearch used its own open-source Agent, Hermes, to refactor the Hermes codebase.
The main run took about 19 effective hours, deploying 1393 sub-agents (peaking at 218 in parallel).This reduced the non-test Python code by 34.
4%, costing about 19,300 US dollars.The manual cost for the same work was estimated at 150,000 to 1.
8 million US dollars.The codebase had over 1.
06 million lines of non-test Python, with one file reaching 34,847 lines.The team had previously prioritized new features and bug fixes over this cleanup.
The process involved a top-level agent that coordinated tasks, dividing the code into 36 groups and creating written task sheets for each worker.
Workers operated in independent git worktrees to avoid conflicts.A key verification step was ensuring the JSON schema and CLI help output matched the original version exactly.
On September 4, after two community reviews, the changes were merged.
用 1393 个 Agent 重构 Hermes Agent @NousResearch 让自家开源 Agent「Hermes」去重构 Hermes 自己的代码库:主运行约 19 个有效小时、派出 1393 个子 Agent(峰值 218 个并行),把超百万行的非测试 Python 代码削减了 34.4%,模型花费约 1.93 万美元;而人工做同样工作的估算成本是 15 万至 180 万美元。 nousresearch.com/refactoring-he… 背景:为什么这次重构一直没做 ? Hermes 仓库的非测试 Python 代码超过 106 万行,gateway/run.py 单文件就有 34847 行。这类清理工作技术含量低但工作量巨大,团队一直优先做新功能和修 Bug,没人愿意排期。 @Teknium 的诉求很明确:文件变小、辅助函数统一、消灭“上帝文件”和超长函数、砍掉层层嵌套的 if/elif 路由,整体代码量至少降 30%,且“不许等他决策,做完直接交 PR”。 # 运作机制:文章最有信息量的部分 1. 技能积累是前提,不是事后包装。 Teknium每天都在用 Hermes 开发 Hermes。当流程被纠正或走通时,Hermes 会自动把经验沉淀为 Skills,例如“失败的检查要在干净的 origin/main 环境复现,以判断是否为存量问题”。几个月的日常协作攒下的工程规范,才是这次能放手交给它的底座。 2. 编排者–工人的树状分工。 顶层 Agent 只做协调(写任务书、读报告、合并分支、跑检查),不直接改代码。它把代码库切成 36 个互不重叠的组,为每个工人写书面任务书;工人在独立的 git worktree 里工作,避免互相覆盖;部分工人还会继续下派,委托树最深达到三层。 3. 接口保真作为验证锚点。 工具的 JSON schema 必须与原版完全一致,CLI 的 --help 输出可以逐字节比对;每完成一个经过验证的步骤就必须提交。这解决了大规模自动重构最核心的风险——改散架了怎么办。 4. 故障恢复。 运行约 50 分钟后,API 认证 token 过期导致整个运行崩溃。但工人的提交和任务书都还在(本地 git worktree 是持久化的)。Teknium 用另一个 Hermes 会话诊断故障、准备交接材料,恢复会话后工人检查各自未完成的改动、修复中断的抽取、继续推进。9 月 4 日,经两轮社区评审修复后合并。 # 结果:数字与诚实的边界 重构前 vs. 重构后 · 非测试 Python 总行数:1063826 vs. 698363 · 超过 5,000 行的文件:37 vs. 6 · 超过 300 行的函数:192 vs. 2 · 最长 if/elif 链:92 分支 vs. 9 分支 · gateway/run.py:34847 行 vs. 5512 行 对 Agent 友好度的量化验证是这一段:他们在重构前后两个版本上模拟同样 4000 个符号的查找(找定义 + 读 60 行窗口,不够再续读 2000 行窗口)。结果每次查找平均返回的 token 从 2218 降到 993,需要追加读取窗口的查找从 628 次降到 184 次。也就是说,重构后的代码库对 Agent(和人)来说“翻代码”的成本大约减半。 连带收获:Agent 基础设施自身的改进 跑几百个 worker 暴露了 Hermes 自身的问题:各 worktree 各自启动了约 30 个 Pyright 语言服务器,吃掉 8.7 GB 内存(后来改为共享一个实例);HTTP 传输重复、已结束的 Agent 被引用滞留在内存中,都做了修复。仓库还新增了文件大小、函数复杂度的规范,以及“被删除的公开名称需要评审”的检查。这些经验又自动写回了 Skills 文档,可供整个团队复用。 Nous Research @NousResearch New blog post: We had a million lines of Python to clean up. On September 2nd @Teknium asked Hermes Agent to do it. 1,393 subagents and nineteen hours later, the codebase was 34.4% smaller, saving us nearly $2m in engineering hours. nousresearch.com/refactoring-he… 🔗 View Quoted Tweet📊 1 ⚡
This report is categorized under AITOP's "Models" desk, focusing on US frontier AI labs and Silicon Valley technology leaders. Within our cross-regional analytical framework, it directly maps to AI developer toolchains, coding agents, and engineering workflows and US-China foundation model benchmarks and commercialization comparison. Key entities and technical topics under continuous tracking include Hermes, NousResearch, AI agents, code refactoring.
Source attribution: curated from shao__meng (@shao__meng).