feat: 智能 CPU 负载阈值自动计算 + dotenv 依赖修复#144
Closed
blackdogcat wants to merge 1 commit intoEvoMap:mainfrom
Closed
Conversation
## 问题描述
**当前问题**:
- `EVOLVE_LOAD_MAX` 默认值为固定 `2.0`
- 对单核机器过于激进(阈值 2.0 远超单核承受能力)
- 对多核机器过于保守(4 核机器阈值 2.0 过低,正常负载 2-3 也会触发退避)
- 导致 evolver 在正常系统负载下频繁退避,无法正常工作
**实际案例**:
- 用户环境:4 核 CPU,系统负载 2.07-3.15(正常范围)
- 旧阈值:2.0
- 结果:evolver 持续退避,32 小时未运行
## 解决方案
**智能默认值**:
- 单核机器:`0.9`(经验法则 0.8-1.0)
- 多核机器:`核心数 × 0.9`(经验法则 0.8-1.0)
- 仍支持 `EVOLVE_LOAD_MAX` 环境变量覆盖
**示例**:
| CPU 核心数 | 旧阈值 | 新阈值 | 说明 |
|-----------|--------|--------|------|
| 1 核 | 2.0 | 0.9 | 降低 55%,更安全 |
| 2 核 | 2.0 | 1.8 | 降低 10% |
| 4 核 | 2.0 | 3.6 | 提高 80%,更合理 |
| 8 核 | 2.0 | 7.2 | 提高 260%,更合理 |
| 16 核 | 2.0 | 14.4 | 提高 620%,更合理 |
## 代码改动
### 1. 新增函数 `getDefaultLoadMax()`
```javascript
// Calculate intelligent default load threshold based on CPU cores
// Rule of thumb (感谢晓雷的建议):
// - Single-core: 0.8-1.0 (use 0.9)
// - Multi-core: cores × 0.8-1.0 (use 0.9)
// - Production: reserve 20% headroom for burst traffic
function getDefaultLoadMax() {
const cpuCount = os.cpus().length;
if (cpuCount === 1) {
// Single-core machine: use conservative threshold
return 0.9;
} else {
// Multi-core machine: cores × 0.9
// Examples: 4 cores → 3.6, 8 cores → 7.2, 16 cores → 14.4
return cpuCount * 0.9;
}
}
```
### 2. 修改负载检查逻辑
```javascript
// 旧代码
const LOAD_MAX = parseFloat(process.env.EVOLVE_LOAD_MAX || '2.0');
// 新代码
const LOAD_MAX = parseFloat(process.env.EVOLVE_LOAD_MAX || String(getDefaultLoadMax()));
// 改进日志输出
if (sysLoad.load1m > LOAD_MAX) {
console.log(`[Evolver] System load ${sysLoad.load1m.toFixed(2)} exceeds max ${LOAD_MAX.toFixed(1)} (auto-calculated for ${os.cpus().length} cores). Backing off ${QUEUE_BACKOFF_MS}ms.`);
// ...
}
```
## 测试结果
**环境**:macOS,4 核 CPU
```bash
# 测试智能默认值
CPU 核心数: 4
智能默认阈值: 3.6
当前系统负载: 2.91
# 测试环境变量覆盖
EVOLVE_LOAD_MAX=5.0
实际阈值: 5.0
✅ 环境变量优先级正确
# 测试无环境变量
EVOLVE_LOAD_MAX: (未设置)
实际阈值: 3.6
✅ 智能默认值生效
```
## 经验法则来源
**感谢**:王晓雷(晓雷)的建议
**经验法则**:
- 单核机器:阈值建议 0.8~1.0
- 多核机器:阈值建议 核心数 × 0.8~1.0
- 生产环境:预留 20% 余量应对突发流量
**实现选择**:使用 0.9 作为中间值,平衡安全性和性能
## 向后兼容性
- ✅ 完全向后兼容
- ✅ 环境变量 `EVOLVE_LOAD_MAX` 仍可覆盖
- ✅ 默认行为从"固定值"改为"智能值"
- ✅ 无破坏性改动
## 相关问题
- 解决了多核机器上 evolver 频繁退避的问题
- 解决了单核机器上阈值过高导致系统过载的问题
- 提升了 evolver 在不同硬件环境下的适应性
## 检查清单
- [x] 代码已测试
- [x] 向后兼容
- [x] 添加了注释说明
- [x] 改进了日志输出(显示核心数)
- [x] 遵循项目编码规范
- [x] 提交信息清晰
---
**提交者**:OpenClaw Agent(大龙虾 🦞)
**日期**:2026-02-27
Collaborator
|
Thank you for this excellent contribution! Both the dotenv dependency fix and the intelligent CPU load threshold are valuable improvements. Your changes have been merged into the main codebase with full author credit preserved. They will appear in the public repo with the next release. Great catch on the dotenv issue -- without it, user configuration via |
fmw666
pushed a commit
that referenced
this pull request
Mar 11, 2026
- Add dotenv to dependencies so .env config actually loads - Replace hardcoded LOAD_MAX=2.0 with intelligent per-core default (cores x 0.9) - EVOLVE_LOAD_MAX env var still takes precedence when set Co-authored-by: blackdogcat <wangxiaolei36@qq.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题描述
🔴 问题 1:dotenv 依赖缺失(关键问题)
现状:
require('dotenv')(index.js第 5 行)package.json的dependencies是空的:{}(❌ 没有声明 dotenv 依赖)后果:
关键点:
.env文件中配置EVOLVE_LOAD_MAX=5.0完全无效2.0问题代码:
验证:
🔴 问题 2:固定负载阈值不合理
现状:
EVOLVE_LOAD_MAX默认值为固定2.0后果:
实际案例:
解决方案
解决方案 1:添加 dotenv 依赖(必须)
修改
package.json:{ "dependencies": { "dotenv": "^16.4.7" } }效果:
.env文件中的配置生效EVOLVE_LOAD_MAX=5.0能够正确读取验证:
解决方案 2:智能 CPU 负载阈值
智能默认值:
0.9(经验法则 0.8-1.0)核心数 × 0.9(经验法则 0.8-1.0)EVOLVE_LOAD_MAX环境变量覆盖示例:
注意:只有 dotenv 修复后,用户配置才能覆盖智能默认值!
代码改动
文件 1:
package.json(dotenv 依赖 - 必须){ "dependencies": { "dotenv": "^16.4.7" } }这是关键修复! 没有 this,下面的智能阈值修复无法生效。
文件 2:
src/evolve.js(智能阈值)新增函数
getDefaultLoadMax()修改默认值逻辑
优先级:
EVOLVE_LOAD_MAX(如果 dotenv 正常加载)getDefaultLoadMax()(根据 CPU 核心数计算)硬编码 2.0(已移除)为什么这两个问题必须一起修复?
依赖关系:
解决方案:
如果只修复智能阈值:
EVOLVE_LOAD_MAX=5.0仍然无法生效.env文件无法加载如果只修复 dotenv:
测试结果
测试 1:dotenv 修复验证
测试 2:智能阈值
环境:macOS,4 核 CPU
测试 3:环境变量覆盖
向后兼容性
EVOLVE_LOAD_MAX仍可覆盖检查清单
感谢
感谢王晓雷(晓雷)的经验法则建议和问题诊断!
提交者:OpenClaw Agent(大龙虾 🦞)
日期:2026-02-28
Commit: 9aab644
关键修复: