openclaw-sc 5.38.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +13 -0
- package/INSTALL.md +13 -0
- package/LICENSE +233 -0
- package/README.md +123 -0
- package/SECURITY.md +27 -0
- package/index.js +3479 -0
- package/lib/code-review-shared.js +164 -0
- package/lib/config.js +164 -0
- package/lib/constants.js +438 -0
- package/lib/decomposer.js +896 -0
- package/lib/dialog-recall.js +389 -0
- package/lib/env.js +59 -0
- package/lib/level-rules.js +72 -0
- package/lib/log-manager.js +258 -0
- package/lib/preemption.js +278 -0
- package/lib/priority-calc.js +134 -0
- package/lib/prompt-injection.js +748 -0
- package/lib/route-evidence.js +701 -0
- package/lib/shared-fs.js +244 -0
- package/lib/steward-rules.js +648 -0
- package/lib/task-center.js +602 -0
- package/lib/task-chain.js +713 -0
- package/lib/task-checker.js +317 -0
- package/lib/task-profiles.js +255 -0
- package/lib/tcell.js +411 -0
- package/lib/tool-auto-discover.js +522 -0
- package/lib/tool-enrich-subagent.js +375 -0
- package/lib/tool-handlers.js +178 -0
- package/lib/tool-selector.js +459 -0
- package/openclaw.plugin.json +55 -0
- package/package.json +63 -0
- package/security.js +141 -0
- package/tools/bridge.js +3288 -0
- package/tools/dashboard/tk-dashboard.py +262 -0
- package/tools/hippocampus-multi-search.js +1038 -0
- package/tools/hippocampus-sqlite.js +738 -0
- package/tools/hippocampus-store.js +262 -0
- package/tools/mcp-tools.config.json +653 -0
- package/tools/pipeline-engine.js +667 -0
- package/tools/sidecar/_check_tools.py +34 -0
- package/tools/sidecar/sidecar-server.cjs +1360 -0
- package/tools/sidecar/subagent-runner.cjs +1471 -0
- package/tools/system-tools.js +619 -0
- package/vector/README.md +100 -0
- package/vector/usearch-bridge.js +639 -0
- package/vector/usearch-http.js +74 -0
- package/vector/usearch-serve.js +156 -0
- package/workers/worker.js +1419 -0
package/lib/constants.js
ADDED
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* sc 核心配置常量
|
|
3
|
+
*
|
|
4
|
+
* 所有硬编码配置统一管理,避免散落在各模块中。
|
|
5
|
+
* 供 index.js(CpuWorkerPool) 及其他模块引用。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { cpus, homedir } from "os";
|
|
9
|
+
import { join } from "path";
|
|
10
|
+
|
|
11
|
+
// ====== 核心配置 ======
|
|
12
|
+
// 🧠 设计决策:PHYSICAL_CORES 全开(管理员2026-06-02确认28核全开,不留余量)。
|
|
13
|
+
// 之前上限20,管理员说不用留,全开。Ollama和系统吃剩下的,CPU自己调度。
|
|
14
|
+
export const PHYSICAL_CORES = cpus().length; // 28核全开(管理员确认,不设上限)
|
|
15
|
+
// ⚠️ 废弃:由 RESERVED_CORES=2 替代。CORE_RESERVED_IDLE=8 是旧版概念(保留空闲核心数),
|
|
16
|
+
// CORE_RESERVED_MIN=4 是旧版最小保留值。两者均与新 RESERVED_CORES=2 逻辑冲突且值不一致(8 vs 2 vs 4)。
|
|
17
|
+
// 保留仅用于下游引用兼容(如 getDynamicMaxWorkers 的 catch fallback),新代码一律用 RESERVED_CORES。
|
|
18
|
+
export const CORE_RESERVED_IDLE = 8;
|
|
19
|
+
export const CORE_RESERVED_MIN = 4;
|
|
20
|
+
export const MIN_WORKERS = Math.max(2, Math.min(PHYSICAL_CORES - 1, 16));
|
|
21
|
+
|
|
22
|
+
// ====== MCP Server 端口 ======
|
|
23
|
+
export const MCP_PORT = 18790;
|
|
24
|
+
|
|
25
|
+
// 最大Worker数 = 物理核心 - 保留给系统主脑+Ollama的核心数(至少比MIN大)
|
|
26
|
+
// 2026-06-02 管理员28核全开,保留2核给系统/Ollama即可
|
|
27
|
+
export const RESERVED_CORES = 2;
|
|
28
|
+
export const MAX_WORKERS = Math.max(MIN_WORKERS + 2, PHYSICAL_CORES - RESERVED_CORES);
|
|
29
|
+
|
|
30
|
+
// 🧠 设计决策:SCALE_UP_THRESHOLD=0.7 不是80%也不是60%。70%是S形曲线上的预警触发点——
|
|
31
|
+
// 先于饱和(100%)给扩容留出缓冲时间。高于70%防过早扩容(抖动),低于80%防饱和后才反应。
|
|
32
|
+
// 刚好覆盖_autoScale(5s)的扩容延迟,爬坡到满配时队列不至于打满。
|
|
33
|
+
export const SCALE_UP_THRESHOLD = 0.7; // 队列深度达70%时扩容
|
|
34
|
+
|
|
35
|
+
// 🧠 CPU饱和度阈值:CPU利用率达85%时抑制扩容,防止正反馈振荡(Worker越多→CPU越满→任务越慢→队列越深→继续扩容→死循环)
|
|
36
|
+
// 取自load prediction经验值,与 os.cpus() 的 CPU 忙闲比配合使用。
|
|
37
|
+
// 配合 _autoScale() 中的饱和度检测一起生效。
|
|
38
|
+
export const CPU_SATURATION_THRESHOLD = 0.85;
|
|
39
|
+
// 🧠 设计决策:HEARTBEAT_MS=15000(15s)。刚好覆盖_autoScale(5s)3个完整周期,
|
|
40
|
+
// 能捕捉到突发负载变化又不浪费。比30s短能更快发现Worker失联,比5s长不刷屏。
|
|
41
|
+
export const HEARTBEAT_MS = 15000;
|
|
42
|
+
// 🧠 ⚠️ 已废弃:TASK_TIMEOUT_MS 已被 calcTimeout(基于 TASK_TIMEOUT_MAP 的动态计算)替代。
|
|
43
|
+
// 保留仅防外部引用意外断裂。只读不改,不需要更新。
|
|
44
|
+
export const TASK_TIMEOUT_MS = 60000;
|
|
45
|
+
// 🧠 设计决策:IDLE_TERMINATE_MS=60000(60s空闲终止)。scale down判据——用户要求删除睡眠学习后改用此值直接判断。
|
|
46
|
+
// 60s足够扛过短暂空闲低谷(不频繁启停),又不至于占着资源不肯放。
|
|
47
|
+
export const IDLE_TERMINATE_MS = 60000;
|
|
48
|
+
|
|
49
|
+
// ====== 缓存配置 ======
|
|
50
|
+
export const STATS_CACHE_TTL = 10000;
|
|
51
|
+
export const MEM_CACHE_TTL = 5000;
|
|
52
|
+
|
|
53
|
+
// ====== 缓存禁用常量 ======
|
|
54
|
+
// 🧠 设计决策:CACHE_DISABLED=0。TTL设为0=禁用缓存。这是个哨兵常量——读到它的地方
|
|
55
|
+
// 调用方就知道缓存是有意关闭的,不是配置忘填了。修改前先确认是否有意禁缓存。
|
|
56
|
+
export const CACHE_DISABLED = 0;
|
|
57
|
+
|
|
58
|
+
// ====== 路由缓存 ======
|
|
59
|
+
export const ROUTE_CACHE_TTL_MS = 300000;
|
|
60
|
+
export const ROUTE_CACHE_MAX = 200;
|
|
61
|
+
|
|
62
|
+
// ====== 文件编辑器 ======
|
|
63
|
+
export const FILE_EDIT_LOCK_TIMEOUT = 300000;
|
|
64
|
+
|
|
65
|
+
// ====== 紧急模式 ======
|
|
66
|
+
// 🧠 设计决策:RECOVERY_COOLDOWN_MS=60000(60s紧急模式防抖)。防止频繁进出紧急模式导致震荡——
|
|
67
|
+
// 灵感来自主板电源管理防抖(debounce)概念。1min内不再次触发紧急模式,确保恢复稳定。
|
|
68
|
+
export const RECOVERY_COOLDOWN_MS = 60000;
|
|
69
|
+
// 🧠 设计决策:EMERGENCY_MODE_SUPPRESSION_MS=30000(30s紧急模式抑制时间)。
|
|
70
|
+
// 这不是bug,也不是忘记配。30s刚好足够一次_autoScale(5s)完成 6 个周期
|
|
71
|
+
// 的扩容评估,同时防止频繁进出紧急模式导致抖动。
|
|
72
|
+
// 配合 RECOVERY_COOLDOWN_MS=60000(整体防抖60s),两层防抖:
|
|
73
|
+
// 第一层30 s:抑制期间不重复触发紧急模式
|
|
74
|
+
// 第二层60 s:退出紧急模式后 60 s内不再次进入
|
|
75
|
+
// 灵感来自电源管理电路中的 debounce 概念——短时波动被滤掉,持续压力才触发保护。
|
|
76
|
+
export const EMERGENCY_MODE_SUPPRESSION_MS = 30000;
|
|
77
|
+
|
|
78
|
+
// ====== 速率限制 ======
|
|
79
|
+
// 🧠 设计决策:RATE_LIMIT_PER_SEC=150 匹配26Worker全速处理。28核全开+26Worker,
|
|
80
|
+
// 150/s接近每s每Worker处理6个工具调用,足够覆盖批量并发。超限走10s封禁。
|
|
81
|
+
export const RATE_LIMIT_PER_SEC = 150; // 每s最多处理150次工具调用(28核全开26Worker)
|
|
82
|
+
export const RATE_BLOCK_DURATION_MS = 10000; // 速率超限封禁时长10s,管理员从30s调到10s,配合150/s用
|
|
83
|
+
|
|
84
|
+
// ====== Spawn 限制 ======
|
|
85
|
+
// 🧠 设计决策:MAX_ACTIVE_SPAWNS=50。28核全开26Worker,50是安全带=Worker数近2倍。
|
|
86
|
+
// 给子agent足够并行空间。MAX_SPAWN_PER_WINDOW=10 是速率限制,60s窗口内最多10 spawn。
|
|
87
|
+
// 管理员从5调到10以支持10路并行。
|
|
88
|
+
export const MAX_ACTIVE_SPAWNS = 16; // 子agent最大同时活跃数16=26Worker+余量,管理员从20调到16
|
|
89
|
+
export const SPAWN_HISTORY_WINDOW_MS = 60000; // spawn速率统计窗口(60s)
|
|
90
|
+
export const MAX_SPAWN_PER_WINDOW = 28; // 子agent 60s窗口内最多28个(管理员从5调到10,10路并行不受限)
|
|
91
|
+
|
|
92
|
+
// ====== 路由守卫 ======
|
|
93
|
+
export const ROUTE_GUARD_VIOLATION_DECAY_MS = 60000;
|
|
94
|
+
|
|
95
|
+
// ====== 任务超时配置 ======
|
|
96
|
+
export const TASK_TIMEOUT_MAP = {
|
|
97
|
+
core_search: { warn: 120, kill: 300, label: '搜索' },
|
|
98
|
+
cpu_semanticSearch: { warn: 120, kill: 300, label: '语义搜索' },
|
|
99
|
+
cpu_scan: { warn: 120, kill: 300, label: '扫描' },
|
|
100
|
+
cpu_systemRun: { warn: 200, kill: 500, label: '系统运维' },
|
|
101
|
+
core_processLog:{ warn: 60, kill: 120, label: '日志分析' },
|
|
102
|
+
cpu_batch: { warn: 60, kill: 120, label: '批量' },
|
|
103
|
+
cpu_diff: { warn: 60, kill: 120, label: '差异分析' },
|
|
104
|
+
cpu_diagnose: { warn: 60, kill: 120, label: '诊断' },
|
|
105
|
+
cpu_codeEdit: { warn: 60, kill: 120, label: '代码编辑' },
|
|
106
|
+
cpu_codeReview:{ warn: 60, kill: 120, label: '代码审查' },
|
|
107
|
+
cpu_bugFix: { warn: 60, kill: 120, label: 'Bug修复' },
|
|
108
|
+
cpu_orchestrate:{warn: 200, kill: 500, label: '编排' },
|
|
109
|
+
cpu_research: { warn: 200, kill: 500, label: '调研' },
|
|
110
|
+
core_backup: { warn: 60, kill: 300, label: '工作空间同步' },
|
|
111
|
+
core_resolveModel:{warn: 60, kill: 120, label: '模型解析' },
|
|
112
|
+
core_routeTask: { warn: 60, kill: 120, label: '路由决策' },
|
|
113
|
+
core_directRun: { warn: 30, kill: 60, label: '快速执行' },
|
|
114
|
+
cpu_monitorSubagents: { warn: 30, kill: 60, label: '子agent监控' },
|
|
115
|
+
cpu_routeEvidence: { warn: 30, kill: 60, label: '路由证据' },
|
|
116
|
+
core_emergencyStop: { warn: 10, kill: 30, label: '紧急停止' },
|
|
117
|
+
core_stagePipeline: { warn: 200, kill: 500, label: '多阶段流水线' },
|
|
118
|
+
cpu_resumeTask: { warn: 10, kill: 30, label: 'Checkpoint恢复' },
|
|
119
|
+
core_createTask: { warn: 30, kill: 60, label: '创建任务' },
|
|
120
|
+
core_reportResult: { warn: 30, kill: 60, label: '报告结果' },
|
|
121
|
+
core_collectResults: { warn: 60, kill: 120, label: '收集结果' },
|
|
122
|
+
core_codeEditor: { warn: 60, kill: 180, label: '领域代码' },
|
|
123
|
+
core_memorySearch: { warn: 60, kill: 180, label: '领域推理' },
|
|
124
|
+
core_webSearch: { warn: 60, kill: 180, label: '领域搜索' },
|
|
125
|
+
core_toolRouter: { warn: 30, kill: 60, label: '领域工具' },
|
|
126
|
+
core_localScheduler: { warn: 200, kill: 500, label: '领域编排' },
|
|
127
|
+
cpu_evolution: { warn: 30, kill: 60, label: '演化' },
|
|
128
|
+
cpu_cerebellumStatus: { warn: 10, kill: 30, label: '小脑状态' },
|
|
129
|
+
cpu_chainDetect: { warn: 30, kill: 60, label: '链路检测' },
|
|
130
|
+
cpu_associativeCache: { warn: 30, kill: 60, label: 'associative cache' },
|
|
131
|
+
cpu_internalization: { warn: 60, kill: 180, label: '内化学习' },
|
|
132
|
+
core_searchEx: { warn: 60, kill: 120, label: '统一搜索' },
|
|
133
|
+
core_batchVision: { warn: 120, kill: 300, label: '图片批量' },
|
|
134
|
+
default: { warn: 120, kill: 300, label: '通用' },
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
export const GRACE_PER_EXTRA_60S = 30000; // 任务超时后每多等1min,额外增加30s宽限期
|
|
138
|
+
export const MAX_GRACE_MULTIPLIER = 2; // 宽限时间最多为基础超时的2倍
|
|
139
|
+
|
|
140
|
+
// ====== Session 清理 ======
|
|
141
|
+
export const SESSION_KEEP_RECENT = 20;
|
|
142
|
+
export const SESSION_DIR = join(homedir(), '.openclaw', 'agents', 'main', 'sessions');
|
|
143
|
+
|
|
144
|
+
// ====== task-states 清理(孤儿任务元数据) ======
|
|
145
|
+
export const TASK_STATES_DIR = join(homedir(), '.openclaw', 'workspace', 'memory', 'task-states');
|
|
146
|
+
|
|
147
|
+
// ====== 共享文件 ======
|
|
148
|
+
export const SHARED_DIR = join(homedir(), '.openclaw', 'workspace', 'memory', 'shared');
|
|
149
|
+
|
|
150
|
+
// ====== 任务执行中心(子agent task-center) ======
|
|
151
|
+
export const TASK_CENTER_DIR = join(homedir(), '.openclaw', 'workspace', 'memory', 'shared', 'tasks');
|
|
152
|
+
export const DEFAULT_BATCH_SIZE = 12;
|
|
153
|
+
export const COLLECT_POLL_INTERVAL = 2000;
|
|
154
|
+
|
|
155
|
+
// ====== 路由审计 ======
|
|
156
|
+
export const ROUTE_AUDIT_DIR = join(homedir(), '.openclaw', 'workspace', 'memory', 'shared', 'route-audit');
|
|
157
|
+
|
|
158
|
+
// ====== Worker 模型warmup ======
|
|
159
|
+
export const WARMED_MODELS_MAX = 50;
|
|
160
|
+
|
|
161
|
+
// ====== steward rules engine ======
|
|
162
|
+
export const STEWARD_MODE = 'strict';
|
|
163
|
+
export const DEFAULT_CONFIG_PATH = join(homedir(), '.openclaw', 'steward-config.json');
|
|
164
|
+
|
|
165
|
+
// ====== 任务分类常量(可判定终止保证) ======
|
|
166
|
+
export const TASK_CATEGORY = {
|
|
167
|
+
FINITE: 'finite', // 有限型:输入确定后必然终止(cpu_search/cpu_diff/cpu_batch 等)
|
|
168
|
+
CONDITIONAL: 'conditional', // 条件型:满足条件即终止(cpu_resolveModel/cpu_codeEdit/cpu_bugFix)
|
|
169
|
+
EXPLORATORY: 'exploratory', // 探索型:无确定终止条件,可能持续探索新路径(cpu_research/cpu_orchestrate)
|
|
170
|
+
INTERACTIVE: 'interactive', // 交互型:需要用户交互才能终止
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
// 每个sc工具对应的任务分类
|
|
174
|
+
// 用于:探索型任务自动写partialResult checkpoint,超时不丢中间结果
|
|
175
|
+
// 以下工具故意不在 TASK_CATEGORY_MAP 中:
|
|
176
|
+
// core_stats、cpu_monitorSubagents、core_backup、cpu_systemRun、
|
|
177
|
+
// core_toolDiscover、cpu_enrichSubagentTask、cpu_resolveModel、
|
|
178
|
+
// cpu_compressTask、cpu_resumeTask、cpu_routeEvidence、core_routeTask、
|
|
179
|
+
// cpu_internalization、cpu_evolution、cpu_cerebellumStatus
|
|
180
|
+
// 故意不加原因:这些工具都不是探索型(EXPLORATORY)任务,不需要写 checkpoint。
|
|
181
|
+
// 非探索型任务分不分类效果都一样——都不写 checkpoint。
|
|
182
|
+
// 不加可以减少新开发者加工具时改常量的工作量。
|
|
183
|
+
export const TASK_CATEGORY_MAP = {
|
|
184
|
+
// ====== 有限型(FINITE) ======
|
|
185
|
+
cpu_search: TASK_CATEGORY.FINITE,
|
|
186
|
+
cpu_diff: TASK_CATEGORY.FINITE,
|
|
187
|
+
cpu_batch: TASK_CATEGORY.FINITE,
|
|
188
|
+
cpu_scan: TASK_CATEGORY.FINITE,
|
|
189
|
+
cpu_diagnose: TASK_CATEGORY.FINITE,
|
|
190
|
+
core_processLog: TASK_CATEGORY.FINITE,
|
|
191
|
+
cpu_codeReview: TASK_CATEGORY.FINITE,
|
|
192
|
+
cpu_semanticSearch: TASK_CATEGORY.FINITE,
|
|
193
|
+
core_batchVision: TASK_CATEGORY.FINITE,
|
|
194
|
+
cpu_dialogRecall: TASK_CATEGORY.FINITE,
|
|
195
|
+
cpu_monitorSubagents: TASK_CATEGORY.FINITE,
|
|
196
|
+
core_emergencyStop: TASK_CATEGORY.FINITE,
|
|
197
|
+
core_backup: TASK_CATEGORY.FINITE,
|
|
198
|
+
core_directRun: TASK_CATEGORY.FINITE,
|
|
199
|
+
cpu_compressTask: TASK_CATEGORY.FINITE,
|
|
200
|
+
cpu_routeEvidence: TASK_CATEGORY.FINITE,
|
|
201
|
+
|
|
202
|
+
// ====== 条件型(CONDITIONAL) ======
|
|
203
|
+
cpu_resolveModel: TASK_CATEGORY.CONDITIONAL,
|
|
204
|
+
cpu_codeEdit: TASK_CATEGORY.CONDITIONAL,
|
|
205
|
+
cpu_bugFix: TASK_CATEGORY.CONDITIONAL,
|
|
206
|
+
core_routeTask: TASK_CATEGORY.CONDITIONAL,
|
|
207
|
+
core_stagePipeline: TASK_CATEGORY.CONDITIONAL,
|
|
208
|
+
|
|
209
|
+
// ====== 探索型(EXPLORATORY) ======
|
|
210
|
+
cpu_research: TASK_CATEGORY.EXPLORATORY,
|
|
211
|
+
cpu_orchestrate: TASK_CATEGORY.EXPLORATORY,
|
|
212
|
+
|
|
213
|
+
// ====== 交互型(INTERACTIVE) ======
|
|
214
|
+
core_mainBrainPause: TASK_CATEGORY.INTERACTIVE,
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
// ====== 探索型 checkpoint 写入间隔(毫秒) ======
|
|
218
|
+
export const EXPLORATORY_CHECKPOINT_INTERVAL_MS = 30000;
|
|
219
|
+
|
|
220
|
+
// ====== 受限工具列表 ======
|
|
221
|
+
export const RESTRICTED_TOOLS = new Set([
|
|
222
|
+
'exec',
|
|
223
|
+
'web_search',
|
|
224
|
+
'tavily_search',
|
|
225
|
+
'tavily_extract',
|
|
226
|
+
'web_fetch',
|
|
227
|
+
'browser',
|
|
228
|
+
'read',
|
|
229
|
+
'write',
|
|
230
|
+
'edit',
|
|
231
|
+
]);
|
|
232
|
+
|
|
233
|
+
export const TOOL_ROUTE_MAP = {
|
|
234
|
+
exec: { reason: 'exec 应由子agent执行', suggest: 'sessions_spawn 派子agent 执行 exec' },
|
|
235
|
+
web_search: { reason: '搜索应由子agent执行', suggest: 'sessions_spawn 派子agent 执行 web_search' },
|
|
236
|
+
tavily_search: { reason: '搜索应由子agent执行', suggest: 'sessions_spawn 派子agent 执行 tavily_search' },
|
|
237
|
+
tavily_extract: { reason: '提取应由子agent执行', suggest: 'sessions_spawn 派子agent 执行 tavily_extract' },
|
|
238
|
+
web_fetch: { reason: '抓取应由子agent执行', suggest: 'sessions_spawn 派子agent 执行 web_fetch' },
|
|
239
|
+
browser: { reason: '浏览器操作应由子agent执行', suggest: 'sessions_spawn 派子agent 执行 browser' },
|
|
240
|
+
read: { reason: 'read 应由子agent执行', suggest: 'sessions_spawn 派子agent 执行 read' },
|
|
241
|
+
write: { reason: 'write 应由子agent执行', suggest: 'sessions_spawn 派子agent 执行 write' },
|
|
242
|
+
edit: { reason: 'edit 应由子agent执行', suggest: 'sessions_spawn 派子agent 执行 edit' },
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
// ====== Worker 寿命上限(Worker寿命管理) ======
|
|
246
|
+
// 🧠 设计决策:MAX_WORKER_AGE=6h(6小时),匹配一个工作会话的典型时长。
|
|
247
|
+
// 小于6小时会频繁滚动重启(浪费创建开销),大于6小时则内存泄漏风险累积增大。
|
|
248
|
+
export const MAX_WORKER_AGE = 6 * 3600 * 1000; // 6小时
|
|
249
|
+
// ====== Worker 最大任务数(完成指定任务后自动下线置换) ======
|
|
250
|
+
// 🧠 设计决策:MAX_WORKER_TASKS=2000。配合6小时寿命使用,大约每小时处理330个任务。
|
|
251
|
+
// 防单个Worker累积过多样本导致worker.js中内存增长。2000任务≈小型批处理全天密集调度。
|
|
252
|
+
export const MAX_WORKER_TASKS = 2000;
|
|
253
|
+
|
|
254
|
+
// ====== Rolling Restart ======
|
|
255
|
+
export const ROLLING_RESTART_BATCH_RATIO = 1 / 3; // 每次最多置换1/3 Worker
|
|
256
|
+
export const ROLLING_RESTART_CHECK_INTERVAL_MS = 60000; // 每min检查一次
|
|
257
|
+
|
|
258
|
+
// ====== 替换日志路径 ======
|
|
259
|
+
export const WORKER_REPLACEMENT_LOG = join(homedir(), '.openclaw', 'workspace', 'memory', 'logs', 'worker-replacement.log');
|
|
260
|
+
|
|
261
|
+
export const CORE_TOOLS = [
|
|
262
|
+
'core_memorySearch',
|
|
263
|
+
'core_webSearch',
|
|
264
|
+
'core_codeEditor',
|
|
265
|
+
'core_batchVision',
|
|
266
|
+
'core_fileManager',
|
|
267
|
+
'core_spawnWorker',
|
|
268
|
+
];
|
|
269
|
+
// 🧠 设计决策:cpu_flowRoute 是内部路由函数,不注册为公开工具。不在白名单,不注册,是专门的设计决定。
|
|
270
|
+
|
|
271
|
+
// ====== 高优任务抢占 ======
|
|
272
|
+
// 🧠 设计决策:PREEMPT_QUEUE_THRESHOLD=26。队列>26才触发preempt评估——
|
|
273
|
+
// 匹配28核全开Worker总数,当所有Worker忙碌且队列积压时触发。
|
|
274
|
+
// 过小(如5)会频繁preempt导致任务抖动,过大会让高优任务等待太久。
|
|
275
|
+
export const PREEMPT_QUEUE_THRESHOLD = 26; // 队列 > 26 才触发preempt评估(匹配28核全开Worker总数)
|
|
276
|
+
// 🧠 设计决策:PREEMPT_TASK_TIMEOUT_MS=300000(5min)。被抢占任务5min不恢复则超时失效。
|
|
277
|
+
// 管理员从10min调到5min(见行内注释)。匹配Worker最长任务配置:60s任务超时x5倍缓冲,够Worker完成清理再重新调度。
|
|
278
|
+
export const PREEMPT_TASK_TIMEOUT_MS = 300000; // 被抢任务5min不恢复则超时失效(管理员从10min调到5min)
|
|
279
|
+
export const PREEMPT_MAX_AGE_MS = 600000; // 抢占状态文件最大存活时间(10min)
|
|
280
|
+
export const PREEMPT_CHECK_INTERVAL_MS = 3000; // 3s检查一次是否需要抢占
|
|
281
|
+
|
|
282
|
+
// ====== 🔧 Worker 分化 ======
|
|
283
|
+
// 🧠 设计决策:ROLE_CONFIG各角色min/max 中 stemcell idleTerminateMs最大90000,因它是兜底角色,
|
|
284
|
+
// 任一角色(scanner/compute/router)全崩时需stemcell立即填补,不能过早终止。
|
|
285
|
+
// compute角色min=6占多数核心,因计算密集型任务是主负载类型。
|
|
286
|
+
export const ROLE_CONFIG = {
|
|
287
|
+
scanner: { min: 2, max: 4, idleTerminateMs: 60000 },
|
|
288
|
+
compute: { min: 6, max: 14, idleTerminateMs: 60000 },
|
|
289
|
+
router: { min: 2, max: 4, idleTerminateMs: 45000 },
|
|
290
|
+
stemcell: { min: 2, max: 4, idleTerminateMs: 90000 },
|
|
291
|
+
// 🆕 2026-06-05 新增角色:转岗制,所有空闲Worker都能互相借调
|
|
292
|
+
planner: { min: 1, max: 3, idleTerminateMs: 60000 }, // 🧩 任务分解师:复杂任务拆DAG子任务
|
|
293
|
+
inspector:{ min: 1, max: 3, idleTerminateMs: 60000 }, // 🔍 质量巡检员:代码审查/安全检查/输出质量
|
|
294
|
+
research: { min: 1, max: 3, idleTerminateMs: 60000 }, // 🕵️ 调研专员:多源信息检索与交叉验证
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
// 任务类型 -> 角色映射
|
|
298
|
+
export const TASK_TYPE_ROLE_MAP = {
|
|
299
|
+
// scanner
|
|
300
|
+
'search': 'scanner',
|
|
301
|
+
'scan': 'scanner',
|
|
302
|
+
'dialog-search': 'scanner',
|
|
303
|
+
'route-quick': 'scanner',
|
|
304
|
+
// compute
|
|
305
|
+
'process-log': 'compute',
|
|
306
|
+
'embedding': 'compute',
|
|
307
|
+
'image-process': 'compute',
|
|
308
|
+
'semantic-search': 'compute',
|
|
309
|
+
// router
|
|
310
|
+
'route-system': 'router',
|
|
311
|
+
'route-intent': 'router',
|
|
312
|
+
'route-capability': 'router',
|
|
313
|
+
'route-strategy': 'router',
|
|
314
|
+
// 🆕 planner - 任务分解师
|
|
315
|
+
'decompose': 'planner',
|
|
316
|
+
'route-decompose': 'planner',
|
|
317
|
+
// 🆕 inspector - 质量巡检员
|
|
318
|
+
'code-review': 'inspector',
|
|
319
|
+
'quality-check': 'inspector',
|
|
320
|
+
// 🆕 research - 调研专员
|
|
321
|
+
'deep-research': 'research',
|
|
322
|
+
'cross-reference': 'research',
|
|
323
|
+
// misc → stemcell 兜底
|
|
324
|
+
'resolve-model': 'stemcell',
|
|
325
|
+
'ping': 'stemcell',
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
// 启动时的角色数量分配
|
|
329
|
+
export function getInitialRoleDistribution() {
|
|
330
|
+
const roleDist = { scanner: 0, compute: 0, router: 0, stemcell: 0 };
|
|
331
|
+
// 按 min 值分配
|
|
332
|
+
for (const [role, cfg] of Object.entries(ROLE_CONFIG)) {
|
|
333
|
+
roleDist[role] = cfg.min;
|
|
334
|
+
}
|
|
335
|
+
return roleDist;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* 根据当前队列情况自适应调整角色数量
|
|
340
|
+
* @param {object} stats - pool.getStats()
|
|
341
|
+
* @param {object} roleCounts - 当前各角色Worker数
|
|
342
|
+
* @returns {object} 目标角色分配 { scanner, compute, router, stemcell }
|
|
343
|
+
*/
|
|
344
|
+
export function getAdaptiveRoleDistribution(stats, roleCounts) {
|
|
345
|
+
const queueDepth = stats?.queueDepth || 0;
|
|
346
|
+
const distribution = Object.fromEntries(
|
|
347
|
+
Object.entries(ROLE_CONFIG).map(([role, cfg]) => [role, cfg.min])
|
|
348
|
+
);
|
|
349
|
+
|
|
350
|
+
if (queueDepth > 20) {
|
|
351
|
+
// 深度排队 -> 优先扩 scanner 和 compute
|
|
352
|
+
distribution.scanner = Math.min(ROLE_CONFIG.scanner.max, (roleCounts.scanner || 0) + 2);
|
|
353
|
+
distribution.compute = Math.min(ROLE_CONFIG.compute.max, (roleCounts.compute || 0) + 2);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
if (queueDepth > 50) {
|
|
357
|
+
// 极高压力 -> 拉满 scanner 到上限
|
|
358
|
+
distribution.scanner = ROLE_CONFIG.scanner.max;
|
|
359
|
+
distribution.compute = Math.min(ROLE_CONFIG.compute.max, ROLE_CONFIG.compute.min + 10);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// stemcell 始终保留至少 2 个做兜底
|
|
363
|
+
distribution.stemcell = Math.max(ROLE_CONFIG.stemcell.min, distribution.stemcell || 0);
|
|
364
|
+
|
|
365
|
+
return distribution;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// ====== 🌊 本能式分流策略(flow-router.js)配置常量 ======
|
|
369
|
+
// 🧠 设计决策:FLOW_ROUTER_CONFIG 各项参数——sigmoidSteepness=2.0(统一折中值,详见fix-comment-bugs-2026-06-02),
|
|
370
|
+
// feedforwardBaseMs=2000(2s前馈基准匹配avg工具调用耗时),habituationMaxEntries=200(防记忆膨胀)。
|
|
371
|
+
// 整套配置是本能式分流策略的超参数,调优来自用户实测。
|
|
372
|
+
export const FLOW_ROUTER_CONFIG = {
|
|
373
|
+
sigmoidSteepness: 2.0, // 🧠 2.0 折中值——与 flow-router.js DEFAULT_CONFIG 对齐(原 constants.js=1.5, flow-router.js=2.5)。
|
|
374
|
+
// 1.5 偏软化(快慢路径区分模糊),2.5 偏果断(二值化)。
|
|
375
|
+
// 2.0 刚好是metabolic rate系统设计值,既保留S 形曲线的渐进过渡,又能清晰区分快/慢路径。
|
|
376
|
+
// 统一配置源见 fix-comment-bugs-2026-06-02。
|
|
377
|
+
sigmoidMidpoint: 0.45,
|
|
378
|
+
sigmoidDecayRate: 0.92,
|
|
379
|
+
feedforwardBaseMs: 2000,
|
|
380
|
+
adaptThreshold: 1.5,
|
|
381
|
+
adaptMinIntervalMs: 5000,
|
|
382
|
+
habituationMaxEntries: 200,
|
|
383
|
+
habituationWeightDecay: 0.95,
|
|
384
|
+
habituationMinWeight: 0.1,
|
|
385
|
+
raceTimeoutMs: 15000,
|
|
386
|
+
raceMinComplexity: 3,
|
|
387
|
+
flowWorkerIdleBoost: 2.0,
|
|
388
|
+
flowWorkerBusyPenalty: 0.3,
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
// ====== 负载模式预测(traffic-patterns.js)配置常量 ======
|
|
392
|
+
// 🧠 设计决策:TRAFFIC_WINDOW_MIN=15(15min窗口),平衡粒度与平滑度。
|
|
393
|
+
// 太细(5min)噪声大,太粗(60min)无法捕捉小时级变化。7天滚动刚好覆盖一个完整工作周。
|
|
394
|
+
// TRAFFIC_WEIGHT_DECAY_RATE=0.85确保旧模式每窗口衰减15%,TRAFFIC_WEIGHT_BOOST_RATE=1.15(管理员从1.05调到1.15,更快收敛)。
|
|
395
|
+
export const TRAFFIC_PATTERNS_DIR = join(homedir(), '.openclaw', 'workspace', 'memory', 'traffic-patterns');
|
|
396
|
+
export const TRAFFIC_WINDOW_MIN = 15;
|
|
397
|
+
export const TRAFFIC_ROLLING_DAYS = 7;
|
|
398
|
+
export const TRAFFIC_PREDICTION_LEAD_MIN = 5;
|
|
399
|
+
export const TRAFFIC_MAX_SNAPSHOTS_PER_DAY = 96; // 24h / 15min
|
|
400
|
+
export const TRAFFIC_WEIGHT_DECAY_RATE = 0.85; // 旧模式权重衰减率:每个15min窗口衰减到0.85
|
|
401
|
+
export const TRAFFIC_WEIGHT_BOOST_RATE = 1.15; // 最新流量模式权重提升系数(管理员从1.05调到1.15,更快收敛)
|
|
402
|
+
|
|
403
|
+
// ====== 有限理性搜索阈值(cpu_search 早停) ======
|
|
404
|
+
// 搜索达到足够匹配数后不再继续搜剩余文件,节约I/O和CPU
|
|
405
|
+
export const SEARCH_MIN_RESULTS = 20;
|
|
406
|
+
|
|
407
|
+
// ====== 🧬 metabolic rate精细调节(连续插值0.2-1.0) ======
|
|
408
|
+
export const METABOLIC_RATE_MIN = 0.2;
|
|
409
|
+
export const METABOLIC_RATE_MAX = 1.0;
|
|
410
|
+
export const METABOLIC_RATE_DEFAULT = 0.5;
|
|
411
|
+
export const METABOLIC_RATE_ADJUST_STEP = 0.05;
|
|
412
|
+
export const METABOLIC_RATE_INTERVAL_MS = 15000;
|
|
413
|
+
|
|
414
|
+
// 🧠 设计决策:METABOLIC_WEIGHTS 包含4个参数(memory/queue/heartbeat/userActive)。用户活跃权重0.15从其他参数均摊。
|
|
415
|
+
// 用户活跃时降metabolic rate(让出CPU给聊天),不活跃时升metabolic rate(更激进后台处理)。
|
|
416
|
+
export const METABOLIC_WEIGHTS = {
|
|
417
|
+
memory: 0.40,
|
|
418
|
+
queue: 0.28,
|
|
419
|
+
heartbeat: 0.17,
|
|
420
|
+
userActive: 0.15,
|
|
421
|
+
};
|
|
422
|
+
|
|
423
|
+
// 🧠 设计决策:METABOLIC_SMOOTH_POINTS 定义插值锚点。
|
|
424
|
+
// 任意metabolic rate值取相邻两点线性插值,不再离散4档。
|
|
425
|
+
// 0.2=休息 0.5=均衡 0.8=专注 1.0=战斗
|
|
426
|
+
export const METABOLIC_SMOOTH_POINTS = [
|
|
427
|
+
{ rate: 0.2, minWorkers: 4, maxWorkers: 6, cacheMult: 3.0, rateMult: 0.5, cacheDisabled: false },
|
|
428
|
+
{ rate: 0.35, minWorkers: 4, maxWorkers: 8, cacheMult: 2.0, rateMult: 0.75, cacheDisabled: false },
|
|
429
|
+
{ rate: 0.5, minWorkers: 4, maxWorkers: 28, cacheMult: 1.0, rateMult: 1.0, cacheDisabled: false },
|
|
430
|
+
{ rate: 0.8, minWorkers: 8, maxWorkers: 26, cacheMult: 0.5, rateMult: 1.5, cacheDisabled: false },
|
|
431
|
+
{ rate: 1.0, minWorkers: 10, maxWorkers: 28, cacheMult: 0.3, rateMult: 2.0, cacheDisabled: true },
|
|
432
|
+
];
|
|
433
|
+
|
|
434
|
+
// 用户活跃检测超时(连续20min无消息判定为离线)
|
|
435
|
+
export const USER_ACTIVE_TIMEOUT_MS = 20 * 60 * 1000;
|
|
436
|
+
|
|
437
|
+
// 对话日记路径,用于判断用户是否在线
|
|
438
|
+
export const DIALOG_DIR = join(homedir(), '.openclaw', 'workspace', 'memory', 'dialog');
|