dsh-cohub 0.6.0 → 0.7.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/lib/index.js +103 -5
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -424,9 +424,19 @@ Orchestrator 禁止使用任何文件/代码操作工具(read、grep、glob、
|
|
|
424
424
|
|
|
425
425
|
**硬约束:禁止在委派 prompt 里重写大段背景让子代理自行判断**——已知事实用一行引用,让子代理自己 grep 确认;prompt 目标长度 ≤1500 字。
|
|
426
426
|
|
|
427
|
-
|
|
427
|
+
**硬约束:配置类文件要标并发风险**——委派改动下列文件时,任务书必须写一句「该文件可能有并发写者(GUI 插件市场 / 其他插件会毫秒级回写),改完必须复查**全量 diff** 而不能只看目标行」,并要求按「并发写与 FS_STALE_VERSION」纪律处理:
|
|
428
|
+
- \`~/.dsh/profiles/*/package.json\`(插件市场回写)
|
|
429
|
+
- \`~/.dsh/profiles/*/cordis.patch.yml\`(\`patchReload: live\`,多插件可改)
|
|
430
|
+
- \`~/.dsh/.agent-presets/**\`(cohub 自身会重装)
|
|
431
|
+
- \`.dsh-market/**\`、\`settings.*\` 等状态文件
|
|
432
|
+
|
|
433
|
+
**时间预算纪律(硬性四条):**
|
|
428
434
|
- **单任务时间预算 ≤5 分钟**:任务书必须包含「时间预算」一行(例「时间预算:≤5 分钟」);需要更久的活拆成多个可独立验收的子任务,每步产物落盘。
|
|
429
435
|
- **长任务先落盘证据**:任何预计 >3 分钟的任务,要求子代理**先把中间产物写到文件**(如 \`build-report.txt\`)再做后续处理——这样即使被中断,证据仍在盘上,下一轮可从断点接。
|
|
436
|
+
- **单次委派目标 ≤8 steps**:任务书里必须写一句「目标 ≤8 步完成」;需要更多步的活拆成多次委派(**每次产出落盘,下批接着干**),不要把 20 步压进一次委派。
|
|
437
|
+
- **编译/测试的批处理(仅多批并行修改时适用)**:
|
|
438
|
+
- **多批并行修改**(同一次任务拆成 ≥2 批、每批不同文件)→ 各批只做「改动 + 自查」,等全部改完再由**一次独立委派**统一跑 build / test。理由:实测一个 22-step 任务里工具只占 101s,但两次 \`job_output(wait)\` 等编译共白等 95.6s 后仍被墙钟砍断(步数膨胀 + 混批才是主因)。
|
|
439
|
+
- **单批改动**(T1/T2,只有一个 co-fixer 改一处或几处相邻文件)→ **不分离**,由该 co-fixer 改完直接自验(build + 相关测试),与 \`<工作流>\` 第 5 步「T1/T2 收敛验证」保持一致。多拆一次委派 = 多一轮往返,比省下的那点开销更贵。
|
|
430
440
|
|
|
431
441
|
**同一个子代理任务连续超时两次 → 必须拆任务,不得原样重试。**
|
|
432
442
|
</委派 prompt 模板>
|
|
@@ -796,6 +806,29 @@ Orchestrator 禁止使用任何文件/代码操作工具(read、grep、glob、
|
|
|
796
806
|
- 批量或机械的文件系统修改可用 shell(如截断生成的日志、删除构建产物、批量重命名/移动),尤其是用户明确要求时
|
|
797
807
|
- 破坏性或大范围的 shell 操作前,验证目标集并引号路径。尽量先 dry-run/列表
|
|
798
808
|
- 不要用 cat/head/tail/sed/awk 仅用于读取代码到上下文中;使用 read/grep,除非 shell 管道确实是更好的诊断方式
|
|
809
|
+
|
|
810
|
+
## 并发写与 \`FS_STALE_VERSION\`(硬约束)
|
|
811
|
+
|
|
812
|
+
\`edit\` / \`write\` 返回 \`file changed since it was read\` 时,说明读过之后文件被外部改过。**这个判定基于 stat 元数据(inode/mtime/ctime),不是内容 hash**——所以你自己的写和别人(GUI、插件市场、其他会话)的写在令牌上完全一样,**无法从错误里分辨是谁**。
|
|
813
|
+
|
|
814
|
+
必须按三段式处理,**不允许含糊报告「已完成」**:
|
|
815
|
+
|
|
816
|
+
1. **重读全文**(不是只读目标行)
|
|
817
|
+
2. **对比两类差异**:
|
|
818
|
+
- 「计划内变更」——你本来要改的那处
|
|
819
|
+
- 「**计划外差异**」——外部同时引入的其他改动
|
|
820
|
+
3. 按结果报告:
|
|
821
|
+
|
|
822
|
+
| 情形 | 动作 |
|
|
823
|
+
|---|---|
|
|
824
|
+
| 目标已达成 + 无计划外差异 | 报告「状态已由外部达成,本次 edit 未生效」 |
|
|
825
|
+
| 目标已达成 + **有计划外差异** | 报告「目标已达成,但外部同时引入了 X、Y」**并逐条列出** |
|
|
826
|
+
| 目标未达成 | rebase 到最新内容重做,不得跳过 |
|
|
827
|
+
|
|
828
|
+
**禁止**:只看目标行是否达成、不检查整文件差异就报完成。状态漂移被隐藏,比改动失败更危险。
|
|
829
|
+
|
|
830
|
+
> **边界声明**:这里的「重读全文 / 全量 diff」**只针对本文件**——目的是确认「这个文件的现状是否与我以为的一致」。它**不等于**扩大验证范围:不触发全量 build / test、不审查无关文件、不追查其他改动的来源(只报告发现了什么,不负责修)。与「验证按委派范围执行,不自行扩大为全量」不矛盾。
|
|
831
|
+
|
|
799
832
|
## 执行器环境契约(通用原则 + 探测式自适应)
|
|
800
833
|
|
|
801
834
|
### A. 通用原则(任何环境成立,强制)
|
|
@@ -1047,6 +1080,18 @@ Orchestrator 会在 prompt 中明确指定:
|
|
|
1047
1080
|
|
|
1048
1081
|
**心法**:不评估、不重述已知事实;只输出能直接派发的子任务与依赖。
|
|
1049
1082
|
|
|
1083
|
+
## 禁止再委派(硬约束)
|
|
1084
|
+
|
|
1085
|
+
- **禁止调用 \`delegate\` / \`delegate_batch\` / \`subagent\` / \`subagent_fork\` / \`task\` 类工具委派子代理**——包括「先让 explorer 帮我找文件」这种看似省事的用法。
|
|
1086
|
+
- 需要文件信息时**自己用 \`read\` / \`grep\` / \`glob\` 读**:定向读,带 \`offset\` / \`limit\`,单次 \`≤200\` 行;不要全文通读,不要为了「了解全貌」多次往返。
|
|
1087
|
+
- 理由:嵌套委派会让每个中间层各自重建完整上下文(系统提示 + 工具表 + skill brief)。实测一条 4 层链 \`main→planner→oracle→explorer→更深\` 的最深层跑 59s **只调了一次 skill 就结束**,是纯开销;机制上 \`delegate\` 在 \`depth ≥ 1\` 时会直接拒绝。
|
|
1088
|
+
|
|
1089
|
+
## 输出上限(硬约束)
|
|
1090
|
+
|
|
1091
|
+
- 方案正文 **≤1500 字符**(不是 token)。子任务清单 ≤6 条、每条一行,用最短的祈使句。
|
|
1092
|
+
- 超长内容**写入文件**(子任务清单 + 依赖 + 验证方式整体落盘,例如 \`docs/plan-<主题>.md\`),回复里只给**文件路径 + 3-5 行摘要**。
|
|
1093
|
+
- 理由:实测一次 co-planner 生成 10,076 output tokens 的方案**独占 265s**——输出越长,父会话等待越久,且全文永久驻留历史被每轮重发;机制上 co-planner 的结果回传被硬裁到 1500 字符(契约段优先保留)。
|
|
1094
|
+
|
|
1050
1095
|
**输出必须包含**:
|
|
1051
1096
|
- 子任务清单:**≤ 6 条,每条一行**;超出则合并同类项或直接建议「拆为多次交付」(不把多个任务压进一行)
|
|
1052
1097
|
- 每个子任务的委派对象(@explorer / @librarian / @fixer / @designer / @oracle / @observer / @council / @rule-user / @rule-project / @rule-app / @planner)
|
|
@@ -1476,6 +1521,16 @@ var DEFAULT_BATCH_TASK_CHARS = 2500;
|
|
|
1476
1521
|
var DEFAULT_BATCH_TOTAL_CHARS = 6000;
|
|
1477
1522
|
var DEFAULT_CONTEXT_WARN_TOKENS = 60000;
|
|
1478
1523
|
var MIN_BUDGET_CHARS = 200;
|
|
1524
|
+
var SKILL_RESULT_BUDGET_CHARS = {
|
|
1525
|
+
"co-planner": 1500
|
|
1526
|
+
};
|
|
1527
|
+
function resolveSkillResultMaxChars(budget, skillName) {
|
|
1528
|
+
const key = typeof skillName === "string" ? skillName.trim() : "";
|
|
1529
|
+
const tier = key.length > 0 ? SKILL_RESULT_BUDGET_CHARS[key] : undefined;
|
|
1530
|
+
if (tier === undefined)
|
|
1531
|
+
return budget.maxChars;
|
|
1532
|
+
return Math.max(MIN_BUDGET_CHARS, Math.min(budget.maxChars, tier));
|
|
1533
|
+
}
|
|
1479
1534
|
var XML_TAGS = ["summary", "results", "answer", "changes", "verification", "risk"];
|
|
1480
1535
|
var PRIMARY_XML_TAGS = ["results", "answer"];
|
|
1481
1536
|
var MD_TITLE_PREFIXES = ["结论", "摘要"];
|
|
@@ -2182,6 +2237,34 @@ async function buildAgentOptions(route, ctx, skillName) {
|
|
|
2182
2237
|
}
|
|
2183
2238
|
return options;
|
|
2184
2239
|
}
|
|
2240
|
+
var MAX_DELEGATION_DEPTH = 1;
|
|
2241
|
+
function resolveDelegationDepth(agent) {
|
|
2242
|
+
const header = agent?.session?.header;
|
|
2243
|
+
const candidates = [
|
|
2244
|
+
header?.delegationDepth,
|
|
2245
|
+
agent?.options?.subagentDepth,
|
|
2246
|
+
agent?.session?.delegationDepth
|
|
2247
|
+
];
|
|
2248
|
+
let depth;
|
|
2249
|
+
for (const value of candidates) {
|
|
2250
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0)
|
|
2251
|
+
continue;
|
|
2252
|
+
depth = depth === undefined ? value : Math.max(depth, value);
|
|
2253
|
+
}
|
|
2254
|
+
if (depth === undefined && header?.origin === "subagent")
|
|
2255
|
+
return MAX_DELEGATION_DEPTH;
|
|
2256
|
+
return depth;
|
|
2257
|
+
}
|
|
2258
|
+
function guardDelegationDepth(agent, logger) {
|
|
2259
|
+
const depth = resolveDelegationDepth(agent);
|
|
2260
|
+
if (depth === undefined) {
|
|
2261
|
+
logger?.warn?.("[cohub depth-guard] 无法读取当前 agent 的嵌套委派深度" + "(session.header.delegationDepth 缺失),本次放行、不做硬拦截");
|
|
2262
|
+
return;
|
|
2263
|
+
}
|
|
2264
|
+
if (depth < MAX_DELEGATION_DEPTH)
|
|
2265
|
+
return;
|
|
2266
|
+
return "拒绝:子代理不可再派子代理(当前嵌套 depth=" + depth + ",上限 " + MAX_DELEGATION_DEPTH + ")。请自己用 read/grep/glob 获取信息,或把需求回传主代理。";
|
|
2267
|
+
}
|
|
2185
2268
|
function resolveOwnerSessionId(exec) {
|
|
2186
2269
|
const id = exec?.agent?.session?.header?.id;
|
|
2187
2270
|
return typeof id === "string" && id.length > 0 ? id : undefined;
|
|
@@ -2383,6 +2466,11 @@ function createDelegateTool(ctx, getRoutes, config = {}, getSettings) {
|
|
|
2383
2466
|
const parent = exec?.agent;
|
|
2384
2467
|
if (!parent)
|
|
2385
2468
|
throw new Error("delegate requires a calling agent (exec.agent was undefined)");
|
|
2469
|
+
const depthRejection = guardDelegationDepth(parent, ctx.logger);
|
|
2470
|
+
if (depthRejection !== undefined) {
|
|
2471
|
+
ctx.logger?.warn?.("[cohub depth-guard] " + depthRejection + "(skill=" + String(args.skill) + ")");
|
|
2472
|
+
throw new Error(depthRejection);
|
|
2473
|
+
}
|
|
2386
2474
|
const { retry, stall, schedule, budget, background } = resolveRuntimeConfig();
|
|
2387
2475
|
const RETRYABLE = new Set(retry.retryableReasons);
|
|
2388
2476
|
ctx.logger?.info?.("[cohub schedule] 生效调度参数:batch=" + (schedule?.maxParallelBatch ?? 3) + " wallMs=" + (schedule?.wallClockBudgetMs ?? WALL_CLOCK_BUDGET_MS));
|
|
@@ -2509,7 +2597,7 @@ function createDelegateTool(ctx, getRoutes, config = {}, getSettings) {
|
|
|
2509
2597
|
}
|
|
2510
2598
|
if (outcome?.stopReason === "completed") {
|
|
2511
2599
|
return await budgetSubagentResult(ctx, exec, contentText(outcome.output), {
|
|
2512
|
-
maxChars: budget.
|
|
2600
|
+
maxChars: resolveSkillResultMaxChars(budget, skill.name),
|
|
2513
2601
|
scope: "delegate",
|
|
2514
2602
|
label: args.skill,
|
|
2515
2603
|
enabled: budget.enabled
|
|
@@ -2611,7 +2699,7 @@ function createDelegateTool(ctx, getRoutes, config = {}, getSettings) {
|
|
|
2611
2699
|
}
|
|
2612
2700
|
if (jobId !== undefined) {
|
|
2613
2701
|
ctx.logger?.info?.("[cohub background] delegate 已注册为后台 job:" + jobId + "(skill=" + skill.name + ")");
|
|
2614
|
-
return "已在后台启动:" + skill.name + "(job: " + jobId + ")。" + '取结果:job_output({ job_id: "' + jobId + '", wait: true });' + "或等自动唤醒(job 完成且你空闲时会自动开新一轮,最多连续 3 次)。" + "
|
|
2702
|
+
return "已在后台启动:" + skill.name + "(job: " + jobId + ")。" + '取结果:job_output({ job_id: "' + jobId + '", wait: true });' + "或等自动唤醒(job 完成且你空闲时会自动开新一轮,最多连续 3 次)。" + "等待期间可以继续派发别的任务或推进其他工作(后台任务不计入当前批次耗时)。";
|
|
2615
2703
|
}
|
|
2616
2704
|
if (bgFallbackReason === undefined) {
|
|
2617
2705
|
bgFallbackReason = "jobs.start 未返回 jobId";
|
|
@@ -2900,6 +2988,11 @@ function createDelegateBatchTool(ctx, getRoutes, config = {}, getSettings) {
|
|
|
2900
2988
|
const parent = exec?.agent;
|
|
2901
2989
|
if (!parent)
|
|
2902
2990
|
throw new Error("delegate_batch requires a calling agent (exec.agent was undefined)");
|
|
2991
|
+
const depthRejection = guardDelegationDepth(parent, ctx.logger);
|
|
2992
|
+
if (depthRejection !== undefined) {
|
|
2993
|
+
ctx.logger?.warn?.("[cohub depth-guard] delegate_batch:" + depthRejection);
|
|
2994
|
+
throw new Error(depthRejection);
|
|
2995
|
+
}
|
|
2903
2996
|
const { wallClockBudgetMs, maxParallelBatch, budget, toolFilterEnabled } = resolveRuntimeConfig();
|
|
2904
2997
|
const tasks = args.tasks ?? [];
|
|
2905
2998
|
if (tasks.length === 0) {
|
|
@@ -2908,6 +3001,7 @@ function createDelegateBatchTool(ctx, getRoutes, config = {}, getSettings) {
|
|
|
2908
3001
|
if (tasks.length > maxParallelBatch) {
|
|
2909
3002
|
return `delegate_batch: 任务数 ${tasks.length} 超过上限 ${maxParallelBatch},请分批执行。`;
|
|
2910
3003
|
}
|
|
3004
|
+
const batchStartedAt = Date.now();
|
|
2911
3005
|
const contractText = envContract.enabled ? resolveContractText(envContract, envSig, ctx).text : "";
|
|
2912
3006
|
const starts = await Promise.allSettled(tasks.map(async (task, index) => {
|
|
2913
3007
|
const startedAt = Date.now();
|
|
@@ -2997,7 +3091,7 @@ function createDelegateBatchTool(ctx, getRoutes, config = {}, getSettings) {
|
|
|
2997
3091
|
]);
|
|
2998
3092
|
if (outcome?.stopReason === "completed") {
|
|
2999
3093
|
const resultText = await budgetSubagentResult(ctx, exec, contentText(outcome.output), {
|
|
3000
|
-
maxChars: budget.batchTaskMaxChars,
|
|
3094
|
+
maxChars: Math.min(budget.batchTaskMaxChars, resolveSkillResultMaxChars(budget, task.skill)),
|
|
3001
3095
|
scope: "batch:task",
|
|
3002
3096
|
label: taskId + ":" + task.skill,
|
|
3003
3097
|
enabled: budget.enabled,
|
|
@@ -3046,8 +3140,12 @@ function createDelegateBatchTool(ctx, getRoutes, config = {}, getSettings) {
|
|
|
3046
3140
|
} catch {}
|
|
3047
3141
|
}
|
|
3048
3142
|
}
|
|
3143
|
+
const batchElapsedMs = Date.now() - batchStartedAt;
|
|
3049
3144
|
const completed = results.filter((r) => r.status === "completed");
|
|
3050
3145
|
const failed = results.filter((r) => r.status !== "completed");
|
|
3146
|
+
const statsLine = `并发 ${tasks.length} 个子代理 · ${completed.length}/${tasks.length} 完成 · 总耗时 ${(batchElapsedMs / 1000).toFixed(1)}s
|
|
3147
|
+
|
|
3148
|
+
`;
|
|
3051
3149
|
let output = `--- delegate_batch 结果 ---
|
|
3052
3150
|
`;
|
|
3053
3151
|
output += `总计: ${results.length} 个任务 | 完成: ${completed.length} | 失败: ${failed.length}
|
|
@@ -3096,7 +3194,7 @@ ${r.result}
|
|
|
3096
3194
|
}).text;
|
|
3097
3195
|
ctx?.logger?.warn?.("[cohub budget] batch:total:拼装结果 " + fullChars + " 字符 ≈ " + approxTokens(fullChars) + " tokens 超过总预算 " + budget.batchTotalMaxChars + " 字符,已裁剪;各任务全文见各自的 locator。仅告警,不阻断。");
|
|
3098
3196
|
}
|
|
3099
|
-
return output;
|
|
3197
|
+
return statsLine + output;
|
|
3100
3198
|
}
|
|
3101
3199
|
});
|
|
3102
3200
|
}
|
package/package.json
CHANGED